In [1]:
from sympy import Symbol, sin, expand
In [2]:
x = Symbol('x')
y = Symbol('y')
In [3]:
expand((x + y) ** 10)
Out[3]:
x**10 + 10*x**9*y + 45*x**8*y**2 + 120*x**7*y**3 + 210*x**6*y**4 + 252*x**5*y**5 + 210*x**4*y**6 + 120*x**3*y**7 + 45*x**2*y**8 + 10*x*y**9 + y**10
In [4]:
from sympy import init_printing
init_printing(use_latex='mathjax')
In [5]:
expand((x + y)**10)
Out[5]:
$$x^{10} + 10 x^{9} y + 45 x^{8} y^{2} + 120 x^{7} y^{3} + 210 x^{6} y^{4} + 252 x^{5} y^{5} + 210 x^{4} y^{6} + 120 x^{3} y^{7} + 45 x^{2} y^{8} + 10 x y^{9} + y^{10}$$
In [6]:
from sympy import Matrix
In [7]:
Matrix([[3, 4], [5, 2]]).inv()
Out[7]:
$$\left[\begin{matrix}- \frac{1}{7} & \frac{2}{7}\\\frac{5}{14} & - \frac{3}{14}\end{matrix}\right]$$
In [8]:
Matrix([[3, 4], [5, 2]]).det()
Out[8]:
$$-14$$
In [9]:
Matrix([[3, 4], [5, 2]]).eigenvects()
Out[9]:
$$\left [ \left ( -2, \quad 1, \quad \left [ \left[\begin{matrix}- \frac{4}{5}\\1\end{matrix}\right]\right ]\right ), \quad \left ( 7, \quad 1, \quad \left [ \left[\begin{matrix}1\\1\end{matrix}\right]\right ]\right )\right ]$$
In [10]:
Matrix([[x, y], [5, 2]]).det()
Out[10]:
$$2 x - 5 y$$
In [11]:
f = (x + y)**2
In [12]:
f
Out[12]:
$$\left(x + y\right)^{2}$$
In [13]:
f.subs({x: 1, y: 2})
Out[13]:
$$9$$
In [14]:
a = Symbol('a')
In [15]:
from sympy import simplify
In [16]:
simplify(x ** 2 + 2 * x * y + y ** 2)
Out[16]:
$$x^{2} + 2 x y + y^{2}$$
In [17]:
expand(f.subs({x: a ** 2, y: a ** 3}))
Out[17]:
$$a^{6} + 2 a^{5} + a^{4}$$
In [18]:
from sympy import pi
In [19]:
pi
Out[19]:
$$\pi$$
In [20]:
sin(pi)
Out[20]:
$$0$$
In [21]:
print(pi.evalf(100000))
3.14159265358979323...
In [22]:
sin(x ** 2 + sin(x)).diff(x, 2)
Out[22]:
$$- (\left(2 x + \cos{\left (x \right )}\right)^{2} \sin{\left (x^{2} + \sin{\left (x \right )} \right )} + \left(\sin{\left (x \right )} - 2\right) \cos{\left (x^{2} + \sin{\left (x \right )} \right )})$$
In [23]:
sin(x ** 2 + sin(x)).series(n=10)
Out[23]:
$$x + x^{2} - \frac{x^{3}}{3} - \frac{x^{4}}{2} - \frac{2 x^{5}}{5} + \frac{x^{6}}{24} + \frac{89 x^{7}}{630} + \frac{23 x^{8}}{720} - \frac{x^{9}}{315} + \mathcal{O}\left(x^{10}\right)$$
In [24]:
from sympy import solve
In [25]:
solve(x ** 2 - 5 * x + 6, x)
Out[25]:
$$\left [ 2, \quad 3\right ]$$
In [26]:
simplify(solve(x ** 3 - 10 * x ** 2 + 5 * x + 1, x)[0])
Out[26]:
$$\frac{1}{12 \left(1 + \sqrt{3} i\right) \sqrt[3]{1523 + 9 \sqrt{1691} i}} \left(- 680 \sqrt[3]{2} + \left(1 + \sqrt{3} i\right) \left(40 - 2^{\frac{2}{3}} \left(1 + \sqrt{3} i\right) \sqrt[3]{1523 + 9 \sqrt{1691} i}\right) \sqrt[3]{1523 + 9 \sqrt{1691} i}\right)$$
In [27]:
from sympy import I
In [28]:
from sympy import trigsimp, cos
In [29]:
simplify(sin(x) ** 2 + cos(x) ** 2)
Out[29]:
$$1$$
In [30]:
# from sympy import *
import sympy as sp
In [31]:
sp.integrate(x ** 2, x)
Out[31]:
$$\frac{x^{3}}{3}$$
In [32]:
sp.integrate(sin(x)**2, x)
Out[32]:
$$\frac{x}{2} - \frac{1}{2} \sin{\left (x \right )} \cos{\left (x \right )}$$
In [33]:
sp.integrate(sp.exp(-x**2), x).subs({x: 3}).evalf()
Out[33]:
$$0.886207348259521$$
In [34]:
sp.integrate(sp.exp(-x**2), x)
Out[34]:
$$\frac{\sqrt{\pi}}{2} \operatorname{erf}{\left (x \right )}$$
In [35]:
# sp.integrate(sin(sp.exp(x)), x)
In [36]:
2
Out[36]:
$$2$$
In [39]:
print(sp.latex(simplify(solve(x ** 3 - 10 * x ** 2 + 5 * x + 1, x)[0])))
\frac{1}{12 \left(1 + \sqrt{3} i\right) \sqrt[3]{1523 + 9 \sqrt{1691} i}} \left(- 680 \sqrt[3]{2} + \left(1 + \sqrt{3} i\right) \left(40 - 2^{\frac{2}{3}} \left(1 + \sqrt{3} i\right) \sqrt[3]{1523 + 9 \sqrt{1691} i}\right) \sqrt[3]{1523 + 9 \sqrt{1691} i}\right)
In [45]:
solve([sp.Eq(x + y, 7), x - y + 3], [x, y])
# x + y == 7
Out[45]:
$$\left \{ x : 2, \quad y : 5\right \}$$
In [46]:
solve(x ** 2 + y * x + y, x)
Out[46]:
$$\left [ - \frac{y}{2} - \frac{1}{2} \sqrt{y \left(y - 4\right)}, \quad - \frac{y}{2} + \frac{1}{2} \sqrt{y \left(y - 4\right)}\right ]$$
In [47]:
solve(x ** 2 + y * x + y, y)
Out[47]:
$$\left [ - \frac{x^{2}}{x + 1}\right ]$$
In [50]:
solve((x ** 3 - 3 * x).diff(x), x)
Out[50]:
$$\left [ -1, \quad 1\right ]$$
In [51]:
(x ** 3 - 3 * x).diff(x, 2)
Out[51]:
$$6 x$$
In [53]:
sp.integrate(y * x**2, (x, -2, 2))
Out[53]:
$$\frac{16 y}{3}$$
In [54]:
sp.integrate(1 / x ** 2, (x, 1, sp.oo))
Out[54]:
$$1$$
In [58]:
sp.limit(x ** 2 / (2 * x ** 2 + 1), x, sp.oo)
Out[58]:
$$\frac{1}{2}$$
In [64]:
import sys
!"{sys.executable}" -m pip install plotly
Requirement already satisfied: plotly in c:\programdata\anaconda3\lib\site-packages (3.7.1)
Requirement already satisfied: pytz in c:\programdata\anaconda3\lib\site-packages (from plotly) (2018.5)
Requirement already satisfied: requests in c:\programdata\anaconda3\lib\site-packages (from plotly) (2.19.1)
Requirement already satisfied: nbformat>=4.2 in c:\programdata\anaconda3\lib\site-packages (from plotly) (4.4.0)
Requirement already satisfied: decorator>=4.0.6 in c:\programdata\anaconda3\lib\site-packages (from plotly) (4.3.0)
Requirement already satisfied: six in c:\programdata\anaconda3\lib\site-packages (from plotly) (1.11.0)
Requirement already satisfied: retrying>=1.3.3 in c:\programdata\anaconda3\lib\site-packages (from plotly) (1.3.3)
Requirement already satisfied: certifi>=2017.4.17 in c:\programdata\anaconda3\lib\site-packages (from requests->plotly) (2018.8.24)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\programdata\anaconda3\lib\site-packages (from requests->plotly) (3.0.4)
Requirement already satisfied: urllib3<1.24,>=1.21.1 in c:\programdata\anaconda3\lib\site-packages (from requests->plotly) (1.23)
Requirement already satisfied: idna<2.8,>=2.5 in c:\programdata\anaconda3\lib\site-packages (from requests->plotly) (2.7)
Requirement already satisfied: traitlets>=4.1 in c:\programdata\anaconda3\lib\site-packages (from nbformat>=4.2->plotly) (4.3.2)
Requirement already satisfied: jupyter-core in c:\programdata\anaconda3\lib\site-packages (from nbformat>=4.2->plotly) (4.4.0)
Requirement already satisfied: jsonschema!=2.5.0,>=2.4 in c:\programdata\anaconda3\lib\site-packages (from nbformat>=4.2->plotly) (2.6.0)
Requirement already satisfied: ipython-genutils in c:\programdata\anaconda3\lib\site-packages (from nbformat>=4.2->plotly) (0.2.0)
twisted 18.7.0 requires PyHamcrest>=1.9.0, which is not installed.
You are using pip version 10.0.1, however version 19.0.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
In [67]:
from plotly.offline import init_notebook_mode, iplot
In [68]:
init_notebook_mode()