34.4.2 Operator Overloading

The following table shows, for each built-in numerical operation, the corresponding function name to use when providing an overloaded method for a user class.

Operation Method Description
a + b plus (a, b) Binary addition
a - b minus (a, b) Binary subtraction
+a uplus (a) Unary addition
-a uminus (a) Unary subtraction
a .* b times (a, b) Element-wise multiplication
a * b mtimes (a, b) Matrix multiplication
a ./ b rdivide (a, b) Element-wise right division
a / b mrdivide (a, b) Matrix right division
a .\ b ldivide (a, b) Element-wise left division
a \ b mldivide (a, b) Matrix left division
a .^ b power (a, b) Element-wise power
a ^ b mpower (a, b) Matrix power
a < b lt (a, b) Less than
a <= b le (a, b) Less than or equal to
a > b gt (a, b) Greater than
a >= b ge (a, b) Greater than or equal to
a == b eq (a, b) Equal to
a != b ne (a, b) Not equal to
a & b and (a, b) Logical and
a | b or (a, b) Logical or
!a not (a) Logical not
a' ctranspose (a) Complex conjugate transpose
a.' transpose (a) Transpose
a:b colon (a, b) Two element range
a:b:c colon (a, b, c) Three element range
[a, b] horzcat (a, b) Horizontal concatenation
[a; b] vertcat (a, b) Vertical concatenation
a(s_1,…,s_n) subsref (a, s) Subscripted reference
a(s_1,…,s_n) = b subsasgn (a, s, b) Subscripted assignment
b(a) subsindex (a) Convert object to index
disp disp (a) Object display

Table 34.1: Available overloaded operators and their corresponding class method

An example mtimes method for the polynomial class might look like

function p = mtimes (a, b)
  p = polynomial (conv (double (a), double (b)));
endfunction

© 1996–2020 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/v6.3.0/Operator-Overloading.html