Package viff :: Module matrix :: Class Matrix
[hide private]
[frames] | no frames]

Class Matrix

source code


A matrix.
Instance Methods [hide private]
 
_init_zeros(self, m, n)
Initialize a new zero matrix with m rows and n columns.
source code
 
_init_set(self, rows)
Initializes a matrix to contain specific values.
source code
 
__init__(self, *args)
Initializates a matrix.
source code
 
__setitem__(self, (i, j), value)
Allows matrix entry assignment using [,].
source code
 
__getitem__(self, (i, j))
Allows matrix entry access using [, ].
source code
 
__add__(self, other)
Adds another matrix or an element to this matrix.
source code
 
__radd__(self, other)
Adds the matrix to an element.
source code
 
__mul__(self, other)
Matrix multiplication.
source code
 
__rmul__(self, other)
Multiplies an element with the matrix.
source code
 
__str__(self)
Returns a string representation of the matrix.
source code
 
transpose(self)
Returns the transpose of the matrix.
source code
 
determinant(mat)
Calculates the determinant of a square matrix.
source code

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

_init_set(self, rows)

source code 

Initializes a matrix to contain specific values.

The rows is a list of lists.

__init__(self, *args)
(Constructor)

source code 

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.

Overrides: object.__init__

__setitem__(self, (i, j), value)
(Index assignment operator)

source code 

Allows matrix entry assignment using [,].

The assignment works as follows:

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

__getitem__(self, (i, j))
(Indexing operator)

source code 

Allows matrix entry access using [, ].

The access works as follows:

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

__add__(self, other)
(Addition operator)

source code 

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]]

__mul__(self, other)

source code 

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

__str__(self)
(Informal representation operator)

source code 

Returns a string representation of the matrix.

>>> print Matrix([[x + 4*y for x in range(4)] for y in range(4)])
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
Overrides: object.__str__