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

""" 

Convert Sage/Python objects to real/complex intervals 

""" 

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

# Copyright (C) 2018 Jeroen Demeyer <J.Demeyer@UGent.be> 

# 

# 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 cpython.float cimport PyFloat_AS_DOUBLE 

from cpython.complex cimport PyComplex_RealAsDouble, PyComplex_ImagAsDouble 

  

from sage.libs.mpfr cimport * 

from sage.libs.mpfi cimport * 

  

from sage.arith.long cimport integer_check_long 

from sage.structure.element cimport Element, parent 

from ..integer cimport Integer 

from ..rational cimport Rational 

from ..real_mpfi cimport RealIntervalFieldElement, RealIntervalField_class 

from ..complex_interval_field import ComplexIntervalField_class 

from ..real_mpfr cimport RealNumber 

from ..real_double cimport RealDoubleElement 

from ..complex_number cimport ComplexNumber 

from ..complex_interval cimport ComplexIntervalFieldElement 

from ..complex_double cimport ComplexDoubleElement 

  

from cypari2.gen cimport Gen 

  

  

cdef inline int return_real(mpfi_ptr im): 

""" 

Called by ``mpfi_set_sage`` on the imaginary part when converting 

a real number. 

""" 

if im is not NULL: 

mpfi_set_ui(im, 0) 

return 0 

  

  

cdef int mpfi_set_sage(mpfi_ptr re, mpfi_ptr im, x, field, int base) except -1: 

""" 

Convert any object ``x`` to an MPFI interval or a pair of 

real and complex intervals. 

  

INPUT: 

  

- ``re`` -- a pre-initialized MPFI interval. 

  

- ``im`` -- a pre-initialized MPFI interval or NULL. 

  

- ``x`` -- any Sage or Python object to be converted to an interval. 

  

- ``field`` -- a ``RealIntervalField`` or ``ComplexIntervalField`` 

of the right precision (real or complex doesn't matter). 

  

- ``base`` -- base to use for string conversion. 

  

OUTPUT: 

  

- if conversion is possible: set ``re`` and ``im`` (if applicable) 

and return 0. 

  

- if ``x`` is complex but ``im`` is ``NULL``: convert only if the 

imaginary component is 0. 

  

- in all other cases: raise an exception. 

""" 

cdef RealIntervalFieldElement ri 

cdef ComplexIntervalFieldElement zi 

cdef ComplexNumber zn 

cdef ComplexDoubleElement zd 

cdef bytes s 

  

if im is not NULL and isinstance(x, tuple): 

# For complex numbers, interpret tuples as real/imag parts 

if len(x) != 2: 

raise TypeError("tuple defining a complex number must have length 2") 

mpfi_set_sage(re, NULL, x[0], field, base) 

mpfi_set_sage(im, NULL, x[1], field, base) 

return 0 

if isinstance(x, list) or isinstance(x, tuple): 

# Interpret entries in x as endpoints of interval 

if len(x) != 2: 

raise TypeError("list defining an interval must have length 2") 

return mpfi_interv_sage(re, im, x[0], x[1], field, base) 

  

cdef long value 

cdef int err 

  

# Check for known types. First check for Element to reduce the 

# number of checks below. 

if isinstance(x, Element): 

# Real 

if isinstance(x, RealIntervalFieldElement): 

mpfi_set(re, (<RealIntervalFieldElement>x).value) 

return return_real(im) 

if isinstance(x, RealNumber): 

mpfi_set_fr(re, (<RealNumber>x).value) 

return return_real(im) 

if isinstance(x, Rational): 

mpfi_set_q(re, (<Rational>x).value) 

return return_real(im) 

if isinstance(x, Integer): 

mpfi_set_z(re, (<Integer>x).value) 

return return_real(im) 

if isinstance(x, RealDoubleElement): 

mpfi_set_d(re, (<RealDoubleElement>x)._value) 

return return_real(im) 

  

# Complex 

if isinstance(x, ComplexIntervalFieldElement): 

zi = <ComplexIntervalFieldElement>x 

if im is NULL: 

if not mpfi_is_zero(zi.__im): 

raise TypeError(f"unable to convert complex interval {x!r} to real interval") 

else: 

mpfi_set(im, zi.__im) 

mpfi_set(re, zi.__re) 

return 0 

if isinstance(x, ComplexNumber): 

zn = <ComplexNumber>x 

if im is NULL: 

if mpfr_sgn(zn.__im) != 0: 

raise TypeError(f"unable to convert complex number {x!r} to real interval") 

else: 

mpfi_set_fr(im, zn.__im) 

mpfi_set_fr(re, zn.__re) 

return 0 

if isinstance(x, ComplexDoubleElement): 

zd = <ComplexDoubleElement>x 

if im is NULL: 

if zd._complex.dat[1] != 0: 

raise TypeError(f"unable to convert complex number {x!r} to real interval") 

else: 

mpfi_set_d(im, zd._complex.dat[1]) 

mpfi_set_d(re, zd._complex.dat[0]) 

return 0 

else: # not a Sage Element 

# Real 

if isinstance(x, float): 

mpfi_set_d(re, PyFloat_AS_DOUBLE(x)) 

return return_real(im) 

if integer_check_long(x, &value, &err): 

if err == 0: 

mpfi_set_si(re, value) 

else: 

mpfi_set_via_RR(re, x, field) 

return return_real(im) 

if isinstance(x, unicode): 

x = x.encode("ascii") 

if isinstance(x, bytes): 

s = (<bytes>x).replace(b'..', b',').replace(b' ', b'').replace(b'+infinity', b'@inf@').replace(b'-infinity', b'-@inf@') 

if mpfi_set_str(re, s, base): 

raise TypeError(f"unable to convert {x!r} to real interval") 

return return_real(im) 

  

# Complex 

if isinstance(x, Gen): 

imag = x.imag() 

if im is NULL: 

if imag: 

raise TypeError(f"unable to convert complex PARI/GP element {x!r} to real interval") 

else: 

mpfi_set_via_RR(im, imag, field) 

mpfi_set_via_RR(re, x.real(), field) 

return 0 

if isinstance(x, complex): 

imag = PyComplex_ImagAsDouble(x) 

if im is NULL: 

if imag: 

raise TypeError(f"unable to convert complex number {x!r} to real interval") 

else: 

mpfi_set_d(im, imag) 

mpfi_set_d(re, PyComplex_RealAsDouble(x)) 

return 0 

  

# No known type, try _real_mpfi_ or _complex_mpfi_ methods 

if im is not NULL: 

try: 

m = x._complex_mpfi_ 

except AttributeError: 

pass 

else: 

if not isinstance(field, ComplexIntervalField_class): 

field = field.complex_field() 

e = <ComplexIntervalFieldElement?>m(field) 

mpfi_swap(re, e.__re) 

mpfi_swap(im, e.__im) 

return 0 

  

try: 

m = x._real_mpfi_ 

except AttributeError: 

pass 

else: 

if not isinstance(field, RealIntervalField_class): 

field = field.real_field() 

ri = <RealIntervalFieldElement?>m(field) 

mpfi_swap(re, ri.value) 

return return_real(im) 

  

# Finally, try converting via the corresponding RealField 

mpfi_set_via_RR(re, x, field) 

return return_real(im) 

  

  

cdef int mpfi_interv_sage(mpfi_ptr re, mpfi_ptr im, x, y, field, int base) except -1: 

""" 

Like ``mpfi_set_sage`` but construct the interval around ``x`` and 

``y``. It is not required that ``x <= y`` or that ``x`` and ``y`` 

are of the same type. 

  

INPUT: see ``mpfi_set_sage`` 

""" 

cdef long valx, valy 

cdef int err 

if type(x) is type(y): 

if isinstance(x, RealNumber): 

mpfi_interv_fr(re, (<RealNumber>x).value, (<RealNumber>y).value) 

return return_real(im) 

if isinstance(x, RealDoubleElement): 

mpfi_interv_d(re, (<RealDoubleElement>x)._value, (<RealDoubleElement>y)._value) 

return return_real(im) 

if isinstance(x, Integer): 

mpfi_interv_z(re, (<Integer>x).value, (<Integer>y).value) 

return return_real(im) 

if isinstance(x, Rational): 

mpfi_interv_q(re, (<Rational>x).value, (<Rational>y).value) 

return return_real(im) 

if isinstance(x, float): 

mpfi_interv_d(re, PyFloat_AS_DOUBLE(x), PyFloat_AS_DOUBLE(y)) 

return return_real(im) 

# General check for C long 

integer_check_long(x, &valx, &err) 

if err == 0: 

integer_check_long(y, &valy, &err) 

if err == 0: 

mpfi_interv_si(re, valx, valy) 

return return_real(im) 

  

# General case: convert both x and y to an interval and take the 

# union 

  

# First handle x 

mpfi_set_sage(re, im, x, field, base) 

  

# Now handle y, which requires temporary mpfi variables. 

cdef mpfi_t tmp1, tmp2 

cdef mpfr_prec_t prec = mpfi_get_prec(re) 

  

mpfi_init2(tmp1, prec) 

cdef mpfi_ptr tmpim = NULL 

if im is not NULL: 

mpfi_init2(tmp2, prec) 

tmpim = tmp2 

  

try: 

mpfi_set_sage(tmp1, tmpim, y, field, base) 

mpfi_union(re, re, tmp1) 

if im is not NULL: 

mpfi_union(im, im, tmpim) 

finally: 

mpfi_clear(tmp1) 

if tmpim is not NULL: 

mpfi_clear(tmpim) 

  

  

cdef int mpfi_set_via_RR(mpfi_ptr re, x, field) except -1: 

""" 

Convert ``x`` to an MPFI interval by converting ``x`` to the 

appropriate real fields. 

  

INPUT: see ``mpfi_set_sage`` 

""" 

cdef RealIntervalField_class RIF 

if isinstance(field, RealIntervalField_class): 

RIF = <RealIntervalField_class>field 

else: 

RIF = <RealIntervalField_class?>field.real_field() 

  

try: 

ra = RIF.__lower_field(x) 

rb = RIF.__upper_field(x) 

except TypeError: 

raise TypeError(f"unable to convert {x!r} to real interval") 

mpfi_interv_fr(re, (<RealNumber>ra).value, (<RealNumber>rb).value)