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

"Global proof preferences" 

from __future__ import print_function 

 

from sage.structure.sage_object import SageObject 

 

class _ProofPref(SageObject): 

""" 

An object that holds global proof preferences. For now these are merely True/False flags for various parts of Sage that use probabilistic algorithms. 

A True flag means that the subsystem (such as linear algebra or number fields) should return results that are true unconditionally: the correctness should not depend on an algorithm with a nonzero probability of returning an incorrect answer or on the truth of any unproven conjectures. 

A False flag means that the subsystem can use faster methods to return answers that have a very small probability of being wrong. 

""" 

def __init__(self, proof = True): 

self._require_proof = {} 

self._require_proof["arithmetic"] = proof 

self._require_proof["elliptic_curve"] = proof 

self._require_proof["linear_algebra"] = proof 

self._require_proof["number_field"] = proof 

self._require_proof["polynomial"] = proof 

self._require_proof["other"] = proof 

 

def arithmetic(self, t = None): 

""" 

Controls the default proof strategy for integer arithmetic algorithms (such as primality testing). 

 

INPUT: 

 

t -- boolean or None 

 

OUTPUT: 

 

If t == True, requires integer arithmetic operations to (by default) return results that are true unconditionally: the correctness will not depend on an algorithm with a nonzero probability of returning an incorrect answer or on the truth of any unproven conjectures. 

If t == False, allows integer arithmetic operations to (by default) return results that may depend on unproven conjectures or on probabilistic algorithms. Such algorithms often have a substantial speed improvement over those requiring proof. 

If t is None, returns the integer arithmetic proof status. 

 

EXAMPLES:: 

 

sage: proof.arithmetic() 

True 

sage: proof.arithmetic(False) 

sage: proof.arithmetic() 

False 

sage: proof.arithmetic(True) 

sage: proof.arithmetic() 

True 

""" 

if t is None: 

return self._require_proof["arithmetic"] 

self._require_proof["arithmetic"] = bool(t) 

 

def elliptic_curve(self, t = None): 

""" 

Controls the default proof strategy for elliptic curve algorithms. 

 

INPUT: 

 

t -- boolean or None 

 

OUTPUT: 

 

If t == True, requires elliptic curve algorithms to (by default) return results that are true unconditionally: the correctness will not depend on an algorithm with a nonzero probability of returning an incorrect answer or on the truth of any unproven conjectures. 

If t == False, allows elliptic curve algorithms to (by default) return results that may depend on unproven conjectures or on probabilistic algorithms. Such algorithms often have a substantial speed improvement over those requiring proof. 

If t is None, returns the current elliptic curve proof status. 

 

EXAMPLES:: 

 

sage: proof.elliptic_curve() 

True 

sage: proof.elliptic_curve(False) 

sage: proof.elliptic_curve() 

False 

sage: proof.elliptic_curve(True) 

sage: proof.elliptic_curve() 

True 

""" 

if t is None: 

return self._require_proof["elliptic_curve"] 

self._require_proof["elliptic_curve"] = bool(t) 

 

def linear_algebra(self, t = None): 

""" 

Controls the default proof strategy for linear algebra algorithms. 

 

INPUT: 

 

t -- boolean or None 

 

OUTPUT: 

 

If t == True, requires linear algebra algorithms to (by default) return results that are true unconditionally: the correctness will not depend on an algorithm with a nonzero probability of returning an incorrect answer or on the truth of any unproven conjectures. 

If t == False, allows linear algebra algorithms to (by default) return results that may depend on unproven conjectures or on probabilistic algorithms. Such algorithms often have a substantial speed improvement over those requiring proof. 

If t is None, returns the current linear algebra proof status. 

 

EXAMPLES:: 

 

sage: proof.linear_algebra() 

True 

sage: proof.linear_algebra(False) 

sage: proof.linear_algebra() 

False 

sage: proof.linear_algebra(True) 

sage: proof.linear_algebra() 

True 

""" 

if t is None: 

return self._require_proof["linear_algebra"] 

self._require_proof["linear_algebra"] = bool(t) 

 

def number_field(self, t = None): 

""" 

Controls the default proof strategy for number field algorithms. 

 

INPUT: 

 

t -- boolean or None 

 

OUTPUT: 

 

If t == True, requires number field algorithms to (by default) return results that are true unconditionally: the correctness will not depend on an algorithm with a nonzero probability of returning an incorrect answer or on the truth of any unproven conjectures. 

If t == False, allows number field algorithms to (by default) return results that may depend on unproven conjectures or on probabilistic algorithms. Such algorithms often have a substantial speed improvement over those requiring proof. 

If t is None, returns the current number field proof status. 

 

EXAMPLES:: 

 

sage: proof.number_field() 

True 

sage: proof.number_field(False) 

sage: proof.number_field() 

False 

sage: proof.number_field(True) 

sage: proof.number_field() 

True 

""" 

if t is None: 

return self._require_proof["number_field"] 

self._require_proof["number_field"] = bool(t) 

 

def polynomial(self, t = None): 

""" 

Controls the default proof strategy for polynomial algorithms. 

 

INPUT: 

 

t -- boolean or None 

 

OUTPUT: 

 

If t == True, requires polynomial algorithms to (by default) return results that are true unconditionally: the correctness will not depend on an algorithm with a nonzero probability of returning an incorrect answer or on the truth of any unproven conjectures. 

If t == False, allows polynomial algorithms to (by default) return results that may depend on unproven conjectures or on probabilistic algorithms. Such algorithms often have a substantial speed improvement over those requiring proof. 

If t is None, returns the current polynomial proof status. 

 

EXAMPLES:: 

 

sage: proof.polynomial() 

True 

sage: proof.polynomial(False) 

sage: proof.polynomial() 

False 

sage: proof.polynomial(True) 

sage: proof.polynomial() 

True 

""" 

if t is None: 

return self._require_proof["polynomial"] 

self._require_proof["polynomial"] = bool(t) 

 

 

_proof_prefs = _ProofPref(True) #Creates the global object that stores proof preferences. 

 

def get_flag(t = None, subsystem = None): 

""" 

Used for easily determining the correct proof flag to use. 

 

EXAMPLES:: 

 

sage: from sage.structure.proof.proof import get_flag 

sage: get_flag(False) 

False 

sage: get_flag(True) 

True 

sage: get_flag() 

True 

sage: proof.all(False) 

sage: get_flag() 

False 

""" 

if t is None: 

if subsystem in ["arithmetic", "elliptic_curve", "linear_algebra", "number_field","polynomial"]: 

return _proof_prefs._require_proof[subsystem] 

else: 

return _proof_prefs._require_proof["other"] 

return t 

 

 

class WithProof(object): 

""" 

Use WithProof to temporarily set the value of one of the proof 

systems for a block of code, with a guarantee that it will be set 

back to how it was before after the block is done, even if there is an error. 

 

EXAMPLES:: 

 

sage: proof.arithmetic(True) 

sage: with proof.WithProof('arithmetic',False): # this would hang "forever" if attempted with proof=True 

....: print((10^1000 + 453).is_prime()) 

....: print(1/0) 

Traceback (most recent call last): 

... 

ZeroDivisionError: rational division by zero 

sage: proof.arithmetic() 

True 

""" 

def __init__(self, subsystem, t): 

""" 

TESTS:: 

 

sage: proof.arithmetic(True) 

sage: P = proof.WithProof('arithmetic',False); P 

<sage.structure.proof.proof.WithProof object at ...> 

sage: P._subsystem 

'arithmetic' 

sage: P._t 

False 

sage: P._t_orig 

True 

""" 

self._subsystem = str(subsystem) 

self._t = bool(t) 

self._t_orig = _proof_prefs._require_proof[subsystem] 

 

def __enter__(self): 

""" 

TESTS:: 

 

sage: proof.arithmetic(True) 

sage: P = proof.WithProof('arithmetic',False) 

sage: P.__enter__() 

sage: proof.arithmetic() 

False 

sage: proof.arithmetic(True) 

""" 

_proof_prefs._require_proof[self._subsystem] = self._t 

 

def __exit__(self, *args): 

""" 

TESTS:: 

 

sage: proof.arithmetic(True) 

sage: P = proof.WithProof('arithmetic',False) 

sage: P.__enter__() 

sage: proof.arithmetic() 

False 

sage: P.__exit__() 

sage: proof.arithmetic() 

True 

""" 

_proof_prefs._require_proof[self._subsystem] = self._t_orig