diff --git a/src/sage/manifolds/chart.py b/src/sage/manifolds/chart.py index b35fe15476c..e5f7ad47a46 100644 --- a/src/sage/manifolds/chart.py +++ b/src/sage/manifolds/chart.py @@ -3037,7 +3037,7 @@ def plot(self, chart=None, ambient_coords=None, mapping=None, from sage.plot.graphics import Graphics from sage.plot.line import line from sage.manifolds.continuous_map import ContinuousMap - from .utilities import set_axes_labels + from sage.manifolds.utilities import set_axes_labels # Extract the kwds options max_range = kwds['max_range'] diff --git a/src/sage/manifolds/differentiable/degenerate.py b/src/sage/manifolds/differentiable/degenerate.py index aa3a301255b..57828ad8dee 100644 --- a/src/sage/manifolds/differentiable/degenerate.py +++ b/src/sage/manifolds/differentiable/degenerate.py @@ -13,6 +13,7 @@ from sage.rings.infinity import infinity from sage.manifolds.structure import DegenerateStructure from sage.manifolds.differentiable.manifold import DifferentiableManifold +from sage.manifolds.differentiable.metric import DegenerateMetric ############################################################################### @@ -143,7 +144,7 @@ def __init__(self, n, name, metric_name=None, signature=None, self._metric_latex_name = metric_latex_name def metric(self, name=None, signature=None, latex_name=None, - dest_map=None): + dest_map=None) -> DegenerateMetric: r""" Return the metric giving the null manifold structure to the manifold, or define a new metric tensor on the manifold. diff --git a/src/sage/manifolds/differentiable/degenerate_submanifold.py b/src/sage/manifolds/differentiable/degenerate_submanifold.py index 03c2f769b12..7ae03974d69 100644 --- a/src/sage/manifolds/differentiable/degenerate_submanifold.py +++ b/src/sage/manifolds/differentiable/degenerate_submanifold.py @@ -162,6 +162,7 @@ TangentTensor) from sage.manifolds.differentiable.differentiable_submanifold import \ DifferentiableSubmanifold +from sage.manifolds.differentiable.metric import DegenerateMetric from sage.manifolds.differentiable.vectorfield_module import VectorFieldModule from sage.rings.infinity import infinity from sage.matrix.constructor import matrix @@ -590,7 +591,7 @@ def screen(self, name, screen, rad, latex_name=None): self._default_screen = self._screens[name] return self._screens[name] - def induced_metric(self): + def induced_metric(self) -> DegenerateMetric: r""" Return the pullback of the ambient metric. diff --git a/src/sage/manifolds/differentiable/diff_form.py b/src/sage/manifolds/differentiable/diff_form.py index 0aa3469770d..6b045a91c57 100644 --- a/src/sage/manifolds/differentiable/diff_form.py +++ b/src/sage/manifolds/differentiable/diff_form.py @@ -45,13 +45,14 @@ # ***************************************************************************** from __future__ import annotations -from typing import Union, TYPE_CHECKING +from typing import Optional, Union, TYPE_CHECKING from sage.misc.cachefunc import cached_method from sage.tensor.modules.free_module_alt_form import FreeModuleAltForm from sage.manifolds.differentiable.tensorfield import TensorField from sage.manifolds.differentiable.tensorfield_paral import TensorFieldParal if TYPE_CHECKING: + from sage.manifolds.differentiable.vectorfield_module import VectorFieldModule from sage.manifolds.differentiable.metric import PseudoRiemannianMetric from sage.manifolds.differentiable.symplectic_form import SymplecticForm @@ -384,7 +385,7 @@ def _del_derived(self): self.exterior_derivative.clear_cache() @cached_method - def exterior_derivative(self): + def exterior_derivative(self) -> DiffForm: r""" Compute the exterior derivative of ``self``. @@ -590,7 +591,7 @@ def wedge(self, other: DiffForm) -> DiffForm: other_r._restrictions[dom]) return resu - def degree(self): + def degree(self) -> int: r""" Return the degree of ``self``. @@ -615,7 +616,9 @@ def degree(self): def hodge_dual( self, - nondegenerate_tensor: Union[PseudoRiemannianMetric, SymplecticForm, None] = None, + nondegenerate_tensor: Union[ + PseudoRiemannianMetric, SymplecticForm, None + ] = None, ) -> DiffForm: r""" Compute the Hodge dual of the differential form with respect to some non-degenerate @@ -1228,8 +1231,8 @@ class DiffFormParal(FreeModuleAltForm, TensorFieldParal, DiffForm): no symmetry; no antisymmetry """ - def __init__(self, vector_field_module, degree, name=None, - latex_name=None): + def __init__(self, vector_field_module: VectorFieldModule, degree: int, name: Optional[str] = None, + latex_name: Optional[str] = None): r""" Construct a differential form. @@ -1382,7 +1385,7 @@ def __call__(self, *args): return TensorFieldParal.__call__(self, *args) @cached_method - def exterior_derivative(self): + def exterior_derivative(self) -> DiffFormParal: r""" Compute the exterior derivative of ``self``. diff --git a/src/sage/manifolds/differentiable/manifold.py b/src/sage/manifolds/differentiable/manifold.py index f83cbdfe096..359edbfa205 100644 --- a/src/sage/manifolds/differentiable/manifold.py +++ b/src/sage/manifolds/differentiable/manifold.py @@ -443,12 +443,15 @@ from typing import Optional, TYPE_CHECKING from sage.categories.manifolds import Manifolds from sage.categories.homset import Hom +from sage.manifolds.differentiable.diff_map import DiffMap from sage.rings.cc import CC from sage.rings.real_mpfr import RR from sage.rings.infinity import infinity, minus_infinity from sage.rings.integer import Integer from sage.manifolds.manifold import TopologicalManifold from sage.manifolds.differentiable.mixed_form_algebra import MixedFormAlgebra +from sage.manifolds.differentiable.vectorfield_module import VectorFieldModule +from sage.manifolds.differentiable.metric import PseudoRiemannianMetric if TYPE_CHECKING: from sage.manifolds.differentiable.vectorfield_module import VectorFieldModule @@ -3945,7 +3948,9 @@ def affine_connection(self, name, latex_name=None): AffineConnection return AffineConnection(self, name, latex_name) - def metric(self, name, signature=None, latex_name=None, dest_map=None): + def metric(self, name: str, signature: Optional[int] = None, + latex_name: Optional[str] = None, + dest_map: Optional[DiffMap] = None) -> PseudoRiemannianMetric: r""" Define a pseudo-Riemannian metric on the manifold. diff --git a/src/sage/manifolds/differentiable/metric.py b/src/sage/manifolds/differentiable/metric.py index feff8a0899b..686af0d3505 100644 --- a/src/sage/manifolds/differentiable/metric.py +++ b/src/sage/manifolds/differentiable/metric.py @@ -41,16 +41,18 @@ # the License, or (at your option) any later version. # https://www.gnu.org/licenses/ # ***************************************************************************** - from __future__ import annotations -from typing import TYPE_CHECKING -from sage.rings.integer import Integer + +from typing import TYPE_CHECKING, overload + from sage.manifolds.differentiable.tensorfield import TensorField from sage.manifolds.differentiable.tensorfield_paral import TensorFieldParal +from sage.rings.integer import Integer if TYPE_CHECKING: from sage.manifolds.differentiable.diff_form import DiffForm + class PseudoRiemannianMetric(TensorField): r""" Pseudo-Riemannian metric with values on an open subset of a @@ -1624,7 +1626,11 @@ def sqrt_abs_det(self, frame=None): self._sqrt_abs_dets[frame] = resu return self._sqrt_abs_dets[frame] - def volume_form(self, contra: int = 0) -> TensorField: + @overload + def volume_form(self) -> DiffForm: ... + @overload + def volume_form(self, contra: int) -> TensorField: ... + def volume_form(self, contra=0): r""" Volume form (Levi-Civita tensor) `\epsilon` associated with the metric. diff --git a/src/sage/manifolds/differentiable/tensorfield.py b/src/sage/manifolds/differentiable/tensorfield.py index 784ae9a6c41..808368cdb6c 100644 --- a/src/sage/manifolds/differentiable/tensorfield.py +++ b/src/sage/manifolds/differentiable/tensorfield.py @@ -53,20 +53,23 @@ # ***************************************************************************** from __future__ import annotations + +from typing import TYPE_CHECKING, Optional, Tuple, Union + from sage.rings.integer import Integer from sage.rings.integer_ring import ZZ from sage.structure.element import ModuleElementWithMutability from sage.tensor.modules.free_module_tensor import FreeModuleTensor from sage.tensor.modules.tensor_with_indices import TensorWithIndices -from typing import Optional, TYPE_CHECKING, Tuple, Union - if TYPE_CHECKING: - from sage.manifolds.differentiable.manifold import DifferentiableManifold from sage.manifolds.differentiable.diff_map import DiffMap + from sage.manifolds.differentiable.manifold import DifferentiableManifold from sage.manifolds.differentiable.metric import PseudoRiemannianMetric - from sage.manifolds.differentiable.symplectic_form import SymplecticForm from sage.manifolds.differentiable.poisson_tensor import PoissonTensorField + from sage.manifolds.differentiable.symplectic_form import SymplecticForm + from sage.manifolds.differentiable.vectorfield_module import VectorFieldModule + from sage.tensor.modules.comp import Components TensorType = Tuple[int, int] @@ -402,8 +405,17 @@ class TensorField(ModuleElementWithMutability): ValueError: the name of an immutable element cannot be changed """ - def __init__(self, vector_field_module, tensor_type, name=None, - latex_name=None, sym=None, antisym=None, parent=None): + + def __init__( + self, + vector_field_module: VectorFieldModule, + tensor_type: TensorType, + name: Optional[str] = None, + latex_name: Optional[str] = None, + sym=None, + antisym=None, + parent=None, + ): r""" Construct a tensor field. @@ -840,7 +852,7 @@ def domain(self) -> DifferentiableManifold: """ return self._domain - def base_module(self): + def base_module(self) -> VectorFieldModule: r""" Return the vector field module on which ``self`` acts as a tensor. @@ -867,7 +879,7 @@ def base_module(self): """ return self._vmodule - def tensor_type(self) -> Tuple[int, int]: + def tensor_type(self) -> TensorType: r""" Return the tensor type of ``self``. @@ -968,7 +980,7 @@ def set_immutable(self): rst.set_immutable() super().set_immutable() - def set_restriction(self, rst): + def set_restriction(self, rst: TensorField): r""" Define a restriction of ``self`` to some subdomain. @@ -1035,7 +1047,9 @@ def set_restriction(self, rst): latex_name=self._latex_name) self._is_zero = False # a priori - def restrict(self, subdomain: DifferentiableManifold, dest_map: Optional[DiffMap] = None) -> TensorField: + def restrict( + self, subdomain: DifferentiableManifold, dest_map: Optional[DiffMap] = None + ) -> TensorField: r""" Return the restriction of ``self`` to some subdomain. @@ -1385,7 +1399,7 @@ def _add_comp_unsafe(self, basis=None): rst = self.restrict(basis._domain, dest_map=basis._dest_map) return rst._add_comp_unsafe(basis) - def add_comp(self, basis=None): + def add_comp(self, basis=None) -> Components: r""" Return the components of ``self`` in a given vector frame for assignment. @@ -3718,7 +3732,13 @@ def at(self, point): if point in dom: return rst.at(point) - def up(self, non_degenerate_form: Union['PseudoRiemannianMetric', 'SymplecticForm', 'PoissonTensorField'], pos: Optional[int] = None) -> 'TensorField': + def up( + self, + non_degenerate_form: Union[ + "PseudoRiemannianMetric", "SymplecticForm", "PoissonTensorField" + ], + pos: Optional[int] = None, + ) -> "TensorField": r""" Compute a dual of the tensor field by raising some index with the given tensor field (usually, a pseudo-Riemannian metric, a symplectic form or a Poisson tensor). @@ -3857,14 +3877,15 @@ def up(self, non_degenerate_form: Union['PseudoRiemannianMetric', 'SymplecticFor k = result._tensor_type[0] result = result.up(non_degenerate_form, k) return result - - if posself._tensor_rank-1: + + if pos < n_con or pos > self._tensor_rank - 1: print("pos = {}".format(pos)) raise ValueError("position out of range") from sage.manifolds.differentiable.metric import PseudoRiemannianMetric from sage.manifolds.differentiable.symplectic_form import SymplecticForm from sage.manifolds.differentiable.poisson_tensor import PoissonTensorField + if isinstance(non_degenerate_form, PseudoRiemannianMetric): return self.contract(pos, non_degenerate_form.inverse(), 1) elif isinstance(non_degenerate_form, SymplecticForm): @@ -3874,7 +3895,11 @@ def up(self, non_degenerate_form: Union['PseudoRiemannianMetric', 'SymplecticFor else: raise ValueError("The non-degenerate form has to be a metric, a symplectic form or a Poisson tensor field") - def down(self, non_degenerate_form: Union[PseudoRiemannianMetric, SymplecticForm], pos: Optional[int] = None) -> TensorField: + def down( + self, + non_degenerate_form: Union[PseudoRiemannianMetric, SymplecticForm], + pos: Optional[int] = None, + ) -> TensorField: r""" Compute a dual of the tensor field by lowering some index with a given non-degenerate form (pseudo-Riemannian metric or symplectic form). @@ -4009,7 +4034,7 @@ def down(self, non_degenerate_form: Union[PseudoRiemannianMetric, SymplecticForm result = self for p in range(n_con): k = result._tensor_type[0] - result = result.down(non_degenerate_form, k-1) + result = result.down(non_degenerate_form, k - 1) return result if not isinstance(pos, (int, Integer)): raise TypeError("the argument 'pos' must be an integer") diff --git a/src/sage/manifolds/differentiable/tensorfield_paral.py b/src/sage/manifolds/differentiable/tensorfield_paral.py index 6bcd68becb6..373f491cfd3 100644 --- a/src/sage/manifolds/differentiable/tensorfield_paral.py +++ b/src/sage/manifolds/differentiable/tensorfield_paral.py @@ -303,12 +303,19 @@ # https://www.gnu.org/licenses/ # ***************************************************************************** -from sage.tensor.modules.free_module_tensor import FreeModuleTensor +from __future__ import annotations + +from typing import TYPE_CHECKING + from sage.manifolds.chart import Chart from sage.manifolds.differentiable.tensorfield import TensorField from sage.parallel.decorate import parallel from sage.parallel.parallelism import Parallelism from sage.symbolic.ring import SR +from sage.tensor.modules.free_module_tensor import FreeModuleTensor + +if TYPE_CHECKING: + from sage.tensor.modules.comp import Components class TensorFieldParal(FreeModuleTensor, TensorField): r""" @@ -1122,7 +1129,7 @@ class :class:`~sage.tensor.modules.comp.Components`; if such # The add_comp operation is performed on the subdomain: return rst.add_comp(basis=basis) - def comp(self, basis=None, from_basis=None): + def comp(self, basis=None, from_basis=None) -> Components: r""" Return the components in a given vector frame. diff --git a/src/sage/manifolds/differentiable/vectorfield_module.py b/src/sage/manifolds/differentiable/vectorfield_module.py index 8c888136430..0b9dedc3b1c 100644 --- a/src/sage/manifolds/differentiable/vectorfield_module.py +++ b/src/sage/manifolds/differentiable/vectorfield_module.py @@ -27,7 +27,7 @@ """ -#****************************************************************************** +# ****************************************************************************** # Copyright (C) 2015 Eric Gourgoulhon # Copyright (C) 2015 Michal Bejger # Copyright (C) 2016 Travis Scrimshaw @@ -36,16 +36,23 @@ # as published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. # http://www.gnu.org/licenses/ -#****************************************************************************** +# ****************************************************************************** + +from __future__ import annotations + +from typing import TYPE_CHECKING, Optional -from typing import Optional -from sage.structure.unique_representation import UniqueRepresentation -from sage.structure.parent import Parent from sage.categories.modules import Modules +from sage.manifolds.differentiable.diff_map import DiffMap +from sage.manifolds.differentiable.vectorfield import VectorField, VectorFieldParal from sage.misc.cachefunc import cached_method from sage.rings.integer import Integer +from sage.structure.parent import Parent +from sage.structure.unique_representation import UniqueRepresentation from sage.tensor.modules.finite_rank_free_module import FiniteRankFreeModule -from sage.manifolds.differentiable.vectorfield import VectorField, VectorFieldParal + +if TYPE_CHECKING: + from sage.manifolds.differentiable.manifold import DifferentiableManifold class VectorFieldModule(UniqueRepresentation, Parent): @@ -182,7 +189,7 @@ class VectorFieldModule(UniqueRepresentation, Parent): """ Element = VectorField - def __init__(self, domain, dest_map=None): + def __init__(self, domain: DifferentiableManifold, dest_map: Optional[DiffMap] = None): r""" Construct the module of vector fields taking values on a (a priori) non-parallelizable differentiable manifold. @@ -387,7 +394,7 @@ def _latex_(self): else: return self._latex_name - def domain(self): + def domain(self) -> DifferentiableManifold: r""" Return the domain of the vector fields in this module. @@ -416,7 +423,7 @@ def domain(self): """ return self._domain - def ambient_domain(self): + def ambient_domain(self) -> DifferentiableManifold: r""" Return the manifold in which the vector fields of this module take their values. @@ -1050,7 +1057,7 @@ def zero(self): zero.set_immutable() return zero - def metric(self, name, signature=None, latex_name=None): + def metric(self, name: str, signature: Optional[int] = None, latex_name: Optional[str] = None): r""" Construct a metric (symmetric bilinear form) on the current vector field module. @@ -1610,7 +1617,7 @@ def _repr_(self): " mapped into the {}".format(self._ambient_domain) return description - def domain(self): + def domain(self) -> DifferentiableManifold: r""" Return the domain of the vector fields in ``self``. @@ -1641,7 +1648,7 @@ def domain(self): """ return self._domain - def ambient_domain(self): + def ambient_domain(self) -> DifferentiableManifold: r""" Return the manifold in which the vector fields of ``self`` take their values. @@ -1673,7 +1680,7 @@ def ambient_domain(self): """ return self._ambient_domain - def destination_map(self): + def destination_map(self) -> DiffMap: r""" Return the differential map associated to ``self``. diff --git a/src/sage/manifolds/manifold.py b/src/sage/manifolds/manifold.py index 4c3d350b044..58e34843230 100644 --- a/src/sage/manifolds/manifold.py +++ b/src/sage/manifolds/manifold.py @@ -325,20 +325,32 @@ # https://www.gnu.org/licenses/ # **************************************************************************** +from __future__ import annotations + +from typing import TYPE_CHECKING, Optional, Union, overload + +import sage.rings.abc from sage.categories.fields import Fields -from sage.categories.manifolds import Manifolds from sage.categories.homset import Hom -import sage.rings.abc -from sage.rings.cc import CC -from sage.rings.real_mpfr import RR -from sage.misc.prandom import getrandbits +from sage.categories.manifolds import Manifolds +from sage.manifolds.structure import ( + DifferentialStructure, + RealDifferentialStructure, + RealTopologicalStructure, + TopologicalStructure, +) +from sage.manifolds.subset import ManifoldSubset from sage.misc.cachefunc import cached_method +from sage.misc.prandom import getrandbits +from sage.rings.cc import CC from sage.rings.integer import Integer +from sage.rings.real_mpfr import RR from sage.structure.global_options import GlobalOptions -from sage.manifolds.subset import ManifoldSubset -from sage.manifolds.structure import ( - TopologicalStructure, RealTopologicalStructure, - DifferentialStructure, RealDifferentialStructure) + +if TYPE_CHECKING: + from sage.manifolds.continuous_map import ContinuousMap + from sage.manifolds.differentiable.diff_map import DiffMap + from sage.manifolds.differentiable.manifold import DifferentiableManifold ############################################################################# @@ -2444,6 +2456,10 @@ def homeomorphism(self, codomain, coord_functions=None, chart1=None, return homset(coord_functions, name=name, latex_name=latex_name, is_isomorphism=True) + @overload + def identity_map(self: TopologicalManifold) -> ContinuousMap: ... + @overload + def identity_map(self: DifferentiableManifold) -> DiffMap: ... @cached_method def identity_map(self): r""" @@ -2701,8 +2717,16 @@ def set_simplify_function(self, simplifying_func, method=None): _manifold_id = Integer(0) -def Manifold(dim, name, latex_name=None, field='real', structure=None, - start_index=0, **extra_kwds): + +def Manifold( + dim: int, + name: Optional[str], + latex_name: Optional[str] = None, + field: str = "real", + structure: Optional[str] = None, + start_index: int = 0, + **extra_kwds, +) -> Union[TopologicalManifold, DifferentiableManifold]: r""" Construct a manifold of a given type over a topological field. diff --git a/src/sage/manifolds/topological_submanifold.py b/src/sage/manifolds/topological_submanifold.py index 62aa78fef3f..91f6f0d6062 100644 --- a/src/sage/manifolds/topological_submanifold.py +++ b/src/sage/manifolds/topological_submanifold.py @@ -513,8 +513,9 @@ def declare_embedding(self): "before calling declare_embedding()") self._embedded = True - def set_embedding(self, phi, inverse=None, var=None, - t_inverse=None): + def set_embedding( + self, phi: ContinuousMap, inverse=None, var=None, t_inverse=None + ): r""" Register the embedding of an embedded submanifold. @@ -812,7 +813,7 @@ def plot(self, param, u, v, chart1=None, chart2=None, **kwargs): return ParametricSurface((fx, fy, fz), (u, v), **kwargs) - def ambient(self): + def ambient(self) -> TopologicalManifold: r""" Return the manifold in which ``self`` is immersed or embedded. @@ -825,7 +826,7 @@ def ambient(self): """ return self._ambient - def immersion(self): + def immersion(self) -> ContinuousMap: r""" Return the immersion of ``self`` into the ambient manifold. @@ -849,9 +850,10 @@ def immersion(self): """ if not self._immersed: raise ValueError("the submanifold is not immersed") + assert self._immersion return self._immersion - def embedding(self): + def embedding(self) -> ContinuousMap: r""" Return the embedding of ``self`` into the ambient manifold. @@ -875,6 +877,7 @@ def embedding(self): """ if not self._embedded: raise ValueError("the submanifold is not embedded") + assert self._immersion return self._immersion def as_subset(self): diff --git a/src/sage/tensor/modules/free_module_tensor.py b/src/sage/tensor/modules/free_module_tensor.py index f5c2da72c7c..487d0d83e0e 100644 --- a/src/sage/tensor/modules/free_module_tensor.py +++ b/src/sage/tensor/modules/free_module_tensor.py @@ -192,6 +192,7 @@ class being: # the License, or (at your option) any later version. # https://www.gnu.org/licenses/ # ***************************************************************************** +from typing import Dict from sage.rings.integer import Integer from sage.structure.element import ModuleElementWithMutability @@ -200,6 +201,7 @@ class being: from sage.tensor.modules.tensor_with_indices import TensorWithIndices from sage.parallel.decorate import parallel from sage.parallel.parallelism import Parallelism +from sage.tensor.modules.free_module_basis import FreeModuleBasis class FreeModuleTensor(ModuleElementWithMutability): r""" @@ -287,7 +289,7 @@ def __init__(self, fmodule, tensor_type, name=None, latex_name=None, self._latex_name = self._name else: self._latex_name = latex_name - self._components = {} # dict. of the sets of components on various + self._components: Dict[FreeModuleBasis, Components] = {} # dict. of the sets of components on various # bases, with the bases as keys (initially empty) # Treatment of symmetry declarations: