You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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?
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.
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.
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.
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.
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.
Activity
maartenvd commentedon Dec 21, 2022
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?
davidschlegel commentedon Jan 10, 2023
The problem is that for symbolic numbers that are defined through
Symbolics.jlthe functioniszerodoes not have a boolean output but rather defines an equation. Therefore, one should use the double-==to check if symbolic expressions are literally zero.davidschlegel commentedon Jan 10, 2023
#7
Jutho commentedon Jan 10, 2023
I am not familiar with Symbolics.jl, but I have to admit that this seems wrong. The documentation of
Base.iszeroliterally states that "Return true ifx == 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.davidschlegel commentedon Jan 11, 2023
Sorry, I got confused myself for a second.
From the julia documentation for
===:Indeed, Symbolics.jl has the behavior that for a symbolic variable,
x==0defines an equation, whileisequal(x, 0)gives a boolean.I think, instead of fiddling with
==and===and their negations, one could useisequalto prevent all issues with of non-numeric types.Jutho commentedon Jan 11, 2023
I had anticipated that, but one problem with using
===orisequalis that, with floating point numbers,returns
false.davidschlegel commentedon Jan 11, 2023
Okay, in that case, I would stick to
===and!==instead of==and!=.Jutho commentedon Jan 11, 2023
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.davidschlegel commentedon Jan 11, 2023
I agree, then, it is a matter of non-numerical types to adequately define
iszero. Submitted an issue inSymbolicUtils.jlfor this. This is already working forNumtypes but not forSymtypes.