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

Commit

Permalink
Chart: WIP WithEqualityById instead of UniqueRepresentation; disambig…
Browse files Browse the repository at this point in the history
…uate _restrictions -> _coordinate_restrictions
  • Loading branch information
Matthias Koeppe committed Jun 24, 2021
1 parent a60179a commit 5215e93
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions src/sage/manifolds/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,19 @@
# ****************************************************************************

from sage.structure.sage_object import SageObject
from sage.structure.unique_representation import UniqueRepresentation
from sage.structure.mutability import Mutability
from sage.symbolic.ring import SR
from sage.rings.infinity import Infinity
from sage.misc.latex import latex
from sage.misc.decorators import options
from sage.misc.fast_methods import WithEqualityById
from sage.manifolds.chart_func import ChartFunctionRing
from sage.manifolds.calculus_method import CalculusMethod
from sage.symbolic.expression import Expression
from sage.ext.fast_callable import fast_callable


class Chart(UniqueRepresentation, SageObject):
class Chart(WithEqualityById, Mutability, SageObject):
r"""
Chart on a topological manifold.
Expand Down Expand Up @@ -315,7 +316,7 @@ def __init__(self, domain, coordinates='', names=None, calc_method=None):
self._domain._charts_by_coord[coord_string] = self
#
# Additional restrictions on the coordinates
self._restrictions = [] # to be set with method add_restrictions()
self._coord_restrictions = [] # to be set with method add_restrictions()
#
# The chart is added to the domain's atlas, as well as to all the
# atlases of the domain's supersets; moreover the first defined chart
Expand Down Expand Up @@ -658,10 +659,12 @@ def add_restrictions(self, restrictions):
False
"""
if self.is_immutable():
raise ValueError("cannot add restrictions to a chart once it has been used or set immutable")
if not isinstance(restrictions, list):
# case of a single condition or conditions to be combined by "or"
restrictions = [restrictions]
self._restrictions.extend(restrictions)
self._coord_restrictions.extend(restrictions)

def restrict(self, subset, restrictions=None):
r"""
Expand Down Expand Up @@ -723,11 +726,12 @@ def restrict(self, subset, restrictions=None):
coordinates += repr(coord) + ' '
res = type(self)(subset, coordinates,
calc_method=self._calc_method._current)
res._restrictions.extend(self._restrictions)
res._coord_restrictions.extend(self._coord_restrictions)
# The coordinate restrictions are added to the result chart and
# possibly transformed into coordinate bounds:
if restrictions is not None:
res.add_restrictions(restrictions)
res.set_immutable()
# Update of supercharts and subcharts:
res._supercharts.update(self._supercharts)
for schart in self._supercharts:
Expand Down Expand Up @@ -791,11 +795,11 @@ def valid_coordinates(self, *coordinates, **kwds):
else:
parameters = None
# Check of restrictions:
if self._restrictions:
if self._coord_restrictions:
substitutions = dict(zip(self._xx, coordinates))
if parameters:
substitutions.update(parameters)
return self._check_restrictions(self._restrictions, substitutions)
return self._check_restrictions(self._coord_restrictions, substitutions)
return True

def _check_restrictions(self, restrict, substitutions):
Expand Down Expand Up @@ -839,6 +843,7 @@ def _check_restrictions(self, restrict, substitutions):
False
"""
self.set_immutable()
if isinstance(restrict, tuple): # case of 'or' conditions
combine = False
for cond in restrict:
Expand Down Expand Up @@ -1886,7 +1891,7 @@ def add_restrictions(self, restrictions):
sage: A = M.open_subset('A') # annulus 1/2 < r < 1
sage: X_A = X.restrict(A, x^2+y^2 > 1/4)
sage: X_A._restrictions
sage: X_A._coord_restrictions
[x^2 + y^2 < 1, x^2 + y^2 > (1/4)]
sage: X_A.valid_coordinates(0,1/3)
False
Expand All @@ -1906,14 +1911,16 @@ def add_restrictions(self, restrictions):
"""
import operator
if self.is_immutable():
raise ValueError("cannot add restrictions to a chart once it has been used or set immutable")
if not isinstance(restrictions, list):
# case of a single condition or conditions to be combined by "or"
restrictions = [restrictions]
self._restrictions.extend(restrictions)
self._coord_restrictions.extend(restrictions)
# Update of the coordinate bounds from the restrictions:
bounds = list(self._bounds) # convert to a list for modifications
new_restrictions = []
for restrict in self._restrictions:
for restrict in self._coord_restrictions:
restrict_used = False # determines whether restrict is used
# to set some coordinate bound
if not isinstance(restrict, (tuple, list)): # case of combined
Expand Down Expand Up @@ -1956,7 +1963,7 @@ def add_restrictions(self, restrictions):
# it is maintained in the list of restrictions:
new_restrictions.append(restrict)
self._bounds = tuple(bounds)
self._restrictions = new_restrictions
self._coord_restrictions = new_restrictions
self._fast_valid_coordinates = None


Expand Down Expand Up @@ -2036,11 +2043,12 @@ def restrict(self, subset, restrictions=None):
res = type(self)(subset, coordinates,
calc_method=self._calc_method._current)
res._bounds = self._bounds
res._restrictions.extend(self._restrictions)
res._coord_restrictions.extend(self._coord_restrictions)
# The coordinate restrictions are added to the result chart and
# possibly transformed into coordinate bounds:
if restrictions is not None:
res.add_restrictions(restrictions)
res.set_immutable()
# Update of supercharts and subcharts:
res._supercharts.update(self._supercharts)
for schart in self._supercharts:
Expand Down Expand Up @@ -2145,11 +2153,11 @@ def valid_coordinates(self, *coordinates, **kwds):
elif x >= xmax:
return False
# Check of additional restrictions:
if self._restrictions:
if self._coord_restrictions:
substitutions = dict(zip(self._xx, coordinates))
if parameters:
substitutions.update(parameters)
return self._check_restrictions(self._restrictions, substitutions)
return self._check_restrictions(self._coord_restrictions, substitutions)
return True

def valid_coordinates_numerical(self, *coordinates):
Expand Down Expand Up @@ -2215,19 +2223,21 @@ def valid_coordinates_numerical(self, *coordinates):
if self._fast_valid_coordinates is not None:
return self._fast_valid_coordinates(*coordinates)

self.set_immutable()

# case fast callable has to be computed
from operator import lt, gt

if not isinstance(self._restrictions, list):
if isinstance(self._restrictions, tuple):
self._restrictions = [self._restrictions]
elif isinstance(self._restrictions, Expression):
self._restrictions = [(self._restrictions,)]
if not isinstance(self._coord_restrictions, list):
if isinstance(self._coord_restrictions, tuple):
self._coord_restrictions = [self._coord_restrictions]
elif isinstance(self._coord_restrictions, Expression):
self._coord_restrictions = [(self._coord_restrictions,)]
else:
raise ValueError("restrictions must be in CNF (list of tuples)")

list_of_clause = []
for clause in self._restrictions:
for clause in self._coord_restrictions:
if not isinstance(clause, tuple):
if isinstance(clause, Expression):
clause = (clause,)
Expand Down

0 comments on commit 5215e93

Please sign in to comment.