Attachment 'parent_examples_travis.sage'
Download 1 from sage.structure.category_object import normalize_names
2
3 class MyFreeAlgebraElement(CombinatorialFreeModule.Element):
4 def _repr_(self):
5 return ' + '.join(repr(c) + '*' + repr(m) for m,c in self)
6
7 class MyFreeAlgebra(CombinatorialFreeModule):
8 def __init__(self, base_ring, names):
9 names = normalize_names(-1, names)
10 indices = FreeMonoid(len(names), names)
11 cat = Algebras(base_ring).WithBasis()
12 CombinatorialFreeModule.__init__(self, base_ring, indices, category=cat)
13
14 def _repr_(self):
15 return "Free algebra in variables {}".format(self.indices().variable_names())
16
17 def product_on_basis(self, x, y):
18 return self.monomial(x*y)
19
20 def one_basis(self):
21 return self.indices().one()
22
23 def _coerce_map_from_(self, R):
24 if isinstance(R, MyFreeAlgebra) and self.base_ring().has_coerce_map_from(R.base_ring()):
25 if set(R.indices().variable_names()).issubset(self.indices().variable_names()):
26 return R.module_morphism(self.monomial, codomain=self)
27 return super(MyFreeAlgebra, self)._coerce_map_from_(R)
28
29 #def one(self):
30 # return self.monomial(self.indices().one())
31
32 def algebra_generators(self):
33 I = self.indices()
34 return Family({repr(g): self.monomial(g) for g in I.gens()})
35
36 def gens(self):
37 I = self.indices()
38 G = self.algebra_generators()
39 return tuple([G[name] for name in I.variable_names()])
40
41 Element = MyFreeAlgebraElement
42
43 ###############################################################################
44
45 from sage.structure.element import Element
46 from sage.structure.parent import Parent
47 from sage.structure.unique_representation import UniqueRepresentation
48 from sage.structure.richcmp import richcmp
49
50 class MyCustomFreeAlgebra(Parent, UniqueRepresentation):
51 def __init__(self, base_ring, names):
52 names = normalize_names(-1, names)
53 cat = Algebras(base_ring).WithBasis()
54 self._indices = FreeMonoid(len(names), names)
55 Parent.__init__(self, base_ring, category=cat)
56
57 def _repr_(self):
58 return "Free algebra in variables {}".format(self._indices.variable_names())
59
60 def _an_element_(self):
61 return self.monomial(self._indices.an_element())
62
63 def product_on_basis(self, x, y):
64 return self.monomial(x*y)
65
66 def one_basis(self):
67 return self._indices.one()
68
69 def one(self):
70 return self.monomial(self.one_basis())
71
72 def algebra_generators(self):
73 I = self._indices
74 return Family({repr(g): self.monomial(g) for g in I.gens()})
75
76 def gens(self):
77 I = self._indices
78 G = self.algebra_generators()
79 return tuple([G[name] for name in I.variable_names()])
80
81 def _element_constructor_(self, x):
82 if x in self.base_ring():
83 return self.element_class(self, {self.one_basis(): x})
84 return super(MyCustomFreeAlgebra, self)._element_constructor_(x)
85
86 def monomial(self, x):
87 one = self.base_ring().one()
88 return self.element_class(self, {self._indices(x): one})
89
90 class Element(Element):
91 def __init__(self, parent, data):
92 self._data = data
93 Element.__init__(self, parent)
94
95 def _repr_(self):
96 return ' + '.join(repr(c) + '*' + repr(m) for m,c in self)
97
98 def __iter__(self):
99 return iter(self._data.items())
100
101 def _richcmp_(self, other, op):
102 return richcmp(self._data, other._data, op)
103
104 def monomial_coefficients(self, copy=True):
105 if copy:
106 return dict(self._data)
107 else:
108 return self._data
109
110 def _add_(self, other):
111 d = dict(self._data)
112 for m,c in other:
113 if m in d:
114 d[m] += c
115 else:
116 d[m] = c
117 if d[m] == 0:
118 del d[m]
119 return type(self)(self.parent(), d)
120
121 def _sub_(self, other):
122 d = dict(self._data)
123 for m,c in other:
124 if m in d:
125 d[m] -= c
126 else:
127 d[m] = c
128 if d[m] == 0:
129 del d[m]
130 return type(self)(self.parent(), d)
131
132 def _mul_(self, other):
133 d = {}
134 for ml,cl in self:
135 for mr,cr in other:
136 m = ml * mr
137 c = cl * cr
138 if m in d:
139 d[m] += c
140 else:
141 d[m] = c
142 if d[m] == 0:
143 del d[m]
144 return type(self)(self.parent(), d)
145
146 def _acted_upon_(self, x, self_on_left=True):
147 if x not in self.base_ring():
148 return None
149 d = dict(self._data)
150 for k in d:
151 d[k] *= x
152 return type(self)(self.parent(), d)
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.