Skip to content

Commit

Permalink
raise ValueError in case (in)equality is not decidable
Browse files Browse the repository at this point in the history
  • Loading branch information
mantepse committed Apr 10, 2023
1 parent 94e0de4 commit b1b8493
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
17 changes: 17 additions & 0 deletions src/sage/data_structures/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,23 @@ def iterate_coefficients(self):
yield self._target[n]
n += 1

def is_nonzero(self):
r"""
Return ``True`` if and only if this stream is known
to be nonzero.
An assumption of this class is that it is nonzero.
EXAMPLES::
sage: from sage.data_structures.stream import Stream_uninitialized
sage: g = Stream_uninitialized(0)
sage: g.is_nonzero()
True
"""
if self._target is None:
return True
return super().is_nonzero()

class Stream_unary(Stream_inexact):
r"""
Expand Down
36 changes: 25 additions & 11 deletions src/sage/rings/lazy_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,9 +938,13 @@ def _richcmp_(self, other, op):
sage: fz = L(lambda n: 0, valuation=0)
sage: L.zero() == fz
False
Traceback (most recent call last):
...
ValueError: undecidable
sage: fz == L.zero()
False
Traceback (most recent call last):
...
ValueError: undecidable
TESTS::
Expand All @@ -952,13 +956,15 @@ def _richcmp_(self, other, op):
"""
if op is op_EQ:
prec = self.parent().options['halting_precision']
if prec is None:
return self._coeff_stream == other._coeff_stream

# they may be trivially equal
if self._coeff_stream == other._coeff_stream:
return True
# they may be trivially different
if self._coeff_stream != other._coeff_stream:
return False
prec = self.parent().options['halting_precision']
if prec is None:
raise ValueError("undecidable")
# otherwise we check the first prec coefficients
m = min(self._coeff_stream._approximate_order,
other._coeff_stream._approximate_order)
Expand Down Expand Up @@ -1003,7 +1009,9 @@ def __bool__(self):
sage: M = L(lambda n: 2*n if n < 10 else 1, valuation=0); M
O(z^7)
sage: bool(M)
True
Traceback (most recent call last):
...
ValueError: undecidable
sage: M[15]
1
sage: bool(M)
Expand All @@ -1013,7 +1021,9 @@ def __bool__(self):
sage: M = L(lambda n: 2*n if n < 10 else 1, valuation=0); M
O(z^7)
sage: bool(M)
True
Traceback (most recent call last):
...
ValueError: undecidable
sage: M[15]
1
sage: bool(M)
Expand All @@ -1040,9 +1050,11 @@ def __bool__(self):
True
sage: g.define(1 + z*g)
sage: bool(g)
True
Traceback (most recent call last):
...
ValueError: undecidable
"""
return bool(self._coeff_stream)
return not (self == self.parent().zero())

def define(self, s):
r"""
Expand Down Expand Up @@ -1716,7 +1728,9 @@ def _acted_upon_(self, scalar, self_on_left):
Different scalars potentially give different series::
sage: 2 * M == 3 * M
False
Traceback (most recent call last):
...
ValueError: undecidable
Sparse series can be multiplied with a scalar::
Expand Down
8 changes: 5 additions & 3 deletions src/sage/rings/lazy_series_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,8 +664,8 @@ class options(GlobalOptions):
description='the number of coefficients to display for nonzero constant series',
checker=lambda x: x in ZZ and x > 0)
halting_precision = dict(default=None,
description='the number of coefficients, beginning with the approximate valuation, to check in equality tests',
checker=lambda x: x is None or x in ZZ and x > 0)
description='the number of coefficients, beginning with the approximate valuation, to check in equality tests',
checker=lambda x: x is None or x in ZZ and x > 0)

@cached_method
def one(self):
Expand Down Expand Up @@ -1117,7 +1117,9 @@ class LazyLaurentSeriesRing(LazySeriesRing):
sage: f2 = f * 2 # currently no coefficients computed
sage: f3 = f * 3 # currently no coefficients computed
sage: f2 == f3
False
Traceback (most recent call last):
...
ValueError: undecidable
sage: f2 # computes some of the coefficients of f2
2*z^-1 - 2 + 2*z - 2*z^2 + 2*z^3 - 2*z^4 + 2*z^5 + O(z^6)
sage: f3 # computes some of the coefficients of f3
Expand Down

0 comments on commit b1b8493

Please sign in to comment.