-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain2matrix.py
73 lines (57 loc) · 2.38 KB
/
chain2matrix.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
# Standard imports
from sys import stdout
from re import sub
from argparse import ArgumentParser
from argparse import FileType
# Local imports
import CellChainParse
from Coalgebra import Coalgebra
__author__ = 'mfansler'
def format_cells(cells):
return sub(',', '_', sub(r'[{}]', '', str(cells)))
argparser = ArgumentParser(description="Parses LaTeX descriptions of differential "
"graded coalgebras and outputs incidence matrices")
argparser.add_argument('--sage', action='store_true', help="output sage matrix")
argparser.add_argument('--chomp', action='store_true', help="output CHomP matrix")
argparser.add_argument('--out', '-o', dest='out', type=FileType('w'), help="location to store output")
argparser.add_argument('file', type=file, help="LaTeX file to be parsed")
args = None
try:
args = argparser.parse_args()
except Exception as e:
print e.strerror
argparser.print_help()
raise SystemExit
else:
data = args.file.read()
args.file.close()
result = CellChainParse.parse(data)
if not result:
raise SystemExit
f = args.out if args.out else stdout
C = Coalgebra(result["groups"], result["differentials"], result["coproducts"])
differential = {n: C.incidence_matrix(n) for n in range(1, C.topDimension() + 1)}
if args.sage:
for n, entries in differential.iteritems():
print >> f, "d{} = matrix(Integers(2), {}, {}, {}, sparse=True)".format(
n, len(C.groups[n-1]), len(C.groups[n]), entries
)
print >> f, "ChainComplex({", ", ".join(["{}: d{}".format(n, n) for n in differential.keys()]), "}, degree=-1)"
print >> f, "var({})".format(", ".join(["'" + format_cells(cell) + "'" for group in C.groups.values() for cell in group]))
first = True
print >> f, "{ ",
for n, group in C.groups.items():
if not first:
print >> f, ", ",
else:
first = False
print >> f, "{}: matrix(SR, [{}])".format(n, ", ".join(
["'" + format_cells(cell) + "'" for cell in group])),
print >> f, "}"
if args.chomp:
for n, entries in differential.iteritems():
print >> f, n - 1
for entry in ["{} {} {}".format(l, r, v) for (l, r), v in entries.iteritems()]:
print >> f, entry
if args.out:
f.close()