How to override the operator. ^ In MATLAB?

How can I override the exponential function .^ In MATLAB? From:

 x.^y 

in

 sign(x).*abs(x.^y)) 
+3
source share
2 answers

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

Should I redefine the arithmetic operator in MATLAB? ... Eh, possibly not.

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

I answered several other related questions that relate to overloading arithmetic operators and shadow built-in behavior, which I definitely suggest reading first to understand the details, difficulties and errors associated with this approach:

And now, when I have finished with my disclaimer, I will give you a weapon with which you can shoot in the leg ...;)


The arithmetic operators in MATLAB have functional equivalents that are called backstage when called, which are listed here . The powerwise .^ Statement calls the called function power .

Now for each which , 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 will be of type double (since they are the default in MATLAB), then you will have to obscure the built-in function @double\power . You can do this by creating a directory (we'll call it temp ), create a subdirectory inside it called @double , and then place the following power submenu in this subdirectory:

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

Now, based on the priority order of the function , it follows that MATLAB adds the temp directory to the MATLAB path or if you just change the current working directory to temp , then instead of the built-in operator when calling the .^ For the double variables, the aforementioned user function power will be called.

+12
source

not to do. This will be wrong. (-1). ^ (1/2) should always give you the imaginary unit (i). The expression you have will give you (-1). ^ (1/2) → -1. Even worse, consider (-1) ^ 2.

Create a separate function to perform the operation you are describing. Sort of

 function a = myPowerFunc(x, y) a = sign(x).*abs(x.^y); 
+8
source

Source: https://habr.com/ru/post/905752/


All Articles