# [PYTHON] Sympy Reverse Lookup Reference

## LaTeX output

In [59]: h_x
Out[59]:
2                          3
(x + 1) ⋅(x + 2)⋅(x + 3) + (x + 4)

In [60]: latex(h_x)
Out[60]: '\\left(x + 1\\right)^{2} \\left(x + 2\\right) \\left(x + 3\\right) + \\left(x + 4\\right)^{3}'


## Expression expansion

In [50]: f_x
Out[50]:
2
(x + 1) ⋅(x + 2)⋅(x + 3)

In [51]: expand(f_x)
Out[51]:
4      3       2
x  + 7⋅x  + 17⋅x  + 17⋅x + 6


## Factorization

In [21]: g_x = x**2 + 2*x + 1

In [22]: factor(g_x,x)
Out[22]:
2
(x + 1)


### Get a list of factors

In [21]: g_x = x**2 + 2*x + 1

In [22]: factor(g_x,x)
Out[22]:
2
(x + 1)

In [23]: factor_list(g_x,x)
Out[23]: (1, [(x + 1, 2)])


http://docs.sympy.org/latest/modules/polys/reference.html?highlight=factor_list#sympy.polys.polytools.factor_list

## Factorization by product

In [35]: f_x
Out[35]:
-2⋅x
ℯ    ⋅sin(2⋅x)

In [39]: f_x.args
Out[39]:
⎛ -2⋅x          ⎞
⎝ℯ    , sin(2⋅x)⎠


## Polynomial division

In [25]: f
Out[25]: x**2 + 4/x**2

In [26]: g = (x + 2/x)**2

In [27]: div(g, f)
Out[27]: (1, 4)

In [28]: q,r = div(g,f)

In [29]: h = q * f + r

In [30]: h
Out[30]: x**2 + 4 + 4/x**2

In [31]: g == h
Out[31]: False

In [32]: g.expand() == h
Out[32]: True


## Function conversion

In [45]: f_x
Out[45]:
⎛    π⎞
sin⎜x + ─⎟
⎝    4⎠

In [46]: f_x.rewrite(cos)
Out[46]:
⎛    π⎞
cos⎜x - ─⎟
⎝    4⎠

In [47]: f_x.rewrite(tan)
Out[47]:
⎛x   π⎞
2⋅tan⎜─ + ─⎟
⎝2   8⎠
───────────────
2⎛x   π⎞
tan ⎜─ + ─⎟ + 1
⎝2   8⎠

In [48]: f_x.rewrite(exp)
Out[48]:
⎛     ⎛     π⎞      ⎛    π⎞⎞
⎜   ⅈ⋅⎜-x - ─⎟    ⅈ⋅⎜x + ─⎟⎟
⎜     ⎝     4⎠      ⎝    4⎠⎟
-ⅈ⋅⎝- ℯ           + ℯ         ⎠
────────────────────────────────
2


## Plane / space vector

There seems to be a way to display it in a matrix, but in the range of high school mathematics, what can be done with Point?

In [64]: Point([3,4])
Out[64]: Point2D(3, 4)


### Scalar times

In scale, you can specify the coefficient to be multiplied for each variable.

In [65]: a = Point([2,-3])

In [70]: a.scale(3)
Out[70]: Point2D(6, -3)

In [71]: a.scale(3,3)
Out[71]: Point2D(6, -9)


### distance

In [104]: a
Out[104]: Point2D(2, -3)

In [105]: a.origin
Out[105]: Point2D(0, 0)

In [106]: a.distance(a.origin)
Out[106]: √13



### inner product

In [92]: a
Out[92]: Point2D(2, -3)

In [93]: c
Out[93]: Point2D(4, 4)

In [94]: a.dot(c)
Out[94]: -4


### Orthogonal vector

In [108]: a
Out[108]: Point2D(2, -3)

In [109]: a.orthogonal_direction
Out[109]: Point2D(3, 2)

In [110]: a.dot(a.orthogonal_direction)
Out[110]: 0


## probability

### Random variable of dice

In [1]: from sympy.stats import P, E, Die

In [2]: X = Die('X',6)

In [3]: X
Out[3]: X

In [4]: P(X>3)
Out[4]: 1/2

In [5]: E(X)
Out[5]: 7/2

In [6]: Y = Die('Y',6)

In [7]: E(X+Y)
Out[7]: 7

In [8]: P(X+Y>10)
Out[8]: 1/12


Besides that, the probability distribution of coin throwing, uniform distribution, etc. are almost complete.

http://docs.sympy.org/latest/modules/stats.html