Matrix Module

Matrix operations. 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 viff.field.GF256 and viff.field.GF() elements.

class viff.matrix.Matrix(*args)

A matrix.

__add__(other)

Adds another matrix or an element to this matrix.

Adds this matrix with another matrix, or adds the matrix with an element. The addition is done element-wise.

>>> A = Matrix([[x + 2*y for x in range(2)] for y in range(2)])
>>> print A
[[0 1]
 [2 3]]
>>> print A + 10
[[10 11]
 [12 13]]
>>> print A + A
[[0 2]
 [4 6]]
__getitem__((i, j))

Allows matrix entry access using z = M[x, y].

The access works as follows:

>>> M = Matrix([[1, 2], [3, 4]])
>>> print M[1,1]
4
__init__(*args)

Initializates a 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.

__mul__(other)

Matrix multiplication.

Multiplies this matrix with another matrix, or multiplies the matrix with an element.

>>> A = Matrix([[x + 2*y for x in range(2)] for y in range(2)])
>>> print A
[[0 1]
 [2 3]]
>>> print A * 10
[[ 0 10]
 [20 30]]
>>> print A * A
[[ 2  3]
 [ 6 11]]

The matrices must have compatible dimensions:

>>> Matrix(1, 5) * Matrix(2, 3)
Traceback (most recent call last):
    ...
ValueError: Matrix dimensions do not match for multiplication
__setitem__((i, j), value)

Allows matrix entry assignment using M[x, y] = z.

The assignment works as follows:

>>> M = Matrix(2, 2)
>>> M[0, 1] = 42
>>> print M
[[ 0 42]
 [ 0  0]]
determinant(mat)

Calculates the determinant of a square matrix.

transpose()

Returns the transpose of the matrix.

>>> M = Matrix([[x + 3*y for x in range(3)] for y in range(3)])
>>> print M
[[0 1 2]
 [3 4 5]
 [6 7 8]]
>>> print M.transpose()
[[0 3 6]
 [1 4 7]
 [2 5 8]]
viff.matrix.hyper(n, field)

Makes an 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 rows and columns of the original matrix (and is not necessarily a contiguous region).

>>> from field import GF
>>> Zp = GF(47)
>>> print hyper(2, Zp)
[[{46}  {2}]
 [{45}  {3}]]
>>> print hyper(3, Zp)
[[ {1} {44}  {3}]
 [ {3} {39}  {6}]
 [ {6} {32} {10}]]

Previous topic

Shamir Module

Next topic

Runtime Module

This Page