Skip to content

Commit c937aae

Browse files
The square root taken after solving for x^2 is exact where it can be (#2173)
A polynomial whose powers share a factor is solved in x^gcd, and its roots are the gcd-th roots of what that gives. With the factor 2 that is a square root, written sqrt(y) whatever y was, so an equation with no term in x kept the square under the root that #2170 took out of the formula: x^2 = (a + b)^2 was { sqrt((a + b)^2), -sqrt((a + b)^2) }. x^2 = y is the quadratic x^2 - y = 0, so with the factor 2 each y goes through the same exact root as the formula, and keeps sqrt where it is not a rational number times a square. The parity check in front refuses a y of odd degree in a symbol, such as x^2 = a, before any polynomial is built. On 2.6.0 and on this branch, Solve: x^2 = (a + b)^2 sqrt form -> { -a - b, a + b } x^4 - (a^2 + b^2)*x^2 + a^2*b^2 = 0 nested sqrt -> { -b, b, -a, a } sin(x)^2 = (a + 1)^2 arcsin(sqrt((a + 1)^2)) -> arcsin(a + 1) The eigenvalues of [[0, J], [J, 0]] are J and -J instead of +-sqrt(J^2); Eigenvalues is new since 2.6.0, so its test and doc comment are updated and no released answer changes there. Twenty-three equations had every root substituted back at sampled values of the other symbols, with residual 0. Unit suite 17,144 passed, 0 failed; RootCheck 596/596 clean; CasBench 120/121, unchanged. Part of #2140 Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 8a85f8c commit c937aae

5 files changed

Lines changed: 37 additions & 6 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ one inside another was answered with the empty set, a claim that it has no solut
6262
**Silent: the same roots, written as what they are.** The quadratic formula wrote `sqrt(b^2 - 4ac)` whatever the
6363
discriminant was, so a symbolic quadratic with a square discriminant came back with the square under the root.
6464
Either square root gives the same pair of roots, so where the coefficients are polynomials and the discriminant is
65-
a rational number times the square of one, the roots are written with that polynomial and reduced over `2a`
65+
a rational number times the square of one, the roots are written with that polynomial and reduced over `2a`. So is
66+
the square root the solver takes after solving for `x^2`, where an equation has no term in `x`
6667
([#2140](https://github.com/asc-community/AngouriMath/issues/2140)).
6768

6869
| Input | Was (2.6.0) | Now |
@@ -71,6 +72,9 @@ a rational number times the square of one, the roots are written with that polyn
7172
| `"x^2 - (p + q)*x + p*q = 0".Solve("x")` | `{ (p + q - sqrt((-(p + q)) ^ 2 - 4 * p * q)) / 2, … }` | `{ q, p }` |
7273
| `"x^2 - 2*a*x + a^2 - 2 = 0".Solve("x")` | `{ (-(-2) * a - sqrt(((-2) * a) ^ 2 - 4 * (a ^ 2 + -2))) / 2, … }` | `{ a - sqrt(2), a + sqrt(2) }` |
7374
| `"2*x^2 - 2*(p + q)*x + p^2 + q^2 = 0".Solve("x")` | `{ (-(-2) * (p + q) - sqrt(((-2) * (p + q)) ^ 2 - 8 * (p ^ 2 + q ^ 2))) / 4, … }` | `{ 1/2 * p + 1/2 * q - i * (1/2 * p - 1/2 * q), … }` |
75+
| `"x^2 = (a + b)^2".Solve("x")` | `{ sqrt((a + b) ^ 2), -sqrt((a + b) ^ 2) }` | `{ -a - b, a + b }` |
76+
| `"x^4 - (a^2 + b^2)*x^2 + a^2*b^2 = 0".Solve("x")` | `{ sqrt((a ^ 2 + b ^ 2 - sqrt((-(a ^ 2 + b ^ 2)) ^ 2 - 4 * a ^ 2 * b ^ 2)) / 2), … }` | `{ -b, b, -a, a }` |
77+
| `"sin(x)^2 = (a + 1)^2".Solve("x")` | `{ arcsin(sqrt((a + 1) ^ 2)) + 2 * pi * n_1, … }` | `{ arcsin(-a - 1) + 2 * pi * n_1, … }` |
7478

7579
### A system with a product equation is solved branch by branch
7680

‎Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,8 @@ public Entity AsScalar()
383383
/// </summary>
384384
/// <remarks>
385385
/// The characteristic polynomial divides by nothing, so its roots carry no condition an
386-
/// elimination's pivots would bring: <c>[[0, J], [J, 0]]</c> has <c>sqrt(J^2)</c> and
387-
/// <c>-sqrt(J^2)</c>, which hold at <c>J = 0</c> too. Up to the fourth degree the roots are
386+
/// elimination's pivots would bring: <c>[[0, J], [J, 0]]</c> has <c>J</c> and <c>-J</c>,
387+
/// which hold at <c>J = 0</c> too. Up to the fourth degree the roots are
388388
/// in radicals, and past it where the polynomial factors; otherwise the set is the one the
389389
/// equation solver leaves.
390390
/// <a href="https://github.com/asc-community/AngouriMath/issues/1676">#1676</a>

‎Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/PolynomialSolver.cs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,14 @@ Entity GetMonomialByPower(EInteger power) =>
400400
} is { } resConcat
401401
? !gcdPower.Equals(EInteger.One)
402402
// if we had x^6 + x^3 + 1, we replace it with x^2 + x + 1 and find all cubic roots of the final answer
403-
? res.Concat(resConcat).SelectMany(_ => gcdPowerRoots,
404-
(root, coef) => coef * MathS.Pow(root, Rational.Create(1, gcdPower)))
403+
// x^2 = y is a quadratic in x, so a y that is a rational number times a square has
404+
// its two roots written without the root, as the formula's are: x^2 = (a + b)^2 was
405+
// { sqrt((a + b)^2), -sqrt((a + b)^2) } and is { -a - b, a + b }.
406+
// https://github.com/asc-community/AngouriMath/issues/2140
407+
? res.Concat(resConcat).SelectMany(root =>
408+
gcdPower.Equals(EInteger.FromInt32(2)) && RootsOverASquareDiscriminant(1, 0, -root) is { } exact
409+
? exact
410+
: gcdPowerRoots.Select(coef => coef * MathS.Pow(root, Rational.Create(1, gcdPower))))
405411
: res.Concat(resConcat)
406412
: null;
407413
}

‎Sources/Tests/UnitTests/Algebra/EigenvaluesTest.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public void ARepeatedRootIsOneMember()
3333
public void OfASymbolicMatrixCarryNoPivotCondition()
3434
{
3535
var eigenvalues = MathS.Matrix(new Entity[,] { { 0, "J" }, { "J", 0 } }).Eigenvalues;
36-
Assert.Equal(new FiniteSet("sqrt(J^2)", "-sqrt(J^2)"), eigenvalues);
36+
// lambda^2 = J^2 is rooted exactly, so these are J and -J rather than the square under
37+
// a root. https://github.com/asc-community/AngouriMath/issues/2140
38+
Assert.Equal(new FiniteSet("-J", "J"), eigenvalues);
3739
Assert.DoesNotContain("provided", eigenvalues!.Stringize());
3840
}
3941

‎Sources/Tests/UnitTests/Algebra/SquareDiscriminantTest.cs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,24 @@ public void OnlyTheRootOfTheNumberIsLeft(string equation, string first, string s
7070
Assert.Contains(roots, root => Equal(root, second.ToEntity()));
7171
Assert.DoesNotContain(roots, root => root.Nodes.Any(node => node is Powf(var radicand, Rational { Denominator: var d }) && d == 2 && radicand is not Number));
7272
}
73+
74+
/// <summary>
75+
/// With no term in <c>x</c> the solver takes <c>x^2</c> as its unknown and the square
76+
/// root after, and that root was left over the square too: <c>x^2 = (a + b)^2</c> was
77+
/// <c>{ sqrt((a + b)^2), -sqrt((a + b)^2) }</c>.
78+
/// </summary>
79+
[Theory]
80+
[InlineData("x^2 = (a + b)^2", new[] { "a + b", "-a - b" })]
81+
[InlineData("x^2 + (p - q)^2/4 = 0", new[] { "i*(p - q)/2", "-i*(p - q)/2" })]
82+
[InlineData("x^2 = 2*a^2", new[] { "sqrt(2)*a", "-sqrt(2)*a" })]
83+
[InlineData("x^4 - (a^2 + b^2)*x^2 + a^2*b^2 = 0", new[] { "a", "-a", "b", "-b" })]
84+
public void APowerOfTwoAgainstASquareIsRootedExactly(string equation, string[] expected)
85+
{
86+
var roots = Roots(equation, "x");
87+
Assert.Equal(expected.Length, roots.Length);
88+
foreach (var root in expected)
89+
Assert.Contains(roots, found => Equal(found, root.ToEntity()));
90+
Assert.DoesNotContain(roots, root => root.Nodes.Any(node => node is Powf(var radicand, Rational { Denominator: var d }) && d == 2 && radicand is not Number));
91+
}
7392
}
7493
}

0 commit comments

Comments
 (0)