Skip to content

Commit b52ecc5

Browse files
committed
rewrite minimiz_result decorator: deal with result is self
1 parent 2051570 commit b52ecc5

File tree

2 files changed

+35
-36
lines changed

2 files changed

+35
-36
lines changed

src/sage/combinat/k_regular_sequence.py

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,8 @@ def _error_if_degenerated_(self):
397397
"for correcting this.")
398398

399399
@cached_method
400-
def regenerated(self, **kwds):
400+
@minimize_result
401+
def regenerated(self):
401402
r"""
402403
Return a `k`-regular sequence that satisfies
403404
`\mu[0] \mathit{right} = \mathit{right}`.
@@ -462,40 +463,7 @@ def regenerated(self, **kwds):
462463
"""
463464
if not self.is_degenerated():
464465
return self
465-
return self._regenerated_(**kwds)
466-
467-
@minimize_result
468-
def _regenerated_(self):
469-
r"""
470-
Return a `k`-regular sequence that satisfies
471-
`\mu[0] \mathit{right} = \mathit{right}`.
472-
473-
Compared to :meth:`regenerated`, this method skips some initial checks
474-
whether the sequence is degenerated or not.
475-
476-
See also :meth:`regenerated`.
477-
478-
INPUT:
479-
480-
- ``minimize`` -- (default: ``None``) a boolean or ``None``.
481-
If ``True``, then :meth:`~RecognizableSeries.minimized` is called after the operation,
482-
if ``False``, then not. If this argument is ``None``, then
483-
the default specified by the parent's ``minimize_results`` is used.
484-
485-
OUTPUT:
486-
487-
A :class:`kRegularSequence`
488-
489-
TESTS::
490466

491-
sage: Seq2 = kRegularSequenceSpace(2, ZZ)
492-
sage: C = Seq2((Matrix([[2, 0], [2, 1]]), Matrix([[0, 1], [-2, 3]])),
493-
....: vector([1, 0]), vector([0, 1]))
494-
sage: C.is_degenerated()
495-
False
496-
sage: C.regenerated() is C # indirect doctest
497-
True
498-
"""
499467
from sage.matrix.constructor import Matrix
500468
from sage.matrix.special import zero_matrix, identity_matrix
501469
from sage.modules.free_module_element import vector

src/sage/combinat/recognizable_series.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,12 @@ def minimize_result(operation):
317317
if ``False``, then not. If this argument is ``None``, then
318318
the default specified by the parent's ``minimize_results`` is used.
319319
320+
.. NOTE::
321+
322+
If the result of ``operation`` is ``self``, then minimization is
323+
not applied unless ``minimize=True`` is explicitly set,
324+
in particular, independent of the parent's ``minimize_results``.
325+
320326
TESTS::
321327
322328
sage: from sage.combinat.recognizable_series import minimize_result
@@ -351,14 +357,39 @@ def minimize_result(operation):
351357
some result minimized
352358
sage: S('some').operation(minimize=False)
353359
some result
360+
361+
::
362+
363+
sage: class T(S):
364+
....: @minimize_result
365+
....: def nooperation(self):
366+
....: return self
367+
sage: t = T('some')
368+
sage: p.minimize_results = True
369+
sage: t.nooperation() is t
370+
True
371+
sage: t.nooperation(minimize=True) is t
372+
False
373+
sage: t.nooperation(minimize=False) is t
374+
True
375+
sage: p.minimize_results = False
376+
sage: t.nooperation() is t
377+
True
378+
sage: t.nooperation(minimize=True) is t
379+
False
380+
sage: t.nooperation(minimize=False) is t
381+
True
354382
"""
355383
@wraps(operation)
356384
def minimized(self, *args, **kwds):
357385
minimize = kwds.pop('minimize', None)
358-
if minimize is None:
359-
minimize = self.parent().minimize_results
360386

361387
result = operation(self, *args, **kwds)
388+
if minimize is not True and result is self:
389+
return result
390+
391+
if minimize is None:
392+
minimize = self.parent().minimize_results
362393

363394
if minimize:
364395
result = result.minimized()

0 commit comments

Comments
 (0)