How to redefine the .^ operator in MATLAB?

▼魔方 西西 提交于 2019-12-06 06:55:30

问题


How can I redefine the exponential function .^ in MATLAB? From:

x.^y

to:

sign(x).*abs(x.^y))

回答1:


Can you redefine an arithmetic operator in MATLAB?... Yes

Should you redefine an arithmetic operator in MATLAB?... Eh, probably not.

Why? Because every other function in MATLAB expects that the arithmetic operator will behave as defined by the built-in implementation.

I've answered a few other related questions that have dealt with overloading arithmetic operators and shadowing built-in behavior, which I definitely suggest reading through first to understand the details, difficulties, and pitfalls involved in such an approach:

  • MATLAB: Is it possible to overload operators on native constructs (cells, structs, etc)?
  • How do I get real integer overflows in MATLAB/Octave?

And now that I'm done with my disclaimer, I'll hand you the gun with which to potentially shoot yourself in the foot... ;)


Arithmetic operators in MATLAB have functional equivalents that are called behind the scenes when you invoke them, which are listed here. The arraywise power operator .^ calls the built-in power function when invoked.

Now, there will be a separate power function defined for each data type that uses it. This function will be placed in an @type directory, which you can see by using the which function to look at the different power functions that exist:

>> which power -all
built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@single\power)  % single method
built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@double\power)  % double method
built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@char\power)    % char method
built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@int64\power)   % int64 method
built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@int32\power)   % int32 method
built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@int16\power)   % int16 method
built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@int8\power)    % int8 method
built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@uint64\power)  % uint64 method
built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@uint32\power)  % uint32 method
built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@uint16\power)  % uint16 method
built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\ops\@uint8\power)   % uint8 method

If your variables x and y are going to be of type double (as they are by default in MATLAB), then you will have to shadow the built-in @double\power function. You can do this by creating a directory (we'll call it temp), creating a subdirectory within that called @double, and then placing the following custom power function in that subdirectory:

function result = power(x, y)
  result = sign(x).*abs(builtin('power', x, y));
end

Now, based on the function precedence order MATLAB follows, if you add the directory temp to the MATLAB path, or if you simply change the current working directory to temp, then the above custom power function will be called instead of the built-in one when using the .^ operator on double variables.




回答2:


Don't. It won't be correct. (-1).^(1/2) should always give you the imaginary unit (i). The expression you have would give you (-1).^(1/2) -> -1. Even worse, consider (-1)^2.

Create a separate function to perform the operation you're describing. Something like

function a = myPowerFunc(x, y)

a = sign(x).*abs(x.^y);


来源:https://stackoverflow.com/questions/7577802/how-to-redefine-the-operator-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!