Linear-Algebra With Python
Linear-Algebra With Python
Matrix Operations
In many instances, numpy arrays can be
thought of as matrices.
In the next slides we explore some matrix
operations on numpy arrays
Determinants
The determinant of an array is found by
using the det() function from the scipy.linalg
module.
>>> import scipy.linalg as slin
>>> a
array([[ 3, -5, 8],
[-1, 2, 3],
[-5, -6, 2]])
>>> slin.det(a)
259.0
3
Trace
The trace of an array is found by using the
trace() function from numpy.
>>> import numpy
>>> a
array([[ 3, -5,
[-1, 2,
[-5, -6,
>>> np.trace(a)
7
as np
8],
3],
2]])
Trace (cont)
Offset traces can also be computed.
>>> a
array([[ 3, -5, 8],
[-1, 2, 3],
[-5, -6, 2]])
>>> np.trace(a,-1)
-7
>>> np.trace(a,1)
-2
5
Inverses
Inverse of a matrix is computed from
scipy.linalg.inv() function.
>>> a
array([[ 3, -5, 8],
[-1, 2, 3],
[-5, -6, 2]])
>>> slin.inv(a)
array([[ 0.08494208, -0.14671815, -0.11969112],
[-0.05019305, 0.17760618, -0.06563707],
[ 0.06177606, 0.16602317, 0.003861 ]])
Inverses
Transpose of a matrix is computed from
numpy.transpose() function.
a
array([[ 3, -5, 8],
[-1, 2, 3],
[-5, -6, 2]])
>>> np.transpose(a)
array([[ 3, -1, -5],
[-5, 2, -6],
[ 8, 3, 2]])
7
8],
3],
2]])
10
a1
a2
a
3
b1
b2
b3
c1 x d1
c2 y d 2
d
c3
z
3
12
4 x 5 y 8z 4
2x 8 y 7z 0
5 x 8 y 5
13
scipy.linalg.solve()
This method takes the coefficient matrix and the
right-hand side vector as arguments and return a
vector with the solutions.
>>> cm = np.array([[4, -5, 8], [2, -8, 7], [-5, 8, 0]])
>>> rhs = np.array([4,0,-5])
>>> soln = slin.solve(cm,rhs)
>>> soln
array([ 1.53112033, 0.33195021, -0.05809129])
14
15
16
LU Decomposition
If the right-hand side vector changes, but the
coefficient matrix doesnt change, then the coefficient
matrix can be decomposed using LU decomposition.
This LU decomposition can then be used to solve the
system for any different right-hand side.
This saves time because the decomposition is the
single biggest drain on resources. So, by not having to
redo it every time, we save computational time.
17
LU Decomposition
To use LU decompositions:
First, use the slin.lu_factor() method on the coefficient
matrix, and assign the result to a new variable.
Then, use the slin.lu_solve() function with the
decomposition and the rhs as arguments.
LU Decomposition Example
>>> cm
array([[ 4, -5, 8],
[ 2, -8, 7],
[-5, 8, 0]])
>>> rhs
array([ 4, 0, -5])
>>> lu = slin.lu_factor(cm)
>>> soln = slin.lu_solve(lu,rhs)
>>> soln
array([ 1.53112033, 0.33195021, -0.05809129])
19
LU Decomposition Example
Once the LU decomposition is accomplished we
can use any right-hand side we want without
redoing the decomposition.
>>> soln = slin.lu_solve(lu,[4, -3, 9])
>>> soln
array([ 0.64315353, 1.52697095, 1.13278008])
>>> soln = slin.lu_solve(lu,[-2, -3, -12])
>>> soln
array([ 1.77593361, -0.39004149, -1.38174274])
20
Banded Matrices
Many large matrices in the sciences and
engineering are of a banded nature, meaning that
their non-zero values are along diagonals.
Methods for efficiently solving these type of
matrices have been developed.
Banded matrices also dont require as much
memory to store, since many of the values are
zero
21
3 2 3 5
0 2 1 5
0 0 9 1
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0
0
9
5
2
2
0
0
0 0 0
0 0 0 0 0 3 5
4 0 0 0 5 3 5
1 2 1 1
3 2 0
0 1 6 3 2 9 0
3 2 7
0 9 1
0
2 6
5 3 1 7
2 0 2 1
2 3 9 0
22
3 2 3 5
0 2 1 5
0 0 9 1
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0
0
9
5
2
2
0
0
0 0 0
0 0 0 0 0 3 5
4 0 0 0 5 3 5
1 2 1 1
3 2 0
0 1 6 3 2 9 0
3 2 7
0 9 1
0
2 6
5 3 1 7
2 0 2 1
2 3 9 0
23
24
In-class Exercise
Solve the set of matrix equations below using
solve_banded().
1 5 3 0
3 2 3 5
0 2 1 5
0 0 9 1
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0
0
9
5
2
2
0
0
0 a 5
0 0 0 b 3
0 0 0 c 3
4 0 0 d 2
0
3 2 0
e
0 1 6 f 7
3 2 7 g 8
0 9 1 h 1
0
25
In-class Results
a = 200.639937
b = -25.491352
c = -107.698899
d = -174.206761
e = 102.750000
f = 70.833333
g = -3.500000
h = 32.500000
26