JuliaSymbolics / SymbolicUtils.jl

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

SafeReal #425

Closed shashi closed 2 years ago

shashi commented 2 years ago

Adds a SafeReal type which does not cancel numerators and denominators on construction (all other things behave as they did with Real).

I wasn't planning on some of this, but things fell into place:

Part of https://github.com/JuliaSymbolics/Symbolics.jl/issues/512

julia> using Symbolics

julia> using Symbolics: unwrap

julia> @variables x::SafeReal y::Real
2-element Vector{Num}:
 x
 y

julia> unwrap(x) |> typeof
SymbolicUtils.Sym{SafeReal, Base.ImmutableDict{DataType, Any}}

julia> unwrap(x + 1) |> typeof
SymbolicUtils.Add{SafeReal, Int64, Dict{Any, Number}, Nothing}

julia> unwrap((x + (1+im)).re) |> typeof
SymbolicUtils.Add{SafeReal, Int64, Dict{Any, Number}, Nothing}

julia> unwrap((x + (1+im)).im) |> typeof
Int64

julia> x/x
x / x

julia> y/y
1

julia> x/(x*y)
x / (x*y)

julia> typeof(unwrap(x/(x*y)))
SymbolicUtils.Div{SafeReal, SymbolicUtils.Sym{SafeReal, Base.ImmutableDict{DataType, Any}}, SymbolicUtils.Mul{SafeReal, Int64, Dict{Any, Number}, Nothing}, Nothing}