|
| 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 | +using static AngouriMath.Entity; |
| 13 | +using static AngouriMath.Entity.Set; |
| 14 | + |
| 15 | +namespace AngouriMath.Tests.Algebra.SolveTest |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Inverting a negative whole power found no roots: the roots of unity it multiplies by were |
| 19 | + /// counted up to the exponent, and up to -2 is none. So an equation that reaches one inside |
| 20 | + /// another, as <c>1/(1 + x^(-2)) = 1/2</c> does, was answered with the empty set, a claim that |
| 21 | + /// it has no roots, where it has 1 and -1. |
| 22 | + /// <a href="https://github.com/asc-community/AngouriMath/issues/2144">#2144</a> |
| 23 | + /// </summary> |
| 24 | + [Trait("Area", "Algebra")] |
| 25 | + public sealed class NegativePowerInversionTest |
| 26 | + { |
| 27 | + private static Set Solve(string statement, string x = "x") => statement.ToEntity().Solve(x); |
| 28 | + |
| 29 | + private static Entity IsIn(string value, Set set) => value.ToEntity().In(set).Simplify(); |
| 30 | + |
| 31 | + [Theory] |
| 32 | + [InlineData("1/(1 + x^(-2)) = 1/2", "1", "-1", "2")] |
| 33 | + [InlineData("1/(2 + x^(-3)) = 1/3", "1", "-1/2 + 1/2 * sqrt(3) * i", "-1")] |
| 34 | + [InlineData("2/(1 + x^(-2)) = 1", "1", "-1", "0")] |
| 35 | + [InlineData("1/(3 + (x + 1)^(-2)) = 1/4", "0", "-2", "1")] |
| 36 | + public void EveryRootIsFound(string equation, string root, string otherRoot, string nonRoot) |
| 37 | + { |
| 38 | + var solved = Solve(equation); |
| 39 | + Assert.Equal(Entity.Boolean.True, IsIn(root, solved)); |
| 40 | + Assert.Equal(Entity.Boolean.True, IsIn(otherRoot, solved)); |
| 41 | + Assert.Equal(Entity.Boolean.False, IsIn(nonRoot, solved)); |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// With a symbol on the right the answer is the two roots in terms of it, which at |
| 46 | + /// y = 1/2 are the roots above. |
| 47 | + /// </summary> |
| 48 | + [Fact] |
| 49 | + public void TheRootsInASymbolAreTheRootsAtEachValue() |
| 50 | + { |
| 51 | + var solved = Assert.IsType<FiniteSet>(Solve("1/(1 + x^(-2)) = y")); |
| 52 | + Assert.Equal(2, solved.Count); |
| 53 | + var atAHalf = solved.Select(root => root.Substitute("y", "1/2".ToEntity()).Simplify()).ToList(); |
| 54 | + Assert.Contains(atAHalf, root => root == 1); |
| 55 | + Assert.Contains(atAHalf, root => root == -1); |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary>A negative power is never zero, so this one does have none.</summary> |
| 59 | + [Fact] |
| 60 | + public void ANegativePowerIsNeverZero() |
| 61 | + => Assert.Equal(Set.Empty, Solve("1/(1 + x^(-2)) = 1")); |
| 62 | + } |
| 63 | +} |
0 commit comments