This document provides a cheat sheet for performing linear algebra operations using the SymPy library in Python. It outlines how to import SymPy, create matrices, perform basic operations like addition and multiplication, find eigenvalues and eigenvectors, decompose matrices, and solve systems of linear equations. Useful functions are listed for indexing matrices, performing row operations, working with vectors, and analyzing vector spaces.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
137 views
Linear Algebra Using Sympy Cheat Sheet: by Via
This document provides a cheat sheet for performing linear algebra operations using the SymPy library in Python. It outlines how to import SymPy, create matrices, perform basic operations like addition and multiplication, find eigenvalues and eigenvectors, decompose matrices, and solve systems of linear equations. Useful functions are listed for indexing matrices, performing row operations, working with vectors, and analyzing vector spaces.
import sympy as sp Sum A+B Find the eigenvalues M.eige‐
Substraction A-B nvals() Matrix Creation Find the eignevalues and the M.eigenve‐ Matrix Multiply A*B normal Matrix sp.Matrix([[1,2],‐ corresponding eigenspace cts() Scalar Multiply 5*A [3,4]]) Diagonalize a matrix P, D = Elementwise sp.matrix_multiply_eleme‐ Matrix with all zeros sp.zeros(4,5) M.diagona‐ product ntwise(A,B) lize() Matrix with all ones sp.ones (4,5) Transpose A.T test if the matrix is diagonali‐ M.is_diag‐ Square matrix with all sp.zeros(5) Determinant A.det() zable onalizable zeros Inverse A.inv() Calculate Jordan From P, J = Square matrix with all sp.ones (5) Condition A.condition_number() M.jordan_‐ ones Number form() Identity matrix sp.eyes(5) Row count A.rows Diagonal Matrix sp.diag (‐ Decomposition Column count A.cols 1,2,3,4) Trace A.trace() LU Decompositio‐ P,L,U=A.LUdecom‐ Generate element with sp.Matrix(2,3,‐ n(PA=LU) position() func(i,j) func) Elementary Row Operations QR Decomposition Q,R=A.QRdecompo‐ sition() Matrix Modification Replac‐ m.row_op(i, lambda ele,col:e‐ ement le+m.row(j)[col]*c) Delete the i-th row M.row_del(i) Vector Operations Interc‐ M.row_swap(i,j) Delete the j-th column M.col_del(j) hange Create a column vector v=sp.Matrix([1,‐ Row join M1 and M2 M1.row_join(M2) 2,3]) Scaling m.row_op(i,lambda ele,col:e‐ Column join M1 and M2 M1.col_join(M2) dot product v1.dot(v2) le*c) cross product v1.cross(v2) Indexing(Slicing) Linear Equations length of the vector v.norm() get the element in M at (i,j) M[i,j] Echelon From M.echelon_form() normalize of vector v.normalize() get the i-th row in M M.row(i) Reduced Echelon M.rref() the projection of v1 on v1.project(v2) get the i-th row in M M[i,:] Form v2 get the j-th column in M M.col(j) Solve AX=B (B x,freevars=A.gauss‐ Gram-Schmidt orthog‐ sp.GramSchmi‐ get the j-th column in M M[:,j] can be a matrix) _jordan_solve(B) onalize dt([v1,v2,v3]) get the i-th and the k-th rows M[[i,k],:] least-square fit A.solve_least_squa‐ Gram-Schmidt orthog‐ sp.GramSchmi‐ Ax=b res(b) onalize with normal‐ dt(‐ get the j-th and the k-th columns M[:,[j,k]] solve Ax=b A.solve(b) ization [v1,v2,v3],True) get rows from i to k M[i:k,:] Singular values M.singlular_val‐ get columns from j to k M[:,j:k] Vector Space ues() get sub-matrix (row i to k,col j to M[i:k,j:l] Basis of column space M.columnspace() l) Block Matrix Basis of null space M.nullspace() Note: All indices start from 0 Create a matrix by M=sp.Matrix([[A,B], Basis of row space M.rowspace() block [C,D]]) Rank M.rank()
By royqh1979 Published 8th August, 2019. Sponsored by Readable.com
cheatography.com/royqh1979/ Last updated 24th August, 2019. Measure your website readability! Page 1 of 2. https://readable.com Linear Algebra Using SymPy Cheat Sheet by royqh1979 via cheatography.com/87753/cs/20231/
Useful Links
SymPy Documentation SymPy Tutorial
By royqh1979 Published 8th August, 2019. Sponsored by Readable.com
cheatography.com/royqh1979/ Last updated 24th August, 2019. Measure your website readability! Page 2 of 2. https://readable.com