In [1]:
from sympy import *
from sympy.interactive.printing import init_printing
init_printing()

In [2]:
from sympa import domath

In [3]:
import pandas as pd

In [4]:
df = pd.DataFrame({'x' : range(7)})
df['x'] = df.x * 10
df['y'] = df.x * 0.05
df['r'] = pi * df['y'] / 2
df


Out[4]:
x y r
0 0 0.0 0
1 10 0.5 0.25*pi
2 20 1.0 0.5*pi
3 30 1.5 0.75*pi
4 40 2.0 1.0*pi
5 50 2.5 1.25*pi
6 60 3.0 1.5*pi

In [5]:
#for those who don't know..."pi" is irrational...
df['r'][1]


Out[5]:
$$0.25 \pi$$

In [6]:
x,y,r,rn1,x1,xn1,yn1 = symbols('x y r r_-1 x_1 x_-1 y_-1')
f = x + xn1
g = x**y + x*y*3.0 + (x1 * 0.5 + xn1 * pi / (yn1 + 1.0))**2
h = sin(r)
i = sin(r) / cos(rn1 + 1)

In [7]:
f, g, h, i


Out[7]:
$$\left ( x + x_{-1}, \quad 3.0 x y + x^{y} + \left(\frac{\pi x_{-1}}{y_{-1} + 1.0} + 0.5 x_{1}\right)^{2}, \quad \sin{\left (r \right )}, \quad \frac{\sin{\left (r \right )}}{\cos{\left (r_{-1} + 1 \right )}}\right )$$

In [8]:
df.x + df.x.shift(1) #this is easy...but not exactly


Out[8]:
0    NaN
1     10
2     30
3     50
4     70
5     90
6    110
Name: x, dtype: float64

In [9]:
#3.0 * df.x * df.y + df.x.mul(df.y) + (df.shift(1).mul(pi).div(df.shift(1) + 1.0))# + (0.5 * df.x.shift(-1))) # what the heck am I doing???

In [10]:
df['f'] = domath(df,f)
df['g'] = domath(df,g)
df['h'] = domath(df,h)
df['i'] = domath(df,i)

In [11]:
#round the excessive decimals...
df.g, df.h, df.i = (df[c].round(4) for c in ('g','h','i')); df


Out[11]:
x y r f g h i
0 0 0.0 0 NaN NaN 0.0000 NaN
1 10 0.5 0.25*pi 10 118.1623 0.7071 1.3087
2 20 1.0 0.5*pi 30 1371.9676 1.0000 -4.6958
3 30 1.5 0.75*pi 50 2942.9143 0.7071 -0.8403
4 40 2.0 1.0*pi 70 5771.1786 0.0000 0.0000
5 50 2.5 1.25*pi 90 23220.5400 -0.7071 1.3087
6 60 3.0 1.5*pi 110 NaN -1.0000 -4.6958

In [12]:
assert f.subs([(x,50),(xn1,40)]) == df['f'].iloc[5]
assert round(g.subs([(x,50),(y,2.5),(xn1,40),(x1,60),(yn1,2.0)]),4) == df['g'].iloc[5]
assert round(h.subs([(r,1.25*pi)]),4) == df['h'].iloc[5]
assert round(i.subs([(r,1.25*pi),(rn1,pi)]),4) == df['i'].iloc[5]

In [12]:


In [ ]: