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
/
tensorfield.py
4598 lines (3822 loc) · 185 KB
/
tensorfield.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"""
Tensor Fields
The class :class:`TensorField` implements tensor fields on differentiable
manifolds. The derived class
:class:`~sage.manifolds.differentiable.tensorfield_paral.TensorFieldParal`
is devoted to tensor fields with values on parallelizable manifolds.
Various derived classes of :class:`TensorField` are devoted to specific tensor
fields:
* :class:`~sage.manifolds.differentiable.vectorfield.VectorField` for vector
fields (rank-1 contravariant tensor fields)
* :class:`~sage.manifolds.differentiable.automorphismfield.AutomorphismField`
for fields of tangent-space automorphisms
* :class:`~sage.manifolds.differentiable.diff_form.DiffForm` for differential
forms (fully antisymmetric covariant tensor fields)
* :class:`~sage.manifolds.differentiable.multivectorfield.MultivectorField`
for multivector fields (fully antisymmetric contravariant tensor fields)
AUTHORS:
- Eric Gourgoulhon, Michal Bejger (2013-2015) : initial version
- Travis Scrimshaw (2016): review tweaks
- Eric Gourgoulhon (2018): operators divergence, Laplacian and d'Alembertian;
method :meth:`TensorField.along`
- Florentin Jaffredo (2018) : series expansion with respect to a given
parameter
- Michael Jung (2019): improve treatment of the zero element; add method
:meth:`TensorField.copy_from`
- Eric Gourgoulhon (2020): add method :meth:`TensorField.apply_map`
REFERENCES:
- [KN1963]_
- [Lee2013]_
- [ONe1983]_
"""
# *****************************************************************************
# Copyright (C) 2015 Eric Gourgoulhon <eric.gourgoulhon@obspm.fr>
# Copyright (C) 2015 Michal Bejger <bejger@camk.edu.pl>
# Copyright (C) 2016 Travis Scrimshaw <tscrimsh@umn.edu>
#
# 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 print_function
from sage.rings.integer import Integer
from sage.rings.integer_ring import ZZ
from sage.structure.element import ModuleElement
from sage.tensor.modules.free_module_tensor import FreeModuleTensor
from sage.tensor.modules.tensor_with_indices import TensorWithIndices
class TensorField(ModuleElement):
r"""
Tensor field along a differentiable manifold.
An instance of this class is a tensor field along a differentiable
manifold `U` with values on a differentiable manifold `M`, via a
differentiable map `\Phi: U \rightarrow M`. More precisely, given two
non-negative integers `k` and `l` and a differentiable map
.. MATH::
\Phi:\ U \longrightarrow M,
a *tensor field of type* `(k,l)` *along* `U` *with values on* `M` is
a differentiable map
.. MATH::
t:\ U \longrightarrow T^{(k,l)}M
(where `T^{(k,l)}M` is the tensor bundle of type `(k,l)` over `M`) such
that
.. MATH::
\forall p \in U,\ t(p) \in T^{(k,l)}(T_q M)
i.e. `t(p)` is a tensor of type `(k,l)` on the tangent space `T_q M` at
the point `q = \Phi(p)`, that is to say a multilinear map
.. MATH::
t(p):\ \underbrace{T_q^*M\times\cdots\times T_q^*M}_{k\ \; \mbox{times}}
\times \underbrace{T_q M\times\cdots\times T_q M}_{l\ \; \mbox{times}}
\longrightarrow K,
where `T_q^* M` is the dual vector space to `T_q M` and `K` is the
topological field over which the manifold `M` is defined. The integer `k+l`
is called the *tensor rank*.
The standard case of a tensor
field *on* a differentiable manifold corresponds to `U=M` and
`\Phi = \mathrm{Id}_M`. Other common cases are `\Phi` being an
immersion and `\Phi` being a curve in `M` (`U` is then an open interval
of `\RR`).
If `M` is parallelizable, the class
:class:`~sage.manifolds.differentiable.tensorfield_paral.TensorFieldParal`
should be used instead.
This is a Sage *element* class, the corresponding *parent* class being
:class:`~sage.manifolds.differentiable.tensorfield_module.TensorFieldModule`.
INPUT:
- ``vector_field_module`` -- module `\mathfrak{X}(U,\Phi)` of vector
fields along `U` associated with the map `\Phi: U \rightarrow M` (cf.
:class:`~sage.manifolds.differentiable.vectorfield_module.VectorFieldModule`)
- ``tensor_type`` -- pair `(k,l)` with `k` being the contravariant rank
and `l` the covariant rank
- ``name`` -- (default: ``None``) name given to the tensor field
- ``latex_name`` -- (default: ``None``) LaTeX symbol to denote the tensor
field; if none is provided, the LaTeX symbol is set to ``name``
- ``sym`` -- (default: ``None``) a symmetry or a list of symmetries among
the tensor arguments: each symmetry is described by a tuple containing
the positions of the involved arguments, with the convention
``position = 0`` for the first argument; for instance:
* ``sym = (0,1)`` for a symmetry between the 1st and 2nd arguments
* ``sym = [(0,2), (1,3,4)]`` for a symmetry between the 1st and 3rd
arguments and a symmetry between the 2nd, 4th and 5th arguments.
- ``antisym`` -- (default: ``None``) antisymmetry or list of antisymmetries
among the arguments, with the same convention as for ``sym``
- ``parent`` -- (default: ``None``) some specific parent (e.g. exterior
power for differential forms); if ``None``,
``vector_field_module.tensor_module(k,l)`` is used
EXAMPLES:
Tensor field of type (0,2) on the sphere `S^2`::
sage: M = Manifold(2, 'S^2') # the 2-dimensional sphere S^2
sage: U = M.open_subset('U') # complement of the North pole
sage: c_xy.<x,y> = U.chart() # stereographic coordinates from the North pole
sage: V = M.open_subset('V') # complement of the South pole
sage: c_uv.<u,v> = V.chart() # stereographic coordinates from the South pole
sage: M.declare_union(U,V) # S^2 is the union of U and V
sage: xy_to_uv = c_xy.transition_map(c_uv, (x/(x^2+y^2), y/(x^2+y^2)),
....: intersection_name='W', restrictions1= x^2+y^2!=0,
....: restrictions2= u^2+v^2!=0)
sage: uv_to_xy = xy_to_uv.inverse()
sage: W = U.intersection(V)
sage: t = M.tensor_field(0,2, name='t') ; t
Tensor field t of type (0,2) on the 2-dimensional differentiable
manifold S^2
sage: t.parent()
Module T^(0,2)(S^2) of type-(0,2) tensors fields on the 2-dimensional
differentiable manifold S^2
sage: t.parent().category()
Category of modules over Algebra of differentiable scalar fields on the
2-dimensional differentiable manifold S^2
The parent of `t` is not a free module, for the sphere `S^2` is not
parallelizable::
sage: isinstance(t.parent(), FiniteRankFreeModule)
False
To fully define `t`, we have to specify its components in some vector
frames defined on subsets of `S^2`; let us start by the open subset `U`::
sage: eU = c_xy.frame()
sage: t[eU,:] = [[1,0], [-2,3]]
sage: t.display(eU)
t = dx*dx - 2 dy*dx + 3 dy*dy
To set the components of `t` on `V` consistently, we copy the expressions
of the components in the common subset `W`::
sage: eV = c_uv.frame()
sage: eVW = eV.restrict(W)
sage: c_uvW = c_uv.restrict(W)
sage: t[eV,0,0] = t[eVW,0,0,c_uvW].expr() # long time
sage: t[eV,0,1] = t[eVW,0,1,c_uvW].expr() # long time
sage: t[eV,1,0] = t[eVW,1,0,c_uvW].expr() # long time
sage: t[eV,1,1] = t[eVW,1,1,c_uvW].expr() # long time
Actually, the above operation can be performed in a single line by means
of the method
:meth:`~sage.manifolds.differentiable.tensorfield.TensorField.add_comp_by_continuation`::
sage: t.add_comp_by_continuation(eV, W, chart=c_uv) # long time
At this stage, `t` is fully defined, having components in frames eU and eV
and the union of the domains of eU and eV being the whole manifold::
sage: t.display(eV) # long time
t = (u^4 - 4*u^3*v + 10*u^2*v^2 + 4*u*v^3 + v^4)/(u^8 + 4*u^6*v^2 + 6*u^4*v^4 + 4*u^2*v^6 + v^8) du*du
- 4*(u^3*v + 2*u^2*v^2 - u*v^3)/(u^8 + 4*u^6*v^2 + 6*u^4*v^4 + 4*u^2*v^6 + v^8) du*dv
+ 2*(u^4 - 2*u^3*v - 2*u^2*v^2 + 2*u*v^3 + v^4)/(u^8 + 4*u^6*v^2 + 6*u^4*v^4 + 4*u^2*v^6 + v^8) dv*du
+ (3*u^4 + 4*u^3*v - 2*u^2*v^2 - 4*u*v^3 + 3*v^4)/(u^8 + 4*u^6*v^2 + 6*u^4*v^4 + 4*u^2*v^6 + v^8) dv*dv
Let us consider two vector fields, `a` and `b`, on `S^2`::
sage: a = M.vector_field({eU: [-y, x]}, name='a')
sage: a.add_comp_by_continuation(eV, W, chart=c_uv)
sage: a.display(eV)
a = -v d/du + u d/dv
sage: b = M.vector_field({eU: [y, -1]}, name='b')
sage: b.add_comp_by_continuation(eV, W, chart=c_uv)
sage: b.display(eV)
b = ((2*u + 1)*v^3 + (2*u^3 - u^2)*v)/(u^2 + v^2) d/du
- (u^4 - v^4 + 2*u*v^2)/(u^2 + v^2) d/dv
As a tensor field of type `(0,2)`, `t` acts on the pair `(a,b)`,
resulting in a scalar field::
sage: f = t(a,b); f
Scalar field t(a,b) on the 2-dimensional differentiable manifold S^2
sage: f.display() # long time
t(a,b): S^2 --> R
on U: (x, y) |--> -2*x*y - y^2 - 3*x
on V: (u, v) |--> -(3*u^3 + (3*u + 1)*v^2 + 2*u*v)/(u^4 + 2*u^2*v^2 + v^4)
The vectors can be defined only on subsets of `S^2`, the domain of the
result is then the common subset::
sage: s = t(a.restrict(U), b) ; s # long time
Scalar field t(a,b) on the Open subset U of the 2-dimensional
differentiable manifold S^2
sage: s.display() # long time
t(a,b): U --> R
(x, y) |--> -2*x*y - y^2 - 3*x
on W: (u, v) |--> -(3*u^3 + (3*u + 1)*v^2 + 2*u*v)/(u^4 + 2*u^2*v^2 + v^4)
sage: s = t(a.restrict(U), b.restrict(W)) ; s # long time
Scalar field t(a,b) on the Open subset W of the 2-dimensional
differentiable manifold S^2
sage: s.display() # long time
t(a,b): W --> R
(x, y) |--> -2*x*y - y^2 - 3*x
(u, v) |--> -(3*u^3 + (3*u + 1)*v^2 + 2*u*v)/(u^4 + 2*u^2*v^2 + v^4)
The tensor itself can be defined only on some open subset of `S^2`,
yielding a result whose domain is this subset::
sage: s = t.restrict(V)(a,b); s # long time
Scalar field t(a,b) on the Open subset V of the 2-dimensional
differentiable manifold S^2
sage: s.display() # long time
t(a,b): V --> R
(u, v) |--> -(3*u^3 + (3*u + 1)*v^2 + 2*u*v)/(u^4 + 2*u^2*v^2 + v^4)
on W: (x, y) |--> -2*x*y - y^2 - 3*x
Tests regarding the multiplication by a scalar field::
sage: f = M.scalar_field({c_xy: 1/(1+x^2+y^2),
....: c_uv: (u^2 + v^2)/(u^2 + v^2 + 1)}, name='f')
sage: t.parent().base_ring() is f.parent()
True
sage: s = f*t; s # long time
Tensor field f*t of type (0,2) on the 2-dimensional differentiable
manifold S^2
sage: s[[0,0]] == f*t[[0,0]] # long time
True
sage: s.restrict(U) == f.restrict(U) * t.restrict(U) # long time
True
sage: s = f*t.restrict(U); s
Tensor field f*t of type (0,2) on the Open subset U of the 2-dimensional
differentiable manifold S^2
sage: s.restrict(U) == f.restrict(U) * t.restrict(U)
True
.. RUBRIC:: Same examples with SymPy as the symbolic engine
From now on, we ask that all symbolic calculus on manifold `M` are
performed by SymPy::
sage: M.set_calculus_method('sympy')
We define the tensor `t` as above::
sage: t = M.tensor_field(0, 2, {eU: [[1,0], [-2,3]]}, name='t')
sage: t.display(eU)
t = dx*dx - 2 dy*dx + 3 dy*dy
sage: t.add_comp_by_continuation(eV, W, chart=c_uv) # long time
sage: t.display(eV) # long time
t = (u**4 - 4*u**3*v + 10*u**2*v**2 + 4*u*v**3 + v**4)/(u**8 +
4*u**6*v**2 + 6*u**4*v**4 + 4*u**2*v**6 + v**8) du*du +
4*u*v*(-u**2 - 2*u*v + v**2)/(u**8 + 4*u**6*v**2 + 6*u**4*v**4
+ 4*u**2*v**6 + v**8) du*dv + 2*(u**4 - 2*u**3*v - 2*u**2*v**2
+ 2*u*v**3 + v**4)/(u**8 + 4*u**6*v**2 + 6*u**4*v**4 +
4*u**2*v**6 + v**8) dv*du + (3*u**4 + 4*u**3*v - 2*u**2*v**2 -
4*u*v**3 + 3*v**4)/(u**8 + 4*u**6*v**2 + 6*u**4*v**4 +
4*u**2*v**6 + v**8) dv*dv
The default coordinate representations of tensor components are now
SymPy objects::
sage: t[eV,1,1,c_uv].expr() # long time
(3*u**4 + 4*u**3*v - 2*u**2*v**2 - 4*u*v**3 + 3*v**4)/(u**8 +
4*u**6*v**2 + 6*u**4*v**4 + 4*u**2*v**6 + v**8)
sage: type(t[eV,1,1,c_uv].expr()) # long time
<class 'sympy.core.mul.Mul'>
Let us consider two vector fields, `a` and `b`, on `S^2`::
sage: a = M.vector_field({eU: [-y, x]}, name='a')
sage: a.add_comp_by_continuation(eV, W, chart=c_uv)
sage: a.display(eV)
a = -v d/du + u d/dv
sage: b = M.vector_field({eU: [y, -1]}, name='b')
sage: b.add_comp_by_continuation(eV, W, chart=c_uv)
sage: b.display(eV)
b = v*(2*u**3 - u**2 + 2*u*v**2 + v**2)/(u**2 + v**2) d/du
+ (-u**4 - 2*u*v**2 + v**4)/(u**2 + v**2) d/dv
As a tensor field of type `(0,2)`, `t` acts on the pair `(a,b)`,
resulting in a scalar field::
sage: f = t(a,b)
sage: f.display() # long time
t(a,b): S^2 --> R
on U: (x, y) |--> -2*x*y - 3*x - y**2
on V: (u, v) |--> -(3*u**3 + 3*u*v**2 + 2*u*v + v**2)/(u**4 + 2*u**2*v**2 + v**4)
The vectors can be defined only on subsets of `S^2`, the domain of the
result is then the common subset::
sage: s = t(a.restrict(U), b)
sage: s.display() # long time
t(a,b): U --> R
(x, y) |--> -2*x*y - 3*x - y**2
on W: (u, v) |--> -(3*u**3 + 3*u*v**2 + 2*u*v + v**2)/(u**4 + 2*u**2*v**2 + v**4)
sage: s = t(a.restrict(U), b.restrict(W)) # long time
sage: s.display() # long time
t(a,b): W --> R
(x, y) |--> -2*x*y - 3*x - y**2
(u, v) |--> -(3*u**3 + 3*u*v**2 + 2*u*v + v**2)/(u**4 + 2*u**2*v**2 + v**4)
The tensor itself can be defined only on some open subset of `S^2`,
yielding a result whose domain is this subset::
sage: s = t.restrict(V)(a,b) # long time
sage: s.display() # long time
t(a,b): V --> R
(u, v) |--> -(3*u**3 + 3*u*v**2 + 2*u*v + v**2)/(u**4 + 2*u**2*v**2 + v**4)
on W: (x, y) |--> -2*x*y - 3*x - y**2
Tests regarding the multiplication by a scalar field::
sage: f = M.scalar_field({c_xy: 1/(1+x^2+y^2),
....: c_uv: (u^2 + v^2)/(u^2 + v^2 + 1)}, name='f')
sage: s = f*t # long time
sage: s[[0,0]] == f*t[[0,0]] # long time
True
sage: s.restrict(U) == f.restrict(U) * t.restrict(U) # long time
True
sage: s = f*t.restrict(U)
sage: s.restrict(U) == f.restrict(U) * t.restrict(U)
True
"""
def __init__(self, vector_field_module, tensor_type, name=None,
latex_name=None, sym=None, antisym=None, parent=None):
r"""
Construct a tensor field.
TESTS:
Construction via ``parent.element_class``, and not via a direct call
to ``TensorField``, to fit with the category framework::
sage: M = Manifold(2, 'M')
sage: U = M.open_subset('U') ; V = M.open_subset('V')
sage: M.declare_union(U,V) # M is the union of U and V
sage: c_xy.<x,y> = U.chart() ; c_uv.<u,v> = V.chart()
sage: transf = c_xy.transition_map(c_uv, (x+y, x-y),
....: intersection_name='W', restrictions1= x>0,
....: restrictions2= u+v>0)
sage: inv = transf.inverse()
sage: W = U.intersection(V)
sage: e_xy = c_xy.frame() ; e_uv = c_uv.frame()
sage: XM = M.vector_field_module()
sage: T02 = M.tensor_field_module((0,2))
sage: t = T02.element_class(XM, (0,2), name='t'); t
Tensor field t of type (0,2) on the 2-dimensional differentiable
manifold M
sage: t[e_xy,:] = [[1+x^2, x*y], [0, 1+y^2]]
sage: t.add_comp_by_continuation(e_uv, W, c_uv)
sage: t.display(e_xy)
t = (x^2 + 1) dx*dx + x*y dx*dy + (y^2 + 1) dy*dy
sage: t.display(e_uv)
t = (3/16*u^2 + 1/16*v^2 + 1/2) du*du
+ (-1/16*u^2 + 1/4*u*v + 1/16*v^2) du*dv
+ (1/16*u^2 + 1/4*u*v - 1/16*v^2) dv*du
+ (1/16*u^2 + 3/16*v^2 + 1/2) dv*dv
sage: TestSuite(t).run(skip='_test_pickling')
Construction with ``DifferentiableManifold.tensor_field``::
sage: t1 = M.tensor_field(0, 2, name='t'); t1
Tensor field t of type (0,2) on the 2-dimensional differentiable
manifold M
sage: type(t1) == type(t)
True
"""
if parent is None:
parent = vector_field_module.tensor_module(*tensor_type)
ModuleElement.__init__(self, parent)
self._vmodule = vector_field_module
self._tensor_type = tuple(tensor_type)
self._tensor_rank = self._tensor_type[0] + self._tensor_type[1]
self._is_zero = False # a priori, may be changed below or via
# method __bool__()
self._name = name
if latex_name is None:
self._latex_name = self._name
else:
self._latex_name = latex_name
self._domain = vector_field_module._domain
self._ambient_domain = vector_field_module._ambient_domain
self._extensions_graph = {self._domain: self}
# dict. of known extensions of self on bigger domains,
# including self, with domains as keys. Its elements can be
# seen as incoming edges on a graph.
self._restrictions_graph = {self._domain: self}
# dict. of known restrictions of self on smaller domains,
# including self, with domains as keys. Its elements can be
# seen as outgoing edges on a graph.
self._restrictions = {} # dict. of restrictions of self on subdomains
# of self._domain, with the subdomains as keys
# Treatment of symmetry declarations:
self._sym = []
if sym is not None and sym != []:
if isinstance(sym[0], (int, Integer)):
# a single symmetry is provided as a tuple -> 1-item list:
sym = [tuple(sym)]
for isym in sym:
if len(isym) > 1:
for i in isym:
if i < 0 or i > self._tensor_rank - 1:
raise IndexError("invalid position: {}".format(i) +
" not in [0,{}]".format(self._tensor_rank-1))
self._sym.append(tuple(isym))
self._antisym = []
if antisym is not None and antisym != []:
if isinstance(antisym[0], (int, Integer)):
# a single antisymmetry is provided as a tuple -> 1-item list:
antisym = [tuple(antisym)]
for isym in antisym:
if len(isym) > 1:
for i in isym:
if i < 0 or i > self._tensor_rank - 1:
raise IndexError("invalid position: {}".format(i) +
" not in [0,{}]".format(self._tensor_rank-1))
self._antisym.append(tuple(isym))
# Final consistency check:
index_list = []
for isym in self._sym:
index_list += isym
for isym in self._antisym:
index_list += isym
if len(index_list) != len(set(index_list)):
# There is a repeated index position:
raise IndexError("incompatible lists of symmetries: the same " +
"position appears more than once")
# Initialization of derived quantities:
self._init_derived()
####### Required methods for ModuleElement (beside arithmetic) #######
def __bool__(self):
r"""
Return ``True`` if ``self`` is nonzero and ``False`` otherwise.
This method is called by :meth:`is_zero`.
EXAMPLES:
Tensor field defined by parts on a 2-dimensional manifold::
sage: M = Manifold(2, 'M')
sage: U = M.open_subset('U')
sage: c_xy.<x, y> = U.chart()
sage: V = M.open_subset('V')
sage: c_uv.<u, v> = V.chart()
sage: M.declare_union(U,V) # M is the union of U and V
sage: t = M.tensor_field(1, 2, name='t')
sage: tu = U.tensor_field(1, 2, name='t')
sage: tv = V.tensor_field(1, 2, name='t')
sage: tu[0,0,0] = 0
sage: tv[0,0,0] = 0
sage: t.set_restriction(tv)
sage: t.set_restriction(tu)
sage: bool(t)
False
sage: t.is_zero() # indirect doctest
True
sage: tv[0,0,0] = 1
sage: t.set_restriction(tv)
sage: bool(t)
True
sage: t.is_zero() # indirect doctest
False
"""
if self._is_zero:
return False
if any(bool(rst) for rst in self._restrictions.values()):
self._is_zero = False
return True
self._is_zero = True
return False
__nonzero__ = __bool__ # For Python2 compatibility
##### End of required methods for ModuleElement (beside arithmetic) #####
def _repr_(self):
r"""
String representation of ``self``.
TESTS::
sage: M = Manifold(2, 'M')
sage: t = M.tensor_field(1, 3, name='t')
sage: t
Tensor field t of type (1,3) on the 2-dimensional differentiable manifold M
"""
# Special cases
if self._tensor_type == (0,2) and self._sym == [(0,1)]:
description = "Field of symmetric bilinear forms "
if self._name is not None:
description += self._name + " "
else:
# Generic case
description = "Tensor field "
if self._name is not None:
description += self._name + " "
description += "of type ({},{}) ".format(
self._tensor_type[0], self._tensor_type[1])
return self._final_repr(description)
def _latex_(self):
r"""
LaTeX representation of ``self``.
TESTS::
sage: M = Manifold(2, 'M')
sage: t = M.tensor_field(1, 3, name='t')
sage: t._latex_()
't'
sage: t = M.tensor_field(1, 3, name='t', latex_name=r'\tau')
sage: latex(t)
\tau
"""
if self._latex_name is None:
return r'\mbox{' + str(self) + r'}'
else:
return self._latex_name
def set_name(self, name=None, latex_name=None):
r"""
Set (or change) the text name and LaTeX name of ``self``.
INPUT:
- ``name`` -- string (default: ``None``); name given to the tensor
field
- ``latex_name`` -- string (default: ``None``); LaTeX symbol to denote
the tensor field; if ``None`` while ``name`` is provided, the LaTeX
symbol is set to ``name``
EXAMPLES::
sage: M = Manifold(2, 'M')
sage: t = M.tensor_field(1, 3); t
Tensor field of type (1,3) on the 2-dimensional differentiable
manifold M
sage: t.set_name(name='t')
sage: t
Tensor field t of type (1,3) on the 2-dimensional differentiable
manifold M
sage: latex(t)
t
sage: t.set_name(latex_name=r'\tau')
sage: latex(t)
\tau
sage: t.set_name(name='a')
sage: t
Tensor field a of type (1,3) on the 2-dimensional differentiable
manifold M
sage: latex(t)
a
"""
if name is not None:
self._name = name
if latex_name is None:
self._latex_name = self._name
if latex_name is not None:
self._latex_name = latex_name
for rst in self._restrictions.values():
rst.set_name(name=name, latex_name=latex_name)
def _new_instance(self):
r"""
Create an instance of the same class as ``self`` on the same
vector field module, with the same tensor type and same symmetries
TESTS::
sage: M = Manifold(2, 'M')
sage: t = M.tensor_field(1, 3, name='t')
sage: t1 = t._new_instance(); t1
Tensor field of type (1,3) on the 2-dimensional differentiable
manifold M
sage: type(t1) == type(t)
True
sage: t1.parent() is t.parent()
True
"""
return type(self)(self._vmodule, self._tensor_type, sym=self._sym,
antisym=self._antisym, parent=self.parent())
def _final_repr(self, description):
r"""
Part of string representation common to all derived classes of
:class:`TensorField`.
TESTS::
sage: M = Manifold(2, 'M')
sage: t = M.tensor_field(1, 3, name='t')
sage: t._final_repr('Tensor field t ')
'Tensor field t on the 2-dimensional differentiable manifold M'
"""
if self._domain == self._ambient_domain:
description += "on the {}".format(self._domain)
else:
description += "along the {} ".format(self._domain) + \
"with values on the {}".format(self._ambient_domain)
return description
def _init_derived(self):
r"""
Initialize the derived quantities.
TESTS::
sage: M = Manifold(2, 'M')
sage: t = M.tensor_field(1, 3, name='t')
sage: t._init_derived()
"""
self._lie_derivatives = {} # dict. of Lie derivatives of self (keys: id(vector))
def _del_derived(self):
r"""
Delete the derived quantities.
TESTS::
sage: M = Manifold(2, 'M')
sage: t = M.tensor_field(1, 3, name='t')
sage: t._del_derived()
"""
# First deletes any reference to self in the vectors' dictionaries:
for vid, val in self._lie_derivatives.items():
del val[0]._lie_der_along_self[id(self)]
# Then clears the dictionary of Lie derivatives
self._lie_derivatives.clear()
def _del_restrictions(self):
r"""
Delete the restrictions defined on ``self``.
TESTS::
sage: M = Manifold(2, 'M')
sage: c_xy.<x,y> = M.chart()
sage: t = M.tensor_field(1,2)
sage: U = M.open_subset('U', coord_def={c_xy: x<0})
sage: h = t.restrict(U)
sage: t._restrictions
{Open subset U of the 2-dimensional differentiable manifold M:
Tensor field of type (1,2) on the Open subset U of the
2-dimensional differentiable manifold M}
sage: t._del_restrictions()
sage: t._restrictions
{}
"""
self._restrictions.clear()
self._extensions_graph = {self._domain: self}
self._restrictions_graph = {self._domain: self}
def _init_components(self, *comp, **kwargs):
r"""
Initialize the tensor field components in some given vector frames.
INPUT:
- ``comp`` -- either the components of the tensor field with respect
to the vector frame specified by the argument ``frame`` or a
dictionary of components, the keys of which are vector frames or
pairs ``(f,c)`` where ``f`` is a vector frame and ``c`` a chart
- ``frame`` -- (default: ``None``; unused if ``comp`` is a dictionary)
vector frame in which the components are given; if ``None``, the
default vector frame on the domain of ``self`` is assumed
- ``chart`` -- (default: ``None``; unused if ``comp`` is a dictionary)
coordinate chart in which the components are expressed; if ``None``,
the default chart on the domain of ``frame`` is assumed
EXAMPLES::
sage: M = Manifold(2, 'M')
sage: X.<x,y> = M.chart()
sage: t = M.tensor_field(1, 1, name='t')
sage: t._init_components([[1+x, x*y], [-2, y^2]])
sage: t.display()
t = (x + 1) d/dx*dx + x*y d/dx*dy - 2 d/dy*dx + y^2 d/dy*dy
sage: Y.<u,v> = M.chart()
sage: t._init_components([[2*u, 3*v], [u+v, -u]], frame=Y.frame(),
....: chart=Y)
sage: t.display(Y)
t = 2*u d/du*du + 3*v d/du*dv + (u + v) d/dv*du - u d/dv*dv
sage: t._init_components({X.frame(): [[2*x, 1-y],[0, x]]})
sage: t.display()
t = 2*x d/dx*dx + (-y + 1) d/dx*dy + x d/dy*dy
sage: t._init_components({(Y.frame(), Y): [[2*u, 0],[v^3, u+v]]})
sage: t.display(Y)
t = 2*u d/du*du + v^3 d/dv*du + (u + v) d/dv*dv
TESTS:
Check that :trac:`29639` is fixed::
sage: v = M.vector_field()
sage: v._init_components(1/2, -1)
sage: v.display()
1/2 d/dx - d/dy
"""
comp0 = comp[0]
self._is_zero = False # a priori
if isinstance(comp0, dict):
for frame, components in comp0.items():
chart = None
if isinstance(frame, tuple):
# frame is actually a pair (frame, chart):
frame, chart = frame
self.add_comp(frame)[:, chart] = components
elif isinstance(comp0, str):
# For compatibility with previous use of tensor_field():
self.set_name(comp0)
else:
if hasattr(comp0, '__len__') and hasattr(comp0, '__getitem__'):
# comp0 is a list/vector of components
# otherwise comp is the tuple of components in a specific frame
comp = comp0
frame = kwargs.get('frame')
chart = kwargs.get('chart')
self.add_comp(frame)[:, chart] = comp
#### Simple accessors ####
def domain(self):
r"""
Return the manifold on which ``self`` is defined.
OUTPUT:
- instance of class
:class:`~sage.manifolds.differentiable.manifold.DifferentiableManifold`
EXAMPLES::
sage: M = Manifold(2, 'M')
sage: c_xy.<x,y> = M.chart()
sage: t = M.tensor_field(1,2)
sage: t.domain()
2-dimensional differentiable manifold M
sage: U = M.open_subset('U', coord_def={c_xy: x<0})
sage: h = t.restrict(U)
sage: h.domain()
Open subset U of the 2-dimensional differentiable manifold M
"""
return self._domain
def base_module(self):
r"""
Return the vector field module on which ``self`` acts as a tensor.
OUTPUT:
- instance of
:class:`~sage.manifolds.differentiable.vectorfield_module.VectorFieldModule`
EXAMPLES:
The module of vector fields on the 2-sphere as a "base module"::
sage: M = Manifold(2, 'S^2')
sage: t = M.tensor_field(0,2)
sage: t.base_module()
Module X(S^2) of vector fields on the 2-dimensional differentiable
manifold S^2
sage: t.base_module() is M.vector_field_module()
True
sage: XM = M.vector_field_module()
sage: XM.an_element().base_module() is XM
True
"""
return self._vmodule
def tensor_type(self):
r"""
Return the tensor type of ``self``.
OUTPUT:
- pair `(k,l)`, where `k` is the contravariant rank and `l` is
the covariant rank
EXAMPLES::
sage: M = Manifold(2, 'S^2')
sage: t = M.tensor_field(1,2)
sage: t.tensor_type()
(1, 2)
sage: v = M.vector_field()
sage: v.tensor_type()
(1, 0)
"""
return self._tensor_type
def tensor_rank(self):
r"""
Return the tensor rank of ``self``.
OUTPUT:
- integer `k+l`, where `k` is the contravariant rank and `l` is
the covariant rank
EXAMPLES::
sage: M = Manifold(2, 'S^2')
sage: t = M.tensor_field(1,2)
sage: t.tensor_rank()
3
sage: v = M.vector_field()
sage: v.tensor_rank()
1
"""
return self._tensor_rank
def symmetries(self):
r"""
Print the list of symmetries and antisymmetries.
EXAMPLES::
sage: M = Manifold(2, 'S^2')
sage: t = M.tensor_field(1,2)
sage: t.symmetries()
no symmetry; no antisymmetry
sage: t = M.tensor_field(1,2, sym=(1,2))
sage: t.symmetries()
symmetry: (1, 2); no antisymmetry
sage: t = M.tensor_field(2,2, sym=(0,1), antisym=(2,3))
sage: t.symmetries()
symmetry: (0, 1); antisymmetry: (2, 3)
sage: t = M.tensor_field(2,2, antisym=[(0,1),(2,3)])
sage: t.symmetries()
no symmetry; antisymmetries: [(0, 1), (2, 3)]
"""
if not self._sym:
s = "no symmetry; "
elif len(self._sym) == 1:
s = "symmetry: {}; ".format(self._sym[0])
else:
s = "symmetries: {}; ".format(self._sym)
if not self._antisym:
a = "no antisymmetry"
elif len(self._antisym) == 1:
a = "antisymmetry: {}".format(self._antisym[0])
else:
a = "antisymmetries: {}".format(self._antisym)
print(s + a)
#### End of simple accessors #####
def set_restriction(self, rst):
r"""
Define a restriction of ``self`` to some subdomain.
INPUT:
- ``rst`` -- :class:`TensorField` of the same type and symmetries
as the current tensor field ``self``, defined on a subdomain of
the domain of ``self``
EXAMPLES::
sage: M = Manifold(2, 'M') # the 2-dimensional sphere S^2
sage: U = M.open_subset('U') # complement of the North pole
sage: c_xy.<x,y> = U.chart() # stereographic coordinates from the North pole
sage: V = M.open_subset('V') # complement of the South pole
sage: c_uv.<u,v> = V.chart() # stereographic coordinates from the South pole
sage: M.declare_union(U,V) # S^2 is the union of U and V
sage: t = M.tensor_field(1, 2, name='t')
sage: s = U.tensor_field(1, 2)
sage: s[0,0,1] = x+y
sage: t.set_restriction(s)
sage: t.display(c_xy.frame())
t = (x + y) d/dx*dx*dy
sage: t.restrict(U) == s
True
If the restriction is defined on the very same domain, the tensor field
becomes a copy of it (see :meth:`copy_from`)::
sage: v = M.tensor_field(1, 2, name='v')
sage: v.set_restriction(t)
sage: v.restrict(U) == t.restrict(U)
True
"""
if not isinstance(rst, TensorField):
raise TypeError("the argument must be a tensor field")
if not rst._domain.is_subset(self._domain):
raise ValueError("the domain of the declared restriction is not " +
"a subset of the field's domain")
if not rst._ambient_domain.is_subset(self._ambient_domain):
raise ValueError("the ambient domain of the declared " +
"restriction is not a subset of the " +
"field's ambient domain")
if rst._tensor_type != self._tensor_type:
raise ValueError("the declared restriction has not the same " +
"tensor type as the current tensor field")
if rst._tensor_type != self._tensor_type:
raise ValueError("the declared restriction has not the same " +
"tensor type as the current tensor field")
if rst._sym != self._sym:
raise ValueError("the declared restriction has not the same " +
"symmetries as the current tensor field")
if rst._antisym != self._antisym:
raise ValueError("the declared restriction has not the same " +
"antisymmetries as the current tensor field")
if self._domain is rst._domain:
self.copy_from(rst)
else:
self._restrictions[rst._domain] = rst.copy()
self._restrictions[rst._domain].set_name(name=self._name,
latex_name=self._latex_name)
self._is_zero = False # a priori
def restrict(self, subdomain, dest_map=None):
r"""
Return the restriction of ``self`` to some subdomain.
If the restriction has not been defined yet, it is constructed here.
INPUT:
- ``subdomain`` --
:class:`~sage.manifolds.differentiable.manifold.DifferentiableManifold`;
open subset `U` of the tensor field domain `S`
- ``dest_map`` --
:class:`~sage.manifolds.differentiable.diff_map.DiffMap`
(default: ``None``); destination map `\Psi:\ U \rightarrow V`,
where `V` is an open subset of the manifold `M` where the tensor
field takes it values; if ``None``, the restriction of `\Phi`
to `U` is used, `\Phi` being the differentiable map
`S \rightarrow M` associated with the tensor field
OUTPUT:
- :class:`TensorField` representing the restriction
EXAMPLES:
Restrictions of a vector field on the 2-sphere::