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

Can't convert Num to Float64 #535

Closed
nathanrboyer opened this issue Feb 24, 2022 · 3 comments
Closed

Can't convert Num to Float64 #535

nathanrboyer opened this issue Feb 24, 2022 · 3 comments

Comments

@nathanrboyer
Copy link

The result of substitute sometimes is type Float and sometimes is type Num. If the output is Num, then I cannot seem to use it generally. (These are the same variables from the problem in #533.)

dict = Dict(P_i => 60, P_o => 0, r_i => 6/2, r_o => 20/2)
typeof(substitute(a_solution, dict)) # BigFloat
typeof(substitute(r_i, dict)) # Num
r_range = substitute(r_i, dict) : 0.1 : substitute(r_o, dict) # Error

Errror Output:

r_range =
Failed to show value:

MethodError: no method matching div(::Symbolics.Num, ::Symbolics.Num, ::RoundingMode{:ToZero})

Closest candidates are:

div(::Real, ::Real, ::RoundingMode) at C:\Users\nboyer.AIP\.julia\juliaup\julia-1.7.2+0~x64\share\julia\base\div.jl:288

div(::Number, !Matched::Missing, ::RoundingMode) at C:\Users\nboyer.AIP\.julia\juliaup\julia-1.7.2+0~x64\share\julia\base\missing.jl:130

div(!Matched::MultivariatePolynomials.AbstractPolynomialLike, ::Number, ::Any...) at C:\Users\nboyer.AIP\.julia\packages\MultivariatePolynomials\JG6mC\src\division.jl:10

...

div(::Symbolics.Num, ::Symbolics.Num, ::RoundingMode{:ToZero})@div.jl:289
div(::Symbolics.Num, ::Symbolics.Num)@div.jl:37
length(::StepRange{Symbolics.Num, Symbolics.Num})@range.jl:693
axes(::StepRange{Symbolics.Num, Symbolics.Num})@range.jl:643
axes@abstractarray.jl:74[inlined]
var"#_latexarray#30"(::Symbol, ::Bool, ::Bool, ::Bool, ::Base.Pairs{Symbol, Symbol, Tuple{Symbol}, NamedTuple{(:env,), Tuple{Symbol}}}, ::typeof(Latexify._latexarray), ::StepRange{Symbolics.Num, Symbolics.Num})@latexarray.jl:20
var"#_latexraw#9"(::Base.Pairs{Symbol, Symbol, Tuple{Symbol}, NamedTuple{(:env,), Tuple{Symbol}}}, ::typeof(Latexify._latexraw), ::StepRange{Symbolics.Num, Symbolics.Num})@latexraw.jl:112
var"#process_latexify#115"(::Base.Pairs{Symbol, Symbol, Tuple{Symbol}, NamedTuple{(:env,), Tuple{Symbol}}}, ::typeof(Latexify.process_latexify), ::StepRange{Symbolics.Num, Symbolics.Num})@latexify_function.jl:25
#latexraw#[email protected]:58[inlined]
latexraw@latexraw.jl:58[inlined]
var"#_latexequation#60"(::Bool, ::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ::typeof(Latexify._latexequation), ::StepRange{Symbolics.Num, Symbolics.Num})@latexequation.jl:5
_latexequation(::StepRange{Symbolics.Num, Symbolics.Num})@latexequation.jl:5
var"#process_latexify#115"(::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ::typeof(Latexify.process_latexify), ::StepRange{Symbolics.Num, Symbolics.Num})@latexify_function.jl:25
process_latexify@latexify_function.jl:18[inlined]
var"#latexify#114"(::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ::typeof(Latexify.latexify), ::StepRange{Symbolics.Num, Symbolics.Num})@latexify_function.jl:3
latexify@latexify_function.jl:2[inlined]
show(::IOBuffer, ::MIME{Symbol("text/latex")}, ::StepRange{Symbolics.Num, Symbolics.Num})@latexify_recipes.jl:85
__binrepr@iobuffer.jl:0[inlined]
var"#repr#1"(::Nothing, ::typeof(repr), ::MIME{Symbol("text/latex")}, ::StepRange{Symbolics.Num, Symbolics.Num})@multimedia.jl:147
repr(::MIME{Symbol("text/latex")}, ::StepRange{Symbolics.Num, Symbolics.Num})@multimedia.jl:147
show_richest(::IOContext{IOBuffer}, ::Any)@PlutoRunner.jl:1014
var"#sprint_withreturned#54"(::IOContext{Base.DevNull}, ::Int64, ::typeof(Main.PlutoRunner.sprint_withreturned), ::Function, ::StepRange{Symbolics.Num, Symbolics.Num})@PlutoRunner.jl:954
format_output_default(::Any, ::Any)@PlutoRunner.jl:862
var"#format_output#42"(::IOContext{Base.DevNull}, ::typeof(Main.PlutoRunner.format_output), ::Any)@PlutoRunner.jl:879
formatted_result_of(::Base.UUID, ::Bool, ::Vector{String}, ::Nothing, ::Module)@PlutoRunner.jl:785
top-level scope@none:1

Attempt to Fix:

BigFloat(substitute(r_i, dict)) # Error

Error Output:

MethodError: no method matching BigFloat(::Symbolics.Num)

Closest candidates are:

BigFloat(::Real, !Matched::RoundingMode; precision) at C:\Users\nboyer.AIP\.julia\juliaup\julia-1.7.2+0~x64\share\julia\base\mpfr.jl:262

(::Type{T})(::Real, !Matched::RoundingMode) where T<:AbstractFloat at C:\Users\nboyer.AIP\.julia\juliaup\julia-1.7.2+0~x64\share\julia\base\rounding.jl:200

BigFloat(::Real, !Matched::Int64) at C:\Users\nboyer.AIP\.julia\juliaup\julia-1.7.2+0~x64\share\julia\base\deprecated.jl:70

...

top-level scope@[Local: 1](http://localhost:1234/edit?id=c3a57e20-9583-11ec-2279-b500f3a93791#)[inlined]

I am also wondering if my approach is just wrong. In general the steps I am taking are:

  1. Define @variables x ...
  2. x_solution = solve_for(exp, x)
  3. x_solution_value = substitute(x_solution, dict_of_input_parameters)
  4. Use x_solution_value with other numerical data to make plots.

Keeping track of whether I should be using x, x_solution, or x_solution_value has been tough since they all represent the same value. I'd rather just transform x at some point in the code I guess, but then I can't use Pluto.jl.

@nathanrboyer
Copy link
Author

I found that you have to use the function Symbolics.value to convert Nums to usable Floats.

julia> typeof(Symbolics.value(substitute(r_i, dict)))
Float64

julia> r_range = Symbolics.value(substitute(r_i, dict)) : 0.1 : Symbolics.value(substitute(r_o, dict))
3.0:0.1:10.0

Couldn't the conversion be done automatically if there are no @variables left? The syntax is very verbose and not well documented.

help?> Symbolics.value
  No documentation found.

It is mentioned here on the website, but I can't make sense of it.

@andreasvarga
Copy link

I wonder what is the simplest way to recover a Float64 matrix from a constant Num matrix

Julia> S = Num.(zeros(2,2))
2×2 Matrix{Num}:
 0.0  0.0
 0.0  0.0

Substitution apparently is not working (there is no variable to substitute).

@Leebre
Copy link

Leebre commented Jul 21, 2023

@andreasvarga what I've done is to just create my own wrapper function:

function Float64(a::Num)
    b = Symbolics.value(a)
end

which then seems to work with:

A = Float64.(S)

but it would be nice if this was included in the Symbolics package by default.

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

3 participants