Next: , Previous: , Up: Overloading Objects   [Contents][Index]


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.

OperationMethodDescription
a + bplus (a, b)Binary addition
a - bminus (a, b)Binary subtraction operator
+ auplus (a)Unary addition operator
- auminus (a)Unary subtraction operator
a .* btimes (a, b)Element-wise multiplication operator
a * bmtimes (a, b)Matrix multiplication operator
a ./ brdivide (a, b)Element-wise right division operator
a / bmrdivide (a, b)Matrix right division operator
a .\ bldivide (a, b)Element-wise left division operator
a \ bmldivide (a, b)Matrix left division operator
a .^ bpower (a, b)Element-wise power operator
a ^ bmpower (a, b)Matrix power operator
a < blt (a, b)Less than operator
a <= ble (a, b)Less than or equal to operator
a > bgt (a, b)Greater than operator
a >= bge (a, b)Greater than or equal to operator
a == beq (a, b)Equal to operator
a != bne (a, b)Not equal to operator
a & band (a, b)Logical and operator
a | bor (a, b)Logical or operator
! bnot (a)Logical not operator
a’ctranspose (a)Complex conjugate transpose operator
a.’transpose (a)Transpose operator
a : bcolon (a, b)Two element range operator
a : b : ccolon (a, b, c)Three element range operator
[a, b]horzcat (a, b)Horizontal concatenation operator
[a; b]vertcat (a, b)Vertical concatenation operator
a(s_1, …, s_n)subsref (a, s)Subscripted reference
a(s_1, …, s_n) = bsubsasgn (a, s, b)Subscripted assignment
b (a)subsindex (a)Convert to zero-based index
displaydisplay (a)Commandline display function

Table 34.1: Available overloaded operators and their corresponding class method

An example mtimes method for our polynomial class might look like

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


Next: , Previous: , Up: Overloading Objects   [Contents][Index]