Show Menu
Cheatography

Linear Algebra Using SymPy Cheat Sheet by

Import SymPy

import sympy as sp

Matrix Creation

normal Matrix
sp.Matrix([[1,2­],[­3,4]])
Matrix with all zeros
sp.zeros(4,5)
Matrix with all ones
sp.ones(4,5)
Square matrix with all zeros
sp.zeros(5)
Square matrix with all ones
sp.ones(5)
Identity matrix
sp.eyes(5)
Diagonal Matrix
sp.diag(1,2,3,4)
Generate element with func(i,j)
sp.Matrix(2,3,func)

Matrix Modifi­cation

Delete the i-th row
M.row_­del(i)
Delete the j-th column
M.col_­del(j)
Row join M1 and M2
M1.row­_jo­in(M2)
Column join M1 and M2
M1.col­_jo­in(M2)

Indexi­ng(­Sli­cing)

get the element in M at (i,j)
M[i,j]
get the i-th row in M
M.row(i)
get the i-th row in M
M[i,:]
get the j-th column in M
M.col(j)
get the j-th column in M
M[:,j]
get the i-th and the k-th rows
M[[i,k],:]
get the j-th and the k-th columns
M[:,[j,k]]
get rows from i to k
M[i:k,:]
get columns from j to k
M[:,j:k]
get sub-matrix (row i to k,col j to l)
M[i:k,j:l]
Note: All indices start from 0
 

Basic opertaions

Sum
A+B
Substr­action
A-B
Matrix Multiply
A*B
Scalar Multiply
5*A
Elemen­twise product
sp.mat­rix­_mu­lti­ply­_el­eme­ntw­ise­(A,B)
Transpose
A.T
Determ­inant
A.det()
Inverse
A.inv()
Condition Number
A.cond­iti­on_­num­ber()
Row count
A.rows
Column count
A.cols
Trace
A.trace()

Elementary Row Operations

Replac­ement
m.row_­op(i, lambda ele,co­l:e­le+­m.r­ow(­j)[­col]*c)
Interc­hange
M.row_­swa­p(i,j)
Scaling
m.row_­op(i,lambda ele,co­l:e­le*c)

Linear Equations

Echelon From
M.eche­lon­_form()
Reduced Echelon Form
M.rref()
Solve AX=B (B can be a matrix)
x,free­var­s=A.ga­uss­_jo­rda­n_s­olve(B)
least-­square fit Ax=b
A.solv­e_l­eas­t_s­qua­res(b)
solve Ax=b
A.solve(b)

Vector Space

Basis of column space
M.colu­mns­pace()
Basis of null space
M.null­space()
Basis of row space
M.rows­pace()
Rank
M.rank()
 

Eigenv­alues amd Eigenv­ectors

Find the eigenv­alues
M.eige­nvals()
Find the eignev­alues and the corres­ponding eigenspace
M.eige­nve­cts()
Diagon­alize a matrix
P, D = M.diag­ona­lize()
test if the matrix is diagon­ali­zable
M.is_d­iag­ona­lizable
Calculate Jordan From
P, J = M.jord­an_­form()

Decomp­osition

LU Decomp­osi­tio­n(P­A=LU)
P,L,U=­A.L­Ude­com­pos­ition()
QR Decomp­osition
Q,R=A.Q­Rd­eco­mpo­sit­ion()

Vector Operations

Create a column vector
v=sp.M­atr­ix(­[1,­2,3])
dot product
v1.dot(v2)
cross product
v1.cro­ss(v2)
length of the vector
v.norm()
normalize of vector
v.norm­alize()
the projection of v1 on v2
v1.pro­jec­t(v2)
Gram-S­chmidt orthog­onalize
sp.Gra­mSc­hmi­dt(­[v1­,v2­,v3])
Gram-S­chmidt orthog­onalize with normal­ization
sp.Gra­mSc­hmi­dt(­[v1­,v2­,v3­],True)
Singular values
M.sing­lul­ar_­val­ues()

Block Matrix

Create a matrix by block
M=sp.M­atr­ix(­[[A­,B]­,[C­,D]])

Useful Links

       
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

            Python 3 Cheat Sheet by Finxter