Breaking News

# NumPy Linear Algebra

12 – Linear Algebra
In [2]:
import numpy as np

In [3]:
arr1 = np.random.rand(5,5)
arr2 = np.random.rand(5,5)


### matrix multiplication¶

In [4]:
print(arr1.dot(arr2))
# or
print(np.dot(arr1, arr2))
# or
print(arr1 @ arr2)

[[1.46213797 0.92819395 1.86768291 1.71630055 1.03009096]
[0.70493158 0.59210137 0.7917084  0.67533243 0.50914254]
[0.99434061 0.95996127 1.14626579 1.26914393 1.12927532]
[1.30237643 1.02029515 1.88921114 1.612508   1.02978894]
[1.10846544 0.65390134 1.6219209  1.36465083 0.688622  ]]
[[1.46213797 0.92819395 1.86768291 1.71630055 1.03009096]
[0.70493158 0.59210137 0.7917084  0.67533243 0.50914254]
[0.99434061 0.95996127 1.14626579 1.26914393 1.12927532]
[1.30237643 1.02029515 1.88921114 1.612508   1.02978894]
[1.10846544 0.65390134 1.6219209  1.36465083 0.688622  ]]
[[1.46213797 0.92819395 1.86768291 1.71630055 1.03009096]
[0.70493158 0.59210137 0.7917084  0.67533243 0.50914254]
[0.99434061 0.95996127 1.14626579 1.26914393 1.12927532]
[1.30237643 1.02029515 1.88921114 1.612508   1.02978894]
[1.10846544 0.65390134 1.6219209  1.36465083 0.688622  ]]


### QR factorization¶

In [5]:
arr = np.random.rand(5,5)
q, r = np.linalg.qr(arr)
print(q)
print(r)

[[-0.60523488 -0.25992456 -0.49563753  0.55892744 -0.08985274]
[-0.51514018 -0.20064846  0.73577013 -0.06079412 -0.38641582]
[-0.2653282  -0.47801828 -0.31254274 -0.76628415  0.12737781]
[-0.53010308  0.78470728 -0.0739863  -0.24310399  0.19660032]
[-0.13005001 -0.21888907  0.33140431  0.19394134  0.88754296]]
[[-1.54645585 -0.85634057 -0.97217391 -1.41743166 -0.64518047]
[ 0.         -0.45667058  0.07052167  0.14336972 -0.4918361 ]
[ 0.          0.         -0.29455796  0.0117106  -0.08615353]
[ 0.          0.          0.         -0.36969503  0.30130687]
[ 0.          0.          0.          0.          0.87003333]]


### singular value decomposition (SVD)¶

In [6]:
arr = np.random.rand(5,5)
u, s, v = np.linalg.svd(arr)
print(u)
print(s)
print(v)

[[-0.37219915  0.76691588  0.07164157 -0.10206272 -0.50769923]
[-0.48226154 -0.6347452   0.12712331  0.01356591 -0.59006608]
[-0.68434347  0.05696186 -0.28705408  0.49567141  0.44759264]
[-0.34344923 -0.07010292 -0.23531328 -0.8603297   0.28563731]
[-0.20641765  0.02791945  0.91702495 -0.05951323  0.33486665]]
[2.34112546 1.04843584 0.5165154  0.35360331 0.22722986]
[[-0.47310184 -0.42833436 -0.43483098 -0.21324734 -0.59845795]
[ 0.4722307  -0.5064642   0.39852432  0.40490165 -0.44466302]
[-0.27198688  0.17130276 -0.33583789  0.88512668  0.02102811]
[ 0.51227768  0.57210692 -0.37153367 -0.08203426 -0.51526533]
[ 0.465583   -0.45096866 -0.63346352 -0.02003411  0.4221165 ]]


### compute eigen values¶

In [7]:
arr = np.random.rand(5,5)
print(np.linalg.eigvals(arr))

[ 2.53685773+0.j         -0.84969131+0.j          0.60140347+0.29420706j
0.60140347-0.29420706j  0.03969261+0.j        ]


### eigen value decomposition¶

In [8]:
arr = np.random.rand(5,5)
w, v = np.linalg.eig(arr)
print(w)    # eigen values
print(v)    # eigen vectors

[ 2.37777714+0.j          0.08487316+0.37269284j  0.08487316-0.37269284j
-0.42509077+0.34513099j -0.42509077-0.34513099j]
[[-0.60881889+0.j         -0.09599198-0.42981173j -0.09599198+0.42981173j
0.10239962+0.00531346j  0.10239962-0.00531346j]
[-0.31954993+0.j         -0.10059891+0.02033935j -0.10059891-0.02033935j
-0.58254482+0.j         -0.58254482-0.j        ]
[-0.51048775+0.j          0.63283506+0.j          0.63283506-0.j
0.07786686+0.35304311j  0.07786686-0.35304311j]
[-0.31263787+0.j          0.11077389+0.40097634j  0.11077389-0.40097634j
0.19468118-0.37914697j  0.19468118+0.37914697j]
[-0.41095889+0.j         -0.46677831+0.06401951j -0.46677831-0.06401951j
0.41305258+0.40885121j  0.41305258-0.40885121j]]


### compute the trace & determinant¶

In [9]:
# notice this is not a function in linalg!!!
print(np.trace(arr))

1.6973419360521853

In [10]:
print(np.linalg.det(arr))

0.10415702298732037


### calculate the inverse/psedo-inverse of a matrix¶

In [11]:
arr = np.random.rand(5,5)

In [12]:
# compute the inverse of a matrix
print(np.linalg.inv(arr))

[[ 1.62830688 -0.06416505 -1.30966272 -0.45611653  0.15201803]
[-4.99190784  3.10651611  2.45847255  0.04818164  1.67114439]
[-2.59657164  2.26571146  1.35045327  0.60869944 -0.65015811]
[ 3.28739144 -3.26946016 -1.8135514   1.05990479 -0.39024731]
[ 4.07086055 -3.32087916 -0.67496677 -0.64585229 -0.0113713 ]]

In [13]:
# compute the psudo-inverse of a matrix
print(np.linalg.pinv(arr))

[[ 1.62830688 -0.06416505 -1.30966272 -0.45611653  0.15201803]
[-4.99190784  3.10651611  2.45847255  0.04818164  1.67114439]
[-2.59657164  2.26571146  1.35045327  0.60869944 -0.65015811]
[ 3.28739144 -3.26946016 -1.8135514   1.05990479 -0.39024731]
[ 4.07086055 -3.32087916 -0.67496677 -0.64585229 -0.0113713 ]]


### solve a linear system¶

In [14]:
# solve a linear system in closed form
y = [1,2,3,4,5]
print(np.linalg.solve(arr, y))

[-3.49338739 17.14499056  5.17021834 -6.40380048 -7.23606375]

In [15]:
# calculate the least-squares solution of a linear system
y = [1,2,3,4,5]
solution, residuals, rank, singular = np.linalg.lstsq(arr, y,rcond=-1)
print(solution)
print(residuals)
print(rank)
print(singular)

[-3.49338739 17.14499056  5.17021834 -6.40380048 -7.23606375]
[]
5
[2.86032336 0.8008827  0.58963958 0.52126536 0.09587094]