The following table lists the arithmetic operations defined for dense matrices. In this table A and B are dense matrices with compatible dimensions, c is a scalar (a Python number or a dense 1 by 1 matrix), and d is a Python number.
Unary plus/minus | +A, -A |
Addition | A+B, A+c, c+A |
Subtraction | A-B, A-c, c-A |
Matrix multiplication | A*B |
Scalar multiplication and division | c*A, A*c, A/c |
Remainder after division | A%c |
Elementwise exponentiation | A**d |
If c in the expressions A+c, c+A, A-c, c-A is a number, then it is interpreted as a matrix with the same dimensions as A, type given by the type of c, and all entries equal to c. If c is a 1 by 1 matrix and A is not 1 by 1, then c is interpreted as a matrix with the same size of A and all entries equal to c[0].
Postmultiplying a matrix with a number c means the same as premultiplying, i.e., scalar multiplication. Dividing a matrix by c means dividing all entries by c. If c is a 1 by 1 matrix and the product c*A or A*c cannot be interpreted as a matrix-matrix product, then it is interpreted as c[0]*A. The division A/c and remainder A%c with c a 1 by 1 matrix are always interpreted as A/c[0], resp., A%c[0].
If one of the operands in the arithmetic operations is integer (a scalar integer or a matrix of type ’i’) and the other operand is double (a scalar float or a matrix of type ’d’), then the integer operand is converted to double, and the result is a matrix of type ’d’. If one of the operands is integer or double, and the other operand is complex (a scalar complex or a matrix of type ’z’), then the first operand is converted to complex, and the result is a matrix of type ’z’.
The result of A**d is a complex matrix if A or d are complex, and real otherwise.
Note that Python rounds the result of an integer division towards minus infinity.
The following in-place operations are also defined, but only if they do not change the type of the matrix A:
In-place addition | A+=B, A+=c |
In-place subtraction | A-=B, A-=c |
In-place scalar multiplication and division | A*=c, A/=c |
In-place remainder | A%=c |
For example, if A has type ’i’, then A+=B is allowed if B has type ’i’. It is not allowed if B has type ’d’or ’z’because the addition A+B results in a matrix of type ’d’or ’z’and therefore cannot be assigned to A without changing its type.
In-place matrix-matrix products are not allowed. (Except when c is a 1 by 1 matrix, in which case A*=c is interpreted as the scalar product A*=c[0].)
It is important to know when a matrix operation creates a new object. The following rules apply.
>>> B = matrix([[1.,2.], [3.,4.]])
>>> print B [ 1.00e+00 3.00e+00] [ 2.00e+00 4.00e+00] >>> A = B >>> A[0,0] = -1 >>> print B # modifying A[0,0] also modified B[0,0] [-1.00e+00 3.00e+00] [ 2.00e+00 4.00e+00] |
>>> B = matrix([[1.,2.], [3.,4.]])
>>> A = +B >>> A[0,0] = -1 >>> print B # modifying A[0,0] does not modify B[0,0] [ 1.00e+00 3.00e+00] [ 2.00e+00 4.00e+00] |
>>> B = matrix([[1.,2.], [3.,4.]])
>>> A = B >>> A *= 2 >>> print B # in-place operation also changed B [ 2.00e+00 6.00e+00] [ 4.00e+00 8.00e+00] >>> A = 2*A >>> print B # regular operation creates a new A, so does not change B [ 2.00e+00 6.00e+00] [ 4.00e+00 8.00e+00] |