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

Promotion with rationals when constructing arrays #849

Closed
ranocha opened this issue Feb 20, 2023 · 2 comments
Closed

Promotion with rationals when constructing arrays #849

ranocha opened this issue Feb 20, 2023 · 2 comments

Comments

@ranocha
Copy link
Contributor

ranocha commented Feb 20, 2023

This was reported originally by @ketch in ranocha/BSeries.jl#96

Using the recommendation in https://discourse.julialang.org/t/symbolic-sqrt-2-and-sin-pi/58633 to represent a square root symbolically does not work well when constructing an array of symbolic expressions and rational numbers:

julia> using Pkg; Pkg.activate(temp = true); Pkg.add("Symbolics")
  Activating new project at `/tmp/jl_r7ULJP`
    Updating registry at `~/.julia/registries/General.toml`
   Resolving package versions...
    Updating `/tmp/jl_r7ULJP/Project.toml`
  [0c5d862f] + Symbolics v5.1.0
...

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

julia> [1, sqrt3]
2-element Vector{Any}:
 1
  sqrt(3)

Note the Vector{Any}.

@bowenszhu
Copy link
Member

Symbolics.jl has corresponding promotion rules.

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

Normally users are supposed not to call internal constructors. But here symbolic version of sqrt(3) is special and we should wrap it with a Num struct.

julia> using Symbolics

julia> sqrt3 = Symbolics.wrap(Symbolics.Term(sqrt, [3]))           # Note this line
sqrt(3)

julia> [1, sqrt3]
2-element Vector{Num}:
       1
 sqrt(3)

@ranocha
Copy link
Contributor Author

ranocha commented Feb 20, 2023

Thanks! It would be great if this could make it to the docs.

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

No branches or pull requests

2 participants