6626070
2997924

PL03-Topic02, SymPy

Back to the previous page | page management
List of posts to read before reading this article


Contents


Installation

For linux

$ 




For windows





Version Control






Symbols

import numpy as np
import sympy
sympy.init_printing()
from sympy import I, pi, oo

x = sympy.Symbol("x")
y = sympy.Symbol("y", real=True) 
y.is_real

\(True\)

x.is_real is None 

\(True\)

sympy.Symbol("z", imaginary=True).is_real

\(False\)

x = sympy.Symbol("x")
y = sympy.Symbol("y", positive=True) 
sympy.sqrt(x ** 2) 

\(\displaystyle \sqrt{x^{2}}\)

sympy.sqrt(y ** 2) 

\(y\)

n1 = sympy.Symbol("n") 
n2 = sympy.Symbol("n", integer=True)
n3 = sympy.Symbol("n", odd=True) 
sympy.cos(n1 * pi) 

\(\displaystyle \cos{\left(\pi n \right)}\)

sympy.cos(n2 * pi) 

\(\displaystyle \left(-1\right)^{n}\)

sympy.cos(n3 * pi) 

\(−1\)

a, b, c = sympy.symbols("a, b, c", negative=True) 
d, e, f = sympy.symbols("d, e, f", positive=True)




numbers





####




####




####




####




####





Expressions

Analytical function definition

from sympy import *

x = symbols("x")
y = lambda x : x**2 + x + 1

print(y(x))
x**2 + x + 1




Numerical function definition

from sympy import *
import numpy as np

x = symbols("x")
y = lambdify(x, x**2 + x + 1)
X = np.mgrid[-1:1:10j]

print(y(x))
print(y(X))
x**2 + x + 1
[1.         0.82716049 0.75308642 0.77777778 0.90123457 1.12345679
 1.44444444 1.86419753 2.38271605 3.        ]
for special function
from sympy import *
import numpy as np

n, x = symbols("n, x")
y = lambdify((n,x), legendre(n, x))

X = np.mgrid[-1:1:10j]

print(y(5, X))

If you just only define the fucntion,y = lambda n,x : legendre(n,x), and then error is happen.






Manipulating expressions

simpification





expand





factor, collect, and combine





apart, tpgether, and cancel





substitutions





Numerical evaluation


Calculus

derivatives





interals





series





limits





sums and products





Equations


Linear algebra


Graph plot

import sympy
from sympy.plotting import plot
from sympy.plotting import plot_parametric
from sympy import plot_implicit
from sympy.plotting import plot3d
from sympy.plotting import plot3d_parametric_line
from sympy.plotting import plot3d_parametric_surface




function plot

one figure function plot

import sympy
from sympy.plotting import plot

# simplt plot
# plot(y)
x = sympy.symbols('x')
p1 = plot(x*x, show=False)
p1.show()
set range
import sympy
from sympy.plotting import plot

# simplt plot
# plot(y)
x = sympy.symbols('x')
p1 = plot(x*x, xlim=[-1,1], ylim=[-1,1], show=False)
p1.show()

다운로드 (3)

import sympy
from sympy.plotting import plot

# setting range
# plot(y, x_range)
x = sympy.symbols('x')
p1 = plot(x*x, (x,-20,20), show=False)
p1.show()

다운로드 (4)


two figure function plot

import sympy
from sympy.plotting import plot

# simple plot
# plot(y1, y2)
x = sympy.symbols('x')
p1 = plot(x*x, x, show=False)
p1.show()

다운로드 (5)

import sympy
from sympy.plotting import plot

x = sympy.symbols('x')
p1 = plot(x*x, x, legend=True ,show=False)
p1[0].line_color = 'red'
p1[0].label = '$x^{2}$'
p1[1].line_color = 'green'
p1[1].label = '$x$'

p1.show()

다운로드 (6)

import sympy
from sympy.plotting import plot

# setting range
# plot((y1,x1_range), (y2,x2_range))
x = sympy.symbols('x')
p1 = plot((x*x,(x,-10,10)), (x,(x,-10,20)), show=False)
p1.show()

다운로드 (7)

import sympy
from sympy.plotting import plot

# plot using append
x = sympy.symbols('x')
p1 = plot(x*x, show=False)
p2 = plot(x, show=False)
p1.append(p2[0])
p1.show()

다운로드 (8)


three figure function plot

import sympy
from sympy.plotting import plot

# simple plot
# plot(y1, y2, y3)
x = sympy.symbols('x')
p1 = plot(x, sympy.sin(x), sympy.cos(x),show=False)
p1.show()

다운로드 (9)

import sympy
from sympy.plotting import plot

# setting range
# plot((y1,x1_range), (y2,x2_range), (y3,x3_range))
x = sympy.symbols('x')
p1 = plot((x,(x,-5,5)), (sympy.sin(x),(x,-5,5)), (sympy.cos(x),(x,-5,5)),show=False)
p1.show()

다운로드 (10)

import sympy
from sympy.plotting import plot

# plot using extend
x = sympy.symbols('x')
p1 = plot(x*x, show=False)
p2 = plot(x,-x, show=False)
p1.extend(p2)
p1.show()

다운로드 (11)



parametric plot

single parametric plot

import sympy
from sympy.plotting import plot_parametric

# simple plot
# plot_parametric(x,y)
u = sympy.symbols('u')
plot_parametric(sympy.cos(u), sympy.sin(u))

다운로드 (12)

import sympy
from sympy.plotting import plot_parametric

# setting range
# plot_parametric(x,y,range)
u = sympy.symbols('u')
plot_parametric(sympy.cos(u), sympy.sin(u), (u,-3,3))

다운로드 (13)


mutiple parametric plot

import sympy
from sympy.plotting import plot_parametric

# simple plot
# plot_parametric((x,y),(x,y))
u = sympy.symbols('u')
plot_parametric((sympy.cos(u), sympy.sin(u)), (u, sympy.cos(u)))

다운로드 (14)

import sympy
from sympy.plotting import plot_parametric

# setting range
# plot_parametric((x,y,range),(x,y,range))
u = sympy.symbols('u')
plot_parametric((sympy.cos(u), sympy.sin(u),(u,-5,5)),
                (u, sympy.cos(u),(u,-5,5)))

다운로드 (15)



implicit plot

from sympy import symbols, Eq
from sympy import plot_implicit

x, y = symbols('x y')
plot_implicit(Eq(x**2 + y**2, 5))

download

from sympy import symbols, Eq
from sympy import plot_implicit

x, y = symbols('x y')
plot_implicit(Eq(x**2 + y**2, 3), (x, -3, 3), (y, -3, 3))

download (1)

from sympy import symbols, Eq
from sympy import plot_implicit

x, y = symbols('x y')
plot_implicit(Eq(x**2 + y**2, 5), (x, -4, 4), (y, -4, 4), depth = 2)

download (2)

from sympy import symbols, Eq
from sympy import plot_implicit

x, y = symbols('x y')
plot_implicit(Eq(x**2 + y**2, 5), (x, -5, 5), (y, -2, 2), adaptive=False)

download (3)

from sympy import symbols, Eq
from sympy import plot_implicit

x, y = symbols('x y')
 plot_implicit(Eq(x**2 + y**2, 5), (x, -5, 5), (y, -2, 2), adaptive=False, points=400)

download (4)

from sympy import symbols
from sympy import plot_implicit

x, y = symbols('x y')
plot_implicit(y > x**2)

download (5)

from sympy import symbols, And
from sympy import plot_implicit

x, y = symbols('x y')
plot_implicit(And(y > x, y > -x))

download (6)

from sympy import symbols
from sympy import plot_implicit

x, y = symbols('x y')
plot_implicit(y - 1, y_var=y)

download (7)

from sympy import symbols
from sympy import plot_implicit

x, y = symbols('x y')
plot_implicit(x - 1, x_var=x)

download (8)



3d function plot

one figure 3d function plot

import sympy
from sympy.plotting import plot3d

# simple plot
# plot3d(z)
x,y = sympy.symbols('x,y')
plot3d(x*y)

다운로드 (16)

import sympy
from sympy.plotting import plot3d

# setting range
# plot(z, x_range, y_range)
x,y = sympy.symbols('x,y')
plot3d(x*y,(x,-5,5),(y,-5,5))

다운로드 (17)


two figure 3d function plot

import sympy
from sympy.plotting import plot3d

# simple plot
# plot3d(z1,z2)
x,y = sympy.symbols('x,y')
plot3d(x*y,-x*y)

다운로드 (18)

import sympy
from sympy.plotting import plot3d

# setting range
# plot(z1, z2, x_range, y_range), common range for z1 and z2
x,y = sympy.symbols('x,y')
plot3d(x*y, -x*y, (x,-5,5), (y,-5,5))

다운로드 (19)

import sympy
from sympy.plotting import plot3d

# setting range
# plot((z1, x1_range, y1_range), (z2, x2_range, y2_range))
x,y = sympy.symbols('x,y')
plot3d((x*y, (x,-3,3), (y,-3,3)),(x**2 + y**2, (x,-5,5), (y,-5,5)))

다운로드 (20)



parametric 3d plot

single parametric 3d line plot

import sympy
from sympy.plotting import plot3d_parametric_line

# simple plot
# plot3d_parametric_line(x,y,z)
u = sympy.symbols('u')
plot3d_parametric_line(sympy.cos(u), sympy.sin(u), u)

다운로드 (21)

import sympy
from sympy.plotting import plot3d_parametric_line

# setting range
# plot3d_parametric_line(x,y,z, range)
u = sympy.symbols('u')
plot3d_parametric_line(sympy.cos(u), sympy.sin(u), u, (u, -5, 5))

다운로드 (22)


multiple parametric 3d line plot

import sympy
from sympy.plotting import plot3d_parametric_line

# setting range
# plot_parametric_line((x,y,z,range),(x,y,z,range))
u = sympy.symbols('u')
plot3d_parametric_line((sympy.cos(u),sympy.sin(u),u,(u,-5,5)), 
                       (sympy.sin(u),u**2,u,(u,-5,5)))

다운로드 (23)


single parametric 3d surface plot

import sympy
from sympy.plotting import plot3d_parametric_surface

# simple plot
# plot3d_parametric_surfcae(x,y,z)
u, v = sympy.symbols('u, v')
plot3d_parametric_surface(sympy.cos(u+v), sympy.sin(u-v), u-v)

다운로드 (24)

import sympy
from sympy.plotting import plot3d_parametric_surface

# setting range
# plot3d_parametric_surfcae(x,y,z,u_range,v_range)
u, v = sympy.symbols('u, v')
plot3d_parametric_surface(sympy.cos(u+v), sympy.sin(u-v), u-v, (u,-5,5), (v,-5,5))

다운로드 (25)


convert sympy-plot(analytic) to matplotlib-plot(numerical)

"""sympy plot  >>> matplotlib plot"""

from sympy import *
import matplotlib.pyplot as plt
import numpy as np

x = symbols("x")
y = lambdify(x, x**2 + x + 1)
X = np.mgrid[-1:1:20j]

plt.plot(X, y(X))
plt.show()





List of posts followed by this article


Reference


OUTPUT