Using the C++ Math Library    

Using the Operators

Many of MATLAB's mathematical operators (+, -, *, /, ^) are the same as those available in C++. The exceptions are ', \, and the array operators .*, ./, .\, and .^, because the syntax of C++ does not support their definition as operators. You must use the functional equivalents provided by the MATLAB C++ Math Library to perform these operations.

This table demonstrates how the library supports mathematical operators. Note that the library also provides functional equivalents for the set of operators that are supported by C++ syntax.

Table 6-1: MATLAB Operator and C++ Function Equivalence 
Description
Definition:
C = A <op> B
MATLAB Operator
C++ Operator
C++ Function
Array multiplication
C[i] = A[i] * B[i]
.*
None
times()
Array right division
C[i] = A[i] / B[i]
./
None
rdivide()
Array left division
C[i] = B[i] / A[i]
.\
None
ldivide()
Array exponentiation
C[i] = A[i] ^ B[i]
.^
None
power()
Array addition
C[i] = A[i] + B[i]
+
+
plus()
Array subtraction
C[i] = A[i] - B[i]
-
-
minus()
Matrix multiplication
Inner product
*
*
mtimes()
Matrix right division
C such that C*B = A
/
/
mrdivide()
Matrix left division
C such that A*C = B
\
None
mldivide()
Matrix exponentiation
C = A*A*...*A
(B times)
^
^
mpower()
Complex transpose
N/A (unary)
'
None
ctranspose()
Transpose
N/A (unary)
.'
None
transpose()

With the exception of the unary transpose() and ctranspose() functions, the C++ functions in the table take two matrix arguments and return a third matrix. To see these functions in action, consider the C++ translation of the MATLAB code presented on page 6-2 at the beginning of this section. Function calls replace the use of operators. (Note that * can be used instead of mtimes.)

Running this code fragment produces:

Use the other binary operator functions in a similar manner.


 Using the Mathematical Operators Defining Your Own Operators