-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
337 lines (298 loc) · 9.69 KB
/
example_test.go
File metadata and controls
337 lines (298 loc) · 9.69 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
330
331
332
333
334
335
336
337
package nag_test
import (
"fmt"
"math"
"github.com/fumin/nag"
)
func Example() {
// This example shows how to simplify the expression
//
// bbaa - aabb + aba
//
// assuming
//
// aba - b = 0
//
// where a, b are noncommutative variables.
// Using Gröbner bases we will eventually see that bbaa-aabb+aba -> b.
expr := "bbaa - aabb + aba"
rules := []string{"aba - b"}
// Compute the Gröbner basis using the Buchberger algorithm.
variables := map[string]nag.Symbol{"a": 1, "b": 2}
ideal := make([]*nag.Polynomial[*nag.Rat], len(rules))
ideal[0], _ = nag.Parse(variables, nag.ElimOrder(), rules[0])
basis, _ := nag.Buchberger(ideal, 50)
// Print the Gröbner basis and notice that we have found an additional
// useful relation: bba = abb
fmt.Printf("Gröbner basis:\n")
for _, b := range basis {
fmt.Printf(" %v = 0\n", b)
}
fmt.Printf("\n")
// Use the Gröbner basis to simplify the target expression.
exprP, _ := nag.Parse(variables, nag.ElimOrder(), expr)
_, simplified := nag.Divide(nil, exprP, basis)
fmt.Printf("Simplified result: %v\n", simplified)
// Output:
// Gröbner basis:
// aba-b = 0
// b^2a-ab^2 = 0
//
// Simplified result: b
}
func Example_equation_solving() {
// This example shows solving a set of equations using Gröbner bases.
//
// Given the set of equations below, we want to obtain an expression for
// the variable G in terms of other variables D, X, A, B,... etc.
//
// The context behind this example is about the boundary value problem of differential equations.
// In fact, the variable G represents the Green's function operator.
// For more details, please see:
// Rosenkranz, M., Buchberger, B., & Engl, H. W. (2003). Solving linear boundary value problems via non-commutative Gröbner bases. Applicable Analysis, 82(7), 655-675.
equations := []string{
"D^2GD^2 - D^2",
"GD^2G - G",
"GD^2 - 1 + (1-X)L + XR",
"D^2G - 1",
"DX - XD - 1",
"DA - 1",
"AD - 1 + L",
"DB + 1",
"BD - R + 1",
"RX - R",
"LX",
}
variables := map[string]nag.Symbol{"D": 1, "L": 2, "X": 3, "A": 4, "B": 5, "R": 6, "G": 7}
ideal := make([]*nag.Polynomial[*nag.Rat], len(equations))
for i, eq := range equations {
ideal[i], _ = nag.Parse(variables, nag.ElimOrder(), eq)
}
basis, _ := nag.Buchberger(ideal, 50)
solution := basis[len(basis)-1]
fmt.Printf("Solution: %v = 0\n", solution)
// Output:
// Solution: G-XBX+XB-XAX+AX = 0
}
func Example_minimal_polynomial() {
// This example shows how to find the minimal polynomial for α = √2+√3+√5.
// The minimal polynomial, P(x), for an algebraic number α is one whose
// coefficients are integers and which evaluates to zero when α is
// passed in, i.e. P(α) = 0.
ideal := []string{
"x^2 - 2",
"y^2 - 3",
"z^2 - 5",
"α - x - y - z",
// Equations below express the fact that all variables commute.
"xy - yx",
"xz - zx",
"xα - αx",
"yz - zy",
"yα - αy",
"zα - αz",
}
// Compute the Gröbner basis.
variables := map[string]nag.Symbol{"x": 4, "y": 3, "z": 2, "α": 1}
idealP := make([]*nag.Polynomial[*nag.Rat], len(ideal))
for i, p := range ideal {
idealP[i], _ = nag.Parse(variables, nag.ElimOrder(), p)
}
basis, _ := nag.Buchberger(idealP, 50)
fmt.Printf("Gröbner basis:\n")
for _, b := range basis {
fmt.Printf(" %v = 0\n", b)
}
fmt.Printf("\n")
// The minimal polynomial for α is one that contains only α without other variables.
// In this case, it is the first basis:
//
// α^8-40α^6+352α^4-960α^2+576
//
// Verify that the above polynomial is indeed zero when α = √2+√3+√5.
minPoly := basis[0]
α := math.Sqrt(2) + math.Sqrt(3) + math.Sqrt(5)
var res float64
for c, w := range minPoly.Terms() {
cf, _ := c.Float64()
pow := float64(len(w))
res += cf * math.Pow(α, pow)
}
fmt.Printf("minPoly(α): %f\n", math.Abs(res))
// Output:
// Gröbner basis:
// α^8-40α^6+352α^4-960α^2+576 = 0
// z-5/576α^7+97/288α^5-95/36α^3+53/12α = 0
// y+1/96α^7-37/96α^5+61/24α^3-15/4α = 0
// x-1/576α^7+7/144α^5+7/72α^3-5/3α = 0
//
// minPoly(α): 0.000000
}
func ExampleElimOrder() {
order := nag.ElimOrder()
fmt.Println(order(nag.Monomial{2, 2, 2}, nag.Monomial{2, 2, 1, 1}))
fmt.Println(order(nag.Monomial{2, 2}, nag.Monomial{2, 2, 1, 1}))
fmt.Println(order(nag.Monomial{1, 1, 2, 2}, nag.Monomial{2, 2, 1, 1}))
// Output:
// 1
// -1
// -1
}
func ExampleDeglex() {
fmt.Println(nag.Deglex(nag.Monomial{2, 2, 2}, nag.Monomial{2, 2, 1, 1}))
fmt.Println(nag.Deglex(nag.Monomial{2, 2, 2}, nag.Monomial{2, 2, 1}))
fmt.Println(nag.Deglex(nag.Monomial{2, 2, 2}, nag.Monomial{2, 3, 1}))
// Output:
// -1
// 1
// -1
}
func ExamplePolynomial_Terms() {
p := nag.NewPolynomial(
nag.NewRat(0, 1), nag.Deglex,
nag.PolynomialTerm[*nag.Rat]{Coefficient: nag.NewRat(3, 2), Monomial: nag.Monomial{1, 1, 1}},
nag.PolynomialTerm[*nag.Rat]{Coefficient: nag.NewRat(-1, 1), Monomial: nag.Monomial{2, 2}},
nag.PolynomialTerm[*nag.Rat]{Coefficient: nag.NewRat(5, 1), Monomial: nag.Monomial{1, 2}},
)
for coeffi, monomial := range p.Terms() {
fmt.Printf("coefficient: %s, monomial: %v\n", coeffi.RatString(), monomial)
}
// Output:
// coefficient: 3/2, monomial: [1 1 1]
// coefficient: -1, monomial: [2 2]
// coefficient: 5, monomial: [1 2]
}
func ExamplePolynomial_LeadingTerm() {
terms := []nag.PolynomialTerm[*nag.Rat]{
{Coefficient: nag.NewRat(1, 2), Monomial: nag.Monomial{1, 1, 1}},
{Coefficient: nag.NewRat(1, 3), Monomial: nag.Monomial{2, 2}},
}
p0 := nag.NewPolynomial(nag.NewRat(0, 1), nag.Deglex, terms...)
fmt.Println(p0.LeadingTerm())
p1 := nag.NewPolynomial(nag.NewRat(0, 1), nag.ElimOrder(), terms...)
fmt.Println(p1.LeadingTerm())
// Output:
// {1/2 [1 1 1]}
// {1/3 [2 2]}
}
func ExampleDivide() {
variables := map[string]nag.Symbol{"x": 3, "y": 2, "z": 1}
f, _ := nag.Parse(variables, nag.Deglex, "zx^2yx")
g := make([]*nag.Polynomial[*nag.Rat], 2)
g[0], _ = nag.Parse(variables, nag.Deglex, "xy + x")
g[1], _ = nag.Parse(variables, nag.Deglex, "x^2 + xz")
// Create a copy of f since nag.Divide modifies f upon return.
fCopy := nag.NewPolynomial(nag.NewRat(0, 1), nag.Deglex).Set(f)
var remainder *nag.Polynomial[*nag.Rat]
quotient := make([][]nag.Quotient[*nag.Rat], 0)
// Perfom the division.
quotient, remainder = nag.Divide(quotient, fCopy, g)
fmt.Println("remainder:", remainder)
// Check that f = quotient*g + remainder.
ff := nag.NewPolynomial(nag.NewRat(0, 1), nag.Deglex)
ff.SymbolStringer = f.SymbolStringer
cw := nag.NewPolynomial(nag.NewRat(0, 1), nag.Deglex)
cwg := nag.NewPolynomial(nag.NewRat(0, 1), nag.Deglex)
cwgw := nag.NewPolynomial(nag.NewRat(0, 1), nag.Deglex)
for i := range quotient {
for j := range quotient[i] {
cij := nag.NewPolynomial(nag.NewRat(0, 1), nag.Deglex, nag.PolynomialTerm[*nag.Rat]{Coefficient: quotient[i][j].Coefficient})
wij := nag.NewPolynomial(nag.NewRat(0, 1), nag.Deglex, nag.PolynomialTerm[*nag.Rat]{Monomial: quotient[i][j].Left})
wPij := nag.NewPolynomial(nag.NewRat(0, 1), nag.Deglex, nag.PolynomialTerm[*nag.Rat]{Monomial: quotient[i][j].Right})
cwgw.Mul(cwg.Mul(cw.Mul(cij, wij), g[i]), wPij)
ff.Add(ff, cwgw)
}
}
ff.Add(ff, remainder)
fmt.Println("g*quotient + remainder:", ff, "==", f)
// Output:
// remainder: zxzx
// g*quotient + remainder: zx^2yx == zx^2yx
}
func ExampleBuchberger() {
ideal := []string{
"aba - b",
"bab - b",
}
// Run the Buchberger algorithm.
variables := map[string]nag.Symbol{"a": 1, "b": 2}
idealP := make([]*nag.Polynomial[*nag.Rat], len(ideal))
idealP[0], _ = nag.Parse(variables, nag.Deglex, ideal[0])
idealP[1], _ = nag.Parse(variables, nag.Deglex, ideal[1])
basis, complete := nag.Buchberger(idealP, 10)
// Print the computed Gröbner basis.
fmt.Println("Gröbner basis:")
fmt.Println("")
for _, b := range basis {
fmt.Println(" ", b)
}
fmt.Println("")
fmt.Println("Basis is complete:", complete)
// Output:
// Gröbner basis:
//
// ba-ab
// b^2-ab
// a^2b-b
//
// Basis is complete: true
}
func ExampleBuchbergerHomogeneous() {
ideal := []string{
"x^2 - 2y^2",
"xy - 3z^2",
}
variables := map[string]nag.Symbol{"x": 1, "y": 2, "z": 3}
idealP := make([]*nag.Polynomial[*nag.Rat], len(ideal))
for i := range ideal {
idealP[i], _ = nag.Parse(variables, nag.Deglex, ideal[i])
}
// Run the homogeneous Buchberger algorithm and truncate at degree 3.
var maxDeg int = 3
basis3, complete := nag.BuchbergerHomogeneous(idealP, maxDeg)
fmt.Printf("Gröbner basis truncated at degree %d:\n", maxDeg)
for _, b := range basis3 {
fmt.Println(" ", b)
}
fmt.Println("Basis is complete:", complete)
fmt.Println("")
// Run Buchberger again and truncate at a higher degree.
// We will get the full basis this time round, since maxDeg is higher than the basis' maximum degree.
maxDeg = 5
basis, complete := nag.BuchbergerHomogeneous(idealP, maxDeg)
fmt.Printf("Gröbner basis truncated at degree %d:\n", maxDeg)
// Skip printing bases that have been printed above.
fmt.Printf(" ...\n")
for _, b := range basis[len(basis3):] {
fmt.Println(" ", b)
}
fmt.Println("Basis is complete:", complete)
// Output:
// Gröbner basis truncated at degree 3:
// y^2-1/2x^2
// z^2-1/3xy
// yx^2-x^2y
// zxy-xyz
// Basis is complete: false
//
// Gröbner basis truncated at degree 5:
// ...
// zx^3-2xyzy
// Basis is complete: true
}
func ExampleParse() {
pStr := "-x^2y^3 + 5/3(y-x)x"
variables := map[string]nag.Symbol{"x": 1, "y": 2}
p, err := nag.Parse(variables, nag.Deglex, pStr)
if err != nil {
fmt.Println("error:", err)
return
}
for coefficient, monomial := range p.Terms() {
fmt.Printf("coefficient: %s, monomial: %v\n", coefficient.RatString(), monomial)
}
// Output:
// coefficient: -1, monomial: [1 1 2 2 2]
// coefficient: 5/3, monomial: [2 1]
// coefficient: -5/3, monomial: [1 1]
}