szcf-weiya / techNotes

My notes about tech (Linux/R/Julia/Python/Cpp/etc.)
https://tech.hohoweiya.xyz/
11 stars 7 forks source link

Symbolic math with julia #8

Closed szcf-weiya closed 2 years ago

szcf-weiya commented 3 years ago

SymPy

http://mth229.github.io/symbolic.html it is required to install sympy

julia> using SymPy
[ Info: Precompiling SymPy [24249f21-da20-56a4-8eb1-6a02cf4ae2e6]
ERROR: InitError: PyError (PyImport_ImportModule

The Python package sympy could not be imported by pyimport. Usually this means
that you did not install sympy in the Python version being used by PyCall.

PyCall is currently configured to use the Python version at:

/home/weiya/anaconda3/envs/py37/bin/python3.7

and you should use whatever mechanism you usually use (apt-get, pip, conda,
etcetera) to install the Python package containing the sympy module.
szcf-weiya commented 3 years ago
julia> @vars x
(x,)

julia> exp(-x^2/2)/sqrt(2pi)
                     2 
                   -x  
                   ────
                    2  
0.398942280401433⋅ℯ    

julia> f(x) = exp(-x^2/2)/sqrt(2pi)
f (generic function with 1 method)

julia> integrate(f(x), x)
                           ⎛√2⋅x⎞
0.199471140200716⋅√2⋅√π⋅erf⎜────⎟
                           ⎝ 2  ⎠

julia> integrate(f(x), (x, -Inf, Inf))
0.398942280401433⋅√2⋅√π

julia> integrate(x*f(x), (x, -Inf, Inf))
0

julia> integrate(x*f(x), x)
                      2 
                    -x  
                    ────
                     2  
-0.398942280401433⋅ℯ    

julia> integrate(x*f(x), (x, -Inf, Inf))
0

julia> integrate(f(x)^2, (x, -Inf, Inf))
0.159154943091895⋅√π

julia> integrate(f(x)^2, x)
0.0795774715459477⋅√π⋅erf(x)

where erf is the https://en.wikipedia.org/wiki/Error_function