summaryrefslogtreecommitdiffstats
path: root/src/sage/modules/fg_pid/fgp_element.py
blob: 4dd63a9ed2bee094d93e0031affa65334e0ecf68 (plain)
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
r"""
Elements of finitely generated modules over a PID

AUTHOR:
    - William Stein, 2009
"""

#*****************************************************************************
#       Copyright (C) 2009 William Stein <wstein@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#                  http://www.gnu.org/licenses/
#*****************************************************************************

from sage.structure.element import ModuleElement
from sage.structure.richcmp import richcmp

# This adds extra maybe-not-necessary checks in the code, but could
# slow things down.  It can impact what happens in more than just this
# file.
DEBUG=True


class FGP_Element(ModuleElement):
    """
    An element of a finitely generated module over a PID.

    INPUT:

    - ``parent`` -- parent module M

    - ``x`` -- element of M.V()

    EXAMPLES::

        sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2])
        sage: Q = V/W
        sage: x = Q(V.0-V.1); x #indirect doctest
        (0, 3)
        sage: isinstance(x, sage.modules.fg_pid.fgp_element.FGP_Element)
        True
        sage: type(x)
        <class 'sage.modules.fg_pid.fgp_module.FGP_Module_class_with_category.element_class'>
        sage: x is Q(x)
        True
        sage: x.parent() is Q
        True

    TESTS::

        sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]); Q = V/W
        sage: loads(dumps(Q.0)) == Q.0
        True
    """
    def __init__(self, parent, x, check=DEBUG):
        """
        INPUT:

        - ``parent`` -- parent module M

        - ``x`` -- element of M.V()

        - ``check`` -- (default: True) if True, verify that x in M.V()

        EXAMPLES::

            sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2])
            sage: Q = V/W
            sage: x = Q(V.0-V.1); type(x)
            <class 'sage.modules.fg_pid.fgp_module.FGP_Module_class_with_category.element_class'>
            sage: isinstance(x,sage.modules.fg_pid.fgp_element.FGP_Element)
            True

        For full documentation, see :class:`FGP_Element`.
        """
        if check: assert x in parent.V(), 'The argument x='+str(x)+' is not in the covering module!'
        ModuleElement.__init__(self, parent)
        self._x = x

    def lift(self):
        """
        Lift self to an element of V, where the parent of self is the quotient module V/W.

        EXAMPLES::

            sage: V = span([[1/2,0,0],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2])
            sage: Q = V/W; Q
            Finitely generated module V/W over Integer Ring with invariants (4, 12)
            sage: Q.0
            (1, 0)
            sage: Q.1
            (0, 1)
            sage: Q.0.lift()
            (0, 0, 1)
            sage: Q.1.lift()
            (0, 2, 0)
            sage: x = Q(V.0); x
            (0, 4)
            sage: x.lift()
            (1/2, 0, 0)
            sage: x == 4*Q.1
            True
            sage: x.lift().parent() == V
            True

        A silly version of the integers modulo 100::

            sage: A = (ZZ^1)/span([[100]], ZZ); A
            Finitely generated module V/W over Integer Ring with invariants (100)
            sage: x = A([5]); x
            doctest:...: DeprecationWarning: The default behaviour changed!
             If you *really* want a linear combination of smith generators,
             use .linear_combination_of_smith_form_gens.
            See http://trac.sagemath.org/16261 for details.
            (5)
            sage: v = x.lift(); v
            (5)
            sage: v.parent()
            Ambient free module of rank 1 over the principal ideal domain Integer Ring
        """
        return self._x


    def __neg__(self):
        """
        EXAMPLES::

            sage: V1 = ZZ^2; W1 = V1.span([[1,2],[3,4]]); A1 = V1/W1; A1
            Finitely generated module V/W over Integer Ring with invariants (2)
            sage: -A1.0
            (1)
            sage: -A1.0 == A1.0           # order 2
            True
        """
        P = self.parent()
        return P.element_class(P, -self._x)


    def _add_(self, other):
        """
        EXAMPLES::

            sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2])
            sage: Q = V/W; Q
            Finitely generated module V/W over Integer Ring with invariants (4, 12)
            sage: x = Q.0; x
            (1, 0)
            sage: y = Q.1; y
            (0, 1)
            sage: x + y                        # indirect doctest
            (1, 1)
            sage: x + x + x + x
            (0, 0)
            sage: x + 0
            (1, 0)
            sage: 0 + x
            (1, 0)

        We test canonical coercion from V and W.

            sage: Q.0 + V.0
            (1, 4)
            sage: V.0 + Q.0
            (1, 4)
            sage: W.0 + Q.0
            (1, 0)
            sage: W.0 + Q.0 == Q.0
            True
        """
        P = self.parent()
        return P.element_class(P, self._x + other._x)


    def _sub_(self, other):
        """
        EXAMPLES::

            sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2])
            sage: Q = V/W; Q
            Finitely generated module V/W over Integer Ring with invariants (4, 12)
            sage: x = Q.0; x
            (1, 0)
            sage: y = Q.1; y
            (0, 1)
            sage: x - y                                # indirect doctest
            (1, 11)
            sage: x - x
            (0, 0)
        """
        P = self.parent()
        return P.element_class(P, self._x - other._x)


    def _rmul_(self, c):
        """
        Multiplication by a scalar from the left (``self`` is on the right).

        INPUT:

        - ``c`` -- an element of ``self.parent().base_ring()``.

        OUTPUT:

        The product ``c * self`` as a new instance of a module
        element.

        EXAMPLES::

            sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2])
            sage: Q = V/W; Q
            Finitely generated module V/W over Integer Ring with invariants (4, 12)
            sage: x = Q.0; x
            (1, 0)
            sage: 2 * x                            # indirect doctest
            (2, 0)
            sage: x._rmul_(4)
            (0, 0)
            sage: V = V.base_extend(QQ); W = V.span([2*V.0+4*V.1])
            sage: Q = V/W; Q
            Vector space quotient V/W of dimension 2 over Rational Field where
            V: Vector space of degree 3 and dimension 3 over Rational Field
            Basis matrix:
            [1 0 0]
            [0 1 0]
            [0 0 1]
            W: Vector space of degree 3 and dimension 1 over Rational Field
            Basis matrix:
            [1 2 0]
            sage: x = Q.0; x
            (1, 0)
            sage: (1/2) * x                            # indirect doctest
            (1/2, 0)
            sage: x._rmul_(1/4)
            (1/4, 0)
        """
        # print "_rmul_"
        P = self.parent()
        return P.element_class(P, self._x._rmul_(c))

    def _lmul_(self, s):
        """
        Multiplication by a scalar from the right (``self`` is on the left).

        INPUT:

        - ``c`` -- an element of ``self.parent().base_ring()``.

        OUTPUT:

        The product ``self * c`` as a new instance of a module
        element.

        EXAMPLES::

            sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2])
            sage: Q = V/W; Q
            Finitely generated module V/W over Integer Ring with invariants (4, 12)
            sage: x = Q.0; x
            (1, 0)
            sage: x * 2                          # indirect doctest
            (2, 0)
            sage: x._lmul_(4)
            (0, 0)
            sage: V = V.base_extend(QQ); W = V.span([2*V.0+4*V.1])
            sage: Q = V/W; Q
            Vector space quotient V/W of dimension 2 over Rational Field where
            V: Vector space of degree 3 and dimension 3 over Rational Field
            Basis matrix:
            [1 0 0]
            [0 1 0]
            [0 0 1]
            W: Vector space of degree 3 and dimension 1 over Rational Field
            Basis matrix:
            [1 2 0]
            sage: x = Q.0; x
            (1, 0)
            sage: x * (1/2)                            # indirect doctest
            (1/2, 0)
            sage: x._lmul_(1/4)
            (1/4, 0)
        """
        # print '_lmul_'
        P = self.parent()
        return P.element_class(P, self._x._lmul_(s))


    def _repr_(self):
        """

        EXAMPLES::

            sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2])
            sage: Q = V/W
            sage: Q(V.1)._repr_()
            '(0, 1)'
        """
        return repr(self.vector())


    def __getitem__(self, *args):
        """
        EXAMPLES::

            sage: V = span([[1/2,0,0],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2])
            sage: Q = V/W; Q
            Finitely generated module V/W over Integer Ring with invariants (4, 12)
            sage: x = Q.0 + 3*Q.1; x
            (1, 3)
            sage: x[0]
            1
            sage: x[1]
            3
            sage: x[-1]
            3
        """
        return self.vector().__getitem__(*args)

    def vector(self):
        """
        EXAMPLES::

            sage: V = span([[1/2,0,0],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2])
            sage: Q = V/W; Q
            Finitely generated module V/W over Integer Ring with invariants (4, 12)
            sage: x = Q.0 + 3*Q.1; x
            (1, 3)
            sage: x.vector()
            (1, 3)
            sage: tuple(x)
            (1, 3)
            sage: list(x)
            [1, 3]
            sage: x.vector().parent()
            Ambient free module of rank 2 over the principal ideal domain Integer Ring
        """
        try: return self.__vector
        except AttributeError:
            self.__vector = self.parent().coordinate_vector(self, reduce=True)
            self.__vector.set_immutable()
            return self.__vector

    def __hash__(self):
        r"""
        TESTS::

            sage: V = span([[1/2,0,0],[3/2,2,1],[0,0,1]],ZZ)
            sage: W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2])
            sage: Q = V/W
            sage: x = Q.0 + 3*Q.1
            sage: hash(x)
            3713081631933328131 # 64-bit
            1298787075          # 32-bit

            sage: A = AdditiveAbelianGroup([3])
            sage: hash(A.an_element())
            3430019387558 # 64-bit
            -1659481946   # 32-bit
        """
        return hash(self.vector())

    def _vector_(self, base_ring=None):
        """
        Support for conversion to vectors.

        INPUT:

        - ``base_ring`` -- the desired base ring of the vector.

        OUTPUT:
        
        A vector over the base ring.

        EXAMPLES::

            sage: V = span([[1/2,0,0],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2])
            sage: Q = V/W; Q
            Finitely generated module V/W over Integer Ring with invariants (4, 12)
            sage: x = Q.0 + 3*Q.1
            sage: vector(x)
            (1, 3)
            sage: vector(CDF, x)
            (1.0, 3.0)

        TESTS::

            sage: V = span([[1/2,0,0],[3/2,2,1],[0,0,1]],ZZ)
            sage: W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2])
            sage: Q = V/W
            sage: x = Q.0 + 3*Q.1
            sage: vector(x).is_mutable()
            True
            sage: vector(CDF,x).is_mutable()
            True
        """
        v = self.vector()
        if base_ring is None or v.base_ring() is base_ring:
            return v.__copy__()
        else:
            return v.change_ring(base_ring)

    def _richcmp_(self, right, op):
        """
        Compare self and right.

        EXAMPLES::

            sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2])
            sage: Q = V/W; Q
            Finitely generated module V/W over Integer Ring with invariants (4, 12)
            sage: x = Q.0; x
            (1, 0)
            sage: y = Q.1; y
            (0, 1)
            sage: x == y
            False
            sage: x == x
            True
            sage: x + x == 2*x
            True
        """
        return