This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
finite_rank_free_module.py
3251 lines (2623 loc) · 121 KB
/
finite_rank_free_module.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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
r"""
Free modules of finite rank
The class :class:`FiniteRankFreeModule` implements free modules of finite rank
over a commutative ring.
A *free module of finite rank* over a commutative ring `R` is a module `M` over
`R` that admits a *finite basis*, i.e. a finite family of linearly independent
generators. Since `R` is commutative, it has the invariant basis number
property, so that the rank of the free module `M` is defined uniquely, as the
cardinality of any basis of `M`.
No distinguished basis of `M` is assumed. On the contrary, many bases can be
introduced on the free module along with change-of-basis rules (as module
automorphisms). Each
module element has then various representations over the various bases.
.. NOTE::
The class :class:`FiniteRankFreeModule` does not inherit from
class :class:`~sage.modules.free_module.FreeModule_generic`
nor from class
:class:`~sage.combinat.free_module.CombinatorialFreeModule`, since
both classes deal with modules with a *distinguished basis* (see
details :ref:`below <diff-FreeModule>`). Accordingly, the class
:class:`FiniteRankFreeModule` inherits directly from the generic class
:class:`~sage.structure.parent.Parent` with the category set to
:class:`~sage.categories.modules.Modules` (and not to
:class:`~sage.categories.modules_with_basis.ModulesWithBasis`).
.. TODO::
- implement submodules
- create a FreeModules category (cf. the *TODO* statement in the
documentation of :class:`~sage.categories.modules.Modules`: *Implement
a ``FreeModules(R)`` category, when so prompted by a concrete use case*)
AUTHORS:
- Eric Gourgoulhon, Michal Bejger (2014-2015): initial version
- Travis Scrimshaw (2016): category set to ``Modules(ring).FiniteDimensional()``
(:trac:`20770`)
- Michael Jung (2019): improve treatment of the zero element
- Eric Gourgoulhon (2021): unicode symbols for tensor and exterior products
- Matthias Koeppe (2022): ``FiniteRankFreeModule_abstract``, symmetric powers
REFERENCES:
- Chap. 10 of R. Godement : *Algebra* [God1968]_
- Chap. 3 of S. Lang : *Algebra* [Lan2002]_
EXAMPLES:
Let us define a free module of rank 2 over `\ZZ`::
sage: M = FiniteRankFreeModule(ZZ, 2, name='M') ; M
Rank-2 free module M over the Integer Ring
sage: M.category()
Category of finite dimensional modules over Integer Ring
We introduce a first basis on ``M``::
sage: e = M.basis('e') ; e
Basis (e_0,e_1) on the Rank-2 free module M over the Integer Ring
The elements of the basis are of course module elements::
sage: e[0]
Element e_0 of the Rank-2 free module M over the Integer Ring
sage: e[1]
Element e_1 of the Rank-2 free module M over the Integer Ring
sage: e[0].parent()
Rank-2 free module M over the Integer Ring
We define a module element by its components w.r.t. basis ``e``::
sage: u = M([2,-3], basis=e, name='u')
sage: u.display(e)
u = 2 e_0 - 3 e_1
Module elements can be also be created by arithmetic expressions::
sage: v = -2*u + 4*e[0] ; v
Element of the Rank-2 free module M over the Integer Ring
sage: v.display(e)
6 e_1
sage: u == 2*e[0] - 3*e[1]
True
We define a second basis on ``M`` from a family of linearly independent
elements::
sage: f = M.basis('f', from_family=(e[0]-e[1], -2*e[0]+3*e[1])) ; f
Basis (f_0,f_1) on the Rank-2 free module M over the Integer Ring
sage: f[0].display(e)
f_0 = e_0 - e_1
sage: f[1].display(e)
f_1 = -2 e_0 + 3 e_1
We may of course express the elements of basis ``e`` in terms of basis ``f``::
sage: e[0].display(f)
e_0 = 3 f_0 + f_1
sage: e[1].display(f)
e_1 = 2 f_0 + f_1
as well as any module element::
sage: u.display(f)
u = -f_1
sage: v.display(f)
12 f_0 + 6 f_1
The two bases are related by a module automorphism::
sage: a = M.change_of_basis(e,f) ; a
Automorphism of the Rank-2 free module M over the Integer Ring
sage: a.parent()
General linear group of the Rank-2 free module M over the Integer Ring
sage: a.matrix(e)
[ 1 -2]
[-1 3]
Let us check that basis ``f`` is indeed the image of basis ``e`` by ``a``::
sage: f[0] == a(e[0])
True
sage: f[1] == a(e[1])
True
The reverse change of basis is of course the inverse automorphism::
sage: M.change_of_basis(f,e) == a^(-1)
True
We introduce a new module element via its components w.r.t. basis ``f``::
sage: v = M([2,4], basis=f, name='v')
sage: v.display(f)
v = 2 f_0 + 4 f_1
The sum of the two module elements ``u`` and ``v`` can be performed even if
they have been defined on different bases, thanks to the known relation
between the two bases::
sage: s = u + v ; s
Element u+v of the Rank-2 free module M over the Integer Ring
We can display the result in either basis::
sage: s.display(e)
u+v = -4 e_0 + 7 e_1
sage: s.display(f)
u+v = 2 f_0 + 3 f_1
Tensor products of elements are implemented::
sage: t = u*v ; t
Type-(2,0) tensor u⊗v on the Rank-2 free module M over the Integer Ring
sage: t.parent()
Free module of type-(2,0) tensors on the
Rank-2 free module M over the Integer Ring
sage: t.display(e)
u⊗v = -12 e_0⊗e_0 + 20 e_0⊗e_1 + 18 e_1⊗e_0 - 30 e_1⊗e_1
sage: t.display(f)
u⊗v = -2 f_1⊗f_0 - 4 f_1⊗f_1
We can access to tensor components w.r.t. to a given basis via the square
bracket operator::
sage: t[e,0,1]
20
sage: t[f,1,0]
-2
sage: u[e,0]
2
sage: u[e,:]
[2, -3]
sage: u[f,:]
[0, -1]
The parent of the automorphism ``a`` is the group `\mathrm{GL}(M)`, but
``a`` can also be considered as a tensor of type `(1,1)` on ``M``::
sage: a.parent()
General linear group of the Rank-2 free module M over the Integer Ring
sage: a.tensor_type()
(1, 1)
sage: a.display(e)
e_0⊗e^0 - 2 e_0⊗e^1 - e_1⊗e^0 + 3 e_1⊗e^1
sage: a.display(f)
f_0⊗f^0 - 2 f_0⊗f^1 - f_1⊗f^0 + 3 f_1⊗f^1
As such, we can form its tensor product with ``t``, yielding a tensor of
type `(3,1)`::
sage: t*a
Type-(3,1) tensor on the Rank-2 free module M over the Integer Ring
sage: (t*a).display(e)
-12 e_0⊗e_0⊗e_0⊗e^0 + 24 e_0⊗e_0⊗e_0⊗e^1 + 12 e_0⊗e_0⊗e_1⊗e^0
- 36 e_0⊗e_0⊗e_1⊗e^1 + 20 e_0⊗e_1⊗e_0⊗e^0 - 40 e_0⊗e_1⊗e_0⊗e^1
- 20 e_0⊗e_1⊗e_1⊗e^0 + 60 e_0⊗e_1⊗e_1⊗e^1 + 18 e_1⊗e_0⊗e_0⊗e^0
- 36 e_1⊗e_0⊗e_0⊗e^1 - 18 e_1⊗e_0⊗e_1⊗e^0 + 54 e_1⊗e_0⊗e_1⊗e^1
- 30 e_1⊗e_1⊗e_0⊗e^0 + 60 e_1⊗e_1⊗e_0⊗e^1 + 30 e_1⊗e_1⊗e_1⊗e^0
- 90 e_1⊗e_1⊗e_1⊗e^1
The parent of `t\otimes a` is itself a free module of finite rank over `\ZZ`::
sage: T = (t*a).parent() ; T
Free module of type-(3,1) tensors on the Rank-2 free module M over the
Integer Ring
sage: T.base_ring()
Integer Ring
sage: T.rank()
16
.. _diff-FreeModule:
.. RUBRIC:: Differences between ``FiniteRankFreeModule`` and ``FreeModule``
(or ``VectorSpace``)
To illustrate the differences, let us create two free modules of rank 3 over
`\ZZ`, one with ``FiniteRankFreeModule`` and the other one with
``FreeModule``::
sage: M = FiniteRankFreeModule(ZZ, 3, name='M') ; M
Rank-3 free module M over the Integer Ring
sage: N = FreeModule(ZZ, 3) ; N
Ambient free module of rank 3 over the principal ideal domain Integer Ring
The main difference is that ``FreeModule`` returns a free module with a
distinguished basis, while ``FiniteRankFreeModule`` does not::
sage: N.basis()
[
(1, 0, 0),
(0, 1, 0),
(0, 0, 1)
]
sage: M.bases()
[]
sage: M.print_bases()
No basis has been defined on the Rank-3 free module M over the Integer Ring
This is also revealed by the category of each module::
sage: M.category()
Category of finite dimensional modules over Integer Ring
sage: N.category()
Category of finite dimensional modules with basis over
(euclidean domains and infinite enumerated sets and metric spaces)
In other words, the module created by ``FreeModule`` is actually `\ZZ^3`,
while, in the absence of any distinguished basis, no *canonical* isomorphism
relates the module created by ``FiniteRankFreeModule`` to `\ZZ^3`::
sage: N is ZZ^3
True
sage: M is ZZ^3
False
sage: M == ZZ^3
False
Because it is `\ZZ^3`, ``N`` is unique, while there may be various modules
of the same rank over the same ring created by ``FiniteRankFreeModule``;
they are then distinguished by their names (actually by the complete
sequence of arguments of ``FiniteRankFreeModule``)::
sage: N1 = FreeModule(ZZ, 3) ; N1
Ambient free module of rank 3 over the principal ideal domain Integer Ring
sage: N1 is N # FreeModule(ZZ, 3) is unique
True
sage: M1 = FiniteRankFreeModule(ZZ, 3, name='M_1') ; M1
Rank-3 free module M_1 over the Integer Ring
sage: M1 is M # M1 and M are different rank-3 modules over ZZ
False
sage: M1b = FiniteRankFreeModule(ZZ, 3, name='M_1') ; M1b
Rank-3 free module M_1 over the Integer Ring
sage: M1b is M1 # because M1b and M1 have the same name
True
As illustrated above, various bases can be introduced on the module created by
``FiniteRankFreeModule``::
sage: e = M.basis('e') ; e
Basis (e_0,e_1,e_2) on the Rank-3 free module M over the Integer Ring
sage: f = M.basis('f', from_family=(-e[0], e[1]-e[2], -2*e[1]+3*e[2])) ; f
Basis (f_0,f_1,f_2) on the Rank-3 free module M over the Integer Ring
sage: M.bases()
[Basis (e_0,e_1,e_2) on the Rank-3 free module M over the Integer Ring,
Basis (f_0,f_1,f_2) on the Rank-3 free module M over the Integer Ring]
Each element of a basis is accessible via its index::
sage: e[0]
Element e_0 of the Rank-3 free module M over the Integer Ring
sage: e[0].parent()
Rank-3 free module M over the Integer Ring
sage: f[1]
Element f_1 of the Rank-3 free module M over the Integer Ring
sage: f[1].parent()
Rank-3 free module M over the Integer Ring
while on module ``N``, the element of the (unique) basis is accessible
directly from the module symbol::
sage: N.0
(1, 0, 0)
sage: N.1
(0, 1, 0)
sage: N.0.parent()
Ambient free module of rank 3 over the principal ideal domain Integer Ring
The arithmetic of elements is similar; the difference lies in the display:
a basis has to be specified for elements of ``M``, while elements of ``N`` are
displayed directly as elements of `\ZZ^3`::
sage: u = 2*e[0] - 3*e[2] ; u
Element of the Rank-3 free module M over the Integer Ring
sage: u.display(e)
2 e_0 - 3 e_2
sage: u.display(f)
-2 f_0 - 6 f_1 - 3 f_2
sage: u[e,:]
[2, 0, -3]
sage: u[f,:]
[-2, -6, -3]
sage: v = 2*N.0 - 3*N.2 ; v
(2, 0, -3)
For the case of ``M``, in order to avoid to specify the basis if the user is
always working with the same basis (e.g. only one basis has been defined),
the concept of *default basis* has been introduced::
sage: M.default_basis()
Basis (e_0,e_1,e_2) on the Rank-3 free module M over the Integer Ring
sage: M.print_bases()
Bases defined on the Rank-3 free module M over the Integer Ring:
- (e_0,e_1,e_2) (default basis)
- (f_0,f_1,f_2)
This is different from the *distinguished basis* of ``N``: it simply means that
the mention of the basis can be omitted in function arguments::
sage: u.display() # equivalent to u.display(e)
2 e_0 - 3 e_2
sage: u[:] # equivalent to u[e,:]
[2, 0, -3]
At any time, the default basis can be changed::
sage: M.set_default_basis(f)
sage: u.display()
-2 f_0 - 6 f_1 - 3 f_2
Another difference between ``FiniteRankFreeModule`` and ``FreeModule`` is that
for the former the range of indices can be specified (by default, it starts
from 0)::
sage: M = FiniteRankFreeModule(ZZ, 3, name='M', start_index=1) ; M
Rank-3 free module M over the Integer Ring
sage: e = M.basis('e') ; e # compare with (e_0,e_1,e_2) above
Basis (e_1,e_2,e_3) on the Rank-3 free module M over the Integer Ring
sage: e[1], e[2], e[3]
(Element e_1 of the Rank-3 free module M over the Integer Ring,
Element e_2 of the Rank-3 free module M over the Integer Ring,
Element e_3 of the Rank-3 free module M over the Integer Ring)
All the above holds for ``VectorSpace`` instead of ``FreeModule``: the object
created by ``VectorSpace`` is actually a Cartesian power of the base field::
sage: V = VectorSpace(QQ,3) ; V
Vector space of dimension 3 over Rational Field
sage: V.category()
Category of finite dimensional vector spaces with basis
over (number fields and quotient fields and metric spaces)
sage: V is QQ^3
True
sage: V.basis()
[
(1, 0, 0),
(0, 1, 0),
(0, 0, 1)
]
To create a vector space without any distinguished basis, one has to use
``FiniteRankFreeModule``::
sage: V = FiniteRankFreeModule(QQ, 3, name='V') ; V
3-dimensional vector space V over the Rational Field
sage: V.category()
Category of finite dimensional vector spaces over Rational Field
sage: V.bases()
[]
sage: V.print_bases()
No basis has been defined on the 3-dimensional vector space V over the
Rational Field
The class :class:`FiniteRankFreeModule` has been created for the needs
of the `SageManifolds project <http://sagemanifolds.obspm.fr/>`_, where
free modules do not have any distinguished basis. Too kinds of free modules
occur in the context of differentiable manifolds (see
`here <http://sagemanifolds.obspm.fr/tensor_modules.html>`_ for more
details):
- the tangent vector space at any point of the manifold (cf.
:class:`~sage.manifolds.differentiable.tangent_space.TangentSpace`);
- the set of vector fields on a parallelizable open subset `U` of the manifold,
which is a free module over the algebra of scalar fields on `U` (cf.
:class:`~sage.manifolds.differentiable.vectorfield_module.VectorFieldFreeModule`).
For instance, without any specific coordinate choice, no basis can be
distinguished in a tangent space.
On the other side, the modules created by ``FreeModule`` have much more
algebraic functionalities than those created by ``FiniteRankFreeModule``. In
particular, submodules have not been implemented yet in
:class:`FiniteRankFreeModule`. Moreover, modules resulting from ``FreeModule``
are tailored to the specific kind of their base ring:
- free module over a commutative ring that is not an integral domain
(`\ZZ/6\ZZ`)::
sage: R = IntegerModRing(6) ; R
Ring of integers modulo 6
sage: FreeModule(R, 3)
Ambient free module of rank 3 over Ring of integers modulo 6
sage: type(FreeModule(R, 3))
<class 'sage.modules.free_module.FreeModule_ambient_with_category'>
- free module over an integral domain that is not principal (`\ZZ[X]`)::
sage: R.<X> = ZZ[] ; R
Univariate Polynomial Ring in X over Integer Ring
sage: FreeModule(R, 3)
Ambient free module of rank 3 over the integral domain Univariate
Polynomial Ring in X over Integer Ring
sage: type(FreeModule(R, 3))
<class 'sage.modules.free_module.FreeModule_ambient_domain_with_category'>
- free module over a principal ideal domain (`\ZZ`)::
sage: R = ZZ ; R
Integer Ring
sage: FreeModule(R,3)
Ambient free module of rank 3 over the principal ideal domain Integer Ring
sage: type(FreeModule(R, 3))
<class 'sage.modules.free_module.FreeModule_ambient_pid_with_category'>
On the contrary, all objects constructed with ``FiniteRankFreeModule`` belong
to the same class::
sage: R = IntegerModRing(6)
sage: type(FiniteRankFreeModule(R, 3))
<class 'sage.tensor.modules.finite_rank_free_module.FiniteRankFreeModule_with_category'>
sage: R.<X> = ZZ[]
sage: type(FiniteRankFreeModule(R, 3))
<class 'sage.tensor.modules.finite_rank_free_module.FiniteRankFreeModule_with_category'>
sage: R = ZZ
sage: type(FiniteRankFreeModule(R, 3))
<class 'sage.tensor.modules.finite_rank_free_module.FiniteRankFreeModule_with_category'>
.. RUBRIC:: Differences between ``FiniteRankFreeModule`` and
``CombinatorialFreeModule``
An alternative to construct free modules in Sage is
:class:`~sage.combinat.free_module.CombinatorialFreeModule`.
However, as ``FreeModule``, it leads to a module with a distinguished basis::
sage: N = CombinatorialFreeModule(ZZ, [1,2,3]) ; N
Free module generated by {1, 2, 3} over Integer Ring
sage: N.category()
Category of finite dimensional modules with basis over Integer Ring
The distinguished basis is returned by the method ``basis()``::
sage: b = N.basis() ; b
Finite family {1: B[1], 2: B[2], 3: B[3]}
sage: b[1]
B[1]
sage: b[1].parent()
Free module generated by {1, 2, 3} over Integer Ring
For the free module ``M`` created above with ``FiniteRankFreeModule``, the
method ``basis`` has at least one argument: the symbol string that
specifies which basis is required::
sage: e = M.basis('e') ; e
Basis (e_1,e_2,e_3) on the Rank-3 free module M over the Integer Ring
sage: e[1]
Element e_1 of the Rank-3 free module M over the Integer Ring
sage: e[1].parent()
Rank-3 free module M over the Integer Ring
The arithmetic of elements is similar::
sage: u = 2*e[1] - 5*e[3] ; u
Element of the Rank-3 free module M over the Integer Ring
sage: v = 2*b[1] - 5*b[3] ; v
2*B[1] - 5*B[3]
One notices that elements of ``N`` are displayed directly in terms of their
expansions on the distinguished basis. For elements of ``M``, one has to use
the method
:meth:`~sage.tensor.modules.free_module_tensor.FreeModuleTensor.display`
in order to specify the basis::
sage: u.display(e)
2 e_1 - 5 e_3
The components on the basis are returned by the square bracket operator for
``M`` and by the method ``coefficient`` for ``N``::
sage: [u[e,i] for i in {1,2,3}]
[2, 0, -5]
sage: u[e,:] # a shortcut for the above
[2, 0, -5]
sage: [v.coefficient(i) for i in {1,2,3}]
[2, 0, -5]
"""
# ******************************************************************************
# Copyright (C) 2014-2021 Eric Gourgoulhon <eric.gourgoulhon@obspm.fr>
# 2014-2016 Travis Scrimshaw <tscrimsh@umn.edu>
# 2015 Michal Bejger <bejger@camk.edu.pl>
# 2016 Frédéric Chapoton
# 2020 Michael Jung
# 2020-2022 Matthias Koeppe
#
# Distributed under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# https://www.gnu.org/licenses/
# ******************************************************************************
from __future__ import annotations
from typing import Generator, Optional
from sage.categories.fields import Fields
from sage.categories.modules import Modules
from sage.categories.rings import Rings
from sage.misc.cachefunc import cached_method
from sage.rings.integer import Integer
from sage.structure.parent import Parent
from sage.structure.unique_representation import UniqueRepresentation
from sage.tensor.modules.free_module_element import FiniteRankFreeModuleElement
class FiniteRankFreeModule_abstract(UniqueRepresentation, Parent):
r"""
Abstract base class for free modules of finite rank over a commutative ring.
"""
def __init__(
self,
ring,
rank,
name=None,
latex_name=None,
category=None,
ambient=None,
):
r"""
See :class:`FiniteRankFreeModule` for documentation and examples.
TESTS::
sage: M = FiniteRankFreeModule(ZZ, 3, name='M')
sage: TestSuite(M).run()
sage: e = M.basis('e')
sage: TestSuite(M).run()
sage: f = M.basis('f')
sage: TestSuite(M).run()
"""
# This duplicates the normalization done in __classcall_private__,
# but it is needed for various subclasses.
if ring not in Rings().Commutative():
raise TypeError("the module base ring must be commutative")
category = Modules(ring).FiniteDimensional().or_subcategory(category)
Parent.__init__(self, base=ring, category=category)
self._ring = ring # same as self._base
if ambient is None:
self._ambient_module = self
else:
self._ambient_module = ambient
self._rank = rank
self._name = name
# This duplicates the normalization done in __classcall_private__,
# but it is needed for various subclasses.
if latex_name is None:
self._latex_name = self._name
else:
self._latex_name = latex_name
def _latex_(self):
r"""
LaTeX representation of ``self``.
EXAMPLES::
sage: M = FiniteRankFreeModule(ZZ, 3, name='M')
sage: M._latex_()
'M'
sage: latex(M)
M
sage: M1 = FiniteRankFreeModule(ZZ, 3, name='M', latex_name=r'\mathcal{M}')
sage: M1._latex_()
'\\mathcal{M}'
sage: latex(M1)
\mathcal{M}
"""
if self._latex_name is None:
return r'\mbox{' + str(self) + r'}'
else:
return self._latex_name
def tensor_power(self, n):
r"""
Return the ``n``-fold tensor product of ``self``.
EXAMPLES::
sage: M = FiniteRankFreeModule(QQ, 2)
sage: M.tensor_power(3)
Free module of type-(3,0) tensors on the 2-dimensional vector space over the Rational Field
sage: M.tensor_module(1,2).tensor_power(3)
Free module of type-(3,6) tensors on the 2-dimensional vector space over the Rational Field
"""
tensor_type = self.tensor_type()
return self.base_module().tensor_module(n * tensor_type[0], n * tensor_type[1])
def tensor_product(self, *others):
r"""
Return the tensor product of ``self`` and ``others``.
EXAMPLES::
sage: M = FiniteRankFreeModule(QQ, 2)
sage: M.tensor_product(M)
Free module of type-(2,0) tensors on the 2-dimensional vector space over the Rational Field
sage: M.tensor_product(M.tensor_module(1,2))
Free module of type-(2,2) tensors on the 2-dimensional vector space over the Rational Field
sage: M.tensor_module(1,2).tensor_product(M)
Free module of type-(2,2) tensors on the 2-dimensional vector space over the Rational Field
sage: M.tensor_module(1,1).tensor_product(M.tensor_module(1,2))
Free module of type-(2,3) tensors on the 2-dimensional vector space over the Rational Field
sage: Sym2M = M.tensor_module(2, 0, sym=range(2)); Sym2M
Free module of fully symmetric type-(2,0) tensors on the 2-dimensional vector space over the Rational Field
sage: Sym01x23M = Sym2M.tensor_product(Sym2M); Sym01x23M
Free module of type-(4,0) tensors on the 2-dimensional vector space over the Rational Field,
with symmetry on the index positions (0, 1), with symmetry on the index positions (2, 3)
sage: Sym01x23M._index_maps
((0, 1), (2, 3))
sage: N = M.tensor_module(3, 3, sym=[1, 2], antisym=[3, 4]); N
Free module of type-(3,3) tensors on the 2-dimensional vector space over the Rational Field,
with symmetry on the index positions (1, 2),
with antisymmetry on the index positions (3, 4)
sage: NxN = N.tensor_product(N); NxN
Free module of type-(6,6) tensors on the 2-dimensional vector space over the Rational Field,
with symmetry on the index positions (1, 2), with symmetry on the index positions (4, 5),
with antisymmetry on the index positions (6, 7), with antisymmetry on the index positions (9, 10)
sage: NxN._index_maps
((0, 1, 2, 6, 7, 8), (3, 4, 5, 9, 10, 11))
"""
from sage.modules.free_module_element import vector
from .comp import CompFullySym, CompFullyAntiSym, CompWithSym
base_module = self.base_module()
if not all(module.base_module() == base_module for module in others):
raise NotImplementedError('all factors must be tensor modules over the same base module')
factors = [self] + list(others)
result_tensor_type = sum(vector(factor.tensor_type()) for factor in factors)
result_sym = []
result_antisym = []
# Keep track of reordering of the contravariant and covariant indices
# (compatible with FreeModuleTensor.__mul__)
index_maps = []
running_indices = vector([0, result_tensor_type[0]])
for factor in factors:
tensor_type = factor.tensor_type()
index_map = tuple(i + running_indices[0] for i in range(tensor_type[0]))
index_map += tuple(i + running_indices[1] for i in range(tensor_type[1]))
index_maps.append(index_map)
if tensor_type[0] + tensor_type[1] > 1:
basis_sym = factor._basis_sym()
all_indices = tuple(range(tensor_type[0] + tensor_type[1]))
if isinstance(basis_sym, CompFullySym):
sym = [all_indices]
antisym = []
elif isinstance(basis_sym, CompFullyAntiSym):
sym = []
antisym = [all_indices]
elif isinstance(basis_sym, CompWithSym):
sym = basis_sym._sym
antisym = basis_sym._antisym
else:
sym = antisym = []
def map_isym(isym):
return tuple(index_map[i] for i in isym)
result_sym.extend(tuple(index_map[i] for i in isym) for isym in sym)
result_antisym.extend(tuple(index_map[i] for i in isym) for isym in antisym)
running_indices += vector(tensor_type)
result = base_module.tensor_module(*result_tensor_type,
sym=result_sym, antisym=result_antisym)
result._index_maps = tuple(index_maps)
return result
def rank(self) -> int:
r"""
Return the rank of the free module ``self``.
Since the ring over which ``self`` is built is assumed to be
commutative (and hence has the invariant basis number property), the
rank is defined uniquely, as the cardinality of any basis of ``self``.
EXAMPLES:
Rank of free modules over `\ZZ`::
sage: M = FiniteRankFreeModule(ZZ, 3)
sage: M.rank()
3
sage: M.tensor_module(0,1).rank()
3
sage: M.tensor_module(0,2).rank()
9
sage: M.tensor_module(1,0).rank()
3
sage: M.tensor_module(1,1).rank()
9
sage: M.tensor_module(1,2).rank()
27
sage: M.tensor_module(2,2).rank()
81
"""
return self._rank
@cached_method
def zero(self):
r"""
Return the zero element of ``self``.
EXAMPLES:
Zero elements of free modules over `\ZZ`::
sage: M = FiniteRankFreeModule(ZZ, 3, name='M')
sage: M.zero()
Element zero of the Rank-3 free module M over the Integer Ring
sage: M.zero().parent() is M
True
sage: M.zero() is M(0)
True
sage: T = M.tensor_module(1,1)
sage: T.zero()
Type-(1,1) tensor zero on the Rank-3 free module M over the Integer Ring
sage: T.zero().parent() is T
True
sage: T.zero() is T(0)
True
Components of the zero element with respect to some basis::
sage: e = M.basis('e')
sage: M.zero()[e,:]
[0, 0, 0]
sage: all(M.zero()[e,i] == M.base_ring().zero() for i in M.irange())
True
sage: T.zero()[e,:]
[0 0 0]
[0 0 0]
[0 0 0]
sage: M.tensor_module(1,2).zero()[e,:]
[[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]]
"""
resu = self._element_constructor_(name='zero', latex_name='0')
for basis in self._known_bases:
resu._add_comp_unsafe(basis)
# (since new components are initialized to zero)
resu._is_zero = True # This element is certainly zero
resu.set_immutable()
return resu
def ambient_module(self): # compatible with sage.modules.free_module.FreeModule_generic
"""
Return the ambient module associated to this module.
EXAMPLES::
sage: M = FiniteRankFreeModule(ZZ, 3, name='M')
sage: M.ambient_module() is M
True
sage: M = FiniteRankFreeModule(ZZ, 3, name='M')
sage: Sym0123x45M = M.tensor_module(6, 0, sym=((0, 1, 2, 3), (4, 5)))
sage: T60M = M.tensor_module(6, 0)
sage: Sym0123x45M.ambient_module() is T60M
True
"""
return self._ambient_module
ambient = ambient_module # compatible with sage.modules.with_basis.subquotient.SubmoduleWithBasis
def is_submodule(self, other):
"""
Return ``True`` if ``self`` is a submodule of ``other``.
EXAMPLES::
sage: M = FiniteRankFreeModule(ZZ, 3, name='M')
sage: N = FiniteRankFreeModule(ZZ, 4, name='N')
sage: M.is_submodule(M)
True
sage: M.is_submodule(N)
False
"""
return self == other or self.ambient_module() == other
class FiniteRankFreeModule(FiniteRankFreeModule_abstract):
r"""
Free module of finite rank over a commutative ring.
A *free module of finite rank* over a commutative ring `R` is a module `M`
over `R` that admits a *finite basis*, i.e. a finite family of linearly
independent generators. Since `R` is commutative, it has the invariant
basis number property, so that the rank of the free module `M` is defined
uniquely, as the cardinality of any basis of `M`.
No distinguished basis of `M` is assumed. On the contrary, many bases can be
introduced on the free module along with change-of-basis rules (as module
automorphisms). Each
module element has then various representations over the various bases.
.. NOTE::
The class :class:`FiniteRankFreeModule` does not inherit from
class :class:`~sage.modules.free_module.FreeModule_generic`
nor from class
:class:`~sage.combinat.free_module.CombinatorialFreeModule`, since
both classes deal with modules with a *distinguished basis* (see
details :ref:`above <diff-FreeModule>`).
Moreover, following the recommendation exposed in :trac:`16427`
the class :class:`FiniteRankFreeModule` inherits directly from
:class:`~sage.structure.parent.Parent` (with the category set to
:class:`~sage.categories.modules.Modules`) and not from the Cython
class :class:`~sage.modules.module.Module`.
The class :class:`FiniteRankFreeModule` is a Sage *parent* class,
the corresponding *element* class being
:class:`~sage.tensor.modules.free_module_element.FiniteRankFreeModuleElement`.
INPUT:
- ``ring`` -- commutative ring `R` over which the free module is
constructed
- ``rank`` -- positive integer; rank of the free module
- ``name`` -- (default: ``None``) string; name given to the free module
- ``latex_name`` -- (default: ``None``) string; LaTeX symbol to denote
the freemodule; if none is provided, it is set to ``name``
- ``start_index`` -- (default: 0) integer; lower bound of the range of
indices in bases defined on the free module
- ``output_formatter`` -- (default: ``None``) function or unbound
method called to format the output of the tensor components;
``output_formatter`` must take 1 or 2 arguments: the first argument
must be an element of the ring `R` and the second one, if any, some
format specification
EXAMPLES:
Free module of rank 3 over `\ZZ`::
sage: FiniteRankFreeModule._clear_cache_() # for doctests only
sage: M = FiniteRankFreeModule(ZZ, 3) ; M
Rank-3 free module over the Integer Ring
sage: M = FiniteRankFreeModule(ZZ, 3, name='M') ; M # declaration with a name
Rank-3 free module M over the Integer Ring
sage: M.category()
Category of finite dimensional modules over Integer Ring
sage: M.base_ring()
Integer Ring
sage: M.rank()
3
If the base ring is a field, the free module is in the category of vector
spaces::
sage: V = FiniteRankFreeModule(QQ, 3, name='V') ; V
3-dimensional vector space V over the Rational Field
sage: V.category()
Category of finite dimensional vector spaces over Rational Field
The LaTeX output is adjusted via the parameter ``latex_name``::
sage: latex(M) # the default is the symbol provided in the string ``name``
M
sage: M = FiniteRankFreeModule(ZZ, 3, name='M', latex_name=r'\mathcal{M}')
sage: latex(M)
\mathcal{M}
The free module M has no distinguished basis::
sage: M in ModulesWithBasis(ZZ)
False
sage: M in Modules(ZZ)
True
In particular, no basis is initialized at the module construction::
sage: M.print_bases()
No basis has been defined on the Rank-3 free module M over the Integer Ring
sage: M.bases()
[]
Bases have to be introduced by means of the method :meth:`basis`,
the first defined basis being considered as the *default basis*, meaning
it can be skipped in function arguments required a basis (this can
be changed by means of the method :meth:`set_default_basis`)::
sage: e = M.basis('e') ; e
Basis (e_0,e_1,e_2) on the Rank-3 free module M over the Integer Ring
sage: M.default_basis()
Basis (e_0,e_1,e_2) on the Rank-3 free module M over the Integer Ring
A second basis can be created from a family of linearly independent
elements expressed in terms of basis ``e``::
sage: f = M.basis('f', from_family=(-e[0], e[1]+e[2], 2*e[1]+3*e[2]))
sage: f
Basis (f_0,f_1,f_2) on the Rank-3 free module M over the Integer Ring
sage: M.print_bases()
Bases defined on the Rank-3 free module M over the Integer Ring:
- (e_0,e_1,e_2) (default basis)
- (f_0,f_1,f_2)
sage: M.bases()
[Basis (e_0,e_1,e_2) on the Rank-3 free module M over the Integer Ring,
Basis (f_0,f_1,f_2) on the Rank-3 free module M over the Integer Ring]
M is a *parent* object, whose elements are instances of
:class:`~sage.tensor.modules.free_module_element.FiniteRankFreeModuleElement`
(actually a dynamically generated subclass of it)::
sage: v = M.an_element() ; v
Element of the Rank-3 free module M over the Integer Ring
sage: from sage.tensor.modules.free_module_element import FiniteRankFreeModuleElement
sage: isinstance(v, FiniteRankFreeModuleElement)
True
sage: v in M
True
sage: M.is_parent_of(v)
True
sage: v.display() # expansion w.r.t. the default basis (e)
e_0 + e_1 + e_2
sage: v.display(f)
-f_0 + f_1
The test suite of the category of modules is passed::
sage: TestSuite(M).run()
Constructing an element of ``M`` from (the integer) 0 yields
the zero element of ``M``::
sage: M(0)
Element zero of the Rank-3 free module M over the Integer Ring
sage: M(0) is M.zero()
True
Non-zero elements are constructed by providing their components in
a given basis::
sage: v = M([-1,0,3]) ; v # components in the default basis (e)
Element of the Rank-3 free module M over the Integer Ring
sage: v.display() # expansion w.r.t. the default basis (e)
-e_0 + 3 e_2
sage: v.display(f)
f_0 - 6 f_1 + 3 f_2
sage: v = M([-1,0,3], basis=f) ; v # components in a specific basis
Element of the Rank-3 free module M over the Integer Ring
sage: v.display(f)
-f_0 + 3 f_2
sage: v.display()
e_0 + 6 e_1 + 9 e_2
sage: v = M([-1,0,3], basis=f, name='v') ; v
Element v of the Rank-3 free module M over the Integer Ring
sage: v.display(f)