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

""" 

Handlers for keys related to number theory: prime, even, odd, etc. 

""" 

from __future__ import print_function, division 

 

from sympy.assumptions import Q, ask 

from sympy.assumptions.handlers import CommonHandler 

from sympy.ntheory import isprime 

from sympy.core import S, Float 

 

 

class AskPrimeHandler(CommonHandler): 

""" 

Handler for key 'prime' 

Test that an expression represents a prime number. When the 

expression is a number the result, when True, is subject to 

the limitations of isprime() which is used to return the result. 

""" 

 

@staticmethod 

def Expr(expr, assumptions): 

return expr.is_prime 

 

@staticmethod 

def _number(expr, assumptions): 

# helper method 

try: 

i = int(expr.round()) 

if not (expr - i).equals(0): 

raise TypeError 

except TypeError: 

return False 

return isprime(expr) 

 

@staticmethod 

def Basic(expr, assumptions): 

# Just use int(expr) once 

# https://github.com/sympy/sympy/issues/4561 

# is solved 

if expr.is_number: 

return AskPrimeHandler._number(expr, assumptions) 

 

@staticmethod 

def Mul(expr, assumptions): 

if expr.is_number: 

return AskPrimeHandler._number(expr, assumptions) 

for arg in expr.args: 

if ask(Q.integer(arg), assumptions): 

pass 

else: 

break 

else: 

# a product of integers can't be a prime 

return False 

 

@staticmethod 

def Pow(expr, assumptions): 

""" 

Integer**Integer -> !Prime 

""" 

if expr.is_number: 

return AskPrimeHandler._number(expr, assumptions) 

if ask(Q.integer(expr.exp), assumptions) and \ 

ask(Q.integer(expr.base), assumptions): 

return False 

 

@staticmethod 

def Integer(expr, assumptions): 

return isprime(expr) 

 

Rational, Infinity, NegativeInfinity, ImaginaryUnit = [staticmethod(CommonHandler.AlwaysFalse)]*4 

 

@staticmethod 

def Float(expr, assumptions): 

return AskPrimeHandler._number(expr, assumptions) 

 

@staticmethod 

def NumberSymbol(expr, assumptions): 

return AskPrimeHandler._number(expr, assumptions) 

 

 

class AskCompositeHandler(CommonHandler): 

 

@staticmethod 

def Expr(expr, assumptions): 

return expr.is_composite 

 

@staticmethod 

def Basic(expr, assumptions): 

_positive = ask(Q.positive(expr), assumptions) 

if _positive: 

_integer = ask(Q.integer(expr), assumptions) 

if _integer: 

_prime = ask(Q.prime(expr), assumptions) 

if _prime is None: 

return 

# Positive integer which is not prime is not 

# necessarily composite 

if expr.equals(1): 

return False 

return not _prime 

else: 

return _integer 

else: 

return _positive 

 

 

class AskEvenHandler(CommonHandler): 

 

@staticmethod 

def Expr(expr, assumptions): 

return expr.is_even 

 

@staticmethod 

def _number(expr, assumptions): 

# helper method 

try: 

i = int(expr.round()) 

if not (expr - i).equals(0): 

raise TypeError 

except TypeError: 

return False 

if isinstance(expr, (float, Float)): 

return False 

return i % 2 == 0 

 

@staticmethod 

def Basic(expr, assumptions): 

if expr.is_number: 

return AskEvenHandler._number(expr, assumptions) 

 

@staticmethod 

def Mul(expr, assumptions): 

""" 

Even * Integer -> Even 

Even * Odd -> Even 

Integer * Odd -> ? 

Odd * Odd -> Odd 

Even * Even -> Even 

Integer * Integer -> Even if Integer + Integer = Odd 

-> ? otherwise 

""" 

if expr.is_number: 

return AskEvenHandler._number(expr, assumptions) 

even, odd, irrational, acc = False, 0, False, 1 

for arg in expr.args: 

# check for all integers and at least one even 

if ask(Q.integer(arg), assumptions): 

if ask(Q.even(arg), assumptions): 

even = True 

elif ask(Q.odd(arg), assumptions): 

odd += 1 

elif not even and acc != 1: 

if ask(Q.odd(acc + arg), assumptions): 

even = True 

elif ask(Q.irrational(arg), assumptions): 

# one irrational makes the result False 

# two makes it undefined 

if irrational: 

break 

irrational = True 

else: 

break 

acc = arg 

else: 

if irrational: 

return False 

if even: 

return True 

if odd == len(expr.args): 

return False 

 

@staticmethod 

def Add(expr, assumptions): 

""" 

Even + Odd -> Odd 

Even + Even -> Even 

Odd + Odd -> Even 

 

""" 

if expr.is_number: 

return AskEvenHandler._number(expr, assumptions) 

_result = True 

for arg in expr.args: 

if ask(Q.even(arg), assumptions): 

pass 

elif ask(Q.odd(arg), assumptions): 

_result = not _result 

else: 

break 

else: 

return _result 

 

@staticmethod 

def Pow(expr, assumptions): 

if expr.is_number: 

return AskEvenHandler._number(expr, assumptions) 

if ask(Q.integer(expr.exp), assumptions): 

if ask(Q.positive(expr.exp), assumptions): 

return ask(Q.even(expr.base), assumptions) 

elif ask(~Q.negative(expr.exp) & Q.odd(expr.base), assumptions): 

return False 

elif expr.base is S.NegativeOne: 

return False 

 

@staticmethod 

def Integer(expr, assumptions): 

return not bool(expr.p & 1) 

 

Rational, Infinity, NegativeInfinity, ImaginaryUnit = [staticmethod(CommonHandler.AlwaysFalse)]*4 

 

@staticmethod 

def NumberSymbol(expr, assumptions): 

return AskEvenHandler._number(expr, assumptions) 

 

@staticmethod 

def Abs(expr, assumptions): 

if ask(Q.real(expr.args[0]), assumptions): 

return ask(Q.even(expr.args[0]), assumptions) 

 

@staticmethod 

def re(expr, assumptions): 

if ask(Q.real(expr.args[0]), assumptions): 

return ask(Q.even(expr.args[0]), assumptions) 

 

@staticmethod 

def im(expr, assumptions): 

if ask(Q.real(expr.args[0]), assumptions): 

return True 

 

 

class AskOddHandler(CommonHandler): 

""" 

Handler for key 'odd' 

Test that an expression represents an odd number 

""" 

 

@staticmethod 

def Expr(expr, assumptions): 

return expr.is_odd 

 

@staticmethod 

def Basic(expr, assumptions): 

_integer = ask(Q.integer(expr), assumptions) 

if _integer: 

_even = ask(Q.even(expr), assumptions) 

if _even is None: 

return None 

return not _even 

return _integer