Linear Algebra

Simple Array Operations

>>> import numpy as np
>>> a = np.array([[1.0, 2.0], [3.0, 4.0]])
>>> print(a)
[[ 1.  2.]
 [ 3.  4.]]

>>> a.transpose()
array([[ 1.,  3.],
       [ 2.,  4.]])

>>> np.linalg.inv(a)
array([[-2. ,  1. ],
       [ 1.5, -0.5]])

>>> u = np.eye(2) # unit 2x2 matrix; "eye" represents "I"
>>> u
array([[ 1.,  0.],
       [ 0.,  1.]])
>>> j = np.array([[0.0, -1.0], [1.0, 0.0]])

>>> np.dot (j, j) # matrix product
array([[-1.,  0.],
       [ 0., -1.]])

>>> np.trace(u)  # trace
2.0

>>> y = np.array([[5.], [7.]])
>>> np.linalg.solve(a, y)
array([[-3.],
       [ 4.]])

>>> np.linalg.eig(j)
(array([ 0.+1.j,  0.-1.j]), array([[ 0.70710678+0.j        ,  0.70710678-0.j        ],
       [ 0.00000000-0.70710678j,  0.00000000+0.70710678j]]))
>>> let a = fromLists [[1.0, 2.0],[3.0,4.0]] :: Matrix R
>>> disp 2 a
2x2
1  2
3  4

>>> tr a
(2><2)
 [ 1.0, 3.0
 , 2.0, 4.0 ]

>>> disp 2 (inv a)
2x2
-2.00   1.00
 1.50  -0.50

>>> let u = ident 2 :: Matrix R
>>> u
(2><2)
 [ 1.0, 0.0
 , 0.0, 1.0 ]
>>> let j = fromLists [[0.0,-1.0],[1.0,0.0]] :: Matrix R

>>> j <> j
(2><2)
 [ -1.0,  0.0
 ,  0.0, -1.0 ]

>>> sumElements (takeDiag u)
2.0

>>> let y = vector [5,7]
>>> disp 2 (asColumn (a <\> y))
2x1
-3.00
 4.00

>>> eig j
([0.0 :+ 1.0,0.0 :+ (-1.0)],(2><2)
 [    0.7071067811865475 :+ 0.0, 0.7071067811865475 :+ (-0.0)
 , 0.0 :+ (-0.7071067811865475),    0.0 :+ 0.7071067811865475 ])