Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/monoids/free_abelian_monoid_element.py
4095 views
1
"""
2
Abelian monoid elements
3
4
AUTHORS:
5
6
- David Kohel (2005-09)
7
8
EXAMPLES:
9
10
Recall the example from abelian monoids.
11
12
::
13
14
sage: F = FreeAbelianMonoid(5,names = list("abcde"))
15
sage: (a,b,c,d,e) = F.gens()
16
sage: a*b^2*e*d
17
a*b^2*d*e
18
sage: x = b^2*e*d*a^7
19
sage: x
20
a^7*b^2*d*e
21
sage: x.list()
22
[7, 2, 0, 1, 1]
23
24
It is important to note that lists are mutable and the returned
25
list is not a copy. As a result, reassignment of an element of the
26
list changes the object.
27
28
::
29
30
sage: x.list()[0] = 0
31
sage: x
32
b^2*d*e
33
"""
34
35
#*****************************************************************************
36
# Copyright (C) 2006 William Stein <[email protected]>
37
# Copyright (C) 2005 David Kohel <[email protected]>
38
#
39
# Distributed under the terms of the GNU General Public License (GPL)
40
# http://www.gnu.org/licenses/
41
#*****************************************************************************
42
43
44
from sage.rings.integer import Integer
45
from sage.structure.element import MonoidElement
46
47
def is_FreeAbelianMonoidElement(x):
48
r"""
49
Queries whether ``x`` is an object of type ``FreeAbelianMonoidElement``.
50
51
INPUT:
52
53
- ``x`` -- an object.
54
55
OUTPUT:
56
57
- ``True`` if ``x`` is an object of type ``FreeAbelianMonoidElement``;
58
``False`` otherwise.
59
"""
60
return isinstance(x, FreeAbelianMonoidElement)
61
62
class FreeAbelianMonoidElement(MonoidElement):
63
def __init__(self, F, x):
64
"""
65
Create the element x of the FreeAbelianMonoid F.
66
67
EXAMPLES::
68
69
sage: F = FreeAbelianMonoid(5, 'abcde')
70
sage: F
71
Free abelian monoid on 5 generators (a, b, c, d, e)
72
sage: F(1)
73
1
74
sage: a, b, c, d, e = F.gens()
75
sage: a^2 * b^3 * a^2 * b^4
76
a^4*b^7
77
sage: F = FreeAbelianMonoid(5, 'abcde')
78
sage: a, b, c, d, e = F.gens()
79
sage: a in F
80
True
81
sage: a*b in F
82
True
83
"""
84
MonoidElement.__init__(self, F)
85
self.__repr = None
86
n = F.ngens()
87
if isinstance(x, (int, long, Integer)) and x == 1:
88
self.__element_vector = [ 0 for i in range(n) ]
89
elif isinstance(x, list):
90
if len(x) != n:
91
raise IndexError, \
92
"Argument length (= %s) must be %s."%(len(x), n)
93
self.__element_vector = x
94
else:
95
raise TypeError, "Argument x (= %s) is of wrong type."%x
96
97
def __repr__(self):
98
s = ""
99
A = self.parent()
100
n = A.ngens()
101
x = A.variable_names()
102
v = self.__element_vector
103
for i in range(n):
104
if v[i] == 0:
105
continue
106
elif v[i] == 1:
107
if len(s) > 0: s += "*"
108
s += "%s"%x[i]
109
else:
110
if len(s) > 0: s += "*"
111
s += "%s^%s"%(x[i],v[i])
112
if len(s) == 0: s = "1"
113
return s
114
115
def __mul__(self, y):
116
if not isinstance(y, FreeAbelianMonoidElement):
117
raise TypeError, "Argument y (= %s) is of wrong type."%y
118
M = self.parent()
119
z = M(1)
120
xelt = self.__element_vector
121
yelt = y.__element_vector
122
z.__element_vector = [ xelt[i]+yelt[i] for i in range(len(xelt)) ]
123
return z
124
125
def __pow__(self, n):
126
"""
127
Raises self to the power of n.
128
129
AUTHORS:
130
131
- Tom Boothby (2007-08): Replaced O(log n) time, O(n) space
132
algorithm with O(1) time and space"algorithm".
133
134
EXAMPLES::
135
136
sage: F = FreeAbelianMonoid(5,names = list("abcde"))
137
sage: (a,b,c,d,e) = F.gens()
138
sage: x = a*b^2*e*d; x
139
a*b^2*d*e
140
sage: x^3
141
a^3*b^6*d^3*e^3
142
sage: x^0
143
1
144
"""
145
146
if not isinstance(n, (int, long, Integer)):
147
raise TypeError, "Argument n (= %s) must be an integer."%n
148
if n < 0:
149
raise IndexError, "Argument n (= %s) must be positive."%n
150
elif n == 1:
151
return self
152
z = self.parent()(1)
153
if n == 0:
154
return z
155
else:
156
z.__element_vector = [i*n for i in self.__element_vector]
157
return z
158
159
160
def list(self):
161
"""
162
Return (a reference to) the underlying list used to represent this
163
element. If this is a monoid in an abelian monoid on `n`
164
generators, then this is a list of nonnegative integers of length
165
`n`.
166
167
EXAMPLES::
168
169
sage: F = FreeAbelianMonoid(5, 'abcde')
170
sage: (a, b, c, d, e) = F.gens()
171
sage: a.list()
172
[1, 0, 0, 0, 0]
173
"""
174
return self.__element_vector
175
176
177
178