Skip to content

Commit

Permalink
doctests for integer matrices.
Browse files Browse the repository at this point in the history
  • Loading branch information
malb committed Jul 13, 2017
1 parent c4ad5fd commit 0a3958c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
52 changes: 49 additions & 3 deletions src/fpylll/fplll/integer_matrix.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ cdef class IntegerMatrix:
"""
Dense matrices over the Integers.
"""

def __init__(self, arg0, arg1=None, int_type="mpz"):
"""Construct a new integer matrix
Expand Down Expand Up @@ -619,11 +620,30 @@ cdef class IntegerMatrix:
return A

def set_matrix(self, A):
"""Set this matrix from matrix-like object A
"""Set this matrix from matrix-like object A.
:param A: a matrix like object, with element access A[i,j] or A[i][j]
.. warning:: entries starting at ``A[nrows, ncols]`` are ignored.
Example::
>>> z = [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]]
>>> A = IntegerMatrix(4, 4)
>>> A.set_matrix(z)
>>> print(A)
[ 1 2 3 4 ]
[ 5 6 7 8 ]
[ 9 10 11 12 ]
[ 13 14 15 16 ]
>>> A = IntegerMatrix(3, 3)
>>> A.set_matrix(z)
>>> print(A)
[ 1 2 3 ]
[ 5 6 7 ]
[ 9 10 11 ]
.. warning:: entries starting from ``A[nrows, ncols]`` are ignored.
"""
cdef int i, j
Expand All @@ -639,12 +659,29 @@ cdef class IntegerMatrix:
for j in range(n):
self._set(i, j, A[i][j])


def set_iterable(self, A):
"""Set this matrix from iterable A
:param A: an iterable object such as a list or tuple
EXAMPLE::
>>> z = range(16)
>>> A = IntegerMatrix(4, 4)
>>> A.set_iterable(z)
>>> print(A)
[ 0 1 2 3 ]
[ 4 5 6 7 ]
[ 8 9 10 11 ]
[ 12 13 14 15 ]
>>> A = IntegerMatrix(3, 3)
>>> A.set_iterable(z)
>>> print(A)
[ 0 1 2 ]
[ 3 4 5 ]
[ 6 7 8 ]
.. warning:: entries starting at ``A[nrows * ncols]`` are ignored.
"""
Expand All @@ -663,6 +700,15 @@ cdef class IntegerMatrix:
:param A: a matrix like object, with element access A[i,j] or A[i][j]
:returns: A
Example::
>>> from fpylll import set_random_seed
>>> z = [[0 for _ in range(10)] for _ in range(10)]
>>> A = IntegerMatrix.random(10, "qary", q=127, k=5)
>>> _ = A.to_matrix(z)
>>> z[0] == list(A[0])
True
"""
cdef int i, j
cdef int m = self._nrows()
Expand Down
15 changes: 9 additions & 6 deletions src/fpylll/fplll/sieve_gauss.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ Gaussian Sieving was proposed in Micciancio, D., & Voulgaris, P. (2010). Faste
algorithms for the shortest vector problem. In M. Charika, 21st SODA (pp. 1468–1480). :
ACM-SIAM.
>>> from fpylll import IntegerMatrix, GaussSieve, SVP, LLL
>>> A = IntegerMatrix.random(30, "qary", k=15, q=127); A = LLL.reduction(A)
>>> w = SVP.shortest_vector(A)
>>> v = GaussSieve(A, algorithm=2)()
>>> sum([w_**2 for w_ in w]) == sum([v_**2 for v_ in v])
True
Example::
>>> from fpylll import IntegerMatrix, GaussSieve, SVP, LLL, set_random_seed
>>> set_random_seed(1337)
>>> A = IntegerMatrix.random(30, "qary", k=15, q=127); A = LLL.reduction(A)
>>> w = SVP.shortest_vector(A)
>>> v = GaussSieve(A, algorithm=2)()
>>> sum([w_**2 for w_ in w]) == sum([v_**2 for v_ in v])
True
"""

Expand Down

0 comments on commit 0a3958c

Please sign in to comment.