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

Commit 8cdf9b1

Browse files
committedMar 24, 2021
Add some functions to Matrix_nmod_dense
1 parent 763533d commit 8cdf9b1

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed
 

‎src/sage/matrix/docs.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,11 @@ class derived from Matrix). They can be either sparse or dense, and
392392
* cdef _list -- list of underlying elements (need not be a copy)
393393
* cdef _dict -- sparse dictionary of underlying elements
394394
* cdef _add_ -- add two matrices with identical parents
395-
* _matrix_times_matrix_c_impl -- multiply two matrices with compatible dimensions and
395+
* _matrix_times_matrix_ -- multiply two matrices with compatible dimensions and
396396
identical base rings (both sparse or both dense)
397397
* cpdef _richcmp_ -- compare two matrices with identical parents
398-
* cdef _lmul_c_impl -- multiply this matrix on the right by a scalar, i.e., self * scalar
399-
* cdef _rmul_c_impl -- multiply this matrix on the left by a scalar, i.e., scalar * self
398+
* cdef _lmul_ -- multiply this matrix on the right by a scalar, i.e., self * scalar
399+
* cdef _rmul_ -- multiply this matrix on the left by a scalar, i.e., scalar * self
400400
* __copy__
401401
* __neg__
402402

‎src/sage/matrix/matrix_nmod_dense.pyx

+36-4
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ from cpython.sequence cimport *
1212
from cysignals.signals cimport sig_str, sig_off
1313

1414
from sage.structure.sage_object cimport SageObject
15+
from sage.structure.element cimport Element, Matrix
1516
from sage.libs.flint.nmod_mat cimport *
1617

1718
from .args cimport SparseEntry, MatrixArgs_init
1819

1920
import sage.matrix.matrix_space as matrix_space
20-
21+
from sage.rings.finite_rings.integer_mod cimport IntegerMod_int, IntegerMod_int64
2122

2223
cdef class Matrix_nmod_dense(Matrix_dense):
23-
def __cinit__(self, parent, entries=None, bint coerce=True):
24+
def __cinit__(self, parent, *args, **kwds):
2425
self._modulus = parent._base._pyx_order
2526
sig_str("FLINT exception")
2627
nmod_mat_init(self._matrix, self._nrows, self._ncols, self._modulus.int64)
@@ -42,7 +43,10 @@ cdef class Matrix_nmod_dense(Matrix_dense):
4243
nmod_mat_print_pretty(self._matrix)
4344

4445
cdef set_unsafe(self, Py_ssize_t i, Py_ssize_t j, object x):
45-
self.set_unsafe_si(i, j, x.ivalue)
46+
if self._modulus.element_class() is IntegerMod_int:
47+
self.set_unsafe_si(i, j, (<IntegerMod_int>x).ivalue)
48+
else:
49+
self.set_unsafe_si(i, j, (<IntegerMod_int64>x).ivalue)
4650

4751
cdef void set_unsafe_si(self, Py_ssize_t i, Py_ssize_t j, long value):
4852
nmod_mat_set_entry(self._matrix, i, j, value)
@@ -68,9 +72,37 @@ cdef class Matrix_nmod_dense(Matrix_dense):
6872
P = self._parent
6973
else:
7074
P = matrix_space.MatrixSpace(self._parent._base, nrows, ncols, sparse=False)
71-
cdef Matrix_nmod_dense ans = Matrix_nmod_dense.__new__(Matrix_nmod_dense, P, None, None, None)
75+
cdef Matrix_nmod_dense ans = Matrix_nmod_dense.__new__(Matrix_nmod_dense, P)
7276
return ans
7377

78+
cpdef _add_(self, _right):
79+
cdef Matrix_nmod_dense right = _right
80+
cdef Matrix_nmod_dense M = self._new(self._nrows, self._ncols)
81+
nmod_mat_add(M._matrix, self._matrix, right._matrix)
82+
return M
83+
84+
cpdef _sub_(self, _right):
85+
cdef Matrix_nmod_dense right = _right
86+
cdef Matrix_nmod_dense M = self._new(self._nrows, self._ncols)
87+
nmod_mat_sub(M._matrix, self._matrix, right._matrix)
88+
return M
89+
90+
cdef Matrix _matrix_times_matrix_(left, Matrix _right):
91+
if left._ncols != _right._nrows:
92+
raise IndexError("Number of columns of self must equal number of rows of right.")
93+
cdef Matrix_nmod_dense right = _right
94+
cdef Matrix_nmod_dense M = left._new(left._nrows, right._ncols)
95+
nmod_mat_mul(M._matrix, left._matrix, right._matrix)
96+
return M
97+
98+
cpdef _lmul_(self, Element right):
99+
cdef Matrix_nmod_dense M = self._new(self._nrows, self._ncols)
100+
print("Hello")
101+
if self._modulus.element_class() is IntegerMod_int:
102+
nmod_mat_scalar_mul(M._matrix, self._matrix, (<IntegerMod_int?>right).ivalue)
103+
else:
104+
nmod_mat_scalar_mul(M._matrix, self._matrix, (<IntegerMod_int64?>right).ivalue)
105+
return M
74106

75107
def strong_echelon_form(self):
76108
if self._nrows >= self._ncols:

0 commit comments

Comments
 (0)
This repository has been archived.