Skip to content

Error with iszero for symbolic matrix elements #372

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

Closed
Suavesito-Olimpiada opened this issue Sep 7, 2021 · 3 comments
Closed

Error with iszero for symbolic matrix elements #372

Suavesito-Olimpiada opened this issue Sep 7, 2021 · 3 comments

Comments

@Suavesito-Olimpiada
Copy link
Contributor

Suavesito-Olimpiada commented Sep 7, 2021

I got to this when working with symbolic matrices. This is the MWE

using Symbolics

@variables a[1, 1]

iszero(a[1, 1]) # throw really long error

The stack-trace is the folowing

Stack Trace
julia> iszero(α[1,1])
ERROR: MethodError: no method matching ^(::SymbolicUtils.Sym{Matrix{Real}, Base.ImmutableDict{DataType, Any}}, ::Int64)
Closest candidates are:
  ^(::Union{AbstractChar, AbstractString}, ::Integer) at strings/basic.jl:718
  ^(::SymbolicUtils.Mul, ::Number) at /home/jose/.julia/packages/SymbolicUtils/L2Usn/src/types.jl:966
  ^(::LinearAlgebra.UniformScaling, ::Number) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/uniformscaling.jl:298
  ...
Stacktrace:
  [1] unstable_pow(a::SymbolicUtils.Sym{Matrix{Real}, Base.ImmutableDict{DataType, Any}}, b::Int64)
    @ SymbolicUtils ~/.julia/packages/SymbolicUtils/L2Usn/src/types.jl:742
  [2] (::SymbolicUtils.var"#111#119"{SymbolicUtils.var"#resolve#116"{PolyForm{Matrix{Real}, Base.ImmutableDict{DataType, Any}}, SymbolicUtils.var"#get_var#115", SymbolicUtils.var"#is_var#114"}})(::Tuple{DynamicPolynomials.PolyVar{true}, Int64})
    @ SymbolicUtils ./none:0
  [3] iterate
    @ ./generator.jl:47 [inlined]
  [4] grow_to!(dest::Vector{Any}, itr::Base.Generator{Base.Iterators.Filter{SymbolicUtils.var"#112#120", Base.Iterators.Zip{Tuple{Vector{DynamicPolynomials.PolyVar{true}}, Vector{Int64}}}}, SymbolicUtils.var"#111#119"{SymbolicUtils.var"#resolve#116"{PolyForm{Matrix{Real}, Base.ImmutableDict{DataType, Any}}, SymbolicUtils.var"#get_var#115", SymbolicUtils.var"#is_var#114"}}})
    @ Base ./array.jl:739
  [5] collect(itr::Base.Generator{Base.Iterators.Filter{SymbolicUtils.var"#112#120", Base.Iterators.Zip{Tuple{Vector{DynamicPolynomials.PolyVar{true}}, Vector{Int64}}}}, SymbolicUtils.var"#111#119"{SymbolicUtils.var"#resolve#116"{PolyForm{Matrix{Real}, Base.ImmutableDict{DataType, Any}}, SymbolicUtils.var"#get_var#115", SymbolicUtils.var"#is_var#114"}}})
    @ Base ./array.jl:676
  [6] arguments(x::PolyForm{Matrix{Real}, Base.ImmutableDict{DataType, Any}})
    @ SymbolicUtils ~/.julia/packages/SymbolicUtils/L2Usn/src/polyform.jl:202
  [7] unsorted_arguments
    @ ~/.julia/packages/SymbolicUtils/L2Usn/src/types.jl:300 [inlined]
  [8] (::SymbolicUtils.Rewriters.Walk{:post, typeof(identity), typeof(similarterm), false})(x::PolyForm{Matrix{Real}, Base.ImmutableDict{DataType, Any}})
    @ SymbolicUtils.Rewriters ~/.julia/packages/SymbolicUtils/L2Usn/src/rewriters.jl:162
  [9] (::SymbolicUtils.Rewriters.PassThrough{SymbolicUtils.Rewriters.Walk{:post, typeof(identity), typeof(similarterm), false}})(x::PolyForm{Matrix{Real}, Base.ImmutableDict{DataType, Any}})
    @ SymbolicUtils.Rewriters ~/.julia/packages/SymbolicUtils/L2Usn/src/rewriters.jl:152
 [10] iterate
    @ ./generator.jl:47 [inlined]
 [11] _collect(c::Vector{Any}, itr::Base.Generator{Vector{Any}, SymbolicUtils.Rewriters.PassThrough{SymbolicUtils.Rewriters.Walk{:post, typeof(identity), typeof(similarterm), false}}}, #unused#::Base.EltypeUnknown, isz::Base.HasShape{1})
    @ Base ./array.jl:691
 [12] collect_similar
    @ ./array.jl:606 [inlined]
 [13] map(f::SymbolicUtils.Rewriters.PassThrough{SymbolicUtils.Rewriters.Walk{:post, typeof(identity), typeof(similarterm), false}}, A::Vector{Any})
    @ Base ./abstractarray.jl:2294
 [14] (::SymbolicUtils.Rewriters.Walk{:post, typeof(identity), typeof(similarterm), false})(x::SymbolicUtils.Term{Real, Nothing})
    @ SymbolicUtils.Rewriters ~/.julia/packages/SymbolicUtils/L2Usn/src/rewriters.jl:162
 [15] PassThrough
    @ ~/.julia/packages/SymbolicUtils/L2Usn/src/rewriters.jl:152 [inlined]
 [16] iterate
    @ ./generator.jl:47 [inlined]
 [17] _collect(c::Vector{SymbolicUtils.Term{Real, Nothing}}, itr::Base.Generator{Vector{SymbolicUtils.Term{Real, Nothing}}, SymbolicUtils.Rewriters.PassThrough{SymbolicUtils.Rewriters.Walk{:post, typeof(identity), typeof(similarterm), false}}}, #unused#::Base.EltypeUnknown, isz::Base.HasShape{1})
    @ Base ./array.jl:691
 [18] collect_similar
    @ ./array.jl:606 [inlined]
 [19] map(f::SymbolicUtils.Rewriters.PassThrough{SymbolicUtils.Rewriters.Walk{:post, typeof(identity), typeof(similarterm), false}}, A::Vector{SymbolicUtils.Term{Real, Nothing}})
    @ Base ./abstractarray.jl:2294
 [20] (::SymbolicUtils.Rewriters.Walk{:post, typeof(identity), typeof(similarterm), false})(x::PolyForm{Real, Nothing})
    @ SymbolicUtils.Rewriters ~/.julia/packages/SymbolicUtils/L2Usn/src/rewriters.jl:162
 [21] expand(expr::SymbolicUtils.Term{Real, Nothing})
    @ SymbolicUtils ~/.julia/packages/SymbolicUtils/L2Usn/src/polyform.jl:228
 [22] (::ComposedFunction{typeof(SymbolicUtils._iszero), typeof(expand)})(x::SymbolicUtils.Term{Real, Nothing})
    @ Base ./operators.jl:938
 [23] _any(f::ComposedFunction{typeof(SymbolicUtils._iszero), typeof(expand)}, itr::Vector{SymbolicUtils.Term{Real, Nothing}}, #unused#::Colon)
    @ Base ./reduce.jl:876
 [24] any(f::Function, a::Vector{SymbolicUtils.Term{Real, Nothing}}; dims::Function)
    @ Base ./reducedim.jl:883
 [25] any(f::Function, a::Vector{SymbolicUtils.Term{Real, Nothing}})
    @ Base ./reducedim.jl:883
 [26] fraction_iszero(x::SymbolicUtils.Term{Real, Nothing})
    @ SymbolicUtils ~/.julia/packages/SymbolicUtils/L2Usn/src/polyform.jl:310
 [27] iszero(x::Num)
    @ Symbolics ~/.julia/packages/Symbolics/GAvUr/src/num.jl:91
 [28] top-level scope
    @ REPL[3]:1

This happens both in Julia-1.7rc4 and Julia-1.6.2, I'm working with Symbolics v3.3.0.

@moble
Copy link
Contributor

moble commented Sep 17, 2021

This isn't just matrices; it's also true of just plain vectors. And it's still a problem on v3.3.1.

using Symbolics
@variables a[1:2] b
iszero(b) # Works just fine; returns `false`
iszero(a[1])  # Raises the error Suavesito noted

I suspect this has to do with the fact that a[1] and b actually have different types deep down. Their actual types are Num in both cases, but Num is just a wrapper containing a val field, and those fields have different types for the two variables:

julia> typeof(a[1].val)
SymbolicUtils.Term{Real, Nothing}

julia> typeof(b.val)
SymbolicUtils.Sym{Real, Base.ImmutableDict{DataType, Any}}

Note that a[1] is really a Term, and b is really a Sym.

@moble
Copy link
Contributor

moble commented Sep 17, 2021

@Suavesito-Olimpiada A workaround would be to define

a = [Symbolics.variable(:a, i, j) for i in 1:N, j in 1:M]

for any NxM matrix. I don't think this should be necessary, though; see #379.

@YingboMa
Copy link
Member

This is fixed.

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

3 participants