from sympy import *
init_printing(use_unicode=True)
import matplotlib.pyplot as plt
%matplotlib inline
from sympy.vector import *
#Define the coordinate system in 3D
R = CoordSys3D('R')
v = 3*R.i + 4*R.j + 5*R.k
v
v.components
#Obtain perpendicular direction
R.i.cross(R.j)
#or shorter form
R.i^R.i
v^R.i
R.i.dot(R.j)
#Shorter form
R.i&R.i
v.dot(R.k)
v.magnitude()
v.normalize()
Rotacional
v1 = R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
v1
curl(v1)
v2 = R.x*R.y*R.z*R.i
v2
curl(v2)
v1 = R.i + R.j + R.k
curl(p*v1)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-19-46cf8ff55cdb> in <module>() 1 v1 = R.i + R.j + R.k ----> 2 curl(p*v1) NameError: name 'p' is not defined
v1 = R.x*R.y*R.z * (R.i+R.j+R.k)
v1
divergence(v1)
v2 = 2*R.y*R.z*R.j
v2
divergence(v2)
s1 = R.x*R.y*R.z
s1
gradient(s1)
from sympy import Curve, line_integrate, E, ln
from sympy.abc import x, y, t
C = Curve([E**t + 1, E**t - 1], (t, 0, ln(2)))
C
line_integrate(x +y, C, [x, y])
x,y,z -> spherical
from sympy.vector import CoordSys3D
a = CoordSys3D('a')
#b = a.create_new('b', transformation='spherical')
#b.transformation_to_parent()
#b.transformation_from_parent()
x, y, z = symbols('x y z')
s = fourier_series(x**2, (x, -pi, pi))
s
s.truncate(4)
k =symbols('k')
fourier_transform(exp(-x**2), x, k)
inverse_fourier_transform(sqrt(pi)*exp(-(pi*k)**2), k, x)
f = x #(x+1)**2
g= 9 #log(x)**2
p = Piecewise((0, x <= -1), (f, x <= 3), (g, True))
p.subs(x, 1)
i = [i for i in range(-10, 10)]
test = map(lambda i: p.subs(x, i).evalf(), i )
plt.plot(i, test)
plt.grid()
Heaviside(9)
Heaviside(-9)
Heaviside(0)
Heaviside(x).fdiff()
from sympy.integrals import laplace_transform
from sympy.abc import t, s, a
laplace_transform(t**a, t, s)
f = Function('f')
eq = sin(x)*cos(f(x)) + cos(x)*sin(f(x))*f(x).diff(x)
eq
dsolve(eq, hint='1st_exact')
eq = f(x).diff(x,x)-0*f(x).diff(x)-4*f(x) - x**2*sin(x)#exp(x)
eq
dsolve(eq)
dsolve(f(x).diff(x, 4) + 2*f(x).diff(x, 3) -
2*f(x).diff(x, 2) - 6*f(x).diff(x) + 5*f(x), f(x),
hint='nth_linear_constant_coeff_homogeneous')
from sympy.abc import rho, phi
f = Function('f')(x, y)
g1 = Function('g')(x, y)
g2 = x**2 + 3*y
f2 = x**3 + 3*y
hessian(f, (x, y), [g1, g2])
X = Matrix([rho*cos(phi), rho*sin(phi)])
Y = Matrix([rho, phi])
X.jacobian(Y)
f = Function('f')
g = Function('g')
f
wronskian([x, x**2], x)
from IPython.display import Image
Image("figures/SL.jpg")
KroneckerDelta(1, 2)
KroneckerDelta(3, 3)
LeviCivita(1, 2, 3)
LeviCivita(1, 3, 2)
LeviCivita(1, 2, 2)
$\left(\frac{n}{k}\right)= \frac{n!}{k!(n-k)!}$
n = Symbol('n', integer=True, positive=True)
binomial(15, 8)
for N in range(8):
print([binomial(N, i) for i in range(N + 1)])
[1] [1, 1] [1, 2, 1] [1, 3, 3, 1] [1, 4, 6, 4, 1] [1, 5, 10, 10, 5, 1] [1, 6, 15, 20, 15, 6, 1] [1, 7, 21, 35, 35, 21, 7, 1]
binomial(n, 4).expand(func=True)
expand_func(binomial(n, 4))
(n - 2k)
factorial(7)
var('n')
factorial2(n + 1)
factorial2(5), factorial2(6)
[fibonacci(x) for x in range(11)]
instead of starting with two predetermined terms, the sequence starts with three predetermined terms and each term afterwards is the sum of the preceding three terms.
0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927, 1705, 3136, 5768, 10609, 19513, 35890, 66012,
[tribonacci(x) for x in range(11)]
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-69-8fd3055a0a90> in <module>() ----> 1 [tribonacci(x) for x in range(11)] NameError: name 'tribonacci' is not defined
[harmonic(n) for n in range(6)]
[harmonic(n, 2) for n in range(6)]
$\sum_{n=1}^\infty \frac{1}{n^2}$, or $\zeta(2)$
harmonic(oo, 2)
expand_func(harmonic(n+4))
from IPython.display import Image
Image("figures/lucas.png")
# The golden ratio
[lucas(x) for x in range(11)]