Basic Operations
Here we discuss some of the most basic operations needed for expression manipulation in SymPy. Some more advanced operations will be discussed later in the advanced expression manipulation section.
We access SymPy from Julia
by loading either the SymPy
or SymPyPythonCall
packages. Once loaded, commands like the following one should run without complaint.
julia> @syms x, y, z
(x, y, z)
Expand for Python example
>>> from sympy import *
+Basic operations · SymPyCore Basic Operations
Here we discuss some of the most basic operations needed for expression manipulation in SymPy. Some more advanced operations will be discussed later in the advanced expression manipulation section.
Julia differences We access SymPy from Julia
by loading either the SymPy
or SymPyPythonCall
packages. Once loaded, commands like the following one should run without complaint.
julia> @syms x, y, z
(x, y, z)
Expand for Python example
>>> from sympy import *
>>> x, y, z = symbols("x y z")
Substitution
One of the most common things you might want to do with a mathematical expression is substitution. Substitution replaces all instances of something in an expression with something else. It is done using the subs
method. For example
Julia differences We can call subs
using the Julia
n notation of subs(expr, ...)
rather than the object methoc syntax more common in Python, expr.subs(...)
. Further, we can use "pairs" notation when calling subs
in this manner.
julia> expr = cos(x) + 1
cos(x) + 1
julia> subs(expr, x=>y)
cos(y) + 1
Expand for Python example
>>> expr = cos(x) + 1
>>> expr.subs(x, y)
cos(y) + 1
Substitution is usually done for one of two reasons:
- Evaluating an expression at a point. For example, if our expression is
cos(x) + 1
and we want to evaluate it at the point x = 0
, so that we get cos(0) + 1
, which is 2.
Julia differences We can also use the object-method syntax.
julia> expr.subs(x,0)
2
Expand for Python example
>>> expr.subs(x, 0)
@@ -48,21 +48,22 @@
>>> expr.subs(x, 2)
19/2
Warning sympify
uses eval
. Don't use it on unsanitized input.
evalf
To evaluate a numerical expression into a floating point number, use evalf
.
Julia differences We need to wrap 8
in Sym
otherwise, sqrt
will dispatch to the base function in Julia
. Also, we could use N(expr)
to get a Julia
value, as evalf
returns a symbolic value.
julia> expr = sqrt(Sym(8))
2⋅√2
julia> expr.evalf()
2.82842712474619
Expand for Python example
>>> expr = sqrt(8)
>>> expr.evalf()
- 2.82842712474619
SymPy can evaluate floating point expressions to arbitrary precision. By default, 15 digits of precision are used, but you can pass any number as the argument to evalf
. Let's compute the first 100 digits of \pi
.
Julia differences We use PI
of Sym(pi)
to express the symbolic value of $\pi$.
julia> PI.evalf(100)
3.1415926535897932384626433832795028841971693993751058209749445923078164062862
-08998628034825342117068
Expand for Python example
>>> pi.evalf(100)
+ 2.82842712474619
SymPy can evaluate floating point expressions to arbitrary precision. By default, 15 digits of precision are used, but you can pass any number as the argument to evalf
. Let's compute the first 100 digits of \pi
.
Julia differences We use PI
of Sym(pi)
to express the symbolic value of $\pi$.
julia> PI.evalf(100)
3.1415926535897932384626433832795028841971693993751058209749445923078164062862 ↪
+
+↪ 08998628034825342117068
Expand for Python example
>>> pi.evalf(100)
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
To numerically evaluate an expression with a Symbol at a point, we might use subs
followed by evalf
, but it is more efficient and numerically stable to pass the substitution to evalf
using the subs
flag, which takes a dictionary of Symbol: point
pairs.
Julia differences A Julia
Dict
can be used when the underlying sympy method expects a Python dict.
julia> expr = cos(2x)
cos(2⋅x)
julia> expr.evalf(subs=Dict(x=>2.4))
0.0874989834394464
Expand for Python example
>>> expr = cos(2*x)
>>> expr.evalf(subs={x: 2.4})
0.0874989834394464
Sometimes there are roundoff errors smaller than the desired precision that remain after an expression is evaluated. Such numbers can be removed at the user's discretion by setting the chop
flag to True.
Julia differences We don't use the reserved name one
, as it is a base function name in Julia
julia> o = cos(Sym(1))^2 + sin(Sym(1))^2
2 2
cos (1) + sin (1)
julia> (o-1).evalf()
-0.e-124
julia> (o - 1).evalf(chop=true)
ERROR: Python: TypeError: must be real number, not BooleanTrue
Python stacktrace:
[1] evalf
- @ sympy.core.evalf ~/work/SymPyCore.jl/SymPyCore.jl/docs/.CondaPkg/env/lib/python3.12/site-packages/sympy/core/evalf.py:1527
+ @ sympy.core.evalf ~/work/SymPyCore.jl/SymPyCore.jl/docs/.CondaPkg/env/lib/python3.12/site-packages/sympy/core/evalf.py:1528
[2] evalf_add
- @ sympy.core.evalf ~/work/SymPyCore.jl/SymPyCore.jl/docs/.CondaPkg/env/lib/python3.12/site-packages/sympy/core/evalf.py:600
+ @ sympy.core.evalf ~/work/SymPyCore.jl/SymPyCore.jl/docs/.CondaPkg/env/lib/python3.12/site-packages/sympy/core/evalf.py:601
[3] evalf
- @ sympy.core.evalf ~/work/SymPyCore.jl/SymPyCore.jl/docs/.CondaPkg/env/lib/python3.12/site-packages/sympy/core/evalf.py:1482
+ @ sympy.core.evalf ~/work/SymPyCore.jl/SymPyCore.jl/docs/.CondaPkg/env/lib/python3.12/site-packages/sympy/core/evalf.py:1483
[4] evalf
- @ sympy.core.evalf ~/work/SymPyCore.jl/SymPyCore.jl/docs/.CondaPkg/env/lib/python3.12/site-packages/sympy/core/evalf.py:1647
Expand for Python example
>>> one = cos(1)**2 + sin(1)**2
+ @ sympy.core.evalf ~/work/SymPyCore.jl/SymPyCore.jl/docs/.CondaPkg/env/lib/python3.12/site-packages/sympy/core/evalf.py:1648
Expand for Python example
>>> one = cos(1)**2 + sin(1)**2
>>> (one - 1).evalf()
-0.e-124
>>> (one - 1).evalf(chop=True)
@@ -91,4 +92,4 @@
... return x
>>> f = lambdify(x, expr, {"sin":mysin})
>>> f(0.1)
- 0.1
TODO Write an advanced numerics section
Settings
This document was generated with Documenter.jl version 1.4.1 on Thursday 30 May 2024. Using Julia version 1.9.4.
+ 0.1
Write an advanced numerics section