开发者

overloading every function operator for a class in matlab

I am creating a class which, for all practical purposes (though it's quite more complex than what it looks like), can be thought of as a matlab real number.

In the class, I can overload a large number of matlab operators, such as plus, mpower, etc..

It's probably impossible, but how would I go about overloading any function of my class? In other words, presume I have an arbitrary function f which takes real numbers and outputs real numbers, and say X is an instance of my class. I would like f(X) to be interpreted correctly by matlab (of course, I have a natural way of taking a function pointer and applying it to my class, which I would do in the code).

Issues as I can see them: matlab may have no way of seeing that a function f takes real number as inputs. But I would leave that to the user not to mess up their function calls.

Am I making any sense?

I don't think it's possible, but if it was, it would be awesome.

ps: I am aware I could probably get around it by creating a method which takes a funciton handle as input, but it's less pretty..

Thanks!

edit:

Sorry, I realize this is a bit confusing. I'll be more clear. Let's say I have a class which represents random, real variables (say over a discrete set for simplicity). My class contains the probability distribution of the random variable, as well as its possible values.

For any two random variables X,Y, the sum X+Y is well defined, so if i have instances X and Y which represent random variables, it would be nice if Z=X+Y defines a new random variable equal to the sum of X and Y, with the proper set and distribution. I have done that, by overloading the plus operator. It's nice.

Say that I have an arbitrary function f, say "cos". Well, for any random variable X, cos(X) is also a random variable, and it would be nice if I could just write Z=cos(X), which would automatically create an instance of my class, compute the appropriate domain and probability distribution.

the issue is that I would like this automatic operation to happen for any function f - i don't want to manually overload every commonly used function (especially since I want the trick to work with user defined functions f).

To give a further example: I create开发者_JAVA技巧 a random variable X, with domain [-2,-1,0,1,2], and probabilities [1/5,1/5,1/5,1/5, 1/5]

I create a (weird) function f such that f(x) = x if x=-2 or 2 f(x) = x^2 otherwise

Then, by setting Z=f(X), i want matlab to automatically create a random variable Z with domain {-2,0,1,2} and probabilities [1/5, 1/5, 2/5, 1/5]

mathematically, I know how to do this. But i need to intercept and overload any function call of my class.

does that make any sense?

ps: I am not formally trained in object-oriented programming, so I may use the wrong word for a concept sometimes.

Again, thanks for any help!


Yes, you can overload operators. However, for what you're trying to do, that is overkill. All you need is a simple input check with the isreal function.

function rejectComplex(inputValue)
    if ~isreal(inputValue)
        error('Input is not a real number')
    end

If you enter a complex number as input to this function, it will display the error, else it won't. Now you can build your function around this, so that all code is executed only when the condition is satisfied (so, continue with your function after the end statement above)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜