Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
22970: _new_matrix/from_columns/add_to_entry
Browse files Browse the repository at this point in the history
  • Loading branch information
videlec committed May 28, 2017
1 parent 14f3fdb commit fceef50
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/sage/matrix/matrix0.pyx
Expand Up @@ -508,6 +508,31 @@ cdef class Matrix(sage.structure.element.Matrix):
"""
raise NotImplementedError("this must be defined in the derived type.")

def add_to_entry(self, Py_ssize_t i, Py_ssize_t j, elt):
r"""
Add ``elt`` to the entry at position ``(i, j)``.
EXAMPLES::
sage: m = matrix(QQ['x,y'], 2, 2)
sage: m.add_to_entry(0, 1, 2)
sage: m
[0 2]
[0 0]
"""
elt = self.base_ring()(elt)
if i < 0:
i += self._nrows
if i < 0 or i >= self._nrows:
raise IndexError("row index out of range")
if j < 0:
j += self._ncols
if j < 0 or j >= self._ncols:
raise IndexError("column index out of range")

self.set_unsafe(i, j, elt + self.get_unsafe(i, j))


## def _get_very_unsafe(self, i, j):
## r"""
## Entry access, but potentially fast since it might be without
Expand Down
2 changes: 2 additions & 0 deletions src/sage/matrix/matrix_rational_dense.pxd
Expand Up @@ -15,6 +15,8 @@ cdef class Matrix_rational_dense(Matrix_dense):
cdef _add_ui_unsafe_assuming_int(self, Py_ssize_t i, Py_ssize_t j, unsigned long int n)
cdef _sub_ui_unsafe_assuming_int(self, Py_ssize_t i, Py_ssize_t j, unsigned long int n)

cdef inline Matrix_rational_dense _new_matrix(self, Py_ssize_t nrows, Py_ssize_t ncols)

cdef class MatrixWindow:
cdef Matrix_rational_dense _matrix
cdef int _row, _col, _nrows, _ncols
72 changes: 72 additions & 0 deletions src/sage/matrix/matrix_rational_dense.pyx
Expand Up @@ -143,6 +143,14 @@ cdef class Matrix_rational_dense(Matrix_dense):
fmpq_mat_init(self._matrix, self._nrows, self._ncols)
sig_off()

cdef inline Matrix_rational_dense _new_matrix(self, Py_ssize_t nrows, Py_ssize_t ncols):
if nrows == self._nrows and ncols == self._ncols:
parent = self._parent
else:
parent = self.matrix_space(nrows, ncols)

return Matrix_rational_dense.__new__(Matrix_rational_dense, parent, None, None, None)

def __dealloc__(self):
sig_on()
fmpq_mat_clear(self._matrix)
Expand Down Expand Up @@ -203,6 +211,70 @@ cdef class Matrix_rational_dense(Matrix_dense):
for i in range(self._nrows):
fmpq_set_mpq(fmpq_mat_entry(self._matrix, i, i), z.value)

def matrix_from_columns(self, columns):
"""
Return the matrix constructed from self using columns with indices
in the columns list.
EXAMPLES::
sage: A = matrix(QQ, 3, range(9))
sage: A
[0 1 2]
[3 4 5]
[6 7 8]
sage: A.matrix_from_columns([2,1])
[2 1]
[5 4]
[8 7]
sage: A.matrix_from_columns((2,1,0,2))
[2 1 0 2]
[5 4 3 5]
[8 7 6 8]
"""
cdef Matrix_rational_dense A
cdef Py_ssize_t ncols, k, r, col

A = self._new_matrix(self._nrows, len(columns))
k = 0
for col in columns:
if col < 0 or col >= self._ncols:
raise IndexError("column out of range")
for r in range(self._nrows):
fmpq_set(fmpq_mat_entry(A._matrix, r, k), fmpq_mat_entry(self._matrix, r, col))
k = k + 1
return A

def add_to_entry(self, Py_ssize_t i, Py_ssize_t j, elt):
r"""
Add ``elt`` to the entry at position ``(i,j)``
EXAMPLES::
sage: m = matrix(QQ, 2, 2)
sage: m.add_to_entry(0, 0, -1/3)
sage: m
[-1/3 0]
[ 0 0]
"""
if not isinstance(elt, Rational):
elt = Rational(elt)
if i < 0:
i += self._nrows
if i < 0 or i >= self._nrows:
raise IndexError("row index out of range")
if j < 0:
j += self._ncols
if j < 0 or j >= self._ncols:
raise IndexError("column index out of range")
cdef fmpq_t tmp
fmpq_init(tmp)
fmpq_set_mpq(tmp, (<Rational>elt).value)
fmpq_add(fmpq_mat_entry(self._matrix, i, j),
fmpq_mat_entry(self._matrix, i, j),
tmp)
fmpq_clear(tmp)

cdef set_unsafe(self, Py_ssize_t i, Py_ssize_t j, value):
fmpq_set_mpq(fmpq_mat_entry(self._matrix, i, j), (<Rational> value).value)

Expand Down
36 changes: 36 additions & 0 deletions src/sage/matrix/matrix_rational_sparse.pyx
Expand Up @@ -171,6 +171,42 @@ cdef class Matrix_rational_sparse(Matrix_sparse):
def __hash__(self):
return self._hash()

def add_to_entry(self, Py_ssize_t i, Py_ssize_t j, elt):
r"""
Add ``elt`` to the entry at position ``(i, j)``.
EXAMPLES::
sage: m = matrix(QQ, 2, 2, sparse=True)
sage: m.add_to_entry(0, 0, -1/3)
sage: m
[-1/3 0]
[ 0 0]
sage: m.add_to_entry(0, 0, 1/3)
sage: m
[0 0]
[0 0]
sage: m.nonzero_positions()
[]
"""
if not isinstance(elt, Rational):
elt = Rational(elt)
if i < 0:
i += self._nrows
if i < 0 or i >= self._nrows:
raise IndexError("row index out of range")
if j < 0:
j += self._ncols
if j < 0 or j >= self._ncols:
raise IndexError("column index out of range")

cdef mpq_t z
mpq_init(z)
mpq_vector_get_entry(z, &self._matrix[i], j)
mpq_add(z, z, (<Rational>elt).value)
mpq_vector_set_entry(&self._matrix[i], j, z)
mpq_clear(z)


########################################################################
# LEVEL 2 functionality
Expand Down

0 comments on commit fceef50

Please sign in to comment.