Vectors and Matrices
Symbolic vectors and matrices can be constructed, manipulated, and
operated on with SymPy. Basic vectors and matrices are represented with
the mutable sp.matrices.dense.MutableDenseMatrix
class and can be constructed with the sp.Matrix constructor, as follows:
u = sp.Matrix([[0], [1], [2]]) # $3\times 1$ column vector
v = sp.Matrix([[3, 4, 5]]) # $1\times 3$ row vector
A = sp.Matrix([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) # $3\times 3$ matrixWithout loss of generality, we can refer to vectors and matrices as matrices.
Symbolic variables can be elements of symbolic matrices; for instance, consider the following:
x1, x2, x3 = sp.symbols("x1, x2, x3")
x = sp.Matrix([[x1], [x2], [x3]]) # $3\times 1$ vectorSymbolic matrix elements can be accessed with the same slicing
notation as lists and NumPy
arrays; for insance:
A[:,0]
A[0,:]
A[1,1:]
x[0:,0]As with lists and
contrary to arrays, these slices return a copy and not a view of the
original matrix. Elements and slices can be overwritten with the same
notation as lists and
arrays, as follows:
A[0,0] = 7; A # A is changed
A[:,1] = sp.Matrix([[8], [8], [8]]); A # A is changedMatrix row i or column j can be deleted with the row_del(i) or col_del(j) method. These methods
operate in place. For instance,
A.row_del(2); A
A.col_del(1); AConversely, a row can be inserted at index i or a column can be inserted at index
j with the method row_insert(i, row) or col_insert(j, col). These methods do
not operate in place. For instance,
A.row_insert(2, sp.Matrix([[9, 9]])) # A is unchanged
A.col_insert(1, sp.Matrix([[9], [9]])) # A is unchangedAddition and subtraction works element-wise, in accordance with the matrix mathematics, as follows:
A = sp.Matrix([[0, 1], [2, 3]]) # $2\times 2$ matrix
B = sp.Matrix([[4, 5], [6, 7]]) # $2\times 2$ matrix
A + B
A - BMatrix multiplication is in accordance with mathematical matrix multiplication (i.e., not element-wise), as follows:
A*B
B*AThe matrix inverse, if it exists, can be computed by raising the
matrix to the power -1,
as follows:
A**-1
B**-1The matrix transpose can be accessed as an attribute T, which returns a transposed copy, as
follows:
A.T
B.TAn n-by-n identity matrix can be constructed
via the eye(n) function, as
follows:
sp.eye(3)An n-by-m matrix with all 0 compenents
can be constructed via the zeros(n, m) function, as follows:
sp.zeros(2,4)Similarly, an n-by-m matrix with all 1 compenents
can be constructed via the ones(n, m) function, as follows:
sp.ones(2,8)A diagonal or block-diagonal matrix can be constructed by providing
the diagonal elements to the diag() function, as follows:
D = sp.diag(1, 2, 3); DThe determinant of a matrix can be computed via the det() method, as follows:
D.det()The eigenvalues and eigenvectors of a matrix can be computed via the
eigenvects() method, which
returns a list of tuples, one for each eigenvalue, of the form (eval, m, evec),
where eval is the
eigenvalue, m is the
corresponding algebraic multiplicity of the eigenvalue, and evec is the corresponding eigenvector.
For instance,
A.eigenvects()[(3/2 - sqrt(17)/2,
1,
[Matrix([
[-sqrt(17)/4 - 3/4],
[ 1]])]),
(3/2 + sqrt(17)/2,
1,
[Matrix([
[-3/4 + sqrt(17)/4],
[ 1]])])]
Online Resources for Section 4.5
No online resources.