在 matlab 中重载类的每个函数运算符

发布于 2024-10-27 05:52:50 字数 1191 浏览 0 评论 0原文

我正在创建一个类,出于所有实际目的(尽管它比看起来要复杂得多),可以将其视为 matlab 实数。

在类中,我可以重载大量的matlab运算符,例如plus、mpower等。

这可能是不可能的,但是我将如何重载类中的任何函数呢? 换句话说,假设我有一个任意函数 f,它接受实数并输出实数,并说 X 是我的类的实例。我希望 matlab 能够正确解释 f(X) (当然,我有一种自然的方式来获取函数指针并将其应用到我的类中,我将在代码中执行此操作)。

我所看到的问题是:matlab 可能无法看到函数 f 将实数作为输入。但我会把这个留给用户不要搞乱他们的函数调用。

我说得有道理吗?

我不认为这是可能的,但如果是的话,那就太棒了。

ps:我知道我可能可以通过创建一个将函数句柄作为输入的方法来解决它,但它不太漂亮..

谢谢!

编辑:

抱歉,我意识到这有点令人困惑。我会更清楚。假设我有一个代表随机实数变量的类(为了简单起见,在离散集上)。我的类包含随机变量的概率分布及其可能的值。

对于任意两个随机变量 X,Y,总和 X+Y 是明确定义的,因此,如果我有代表随机变量的实例 X 和 Y,那么如果 Z=X+Y 定义一个等于总和的新随机变量,那就太好了X 和 Y 的值,具有适当的设置和分布。我已经通过重载加号运算符做到了这一点。很好。

假设我有一个任意函数 f,比如“cos”。好吧,对于任何随机变量 X,cos(X) 也是一个随机变量,如果我可以写 Z=cos(X) 那就太好了,它会自动创建我的类的实例,计算适当的域并概率分布。

问题是我希望任何函数 f 都发生这种自动操作 - 我不想手动重载每个常用函数(特别是因为我希望该技巧可以与用户定义的函数 f 一起使用)。

再举一个例子: 我创建一个随机变量 X,其域为 [-2,-1,0,1,2],概率为 [1/5,1/5,1/5,1/5, 1/5]

我创建了一个(奇怪的)函数 f 使得 f(x) = x 如果 x=-2 或 2 f(x) = x^2 否则

然后,通过设置 Z=f(X),我希望 matlab 自动创建一个域为 {-2,0,1,2} 的随机变量 Z 和概率 [1/5, 1/5, 2/5, 1/5]

数学上,我知道如何做到这一点。但我需要拦截和重载我的类的任何函数调用。

这有什么意义吗?

ps:我没有接受过面向对象编程的正式培训,所以有时我可能会用错词来表达某个概念。

再次感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘你却要生生世世 2024-11-03 05:52:50

是的,您可以重载运算符。然而,对于你想要做的事情来说,这是矫枉过正了。您所需要的只是使用 isreal 函数进行简单的输入检查。

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

如果您输入一个复数作为此函数的输入,它将显示错误,否则不会。现在您可以围绕此构建您的函数,以便仅在满足条件时才执行所有代码(因此,在上面的 end 语句之后继续您的函数)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文