|
| 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.Extensions; |
| 10 | +using PeterO.Numbers; |
| 11 | +using Xunit; |
| 12 | +using static AngouriMath.Entity; |
| 13 | +using static AngouriMath.Entity.Number; |
| 14 | + |
| 15 | +namespace AngouriMath.Tests.Algebra |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// An equation in <c>sqrt(x)</c> and <c>x</c> is solved by raising the radical's side to a power. |
| 19 | + /// Cleared of its denominator, <c>sqrt(x) - 1/x - 1 - i = 0</c> holds <c>x * sqrt(x)</c>, which |
| 20 | + /// the power rules write as <c>x ^ (1/2 + 1)</c>; read with its exponent a sum, that was no |
| 21 | + /// fractional power, and the equation went to Newton's method, which answered one decimal root |
| 22 | + /// and not the other, -1. A power past one is read as a whole power times a proper fractional |
| 23 | + /// one, so <c>x^(3/2)</c> shares its radical with <c>sqrt(x)</c>. Found by a check of systems |
| 24 | + /// built from known solutions, which met this equation after an elimination; no issue was filed. |
| 25 | + /// </summary> |
| 26 | + [Trait("Area", "Algebra")] |
| 27 | + public sealed class ARadicalTimesAPowerTest |
| 28 | + { |
| 29 | + [Theory] |
| 30 | + [InlineData("sqrt(x) - 1/x - 1 - i = 0", "sqrt(x) - 1/x - 1 - i", 2, "-1")] |
| 31 | + [InlineData("x*sqrt(x) - 1 - (1 + i)*x = 0", "x*sqrt(x) - 1 - (1 + i)*x", 2, "-1")] |
| 32 | + // 3 sqrt(x) - x^(3/2) is the one term (3 - x) sqrt(x). Read as sqrt(x) and sqrt(x^3), two |
| 33 | + // radicals, it cannot be raised to a power, and Newton's method finds no root 0. |
| 34 | + [InlineData("(3 - x)*sqrt(x) - x = 0", "(3 - x)*sqrt(x) - x", 2, "0")] |
| 35 | + [InlineData("x^2*sqrt(x) + 3*sqrt(x) + 2*x = 0", "x^2*sqrt(x) + 3*sqrt(x) + 2*x", 3, "0")] |
| 36 | + // s^3 + 1 = 0 in s = sqrt(x): the two roots with a principal square root, and not s = -1. |
| 37 | + [InlineData("sqrt(x) + 1/x = 0", "sqrt(x) + 1/x", 2, "-1/2 + sqrt(3)/2 * i")] |
| 38 | + // s^3 - s^2 - 1 = 0: the real root only, as the other two have a negative real part. |
| 39 | + [InlineData("sqrt(x) - 1/x - 1 = 0", "sqrt(x) - 1/x - 1", 1, null)] |
| 40 | + public void EveryRootIsExactAndSatisfiesTheEquation(string equation, string residual, int count, string? among) |
| 41 | + { |
| 42 | + var roots = Assert.IsType<Set.FiniteSet>(equation.ToEntity().Solve("x")); |
| 43 | + Assert.Equal(count, roots.Count); |
| 44 | + foreach (var root in roots) |
| 45 | + { |
| 46 | + Assert.True(root.Nodes.OfType<Complex>().All(number => number.RealPart is Rational && number.ImaginaryPart is Rational), |
| 47 | + $"{root} is in decimals"); |
| 48 | + var value = residual.ToEntity().Substitute("x", root).EvalNumerical(); |
| 49 | + Assert.True(Complex.Abs(value).EDecimal.CompareTo(EDecimal.FromString("1E-20")) < 0, $"{root} leaves {value}"); |
| 50 | + } |
| 51 | + if (among is { }) |
| 52 | + Assert.Contains(roots, root => (root - among.ToEntity()).Simplify() == 0); |
| 53 | + } |
| 54 | + } |
| 55 | +} |
0 commit comments