class Matrix(object):
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 |
|
Creates a matrix filled with the given value |
Class Method |
|
Creates an identity matrix. |
Class Method |
|
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 | __ne__ |
Checks whether a given matrix is not equal to another one |
Method | __plot__ |
Plots the matrix to the given Cairo context in the given box |
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 |
Returns the data stored in the matrix as a list of lists |
Method | _set |
Sets the data stored in the matrix |
Instance Variable | _data |
Undocumented |
Instance Variable | _ncol |
Undocumented |
Instance Variable | _nrow |
Undocumented |
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. |
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. |
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. |
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 |
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. |
Initializes a matrix.
Parameters | |
data | the elements of the matrix as a list of lists, or None to create a 0x0 matrix. |
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.
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.
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 |
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 |
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. |