|
27 | 27 | # http://www.gnu.org/licenses/ |
28 | 28 | #****************************************************************************** |
29 | 29 |
|
30 | | -from sage.structure.sage_object import SageObject |
31 | 30 | from sage.structure.unique_representation import UniqueRepresentation |
| 31 | +from sage.structure.sage_object import SageObject |
| 32 | +from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets |
| 33 | + |
| 34 | +class Basis_abstract(UniqueRepresentation, SageObject): |
| 35 | + """ |
| 36 | + Abstract base class for (dual) bases of free modules. |
| 37 | + """ |
| 38 | + def __init__(self, fmodule, symbol, latex_symbol, latex_name): |
| 39 | + """ |
| 40 | + Initialize ``self``. |
| 41 | +
|
| 42 | + EXAMPLES:: |
| 43 | +
|
| 44 | + sage: M = FiniteRankFreeModule(ZZ, 3, name='M') |
| 45 | + sage: e = M.basis('e') |
| 46 | + sage: e._fmodule is M |
| 47 | + True |
| 48 | + """ |
| 49 | + self._symbol = symbol |
| 50 | + self._latex_symbol = latex_symbol |
| 51 | + self._latex_name = latex_name |
| 52 | + self._fmodule = fmodule |
| 53 | + |
| 54 | + def __iter__(self): |
| 55 | + r""" |
| 56 | + Return the list of basis elements of ``self``. |
| 57 | +
|
| 58 | + EXAMPLES:: |
| 59 | +
|
| 60 | + sage: M = FiniteRankFreeModule(ZZ, 3, name='M') |
| 61 | + sage: e = M.basis('e') |
| 62 | + sage: list(e) |
| 63 | + [Element e_0 of the Rank-3 free module M over the Integer Ring, |
| 64 | + Element e_1 of the Rank-3 free module M over the Integer Ring, |
| 65 | + Element e_2 of the Rank-3 free module M over the Integer Ring] |
| 66 | + sage: ed = e.dual_basis() |
| 67 | + sage: list(ed) |
| 68 | + [Linear form e^0 on the Rank-3 free module M over the Integer Ring, |
| 69 | + Linear form e^1 on the Rank-3 free module M over the Integer Ring, |
| 70 | + Linear form e^2 on the Rank-3 free module M over the Integer Ring] |
| 71 | + """ |
| 72 | + for i in range(self._fmodule._rank): |
| 73 | + yield self[i] |
| 74 | + |
| 75 | + def __len__(self): |
| 76 | + r""" |
| 77 | + Return the basis length, i.e. the rank of the free module. |
| 78 | +
|
| 79 | + NB: the method ``__len__()`` is required for the basis to act as a |
| 80 | + "frame" in the class :class:`~sage.tensor.modules.comp.Components`. |
| 81 | +
|
| 82 | + EXAMPLES:: |
| 83 | +
|
| 84 | + sage: M = FiniteRankFreeModule(ZZ, 3, name='M') |
| 85 | + sage: e = M.basis('e') |
| 86 | + sage: e.__len__() |
| 87 | + 3 |
| 88 | + sage: len(e) |
| 89 | + 3 |
| 90 | + """ |
| 91 | + return self._fmodule._rank |
| 92 | + |
| 93 | + def _latex_(self): |
| 94 | + r""" |
| 95 | + Return a LaTeX representation of ``self``. |
| 96 | +
|
| 97 | + EXAMPLES:: |
| 98 | +
|
| 99 | + sage: FiniteRankFreeModule._clear_cache_() # for doctests only |
| 100 | + sage: M = FiniteRankFreeModule(ZZ, 3, name='M') |
| 101 | + sage: e = M.basis('e') |
| 102 | + sage: e._latex_() |
| 103 | + '\\left(e_0,e_1,e_2\\right)' |
| 104 | + sage: latex(e) |
| 105 | + \left(e_0,e_1,e_2\right) |
| 106 | + sage: f = M.basis('eps', latex_symbol=r'\epsilon') |
| 107 | + sage: f._latex_() |
| 108 | + '\\left(\\epsilon_0,\\epsilon_1,\\epsilon_2\\right)' |
| 109 | + sage: latex(f) |
| 110 | + \left(\epsilon_0,\epsilon_1,\epsilon_2\right) |
| 111 | +
|
| 112 | + :: |
| 113 | +
|
| 114 | + sage: M = FiniteRankFreeModule(ZZ, 3, name='M') |
| 115 | + sage: e = M.basis('e') |
| 116 | + sage: f = e.dual_basis() |
| 117 | + sage: f._latex_() |
| 118 | + '\\left(e^0,e^1,e^2\\right)' |
| 119 | +
|
| 120 | + """ |
| 121 | + return self._latex_name |
| 122 | + |
| 123 | + def free_module(self): |
| 124 | + """ |
| 125 | + Return the free module of ``self``. |
| 126 | +
|
| 127 | + EXAMPLES:: |
| 128 | +
|
| 129 | + sage: M = FiniteRankFreeModule(QQ, 2, name='M', start_index=1) |
| 130 | + sage: e = M.basis('e') |
| 131 | + sage: e.free_module() is M |
| 132 | + True |
| 133 | + """ |
| 134 | + return self._fmodule |
32 | 135 |
|
33 | | -class FreeModuleBasis(UniqueRepresentation, SageObject): |
| 136 | + |
| 137 | +class FreeModuleBasis(Basis_abstract): |
34 | 138 | r""" |
35 | 139 | Basis of a free module over a commutative ring `R`. |
36 | 140 |
|
@@ -119,15 +223,15 @@ def __init__(self, fmodule, symbol, latex_symbol=None): |
119 | 223 | sage: TestSuite(e).run() |
120 | 224 |
|
121 | 225 | """ |
122 | | - self._fmodule = fmodule |
123 | 226 | if latex_symbol is None: |
124 | 227 | latex_symbol = symbol |
125 | 228 | self._name = "(" + \ |
126 | 229 | ",".join([symbol + "_" + str(i) for i in fmodule.irange()]) +")" |
127 | | - self._latex_name = r"\left(" + ",".join([latex_symbol + "_" + str(i) |
| 230 | + latex_name = r"\left(" + ",".join([latex_symbol + "_" + str(i) |
128 | 231 | for i in fmodule.irange()]) + r"\right)" |
129 | | - self._symbol = symbol |
130 | | - self._latex_symbol = latex_symbol |
| 232 | + |
| 233 | + Basis_abstract.__init__(self, fmodule, symbol, latex_symbol, latex_name) |
| 234 | + |
131 | 235 | # The basis is added to the module list of bases |
132 | 236 | for other in fmodule._known_bases: |
133 | 237 | if symbol == other._symbol: |
@@ -270,28 +374,6 @@ def dual_basis(self): |
270 | 374 | """ |
271 | 375 | return self._dual_basis |
272 | 376 |
|
273 | | - def _latex_(self): |
274 | | - r""" |
275 | | - LaTeX representation of the object. |
276 | | -
|
277 | | - EXAMPLES:: |
278 | | -
|
279 | | - sage: FiniteRankFreeModule._clear_cache_() # for doctests only |
280 | | - sage: M = FiniteRankFreeModule(ZZ, 3, name='M') |
281 | | - sage: e = M.basis('e') |
282 | | - sage: e._latex_() |
283 | | - '\\left(e_0,e_1,e_2\\right)' |
284 | | - sage: latex(e) |
285 | | - \left(e_0,e_1,e_2\right) |
286 | | - sage: f = M.basis('eps', latex_symbol=r'\epsilon') |
287 | | - sage: f._latex_() |
288 | | - '\\left(\\epsilon_0,\\epsilon_1,\\epsilon_2\\right)' |
289 | | - sage: latex(f) |
290 | | - \left(\epsilon_0,\epsilon_1,\epsilon_2\right) |
291 | | -
|
292 | | - """ |
293 | | - return self._latex_name |
294 | | - |
295 | 377 | def __getitem__(self, index): |
296 | 378 | r""" |
297 | 379 | Return the basis element corresponding to the given index ``index``. |
@@ -333,25 +415,6 @@ def __getitem__(self, index): |
333 | 415 | str(n-1+si) + "]") |
334 | 416 | return self._vec[i] |
335 | 417 |
|
336 | | - def __len__(self): |
337 | | - r""" |
338 | | - Return the basis length, i.e. the rank of the free module. |
339 | | -
|
340 | | - NB: the method ``__len__()`` is required for the basis to act as a |
341 | | - "frame" in the class :class:`~sage.tensor.modules.comp.Components`. |
342 | | -
|
343 | | - EXAMPLES:: |
344 | | -
|
345 | | - sage: M = FiniteRankFreeModule(ZZ, 3, name='M') |
346 | | - sage: e = M.basis('e') |
347 | | - sage: e.__len__() |
348 | | - 3 |
349 | | - sage: len(e) |
350 | | - 3 |
351 | | -
|
352 | | - """ |
353 | | - return self._fmodule._rank |
354 | | - |
355 | 418 | def new_basis(self, change_of_basis, symbol, latex_symbol=None): |
356 | 419 | r""" |
357 | 420 | Define a new module basis from ``self``. |
@@ -441,10 +504,9 @@ def new_basis(self, change_of_basis, symbol, latex_symbol=None): |
441 | 504 | # |
442 | 505 | return the_new_basis |
443 | 506 |
|
444 | | - |
445 | 507 | #****************************************************************************** |
446 | 508 |
|
447 | | -class FreeModuleCoBasis(UniqueRepresentation, SageObject): |
| 509 | +class FreeModuleCoBasis(Basis_abstract): |
448 | 510 | r""" |
449 | 511 | Dual basis of a free module over a commutative ring. |
450 | 512 |
|
@@ -497,14 +559,16 @@ def __init__(self, basis, symbol, latex_symbol=None): |
497 | 559 |
|
498 | 560 | """ |
499 | 561 | self._basis = basis |
500 | | - self._fmodule = basis._fmodule |
501 | 562 | self._name = "(" + \ |
502 | | - ",".join([symbol + "^" + str(i) for i in self._fmodule.irange()]) +")" |
| 563 | + ",".join([symbol + "^" + str(i) for i in basis._fmodule.irange()]) +")" |
503 | 564 | if latex_symbol is None: |
504 | 565 | latex_symbol = symbol |
505 | | - self._latex_name = r"\left(" + \ |
| 566 | + latex_name = r"\left(" + \ |
506 | 567 | ",".join([latex_symbol + "^" + str(i) |
507 | | - for i in self._fmodule.irange()]) + r"\right)" |
| 568 | + for i in basis._fmodule.irange()]) + r"\right)" |
| 569 | + |
| 570 | + Basis_abstract.__init__(self, basis._fmodule, symbol, latex_symbol, latex_name) |
| 571 | + |
508 | 572 | # The individual linear forms: |
509 | 573 | vl = list() |
510 | 574 | for i in self._fmodule.irange(): |
@@ -533,21 +597,6 @@ def _repr_(self): |
533 | 597 | """ |
534 | 598 | return "Dual basis {} on the {}".format(self._name, self._fmodule) |
535 | 599 |
|
536 | | - def _latex_(self): |
537 | | - r""" |
538 | | - Return a LaTeX representation of ``self``. |
539 | | -
|
540 | | - EXAMPLES:: |
541 | | -
|
542 | | - sage: M = FiniteRankFreeModule(ZZ, 3, name='M') |
543 | | - sage: e = M.basis('e') |
544 | | - sage: f = e.dual_basis() |
545 | | - sage: f._latex_() |
546 | | - '\\left(e^0,e^1,e^2\\right)' |
547 | | -
|
548 | | - """ |
549 | | - return self._latex_name |
550 | | - |
551 | 600 | def __getitem__(self, index): |
552 | 601 | r""" |
553 | 602 | Return the basis linear form corresponding to a given index. |
@@ -586,3 +635,4 @@ def __getitem__(self, index): |
586 | 635 | raise IndexError("out of range: {} not in [{},{}]".format(i+si, |
587 | 636 | si, n-1+si)) |
588 | 637 | return self._form[i] |
| 638 | + |
0 commit comments