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

""" 

Optimized Cython code needed by quaternion algebras. 

  

This is a collection of miscellaneous routines that are in Cython for 

speed purposes and are used by the quaternion algebra code. For 

example, there are functions for quickly constructing an n x 4 matrix 

from a list of n rational quaternions. 

  

AUTHORS: 

  

- William Stein 

""" 

  

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

# Copyright (C) 2009 William Stein <wstein@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 absolute_import 

  

from sage.rings.integer_ring import ZZ 

from sage.rings.rational_field import QQ 

from sage.rings.integer cimport Integer 

from sage.matrix.matrix_space import MatrixSpace 

from sage.matrix.matrix_integer_dense cimport Matrix_integer_dense 

from sage.matrix.matrix_rational_dense cimport Matrix_rational_dense 

  

from .quaternion_algebra_element cimport QuaternionAlgebraElement_rational_field 

  

from sage.libs.gmp.mpz cimport mpz_t, mpz_lcm, mpz_init, mpz_set, mpz_clear, mpz_init_set, mpz_mul, mpz_fdiv_q, mpz_cmp_si 

from sage.libs.gmp.mpq cimport mpq_set_num, mpq_set_den, mpq_canonicalize 

  

from sage.libs.flint.fmpz cimport fmpz_set_mpz 

from sage.libs.flint.fmpq cimport fmpq_canonicalise 

from sage.libs.flint.fmpq_mat cimport fmpq_mat_entry_num, fmpq_mat_entry_den, fmpq_mat_entry 

  

def integral_matrix_and_denom_from_rational_quaternions(v, reverse=False): 

r""" 

Given a list of rational quaternions, return matrix `A` over `\ZZ` 

and denominator `d`, such that the rows of `(1/d)A` are the 

entries of the quaternions. 

  

INPUT: 

  

- ``v`` -- a list of quaternions in a rational quaternion algebra 

- ``reverse`` -- whether order of the coordinates as well as the 

order of the list ``v`` should be reversed 

  

OUTPUT: 

  

- a matrix over `\ZZ` 

- an integer (the common denominator) 

  

EXAMPLES:: 

  

sage: A.<i,j,k>=QuaternionAlgebra(-4,-5) 

sage: sage.algebras.quatalg.quaternion_algebra_cython.integral_matrix_and_denom_from_rational_quaternions([i/2,1/3+j+k]) 

( 

[0 3 0 0] 

[2 0 6 6], 6 

) 

  

sage: sage.algebras.quatalg.quaternion_algebra_cython.integral_matrix_and_denom_from_rational_quaternions([i/2,1/3+j+k], reverse=True) 

( 

[6 6 0 2] 

[0 0 3 0], 6 

) 

""" 

# This function is an optimized version of 

# MatrixSpace(QQ,len(v),4)([x.coefficient_tuple() for x in v], coerce=False)._clear_denom 

  

cdef Py_ssize_t i, n=len(v) 

M = MatrixSpace(ZZ, n, 4) 

cdef Matrix_integer_dense A = M.zero_matrix().__copy__() 

if n == 0: return A 

  

# Find least common multiple of the denominators 

cdef QuaternionAlgebraElement_rational_field x 

cdef Integer d = Integer() 

# set denom to the denom of the first quaternion 

x = v[0]; mpz_set(d.value, x.d) 

for x in v[1:]: 

mpz_lcm(d.value, d.value, x.d) 

  

# Now fill in each row x of A, multiplying it by q = d/denom(x) 

cdef mpz_t q 

cdef mpz_t* row 

cdef mpz_t tmp 

mpz_init(q) 

mpz_init(tmp) 

for i in range(n): 

x = v[i] 

mpz_fdiv_q(q, d.value, x.d) 

if reverse: 

mpz_mul(tmp, q, x.x) 

A.set_unsafe_mpz(n-i-1,3,tmp) 

mpz_mul(tmp, q, x.y) 

A.set_unsafe_mpz(n-i-1,2,tmp) 

mpz_mul(tmp, q, x.z) 

A.set_unsafe_mpz(n-i-1,1,tmp) 

mpz_mul(tmp, q, x.w) 

A.set_unsafe_mpz(n-i-1,0,tmp) 

else: 

mpz_mul(tmp, q, x.x) 

A.set_unsafe_mpz(i,0,tmp) 

mpz_mul(tmp, q, x.y) 

A.set_unsafe_mpz(i,1,tmp) 

mpz_mul(tmp, q, x.z) 

A.set_unsafe_mpz(i,2,tmp) 

mpz_mul(tmp, q, x.w) 

A.set_unsafe_mpz(i,3,tmp) 

mpz_clear(q) 

mpz_clear(tmp) 

return A, d 

  

def rational_matrix_from_rational_quaternions(v, reverse=False): 

r""" 

Return matrix over the rationals whose rows have entries the 

coefficients of the rational quaternions in ``v``. 

  

INPUT: 

  

- ``v`` -- a list of quaternions in a rational quaternion algebra 

- ``reverse`` -- whether order of the coordinates as well as the 

order of the list ``v`` should be reversed 

  

OUTPUT: 

  

- a matrix over `\QQ` 

  

EXAMPLES:: 

  

sage: A.<i,j,k>=QuaternionAlgebra(-4,-5) 

sage: sage.algebras.quatalg.quaternion_algebra_cython.rational_matrix_from_rational_quaternions([i/2,1/3+j+k]) 

[ 0 1/2 0 0] 

[1/3 0 1 1] 

  

sage: sage.algebras.quatalg.quaternion_algebra_cython.rational_matrix_from_rational_quaternions([i/2,1/3+j+k], reverse=True) 

[ 1 1 0 1/3] 

[ 0 0 1/2 0] 

""" 

cdef Py_ssize_t i, j, n=len(v) 

M = MatrixSpace(QQ, n, 4) 

cdef Matrix_rational_dense A = M.zero_matrix().__copy__() 

if n == 0: return A 

  

cdef QuaternionAlgebraElement_rational_field x 

if reverse: 

for i in range(n): 

x = v[i] 

fmpz_set_mpz(fmpq_mat_entry_num(A._matrix, n-i-1, 3), x.x) 

fmpz_set_mpz(fmpq_mat_entry_num(A._matrix, n-i-1, 2), x.y) 

fmpz_set_mpz(fmpq_mat_entry_num(A._matrix, n-i-1, 1), x.z) 

fmpz_set_mpz(fmpq_mat_entry_num(A._matrix, n-i-1, 0), x.w) 

  

if mpz_cmp_si(x.d,1): 

for j in range(4): 

fmpz_set_mpz(fmpq_mat_entry_den(A._matrix, n-i-1, j), x.d) 

fmpq_canonicalise(fmpq_mat_entry(A._matrix, n-i-1, j)) 

else: 

for i in range(n): 

x = v[i] 

fmpz_set_mpz(fmpq_mat_entry_num(A._matrix, i, 0), x.x) 

fmpz_set_mpz(fmpq_mat_entry_num(A._matrix, i, 1), x.y) 

fmpz_set_mpz(fmpq_mat_entry_num(A._matrix, i, 2), x.z) 

fmpz_set_mpz(fmpq_mat_entry_num(A._matrix, i, 3), x.w) 

  

if mpz_cmp_si(x.d,1): 

for j in range(4): 

fmpz_set_mpz(fmpq_mat_entry_den(A._matrix, i, j), x.d) 

fmpq_canonicalise(fmpq_mat_entry(A._matrix, i, j)) 

  

return A 

  

def rational_quaternions_from_integral_matrix_and_denom(A, Matrix_integer_dense H, Integer d, reverse=False): 

r""" 

Given an integral matrix and denominator, returns a list of 

rational quaternions. 

  

INPUT: 

  

- ``A`` -- rational quaternion algebra 

- ``H`` -- matrix over the integers 

- ``d`` -- integer 

- ``reverse`` -- whether order of the coordinates as well as the 

order of the list ``v`` should be reversed 

  

OUTPUT: 

  

- list of ``H.nrows()`` elements of ``A`` 

  

EXAMPLES:: 

  

sage: A.<i,j,k>=QuaternionAlgebra(-1,-2) 

sage: f = sage.algebras.quatalg.quaternion_algebra_cython.rational_quaternions_from_integral_matrix_and_denom 

sage: f(A, matrix([[1,2,3,4],[-1,2,-4,3]]), 3) 

[1/3 + 2/3*i + j + 4/3*k, -1/3 + 2/3*i - 4/3*j + k] 

  

sage: f(A, matrix([[3,-4,2,-1],[4,3,2,1]]), 3, reverse=True) 

[1/3 + 2/3*i + j + 4/3*k, -1/3 + 2/3*i - 4/3*j + k] 

""" 

# 

# This is an optimized version of the following interpreted Python code. 

# H2 = H.change_ring(QQ)._rmul_(1/d) 

# return [A(v.list()) for v in H2.rows()] 

# 

cdef QuaternionAlgebraElement_rational_field x 

v = [] 

cdef Integer a, b 

a = Integer(A.invariants()[0]) 

b = Integer(A.invariants()[1]) 

cdef Py_ssize_t i, j 

cdef mpz_t tmp 

mpz_init(tmp) 

  

if reverse: 

rng = xrange(H.nrows()-1, -1, -1) 

else: 

rng = xrange(H.nrows()) 

  

for i in rng: 

x = <QuaternionAlgebraElement_rational_field> QuaternionAlgebraElement_rational_field.__new__(QuaternionAlgebraElement_rational_field) 

x._parent = A 

mpz_set(x.a, a.value) 

mpz_set(x.b, b.value) 

if reverse: 

H.get_unsafe_mpz(i,3,tmp) 

mpz_init_set(x.x, tmp) 

H.get_unsafe_mpz(i,2,tmp) 

mpz_init_set(x.y, tmp) 

H.get_unsafe_mpz(i,1,tmp) 

mpz_init_set(x.z, tmp) 

H.get_unsafe_mpz(i,0,tmp) 

mpz_init_set(x.w, tmp) 

else: 

H.get_unsafe_mpz(i,0,tmp) 

mpz_init_set(x.x, tmp) 

H.get_unsafe_mpz(i,1,tmp) 

mpz_init_set(x.y, tmp) 

H.get_unsafe_mpz(i,2,tmp) 

mpz_init_set(x.z, tmp) 

H.get_unsafe_mpz(i,3,tmp) 

mpz_init_set(x.w, tmp) 

mpz_init_set(x.d, d.value) 

# WARNING -- we do *not* canonicalize the entries in the quaternion. This is 

# I think _not_ needed for quaternion_element.pyx 

v.append(x) 

mpz_clear(tmp) 

return v