'julia.core.JuliaError: Exception 'MethodError: no method matching Float64(::Num)

Attempting to call the julia ModellingToolkit in python (using JuliaCall) and obtaining the following error when attempting to define symbolic variables.

from julia import Main as jl

jl.eval("using ModelingToolkit")
jl.eval("using DifferentialEquations")
jl.eval("@variables t")

Getting the following error as a result

julia.core.JuliaError: Exception 'MethodError: no method matching Float64(::Num)
Closest candidates are:
  (::Type{T})(::Real, !Matched::RoundingMode) where T<:AbstractFloat at ~/Julia/julia-1.7.2/share/julia/base/rounding.jl:200
  (::Type{T})(::T) where T<:Number at ~/Julia/julia-1.7.2/share/julia/base/boot.jl:770
  (::Type{T})(!Matched::VectorizationBase.Double{T}) where T<:Union{Float16, Float32, Float64, VectorizationBase.Vec{<:Any, <:Union{Float16, Float32, Float64}}, VectorizationBase.VecUnroll{var"#s35", var"#s34", var"#s33", V} where {var"#s35", var"#s34", var"#s33"<:Union{Float16, Float32, Float64}, V<:Union{Bool, Float16, Float32, Float64, Int16, Int32, Int64, Int8, UInt16, UInt32, UInt64, UInt8, SIMDTypes.Bit, VectorizationBase.AbstractSIMD{var"#s34", var"#s33"}}}} at ~/.julia/packages/VectorizationBase/xnzUX/src/special/double.jl:84
  ...' occurred while calling julia code:
convert(PyCall.PyObject, @variables t)


Solution 1:[1]

Seems that supertype(Num) yields Real and hence PyCall tries to convert it into Float64 before showing in Python.

A simple workaround would be to provide your own wrapper:

jl.eval("Float64(::Num) = NaN")

Now you will not be able to see object from ModelingToolkit but you can manipulate them in Python.

>>> jl.eval("@variables x y")
[nan, nan]
>>> jl.eval("@show simplify((x+y)^2 - (x-y)^2; expand=true)")
simplify((x + y) ^ 2 - (x - y) ^ 2; expand = true) = 4x*y
nan

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Przemyslaw Szufel