In [5]:
from sympy import *
init_printing()
In [2]:
x = symbols('x')
In [3]:
type(x)
Out[3]:
sympy.core.symbol.Symbol
In [6]:
x**2
Out[6]:
$$x^{2}$$
In [7]:
x,y = symbols('x,y')
In [8]:
(x+y)**2
Out[8]:
$$\left(x + y\right)^{2}$$
In [9]:
expand((x+y)**2)
Out[9]:
$$x^{2} + 2 x y + y^{2}$$
In [10]:
w = x**2 + 3*x + 6
In [11]:
w
Out[11]:
$$x^{2} + 3 x + 6$$
In [13]:
diff(w,x)
Out[13]:
$$2 x + 3$$
In [15]:
integrate(w,x)
Out[15]:
$$\frac{x^{3}}{3} + \frac{3 x^{2}}{2} + 6 x$$
In [20]:
integrate(w,(x,0,4))
Out[20]:
$$\frac{208}{3}$$
In [21]:
y = sin(x)
In [22]:
diff(y,x)
Out[22]:
$$\cos{\left (x \right )}$$

See lambdify for conveting sympy expressions to numpy-friendly functions

OO programming

In [23]:
from numpy import *
In [30]:
class segment:
    
    # constructor
    def __init__(self, point,direction,length):
        self.p = array(point,dtype=float)
        self.u = array(direction,dtype=float)
        self.u /= linalg.norm(self.u)
        self.l = length
        
    def __repr__(self):
        #return 'Wow! My basepoint is ' + str(self.p)        
        return '('+str(self.p)+','+str(self.u)+','+str(self.l)+')'
        #return 'Wow! My basepoint is ' + str(self.p)
In [31]:
s = segment([3,4],[1.2,4],7)
In [32]:
print(s)
([ 3.  4.],[ 0.28734789  0.95782629],7)
In [39]:
class interface(segment):
    def __init__(self,point,direction,length,refindexL,refindexR):
        segment.__init__(self,point,direction,length)
        self.nL = refindexL
        self.nR = refindexR
    def __repr__(self):
        return 'Hahaha'
In [40]:
i = interface([0,0],[1,1],7,1.3,1.5)
i
Out[40]:
Hahaha