From e49f153a9087e1ab20ee65a440a02c15a4939054 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 27 Aug 2024 10:17:24 -0400 Subject: [PATCH] Add symbolics_to_float converter --- docs/src/manual/expression_manipulation.md | 2 +- src/utils.jl | 24 ++++++++++++++++++++++ test/runtests.jl | 1 + test/utils.jl | 6 ++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/docs/src/manual/expression_manipulation.md b/docs/src/manual/expression_manipulation.md index 102aa985c..85c3be87d 100644 --- a/docs/src/manual/expression_manipulation.md +++ b/docs/src/manual/expression_manipulation.md @@ -25,7 +25,6 @@ Other additional manipulation functions are given below. Symbolics.get_variables Symbolics.tosymbol Symbolics.diff2term -Symbolics.solve_for Symbolics.degree Symbolics.coeff Symbolics.replace @@ -33,4 +32,5 @@ Symbolics.occursin Symbolics.filterchildren Symbolics.fixpoint_sub Symbolics.fast_substitute +Symbolics.symbolic_to_float ``` diff --git a/src/utils.jl b/src/utils.jl index 7628e1394..5e34e2d66 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -414,3 +414,27 @@ symbolic expressions mapping variables w.r.t pvar2sym function poly_to_symbol(polys, pvar2sym, sym2term, ::Type{T}) where {T} map(f -> PolyForm{T}(f, pvar2sym, sym2term), polys) end + +""" + symbolic_to_float(x::Union{Num, BasicSymbolic})::Union{AbstractFloat, BasicSymbolic} + +If the symbolic value is exactly equal to a number, converts the symbolic value +to a floating point number. Otherwise retains the symbolic value. + +## Examples + +```julia +symbolic_to_float((1//2 * x)/x) # 0.5 +symbolic_to_float((1/2 * x)/x) # 0.5 +symbolic_to_float((1//2)*√(279//4)) # 4.175823272122517 +``` +""" +function symbolic_to_float end +symbolic_to_float(x::Num) = symbolic_to_float(unwrap(x)) +function symbolic_to_float(x::SymbolicUtils.BasicSymbolic) + if _x isa Number + return _x + else + substitute(x,Dict()) + end +end diff --git a/test/runtests.jl b/test/runtests.jl index 4fc68f740..1aaafe4eb 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -27,6 +27,7 @@ if GROUP == "All" || GROUP == "Core" @safetestset "Semi-polynomial" begin include("semipoly.jl") end @safetestset "Fuzz Arrays" begin include("fuzz-arrays.jl") end @safetestset "Differentiation Test" begin include("diff.jl") end + @safetestset "Utils Test" begin include("utils.jl") end @safetestset "ADTypes Test" begin include("adtypes.jl") end @safetestset "Difference Test" begin include("difference.jl") end @safetestset "Degree Test" begin include("degree.jl") end diff --git a/test/utils.jl b/test/utils.jl index dfa0ff31c..386da8613 100644 --- a/test/utils.jl +++ b/test/utils.jl @@ -1,4 +1,5 @@ using Symbolics +using Symbolics: symbolic_to_float @testset "get_variables" begin @variables t x y z(t) @@ -19,3 +20,8 @@ using Symbolics sorted_vars2 = Symbolics.get_variables(ex2; sort = true) @test isequal(sorted_vars2, [x, y]) end + +symbolic_to_float((1//2 * x)/x) isa Float64 +symbolic_to_float((1/2 * x)/x) isa Float64 +symbolic_to_float((1//2)*√(279//4)) isa Float64 +symbolic_to_float((-1//2)*√(279//4)) isa Float64