|
| 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 AngouriMath; |
| 9 | +using AngouriMath.Extensions; |
| 10 | +using System.Linq; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace AngouriMath.Tests.Algebra |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Square systems where solving one equation widens it, so the elimination finds rows that |
| 17 | + /// are no solution: <c>sqrt(y) = x</c> is solved for <c>y</c> as <c>x^2</c>, which holds only |
| 18 | + /// where <c>x</c> is not negative. Each was answered with such a row, and the rows are now |
| 19 | + /// put back into the system. |
| 20 | + /// <a href="https://github.com/asc-community/AngouriMath/issues/2123">#2123</a> |
| 21 | + /// </summary> |
| 22 | + [Trait("Area", "Algebra")] |
| 23 | + public sealed class SpuriousSystemRowTest |
| 24 | + { |
| 25 | + private static Entity.Matrix? Solve(params string[] equations) |
| 26 | + => MathS.Equations(equations.Select(e => e.ToEntity()).ToArray()).Solve("x", "y"); |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// A row that fails an equation with no symbol left in it is no solution for any value |
| 30 | + /// of anything, and is dropped. <c>[-2, 4]</c> leaves <c>sqrt(4) - (-2) = 4</c>, and |
| 31 | + /// <c>[-1, -1]</c> leaves <c>ln(-1) + ln(-1) = 2 pi i</c>. |
| 32 | + /// </summary> |
| 33 | + [Theory] |
| 34 | + [InlineData("sqrt(y) - x", "y - 4", "2", "4")] |
| 35 | + [InlineData("ln(x) + ln(y)", "x - y", "1", "1")] |
| 36 | + public void ARowThatIsNoSolutionIsDropped(string first, string second, string x, string y) |
| 37 | + { |
| 38 | + var solutions = Solve(first, second); |
| 39 | + Assert.NotNull(solutions); |
| 40 | + Assert.Equal(1, solutions!.RowCount); |
| 41 | + Assert.Equal(Entity.Number.Integer.Create(0), (solutions[0, 0] - x).Simplify()); |
| 42 | + Assert.Equal(Entity.Number.Integer.Create(0), (solutions[0, 1] - y).Simplify()); |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// <c>[-1, 1]</c> was the only row, and <c>sqrt(1) - (-1)</c> is 2: the system has no |
| 47 | + /// solution, which <see cref="EquationSystem.Solve"/> answers as <see langword="null"/>. |
| 48 | + /// </summary> |
| 49 | + [Fact] |
| 50 | + public void ASystemWhoseOnlyRowIsNoSolutionHasNone() |
| 51 | + => Assert.Null(Solve("sqrt(y) - x", "x + 1")); |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// <c>[-sqrt(a), a]</c> fails at <c>a = 137/100</c> and holds at <c>a = 0</c>, so it |
| 55 | + /// cannot be dropped as no solution, and the system is declined instead of being |
| 56 | + /// answered by the other row alone. A row like it that holds for some values only, as |
| 57 | + /// <c>sqrt(t^2) = t</c> does for positive <c>t</c>, would be lost by dropping it. |
| 58 | + /// </summary> |
| 59 | + [Fact] |
| 60 | + public void ARowThatFailsForSomeValueDeclinesTheSystem() |
| 61 | + => Assert.Null(Solve("sqrt(y) - x", "y - a")); |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// What was answered rightly still is: rows in a symbol of the system's own, and rows in |
| 65 | + /// an integer parameter, which are tried with it at a whole number and not at the value |
| 66 | + /// the system's own symbols are tried at. Each row is checked here at several values of |
| 67 | + /// both, negative ones among them. |
| 68 | + /// </summary> |
| 69 | + [Theory] |
| 70 | + [InlineData("x * y - a", "x - y", 2)] |
| 71 | + [InlineData("x ^ 2 - a", "y - x", 2)] |
| 72 | + [InlineData("sin(x) - y", "y - 1/2", 2)] |
| 73 | + [InlineData("tan(x) - y", "y - 1", 1)] |
| 74 | + [InlineData("sqrt(x) + y", "x - 1", 1)] |
| 75 | + public void ARowThatHoldsIsKept(string first, string second, int rows) |
| 76 | + { |
| 77 | + var solutions = Solve(first, second); |
| 78 | + Assert.NotNull(solutions); |
| 79 | + Assert.Equal(rows, solutions!.RowCount); |
| 80 | + foreach (var at in new[] { -1, 0, 2 }) |
| 81 | + for (var row = 0; row < solutions.RowCount; row++) |
| 82 | + foreach (var equation in new[] { first, second }) |
| 83 | + { |
| 84 | + var residual = equation.ToEntity().Substitute("x", solutions[row, 0]).Substitute("y", solutions[row, 1]); |
| 85 | + foreach (var symbol in residual.Vars.ToList()) |
| 86 | + residual = residual.Substitute(symbol, symbol.Name == "a" ? 1.7 + at : at); |
| 87 | + var value = Assert.IsAssignableFrom<Entity.Number.Complex>(residual.Evaled); |
| 88 | + Assert.True(((Entity.Number.Real)value.Abs()).EDecimal.ToDouble() < 1e-9, $"{equation} at row {row}, {at}: {value}"); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments