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

Display function fails with "LoadError: MethodError: no method matching nameof(::Bool)" #1281

Open
orebas opened this issue Sep 19, 2024 · 0 comments

Comments

@orebas
Copy link

orebas commented Sep 19, 2024

To replicate this error, it is necessary to add some debug statements to the expand_derivatives function. In the below MWE, that function is modified from version 6.12.0 only by adding debug printing. It's about line 78 of the MWE, after the comment "#DEBUG PRINTING".

I was trying to debug #1262 and one of the intermediate variables fails to print. It has type "SymbolicUtils.BasicSymbolic{Real}". There might be two things going on

  1. This variable "occurrences" is in some corrupt state that doesn't stand a chance of being displayed
  2. It should be display()-able but there is a bug somewhere in the display code, perhaps in show_call or show_term

Here is the error output first; it includes a full dump() of the offending variable. I can't read it but hopefully someone can!

julia> include("mwe-modules.jl")
┌ Info: before loop
│   O = Differential(t)(b(t) - (Differential(t)(b(t))^2)*Differential(t)(Differential(t)(b(t))))
└   line 79 = "line 79"
SymbolicUtils.BasicSymbolic{Real}
SymbolicUtils.BasicSymbolic{Real}
  metadata: Nothing nothing
  ###simplified###2: Bool false
  ###Any###3: Bool true
  ###Any###4: Array{Any}((2,))
    1: SymbolicUtils.BasicSymbolic{Real}
      metadata: Nothing nothing
      ###simplified###2: Bool false
      ###Any###3: Bool true
      ###Any###4: Array{Any}((1,))
        1: Bool true
      ###Any###5: Base.RefValue{UInt64}
        x: UInt64 0x0000000000000000
      ###Any###6: Array{Any}((0,))
      ###Any###7: Base.RefValue{Bool}
        x: Bool false
      ###tag###8: SymbolicUtils.var"###BasicSymbolic###1" SymbolicUtils.₋₃₋₁₂₉BasicSymbolic₋__Term₋₃₋₁₉₉₂₋₋
    2: SymbolicUtils.BasicSymbolic{Real}
      metadata: Nothing nothing
      ###simplified###2: Bool false
      ###Any###3: Bool true
      ###Any###4: Array{Any}((3,))
        1: Bool false
        2: SymbolicUtils.BasicSymbolic{Real}
          metadata: Nothing nothing
          ###simplified###2: Bool false
          ###Any###3: Bool true
          ###Any###4: Array{Any}((1,))
            1: SymbolicUtils.BasicSymbolic{Real}
              metadata: Nothing nothing
              ###simplified###2: Bool false
              ###Any###3: Bool true
              ###Any###4: Array{Any}((1,))
                1: SymbolicUtils.BasicSymbolic{Real}
              ###Any###5: Base.RefValue{UInt64}
                x: UInt64 0x0000000000000000
              ###Any###6: Array{Any}((0,))
              ###Any###7: Base.RefValue{Bool}
                x: Bool false
              ###tag###8: SymbolicUtils.var"###BasicSymbolic###1" SymbolicUtils.₋₃₋₁₂₉BasicSymbolic₋__Term₋₃₋₁₉₉₂₋₋
          ###Any###5: Base.RefValue{UInt64}
            x: UInt64 0x0000000000000000
          ###Any###6: Array{Any}((0,))
          ###Any###7: Base.RefValue{Bool}
            x: Bool false
          ###tag###8: SymbolicUtils.var"###BasicSymbolic###1" SymbolicUtils.₋₃₋₁₂₉BasicSymbolic₋__Term₋₃₋₁₉₉₂₋₋
        3: SymbolicUtils.BasicSymbolic{Real}
          metadata: Nothing nothing
          ###simplified###2: Bool false
          ###Any###3: Bool true
          ###Any###4: Array{Any}((2,))
            1: SymbolicUtils.BasicSymbolic{Real}
              metadata: Nothing nothing
              ###simplified###2: Bool false
              ###Any###3: Bool true
              ###Any###4: Array{Any}((1,))
                1: SymbolicUtils.BasicSymbolic{Real}
              ###Any###5: Base.RefValue{UInt64}
                x: UInt64 0x0000000000000000
              ###Any###6: Array{Any}((0,))
              ###Any###7: Base.RefValue{Bool}
                x: Bool false
              ###tag###8: SymbolicUtils.var"###BasicSymbolic###1" SymbolicUtils.₋₃₋₁₂₉BasicSymbolic₋__Term₋₃₋₁₉₉₂₋₋
            2: Bool false
          ###Any###5: Base.RefValue{UInt64}
            x: UInt64 0x0000000000000000
          ###Any###6: Array{Any}((0,))
          ###Any###7: Base.RefValue{Bool}
            x: Bool false
          ###tag###8: SymbolicUtils.var"###BasicSymbolic###1" SymbolicUtils.₋₃₋₁₂₉BasicSymbolic₋__Term₋₃₋₁₉₉₂₋₋
      ###Any###5: Base.RefValue{UInt64}
        x: UInt64 0x0000000000000000
      ###Any###6: Array{Any}((0,))
      ###Any###7: Base.RefValue{Bool}
        x: Bool false
      ###tag###8: SymbolicUtils.var"###BasicSymbolic###1" SymbolicUtils.₋₃₋₁₂₉BasicSymbolic₋__Term₋₃₋₁₉₉₂₋₋
  ###Any###5: Base.RefValue{UInt64}
    x: UInt64 0x0000000000000000
  ###Any###6: Array{Any}((0,))
  ###Any###7: Base.RefValue{Bool}
    x: Bool false
  ###tag###8: SymbolicUtils.var"###BasicSymbolic###1" SymbolicUtils.₋₃₋₁₂₉BasicSymbolic₋__Term₋₃₋₁₉₉₂₋₋
ERROR: LoadError: MethodError: no method matching nameof(::Bool)
The function `nameof` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  nameof(::JuliaInterpreter.Frame)
   @ JuliaInterpreter ~/.julia/packages/JuliaInterpreter/cxlKp/src/utils.jl:17
  nameof(::Core.IntrinsicFunction)
   @ Base reflection.jl:2273
  nameof(::Differential)
   @ Symbolics ~/.julia/packages/Symbolics/XnDVB/src/diff.jl:58
  ...

Stacktrace:
  [1] show_call(io::IOContext{Base.TTY}, f::Bool, args::Vector{Any})
    @ SymbolicUtils ~/.julia/packages/SymbolicUtils/ij6YM/src/types.jl:820
  [2] show_term(io::IOContext{Base.TTY}, t::SymbolicUtils.BasicSymbolic{Real})
    @ SymbolicUtils ~/.julia/packages/SymbolicUtils/ij6YM/src/types.jl:865
  [3] show
    @ ~/.julia/packages/SymbolicUtils/ij6YM/src/types.jl:878 [inlined]
  [4] show(io::IOContext{Base.TTY}, ::MIME{Symbol("text/plain")}, x::SymbolicUtils.BasicSymbolic{Real})
    @ Base.Multimedia ./multimedia.jl:47
  [5] (::REPL.var"#68#69"{REPL.REPLDisplay{REPL.LineEditREPL}, MIME{Symbol("text/plain")}, Base.RefValue{Any}})(io::Any)
    @ REPL ~/.julia/juliaup/julia-1.11.0-rc3+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:348
  [6] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
    @ REPL ~/.julia/juliaup/julia-1.11.0-rc3+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:646
  [7] display(d::REPL.REPLDisplay, mime::MIME{Symbol("text/plain")}, x::Any)
    @ REPL ~/.julia/juliaup/julia-1.11.0-rc3+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:334
  [8] display
    @ ~/.julia/juliaup/julia-1.11.0-rc3+0.x64.linux.gnu/share/julia/stdlib/v1.11/REPL/src/REPL.jl:353 [inlined]
  [9] display(x::Any)
    @ Base.Multimedia ./multimedia.jl:340
 [10] expand_derivatives(O::SymbolicUtils.BasicSymbolic{Real}, simplify::Bool; occurrences::Nothing)
    @ Symbolics ~/MWE-ex/mwe-modules.jl:82
 [11] expand_derivatives(n::Num, simplify::Bool; occurrences::Nothing)
    @ Symbolics ~/.julia/packages/Symbolics/XnDVB/src/diff.jl:299
 [12] expand_derivatives
    @ ~/.julia/packages/Symbolics/XnDVB/src/diff.jl:298 [inlined]
 [13] expand_derivatives(n::Num)
    @ Symbolics ~/.julia/packages/Symbolics/XnDVB/src/diff.jl:298
 [14] main()
    @ Main ~/MWE-ex/mwe-modules.jl:138
 [15] top-level scope
    @ ~/MWE-ex/mwe-modules.jl:141
 [16] include(fname::String)
    @ Main ./sysimg.jl:38
 [17] top-level scope
    @ REPL[3]:1
in expression starting at /home/orebas/MWE-ex/mwe-modules.jl:141

MWE:

using Symbolics

@eval Symbolics begin
function expand_derivatives(O::Symbolics.Symbolic, simplify = false; occurrences = nothing)
    if iscall(O) && isa(operation(O), Differential)
        arg = only(arguments(O))
        arg = expand_derivatives(arg, false)

        if occurrences == nothing
            occurrences = occursin_info(operation(O).x, arg)
        end

        _isfalse(occurrences) && return 0
        occurrences isa Bool && return 1 # means it's a `true`

        D = operation(O)

        if !iscall(arg)
            return D(arg) # Cannot expand
        elseif (op = operation(arg); issym(op))
            inner_args = arguments(arg)
            if any(isequal(D.x), inner_args)
                return D(arg) # base case if any argument is directly equal to the i.v.
            else
                return sum(inner_args, init = 0) do a
                    return expand_derivatives(Differential(a)(arg)) *
                           expand_derivatives(D(a))
                end
            end
        elseif op === (IfElse.ifelse)
            args = arguments(arg)
            O = op(args[1], D(args[2]), D(args[3]))
            return expand_derivatives(O, simplify; occurrences)
        elseif isa(op, Differential)
            # The recursive expand_derivatives was not able to remove
            # a nested Differential. We can attempt to differentiate the
            # inner expression wrt to the outer iv. And leave the
            # unexpandable Differential outside.
            if isequal(op.x, D.x)
                return D(arg)
            else
                inner = expand_derivatives(D(arguments(arg)[1]), false)
                # if the inner expression is not expandable either, return
                if iscall(inner) && operation(inner) isa Differential
                    return D(arg)
                else
                    return expand_derivatives(op(inner), simplify)
                end
            end
        elseif isa(op, Integral)
            if isa(op.domain.domain, AbstractInterval)
                domain = op.domain.domain
                a, b = DomainSets.endpoints(domain)
                c = 0
                inner_function = expand_derivatives(arguments(arg)[1])
                if iscall(value(a))
                    t1 = SymbolicUtils.substitute(
                        inner_function, Dict(op.domain.variables => value(a)))
                    t2 = D(a)
                    c -= t1 * t2
                end
                if iscall(value(b))
                    t1 = SymbolicUtils.substitute(
                        inner_function, Dict(op.domain.variables => value(b)))
                    t2 = D(b)
                    c += t1 * t2
                end
                inner = expand_derivatives(D(arguments(arg)[1]))
                c += op(inner)
                return value(c)
            end
        end

        inner_args = arguments(arg)
        l = length(inner_args)
        exprs = []
        c = 0
         #DEBUG PRINTING

        @info "before loop" O "line 79"
        println(typeof(occurrences))
        dump(occurrences)
        display(occurrences)

        for i in 1:l
            t2 = expand_derivatives(D(inner_args[i]),false, occurrences=arguments(occurrences)[i])

            x = if _iszero(t2)
                t2
            elseif _isone(t2)
                d = derivative_idx(arg, i)
                d isa NoDeriv ? D(arg) : d
            else
                t1 = derivative_idx(arg, i)
                t1 = t1 isa NoDeriv ? D(arg) : t1
                t1 * t2
            end

            if _iszero(x)
                continue
            elseif x isa Symbolic
                push!(exprs, x)
            else
                c += x
            end
        end

        if isempty(exprs)
            return c
        elseif length(exprs) == 1
            term = (simplify ? SymbolicUtils.simplify(exprs[1]) : exprs[1])
            return _iszero(c) ? term : c + term
        else
            x = +((!_iszero(c) ? vcat(c, exprs) : exprs)...)
            return simplify ? SymbolicUtils.simplify(x) : x
        end
    elseif iscall(O) && isa(operation(O), Integral)
        return operation(O)(expand_derivatives(arguments(O)[1]))
    elseif !hasderiv(O)
        return O
    else
        args = map(a -> expand_derivatives(a, false), arguments(O))
        O1 = operation(O)(args...)
        return simplify ? SymbolicUtils.simplify(O1) : O1
    end
end
end


function main()
	@variables t  b(t)
	D = Differential(t)
	expr = b - 	D(b) * D(b) * D(D(b))
	expr2 = D(expr)
	#display(expr)
	#display(expand_derivatives(expr))
	
	#display(expr2)
	expand_derivatives(expr2)

end
main()
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

1 participant