Matrix Module
A simple functionnal module to manipulate matrices,
and a simple class for matrices.
Efficiency:
Warning
This module is not designed to be used for mathematics purpose.
And the list of list structure is not good for efficiency of maths calculus.
The Matrix.Matrix class is designed to be used as a data type,
and pretty printing is the main goal.
Example:
>>> m=Matrix.Matrix(0,2,3) # Creating a matrix. 2 lines, 3 colums.
>>> print repr(m) # Representation of m.mat is done with usual representation of a list.
maxStr=3;mat=[[0, 0, 0], [0, 0, 0]]
>>> print m # beautiful !
┌─┬─┬─┐
│0│0│0│
├─┼─┼─┤
│0│0│0│
└─┴─┴─┘
>>> m.box=Matrix.boxASCII
>>> print m # less pretty... but more universal (only ASCII symbols)
/-+-+-\
|0|0|0|
+-+-+-+
|0|0|0|
\-+-+-/
>>> m[1,2]='ok' # Support different data type for one matrix.
>>> m.box=Matrix.boxnoASCII
>>> print m
┌──┬──┬──┐
│0 │0 │0 │
├──┼──┼──┤
│0 │0 │ok│
└──┴──┴──┘
-
Matrix.matrix_init(value, lx=1, ly=1)[source]
A shortcut to create a matrix [lx] * [ly], initialized with value [value].
Element in (i,j), i for x, j for y, is given by <matrix>[i][j].
x, i, lx are for columns.
y, j, ly are for lines.
The returned value IS NOT a instance of Matrix.Matrix class !
-
Matrix.boxnoASCII = {'cl': '\xe2\x94\xa4', 'bl': '\xe2\x94\x94', 'cb': '\xe2\x94\xac', 'br': '\xe2\x94\x98', 'cr': '\xe2\x94\x9c', 'ct': '\xe2\x94\xb4', 'c': '\xe2\x94\xbc', 'e': ' ', 'h': '\xe2\x94\x80', 'tr': '\xe2\x94\x90', 'tl': '\xe2\x94\x8c', 'v': '\xe2\x94\x82'}
UTF8 (noASCII) box caracters to print boxes.
-
Matrix.boxASCII = {'cl': '+', 'bl': '\\', 'cb': '+', 'br': '/', 'cr': '+', 'ct': '+', 'c': '+', 'e': ' ', 'h': '-', 'tr': '\\', 'tl': '/', 'v': '|'}
ASCII caracters to print boxes
-
Matrix.tostr(mat, lgmax=3, lgmaxANSI=36, box={'cl': '\xe2\x94\xa4', 'bl': '\xe2\x94\x94', 'cb': '\xe2\x94\xac', 'br': '\xe2\x94\x98', 'cr': '\xe2\x94\x9c', 'ct': '\xe2\x94\xb4', 'c': '\xe2\x94\xbc', 'e': ' ', 'h': '\xe2\x94\x80', 'tr': '\xe2\x94\x90', 'tl': '\xe2\x94\x8c', 'v': '\xe2\x94\x82'})[source]
- Uses ANSI box symbol to print the matrix [mat], understandood as a list of list of element.
- [mat] have to be a matrix of printable element, with str(element),
- (ie, instances of a class providing __str__ method).
Warning
str(element) have to return a string OVER ONE UNIQUE LINE.
Example: str(element) = “D”, “P”, “C”, “F”, “R”, “T” for a chess game.
But you can use ANSIColors escaped string to augment possibilities.
- About:
-
- Box:
- Uses the box, a dictionnary containing c, cr, cl, ct, cb, br, bl, tl, tr, h, v, e.
Two boxes are preset : boxANSI and boxNoANSI.
The first one is the default value, and uses ANSI specials caracters, not supported for all encoding (try UTF8).
The second one is less pretty (see examples), but more universal because / + - | are supported for all encoding.
-
class Matrix.Matrix(value=None, lx=1, ly=1, box={'cl': 'xe2x94xa4', 'bl': 'xe2x94x94', 'cb': 'xe2x94xac', 'br': 'xe2x94x98', 'cr': 'xe2x94x9c', 'ct': 'xe2x94xb4', 'c': 'xe2x94xbc', 'e': ' ', 'h': 'xe2x94x80', 'tr': 'xe2x94x90', 'tl': 'xe2x94x8c', 'v': 'xe2x94x82'}, maxStr=3, new=True)[source]
A class to represent matrices.
- Attributes:
- mat – list of list of values,
- box – a box dictionnary for printing lines
- maxStr – max length of printed element of [mat].
-
__init__(value=None, lx=1, ly=1, box={'cl': 'xe2x94xa4', 'bl': 'xe2x94x94', 'cb': 'xe2x94xac', 'br': 'xe2x94x98', 'cr': 'xe2x94x9c', 'ct': 'xe2x94xb4', 'c': 'xe2x94xbc', 'e': ' ', 'h': 'xe2x94x80', 'tr': 'xe2x94x90', 'tl': 'xe2x94x8c', 'v': 'xe2x94x82'}, maxStr=3, new=True)[source]
Create a matrix filled of values = [value], dimensionized at [lx]*[ly].
If [new], all values in the new matrix are fresh copies of [value].
If almost all situations, it’s better to have [new]=True (otherwise you work with references, without saying it).
-
maxStr = None
max length of printed element of [mat].
-
box = None
a box dictionnary for printing lines
-
mat = None
list of list of values,
-
lx(self) → integer[source]
Number of laws.
-
ly(self) → integer[source]
Number of rows.
-
__eq__(m)[source]
self.__eq__(m) <==> m == self
-
__contains__(u)[source]
self.__contains__(y) <==> y in self
-
__len__()[source]
Dimension of a matrix.
-
__str__()[source]
Transformation to a string.
Warning : here it’s different to __repr__.
-
__getitem__((i, j))[source]
A shortcut to self.mat[i][j].
Allow self[i,j]
-
__setitem__((i, j), val)[source]
A shortcut to self.mat[i][j]=val
Allow self[i,j] = v.
- Warning:
- Use copy.copy to be sure of producing a fresh copy of [val].
-
__repr__()[source]
Toplevel representation of a Board, to a string.