|
10 | 10 | using AngouriMath; |
11 | 11 | using AngouriMath.Core; |
12 | 12 | using AngouriMath.Core.Exceptions; |
| 13 | +using AngouriMath.Extensions; |
| 14 | +using AngouriMath.Functions; |
| 15 | +using AngouriMath.Functions.Algebra.Groebner; |
13 | 16 | using Xunit; |
14 | 17 | using static AngouriMath.Entity; |
15 | 18 |
|
@@ -177,6 +180,77 @@ public void SystemsWithIrrationalSolutionsAreSolved(string first, string second, |
177 | 180 | AssertEverySolutionSatisfies(equations, variables, solutions); |
178 | 181 | } |
179 | 182 |
|
| 183 | + /// <summary> |
| 184 | + /// The basis of <c>x * y, x + y^2 - 1</c> in degree-reverse-lexicographic order has |
| 185 | + /// <c>1</c>, <c>y</c> and <c>x</c> under its staircase, and converting it reaches |
| 186 | + /// <c>y^2 = 1 - x</c> with the row for <c>1</c> already standing. The row kept for |
| 187 | + /// <c>y^2</c> is then <c>-x</c>, and recording it as <c>y^2</c> alone made the element |
| 188 | + /// for <c>x</c> come out as <c>x + y^2</c>, whose root <c>(0, 0)</c> is no solution. |
| 189 | + /// </summary> |
| 190 | + [Fact] |
| 191 | + public void TheLexicographicBasisKeepsWhatARowWasReducedBy() |
| 192 | + { |
| 193 | + Variable[] variables = { "x", "y" }; |
| 194 | + var index = new Dictionary<Variable, int> { [variables[0]] = 0, [variables[1]] = 1 }; |
| 195 | + var generators = new[] { "x * y", "x + y^2 - 1" } |
| 196 | + .Select(text => MultivariatePolynomial.TryParse(text.ToEntity(), index)!) |
| 197 | + .ToList(); |
| 198 | + var budget = new GroebnerBudget(); |
| 199 | + var basis = Buchberger.Compute(generators, MonomialOrder.DegreeReverseLexicographic, budget); |
| 200 | + Assert.NotNull(basis); |
| 201 | + var lexicographic = Fglm.ToLexicographic(basis!, variables.Length, budget); |
| 202 | + Assert.NotNull(lexicographic); |
| 203 | + |
| 204 | + var elements = lexicographic!.Select(element => element.ToEntity(variables)).ToList(); |
| 205 | + Assert.Equal(2, elements.Count); |
| 206 | + foreach (var expected in new[] { "y^3 - y", "x + y^2 - 1" }) |
| 207 | + Assert.Contains(elements, element => (element - expected.ToEntity()).Simplify() == 0); |
| 208 | + } |
| 209 | + |
| 210 | + /// <summary> |
| 211 | + /// The system the basis above belongs to. With the wrong element the triangularisation |
| 212 | + /// was declined, and the eliminator that answered instead solved <c>x * y = 0</c> for |
| 213 | + /// <c>y</c> as <c>0</c> alone, which kept <c>(1, 0)</c> and lost <c>(0, 1)</c> and |
| 214 | + /// <c>(0, -1)</c>. |
| 215 | + /// </summary> |
| 216 | + [Fact] |
| 217 | + public void ASystemWhoseBasisLeansOnAReducedRowIsSolvedWhole() |
| 218 | + { |
| 219 | + Entity[] equations = { "x * y", "x + y^2 - 1" }; |
| 220 | + Variable[] variables = { "x", "y" }; |
| 221 | + |
| 222 | + var solutions = MathS.Equations(equations).Solve(variables); |
| 223 | + |
| 224 | + Assert.NotNull(solutions); |
| 225 | + Assert.Equal(3, solutions!.RowCount); |
| 226 | + AssertEverySolutionSatisfies(equations, variables, solutions); |
| 227 | + foreach (var (x, y) in new[] { (1, 0), (0, 1), (0, -1) }) |
| 228 | + Assert.Contains(Enumerable.Range(0, solutions.RowCount), |
| 229 | + row => solutions[row, 0] == Number.Integer.Create(x) && solutions[row, 1] == Number.Integer.Create(y)); |
| 230 | + } |
| 231 | + |
| 232 | + /// <summary> |
| 233 | + /// A basis that is not in shape position. Its element <c>x y + 3 x - 5 y - 15</c> is |
| 234 | + /// <c>0</c> at <c>y = -3</c> but was left <c>x * (-3) + 3 * x</c>, which still mentions |
| 235 | + /// <c>x</c>, so it was the equation <c>x</c> was solved from there. That solved to every |
| 236 | + /// number, the triangularisation was declined, and the eliminator lost <c>(5, 0)</c>. |
| 237 | + /// </summary> |
| 238 | + [Fact] |
| 239 | + public void ARootThatZeroesALeadingCoefficientUsesTheNextElement() |
| 240 | + { |
| 241 | + Entity[] equations = { "x * y + 3 * x - 5 * y - 15", "x^2 - 3 * x - 4 * y - 10" }; |
| 242 | + Variable[] variables = { "x", "y" }; |
| 243 | + |
| 244 | + var solutions = MathS.Equations(equations).Solve(variables); |
| 245 | + |
| 246 | + Assert.NotNull(solutions); |
| 247 | + Assert.Equal(3, solutions!.RowCount); |
| 248 | + AssertEverySolutionSatisfies(equations, variables, solutions); |
| 249 | + foreach (var (x, y) in new[] { (5, 0), (1, -3), (2, -3) }) |
| 250 | + Assert.Contains(Enumerable.Range(0, solutions.RowCount), |
| 251 | + row => solutions[row, 0] == Number.Integer.Create(x) && solutions[row, 1] == Number.Integer.Create(y)); |
| 252 | + } |
| 253 | + |
180 | 254 | /// <summary> |
181 | 255 | /// The answers stay exact rather than being handed back as decimals — which is the |
182 | 256 | /// point of triangularising rather than evaluating. |
|
0 commit comments