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 wrap: on
line diff
--- a/doc/implementation.txt	Tue Apr 22 12:16:17 2008 +0200
+++ b/doc/implementation.txt	Tue Apr 22 12:39:11 2008 +0200
@@ -8,3 +8,4 @@
 
    field
    shamir
+   matrix
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/doc/matrix.txt	Tue Apr 22 12:39:11 2008 +0200
@@ -0,0 +1,10 @@
+
+Matrix Module
+=============
+
+.. automodule:: viff.matrix
+
+   .. autoclass:: viff.matrix.Matrix
+      :members: __add__, __mul__, __str__, transpose, determinant
+
+   .. autofunction:: viff.matrix.hyper
--- a/viff/matrix.py	Tue Apr 22 12:16:17 2008 +0200
+++ b/viff/matrix.py	Tue Apr 22 12:39:11 2008 +0200
@@ -20,9 +20,12 @@
 This module contains basic matrix operations as well as a function to
 build square hyper-invertible matrices. The matrix implementation
 provides operator overloading and works with any type that acts like a
-number, including L{viff.field.GF256} and L{viff.field.GF} elements.
+number, including :class:`viff.field.GF256` and :func:`viff.field.GF`
+elements.
+"""
 
-"""
+__docformat__ = "restructuredtext"
+
 
 from __future__ import division
 
@@ -31,11 +34,7 @@
     """A matrix."""
 
     def _init_zeros(self, m, n):
-        """Initialize a new m times n matrix containing zeros.
-
-        @param m: The number of rows.
-        @param n: The number of columns.
-        """
+        """Initialize a new zero matrix with *m* rows and *n* columns."""
         self.rows = [[0 for _ in range(n)] for _ in range(m)]
         self.m = m
         self.n = n
@@ -43,7 +42,7 @@
     def _init_set(self, rows):
         """Initializes a matrix to contain specific values.
 
-        @param rows: The rows of the matrix, given as a list of lists.
+        The *rows* is a list of lists.
         """
         self.rows = rows
         self.m = len(rows)
@@ -52,9 +51,9 @@
     def __init__(self, *args):
         """Initializates a matrix.
 
-        @param args: Either a number m and n counting rows and columns
-        of an all-zero matrix, or a list of lists representing the
-        rows of the matrix.
+        The arguments can be either a number *m* and *n* counting rows
+        and columns of an all-zero matrix, or a list of lists
+        representing the rows of the matrix.
         """
         if len(args) == 1:
             self._init_set(*args)
@@ -62,7 +61,7 @@
             self._init_zeros(*args)
 
     def __setitem__(self, (i, j), value):
-        """Allows matrix entry assignment using C{[,]}.
+        """Allows matrix entry assignment using ``[,]``.
 
         The assignment works as follows:
 
@@ -71,24 +70,17 @@
         >>> print M
         [[ 0 42]
          [ 0  0]]
-
-        param i: The entry row.
-        param j: The entry column.
-        param value: The value to store in the entry.
         """
         self.rows[i][j] = value
 
     def __getitem__(self, (i, j)):
-        """Allows matrix entry access using C{[, ]}.
+        """Allows matrix entry access using ``[, ]``.
 
         The access works as follows:
 
         >>> M = Matrix([[1, 2], [3, 4]])
         >>> print M[1,1]
         4
-
-        @param i: The entry row.
-        @param j: The entry column.
         """
         return self.rows[i][j]
 
@@ -108,9 +100,6 @@
         >>> print A + A
         [[0 2]
          [4 6]]
-
-        @param other: The matrix or element to add to this one.
-        @return: The sum.
         """
         # we should check that the two matrices have the same size
         result = Matrix(self.m, self.n)
@@ -133,9 +122,6 @@
         >>> print 10 + Matrix([[0, 1], [2, 3]])
         [[10 11]
          [12 13]]
-
-        @param other: The element to which the matrix will be added.
-        @return: The sum.
         """
         result = Matrix(self.m, self.n)
         for i in range(0, self.m):
@@ -166,9 +152,6 @@
         Traceback (most recent call last):
             ...
         ValueError: Matrix dimensions do not match for multiplication
-
-        @param other: The matrix or element to multiply with this one.
-        @return: The product.
         """
 
         if not isinstance(other, Matrix):
@@ -198,9 +181,6 @@
         >>> print 10 * Matrix([[0, 1], [2, 3]])
         [[ 0 10]
          [20 30]]
-
-        @param other: The element with which the matrix will be multiplied.
-        @return: The product.
         """
         result = Matrix(self.m, self.n)
         for i in range(0, self.m):
@@ -216,8 +196,6 @@
          [ 4  5  6  7]
          [ 8  9 10 11]
          [12 13 14 15]]
-
-        @return: A string representation of the matrix.
         """
         width = max([len(str(elem)) for row in self.rows for elem in row])
         output = [" ".join(["%*s" % (width, e) for e in r]) for r in self.rows]
@@ -236,8 +214,6 @@
         [[0 3 6]
          [1 4 7]
          [2 5 8]]
-
-        @return: The transpose of the matrix.
         """
         result = Matrix(self.n, self.m)
         for i in range(self.m):
@@ -247,11 +223,7 @@
         return result
 
     def determinant(mat):
-        """Calculates the determinant of a matrix.
-
-        @param mat: A square matrix.
-        @return: The determinant of the matrix.
-        """
+        """Calculates the determinant of a square matrix."""
         if mat.m == 1:
             return mat[0, 0]
         if mat.m == 2:
@@ -271,7 +243,8 @@
 
 
 def hyper(n, field):
-    """Makes a hyper-invertible square matrix.
+    """Makes a *n* times *n* hyper-invertible square matrix.
+    The matrix entries will belong to *field*.
 
     A hyper-invertible matrix is a matrix where every sub-matrix is
     invertible. A sub-matrix consists of an arbitrary subset of the
@@ -287,10 +260,6 @@
     [[ {1} {44}  {3}]
      [ {3} {39}  {6}]
      [ {6} {32} {10}]]
-
-    @param n: The dimension of the matrix (it will be n times n).
-    @param field: The field to use. Expected to be a Zp field.
-    @return: A hyper-invertible square matrix.
     """
     result = Matrix(n, n)
     for i in range(0, n):