diff --git a/README.md b/README.md new file mode 100644 index 0000000..ac733ea --- /dev/null +++ b/README.md @@ -0,0 +1,57 @@ +# Symbolic solver for polynomials with rational coefficients # + +## Program output ## + + Equation: (x + 50) * 10 - 150 = 100 + Parsed: (- (- (* (+ x 50) 10) 150) 100) + Factorized normal form: 10(25 + 1x) + Solutions: { x = -25 } + + Equation: (x - 2) * (x + 2) = 0 + Parsed: (- (* (- x 2) (+ x 2)) 0) + Factorized normal form: (-2 + 1x)(2 + 1x) + Solutions: { x = 2, x = -2 } + + Equation: x ^ 2 = 4 + Parsed: (- (^ x 2) 4) + Factorized normal form: (-4 + 0x + 1x^2) + Solutions: { x = 2, x = -2 } + + Equation: x ^ 2 - 2 = 0 + Parsed: (- (- (^ x 2) 2) 0) + Factorized normal form: (-2 + 0x + 1x^2) + Solutions: { x = 2 ^ (1/2), x = -2 ^ (1/2) } + + Equation: x ^ 2 = 2 * x + 15 + Parsed: (- (^ x 2) (+ (* 2 x) 15)) + Factorized normal form: (-15 + -2x + 1x^2) + Solutions: { x = 5, x = -3 } + + Equation: (x ^ 2 - 2 * x - 15) * (x + 5) = 0 + Parsed: (- (* (- (- (^ x 2) (* 2 x)) 15) (+ x 5)) 0) + Factorized normal form: (-15 + -2x + 1x^2)(5 + 1x) + Solutions: { x = 5, x = -3, x = -5 } + + Equation: x ^ 3 + 3 * x ^ 2 - 25 * x - 75 = 0 + Parsed: (- (- (- (+ (^ x 3) (* 3 (^ x 2))) (* 25 x)) 75) 0) + Factorized normal form: (3 + 1x)(-25 + 0x + 1x^2) + Solutions: { x = -3, x = 5, x = -5 } + + Equation: x ^ 3 - 91 * x - 90 = 0 + Parsed: (- (- (- (^ x 3) (* 91 x)) 90) 0) + Factorized normal form: (-90 + -91x + 0x^2 + 1x^3) + Guessing rational solutions: 10, -9, -1 + Verified guessed solutions! + Solutions: { x = 10, x = -9, x = -1 } + +## Notes ## + +This uses an egraph with rewrite rules representing the usual rules of arithmetic, and constant folding with rational numbers. This alone is enough to solve linear equations, by just starting with the difference of the two sides and minimizing AST size. However, it can't suffice for quadratic equations, as the egraph can't create values "out of this air". For example, to solve `x^2 = 2` the egraph must at least have "seen" the expression `2^(1/2)` once. + +So instead, the egraph is used to bring the expression into a standard form: a product of monic polynomials which is factored as much as possible. Since this is difficult to model with a cost function, I wrote a custom extractor (in `normal_form.rs`). + +Then we can use the usual quadratic formula to find solutions. The obtained solutions are afterwards simplified by another egraph, with a special "square root of a square integer" rule. This is pretty minimal, a more complete solution would also be able to deal with square roots of rational numbers etc. + +Finally, the unfactorized cubic equation from the examples gets factored into a linear and a quadratic factor by the egraph. This is kind of "magic" though (the right factor just seems to randomly come up elsewhere), and for general cubics we can't expect that to happen, even if they have integer solutions. The additional last equation is an example for this. So we use another strategy to deal with cubic equations with rational solutions: they are solved numerically by the general cubic formula, the numerical solutions are then approximated by rational numbers using continued fractions, and finally the result is verified by using an egraph to see if the obtained factization is equivalent to the original polynomial. + +This has essentially only been tested on the 8 examples above. So I would expect there to still be a few bugs.