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

Simplified derivative expansion gives float coefficients #1083

Closed
hersle opened this issue Mar 1, 2024 · 7 comments
Closed

Simplified derivative expansion gives float coefficients #1083

hersle opened this issue Mar 1, 2024 · 7 comments

Comments

@hersle
Copy link
Contributor

hersle commented Mar 1, 2024

The simplified derivative expansion

using Symbolics
@variables t a(t)
D(expr) = expand_derivatives(Differential(t)(expr))
simplify_fractions(D(D(a)/a) + D(D(a)/a))

outputs

(2.0a(t)*Differential(t)(Differential(t)(a(t))) - 2.0(Differential(t)(a(t))^2)) / (a(t)^2)

Why are the coefficients 2.0 instead of 2? I would like them to be exact integers/rationals instead of floats. Thanks!

@ChrisRackauckas
Copy link
Member

It's a bug, we should find how what causes it.

@hersle
Copy link
Contributor Author

hersle commented Mar 2, 2024

Here is the most minimal example I am able to reduce it to:

using Symbolics
@variables a b
simplify(a/b + 2*a^2/b)

This outputs (a + 2.0(a^2)) / b, but I want (a + 2(a^2)) / b. Replacing simplify with simplify_fractions changes nothing.

With expand(a/b + 2*a^2/b) instead, I get a / b + (2(a^2)) / b, so perhaps the problem is related to how simplify puts everything in one fraction?

@ChrisRackauckas
Copy link
Member

Possibly, and then it evaluates the fraction with / which gives a float back for two ints.

@hersle
Copy link
Contributor Author

hersle commented Mar 4, 2024

I've done some digging. It seems that even

using SymbolicUtils
@syms x
pf = PolyForm(x) # polynomial 1*x^1
div(pf, pf) # x / x

outputs 1.0 instead of 1.

Digging into in SymbolicUtils.jl's polyform.jl, it seems that it ultimately relies on DynamicPolynomials.jl to do the division.

There is an identical open/unresolved issue in that library. But it seems to me that this SymbolicUtils.jl commit was made to provide a workaround?

What is the way to go about this? I suppose the ideal way would be to fix it upstream?

@bowenszhu
Copy link
Member

@hersle
Copy link
Contributor Author

hersle commented Apr 29, 2024

All examples in this issue would be fixed by JuliaAlgebra/MultivariatePolynomials.jl#294.

@hersle
Copy link
Contributor Author

hersle commented May 9, 2024

This was fixed by JuliaAlgebra/MultivariatePolynomials.jl/pull/296 😃

using Symbolics
@variables t a(t)
D(expr) = expand_derivatives(Differential(t)(expr))
simplify_fractions(D(D(a)/a) + D(D(a)/a))

now gives rational coefficients:

((2//1)*a(t)*Differential(t)(Differential(t)(a(t))) - (2//1)*(Differential(t)(a(t))^2)) / (a(t)^2)

Similarly,

using Symbolics
@variables a b
simplify(a/b + 2*a^2/b)

gives

(a + (2//1)*(a^2)) / b

@hersle hersle closed this as completed May 9, 2024
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