Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/matrix/matrix_modn_dense_double.pyx
4077 views
1
"""
2
Dense matrices over `\ZZ/n\ZZ` for `n < 2^{23}` using LinBox's ``Modular<float>``
3
4
AUTHORS:
5
- Burcin Erocal
6
- Martin Albrecht
7
"""
8
###############################################################################
9
# SAGE: Open Source Mathematical Software
10
# Copyright (C) 2011 Burcin Erocal <[email protected]>
11
# Copyright (C) 2011 Martin Albrecht <[email protected]>
12
# Distributed under the terms of the GNU General Public License (GPL),
13
# version 2 or any later version. The full text of the GPL is available at:
14
# http://www.gnu.org/licenses/
15
###############################################################################
16
17
include "../ext/stdsage.pxi"
18
include "../ext/interrupt.pxi"
19
20
# randstate in template needs this
21
include '../ext/random.pxi'
22
23
from sage.libs.linbox.echelonform cimport BlasMatrix
24
25
from sage.libs.linbox.modular cimport ModDoubleField as ModField, ModDoubleFieldElement as ModFieldElement
26
from sage.libs.linbox.echelonform cimport EchelonFormDomainDouble as EchelonFormDomain
27
28
from sage.libs.linbox.fflas cimport ModDouble_fgemm as Mod_fgemm, ModDouble_fgemv as Mod_fgemv, \
29
ModDoubleDet as ModDet, \
30
ModDoubleRank as ModRank, ModDouble_echelon as Mod_echelon, \
31
ModDouble_applyp as Mod_applyp, \
32
ModDouble_MinPoly as Mod_MinPoly, \
33
ModDouble_CharPoly as Mod_CharPoly
34
35
MAX_MODULUS = 2**23
36
37
cdef extern from "../rings/finite_rings/stdint.h":
38
ctypedef int int_fast32_t
39
ctypedef int int_fast64_t
40
int_fast32_t INTEGER_MOD_INT32_LIMIT
41
int_fast64_t INTEGER_MOD_INT64_LIMIT
42
43
from sage.rings.finite_rings.integer_mod cimport IntegerMod_int64
44
45
include "matrix_modn_dense_template.pxi"
46
47
48
cdef class Matrix_modn_dense_double(Matrix_modn_dense_template):
49
r"""
50
Dense matrices over `\ZZ/n\ZZ` for `n < 2^{23}` using LinBox's ``Modular<float>``
51
52
These are matrices with integer entries mod ``n`` represented as
53
floating-point numbers in a 64-bit word for use with LinBox routines.
54
This allows for ``n`` up to `2^{23}`. The
55
``Matrix_modn_dense_float`` class specializes to smaller moduli.
56
57
Routines here are for the most basic access, see the
58
`matrix_modn_dense_template.pxi` file for higher-level routines.
59
"""
60
cdef set_unsafe_int(self, Py_ssize_t i, Py_ssize_t j, int value):
61
r"""
62
Set the (i,j) entry of self to the int value.
63
64
EXAMPLE::
65
66
sage: A = random_matrix(GF(3016963), 4, 4); A
67
[ 220081 2824836 765701 2282256]
68
[1795330 767112 2967421 1373921]
69
[2757699 1142917 2720973 2877160]
70
[1674049 1341486 2641133 2173280]
71
sage: A[0,0] = 220082r; A
72
[ 220082 2824836 765701 2282256]
73
[1795330 767112 2967421 1373921]
74
[2757699 1142917 2720973 2877160]
75
[1674049 1341486 2641133 2173280]
76
sage: a = A[0,0]; a
77
220082
78
sage: ~a
79
2859358
80
81
sage: A = random_matrix(Integers(5099106), 4, 4); A
82
[2629491 1237101 2033003 3788106]
83
[4649912 1157595 4928315 4382585]
84
[4252686 978867 2601478 1759921]
85
[1303120 1860486 3405811 2203284]
86
sage: A[0,0] = 220082r; A
87
[ 220082 1237101 2033003 3788106]
88
[4649912 1157595 4928315 4382585]
89
[4252686 978867 2601478 1759921]
90
[1303120 1860486 3405811 2203284]
91
sage: a = A[0,0]; a
92
220082
93
sage: a*a
94
4777936
95
"""
96
self._matrix[i][j] = <double>value
97
98
cdef set_unsafe(self, Py_ssize_t i, Py_ssize_t j, x):
99
r"""
100
Set the (i,j) entry with no bounds-checking, or any other checks.
101
102
Assumes that `x` is in the base ring.
103
104
EXAMPLE::
105
106
sage: A = random_matrix(GF(3016963), 4, 4); A
107
[ 220081 2824836 765701 2282256]
108
[1795330 767112 2967421 1373921]
109
[2757699 1142917 2720973 2877160]
110
[1674049 1341486 2641133 2173280]
111
sage: K = A.base_ring()
112
sage: A[0,0] = K(220082); A
113
[ 220082 2824836 765701 2282256]
114
[1795330 767112 2967421 1373921]
115
[2757699 1142917 2720973 2877160]
116
[1674049 1341486 2641133 2173280]
117
sage: a = A[0,0]; a
118
220082
119
sage: ~a
120
2859358
121
122
sage: A = random_matrix(Integers(5099106), 4, 4); A
123
[2629491 1237101 2033003 3788106]
124
[4649912 1157595 4928315 4382585]
125
[4252686 978867 2601478 1759921]
126
[1303120 1860486 3405811 2203284]
127
sage: K = A.base_ring()
128
sage: A[0,0] = K(220081)
129
sage: a = A[0,0]; a
130
220081
131
sage: a*a
132
4337773
133
"""
134
# note that INTEGER_MOD_INT32_LIMIT is ceil(sqrt(2^31-1)) < 2^23
135
if (<Matrix_modn_dense_template>self).p <= INTEGER_MOD_INT32_LIMIT:
136
self._matrix[i][j] = <double>(<IntegerMod_int>x).ivalue
137
else:
138
self._matrix[i][j] = <double>(<IntegerMod_int64>x).ivalue
139
140
cdef IntegerMod_abstract get_unsafe(self, Py_ssize_t i, Py_ssize_t j):
141
r"""
142
Return the (i,j) entry with no bounds-checking.
143
144
EXAMPLE::
145
146
sage: A = random_matrix(GF(3016963), 4, 4); A
147
[ 220081 2824836 765701 2282256]
148
[1795330 767112 2967421 1373921]
149
[2757699 1142917 2720973 2877160]
150
[1674049 1341486 2641133 2173280]
151
sage: a = A[0,0]; a
152
220081
153
sage: ~a
154
697224
155
sage: K = A.base_ring()
156
sage: ~K(220081)
157
697224
158
159
sage: A = random_matrix(Integers(5099106), 4, 4); A
160
[2629491 1237101 2033003 3788106]
161
[4649912 1157595 4928315 4382585]
162
[4252686 978867 2601478 1759921]
163
[1303120 1860486 3405811 2203284]
164
sage: a = A[0,1]; a
165
1237101
166
sage: a*a
167
3803997
168
sage: K = A.base_ring()
169
sage: K(1237101)^2
170
3803997
171
"""
172
# note that INTEGER_MOD_INT32_LIMIT is ceil(sqrt(2^31-1)) < 2^23
173
if (<Matrix_modn_dense_template>self).p <= INTEGER_MOD_INT32_LIMIT:
174
return IntegerMod_int(self._base_ring, <mod_int>(<Matrix_modn_dense_template>self)._matrix[i][j])
175
else:
176
return IntegerMod_int64(self._base_ring, <mod_int>(<Matrix_modn_dense_template>self)._matrix[i][j])
177
178
179