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
20 changes: 20 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,26 @@ did, and `ln(x + 2) / ln(x + 3)` at 0 was `ln(2) / ln(3)`. With symbols it was n
| `"limit(ln(a + 1/x) / ln(b + 1/x), x, +oo)".ToEntity().Simplify()` | `a / b` | `ln(a) / ln(b)` |
| `"limit(ln(1 + x) / ln(y + x), x, 0)".ToEntity().Simplify()` | `1 / y` | `0 provided not ln(y) = 0` |

### A radical times a power of its variable is solved exactly

**Silent: an equation in `sqrt(x)` or a cube root of `x` beside powers of `x` is answered in radicals and with all its
roots, where it was answered in decimals and could miss roots or include a number that is not one.** Such an equation is
solved by raising the radical's side to a power, and the solver looks for each fractional power before doing it. The
power rules write `x * sqrt(x)` as `x ^ (1/2 + 1)`, and an exponent written as a sum was not read as a fractional power.
Equations with that term, which includes any `c/x` cleared of its denominator, went to Newton's method. It answered in
decimals, and in `sqrt(x) - 1/x - 1 - i = 0` it found one root and missed the root -1. The exponent is now folded, and a
power past one is read as a whole power times a proper fractional one, so `x^(3/2)` shares the radical of `sqrt(x)`. One
answer that was exact is spelled differently. No issue was filed; a check of systems built from known solutions met this
equation after an elimination. On 200 generated equations of this shape, 2.6.0 missed 45 roots in 22 of them and
answered 5 numbers that are not roots. Now 2 roots are missing and none is wrong.

| Input | Was (2.6.0) | Now |
|---|---|---|
| `"sqrt(x) - 1/x - 1 - i = 0".Solve("x")` | `{ 1.3002… + 1.6248…i }` | `-1` and that root in radicals |
| `"sqrt(x) + 1/x = 0".Solve("x")` | `{ -0.5 - 0.8660…i, -0.5 + 0.8660…i }` | `{ -1/2 + i * 1/2 * sqrt(3), -1/2 + i * -1/2 * sqrt(3) }` |
| `"sqrt(x) - 1/x - 1 = 0".Solve("x")` | `{ 2.1478… }` | that root in radicals |
| `"(3 - x)*sqrt(x) - x = 0".Solve("x")` | `{ 0, (7 - sqrt(13)) / 2 }` | `{ 0, (-7 + sqrt(13)) / (-2) }` |

### A cube shifted is solved

**Silent: a cubic `a(x + h)^3 = k` written out, with `b^2 = 3ac`, is solved where it was answered `{ }`.** Cardano's
Expand Down
3 changes: 3 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
[Tests/UnitTests/Algebra/SolveTest/AQuotientIsZeroWhereItsNumeratorIsTest.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/ARadicalTimesAPowerTest.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/Core/FactorialCacheIsThreadSafeTest.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 @@ -6,6 +6,7 @@
//

using AngouriMath.Core.Multithreading;
using PeterO.Numbers;

namespace AngouriMath.Functions.Algebra.AnalyticalSolving
{
Expand Down Expand Up @@ -34,15 +35,34 @@ internal static bool TrySolve(Entity expr, Variable x, out Set dst)
potentialFraction.fracs = new List<(Entity main, Integer pow)>();
foreach (var mpChild in Mulf.LinearChildren(child))
{
if (!(mpChild is Powf(var @base, Number num and not Integer))) // (x + 3) ^ 3
// The exponent is folded first: the power rules write x * sqrt(x) as x ^ (1/2 + 1),
// whose exponent is a sum, and read as it stands that was no fractional power, so
// sqrt(x) - 1/x - 1 - i = 0, cleared of its denominator, went to Newton's method and
// was answered without its root -1.
if (!(mpChild is Powf(var @base, var exponent) && exponent.InnerSimplified is Number num and not Integer)) // (x + 3) ^ 3
{
potentialFraction.multiplier *= mpChild;
continue;
}
if (num is not Rational fracNum)
return false; // (x + 1)^0.2348728
var newChild = MathS.Pow(@base, fracNum.ERational.Numerator).InnerSimplified;
// A power past one is a whole power times a proper fractional one, x^(3/2) being
// x * sqrt(x), so it shares its radical with sqrt(x): 3 sqrt(x) - x^(3/2) is the one
// term (3 - x) sqrt(x), where read as sqrt(x) and sqrt(x^3) it was two radicals that
// cannot be raised together, and the equation went to Newton's method without its
// root 0. Raising to the power keeps every root whichever root is taken, since
// (x^(p/q))^q is x^p, and the roots it adds are not the equation's.
var den = fracNum.ERational.Denominator;
var whole = fracNum.ERational.Numerator.Divide(den);
var rest = fracNum.ERational.Numerator.Subtract(whole.Multiply(den));
if (rest.Sign < 0)
{
whole = whole.Subtract(EInteger.One);
rest = rest.Add(den);
}
if (!whole.IsZero)
potentialFraction.multiplier *= MathS.Pow(@base, Integer.Create(whole));
var newChild = MathS.Pow(@base, Integer.Create(rest)).InnerSimplified;
potentialFraction.fracs.Add((newChild, den));

MultithreadingFunctional.ExitIfCancelled();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// 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>
/// An equation in <c>sqrt(x)</c> and <c>x</c> is solved by raising the radical's side to a power.
/// Cleared of its denominator, <c>sqrt(x) - 1/x - 1 - i = 0</c> holds <c>x * sqrt(x)</c>, which
/// the power rules write as <c>x ^ (1/2 + 1)</c>; read with its exponent a sum, that was no
/// fractional power, and the equation went to Newton's method, which answered one decimal root
/// and not the other, -1. A power past one is read as a whole power times a proper fractional
/// one, so <c>x^(3/2)</c> shares its radical with <c>sqrt(x)</c>. Found by a check of systems
/// built from known solutions, which met this equation after an elimination; no issue was filed.
/// </summary>
[Trait("Area", "Algebra")]
public sealed class ARadicalTimesAPowerTest
{
[Theory]
[InlineData("sqrt(x) - 1/x - 1 - i = 0", "sqrt(x) - 1/x - 1 - i", 2, "-1")]
[InlineData("x*sqrt(x) - 1 - (1 + i)*x = 0", "x*sqrt(x) - 1 - (1 + i)*x", 2, "-1")]
// 3 sqrt(x) - x^(3/2) is the one term (3 - x) sqrt(x). Read as sqrt(x) and sqrt(x^3), two
// radicals, it cannot be raised to a power, and Newton's method finds no root 0.
[InlineData("(3 - x)*sqrt(x) - x = 0", "(3 - x)*sqrt(x) - x", 2, "0")]
[InlineData("x^2*sqrt(x) + 3*sqrt(x) + 2*x = 0", "x^2*sqrt(x) + 3*sqrt(x) + 2*x", 3, "0")]
// s^3 + 1 = 0 in s = sqrt(x): the two roots with a principal square root, and not s = -1.
[InlineData("sqrt(x) + 1/x = 0", "sqrt(x) + 1/x", 2, "-1/2 + sqrt(3)/2 * i")]
// s^3 - s^2 - 1 = 0: the real root only, as the other two have a negative real part.
[InlineData("sqrt(x) - 1/x - 1 = 0", "sqrt(x) - 1/x - 1", 1, null)]
public void EveryRootIsExactAndSatisfiesTheEquation(string equation, string residual, int count, string? among)
{
var roots = Assert.IsType<Set.FiniteSet>(equation.ToEntity().Solve("x"));
Assert.Equal(count, roots.Count);
foreach (var root in roots)
{
Assert.True(root.Nodes.OfType<Complex>().All(number => number.RealPart is Rational && number.ImaginaryPart is Rational),
$"{root} is in decimals");
var value = residual.ToEntity().Substitute("x", root).EvalNumerical();
Assert.True(Complex.Abs(value).EDecimal.CompareTo(EDecimal.FromString("1E-20")) < 0, $"{root} leaves {value}");
}
if (among is { })
Assert.Contains(roots, root => (root - among.ToEntity()).Simplify() == 0);
}
}
}
Loading