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

Commit

Permalink
Trac #30266: hash function condition + treatment of restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Jung committed Aug 5, 2020
1 parent 4f07938 commit 3faa75a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
33 changes: 31 additions & 2 deletions src/sage/manifolds/scalarfield.py
Expand Up @@ -2180,6 +2180,8 @@ def restrict(self, subdomain):
resu = type(self)(subdomain.scalar_field_algebra(),
coord_expression=sexpress, name=self._name,
latex_name=self._latex_name)
if self.is_immutable():
resu.set_immutable() # restriction must be immutable, too
self._restrictions[subdomain] = resu
return self._restrictions[subdomain]

Expand Down Expand Up @@ -3516,6 +3518,26 @@ def set_calc_order(self, symbol, order, truncate=False):
expr.simplify()
self._del_derived()

def set_immutable(self):
r"""
Set ``self`` and all restrictions of ``self`` immutable.
EXAMPLES::
sage: M = Manifold(2, 'M')
sage: X.<x,y> = M.chart()
sage: U = M.open_subset('U', coord_def={X: x^2+y^2<1})
sage: f = M.scalar_field(x^2, name='f')
sage: fU = f.restrict(U)
sage: f.set_immutable()
sage: fU.is_immutable()
True
"""
for rst in self._restrictions.values():
rst.set_immutable()
super().set_immutable()

@cached_method
def __hash__(self):
r"""
Expand All @@ -3527,7 +3549,14 @@ def __hash__(self):
sage: X.<x,y> = M.chart()
sage: f = M.scalar_field(x^2, name='f')
sage: f.set_immutable()
sage: hash(f) == f.__hash__()
sage: g = M.scalar_field(x^2, name='f')
sage: g.set_immutable()
Check whether equality implies equality of hash::
sage: f == g
True
sage: hash(f) == hash(g)
True
Let us check that ``f`` can be used as a dictionary key::
Expand All @@ -3539,4 +3568,4 @@ def __hash__(self):
if self.is_mutable():
raise ValueError('element must be immutable in order to be '
'hashable')
return hash((self._domain, repr(self)))
return hash(self._manifold)
7 changes: 4 additions & 3 deletions src/sage/tensor/modules/free_module_tensor.py
Expand Up @@ -1507,7 +1507,8 @@ def del_other_comp(self, basis=None):
[Basis (e_1,e_2,e_3) on the Rank-3 free module M over the Integer Ring]
"""
if basis is None: basis = self._fmodule._def_basis
if basis is None:
basis = self._fmodule._def_basis
if basis not in self._components:
raise ValueError("the components w.r.t. the {}".format(basis) +
" have not been defined")
Expand Down Expand Up @@ -2368,8 +2369,8 @@ def __call__(self, *args):
resu = 0
for i in fmodule.irange():
resu += omega[[i]]*vv[[i]]
parent = resu.parent()
if resu is not parent.zero() and resu is not parent.one():
# Rename resu only if it is mutable and has no name yet:
if resu.is_mutable() and resu._name is None:
# Name and LaTeX symbol of the output:
if hasattr(resu, '_name'):
if self._name is not None and vector._name is not None:
Expand Down

0 comments on commit 3faa75a

Please sign in to comment.