JuliaSymbolics / Symbolics.jl

Symbolic programming for the next generation of numerical software
https://docs.sciml.ai/Symbolics/stable/
Other
1.37k stars 154 forks source link

substituter error #523

Open isaacsas opened 2 years ago

isaacsas commented 2 years ago

It seems something has changed in substitute, breaking ex2 below

using ModelingToolkit
@parameters k
@variables t Y(t)
ex1 = Symbolics.unwrap(Y^2)
ex2 = Symbolics.unwrap(k*Y^2)
subber = Symbolics.substituter(Dict(Symbolics.unwrap(Y) => Symbolics.operation(Symbolics.unwrap(Y))))
subber(ex1)   # works and give  Y^2 instead of Y(t)^2

however the following now errors

julia> subber(ex2) 
Error showing value of type SymbolicUtils.Mul{Real, Int64, Dict{Any, Number}, Nothing}:
ERROR: MethodError: no method matching ^(::Sym{SymbolicUtils.FnType{Tuple{Any}, Real}, Nothing}, ::Int64)
Closest candidates are:
  ^(::Union{AbstractChar, AbstractString}, ::Integer) at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/strings/basic.jl:721
  ^(::Union{AbstractAlgebra.Generic.PuiseuxSeriesFieldElem{T}, AbstractAlgebra.Generic.PuiseuxSeriesRingElem{T}}, ::Int64) where T<:AbstractAlgebra.RingElement at ~/.julia/packages/AbstractAlgebra/KPT2D/src/generic/PuiseuxSeries.jl:509
  ^(::Union{AbstractAlgebra.Generic.LaurentSeriesFieldElem{T}, AbstractAlgebra.Generic.LaurentSeriesRingElem{T}}, ::Int64) where T<:AbstractAlgebra.RingElement at ~/.julia/packages/AbstractAlgebra/KPT2D/src/generic/LaurentSeries.jl:853
  ...
Stacktrace:
  [1] unstable_pow(a::Sym{SymbolicUtils.FnType{Tuple{Any}, Real}, Nothing}, b::Int64)
    @ SymbolicUtils ~/.julia/packages/SymbolicUtils/shh4H/src/types.jl:800
  [2] (::SymbolicUtils.var"#44#45")(::Pair{Any, Number})
    @ SymbolicUtils ./none:0
  [3] iterate
    @ ./generator.jl:47 [inlined]
  [4] collect(itr::Base.Generator{Dict{Any, Number}, SymbolicUtils.var"#44#45"})
    @ Base ./array.jl:724
  [5] arguments(a::SymbolicUtils.Mul{Real, Int64, Dict{Any, Number}, Nothing})
    @ SymbolicUtils ~/.julia/packages/SymbolicUtils/shh4H/src/types.jl:809
  [6] show_term(io::IOContext{Base.TTY}, t::SymbolicUtils.Mul{Real, Int64, Dict{Any, Number}, Nothing})
    @ SymbolicUtils ~/.julia/packages/SymbolicUtils/shh4H/src/types.jl:571
  [7] show
    @ ~/.julia/packages/SymbolicUtils/shh4H/src/types.jl:815 [inlined]
  [8] show(io::IOContext{Base.TTY}, #unused#::MIME{Symbol("text/plain")}, x::SymbolicUtils.Mul{Real, Int64, Dict{Any, Number}, Nothing})
    @ Base.Multimedia ./multimedia.jl:47
  [9] (::REPL.var"#43#44"{REPL.REPLDisplay{REPL.LineEditREPL}, MIME{Symbol("text/plain")}, Base.RefValue{Any}})(io::Any)
    @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:264
 [10] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
    @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:508
 [11] display(d::REPL.REPLDisplay, mime::MIME{Symbol("text/plain")}, x::Any)
    @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:257
 [12] display(d::REPL.REPLDisplay, x::Any)
    @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:269
 [13] display(x::Any)
    @ Base.Multimedia ./multimedia.jl:328
 [14] print_response(errio::IO, response::Any, show_value::Bool, have_color::Bool, specialdisplay::Union{Nothing, AbstractDisplay})
    @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:0
 [15] (::REPL.var"#45#46"{REPL.LineEditREPL, Pair{Any, Bool}, Bool, Bool})(io::Any)
    @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:275
 [16] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
    @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:508
 [17] print_response(repl::REPL.AbstractREPL, response::Any, show_value::Bool, have_color::Bool)
    @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:273
 [18] (::REPL.var"#do_respond#66"{Bool, Bool, REPL.var"#77#87"{REPL.LineEditREPL, REPL.REPLHistoryProvider}, REPL.LineEditREPL, REPL.LineEdit.Prompt})(s::REPL.LineEdit.MIState, buf::Any, ok::Bool)
    @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:844
 [19] #invokelatest#2
    @ ./essentials.jl:716 [inlined]
 [20] invokelatest
    @ ./essentials.jl:714 [inlined]
 [21] run_interface(terminal::REPL.Terminals.TextTerminal, m::REPL.LineEdit.ModalInterface, s::REPL.LineEdit.MIState)
    @ REPL.LineEdit /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:2493
 [22] run_frontend(repl::REPL.LineEditREPL, backend::REPL.REPLBackendRef)
    @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:1230
 [23] (::REPL.var"#49#54"{REPL.LineEditREPL, REPL.REPLBackendRef})()
    @ REPL ./task.jl:423

┆Issue is synchronized with this Trello card by Unito

fonsp commented 2 years ago

Note that subber(ex2); works, but the show method of the result triggers an error