Repository navigation
Expand file tree
/
Copy pathGroebnerSystemTest.cs
More file actions
329 lines (296 loc) · 14.3 KB
/
Copy pathGroebnerSystemTest.cs
File metadata and controls
329 lines (296 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//
// 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.Collections.Generic;
using System.Linq;
using AngouriMath;
using AngouriMath.Core;
using AngouriMath.Core.Exceptions;
using AngouriMath.Extensions;
using AngouriMath.Functions;
using AngouriMath.Functions.Algebra.Groebner;
using Xunit;
using static AngouriMath.Entity;
namespace AngouriMath.Tests.Algebra
{
/// <summary>
/// Systems that are triangularised by a Gröbner basis rather than eliminated in
/// radicals — <a href="https://github.com/asc-community/AngouriMath/issues/860">#860</a>.
/// </summary>
[Trait("Area", "Algebra")]
public sealed class GroebnerSystemTest
{
static Variable[] Vars(int count) =>
Enumerable.Range(0, count).Select(i => (Variable)$"x_{i}").ToArray();
/// <summary>n unknowns that are a permutation of 1..n, written as n power sums.</summary>
static Entity[] PowerSums(int n)
{
var equations = new Entity[n];
for (var power = 1; power <= n; power++)
{
Entity sum = 0;
for (var v = 0; v < n; v++)
sum += MathS.Pow($"x_{v}", power);
var target = 0L;
for (var k = 1; k <= n; k++)
{
var term = 1L;
for (var e = 0; e < power; e++) term *= k;
target += term;
}
equations[power - 1] = sum - target;
}
return equations;
}
static void AssertEverySolutionSatisfies(Entity[] equations, Variable[] variables, Matrix solutions)
{
for (var row = 0; row < solutions.RowCount; row++)
{
var substitutions = new Dictionary<Variable, Entity>();
for (var column = 0; column < variables.Length; column++)
substitutions[variables[column]] = solutions[row, column];
foreach (var equation in equations)
Assert.Equal(0, equation.Substitute(substitutions).EvalNumerical());
}
}
/// <summary>
/// Four coupled equations did not finish in three hundred seconds when each
/// elimination went through the radical formulas, while four uncoupled ones with 256
/// solutions took seventeen milliseconds. The cost was the radicals, not the size.
/// </summary>
[Theory]
[InlineData(2, 2)]
[InlineData(3, 6)]
[InlineData(4, 24)]
[InlineData(5, 120)]
public void CoupledSystemsAreSolved(int variableCount, int expectedSolutions)
{
var equations = PowerSums(variableCount);
var variables = Vars(variableCount);
var solutions = MathS.Equations(equations).Solve(variables);
Assert.NotNull(solutions);
Assert.Equal(expectedSolutions, solutions.RowCount);
Assert.Equal(variableCount, solutions.ColumnCount);
AssertEverySolutionSatisfies(equations, variables, solutions);
}
/// <summary>
/// More equations than unknowns used to be refused outright, and a Gröbner basis has
/// no use for as many of one as the other.
/// </summary>
[Fact]
public void AnOverDeterminedConsistentSystemIsSolved()
{
Entity[] equations = { "x^2 + y^2 - 25", "x + y - 7", "x*y - 12" };
Variable[] variables = { "x", "y" };
var solutions = MathS.Equations(equations).Solve(variables);
Assert.NotNull(solutions);
Assert.Equal(2, solutions.RowCount);
AssertEverySolutionSatisfies(equations, variables, solutions);
}
/// <summary>
/// The textbook signal for an inconsistent system is that the basis contains a
/// nonzero constant. It has to arrive as "no solutions" and not as an exception.
/// </summary>
[Fact]
public void AnOverDeterminedInconsistentSystemHasNoSolutions()
{
Entity[] equations = { "x^2 + y^2 - 25", "x + y - 7", "x*y - 99" };
Assert.Null(MathS.Equations(equations).Solve("x", "y"));
}
[Fact]
public void AConsistentSquareSystemIsUnaffected()
{
Entity[] equations = { "x^2 + y^2 - 25", "x + y - 7" };
Variable[] variables = { "x", "y" };
var solutions = MathS.Equations(equations).Solve(variables);
Assert.NotNull(solutions);
Assert.Equal(2, solutions.RowCount);
AssertEverySolutionSatisfies(equations, variables, solutions);
}
/// <summary>
/// The columns are the variables in the order they were asked for, which the
/// triangular back-substitution fills in from the last one first.
/// </summary>
[Fact]
public void ColumnsFollowTheOrderTheVariablesWereGivenIn()
{
Entity[] equations = { "x - 1", "y - 2" };
var solutions = MathS.Equations(equations).Solve("x", "y");
Assert.NotNull(solutions);
Assert.Equal(1, solutions.RowCount);
Assert.Equal(1, solutions[0, 0].EvalNumerical());
Assert.Equal(2, solutions[0, 1].EvalNumerical());
var swapped = MathS.Equations(equations).Solve("y", "x");
Assert.NotNull(swapped);
Assert.Equal(2, swapped[0, 0].EvalNumerical());
Assert.Equal(1, swapped[0, 1].EvalNumerical());
}
/// <summary>
/// Not a polynomial over Q in these variables, so the Gröbner path must decline and
/// leave the answer exactly as it was.
/// </summary>
[Fact]
public void ANonPolynomialSystemIsLeftToTheExistingSolver()
{
Entity[] equations = { "cos(x2 + 1)^2 + 3y", "y * (-1) + 4cos(x2 + 1)" };
var solutions = MathS.Equations(equations).Solve("x", "y");
Assert.NotNull(solutions);
Assert.Equal(8, solutions.RowCount);
}
/// <summary>
/// A radical solution is still an exact one, and one structural pass is enough to
/// prove it satisfies the system, so these are in reach too.
/// </summary>
[Theory]
[InlineData("x^2 - 2", "y - x", 2)]
[InlineData("x^2 - 2", "y^2 - 3", 4)]
[InlineData("x^2 + y^2 - 4", "x - y", 2)]
[InlineData("x^2 + x - 1", "y - x^2", 2)]
[InlineData("x^3 - 2", "y - x", 3)]
public void SystemsWithIrrationalSolutionsAreSolved(string first, string second, int expected)
{
Entity[] equations = { first, second };
Variable[] variables = { "x", "y" };
var solutions = MathS.Equations(equations).Solve(variables);
Assert.NotNull(solutions);
Assert.Equal(expected, solutions.RowCount);
AssertEverySolutionSatisfies(equations, variables, solutions);
}
/// <summary>
/// The basis of <c>x * y, x + y^2 - 1</c> in degree-reverse-lexicographic order has
/// <c>1</c>, <c>y</c> and <c>x</c> under its staircase, and converting it reaches
/// <c>y^2 = 1 - x</c> with the row for <c>1</c> already standing. The row kept for
/// <c>y^2</c> is then <c>-x</c>, and recording it as <c>y^2</c> alone made the element
/// for <c>x</c> come out as <c>x + y^2</c>, whose root <c>(0, 0)</c> is no solution.
/// </summary>
[Fact]
public void TheLexicographicBasisKeepsWhatARowWasReducedBy()
{
Variable[] variables = { "x", "y" };
var index = new Dictionary<Variable, int> { [variables[0]] = 0, [variables[1]] = 1 };
var generators = new[] { "x * y", "x + y^2 - 1" }
.Select(text => MultivariatePolynomial.TryParse(text.ToEntity(), index)!)
.ToList();
var budget = new GroebnerBudget();
var basis = Buchberger.Compute(generators, MonomialOrder.DegreeReverseLexicographic, budget);
Assert.NotNull(basis);
var lexicographic = Fglm.ToLexicographic(basis!, variables.Length, budget);
Assert.NotNull(lexicographic);
var elements = lexicographic!.Select(element => element.ToEntity(variables)).ToList();
Assert.Equal(2, elements.Count);
foreach (var expected in new[] { "y^3 - y", "x + y^2 - 1" })
Assert.Contains(elements, element => (element - expected.ToEntity()).Simplify() == 0);
}
/// <summary>
/// The system the basis above belongs to. With the wrong element the triangularisation
/// was declined, and the eliminator that answered instead solved <c>x * y = 0</c> for
/// <c>y</c> as <c>0</c> alone, which kept <c>(1, 0)</c> and lost <c>(0, 1)</c> and
/// <c>(0, -1)</c>.
/// </summary>
[Fact]
public void ASystemWhoseBasisLeansOnAReducedRowIsSolvedWhole()
{
Entity[] equations = { "x * y", "x + y^2 - 1" };
Variable[] variables = { "x", "y" };
var solutions = MathS.Equations(equations).Solve(variables);
Assert.NotNull(solutions);
Assert.Equal(3, solutions!.RowCount);
AssertEverySolutionSatisfies(equations, variables, solutions);
foreach (var (x, y) in new[] { (1, 0), (0, 1), (0, -1) })
Assert.Contains(Enumerable.Range(0, solutions.RowCount),
row => solutions[row, 0] == Number.Integer.Create(x) && solutions[row, 1] == Number.Integer.Create(y));
}
/// <summary>
/// A basis that is not in shape position. Its element <c>x y + 3 x - 5 y - 15</c> is
/// <c>0</c> at <c>y = -3</c> but was left <c>x * (-3) + 3 * x</c>, which still mentions
/// <c>x</c>, so it was the equation <c>x</c> was solved from there. That solved to every
/// number, the triangularisation was declined, and the eliminator lost <c>(5, 0)</c>.
/// </summary>
[Fact]
public void ARootThatZeroesALeadingCoefficientUsesTheNextElement()
{
Entity[] equations = { "x * y + 3 * x - 5 * y - 15", "x^2 - 3 * x - 4 * y - 10" };
Variable[] variables = { "x", "y" };
var solutions = MathS.Equations(equations).Solve(variables);
Assert.NotNull(solutions);
Assert.Equal(3, solutions!.RowCount);
AssertEverySolutionSatisfies(equations, variables, solutions);
foreach (var (x, y) in new[] { (5, 0), (1, -3), (2, -3) })
Assert.Contains(Enumerable.Range(0, solutions.RowCount),
row => solutions[row, 0] == Number.Integer.Create(x) && solutions[row, 1] == Number.Integer.Create(y));
}
/// <summary>
/// The answers stay exact rather than being handed back as decimals — which is the
/// point of triangularising rather than evaluating.
/// </summary>
[Fact]
public void AnIrrationalSolutionComesBackInRadicals()
{
var solutions = MathS.Equations(new Entity[] { "x^2 - 2", "y - x" }).Solve("x", "y");
Assert.NotNull(solutions);
for (var row = 0; row < solutions.RowCount; row++)
for (var column = 0; column < solutions.ColumnCount; column++)
Assert.DoesNotContain(
solutions[row, column].Nodes,
node => node is Number.Real and not Number.Rational);
}
/// <summary>
/// Where the univariate's roots are decimals there is nothing to prove an identity
/// with, so the system is declined — and the declining has to be quick, because an
/// earlier version proved nothing slowly and cost more than it saved.
/// </summary>
[Fact]
public void APolynomialSystemWithDecimalRootsIsStillAnswered()
{
Entity[] equations = { "x3 + 9 x2 y - 10", "y3 + x y2 - 2" };
var solutions = MathS.Equations(equations).Solve("x", "y");
Assert.NotNull(solutions);
Assert.Equal(9, solutions.RowCount);
}
/// <summary>
/// A free variable means infinitely many solutions, which is not something a
/// triangular basis enumerates — the ideal is not zero-dimensional, so there is no
/// finite set of standard monomials and the conversion declines. What this path hands
/// on is therefore unchanged; what happens after it is not.
/// </summary>
/// <remarks>
/// This used to assert a <c>WrongNumberOfArgumentsException</c>, which is what the whole
/// call raised for a system with fewer equations than unknowns. Such a system is now
/// answered as the family of all its solutions where it is linear
/// (<a href="https://github.com/asc-community/AngouriMath/issues/212">#212</a>), so the
/// assertion here is only that this path declines and not that the call fails.
/// </remarks>
[Fact]
public void AnUnderDeterminedSystemIsNotTriangularised()
{
Assert.False(Functions.Algebra.Groebner.GroebnerSystemSolver.TrySolve(
new List<Entity> { "x + y - 3" }, new Entity.Variable[] { "x", "y" }, out _));
// And the call as a whole answers it, rather than raising as it used to.
var solutions = MathS.Equations(new Entity[] { "x + y - 3" }).Solve("x", "y");
Assert.NotNull(solutions);
Assert.Equal(1, solutions!.RowCount);
Assert.Equal(2, solutions.ColumnCount);
}
/// <summary>
/// Eight unknowns is the ceiling of the packed representation; nine has to fall
/// through rather than be truncated.
/// </summary>
[Fact]
public void MoreVariablesThanThePackingHoldsFallsThrough()
{
var equations = new Entity[9];
var variables = new Variable[9];
for (var i = 0; i < 9; i++)
{
variables[i] = $"v_{i}";
equations[i] = variables[i] - (i + 1);
}
var solutions = MathS.Equations(equations).Solve(variables);
Assert.NotNull(solutions);
Assert.Equal(1, solutions.RowCount);
}
}
}