Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commit 5bcc7af

Browse files
author
Michael Jung
committed
Alternative solution via unsafe method
1 parent 1b1ee7f commit 5bcc7af

14 files changed

+473
-62
lines changed

src/sage/manifolds/differentiable/automorphismfield.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ class :class:`~sage.tensor.modules.comp.Components`; if such
317317
if self._is_identity:
318318
raise AssertionError("the components of the identity map cannot be "
319319
"changed")
320-
return TensorField.set_comp(self, basis=basis, check_elements=False)
320+
return TensorField._set_comp_unsafe(self, basis=basis)
321321

322322
def add_comp(self, basis=None, **kwargs):
323323
r"""
@@ -391,7 +391,7 @@ class :class:`~sage.tensor.modules.comp.Components`;
391391
if self._is_identity:
392392
raise AssertionError("the components of the identity map cannot be "
393393
"changed")
394-
return TensorField.add_comp(self, basis=basis, check_elements=False)
394+
return TensorField._add_comp_unsafe(self, basis=basis)
395395

396396
def _new_instance(self):
397397
r"""

src/sage/manifolds/differentiable/diff_form_module.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ def zero(self):
446446
zero = self._element_constructor_(name='zero', latex_name='0')
447447
for frame in self._domain._frames:
448448
if self._dest_map.restrict(frame._domain) == frame._dest_map:
449-
zero.add_comp(frame, check_elements=False)
449+
zero._add_comp_unsafe(frame)
450450
# (since new components are initialized to zero)
451451
zero._is_zero = True # This element is certainly zero
452452
return zero

src/sage/manifolds/differentiable/multivector_module.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ def zero(self):
395395
zero = self._element_constructor_(name='zero', latex_name='0')
396396
for frame in self._domain._frames:
397397
if self._dest_map.restrict(frame._domain) == frame._dest_map:
398-
zero.add_comp(frame, check_elements=False)
398+
zero._add_comp_unsafe(frame)
399399
# (since new components are initialized to zero)
400400
return zero
401401

src/sage/manifolds/differentiable/tensorfield.py

Lines changed: 137 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,71 @@ def restrict(self, subdomain, dest_map=None):
10771077

10781078
return self._restrictions[subdomain]
10791079

1080-
def set_comp(self, basis=None, **kwargs):
1080+
def _set_comp_unsafe(self, basis=None):
1081+
r"""
1082+
Return the components of ``self`` in a given vector frame
1083+
for assignment. This private method invokes no security check. Use
1084+
this method at your own risk.
1085+
1086+
The components with respect to other frames having the same domain
1087+
as the provided vector frame are deleted, in order to avoid any
1088+
inconsistency. To keep them, use the method :meth:`_add_comp_unsafe`
1089+
instead.
1090+
1091+
INPUT:
1092+
1093+
- ``basis`` -- (default: ``None``) vector frame in which the
1094+
components are defined; if none is provided, the components are
1095+
assumed to refer to the tensor field domain's default frame
1096+
1097+
OUTPUT:
1098+
1099+
- components in the given frame, as a
1100+
:class:`~sage.tensor.modules.comp.Components`; if such
1101+
components did not exist previously, they are created
1102+
1103+
TESTS::
1104+
1105+
sage: M = Manifold(2, 'M') # the 2-dimensional sphere S^2
1106+
sage: U = M.open_subset('U') # complement of the North pole
1107+
sage: c_xy.<x,y> = U.chart() # stereographic coordinates from the North pole
1108+
sage: V = M.open_subset('V') # complement of the South pole
1109+
sage: c_uv.<u,v> = V.chart() # stereographic coordinates from the South pole
1110+
sage: M.declare_union(U,V) # S^2 is the union of U and V
1111+
sage: e_uv = c_uv.frame()
1112+
sage: t = M.tensor_field(1, 2, name='t')
1113+
sage: t._set_comp_unsafe(e_uv)
1114+
3-indices components w.r.t. Coordinate frame (V, (d/du,d/dv))
1115+
sage: t._set_comp_unsafe(e_uv)[1,0,1] = u+v
1116+
sage: t.display(e_uv)
1117+
t = (u + v) d/dv*du*dv
1118+
1119+
Setting the components in a new frame (``e``)::
1120+
1121+
sage: e = V.vector_frame('e')
1122+
sage: t._set_comp_unsafe(e)
1123+
3-indices components w.r.t. Vector frame (V, (e_0,e_1))
1124+
sage: t._set_comp_unsafe(e)[0,1,1] = u*v
1125+
sage: t.display(e)
1126+
t = u*v e_0*e^1*e^1
1127+
1128+
Since the frames ``e`` and ``e_uv`` are defined on the same domain, the
1129+
components w.r.t. ``e_uv`` have been erased::
1130+
1131+
sage: t.display(c_uv.frame())
1132+
Traceback (most recent call last):
1133+
...
1134+
ValueError: no basis could be found for computing the components
1135+
in the Coordinate frame (V, (d/du,d/dv))
1136+
1137+
"""
1138+
if basis is None:
1139+
basis = self._domain._def_frame
1140+
self._del_derived() # deletes the derived quantities
1141+
rst = self.restrict(basis._domain, dest_map=basis._dest_map)
1142+
return rst._set_comp_unsafe(basis)
1143+
1144+
def set_comp(self, basis=None):
10811145
r"""
10821146
Return the components of ``self`` in a given vector frame
10831147
for assignment.
@@ -1141,19 +1205,76 @@ def set_comp(self, basis=None, **kwargs):
11411205
AssertionError: the components of the zero element cannot be changed
11421206
11431207
"""
1144-
check_elements = kwargs.pop('check_elements', True)
1145-
if check_elements:
1146-
if self is self.parent().zero():
1147-
raise AssertionError("the components of the zero element "
1148-
"cannot be changed")
1149-
self._is_zero = False # a priori
1208+
if self is self.parent().zero():
1209+
raise AssertionError("the components of the zero element "
1210+
"cannot be changed")
1211+
self._is_zero = False # a priori
11501212
if basis is None:
11511213
basis = self._domain._def_frame
11521214
self._del_derived() # deletes the derived quantities
11531215
rst = self.restrict(basis._domain, dest_map=basis._dest_map)
1154-
return rst.set_comp(basis, **kwargs)
1216+
return rst.set_comp(basis)
11551217

1156-
def add_comp(self, basis=None, **kwargs):
1218+
def _add_comp_unsafe(self, basis=None):
1219+
r"""
1220+
Return the components of ``self`` in a given vector frame
1221+
for assignment. This private method invokes no security check. Use
1222+
this method at your own risk.
1223+
1224+
The components with respect to other frames having the same domain
1225+
as the provided vector frame are kept. To delete them, use the
1226+
method :meth:`_set_comp_unsafe` instead.
1227+
1228+
INPUT:
1229+
1230+
- ``basis`` -- (default: ``None``) vector frame in which the
1231+
components are defined; if ``None``, the components are assumed
1232+
to refer to the tensor field domain's default frame
1233+
1234+
OUTPUT:
1235+
1236+
- components in the given frame, as a
1237+
:class:`~sage.tensor.modules.comp.Components`; if such
1238+
components did not exist previously, they are created
1239+
1240+
TESTS::
1241+
1242+
sage: M = Manifold(2, 'M') # the 2-dimensional sphere S^2
1243+
sage: U = M.open_subset('U') # complement of the North pole
1244+
sage: c_xy.<x,y> = U.chart() # stereographic coordinates from the North pole
1245+
sage: V = M.open_subset('V') # complement of the South pole
1246+
sage: c_uv.<u,v> = V.chart() # stereographic coordinates from the South pole
1247+
sage: M.declare_union(U,V) # S^2 is the union of U and V
1248+
sage: e_uv = c_uv.frame()
1249+
sage: t = M.tensor_field(1, 2, name='t')
1250+
sage: t._add_comp_unsafe(e_uv)
1251+
3-indices components w.r.t. Coordinate frame (V, (d/du,d/dv))
1252+
sage: t._add_comp_unsafe(e_uv)[1,0,1] = u+v
1253+
sage: t.display(e_uv)
1254+
t = (u + v) d/dv*du*dv
1255+
1256+
Setting the components in a new frame::
1257+
1258+
sage: e = V.vector_frame('e')
1259+
sage: t._add_comp_unsafe(e)
1260+
3-indices components w.r.t. Vector frame (V, (e_0,e_1))
1261+
sage: t._add_comp_unsafe(e)[0,1,1] = u*v
1262+
sage: t.display(e)
1263+
t = u*v e_0*e^1*e^1
1264+
1265+
The components with respect to ``e_uv`` are kept::
1266+
1267+
sage: t.display(e_uv)
1268+
t = (u + v) d/dv*du*dv
1269+
1270+
"""
1271+
if basis is None:
1272+
basis = self._domain._def_frame
1273+
self._del_derived() # deletes the derived quantities
1274+
rst = self.restrict(basis._domain, dest_map=basis._dest_map)
1275+
return rst._add_comp_unsafe(basis)
1276+
1277+
def add_comp(self, basis=None):
11571278
r"""
11581279
Return the components of ``self`` in a given vector frame
11591280
for assignment.
@@ -1213,17 +1334,15 @@ def add_comp(self, basis=None, **kwargs):
12131334
AssertionError: the components of the zero element cannot be changed
12141335
12151336
"""
1216-
check_elements = kwargs.pop('check_elements', True)
1217-
if check_elements:
1218-
if self is self.parent().zero():
1219-
raise AssertionError("the components of the zero element "
1220-
"cannot be changed")
1221-
self._is_zero = False # a priori
1337+
if self is self.parent().zero():
1338+
raise AssertionError("the components of the zero element "
1339+
"cannot be changed")
1340+
self._is_zero = False # a priori
12221341
if basis is None:
12231342
basis = self._domain._def_frame
12241343
self._del_derived() # deletes the derived quantities
12251344
rst = self.restrict(basis._domain, dest_map=basis._dest_map)
1226-
return rst.add_comp(basis, **kwargs)
1345+
return rst.add_comp(basis)
12271346

12281347
def add_comp_by_continuation(self, frame, subdomain, chart=None):
12291348
r"""
@@ -1297,8 +1416,7 @@ def add_comp_by_continuation(self, frame, subdomain, chart=None):
12971416
sframe = frame.restrict(subdomain)
12981417
schart = chart.restrict(subdomain)
12991418
scomp = self.comp(sframe)
1300-
resu = self.add_comp(frame, check_elements=False) # _del_derived is
1301-
# performed here
1419+
resu = self._add_comp_unsafe(frame) # _del_derived is performed here
13021420
for ind in resu.non_redundant_index_generator():
13031421
resu[[ind]] = dom.scalar_field({chart: scomp[[ind]].expr(schart)})
13041422

@@ -1392,7 +1510,7 @@ def add_expr_from_subdomain(self, frame, subdomain):
13921510
if frame not in self.restrict(frame.domain())._components:
13931511
raise ValueError("the tensor doesn't have an expression in "
13941512
"the frame"+frame._repr_())
1395-
comp = self.add_comp(frame, check_elements=False)
1513+
comp = self._add_comp_unsafe(frame)
13961514
scomp = self.restrict(subdomain).comp(frame.restrict(subdomain))
13971515
for ind in comp.non_redundant_index_generator():
13981516
comp[[ind]]._express.update(scomp[[ind]]._express)

src/sage/manifolds/differentiable/tensorfield_module.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ def zero(self):
575575
resu = self._element_constructor_(name='zero', latex_name='0')
576576
for frame in self._domain._frames:
577577
if self._dest_map.restrict(frame._domain) == frame._dest_map:
578-
resu.add_comp(frame, check_elements=False)
578+
resu._add_comp_unsafe(frame)
579579
# (since new components are initialized to zero)
580580
resu._is_zero = True # This element is certainly zero
581581
return resu

0 commit comments

Comments
 (0)