The following table lists the arithmetic operations defined for dense and sparse matrices. In the table A and B are dense or sparse matrices of compatible dimensions, c is a scalar (a Python number or a dense 1 by 1 matrix), D is a dense matrix, and e 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 | D % c |
Elementwise exponentiation | D**e |
If one of the operands 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’. (An exception to this rule is elementwise exponentiation: the result of D**e is a real matrix if D and e are integer.)
Addition, subtraction, and matrix multiplication with two matrix operands result in a sparse matrix if both matrices are sparse, and in a dense matrix otherwise. The result of a scalar multiplication or division is dense if A is dense, and sparse if A is sparse. 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 in the expressions A + c, c + A, A - c, c - A is a number, then it is interpreted as a dense 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 dense matrix and A is not 1 by 1, then c is interpreted as a dense matrix with the same size of A and all entries equal to c[0].
If c is a 1 by 1 dense matrix, then, if possible, the products c * A and A * c are interpreted as matrix-matrix products. If the product cannot be interpreted as a matrix-matrix product (because the dimensions of A are incompatible), then the product is interpreted as the scalar multiplication with c[0]. 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].
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 (sparse or dense, integer, real, or complex) of the matrix A. These in-place operations do not return a new matrix but modify the existing object 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 typecode ’i’, then A += B is allowed if B has typecode ’i’. It is not allowed if B has typecode ’d’ or ’z’ because the addition A + B results in a ’d’ or ’z’ matrix of and therefore cannot be assigned to A without changing its type. As another example, if A is a sparse matrix, then ”A += 1.0” is not allowed because the operation ”A = A + 1.0” results in a dense matrix, so it 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 dense matrix, in which case A *= c is interpreted as the scalar product A *= c[0].)
In-place remainder is only defined for dense A.
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] |