import sympy
from sympy import I, pi, oo
sympy.init_printing()
from IPython.display import display
print("sympy: ", sympy.__version__)
sympy: 1.12
\(~\)
\(~\)
import sympy
from sympy import I, pi, oo
sympy.init_printing()
from IPython.display import display
print("sympy: ", sympy.__version__)
sympy: 1.12
= sympy.Symbol('x')
x print(x.is_real)
None
= sympy.Symbol('y', real=True)
y y.is_real
True
= sympy.Symbol('z', complex=True)
z z.is_complex
True
print(z.is_real)
None
= sympy.Symbol('x')
x = sympy.Symbol('y', positive=True)
y = sympy.Symbol('z', negative=True) z
**2) sympy.sqrt(x
\(\displaystyle \sqrt{x^{2}}\)
**2) sympy.sqrt(y
\(\displaystyle y\)
**2) sympy.sqrt(z
\(\displaystyle - z\)
= sympy.Symbol('n')
n1 = sympy.Symbol('n', integer=True)
n2 = sympy.Symbol('n', odd=True)
n3 = sympy.Symbol('n', even=True) n4
*pi) sympy.cos(n1
\(\displaystyle \cos{\left(\pi n \right)}\)
*pi) sympy.cos(n2
\(\displaystyle \left(-1\right)^{n}\)
*pi) sympy.cos(n3
\(\displaystyle -1\)
*pi) sympy.cos(n4
\(\displaystyle 1\)
= sympy.symbols('a, b, c', negative=True) a, b, c
= sympy.symbols('d, e, f', positive=True) d, e, f
We cannot directly use the built-in Python objects for integers, int
, and floating-point numbers, float
, and so on. Instead, sympy
provides the classes sympy.Integer
and sympy.Float
for representing integers and floating-point numbers within the sympy
framework
This distinction is important to be aware of when working with sympy
, but fortunately we rarely need to concern ourselves with creating objects of type sympy.Integer
and sympy.Float
to representing specific numbers, \(~\)since sympy
automatically promotes Python numbers to instances of these classes when they occur in sympy
expressions
= sympy.Integer(18)
i type(i)
sympy.core.numbers.Integer
i.is_Integer, i.is_real, i.is_odd, i.is_even
(True, True, False, True)
= sympy.Float(2.3)
f type(f)
sympy.core.numbers.Float
f.is_Float, f.is_real, f.is_complex
(True, True, True)
= sympy.sympify(19), sympy.sympify(2.3)
i, f type(i), type(f)
(sympy.core.numbers.Integer, sympy.core.numbers.Float)
While the Symbol
with integer=True
represents some integer, \(\,\)the Integer
instance represents a specific integer
For both cases, \(\,\)the is_integer
attribute is True
, \(\,\)but there is also an attribute is_Integer
(note the capital I
), \(\,\)which is only True
for Integer
instances
= sympy.Symbol('n', integer=True)
n n.is_integer, n.is_Integer, n.is_positive, n.is_Symbol
(True, False, None, True)
= sympy.Integer(19)
i i.is_integer, i.is_Integer, i.is_positive, i.is_Symbol
(True, True, True, False)
'19' *20) sympy.Integer(
\(\displaystyle 1919191919191919191919191919191919191919\)
'12_345_678'), sympy.Integer(12_345_678) sympy.Integer(
\(\displaystyle \left( 12345678, \ 12345678\right)\)
# great common division, leat common multiple
36, 15), sympy.ilcm(7, 34) sympy.igcd(
\(\displaystyle \left( 3, \ 238\right)\)
sympy
are arbitrary precision, \(\,\)meaning that they have no fixed lower and upper bounds, \(\,\)which is the case when representing integers with a specific bit-size, as, for example, in numpy
= sympy.Integer(19)
i **100 i
\(\displaystyle 75051624198251984443456989853061891539043939434909537798332873934101480896578056472849915762891214746171016655874432115640378001\)
100) sympy.factorial(
\(\displaystyle 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000\)
Like Integer
, \(\,\)Float
is arbitrary precision, \(\,\)in contrast to Python’s built-in float
type and the float
types in numpy
. \(\,\)This means that any Float
can represent a float
with arbitrary number of decimals
When a Float
instance is created using its constructor, \(\,\)there are two arguments: the first argument is a Python float
or a string representing a floating-point number, and the second (optional) argument is the precision (number of significant decimal digits) of the Float
object
# create a string representation with 25 decimals
f'{0.3:.25f}'
'0.2999999999999999888977698'
0.3, 25) sympy.Float(
\(\displaystyle 0.2999999999999999888977698\)
'0.3', 25) sympy.Float(
\(\displaystyle 0.3\)
'123 456 789.123_456', '') sympy.Float(
\(\displaystyle 123456789.123456\)
= sympy.Rational(11, 13)
r0 r0
\(\displaystyle \frac{11}{13}\)
r0.p, r0.q
\(\displaystyle \left( 11, \ 13\right)\)
= sympy.Rational(2, 3)
r1 = sympy.Rational(4, 5) r2
* r2 r1
\(\displaystyle \frac{8}{15}\)
/ r2 r1
\(\displaystyle \frac{5}{6}\)
0.2), sympy.Rational('0.2') sympy.Rational(
\(\displaystyle \left( \frac{3602879701896397}{18014398509481984}, \ \frac{1}{5}\right)\)
\(\displaystyle \gamma = \lim_{n \to \infty} \left( \sum_{k=1}^n \frac{1}{k} -\ln n\right)\)
sympy.pi, sympy.E, sympy.EulerGamma, sympy.I, sympy.oo
\(\displaystyle \left( \pi, \ e, \ \gamma, \ i, \ \infty\right)\)
= sympy.symbols('x, y, z') x, y, z
= sympy.Function('f')
f type(f)
sympy.core.function.UndefinedFunction
f(x)
\(\displaystyle f{\left(x \right)}\)
= sympy.Function('g')(x, y, z)
g g
\(\displaystyle g{\left(x,y,z \right)}\)
g.free_symbols
\(\displaystyle \left\{x, y, z\right\}\)
sympy.sin
sin
sympy.sin(x)
\(\displaystyle \sin{\left(x \right)}\)
*1.5) sympy.sin(pi
\(\displaystyle -1\)
= sympy.Symbol('n', integer=True)
n * n) sympy.sin(pi
\(\displaystyle 0\)
= sympy.Lambda(x, x**2)
h h
\(\displaystyle \left( x \mapsto x^{2} \right)\)
5) h(
\(\displaystyle 25\)
1 + x) h(
\(\displaystyle \left(x + 1\right)^{2}\)
= sympy.Symbol('x')
x
= 1 +2*x**2 +3*x**3
expr expr
\(\displaystyle 3 x^{3} + 2 x^{2} + 1\)
expr.args
\(\displaystyle \left( 1, \ 2 x^{2}, \ 3 x^{3}\right)\)
2] expr.args[
\(\displaystyle 3 x^{3}\)
2].args expr.args[
\(\displaystyle \left( 3, \ x^{3}\right)\)
2].args[1] expr.args[
\(\displaystyle x^{3}\)
2].args[1].args expr.args[
\(\displaystyle \left( x, \ 3\right)\)
2].args[1].args[1] expr.args[
\(\displaystyle 3\)
2].args[1].args[1].args expr.args[
\(\displaystyle \left( \right)\)
= 2 * (x**2 - x) - x * (x + 1)
expr expr
\(\displaystyle 2 x^{2} - x \left(x + 1\right) - 2 x\)
sympy.simplify(expr)
\(\displaystyle x \left(x - 3\right)\)
expr.simplify()
\(\displaystyle x \left(x - 3\right)\)
expr
\(\displaystyle 2 x^{2} - x \left(x + 1\right) - 2 x\)
= 2 * sympy.cos(x) * sympy.sin(x)
expr expr
\(\displaystyle 2 \sin{\left(x \right)} \cos{\left(x \right)}\)
sympy.simplify(expr)
\(\displaystyle \sin{\left(2 x \right)}\)
= sympy.exp(x) * sympy.exp(y)
expr expr
\(\displaystyle e^{x} e^{y}\)
sympy.simplify(expr)
\(\displaystyle e^{x + y}\)
= (x + 1) * (x + 2)
expr expr
\(\displaystyle \left(x + 1\right) \left(x + 2\right)\)
expr.expand()
\(\displaystyle x^{2} + 3 x + 2\)
+ y).expand(trig=True) sympy.sin(x
\(\displaystyle \sin{\left(x \right)} \cos{\left(y \right)} + \sin{\left(y \right)} \cos{\left(x \right)}\)
= sympy.symbols('a, b', positive=True)
a, b * b).expand(log=True) sympy.log(a
\(\displaystyle \log{\left(a \right)} + \log{\left(b \right)}\)
*a + b).expand(complex=True) sympy.exp(I
\(\displaystyle i e^{b} \sin{\left(a \right)} + e^{b} \cos{\left(a \right)}\)
* b)**x, power_base=True) sympy.expand((a
\(\displaystyle a^{x} b^{x}\)
*(a -b)*x).expand(power_exp=True) sympy.exp(I
\(\displaystyle e^{i a x} e^{- i b x}\)
**2 - 1) sympy.factor(x
\(\displaystyle \left(x - 1\right) \left(x + 1\right)\)
*sympy.cos(y) + x *sympy.sin(z)) sympy.factor(x
\(\displaystyle x \left(\sin{\left(z \right)} + \cos{\left(y \right)}\right)\)
= x + y + x * y * z
expr expr
\(\displaystyle x y z + x + y\)
expr.collect(x)
\(\displaystyle x \left(y z + 1\right) + y\)
expr.collect(y)
\(\displaystyle x + y \left(x z + 1\right)\)
= sympy.cos(x + y) + sympy.sin(x - y) expr
= expr.expand(trig=True)
expr1 expr1
\(\displaystyle - \sin{\left(x \right)} \sin{\left(y \right)} + \sin{\left(x \right)} \cos{\left(y \right)} - \sin{\left(y \right)} \cos{\left(x \right)} + \cos{\left(x \right)} \cos{\left(y \right)}\)
= expr1.collect([sympy.cos(x), sympy.sin(x)])
expr2 expr2
\(\displaystyle \left(- \sin{\left(y \right)} + \cos{\left(y \right)}\right) \sin{\left(x \right)} + \left(- \sin{\left(y \right)} + \cos{\left(y \right)}\right) \cos{\left(x \right)}\)
= expr2.collect(sympy.cos(y) - sympy.sin(y))
expr3 expr3
\(\displaystyle \left(\sin{\left(x \right)} + \cos{\left(x \right)}\right) \left(- \sin{\left(y \right)} + \cos{\left(y \right)}\right)\)
- sympy.log(b)) sympy.logcombine(sympy.log(a)
\(\displaystyle \log{\left(\frac{a}{b} \right)}\)
= 1/(x**2 + 3*x + 2)
expr1 expr1
\(\displaystyle \frac{1}{x^{2} + 3 x + 2}\)
sympy.apart(expr1, x)
\(\displaystyle - \frac{1}{x + 2} + \frac{1}{x + 1}\)
= 1 / (y * x + y) + 1 / (1+x)
expr2 expr2
\(\displaystyle \frac{1}{x y + y} + \frac{1}{x + 1}\)
sympy.together(expr2)
\(\displaystyle \frac{y + 1}{y \left(x + 1\right)}\)
= y / (y * x + y)
expr3 expr3
\(\displaystyle \frac{y}{x y + y}\)
sympy.cancel(expr3)
\(\displaystyle \frac{1}{x + 1}\)
+ y).subs(x, y) (x
\(\displaystyle 2 y\)
* sympy.exp(x)).subs(x, y) sympy.sin(x
\(\displaystyle \sin{\left(y e^{y} \right)}\)
* z).subs({z: sympy.exp(y), x: y, sympy.sin: sympy.cos}) sympy.sin(x
\(\displaystyle \cos{\left(y e^{y} \right)}\)
= x * y + z**2 *x
expr expr
\(\displaystyle x y + x z^{2}\)
= {x: 1.25, y: 0.4, z: 3.2}
values expr.subs(values)
\(\displaystyle 13.3\)
1 + pi) sympy.N(
\(\displaystyle 4.14159265358979\)
1 + pi, 50) sympy.N(
\(\displaystyle 4.1415926535897932384626433832795028841971693993751\)
+ 1/pi).evalf(10) (x
\(\displaystyle x + 0.3183098862\)
= sympy.sin(pi * x * sympy.exp(x))
expr expr
\(\displaystyle \sin{\left(\pi x e^{x} \right)}\)
3) for i in range(0, 10)] # rather slow [expr.subs(x, i).evalf(
\(\displaystyle \left[ 0, \ 0.774, \ 0.642, \ 0.722, \ 0.944, \ 0.205, \ 0.974, \ 0.977, \ -0.87, \ -0.695\right]\)
= sympy.lambdify(x, expr)
expr_func 1.0) expr_func(
\(\displaystyle 0.773942685266709\)
import numpy as np
= np.arange(0, 10)
xvalues
= sympy.lambdify(x, expr, 'numpy')
expr_func # efficient method expr_func(xvalues)
array([ 0. , 0.77394269, 0.64198244, 0.72163867, 0.94361635,
0.20523391, 0.97398794, 0.97734066, -0.87034418, -0.69512687])
= sympy.Function('f')(x)
f # equivalent to f.diff(x) sympy.diff(f, x)
\(\displaystyle \frac{d}{d x} f{\left(x \right)}\)
sympy.diff(f, x, x)
\(\displaystyle \frac{d^{2}}{d x^{2}} f{\left(x \right)}\)
3) # equivalent to sympy.diff(f, x, x, x) sympy.diff(f, x,
\(\displaystyle \frac{d^{3}}{d x^{3}} f{\left(x \right)}\)
= sympy.Function('g')(x, y)
g # equivalent to sympy.diff(g, x, y) g.diff(x, y)
\(\displaystyle \frac{\partial^{2}}{\partial y\partial x} g{\left(x,y \right)}\)
3, y, 2) # equivalent to sympy.diff(g, x, x, x, y, y) g.diff(x,
\(\displaystyle \frac{\partial^{5}}{\partial y^{2}\partial x^{3}} g{\left(x,y \right)}\)
= x**4 + x**3 + x**2 + x + 1
expr expr
\(\displaystyle x^{4} + x^{3} + x^{2} + x + 1\)
expr.diff(x)
\(\displaystyle 4 x^{3} + 3 x^{2} + 2 x + 1\)
expr.diff(x, x)
\(\displaystyle 2 \cdot \left(6 x^{2} + 3 x + 1\right)\)
= (x + 1)**3 * y ** 2 * (z - 1)
expr expr
\(\displaystyle y^{2} \left(x + 1\right)^{3} \left(z - 1\right)\)
expr.diff(x, y, z)
\(\displaystyle 6 y \left(x + 1\right)^{2}\)
= sympy.sin(x * y) * sympy.cos(x / 2)
expr expr
\(\displaystyle \sin{\left(x y \right)} \cos{\left(\frac{x}{2} \right)}\)
expr.diff(x)
\(\displaystyle y \cos{\left(\frac{x}{2} \right)} \cos{\left(x y \right)} - \frac{\sin{\left(\frac{x}{2} \right)} \sin{\left(x y \right)}}{2}\)
= sympy.functions.special.polynomials.hermite(x, 0)
expr expr
\(\displaystyle \frac{2^{x} \sqrt{\pi}}{\Gamma\left(\frac{1}{2} - \frac{x}{2}\right)}\)
expr.diff(x)
\(\displaystyle \frac{2^{x} \sqrt{\pi} \operatorname{polygamma}{\left(0,\frac{1}{2} - \frac{x}{2} \right)}}{2 \Gamma\left(\frac{1}{2} - \frac{x}{2}\right)} + \frac{2^{x} \sqrt{\pi} \log{\left(2 \right)}}{\Gamma\left(\frac{1}{2} - \frac{x}{2}\right)}\)
= sympy.Derivative(sympy.exp(sympy.cos(x)), x)
d d
\(\displaystyle \frac{d}{d x} e^{\cos{\left(x \right)}}\)
d.doit()
\(\displaystyle - e^{\cos{\left(x \right)}} \sin{\left(x \right)}\)
= sympy.symbols('a, b, x, y')
a, b, x, y = sympy.Function('f')(x)
f sympy.integrate(f)
\(\displaystyle \int f{\left(x \right)}\, dx\)
sympy.integrate(f, (x, a, b))
\(\displaystyle \int\limits_{a}^{b} f{\left(x \right)}\, dx\)
sympy.integrate(sympy.sin(x))
\(\displaystyle - \cos{\left(x \right)}\)
sympy.integrate(sympy.sin(x), (x, a, b))
\(\displaystyle \cos{\left(a \right)} - \cos{\left(b \right)}\)
-x**2), (x, 0, oo)) sympy.integrate(sympy.exp(
\(\displaystyle \frac{\sqrt{\pi}}{2}\)
= sympy.symbols('a, b, c', positive=True)
a, b, c * sympy.exp(-((x -b)/c)**2), (x, -oo, oo)) sympy.integrate(a
\(\displaystyle \sqrt{\pi} a c\)
* sympy.cos(x))) # No analytic integration sympy.integrate(sympy.sin(x
\(\displaystyle \int \sin{\left(x \cos{\left(x \right)} \right)}\, dx\)
= sympy.sin(x*sympy.exp(y))
expr expr
\(\displaystyle \sin{\left(x e^{y} \right)}\)
sympy.integrate(expr, x)
\(\displaystyle - e^{- y} \cos{\left(x e^{y} \right)}\)
= (x + y)**2
expr expr
\(\displaystyle \left(x + y\right)^{2}\)
sympy.integrate(expr, x)
\(\displaystyle \frac{x^{3}}{3} + x^{2} y + x y^{2}\)
sympy.integrate(expr, x, y)
\(\displaystyle \frac{x^{3} y}{3} + \frac{x^{2} y^{2}}{2} + \frac{x y^{3}}{3}\)
0, 1), (y, 0, 1)) sympy.integrate(expr, (x,
\(\displaystyle \frac{7}{6}\)
/ x, x, 0) sympy.limit(sympy.sin(x)
\(\displaystyle 1\)
= sympy.symbols('x, h')
x, h = sympy.Function('f')
f = (f(x + h) - f(x)) / h diff_limit
0) sympy.limit(diff_limit.subs(f, sympy.cos), h,
\(\displaystyle - \sin{\left(x \right)}\)
0) sympy.limit(diff_limit.subs(f, sympy.sin), h,
\(\displaystyle \cos{\left(x \right)}\)
= (x**2 - 3*x) / (2*x - 2)
expr expr
\(\displaystyle \frac{x^{2} - 3 x}{2 x - 2}\)
= sympy.limit(expr/x, x, sympy.oo) p
= sympy.limit(expr - p*x, x, sympy.oo) q
p, q
\(\displaystyle \left( \frac{1}{2}, \ -1\right)\)
= sympy.symbols('n', integer=True)
n = sympy.Sum(1/(n**2), (n, 1, oo))
x x
\(\displaystyle \sum_{n=1}^{\infty} \frac{1}{n^{2}}\)
x.doit()
\(\displaystyle \frac{\pi^{2}}{6}\)
= sympy.Product(n, (n, 1, 7))
x x
\(\displaystyle \prod_{n=1}^{7} n\)
x.doit()
\(\displaystyle 5040\)
= sympy.Symbol('x')
x **n/(sympy.factorial(n)), (n, 1, oo)).doit().simplify() sympy.Sum((x)
\(\displaystyle e^{x} - 1\)
= sympy.Symbol('x')
x **2 +2*x -3) sympy.solve(x
\(\displaystyle \left[ -3, \ 1\right]\)
= sympy.symbols('a, b, c')
a, b, c *x**2 +b *x +c, x) sympy.solve(a
\(\displaystyle \left[ \frac{- b - \sqrt{- 4 a c + b^{2}}}{2 a}, \ \frac{- b + \sqrt{- 4 a c + b^{2}}}{2 a}\right]\)
- sympy.cos(x), x) sympy.solve(sympy.sin(x)
\(\displaystyle \left[ \frac{\pi}{4}\right]\)
+ 2 *x, x) sympy.solve(sympy.exp(x)
\(\displaystyle \left[ - W\left(\frac{1}{2}\right)\right]\)
The value of LambertW
function \(W(z)\) is such that \(z = W(z)\exp(W(z))\) for any complex number \(z\)
-sympy.LambertW(1/2)
\(\displaystyle -0.351733711249196\)
= sympy.solve(x**5 - x**2 + 1, x)
sols for i in range(5):
display(sols[i])
\(\displaystyle \operatorname{CRootOf} {\left(x^{5} - x^{2} + 1, 0\right)}\)
\(\displaystyle \operatorname{CRootOf} {\left(x^{5} - x^{2} + 1, 1\right)}\)
\(\displaystyle \operatorname{CRootOf} {\left(x^{5} - x^{2} + 1, 2\right)}\)
\(\displaystyle \operatorname{CRootOf} {\left(x^{5} - x^{2} + 1, 3\right)}\)
\(\displaystyle \operatorname{CRootOf} {\left(x^{5} - x^{2} + 1, 4\right)}\)
#sympy.solve(sympy.tan(x) + x, x)
NotImplementedError: multiple generators [x, tan(x)]
No algorithms are implemented to solve equation x + tan(x)
= x +2 *y -1
eq1 = x -y +1 eq2
dict=True) sympy.solve([eq1, eq2], [x, y],
\(\displaystyle \left[ \left\{ x : - \frac{1}{3}, \ y : \frac{2}{3}\right\}\right]\)
= x**2 -y
eq1 = y**2 -x eq2
= sympy.solve([eq1, eq2], [x, y], dict=True)
sols for i in range(4):
display(sols[i])
\(\displaystyle \left\{ x : 0, \ y : 0\right\}\)
\(\displaystyle \left\{ x : 1, \ y : 1\right\}\)
\(\displaystyle \left\{ x : \left(- \frac{1}{2} - \frac{\sqrt{3} i}{2}\right)^{2}, \ y : - \frac{1}{2} - \frac{\sqrt{3} i}{2}\right\}\)
\(\displaystyle \left\{ x : \left(- \frac{1}{2} + \frac{\sqrt{3} i}{2}\right)^{2}, \ y : - \frac{1}{2} + \frac{\sqrt{3} i}{2}\right\}\)
== 0 and
[eq1.subs(sol).simplify() == 0 for sol in sols] eq2.subs(sol).simplify()
[True, True, True, True]
1, 2]) sympy.Matrix([
\(\displaystyle \left[\begin{matrix}1\\2\end{matrix}\right]\)
1, 2]]) sympy.Matrix([[
\(\displaystyle \left[\begin{matrix}1 & 2\end{matrix}\right]\)
1, 2], [3, 4]]) sympy.Matrix([[
\(\displaystyle \left[\begin{matrix}1 & 2\\3 & 4\end{matrix}\right]\)
3, 4, lambda m, n: 10 * m + n) sympy.Matrix(
\(\displaystyle \left[\begin{matrix}0 & 1 & 2 & 3\\10 & 11 & 12 & 13\\20 & 21 & 22 & 23\end{matrix}\right]\)
= sympy.symbols('a, b, c, d')
a, b, c, d
= sympy.Matrix([[a, b], [c, d]])
M M
\(\displaystyle \left[\begin{matrix}a & b\\c & d\end{matrix}\right]\)
* M # Matrix multiplication M
\(\displaystyle \left[\begin{matrix}a^{2} + b c & a b + b d\\a c + c d & b c + d^{2}\end{matrix}\right]\)
= sympy.Matrix(sympy.symbols('x_1, x_2'))
x x
\(\displaystyle \left[\begin{matrix}x_{1}\\x_{2}\end{matrix}\right]\)
* x M
\(\displaystyle \left[\begin{matrix}a x_{1} + b x_{2}\\c x_{1} + d x_{2}\end{matrix}\right]\)
= sympy.symbols('p, q')
p, q = sympy.Matrix([[1, p], [q, 1]])
M M
\(\displaystyle \left[\begin{matrix}1 & p\\q & 1\end{matrix}\right]\)
= sympy.Matrix(sympy.symbols('b_1, b_2'))
b b
\(\displaystyle \left[\begin{matrix}b_{1}\\b_{2}\end{matrix}\right]\)
= M.LUsolve(b)
x x
\(\displaystyle \left[\begin{matrix}b_{1} - \frac{p \left(- b_{1} q + b_{2}\right)}{- p q + 1}\\\frac{- b_{1} q + b_{2}}{- p q + 1}\end{matrix}\right]\)
= M.inv() *b
x x
\(\displaystyle \left[\begin{matrix}\frac{b_{1}}{- p q + 1} - \frac{b_{2} p}{- p q + 1}\\- \frac{b_{1} q}{- p q + 1} + \frac{b_{2}}{- p q + 1}\end{matrix}\right]\)
= sympy.Symbol('x')
x = sympy.Function('f') f
+9 *f(x), f(x)) sympy.dsolve(sympy.Derivative(f(x), x, x)
\(\displaystyle f{\left(x \right)} = C_{1} \sin{\left(3 x \right)} + C_{2} \cos{\left(3 x \right)}\)
= (sympy.sin(x) *sympy.cos(f(x))
eq + sympy.cos(x) *sympy.sin(f(x)) *f(x).diff(x))
eq
\(\displaystyle \sin{\left(x \right)} \cos{\left(f{\left(x \right)} \right)} + \sin{\left(f{\left(x \right)} \right)} \cos{\left(x \right)} \frac{d}{d x} f{\left(x \right)}\)
='1st_exact') sympy.dsolve(eq, hint
\(\displaystyle \left[ f{\left(x \right)} = - \operatorname{acos}{\left(\frac{C_{1}}{\cos{\left(x \right)}} \right)} + 2 \pi, \ f{\left(x \right)} = \operatorname{acos}{\left(\frac{C_{1}}{\cos{\left(x \right)}} \right)}\right]\)
= sympy.Symbol('t')
t = sympy.Function('x'), sympy.Function('y')
x, y
= (sympy.Eq(sympy.Derivative(x(t),t), 12 *t *x(t) + 8 *y(t)),
eq 21 *x(t) + 7 *t *y(t)))
sympy.Eq(sympy.Derivative(y(t),t), eq
\(\displaystyle \left( \frac{d}{d t} x{\left(t \right)} = 12 t x{\left(t \right)} + 8 y{\left(t \right)}, \ \frac{d}{d t} y{\left(t \right)} = 7 t y{\left(t \right)} + 21 x{\left(t \right)}\right)\)
= sympy.dsolve(eq)
sols for i in [0, 1]:
display(sols[i])
\(\displaystyle x{\left(t \right)} = C_{1} x_{0}{\left(t \right)} + C_{2} x_{0}{\left(t \right)} \int \frac{8 \left(e^{\int 7 t\, dt}\right) e^{\int 12 t\, dt}}{x_{0}^{2}{\left(t \right)}}\, dt\)
\(\displaystyle y{\left(t \right)} = C_{1} y_{0}{\left(t \right)} + C_{2} \left(y_{0}{\left(t \right)} \int \frac{8 \left(e^{\int 7 t\, dt}\right) e^{\int 12 t\, dt}}{x_{0}^{2}{\left(t \right)}}\, dt + \frac{\left(e^{\int 7 t\, dt}\right) e^{\int 12 t\, dt}}{x_{0}{\left(t \right)}}\right)\)
= (sympy.Eq(sympy.Derivative(x(t),t), x(t) *y(t) *sympy.sin(t)),
eq **2 *sympy.sin(t)))
sympy.Eq(sympy.Derivative(y(t),t), y(t) eq
\(\displaystyle \left( \frac{d}{d t} x{\left(t \right)} = x{\left(t \right)} y{\left(t \right)} \sin{\left(t \right)}, \ \frac{d}{d t} y{\left(t \right)} = y^{2}{\left(t \right)} \sin{\left(t \right)}\right)\)
sympy.dsolve(eq)
\(\displaystyle \left\{x{\left(t \right)} = - \frac{e^{C_{1}}}{C_{2} e^{C_{1}} - \cos{\left(t \right)}}, y{\left(t \right)} = - \frac{1}{C_{1} - \cos{\left(t \right)}}\right\}\)
from sympy.plotting import plot
= sympy.symbols('x')
x = plot(x**2, (x - 1) * x * (x + 1), (x, -1.2, 1.2)) p1
from sympy.plotting import plot_parametric
= sympy.symbols('u')
u = plot_parametric((sympy.cos(u), sympy.sin(u)),
p2 -3, 3)) (u, sympy.cos(u)), (u,
= (u, sympy.cos(2 *pi *u)/2 + 1/2)
expr1 = (u, sympy.sin(2 *pi *u)/2 + 1/2)
expr2
= plot_parametric(expr1, expr2, (u, 0, 1), line_color='blue') p3
0].line_color = 'red'
p3[ p3.show()
from sympy.plotting import plot3d
= sympy.symbols('x y')
x, y = plot3d((x**2 + y**2, (x, -5, 5), (y, -5, 5)),
p4 *y, (x, -3, 3), (y, -3, 3))) (x
from sympy.plotting import plot3d_parametric_line
= sympy.symbols('u')
u = plot3d_parametric_line(sympy.cos(u), sympy.sin(u), u, (u, -5, 5)) p5
from sympy.plotting import plot3d_parametric_surface
= sympy.symbols('u v')
u, v = plot3d_parametric_surface(sympy.cos(u + v),
p6 - v), u - v, (u, -5, 5), (v, -5, 5)) sympy.sin(u
from sympy import plot_implicit, Eq, And
= plot_implicit(
p7 **2 + y**2, 3), (x, -3, 3), (y, -3, 3)) Eq(x
= plot_implicit(And(y > x, y > -x)) p8
from sympy.plotting import PlotGrid
= PlotGrid(2, 1, p1, p2) p9