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

Cannot sum an array of symbolics because of conversion error #1063

Closed
akriegman opened this issue Feb 20, 2024 · 2 comments
Closed

Cannot sum an array of symbolics because of conversion error #1063

akriegman opened this issue Feb 20, 2024 · 2 comments

Comments

@akriegman
Copy link

When I try to sum an array of Symbolics I get a conversion error.

MWE:

@syms a b
l = [a, b]
sum(l, dims=1)

Output:

MethodError: Cannot `convert` an object of type SymbolicUtils.BasicSymbolic{Number} to an object of type Int64

Closest candidates are:

convert(::Type{T}, !Matched::DualNumbers.Dual) where T<:Union{Real, Complex}

@ DualNumbers ~/.julia/packages/DualNumbers/5knFX/src/dual.jl:24

convert(::Type{T}, !Matched::MultivariatePolynomials.AbstractPolynomialLike) where T<:Number

@ MultivariatePolynomials ~/.julia/packages/MultivariatePolynomials/TpRhf/src/conversion.jl:96

convert(::Type{T}, !Matched::T) where T

@ Base Base.jl:84

...

    setindex!(::Vector{Int64}, ::SymbolicUtils.BasicSymbolic{Number}, ::Int64)@array.jl:1021
    setindex!@multidimensional.jl:698[inlined]
    _mapreducedim!(::typeof(identity), ::typeof(Base.add_sum), ::Vector{Int64}, ::Vector{SymbolicUtils.BasicSymbolic{Number}})@reducedim.jl:311
    mapreducedim!(::Function, ::Function, ::Vector{Int64}, ::Vector{SymbolicUtils.BasicSymbolic{Number}})@reducedim.jl:324
    _mapreduce_dim(::Function, ::Function, ::Base._InitialValue, ::Vector{SymbolicUtils.BasicSymbolic{Number}}, ::Int64)@reducedim.jl:371
    mapreduce@reducedim.jl:357[inlined]
    _sum@reducedim.jl:1039[inlined]
    _sum@reducedim.jl:1038[inlined]
    sum@reducedim.jl:1010[inlined]
    top-level scope@[Local: 4](http://localhost:1234/edit?id=f65195a4-d00d-11ee-25e9-41c812cd54ea#)[inlined]

I think what's happening is roughly this: it creates an array to put the results of the summation into, and for some reason it thinks the appropriate type to use is Array{Int64, ...}. So then when it goes to place a BasicSymbolic{Number} in there we get an error. So I guess the problem is with the type prediction infrastructure.

@akriegman akriegman changed the title Cannot sum an array using dims keyword because of conversion error Cannot sum an array of symbolics because of conversion error Feb 21, 2024
@akriegman
Copy link
Author

I looked into this more, and I think the deeper problem is that when you call sum with dims it needs an init value, and there is no zero(Any) or zero(BasicSymbolic{Number}), so instead it uses zero(Int64). It would be fine if we had and Any[] full of zero(Int64)s, but instead we have an Int64[], so when we finally do the addition we promote the type correctly but can't store it in the result array.

Anyways, I found a workaround:

@syms a b c d
l = [
    a b
    c d
]
sum(eachslice(l, dims=1))

@ChrisRackauckas
Copy link
Member

The documentation doesn't use @syms for a reason. Please use @variables as documented.

using Symbolics
@variables a b
l = [a, b]
sum(l, dims=1)

this works fine.

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