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

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

""" 

Examples of sets 

""" 

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

# Copyright (C) 2009 Florent Hivert <Florent.Hivert@univ-rouen.fr> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# http://www.gnu.org/licenses/ 

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

 

from sage.structure.parent import Parent 

from sage.structure.element import Element 

from sage.categories.sets_cat import Sets 

from sage.rings.integer import Integer, IntegerWrapper 

from sage.rings.integer_ring import IntegerRing 

from sage.arith.all import is_prime 

from sage.structure.unique_representation import UniqueRepresentation 

 

 

class PrimeNumbers(UniqueRepresentation, Parent): 

""" 

An example of parent in the category of sets: the set of prime numbers. 

 

The elements are represented as plain integers in `\ZZ` (facade 

implementation). 

 

This is a minimal implementations. For more advanced examples of 

implementations, see also:: 

 

sage: P = Sets().example("facade") 

sage: P = Sets().example("inherits") 

sage: P = Sets().example("wrapper") 

 

EXAMPLES:: 

 

sage: P = Sets().example() 

sage: P(12) 

Traceback (most recent call last): 

... 

AssertionError: 12 is not a prime number 

sage: a = P.an_element() 

sage: a.parent() 

Integer Ring 

sage: x = P(13); x 

13 

sage: type(x) 

<type 'sage.rings.integer.Integer'> 

sage: x.parent() 

Integer Ring 

sage: 13 in P 

True 

sage: 12 in P 

False 

sage: y = x+1; y 

14 

sage: type(y) 

<type 'sage.rings.integer.Integer'> 

 

sage: TestSuite(P).run(verbose=True) 

running ._test_an_element() . . . pass 

running ._test_cardinality() . . . pass 

running ._test_category() . . . pass 

running ._test_elements() . . . 

Running the test suite of self.an_element() 

running ._test_category() . . . pass 

running ._test_eq() . . . pass 

running ._test_new() . . . pass 

running ._test_nonzero_equal() . . . pass 

running ._test_not_implemented_methods() . . . pass 

running ._test_pickling() . . . pass 

pass 

running ._test_elements_eq_reflexive() . . . pass 

running ._test_elements_eq_symmetric() . . . pass 

running ._test_elements_eq_transitive() . . . pass 

running ._test_elements_neq() . . . pass 

running ._test_eq() . . . pass 

running ._test_new() . . . pass 

running ._test_not_implemented_methods() . . . pass 

running ._test_pickling() . . . pass 

running ._test_some_elements() . . . pass 

""" 

def __init__(self): 

""" 

TESTS:: 

 

sage: from sage.categories.examples.sets_cat import PrimeNumbers 

sage: P = PrimeNumbers() 

sage: P.category() 

Category of facade sets 

sage: P is Sets().example() 

True 

""" 

Parent.__init__(self, facade = IntegerRing(), category = Sets()) 

 

def _repr_(self): 

""" 

TESTS:: 

 

sage: Sets().example() # indirect doctest 

Set of prime numbers (basic implementation) 

""" 

return "Set of prime numbers (basic implementation)" 

 

def an_element(self): 

""" 

Implements :meth:`Sets.ParentMethods.an_element`. 

 

TESTS:: 

 

sage: P = Sets().example() 

sage: x = P.an_element(); x 

47 

sage: x.parent() 

Integer Ring 

""" 

return self(47) # if speed is needed, call: self.element_class(47) 

 

def __contains__(self, p): 

""" 

TESTS:: 

 

sage: P = Sets().example() 

sage: 13 in P 

True 

sage: 12 in P 

False 

""" 

return isinstance(p, Integer) and p.is_prime() 

 

def _element_constructor_(self, e): 

""" 

TESTS:: 

 

sage: P = Sets().example() 

sage: P._element_constructor_(13) == 13 

True 

sage: P._element_constructor_(13).parent() 

Integer Ring 

sage: P._element_constructor_(14) 

Traceback (most recent call last): 

... 

AssertionError: 14 is not a prime number 

""" 

p = self.element_class(e) 

assert is_prime(p), "%s is not a prime number"%(p) 

return p 

 

element_class = Integer 

 

 

 

 

 

from sage.misc.abstract_method import abstract_method 

class PrimeNumbers_Abstract(UniqueRepresentation, Parent): 

""" 

This class shows how to write a parent while keeping the choice of the 

datastructure for the children open. Different class with fixed 

datastructure will then be constructed by inheriting from 

:class:`PrimeNumbers_Abstract`. 

 

This is used by: 

 

sage: P = Sets().example("facade") 

sage: P = Sets().example("inherits") 

sage: P = Sets().example("wrapper") 

""" 

def __init__(self): 

""" 

TESTS:: 

 

sage: P = Sets().example("inherits") 

""" 

Parent.__init__(self, category = Sets()) 

 

def _repr_(self): 

""" 

TESTS:: 

 

sage: Sets().example("inherits") # indirect doctest 

Set of prime numbers 

""" 

return "Set of prime numbers" 

 

def an_element(self): 

""" 

Implements :meth:`Sets.ParentMethods.an_element`. 

 

TESTS:: 

 

sage: P = Sets().example("inherits") 

sage: x = P.an_element(); x 

47 

sage: x.parent() 

Set of prime numbers 

""" 

return self._from_integer_(47) 

 

def _element_constructor_(self, i): 

""" 

Constructs an element of self from an integer, testing that 

this integer is indeed prime. 

 

EXAMPLES:: 

 

sage: P = Sets().example("inherits") 

sage: P(13) # indirect doctest 

13 

sage: P(42) 

Traceback (most recent call last): 

... 

ValueError: 42 is not a prime number 

""" 

if i in self: 

return self._from_integer_(i) 

else: 

raise ValueError("%s is not a prime number"%(i)) 

 

@abstract_method 

def _from_integer_(self, i): 

""" 

Fast construction of an element of self from an integer. No prime 

checking is performed. To be defined. 

 

EXAMPLES:: 

 

sage: P = Sets().example("inherits") 

sage: P._from_integer_(13) 

13 

sage: P._from_integer_(42) # Don't do that at home kids! 

42 

sage: P(42) 

Traceback (most recent call last): 

... 

ValueError: 42 is not a prime number 

""" 

 

def next(self, i): 

""" 

Return the next prime number. 

 

EXAMPLES:: 

 

sage: P = Sets().example("inherits") 

sage: x = P.next(P.an_element()); x 

53 

sage: x.parent() 

Set of prime numbers 

""" 

assert(i in self) 

return self._from_integer_((Integer(i) + 1).next_prime()) 

 

def some_elements(self): 

""" 

Return some prime numbers. 

 

EXAMPLES:: 

 

sage: P = Sets().example("inherits") 

sage: P.some_elements() 

[47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] 

""" 

x = self.an_element() 

res = [x] 

for i in range(10): 

x = self.next(x) 

res.append(x) 

return res 

 

class Element(Element): 

def is_prime(self): 

""" 

Return whether ``self`` is a prime number. 

 

EXAMPLES:: 

 

sage: P = Sets().example("inherits") 

sage: x = P.an_element() 

sage: P.an_element().is_prime() 

True 

""" 

return True 

 

def next(self): 

""" 

Return the next prime number. 

 

EXAMPLES:: 

 

sage: P = Sets().example("inherits") 

sage: p = P.an_element(); p 

47 

sage: p.next() 

53 

 

.. NOTE:: 

 

This method is not meant to implement the protocol iterator, 

and thus not subject to Python 2 vs Python 3 incompatibilities. 

""" 

return self.parent().next(self) 

 

 

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

class PrimeNumbers_Inherits(PrimeNumbers_Abstract): 

""" 

An example of parent in the category of sets: the set of prime numbers. 

In this implementation, the element are stored as object of a new class 

which inherits from the class Integer (technically :class:`IntegerWrapper`). 

 

EXAMPLES:: 

 

sage: P = Sets().example("inherits") 

sage: P 

Set of prime numbers 

sage: P(12) 

Traceback (most recent call last): 

... 

ValueError: 12 is not a prime number 

sage: a = P.an_element() 

sage: a.parent() 

Set of prime numbers 

sage: x = P(13); x 

13 

sage: x.is_prime() 

True 

sage: type(x) 

<class 'sage.categories.examples.sets_cat.PrimeNumbers_Inherits_with_category.element_class'> 

sage: x.parent() 

Set of prime numbers 

sage: P(13) in P 

True 

sage: y = x+1; y 

14 

sage: type(y) 

<type 'sage.rings.integer.Integer'> 

sage: y.parent() 

Integer Ring 

sage: type(P(13)+P(17)) 

<type 'sage.rings.integer.Integer'> 

sage: type(P(2)+P(3)) 

<type 'sage.rings.integer.Integer'> 

 

sage: z = P.next(x); z 

17 

sage: type(z) 

<class 'sage.categories.examples.sets_cat.PrimeNumbers_Inherits_with_category.element_class'> 

sage: z.parent() 

Set of prime numbers 

 

sage: TestSuite(P).run(verbose=True) 

running ._test_an_element() . . . pass 

running ._test_cardinality() . . . pass 

running ._test_category() . . . pass 

running ._test_elements() . . . 

Running the test suite of self.an_element() 

running ._test_category() . . . pass 

running ._test_eq() . . . pass 

running ._test_new() . . . pass 

running ._test_not_implemented_methods() . . . pass 

running ._test_pickling() . . . pass 

pass 

running ._test_elements_eq_reflexive() . . . pass 

running ._test_elements_eq_symmetric() . . . pass 

running ._test_elements_eq_transitive() . . . pass 

running ._test_elements_neq() . . . pass 

running ._test_eq() . . . pass 

running ._test_new() . . . pass 

running ._test_not_implemented_methods() . . . pass 

running ._test_pickling() . . . pass 

running ._test_some_elements() . . . pass 

 

See also:: 

 

sage: P = Sets().example("facade") 

sage: P = Sets().example("inherits") 

sage: P = Sets().example("wrapper") 

""" 

 

def __init__(self): 

""" 

TESTS:: 

 

sage: P = Sets().example("inherits") 

sage: type(P(13)+P(17)) 

<type 'sage.rings.integer.Integer'> 

sage: type(P(2)+P(3)) 

<type 'sage.rings.integer.Integer'> 

""" 

super(PrimeNumbers_Inherits, self).__init__() 

self._populate_coercion_lists_(embedding=IntegerRing()) 

 

def __contains__(self, p): 

""" 

TESTS:: 

 

sage: P = Sets().example("inherits") 

sage: 13 in P, P(13) in P 

(True, True) 

sage: 12 in P 

False 

""" 

return (isinstance(p, self.element_class) and p.parent() is self 

or isinstance(p, Integer) and p.is_prime()) 

 

def _from_integer_(self, p): 

""" 

TESTS:: 

 

sage: P = Sets().example("inherits") 

sage: P._from_integer_(13) 

13 

sage: P._from_integer_(42) # Don't do that at home kids! 

42 

""" 

return self.element_class(self, p) 

 

class Element(IntegerWrapper, PrimeNumbers_Abstract.Element): 

def __init__(self, parent, p): 

""" 

TESTS:: 

 

sage: P = Sets().example("inherits") 

sage: P(12) 

Traceback (most recent call last): 

... 

ValueError: 12 is not a prime number 

sage: x = P(13); type(x) 

<class 'sage.categories.examples.sets_cat.PrimeNumbers_Inherits_with_category.element_class'> 

sage: x.parent() is P 

True 

""" 

IntegerWrapper.__init__(self, parent, p) 

 

 

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

class PrimeNumbers_Wrapper(PrimeNumbers_Abstract): 

""" 

An example of parent in the category of sets: the set of prime numbers. 

 

In this second alternative implementation, the prime integer are stored as 

a attribute of a sage object by inheriting from :class:`ElementWrapper`. In 

this case we need to ensure conversion and coercion from this parent and 

its element to ``ZZ`` and ``Integer``. 

 

EXAMPLES:: 

 

sage: P = Sets().example("wrapper") 

sage: P(12) 

Traceback (most recent call last): 

... 

ValueError: 12 is not a prime number 

sage: a = P.an_element() 

sage: a.parent() 

Set of prime numbers (wrapper implementation) 

sage: x = P(13); x 

13 

sage: type(x) 

<class 'sage.categories.examples.sets_cat.PrimeNumbers_Wrapper_with_category.element_class'> 

sage: x.parent() 

Set of prime numbers (wrapper implementation) 

sage: 13 in P 

True 

sage: 12 in P 

False 

sage: y = x+1; y 

14 

sage: type(y) 

<type 'sage.rings.integer.Integer'> 

 

sage: z = P.next(x); z 

17 

sage: type(z) 

<class 'sage.categories.examples.sets_cat.PrimeNumbers_Wrapper_with_category.element_class'> 

sage: z.parent() 

Set of prime numbers (wrapper implementation) 

 

TESTS:: 

 

sage: TestSuite(P).run() 

""" 

def __init__(self): 

""" 

TESTS:: 

 

sage: P = Sets().example("wrapper") 

sage: P.category() 

Category of sets 

sage: P(13) == 13 

True 

sage: ZZ(P(13)) == 13 

True 

sage: P(13) + 1 == 14 

True 

""" 

Parent.__init__(self, category = Sets()) 

from sage.rings.integer_ring import IntegerRing 

from sage.categories.homset import Hom 

self.mor = Hom(self, IntegerRing())(lambda z: z.value) 

self._populate_coercion_lists_(embedding=self.mor) 

 

def _repr_(self): 

""" 

TESTS:: 

 

sage: Sets().example("wrapper") # indirect doctest 

Set of prime numbers (wrapper implementation) 

""" 

return "Set of prime numbers (wrapper implementation)" 

 

def __contains__(self, p): 

""" 

TESTS:: 

 

sage: P = Sets().example("wrapper") 

sage: 13 in P 

True 

sage: 12 in P 

False 

""" 

return (isinstance(p, self.element_class) and p.parent() == self or 

isinstance(p, Integer) and p.is_prime()) 

 

def _from_integer_(self, e): 

""" 

TESTS:: 

 

sage: P = Sets().example("wrapper") 

sage: P._from_integer_(13).parent() 

Set of prime numbers (wrapper implementation) 

sage: P._from_integer_(14) # Don't do that at home kids! 

14 

sage: P._element_constructor_(14) 

Traceback (most recent call last): 

... 

ValueError: 14 is not a prime number 

""" 

return self.element_class(self, Integer(e)) 

 

from sage.structure.element_wrapper import ElementWrapper 

class Element (ElementWrapper, PrimeNumbers_Abstract.Element): 

def _integer_(self, IntRing): 

""" 

Convert to an integer. 

 

TESTS:: 

 

sage: P = Sets().example("wrapper") 

sage: x = P.an_element() 

sage: Integer(x) # indirect doctest 

47 

""" 

return IntRing(self.value) 

 

 

 

 

 

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

class PrimeNumbers_Facade(PrimeNumbers_Abstract): 

""" 

An example of parent in the category of sets: the set of prime numbers. 

 

In this alternative implementation, the elements are represented 

as plain integers in `\ZZ` (facade implementation). 

 

EXAMPLES:: 

 

sage: P = Sets().example("facade") 

sage: P(12) 

Traceback (most recent call last): 

... 

ValueError: 12 is not a prime number 

sage: a = P.an_element() 

sage: a.parent() 

Integer Ring 

sage: x = P(13); x 

13 

sage: type(x) 

<type 'sage.rings.integer.Integer'> 

sage: x.parent() 

Integer Ring 

sage: 13 in P 

True 

sage: 12 in P 

False 

sage: y = x+1; y 

14 

sage: type(y) 

<type 'sage.rings.integer.Integer'> 

 

sage: z = P.next(x); z 

17 

sage: type(z) 

<type 'sage.rings.integer.Integer'> 

sage: z.parent() 

Integer Ring 

 

The disadvantage of this implementation is that the elements do not know 

that they are prime, so that prime testing is slow:: 

 

sage: pf = Sets().example("facade").an_element() 

sage: timeit("pf.is_prime()") # random 

625 loops, best of 3: 4.1 us per loop 

 

compared to the other implementations where prime testing is only done if 

needed during the construction of the element, and later on the elements 

"know" that they are prime:: 

 

sage: pw = Sets().example("wrapper").an_element() 

sage: timeit("pw.is_prime()") # random 

625 loops, best of 3: 859 ns per loop 

 

sage: pi = Sets().example("inherits").an_element() 

sage: timeit("pw.is_prime()") # random 

625 loops, best of 3: 854 ns per loop 

 

Note also that the ``next`` method for the elements does not exist:: 

 

sage: pf.next() 

Traceback (most recent call last): 

... 

AttributeError: 'sage.rings.integer.Integer' object has no attribute 'next' 

 

unlike in the other implementations:: 

 

sage: pw.next() 

53 

sage: pi.next() 

53 

 

TESTS:: 

 

sage: TestSuite(P).run(verbose = True) 

running ._test_an_element() . . . pass 

running ._test_cardinality() . . . pass 

running ._test_category() . . . pass 

running ._test_elements() . . . 

Running the test suite of self.an_element() 

running ._test_category() . . . pass 

running ._test_eq() . . . pass 

running ._test_new() . . . pass 

running ._test_nonzero_equal() . . . pass 

running ._test_not_implemented_methods() . . . pass 

running ._test_pickling() . . . pass 

pass 

running ._test_elements_eq_reflexive() . . . pass 

running ._test_elements_eq_symmetric() . . . pass 

running ._test_elements_eq_transitive() . . . pass 

running ._test_elements_neq() . . . pass 

running ._test_eq() . . . pass 

running ._test_new() . . . pass 

running ._test_not_implemented_methods() . . . pass 

running ._test_pickling() . . . pass 

running ._test_some_elements() . . . pass 

""" 

 

def __init__(self): 

""" 

TESTS:: 

 

sage: P = Sets().example("inherits") 

""" 

Parent.__init__(self, facade = IntegerRing(), category = Sets()) 

 

def _repr_(self): 

""" 

TESTS:: 

 

sage: Sets().example("facade") # indirect doctest 

Set of prime numbers (facade implementation) 

""" 

return "Set of prime numbers (facade implementation)" 

 

def __contains__(self, p): 

""" 

TESTS:: 

 

sage: P = Sets().example("facade") 

sage: 13 in P 

True 

sage: 12 in P 

False 

""" 

return isinstance(p, Integer) and p.is_prime() 

 

def _from_integer_(self, e): 

""" 

TESTS:: 

 

sage: P = Sets().example("facade") 

sage: P._from_integer_(13).parent() 

Integer Ring 

sage: P._from_integer_(14) # Don't do that at home kids! 

14 

sage: P._element_constructor_(14) 

Traceback (most recent call last): 

... 

ValueError: 14 is not a prime number 

""" 

return self.element_class(e) 

 

element_class = Integer