I was trying to write the analyical solution of a cantilever beam problem as
a symbolic expressions and I ran into a problem when I had just finished substituting the variables by numerical values and I wanted to plot these values.
I have substituted all my expressions completely with values, i.e, there are no more variables, however the problem seems to be due to symbolic expressions being of type Num and Plots/Makie/other packages, seem not to be able to convert Nums to floats, int, rationals and etc… And honestly neither can I,
I tried converting my values using float(), convert(), eval(), build_function(), I ofcourse looked up if Symbolics.jl had something build in, but I only came past the mention of symbolic_to_float() in “Symbolics.jl/test/utils.jl” file on GitHub, but I could not use/find this function as it does not seem to be listed anywere.
I could rewrite my code and avoid using symbolics, but I would like to know what the propper/recommended way of converting these Nums would be.
Below I have added some code to demonstrate the errors I get with the simplest code possible together with a test function to show that it is not a syntax error. How to reproduce
using Symbolics
@variables a b
f(x) = a*x^2 + b
f_subs(x) = substitute(f(x), Dict(a=>1, b=>2))
f_subs(1), typeof(f_subs(1))
> (3, Num)
f_test(x) = x^2 + 2
f_test(1), typeof(f_test(1))
> (3, Int64)
Output with Plots.jl
using Plots
plot(f_test)
> # Plots normally
plot(f_subs)
> ERROR: MethodError: no method matching Float64(::Num)
>
> Closest candidates are:
(::Type{T})(::Real, ::RoundingMode) where T<:AbstractFloat
@ Base rounding.jl:207
(::Type{T})(::T) where T<:Number
@ Core boot.jl:792
(::Type{T})(::SymbolicUtils.Symbolic) where T<:Union{AbstractFloat, Integer, Complex{<:AbstractFloat}, Complex{<:Integer}}
@ Symbolics C:\Users\userName\.julia\packages\Symbolics\VIBnK\src\Symbolics.jl:146
...
Output with GLMakie.jl (Plots is not loaded)
using GLMakie
lines(f_test.(-5:5))
> # Plots normally
lines(f_subs.(-5:5))
> `Makie.convert_arguments` for the plot type Lines and its conversion trait PointBased() was unsuccessful.
The signature that could not be converted was:
::Vector{Num}
Makie needs to convert all plot input arguments to types that can be consumed by the backends (typically Arrays with Float32 elements).
You can define a method for `Makie.convert_arguments` (a type recipe) for these types or their supertypes to make this set of arguments convertible (See http://docs.makie.org/stable/documentation/recipes/index.html).
Alternatively, you can define `Makie.convert_single_argument` for single arguments which have types that are unknown to Makie but which can be converted to known types and fed back to the conversion pipeline.
Substrace:
...
caused by: MethodError: no method matching Float32(::Num)
...
etc.
```