# ``igraph`.`datatypes`.`Matrix``

class documentation

`class Matrix: (source)`

Constructor: `Matrix(data)`

View In Hierarchy

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).

Class Method | `Fill` | Creates a matrix filled with the given value
Class Method | `Identity` | Creates an identity matrix.
Class Method | `Zero` | Creates a matrix filled with zeros.
Method | `__add__` | Adds the given value to the matrix.
Method | `__eq__` | Checks whether a given matrix is equal to another one
Method | `__getitem__` | Returns a single item, a row or a column of the matrix
Method | `__hash__` | Returns a hash value for a matrix.
Method | `__iadd__` | In-place addition of a matrix or scalar.
Method | `__init__` | Initializes a matrix.
Method | `__isub__` | In-place subtraction of a matrix or scalar.
Method | `__iter__` | Support for iteration.
Method | `__len__` | Returns the number of rows in the matrix.
Method | `__ne__` | Checks whether a given matrix is not equal to another one
Method | `__plot__` | Plots the matrix to the given Cairo context or matplotlib Axes.
Method | `__repr__` | Undocumented
Method | `__setitem__` | Sets a single item, a row or a column of the matrix
Method | `__str__` | Undocumented
Method | `__sub__` | Subtracts the given value from the matrix.
Method | `max` | Returns the maximum of the matrix along the given dimension
Method | `min` | Returns the minimum of the matrix along the given dimension
Instance Variable | `data` | Undocumented
Property | `shape` | Returns the shape of the matrix as a tuple
Method | `_get_data` | Returns the data stored in the matrix as a list of lists
Method | `_set_data` | Sets the data stored in the matrix
Instance Variable | `_data` | Undocumented
Instance Variable | `_ncol` | Undocumented
Instance Variable | `_nrow` | Undocumented

@classmethod
def Fill(cls, value, *args): (source)

Creates a matrix filled with the given value

Parameters
value | the value to be used
*args | Undocumented
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.

@classmethod
def Identity(cls, *args): (source)

Creates an identity matrix.

Parameters
*args | Undocumented
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.

@classmethod
def Zero(cls, *args): (source)

Creates a matrix filled with zeros.

Parameters
*args | Undocumented
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.

def __add__(self, other): (source)

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

def __eq__(self, other): (source)

Checks whether a given matrix is equal to another one

def __getitem__(self, i): (source)

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.

def __hash__(self): (source)

Returns a hash value for a matrix.

def __iadd__(self, other): (source)

In-place addition of a matrix or scalar.

def __init__(self, data=None): (source)

Initializes a matrix.

Parameters
data | the elements of the matrix as a list of lists, or `None` to create a 0x0 matrix.

def __isub__(self, other): (source)

In-place subtraction of a matrix or scalar.

def __iter__(self): (source)

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.

def __len__(self): (source)

Returns the number of rows in the matrix.

def __ne__(self, other): (source)

Checks whether a given matrix is not equal to another one

def __plot__(self, backend, context, **kwds): (source)

Plots the matrix to the given Cairo context or matplotlib Axes.

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.

def __repr__(self): (source)

Undocumented

def __setitem__(self, i, value): (source)

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

def __str__(self): (source)

Undocumented

def __sub__(self, other): (source)

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

def max(self, dim=None): (source)

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.

def min(self, dim=None): (source)

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.

data = (source)

Undocumented

@property
shape = (source)

Returns the shape of the matrix as a tuple

def _get_data(self): (source)

Returns the data stored in the matrix as a list of lists

def _set_data(self, data=None): (source)

Sets the data stored in the matrix

_data = (source)

Undocumented

_ncol = (source)

Undocumented

_nrow = (source)

Undocumented