JuliaSymbolics / SymbolicUtils.jl

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

Simplifying a/a #388

Closed owiecc closed 3 months ago

owiecc commented 2 years ago

Simplification of a simple division a/a is currently not performed by the package.

using SymbolicUtils
@syms a
simplify(a/a) # ➞ a/a

I am aware of the special cases where a = 0 or a = ∞ but these could be avoided if we attach metadata (e.g. nonzero) to the variable. There was a small discussion on metadata in https://github.com/JuliaSymbolics/Symbolics.jl/pull/351#issuecomment-901453080

PS. Should we have a documented set of metadata (potentially) handled by the package? I cannot find any documentation on this.

shashi commented 2 years ago

@YingboMa

0x0f0f0f commented 2 years ago

See https://juliasymbolics.github.io/Metatheory.jl/dev/egraphs/#EGraph-Analyses

0x0f0f0f commented 2 years ago

We could have @rule a a::(nonzero && noninfinite)/a => a Where

nonzero(x::Symbolic) = getmetadata(x, SignAnalysis) != 0

and

nonzero(g::EGraph, x::EClass) = let sign Metatheory.getdata(x, SignAnalysis, nothing)
    sign != 0 && !isnothing(sign)
end

(same goes for noninfinite) Where && is the unary functional and (&&)(a::Function, b::Function) = x -> a(x) && b(x)

hersle commented 3 months ago

Now

using SymbolicUtils
@syms a
simplify(a/a)

gives

1