python-igraph manual

For using igraph from Python

   Home       Trees       Indices       Help   
Package igraph :: Module datatypes :: Class Matrix
[hide private]

Class Matrix

source code

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]
 
__init__(self, data=None)
Initializes a matrix.
source code
 
_set_data(self, data=None)
Sets the data stored in the matrix
source code
 
_get_data(self)
Returns the data stored in the matrix as a list of lists
source code
 
__add__(self, other)
Adds the given value to the matrix.
source code
 
__eq__(self, other)
Checks whether a given matrix is equal to another one
source code
 
__getitem__(self, i)
Returns a single item, a row or a column of the matrix
source code
 
__hash__(self)
Returns a hash value for a matrix.
source code
 
__iadd__(self, other)
In-place addition of a matrix or scalar.
source code
 
__isub__(self, other)
In-place subtraction of a matrix or scalar.
source code
 
__ne__(self, other)
Checks whether a given matrix is not equal to another one
source code
 
__setitem__(self, i, value)
Sets a single item, a row or a column of the matrix
source code
 
__sub__(self, other)
Subtracts the given value from the matrix.
source code
 
__repr__(self)
repr(x)
source code
 
__str__(self)
str(x)
source code
 
__iter__(self)
Support for iteration.
source code
 
__plot__(self, context, bbox, palette, **kwds)
Plots the matrix to the given Cairo context in the given box
source code
 
min(self, dim=None)
Returns the minimum of the matrix along the given dimension
source code
 
max(self, dim=None)
Returns the maximum of the matrix along the given dimension
source code

Inherited from object: __delattr__, __format__, __getattribute__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __subclasshook__

Class Methods [hide private]
 
Fill(cls, value, *args)
Creates a matrix filled with the given value
source code
 
Zero(cls, *args)
Creates a matrix filled with zeros.
source code
 
Identity(cls, *args)
Creates an identity matrix.
source code
Properties [hide private]
  data
Returns the data stored in the matrix as a list of lists
  shape
Returns the shape of the matrix as a tuple

Inherited from object: __class__

Method Details [hide private]

__init__(self, data=None)
(Constructor)

source code 

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__

Fill(cls, value, *args)
Class Method

source code 

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.

Zero(cls, *args)
Class Method

source code 

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.

Identity(cls, *args)
Class Method

source code 

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.

__add__(self, other)
(Addition operator)

source code 

Adds the given value to the matrix.

Parameters:
  • other - either a scalar or a matrix. Scalars will be added to each element of the matrix. Matrices will be added together elementwise.
Returns:
the result matrix

__getitem__(self, i)
(Indexing operator)

source code 

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.

__hash__(self)
(Hashing function)

source code 

Returns a hash value for a matrix.

Overrides: object.__hash__

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

source code 

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

__sub__(self, other)
(Subtraction operator)

source code 

Subtracts the given value from the matrix.

Parameters:
  • other - either a scalar or a matrix. Scalars will be subtracted from each element of the matrix. Matrices will be subtracted together elementwise.
Returns:
the result matrix

__repr__(self)
(Representation operator)

source code 

repr(x)

Overrides: object.__repr__
(inherited documentation)

__str__(self)
(Informal representation operator)

source code 

str(x)

Overrides: object.__str__
(inherited documentation)

__iter__(self)

source code 

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, **kwds)

source code 

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. None draws transparent cell backgrounds only. 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 drawn 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 or a callable that specifies how the values should be plotted. If it is a callable, it must be a function that expects a single value and returns a string. 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 the str function.

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.

min(self, dim=None)

source code 

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.

max(self, dim=None)

source code 

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.

Property Details [hide private]

data

Returns the data stored in 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

Returns the shape of the matrix as a tuple

Get Method:
unreachable.shape(self) - Returns the shape of the matrix as a tuple

   Home       Trees       Indices       Help