Skip to content

Assigning symbolic SparseArrays fails #6

Open

Description

@davidschlegel

If I want to have a multi-dimensional sparse array. I could define

using Symbolics
using SparseArrayKit

@variables a
 A = SparseArray{Num}(undef, (1,2,3)) #3dimesional array with size 1, 2, and 3 each
A[1,1,1] = a

But this gives the well-known error
ERROR: TypeError: non-boolean used in boolean context
This is due to the fact that in line #34 of sparsearray.jl we have the zero-comparison if v != zero(v).
This however fails for symbolic expressions as == or != defines an equation.
In this context, one should use if v !== zero(v) to evaluate the expression. Then everything works and still works for regular numbers.

Activity

  1. maartenvd commented on Dec 21, 2022

    @maartenvd
    Collaborator

    Sorry for the late reply, I totally forgot about this issue!

    that line really should've used iszero, can you verify that everything (the minimal example you provide works, but presumably you do other operations as well) works on the latest master?

  2. davidschlegel commented on Jan 10, 2023

    @davidschlegel
    Author

    The problem is that for symbolic numbers that are defined through Symbolics.jl the function iszero does not have a boolean output but rather defines an equation. Therefore, one should use the double-== to check if symbolic expressions are literally zero.

  3. davidschlegel commented on Jan 10, 2023

    @davidschlegel
    Author
  4. Jutho commented on Jan 10, 2023

    @Jutho
    Member

    The problem is that for symbolic numbers that are defined through Symbolics.jl the function iszero does not have a boolean output but rather defines an equation. Therefore, one should use the double-== to check if symbolic expressions are literally zero.

    I am not familiar with Symbolics.jl, but I have to admit that this seems wrong. The documentation of Base.iszero literally states that "Return true if x == zero(x)". If Symbolics.jl choses to have different behaviour, then this is not a method extension of Base.iszero, and thus, it should be a different function.

  5. davidschlegel commented on Jan 11, 2023

    @davidschlegel
    Author

    Sorry, I got confused myself for a second.
    From the julia documentation for ===:

    Determine whether x and y are identical, in the sense that no program could distinguish them. First the types of x and y are compared. If those are identical, mutable objects are compared by address in memory and
    immutable objects (such as numbers) are compared by contents at the bit level. This function is sometimes called "egal". It always returns a Bool value.

    Indeed, Symbolics.jl has the behavior that for a symbolic variable, x==0 defines an equation, while isequal(x, 0) gives a boolean.

    I think, instead of fiddling with == and === and their negations, one could use isequal to prevent all issues with of non-numeric types.

  6. Jutho commented on Jan 11, 2023

    @Jutho
    Member

    I had anticipated that, but one problem with using === or isequal is that, with floating point numbers,

    isequal(+0., -0.)

    returns false.

  7. davidschlegel commented on Jan 11, 2023

    @davidschlegel
    Author

    Okay, in that case, I would stick to === and !== instead of == and !=.

  8. Jutho commented on Jan 11, 2023

    @Jutho
    Member

    Well, but these have the same problem. Also -0. === +0. is false, or thus -0. would not be recognised as a zero entry. I don't know how troublesome that would be in practice.

  9. davidschlegel commented on Jan 11, 2023

    @davidschlegel
    Author

    I agree, then, it is a matter of non-numerical types to adequately define iszero. Submitted an issue in SymbolicUtils.jl for this. This is already working for Num types but not for Sym types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions