In [1]:
from __future__ import division
In [2]:
5 / 2
Out[2]:
In [3]:
from sympy import Rational
Rational(5,2)
Out[3]:
In [4]:
from sympy import S
S?
In [8]:
type(S(5))
Out[8]:
In [9]:
S(5)/2
Out[9]:
In [14]:
S('13/2') + S(5)/7
Out[14]:
In [15]:
from sympy import arg,re,im,I
In [16]:
a = 3 + 5*I
In [18]:
re(a)
Out[18]:
In [19]:
im(a)
Out[19]:
In [22]:
arg(a).n()
Out[22]:
In [23]:
abs(a)
Out[23]:
In [24]:
from sympy import symbols
In [28]:
x3,x4,x5,x6,x7 = symbols('x3:8')
In [29]:
x3 + x4+ 6 *x7
Out[29]:
In [ ]:
Importer toutes les fonctions de Sympy:
In [30]:
from sympy import *
init_printing(pretty_print=True, use_latex='mathjax')
Importer quelques variables du module abc:
In [31]:
#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$
In [35]:
eq1 = Eq(x, 3)
Définir l'équation $x^2+y^2=16$
In [37]:
eq2 = Eq(x**2+y**2, 16)
In [43]:
solve(eq1, x)
Out[43]:
In [40]:
solve(eq2, y)
Out[40]:
Résoudre l'équation $x^2+2x=4$
In [44]:
solve(Eq(x**2+2*x, 4))
Out[44]:
In [ ]:
Résoudre le système $x+y=34$, $xy=34$
In [47]:
eq1 = Eq(x+y, 34)
eq2 = Eq(x*y, 34)
systeme = [eq1, eq2]
solve(systeme)
Out[47]:
Résoudre le système $x+y=34$, $xy^2=34$
In [48]:
eq1 = Eq(x+y, 34)
eq2 = Eq(x*y**2, 34)
systeme = [eq1, eq2]
solve(systeme)
Out[48]:
In [ ]:
Résoudre les systèmes ci-haut avec la syntaxe abrégée
In [49]:
solve([x+y-34, x*y-34])
Out[49]:
In [50]:
roots(3*x**2+52*x-265)
Out[50]:
In [52]:
r1,r2,r3 = roots(3*x**3+52*x-265)
In [54]:
r1.n(), r2.n(), r3.n()
Out[54]:
In [55]:
%matplotlib inline
In [71]:
f = x**2
plot(x**2, (x,-20,20))
Out[71]:
In [63]:
g = (3*x**2+52*x-265) / ((x-7)*(x-1)*(x+34))
plot(g, (x,-40,10), xlim=(-40, 10), ylim=(-2,2))
Out[63]:
Approximer les racines de $$x^3 + 2x^2 - 1$$ avec un dessin
In [69]:
plot(x**3+2*x**2-1, (x,-2,1), ylim=(-1,1))
Out[69]:
In [70]:
roots(x**3+2*x**2-1)
Out[70]:
Tracer $f(x)$ en vert et $g(x)$ en jaune dans le même dessin
In [72]:
plot(f, g)
Out[72]:
In [73]:
from sympy.plotting import plot3d
In [74]:
j
Out[74]:
In [75]:
plot3d(x**2-y**2)
Out[75]:
In [76]:
plot3d((x**2-y**2)*sin(x))
Out[76]:
In [77]:
from sympy.plotting import plot_parametric
In [78]:
plot_parametric(sin(t), cos(t))
Out[78]:
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
In [80]:
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)
Out[80]:
In [81]:
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)
Out[81]:
In [ ]:
In [82]:
from sympy.plotting import plot3d_parametric_line
In [83]:
x = cos(u)
y = sin(u)
z = u
plot3d_parametric_line(x,y,z)
Out[83]:
In [85]:
x = u*cos(u)
y = u*sin(u)
z = u
plot3d_parametric_line(x,y,z)
Out[85]:
In [86]:
from sympy.plotting import plot3d_parametric_surface
In [87]:
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))
Out[87]:
In [ ]:
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$$
In [ ]:
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$$
In [ ]:
In [90]:
#from sympy import mpmath # Sympy (installation normale)
import mpmath # SageMath
Tracer la fonction complexe identité $$f(z)=z$$
In [92]:
from mpmath import cplot
In [93]:
cplot(lambda z:z)
Tracer la fonction complexe $$f(z)=z^5-1$$
In [94]:
cplot(lambda z:z**5-1)
In [97]:
from sympy.abc import x
In [101]:
limit(1/x, x, 0, dir='+')
Out[101]:
In [103]:
oo
Out[103]:
In [104]:
limit(1/x, x, oo)
Out[104]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: