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

Commit

Permalink
extend to negative shifts in subsequence
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrenn committed Aug 23, 2016
1 parent d8a9173 commit 01495cb
Showing 1 changed file with 87 additions and 5 deletions.
92 changes: 87 additions & 5 deletions src/sage/combinat/k_regular_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ def subsequence(self, a, b, minimize=True):
INPUT:
- ``a``, ``b`` -- integers.
- ``a`` -- a nonnegative integer.
- ``b`` -- an integer.
- ``minimize`` -- (default: ``True``) a boolean. If set, then
:meth:`minimized` is called after the addition.
Expand Down Expand Up @@ -289,18 +291,83 @@ def subsequence(self, a, b, minimize=True):
sage: C.subsequence(3, 2)
2-regular sequence 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, ...
::
sage: S = C.subsequence(1, -1)
sage: S
2-regular sequence 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, ...
sage: S.mu[0], S.mu[1], S.left, S.right
(
[ 0 1 0] [ -2 2 0]
[-2 3 0] [ 0 0 1]
[-4 4 1], [ 12 -12 5], (1, 0, 0), (0, 0, 1)
)
TESTS::
sage: C.subsequence(0, 4)
2-regular sequence 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, ...
sage: C.subsequence(1, 0) is C
True
The following test that the range for `c` in the code
is sufficient::
sage: C.subsequence(1, -1, minimize=False)
2-regular sequence 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, ...
sage: C.subsequence(1, -2, minimize=False)
2-regular sequence 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, ...
sage: C.subsequence(2, -1, minimize=False)
2-regular sequence 0, 1, 3, 5, 7, 9, 11, 13, 15, 17, ...
sage: C.subsequence(2, -2, minimize=False)
2-regular sequence 0, 0, 2, 4, 6, 8, 10, 12, 14, 16, ...
sage: C.subsequence(2, 21, minimize=False)
2-regular sequence 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, ...
sage: C.subsequence(2, 20, minimize=False)
2-regular sequence 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, ...
sage: C.subsequence(2, 19, minimize=False)
2-regular sequence 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, ...
sage: C.subsequence(2, -9, minimize=False)
2-regular sequence 0, 0, 0, 0, 0, 1, 3, 5, 7, 9, ...
sage: C.subsequence(3, 21, minimize=False)
2-regular sequence 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, ...
sage: C.subsequence(3, 20, minimize=False)
2-regular sequence 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, ...
sage: C.subsequence(3, 19, minimize=False)
2-regular sequence 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, ...
sage: C.subsequence(3, 18, minimize=False)
2-regular sequence 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, ...
sage: C.subsequence(10, 2, minimize=False)
2-regular sequence 2, 12, 22, 32, 42, 52, 62, 72, 82, 92, ...
sage: C.subsequence(10, 1, minimize=False)
2-regular sequence 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, ...
sage: C.subsequence(10, 0, minimize=False)
2-regular sequence 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, ...
sage: C.subsequence(10, -1, minimize=False)
2-regular sequence 0, 9, 19, 29, 39, 49, 59, 69, 79, 89, ...
sage: C.subsequence(10, -2, minimize=False)
2-regular sequence 0, 8, 18, 28, 38, 48, 58, 68, 78, 88, ...
::
sage: C.subsequence(-1, 0)
Traceback (most recent call last):
...
ValueError: a=-1 is not nonnegative.
"""
from sage.rings.integer_ring import ZZ
a = ZZ(a)
b = ZZ(b)

if a == 0:
return self[b] * self.parent().one_hadamard()
if a == 1 and b == 0:
elif a == 1 and b == 0:
return self
elif a < 0:
raise ValueError('a={} is not nonnegative.'.format(a))

from sage.arith.srange import srange
from sage.matrix.constructor import Matrix
Expand All @@ -309,11 +376,25 @@ def subsequence(self, a, b, minimize=True):

k = self.parent().k
dim = self.dimension()
kernel = tuple((i, c) for c in srange(a+b) for i in srange(dim))
min_c = min(b, 0)
max_c = max(a, a + (b-1)//k + 1)
range_c = srange(min_c, max_c)
# We use the 'if' below instead of the less efficient
# max_c = max(a, a + b)
# or
# max_c = max(a, a + (b-1)//k + 1, b + 1)
# above.
if b not in range_c:
range_c.append(b)
b = max_c
kernel = tuple((i, c) for c in range_c for i in srange(dim))

def pad(T, d):
return d*dim*(0,) + T + (a+b-1-d)*dim*(0,)
dd = d - min_c
return dd*dim*(0,) + T + (len(range_c)-1-dd)*dim*(0,)
def mu_line(r, i, c):
d, f = (a*r + c).quo_rem(k)
assert d in range_c
return pad(tuple(self.mu[f].rows()[i]), d)

result = P.element_class(
Expand All @@ -322,7 +403,8 @@ def mu_line(r, i, c):
for r in srange(k)),
vector(pad(tuple(self.left), b)),
vector(sum((tuple(self.__getitem__(c, multiply_left=False))
for c in srange(a+b)), tuple())))
if c >= 0 else dim*(ZZ(0),)
for c in range_c), tuple())))

if minimize:
return result.minimized()
Expand Down

0 comments on commit 01495cb

Please sign in to comment.