numpy
numpy
Numpy
We can use linear algebra in Python programs using the numpy package:
import numpy
For example:
prints:
[[1 2]
[3 4]]
Transpose (row) vectors can be defined from simple lists of numbers. For example:
prints:
[[1 2]]
6.2 Transposition
Matrices can be transposed by accessing a .T member:
hmatrix-expri → hmatrix-expri .T
For example:
prints:
[[1 3]
[2 4]]
1
6.3 Matrix operations
Matrices can be added/subtracted/multiplied/divided using the usual operators:
hmatrix-expri → hmatrix/num-expri + hmatrix/num-expri
hmatrix-expri → hmatrix/num-expri - hmatrix/num-expri
hmatrix-expri → hmatrix/num-expri * hmatrix/num-expri
hmatrix-expri → hmatrix/num-expri / hmatrix/num-expri
For example:
print ( numpy.matrix( [[1,2],[3,4]] ) * numpy.matrix( [[1,2],[3,4]] ) )
prints:
[[ 7 10]
[15 22]]
6.4 Normalization
Matrices can be normalized:
hnum-expri → numpy.linalg.norm ( hmatrix-expri, hnum-expri )
For example:
v = numpy.matrix( [1,1] ).T
print ( numpy.linalg.norm(v,2) )
prints:
1.41421356237
6.5 Diagonalization
Matrices can be diagonalized:
hmatrix-expri → numpy.diagflat ( hmatrix-expri )
For example:
v = numpy.matrix( [1,1] ).T
print ( numpy.diagflat(v) )
prints:
[[1 0]
[0 1]]