Repository navigation
Commit bcfefc2
* Solve a polynomial through its factors rather than by its degree (#272)
The solver read a polynomial equation only as a polynomial: it gathered the
monomials, took the degree, and applied the formula for that degree -- Cardano at
three, Ferrari at four, nothing at all above four. A factorization was never
looked for, and one handed over outright was expanded away before the degree was
taken. So (x - 1)(x^2 - 3) = 0, whose roots are written on its face, came back as
two nested cube roots of 26 + 18i, and a quintic that splits into a cubic's worth
of whole roots and a quadratic fell through to the numeric solver.
Two things are added. A product is now solved factor by factor, since a product
is zero exactly where one of its factors is; and where the equation is written
out as a sum, its rational roots are divided out first, which leaves a factor of
lower degree for the formulas to answer exactly.
Zeroing one factor only makes the product zero where the others are defined, so
each root is checked against the whole product before it is kept -- x * (1/x) is
undefined at 0, not zero there, and the verification the solvers share cannot
catch that, since it drops a root only on positive evidence and a NaN residual is
not evidence.
Two-term polynomials are left alone. a*x^n + b is answered whole by inverting
x^n = -b/a, which gives the roots as a + bi; splitting it off at its rational
roots leaves the rest to be dug out of a quotient, and x^3 - 8 would read
(-2 -+ sqrt(-12))/2 rather than (-1/2 +- i*sqrt(3)/2)*2. A test pins that.
lim of the measured effect, against 40ce109:
x^3 - x^2 - 3x + 3 = 0 -(-1 + (26 + 18i)^(1/3) + 10/(26 + 18i)^(1/3))/3, ...
-> { 1, sqrt(3), -sqrt(3) } 160 ms -> 9 ms
x^5 - x^4 - 7x^3 + 7x^2 + 12x - 12 = 0
{ -2, -1.73205080756887741... - 2.3e-15i, 1, ... }
-> { 1, 2, -2, sqrt(3), -sqrt(3) } 153 ms -> 12 ms
(x - 1)(x^2 - 3) = 0 -> { 1, sqrt(3), -sqrt(3) } 138 ms -> 8 ms
x^3 - 6x^2 + 11x - 6 = 0-> { 1, 2, 3 } 160 ms -> 1 ms
4929 unit tests and 130 F# tests pass; corpus 112/117 with 0 wrong, 0 error and
0 timeout, unchanged.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* Pin the roots the split turns out to recover
A sweep over 572 polynomials built from factors whose roots are known in advance
-- every product and every expansion of two or three factors drawn from a pool of
linear and irreducible quadratic ones -- says the fix does more than tidy the
output. Before it, ten of those came back **short of roots**, and after it none
do; no case gained a root that is not one, in either direction.
Every one of the ten had the same shape, a rational root times a quadratic times
x^2 + x + 1:
x^5 - 2x^3 - x^2 + 2 = 0 { 1, sqrt(2), -sqrt(2) }
-> all five, the pair (-1 +- i*sqrt(3))/2 included
Above degree four the equation went to the numeric solver whole, and that
searches the real axis, so the complex conjugate pair was simply absent -- and a
set two roots short reads exactly like a complete one. Splitting off the rational
root leaves a quartic, which is answered in one piece.
This is the stronger half of #272 and it was not visible from the issue, which
reports the answers as ugly rather than as incomplete.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
1 parent 61af68a commit bcfefc2
3 files changed
Lines changed: 274 additions & 0 deletions
File tree
- Sources
- AngouriMath/Functions
- Continuous/Solvers/EquationSolver
- Simplification
- Tests/UnitTests/Algebra/SolveTest
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
143 | 177 | | |
144 | 178 | | |
145 | 179 | | |
146 | 180 | | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
147 | 190 | | |
148 | 191 | | |
149 | 192 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
108 | 164 | | |
109 | 165 | | |
110 | 166 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
0 commit comments