Skip to content

solve_for gives Float64 instead of rational solutions #524

@knuesel

Description

@knuesel

For example the following gives Float64 instead of rational values:

julia> @variables x y;

julia> Symbolics.solve_for([x + y ~ 2, x - y ~ 1], [x, y])
2-element Vector{Float64}:
 1.5
 0.5

julia> Symbolics.solve_for([x + y ~ 2//1, x - y ~ 1//1], [x, y])
2-element Vector{Float64}:
 1.5
 0.5

Here using rational coefficients makes no difference.

Note that the behavior depends on the values of the coefficients. Here's a case where using rational literals results in a mix of Rational and Float64:

julia> Symbolics.solve_for([x + y ~ 3, x - y ~ 1], [x, y])
2-element Vector{Float64}:
 2.0
 1.0

julia> Symbolics.solve_for([x + y ~ 3//1, x - y ~ 1//1], [x, y])
2-element Vector{Real}:
 2
  1.0

And here's one where a rational coefficient gives all-rational output:

julia> Symbolics.solve_for([x + 2y ~ 2, x - y ~ 1], [x, y])
2-element Vector{Float64}:
 1.3333333333333335
 0.3333333333333333

julia> Symbolics.solve_for([x + 2//1*y ~ 2, x - y ~ 1], [x, y])
2-element Vector{Rational{Int64}}:
 4/3
 1/3

┆Issue is synchronized with this Trello card by Unito

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions