# Sympy Python

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.

## A Symbolic Matrix¶

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

Python is the #1 programming language in the world. It is used extensively in data analytics, machine learning and robotics. Due to its power, simplicity, and complete object model, Python has become the scripting language of choice for many large organizations, including Google, Yahoo, and IBM. A computer algebra system written in pure Python. Python science math computer-algebra hacktoberfest. Python 3,387 8,021 3,618 (3 issues need help) 509 Updated 35 minutes ago.

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:

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

## Sympy Library For Python

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¶

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

## Install Sympy Python

Differentiation with respect to (x) leads to

## Install Sympy Python

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

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:

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.