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
/
scalarfield.py
3571 lines (2913 loc) · 122 KB
/
scalarfield.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"""
Scalar Fields
Given a topological manifold `M` over a topological field `K` (in most
applications, `K = \RR` or `K = \CC`), a *scalar field* on `M` is a
continuous map
.. MATH::
f: M \longrightarrow K
Scalar fields are implemented by the class :class:`ScalarField`.
AUTHORS:
- Eric Gourgoulhon, Michal Bejger (2013-2015): initial version
- Travis Scrimshaw (2016): review tweaks
- Marco Mancini (2017): SymPy as an optional symbolic engine, alternative to SR
- Florentin Jaffredo (2018) : series expansion with respect to a given
parameter
- Michael Jung (2019) : improve restrictions; make ``display()`` show all
distinct expressions
REFERENCES:
- [Lee2011]_
- [KN1963]_
"""
# *****************************************************************************
# 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>
# Copyright (C) 2017 Marco Mancini <marco.mancini@obspm.fr>
#
# 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 sage.structure.element import (CommutativeAlgebraElement,
ModuleElementWithMutability)
from sage.symbolic.expression import Expression
from sage.manifolds.chart_func import ChartFunction
from sage.misc.cachefunc import cached_method
class ScalarField(CommutativeAlgebraElement, ModuleElementWithMutability):
r"""
Scalar field on a topological manifold.
Given a topological manifold `M` over a topological field `K` (in most
applications, `K = \RR` or `K = \CC`), a *scalar field on* `M` is a
continuous map
.. MATH::
f: M \longrightarrow K.
A scalar field on `M` is an element of the commutative algebra
`C^0(M)` (see
:class:`~sage.manifolds.scalarfield_algebra.ScalarFieldAlgebra`).
INPUT:
- ``parent`` -- the algebra of scalar fields containing the scalar field
(must be an instance of class
:class:`~sage.manifolds.scalarfield_algebra.ScalarFieldAlgebra`)
- ``coord_expression`` -- (default: ``None``) coordinate expression(s) of
the scalar field; this can be either
* a dictionary of coordinate expressions in various charts on
the domain, with the charts as keys;
* a single coordinate expression; if the argument ``chart`` is
``'all'``, this expression is set to all the charts defined
on the open set; otherwise, the expression is set in the
specific chart provided by the argument ``chart``
- ``chart`` -- (default: ``None``) chart defining the coordinates used
in ``coord_expression`` when the latter is a single coordinate
expression; if none is provided (default), the default chart of the
open set is assumed. If ``chart=='all'``, ``coord_expression`` is
assumed to be independent of the chart (constant scalar field).
- ``name`` -- (default: ``None``) string; name (symbol) given to the
scalar field
- ``latex_name`` -- (default: ``None``) string; LaTeX symbol to denote the
scalar field; if none is provided, the LaTeX symbol is set to ``name``
If ``coord_expression`` is ``None`` or incomplete, coordinate
expressions can be added after the creation of the object, by means of
the methods :meth:`add_expr`, :meth:`add_expr_by_continuation` and
:meth:`set_expr`.
EXAMPLES:
A scalar field on the 2-sphere::
sage: M = Manifold(2, 'M', structure='topological') # 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: f = M.scalar_field({c_xy: 1/(1+x^2+y^2), c_uv: (u^2+v^2)/(1+u^2+v^2)},
....: name='f') ; f
Scalar field f on the 2-dimensional topological manifold M
sage: f.display()
f: M --> R
on U: (x, y) |--> 1/(x^2 + y^2 + 1)
on V: (u, v) |--> (u^2 + v^2)/(u^2 + v^2 + 1)
For scalar fields defined by a single coordinate expression, the latter
can be passed instead of the dictionary over the charts::
sage: g = U.scalar_field(x*y, chart=c_xy, name='g') ; g
Scalar field g on the Open subset U of the 2-dimensional topological
manifold M
The above is indeed equivalent to::
sage: g = U.scalar_field({c_xy: x*y}, name='g') ; g
Scalar field g on the Open subset U of the 2-dimensional topological
manifold M
Since ``c_xy`` is the default chart of ``U``, the argument ``chart`` can
be skipped::
sage: g = U.scalar_field(x*y, name='g') ; g
Scalar field g on the Open subset U of the 2-dimensional topological
manifold M
The scalar field `g` is defined on `U` and has an expression in terms of
the coordinates `(u,v)` on `W=U\cap V`::
sage: g.display()
g: U --> R
(x, y) |--> x*y
on W: (u, v) |--> u*v/(u^4 + 2*u^2*v^2 + v^4)
Scalar fields on `M` can also be declared with a single chart::
sage: f = M.scalar_field(1/(1+x^2+y^2), chart=c_xy, name='f') ; f
Scalar field f on the 2-dimensional topological manifold M
Their definition must then be completed by providing the expressions on
other charts, via the method :meth:`add_expr`, to get a global cover of
the manifold::
sage: f.add_expr((u^2+v^2)/(1+u^2+v^2), chart=c_uv)
sage: f.display()
f: M --> R
on U: (x, y) |--> 1/(x^2 + y^2 + 1)
on V: (u, v) |--> (u^2 + v^2)/(u^2 + v^2 + 1)
We can even first declare the scalar field without any coordinate
expression and provide them subsequently::
sage: f = M.scalar_field(name='f')
sage: f.add_expr(1/(1+x^2+y^2), chart=c_xy)
sage: f.add_expr((u^2+v^2)/(1+u^2+v^2), chart=c_uv)
sage: f.display()
f: M --> R
on U: (x, y) |--> 1/(x^2 + y^2 + 1)
on V: (u, v) |--> (u^2 + v^2)/(u^2 + v^2 + 1)
We may also use the method :meth:`add_expr_by_continuation` to complete
the coordinate definition using the analytic continuation from domains in
which charts overlap::
sage: f = M.scalar_field(1/(1+x^2+y^2), chart=c_xy, name='f') ; f
Scalar field f on the 2-dimensional topological manifold M
sage: f.add_expr_by_continuation(c_uv, U.intersection(V))
sage: f.display()
f: M --> R
on U: (x, y) |--> 1/(x^2 + y^2 + 1)
on V: (u, v) |--> (u^2 + v^2)/(u^2 + v^2 + 1)
A scalar field can also be defined by some unspecified function of the
coordinates::
sage: h = U.scalar_field(function('H')(x, y), name='h') ; h
Scalar field h on the Open subset U of the 2-dimensional topological
manifold M
sage: h.display()
h: U --> R
(x, y) |--> H(x, y)
on W: (u, v) |--> H(u/(u^2 + v^2), v/(u^2 + v^2))
We may use the argument ``latex_name`` to specify the LaTeX symbol denoting
the scalar field if the latter is different from ``name``::
sage: latex(f)
f
sage: f = M.scalar_field({c_xy: 1/(1+x^2+y^2), c_uv: (u^2+v^2)/(1+u^2+v^2)},
....: name='f', latex_name=r'\mathcal{F}')
sage: latex(f)
\mathcal{F}
The coordinate expression in a given chart is obtained via the method
:meth:`expr`, which returns a symbolic expression::
sage: f.expr(c_uv)
(u^2 + v^2)/(u^2 + v^2 + 1)
sage: type(f.expr(c_uv))
<type 'sage.symbolic.expression.Expression'>
The method :meth:`coord_function` returns instead a function of the
chart coordinates, i.e. an instance of
:class:`~sage.manifolds.chart_func.ChartFunction`::
sage: f.coord_function(c_uv)
(u^2 + v^2)/(u^2 + v^2 + 1)
sage: type(f.coord_function(c_uv))
<class 'sage.manifolds.chart_func.ChartFunctionRing_with_category.element_class'>
sage: f.coord_function(c_uv).display()
(u, v) |--> (u^2 + v^2)/(u^2 + v^2 + 1)
The value returned by the method :meth:`expr` is actually the coordinate
expression of the chart function::
sage: f.expr(c_uv) is f.coord_function(c_uv).expr()
True
A constant scalar field is declared by setting the argument ``chart`` to
``'all'``::
sage: c = M.scalar_field(2, chart='all', name='c') ; c
Scalar field c on the 2-dimensional topological manifold M
sage: c.display()
c: M --> R
on U: (x, y) |--> 2
on V: (u, v) |--> 2
A shortcut is to use the method
:meth:`~sage.manifolds.manifold.TopologicalManifold.constant_scalar_field`::
sage: c == M.constant_scalar_field(2)
True
The constant value can be some unspecified parameter::
sage: var('a')
a
sage: c = M.constant_scalar_field(a, name='c') ; c
Scalar field c on the 2-dimensional topological manifold M
sage: c.display()
c: M --> R
on U: (x, y) |--> a
on V: (u, v) |--> a
A special case of constant field is the zero scalar field::
sage: zer = M.constant_scalar_field(0) ; zer
Scalar field zero on the 2-dimensional topological manifold M
sage: zer.display()
zero: M --> R
on U: (x, y) |--> 0
on V: (u, v) |--> 0
It can be obtained directly by means of the function
:meth:`~sage.manifolds.manifold.TopologicalManifold.zero_scalar_field`::
sage: zer is M.zero_scalar_field()
True
A third way is to get it as the zero element of the algebra `C^0(M)`
of scalar fields on `M` (see below)::
sage: zer is M.scalar_field_algebra().zero()
True
The constant scalar fields zero and one are immutable, and therefore
their expressions cannot be changed::
sage: zer.is_immutable()
True
sage: zer.set_expr(x)
Traceback (most recent call last):
...
AssertionError: the expressions of an immutable element cannot be
changed
sage: one = M.one_scalar_field()
sage: one.is_immutable()
True
sage: one.set_expr(x)
Traceback (most recent call last):
...
AssertionError: the expressions of an immutable element cannot be
changed
Other scalar fields can be declared immutable, too::
sage: c.is_immutable()
False
sage: c.set_immutable()
sage: c.is_immutable()
True
sage: c.set_expr(y^2)
Traceback (most recent call last):
...
AssertionError: the expressions of an immutable element cannot be
changed
sage: c.set_name('b')
Traceback (most recent call last):
...
AssertionError: the name of an immutable element cannot be changed
Immutable elements are hashable and can therefore be used as keys for
dictionaries::
sage: {c: 1}[c]
1
By definition, a scalar field acts on the manifold's points, sending
them to elements of the manifold's base field (real numbers in the
present case)::
sage: N = M.point((0,0), chart=c_uv) # the North pole
sage: S = M.point((0,0), chart=c_xy) # the South pole
sage: E = M.point((1,0), chart=c_xy) # a point at the equator
sage: f(N)
0
sage: f(S)
1
sage: f(E)
1/2
sage: h(E)
H(1, 0)
sage: c(E)
a
sage: zer(E)
0
A scalar field can be compared to another scalar field::
sage: f == g
False
...to a symbolic expression::
sage: f == x*y
False
sage: g == x*y
True
sage: c == a
True
...to a number::
sage: f == 2
False
sage: zer == 0
True
...to anything else::
sage: f == M
False
Standard mathematical functions are implemented::
sage: sqrt(f)
Scalar field sqrt(f) on the 2-dimensional topological manifold M
sage: sqrt(f).display()
sqrt(f): M --> R
on U: (x, y) |--> 1/sqrt(x^2 + y^2 + 1)
on V: (u, v) |--> sqrt(u^2 + v^2)/sqrt(u^2 + v^2 + 1)
::
sage: tan(f)
Scalar field tan(f) on the 2-dimensional topological manifold M
sage: tan(f).display()
tan(f): M --> R
on U: (x, y) |--> sin(1/(x^2 + y^2 + 1))/cos(1/(x^2 + y^2 + 1))
on V: (u, v) |--> sin((u^2 + v^2)/(u^2 + v^2 + 1))/cos((u^2 + v^2)/(u^2 + v^2 + 1))
.. RUBRIC:: Arithmetics of scalar fields
Scalar fields on `M` (resp. `U`) belong to the algebra `C^0(M)`
(resp. `C^0(U)`)::
sage: f.parent()
Algebra of scalar fields on the 2-dimensional topological manifold M
sage: f.parent() is M.scalar_field_algebra()
True
sage: g.parent()
Algebra of scalar fields on the Open subset U of the 2-dimensional
topological manifold M
sage: g.parent() is U.scalar_field_algebra()
True
Consequently, scalar fields can be added::
sage: s = f + c ; s
Scalar field f+c on the 2-dimensional topological manifold M
sage: s.display()
f+c: M --> R
on U: (x, y) |--> (a*x^2 + a*y^2 + a + 1)/(x^2 + y^2 + 1)
on V: (u, v) |--> ((a + 1)*u^2 + (a + 1)*v^2 + a)/(u^2 + v^2 + 1)
and subtracted::
sage: s = f - c ; s
Scalar field f-c on the 2-dimensional topological manifold M
sage: s.display()
f-c: M --> R
on U: (x, y) |--> -(a*x^2 + a*y^2 + a - 1)/(x^2 + y^2 + 1)
on V: (u, v) |--> -((a - 1)*u^2 + (a - 1)*v^2 + a)/(u^2 + v^2 + 1)
Some tests::
sage: f + zer == f
True
sage: f - f == zer
True
sage: f + (-f) == zer
True
sage: (f+c)-f == c
True
sage: (f-c)+c == f
True
We may add a number (interpreted as a constant scalar field) to a scalar
field::
sage: s = f + 1 ; s
Scalar field f+1 on the 2-dimensional topological manifold M
sage: s.display()
f+1: M --> R
on U: (x, y) |--> (x^2 + y^2 + 2)/(x^2 + y^2 + 1)
on V: (u, v) |--> (2*u^2 + 2*v^2 + 1)/(u^2 + v^2 + 1)
sage: (f+1)-1 == f
True
The number can represented by a symbolic variable::
sage: s = a + f ; s
Scalar field on the 2-dimensional topological manifold M
sage: s == c + f
True
However if the symbolic variable is a chart coordinate, the addition
is performed only on the chart domain::
sage: s = f + x; s
Scalar field on the 2-dimensional topological manifold M
sage: s.display()
M --> R
on U: (x, y) |--> (x^3 + x*y^2 + x + 1)/(x^2 + y^2 + 1)
on W: (u, v) |--> (u^4 + v^4 + u^3 + (2*u^2 + u)*v^2 + u)/(u^4 + v^4 + (2*u^2 + 1)*v^2 + u^2)
sage: s = f + u; s
Scalar field on the 2-dimensional topological manifold M
sage: s.display()
M --> R
on W: (x, y) |--> (x^3 + (x + 1)*y^2 + x^2 + x)/(x^4 + y^4 + (2*x^2 + 1)*y^2 + x^2)
on V: (u, v) |--> (u^3 + (u + 1)*v^2 + u^2 + u)/(u^2 + v^2 + 1)
The addition of two scalar fields with different domains is possible if
the domain of one of them is a subset of the domain of the other; the
domain of the result is then this subset::
sage: f.domain()
2-dimensional topological manifold M
sage: g.domain()
Open subset U of the 2-dimensional topological manifold M
sage: s = f + g ; s
Scalar field f+g on the Open subset U of the 2-dimensional topological
manifold M
sage: s.domain()
Open subset U of the 2-dimensional topological manifold M
sage: s.display()
f+g: U --> R
(x, y) |--> (x*y^3 + (x^3 + x)*y + 1)/(x^2 + y^2 + 1)
on W: (u, v) |--> (u^6 + 3*u^4*v^2 + 3*u^2*v^4 + v^6 + u*v^3
+ (u^3 + u)*v)/(u^6 + v^6 + (3*u^2 + 1)*v^4 + u^4 + (3*u^4 + 2*u^2)*v^2)
The operation actually performed is `f|_U + g`::
sage: s == f.restrict(U) + g
True
In Sage framework, the addition of `f` and `g` is permitted because
there is a *coercion* of the parent of `f`, namely `C^0(M)`, to
the parent of `g`, namely `C^0(U)` (see
:class:`~sage.manifolds.scalarfield_algebra.ScalarFieldAlgebra`)::
sage: CM = M.scalar_field_algebra()
sage: CU = U.scalar_field_algebra()
sage: CU.has_coerce_map_from(CM)
True
The coercion map is nothing but the restriction to domain `U`::
sage: CU.coerce(f) == f.restrict(U)
True
Since the algebra `C^0(M)` is a vector space over `\RR`, scalar fields
can be multiplied by a number, either an explicit one::
sage: s = 2*f ; s
Scalar field on the 2-dimensional topological manifold M
sage: s.display()
M --> R
on U: (x, y) |--> 2/(x^2 + y^2 + 1)
on V: (u, v) |--> 2*(u^2 + v^2)/(u^2 + v^2 + 1)
or a symbolic one::
sage: s = a*f ; s
Scalar field on the 2-dimensional topological manifold M
sage: s.display()
M --> R
on U: (x, y) |--> a/(x^2 + y^2 + 1)
on V: (u, v) |--> (u^2 + v^2)*a/(u^2 + v^2 + 1)
However, if the symbolic variable is a chart coordinate, the
multiplication is performed only in the corresponding chart::
sage: s = x*f; s
Scalar field on the 2-dimensional topological manifold M
sage: s.display()
M --> R
on U: (x, y) |--> x/(x^2 + y^2 + 1)
on W: (u, v) |--> u/(u^2 + v^2 + 1)
sage: s = u*f; s
Scalar field on the 2-dimensional topological manifold M
sage: s.display()
M --> R
on W: (x, y) |--> x/(x^4 + y^4 + (2*x^2 + 1)*y^2 + x^2)
on V: (u, v) |--> (u^2 + v^2)*u/(u^2 + v^2 + 1)
Some tests::
sage: 0*f == 0
True
sage: 0*f == zer
True
sage: 1*f == f
True
sage: (-2)*f == - f - f
True
The ring multiplication of the algebras `C^0(M)` and `C^0(U)`
is the pointwise multiplication of functions::
sage: s = f*f ; s
Scalar field f*f on the 2-dimensional topological manifold M
sage: s.display()
f*f: M --> R
on U: (x, y) |--> 1/(x^4 + y^4 + 2*(x^2 + 1)*y^2 + 2*x^2 + 1)
on V: (u, v) |--> (u^4 + 2*u^2*v^2 + v^4)/(u^4 + v^4 + 2*(u^2 + 1)*v^2
+ 2*u^2 + 1)
sage: s = g*h ; s
Scalar field g*h on the Open subset U of the 2-dimensional topological
manifold M
sage: s.display()
g*h: U --> R
(x, y) |--> x*y*H(x, y)
on W: (u, v) |--> u*v*H(u/(u^2 + v^2), v/(u^2 + v^2))/(u^4 + 2*u^2*v^2 + v^4)
Thanks to the coercion `C^0(M) \to C^0(U)` mentioned above,
it is possible to multiply a scalar field defined on `M` by a
scalar field defined on `U`, the result being a scalar field
defined on `U`::
sage: f.domain(), g.domain()
(2-dimensional topological manifold M,
Open subset U of the 2-dimensional topological manifold M)
sage: s = f*g ; s
Scalar field f*g on the Open subset U of the 2-dimensional topological
manifold M
sage: s.display()
f*g: U --> R
(x, y) |--> x*y/(x^2 + y^2 + 1)
on W: (u, v) |--> u*v/(u^4 + v^4 + (2*u^2 + 1)*v^2 + u^2)
sage: s == f.restrict(U)*g
True
Scalar fields can be divided (pointwise division)::
sage: s = f/c ; s
Scalar field f/c on the 2-dimensional topological manifold M
sage: s.display()
f/c: M --> R
on U: (x, y) |--> 1/(a*x^2 + a*y^2 + a)
on V: (u, v) |--> (u^2 + v^2)/(a*u^2 + a*v^2 + a)
sage: s = g/h ; s
Scalar field g/h on the Open subset U of the 2-dimensional topological
manifold M
sage: s.display()
g/h: U --> R
(x, y) |--> x*y/H(x, y)
on W: (u, v) |--> u*v/((u^4 + 2*u^2*v^2 + v^4)*H(u/(u^2 + v^2), v/(u^2 + v^2)))
sage: s = f/g ; s
Scalar field f/g on the Open subset U of the 2-dimensional topological
manifold M
sage: s.display()
f/g: U --> R
(x, y) |--> 1/(x*y^3 + (x^3 + x)*y)
on W: (u, v) |--> (u^6 + 3*u^4*v^2 + 3*u^2*v^4 + v^6)/(u*v^3 + (u^3 + u)*v)
sage: s == f.restrict(U)/g
True
For scalar fields defined on a single chart domain, we may perform some
arithmetics with symbolic expressions involving the chart coordinates::
sage: s = g + x^2 - y ; s
Scalar field on the Open subset U of the 2-dimensional topological
manifold M
sage: s.display()
U --> R
(x, y) |--> x^2 + (x - 1)*y
on W: (u, v) |--> -(v^3 - u^2 + (u^2 - u)*v)/(u^4 + 2*u^2*v^2 + v^4)
::
sage: s = g*x ; s
Scalar field on the Open subset U of the 2-dimensional topological
manifold M
sage: s.display()
U --> R
(x, y) |--> x^2*y
on W: (u, v) |--> u^2*v/(u^6 + 3*u^4*v^2 + 3*u^2*v^4 + v^6)
::
sage: s = g/x ; s
Scalar field on the Open subset U of the 2-dimensional topological
manifold M
sage: s.display()
U --> R
(x, y) |--> y
on W: (u, v) |--> v/(u^2 + v^2)
sage: s = x/g ; s
Scalar field on the Open subset U of the 2-dimensional topological
manifold M
sage: s.display()
U --> R
(x, y) |--> 1/y
on W: (u, v) |--> (u^2 + v^2)/v
.. RUBRIC:: 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 `f` as above::
sage: f = M.scalar_field({c_xy: 1/(1+x^2+y^2), c_uv: (u^2+v^2)/(1+u^2+v^2)},
....: name='f') ; f
Scalar field f on the 2-dimensional topological manifold M
sage: f.display() # notice the SymPy display of exponents
f: M --> R
on U: (x, y) |--> 1/(x**2 + y**2 + 1)
on V: (u, v) |--> (u**2 + v**2)/(u**2 + v**2 + 1)
sage: type(f.coord_function(c_xy).expr())
<class 'sympy.core.power.Pow'>
The scalar field `g` defined on `U`::
sage: g = U.scalar_field({c_xy: x*y}, name='g')
sage: g.display() # again notice the SymPy display of exponents
g: U --> R
(x, y) |--> x*y
on W: (u, v) |--> u*v/(u**4 + 2*u**2*v**2 + v**4)
Definition on a single chart and subsequent completion::
sage: f = M.scalar_field(1/(1+x^2+y^2), chart=c_xy, name='f')
sage: f.add_expr((u^2+v^2)/(1+u^2+v^2), chart=c_uv)
sage: f.display()
f: M --> R
on U: (x, y) |--> 1/(x**2 + y**2 + 1)
on V: (u, v) |--> (u**2 + v**2)/(u**2 + v**2 + 1)
Definition without any coordinate expression and subsequent completion::
sage: f = M.scalar_field(name='f')
sage: f.add_expr(1/(1+x^2+y^2), chart=c_xy)
sage: f.add_expr((u^2+v^2)/(1+u^2+v^2), chart=c_uv)
sage: f.display()
f: M --> R
on U: (x, y) |--> 1/(x**2 + y**2 + 1)
on V: (u, v) |--> (u**2 + v**2)/(u**2 + v**2 + 1)
Use of :meth:`add_expr_by_continuation`::
sage: f = M.scalar_field(1/(1+x^2+y^2), chart=c_xy, name='f')
sage: f.add_expr_by_continuation(c_uv, U.intersection(V))
sage: f.display()
f: M --> R
on U: (x, y) |--> 1/(x**2 + y**2 + 1)
on V: (u, v) |--> (u**2 + v**2)/(u**2 + v**2 + 1)
A scalar field defined by some unspecified function of the
coordinates::
sage: h = U.scalar_field(function('H')(x, y), name='h') ; h
Scalar field h on the Open subset U of the 2-dimensional topological
manifold M
sage: h.display()
h: U --> R
(x, y) |--> H(x, y)
on W: (u, v) |--> H(u/(u**2 + v**2), v/(u**2 + v**2))
The coordinate expression in a given chart is obtained via the method
:meth:`expr`, which in the present context, returns a SymPy object::
sage: f.expr(c_uv)
(u**2 + v**2)/(u**2 + v**2 + 1)
sage: type(f.expr(c_uv))
<class 'sympy.core.mul.Mul'>
The method :meth:`coord_function` returns instead a function of the
chart coordinates, i.e. an instance of
:class:`~sage.manifolds.chart_func.ChartFunction`::
sage: f.coord_function(c_uv)
(u**2 + v**2)/(u**2 + v**2 + 1)
sage: type(f.coord_function(c_uv))
<class 'sage.manifolds.chart_func.ChartFunctionRing_with_category.element_class'>
sage: f.coord_function(c_uv).display()
(u, v) |--> (u**2 + v**2)/(u**2 + v**2 + 1)
The value returned by the method :meth:`expr` is actually the coordinate
expression of the chart function::
sage: f.expr(c_uv) is f.coord_function(c_uv).expr()
True
We may ask for the ``SR`` representation of the coordinate function::
sage: f.coord_function(c_uv).expr('SR')
(u^2 + v^2)/(u^2 + v^2 + 1)
A constant scalar field with SymPy representation::
sage: c = M.constant_scalar_field(2, name='c')
sage: c.display()
c: M --> R
on U: (x, y) |--> 2
on V: (u, v) |--> 2
sage: type(c.expr(c_xy))
<class 'sympy.core.numbers.Integer'>
The constant value can be some unspecified parameter::
sage: var('a')
a
sage: c = M.constant_scalar_field(a, name='c')
sage: c.display()
c: M --> R
on U: (x, y) |--> a
on V: (u, v) |--> a
sage: type(c.expr(c_xy))
<class 'sympy.core.symbol.Symbol'>
The zero scalar field::
sage: zer = M.constant_scalar_field(0) ; zer
Scalar field zero on the 2-dimensional topological manifold M
sage: zer.display()
zero: M --> R
on U: (x, y) |--> 0
on V: (u, v) |--> 0
sage: type(zer.expr(c_xy))
<class 'sympy.core.numbers.Zero'>
sage: zer is M.zero_scalar_field()
True
Action of scalar fields on manifold's points::
sage: N = M.point((0,0), chart=c_uv) # the North pole
sage: S = M.point((0,0), chart=c_xy) # the South pole
sage: E = M.point((1,0), chart=c_xy) # a point at the equator
sage: f(N)
0
sage: f(S)
1
sage: f(E)
1/2
sage: h(E)
H(1, 0)
sage: c(E)
a
sage: zer(E)
0
A scalar field can be compared to another scalar field::
sage: f == g
False
...to a symbolic expression::
sage: f == x*y
False
sage: g == x*y
True
sage: c == a
True
...to a number::
sage: f == 2
False
sage: zer == 0
True
...to anything else::
sage: f == M
False
Standard mathematical functions are implemented::
sage: sqrt(f)
Scalar field sqrt(f) on the 2-dimensional topological manifold M
sage: sqrt(f).display()
sqrt(f): M --> R
on U: (x, y) |--> 1/sqrt(x**2 + y**2 + 1)
on V: (u, v) |--> sqrt(u**2 + v**2)/sqrt(u**2 + v**2 + 1)
::
sage: tan(f)
Scalar field tan(f) on the 2-dimensional topological manifold M
sage: tan(f).display()
tan(f): M --> R
on U: (x, y) |--> tan(1/(x**2 + y**2 + 1))
on V: (u, v) |--> tan((u**2 + v**2)/(u**2 + v**2 + 1))
.. RUBRIC:: Arithmetics of scalar fields with SymPy
Scalar fields on `M` (resp. `U`) belong to the algebra `C^0(M)`
(resp. `C^0(U)`)::
sage: f.parent()
Algebra of scalar fields on the 2-dimensional topological manifold M
sage: f.parent() is M.scalar_field_algebra()
True
sage: g.parent()
Algebra of scalar fields on the Open subset U of the 2-dimensional
topological manifold M
sage: g.parent() is U.scalar_field_algebra()
True
Consequently, scalar fields can be added::
sage: s = f + c ; s
Scalar field f+c on the 2-dimensional topological manifold M
sage: s.display()
f+c: M --> R
on U: (x, y) |--> (a*x**2 + a*y**2 + a + 1)/(x**2 + y**2 + 1)
on V: (u, v) |--> (a*u**2 + a*v**2 + a + u**2 + v**2)/(u**2 + v**2 + 1)
and subtracted::
sage: s = f - c ; s
Scalar field f-c on the 2-dimensional topological manifold M
sage: s.display()
f-c: M --> R
on U: (x, y) |--> (-a*x**2 - a*y**2 - a + 1)/(x**2 + y**2 + 1)
on V: (u, v) |--> (-a*u**2 - a*v**2 - a + u**2 + v**2)/(u**2 + v**2 + 1)
Some tests::
sage: f + zer == f
True
sage: f - f == zer
True
sage: f + (-f) == zer
True
sage: (f+c)-f == c
True
sage: (f-c)+c == f
True
We may add a number (interpreted as a constant scalar field) to a scalar
field::
sage: s = f + 1 ; s
Scalar field f+1 on the 2-dimensional topological manifold M
sage: s.display()
f+1: M --> R
on U: (x, y) |--> (x**2 + y**2 + 2)/(x**2 + y**2 + 1)
on V: (u, v) |--> (2*u**2 + 2*v**2 + 1)/(u**2 + v**2 + 1)
sage: (f+1)-1 == f
True
The number can represented by a symbolic variable::
sage: s = a + f ; s
Scalar field on the 2-dimensional topological manifold M
sage: s == c + f
True
However if the symbolic variable is a chart coordinate, the addition
is performed only on the chart domain::
sage: s = f + x; s
Scalar field on the 2-dimensional topological manifold M
sage: s.display()
M --> R
on U: (x, y) |--> (x**3 + x*y**2 + x + 1)/(x**2 + y**2 + 1)
on W: (u, v) |--> (u**4 + u**3 + 2*u**2*v**2 + u*v**2 + u + v**4)/(u**4 + 2*u**2*v**2 + u**2 + v**4 + v**2)
sage: s = f + u; s
Scalar field on the 2-dimensional topological manifold M
sage: s.display()
M --> R
on W: (x, y) |--> (x**3 + x**2 + x*y**2 + x + y**2)/(x**4 + 2*x**2*y**2 + x**2 + y**4 + y**2)
on V: (u, v) |--> (u**3 + u**2 + u*v**2 + u + v**2)/(u**2 + v**2 + 1)
The addition of two scalar fields with different domains is possible if
the domain of one of them is a subset of the domain of the other; the
domain of the result is then this subset::
sage: f.domain()
2-dimensional topological manifold M
sage: g.domain()
Open subset U of the 2-dimensional topological manifold M
sage: s = f + g ; s
Scalar field f+g on the Open subset U of the 2-dimensional topological
manifold M
sage: s.domain()
Open subset U of the 2-dimensional topological manifold M
sage: s.display()
f+g: U --> R
(x, y) |--> (x**3*y + x*y**3 + x*y + 1)/(x**2 + y**2 + 1)
on W: (u, v) |--> (u**6 + 3*u**4*v**2 + u**3*v + 3*u**2*v**4 + u*v**3 + u*v + v**6)/(u**6 + 3*u**4*v**2 + u**4 + 3*u**2*v**4 + 2*u**2*v**2 + v**6 + v**4)
The operation actually performed is `f|_U + g`::
sage: s == f.restrict(U) + g
True
Since the algebra `C^0(M)` is a vector space over `\RR`, scalar fields
can be multiplied by a number, either an explicit one::
sage: s = 2*f ; s
Scalar field on the 2-dimensional topological manifold M
sage: s.display()
M --> R
on U: (x, y) |--> 2/(x**2 + y**2 + 1)
on V: (u, v) |--> 2*(u**2 + v**2)/(u**2 + v**2 + 1)
or a symbolic one::
sage: s = a*f ; s
Scalar field on the 2-dimensional topological manifold M
sage: s.display()
M --> R
on U: (x, y) |--> a/(x**2 + y**2 + 1)
on V: (u, v) |--> a*(u**2 + v**2)/(u**2 + v**2 + 1)
However, if the symbolic variable is a chart coordinate, the
multiplication is performed only in the corresponding chart::
sage: s = x*f; s
Scalar field on the 2-dimensional topological manifold M
sage: s.display()
M --> R
on U: (x, y) |--> x/(x**2 + y**2 + 1)
on W: (u, v) |--> u/(u**2 + v**2 + 1)
sage: s = u*f; s
Scalar field on the 2-dimensional topological manifold M
sage: s.display()
M --> R
on W: (x, y) |--> x/(x**4 + 2*x**2*y**2 + x**2 + y**4 + y**2)
on V: (u, v) |--> u*(u**2 + v**2)/(u**2 + v**2 + 1)
Some tests::
sage: 0*f == 0
True
sage: 0*f == zer
True
sage: 1*f == f
True
sage: (-2)*f == - f - f
True
The ring multiplication of the algebras `C^0(M)` and `C^0(U)`
is the pointwise multiplication of functions::
sage: s = f*f ; s
Scalar field f*f on the 2-dimensional topological manifold M