matrix.sherman_morrison

Classes

Matrix

<class Matrix>

Functions

test1(→ None)

Module Contents

class matrix.sherman_morrison.Matrix(row: int, column: int, default_value: float = 0)

<class Matrix> Matrix structure.

__add__(another: Matrix) Matrix

<method Matrix.__add__> Return self + another. Example: >>> a = Matrix(2, 1, -4) >>> b = Matrix(2, 1, 3) >>> a+b Matrix consist of 2 rows and 1 columns [-1] [-1]

__getitem__(loc: tuple[int, int]) Any

<method Matrix.__getitem__> Return array[row][column] where loc = (row, column). Example: >>> a = Matrix(3, 2, 7) >>> a[1, 0] 7

__mul__(another: float | Matrix) Matrix

<method Matrix.__mul__> Return self * another. Example: >>> a = Matrix(2, 3, 1) >>> a[0,2] = a[1,2] = 3 >>> a * -2 Matrix consist of 2 rows and 3 columns [-2, -2, -6] [-2, -2, -6]

__neg__() Matrix

<method Matrix.__neg__> Return -self. Example: >>> a = Matrix(2, 2, 3) >>> a[0, 1] = a[1, 0] = -2 >>> -a Matrix consist of 2 rows and 2 columns [-3, 2] [ 2, -3]

__repr__() str
__setitem__(loc: tuple[int, int], value: float) None

<method Matrix.__setitem__> Set array[row][column] = value where loc = (row, column). Example: >>> a = Matrix(2, 3, 1) >>> a[1, 2] = 51 >>> a Matrix consist of 2 rows and 3 columns [ 1, 1, 1] [ 1, 1, 51]

__str__() str

<method Matrix.__str__> Return string representation of this matrix.

__sub__(another: Matrix) Matrix
sherman_morrison(u: Matrix, v: Matrix) Any

<method Matrix.sherman_morrison> Apply Sherman-Morrison formula in O(n^2). To learn this formula, please look this: https://en.wikipedia.org/wiki/Sherman%E2%80%93Morrison_formula This method returns (A + uv^T)^(-1) where A^(-1) is self. Returns None if it’s impossible to calculate. Warning: This method doesn’t check if self is invertible.

Make sure self is invertible before execute this method.

Example: >>> ainv = Matrix(3, 3, 0) >>> for i in range(3): ainv[i,i] = 1 … >>> u = Matrix(3, 1, 0) >>> u[0,0], u[1,0], u[2,0] = 1, 2, -3 >>> v = Matrix(3, 1, 0) >>> v[0,0], v[1,0], v[2,0] = 4, -2, 5 >>> ainv.sherman_morrison(u, v) Matrix consist of 3 rows and 3 columns [ 1.2857142857142856, -0.14285714285714285, 0.3571428571428571] [ 0.5714285714285714, 0.7142857142857143, 0.7142857142857142] [ -0.8571428571428571, 0.42857142857142855, -0.0714285714285714]

transpose() Matrix

<method Matrix.transpose> Return self^T. Example: >>> a = Matrix(2, 3) >>> for r in range(2): … for c in range(3): … a[r,c] = r*c … >>> a.transpose() Matrix consist of 3 rows and 2 columns [0, 0] [0, 1] [0, 2]

validate_indices(loc: tuple[int, int]) bool

<method Matrix.validate_indicies> Check if given indices are valid to pick element from matrix. Example: >>> a = Matrix(2, 6, 0) >>> a.validate_indices((2, 7)) False >>> a.validate_indices((0, 0)) True

array
matrix.sherman_morrison.test1() None