SymPy and Continuity

This page provides a brief introduction to SymPy for use with Continuity.

SymPy allows for symbolic mathematics with Python, and it provides for a user-friendly syntax for writing models within Continuity. The material coordinates, constitutive, and ionic model editors all use SymPy syntax. Upon compiling a model, Continuity converts the SymPy syntax into C code that can be efficiently executed. Additional information is available at the SymPy homepage.

SymPy Syntax

The SymPy manual page on matrices and linear algebra is also a useful reference. Not all of these commands are used in Continuity, but all are useful in understanding the continuum mechanics underlying the tutorials.


Basic Math

>>> c = a*b        # multiplication
>>> d = a/b        # division
>>> e = a**b       # exponents
>>> f = sqrt(a)    # square root
>>> g = cos(b)     # trig functions
>>> h = asin(a)    # inverse trig function
>>> pi = mpmath.pi # pi

Matrices and Linear Algebra

Matrix() – Create a matrix

>>> A = Matrix([[1,2,3],[4,5,6],[7,8,9]])
>>> print A
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
>>> A[2,1]    #Python indexing starts at zero
8
>>> X = Matrix([10, 0, 50])
>>> print X
[10]
[ 0]
[50]
>>> Y = Matrix([[8,13,72]])
>>> print Y
[8, 13, 72]

eye() – Create an identity matrix

>>> print A
[1, 0, 0]
[0, 1, 0]
[0, 0, 1]
>>> B = eye(2)
>>> print B
[1, 0]
[0, 1]

.T or .transpose() – Calculate the transpose of a matrix

>>> A = Matrix([[1,2,3],[4,5,6],[7,8,9]])
>>> print A
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
>>> AT = A.T
>>> print AT
[1, 4, 7]
[2, 5, 8]
[3, 6, 9]

.det() – Calculate the determinant of a matrix

>>> M = Matrix(( [1, 2, 3], [3, 6, 2], [2, 0, 1] ))
>>> M.det()
-28
>>> M2 = eye(3)
>>> M2.det()
1
>>> M3 = Matrix(( [1, 0, 0], [1, 0, 0], [1, 0, 0] ))
>>> M3.det()
0

.trace() – Calculate the trace of a matrix

>>> F = Matrix([[1.5, 0, 5],[2.4, 3., 0.5],[0, 1.1, 3.5]])
>>> F.trace()
8.00000000000000
>>> I = eye(3)
>>> I.trace()
3

.inv() – Calculate the inverse of a matrix

>>> Z=Matrix([[2,0,-1],[-1,1,2],[-3,0,1]])
>>> print Z
[ 2, 0, -1]
[-1, 1,  2]
[-3, 0,  1]
>>> Z.inv()
⎡-1  0  -1⎤
⎢         ⎥
⎢5   1  3 ⎥
⎢         ⎥
⎣-3  0  -2⎦
>>> Z*Z.inv()
⎡1  0  0⎤
⎢       ⎥
⎢0  1  0⎥
⎢       ⎥
⎣0  0  1⎦

#* – Evaluate a function non-symbolically

>>> C = F.T*F#*

The following functions are not used in the Continuity model editors but may be useful when using SymPy externally.

.jacobian() – Calculate the Jacobian matrix of a vectorial function.

>>> r, theta = symbols('r, theta')
>>> X = Matrix([r*cos(theta), r*sin(theta)])
>>> Y = Matrix([r, theta])
>>> X.jacobian(Y)
⎡cos(θ)  -r⋅sin(θ)⎤
⎢                 ⎥
⎣sin(θ)  r⋅cos(θ) ⎦

is_symmetric – Check if a matrix is a symmetric matrix

>>> M = Matrix([[1,2,3],[2,1,2],[3,2,1]])
>>> print M
[1, 2, 3]
[2, 1, 2]
[3, 2, 1]
>>> M.is_symmetric()
True
>>> M - M.T
⎡0  0  0⎤
⎢       ⎥
⎢0  0  0⎥
⎢       ⎥
⎣0  0  0⎦

Symbolic Math

Symbol() – Define a variable that you would like to keep as a variable and not solve for a numerical value

>>> A = Symbol('A')
>>> B, C, D = symbols('B, C, D')
>>> A = B**2 + 3*D - 2*C
>>> print A
B**2 - 3*C + 2*D

.subs() – Substitute symbolic entries with a numerical value

>>> y, x = symbols('y, x')
>>> y = 2*x**2 +4*x + 14
>>> print y
2*x**2 + 4*x + 14
>>> y.subs(x,2)
30
>>> print y   #note that the original symbolic value for y is not overwritten
2*x**2 + 4*x + 14

.simplify() – Simplify symbolic algebraic expression. This is very helpful after doing matrix manipulations.

>>> F,B,X,Y,Z,a,b = symbols('F,B,X,Y,Z,a,b')
>>> F = Matrix([[1,-a*Z,-a*Y],[a*Z,1,a*X],[0,0,b]])
>>> B = F*F.T
>>> Binv
(I'll omit the mess of a matrix this prints out)
>>> B.simplify()
>>> B
[Y**2*a**2 + Z**2*a**2 + 1,                 -X*Y*a**2, -Y*a*b]
[                -X*Y*a**2, X**2*a**2 + Z**2*a**2 + 1,  X*a*b]
[                   -Y*a*b,                     X*a*b,   b**2]

If you would like to see additional entries in this list, please email kevin.p.vincent@gmail.com with suggestions.


Using SymPy

There are many ways to use SymPy inside and outside of Continuity. SymPy is required in the model editors, but the Python Shell can also be used as a SymPy sandbox. You can test the equations used in the model editor for simple cases. SymPy can also be used with other Python interpreters outside of Continuity such as IDLE, IPython, and the basic command line Python.

The following example of simple extension illustrates how a Python interpreter with SymPy can be used to test the equations in a constitutive model.

  • First, open the Python IDLE Shell in Continuity by clicking the furthest left icon in the toolbar

  • Click in the new screen that opens and press the return key

CONTINUITY6.4 >>> from __future__ import division
CONTINUITY6.4 >>> from sympy import *
CONTINUITY6.4 >>> F, B, e = symbols('F, B, e')
CONTINUITY6.4 >>> l1, l2, l3 = symbols('l1, l2, l3')
CONTINUITY6.4 >>> F = Matrix([[l1,0,0],[0,l2,0],[0,0,l3]])
CONTINUITY6.4 >>> print F
[l1,  0,  0]
[ 0, l2,  0]
[ 0,  0, l3]
CONTINUITY6.4 >>> B = F*F.T
CONTINUITY6.4 >>> print B
[l1,  0,  0]
[ 0, l2,  0]
[ 0,  0, l3]
CONTINUITY6.4 >>> e = 0.5*(eye(3)-B.inv())
CONTINUITY6.4 >>> print e
[0.5 - 0.5/l1**2,               0,               0]
[              0, 0.5 - 0.5/l2**2,               0]
[              0,               0, 0.5 - 0.5/l3**2]
CONTINUITY6.4 >>>e.subs(l1,1.09).subs(l2,0.96).subs(l3,0.96)
⎡0.07916000336672           0                    0         ⎤
⎢                                                          ⎥
⎢       0          -0.0425347222222222           0         ⎥
⎢                                                          ⎥
⎣       0                   0           -0.0425347222222222⎦