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

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

from __future__ import print_function, division 

 

from collections import MutableMapping, defaultdict 

 

from sympy.core import (Add, Mul, Pow, Integer, Number, NumberSymbol,) 

from sympy.core.numbers import ImaginaryUnit 

from sympy.core.sympify import _sympify 

from sympy.core.rules import Transform 

from sympy.core.logic import fuzzy_or, fuzzy_and 

from sympy.matrices.expressions import MatMul 

 

from sympy.functions.elementary.complexes import Abs 

 

from sympy.assumptions.ask import Q 

from sympy.assumptions.assume import Predicate, AppliedPredicate 

from sympy.logic.boolalg import (Equivalent, Implies, And, Or, 

BooleanFunction, Not) 

 

# APIs here may be subject to change 

 

# XXX: Better name? 

class UnevaluatedOnFree(BooleanFunction): 

""" 

Represents a Boolean function that remains unevaluated on free predicates 

 

This is intended to be a superclass of other classes, which define the 

behavior on singly applied predicates. 

 

A free predicate is a predicate that is not applied, or a combination 

thereof. For example, Q.zero or Or(Q.positive, Q.negative). 

 

A singly applied predicate is a free predicate applied everywhere to a 

single expression. For instance, Q.zero(x) and Or(Q.positive(x*y), 

Q.negative(x*y)) are singly applied, but Or(Q.positive(x), Q.negative(y)) 

and Or(Q.positive, Q.negative(y)) are not. 

 

The boolean literals True and False are considered to be both free and 

singly applied. 

 

This class raises ValueError unless the input is a free predicate or a 

singly applied predicate. 

 

On a free predicate, this class remains unevaluated. On a singly applied 

predicate, the method apply() is called and returned, or the original 

expression returned if apply() returns None. When apply() is called, 

self.expr is set to the unique expression that the predicates are applied 

at. self.pred is set to the free form of the predicate. 

 

The typical usage is to create this class with free predicates and 

evaluate it using .rcall(). 

 

""" 

def __new__(cls, arg): 

# Mostly type checking here 

arg = _sympify(arg) 

predicates = arg.atoms(Predicate) 

applied_predicates = arg.atoms(AppliedPredicate) 

if predicates and applied_predicates: 

raise ValueError("arg must be either completely free or singly applied") 

if not applied_predicates: 

obj = BooleanFunction.__new__(cls, arg) 

obj.pred = arg 

obj.expr = None 

return obj 

predicate_args = {pred.args[0] for pred in applied_predicates} 

if len(predicate_args) > 1: 

raise ValueError("The AppliedPredicates in arg must be applied to a single expression.") 

obj = BooleanFunction.__new__(cls, arg) 

obj.expr = predicate_args.pop() 

obj.pred = arg.xreplace(Transform(lambda e: e.func, lambda e: 

isinstance(e, AppliedPredicate))) 

applied = obj.apply() 

if applied is None: 

return obj 

return applied 

 

def apply(self): 

return 

 

 

class AllArgs(UnevaluatedOnFree): 

""" 

Class representing vectorizing a predicate over all the .args of an 

expression 

 

See the docstring of UnevaluatedOnFree for more information on this 

class. 

 

The typical usage is to evaluate predicates with expressions using .rcall(). 

 

Example 

======= 

 

>>> from sympy.assumptions.sathandlers import AllArgs 

>>> from sympy import symbols, Q 

>>> x, y = symbols('x y') 

>>> a = AllArgs(Q.positive | Q.negative) 

>>> a 

AllArgs(Or(Q.negative, Q.positive)) 

>>> a.rcall(x*y) 

And(Or(Q.negative(x), Q.positive(x)), Or(Q.negative(y), Q.positive(y))) 

""" 

 

def apply(self): 

return And(*[self.pred.rcall(arg) for arg in self.expr.args]) 

 

 

class AnyArgs(UnevaluatedOnFree): 

""" 

Class representing vectorizing a predicate over any of the .args of an 

expression. 

 

See the docstring of UnevaluatedOnFree for more information on this 

class. 

 

The typical usage is to evaluate predicates with expressions using .rcall(). 

 

Example 

======= 

 

>>> from sympy.assumptions.sathandlers import AnyArgs 

>>> from sympy import symbols, Q 

>>> x, y = symbols('x y') 

>>> a = AnyArgs(Q.positive & Q.negative) 

>>> a 

AnyArgs(And(Q.negative, Q.positive)) 

>>> a.rcall(x*y) 

Or(And(Q.negative(x), Q.positive(x)), And(Q.negative(y), Q.positive(y))) 

""" 

 

def apply(self): 

return Or(*[self.pred.rcall(arg) for arg in self.expr.args]) 

 

 

class ExactlyOneArg(UnevaluatedOnFree): 

""" 

Class representing a predicate holding on exactly one of the .args of an 

expression. 

 

See the docstring of UnevaluatedOnFree for more information on this 

class. 

 

The typical usage is to evaluate predicate with expressions using 

.rcall(). 

 

Example 

======= 

 

>>> from sympy.assumptions.sathandlers import ExactlyOneArg 

>>> from sympy import symbols, Q 

>>> x, y = symbols('x y') 

>>> a = ExactlyOneArg(Q.positive) 

>>> a 

ExactlyOneArg(Q.positive) 

>>> a.rcall(x*y) 

Or(And(Not(Q.positive(x)), Q.positive(y)), And(Not(Q.positive(y)), Q.positive(x))) 

""" 

def apply(self): 

expr = self.expr 

pred = self.pred 

pred_args = [pred.rcall(arg) for arg in expr.args] 

# Technically this is xor, but if one term in the disjunction is true, 

# it is not possible for the remainder to be true, so regular or is 

# fine in this case. 

return Or(*[And(pred_args[i], *map(Not, pred_args[:i] + 

pred_args[i+1:])) for i in range(len(pred_args))]) 

# Note: this is the equivalent cnf form. The above is more efficient 

# as the first argument of an implication, since p >> q is the same as 

# q | ~p, so the the ~ will convert the Or to and, and one just needs 

# to distribute the q across it to get to cnf. 

 

# return And(*[Or(*map(Not, c)) for c in combinations(pred_args, 2)]) & Or(*pred_args) 

 

 

def _old_assump_replacer(obj): 

# Things to be careful of: 

# - real means real or infinite in the old assumptions. 

# - nonzero does not imply real in the old assumptions. 

# - finite means finite and not zero in the old assumptions. 

if not isinstance(obj, AppliedPredicate): 

return obj 

 

e = obj.args[0] 

ret = None 

 

if obj.func == Q.positive: 

ret = fuzzy_and([e.is_finite, e.is_positive]) 

if obj.func == Q.zero: 

ret = e.is_zero 

if obj.func == Q.negative: 

ret = fuzzy_and([e.is_finite, e.is_negative]) 

if obj.func == Q.nonpositive: 

ret = fuzzy_and([e.is_finite, e.is_nonpositive]) 

if obj.func == Q.nonzero: 

ret = fuzzy_and([e.is_nonzero, e.is_finite]) 

if obj.func == Q.nonnegative: 

ret = fuzzy_and([fuzzy_or([e.is_zero, e.is_finite]), 

e.is_nonnegative]) 

 

if obj.func == Q.rational: 

ret = e.is_rational 

if obj.func == Q.irrational: 

ret = e.is_irrational 

 

if obj.func == Q.even: 

ret = e.is_even 

if obj.func == Q.odd: 

ret = e.is_odd 

if obj.func == Q.integer: 

ret = e.is_integer 

if obj.func == Q.imaginary: 

ret = e.is_imaginary 

if obj.func == Q.commutative: 

ret = e.is_commutative 

 

if ret is None: 

return obj 

return ret 

 

 

def evaluate_old_assump(pred): 

""" 

Replace assumptions of expressions replaced with their values in the old 

assumptions (like Q.negative(-1) => True). Useful because some direct 

computations for numeric objects is defined most conveniently in the old 

assumptions. 

 

""" 

return pred.xreplace(Transform(_old_assump_replacer)) 

 

 

class CheckOldAssump(UnevaluatedOnFree): 

def apply(self): 

return Equivalent(self.args[0], evaluate_old_assump(self.args[0])) 

 

 

class CheckIsPrime(UnevaluatedOnFree): 

def apply(self): 

from sympy import isprime 

return Equivalent(self.args[0], isprime(self.expr)) 

 

 

class CustomLambda(object): 

""" 

Interface to lambda with rcall 

 

Workaround until we get a better way to represent certain facts. 

""" 

def __init__(self, lamda): 

self.lamda = lamda 

 

def rcall(self, *args): 

return self.lamda(*args) 

 

 

class ClassFactRegistry(MutableMapping): 

""" 

Register handlers against classes 

 

``registry[C] = handler`` registers ``handler`` for class 

``C``. ``registry[C]`` returns a set of handlers for class ``C``, or any 

of its superclasses. 

""" 

def __init__(self, d=None): 

d = d or {} 

self.d = defaultdict(frozenset, d) 

super(ClassFactRegistry, self).__init__() 

 

def __setitem__(self, key, item): 

self.d[key] = frozenset(item) 

 

def __getitem__(self, key): 

ret = self.d[key] 

for k in self.d: 

if issubclass(key, k): 

ret |= self.d[k] 

return ret 

 

def __delitem__(self, key): 

del self.d[key] 

 

def __iter__(self): 

return self.d.__iter__() 

 

def __len__(self): 

return len(self.d) 

 

def __repr__(self): 

return repr(self.d) 

 

 

fact_registry = ClassFactRegistry() 

 

 

def register_fact(klass, fact, registry=fact_registry): 

registry[klass] |= {fact} 

 

 

for klass, fact in [ 

(Mul, Equivalent(Q.zero, AnyArgs(Q.zero))), 

(MatMul, Implies(AllArgs(Q.square), Equivalent(Q.invertible, AllArgs(Q.invertible)))), 

(Add, Implies(AllArgs(Q.positive), Q.positive)), 

(Add, Implies(AllArgs(Q.negative), Q.negative)), 

(Mul, Implies(AllArgs(Q.positive), Q.positive)), 

(Mul, Implies(AllArgs(Q.commutative), Q.commutative)), 

(Mul, Implies(AllArgs(Q.real), Q.commutative)), 

 

(Pow, CustomLambda(lambda power: Implies(Q.real(power.base) & 

Q.even(power.exp) & Q.nonnegative(power.exp), Q.nonnegative(power)))), 

(Pow, CustomLambda(lambda power: Implies(Q.nonnegative(power.base) & Q.odd(power.exp) & Q.nonnegative(power.exp), Q.nonnegative(power)))), 

(Pow, CustomLambda(lambda power: Implies(Q.nonpositive(power.base) & Q.odd(power.exp) & Q.nonnegative(power.exp), Q.nonpositive(power)))), 

 

# This one can still be made easier to read. I think we need basic pattern 

# matching, so that we can just write Equivalent(Q.zero(x**y), Q.zero(x) & Q.positive(y)) 

(Pow, CustomLambda(lambda power: Equivalent(Q.zero(power), Q.zero(power.base) & Q.positive(power.exp)))), 

(Integer, CheckIsPrime(Q.prime)), 

# Implicitly assumes Mul has more than one arg 

# Would be AllArgs(Q.prime | Q.composite) except 1 is composite 

(Mul, Implies(AllArgs(Q.prime), ~Q.prime)), 

# More advanced prime assumptions will require inequalities, as 1 provides 

# a corner case. 

(Mul, Implies(AllArgs(Q.imaginary | Q.real), Implies(ExactlyOneArg(Q.imaginary), Q.imaginary))), 

(Mul, Implies(AllArgs(Q.real), Q.real)), 

(Add, Implies(AllArgs(Q.real), Q.real)), 

# General Case: Odd number of imaginary args implies mul is imaginary(To be implemented) 

(Mul, Implies(AllArgs(Q.real), Implies(ExactlyOneArg(Q.irrational), 

Q.irrational))), 

(Add, Implies(AllArgs(Q.real), Implies(ExactlyOneArg(Q.irrational), 

Q.irrational))), 

(Mul, Implies(AllArgs(Q.rational), Q.rational)), 

(Add, Implies(AllArgs(Q.rational), Q.rational)), 

 

(Abs, Q.nonnegative), 

(Abs, Equivalent(AllArgs(~Q.zero), ~Q.zero)), 

 

# Including the integer qualification means we don't need to add any facts 

# for odd, since the assumptions already know that every integer is 

# exactly one of even or odd. 

(Mul, Implies(AllArgs(Q.integer), Equivalent(AnyArgs(Q.even), Q.even))), 

 

(Abs, Implies(AllArgs(Q.even), Q.even)), 

(Abs, Implies(AllArgs(Q.odd), Q.odd)), 

 

(Add, Implies(AllArgs(Q.integer), Q.integer)), 

(Add, Implies(ExactlyOneArg(~Q.integer), ~Q.integer)), 

(Mul, Implies(AllArgs(Q.integer), Q.integer)), 

(Mul, Implies(ExactlyOneArg(~Q.rational), ~Q.integer)), 

(Abs, Implies(AllArgs(Q.integer), Q.integer)), 

 

(Number, CheckOldAssump(Q.negative)), 

(Number, CheckOldAssump(Q.zero)), 

(Number, CheckOldAssump(Q.positive)), 

(Number, CheckOldAssump(Q.nonnegative)), 

(Number, CheckOldAssump(Q.nonzero)), 

(Number, CheckOldAssump(Q.nonpositive)), 

(Number, CheckOldAssump(Q.rational)), 

(Number, CheckOldAssump(Q.irrational)), 

(Number, CheckOldAssump(Q.even)), 

(Number, CheckOldAssump(Q.odd)), 

(Number, CheckOldAssump(Q.integer)), 

(Number, CheckOldAssump(Q.imaginary)), 

# For some reason NumberSymbol does not subclass Number 

(NumberSymbol, CheckOldAssump(Q.negative)), 

(NumberSymbol, CheckOldAssump(Q.zero)), 

(NumberSymbol, CheckOldAssump(Q.positive)), 

(NumberSymbol, CheckOldAssump(Q.nonnegative)), 

(NumberSymbol, CheckOldAssump(Q.nonzero)), 

(NumberSymbol, CheckOldAssump(Q.nonpositive)), 

(NumberSymbol, CheckOldAssump(Q.rational)), 

(NumberSymbol, CheckOldAssump(Q.irrational)), 

(NumberSymbol, CheckOldAssump(Q.imaginary)), 

(ImaginaryUnit, CheckOldAssump(Q.negative)), 

(ImaginaryUnit, CheckOldAssump(Q.zero)), 

(ImaginaryUnit, CheckOldAssump(Q.positive)), 

(ImaginaryUnit, CheckOldAssump(Q.nonnegative)), 

(ImaginaryUnit, CheckOldAssump(Q.nonzero)), 

(ImaginaryUnit, CheckOldAssump(Q.nonpositive)), 

(ImaginaryUnit, CheckOldAssump(Q.rational)), 

(ImaginaryUnit, CheckOldAssump(Q.irrational)), 

(ImaginaryUnit, CheckOldAssump(Q.imaginary)) 

]: 

 

register_fact(klass, fact)