Sympy

Posted onby admin

In this lesson, we’ll review some of the basics of linear algebraopertations using SymPy. Before diving in, let’s import and initializeeverything we’ll need.

SymPy is very good at integral and differential calculus, though it doesn't know the scientific names of beings animalculous. With symbolic solvers for differentiation, integration, and many ODEs and PDEs in several variables, it is a great resource - just make sure you sanity-check the answers you get.

A Symbolic Matrix¶

Diabetes

SymPy Live is SymPy running on the Google App Engine. This is just a regular Python shell, with the following commands executed by default. SymPy follows Python’s naming conventions for inverse trigonometric functions, which is to append an a to the front of the function’s name. For example, the inverse cosine, or arc cosine, is called acos. acos(x) acos (x) cos(acos(x)) x asin(1) π ─ 2.

Matrices are easy to define in SymPy. For instance, a (2times 3)matrix is easily constructed via

[begin{split}left[begin{matrix}a & b & cd & e & fend{matrix}right]end{split}]

Individual elements of (mathbf{A}) (often denoted(mathbf{A}_{ij}) for row (i) and column (j)) can beaccessed directly by familiar array indexing,

[d]

while rows and columns can be accessed by appropriate slices,

[begin{split}left ( left[begin{matrix}adend{matrix}right], quad left[begin{matrix}d & e & fend{matrix}right]right )end{split}]

Such indexing or slicing can be used to change elements, e.g.,

[begin{split}left[begin{matrix}i & bd & eend{matrix}right]end{split}]

Hence, a Matrix is a mutable object. If for some reason theelements of a matrix should never be changed, one can use anImmutableMatrix, e.g.,

A Matrix object has associated with it a number of attributes. Forexample, the number of rows and columns can always be found individuallyor collectively via

[2]
[3]
[left ( 2, quad 3right )]

The transpose of a matrix (in which rows are swapped with columns) isfound via the .T operator, or

[begin{split}left[begin{matrix}a & db & ec & fend{matrix}right]end{split}]

SymPy has no special definitions for vectors. Rather, vectors arematrices with a single column or single row, e.g.,

[begin{split}left[begin{matrix}a & bend{matrix}right]end{split}]
[begin{split}left[begin{matrix}cdend{matrix}right]end{split}]

If we include only a single pair of brackets [] in the matrixconstruction, a column vector is produced by default, e.g.,

[begin{split}left[begin{matrix}abend{matrix}right]end{split}]

Matrix Operations¶

At the core of linear algebra are linear operations on vectors. Thesimplest of this is the linear combination of two vectors(a mathbf{v}_1 + b mathbf{v}_2):

[begin{split}left[begin{matrix}a c + b ea d + b fend{matrix}right]end{split}]

Another important operation is the inner (or dot) product (i.e., thesum of the element-wise products). The inner product is usually denotedfor two (column) vectors by (mathbf{v}_1 cdot mathbf{v}_2) or(mathbf{v}_1^T mathbf{v}_2). In SymPy, both the inner productcan be computed in two ways:

[left[begin{matrix}c e + d fend{matrix}right]]
[c e + d f]
Sympy download

Similarly, the outer product(mathbf{v}_1 mathbf{v}_2^T) oftwo column vectors can be computed via

[begin{split}left[begin{matrix}c e & c fd e & d fend{matrix}right]end{split}]

Probably the most important operation in all of scientific computingis the product of matrix and a vector. The inner and outer products justobserved are special cases of matrix-vector multiplication. More generalmatrix-matrix multiplication can be consider a sequence of matrix-vectormultiplications. SymPy handles matrix-vector multiplication with ease:

Symptoms

[begin{split}left[begin{matrix}a g + b h + c id g + e h + f iend{matrix}right]end{split}]

Of course, the multiplication of a (mtimes n) matrix(mathbf{A}) by a (ntimes 1) vector (mathbf{v})should result in a (m times 1). For our example, (m=2),(n=3), and the result is consistent.

Fundamentally, matrix-vector multiplication can be deconstructed into asequence of simpler vector operations. You have most likely learned the“sequence of dot products” definition, in which the inner product ofeach row of (mathbf{A}) with the vector (mathbf{v})defines one element of the maxrix-vector product. For our example, thiswould be defined as

[begin{split}left[begin{matrix}a g + b h + c id g + e h + f iend{matrix}right]end{split}]

which is identical to the result above. There is a different way to viewmatrix-vector multiplication that will be important in the next lessonon linear systems. Rather than decompose (mathbf{A}) by rowfollowed by inner products with (mathbf{v}), consider decomposing(mathbf{A}) by columns. Look carefully at the (2times 1)matrix-vector product above. The first element starts with (ag)and the second with (dg), where (g) is the first element of(mathbf{v}) and ((a, d)^T) is the first column of(mathbf{A}). The pattern repeats, and we find that thematrix-vector product is nothing more than a linear combination of thecolumns of (mathbf{A}):

[begin{split}left[begin{matrix}a g + b h + c id g + e h + f iend{matrix}right]end{split}]

This column-oriented view is incredibly useful and has long beenpromoted by Gilbert Strang at MIT (whose book free videos on linearalgebra are quite good!).

Other Operations¶

Sympy Lambdify

Because matrices can have symbols, a variety of symbolic operations arealso applicable to matrices. For example, consider the matrix-vectorproduct

[begin{split}left[begin{matrix}2 x - y- x + 2 yend{matrix}right]end{split}]

Differentiation with respect to (x) leads to

Documentation
[begin{split}left[begin{matrix}2-1end{matrix}right]end{split}]

Matrix Properties¶

SymPy provides a number of methods for determining matrix properties.For instance, the aptly-named is_symbolic tells if a matrix consistsof symbolic elements or not:

Other such methods include is_symmetric, is_hermitian, andis_upper, for which more information may be found in the the SymPydocumentation.

Creating Matrices¶

Sometimes, an application requires an arbitrary matrix, or a matrix withspecific properties. For instance, the a (3times 3) identitymatrix (mathbf{I}) can be created using

Sympy Solve

[begin{split}left[begin{matrix}1 & 0 & 00 & 1 & 00 & 0 & 1end{matrix}right]end{split}]

Similarly, matrices of zeros or ones are also easy:

Symptoms
[begin{split}left[begin{matrix}0 & 00 & 00 & 0end{matrix}right]end{split}]
[begin{split}left[begin{matrix}1 & 1 & 11 & 1 & 1end{matrix}right]end{split}]

Sometimes, a matrix with arbitrary constants is useful. SymPy doesn’tappear to have that as a built-in function, so here’s my attempt:

[begin{split}left[begin{matrix}c_{00} & c_{01} & c_{02}c_{10} & c_{11} & c_{12}end{matrix}right]end{split}]

Sympy Python

[c_{00}]

Here, the exec function is exploited to do some on-the-fly symbolgeneration and assignments–pretty nifty stuff.

Further Reading¶

Please read the SymPy tutorial section onmatrices.