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

""" 

Ring of pari objects 

 

AUTHORS: 

 

- William Stein (2004): Initial version. 

- Simon King (2011-08-24): Use UniqueRepresentation, element_class and 

proper initialisation of elements. 

""" 

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

# Copyright (C) 2004 William Stein <wstein@gmail.com> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# 

# The full text of the GPL is available at: 

# 

# http://www.gnu.org/licenses/ 

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

import sage.libs.pari.all as pari 

import sage.rings.ring as ring 

from sage.structure.element import RingElement 

from sage.structure.richcmp import richcmp 

from sage.misc.fast_methods import Singleton 

 

 

class Pari(RingElement): 

""" 

Element of Pari pseudo-ring. 

""" 

def __init__(self, x, parent=None): 

""" 

EXAMPLES:: 

 

sage: R = PariRing() 

sage: f = R('x^3 + 1/2') 

sage: f 

x^3 + 1/2 

sage: type(f) 

<class 'sage.rings.pari_ring.PariRing_with_category.element_class'> 

sage: loads(f.dumps()) == f 

True 

""" 

if parent is None: 

parent = _inst 

RingElement.__init__(self, parent) 

self.__x = pari.pari(x) 

 

def __repr__(self): 

""" 

EXAMPLES:: 

 

sage: R = PariRing() 

sage: a = R(3); a 

3 

""" 

return str(self.__x) 

 

def _add_(self, other): 

""" 

EXAMPLES:: 

 

sage: R = PariRing() 

sage: b = R(11) 

sage: a = R(3) 

sage: a + b 

14 

""" 

return self.__class__(self.__x + other.__x, parent=_inst) 

 

def _sub_(self, other): 

""" 

EXAMPLES:: 

 

sage: R = PariRing() 

sage: a = R(3) 

sage: b = R(11) 

sage: b - a 

8 

""" 

return self.__class__(self.__x - other.__x, parent=_inst) 

 

def _mul_(self, other): 

""" 

EXAMPLES:: 

 

sage: R = PariRing() 

sage: a = R(3) 

sage: b = R(11) 

sage: b * a 

33 

""" 

return self.__class__(self.__x * other.__x, parent=_inst) 

 

def _div_(self, other): 

""" 

EXAMPLES:: 

 

sage: R = PariRing() 

sage: a = R(3) 

sage: b = R(11) 

sage: b / a 

11/3 

""" 

return self.__x * (~other.__x) 

 

def __neg__(self): 

""" 

EXAMPLES:: 

 

sage: R = PariRing() 

sage: a = R(3) 

sage: -a 

-3 

""" 

return self.__class__(-self.__x, parent=_inst) 

 

def __pow__(self, other): 

""" 

EXAMPLES:: 

 

sage: R = PariRing() 

sage: a = R(3) 

sage: a^2 

9 

""" 

if not(other in PariRing()): 

other = Pari(other) 

return self.__class__(self.__x ** other.__x, parent=_inst) 

 

def __invert__(self): 

""" 

EXAMPLES:: 

 

sage: R = PariRing() 

sage: a = R(3) 

sage: ~a 

1/3 

""" 

return self.__class__(~self.__x, parent=_inst) 

 

def _richcmp_(self, other, op): 

""" 

EXAMPLES:: 

 

sage: R = PariRing() 

sage: a = R(3) 

sage: b = R(11) 

sage: a < b 

True 

sage: a == b 

False 

sage: a > b 

False 

""" 

return richcmp(self.__x, other.__x, op) 

 

def __int__(self): 

return int(self.__x) 

 

 

class PariRing(Singleton, ring.Ring): 

""" 

EXAMPLES:: 

 

sage: R = PariRing(); R 

Pseudoring of all PARI objects. 

sage: loads(R.dumps()) is R 

True 

""" 

Element = Pari 

 

def __init__(self): 

ring.Ring.__init__(self, self) 

 

def __repr__(self): 

return 'Pseudoring of all PARI objects.' 

 

def _element_constructor_(self, x): 

if isinstance(x, Pari): 

return x 

return self.element_class(x, parent=self) 

 

def is_field(self, proof=True): 

return False 

 

def characteristic(self): 

raise RuntimeError("Not defined.") 

 

def random_element(self, x=None, y=None, distribution=None): 

""" 

Return a random integer in Pari. 

 

.. NOTE:: 

 

The given arguments are passed to ``ZZ.random_element(...)``. 

 

INPUT: 

 

- `x`, `y` -- optional integers, that are lower and upper bound 

for the result. If only `x` is provided, then the result is 

between 0 and `x-1`, inclusive. If both are provided, then the 

result is between `x` and `y-1`, inclusive. 

 

- `distribution` -- optional string, so that ``ZZ`` can make sense 

of it as a probability distribution. 

 

EXAMPLES:: 

 

sage: R = PariRing() 

sage: R.random_element() 

-8 

sage: R.random_element(5,13) 

12 

sage: [R.random_element(distribution="1/n") for _ in range(10)] 

[0, 1, -1, 2, 1, -95, -1, -2, -12, 0] 

 

""" 

from sage.all import ZZ 

return self(ZZ.random_element(x, y, distribution)) 

 

def zeta(self): 

""" 

Return -1. 

 

EXAMPLES:: 

 

sage: R = PariRing() 

sage: R.zeta() 

-1 

""" 

return self(-1) 

 

_inst = PariRing()