-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgebra.py
118 lines (99 loc) · 4.48 KB
/
algebra.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
import polygnomeObject
import relation
import reductionFunction
import monomial
class algebra(polygnomeObject.polygnomeObject):
"""
File: algebra.py
Author: Chris Campbell
Email: c (dot) j (dot) campbell (at) ed (dot) ac (dot) uk
Github: https://github.com/campbellC
Description: An algebra stores a list of relations and can reduce words in
the free algebra by these relations. The relations have a choice of highest monomial already.
"""
def __init__(self,relations=()):
if isinstance(relations,relation.relation):
relations = (relations,)
relations = tuple(relations)
# If you only want one relation the following saves you having to
# define it seperately before making the algebra.
if len(relations) > 0 and isinstance(relations[0],tuple):
assert len(relations[0]) == 2
relations = map(lambda x: relation.relation( *x),relations)
for i in relations:
assert isinstance(i,relation.relation)
self.relations = relations
def zero(self):
return monomial.monomial(0)
def __repr__(self):
if len(self.relations) == 0:
return 'The free algebra'
return "Algebra subject to relations " + repr(self.relations)
def toLatex(self):
if len(self.relations) == 0:
return 'The free algebra'
return "Algebra subject to relations $" + "$,$".join([i.toLatex() for i in self.relations]) + "$"
def __iter__(self):
"""Iterating through an algebra simply returns the relations of that algebra"""
for i in self.relations:
yield i
def doesAct(self,poly):
"""Test if the polynomial has any monomial on which there is a relation that acts"""
for mono in poly:
monoDegree = mono.degree()
if monoDegree <= 1:
continue
for rel in self.relations:
# For each relation we check if it applies to the monomial.
# Firstly we check if the monomial has too low a degree
relDegree = rel.degree()
if monoDegree < relDegree:
continue
# Secondly we iterate through the submonomials of length
# relDegree and see if any of them are the leading monomial of
# rel.
for index in xrange(monoDegree - relDegree + 1):
if rel.doesAct(mono[index: index + relDegree]):
return True
return False
def makeReductionFunction(self, poly):
"""Only run this if you have already checked doesAct"""
for mono in poly:
monoDegree = mono.degree()
if monoDegree <= 1:
continue
for rel in self.relations:
# For each relation we check if it applies to the monomial.
# Firstly we check if the monomial has too low a degree
relDegree = rel.degree()
if monoDegree < relDegree:
continue
# Secondly we iterate through the submonomials of length
# relDegree and see if any of them are the leading monomial of
# rel.
for index in xrange(monoDegree - relDegree + 1):
if rel.doesAct(mono[index: index + relDegree]):
return (reductionFunction.reductionFunction(mono[0:index],
rel,
mono[index + relDegree:])
, mono.coefficient)
def makeReductionSequence(self,poly):
sequence = []
while self.doesAct(poly):
reduction, weight = self.makeReductionFunction(poly)
sequence.append((reduction,weight))
poly = reduction(poly)
return sequence
def reductionSequenceGenerator(self,poly):
while self.doesAct(poly):
reduction, weight = self.makeReductionFunction(poly)
yield (reduction, weight)
poly = reduction(poly)
def reduce(self,poly): # TODO: check running time on this, this is a slow way of doing iterable
for reduction, weight in self.makeReductionSequence(poly):
poly = reduction(poly)
return poly
def equivalent(self,polynomial1,polynomial2):
return self.reduce(polynomial1) == self.reduce(polynomial2)
if __name__ == '__main__':
pass