diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index d1c0f8229..ebe80e78e 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -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 diff --git a/Sources/.editorconfig b/Sources/.editorconfig index ea7750004..b5a9cb974 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -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 diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/FractionedPolynoms.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/FractionedPolynoms.cs index a6b1ae3be..2bfae909e 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/FractionedPolynoms.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/FractionedPolynoms.cs @@ -6,6 +6,7 @@ // using AngouriMath.Core.Multithreading; +using PeterO.Numbers; namespace AngouriMath.Functions.Algebra.AnalyticalSolving { @@ -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(); diff --git a/Sources/Tests/UnitTests/Algebra/SolveTest/ARadicalTimesAPowerTest.cs b/Sources/Tests/UnitTests/Algebra/SolveTest/ARadicalTimesAPowerTest.cs new file mode 100644 index 000000000..170a745ee --- /dev/null +++ b/Sources/Tests/UnitTests/Algebra/SolveTest/ARadicalTimesAPowerTest.cs @@ -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 +{ + /// + /// An equation in sqrt(x) and x is solved by raising the radical's side to a power. + /// Cleared of its denominator, sqrt(x) - 1/x - 1 - i = 0 holds x * sqrt(x), which + /// the power rules write as x ^ (1/2 + 1); 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 x^(3/2) shares its radical with sqrt(x). Found by a check of systems + /// built from known solutions, which met this equation after an elimination; no issue was filed. + /// + [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(equation.ToEntity().Solve("x")); + Assert.Equal(count, roots.Count); + foreach (var root in roots) + { + Assert.True(root.Nodes.OfType().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); + } + } +}