Cheatography

Linear Algebra Using SymPy Cheat Sheet by royqh1979

Import SymPy

 import sympy as sp

Matrix Creation

 normal Matrix sp.M­atr­ix­([­[1,­2],­[3,4]]) Matrix with all zeros sp.z­ero­s­(4,5) Matrix with all ones sp.o­nes­(4,5) Square matrix with all zeros sp.z­ero­s(5) Square matrix with all ones sp.o­nes­(5) Identity matrix sp.e­yes­(5) Diagonal Matrix sp.d­iag­(­1,2­,3,4) Generate element with func(i,j) sp.M­atr­ix­(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,­la­mbda 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]])

 SymPy Documentation SymPy Tutorial

2 Pages
//media.cheatography.com/storage/thumb/royqh1979_linear-algebra-using-sympy.750.jpg

PDF (recommended)