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

""" 

Convert PARI objects to/from Flint objects 

  

Utility function to convert PARI ``GEN``s to/from flint types. 

  

AUTHORS: 

  

- Luca De Feo (2016-09-06): Separate Sage-specific components from 

generic C-interface in ``Pari`` (:trac:`20241`) 

""" 

  

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

# Copyright (C) 2016 Luca De Feo <luca.defeo@polytechnique.edu> 

# 

# 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, division, print_function 

  

from cysignals.signals cimport sig_on 

  

from sage.libs.flint.fmpz cimport fmpz_get_mpz, COEFF_IS_MPZ, COEFF_TO_PTR, fmpz_is_one 

from sage.libs.flint.fmpq cimport fmpq_numref, fmpq_denref 

from sage.libs.flint.fmpz_mat cimport fmpz_mat_nrows, fmpz_mat_ncols, fmpz_mat_entry 

from sage.libs.flint.fmpq_mat cimport fmpq_mat_nrows, fmpq_mat_ncols, fmpq_mat_entry 

  

from cypari2.paridecl cimport * 

from cypari2.stack cimport new_gen 

from .convert_gmp cimport _new_GEN_from_mpz_t 

  

  

cdef inline GEN _new_GEN_from_fmpz_t(fmpz_t value): 

r""" 

Create a new PARI ``t_INT`` from a ``fmpz_t``. 

  

For internal use only; this directly uses the PARI stack. 

One should call ``sig_on()`` before and ``sig_off()`` after. 

""" 

if COEFF_IS_MPZ(value[0]): 

return _new_GEN_from_mpz_t(COEFF_TO_PTR(value[0])) 

else: 

return stoi(value[0]) 

  

  

cdef inline GEN _new_GEN_from_fmpq_t(fmpq_t value): 

r""" 

Create a new PARI ``t_RAT`` from a ``fmpq_t``. 

  

For internal use only; this directly uses the PARI stack. 

One should call ``sig_on()`` before and ``sig_off()`` after. 

""" 

cdef GEN num = _new_GEN_from_fmpz_t(fmpq_numref(value)) 

if fmpz_is_one(fmpq_denref(value)): 

return num 

cdef GEN denom = _new_GEN_from_fmpz_t(fmpq_denref(value)) 

return mkfrac(num, denom) 

  

  

cdef GEN _new_GEN_from_fmpz_mat_t(fmpz_mat_t B): 

r""" 

Create a new PARI ``t_MAT`` with ``nr`` rows and ``nc`` columns 

from a ``fmpz_mat_t``. 

  

For internal use only; this directly uses the PARI stack. 

One should call ``sig_on()`` before and ``sig_off()`` after. 

""" 

cdef GEN x 

cdef GEN A = zeromatcopy(fmpz_mat_nrows(B), fmpz_mat_ncols(B)) 

cdef Py_ssize_t i, j 

for i in range(fmpz_mat_nrows(B)): 

for j in range(fmpz_mat_ncols(B)): 

x = _new_GEN_from_fmpz_t(fmpz_mat_entry(B, i, j)) 

set_gcoeff(A, i+1, j+1, x) # A[i+1, j+1] = x (using 1-based indexing) 

return A 

  

  

cdef GEN _new_GEN_from_fmpq_mat_t(fmpq_mat_t B): 

cdef GEN x 

cdef GEN A = zeromatcopy(fmpq_mat_nrows(B), fmpq_mat_ncols(B)) 

cdef Py_ssize_t i, j 

for i in range(fmpq_mat_nrows(B)): 

for j in range(fmpq_mat_ncols(B)): 

x = _new_GEN_from_fmpq_t(fmpq_mat_entry(B, i, j)) 

set_gcoeff(A, i+1, j+1, x) # A[i+1, j+1] = x (using 1-based indexing) 

return A 

  

cdef GEN _new_GEN_from_fmpz_mat_t_rotate90(fmpz_mat_t B): 

r""" 

Create a new PARI ``t_MAT`` with ``nr`` rows and ``nc`` columns 

from a ``fmpz_mat_t`` and rotate the matrix 90 degrees 

counterclockwise. So the resulting matrix will have ``nc`` rows 

and ``nr`` columns. 

  

For internal use only; this directly uses the PARI stack. 

One should call ``sig_on()`` before and ``sig_off()`` after. 

""" 

cdef GEN x 

cdef GEN A = zeromatcopy(fmpz_mat_ncols(B), fmpz_mat_nrows(B)) 

cdef Py_ssize_t i, j 

for i in range(fmpz_mat_nrows(B)): 

for j in range(fmpz_mat_ncols(B)): 

x = _new_GEN_from_fmpz_t(fmpz_mat_entry(B, i, fmpz_mat_ncols(B) - j - 1)) 

set_gcoeff(A, j+1, i+1, x) # A[j+1, i+1] = x (using 1-based indexing) 

return A 

  

  

cdef GEN _new_GEN_from_fmpq_mat_t_rotate90(fmpq_mat_t B): 

r""" 

Create a new PARI ``t_MAT`` with ``nr`` rows and ``nc`` columns 

from a ``fmpq_mat_t`` and rotate the matrix 90 degrees 

counterclockwise. So the resulting matrix will have ``nc`` rows 

and ``nr`` columns. 

  

For internal use only; this directly uses the PARI stack. 

One should call ``sig_on()`` before and ``sig_off()`` after. 

""" 

cdef GEN x 

cdef GEN A = zeromatcopy(fmpq_mat_ncols(B), fmpq_mat_nrows(B)) 

cdef Py_ssize_t i, j 

for i in range(fmpq_mat_nrows(B)): 

for j in range(fmpq_mat_ncols(B)): 

x = _new_GEN_from_fmpq_t(fmpq_mat_entry(B, i, fmpq_mat_ncols(B) - j - 1)) 

set_gcoeff(A, j+1, i+1, x) # A[j+1, i+1] = x (using 1-based indexing) 

return A 

  

  

cdef Gen integer_matrix(fmpz_mat_t B, bint rotate): 

""" 

EXAMPLES:: 

  

sage: matrix(ZZ,2,[1..6]).__pari__() # indirect doctest 

[1, 2, 3; 4, 5, 6] 

""" 

cdef GEN g 

sig_on() 

if rotate: 

g = _new_GEN_from_fmpz_mat_t_rotate90(B) 

else: 

g = _new_GEN_from_fmpz_mat_t(B) 

return new_gen(g) 

  

  

cdef Gen rational_matrix(fmpq_mat_t B, bint rotate): 

""" 

EXAMPLES:: 

  

sage: matrix(QQ, 2, [1/2, 2/3, 3/4, 4/5]).__pari__() # indirect doctest 

[1/2, 2/3; 3/4, 4/5] 

""" 

cdef GEN g 

sig_on() 

if rotate: 

g = _new_GEN_from_fmpq_mat_t_rotate90(B) 

else: 

g = _new_GEN_from_fmpq_mat_t(B) 

return new_gen(g)