Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Matrix windows """
#***************************************************************************** # Distributed under the terms of the GNU General Public License (GPL) # as published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. # http://www.gnu.org/licenses/ #*****************************************************************************
from cpython.tuple cimport *
######################################################################### # Generic matrix windows, which are used for block echelon and strassen # # algorithms. # ######################################################################### cdef class MatrixWindow: ############################ # creation and initialization ############################
cpdef MatrixWindow new_matrix_window(MatrixWindow self, Matrix matrix, Py_ssize_t row, Py_ssize_t col, Py_ssize_t n_rows, Py_ssize_t n_cols): """ This method is here only to provide a fast cdef way of constructing new matrix windows. The only implicit assumption is that self._matrix and matrix are over the same base ring (so share the zero). """
def __init__(MatrixWindow self, Matrix matrix, Py_ssize_t row, Py_ssize_t col, Py_ssize_t nrows, Py_ssize_t ncols):
cdef object _zero(self):
cpdef MatrixWindow matrix_window(MatrixWindow self, Py_ssize_t row, Py_ssize_t col, Py_ssize_t n_rows, Py_ssize_t n_cols): """ Returns a matrix window relative to this window of the underlying matrix. """
cpdef new_empty_window(MatrixWindow self, Py_ssize_t nrows, Py_ssize_t ncols):
def __repr__(self):
############################ # Getting and setting entries ############################
def set(MatrixWindow self, MatrixWindow src): raise TypeError("Parents must be equal.")
cpdef set_unsafe(self, Py_ssize_t i, Py_ssize_t j, x):
cpdef get_unsafe(self, Py_ssize_t i, Py_ssize_t j):
def __setitem__(self, ij, x): cdef Py_ssize_t i, j if isinstance(ij, tuple): # ij is a tuple, so we get i and j efficiently, construct corresponding integer entry. if PyTuple_Size(ij) != 2: raise IndexError("index must be an integer or pair of integers") i = <object> PyTuple_GET_ITEM(ij, 0) j = <object> PyTuple_GET_ITEM(ij, 1) if i<0 or i >= self._nrows or j<0 or j >= self._ncols: raise IndexError("matrix index out of range") self.set_unsafe(i, j, x) else: # If ij is not a tuple, coerce to an integer and set the row. i = ij for j from 0 <= j < self._ncols: self.set_unsafe(i, j, x)
def __getitem__(self, ij): cdef Py_ssize_t i, j cdef object x
if isinstance(ij, tuple): # ij is a tuple, so we get i and j efficiently, construct corresponding integer entry. if PyTuple_Size(ij) != 2: raise IndexError("index must be an integer or pair of integers") i = <object> PyTuple_GET_ITEM(ij, 0) j = <object> PyTuple_GET_ITEM(ij, 1) if i<0 or i >= self._nrows or j<0 or j >= self._ncols: raise IndexError("matrix index out of range") return self.get_unsafe(i, j) else: # If ij is not a tuple, coerce to an integer and get the row. i = ij return self.row(i)
cpdef matrix(MatrixWindow self): """ Returns the underlying matrix that this window is a view of. """ return self._matrix
cpdef to_matrix(MatrixWindow self): """ Returns an actual matrix object representing this view. """ cdef MatrixWindow w
def nrows(MatrixWindow self):
def ncols(MatrixWindow self):
cpdef set_to(MatrixWindow self, MatrixWindow A): """ Change self, making it equal A. """ cdef Py_ssize_t i, j raise ArithmeticError("incompatible dimensions")
cpdef set_to_zero(MatrixWindow self): cdef Py_ssize_t i, j
cpdef add(MatrixWindow self, MatrixWindow A): cdef Py_ssize_t i, j if self._nrows != A._nrows or self._ncols != A._ncols: raise ArithmeticError("incompatible dimensions") for i from 0 <= i < self._nrows: for j from 0 <= j < self._ncols: self.set_unsafe(i, j, self.get_unsafe(i, j) + A.get_unsafe(i, j))
cpdef subtract(MatrixWindow self, MatrixWindow A): cdef Py_ssize_t i, j if self._nrows != A._nrows or self._ncols != A._ncols: raise ArithmeticError("incompatible dimensions") for i from 0 <= i < self._nrows: for j from 0 <= j < self._ncols: self.set_unsafe(i, j, self.get_unsafe(i, j) - A.get_unsafe(i, j))
cpdef set_to_sum(MatrixWindow self, MatrixWindow A, MatrixWindow B): cdef Py_ssize_t i, j raise ArithmeticError("incompatible dimensions") raise ArithmeticError("incompatible dimensions")
cpdef set_to_diff(MatrixWindow self, MatrixWindow A, MatrixWindow B): cdef Py_ssize_t i, j
cpdef set_to_prod(MatrixWindow self, MatrixWindow A, MatrixWindow B): cdef Py_ssize_t i, j, k raise ArithmeticError("incompatible dimensions")
cpdef add_prod(MatrixWindow self, MatrixWindow A, MatrixWindow B): cdef Py_ssize_t i, j, k raise ArithmeticError("incompatible dimensions")
cpdef subtract_prod(MatrixWindow self, MatrixWindow A, MatrixWindow B): cdef Py_ssize_t i, j, k raise ArithmeticError("incompatible dimensions")
cpdef swap_rows(MatrixWindow self, Py_ssize_t a, Py_ssize_t b):
def echelon_in_place(MatrixWindow self): """ Calculate the echelon form of this matrix, returning the list of pivot columns """
cpdef bint element_is_zero(MatrixWindow self, Py_ssize_t i, Py_ssize_t j): |