matrix.sherman_morrison ======================= .. py:module:: matrix.sherman_morrison Classes ------- .. autoapisummary:: matrix.sherman_morrison.Matrix Functions --------- .. autoapisummary:: matrix.sherman_morrison.test1 Module Contents --------------- .. py:class:: Matrix(row: int, column: int, default_value: float = 0) Matrix structure. .. py:method:: __add__(another: Matrix) -> Matrix 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] .. py:method:: __getitem__(loc: tuple[int, int]) -> Any Return array[row][column] where loc = (row, column). Example: >>> a = Matrix(3, 2, 7) >>> a[1, 0] 7 .. py:method:: __mul__(another: float | Matrix) -> Matrix 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] .. py:method:: __neg__() -> Matrix 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] .. py:method:: __repr__() -> str .. py:method:: __setitem__(loc: tuple[int, int], value: float) -> None 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] .. py:method:: __str__() -> str Return string representation of this matrix. .. py:method:: __sub__(another: Matrix) -> Matrix .. py:method:: sherman_morrison(u: Matrix, v: Matrix) -> Any 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] .. py:method:: transpose() -> Matrix 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] .. py:method:: validate_indices(loc: tuple[int, int]) -> bool 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 .. py:attribute:: array .. py:function:: test1() -> None