Hide keyboard shortcuts

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

""" 

Cremona matrices 

""" 

from __future__ import print_function 

  

from ..eclib cimport scalar, addscalar 

  

from sage.matrix.all import MatrixSpace 

from sage.rings.all import ZZ 

  

from sage.matrix.matrix_integer_sparse cimport Matrix_integer_sparse 

from sage.matrix.matrix_integer_dense cimport Matrix_integer_dense 

from sage.rings.integer cimport Integer 

  

  

cdef class Matrix: 

""" 

A Cremona Matrix. 

  

EXAMPLES:: 

  

sage: M = CremonaModularSymbols(225) 

sage: t = M.hecke_matrix(2) 

sage: type(t) 

<type 'sage.libs.eclib.mat.Matrix'> 

sage: t 

61 x 61 Cremona matrix over Rational Field 

  

TESTS:: 

  

sage: t = CremonaModularSymbols(11).hecke_matrix(2); t 

3 x 3 Cremona matrix over Rational Field 

sage: type(t) 

<type 'sage.libs.eclib.mat.Matrix'> 

""" 

def __repr__(self): 

""" 

String representation of this matrix. Use print(self.str()) to 

print out the matrix entries on the screen. 

  

EXAMPLES:: 

  

sage: M = CremonaModularSymbols(23) 

sage: t = M.hecke_matrix(2); t 

5 x 5 Cremona matrix over Rational Field 

sage: print(t.str()) 

[ 3 0 0 0 0] 

[-1 -1 0 0 -1] 

[ 1 1 0 1 1] 

[-1 1 1 -1 0] 

[ 0 -1 0 0 0] 

""" 

return "%s x %s Cremona matrix over Rational Field"%(self.nrows(), self.ncols()) 

  

def str(self): 

r""" 

Return full string representation of this matrix, never in compact form. 

  

EXAMPLES:: 

  

sage: M = CremonaModularSymbols(22, sign=1) 

sage: t = M.hecke_matrix(13) 

sage: t.str() 

'[14 0 0 0 0]\n[-4 12 0 8 4]\n[ 0 -6 4 -6 0]\n[ 4 2 0 6 -4]\n[ 0 0 0 0 14]' 

""" 

return self.sage_matrix_over_ZZ(sparse=False).str() 

  

def __dealloc__(self): 

del self.M 

  

def __getitem__(self, ij): 

""" 

Return the (i,j) entry of this matrix. 

  

Here, ij is a 2-tuple (i,j) and the row and column indices start 

at 1 and not 0. 

  

EXAMPLES:: 

  

sage: M = CremonaModularSymbols(19, sign=1) 

sage: t = M.hecke_matrix(13); t 

2 x 2 Cremona matrix over Rational Field 

sage: t.sage_matrix_over_ZZ() 

[ 28 0] 

[-12 -8] 

sage: [[t.__getitem__((i,j)) for j in [1,2]] for i in [1,2]] 

[[28, 0], [-12, -8]] 

sage: t.__getitem__((0,0)) 

Traceback (most recent call last): 

... 

IndexError: matrix indices out of range 

""" 

cdef long i, j 

if self.M: 

i, j = ij 

if 0<i and i<=self.M[0].nrows() and 0<j and j<=self.M[0].ncols(): 

return self.M.sub(i,j) 

raise IndexError("matrix indices out of range") 

raise IndexError("cannot index into an undefined matrix") 

  

def nrows(self): 

""" 

Return the number of rows of this matrix. 

  

EXAMPLES:: 

  

sage: M = CremonaModularSymbols(19, sign=1) 

sage: t = M.hecke_matrix(13); t 

2 x 2 Cremona matrix over Rational Field 

sage: t.nrows() 

2 

""" 

return self.M[0].nrows() 

  

def ncols(self): 

""" 

Return the number of columns of this matrix. 

  

EXAMPLES:: 

  

sage: M = CremonaModularSymbols(1234, sign=1) 

sage: t = M.hecke_matrix(3); t.ncols() 

156 

sage: M.dimension() 

156 

""" 

return self.M[0].ncols() 

  

# Commented out since it gives very weird 

# results when sign != 0. 

## def rank(self): 

## """ 

## Return the rank of this matrix. 

  

## EXAMPLES: 

## sage: M = CremonaModularSymbols(389) 

## sage: t = M.hecke_matrix(2) 

## sage: t.rank() 

## 65 

## sage: M = CremonaModularSymbols(389, cuspidal=True) 

## sage: t = M.hecke_matrix(2) 

## sage: t.rank() 

## 64 

  

## sage: M = CremonaModularSymbols(389,sign=1) 

## sage: t = M.hecke_matrix(2) 

## sage: t.rank() # known bug. 

## 16 

## """ 

## return rank(self.M[0]) 

  

def add_scalar(self, scalar s): 

""" 

Return new matrix obtained by adding s to each diagonal entry of self. 

  

EXAMPLES:: 

  

sage: M = CremonaModularSymbols(23, cuspidal=True, sign=1) 

sage: t = M.hecke_matrix(2); print(t.str()) 

[ 0 1] 

[ 1 -1] 

sage: w = t.add_scalar(3); print(w.str()) 

[3 1] 

[1 2] 

""" 

return new_Matrix(addscalar(self.M[0], s)) 

  

def charpoly(self, var='x'): 

""" 

Return the characteristic polynomial of this matrix, viewed as 

as a matrix over the integers. 

  

ALGORITHM: 

  

Note that currently, this function converts this matrix into a 

dense matrix over the integers, then calls the charpoly 

algorithm on that, which I think is LinBox's. 

  

EXAMPLES:: 

  

sage: M = CremonaModularSymbols(33, cuspidal=True, sign=1) 

sage: t = M.hecke_matrix(2) 

sage: t.charpoly() 

x^3 + 3*x^2 - 4 

sage: t.charpoly().factor() 

(x - 1) * (x + 2)^2 

""" 

return self.sage_matrix_over_ZZ(sparse=False).charpoly(var) 

  

def sage_matrix_over_ZZ(self, sparse=True): 

""" 

Return corresponding Sage matrix over the integers. 

  

INPUT: 

  

- ``sparse`` -- (default: True) whether the return matrix has 

a sparse representation 

  

EXAMPLES:: 

  

sage: M = CremonaModularSymbols(23, cuspidal=True, sign=1) 

sage: t = M.hecke_matrix(2) 

sage: s = t.sage_matrix_over_ZZ(); s 

[ 0 1] 

[ 1 -1] 

sage: type(s) 

<type 'sage.matrix.matrix_integer_sparse.Matrix_integer_sparse'> 

sage: s = t.sage_matrix_over_ZZ(sparse=False); s 

[ 0 1] 

[ 1 -1] 

sage: type(s) 

<type 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'> 

""" 

cdef long n = self.nrows() 

cdef long i, j, k 

cdef scalar* v = <scalar*> self.M.get_entries() # coercion needed to deal with const 

  

cdef Matrix_integer_dense Td 

cdef Matrix_integer_sparse Ts 

  

# Ugly code... 

if sparse: 

Ts = MatrixSpace(ZZ, n, sparse=sparse).zero_matrix().__copy__() 

k = 0 

for i from 0 <= i < n: 

for j from 0 <= j < n: 

if v[k]: 

Ts.set_unsafe(i, j, Integer(v[k])) 

k += 1 

return Ts 

else: 

Td = MatrixSpace(ZZ, n, sparse=sparse).zero_matrix().__copy__() 

k = 0 

for i from 0 <= i < n: 

for j from 0 <= j < n: 

if v[k]: 

Td.set_unsafe(i, j, Integer(v[k])) 

k += 1 

return Td 

  

  

cdef class MatrixFactory: 

cdef new_matrix(self, mat M): 

return new_Matrix(M) 

  

  

cdef Matrix new_Matrix(mat M): 

cdef Matrix A = Matrix() 

A.M = new mat(M) 

return A