Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplifying construstion of symbolic numbers #853

Open
bowenszhu opened this issue Feb 23, 2023 · 1 comment
Open

Simplifying construstion of symbolic numbers #853

bowenszhu opened this issue Feb 23, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@bowenszhu
Copy link
Member

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.

function wrap(x)
T = SymbolicUtils.symtype(x)
Symbolics.has_symwrapper(T) ?
Symbolics.wrapper_type(T)(x) : x
end

@symbolic_wrap struct Num <: Real
val
end

Symbolics.jl/src/num.jl

Lines 98 to 99 in 426eb80

Base.promote_rule(::Type{<:Number}, ::Type{<:Num}) = Num
Base.promote_rule(::Type{<:Symbolic{<:Number}}, ::Type{<:Num}) = Num

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))
@bowenszhu bowenszhu added the enhancement New feature or request label Feb 23, 2023
@bowenszhu bowenszhu self-assigned this Feb 23, 2023
@orebas
Copy link

orebas commented Mar 14, 2024

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}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants