/
test.py
550 lines (456 loc) · 12.6 KB
/
test.py
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
from options import opts
#from dual_st_alg import Monomial, Polynomial, opts, TensorMonomial, TensorPolynomial
from dual_st_alg import *
from mod_lin_alg import ModVector, ModMatrix
from cohomology import Cohomology
from cobar_complex import CobarMonomial, CobarPolynomial, CobarModule, CobarComplex
import random
#opts.prime = 3
opts.prime = 3
#opts.prime = 13
#opts.prime = 541
def simplify_test(length, n):
"""
length is length of monomials
n dictates how many tests are done
"""
for i in range(n):
xx = Monomial.random(length)
print xx
xx.simplify()
print xx
print "\n"
#simplify_test(10,5)
def product_test(length, n):
for i in range(n):
xx = Monomial.random(length)
yy = Monomial.random(length)
zz = xx*yy
tt = yy*xx
xx.simplify()
yy.simplify()
print xx
print yy
print zz
print tt
print "\n"
# product_test(1, 10)
# xx = Monomial.tau_list([1])
# yy = Monomial.tau_list([0,1])
# zz = Monomial.tau_list([0,0,1])
# print "x", xx
# print "y", yy
# print "z", zz
# print "xyz", xx*yy*zz
# print "zxy", zz*xx*yy
# print "zyx", zz*yy*xx
# print "xzy", xx*zz*yy
# print "yzx", yy*zz*xx
def equality_test(length, n):
for i in range(n):
xx = Monomial.random(length)
yy = Monomial.random(length)
print xx, yy, xx==yy
#equality_test(2, 100)
def degree_test(length, n):
for i in range(n):
xx = Monomial.random(length)
print xx
print xx.get_degree()
for i in range(n):
xx = Monomial.term(("t", i))
print xx
print xx.get_degree()
for i in range(n):
xx = Monomial.term(("x", i+1))
print xx
print xx.get_degree()
#print opts.prime
#degree_test(2,10)
def homogeneous_test(x,y,n):
for i in range(n):
xx = Polynomial.random(x,y)
xx.simplify()
print xx, " ... ", str(xx.homogeneous())
#homogeneous_test(2,2,100)
def poly_simplify_test(x,y,n):
for i in range(n):
xx = Polynomial.random(x,y)
print xx
xx.stupid_simplify()
print str(xx) + "\n"
#print opts.prime
#poly_simplify_test(4,2,10)
def poly_simplify_test(x,y,n):
for i in range(n):
xx = Polynomial.random(x,y)
yy = xx + xx
print yy
yy.simplify()
print str(yy) + "\n"
#poly_simplify_test(2,3,20)
def poly_test_eq(x,y,n):
for i in range(n):
xx = Polynomial.random(x,y)
yy = Polynomial.random(x,y)
print xx, "...", yy, "...", xx==yy
#poly_test_eq(2,2,1000)
def tensmon_simplify(x,y,n):
for i in range(n):
xx = Monomial.random(x)
yy = Monomial.random(y)
zz = TensorMonomial([xx,yy], random.randrange(opts.prime))
print zz
zz.simplify()
print zz, "\n"
#tensmon_simplify(3,3,100)
def tenspoly_str(x,y,n):
for i in range(n):
xx = TensorPolynomial.random(x,y)
print xx
#tenspoly_str(2,3,10)
def tenspoly_sum(x,y,n):
for i in range(n):
xx = TensorPolynomial.random(x,y)
yy = TensorPolynomial.random(x,y)
zz = xx + yy
print xx
print yy
print str(zz) + "\n"
#tenspoly_sum(3,3,10)
def tensmon_eq(x,n):
for i in range(n):
xx = TensorMonomial.random(x)
yy =TensorMonomial.random(x)
xx.simplify()
yy.simplify()
print xx, yy, xx == yy
#tensmon_eq(3,100000)
def tenspoly_simplify(x,y,n):
for i in range(n):
xx = TensorPolynomial.random(x,y)
print xx
xx.simplify()
print xx, "\n"
#tenspoly_simplify(3,3,100)
def tenspoly_add(x,y,n):
for i in range(n):
xx = TensorPolynomial.random(x,y)
yy = TensorPolynomial.random(x,y)
print xx
print yy
print xx + yy + xx
zz = xx + yy + xx
zz.simplify()
print zz, "\n"
#tenspoly_add(1,1,100)
def tenspoly_mult(x,y,n):
for i in range(n):
xx = TensorPolynomial.random(x,y)
yy = TensorPolynomial.random(x,y)
print xx
print yy
zz = xx * yy
zz.simplify()
print zz, "\n"
#tenspoly_mult(3,3,100)
def coprod_term(n):
for i in range(1,n):
print Monomial.term_coproduct(("x", i))
for i in range(n):
print Monomial.term_coproduct(("t", i))
#coprod_term(4)
def coprod_test(x,n):
for i in range(n):
xx = Monomial.random(x)
print xx
yy = xx.coproduct()
print yy, "\n"
#coprod_test(2, 5)
def reduced_coprod_test(x,n):
for i in range(n):
xx = Monomial.random(x)
print xx
print "coprod", xx.coproduct(), "\n"
print "reduced", xx.reduced_coproduct(), "\n"
#reduced_coprod_test(2,5)
#####################################################
def vector_add(x, n):
for i in range(n):
xx = ModVector.random(x)
yy = ModVector.random(x)
print xx
print yy
print xx+yy, "\n"
# opts.prime = 13
# print opts.prime
# vector_add(5, 10)
def vector_mul(x,n):
for i in range(n):
xx = ModVector.random(x)
yy = ModVector.random(x)
print xx
print yy
print xx * yy, "\n"
# opts.prime = 3
# print opts.prime
# vector_mul(5, 10)
def scalar_mul(x,n):
for i in range(n):
xx = ModVector.random(x)
aa = random.randrange(opts.prime)
print aa, xx
print aa * xx
# opts.prime = 5
# scalar_mul(3,10)
# vector_mul(3,10)
def leading_index(x,n):
for i in range(n):
xx = ModVector.random(x)
print xx
print xx.get_leading_index()
print len(xx)
#leading_index(2,10)
# print ModMatrix.random(4,5)
# print ModMatrix.null(4, 4)
# print ModMatrix.identity(7)
def matrix_add(i, j, n):
for a in range(n):
xx = ModMatrix.random(i, j)
yy = ModMatrix.random(i, j)
print xx, "\n", yy, "\n", xx + yy, "\n"
xx = ModMatrix.random(i, j)
yy = ModMatrix.random(i+1, j+1)
try:
print xx + yy
except TypeError:
print xx, "\n", "wrong size", "\n", yy
#matrix_add(3,4,2)
def matrix_mul(i,j,k,n):
for a in range(n):
xx = ModMatrix.random(i,j)
yy = ModMatrix.random(j,k)
print xx, "\n\n", yy, "\n\n", xx*yy, "\n"
#matrix_mul(10,23,15,2)
def row_op_test(x,y,n):
for i in range(n):
xx = ModMatrix.random(x,y)
print xx
xx.el_row_op( random.randrange(opts.prime), 0, 1)
print xx
#row_op_test(2,4,5)
def rref_test(x,y,n):
for i in range(n):
xx = ModMatrix.random(x,y)
xx.compute_rref()
print xx, "\n"
print xx.rref, "\n"
print xx.basis_change, "\n"
pp = xx.basis_change
print pp * xx
rr = pp * xx
print rr == xx.rref
#rref_test(7,4,1)
def solve_test(x,y,n):
for i in range(n):
AA = ModMatrix.random(x,y)
bb = ModVector.random(x)
print AA.can_solve(bb)
xx = AA.solve(bb)
print "A\n", AA, "\n"
print "rrefA\n", AA.get_rref(), "\n"
print "b\n", bb, "\n"
print "x\n", xx, "\n"
print "Ax\n", AA * xx
print AA * xx == ModMatrix([bb]).get_transpose()
#solve_test(12,28,1)
# AA = ModMatrix([ModVector([0,1]), ModVector([2,2]), ModVector([1,2])])
# bb = ModVector([1,0,1])
# print AA, "\n"
# print bb, "\n"
# print "row weights\n", AA.get_row_weights(), "\n"
# print "rref\n", AA.get_rref(), "\n"
# print "basis change\n", AA.get_basis_change(), "\n"
# print AA.can_solve(bb)
def ker_test(x,y,n):
for i in range(n):
AA = ModMatrix.random(x,y)
print AA.get_rref(), "\n"
ker = AA.get_kernel()
for xx in ker:
print "soln\n", xx, "\n"
print "result\n", AA * xx
#ker_test(3,4,1)
def rank_test(x,y,n):
for i in range(n):
AA = ModMatrix.random(x,y)
print "AA\n", AA, "\n"
print AA.get_rank()
#rank_test(3,3,5)
def inv_test(x,n):
for i in range(n):
AA = ModMatrix.random(x,x)
print "AA\n", AA, "\n"
print "inv\n", AA.get_inverse(), "\n"
if AA.get_inverse():
print "Ix\n", AA * AA.get_inverse(), "\n"
#inv_test(2,2)
#inv_test(3,2)
def append_test(x,y,n):
for i in range(n):
AA = ModMatrix.random(x,y)
zz = ModVector.random(x)
print "AA\n", AA, "\n"
print "zz\n", zz, "\n"
print "append\n", AA.get_append_columns([zz,zz + zz]), "\n"
#append_test(2,3,5)
def cohom_test(x,y,z,n):
for i in range(n):
AA = ModMatrix.random(y,z)
BB = ModMatrix.random(x,y)
if (BB * AA).is_zero():
print"BB\n", BB, "\n"
print "AA\n", AA, "\n"
coh = Cohomology(BB, AA)
print coh.get_cohomology()
#cohom_test(0,0,0,2)
def cobar_mon(x,f,n):
for i in range(n):
xx = CobarMonomial.random(x,f)
print xx, "\n"
print xx.get_degree(), "\n"
#cobar_mon(2,4,3)
def cobar_simp(x,f,n):
for i in range(n):
xx = CobarMonomial.random(x,f)
print xx
xx.simplify()
print xx
for term in xx.factors:
print term
#cobar_simp(2,3,2)
def cobar_poly_simp(x,y,f,n):
for i in range(n):
xx = CobarPolynomial.random(x,y,f)
yy = CobarPolynomial.random(x,y,f)
zz = xx + yy
print zz
zz.simplify()
print zz
zz = zz + zz
zz.simplify()
print zz
#cobar_poly_simp(2,3,3,2)
def cobar_concat(x,y,f,n):
for i in range(n):
xx = CobarPolynomial.random(x,y,f)
yy = CobarPolynomial.random(x,y,f)
xx.simplify()
yy.simplify()
zz = xx & yy
print xx
print yy
print zz, "\n"
#cobar_concat(2,2,2,2)
# opts.prime = 3
# opts.bounds = 20
# print opts.prime, opts.bounds
# print xi_indices()
# print [xi_deg(y) for y in xi_indices()]
# print tau_indices()
# print [tau_deg(y) for y in tau_indices()]
# new = sorted(tau_indices(), key=lambda y: tau_deg(y))
# print new
# print [tau_deg(y) for y in new]
# print "\n"
# print all_indices()
# print [tau_deg(p[0]) + xi_deg(p[1]) for p in all_indices()]
def cplx_test(length):
C = CobarComplex(length)
C.generate_modules()
for i in range(C.length):
print "module", i
for deg in C.cplx[i]._dict:
print "deg", deg
for thing in C.cplx[i]._dict[deg]:
print thing
#opts.prime = 3
#opts.bounds = 20
#cplx_test(7)
def map_test(f,x,n):
for i in range(n):
xx = CobarMonomial.random(x,f)
print xx
print xx._map_reduced()
#map_test(1,1,1)
def summand_test(x,y,f,n):
for i in range(n):
xx = CobarMonomial.random(x,f)
yy = CobarPolynomial.random(x,y,f)
xx.simplify()
yy.simplify()
print xx
print yy
print yy.is_summand(xx), "\n"
#summand_test(2,5,2,500)
def vfe_test(x,f,n):
C= CobarComplex(7)
print "calculating complex"
cplx = C.get_cplx()
for i in range(n):
xx = CobarMonomial.random(x,f)
print "finding good xx"
while xx.get_degree()[0] > opts.bounds:
xx = CobarMonomial.random(x,f)
yy = xx._map()
vect = C.vector_from_element(yy)
deg = yy.get_degree()[0]
wt = yy.get_degree()[1]
for thing in C.get_cplx()[f+1]._dict[(deg,wt)]:
print thing
print "\n"
print xx
print yy
print vect
#opts.prime=5
#opts.bounds=15
#vfe_test(1,1,10)
def cohom_test(f):
out = open("cohom"+str(opts.prime)+"-"+str(opts.bounds)+".txt", "a")
C = CobarComplex(f+2)
C.get_pickled_cplx()
#C.make_maps()
for i in range(1,f+1):
for bideg in C.get_cplx()[i]._dict.keys():
d = bideg[0]
w = bideg[1]
cohom = C.get_cohomology(i,d,w)
out.write("Filt, deg, wt"+str(i)+" "+str(d)+" "+str(w)+"\n")
cc = cohom.get_cohomology()
out.write(str(cc)+"\n")
if cc.get_basis():
for vect in cc.get_basis():
out.write(str(C.element_from_vector(vect, i,d,w))+"\n")
C.compute_product_structure()
for thing in C.product_generators:
out.write(str(thing.get_degree()) + " " + str(thing) + "\n")
out.close()
C.pickle_cplx()
opts.prime = 5
opts.bounds = 35
#C = CobarComplex(5)
#C.get_pickled_cplx()
#C.extend_complex(5)
#C.pickle_cplx()
cohom_test(3)
#C.compute_product_structure()
#for thing in C.product_generators:
# print thing.get_degree(), thing, "\n"
#C = CobarComplex(4)
#C.get_pickled_cplx()
#C.extend_complex(5)
#C.pickle_cplx()
#cohom_test(5)
#C = CobarComplex(7)
#C.make_map(1)
#C.make_map(2)