Skip to content

Commit 53fa530

Browse files
Solve a polynomial with non-real coefficients through its product with its conjugate (#2505)
A polynomial whose coefficients are not all real, with no closed form, went to Newton's method, which crawls to a repeated root: each start that crawled there came back as a root of its own. (x - i)(x^5 - i), of degree 6, was answered with 17 numbers, 13 of them within 1e-6 of i, and sin(3x) = cos(2x), that polynomial in e^(ix), with a family of 17 members. p times the polynomial with its coefficients conjugated has rational coefficients, and its roots are p's and their conjugates. Those are found square-free part by square-free part by Durand-Kerner, as for any rational polynomial, and kept where p vanishes. The four rows now have 5, 5, 6 and 5 roots, the number each has. Found by a generated cross-check of trigonometric equations against an mpmath Newton search; on the branch it finds nothing in 600 equations. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6537203 commit 53fa530

3 files changed

Lines changed: 159 additions & 1 deletion

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ membership in such a family is decided the same way
114114
| `"tan(x)/x = 0 and x in [-4; 4]".ToEntity().Solve("x")` | `union({ pi * n provided not pi * n = 0 }, n in ZZ) /\ [-4; 4]` | `{ pi * (-1), pi }` |
115115
| `0` in `union({ 2 * pi * n provided not 2 * pi * n = 0 }, n in ZZ)` | not decided | `false` |
116116

117+
### A polynomial with non-real coefficients and a repeated root has each root once
118+
119+
**Silent: wrong answers become right.** A polynomial whose coefficients are not all real reached Newton's method if
120+
no closed form answered it. Newton's method crawls to a repeated root, and each start that crawled there came back as a
121+
root of its own, so the answer had more numbers than the polynomial has roots, most of them near-copies that are not
122+
roots. Such a polynomial is now solved through the product with its conjugate, which has rational coefficients and is
123+
solved square-free part by square-free part, so each distinct root comes back once. A trigonometric equation that is
124+
such a polynomial in `e^(ix)` is answered the same way, one family member per root.
125+
126+
| Input | Was (2.8.0) | Now |
127+
|---|---|---|
128+
| `"x^6 - i x^5 - i x - 1 = 0".ToEntity().Solve("x")`, which is `(x - i)(x^5 - i)` | 17 numbers, 13 of them within `1e-6` of `i` | `{ 0.951056… + 0.309016…i, -0.951056… + 0.309016…i, -0.587785… - 0.809016…i, 0.587785… - 0.809016…i, i }` |
129+
| `(x^5 + i x + 1)^2 = 0`, written out | 10 numbers | its 5 roots |
130+
| `(x - i)^2 (x^5 + 2x + i) = 0`, written out | 11 numbers | its 6 roots |
131+
| `sin(3x) = cos(2x)` | a family of 17 members | a family of 5 members |
132+
117133
## 2.8.0 — since 2.7.0
118134

119135
**Read these first.**

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

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
using AngouriMath.Core.Multithreading;
1010
using AngouriMath.Extensions;
1111
using AngouriMath.Functions.Algebra;
12+
using System;
1213
using System.Linq.Expressions;
14+
using PeterO.Numbers;
1315
using static AngouriMath.Entity;
1416
using static AngouriMath.Entity.Set;
1517

@@ -447,7 +449,8 @@ bool ProductIsDefinedAt(Entity root)
447449
// near -18.5, and was answered with four roots of its five.
448450
if (MathS.Settings.AllowNewton && expr.Vars.Count == 1
449451
&& (SumOverSet.NumericRoots(expr, x)
450-
?? Algebra.NumericalSolving.DurandKerner.RootsOf(expr, x, MathS.Settings.DecimalPrecisionContext.Value)) is { Count: > 0 } numeric)
452+
?? Algebra.NumericalSolving.DurandKerner.RootsOf(expr, x, MathS.Settings.DecimalPrecisionContext.Value)
453+
?? RootsOfAPolynomialWithNonRealCoefficients(expr, x)) is { Count: > 0 } numeric)
451454
return numeric.Select(ent => TryDowncast(expr, x, ent)).ToSet();
452455

453456
// An identity is satisfied by every x, and not by the points a numerical search
@@ -491,6 +494,91 @@ bool ProductIsDefinedAt(Entity root)
491494
return Unsolved(expr, x);
492495
}
493496

497+
/// <summary>
498+
/// Every distinct root of <paramref name="expr"/>, a polynomial in <paramref name="x"/> whose
499+
/// coefficients are Gaussian rationals and not all real, to the working precision; or
500+
/// <see langword="null"/>.
501+
/// </summary>
502+
/// <remarks>
503+
/// <para>
504+
/// The polynomial p times the polynomial with its coefficients conjugated has rational
505+
/// coefficients, and its roots are the roots of p and their conjugates, since p(z) = 0 exactly
506+
/// where the conjugated polynomial is 0 at conj z. That product's roots are found as any
507+
/// polynomial's with rational coefficients is, square-free part by square-free part, and those
508+
/// at which p vanishes are kept.
509+
/// </para>
510+
/// <para>
511+
/// Newton's method answered these before, and crawls to a repeated root: each start that
512+
/// crawled there came back as a root of its own, so (x - i)(x^5 - i), of degree 6, was answered
513+
/// with 17 numbers, 13 of them within 1e-6 of i.
514+
/// </para>
515+
/// </remarks>
516+
private static IReadOnlyList<Entity>? RootsOfAPolynomialWithNonRealCoefficients(Entity expr, Variable x)
517+
{
518+
if (!TreeAnalyzer.TryGetPolynomial(expr, x, out var monomials) || monomials.Count == 0)
519+
return null;
520+
var degree = 0;
521+
foreach (var power in monomials.Keys)
522+
{
523+
if (power.Sign < 0 || power.CompareTo(EInteger.FromInt32(64)) > 0)
524+
return null;
525+
degree = Math.Max(degree, power.ToInt32Checked());
526+
}
527+
if (degree < 1)
528+
return null;
529+
var coefficients = new Complex[degree + 1];
530+
for (var i = 0; i <= degree; i++)
531+
coefficients[i] = Integer.Zero;
532+
var nonReal = false;
533+
foreach (var monomial in monomials)
534+
{
535+
var (power, coefficient) = (monomial.Key, monomial.Value);
536+
if (coefficient.InnerSimplified is not Complex { RealPart: Rational, ImaginaryPart: Rational } exact)
537+
return null;
538+
nonReal |= exact is not Real;
539+
coefficients[power.ToInt32Checked()] = exact;
540+
}
541+
if (!nonReal)
542+
return null;
543+
// p(x) conj(p)(x), whose coefficients are real: the k-th is the sum of p_i conj(p_j) over i + j = k.
544+
Entity product = Integer.Zero;
545+
for (var k = 0; k <= 2 * degree; k++)
546+
{
547+
Complex sum = Integer.Zero;
548+
for (var i = Math.Max(0, k - degree); i <= Math.Min(k, degree); i++)
549+
sum += coefficients[i] * Complex.Create(coefficients[k - i].RealPart, -coefficients[k - i].ImaginaryPart);
550+
if (sum is not Rational real)
551+
return null;
552+
if (!real.IsZero)
553+
product += real * MathS.Pow(x, k);
554+
}
555+
if (SumOverSet.NumericRoots(product, x) is not { } candidates)
556+
return null;
557+
// A root of p leaves p as small as the working precision allows, and a root of the
558+
// conjugated polynomial alone leaves it as far from 0 as the two polynomials' roots are apart.
559+
var context = MathS.Settings.DecimalPrecisionContext.Value;
560+
var digits = context.Precision.ToInt32Checked();
561+
var tolerance = EDecimal.Create(EInteger.One, EInteger.FromInt32(-(digits / 2)));
562+
var roots = new List<Entity>();
563+
foreach (var candidate in candidates)
564+
{
565+
if (candidate.Evaled is not Complex z)
566+
return null;
567+
Complex value = Integer.Zero;
568+
var scale = EDecimal.Zero;
569+
var radius = EDecimal.Max(z.Abs().EDecimal, EDecimal.One);
570+
for (var i = degree; i >= 0; i--)
571+
{
572+
value = value * z + coefficients[i];
573+
scale = scale.Multiply(radius, context).Add(coefficients[i].Abs().EDecimal, context);
574+
}
575+
if (value.Abs().EDecimal.CompareTo(scale.Multiply(tolerance, context)) <= 0)
576+
roots.Add(z);
577+
}
578+
// At most as many as the degree, and at least one, as a polynomial of degree one or more has.
579+
return roots.Count >= 1 && roots.Count <= degree ? roots : null;
580+
}
581+
494582
/// <summary>
495583
/// The spellings of an equation the replacement machinery tries, in order: the
496584
/// equation as it stands, and then every alternative the simplifier can offer.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using System.Linq;
9+
using AngouriMath;
10+
using AngouriMath.Extensions;
11+
using Xunit;
12+
using static AngouriMath.Entity;
13+
using static AngouriMath.Entity.Set;
14+
15+
namespace AngouriMath.Tests.Algebra
16+
{
17+
/// <summary>
18+
/// A polynomial whose coefficients are not all real, with a repeated root and no closed form,
19+
/// has each of its distinct roots once. Newton's method crawls to a repeated root, and every
20+
/// start that crawled there came back as a root of its own: a polynomial of degree 6 was
21+
/// answered with 17 numbers, 13 of them within 1e-6 of i.
22+
/// </summary>
23+
[Trait("Area", "Algebra")]
24+
public sealed class APolynomialWithNonRealCoefficientsTest
25+
{
26+
[Theory]
27+
// (x - i) (x^5 - i): i twice, and the four other fifth roots of i
28+
[InlineData("x^6 - i x^5 - i x - 1 = 0", 5)]
29+
// (x^5 + i x + 1)^2
30+
[InlineData("1 + 2i x - x^2 + 2 x^5 + 2i x^6 + x^10 = 0", 5)]
31+
// (x - i)^2 (x^5 + 2x + i)
32+
[InlineData("-i - 3i x^2 + 2 x^3 - x^5 - 2i x^6 + x^7 = 0", 6)]
33+
public void EachDistinctRootIsAnsweredOnce(string equation, int distinct)
34+
{
35+
var roots = Assert.IsType<FiniteSet>(equation.ToEntity().Solve("x"));
36+
Assert.Equal(distinct, roots.Count);
37+
var difference = equation.ToEntity() is Equalsf(var left, var right) ? left - right : equation.ToEntity();
38+
foreach (var root in roots)
39+
{
40+
var value = difference.Substitute("x", root).EvalNumerical();
41+
Assert.True(value.Abs().EDecimal.Abs().CompareTo(PeterO.Numbers.EDecimal.FromString("1e-30")) < 0,
42+
$"{root} leaves {value}");
43+
}
44+
}
45+
46+
[Fact]
47+
public void TheFamilyHasFiveMembersAPeriod()
48+
{
49+
// sin(3x) - cos(2x) is a polynomial in e^(ix) of the first case above
50+
var answer = Assert.IsType<IndexedUnionf>("sin(3x) = cos(2x)".ToEntity().Solve("x"));
51+
Assert.Equal(5, Assert.IsType<FiniteSet>(answer.Body).Count);
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)