# Immutable Matrices¶

The standard Matrix class in SymPy is mutable. This is important for performance reasons but means that standard matrices cannot interact well with the rest of SymPy. This is because the Basic object, from which most SymPy classes inherit, is immutable.

The mission of the ImmutableDenseMatrix class, which is aliased as ImmutableMatrix for short, 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 SymPy. ImmutableMatrix also inherits from MatrixExpr, allowing it to interact freely with SymPy’s Matrix Expression module.

You can turn any Matrix-like object into an ImmutableMatrix by calling the constructor

>>> from sympy import Matrix, ImmutableMatrix
>>> 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¶

sympy.matrices.immutable.ImmutableMatrix[source]
class sympy.matrices.immutable.ImmutableDenseMatrix[source]

Create an immutable version of a matrix.

Examples

>>> from sympy import eye
>>> from sympy.matrices import ImmutableMatrix
>>> 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

is_diagonalizable(reals_only=False, **kwargs)[source]

Returns True if a matrix is diagonalizable.

Parameters

reals_only : bool, optional

If True, it tests whether the matrix can be diagonalized without complex numbers. (Orthogonally diagonalizable)

If False, it tests whether the matrix can be unitarily diagonalizable.

dotprodsimp : bool, optional

Specifies whether intermediate term algebraic simplification is used during matrix multiplications to control expression blowup and thus speed up calculation.

Examples

Example of a diagonalizable matrix:

>>> from sympy import Matrix
>>> M = Matrix([[1, 2, 0], [0, 3, 0], [2, -4, 2]])
>>> M.is_diagonalizable()
True


Example of a non-diagonalizable matrix:

>>> M = Matrix([[0, 1], [0, 0]])
>>> M.is_diagonalizable()
False


Example of a unitarily diagonalizable, but not orthogonally diagonalizable:

>>> M = Matrix([[0, 1], [-1, 0]])
>>> M.is_diagonalizable(reals_only=False)
True
>>> M.is_diagonalizable(reals_only=True)
False

property is_zero

Checks if a matrix is a zero matrix.

A matrix is zero if every element is zero. A matrix need not be square to be considered zero. The empty matrix is zero by the principle of vacuous truth. For a matrix that may or may not be zero (e.g. contains a symbol), this will be None

Examples

>>> from sympy import Matrix, zeros
>>> from sympy.abc import x
>>> a = Matrix([[0, 0], [0, 0]])
>>> b = zeros(3, 4)
>>> c = Matrix([[0, 1], [0, 0]])
>>> d = Matrix([])
>>> e = Matrix([[x, 0], [0, 0]])
>>> a.is_zero
True
>>> b.is_zero
True
>>> c.is_zero
False
>>> d.is_zero
True
>>> e.is_zero