viff

changeset 704:0ec32944ab86

Converted viff.matrix too.
author Martin Geisler <mg@daimi.au.dk>
date Tue, 22 Apr 2008 12:39:11 +0200
parents dc4319f5dd24
children d2fd45609a54
files doc/implementation.txt doc/matrix.txt viff/matrix.py
diffstat 3 files changed, 26 insertions(+), 46 deletions(-) [+]
line diff
     1.1 --- a/doc/implementation.txt	Tue Apr 22 12:16:17 2008 +0200
     1.2 +++ b/doc/implementation.txt	Tue Apr 22 12:39:11 2008 +0200
     1.3 @@ -8,3 +8,4 @@
     1.4  
     1.5     field
     1.6     shamir
     1.7 +   matrix
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/doc/matrix.txt	Tue Apr 22 12:39:11 2008 +0200
     2.3 @@ -0,0 +1,10 @@
     2.4 +
     2.5 +Matrix Module
     2.6 +=============
     2.7 +
     2.8 +.. automodule:: viff.matrix
     2.9 +
    2.10 +   .. autoclass:: viff.matrix.Matrix
    2.11 +      :members: __add__, __mul__, __str__, transpose, determinant
    2.12 +
    2.13 +   .. autofunction:: viff.matrix.hyper
     3.1 --- a/viff/matrix.py	Tue Apr 22 12:16:17 2008 +0200
     3.2 +++ b/viff/matrix.py	Tue Apr 22 12:39:11 2008 +0200
     3.3 @@ -20,9 +20,12 @@
     3.4  This module contains basic matrix operations as well as a function to
     3.5  build square hyper-invertible matrices. The matrix implementation
     3.6  provides operator overloading and works with any type that acts like a
     3.7 -number, including L{viff.field.GF256} and L{viff.field.GF} elements.
     3.8 +number, including :class:`viff.field.GF256` and :func:`viff.field.GF`
     3.9 +elements.
    3.10 +"""
    3.11  
    3.12 -"""
    3.13 +__docformat__ = "restructuredtext"
    3.14 +
    3.15  
    3.16  from __future__ import division
    3.17  
    3.18 @@ -31,11 +34,7 @@
    3.19      """A matrix."""
    3.20  
    3.21      def _init_zeros(self, m, n):
    3.22 -        """Initialize a new m times n matrix containing zeros.
    3.23 -
    3.24 -        @param m: The number of rows.
    3.25 -        @param n: The number of columns.
    3.26 -        """
    3.27 +        """Initialize a new zero matrix with *m* rows and *n* columns."""
    3.28          self.rows = [[0 for _ in range(n)] for _ in range(m)]
    3.29          self.m = m
    3.30          self.n = n
    3.31 @@ -43,7 +42,7 @@
    3.32      def _init_set(self, rows):
    3.33          """Initializes a matrix to contain specific values.
    3.34  
    3.35 -        @param rows: The rows of the matrix, given as a list of lists.
    3.36 +        The *rows* is a list of lists.
    3.37          """
    3.38          self.rows = rows
    3.39          self.m = len(rows)
    3.40 @@ -52,9 +51,9 @@
    3.41      def __init__(self, *args):
    3.42          """Initializates a matrix.
    3.43  
    3.44 -        @param args: Either a number m and n counting rows and columns
    3.45 -        of an all-zero matrix, or a list of lists representing the
    3.46 -        rows of the matrix.
    3.47 +        The arguments can be either a number *m* and *n* counting rows
    3.48 +        and columns of an all-zero matrix, or a list of lists
    3.49 +        representing the rows of the matrix.
    3.50          """
    3.51          if len(args) == 1:
    3.52              self._init_set(*args)
    3.53 @@ -62,7 +61,7 @@
    3.54              self._init_zeros(*args)
    3.55  
    3.56      def __setitem__(self, (i, j), value):
    3.57 -        """Allows matrix entry assignment using C{[,]}.
    3.58 +        """Allows matrix entry assignment using ``[,]``.
    3.59  
    3.60          The assignment works as follows:
    3.61  
    3.62 @@ -71,24 +70,17 @@
    3.63          >>> print M
    3.64          [[ 0 42]
    3.65           [ 0  0]]
    3.66 -
    3.67 -        param i: The entry row.
    3.68 -        param j: The entry column.
    3.69 -        param value: The value to store in the entry.
    3.70          """
    3.71          self.rows[i][j] = value
    3.72  
    3.73      def __getitem__(self, (i, j)):
    3.74 -        """Allows matrix entry access using C{[, ]}.
    3.75 +        """Allows matrix entry access using ``[, ]``.
    3.76  
    3.77          The access works as follows:
    3.78  
    3.79          >>> M = Matrix([[1, 2], [3, 4]])
    3.80          >>> print M[1,1]
    3.81          4
    3.82 -
    3.83 -        @param i: The entry row.
    3.84 -        @param j: The entry column.
    3.85          """
    3.86          return self.rows[i][j]
    3.87  
    3.88 @@ -108,9 +100,6 @@
    3.89          >>> print A + A
    3.90          [[0 2]
    3.91           [4 6]]
    3.92 -
    3.93 -        @param other: The matrix or element to add to this one.
    3.94 -        @return: The sum.
    3.95          """
    3.96          # we should check that the two matrices have the same size
    3.97          result = Matrix(self.m, self.n)
    3.98 @@ -133,9 +122,6 @@
    3.99          >>> print 10 + Matrix([[0, 1], [2, 3]])
   3.100          [[10 11]
   3.101           [12 13]]
   3.102 -
   3.103 -        @param other: The element to which the matrix will be added.
   3.104 -        @return: The sum.
   3.105          """
   3.106          result = Matrix(self.m, self.n)
   3.107          for i in range(0, self.m):
   3.108 @@ -166,9 +152,6 @@
   3.109          Traceback (most recent call last):
   3.110              ...
   3.111          ValueError: Matrix dimensions do not match for multiplication
   3.112 -
   3.113 -        @param other: The matrix or element to multiply with this one.
   3.114 -        @return: The product.
   3.115          """
   3.116  
   3.117          if not isinstance(other, Matrix):
   3.118 @@ -198,9 +181,6 @@
   3.119          >>> print 10 * Matrix([[0, 1], [2, 3]])
   3.120          [[ 0 10]
   3.121           [20 30]]
   3.122 -
   3.123 -        @param other: The element with which the matrix will be multiplied.
   3.124 -        @return: The product.
   3.125          """
   3.126          result = Matrix(self.m, self.n)
   3.127          for i in range(0, self.m):
   3.128 @@ -216,8 +196,6 @@
   3.129           [ 4  5  6  7]
   3.130           [ 8  9 10 11]
   3.131           [12 13 14 15]]
   3.132 -
   3.133 -        @return: A string representation of the matrix.
   3.134          """
   3.135          width = max([len(str(elem)) for row in self.rows for elem in row])
   3.136          output = [" ".join(["%*s" % (width, e) for e in r]) for r in self.rows]
   3.137 @@ -236,8 +214,6 @@
   3.138          [[0 3 6]
   3.139           [1 4 7]
   3.140           [2 5 8]]
   3.141 -
   3.142 -        @return: The transpose of the matrix.
   3.143          """
   3.144          result = Matrix(self.n, self.m)
   3.145          for i in range(self.m):
   3.146 @@ -247,11 +223,7 @@
   3.147          return result
   3.148  
   3.149      def determinant(mat):
   3.150 -        """Calculates the determinant of a matrix.
   3.151 -
   3.152 -        @param mat: A square matrix.
   3.153 -        @return: The determinant of the matrix.
   3.154 -        """
   3.155 +        """Calculates the determinant of a square matrix."""
   3.156          if mat.m == 1:
   3.157              return mat[0, 0]
   3.158          if mat.m == 2:
   3.159 @@ -271,7 +243,8 @@
   3.160  
   3.161  
   3.162  def hyper(n, field):
   3.163 -    """Makes a hyper-invertible square matrix.
   3.164 +    """Makes a *n* times *n* hyper-invertible square matrix.
   3.165 +    The matrix entries will belong to *field*.
   3.166  
   3.167      A hyper-invertible matrix is a matrix where every sub-matrix is
   3.168      invertible. A sub-matrix consists of an arbitrary subset of the
   3.169 @@ -287,10 +260,6 @@
   3.170      [[ {1} {44}  {3}]
   3.171       [ {3} {39}  {6}]
   3.172       [ {6} {32} {10}]]
   3.173 -
   3.174 -    @param n: The dimension of the matrix (it will be n times n).
   3.175 -    @param field: The field to use. Expected to be a Zp field.
   3.176 -    @return: A hyper-invertible square matrix.
   3.177      """
   3.178      result = Matrix(n, n)
   3.179      for i in range(0, n):