from __future__ import division
Quelques ajouts dans les sections déjà vues...
Une autre façon de créer un nombre rationnel
5 / 2
2.5
from sympy import Rational
Rational(5,2)
5/2
from sympy import S
S?
type(S(5))
sympy.core.numbers.Integer
S(5)/2
5/2
S('13/2') + S(5)/7
101/14
from sympy import arg,re,im,I
a = 3 + 5*I
re(a)
3
im(a)
5
arg(a).n()
1.03037682652431
abs(a)
sqrt(34)
from sympy import symbols
x3,x4,x5,x6,x7 = symbols('x3:8')
x3 + x4+ 6 *x7
x3 + x4 + 6*x7
Importer toutes les fonctions de Sympy:
from sympy import *
init_printing(pretty_print=True, use_latex='mathjax')
Importer quelques variables du module abc:
#from sympy.abc import * # à éviter
from sympy.abc import a,b,c,n,t,u,v,w,x,y,z
Définir l'équation $x=3$
eq1 = Eq(x, 3)
Définir l'équation $x^2+y^2=16$
eq2 = Eq(x**2+y**2, 16)
solve(eq1, x)
solve(eq2, y)
Résoudre l'équation $x^2+2x=4$
solve(Eq(x**2+2*x, 4))
Résoudre le système $x+y=34$, $xy=34$
eq1 = Eq(x+y, 34)
eq2 = Eq(x*y, 34)
systeme = [eq1, eq2]
solve(systeme)
Résoudre le système $x+y=34$, $xy^2=34$
eq1 = Eq(x+y, 34)
eq2 = Eq(x*y**2, 34)
systeme = [eq1, eq2]
solve(systeme)
Résoudre les systèmes ci-haut avec la syntaxe abrégée
solve([x+y-34, x*y-34])
roots(3*x**2+52*x-265)
r1,r2,r3 = roots(3*x**3+52*x-265)
r1.n(), r2.n(), r3.n()
%matplotlib inline
f = x**2
plot(x**2, (x,-20,20))
<sympy.plotting.plot.Plot at 0x1140a8090>
g = (3*x**2+52*x-265) / ((x-7)*(x-1)*(x+34))
plot(g, (x,-40,10), xlim=(-40, 10), ylim=(-2,2))
<sympy.plotting.plot.Plot at 0x11393ee90>
Approximer les racines de $$x^3 + 2x^2 - 1$$ avec un dessin
plot(x**3+2*x**2-1, (x,-2,1), ylim=(-1,1))
<sympy.plotting.plot.Plot at 0x113d2e110>
roots(x**3+2*x**2-1)
Tracer $f(x)$ en vert et $g(x)$ en jaune dans le même dessin
plot(f, g)
<sympy.plotting.plot.Plot at 0x1142bbe50>
from sympy.plotting import plot3d
j
<sympy.plotting.plot.Plot at 0x11435b990>
plot3d(x**2-y**2)
<sympy.plotting.plot.Plot at 0x114497d50>
plot3d((x**2-y**2)*sin(x))
<sympy.plotting.plot.Plot at 0x114599f90>
from sympy.plotting import plot_parametric
plot_parametric(sin(t), cos(t))
<sympy.plotting.plot.Plot at 0x114a65810>
Butterfly curve (transcendental)
$$x = \sin(t) \left(e^{\cos(t)} - 2\cos(4t) - \sin^5\left({t \over 12}\right)\right)$$$$y = \cos(t) \left(e^{\cos(t)} - 2\cos(4t) - \sin^5\left({t \over 12}\right)\right)$$Voir: https://en.wikipedia.org/wiki/Butterfly_curve_%28transcendental%29
x = sin(t)*(E**cos(t)-2*cos(4*t)-sin(t/12)**5)
y = cos(t)*(E**cos(t)-2*cos(4*t)-sin(t/12)**5)
plot_parametric(x, y)
<sympy.plotting.plot.Plot at 0x114b6c610>
from IPython.display import Image
url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/f/f5/Param_02.jpg/800px-Param_02.jpg'
Image(url=url, width=500)
from sympy.plotting import plot3d_parametric_line
x = cos(u)
y = sin(u)
z = u
plot3d_parametric_line(x,y,z)
<sympy.plotting.plot.Plot at 0x11202a850>
x = u*cos(u)
y = u*sin(u)
z = u
plot3d_parametric_line(x,y,z)
<sympy.plotting.plot.Plot at 0x1155a5f10>
from sympy.plotting import plot3d_parametric_surface
from sympy.abc import u,v
R = 5
r = 2
X = cos(u)*(R+r*cos(v))
Y = sin(u)*(R+r*cos(v))
Z = r*sin(v)
plot3d_parametric_surface(X, Y, Z, (u, -.5, 4), (v, -5, 5))
<sympy.plotting.plot.Plot at 0x115568a90>
from sympy.abc import theta,phi
from sympy import pi,cos,sin
x = cos(theta)*sin(phi)
y = sin(theta)*sin(phi)
z = cos(theta)
plot3d_parametric_surface(x,y,z,(theta,0,pi), (phi,0,pi))
Tracer les solutions de l'équation $$x^2+y^2+xy-2x =5$$
from sympy import plot_implicit
eq = Eq(x**2+y**2+x*y-2*x, 5)
plot_implicit(eq)
Tracer la région délimitée par $$y>2x+1, y<5x, x+y<5$$
#from sympy import mpmath # Sympy (installation normale)
import mpmath # SageMath
Tracer la fonction complexe identité $$f(z)=z$$
from mpmath import cplot
cplot(lambda z:z)
Tracer la fonction complexe $$f(z)=z^5-1$$
cplot(lambda z:z**5-1)
from sympy.abc import x
limit(1/x, x, 0, dir='+')
oo
limit(1/x, x, oo)