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

r""" 

An example of set factory 

========================= 

 

The goal of this module is to exemplify the use of set factories. Note 

that the code is intentionally kept minimal; many things and in 

particular several iterators could be written in a more efficient way. 

 

.. SEEALSO:: 

 

:mod:`.set_factories` for an introduction to set 

factories, their specifications, and examples of their use and 

implementation based on this module. 

 

We describe here a factory used to construct the set `S` of couples `(x,y)` 

with `x` and `y` in `I:=\{0,1,2,3,4\}`, together with the following subsets, 

where `(a, b)\in S` 

 

.. MATH:: 

 

S_a := \{(x,y) \in S \mid x = a\}, 

 

S^b := \{(x,y) \in S \mid y = b\}, 

 

S_a^b := \{(x,y) \in S \mid x = a, y = b\}. 

 

 

""" 

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

# Copyright (C) 2012 Florent Hivert <florent.hivert at lri.fr> 

# 

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

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

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

 

from sage.structure.unique_representation import UniqueRepresentation 

from sage.structure.element_wrapper import ElementWrapper 

from sage.structure.set_factories import ( 

SetFactory, ParentWithSetFactory, TopMostParentPolicy) 

from sage.sets.all import DisjointUnionEnumeratedSets 

from sage.sets.family import LazyFamily 

from sage.categories.enumerated_sets import EnumeratedSets 

from sage.rings.integer import Integer 

from sage.misc.lazy_attribute import lazy_attribute 

 

MAX = 5 

 

 

class XYPairsFactory(SetFactory): 

r""" 

An example of set factory, for sets of pairs of integers. 

 

.. SEEALSO:: 

 

:mod:`.set_factories` for an introduction to set factories. 

""" 

def __call__(self, x=None, y=None, policy=None): 

r""" 

Construct the subset from constraints. 

 

Consider the set `S` of couples `(x,y)` with `x` and `y` in 

`I:=\{0,1,2,3,4\}`. Returns the subsets of element of `S` satisfying 

some constraints. 

 

INPUT: 

 

- ``x=a`` -- where ``a`` is an integer (default to ``None``). 

- ``y=b`` -- where ``b`` is an integer (default to ``None``). 

- ``policy`` -- the policy passed to the created set. 

 

.. SEEALSO:: 

 

:class:`.set_factories.SetFactoryPolicy` 

 

EXAMPLES: 

 

Let us first create the set factory:: 

 

sage: from sage.structure.set_factories_example import XYPairsFactory 

sage: XYPairs = XYPairsFactory() 

 

One can then use the set factory to construct a set:: 

 

sage: P = XYPairs(); P.list() 

[(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (0, 4), (1, 4), (2, 4), (3, 4), (4, 4)] 

 

.. NOTE:: 

 

This function is actually the ``__call__`` method of 

:class:`XYPairsFactory`. 

 

TESTS:: 

 

sage: TestSuite(P).run() 

 

""" 

if policy is None: 

policy = self._default_policy 

 

if isinstance(x, (Integer, int)): 

if isinstance(y, (Integer, int)): 

return SingletonPair(x, y, policy) 

return PairsX_(x, policy) 

elif isinstance(y, (Integer, int)): 

return Pairs_Y(y, policy) 

return AllPairs(policy) 

 

def add_constraints(self, cons, args_opts): 

r""" 

Add constraints to the set ``cons`` as per 

:meth:`SetFactory.add_constraints<.set_factories.SetFactory.add_constraints>`. 

 

This is a crude implementation for the sake of the demonstration which 

should not be taken as an example. 

 

EXAMPLES:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: XYPairs.add_constraints((3,None), ((2,), {})) 

Traceback (most recent call last): 

... 

ValueError: Duplicate value for constraints 'x': was 3 now 2 

sage: XYPairs.add_constraints((), ((2,), {})) 

(2, None) 

sage: XYPairs.add_constraints((), ((2,), {'y':3})) 

(2, 3) 

""" 

args, opts = args_opts 

 

res = list(cons) 

res += [None] * (2 - len(res)) 

 

def set_args(argss): 

for i, v in enumerate(argss): 

if res[i] is not None and v is not None: 

raise ValueError("Duplicate value for constraints '{}': " 

"was {} now {}".format(['x', 'y'][i], 

res[i], v)) 

if v is not None: 

res[i] = v 

set_args(args) 

 

def parse_args(x=None, y=None): 

set_args((x, y)) 

parse_args(**opts) 

if res == (None, None): 

return () 

return tuple(res) 

 

@lazy_attribute 

def _default_policy(self): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairsFactory 

sage: XYPairs = XYPairsFactory() 

sage: XYPairs._default_policy 

Set factory policy for <class 'sage.structure.set_factories_example.XYPair'> with parent AllPairs[=Factory for XY pairs(())] 

""" 

return TopMostParentPolicy(self, (), XYPair) 

 

def _repr_(self): 

""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: XYPairs # indirect doctest 

Factory for XY pairs 

""" 

return "Factory for XY pairs" 

 

XYPairs = XYPairsFactory() 

XYPairs.__doc__ = XYPairsFactory.__call__.__doc__ 

 

 

class XYPair(ElementWrapper): 

r""" 

A class for Elements `(x,y)` with `x` and `y` in `\{0,1,2,3,4\}`. 

 

EXAMPLES:: 

 

sage: from sage.structure.set_factories_example import XYPair 

sage: p = XYPair(Parent(), (0,1)); p 

(0, 1) 

sage: p = XYPair(Parent(), (0,8)) 

Traceback (most recent call last): 

... 

ValueError: numbers must be in range(5) 

""" 

def __init__(self, parent, value, check=True): 

""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: P = XYPairs(); p = P.list()[0] 

sage: TestSuite(p).run() 

""" 

if check: 

if not isinstance(value, tuple): 

raise ValueError("Value {} must be a tuple".format(value)) 

if len(value) != 2: 

raise ValueError("Value must be of length 2") 

if not all(int(x) in range(MAX) for x in value): 

raise ValueError("numbers must be in range({})".format(MAX)) 

ElementWrapper.__init__(self, parent, value) 

 

 

class AllPairs(ParentWithSetFactory, DisjointUnionEnumeratedSets): 

r""" 

This parent shows how one can use set factories together with 

:class:`DisjointUnionEnumeratedSets`. 

 

It is constructed as the disjoint union 

(:class:`DisjointUnionEnumeratedSets`) of :class:`Pairs_Y` parents: 

 

.. MATH:: 

 

S := \bigcup_{i = 0,1,..., 4} S^y 

 

.. WARNING:: 

 

When writing a parent ``P`` as a disjoint union of a family of parents 

``P_i``, the parents ``P_i`` must be constructed as facade parents for 

``P``. As a consequence, it should be passed ``P.facade_policy()`` as 

policy argument. See the source code of :meth:`pairs_y` for an 

example. 

 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: P = XYPairs(); P.list() 

[(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (0, 4), (1, 4), (2, 4), (3, 4), (4, 4)] 

""" 

def __init__(self, policy): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: TestSuite(XYPairs()).run() 

""" 

ParentWithSetFactory.__init__(self, (), policy=policy, 

category=EnumeratedSets().Finite()) 

DisjointUnionEnumeratedSets.__init__(self, 

LazyFamily(range(MAX), 

self.pairs_y), 

facade=True, keepkey=False, 

category=self.category()) 

 

def pairs_y(self, letter): 

r""" 

Construct the parent for the disjoint union 

 

Construct a parent in :class:`Pairs_Y` as a facade parent for 

``self``. 

 

This is an internal function which should be hidden from the user 

(typically under the name ``_pairs_y``. We put it here for 

documentation. 

 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs, XYPair 

sage: S = XYPairs() 

sage: S1 = S.pairs_y(1); S1 

{(a, 1) | a in range(5)} 

sage: S.an_element().parent() 

AllPairs 

 

sage: from sage.structure.set_factories import SelfParentPolicy 

sage: selfpolicy = SelfParentPolicy(XYPairs, XYPair) 

sage: selfS = XYPairs(policy=selfpolicy) 

sage: selfS1 = selfS.pairs_y(1); selfS1 

{(a, 1) | a in range(5)} 

sage: S.an_element().parent() is selfS 

False 

sage: selfS.an_element().parent() is selfS 

True 

""" 

return Pairs_Y(letter, policy=self.facade_policy()) 

 

def _repr_(self): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: XYPairs() # indirect doctest 

AllPairs 

""" 

return "AllPairs" 

 

def check_element(self, el, check): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: P = XYPairs() 

sage: P.check_element(P.an_element(), True) 

sage: XYPairs()((7, 0)) # indirect doctest 

Traceback (most recent call last): 

... 

ValueError: numbers must be in range(5) 

""" 

pass 

 

 

class PairsX_(ParentWithSetFactory, UniqueRepresentation): 

r""" 

The set of pairs `(x, 0), (x, 1), ..., (x, 4)`. 

 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: P = XYPairs(0); P.list() 

[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4)] 

""" 

def __init__(self, x, policy): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: TestSuite(XYPairs(0)).run() 

""" 

self._x = x 

ParentWithSetFactory.__init__(self, (x, None), policy=policy, 

category=EnumeratedSets().Finite()) 

 

def _repr_(self): 

""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: XYPairs(x=1) 

{(1, b) | b in range(5)} 

""" 

return "{(%s, b) | b in range(%s)}" % (self._x, MAX) 

 

def an_element(self): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: P = XYPairs(x=0); P.an_element() 

(0, 0) 

""" 

return self._element_constructor_((self._x, 0), check=False) 

 

def check_element(self, el, check): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: P = XYPairs(x=1) 

sage: P.check_element(P.an_element(), True) 

sage: XYPairs(x=1)((0, 0)) # indirect doctest 

Traceback (most recent call last): 

... 

ValueError: Wrong first coordinate 

""" 

(x, y) = el.value 

if x != self._x: 

raise ValueError("Wrong first coordinate") 

 

def __iter__(self): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: list(XYPairs(x=1)) 

[(1, 0), (1, 1), (1, 2), (1, 3), (1, 4)] 

""" 

for i in range(MAX): 

yield self._element_constructor_((self._x, i), check=False) 

 

 

class Pairs_Y(ParentWithSetFactory, DisjointUnionEnumeratedSets): 

r""" 

The set of pairs `(0, y), (1, y), ..., (4, y)`. 

 

It is constructed as the disjoint union 

(:class:`DisjointUnionEnumeratedSets`) of :class:`SingletonPair` parents: 

 

.. MATH:: 

 

S^y := \bigcup_{i = 0,1,..., 4} S_i^y 

 

.. SEEALSO:: 

 

:class:`AllPairs` for how to properly construct 

:class:`DisjointUnionEnumeratedSets` using 

:class:`~sage.structure.set_factories.ParentWithSetFactory`. 

 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: P = XYPairs(y=1); P.list() 

[(0, 1), (1, 1), (2, 1), (3, 1), (4, 1)] 

""" 

def __init__(self, y, policy): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: TestSuite(XYPairs(y=1)).run() 

""" 

self._y = y 

ParentWithSetFactory.__init__(self, (None, y), policy=policy, 

category=EnumeratedSets().Finite()) 

DisjointUnionEnumeratedSets.__init__( 

self, LazyFamily(range(MAX), self.single_pair), 

facade=True, keepkey=False, 

category=self.category()) # TODO remove and fix disjoint union. 

 

def _repr_(self): 

""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: XYPairs(y=1) 

{(a, 1) | a in range(5)} 

""" 

return "{(a, %s) | a in range(%s)}" % (self._y, MAX) 

 

def an_element(self): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: XYPairs(y=1).an_element() 

(0, 1) 

""" 

return self._element_constructor_((0, self._y), check=False) 

 

def single_pair(self, letter): 

r""" 

Construct the singleton pair parent 

 

Construct a singleton pair for ``(self.y, letter)`` as a facade parent 

for ``self``. 

 

.. SEEALSO:: 

 

:class:`AllPairs` for how to properly construct 

:class:`DisjointUnionEnumeratedSets` using 

:class:`~sage.structure.set_factories.ParentWithSetFactory`. 

 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: P = XYPairs(y=1) 

sage: P.single_pair(0) 

{(0, 1)} 

sage: P.single_pair(0).an_element().parent() 

AllPairs 

""" 

return SingletonPair(letter, self._y, policy=self.facade_policy()) 

 

def check_element(self, el, check): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: P = XYPairs(y=1) 

sage: P.check_element(P.an_element(), True) 

sage: XYPairs(y=1)((1, 0)) # indirect doctest 

Traceback (most recent call last): 

... 

ValueError: Wrong second coordinate 

""" 

(x, y) = el.value 

if y != self._y: 

raise ValueError("Wrong second coordinate") 

 

 

class SingletonPair(ParentWithSetFactory, UniqueRepresentation): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: P = XYPairs(0,1); P.list() 

[(0, 1)] 

""" 

def __init__(self, x, y, policy): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: TestSuite(XYPairs(0,1)).run() 

""" 

self._xy = (x, y) 

ParentWithSetFactory.__init__(self, (x, y), policy=policy, 

category=EnumeratedSets().Finite()) 

 

def _repr_(self): 

""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: XYPairs(x=2, y=1) 

{(2, 1)} 

""" 

return "{%s}" % (self._xy,) 

 

def check_element(self, el, check): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: XYPairs(0,1).check_element(XYPairs()((0,1)), True) 

sage: XYPairs(0,1).check_element(XYPairs()((1,0)), True) 

Traceback (most recent call last): 

... 

ValueError: Wrong coordinate 

sage: XYPairs(0,1)((1,1)) 

Traceback (most recent call last): 

... 

ValueError: Wrong coordinate 

""" 

if el.value != self._xy: 

raise ValueError("Wrong coordinate") 

 

def __iter__(self): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: list(XYPairs(0,1)) 

[(0, 1)] 

""" 

yield self._element_constructor_(self._xy, check=False)