import sympy as sp3.3 Basics of python
This chapter covers the Python tools we use throughout this book: SymPy for symbolic mathematics, NumPy for numerical computation, and Matplotlib for visualization. The focus here is on the subset needed for mechanics. For a comprehensive treatment of computational thinking, applied mathematics and Python applications, see python.ju.se.
We also use MechanicsKit, a lightweight package that provides convenience functions for engineering education: LaTeX rendering of arrays, MATLAB-style plotting of symbolic expressions with fplot, 1-based indexing for FEM workflows, and more. Install it with:
uv pip install git+https://github.com/cenmir/MechanicsKit.gitSymbolics using SymPy
To get started with symbolic computation in Python, we will use the SymPy library. SymPy is a Python library for symbolic mathematics, also known as computer algebra system (CAS). We can use SymPy to perform algebraic manipulations, calculus, solve equations, and much more. We load the SymPy library using the following command:
Let’s showcase some of the basic functionalities of SymPy using an example, we will solve the following equation: \(x^2 = 4\).
First we create a symbol \(x\) using the symbols function. We then define the equation \(x^2 = 4\) using the Eq function. Finally, we solve the equation using the solve function.
x = sp.symbols('x') # create a symbol x
eq = sp.Eq(x**2, 4) # define the equation x^2 = 4
sol = sp.solve(eq, x) # solve the equation
sol[-2, 2]
We could also utillize assumptions to solve the equation. For example, if we know that \(x\) is a real number, we can use the solve function with the real=True argument. Or if we know that \(x\) is a positive number, we can use the positive=True argument.
x = sp.symbols('x', positive=True) # create a symbol x which is positive
eq = sp.Eq(x**2, 4) # define the equation x^2 = 4
sol = sp.solve(eq, x) # solve the equation
sol[2]
Example 1 - Creating a vector and equation system
We will create a force vector with two unknown forces \(F_x\) and \(F_y\).
F_x, F_y = sp.symbols('F_x F_y') # create symbols F_x and F_y
FF = sp.Matrix([F_x, F_y, 0]) # create a matrix FF with F_x and F_y and 0
FF\(\displaystyle \left[\begin{matrix}F_{x}\\F_{y}\\0\end{matrix}\right]\)
Next we create an equation system
from sympy import pi, rad
# Define convenience functions for degrees
def sind(x):
return sp.sin(rad(x))
def cosd(x):
return sp.cos(rad(x))
# create a matrix FF1 = [250*cosd(38), 250*sind(38), 0]
FF1 = 250*sp.Matrix([cosd(38), sind(38), 0])
# define the equation FF + FF1 = [1000, 0, 0]
eq = sp.Eq(FF + FF1, sp.Matrix([1000, 0, 0]))
eq\(\displaystyle \left[\begin{matrix}F_{x} + 250 \cos{\left(\frac{19 \pi}{90} \right)}\\F_{y} + 250 \sin{\left(\frac{19 \pi}{90} \right)}\\0\end{matrix}\right] = \left[\begin{matrix}1000\\0\\0\end{matrix}\right]\)
The output can be displayed as a formatted equation system. The MechanicsKit package provides la, which renders arrays, matrices and solver dictionaries as LaTeX using the pipe syntax:
sol = sp.solve(eq, [F_x, F_y]) # solve the equation
sol{F_x: 1000 - 250*cos(19*pi/90), F_y: -250*sin(19*pi/90)}
The output could be printed in a more readable format. The MechanicsKit package provides la, which renders any NumPy array or SymPy expression as formatted LaTeX using the pipe syntax:
from mechanicskit import la
sol | la\[ \begin{aligned} F_{x} &= 1000 - 250 \cos{\left(\frac{19 \pi}{90} \right)} \\ F_{y} &= - 250 \sin{\left(\frac{19 \pi}{90} \right)} \end{aligned} \]
The output of solve is a dictionary where the keys are the variables and the values are the solutions. We can access the solutions using the variable names as keys. The variables are SymPy expressions, so we can use them in further calculations or convert them to floats using evalf.
# extract the values of F_x and F_y into a matrix and evaluate the matrix
sp.Matrix([sol[F_x], sol[F_y], 0]).evalf() \(\displaystyle \left[\begin{matrix}802.997311598319\\-153.915368831415\\0\end{matrix}\right]\)