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

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

r""" 

Wrappers on GAP matrices 

""" 

#***************************************************************************** 

# Copyright (C) 2017 Vincent Delecroix <20100.delecroix@gmail.com> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License 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 __future__ import print_function, absolute_import 

  

from sage.libs.gap.libgap import libgap 

from . import matrix_space 

from sage.structure.element cimport Matrix 

  

cdef class Matrix_gap(Matrix_dense): 

r""" 

A Sage matrix wrapper over a GAP matrix. 

  

EXAMPLES:: 

  

sage: M = MatrixSpace(ZZ, 2, implementation='gap') 

sage: m1 = M([1, 0, 2, -3]) 

sage: m2 = M([2, 2, 5, -1]) 

sage: type(m1) 

<type 'sage.matrix.matrix_gap.Matrix_gap'> 

  

sage: m1 * m2 

[ 2 2] 

[-11 7] 

sage: type(m1 * m2) 

<type 'sage.matrix.matrix_gap.Matrix_gap'> 

  

sage: M = MatrixSpace(QQ, 5, 3, implementation='gap') 

sage: m = M(range(15)) 

sage: m.left_kernel() 

Vector space of degree 5 and dimension 3 over Rational Field 

Basis matrix: 

[ 1 0 0 -4 3] 

[ 0 1 0 -3 2] 

[ 0 0 1 -2 1] 

  

sage: M = MatrixSpace(ZZ, 10, implementation='gap') 

sage: m = M(range(100)) 

sage: m.transpose().parent() is M 

True 

  

sage: UCF = UniversalCyclotomicField() 

sage: M = MatrixSpace(UCF, 3, implementation='gap') 

sage: m = M([UCF.zeta(i) for i in range(1,10)]) 

sage: m 

[ 1 -1 E(3)] 

[ E(4) E(5) -E(3)^2] 

[ E(7) E(8) -E(9)^4 - E(9)^7] 

sage: (m^2)[1,2] 

E(180)^32 - E(180)^33 + E(180)^68 - E(180)^69 + E(180)^104 - E(180)^141 - E(180)^156 + E(180)^176 - E(180)^177 

  

TESTS:: 

  

sage: for ring in [ZZ, QQ, UniversalCyclotomicField(), GF(2), GF(3)]: 

....: M = MatrixSpace(ring, 2, implementation='gap') 

....: TestSuite(M).run() 

....: M = MatrixSpace(ring, 2, 3, implementation='gap') 

....: TestSuite(M).run() 

""" 

def __init__(self, parent, entries, coerce, copy): 

r""" 

TESTS:: 

  

sage: M = MatrixSpace(ZZ, 2, implementation='gap') 

sage: M(0) 

[0 0] 

[0 0] 

sage: M(1) 

[1 0] 

[0 1] 

sage: M(2) 

[2 0] 

[0 2] 

sage: type(M(0)) 

<type 'sage.matrix.matrix_gap.Matrix_gap'> 

sage: type(M(1)) 

<type 'sage.matrix.matrix_gap.Matrix_gap'> 

sage: type(M(2)) 

<type 'sage.matrix.matrix_gap.Matrix_gap'> 

  

sage: M = MatrixSpace(QQ, 2, 3, implementation='gap') 

sage: M(0) 

[0 0 0] 

[0 0 0] 

sage: M(1) 

Traceback (most recent call last): 

... 

TypeError: identity matrix must be square 

  

sage: MatrixSpace(QQ, 1, 2)(0) 

[0 0] 

sage: MatrixSpace(QQ, 2, 1)(0) 

[0] 

[0] 

""" 

Matrix_dense.__init__(self, parent) 

  

R = self._base_ring 

  

if isinstance(entries, (tuple, list)): 

entries = [[R(x) for x in entries[i * self._ncols: (i+1) * self._ncols]] for i in range(self._nrows)] 

else: 

zero = R.zero() 

if entries is None: 

elt = zero 

else: 

elt = R(entries) 

entries = [[zero] * self._ncols for i in range(self._nrows)] 

if not elt.is_zero(): 

if self._nrows != self._ncols: 

raise TypeError('non diagonal matrices can not be initialized from a scalar') 

for i in range(self._nrows): 

entries[i][i] = elt 

  

self._libgap = libgap(entries) 

  

cdef Matrix_gap _new(self, Py_ssize_t nrows, Py_ssize_t ncols): 

if nrows == self._nrows and ncols == self._ncols: 

P = self._parent 

else: 

P = self.matrix_space(nrows, ncols) 

  

cdef Matrix_gap M = Matrix_gap.__new__(Matrix_gap, P, None, None, None) 

Matrix_dense.__init__(M, P) 

return M 

  

def __copy__(self): 

r""" 

TESTS:: 

  

sage: M = MatrixSpace(QQ, 2, implementation='gap') 

sage: m1 = M([1,2,0,3]) 

sage: m2 = m1.__copy__() 

sage: m2 

[1 2] 

[0 3] 

sage: m1[0,1] = -2 

sage: m1 

[ 1 -2] 

[ 0 3] 

sage: m2 

[1 2] 

[0 3] 

""" 

cdef Matrix_gap M = self._new(self._nrows, self._ncols) 

M._libgap = self._libgap.deepcopy(1) 

return M 

  

def __reduce__(self): 

r""" 

TESTS:: 

  

sage: M = MatrixSpace(ZZ, 2, implementation='gap') 

sage: m = M([1,2,1,2]) 

sage: loads(dumps(m)) == m 

True 

""" 

return self._parent, (self.list(),) 

  

cpdef GapElement gap(self): 

r""" 

Return the underlying gap object. 

  

EXAMPLES:: 

  

sage: M = MatrixSpace(ZZ, 2, implementation='gap') 

sage: m = M([1,2,2,1]).gap() 

sage: m 

[ [ 1, 2 ], [ 2, 1 ] ] 

sage: type(m) 

<type 'sage.libs.gap.element.GapElement_List'> 

  

sage: m.MatrixAutomorphisms() 

Group([ (1,2) ]) 

""" 

return self._libgap 

  

cdef get_unsafe(self, Py_ssize_t i, Py_ssize_t j): 

return self._base_ring(self._libgap[i,j]) 

  

cdef set_unsafe(self, Py_ssize_t i, Py_ssize_t j, object x): 

r""" 

TESTS:: 

  

sage: M = MatrixSpace(ZZ, 2, implementation='gap') 

sage: m = M(0) 

sage: m[0,1] = 13 

sage: m 

[ 0 13] 

[ 0 0] 

sage: m[1,0] = -1/2 

Traceback (most recent call last): 

... 

TypeError: no conversion of this rational to integer 

""" 

self._libgap[i,j] = x 

  

cpdef _richcmp_(self, other, int op): 

r""" 

Compare ``self`` and ``right``. 

  

EXAMPLES:: 

  

sage: M = MatrixSpace(ZZ, 2, implementation='gap') 

sage: m1 = M([1,2,3,4]) 

sage: m2 = M([1,2,3,4]) 

sage: m3 = M([1,2,0,4]) 

sage: m1 == m2 

True 

sage: m1 != m2 

False 

sage: m1 == m3 

False 

sage: m1 != m3 

True 

  

sage: M = MatrixSpace(QQ, 2, implementation='gap') 

sage: m1 = M([1/2, 1/3, 2, -5]) 

sage: m2 = M([1/2, 1/3, 2, -5]) 

sage: m3 = M([1/2, 0, 2, -5]) 

sage: m1 == m2 

True 

sage: m1 != m2 

False 

sage: m1 == m3 

False 

sage: m1 != m3 

True 

  

sage: UCF = UniversalCyclotomicField() 

sage: M = MatrixSpace(UCF, 2, implementation='gap') 

sage: m1 = M([E(2), E(3), 0, E(4)]) 

sage: m2 = M([E(2), E(3), 0, E(4)]) 

sage: m3 = M([E(2), E(3), 0, E(5)]) 

sage: m1 == m2 

True 

sage: m1 != m2 

False 

sage: m1 == m3 

False 

sage: m1 != m3 

True 

""" 

return (<Matrix_gap> self)._libgap._richcmp_((<Matrix_gap> other)._libgap, op) 

  

def __neg__(self): 

r""" 

TESTS:: 

  

sage: M = MatrixSpace(ZZ, 2, 3, implementation='gap') 

sage: m = M([1, -1, 3, 2, -5, 1]) 

sage: -m 

[-1 1 -3] 

[-2 5 -1] 

""" 

cdef Matrix_gap M = self._new(self._nrows, self._ncols) 

M._libgap = self._libgap.AdditiveInverse() 

return M 

  

def __invert__(self): 

r""" 

TESTS:: 

  

sage: M = MatrixSpace(QQ, 2) 

sage: ~M([4,2,2,2]) 

[ 1/2 -1/2] 

[-1/2 1] 

""" 

cdef Matrix_gap M 

if self._base_ring.is_field(): 

M = self._new(self._nrows, self._ncols) 

M._libgap = self._libgap.Inverse() 

return M 

else: 

return Matrix_dense.__invert__(self) 

  

  

cpdef _add_(left, right): 

r""" 

TESTS:: 

  

sage: M = MatrixSpace(ZZ, 2, 3, implementation='gap') 

sage: M([1,2,3,4,3,2]) + M([1,1,1,1,1,1]) == M([2,3,4,5,4,3]) 

True 

""" 

cdef Matrix_gap cleft = <Matrix_gap> left 

cdef Matrix_gap ans = cleft._new(cleft._nrows, cleft._ncols) 

ans._libgap = left._libgap + (<Matrix_gap> right)._libgap 

return ans 

  

cpdef _sub_(left, right): 

r""" 

TESTS:: 

  

sage: M = MatrixSpace(ZZ, 2, 3, implementation='gap') 

sage: M([1,2,3,4,3,2]) - M([1,1,1,1,1,1]) == M([0,1,2,3,2,1]) 

True 

""" 

cdef Matrix_gap cleft = <Matrix_gap> left 

cdef Matrix_gap ans = cleft._new(cleft._nrows, cleft._ncols) 

ans._libgap = left._libgap - (<Matrix_gap> right)._libgap 

return ans 

  

cdef Matrix _matrix_times_matrix_(left, Matrix right): 

r""" 

TESTS:: 

  

sage: M = MatrixSpace(QQ, 2, implementation='gap') 

sage: m1 = M([1,2,-4,3]) 

sage: m2 = M([-1,1,1,-1]) 

sage: m1 * m2 

[ 1 -1] 

[ 7 -7] 

""" 

if left._ncols != right._nrows: 

raise IndexError("Number of columns of self must equal number of rows of right.") 

cdef Matrix_gap M = left._new(left._nrows, right._ncols) 

M._libgap = <Matrix_gap> ((<Matrix_gap> left)._libgap * (<Matrix_gap> right)._libgap) 

return M 

  

def determinant(self): 

r""" 

Return the determinant of this matrix. 

  

EXAMPLES:: 

  

sage: M = MatrixSpace(ZZ, 2, implementation='gap') 

sage: M([2, 1, 1, 1]).determinant() 

1 

sage: M([2, 1, 3, 3]).determinant() 

3 

  

TESTS:: 

  

sage: M = MatrixSpace(ZZ, 1, implementation='gap') 

sage: parent(M(1).determinant()) 

Integer Ring 

  

sage: M = MatrixSpace(QQ, 1, implementation='gap') 

sage: parent(M(1).determinant()) 

Rational Field 

  

sage: M = MatrixSpace(UniversalCyclotomicField(), 1, implementation='gap') 

sage: parent(M(1).determinant()) 

Universal Cyclotomic Field 

""" 

return self._base_ring(self._libgap.Determinant()) 

  

def trace(self): 

r""" 

Return the trace of this matrix. 

  

EXAMPLES:: 

  

sage: M = MatrixSpace(ZZ, 2, implementation='gap') 

sage: M([2, 1, 1, 1]).trace() 

3 

sage: M([2, 1, 3, 3]).trace() 

5 

  

TESTS:: 

  

sage: M = MatrixSpace(ZZ, 1, implementation='gap') 

sage: parent(M(1).trace()) 

Integer Ring 

  

sage: M = MatrixSpace(QQ, 1, implementation='gap') 

sage: parent(M(1).trace()) 

Rational Field 

  

sage: M = MatrixSpace(UniversalCyclotomicField(), 1, implementation='gap') 

sage: parent(M(1).trace()) 

Universal Cyclotomic Field 

""" 

return self._base_ring(self._libgap.Trace()) 

  

def rank(self): 

r""" 

Return the rank of this matrix. 

  

EXAMPLES:: 

  

sage: M = MatrixSpace(ZZ, 2, implementation='gap') 

sage: M([2, 1, 1, 1]).rank() 

2 

sage: M([2, 1, 4, 2]).rank() 

1 

""" 

return int(self._libgap.Rank())