Immutable Matrices
The standard Matrix class in Diofant is mutable. This is important for
performance reasons but means that standard matrices cannot interact well with
the rest of Diofant. This is because the Basic
object, from which most
Diofant classes inherit, is immutable.
The mission of the ImmutableMatrix
class is to bridge the tension
between performance/mutability and safety/immutability. Immutable matrices can
do almost everything that normal matrices can do but they inherit from
Basic
and can thus interact more naturally with the rest of Diofant.
ImmutableMatrix
also inherits from MatrixExpr
, allowing it to
interact freely with Diofant’s Matrix Expression module.
You can turn any Matrix-like object into an ImmutableMatrix
by calling
the constructor
>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> M[1, 1] = 0
>>> IM = ImmutableMatrix(M)
>>> IM
Matrix([
[1, 2, 3],
[4, 0, 6],
[7, 8, 9]])
>>> IM[1, 1] = 5
Traceback (most recent call last):
...
TypeError: Can not set values in Immutable Matrix. Use Matrix instead.
ImmutableMatrix Class Reference
- class diofant.matrices.immutable.ImmutableMatrix(*args, **kwargs)[source]
Create an immutable version of a matrix.
Examples
>>> ImmutableMatrix(eye(3)) Matrix([ [1, 0, 0], [0, 1, 0], [0, 0, 1]]) >>> _[0, 0] = 42 Traceback (most recent call last): ... TypeError: Cannot set values of ImmutableDenseMatrix
- property C
By-element conjugation.
- as_mutable()[source]
Returns a mutable version of this matrix
Examples
>>> X = ImmutableMatrix([[1, 2], [3, 4]]) >>> Y = X.as_mutable() >>> Y[1, 1] = 5 # Can set values in Y >>> Y Matrix([ [1, 2], [3, 5]])
- diff(*args)[source]
Calculate the derivative of each element in the matrix.
Examples
>>> M = Matrix([[x, y], [1, 0]]) >>> M.diff(x) Matrix([ [1, 0], [0, 0]])
- equals(other, failing_expression=False)[source]
Applies
equals
to corresponding elements of the matrices, trying to prove that the elements are equivalent, returning True if they are, False if any pair is not, and None (or the first failing expression if failing_expression is True) if it cannot be decided if the expressions are equivalent or not. This is, in general, an expensive operation.Examples
>>> A = Matrix([x*(x - 1), 0]) >>> B = Matrix([x**2 - x, 0]) >>> A == B False >>> A.simplify() == B.simplify() True >>> A.equals(B) True >>> A.equals(2) False
See also