Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Merge branch 'u/dkrenn/sequences/recognizable' into u/dkrenn/sequence…
Browse files Browse the repository at this point in the history
…s/k-regular

* u/dkrenn/sequences/recognizable:
  add experimental warning
  extend element constructor
  remove None-sense to simplify code
  • Loading branch information
dkrenn committed Aug 22, 2016
2 parents cf1b8cc + 9e12383 commit 69289fe
Showing 1 changed file with 74 additions and 68 deletions.
142 changes: 74 additions & 68 deletions src/sage/combinat/recognizable_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@
\mathit{left} \, \mu(w) \, \mathit{right}.
.. WARNING::
As this code is experimental, warnings are thrown when a a
recognizable series space is created for the first time in a
session (see :class:`sage.misc.superseded.experimental`).
TESTS::
sage: Rec = RecognizableSeriesSpace(ZZ, [0, 1])
doctest:...: FutureWarning: This class/method/function is
marked as experimental. It, its functionality or its interface
might change without a formal deprecation.
See http://trac.sagemath.org/21202 for details.
Various
=======
Expand Down Expand Up @@ -58,6 +74,7 @@
#*****************************************************************************

from sage.misc.cachefunc import cached_method
from sage.misc.superseded import experimental
from sage.structure.element import Element
from sage.structure.parent import Parent
from sage.structure.unique_representation import UniqueRepresentation
Expand Down Expand Up @@ -237,7 +254,7 @@ def prefix_set(self):

class RecognizableSeries(Element):

def __init__(self, parent, mu, left=None, right=None):
def __init__(self, parent, mu, left, right):
r"""
A recognizable series.
Expand All @@ -248,16 +265,14 @@ def __init__(self, parent, mu, left=None, right=None):
``mu`` may be a list or tuple of the same cardinality as the
alphabet as well. See :meth:`mu <mu>` for more details.
- ``left`` -- (default: ``None``) a vector. When evaluating a
- ``left`` -- a vector. When evaluating a
coefficient, this vector is multiplied from the left to the
matrix obtained from :meth:`mu <mu>` applying on a word. If
``None``, then this multiplication is skipped.
matrix obtained from :meth:`mu <mu>` applying on a word.
See :meth`left <left>` for more details.
- ``right`` -- (default: ``None``) a vector. When evaluating a
- ``right`` -- a vector. When evaluating a
coefficient, this vector is multiplied from the right to the
matrix obtained from :meth:`mu <mu>` applying on a word. If
``None``, then this multiplication is skipped.
matrix obtained from :meth:`mu <mu>` applying on a word.
See :meth:`right <right>` for more details.
When created via the parent :class:`RecognizableSeriesSpace`, then
Expand Down Expand Up @@ -310,7 +325,7 @@ def mu(self):
sage: Rec = RecognizableSeriesSpace(ZZ, [0, 1])
sage: M0 = Matrix([[1, 0], [0, 1]])
sage: M1 = Matrix([[0, -1], [1, 2]])
sage: S = Rec((M0, M1), [0, 1])
sage: S = Rec((M0, M1), [0, 1], [1, 1])
sage: S.mu[0] == M0 and S.mu[1] == M1
True
"""
Expand Down Expand Up @@ -444,12 +459,7 @@ def __getitem__(self, w):
sage: S[W(7.digits(2))]
3
"""
result = self._mu_of_word_(w)
if self.left is not None:
result = self.left * result
if self.right is not None:
result = result * self.right
return result
return self.left * self._mu_of_word_(w) * self.right


@cached_method
Expand All @@ -467,12 +477,12 @@ def _mu_of_empty_word_(self):
sage: W = Rec.indices()
sage: M0 = Matrix([[1, 0], [0, 1]])
sage: M1 = Matrix([[0, -1], [1, 2]])
sage: S = Rec({W([0]): M0, W([1]): M1})
sage: S = Rec({W([0]): M0, W([1]): M1}, [0, 1], [1, 1])
sage: S._mu_of_empty_word_()
[1 0]
[0 1]
sage: I = Matrix([[1, 0], [0, 1]])
sage: T = Rec({W([]): I, W([0]): M0, W([1]): M1})
sage: T = Rec({W([]): I, W([0]): M0, W([1]): M1}, [0, 1], [1, 1])
sage: T._mu_of_empty_word_()
[1 0]
[0 1]
Expand Down Expand Up @@ -506,7 +516,7 @@ def _mu_of_word_(self, w):
sage: W = Rec.indices()
sage: M0 = Matrix([[1, 0], [0, 1]])
sage: M1 = Matrix([[0, -1], [1, 2]])
sage: S = Rec((M0, M1))
sage: S = Rec((M0, M1), [0, 1], [1, 1])
sage: S._mu_of_word_(W([0])) == M0
True
sage: S._mu_of_word_(W([1])) == M1
Expand Down Expand Up @@ -596,19 +606,6 @@ def is_trivial_zero(self):
sage: Rec.zero().is_trivial_zero()
True
The following are nonzero as the coefficient of the empty word
is nonzero::
sage: Rec((Matrix([[0, 0], [0, 0]]), Matrix([[0, 0], [0, 0]])),
....: left=None, right=vector([1, 0])).is_trivial_zero()
False
sage: Rec((Matrix([[0, 0], [0, 0]]), Matrix([[0, 0], [0, 0]])),
....: left=None, right=None).is_trivial_zero()
False
sage: Rec((Matrix([[0, 0], [0, 0]]), Matrix([[0, 0], [0, 0]])),
....: left=vector([0, 1]), right=None).is_trivial_zero()
False
The following is zero, but not trivially zero::
sage: S = Rec((Matrix([[1, 0], [0, 0]]), Matrix([[1, 0], [0, 0]])),
Expand All @@ -618,8 +615,7 @@ def is_trivial_zero(self):
sage: S.is_zero()
True
"""
return (self.left is not None and not self.left) or \
(self.right is not None and not self.right) or \
return not self.left or not self.right or \
(all(not self.mu[a] for a in self.parent().alphabet()) and
not self[self.parent().indices()()])

Expand Down Expand Up @@ -698,14 +694,9 @@ def transposed(self):
[0 1], [ 1 5], (0, 1), (1, 0)
)
"""
def tr(M):
try:
return M.transpose()
except AttributeError:
return M
return self.parent()(self.mu.map(tr),
left=tr(self.right),
right=tr(self.left))
return self.parent()(self.mu.map(lambda M: M.transpose()),
left=self.right,
right=self.left)


@cached_method
Expand Down Expand Up @@ -778,17 +769,7 @@ def _minimized_right_(self):
sage: M = S._minimized_right_()
sage: M.mu[0], M.mu[1], M.left, M.right
([0], [0], (2), (1))
::
sage: S = Rec((Matrix([[0, 0], [0, 0]]), Matrix([[0, 0], [0, 0]])))
sage: S._minimized_right_()
Traceback (most recent call last):
...
ValueError: Cannot minmize as 'right' is None.
"""
if self.right is None:
raise ValueError("Cannot minmize as 'right' is None.")
return self.transposed()._minimized_left_().transposed()


Expand Down Expand Up @@ -842,18 +823,7 @@ def _minimized_left_(self):
sage: M = S.minimized()
sage: M.mu[0], M.mu[1], M.left, M.right
([], [], (), ())
::
sage: S = Rec((Matrix([[0, 0], [0, 0]]), Matrix([[0, 0], [0, 0]])))
sage: S._minimized_left_()
Traceback (most recent call last):
...
ValueError: Cannot minmize as 'left' is None.
"""
if self.left is None:
raise ValueError("Cannot minmize as 'left' is None.")

from sage.matrix.constructor import Matrix
from sage.modules.free_module_element import vector
from sage.rings.integer_ring import ZZ
Expand Down Expand Up @@ -896,10 +866,7 @@ def mu_prime_entry(a, p, q, iq):
mu_prime.append(Matrix(M))

left_prime = vector([ZZ(1)] + (len(P)-1)*[ZZ(0)])
if self.right is None:
right_prime = None
else:
right_prime = vector(self[p] for p in P)
right_prime = vector(self[p] for p in P)

return self.parent().element_class(
self.parent(), mu_prime, left_prime, right_prime)
Expand Down Expand Up @@ -1041,6 +1008,7 @@ def __normalize__(cls,
return (coefficients, indices, category)


@experimental(trac_number=21202)
def __init__(self, coefficients, indices, category):
r"""
See :class`RecognizableSeriesSpace` for details.
Expand Down Expand Up @@ -1152,7 +1120,7 @@ def zero(self):
return self(0)


def _element_constructor_(self, mu,
def _element_constructor_(self, data,
left=None, right=None,
transpose=False):
r"""
Expand All @@ -1163,12 +1131,38 @@ def _element_constructor_(self, mu,
TESTS::
sage: Rec = RecognizableSeriesSpace(ZZ, [0, 1])
sage: Rec.zero()
0
sage: type(_)
<class 'sage.combinat.recognizable_series.RecognizableSeriesSpace_with_category.element_class'>
::
sage: M0 = Matrix([[1, 0], [0, 1]])
sage: M1 = Matrix([[0, -1], [1, 2]])
sage: S = Rec((M0, M1), [0, 1], [1, 1])
sage: Rec(S) is S
True
sage: Rec((M0, M1))
Traceback (most recent call last):
...
ValueError: Left or right vector is None.
sage: Rec((M0, M1), [0, 1])
Traceback (most recent call last):
...
ValueError: Left or right vector is None.
sage: Rec((M0, M1), left=[0, 1])
Traceback (most recent call last):
...
ValueError: Left or right vector is None.
sage: Rec((M0, M1), right=[0, 1])
Traceback (most recent call last):
...
ValueError: Left or right vector is None.
"""
if isinstance(mu, int) and mu == 0:
if isinstance(data, int) and data == 0:
from sage.matrix.constructor import Matrix
from sage.modules.free_module_element import vector
from sage.sets.family import Family
Expand All @@ -1177,7 +1171,19 @@ def _element_constructor_(self, mu,
self, Family(self.alphabet(), lambda a: Matrix()),
vector([]), vector([]))

element = self.element_class(self, mu, left, right)
if type(data) == self.element_class and data.parent() == self:
element = data

elif isinstance(data, RecognizableSeries):
element = self.element_class(self, data.mu, data.left, data.right)

else:
mu = data
if left is None or right is None:
raise ValueError('Left or right vector is None.')

element = self.element_class(self, mu, left, right)

if transpose:
return element.transposed()
else:
Expand Down

0 comments on commit 69289fe

Please sign in to comment.