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

Unable to sum ::SparseVector{Num,Int} s #42730

Closed
abraunst opened this issue Oct 21, 2021 · 4 comments
Closed

Unable to sum ::SparseVector{Num,Int} s #42730

abraunst opened this issue Oct 21, 2021 · 4 comments
Labels
sparse Sparse arrays

Comments

@abraunst
Copy link
Contributor

From https://discourse.julialang.org/t/is-there-a-defined-minimal-interface-for-a-type-to-work-with-sparsearrays/69602/2

using Symbolics, SparseArrays

@variables a
v = sparsevec([1], [a])
v+v #  throws 
@jlapeyre
Copy link
Contributor

SparseArrays would benefit from checking for the unstored value in a uniform way. Perhaps always using iszero. But, accommodating Symbolics should play no part in deciding how to do this.

Symbolics doesn't follow the documented and de facto semantics of == and iszero. This makes it very difficult for packages to interoperate with Symbolics. Symbolics needs to adapt to the ecosystem, rather than the other way around.

For example, the docstring for iszero says "Return true if x == zero(x)". But,

julia> using Symbolics

julia> @variables x
1-element Vector{Num}:
 x

julia> iszero(x)
false

julia> x == zero(x)
x == 0

Opening an issue in Symbolics on how to fix this might be useful.

@jlapeyre
Copy link
Contributor

I can't find this addressed directly in the Symbolics.jl repo. But this blog post suggests that the behavior of the code in my comment above is desirable for something other than facilitating generic programming (you can read about it there). At any rate, it is a conscious decision. At first glance, I would have said it's better to construct an equation expression with a different function (not ==), leaving == free to follow documented semantics. At any rate it is still better to clean up SparseArrays with reference to standard Julia. If this allows it to work with Symbolics, fine. That's a bonus.

@abraunst
Copy link
Contributor Author

In any case, it's rather arbitrary to draw the line between x == zero(x) and iszero(x) (which should be semantically equivalent). We could (and probably will) modify the SparseArrays code to use the latter instead of the former, but I wouldn't be surprised if in the future Symbolics makes iszero(x::Num) return a Num...

@ViralBShah ViralBShah added the sparse Sparse arrays label Jul 19, 2023
@ViralBShah
Copy link
Member

Should be moved to SparseArrays.jl

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

No branches or pull requests

4 participants