JuliaSymbolics / Symbolics.jl

Symbolic programming for the next generation of numerical software
https://docs.sciml.ai/Symbolics/stable/
Other
1.36k stars 154 forks source link

Simplifying construstion of symbolic numbers #853

Open bowenszhu opened 1 year ago

bowenszhu commented 1 year ago

Following the discussion in

As of Symbolics v5.1.0 and SymbolicUtils v1.0.3, it is complicated to create a symbolic number that doesn't include a variable.

For example, to create a symbolic $\sqrt 3$, one needs to call

sqrt3 = Symbolics.wrap(Symbolics.Term(sqrt, [3]))

Here, users have to be aware of the Term data structure, in which the field arguments is a Vector instead of just a scalar number.

And a BasicSymbolic shall be wrapped in a Num in order to properly use promotion rules. https://github.com/JuliaSymbolics/Symbolics.jl/blob/426eb803efdb3bedf508b1d2dc547c2445f44252/src/wrapper-types.jl#L48-L52 https://github.com/JuliaSymbolics/Symbolics.jl/blob/426eb803efdb3bedf508b1d2dc547c2445f44252/src/num.jl#L1-L3 https://github.com/JuliaSymbolics/Symbolics.jl/blob/426eb803efdb3bedf508b1d2dc547c2445f44252/src/num.jl#L98-L99

We should hide these internal details to make it simpler for end users.

In SymPy and SymEngine, we can do it as follows, which are very straightforward.

using SymPy; Sympy.sqrt(Sym(3))
using SymEngine; sqrt(Basic(3))
orebas commented 7 months ago

What is the current way to just convert a number (and indeed, the number 0) into a symbolic that can be stored in a vector with other symbolic expressions? I tried (Symbolics.Term(Symbolics.identity, [Real(Float64(result))]) ) but this gives an error when I try to store it in an array, the error is

ERROR: LoadError: MethodError: Cannot `convert` an object of type
  SymbolicUtils.BasicSymbolic{Float64} to an object of type
  SymbolicUtils.BasicSymbolic{Real}