Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Merge branch 't/30310/immutability_of_chart_functions' into t/30311/i…
Browse files Browse the repository at this point in the history
…mmutability_of_scalar_fields_ii
  • Loading branch information
Michael Jung committed Aug 7, 2020
2 parents 2f72e19 + 76229bd commit 48a35b1
Showing 1 changed file with 82 additions and 6 deletions.
88 changes: 82 additions & 6 deletions src/sage/manifolds/chart_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# the License, or (at your option) any later version.
# https://www.gnu.org/licenses/
# ****************************************************************************
from sage.structure.element import AlgebraElement
from sage.structure.element import AlgebraElement, ModuleElementWithMutability
from sage.structure.parent import Parent
from sage.structure.sage_object import SageObject
from sage.structure.unique_representation import UniqueRepresentation
Expand All @@ -44,7 +44,7 @@
import sympy


class ChartFunction(AlgebraElement):
class ChartFunction(AlgebraElement, ModuleElementWithMutability):
r"""
Function of coordinates of a given chart.
Expand Down Expand Up @@ -357,7 +357,7 @@ def __init__(self, parent, expression=None, calc_method=None,
sage: TestSuite(g).run()
"""
AlgebraElement.__init__(self, parent)
super().__init__(parent)
self._chart = parent._chart
self._nc = len(self._chart[:])
self._express = {}
Expand Down Expand Up @@ -425,7 +425,6 @@ def chart(self):
"""
return self._chart


def scalar_field(self, name=None, latex_name=None):
r"""
Construct the scalar field that has ``self`` as coordinate expression.
Expand Down Expand Up @@ -597,6 +596,9 @@ def set_expr(self, calc_method, expression):
ValueError: Expressions are not equal
"""
if self.is_immutable():
raise AssertionError("the expressions of an immutable element "
"cannot be changed")
for vv in self._express.values():
if not bool(self._calc_method._tranf[calc_method](expression) ==
self._calc_method._tranf[calc_method](vv)):
Expand Down Expand Up @@ -2731,7 +2733,9 @@ def zero(self):
elt = SR.zero()
else:
elt = self._chart.manifold().base_field().zero()
return self.element_class(self, elt)
res = self.element_class(self, elt)
res.set_immutable()
return res

@cached_method
def one(self):
Expand All @@ -2755,7 +2759,9 @@ def one(self):
elt = SR.one()
else:
elt = self._chart.manifold().base_field().one()
return self.element_class(self, elt)
res = self.element_class(self, elt)
res.set_immutable()
return res

is_field = is_integral_domain

Expand Down Expand Up @@ -2868,6 +2874,7 @@ def __init__(self, chart, expressions):
self._chart = chart
self._nc = len(self._chart._xx) # number of coordinates
self._nf = len(expressions) # number of functions
self._is_immutable = False
self._functions = tuple(chart.function(express)
for express in expressions)

Expand Down Expand Up @@ -3244,3 +3251,72 @@ def jacobian_det(self):
func = self._functions[0]
return type(func)(func.parent(), func._calc_method.simplify(det, method='SR'),
calc_method=self._chart._calc_method._current)

def set_immutable(self):
r"""
Set ``self`` and all chart functions of ``self`` immutable.
EXAMPLES:
Declare a coordinate function immutable::
sage: M = Manifold(3, 'M', structure='topological')
sage: X.<x,y,z> = M.chart()
sage: f = X.multifunction(x+y+z, x*y*z)
sage: f.is_immutable()
False
sage: f.set_immutable()
sage: f.is_immutable()
True
The chart functions are now immutable, too::
sage: f[0].parent()
Ring of chart functions on Chart (M, (x, y, z))
sage: f[0].is_immutable()
True
"""
for func in self._functions:
func.set_immutable()
self._is_immutable = True

def is_immutable(self):
r"""
Return ``True`` if this object is immutable, i.e. its expressions
cannot be chanced, and ``False`` if it is not.
To set a coordinate function immutable, use :meth:`set_immutable`.
EXAMPLES::
sage: M = Manifold(3, 'M', structure='topological')
sage: X.<x,y,z> = M.chart()
sage: f = X.multifunction(x+y+z, x*y*z)
sage: f.is_immutable()
False
sage: f.set_immutable()
sage: f.is_immutable()
True
"""
return self._is_immutable

def is_mutable(self):
r"""
Return ``True`` if this object is mutable, i.e. its expressions can
be changed, and ``False`` if it is not.
EXAMPLES::
sage: M = Manifold(3, 'M', structure='topological')
sage: X.<x,y,z> = M.chart()
sage: f = X.multifunction(x+y+z, x*y*z)
sage: f.is_mutable()
True
sage: f.set_immutable()
sage: f.is_mutable()
False
"""
return not self._is_immutable

0 comments on commit 48a35b1

Please sign in to comment.