JuliaSymbolics / SymbolicUtils.jl

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

SymbolicRegression.jl simplification issue #428

Closed MilesCranmer closed 2 years ago

MilesCranmer commented 2 years ago

I was wondering if there is a way to turn off automatic simplification? Perhaps there is something like #340 set up, also discussed in #327? cc @shashi @ChrisRackauckas @AlCap23

For example:

julia> using SymbolicUtils
julia> @syms x
julia> x * x
x^2

This automatic simplification seems to have broken SymbolicRegression.jl's simplification algorithm in the current SymbolicUtils.jl versions.

For example, if a user provides operators (+, *, /, -), then it is impossible for me to convert the expression:

x * x + x * x

back and forth between SymbolicRegression.jl and SymbolicUtils.jl. I can only go SymbolicRegression.jl => SymbolicUtils.jl, but not backwards.

What I did in previous versions of SymbolicUtils.jl was run simplify() as usual (defining a set of valid simplification rules given the operators, adding ^ to their set), but then convert x ^ 2 => x * x at the very end. However, it seems like I can no longer do this as even writing x * x simplifies directly to a power. i.e., the algebra is no longer literal:

julia> operation(x * x)
^ (generic function with 82 methods)

I also tried this:

julia> SymbolicUtils.show_simplified[] = false
julia> x * x
x^2

but the issue still occurs.

Is there anything I can do to prevent auto-simplification to operators I do not manually specify?

Thanks, Miles

MilesCranmer commented 2 years ago

For example, this is SymbolicUtils.jl v0.6:

julia> SymbolicUtils.operation(x * x)
* (generic function with 444 methods)
MasonProtter commented 2 years ago

Unfortunately, SymbllicUtils.jl bakes in some rules at the constructor level. So if you don’t want those rules, your only option is to roll your own types that don’t do those.

MilesCranmer commented 2 years ago

Is there a way I can manually create SymbolicUtils.jl types, rather than calling these auto-simplifying constructors? This would be good for me.

ChrisRackauckas commented 2 years ago

@shashi how hard would it be to support a @variables x::NoSimplifyReal type?

shashi commented 2 years ago

Yeah I'm gonna add it soon -- have an issue here https://github.com/JuliaSymbolics/SymbolicUtils.jl/issues/340