Package igraph :: Module datatypes :: Class Matrix
[hide private]
[frames] | no frames]

Class Matrix

object --+
         |
        Matrix

Simple matrix data type.

Of course there are much more advanced matrix data types for Python (for instance, the ndarray data type of Numeric Python) and this implementation does not want to compete with them. The only role of this data type is to provide a convenient interface for the matrices returned by the Graph object (for instance, allow indexing with tuples in the case of adjacency matrices and so on).

Instance Methods [hide private]
 
__getitem__(self, i)
Returns a single item, a row or a column of the matrix
 
__init__(self, data=None)
Initializes a matrix.
 
__iter__(self)
Support for iteration.
 
__plot__(self, context, bbox, palette, *args, **kwds)
Plots the matrix to the given Cairo context in the given box
 
__repr__(self)
repr(x)
 
__setitem__(self, i, value)
Sets a single item, a row or a column of the matrix
 
__str__(self)
str(x)
 
_get_data(self)
Returns the data stored in the matrix as a list of lists
 
_get_shape(self)
Returns the shape of the matrix
 
_set_data(self, data=None)
Sets the data stored in the matrix
 
max(self, dim=None)
Returns the maximum of the matrix along the given dimension
 
min(self, dim=None)
Returns the minimum of the matrix along the given dimension

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

Class Methods [hide private]
 
Fill(klass, value, *args)
Creates a matrix filled with the given value
 
Identity(klass, *args)
Creates an identity matrix.
 
Zero(klass, *args)
Creates a matrix filled with zeros.
Properties [hide private]
  data
Elements of the matrix as a list of lists
  shape
Shape of the matrix as a tuple

Inherited from object: __class__

Method Details [hide private]

Fill(klass, value, *args)
Class Method

 

Creates a matrix filled with the given value

Parameters:
  • value - the value to be used
  • shape - the shape of the matrix. Can be a single integer, two integers or a tuple. If a single integer is given here, the matrix is assumed to be square-shaped.

Identity(klass, *args)
Class Method

 

Creates an identity matrix.

Parameters:
  • shape - the shape of the matrix. Can be a single integer, two integers or a tuple. If a single integer is given here, the matrix is assumed to be square-shaped.

Zero(klass, *args)
Class Method

 

Creates a matrix filled with zeros.

Parameters:
  • shape - the shape of the matrix. Can be a single integer, two integers or a tuple. If a single integer is given here, the matrix is assumed to be square-shaped.

__getitem__(self, i)
(Indexing operator)

 

Returns a single item, a row or a column of the matrix

Parameters:
  • i - if a single integer, returns the ith row as a list. If a slice, returns the corresponding rows as another Matrix object. If a 2-tuple, the first element of the tuple is used to select a row and the second is used to select a column.

__init__(self, data=None)
(Constructor)

 

Initializes a matrix.

Parameters:
  • data - the elements of the matrix as a list of lists, or None to create a 0x0 matrix.
Overrides: object.__init__

__iter__(self)

 

Support for iteration.

This is actually implemented as a generator, so there is no need for a separate iterator class. The generator returns copies of the rows in the matrix as lists to avoid messing around with the internals. Feel free to do anything with the copies, the changes won't be reflected in the original matrix.

__plot__(self, context, bbox, palette, *args, **kwds)

 

Plots the matrix to the given Cairo context in the given box

Besides the usual self-explanatory plotting parameters (context, bbox, palette), it accepts the following keyword arguments:

  • style: the style of the plot. boolean is useful for plotting matrices with boolean (True/False or 0/1) values: False will be shown with a white box and True with a black box. palette uses the given palette to represent numbers by colors, the minimum will be assigned to palette color index 0 and the maximum will be assigned to the length of the palette. The default style is boolean (but it may change in the future). None values in the matrix are treated specially in both cases: nothing is drawn in the cell corresponding to None.
  • square: whether the cells of the matrix should be square or not. Default is True.
  • grid_width: line width of the grid shown on the matrix. If zero or negative, the grid is turned off. The grid is also turned off if the size of a cell is less than three times the given line width. Default is 1. Fractional widths are also allowed.
  • border_width: line width of the border shown around the matrix. If zero or negative, the border is turned off. Default is 1.
  • row_names: the names of the rows
  • col_names: the names of the columns.
  • values: values to be displayed in the cells. If None or False, no values are displayed. If True, the values come from the matrix being plotted. If it is another matrix, the values of that matrix are shown in the cells. In this case, the shape of the value matrix must match the shape of the matrix being plotted.
  • value_format: a format string that specifies how the values should be plotted. Example: "%#.2f" for floating-point numbers with always exactly two digits after the decimal point. See the Python documentation of the % operator for details on the format string. If the format string is not given, it defaults to "%s" and an implicit conversion to strings is performed on the elements of the value matrix.

If only the row names or the column names are given and the matrix is square-shaped, the same names are used for both column and row names.

__repr__(self)
(Representation operator)

 

repr(x)

Overrides: object.__repr__
(inherited documentation)

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

 

Sets a single item, a row or a column of the matrix

Parameters:
  • i - if a single integer, sets the ith row as a list. If a slice, sets the corresponding rows from another Matrix object. If a 2-tuple, the first element of the tuple is used to select a row and the second is used to select a column.
  • value - the new value

__str__(self)
(Informal representation operator)

 

str(x)

Overrides: object.__str__
(inherited documentation)

max(self, dim=None)

 

Returns the maximum of the matrix along the given dimension

Parameters:
  • dim - the dimension. 0 means determining the column maximums, 1 means determining the row maximums. If None, the global maximum is returned.

min(self, dim=None)

 

Returns the minimum of the matrix along the given dimension

Parameters:
  • dim - the dimension. 0 means determining the column minimums, 1 means determining the row minimums. If None, the global minimum is returned.

Property Details [hide private]

data

Elements of the matrix as a list of lists

Get Method:
_get_data(self) - Returns the data stored in the matrix as a list of lists
Set Method:
_set_data(self, data=None) - Sets the data stored in the matrix

shape

Shape of the matrix as a tuple

Get Method:
_get_shape(self) - Returns the shape of the matrix