overloading
Customizing Operators and Functions
Description
In various scenarios, you may find it necessary to modify the behavior of Nelson's operators and functions when they operate on objects or basic types.
This customization can be achieved by overloading the relevant functions, allowing them to handle diverse types and quantities of input arguments and execute the appropriate operation for the highest-priority object.
Overloading Operators:
Each built-in operator corresponds to a specific function name (e.g., the - operator is associated with the minus.m function).
You can overload any operator by creating an M-file with the appropriate name within the class directory.
For instance, if either A or B is an object of type classname, the expression A - B triggers a call to a function @classname/minus.m, provided it exists.
When A and B belong to different classes, Nelson employs precedence rules to determine which method to apply.
The table below provides a list of function names associated with most of the Nelson operators:
Binary addition
a + b
plus(a, b)
Binary subtraction
a - b
minus(a, b)
Unary minus
-a
uminus(a)
Unary plus
+a
uplus(a)
Element-wise multiplication
a .* b
times(a, b)
Matrix multiplication
a * b
mtimes(a, b)
Right element-wise division
a ./ b
rdivide(a, b)
Left element-wise division
a .\ b
ldivide(a, b)
Matrix right division
a / b
mrdivide(a, b)
Matrix left division
a \ b
mldivide(a, b)
Element-wise power
a .^ b
power(a, b)
Matrix power
a ^ b
mpower(a, b)
Less than
a < b
lt(a, b)
Greater than
a > b
gt(a, b)
Less than or equal to
a <= b
le(a, b)
Greater than or equal to
a >= b
ge(a, b)
Not equal to
a ~= b
ne(a, b)
Equality
a == b
eq(a, b)
Logical AND
a & b
and(a, b)
Logical OR
a | b
or(a, b)
Logical NOT
~a
not(a)
Colon operator
a:d:b
colon(a, d, b)
Complex conjugate transpose
a'
ctranspose(a)
Matrix transpose
a.'
transpose(a)
Display method
command window output
display(a)
Horizontal concatenation
[a, b]
horzcat(a, b, ...)
Vertical concatenation
[a; b]
vertcat(a, b, ...)
Subscripted reference
a(s1, s2, ... , sn)
subsref(a, s)
Subscripted assignment
a(s1, ... , sn) = b
subsasgn(a, s, b)
Subscript index
b(a)
subsindex(a)
Example
Overload minus operator with double
See also
plus, minus, uminus, uplus, times, mtimes, rdivide, ldivide, mrdivide, mldivide, power, mpower, lt, gt, le, ge, ne, eq, and, or, not, colon, ctranspose, transpose, display, horzcat, vertcat, subsref, subsasgn, subsindex.
History
1.0.0
initial version
Author
Allan CORNET
Last updated