Skip to content

Commit 848565f

Browse files
committed
Trac #21343: rename to "degenerate" and co
1 parent 21ed597 commit 848565f

File tree

1 file changed

+94
-72
lines changed

1 file changed

+94
-72
lines changed

src/sage/combinat/k_regular_sequence.py

Lines changed: 94 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,25 @@ def split_interlace(n, k, p):
213213
for d in n.digits(k, padto=1))))
214214

215215

216+
class DegeneratedSequenceError(RuntimeError):
217+
r"""
218+
Exception raised if a degenerated sequence
219+
(see :meth:`~kRegularSequence.is_degenerated`) is detected.
220+
221+
EXAMPLES::
222+
223+
sage: Seq2 = kRegularSequenceSpace(2, ZZ)
224+
sage: Seq2((Matrix([2]), Matrix([3])), vector([1]), vector([1]))
225+
Traceback (most recent call last):
226+
...
227+
DegeneratedSequenceError: degenerated sequence: mu[0]*right != right.
228+
Using such a sequence might lead to wrong results.
229+
You can use 'allow_degenerated_sequence=True' followed
230+
by a call of method .regenerated() for correcting this.
231+
"""
232+
pass
233+
234+
216235
class kRegularSequence(RecognizableSeries):
217236

218237
def __init__(self, parent, mu, left=None, right=None):
@@ -253,8 +272,11 @@ def __init__(self, parent, mu, left=None, right=None):
253272
are switched.
254273
(This is done by calling :meth:`~sage.combinat.recognizable_series.RecognizableSeries.transposed`.)
255274
256-
- ``heal`` -- (default: ``False``) a boolean. If set, then
257-
:meth:`healed` is called at the end of creating the element.
275+
- ``allow_degenerated_sequence`` -- (default: ``False``) a boolean. If set, then
276+
there will be no check if the input is a degenerated sequence
277+
(see :meth:`is_degenerated`).
278+
Otherwise the input is checked and a :class:`DegeneratedSequenceError`
279+
is raised if such a sequence is detected.
258280
259281
EXAMPLES::
260282
@@ -368,36 +390,41 @@ def __iter__(self):
368390

369391

370392
@cached_method
371-
def is_healthy(self):
393+
def is_degenerated(self):
372394
r"""
373395
Return whether this `k`-regular sequence satisfies
374396
`\mu[0] \mathit{right} = \mathit{right}`.
375397
376398
EXAMPLES::
377399
378400
sage: Seq2 = kRegularSequenceSpace(2, ZZ)
379-
sage: S = Seq2((Matrix([2]), Matrix([3])), vector([1]), vector([1]))
380-
WARNING:...:Unhealthy sequence: mu[0]*right != right.
381-
Results might be wrong. Use heal=True or
382-
method .healed() for correcting this.
401+
sage: Seq2((Matrix([2]), Matrix([3])), vector([1]), vector([1])) # indirect doctest
402+
Traceback (most recent call last):
403+
...
404+
DegeneratedSequenceError: degenerated sequence: mu[0]*right != right.
405+
Using such a sequence might lead to wrong results.
406+
You can use 'allow_degenerated_sequence=True' followed
407+
by a call of method .regenerated() for correcting this.
408+
sage: S = Seq2((Matrix([2]), Matrix([3])), vector([1]), vector([1]),
409+
....: allow_degenerated_sequence=True)
383410
sage: S
384411
2-regular sequence 1, 3, 6, 9, 12, 18, 18, 27, 24, 36, ...
385-
sage: S.is_healthy()
386-
False
412+
sage: S.is_degenerated()
413+
True
387414
388415
::
389416
390417
sage: C = Seq2((Matrix([[2, 0], [2, 1]]), Matrix([[0, 1], [-2, 3]])),
391418
....: vector([1, 0]), vector([0, 1]))
392-
sage: C.is_healthy()
393-
True
419+
sage: C.is_degenerated()
420+
False
394421
"""
395422
from sage.rings.integer_ring import ZZ
396-
return (self.mu[ZZ(0)] * self.right) == self.right
423+
return (self.mu[ZZ(0)] * self.right) != self.right
397424

398425

399426
@cached_method
400-
def healed(self, minimize=True):
427+
def regenerated(self, minimize=True):
401428
r"""
402429
Return a `k`-regular sequence that satisfies
403430
`\mu[0] \mathit{right} = \mathit{right}`.
@@ -416,56 +443,52 @@ def healed(self, minimize=True):
416443
sage: Seq2 = kRegularSequenceSpace(2, ZZ)
417444
418445
The following linear representation of `S` is chosen bad (is
419-
unhealty, see :meth:`is_healthy`), as `\mu(0)` applied on
446+
degenerated, see :meth:`is_degenerated`), as `\mu(0)` applied on
420447
`\mathit{right}` does not equal `\mathit{right}`::
421448
422-
sage: S = Seq2((Matrix([2]), Matrix([3])), vector([1]), vector([1]))
423-
WARNING:...:Unhealthy sequence: mu[0]*right != right.
424-
Results might be wrong. Use heal=True or
425-
method .healed() for correcting this.
449+
sage: S = Seq2((Matrix([2]), Matrix([3])), vector([1]), vector([1]),
450+
....: allow_degenerated_sequence=True)
426451
sage: S
427452
2-regular sequence 1, 3, 6, 9, 12, 18, 18, 27, 24, 36, ...
428-
sage: S.is_healthy()
429-
False
453+
sage: S.is_degenerated()
454+
True
430455
431-
However, we can heal the sequence `S`::
456+
However, we can regenerate the sequence `S`::
432457
433-
sage: H = S.healed()
458+
sage: H = S.regenerated()
434459
sage: H
435460
2-regular sequence 1, 3, 6, 9, 12, 18, 18, 27, 24, 36, ...
436461
sage: H.mu[0], H.mu[1], H.left, H.right
437462
(
438463
[ 0 1] [3 0]
439464
[-2 3], [6 0], (1, 0), (1, 1)
440465
)
441-
sage: H.is_healthy()
442-
True
466+
sage: H.is_degenerated()
467+
False
443468
444469
TESTS::
445470
446-
sage: S = Seq2((Matrix([2]), Matrix([3])), vector([1]), vector([1]))
447-
WARNING:...:Unhealthy sequence: mu[0]*right != right.
448-
Results might be wrong. Use heal=True or
449-
method .healed() for correcting this.
450-
sage: H = S.healed(minimize=False)
471+
sage: S = Seq2((Matrix([2]), Matrix([3])), vector([1]), vector([1]),
472+
....: allow_degenerated_sequence=True)
473+
sage: H = S.regenerated(minimize=False)
451474
sage: H.mu[0], H.mu[1], H.left, H.right
452475
(
453476
[1 0] [0 0]
454477
[0 2], [3 3], (1, 1), (1, 0)
455478
)
456-
sage: H.is_healthy()
457-
True
479+
sage: H.is_degenerated()
480+
False
458481
459482
::
460483
461484
sage: C = Seq2((Matrix([[2, 0], [2, 1]]), Matrix([[0, 1], [-2, 3]])),
462485
....: vector([1, 0]), vector([0, 1]))
463-
sage: C.is_healthy()
464-
True
465-
sage: C.healed() is C
486+
sage: C.is_degenerated()
487+
False
488+
sage: C.regenerated() is C
466489
True
467490
"""
468-
if self.is_healthy():
491+
if not self.is_degenerated():
469492
return self
470493

471494
from sage.matrix.special import zero_matrix, identity_matrix
@@ -617,13 +640,11 @@ def subsequence(self, a, b, minimize=True):
617640
ValueError: a=-1 is not nonnegative.
618641
619642
The following linear representation of `S` is chosen bad (is
620-
unhealty, see :meth:`is_healthy`), as `\mu(0)` applied on
643+
degenerated, see :meth:`is_degenerated`), as `\mu(0)` applied on
621644
`\mathit{right}` does not equal `\mathit{right}`::
622645
623-
sage: S = Seq2((Matrix([2]), Matrix([3])), vector([1]), vector([1]))
624-
WARNING:...:Unhealthy sequence: mu[0]*right != right.
625-
Results might be wrong. Use heal=True or
626-
method .healed() for correcting this.
646+
sage: S = Seq2((Matrix([2]), Matrix([3])), vector([1]), vector([1]),
647+
....: allow_degenerated_sequence=True)
627648
sage: S
628649
2-regular sequence 1, 3, 6, 9, 12, 18, 18, 27, 24, 36, ...
629650
@@ -636,7 +657,7 @@ def subsequence(self, a, b, minimize=True):
636657
We get the correct result by
637658
::
638659
639-
sage: S.healed().subsequence(1, -4)
660+
sage: S.regenerated().subsequence(1, -4)
640661
2-regular sequence 0, 0, 0, 0, 1, 3, 6, 9, 12, 18, ...
641662
"""
642663
from sage.rings.integer_ring import ZZ
@@ -826,13 +847,11 @@ def partial_sums(self, include_n=False, minimize=True):
826847
2-regular sequence 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, ...
827848
828849
The following linear representation of `S` is chosen bad (is
829-
unhealty, see :meth:`is_healthy`), as `\mu(0)` applied on
850+
degenerated, see :meth:`is_degenerated`), as `\mu(0)` applied on
830851
`\mathit{right}` does not equal `\mathit{right}`::
831852
832-
sage: S = Seq2((Matrix([2]), Matrix([3])), vector([1]), vector([1]))
833-
WARNING:...:Unhealthy sequence: mu[0]*right != right.
834-
Results might be wrong. Use heal=True or
835-
method .healed() for correcting this.
853+
sage: S = Seq2((Matrix([2]), Matrix([3])), vector([1]), vector([1]),
854+
....: allow_degenerated_sequence=True)
836855
sage: S
837856
2-regular sequence 1, 3, 6, 9, 12, 18, 18, 27, 24, 36, ...
838857
@@ -865,11 +884,11 @@ def partial_sums(self, include_n=False, minimize=True):
865884
sage: G.minimized().dimension() == G.dimension()
866885
True
867886
868-
Or we heal the sequence `S` first::
887+
Or we regenerate the sequence `S` first::
869888
870-
sage: S.healed().partial_sums(include_n=True, minimize=False)
889+
sage: S.regenerated().partial_sums(include_n=True, minimize=False)
871890
2-regular sequence 1, 4, 10, 19, 31, 49, 67, 94, 118, 154, ...
872-
sage: S.healed().partial_sums(minimize=False)
891+
sage: S.regenerated().partial_sums(minimize=False)
873892
2-regular sequence 0, 1, 4, 10, 19, 31, 49, 67, 94, 118, ...
874893
875894
TESTS::
@@ -1131,7 +1150,10 @@ def some_elements(self):
11311150
...
11321151
2-regular sequence 2210, 170, 0, 0, 0, 0, 0, 0, 0, 0, ...)
11331152
"""
1134-
return super(kRegularSequenceSpace, self).some_elements(heal=True)
1153+
return iter(element.regenerated()
1154+
for element
1155+
in super(kRegularSequenceSpace, self).some_elements(
1156+
allow_degenerated_sequence=True))
11351157

11361158

11371159
def _element_constructor_(self, *args, **kwds):
@@ -1144,25 +1166,27 @@ def _element_constructor_(self, *args, **kwds):
11441166
11451167
sage: Seq2 = kRegularSequenceSpace(2, ZZ)
11461168
sage: Seq2((Matrix([2]), Matrix([3])), vector([1]), vector([1]))
1147-
WARNING:...:Unhealthy sequence: mu[0]*right != right.
1148-
Results might be wrong. Use heal=True or
1149-
method .healed() for correcting this.
1169+
Traceback (most recent call last):
1170+
...
1171+
DegeneratedSequenceError: degenerated sequence: mu[0]*right != right.
1172+
Using such a sequence might lead to wrong results.
1173+
You can use 'allow_degenerated_sequence=True' followed
1174+
by a call of method .regenerated() for correcting this.
1175+
sage: Seq2((Matrix([2]), Matrix([3])), vector([1]), vector([1]),
1176+
....: allow_degenerated_sequence=True)
11501177
2-regular sequence 1, 3, 6, 9, 12, 18, 18, 27, 24, 36, ...
11511178
sage: Seq2((Matrix([2]), Matrix([3])), vector([1]), vector([1]),
1152-
....: heal=True)
1179+
....: allow_degenerated_sequence=True).regenerated()
11531180
2-regular sequence 1, 3, 6, 9, 12, 18, 18, 27, 24, 36, ...
11541181
"""
1155-
heal = kwds.pop('heal', False)
1182+
allow_degenerated_sequence = kwds.pop('allow_degenerated_sequence', False)
11561183
element = super(kRegularSequenceSpace, self)._element_constructor_(*args, **kwds)
1157-
if heal:
1158-
element = element.healed()
1159-
elif not element.is_healthy():
1160-
import logging
1161-
logger = logging.getLogger(__name__)
1162-
logger.warning('Unhealthy sequence: mu[0]*right != right. '
1163-
'Results might be wrong. '
1164-
'Use heal=True or method .healed() '
1165-
'for correcting this.')
1184+
if not allow_degenerated_sequence and element.is_degenerated():
1185+
raise DegeneratedSequenceError("degenerated sequence: mu[0]*right != right. "
1186+
"Using such a sequence might lead to wrong results. "
1187+
"You can use 'allow_degenerated_sequence=True' followed by "
1188+
"a call of method .regenerated() "
1189+
"for correcting this.")
11661190
return element
11671191

11681192

@@ -1271,17 +1295,15 @@ def guess(self, f, n_max=100, max_dimension=10, sequence=None):
12711295
)
12721296
12731297
The following linear representation of `S` is chosen bad (is
1274-
unhealty, see :meth:`is_healthy`), as `\mu(0)` applied on
1298+
degenerated, see :meth:`is_degenerated`), as `\mu(0)` applied on
12751299
`\mathit{right}` does not equal `\mathit{right}`::
12761300
1277-
sage: S = Seq2((Matrix([2]), Matrix([3])), vector([1]), vector([1]))
1278-
WARNING:...:Unhealthy sequence: mu[0]*right != right.
1279-
Results might be wrong. Use heal=True or
1280-
method .healed() for correcting this.
1301+
sage: S = Seq2((Matrix([2]), Matrix([3])), vector([1]), vector([1]),
1302+
....: allow_degenerated_sequence=True)
12811303
sage: S
12821304
2-regular sequence 1, 3, 6, 9, 12, 18, 18, 27, 24, 36, ...
1283-
sage: S.is_healthy()
1284-
False
1305+
sage: S.is_degenerated()
1306+
True
12851307
12861308
However, we can :meth:`~kRegularSequenceSpace.guess` a `2`-regular sequence of dimension `2`::
12871309
@@ -1294,7 +1316,7 @@ def guess(self, f, n_max=100, max_dimension=10, sequence=None):
12941316
[-2 3], [6 0], (1, 0), (1, 1)
12951317
)
12961318
1297-
sage: G == S.healed()
1319+
sage: G == S.regenerated()
12981320
True
12991321
13001322
TESTS::

0 commit comments

Comments
 (0)