# Sympy

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¶

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

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

while rows and columns can be accessed by appropriate slices,

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

Hence, a `Matrix`

is a *mutable* object. If for some reason theelements of a matrix should never be changed, one can use an`ImmutableMatrix`

, 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

The *transpose* of a matrix (in which rows are swapped with columns) isfound via the `.T`

operator, or

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

If we include only a single pair of brackets `[]`

in the matrixconstruction, a column vector is produced by default, e.g.,

## 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):

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:

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

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

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

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}):

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

Differentiation with respect to (x) leads to

## 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`

, and`is_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

Similarly, matrices of zeros or ones are also easy:

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

### Sympy Python

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.