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:
= sp.Matrix([[0], [1], [2]]) # $3\times 1$ column vector
u = sp.Matrix([[3, 4, 5]]) # $1\times 3$ row vector
v = sp.Matrix([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) # $3\times 3$ matrix A
Without 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:
= sp.symbols("x1, x2, x3")
x1, x2, x3 = sp.Matrix([[x1], [x2], [x3]]) # $3\times 1$ vector x
Symbolic matrix elements can be accessed with the same slicing
notation as list
s and NumPy
arrays; for insance:
0]
A[:,0,:]
A[1,1:]
A[0:,0] x[
As with list
s 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 list
s and
arrays, as follows:
0,0] = 7; A # A is changed
A[1] = sp.Matrix([[8], [8], [8]]); A # A is changed A[:,
Matrix 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,
2); A
A.row_del(1); A A.col_del(
Conversely, 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,
2, sp.Matrix([[9, 9]])) # A is unchanged
A.row_insert(1, sp.Matrix([[9], [9]])) # A is unchanged A.col_insert(
Addition and subtraction works element-wise, in accordance with the matrix mathematics, as follows:
= sp.Matrix([[0, 1], [2, 3]]) # $2\times 2$ matrix
A = sp.Matrix([[4, 5], [6, 7]]) # $2\times 2$ matrix
B + B
A - B A
Matrix multiplication is in accordance with mathematical matrix multiplication (i.e., not element-wise), as follows:
*B
A*A B
The matrix inverse, if it exists, can be computed by raising the
matrix to the power -1
,
as follows:
**-1
A**-1 B
The matrix transpose can be accessed as an attribute T
, which returns a transposed copy, as
follows:
A.T B.T
An n
-by-n
identity matrix can be constructed
via the eye(n)
function, as
follows:
3) sp.eye(
An n
-by-m
matrix with all 0
compenents
can be constructed via the zeros(n, m)
function, as follows:
2,4) sp.zeros(
Similarly, an n
-by-m
matrix with all 1
compenents
can be constructed via the ones(n, m)
function, as follows:
2,8) sp.ones(
A diagonal or block-diagonal matrix can be constructed by providing
the diagonal elements to the diag()
function, as follows:
= sp.diag(1, 2, 3); D D
The 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.