Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,27 @@ one inside another was answered with the empty set, a claim that it has no solut
| `"1/(2 + p^(-3)) = 1/3".Solve("p")` | `{ }` | `{ 1, -1/2 + i * 1/2 * sqrt(3), -1/2 + i * -1/2 * sqrt(3) }` |
| `"1/(1 + x^(-2)) = y".Solve("x")` | `{ }` | `{ ((-1) / (-y) - 1) ^ (-1/2), -((-1) / (-y) - 1) ^ (-1/2) }` |

### A polynomial with no roots in closed form is answered with all of them

**Silent: a polynomial equation with rational coefficients and no roots in closed form is answered with every root,
where it could be answered with some.** Two defects lost them. An equation in powers of `x` was solved through `x` to
the least of its powers, so `x^5 - 30x^4 + 1` was written in `t = x^4` with `x^5` as `t^(5/4)`, which is not `x^5` off
the positive reals; the substitution is now `x` to the greatest common divisor of the powers, and none where that is 1.
And where no route writes the roots in radicals, a polynomial went to Newton's method. It starts from points inside a
bounded region and answered the roots it found there as if they were all. `x^5 - 30x^4 + 1 = 0` has three real roots,
one of them near 30, and two off the real line, and was answered with one root. The roots now come from the
Durand–Kerner iteration, which settles every root of each square-free part at once or answers nothing, to
`MathS.Settings.DecimalPrecisionContext`'s digits. So a polynomial Newton's method did answer in full has the same roots
with more digits. No issue was filed; it was found following an equation in `x^(1/3)` that cubes to a polynomial with a
root near 23.

| Input | Was (2.6.0) | Now |
|---|---|---|
| `"x^5 - 30x^4 + 1 = 0".Solve("x")` | one root | five roots, one of them `29.9999987…` |
| `"x^7 - 30x^6 + 1 = 0".Solve("x")` | one root | seven roots |
| `"x^7 - 5x^6 + 1 = 0".Solve("x")` | six roots, none of them near 5 | seven roots |
| `"x^4 - 3*x^(5/2) + 2 = 0".Solve("x")` | `{ 1, 4.8870…^(2/5) }` | four roots: `1`, `1.3734…^2`, and two off the real line |

### A quartic that is biquadratic once depressed is solved

**Silent: a quartic whose depressed form `y^4 + alpha y^2 + gamma`, for `y = x + b/(4a)`, has no cubic term is solved,
Expand Down
3 changes: 3 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
[Tests/UnitTests/Algebra/SolveTest/APolynomialHasARootTest.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[Tests/UnitTests/Algebra/SolveTest/APolynomialWithNoClosedFormHasAllItsRootsTest.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[Tests/UnitTests/Convenience/RoundIsExactTest.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,15 @@ bool ProductIsDefinedAt(Entity root)
// TODO: Solve factorials (Needs Lambert W function)
// https://mathoverflow.net/a/28977

// A polynomial with rational coefficients that no route above writes in closed form has
// all its roots by the Durand-Kerner iteration, which settles every root of each square-free
// part at once or answers nothing. Newton's method is started inside a bounded region, and
// answered the roots it found there as though they were all: x^5 - 30x^4 + 1 = 0 has a root
// near 30, and was answered with one root of its five. It is numerical, so it is asked
// only where Newton's method would have been: turning that off asks for analytical answers.
if (MathS.Settings.AllowNewton && expr.Vars.Count == 1 && SumOverSet.NumericRoots(expr, x) is { Count: > 0 } numeric)
return numeric.Select(ent => TryDowncast(expr, x, ent)).ToSet();

// An identity is satisfied by every x, and not by the points a numerical search
// starts from: `sqrt(x)^2 - x` is zero wherever it is evaluated, so every start of
// Newton's grid converged at once and each was kept, two hundred numbers for an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using AngouriMath.Core.Multithreading;
using AngouriMath.Extensions;
using PeterO.Numbers;
using System;
using static AngouriMath.Entity;
using static AngouriMath.Entity.Set;
Expand Down Expand Up @@ -192,14 +193,34 @@ Entity ApplyPowerTransform(Entity @base, Entity arg)
// handle special case when all bases are numerical
if (innerPowerList.All(e => e.EvaluableNumerical && e.Evaled is Real))
{
var minPow = innerPowerList.Aggregate((a, b)
=> (a.EvalNumerical() < b.EvalNumerical()).EvalBoolean() ? a : b);
// Each power is to be a whole power of the substitution. x^5 written as (x^4)^(5/4) is
// not x^5 off the positive reals, and solved through it x^5 - 30x^4 + 1 = 0 lost roots;
// so the substitution is x to the greatest common divisor of the powers where they are
// rational, and to the least of them only where each is a whole multiple of it. A
// divisor of 1 is no substitution: the equation is a polynomial in x as it stands.
Entity unit;
if (innerPowerList.All(e => e.InnerSimplified is Rational))
{
var powers = innerPowerList.Select(e => ((Rational)e.InnerSimplified).ERational).ToList();
var denominator = powers.Select(r => r.Denominator)
.Aggregate((a, b) => a.Divide(a.GreatestCommonDivisor(b)).Multiply(b));
var numerator = powers.Select(r => r.Numerator.Multiply(denominator).Divide(r.Denominator).Abs())
.Aggregate((a, b) => a.GreatestCommonDivisor(b));
if (numerator.Equals(denominator))
return null;
unit = Rational.Create(ERational.Create(numerator, denominator));
}
else
unit = innerPowerList.Aggregate((a, b)
=> (a.EvalNumerical() < b.EvalNumerical()).EvalBoolean() ? a : b);

substitution = MathS.Pow(x, minPow).InnerSimplified;
substitution = MathS.Pow(x, unit).InnerSimplified;

foreach (var pow in innerPowerList)
{
var divided = (pow / minPow).InnerSimplified;
var divided = (pow / unit).InnerSimplified;
if (divided is not Integer)
return null;
expr = expr.Substitute(MathS.Pow(x, pow.InnerSimplified), MathS.Pow(substitution, divided));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// Copyright (c) 2019-2026 Angouri.
// AngouriMath is licensed under MIT.
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
// Website: https://am.angouri.org.
//

using System.Linq;
using AngouriMath.Extensions;
using PeterO.Numbers;
using Xunit;
using static AngouriMath.Entity;
using static AngouriMath.Entity.Number;

namespace AngouriMath.Tests.Algebra
{
/// <summary>
/// A polynomial with rational coefficients and no roots in closed form has all its roots, found
/// by the Durand-Kerner iteration, and is not first written in x to the least of its powers,
/// which lost roots of its own. Newton's method was started inside a bounded region and
/// answered the roots it found there as though they were all: <c>x^5 - 30x^4 + 1 = 0</c> has three
/// real roots, one of them near 30, and two off the real line, and was answered with one of them.
/// Found following an equation in <c>x^(1/3)</c> that cubes to a polynomial of degree 7 with a
/// root near 23; no issue was filed.
/// </summary>
[Trait("Area", "Algebra")]
public sealed class APolynomialWithNoClosedFormHasAllItsRootsTest
{
[Theory]
[InlineData("x^5 - 30x^4 + 1", 5, "29.9")]
[InlineData("x^7 - 30x^6 + 1", 7, "29.9")]
[InlineData("x^7 - 5x^6 + 1", 7, "4.9")]
[InlineData("(x^2 + 3)^3 * x - (3x^2 - 3x + 1)^3", 7, "23.3")]
public void EveryRootIsThere(string polynomial, int degree, string farRoot)
{
var roots = Assert.IsType<Set.FiniteSet>($"{polynomial} = 0".ToEntity().Solve("x"));
Assert.Equal(degree, roots.Count);
foreach (var root in roots)
{
var at = root.EvalNumerical();
var size = EDecimal.Max(EDecimal.One, Complex.Abs(at).EDecimal);
var residual = Complex.Abs(polynomial.ToEntity().Substitute("x", at).EvalNumerical()).EDecimal;
Assert.True(residual.CompareTo(EDecimal.FromString("1E-17").Multiply(size.Pow(degree))) < 0,
$"{root} leaves {residual}");
}
Assert.Contains(roots, root => root.EvalNumerical() is Real real && real.EDecimal.CompareTo(EDecimal.FromString(farRoot)) > 0);
}

/// <summary>
/// An equation in powers of x is solved through x to the greatest common divisor of the
/// powers. Through the least of them, x^4 - 3x^(5/2) + 2 was written in t = x^(5/2) with
/// x^4 as t^(8/5), which is not x^4 off the positive reals, and two of its four roots were lost.
/// </summary>
[Fact]
public void AnEquationInPowersIsSolvedThroughTheirCommonDivisor()
{
var roots = Assert.IsType<Set.FiniteSet>("x^4 - 3*x^(5/2) + 2 = 0".ToEntity().Solve("x"));
Assert.Equal(4, roots.Count);
foreach (var root in roots)
{
var residual = Complex.Abs("x^4 - 3*x^(5/2) + 2".ToEntity().Substitute("x", root.EvalNumerical()).EvalNumerical()).EDecimal;
Assert.True(residual.CompareTo(EDecimal.FromString("1E-20")) < 0, $"{root} leaves {residual}");
}
}

/// <summary>Where the polynomial has a closed form, it is still answered in it.</summary>
[Fact]
public void APolynomialInACubeIsStillExact()
{
var roots = Assert.IsType<Set.FiniteSet>("x^6 + x^3 + 1 = 0".ToEntity().Solve("x"));
Assert.Equal(6, roots.Count);
Assert.All(roots, root => Assert.True(root.Nodes.OfType<Complex>().All(number => number.RealPart is Rational && number.ImaginaryPart is Rational),
$"{root} is in decimals"));
}
}
}
Loading