52
52
from sage .tensor .modules .format_utilities import FormattedExpansion
53
53
from sage .manifolds .chart import Chart
54
54
55
+
55
56
class ScalarField (CommutativeAlgebraElement , ModuleElementWithMutability ):
56
57
r"""
57
58
Scalar field on a topological manifold.
@@ -1133,15 +1134,19 @@ def __init__(self, parent, coord_expression=None, chart=None, name=None,
1133
1134
domain = parent ._domain
1134
1135
self ._domain = domain
1135
1136
self ._manifold = domain .manifold ()
1136
- self ._is_zero = False # a priori, may be changed below or via
1137
- # method __bool__()
1137
+ self ._is_zero = False
1138
+ # a priori, may be changed below or via
1139
+ # method __bool__()
1140
+
1138
1141
self ._name = name
1139
1142
if latex_name is None :
1140
1143
self ._latex_name = self ._name
1141
1144
else :
1142
1145
self ._latex_name = latex_name
1143
- self ._express = {} # dict of coordinate expressions (ChartFunction
1144
- # instances) with charts as keys
1146
+ self ._express = {}
1147
+ # dict of coordinate expressions (ChartFunction
1148
+ # instances) with charts as keys
1149
+
1145
1150
if coord_expression is not None :
1146
1151
if isinstance (coord_expression , dict ):
1147
1152
for chart , expression in coord_expression .items ():
@@ -1163,7 +1168,7 @@ def __init__(self, parent, coord_expression=None, chart=None, name=None,
1163
1168
self ._express [chart ] = chart .function (coord_expression )
1164
1169
self ._init_derived () # initialization of derived quantities
1165
1170
1166
- ####### Required methods for an algebra element (beside arithmetic) #### ###
1171
+ # ### Required methods for an algebra element (beside arithmetic) ###
1167
1172
1168
1173
def __bool__ (self ):
1169
1174
r"""
@@ -1425,11 +1430,10 @@ def __ne__(self, other):
1425
1430
sage: g = M.scalar_field({X: x+y})
1426
1431
sage: f != g
1427
1432
False
1428
-
1429
1433
"""
1430
1434
return not (self == other )
1431
1435
1432
- ####### End of required methods for an algebra element (beside arithmetic) ##### ##
1436
+ # ## End of required methods for an algebra element (beside arithmetic) ##
1433
1437
1434
1438
def _init_derived (self ):
1435
1439
r"""
@@ -1441,10 +1445,10 @@ def _init_derived(self):
1441
1445
sage: X.<x,y> = M.chart()
1442
1446
sage: f = M.scalar_field({X: x+y})
1443
1447
sage: f._init_derived()
1444
-
1445
1448
"""
1446
- self ._restrictions = {} # dict. of restrictions of self on subsets
1447
- # of self._domain, with the subsets as keys
1449
+ self ._restrictions = {}
1450
+ # dict. of restrictions of self on subsets
1451
+ # of self._domain, with the subsets as keys
1448
1452
1449
1453
def _del_derived (self ):
1450
1454
r"""
@@ -1466,7 +1470,6 @@ def _del_derived(self):
1466
1470
sage: f._del_derived()
1467
1471
sage: f._restrictions # restrictions are derived quantities
1468
1472
{}
1469
-
1470
1473
"""
1471
1474
self ._restrictions .clear ()
1472
1475
@@ -1486,7 +1489,6 @@ def _repr_(self):
1486
1489
'Scalar field f on the 2-dimensional topological manifold M'
1487
1490
sage: f
1488
1491
Scalar field f on the 2-dimensional topological manifold M
1489
-
1490
1492
"""
1491
1493
description = "Scalar field"
1492
1494
if self ._name is not None :
@@ -1513,12 +1515,10 @@ def _latex_(self):
1513
1515
'\\Phi'
1514
1516
sage: latex(f)
1515
1517
\Phi
1516
-
1517
1518
"""
1518
1519
if self ._latex_name is None :
1519
1520
return r'\text{' + str (self ) + r'}'
1520
- else :
1521
- return self ._latex_name
1521
+ return self ._latex_name
1522
1522
1523
1523
def set_name (self , name = None , latex_name = None ):
1524
1524
r"""
@@ -1551,7 +1551,7 @@ def set_name(self, name=None, latex_name=None):
1551
1551
"""
1552
1552
if self .is_immutable ():
1553
1553
raise ValueError ("the name of an immutable element "
1554
- "cannot be changed" )
1554
+ "cannot be changed" )
1555
1555
if name is not None :
1556
1556
self ._name = name
1557
1557
if latex_name is None :
@@ -1787,7 +1787,7 @@ def coord_function(self, chart=None, from_chart=None):
1787
1787
found = True
1788
1788
if skchart not in self ._express :
1789
1789
self ._express [skchart ] = skchart .function (
1790
- self ._express [kchart ].expr ())
1790
+ self ._express [kchart ].expr ())
1791
1791
break
1792
1792
if found :
1793
1793
break
@@ -1796,8 +1796,8 @@ def coord_function(self, chart=None, from_chart=None):
1796
1796
"compute the expression in the {}" .format (chart ))
1797
1797
change = self ._domain ._coord_changes [(chart , from_chart )]
1798
1798
# old coordinates expressed in terms of the new ones:
1799
- coords = [ change ._transf ._functions [i ].expr ()
1800
- for i in range (self ._manifold .dim ()) ]
1799
+ coords = [change ._transf ._functions [i ].expr ()
1800
+ for i in range (self ._manifold .dim ())]
1801
1801
new_expr = self ._express [from_chart ](* coords )
1802
1802
self ._express [chart ] = chart .function (new_expr )
1803
1803
self ._del_derived ()
@@ -1921,7 +1921,7 @@ def set_expr(self, coord_expression, chart=None):
1921
1921
chart = self ._domain ._def_chart
1922
1922
self ._express .clear ()
1923
1923
self ._express [chart ] = chart .function (coord_expression )
1924
- self ._is_zero = False # a priori
1924
+ self ._is_zero = False # a priori
1925
1925
self ._del_derived ()
1926
1926
1927
1927
def add_expr (self , coord_expression , chart = None ):
@@ -2050,13 +2050,13 @@ def add_expr_by_continuation(self, chart, subdomain):
2050
2050
"""
2051
2051
if self .is_immutable ():
2052
2052
raise ValueError ("the expressions of an immutable element "
2053
- "cannot be changed" )
2053
+ "cannot be changed" )
2054
2054
if not chart .domain ().is_subset (self ._domain ):
2055
2055
raise ValueError ("the chart is not defined on a subset of " +
2056
2056
"the scalar field domain" )
2057
2057
schart = chart .restrict (subdomain )
2058
2058
self ._express [chart ] = chart .function (self .expr (schart ))
2059
- self ._is_zero = False # a priori
2059
+ self ._is_zero = False # a priori
2060
2060
self ._del_derived ()
2061
2061
2062
2062
def set_restriction (self , rst ):
@@ -2101,7 +2101,7 @@ def set_restriction(self, rst):
2101
2101
self ._express [chart .restrict (intersection )] = expr
2102
2102
self ._is_zero = False # a priori
2103
2103
2104
- def display (self , chart : Optional [Chart ]= None ) -> FormattedExpansion :
2104
+ def display (self , chart : Optional [Chart ] = None ) -> FormattedExpansion :
2105
2105
r"""
2106
2106
Display the expression of the scalar field in a given chart.
2107
2107
@@ -2155,11 +2155,12 @@ def display(self, chart: Optional[Chart]=None) -> FormattedExpansion:
2155
2155
on U: (x, y) ↦ y^2
2156
2156
sage: latex(f.display())
2157
2157
\begin{array}{llcl} f:& M & \longrightarrow & \mathbb{R} \\ \text{on}\ U : & \left(x, y\right) & \longmapsto & y^{2} \end{array}
2158
-
2159
2158
"""
2160
2159
from sage .misc .latex import latex
2161
2160
from sage .typeset .unicode_characters import (unicode_to ,
2162
- unicode_mapsto , unicode_mathbbR , unicode_mathbbC )
2161
+ unicode_mapsto ,
2162
+ unicode_mathbbR ,
2163
+ unicode_mathbbC )
2163
2164
from sage .tensor .modules .format_utilities import FormattedExpansion
2164
2165
2165
2166
def _display_expression (self , chart , result ):
@@ -2184,9 +2185,9 @@ def _display_expression(self, chart, result):
2184
2185
result ._latex += r"\text{on}\ " + latex (chart .domain ()) \
2185
2186
+ r": & "
2186
2187
result ._txt += repr (coords ) + " " + unicode_mapsto + " " \
2187
- + repr (expression ) + "\n "
2188
+ + repr (expression ) + "\n "
2188
2189
result ._latex += latex (coords ) + r"& \longmapsto & " \
2189
- + latex (expression ) + r"\\"
2190
+ + latex (expression ) + r"\\"
2190
2191
2191
2192
# Name of the base field:
2192
2193
field = self ._domain .base_field ()
@@ -2207,7 +2208,7 @@ def _display_expression(self, chart, result):
2207
2208
else :
2208
2209
symbol = self ._name + ": "
2209
2210
result ._txt = symbol + self ._domain ._name + " " + unicode_to + " " \
2210
- + field_name + "\n "
2211
+ + field_name + "\n "
2211
2212
if self ._latex_name is None :
2212
2213
symbol = ""
2213
2214
else :
@@ -2311,7 +2312,7 @@ def restrict(self, subdomain):
2311
2312
self ._restrictions [subdomain ] = rst .restrict (subdomain )
2312
2313
break
2313
2314
else :
2314
- # If this fails, the restriction must be created from scratch:
2315
+ # If this fails, the restriction must be created from scratch:
2315
2316
sexpress = {}
2316
2317
for chart , funct in self ._express .items ():
2317
2318
for schart in subdomain .atlas ():
@@ -2515,7 +2516,7 @@ def __call__(self, p, chart=None):
2515
2516
(-3, 7)
2516
2517
2517
2518
"""
2518
- #! # it should be "if p not in self_domain:" instead, but this test is
2519
+ # ! # it should be "if p not in self_domain:" instead, but this test is
2519
2520
# skipped for efficiency
2520
2521
if p not in self ._manifold :
2521
2522
raise ValueError ("the {} " .format (p ) + "does not belong " +
@@ -2657,7 +2658,7 @@ def __neg__(self):
2657
2658
result ._latex_name = '-' + self ._latex_name
2658
2659
return result
2659
2660
2660
- ######### CommutativeAlgebraElement arithmetic operators ##### ###
2661
+ # ### CommutativeAlgebraElement arithmetic operators ###
2661
2662
2662
2663
def _add_ (self , other ):
2663
2664
r"""
@@ -2810,7 +2811,7 @@ def _mul_(self, other):
2810
2811
result ._express [chart ] = self ._express [chart ] * other ._express [chart ]
2811
2812
result ._name = format_mul_txt (self ._name , '*' , other ._name )
2812
2813
result ._latex_name = format_mul_latex (self ._latex_name , r' \cdot ' ,
2813
- other ._latex_name )
2814
+ other ._latex_name )
2814
2815
return result
2815
2816
2816
2817
def _div_ (self , other ):
@@ -2843,10 +2844,9 @@ def _div_(self, other):
2843
2844
Traceback (most recent call last):
2844
2845
...
2845
2846
ZeroDivisionError: division of a scalar field by zero
2846
-
2847
2847
"""
2848
2848
from sage .tensor .modules .format_utilities import format_mul_txt , \
2849
- format_mul_latex
2849
+ format_mul_latex
2850
2850
# Trivial cases:
2851
2851
if other .is_trivial_zero ():
2852
2852
raise ZeroDivisionError ("division of a scalar field by zero" )
@@ -2862,7 +2862,7 @@ def _div_(self, other):
2862
2862
result ._express [chart ] = self ._express [chart ] / other ._express [chart ]
2863
2863
result ._name = format_mul_txt (self ._name , '/' , other ._name )
2864
2864
result ._latex_name = format_mul_latex (self ._latex_name , '/' ,
2865
- other ._latex_name )
2865
+ other ._latex_name )
2866
2866
return result
2867
2867
2868
2868
def _lmul_ (self , number ):
@@ -2966,7 +2966,7 @@ def _lmul_(self, number):
2966
2966
result ._express [chart ] = number * expr
2967
2967
return result
2968
2968
2969
- ######### End of CommutativeAlgebraElement arithmetic operators ##### ###
2969
+ # ### End of CommutativeAlgebraElement arithmetic operators ###
2970
2970
2971
2971
def _function_name (self , func , func_latex , parentheses = True ):
2972
2972
r"""
@@ -2985,20 +2985,17 @@ def _function_name(self, func, func_latex, parentheses=True):
2985
2985
sage: f = M.scalar_field({X: x+y}) # no name given to f
2986
2986
sage: f._function_name("cos", r"\cos")
2987
2987
(None, None)
2988
-
2989
2988
"""
2990
2989
if self ._name is None :
2991
2990
name = None
2992
2991
else :
2993
2992
name = func + "(" + self ._name + ")"
2994
2993
if self ._latex_name is None :
2995
2994
latex_name = None
2995
+ elif parentheses :
2996
+ latex_name = func_latex + r"\left(" + self ._latex_name + r"\right)"
2996
2997
else :
2997
- if parentheses :
2998
- latex_name = func_latex + r"\left(" + self ._latex_name + \
2999
- r"\right)"
3000
- else :
3001
- latex_name = func_latex + r"{" + self ._latex_name + r"}"
2998
+ latex_name = func_latex + r"{" + self ._latex_name + r"}"
3002
2999
return name , latex_name
3003
3000
3004
3001
def exp (self ):
0 commit comments