-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflint_fmpz_poly_test.go
258 lines (227 loc) · 5.53 KB
/
flint_fmpz_poly_test.go
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
package goflint
import (
"fmt"
"testing"
)
func TestFmpzPolyString(t *testing.T) {
for _, tc := range []struct {
name string
m *FmpzPoly
want string
}{
{
name: "f(x)=5x^3+2x+1",
m: NewFmpzPoly().SetCoeffUI(0, 1).SetCoeffUI(1, 2).SetCoeffUI(2, 0).SetCoeffUI(3, 5),
want: "5*x^3+2*x+1",
},
} {
got := tc.m.String()
if got != tc.want {
t.Errorf("String() %s want / got mismatch: %v / %v", tc.name, tc.want, got)
}
}
}
func TestFmpzPolyStringSimple(t *testing.T) {
for _, tc := range []struct {
name string
m *FmpzPoly
want string
}{
{
name: "f(x)=5x^3+2x+1",
m: NewFmpzPoly().SetCoeffUI(0, 1).SetCoeffUI(1, 2).SetCoeffUI(2, 0).SetCoeffUI(3, 5),
want: "4 1 2 0 5",
},
} {
got := tc.m.StringSimple()
if got != tc.want {
t.Errorf("StringSimple() %s want / got mismatch: %v / %v", tc.name, tc.want, got)
}
}
}
func TestFmpzPolySetString(t *testing.T) {
for _, tc := range []struct {
name string
poly string
want *FmpzPoly
}{
{
name: "f(x)=5x^3+2x+1",
poly: "4 1 2 0 5",
want: NewFmpzPoly().SetCoeffUI(0, 1).SetCoeffUI(1, 2).SetCoeffUI(2, 0).SetCoeffUI(3, 5),
},
} {
got, err := FmpzPolySetString(tc.poly)
if err != nil {
t.Errorf("FmpzPolySetString() got error when not expected: %v", err)
}
if !got.Equal(tc.want) {
t.Errorf("FmpzPolySetString() %s want / got mismatch: %v / %v", tc.name, tc.want, got)
}
}
}
func TestFmpzPolyPow(t *testing.T) {
for _, tc := range []struct {
name string
m *FmpzPoly
e int
want string
}{
{
name: "f(x)=5x^3+2x+1",
m: NewFmpzPoly().SetCoeffUI(0, 1).SetCoeffUI(1, 2).SetCoeffUI(2, 0).SetCoeffUI(3, 5),
e: 3,
want: "125*x^9+150*x^7+75*x^6+60*x^5+60*x^4+23*x^3+12*x^2+6*x+1",
},
} {
z := tc.m.Pow(tc.m, tc.e)
got := z.String()
if got != tc.want {
t.Errorf("Pow() %s want / got mismatch: %v / %v", tc.name, tc.want, got)
}
}
}
func TestFmpzPolyGCD(t *testing.T) {
for _, tc := range []struct {
name string
m *FmpzPoly
want string
}{
{
name: "f(x)=5x^3+2x+1",
m: NewFmpzPoly().SetCoeffUI(0, 1).SetCoeffUI(1, 2).SetCoeffUI(2, 0).SetCoeffUI(3, 5),
want: "25*x^6+20*x^4+10*x^3+4*x^2+4*x+1",
},
} {
y := NewFmpzPoly().Mul(tc.m, tc.m)
z := NewFmpzPoly().Mul(tc.m, tc.m)
z.Mul(z, tc.m)
g := NewFmpzPoly().GCD(y, z)
got := g.String()
if got != tc.want {
t.Errorf("GCD() %s want / got mismatch: %v / %v", tc.name, tc.want, got)
}
}
}
func TestFmpzPolyLen(t *testing.T) {
for _, tc := range []struct {
name string
m *FmpzPoly
want int
}{
{
name: "f(x)=5x^3+2x+1 has len 4",
m: NewFmpzPoly().SetCoeffUI(0, 1).SetCoeffUI(1, 2).SetCoeffUI(2, 0).SetCoeffUI(3, 5),
want: 4,
},
} {
got := tc.m.Len()
if got != tc.want {
t.Errorf("Len() %s want / got mismatch: %v / %v", tc.name, tc.want, got)
}
}
}
func TestFmpzPolyGetCoeff(t *testing.T) {
for _, tc := range []struct {
name string
m *FmpzPoly
c int
want *Fmpz
}{
{
name: "f(x)=5x^3+2x+1 3rd coefficient of 5",
m: NewFmpzPoly().SetCoeffUI(0, 1).SetCoeffUI(1, 2).SetCoeffUI(2, 0).SetCoeffUI(3, 5),
c: 3,
want: NewFmpz(5),
},
} {
got := tc.m.GetCoeff(tc.c)
if got.Cmp(tc.want) != 0 {
t.Errorf("GetCoeff() %s want / got mismatch: %v / %v", tc.name, tc.want, got)
}
}
}
func TestFmpzPolyGetCoeffs(t *testing.T) {
for _, tc := range []struct {
name string
m *FmpzPoly
want []*Fmpz
}{
{
name: "f(x)=5x^3+2x+1 coeffs are 1,2,0,5",
m: NewFmpzPoly().SetCoeffUI(0, 1).SetCoeffUI(1, 2).SetCoeffUI(2, 0).SetCoeffUI(3, 5),
want: []*Fmpz{NewFmpz(1), NewFmpz(2), NewFmpz(0), NewFmpz(5)},
},
} {
got := tc.m.GetCoeffs()
for i, g := range got {
if g.Cmp(tc.want[i]) != 0 {
t.Errorf("GetCoeffs() %s want / got mismatch: %v / %v", tc.name, tc.want, got)
}
}
}
}
func TestFmpzPolyAdd(t *testing.T) {
for _, tc := range []struct {
name string
m1 *FmpzPoly
m2 *FmpzPoly
want string
}{
{
name: "f(x)=5x^3+2x+1 + x^3+3x^2+2 = 3*x^2+2*x+3",
m1: NewFmpzPoly().SetCoeffUI(0, 1).SetCoeffUI(1, 2).SetCoeffUI(2, 0).SetCoeffUI(3, 5),
m2: NewFmpzPoly().SetCoeffUI(0, 2).SetCoeffUI(1, 0).SetCoeffUI(2, 3).SetCoeffUI(3, 1),
want: "6*x^3+3*x^2+2*x+3",
},
} {
got := NewFmpzPoly().Add(tc.m1, tc.m2).String()
if got != tc.want {
t.Errorf("Add() %s want / got mismatch: %v / %v", tc.name, tc.want, got)
}
}
}
func TestFmpzPolySub(t *testing.T) {
for _, tc := range []struct {
name string
m1 *FmpzPoly
m2 *FmpzPoly
want string
}{
{
name: "f(x)=5x^3+2x+1 - x^3+3x^2+2 = 4*x^3+3*x^2+2*x+5",
m1: NewFmpzPoly().SetCoeffUI(0, 1).SetCoeffUI(1, 2).SetCoeffUI(2, 0).SetCoeffUI(3, 5),
m2: NewFmpzPoly().SetCoeffUI(0, 2).SetCoeffUI(1, 0).SetCoeffUI(2, 3).SetCoeffUI(3, 1),
want: "4*x^3-3*x^2+2*x-1",
},
} {
got := NewFmpzPoly().Sub(tc.m1, tc.m2).String()
if got != tc.want {
t.Errorf("Sub() %s want / got mismatch: %v / %v", tc.name, tc.want, got)
}
}
}
func TestFmpzPolyFactor(t *testing.T) {
for _, tc := range []struct {
name string
poly *FmpzPoly
want string
}{
{
name: "f(x)=10x^3+5x^2+5x+5 factors to 5, (2*x^3+x^2+x+1, 1)",
poly: NewFmpzPoly().SetCoeffUI(0, 5).SetCoeffUI(1, 5).SetCoeffUI(2, 5).SetCoeffUI(3, 10),
want: "5, (2*x^3+x^2+x+1, 1)",
},
} {
fac := tc.poly.Factor()
got := fac.GetCoeff().String()
for i := 0; i < fac.Len(); i++ {
p := fac.GetPoly(i)
e := fac.GetExp(i)
got = fmt.Sprintf("%s, (%v, %d)", got, p, e)
}
if got != tc.want {
t.Errorf("Factor() %s want / got mismatch: %v / %v", tc.name, tc.want, got)
}
}
}