Symbolics.jl
Symbolics.jl copied to clipboard
Issues with rationals in symbolic expressions
Not sure if this should be two issues or just put up as one but, I will just put them up as one issue.
Firstly it seems that it is not possible to store a 1 as a rational in a symbolic equation, this can be demonstrated with the code
using Symbolics @variables x A = 1//1 * x B = Symbolics.coeff(A, x) println(B) typeof(B)
doing this results in a Int64 however one might expect a rational{Int64} like it will if you use A = 5//1 *x.
Secondly, if I try to simplify the expression
using Symbolics
@variables t
B = (((t^2)^3)*(((800//1)*(t^3) / ((800//1)*(t^3)
- (14//1)*t))^2)) / ((( (60//1)*(t^2))^2)
* ( (14//1)*t)^3)
B = Symbolics.simplify(B)
The result is
ERROR: MethodError: no method matching div(::PolyForm{Real}, ::Rational{Int64}, ::RoundingMode{:ToZero})
Closest candidates are:
div(::MultivariatePolynomials.AbstractPolynomialLike, ::Number, ::Any...)
@ MultivariatePolynomials ~/.julia/packages/MultivariatePolynomials/ckbfK/src/division.jl:8
div(::P, ::Real, ::RoundingMode) where P<:Dates.Period
@ Dates /usr/share/julia/stdlib/v1.9/Dates/src/periods.jl:81
div(::Integer, ::Rational, ::RoundingMode)
@ Base rational.jl:449
...
Stacktrace:
[1] div(a::PolyForm{Real}, b::Rational{Int64})
@ Base ./div.jl:47
[2] rm_gcds(ns::Vector{Any}, ds::Vector{Any})
@ SymbolicUtils ~/.julia/packages/SymbolicUtils/Oyu8Z/src/polyform.jl:538
[3] simplify_div(d::SymbolicUtils.BasicSymbolic{Real})
@ SymbolicUtils ~/.julia/packages/SymbolicUtils/Oyu8Z/src/polyform.jl:275
[4] (::SymbolicUtils.var"#sdiv#126")(a::SymbolicUtils.BasicSymbolic{Real})
@ SymbolicUtils ~/.julia/packages/SymbolicUtils/Oyu8Z/src/polyform.jl:328
[5] call_composed
@ ./operators.jl:1034 [inlined]
[6] (::ComposedFunction{SymbolicUtils.var"#sdiv#126", typeof(quick_cancel)})(x::SymbolicUtils.BasicSymbolic{Real}; kw::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
@ Base ./operators.jl:1031
[7] (::ComposedFunction{SymbolicUtils.var"#sdiv#126", typeof(quick_cancel)})(x::SymbolicUtils.BasicSymbolic{Real})
@ Base ./operators.jl:1031
[8] (::SymbolicUtils.Rewriters.Walk{:post, ComposedFunction{SymbolicUtils.var"#sdiv#126", typeof(quick_cancel)}, typeof(SymbolicUtils.frac_similarterm), false})(x::SymbolicUtils.BasicSymbolic{Real})
@ SymbolicUtils.Rewriters ~/.julia/packages/SymbolicUtils/Oyu8Z/src/rewriters.jl:200
[9] simplify_fractions(x::SymbolicUtils.BasicSymbolic{Real}; polyform::Bool)
@ SymbolicUtils ~/.julia/packages/SymbolicUtils/Oyu8Z/src/polyform.jl:330
[10] simplify_fractions(x::SymbolicUtils.BasicSymbolic{Real})
@ SymbolicUtils ~/.julia/packages/SymbolicUtils/Oyu8Z/src/polyform.jl:322
[11] simplify(x::SymbolicUtils.BasicSymbolic{Real}; expand::Bool, polynorm::Nothing, threaded::Bool, simplify_fractions::Bool, thread_subtree_cutoff::Int64, rewriter::Nothing)
@ SymbolicUtils ~/.julia/packages/SymbolicUtils/Oyu8Z/src/simplify.jl:42
[12] simplify(x::SymbolicUtils.BasicSymbolic{Real})
@ SymbolicUtils ~/.julia/packages/SymbolicUtils/Oyu8Z/src/simplify.jl:16
[13] simplify(n::Num; kw::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
@ Symbolics ~/.julia/packages/Symbolics/BQlmn/src/Symbolics.jl:146
[14] simplify(n::Num)
@ Symbolics ~/.julia/packages/Symbolics/BQlmn/src/Symbolics.jl:146
[15] top-level scope
@ Untitled-1:17
This is not the equation that originally gave me this error but this is as far as I could simplify it without losing the actual error message.
The Julia VERSION that is running is v"1.9.3" and the Symbolics version is Symbolics v5.5.1
This should probably be 2 issues.
the first one can be fixed by using @variables x::LiteralReal and then avoiding coeff but using semipolynomial_form:
julia> @variables x::LiteralReal
1-element Vector{Num}:
x
julia> Symbolics.semipolynomial_form(1//1 * x, [x], 1)
(Dict{Any, Any}(1 * x => 1//1), 0)
So it appears that the second issue was fixed since Symbolics v5.5.1, as it will now run with Symbolics v5.10.0 although there is quite a bit of type changes of the terms as the code
using Symbolics
@variables t
B = (((t^2)^3)*(((800//1)*(t^3) / ((800//1)*(t^3)
- (14//1)*t))^2)) / ((( (60//1)*(t^2))^2)
* ( (14//1)*t)^3)
B = Symbolics.simplify(B)
after getting everything updated, now leads to
(50(t^3)) / (3087((-7.0 + 400.0(t^2))^2))
still not what I was expecting since all the rational terms appear to have been changed to Int’s or Floats, but not the outright error that it was giving.
Sorry about that, for some reason I thought that I was up to date on symbolics.
Strangely enough this now appears more closely related to the first issue but maybe I just don’t know enough about what is going on here.
After JuliaAlgebra/MultivariatePolynomials.jl/pull/296
using Symbolics
@variables t
B = (((t^2)^3)*(((800//1)*(t^3) / ((800//1)*(t^3)
- (14//1)*t))^2)) / ((( (60//1)*(t^2))^2)
* ( (14//1)*t)^3)
B = Symbolics.simplify(B)
now gives rational coefficients:
(50(t^3)) / (3087((-(7//1) + (400//1)*(t^2))^2))
Maybe this can be closed?