mathnet / mathnet-symbolics

Math.NET Symbolics
http://symbolics.mathdotnet.com
MIT License
341 stars 66 forks source link

Rational.Simplify gets stuck in endless loop #84

Closed mkruijver closed 3 years ago

mkruijver commented 3 years ago

When attempting to simplify a fraction using Rational.Simplify, the library seems to get stuck in an endless loop:

var numerator = Infix.ParseOrThrow("0.1*x");
var denominator = Infix.ParseOrThrow("x+x^2");
var fraction = numerator / denominator;

var x = Infix.ParseOrThrow("x");
var simplified = Rational.Simplify(x, fraction); // hangs
cdrnet commented 3 years ago

There must be something wrong with how we handle approximations like 0.1 there. Indeed, when replacing 0.1*x with x/10 simplify returns 1/(10 + 10*x) as expected - but of course that's a very different expression and thus simplification problem. I guess the desired expression would be something along the lines of 0.1/(1.0 + x)?

cdrnet commented 3 years ago

This is fixed in v0.22.0. Thanks for reporting!

mkruijver commented 3 years ago

Thanks for the fix. The above example now works fine. Unfortunately I still experience problems with slightly different problems, e.g.:

var numerator = Infix.ParseOrThrow("a + a*b + a^2*b + a*b^2");
var denominator = Infix.ParseOrThrow("a + a*b + a^2*b + a^2");

var fraction = numerator / denominator;

var simplified1 = Rational.Simplify(Infix.ParseOrThrow("a"), fraction); // result is a long expression
var simplified2 = Rational.Simplify(Infix.ParseOrThrow("b"), simplified1); // does not seem to finish

For this (and also the earlier example) the exact output is not that important to me, however it is important in my use case that the software does not hang unexpectedly.