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

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

""" 

Handlers for predicates related to set membership: integer, rational, etc. 

""" 

from __future__ import print_function, division 

 

from sympy.assumptions import Q, ask 

from sympy.assumptions.handlers import CommonHandler, test_closed_group 

from sympy.core.numbers import pi 

from sympy.functions.elementary.exponential import exp, log 

from sympy import I 

 

 

class AskIntegerHandler(CommonHandler): 

""" 

Handler for Q.integer 

Test that an expression belongs to the field of integer numbers 

""" 

 

@staticmethod 

def Expr(expr, assumptions): 

return expr.is_integer 

 

@staticmethod 

def _number(expr, assumptions): 

# helper method 

try: 

i = int(expr.round()) 

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

raise TypeError 

return True 

except TypeError: 

return False 

 

@staticmethod 

def Add(expr, assumptions): 

""" 

Integer + Integer -> Integer 

Integer + !Integer -> !Integer 

!Integer + !Integer -> ? 

""" 

if expr.is_number: 

return AskIntegerHandler._number(expr, assumptions) 

return test_closed_group(expr, assumptions, Q.integer) 

 

@staticmethod 

def Mul(expr, assumptions): 

""" 

Integer*Integer -> Integer 

Integer*Irrational -> !Integer 

Odd/Even -> !Integer 

Integer*Rational -> ? 

""" 

if expr.is_number: 

return AskIntegerHandler._number(expr, assumptions) 

_output = True 

for arg in expr.args: 

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

if arg.is_Rational: 

if arg.q == 2: 

return ask(Q.even(2*expr), assumptions) 

if ~(arg.q & 1): 

return None 

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

if _output: 

_output = False 

else: 

return 

else: 

return 

else: 

return _output 

 

Pow = Add 

 

int, Integer = [staticmethod(CommonHandler.AlwaysTrue)]*2 

 

Pi, Exp1, GoldenRatio, Infinity, NegativeInfinity, ImaginaryUnit = \ 

[staticmethod(CommonHandler.AlwaysFalse)]*6 

 

@staticmethod 

def Rational(expr, assumptions): 

# rationals with denominator one get 

# evaluated to Integers 

return False 

 

@staticmethod 

def Abs(expr, assumptions): 

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

 

@staticmethod 

def MatrixElement(expr, assumptions): 

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

 

Determinant = Trace = MatrixElement 

 

 

class AskRationalHandler(CommonHandler): 

""" 

Handler for Q.rational 

Test that an expression belongs to the field of rational numbers 

""" 

 

 

@staticmethod 

def Expr(expr, assumptions): 

return expr.is_rational 

 

@staticmethod 

def Add(expr, assumptions): 

""" 

Rational + Rational -> Rational 

Rational + !Rational -> !Rational 

!Rational + !Rational -> ? 

""" 

if expr.is_number: 

if expr.as_real_imag()[1]: 

return False 

return test_closed_group(expr, assumptions, Q.rational) 

 

Mul = Add 

 

@staticmethod 

def Pow(expr, assumptions): 

""" 

Rational ** Integer -> Rational 

Irrational ** Rational -> Irrational 

Rational ** Irrational -> ? 

""" 

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

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

elif ask(Q.rational(expr.exp), assumptions): 

if ask(Q.prime(expr.base), assumptions): 

return False 

 

Rational, Float = \ 

[staticmethod(CommonHandler.AlwaysTrue)]*2 # Float is finite-precision 

 

ImaginaryUnit, Infinity, NegativeInfinity, Pi, Exp1, GoldenRatio = \ 

[staticmethod(CommonHandler.AlwaysFalse)]*6 

 

@staticmethod 

def exp(expr, assumptions): 

x = expr.args[0] 

if ask(Q.rational(x), assumptions): 

return ask(~Q.nonzero(x), assumptions) 

 

@staticmethod 

def cot(expr, assumptions): 

x = expr.args[0] 

if ask(Q.rational(x), assumptions): 

return False 

 

@staticmethod 

def log(expr, assumptions): 

x = expr.args[0] 

if ask(Q.rational(x), assumptions): 

return ask(~Q.nonzero(x - 1), assumptions) 

 

sin, cos, tan, asin, atan = [exp]*5 

acos, acot = log, cot 

 

 

class AskIrrationalHandler(CommonHandler): 

 

 

@staticmethod 

def Expr(expr, assumptions): 

return expr.is_irrational 

 

@staticmethod 

def Basic(expr, assumptions): 

_real = ask(Q.real(expr), assumptions) 

if _real: 

_rational = ask(Q.rational(expr), assumptions) 

if _rational is None: 

return None 

return not _rational 

else: 

return _real 

 

 

class AskRealHandler(CommonHandler): 

""" 

Handler for Q.real 

Test that an expression belongs to the field of real numbers 

""" 

 

@staticmethod 

def Expr(expr, assumptions): 

return expr.is_real 

 

@staticmethod 

def _number(expr, assumptions): 

# let as_real_imag() work first since the expression may 

# be simpler to evaluate 

i = expr.as_real_imag()[1].evalf(2) 

if i._prec != 1: 

return not i 

# allow None to be returned if we couldn't show for sure 

# that i was 0 

 

@staticmethod 

def Add(expr, assumptions): 

""" 

Real + Real -> Real 

Real + (Complex & !Real) -> !Real 

""" 

if expr.is_number: 

return AskRealHandler._number(expr, assumptions) 

return test_closed_group(expr, assumptions, Q.real) 

 

@staticmethod 

def Mul(expr, assumptions): 

""" 

Real*Real -> Real 

Real*Imaginary -> !Real 

Imaginary*Imaginary -> Real 

""" 

if expr.is_number: 

return AskRealHandler._number(expr, assumptions) 

result = True 

for arg in expr.args: 

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

pass 

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

result = result ^ True 

else: 

break 

else: 

return result 

 

@staticmethod 

def Pow(expr, assumptions): 

""" 

Real**Integer -> Real 

Positive**Real -> Real 

Real**(Integer/Even) -> Real if base is nonnegative 

Real**(Integer/Odd) -> Real 

Imaginary**(Integer/Even) -> Real 

Imaginary**(Integer/Odd) -> not Real 

Imaginary**Real -> ? since Real could be 0 (giving real) or 1 (giving imaginary) 

b**Imaginary -> Real if log(b) is imaginary and b != 0 and exponent != integer multiple of I*pi/log(b) 

Real**Real -> ? e.g. sqrt(-1) is imaginary and sqrt(2) is not 

""" 

if expr.is_number: 

return AskRealHandler._number(expr, assumptions) 

 

if expr.base.func == exp: 

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

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

return True 

# If the i = (exp's arg)/(I*pi) is an integer or half-integer 

# multiple of I*pi then 2*i will be an integer. In addition, 

# exp(i*I*pi) = (-1)**i so the overall realness of the expr 

# can be determined by replacing exp(i*I*pi) with (-1)**i. 

i = expr.base.args[0]/I/pi 

if ask(Q.integer(2*i), assumptions): 

return ask(Q.real(((-1)**i)**expr.exp), assumptions) 

return 

 

if ask(Q.imaginary(expr.base), assumptions): 

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

odd = ask(Q.odd(expr.exp), assumptions) 

if odd is not None: 

return not odd 

return 

 

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

imlog = ask(Q.imaginary(log(expr.base)), assumptions) 

if imlog is not None: 

# I**i -> real, log(I) is imag; 

# (2*I)**i -> complex, log(2*I) is not imag 

return imlog 

 

if ask(Q.real(expr.base), assumptions): 

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

if expr.exp.is_Rational and \ 

ask(Q.even(expr.exp.q), assumptions): 

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

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

return True 

elif ask(Q.positive(expr.base), assumptions): 

return True 

elif ask(Q.negative(expr.base), assumptions): 

return False 

 

Rational, Float, Pi, Exp1, GoldenRatio, Abs, re, im = \ 

[staticmethod(CommonHandler.AlwaysTrue)]*8 

 

ImaginaryUnit, Infinity, NegativeInfinity = \ 

[staticmethod(CommonHandler.AlwaysFalse)]*3 

 

@staticmethod 

def sin(expr, assumptions): 

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

return True 

 

cos = sin 

 

@staticmethod 

def exp(expr, assumptions): 

return ask(Q.integer(expr.args[0]/I/pi) | Q.real(expr.args[0]), assumptions) 

 

@staticmethod 

def log(expr, assumptions): 

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

 

@staticmethod 

def MatrixElement(expr, assumptions): 

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

 

Determinant = Trace = MatrixElement 

 

 

class AskExtendedRealHandler(AskRealHandler): 

""" 

Handler for Q.extended_real 

Test that an expression belongs to the field of extended real numbers, 

that is real numbers union {Infinity, -Infinity} 

""" 

 

@staticmethod 

def Add(expr, assumptions): 

return test_closed_group(expr, assumptions, Q.extended_real) 

 

Mul, Pow = [Add]*2 

 

Infinity, NegativeInfinity = [staticmethod(CommonHandler.AlwaysTrue)]*2 

 

 

class AskHermitianHandler(AskRealHandler): 

""" 

Handler for Q.hermitian 

Test that an expression belongs to the field of Hermitian operators 

""" 

 

@staticmethod 

def Add(expr, assumptions): 

""" 

Hermitian + Hermitian -> Hermitian 

Hermitian + !Hermitian -> !Hermitian 

""" 

if expr.is_number: 

return AskRealHandler._number(expr, assumptions) 

return test_closed_group(expr, assumptions, Q.hermitian) 

 

@staticmethod 

def Mul(expr, assumptions): 

""" 

As long as there is at most only one noncommutative term: 

Hermitian*Hermitian -> Hermitian 

Hermitian*Antihermitian -> !Hermitian 

Antihermitian*Antihermitian -> Hermitian 

""" 

if expr.is_number: 

return AskRealHandler._number(expr, assumptions) 

nccount = 0 

result = True 

for arg in expr.args: 

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

result = result ^ True 

elif not ask(Q.hermitian(arg), assumptions): 

break 

if ask(~Q.commutative(arg), assumptions): 

nccount += 1 

if nccount > 1: 

break 

else: 

return result 

 

@staticmethod 

def Pow(expr, assumptions): 

""" 

Hermitian**Integer -> Hermitian 

""" 

if expr.is_number: 

return AskRealHandler._number(expr, assumptions) 

if ask(Q.hermitian(expr.base), assumptions): 

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

return True 

 

@staticmethod 

def sin(expr, assumptions): 

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

return True 

 

cos, exp = [sin]*2 

 

 

class AskComplexHandler(CommonHandler): 

""" 

Handler for Q.complex 

Test that an expression belongs to the field of complex numbers 

""" 

 

@staticmethod 

def Expr(expr, assumptions): 

return expr.is_complex 

 

@staticmethod 

def Add(expr, assumptions): 

return test_closed_group(expr, assumptions, Q.complex) 

 

Mul, Pow = [Add]*2 

 

Number, sin, cos, log, exp, re, im, NumberSymbol, Abs, ImaginaryUnit = \ 

[staticmethod(CommonHandler.AlwaysTrue)]*10 # they are all complex functions or expressions 

 

Infinity, NegativeInfinity = [staticmethod(CommonHandler.AlwaysFalse)]*2 

 

@staticmethod 

def MatrixElement(expr, assumptions): 

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

 

Determinant = Trace = MatrixElement 

 

 

class AskImaginaryHandler(CommonHandler): 

""" 

Handler for Q.imaginary 

Test that an expression belongs to the field of imaginary numbers, 

that is, numbers in the form x*I, where x is real 

""" 

 

@staticmethod 

def Expr(expr, assumptions): 

return expr.is_imaginary 

 

@staticmethod 

def _number(expr, assumptions): 

# let as_real_imag() work first since the expression may 

# be simpler to evaluate 

r = expr.as_real_imag()[0].evalf(2) 

if r._prec != 1: 

return not r 

# allow None to be returned if we couldn't show for sure 

# that r was 0 

 

@staticmethod 

def Add(expr, assumptions): 

""" 

Imaginary + Imaginary -> Imaginary 

Imaginary + Complex -> ? 

Imaginary + Real -> !Imaginary 

""" 

if expr.is_number: 

return AskImaginaryHandler._number(expr, assumptions) 

 

reals = 0 

for arg in expr.args: 

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

pass 

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

reals += 1 

else: 

break 

else: 

if reals == 0: 

return True 

if reals == 1 or (len(expr.args) == reals): 

# two reals could sum 0 thus giving an imaginary 

return False 

 

@staticmethod 

def Mul(expr, assumptions): 

""" 

Real*Imaginary -> Imaginary 

Imaginary*Imaginary -> Real 

""" 

if expr.is_number: 

return AskImaginaryHandler._number(expr, assumptions) 

result = False 

reals = 0 

for arg in expr.args: 

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

result = result ^ True 

elif not ask(Q.real(arg), assumptions): 

break 

else: 

if reals == len(expr.args): 

return False 

return result 

 

@staticmethod 

def Pow(expr, assumptions): 

""" 

Imaginary**Odd -> Imaginary 

Imaginary**Even -> Real 

b**Imaginary -> !Imaginary if exponent is an integer multiple of I*pi/log(b) 

Imaginary**Real -> ? 

Positive**Real -> Real 

Negative**Integer -> Real 

Negative**(Integer/2) -> Imaginary 

Negative**Real -> not Imaginary if exponent is not Rational 

""" 

if expr.is_number: 

return AskImaginaryHandler._number(expr, assumptions) 

 

if expr.base.func == exp: 

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

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

return False 

i = expr.base.args[0]/I/pi 

if ask(Q.integer(2*i), assumptions): 

return ask(Q.imaginary(((-1)**i)**expr.exp), assumptions) 

 

if ask(Q.imaginary(expr.base), assumptions): 

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

odd = ask(Q.odd(expr.exp), assumptions) 

if odd is not None: 

return odd 

return 

 

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

imlog = ask(Q.imaginary(log(expr.base)), assumptions) 

if imlog is not None: 

return False # I**i -> real; (2*I)**i -> complex ==> not imaginary 

 

if ask(Q.real(expr.base) & Q.real(expr.exp), assumptions): 

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

return False 

else: 

rat = ask(Q.rational(expr.exp), assumptions) 

if not rat: 

return rat 

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

return False 

else: 

half = ask(Q.integer(2*expr.exp), assumptions) 

if half: 

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

return half 

 

 

@staticmethod 

def log(expr, assumptions): 

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

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

return False 

return 

# XXX it should be enough to do 

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

# but ask(Q.nonpositive(exp(x)), Q.imaginary(x)) -> None; 

# it should return True since exp(x) will be either 0 or complex 

if expr.args[0].func == exp: 

if expr.args[0].args[0] in [I, -I]: 

return True 

im = ask(Q.imaginary(expr.args[0]), assumptions) 

if im is False: 

return False 

 

@staticmethod 

def exp(expr, assumptions): 

a = expr.args[0]/I/pi 

return ask(Q.integer(2*a) & ~Q.integer(a), assumptions) 

 

@staticmethod 

def Number(expr, assumptions): 

return not (expr.as_real_imag()[1] == 0) 

 

NumberSymbol = Number 

 

ImaginaryUnit = staticmethod(CommonHandler.AlwaysTrue) 

 

 

class AskAntiHermitianHandler(AskImaginaryHandler): 

""" 

Handler for Q.antihermitian 

Test that an expression belongs to the field of anti-Hermitian operators, 

that is, operators in the form x*I, where x is Hermitian 

""" 

 

@staticmethod 

def Add(expr, assumptions): 

""" 

Antihermitian + Antihermitian -> Antihermitian 

Antihermitian + !Antihermitian -> !Antihermitian 

""" 

if expr.is_number: 

return AskImaginaryHandler._number(expr, assumptions) 

return test_closed_group(expr, assumptions, Q.antihermitian) 

 

@staticmethod 

def Mul(expr, assumptions): 

""" 

As long as there is at most only one noncommutative term: 

Hermitian*Hermitian -> !Antihermitian 

Hermitian*Antihermitian -> Antihermitian 

Antihermitian*Antihermitian -> !Antihermitian 

""" 

if expr.is_number: 

return AskImaginaryHandler._number(expr, assumptions) 

nccount = 0 

result = False 

for arg in expr.args: 

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

result = result ^ True 

elif not ask(Q.hermitian(arg), assumptions): 

break 

if ask(~Q.commutative(arg), assumptions): 

nccount += 1 

if nccount > 1: 

break 

else: 

return result 

 

@staticmethod 

def Pow(expr, assumptions): 

""" 

Hermitian**Integer -> !Antihermitian 

Antihermitian**Even -> !Antihermitian 

Antihermitian**Odd -> Antihermitian 

""" 

if expr.is_number: 

return AskImaginaryHandler._number(expr, assumptions) 

if ask(Q.hermitian(expr.base), assumptions): 

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

return False 

elif ask(Q.antihermitian(expr.base), assumptions): 

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

return False 

elif ask(Q.odd(expr.exp), assumptions): 

return True 

 

 

class AskAlgebraicHandler(CommonHandler): 

"""Handler for Q.algebraic key. """ 

 

@staticmethod 

def Add(expr, assumptions): 

return test_closed_group(expr, assumptions, Q.algebraic) 

 

@staticmethod 

def Mul(expr, assumptions): 

return test_closed_group(expr, assumptions, Q.algebraic) 

 

@staticmethod 

def Pow(expr, assumptions): 

return expr.exp.is_Rational and ask( 

Q.algebraic(expr.base), assumptions) 

 

@staticmethod 

def Rational(expr, assumptions): 

return expr.q != 0 

 

Float, GoldenRatio, ImaginaryUnit, AlgebraicNumber = \ 

[staticmethod(CommonHandler.AlwaysTrue)]*4 

 

Infinity, NegativeInfinity, ComplexInfinity, Pi, Exp1 = \ 

[staticmethod(CommonHandler.AlwaysFalse)]*5 

 

@staticmethod 

def exp(expr, assumptions): 

x = expr.args[0] 

if ask(Q.algebraic(x), assumptions): 

return ask(~Q.nonzero(x), assumptions) 

 

@staticmethod 

def cot(expr, assumptions): 

x = expr.args[0] 

if ask(Q.algebraic(x), assumptions): 

return False 

 

@staticmethod 

def log(expr, assumptions): 

x = expr.args[0] 

if ask(Q.algebraic(x), assumptions): 

return ask(~Q.nonzero(x - 1), assumptions) 

 

sin, cos, tan, asin, atan = [exp]*5 

acos, acot = log, cot