JuliaSymbolics / SymbolicUtils.jl

Symbolic expressions, rewriting and simplification
https://docs.sciml.ai/SymbolicUtils/stable/
Other
527 stars 100 forks source link

Support for rules containing MathConstants #463

Open ArnoStrouwen opened 1 year ago

ArnoStrouwen commented 1 year ago

Many mathematical equations involve numbers such as pi, Euler's constant and the golden ratio. Currently, Symbolics does not seem to handle AbstractIrrational well. Expressions like sin(2π+x) can not be simplified.

bowenszhu commented 1 year ago

Currently, Symbolics does not seem to handle AbstractIrrational well.

This is partially improved by https://github.com/jump-dev/MutableArithmetics.jl/pull/165.

Expressions like sin(2π+x) can not be simplified.

sin(2π+x) can not be simplified easily because firstly promotes the Irrational π to Float64 $3.141592653589793$, then multiplies it with $2$ and produces Float64 $6.283185307179586$. It's very hard for symbolic computation to deal with floating-point numbers. https://github.com/JuliaLang/julia/blob/36034abf26062acad4af9dcec7c4fc53b260dbb4/base/irrationals.jl#L41-L45

To facilitate important math constants, we can probably define them symbolically.

bowenszhu commented 1 year ago
using SymbolicUtils, SymbolicUtils.Rewriters
@syms x
@syms ♓ # \pisces, a symbolic π
rules = Chain([@rule sin(~y + ~k::iseven * ♓) => sin(~y)
               @rule sin(~y + ~k::isodd * ♓) => sin(~y + ♓)])
julia> rules(sin(100♓+ x))
sin(x)

julia> rules(sin(5♓+ x))
sin(x + ♓)

julia> rules(sin(-13♓+ x))
sin(x + ♓)