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

Type error in coeff using Symbolics 5.5.1 #952

Open
Johan-Gronqvist opened this issue Aug 11, 2023 · 3 comments
Open

Type error in coeff using Symbolics 5.5.1 #952

Johan-Gronqvist opened this issue Aug 11, 2023 · 3 comments

Comments

@Johan-Gronqvist
Copy link

I see an error when running

@syms a b c d

expr = 2.0*(a+b*c^2-10d)

Symbolics.coeff(expr, b)

The error message is

TypeError: non-boolean (SymbolicUtils.BasicSymbolic{Bool}) used in boolean context

Stacktrace:
 [1] _all(f::typeof(iszero), itr::Vector{Any}, #unused#::Colon)
   @ Base ./reduce.jl:1287
 [2] all(f::Function, a::Vector{Any}; dims::Function)
   @ Base ./reducedim.jl:1007
 [3] all(f::Function, a::Vector{Any})
   @ Base ./reducedim.jl:1007
 [4] coeff(p::SymbolicUtils.BasicSymbolic{Number}, sym::SymbolicUtils.BasicSymbolic{Number})
   @ Symbolics ~/.julia/packages/Symbolics/BQlmn/src/utils.jl:321
 [5] top-level scope
   @ In[40]:5

I use Julia 1.9.0 with the following output of Pkg.status()

  [8e7c35d0] BlockArrays v0.16.36
  [a6e380b2] ControlSystems v1.8.0
  [587475ba] Flux v0.14.2
  [b99e6be6] Hypatia v0.7.3
  [d1acc4aa] IntervalArithmetic v0.20.9
  [4076af6c] JuMP v1.13.0
  [d8e11817] MLStyle v0.4.17
  [b8f27783] MathOptInterface v1.18.0
  [1ec41992] MosekTools v0.15.0
  [872c559c] NNlib v0.9.4
  [bac558e1] OrderedCollections v1.6.2
  [91a5bcdd] Plots v1.38.17
  [f27b6e38] Polynomials v3.2.13
  [92933f4c] ProgressMeter v1.7.2
  [295af30f] Revise v3.5.3
  [0c5d862f] Symbolics v5.5.1
  [e88e6eb3] Zygote v0.6.63
  [37e2e46d] LinearAlgebra
  [8dfed614] Test
@Johan-Gronqvist
Copy link
Author

I am now using Symbolics v4.14.0 instead, which works for me.

@shashi
Copy link
Member

shashi commented Aug 16, 2023

@YingboMa

@Johan-Gronqvist
Copy link
Author

The problem seems to appear if the expression is a multiplication and one of the terms contain coefficients that are themselves symbolic expressions. A check if those expressions are zero fails.

Removing the following if-clause in utils.jl and just keeping the else-clause seems to fix the problem.

        if all(iszero, coeffs)
            return 0
        else
            @views prod(Iterators.flatten((coeffs[findall(!iszero, coeffs)], args[findall(iszero, coeffs)])))
        end

A better fix may be to replace the call to iszero with a call to isequal that checks equality with zero. The call to iszero appears to call a method from base that uses ==, as warned against in the FAQ https://symbolics.juliasymbolics.org/stable/manual/faq/#Equality-and-set-membership-tests

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