|
9 | 9 | using AngouriMath.Core.Multithreading; |
10 | 10 | using AngouriMath.Extensions; |
11 | 11 | using AngouriMath.Functions.Algebra; |
| 12 | +using System; |
12 | 13 | using System.Linq.Expressions; |
| 14 | +using PeterO.Numbers; |
13 | 15 | using static AngouriMath.Entity; |
14 | 16 | using static AngouriMath.Entity.Set; |
15 | 17 |
|
@@ -447,7 +449,8 @@ bool ProductIsDefinedAt(Entity root) |
447 | 449 | // near -18.5, and was answered with four roots of its five. |
448 | 450 | if (MathS.Settings.AllowNewton && expr.Vars.Count == 1 |
449 | 451 | && (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) |
451 | 454 | return numeric.Select(ent => TryDowncast(expr, x, ent)).ToSet(); |
452 | 455 |
|
453 | 456 | // An identity is satisfied by every x, and not by the points a numerical search |
@@ -491,6 +494,91 @@ bool ProductIsDefinedAt(Entity root) |
491 | 494 | return Unsolved(expr, x); |
492 | 495 | } |
493 | 496 |
|
| 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 | + |
494 | 582 | /// <summary> |
495 | 583 | /// The spellings of an equation the replacement machinery tries, in order: the |
496 | 584 | /// equation as it stands, and then every alternative the simplifier can offer. |
|
0 commit comments