Sympy is a Python package used to work with symbolic math. Symbolic math refers to calculations that use symbolic variables rather than variables that contain numerical values. Let's take a simple example:
$$ sin(\frac{\pi}{2}) $$If we define
$$ \pi = 3.1459 $$Then calculate the arcsine of pi/2, we should get 1. But the value doesn't exactly equal 1:
In [2]:
import math
pi = 3.1459
print(math.sin(pi/2))
Instead, if we define pi symbolically
$$ \pi = \pi $$Then when we calculate the sine of pi/2, we get an exact value, 1:
pi = symbolic math symbol version of pi
print(sin(pi/2))
1
Now let's take a concrete problem and solve it with symbolic math
In [3]:
import sympy
In [4]:
a, T1, T2, L1, L2 = sympy.symbols('a T1 T2 L1 L2')
In [5]:
CTE_Eq = sympy.Eq(a*L1*(T2-T1), L2)
In [6]:
CTE_Eq.subs(T1, 20)
CTE_Eq.subs(L1, 80.00)
CTE_Eq.subs(L2, 80.10)
CTE_Eq.subs(a, 24e-6)
Out[6]:
In [7]:
CTE_Eq
Out[7]:
In [8]:
expr = sympy.solveset(CTE_Eq, T2)
In [9]:
expr.subs(T1, 20)
expr.subs(L1, 80.00)
expr.subs(L2, 80.10)
expr.subs(a, 24e-6)
Out[9]:
In [10]:
expr
Out[10]:
In [11]:
T1 = 20
#T2 = ?
a = 24e-6
L1 = 80
L2 = 80.10
In [12]:
T2 = T1 + (L2)/(a*L1)
T2
Out[12]:
In [13]:
a*L1*(72-20)
Out[13]:
In [14]:
T1+ (L2-L1)/(a*L1)
Out[14]:
In [15]:
def clearance(cl):
T1 = 20
#T2 = ?
a = 24e-6
L1 = 80
L2 = 80.10 + cl
return T1+ (L2-L1)/(a*L1)
In [19]:
clearance(0.10)
Out[19]:
In [ ]: