From e49f153a9087e1ab20ee65a440a02c15a4939054 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 27 Aug 2024 10:17:24 -0400 Subject: [PATCH 1/4] 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 From b7b6add4651b170cfbe09d6f7f92dc9d769977e8 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 27 Aug 2024 11:12:51 -0400 Subject: [PATCH 2/4] Update test/utils.jl --- test/utils.jl | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/utils.jl b/test/utils.jl index 386da8613..26988bb96 100644 --- a/test/utils.jl +++ b/test/utils.jl @@ -21,7 +21,10 @@ using Symbolics: symbolic_to_float @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 +@testset "symbolic_to_float" begin + @variables x + 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 +end From 265ef47bd9eb30e08d11e1838f36307d9b5e80d2 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 27 Aug 2024 11:33:26 -0400 Subject: [PATCH 3/4] Update src/utils.jl --- src/utils.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utils.jl b/src/utils.jl index 5e34e2d66..c1aeaaa0b 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -431,6 +431,7 @@ symbolic_to_float((1//2)*√(279//4)) # 4.175823272122517 """ function symbolic_to_float end symbolic_to_float(x::Num) = symbolic_to_float(unwrap(x)) +symbolic_to_float(x::Number) = x function symbolic_to_float(x::SymbolicUtils.BasicSymbolic) if _x isa Number return _x From 1d60006f2742ecac281055c0d0bc2a1b1eabecdc Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 27 Aug 2024 11:39:06 -0400 Subject: [PATCH 4/4] Update utils.jl --- test/utils.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/utils.jl b/test/utils.jl index 26988bb96..c601f653b 100644 --- a/test/utils.jl +++ b/test/utils.jl @@ -26,5 +26,6 @@ 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((big(1)//2)*√(279//4)) isa BigFloat symbolic_to_float((-1//2)*√(279//4)) isa Float64 end