Linear mappings: Linear mappings in Python
Kernel and image
Ready-to-use methods for the calculation of the kernel and the image of a matrix are only provided by the Symbolic Python (sympy
) package.
>>> import sympy as sy for pretty printingvoor iets mooiere uitvoer
>>> A = sy.Matrix([[1, 0, 1], [2, 1, 1], [-1, 1, -2]]); A
⎡1 0 1 ⎤
⎢ ⎥
⎢2 1 1 ⎥
⎢ ⎥
⎣-1 1 -2⎦
>>> A.nullspace() # spanning vectors of the kernel of A
⎡⎡-1⎤⎤
⎢⎢ ⎥⎥
⎢⎢1 ⎥⎥
⎢⎢ ⎥⎥
⎣⎣1 ⎦⎦
>>> A.rank() # rank of A
2
>>> A.columnspace() # spanning vectors of the image of A
⎡⎡1 ⎤ ⎡0⎤⎤
⎢⎢ ⎥ ⎢ ⎥⎥
⎢⎢2 ⎥, ⎢1⎥⎥
⎢⎢ ⎥ ⎢ ⎥⎥
⎣⎣-1⎦ ⎣1⎦⎦
The Symbolic Python (scipy
) package has a linear algebra module containing similar features as MATLAB. For numerical calculations, this approach is preferred.
>>> import scipy.linalg as la
>>> A = sp.array(A); print(A)
[[1 0 1]
[2 1 1]
[-1 1 -2]]
>>> la.null_space(A) # spanning vectors of the kernel of A
[[-0.57735027]
[ 0.57735027]
[ 0.57735027]]
>>> from numpy.linalg import matrix_rank
>>> matrix_rank(A) # rank of A
2
>>> la.orth(A) # spanning vectors of the image of A
[[-4.26401433e-01 -5.60016277e-18]
[-6.39602149e-01 -7.07106781e-01]
[ 6.39602149e-01 -7.07106781e-01]]
Unlock full access