JuliaPy / SymPy.jl

Julia interface to SymPy via PyCall
http://juliapy.github.io/SymPy.jl/
MIT License
269 stars 62 forks source link

Behavior of iszero(x::Sym) #542

Open cvsvensson opened 8 months ago

cvsvensson commented 8 months ago

In SymPy.jl v1.2.1, iszero(x::Sym) returns false. In v2.0.1, it throws ERROR: KeyError: key :boolalg not found.

using SymPy
@syms x 
iszero(x) # returns false in v1, but errors in v2

Is this change intended? One consequence of this is that multiplying variables with sparse matrices also errors in v2.

using SparseArrays, SymPy
@syms x
x * sprand(2,2,0.5) #ERROR: KeyError: key :boolalg not found
jverzani commented 8 months ago

Thanks, this seems like a bug to me. Let me look into it.

jverzani commented 8 months ago

Hmm, I can't seem to replicate this:

julia> using Revise; using SymPy

julia> @syms x
(x,)

julia> iszero(x)
false

(@v1.10) pkg> st
Status `~/.julia/environments/v1.10/Project.toml`
  [c29ec348] AbstractDifferentiation v0.6.0
⌃ [7d9f7c33] Accessors v0.1.33
⌃ [cbdf2221] AlgebraOfGraphics v0.6.16
  [4c88cf16] Aqua v0.8.4
  [6e4b80f9] BenchmarkTools v1.4.0
  [4c24c29e] BoxCoxTrans v2.0.2
  [336ed68f] CSV v0.10.12
  [324d7699] CategoricalArrays v0.10.8
  [8be319e6] Chain v0.5.0
  [a93c6f00] DataFrames v1.6.1
  [31a5f54b] Debugger v0.7.8
  [e30172f5] Documenter v1.2.1
  [35a29f4d] DocumenterTools v0.1.18
  [f6369f11] ForwardDiff v0.10.36
  [da1fdf0e] FreqTables v0.4.6
  [38e38edf] GLM v1.9.0
⌃ [e9467ef8] GLMakie v0.8.12
  [7073ff75] IJulia v1.24.2
  [57b37032] ImplicitDifferentiation v0.5.2
  [95701278] ImplicitEquations v1.0.9 `~/julia/ImplicitEquations`
  [601899f1] JuliaWeBWorK v0.3.2 `~/julia/JuliaWeBWorK.jl`
  [4345ca2d] Loess v0.6.3
  [ebaf19f5] MTH229 v0.2.12 `../../../export/JuliaProjects/MTH229/MTH229.jl`
  [f77d288f] MTH229Lite v1.0.2 `../../../julia/MTH229Lite`
  [ffc61752] Mustache v1.0.19 `~/julia/Mustache`
⌃ [14b8a8f1] PkgTemplates v0.7.46
  [995b91a9] PlotUtils v1.4.0
  [ca7969ec] PlotlyLight v0.7.4
  [91a5bcdd] Plots v1.39.0
  [f27b6e38] Polynomials v4.0.5 `~/julia/Polynomials`
  [92209980] Purl v1.0.0-DEV `~/julia/Purl`
⌃ [1fd47b50] QuadGK v2.9.1
  [ce6b1742] RDatasets v0.7.7
⌃ [295af30f] Revise v3.5.12
  [d6ea1423] RobustModels v0.5.0
  [f2b01f46] Roots v2.1.0
  [deba94f7] SimpleExpressions v1.0.8 `~/julia/SimpleExpressions`
  [276daf66] SpecialFunctions v2.3.1
  [03a91e81] SplitApplyCombine v1.2.2
  [2913bbd2] StatsBase v0.34.2
  [3eaba693] StatsModels v0.7.3
  [f3b207a7] StatsPlots v0.15.6
  [123dc426] SymEngine v0.11.1 `~/julia/SymEngine.jl`
  [24249f21] SymPy v2.0.1 `~/julia/SymPy`
  [458b697b] SymPyCore v0.1.14 `~/julia/SymPyCore`
  [bc8888f7] SymPyPythonCall v0.2.5 `~/julia/SymPyPythonCall`
  [bd369af6] Tables v1.11.1
  [8ea1fca8] TermInterface v0.3.3
  [fdbf4ff8] XLSX v0.10.1
Info Packages marked with ⌃ have new versions available and may be upgradable.

julia> using SparseArrays, SymPy

julia> @syms x
(x,)

julia> x * sprand(2,2,0.5) 
2×2 SparseMatrixCSC{Any, Int64} with 4 stored entries:
 0.595397486816609⋅x  0.393331441501513⋅x
 0.651217482283023⋅x  0.398380404221288⋅x

Can you share your error message?

cvsvensson commented 8 months ago

Here's a more complete log. There is also a deprecation warning included here that shows up on the first call. My julia version is 1.10.

(jl_KLFI0z) pkg> st
Status `C:\Users\svens\AppData\Local\Temp\jl_KLFI0z\Project.toml`
  [24249f21] SymPy v2.0.1

 julia> iszero(x)
C:\Users\svens\.julia\conda\3\lib\site-packages\sympy\__init__.py:672: SymPyDeprecationWarning:

importing sympy.core.logic with 'from sympy import *' has been
deprecated since SymPy 1.6. Use import sympy.core.logic instead. See
https://github.com/sympy/sympy/issues/18245 for more info.

  self.Warn(
ERROR: KeyError: key :boolalg not found
Stacktrace:
 [1] __getproperty
   @ PyCall C:\Users\svens\.julia\packages\PyCall\1gn3u\src\PyCall.jl:313 [inlined]
 [2] getproperty(o::PyCall.PyObject, s::Symbol)
   @ PyCall C:\Users\svens\.julia\packages\PyCall\1gn3u\src\PyCall.jl:318
 [3] _convert(::Type{Bool}, x::PyCall.PyObject)
   @ SymPy C:\Users\svens\.julia\packages\SymPy\NveHe\src\python_connection.jl:16
 [4] convert(::Type{SymPyCore.Bool3}, x::Sym{PyCall.PyObject})
   @ SymPyCore C:\Users\svens\.julia\packages\SymPyCore\PayoX\src\equality.jl:51
 [5] ==(x::Sym{PyCall.PyObject}, y::Sym{PyCall.PyObject})
   @ SymPyCore C:\Users\svens\.julia\packages\SymPyCore\PayoX\src\equality.jl:35
 [6] iszero(x::Sym{PyCall.PyObject})
   @ Base .\number.jl:42
 [7] top-level scope
   @ REPL[5]:1
jverzani commented 8 months ago

Sorry, one more check. Can you run

julia> sympy.__version__
1.10.1

My guess is I need to update sympy seeing that deprecation warning.

cvsvensson commented 8 months ago
julia> sympy.__version__
1.6.2
jverzani commented 8 months ago

Ohh, that is quite old, I believe. Any chance you can update?

cvsvensson commented 8 months ago

I see, I wasn't aware that the versioning was decoupled like this. This solves the problem with iszero. But there is another problem in v2 now: multiplication with a sparse matrix gives a new sparse matrix with eltype Any. Multiplication with vectors works fine.

julia> @syms x

julia> x * sprand(2,.5) # multiplying by a sparse vector works fine
2-element SparseVector{Sym{PyCall.PyObject}, Int64} with 2 stored entries:
  [1]  =  0.455062571762008*x
  [2]  =  0.356216018302708*x

julia> m = x * sprand(2,2,.5);

julia> typeof(m)
SparseMatrixCSC{Any, Int64}

julia> m
2×2 SparseMatrixCSC{Any, Int64} with 2 stored entries:
Error showing value of type SparseMatrixCSC{Any, Int64}:
ERROR: MethodError: no method matching zero(::Type{Any})