From 4f3b69f6159dbdc127070fce043a3428e09326c0 Mon Sep 17 00:00:00 2001 From: Michael Jung Date: Wed, 5 Aug 2020 14:11:55 +0200 Subject: [PATCH] Trac 30291: simple checks for trivial cases in _mul_, _add_ and _div_ --- src/sage/manifolds/differentiable/chart.py | 2 +- .../manifolds/differentiable/tensorfield.py | 4 +-- src/sage/manifolds/local_frame.py | 2 +- src/sage/manifolds/scalarfield.py | 35 +++++++++++-------- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/sage/manifolds/differentiable/chart.py b/src/sage/manifolds/differentiable/chart.py index a333c2bddef..789a2bb774c 100644 --- a/src/sage/manifolds/differentiable/chart.py +++ b/src/sage/manifolds/differentiable/chart.py @@ -488,7 +488,7 @@ def coframe(self): sage: ey = c_xy.frame()[1] ; ey Vector field d/dy on the 2-dimensional differentiable manifold M sage: dx(ex).display() - dx(d/dx): M --> R + dx(d/dx): M --> R (x, y) |--> 1 sage: dx(ey).display() dx(d/dy): M --> R diff --git a/src/sage/manifolds/differentiable/tensorfield.py b/src/sage/manifolds/differentiable/tensorfield.py index 654096b41ae..57fb7f24250 100644 --- a/src/sage/manifolds/differentiable/tensorfield.py +++ b/src/sage/manifolds/differentiable/tensorfield.py @@ -4538,7 +4538,7 @@ def apply_map(self, fun, frame=None, chart=None, sage: v.display(P.frame(), P) (p^2 - q^2 - 2*p - 2*q) d/dp + (2*pi - 2*pi^2 - 2*(pi - pi^2)*p) d/dq sage: v.display(P.frame(), X) - (x^2 - y^2) d/dp - 2*(pi - pi^2)*x d/dq + (x + y)*(x - y) d/dp + 2*pi*(pi - 1)*x d/dq By default, the components of ``v`` in frames distinct from the specified one have been deleted:: @@ -4553,7 +4553,7 @@ def apply_map(self, fun, frame=None, chart=None, is effective in ``X.frame()`` as well:: sage: v.display(X.frame(), X) - (x^2 - y^2) d/dx - 2*(pi - pi^2)*x d/dy + (x + y)*(x - y) d/dx + 2*pi*(pi - 1)*x d/dy When the requested operation does not change the value of the tensor field, one can use the keyword argument ``keep_other_components=True``, diff --git a/src/sage/manifolds/local_frame.py b/src/sage/manifolds/local_frame.py index 507c37055c6..7830e24a280 100644 --- a/src/sage/manifolds/local_frame.py +++ b/src/sage/manifolds/local_frame.py @@ -119,7 +119,7 @@ Let us check that the coframe `(e^i)` is indeed the dual of the vector frame `(e_i)`:: - sage: e_dual[1](e[1]) # the linear form e^1 applied to the local section e_1 + sage: e_dual[1](e[1]) # linear form e^1 applied to local section e_1 Scalar field e^1(e_1) on the Open subset U of the 3-dimensional topological manifold M sage: e_dual[1](e[1]).expr() # the explicit expression of e^1(e_1) diff --git a/src/sage/manifolds/scalarfield.py b/src/sage/manifolds/scalarfield.py index 5f38403936a..6aac79ce463 100644 --- a/src/sage/manifolds/scalarfield.py +++ b/src/sage/manifolds/scalarfield.py @@ -2450,10 +2450,10 @@ def _add_(self, other): True """ - # Special cases: - if self._is_zero: + # Trivial cases: + if self.is_trivial_zero(): return other - if other._is_zero: + if other.is_trivial_zero(): return self # Generic case: com_charts = self.common_charts(other) @@ -2499,11 +2499,13 @@ def _sub_(self, other): True """ - # Special cases: - if self._is_zero: + # Trivial cases: + if self.is_trivial_zero(): return -other - if other._is_zero: + if other.is_trivial_zero(): return self + if self is other: + return self.parent().zero() # Generic case: com_charts = self.common_charts(other) if com_charts is None: @@ -2518,7 +2520,6 @@ def _sub_(self, other): result._latex_name = self._latex_name + '-' + other._latex_name return result - def _mul_(self, other): r""" Scalar field multiplication. @@ -2551,12 +2552,16 @@ def _mul_(self, other): True """ - from sage.tensor.modules.format_utilities import (format_mul_txt, - format_mul_latex) - # Special cases: - if self._is_zero or other._is_zero: + # Trivial cases: + if self.is_trivial_zero() or other.is_trivial_zero(): return self._domain.zero_scalar_field() + if (self - 1).is_trivial_zero(): + return other + if (other - 1).is_trivial_zero(): + return self # Generic case: + from sage.tensor.modules.format_utilities import (format_mul_txt, + format_mul_latex) com_charts = self.common_charts(other) if com_charts is None: raise ValueError("no common chart for the multiplication") @@ -2603,10 +2608,10 @@ def _div_(self, other): """ from sage.tensor.modules.format_utilities import format_mul_txt, \ format_mul_latex - # Special cases: - if other._is_zero: + # Trivial cases: + if other.is_trivial_zero(): raise ZeroDivisionError("division of a scalar field by zero") - if self._is_zero: + if self.is_trivial_zero(): return self._domain.zero_scalar_field() # Generic case: com_charts = self.common_charts(other) @@ -2668,6 +2673,7 @@ def _lmul_(self, number): True """ + # Trivial cases: try: if number.is_trivial_zero(): return self.parent().zero() @@ -2679,6 +2685,7 @@ def _lmul_(self, number): return self.parent().zero() if number == 1: return self + # Generic case: result = type(self)(self.parent()) if isinstance(number, Expression): var = number.variables() # possible symbolic variables in number