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

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

""" 

Families 

 

A Family is an associative container which models a family 

`(f_i)_{i \in I}`. Then, ``f[i]`` returns the element of the family indexed by 

``i``. Whenever available, set and combinatorial class operations (counting, 

iteration, listing) on the family are induced from those of the index 

set. Families should be created through the :func:`Family` function. 

 

AUTHORS: 

 

- Nicolas Thiery (2008-02): initial release 

 

- Florent Hivert (2008-04): various fixes, cleanups and improvements. 

 

TESTS: 

 

Check :trac:`12482` (shall be run in a fresh session):: 

 

sage: P = Partitions(3) 

sage: Family(P, lambda x: x).category() 

Category of finite enumerated sets 

""" 

 

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

# Copyright (C) 2008 Nicolas Thiery <nthiery at users.sf.net>, 

# Mike Hansen <mhansen@gmail.com>, 

# Florent Hivert <Florent.Hivert@univ-rouen.fr> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

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

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

import types 

from copy import copy 

 

from six import itervalues 

from six.moves import range 

 

from sage.misc.cachefunc import cached_method 

from sage.structure.parent import Parent 

from sage.categories.enumerated_sets import EnumeratedSets 

from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets 

from sage.categories.infinite_enumerated_sets import InfiniteEnumeratedSets 

from sage.sets.finite_enumerated_set import FiniteEnumeratedSet 

from sage.misc.lazy_import import lazy_import 

from sage.rings.integer import Integer 

from sage.misc.misc import AttrCallObject 

lazy_import('sage.combinat.combinat', 'CombinatorialClass') 

 

def Family(indices, function=None, hidden_keys=[], hidden_function=None, lazy=False, name=None): 

r""" 

A Family is an associative container which models a family 

`(f_i)_{i \in I}`. Then, ``f[i]`` returns the element of the family 

indexed by `i`. Whenever available, set and combinatorial class 

operations (counting, iteration, listing) on the family are induced 

from those of the index set. 

 

There are several available implementations (classes) for different 

usages; Family serves as a factory, and will create instances of 

the appropriate classes depending on its arguments. 

 

INPUT: 

 

- ``indices`` -- the indices for the family 

- ``function`` -- (optional) the function `f` applied to all visible 

indices; the default is the identity function 

- ``hidden_keys`` -- (optional) a list of hidden indices that can be 

accessed through ``my_family[i]`` 

- ``hidden_function`` -- (optional) a function for the hidden indices 

- ``lazy`` -- boolean (default: ``False``); whether the family is lazily 

created or not; if the indices are infinite, then this is automatically 

made ``True`` 

- ``name`` -- (optional) the name of the function; only used when the 

family is lazily created via a function 

 

EXAMPLES: 

 

In its simplest form, a list `l = [l_0, l_1, \ldots, l_{\ell}]` or a 

tuple by itself is considered as the family `(l_i)_{i \in I}` where 

`I` is the set `\{0, \ldots, \ell\}` where `\ell` is ``len(l) - 1``. 

So ``Family(l)`` returns the corresponding family:: 

 

sage: f = Family([1,2,3]) 

sage: f 

Family (1, 2, 3) 

sage: f = Family((1,2,3)) 

sage: f 

Family (1, 2, 3) 

 

Instead of a list you can as well pass any iterable object:: 

 

sage: f = Family(2*i+1 for i in [1,2,3]); 

sage: f 

Family (3, 5, 7) 

 

A family can also be constructed from a dictionary ``t``. The resulting 

family is very close to ``t``, except that the elements of the family 

are the values of ``t``. Here, we define the family 

`(f_i)_{i \in \{3,4,7\}}` with `f_3 = a`, `f_4 = b`, and `f_7 = d`:: 

 

sage: f = Family({3: 'a', 4: 'b', 7: 'd'}) 

sage: f 

Finite family {3: 'a', 4: 'b', 7: 'd'} 

sage: f[7] 

'd' 

sage: len(f) 

3 

sage: list(f) 

['a', 'b', 'd'] 

sage: [ x for x in f ] 

['a', 'b', 'd'] 

sage: f.keys() 

[3, 4, 7] 

sage: 'b' in f 

True 

sage: 'e' in f 

False 

 

A family can also be constructed by its index set `I` and 

a function `f`, as in `(f(i))_{i \in I}`:: 

 

sage: f = Family([3,4,7], lambda i: 2*i) 

sage: f 

Finite family {3: 6, 4: 8, 7: 14} 

sage: f.keys() 

[3, 4, 7] 

sage: f[7] 

14 

sage: list(f) 

[6, 8, 14] 

sage: [x for x in f] 

[6, 8, 14] 

sage: len(f) 

3 

 

By default, all images are computed right away, and stored in an internal 

dictionary:: 

 

sage: f = Family((3,4,7), lambda i: 2*i) 

sage: f 

Finite family {3: 6, 4: 8, 7: 14} 

 

Note that this requires all the elements of the list to be 

hashable. One can ask instead for the images `f(i)` to be computed 

lazily, when needed:: 

 

sage: f = Family([3,4,7], lambda i: 2*i, lazy=True) 

sage: f 

Lazy family (<lambda>(i))_{i in [3, 4, 7]} 

sage: f[7] 

14 

sage: list(f) 

[6, 8, 14] 

sage: [x for x in f] 

[6, 8, 14] 

 

This allows in particular for modeling infinite families:: 

 

sage: f = Family(ZZ, lambda i: 2*i, lazy=True) 

sage: f 

Lazy family (<lambda>(i))_{i in Integer Ring} 

sage: f.keys() 

Integer Ring 

sage: f[1] 

2 

sage: f[-5] 

-10 

sage: i = iter(f) 

sage: next(i), next(i), next(i), next(i), next(i) 

(0, 2, -2, 4, -4) 

 

Note that the ``lazy`` keyword parameter is only needed to force 

laziness. Usually it is automatically set to a correct default value (ie: 

``False`` for finite data structures and ``True`` for enumerated sets:: 

 

sage: f == Family(ZZ, lambda i: 2*i) 

True 

 

Beware that for those kind of families len(f) is not supposed to 

work. As a replacement, use the .cardinality() method:: 

 

sage: f = Family(Permutations(3), attrcall("to_lehmer_code")) 

sage: list(f) 

[[0, 0, 0], [0, 1, 0], [1, 0, 0], [1, 1, 0], [2, 0, 0], [2, 1, 0]] 

sage: f.cardinality() 

6 

 

Caveat: Only certain families with lazy behavior can be pickled. In 

particular, only functions that work with Sage's pickle_function 

and unpickle_function (in sage.misc.fpickle) will correctly 

unpickle. The following two work:: 

 

sage: f = Family(Permutations(3), lambda p: p.to_lehmer_code()); f 

Lazy family (<lambda>(i))_{i in Standard permutations of 3} 

sage: f == loads(dumps(f)) 

True 

 

sage: f = Family(Permutations(3), attrcall("to_lehmer_code")); f 

Lazy family (i.to_lehmer_code())_{i in Standard permutations of 3} 

sage: f == loads(dumps(f)) 

True 

 

But this one does not:: 

 

sage: def plus_n(n): return lambda x: x+n 

sage: f = Family([1,2,3], plus_n(3), lazy=True); f 

Lazy family (<lambda>(i))_{i in [1, 2, 3]} 

sage: f == loads(dumps(f)) 

Traceback (most recent call last): 

... 

ValueError: Cannot pickle code objects from closures 

 

Finally, it can occasionally be useful to add some hidden elements 

in a family, which are accessible as ``f[i]``, but do not appear in the 

keys or the container operations:: 

 

sage: f = Family([3,4,7], lambda i: 2*i, hidden_keys=[2]) 

sage: f 

Finite family {3: 6, 4: 8, 7: 14} 

sage: f.keys() 

[3, 4, 7] 

sage: f.hidden_keys() 

[2] 

sage: f[7] 

14 

sage: f[2] 

4 

sage: list(f) 

[6, 8, 14] 

sage: [x for x in f] 

[6, 8, 14] 

sage: len(f) 

3 

 

The following example illustrates when the function is actually 

called:: 

 

sage: def compute_value(i): 

....: print('computing 2*'+str(i)) 

....: return 2*i 

sage: f = Family([3,4,7], compute_value, hidden_keys=[2]) 

computing 2*3 

computing 2*4 

computing 2*7 

sage: f 

Finite family {3: 6, 4: 8, 7: 14} 

sage: f.keys() 

[3, 4, 7] 

sage: f.hidden_keys() 

[2] 

sage: f[7] 

14 

sage: f[2] 

computing 2*2 

4 

sage: f[2] 

4 

sage: list(f) 

[6, 8, 14] 

sage: [x for x in f] 

[6, 8, 14] 

sage: len(f) 

3 

 

Here is a close variant where the function for the hidden keys is 

different from that for the other keys:: 

 

sage: f = Family([3,4,7], lambda i: 2*i, hidden_keys=[2], hidden_function = lambda i: 3*i) 

sage: f 

Finite family {3: 6, 4: 8, 7: 14} 

sage: f.keys() 

[3, 4, 7] 

sage: f.hidden_keys() 

[2] 

sage: f[7] 

14 

sage: f[2] 

6 

sage: list(f) 

[6, 8, 14] 

sage: [x for x in f] 

[6, 8, 14] 

sage: len(f) 

3 

 

Family accept finite and infinite EnumeratedSets as input:: 

 

sage: f = Family(FiniteEnumeratedSet([1,2,3])) 

sage: f 

Family (1, 2, 3) 

sage: from sage.categories.examples.infinite_enumerated_sets import NonNegativeIntegers 

sage: f = Family(NonNegativeIntegers()) 

sage: f 

Family (An example of an infinite enumerated set: the non negative integers) 

 

:: 

 

sage: f = Family(FiniteEnumeratedSet([3,4,7]), lambda i: 2*i) 

sage: f 

Finite family {3: 6, 4: 8, 7: 14} 

sage: f.keys() 

{3, 4, 7} 

sage: f[7] 

14 

sage: list(f) 

[6, 8, 14] 

sage: [x for x in f] 

[6, 8, 14] 

sage: len(f) 

3 

 

TESTS:: 

 

sage: f = Family({1:'a', 2:'b', 3:'c'}) 

sage: f 

Finite family {1: 'a', 2: 'b', 3: 'c'} 

sage: f[2] 

'b' 

sage: loads(dumps(f)) == f 

True 

 

:: 

 

sage: f = Family({1:'a', 2:'b', 3:'c'}, lazy=True) 

Traceback (most recent call last): 

ValueError: lazy keyword only makes sense together with function keyword ! 

 

:: 

 

sage: f = Family(list(range(1,27)), lambda i: chr(i+96)) 

sage: f 

Finite family {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'} 

sage: f[2] 

'b' 

 

The factory ``Family`` is supposed to be idempotent. We test this feature here:: 

 

sage: from sage.sets.family import FiniteFamily, LazyFamily, TrivialFamily 

sage: f = FiniteFamily({3: 'a', 4: 'b', 7: 'd'}) 

sage: g = Family(f) 

sage: f == g 

True 

 

sage: f = Family([3,4,7], lambda i: 2*i, hidden_keys=[2]) 

sage: g = Family(f) 

sage: f == g 

True 

 

sage: f = LazyFamily([3,4,7], lambda i: 2*i) 

sage: g = Family(f) 

sage: f == g 

True 

 

sage: f = TrivialFamily([3,4,7]) 

sage: g = Family(f) 

sage: f == g 

True 

 

A family should keep the order of the keys:: 

 

sage: f = Family(["c", "a", "b"], lambda i: 2*i) 

sage: list(f) 

['cc', 'aa', 'bb'] 

 

Even with hidden keys (see :trac:`22955`):: 

 

sage: f = Family(["c", "a", "b"], lambda i: 2*i, 

....: hidden_keys=[5], hidden_function=lambda i: i%2) 

sage: list(f) 

['cc', 'aa', 'bb'] 

 

Only the hidden function is applied to the hidden keys:: 

 

sage: f[5] 

1 

""" 

assert(isinstance(hidden_keys, list)) 

assert(isinstance(lazy, bool)) 

 

if hidden_keys == []: 

if hidden_function is not None: 

raise ValueError("hidden_function keyword only makes sense " 

"together with hidden_keys keyword !") 

if function is None: 

if lazy: 

raise ValueError("lazy keyword only makes sense together with function keyword !") 

if isinstance(indices, dict): 

return FiniteFamily(indices) 

if isinstance(indices, (list, tuple) ): 

return TrivialFamily(indices) 

if isinstance(indices, (FiniteFamily, LazyFamily, TrivialFamily) ): 

return indices 

if (indices in EnumeratedSets() 

or isinstance(indices, CombinatorialClass)): 

return EnumeratedFamily(indices) 

if hasattr(indices, "__iter__"): 

return TrivialFamily(indices) 

 

raise NotImplementedError 

if (isinstance(indices, (list, tuple, FiniteEnumeratedSet)) 

and not lazy): 

return FiniteFamily({i: function(i) for i in indices}, 

keys=indices) 

 

return LazyFamily(indices, function, name) 

if lazy: 

raise ValueError("lazy keyword is incompatible with hidden keys !") 

if hidden_function is None: 

hidden_function = function 

return FiniteFamilyWithHiddenKeys({i: function(i) for i in indices}, 

hidden_keys, hidden_function, 

keys=indices) 

 

class AbstractFamily(Parent): 

""" 

The abstract class for family 

 

Any family belongs to a class which inherits from :class:`AbstractFamily`. 

""" 

def hidden_keys(self): 

""" 

Returns the hidden keys of the family, if any. 

 

EXAMPLES:: 

 

sage: f = Family({3: 'a', 4: 'b', 7: 'd'}) 

sage: f.hidden_keys() 

[] 

""" 

return [] 

 

def zip(self, f, other, name=None): 

""" 

Given two families with same index set `I` (and same hidden 

keys if relevant), returns the family 

`( f(self[i], other[i]) )_{i \in I}` 

 

.. TODO:: generalize to any number of families and merge with map? 

 

EXAMPLES:: 

 

sage: f = Family({3: 'a', 4: 'b', 7: 'd'}) 

sage: g = Family({3: '1', 4: '2', 7: '3'}) 

sage: h = f.zip(lambda x,y: x+y, g) 

sage: list(h) 

['a1', 'b2', 'd3'] 

""" 

assert(self.keys() == other.keys()) 

assert(self.hidden_keys() == other.hidden_keys()) 

return Family(self.keys(), lambda i: f(self[i],other[i]), hidden_keys=self.hidden_keys(), name=name) 

 

def map(self, f, name=None): 

""" 

Returns the family `( f(\mathtt{self}[i]) )_{i \in I}`, where 

`I` is the index set of self. 

 

.. TODO:: good name? 

 

EXAMPLES:: 

 

sage: f = Family({3: 'a', 4: 'b', 7: 'd'}) 

sage: g = f.map(lambda x: x+'1') 

sage: list(g) 

['a1', 'b1', 'd1'] 

""" 

return Family(self.keys(), lambda i: f(self[i]), hidden_keys=self.hidden_keys(), name=name) 

 

# temporary; tested by TestSuite. 

_an_element_ = EnumeratedSets.ParentMethods._an_element_ 

 

@cached_method 

def inverse_family(self): 

""" 

Returns the inverse family, with keys and values 

exchanged. This presumes that there are no duplicate values in 

``self``. 

 

This default implementation is not lazy and therefore will 

only work with not too big finite families. It is also cached 

for the same reason:: 

 

sage: Family({3: 'a', 4: 'b', 7: 'd'}).inverse_family() 

Finite family {'a': 3, 'b': 4, 'd': 7} 

 

sage: Family((3,4,7)).inverse_family() 

Finite family {3: 0, 4: 1, 7: 2} 

 

""" 

return Family( dict( (self[k], k) for k in self.keys()) ) 

 

class FiniteFamily(AbstractFamily): 

r""" 

A :class:`FiniteFamily` is an associative container which models a finite 

family `(f_i)_{i \in I}`. Its elements `f_i` are therefore its 

values. Instances should be created via the :func:`Family` factory. See its 

documentation for examples and tests. 

 

EXAMPLES: 

 

We define the family `(f_i)_{i \in \{3,4,7\}}` with `f_3=a`, 

`f_4=b`, and `f_7=d`:: 

 

sage: from sage.sets.family import FiniteFamily 

sage: f = FiniteFamily({3: 'a', 4: 'b', 7: 'd'}) 

 

Individual elements are accessible as in a usual dictionary:: 

 

sage: f[7] 

'd' 

 

And the other usual dictionary operations are also available:: 

 

sage: len(f) 

3 

sage: f.keys() 

[3, 4, 7] 

 

However f behaves as a container for the `f_i`'s:: 

 

sage: list(f) 

['a', 'b', 'd'] 

sage: [ x for x in f ] 

['a', 'b', 'd'] 

 

The order of the elements can be specified using the ``keys`` optional argument:: 

 

sage: f = FiniteFamily({"a": "aa", "b": "bb", "c" : "cc" }, keys = ["c", "a", "b"]) 

sage: list(f) 

['cc', 'aa', 'bb'] 

 

""" 

 

def __init__(self, dictionary, keys=None): 

""" 

TESTS:: 

 

sage: from sage.sets.family import FiniteFamily 

sage: f = FiniteFamily({3: 'a', 4: 'b', 7: 'd'}) 

sage: TestSuite(f).run() 

 

Check for bug :trac:`5538`:: 

 

sage: d = {1:"a", 3:"b", 4:"c"} 

sage: f = Family(d) 

sage: d[2] = 'DD' 

sage: f 

Finite family {1: 'a', 3: 'b', 4: 'c'} 

""" 

# TODO: use keys to specify the order of the elements 

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

self._dictionary = dict(dictionary) 

self._keys = keys 

 

@cached_method 

def __hash__(self): 

""" 

Return a hash value for ``self``. 

 

EXAMPLES:: 

 

sage: f = Family(["c", "a", "b"], lambda x: x+x) 

sage: hash(f) == hash(f) 

True 

sage: f2 = Family(["a", "c", "b"], lambda x: x+x) 

sage: hash(f) == hash(f2) 

True 

sage: g = Family(["b", "c", "a"], lambda x: x+x+x) 

sage: hash(f) == hash(g) 

False 

 

:: 

 

sage: f = Family({1:[1,2]}) 

sage: hash(f) == hash(f) 

True 

""" 

try: 

return hash(frozenset(self._dictionary.items())) 

except (TypeError, ValueError): 

return hash(frozenset(self.keys() + 

[repr(v) for v in self.values()])) 

 

def keys(self): 

""" 

Returns the index set of this family 

 

EXAMPLES:: 

 

sage: f = Family(["c", "a", "b"], lambda x: x+x) 

sage: f.keys() 

['c', 'a', 'b'] 

""" 

return (self._keys if self._keys is not None 

else list(self._dictionary)) 

 

def values(self): 

""" 

Returns the elements of this family 

 

EXAMPLES:: 

 

sage: f = Family(["c", "a", "b"], lambda x: x+x) 

sage: f.values() 

['cc', 'aa', 'bb'] 

""" 

if self._keys is not None: 

return [self._dictionary[key] for key in self._keys] 

else: 

return list(itervalues(self._dictionary)) 

 

def has_key(self, k): 

""" 

Returns whether ``k`` is a key of ``self`` 

 

EXAMPLES:: 

 

sage: Family({"a":1, "b":2, "c":3}).has_key("a") 

True 

sage: Family({"a":1, "b":2, "c":3}).has_key("d") 

False 

""" 

return k in self._dictionary 

 

def __eq__(self, other): 

""" 

EXAMPLES:: 

 

sage: f = Family({1:'a', 2:'b', 3:'c'}) 

sage: g = Family({1:'a', 2:'b', 3:'c'}) 

sage: f == g 

True 

 

TESTS:: 

 

sage: from sage.sets.family import FiniteFamily 

 

sage: f1 = FiniteFamily({1:'a', 2:'b', 3:'c'}, keys = [1,2,3]) 

sage: g1 = FiniteFamily({1:'a', 2:'b', 3:'c'}, keys = [1,2,3]) 

sage: h1 = FiniteFamily({1:'a', 2:'b', 3:'c'}, keys = [2,1,3]) 

 

sage: f1 == g1 

True 

sage: f1 == h1 

False 

sage: f1 == f 

False 

""" 

return (isinstance(other, self.__class__) and 

self._keys == other._keys and 

self._dictionary == other._dictionary) 

 

def _repr_(self): 

""" 

EXAMPLES:: 

 

sage: from sage.sets.family import FiniteFamily 

sage: FiniteFamily({3: 'a'}) # indirect doctest 

Finite family {3: 'a'} 

""" 

return "Finite family %s"%self._dictionary 

 

def __contains__(self, x): 

""" 

EXAMPLES:: 

 

sage: from sage.sets.family import FiniteFamily 

sage: f = FiniteFamily({3: 'a'}) 

sage: 'a' in f 

True 

sage: 'b' in f 

False 

""" 

return x in self.values() 

 

def __len__(self): 

""" 

Returns the number of elements in self. 

 

EXAMPLES:: 

 

sage: from sage.sets.family import FiniteFamily 

sage: f = FiniteFamily({3: 'a', 4: 'b', 7: 'd'}) 

sage: len(f) 

3 

""" 

return len(self._dictionary) 

 

def cardinality(self): 

""" 

Returns the number of elements in self. 

 

EXAMPLES:: 

 

sage: from sage.sets.family import FiniteFamily 

sage: f = FiniteFamily({3: 'a', 4: 'b', 7: 'd'}) 

sage: f.cardinality() 

3 

""" 

return Integer(len(self._dictionary)) 

 

def __iter__(self): 

""" 

EXAMPLES:: 

 

sage: from sage.sets.family import FiniteFamily 

sage: f = FiniteFamily({3: 'a'}) 

sage: i = iter(f) 

sage: next(i) 

'a' 

""" 

return iter(self.values()) 

 

def __getitem__(self, i): 

""" 

Note that we can't just do self.__getitem__ = 

dictionary.__getitem__ in the __init__ method since Python 

queries the object's type/class for the special methods rather than 

querying the object itself. 

 

EXAMPLES:: 

 

sage: from sage.sets.family import FiniteFamily 

sage: f = FiniteFamily({3: 'a', 4: 'b', 7: 'd'}) 

sage: f[3] 

'a' 

""" 

return self._dictionary[i] 

 

# For the pickle and copy modules 

def __getstate__(self): 

""" 

TESTS:: 

 

sage: from sage.sets.family import FiniteFamily 

sage: f = FiniteFamily({3: 'a'}) 

sage: f.__getstate__() 

{'dictionary': {3: 'a'}, 'keys': None} 

""" 

return {'dictionary': self._dictionary, 'keys': self._keys} 

 

def __setstate__(self, state): 

""" 

TESTS:: 

 

sage: from sage.sets.family import FiniteFamily 

sage: f = FiniteFamily({3: 'a'}) 

sage: f.__setstate__({'dictionary': {4:'b'}}) 

sage: f 

Finite family {4: 'b'} 

""" 

self.__init__(state['dictionary'], keys = state.get("keys")) 

 

class FiniteFamilyWithHiddenKeys(FiniteFamily): 

r""" 

A close variant of :class:`FiniteFamily` where the family contains some 

hidden keys whose corresponding values are computed lazily (and 

remembered). Instances should be created via the :func:`Family` factory. 

See its documentation for examples and tests. 

 

Caveat: Only instances of this class whose functions are compatible 

with :mod:`sage.misc.fpickle` can be pickled. 

""" 

def __init__(self, dictionary, hidden_keys, hidden_function, keys=None): 

""" 

EXAMPLES:: 

 

sage: f = Family([3,4,7], lambda i: 2*i, hidden_keys=[2]) 

sage: TestSuite(f).run() 

""" 

FiniteFamily.__init__(self, dictionary, keys=keys) 

self._hidden_keys = hidden_keys 

self.hidden_function = hidden_function 

self.hidden_dictionary = {} 

 

# would be better to define as usual method 

# any better to unset the def of __getitem__ by FiniteFamily? 

#self.__getitem__ = lambda i: dictionary[i] if dictionary.has_key(i) else hidden_dictionary[i] 

 

def __getitem__(self, i): 

""" 

EXAMPLES:: 

 

sage: f = Family([3,4,7], lambda i: 2*i, hidden_keys=[2]) 

sage: f[3] 

6 

sage: f[2] 

4 

sage: f[5] 

Traceback (most recent call last): 

... 

KeyError 

""" 

if i in self._dictionary: 

return self._dictionary[i] 

 

if i not in self.hidden_dictionary: 

if i not in self._hidden_keys: 

raise KeyError 

self.hidden_dictionary[i] = self.hidden_function(i) 

 

return self.hidden_dictionary[i] 

 

def hidden_keys(self): 

""" 

Returns self's hidden keys. 

 

EXAMPLES:: 

 

sage: f = Family([3,4,7], lambda i: 2*i, hidden_keys=[2]) 

sage: f.hidden_keys() 

[2] 

""" 

return self._hidden_keys 

 

def __getstate__(self): 

""" 

TESTS:: 

 

sage: f = Family([3,4,7], lambda i: 2*i, hidden_keys=[2]) 

sage: d = f.__getstate__() 

sage: d['hidden_keys'] 

[2] 

""" 

from sage.misc.fpickle import pickle_function 

f = pickle_function(self.hidden_function) 

return {'dictionary': self._dictionary, 

'hidden_keys': self._hidden_keys, 

'hidden_dictionary': self.hidden_dictionary, 

'hidden_function': f, 

'keys': self._keys} 

 

def __setstate__(self, d): 

""" 

TESTS:: 

 

sage: f = Family([3,4,7], lambda i: 2*i, hidden_keys=[2]) 

sage: d = f.__getstate__() 

sage: f = Family([4,5,6], lambda i: 2*i, hidden_keys=[2]) 

sage: f.__setstate__(d) 

sage: f.keys() 

[3, 4, 7] 

sage: f[3] 

6 

""" 

hidden_function = d['hidden_function'] 

if isinstance(hidden_function, str): 

# Let's assume that hidden_function is an unpickled function. 

from sage.misc.fpickle import unpickle_function 

hidden_function = unpickle_function(hidden_function) 

self.__init__(d['dictionary'], d['hidden_keys'], hidden_function) 

self.hidden_dictionary = d['hidden_dictionary'] 

# Old pickles from before trac #22955 may not have a 'keys' 

if 'keys' in d: 

self._keys = d['keys'] 

else: 

self._keys = None 

 

 

class LazyFamily(AbstractFamily): 

r""" 

A LazyFamily(I, f) is an associative container which models the 

(possibly infinite) family `(f(i))_{i \in I}`. 

 

Instances should be created via the :func:`Family` factory. See its 

documentation for examples and tests. 

""" 

def __init__(self, set, function, name=None): 

""" 

TESTS:: 

 

sage: from sage.sets.family import LazyFamily 

sage: f = LazyFamily([3,4,7], lambda i: 2*i); f 

Lazy family (<lambda>(i))_{i in [3, 4, 7]} 

sage: TestSuite(f).run() # __contains__ is not implemented 

Failure ... 

The following tests failed: _test_an_element, _test_enumerated_set_contains, _test_some_elements 

 

Check for :trac:`5538`:: 

 

sage: l = [3,4,7] 

sage: f = LazyFamily(l, lambda i: 2*i); 

sage: l[1] = 18 

sage: f 

Lazy family (<lambda>(i))_{i in [3, 4, 7]} 

""" 

if set in FiniteEnumeratedSets(): 

category = FiniteEnumeratedSets() 

elif set in InfiniteEnumeratedSets(): 

category = InfiniteEnumeratedSets() 

elif isinstance(set, (list, tuple, CombinatorialClass)): 

category = FiniteEnumeratedSets() 

else: 

category = EnumeratedSets() 

 

Parent.__init__(self, category=category) 

 

self.set = copy(set) 

self.function = function 

self.function_name = name 

 

@cached_method 

def __hash__(self): 

""" 

Return a hash value for ``self``. 

 

EXAMPLES:: 

 

sage: from sage.sets.family import LazyFamily 

sage: f = LazyFamily([3,4,7], lambda i: 2*i) 

sage: hash(f) == hash(f) 

True 

sage: g = LazyFamily(ZZ, lambda i: 2*i) 

sage: hash(g) == hash(g) 

True 

sage: h = LazyFamily(ZZ, lambda i: 2*i, name='foo') 

sage: hash(h) == hash(h) 

True 

 

:: 

 

sage: class X(object): 

....: def __call__(self, x): 

....: return x 

....: __hash__ = None 

sage: f = Family([1,2,3], X()) 

sage: hash(f) == hash(f) 

True 

""" 

try: 

return hash(self.keys()) + hash(self.function) 

except (TypeError, ValueError): 

return super(LazyFamily, self).__hash__() 

 

def __eq__(self, other): 

""" 

WARNING: Since there is no way to compare function, we only compare 

their name. 

 

TESTS:: 

 

sage: from sage.sets.family import LazyFamily 

sage: fun = lambda i: 2*i 

sage: f = LazyFamily([3,4,7], fun) 

sage: g = LazyFamily([3,4,7], fun) 

sage: f == g 

True 

""" 

from sage.misc.fpickle import pickle_function 

if not isinstance(other, self.__class__): 

return False 

if not self.set == other.set: 

return False 

return repr(self) == repr(other) 

 

def _repr_(self): 

""" 

EXAMPLES:: 

 

sage: from sage.sets.family import LazyFamily 

sage: def fun(i): 2*i 

sage: f = LazyFamily([3,4,7], fun); f 

Lazy family (fun(i))_{i in [3, 4, 7]} 

 

sage: f = Family(Permutations(3), attrcall("to_lehmer_code"), lazy=True); f 

Lazy family (i.to_lehmer_code())_{i in Standard permutations of 3} 

 

sage: f = LazyFamily([3,4,7], lambda i: 2*i); f 

Lazy family (<lambda>(i))_{i in [3, 4, 7]} 

 

sage: f = LazyFamily([3,4,7], lambda i: 2*i, name='foo'); f 

Lazy family (foo(i))_{i in [3, 4, 7]} 

 

TESTS: 

 

Check that a using a class as the function is correctly handled:: 

 

sage: Family(NonNegativeIntegers(), PerfectMatchings) 

Lazy family (<class 'sage.combinat.perfect_matching.PerfectMatchings'>(i))_{i in Non negative integers} 

""" 

if self.function_name is not None: 

name = self.function_name + "(i)" 

elif isinstance(self.function, type(lambda x:1)): 

name = self.function.__name__ 

name = name+"(i)" 

else: 

name = repr(self.function) 

if isinstance(self.function, AttrCallObject): 

name = "i"+name[1:] 

else: 

name = name+"(i)" 

return "Lazy family ({})_{{i in {}}}".format(name, self.set) 

 

def keys(self): 

""" 

Returns self's keys. 

 

EXAMPLES:: 

 

sage: from sage.sets.family import LazyFamily 

sage: f = LazyFamily([3,4,7], lambda i: 2*i) 

sage: f.keys() 

[3, 4, 7] 

""" 

return self.set 

 

def cardinality(self): 

""" 

Return the number of elements in self. 

 

EXAMPLES:: 

 

sage: from sage.sets.family import LazyFamily 

sage: f = LazyFamily([3,4,7], lambda i: 2*i) 

sage: f.cardinality() 

3 

sage: from sage.categories.examples.infinite_enumerated_sets import NonNegativeIntegers 

sage: l = LazyFamily(NonNegativeIntegers(), lambda i: 2*i) 

sage: l.cardinality() 

+Infinity 

 

TESTS: 

 

Check that :trac:`15195` is fixed:: 

 

sage: C = cartesian_product([PositiveIntegers(), [1,2,3]]) 

sage: C.cardinality() 

+Infinity 

sage: F = Family(C, lambda x: x) 

sage: F.cardinality() 

+Infinity 

""" 

try: 

return Integer(len(self.set)) 

except (AttributeError, NotImplementedError, TypeError): 

return self.set.cardinality() 

 

def __iter__(self): 

""" 

EXAMPLES:: 

 

sage: from sage.sets.family import LazyFamily 

sage: f = LazyFamily([3,4,7], lambda i: 2*i) 

sage: [i for i in f] 

[6, 8, 14] 

""" 

for i in self.set: 

yield self[i] 

 

def __getitem__(self, i): 

""" 

EXAMPLES:: 

 

sage: from sage.sets.family import LazyFamily 

sage: f = LazyFamily([3,4,7], lambda i: 2*i) 

sage: f[3] 

6 

 

TESTS:: 

 

sage: f[5] 

10 

""" 

return self.function(i) 

 

def __getstate__(self): 

""" 

EXAMPLES:: 

 

sage: from sage.sets.family import LazyFamily 

sage: f = LazyFamily([3,4,7], lambda i: 2*i) 

sage: d = f.__getstate__() 

sage: d['set'] 

[3, 4, 7] 

 

sage: f = LazyFamily(Permutations(3), lambda p: p.to_lehmer_code()) 

sage: f == loads(dumps(f)) 

True 

 

sage: f = LazyFamily(Permutations(3), attrcall("to_lehmer_code")) 

sage: f == loads(dumps(f)) 

True 

""" 

f = self.function 

# This should be done once for all by registering 

# sage.misc.fpickle.pickle_function to copyreg 

if isinstance(f, types.FunctionType): 

from sage.misc.fpickle import pickle_function 

f = pickle_function(f) 

 

return {'set': self.set, 

'function': f} 

 

def __setstate__(self, d): 

""" 

EXAMPLES:: 

 

sage: from sage.sets.family import LazyFamily 

sage: f = LazyFamily([3,4,7], lambda i: 2*i) 

sage: d = f.__getstate__() 

sage: f = LazyFamily([4,5,6], lambda i: 2*i) 

sage: f.__setstate__(d) 

sage: f.keys() 

[3, 4, 7] 

sage: f[3] 

6 

""" 

function = d['function'] 

if isinstance(function, bytes): 

# Let's assume that function is an unpickled function. 

from sage.misc.fpickle import unpickle_function 

function = unpickle_function(function) 

 

self.__init__(d['set'], function) 

 

 

class TrivialFamily(AbstractFamily): 

r""" 

:class:`TrivialFamily` turns a list/tuple `c` into a family indexed by the 

set `\{0, \dots, |c|-1\}`. 

 

Instances should be created via the :func:`Family` factory. See its 

documentation for examples and tests. 

""" 

def __init__(self, enumeration): 

""" 

EXAMPLES:: 

 

sage: from sage.sets.family import TrivialFamily 

sage: f = TrivialFamily((3,4,7)); f 

Family (3, 4, 7) 

sage: f = TrivialFamily([3,4,7]); f 

Family (3, 4, 7) 

sage: TestSuite(f).run() 

""" 

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

self._enumeration = tuple(enumeration) 

 

def __eq__(self, other): 

""" 

TESTS:: 

 

sage: f = Family((3,4,7)) 

sage: g = Family([3,4,7]) 

sage: f == g 

True 

""" 

return (isinstance(other, self.__class__) and 

self._enumeration == other._enumeration) 

 

def __hash__(self): 

""" 

Return a hash value for ``self``. 

 

EXAMPLES:: 

 

sage: from sage.sets.family import TrivialFamily 

sage: f = TrivialFamily((3,4,7)) 

sage: hash(f) == hash(f) 

True 

""" 

return hash(self._enumeration) 

 

def _repr_(self): 

""" 

EXAMPLES:: 

 

sage: from sage.sets.family import TrivialFamily 

sage: f = TrivialFamily([3,4,7]); f # indirect doctest 

Family (3, 4, 7) 

""" 

return "Family %s"%((self._enumeration),) 

 

def keys(self): 

""" 

Returns self's keys. 

 

EXAMPLES:: 

 

sage: from sage.sets.family import TrivialFamily 

sage: f = TrivialFamily([3,4,7]) 

sage: f.keys() 

[0, 1, 2] 

""" 

return list(range(len(self._enumeration))) 

 

def cardinality(self): 

""" 

Return the number of elements in self. 

 

EXAMPLES:: 

 

sage: from sage.sets.family import TrivialFamily 

sage: f = TrivialFamily([3,4,7]) 

sage: f.cardinality() 

3 

""" 

return Integer(len(self._enumeration)) 

 

def __iter__(self): 

""" 

EXAMPLES:: 

 

sage: from sage.sets.family import TrivialFamily 

sage: f = TrivialFamily([3,4,7]) 

sage: [i for i in f] 

[3, 4, 7] 

""" 

return iter(self._enumeration) 

 

def __contains__(self, x): 

""" 

EXAMPLES:: 

 

sage: from sage.sets.family import TrivialFamily 

sage: f = TrivialFamily([3,4,7]) 

sage: 3 in f 

True 

sage: 5 in f 

False 

""" 

return x in self._enumeration 

 

def __getitem__(self, i): 

""" 

EXAMPLES:: 

 

sage: from sage.sets.family import TrivialFamily 

sage: f = TrivialFamily([3,4,7]) 

sage: f[1] 

4 

""" 

return self._enumeration[i] 

 

def __getstate__(self): 

""" 

TESTS:: 

 

sage: from sage.sets.family import TrivialFamily 

sage: f = TrivialFamily([3,4,7]) 

sage: f.__getstate__() 

{'_enumeration': (3, 4, 7)} 

""" 

return {'_enumeration': self._enumeration} 

 

def __setstate__(self, state): 

""" 

TESTS:: 

 

sage: from sage.sets.family import TrivialFamily 

sage: f = TrivialFamily([3,4,7]) 

sage: f.__setstate__({'_enumeration': (2, 4, 8)}) 

sage: f 

Family (2, 4, 8) 

""" 

self.__init__(state['_enumeration']) 

 

 

 

from sage.categories.examples.infinite_enumerated_sets import NonNegativeIntegers 

from sage.rings.infinity import Infinity 

 

class EnumeratedFamily(LazyFamily): 

r""" 

:class:`EnumeratedFamily` turns an enumerated set ``c`` into a family 

indexed by the set `\{0,\dots, |c|-1\}`. 

 

Instances should be created via the :func:`Family` factory. See its 

documentation for examples and tests. 

""" 

def __init__(self, enumset): 

""" 

EXAMPLES:: 

 

sage: from sage.sets.family import EnumeratedFamily 

sage: f = EnumeratedFamily(Permutations(3)) 

sage: TestSuite(f).run() 

 

sage: from sage.categories.examples.infinite_enumerated_sets import NonNegativeIntegers 

sage: f = Family(NonNegativeIntegers()) 

sage: TestSuite(f).run() 

""" 

if enumset.cardinality() == Infinity: 

baseset = NonNegativeIntegers() 

else: 

baseset = range(enumset.cardinality()) 

LazyFamily.__init__(self, baseset, enumset.unrank) 

self.enumset = enumset 

 

def __eq__(self, other): 

""" 

EXAMPLES:: 

 

sage: f = Family(Permutations(3)) 

sage: g = Family(Permutations(3)) 

sage: f == g 

True 

""" 

return (isinstance(other, self.__class__) and 

self.enumset == other.enumset) 

 

def __repr__(self): 

""" 

EXAMPLES:: 

 

sage: f = Family(Permutations(3)); f # indirect doctest 

Family (Standard permutations of 3) 

 

sage: from sage.categories.examples.infinite_enumerated_sets import NonNegativeIntegers 

sage: f = Family(NonNegativeIntegers()); f 

Family (An example of an infinite enumerated set: the non negative integers) 

""" 

# return "Family ((%s)[i])_(i=1...%s)"%(self.enumset, self.enumset.cardinality()) 

if isinstance(self.enumset, FiniteEnumeratedSet): 

return "Family %s"%(self.enumset._elements,) 

return "Family (%s)"%(self.enumset) 

 

def __contains__(self, x): 

""" 

EXAMPLES:: 

 

sage: f = Family(Permutations(3)) 

sage: f.keys() 

Standard permutations of 3 

sage: [2,1,3] in f 

True 

""" 

return x in self.enumset 

 

 

def keys(self): 

""" 

Returns self's keys. 

 

EXAMPLES:: 

 

sage: from sage.sets.family import EnumeratedFamily 

sage: f = EnumeratedFamily(Permutations(3)) 

sage: f.keys() 

Standard permutations of 3 

 

sage: from sage.categories.examples.infinite_enumerated_sets import NonNegativeIntegers 

sage: f = Family(NonNegativeIntegers()) 

sage: f.keys() 

An example of an infinite enumerated set: the non negative integers 

""" 

return self.enumset 

 

def cardinality(self): 

""" 

Return the number of elements in self. 

 

EXAMPLES:: 

 

sage: from sage.sets.family import EnumeratedFamily 

sage: f = EnumeratedFamily(Permutations(3)) 

sage: f.cardinality() 

6 

 

sage: from sage.categories.examples.infinite_enumerated_sets import NonNegativeIntegers 

sage: f = Family(NonNegativeIntegers()) 

sage: f.cardinality() 

+Infinity 

""" 

return self.enumset.cardinality() 

 

def __iter__(self): 

""" 

EXAMPLES:: 

 

sage: from sage.sets.family import EnumeratedFamily 

sage: f = EnumeratedFamily(Permutations(3)) 

sage: [i for i in f] 

[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]] 

""" 

for i in self.enumset: 

yield i 

 

def __getitem__(self, i): 

""" 

EXAMPLES:: 

 

sage: from sage.sets.family import EnumeratedFamily 

sage: f = EnumeratedFamily(Permutations(3)); 

sage: f[1] 

[1, 3, 2] 

""" 

return self.enumset.unrank(i) 

 

def __getstate__(self): 

""" 

EXAMPLES:: 

 

sage: from sage.sets.family import EnumeratedFamily 

sage: f = EnumeratedFamily(Permutations(3)); 

sage: f.__getstate__() 

{'enumset': Standard permutations of 3} 

sage: loads(dumps(f)) == f 

True 

""" 

return {'enumset': self.enumset} 

 

def __setstate__(self, state): 

""" 

EXAMPLES:: 

 

sage: from sage.sets.family import EnumeratedFamily 

sage: f = EnumeratedFamily(Permutations(0)); 

sage: f.__setstate__({'enumset': Permutations(3)}) 

sage: f 

Family (Standard permutations of 3) 

""" 

self.__init__(state['enumset'])