2

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 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!

2
  • I'm not quite sure what it is you are trying to do, can you provided an example of the wanted behavior? Are you trying to create f(double d) and f(int i), that's how I read it? Commented Mar 27, 2011 at 20:39
  • sorry, i was not very clear. i added some clarifications Commented Mar 27, 2011 at 21:27

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

7 Comments

I might be confused, but my problem is that I want to overload all functions. I realize this might sound crazy - see above for what I am trying to do..
I understand what you're trying to do, however I'm not certain that's the correct approach for what you need. You're trying to get the range of your functions, given the domain. Given a domain D={d1,d2,d3} and a function f(x), the range is simply R=f(D). Now, as in your example, you'll get duplicate values, which can be weeded out using the function unique. To get the probability of each of those unique elements, see my answer to a similar question.
Thanks for your answers. I have remaining two problems : a) my class is significantly more complex than just containing the domain and the probabilities (which I do not want to compute using statistical frequencies, because it would be too slow). b) The class interfaces to a bunch of other stuff, and for user-friendliness purposes, being able to write Z=f(X) is significantly better than creating Z from scratch and filling it with the all the appropriate stuff. The syntax would become ugly, and it would remove the point of what I am trying to do - i.e. a nice interface to handle random vars.
I should add that your approach is exactly how I would compute the new domain (though I want to be able to generalize to functions of multiple inputs, in which case I need to sweep over the domain of all random variables, but I can do that nicely with a setprod). But as I said, there is quite a bit more stuff under the belly of the class.
You should take a look at the answer to a similar question on overloading. That would be a good starting point. Once you've covered all the arithmetic operators, most of the functions and user supplied functions will fall in place, as most functions are built upon basic arithmetic. The exception might be functions that reject inputs not in a particular class.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.