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

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

""" 

Sets 

 

AUTHORS: 

 

- William Stein (2005) - first version 

 

- William Stein (2006-02-16) - large number of documentation and 

examples; improved code 

 

- Mike Hansen (2007-3-25) - added differences and symmetric 

differences; fixed operators 

 

- Florent Hivert (2010-06-17) - Adapted to categories 

 

- Nicolas M. Thiery (2011-03-15) - Added subset and superset methods 

 

- Julian Rueth (2013-04-09) - Collected common code in 

:class:`Set_object_binary`, fixed ``__hash__``. 

 

""" 

 

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

# Copyright (C) 2005 William Stein <wstein@gmail.com> 

# 2013 Julian Rueth <julian.rueth@fsfe.org> 

# 

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

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

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

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

from __future__ import print_function 

import six 

from six import integer_types 

 

from sage.misc.latex import latex 

from sage.misc.prandom import choice 

from sage.misc.misc import is_iterator 

 

from sage.structure.category_object import CategoryObject 

from sage.structure.element import Element 

from sage.structure.parent import Parent, Set_generic 

from sage.structure.richcmp import richcmp_method, richcmp, rich_to_bool 

 

from sage.categories.sets_cat import Sets 

from sage.categories.enumerated_sets import EnumeratedSets 

 

import sage.rings.infinity 

 

 

def has_finite_length(obj): 

""" 

Return ``True`` if ``obj`` is known to have finite length. 

 

This is mainly meant for pure Python types, so we do not call any 

Sage-specific methods. 

 

EXAMPLES:: 

 

sage: from sage.sets.set import has_finite_length 

sage: has_finite_length(tuple(range(10))) 

True 

sage: has_finite_length(list(range(10))) 

True 

sage: has_finite_length(set(range(10))) 

True 

sage: has_finite_length(iter(range(10))) 

False 

sage: has_finite_length(GF(17^127)) 

True 

sage: has_finite_length(ZZ) 

False 

""" 

try: 

len(obj) 

except OverflowError: 

return True 

except Exception: 

return False 

else: 

return True 

 

 

def Set(X=[]): 

r""" 

Create the underlying set of ``X``. 

 

If ``X`` is a list, tuple, Python set, or ``X.is_finite()`` is 

``True``, this returns a wrapper around Python's enumerated immutable 

``frozenset`` type with extra functionality. Otherwise it returns a 

more formal wrapper. 

 

If you need the functionality of mutable sets, use Python's 

builtin set type. 

 

EXAMPLES:: 

 

sage: X = Set(GF(9,'a')) 

sage: X 

{0, 1, 2, a, a + 1, a + 2, 2*a, 2*a + 1, 2*a + 2} 

sage: type(X) 

<class 'sage.sets.set.Set_object_enumerated_with_category'> 

sage: Y = X.union(Set(QQ)) 

sage: Y 

Set-theoretic union of {0, 1, 2, a, a + 1, a + 2, 2*a, 2*a + 1, 2*a + 2} and Set of elements of Rational Field 

sage: type(Y) 

<class 'sage.sets.set.Set_object_union_with_category'> 

 

Usually sets can be used as dictionary keys. 

 

:: 

 

sage: d={Set([2*I,1+I]):10} 

sage: d # key is randomly ordered 

{{I + 1, 2*I}: 10} 

sage: d[Set([1+I,2*I])] 

10 

sage: d[Set((1+I,2*I))] 

10 

 

The original object is often forgotten. 

 

:: 

 

sage: v = [1,2,3] 

sage: X = Set(v) 

sage: X 

{1, 2, 3} 

sage: v.append(5) 

sage: X 

{1, 2, 3} 

sage: 5 in X 

False 

 

Set also accepts iterators, but be careful to only give *finite* 

sets:: 

 

sage: from six.moves import range 

sage: sorted(Set(range(1,6))) 

[1, 2, 3, 4, 5] 

sage: sorted(Set(list(range(1,6)))) 

[1, 2, 3, 4, 5] 

sage: sorted(Set(iter(range(1,6)))) 

[1, 2, 3, 4, 5] 

 

We can also create sets from different types:: 

 

sage: sorted(Set([Sequence([3,1], immutable=True), 5, QQ, Partition([3,1,1])]), key=str) 

[5, Rational Field, [3, 1, 1], [3, 1]] 

 

Sets with unhashable objects work, but with less functionality:: 

 

sage: A = Set([QQ, (3, 1), 5]) # hashable 

sage: sorted(A.list(), key=repr) 

[(3, 1), 5, Rational Field] 

sage: type(A) 

<class 'sage.sets.set.Set_object_enumerated_with_category'> 

sage: B = Set([QQ, [3, 1], 5]) # unhashable 

sage: sorted(B.list(), key=repr) 

Traceback (most recent call last): 

... 

AttributeError: 'Set_object_with_category' object has no attribute 'list' 

sage: type(B) 

<class 'sage.sets.set.Set_object_with_category'> 

 

TESTS:: 

 

sage: Set(Primes()) 

Set of all prime numbers: 2, 3, 5, 7, ... 

sage: Set(Subsets([1,2,3])).cardinality() 

8 

sage: S = Set(iter([1,2,3])); S 

{1, 2, 3} 

sage: type(S) 

<class 'sage.sets.set.Set_object_enumerated_with_category'> 

sage: S = Set([]) 

sage: TestSuite(S).run() 

 

Check that :trac:`16090` is fixed:: 

 

sage: Set() 

{} 

""" 

if isinstance(X, CategoryObject): 

if isinstance(X, Set_generic): 

return X 

elif X in Sets().Finite(): 

return Set_object_enumerated(X) 

else: 

return Set_object(X) 

 

if isinstance(X, Element): 

raise TypeError("Element has no defined underlying set") 

 

try: 

X = frozenset(X) 

except TypeError: 

return Set_object(X) 

else: 

return Set_object_enumerated(X) 

 

 

@richcmp_method 

class Set_object(Set_generic): 

r""" 

A set attached to an almost arbitrary object. 

 

EXAMPLES:: 

 

sage: K = GF(19) 

sage: Set(K) 

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18} 

sage: S = Set(K) 

 

sage: latex(S) 

\left\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18\right\} 

sage: TestSuite(S).run() 

 

sage: latex(Set(ZZ)) 

\Bold{Z} 

 

TESTS: 

 

See trac ticket :trac:`14486`:: 

 

sage: 0 == Set([1]), Set([1]) == 0 

(False, False) 

sage: 1 == Set([0]), Set([0]) == 1 

(False, False) 

""" 

def __init__(self, X, category=None): 

""" 

Create a Set_object 

 

This function is called by the Set function; users 

shouldn't call this directly. 

 

EXAMPLES:: 

 

sage: type(Set(QQ)) 

<class 'sage.sets.set.Set_object_with_category'> 

sage: Set(QQ).category() 

Category of sets 

 

TESTS:: 

 

sage: _a, _b = get_coercion_model().canonical_coercion(Set([0]), 0) 

Traceback (most recent call last): 

... 

TypeError: no common canonical parent for objects with parents: 

'<class 'sage.sets.set.Set_object_enumerated_with_category'>' 

and 'Integer Ring' 

""" 

from sage.rings.integer import is_Integer 

if isinstance(X, integer_types) or is_Integer(X): 

# The coercion model will try to call Set_object(0) 

raise ValueError('underlying object cannot be an integer') 

 

if category is None: 

category = Sets() 

Parent.__init__(self, category=category) 

self.__object = X 

 

def __hash__(self): 

""" 

Return the hash value of ``self``. 

 

EXAMPLES:: 

 

sage: hash(Set(QQ)) == hash(QQ) 

True 

""" 

return hash(self.__object) 

 

def _latex_(self): 

r""" 

Return latex representation of this set. 

 

This is often the same as the latex representation of this 

object when the object is infinite. 

 

EXAMPLES:: 

 

sage: latex(Set(QQ)) 

\Bold{Q} 

 

When the object is finite or a special set then the latex 

representation can be more interesting. 

 

:: 

 

sage: print(latex(Primes())) 

\text{\texttt{Set{ }of{ }all{ }prime{ }numbers:{ }2,{ }3,{ }5,{ }7,{ }...}} 

sage: print(latex(Set([1,1,1,5,6]))) 

\left\{1, 5, 6\right\} 

""" 

return latex(self.__object) 

 

def _repr_(self): 

""" 

Print representation of this set. 

 

EXAMPLES:: 

 

sage: X = Set(ZZ) 

sage: X 

Set of elements of Integer Ring 

sage: X.rename('{ integers }') 

sage: X 

{ integers } 

""" 

return "Set of elements of " + repr(self.__object) 

 

def __iter__(self): 

""" 

Iterate over the elements of this set. 

 

EXAMPLES:: 

 

sage: X = Set(ZZ) 

sage: I = X.__iter__() 

sage: next(I) 

0 

sage: next(I) 

1 

sage: next(I) 

-1 

sage: next(I) 

2 

""" 

return iter(self.__object) 

 

an_element = EnumeratedSets.ParentMethods.__dict__['_an_element_from_iterator'] 

 

def __contains__(self, x): 

""" 

Return ``True`` if `x` is in ``self``. 

 

EXAMPLES:: 

 

sage: X = Set(ZZ) 

sage: 5 in X 

True 

sage: GF(7)(3) in X 

True 

sage: 2/1 in X 

True 

sage: 2/1 in ZZ 

True 

sage: 2/3 in X 

False 

 

Finite fields better illustrate the difference between 

``__contains__`` for objects and their underlying sets. 

 

sage: X = Set(GF(7)) 

sage: X 

{0, 1, 2, 3, 4, 5, 6} 

sage: 5/3 in X 

False 

sage: 5/3 in GF(7) 

False 

sage: Set(GF(7)).union(Set(GF(5))) 

{0, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 0} 

sage: Set(GF(7)).intersection(Set(GF(5))) 

{} 

""" 

return x in self.__object 

 

def __richcmp__(self, right, op): 

r""" 

Compare ``self`` and ``right``. 

 

If ``right`` is not a :class:`Set_object`, return ``NotImplemented``. 

If ``right`` is also a :class:`Set_object`, returns comparison 

on the underlying objects. 

 

.. NOTE:: 

 

If `X < Y` is true this does *not* necessarily mean 

that `X` is a subset of `Y`. Also, any two sets can be 

compared still, but the result need not be meaningful 

if they are not equal. 

 

EXAMPLES:: 

 

sage: Set(ZZ) == Set(QQ) 

False 

sage: Set(ZZ) < Set(QQ) 

True 

sage: Primes() == Set(QQ) 

False 

 

The following is random, illustrating that comparison of 

sets is not the subset relation, when they are not equal:: 

 

sage: Primes() < Set(QQ) # random 

True or False 

""" 

if not isinstance(right, Set_object): 

return NotImplemented 

return richcmp(self.__object, right.__object, op) 

 

def union(self, X): 

""" 

Return the union of ``self`` and ``X``. 

 

EXAMPLES:: 

 

sage: Set(QQ).union(Set(ZZ)) 

Set-theoretic union of Set of elements of Rational Field and Set of elements of Integer Ring 

sage: Set(QQ) + Set(ZZ) 

Set-theoretic union of Set of elements of Rational Field and Set of elements of Integer Ring 

sage: X = Set(QQ).union(Set(GF(3))); X 

Set-theoretic union of Set of elements of Rational Field and {0, 1, 2} 

sage: 2/3 in X 

True 

sage: GF(3)(2) in X 

True 

sage: GF(5)(2) in X 

False 

sage: Set(GF(7)) + Set(GF(3)) 

{0, 1, 2, 3, 4, 5, 6, 1, 2, 0} 

""" 

if isinstance(X, Set_generic): 

if self is X: 

return self 

return Set_object_union(self, X) 

raise TypeError("X (=%s) must be a Set"%X) 

 

def __add__(self, X): 

""" 

Return the union of ``self`` and ``X``. 

 

EXAMPLES:: 

 

sage: Set(RealField()) + Set(QQ^5) 

Set-theoretic union of Set of elements of Real Field with 53 bits of precision and Set of elements of Vector space of dimension 5 over Rational Field 

sage: Set(GF(3)) + Set(GF(2)) 

{0, 1, 2, 0, 1} 

sage: Set(GF(2)) + Set(GF(4,'a')) 

{0, 1, a, a + 1} 

sage: Set(GF(8,'b')) + Set(GF(4,'a')) 

{0, 1, b, b + 1, b^2, b^2 + 1, b^2 + b, b^2 + b + 1, a, a + 1, 1, 0} 

""" 

return self.union(X) 

 

def __or__(self, X): 

""" 

Return the union of ``self`` and ``X``. 

 

EXAMPLES:: 

 

sage: Set([2,3]) | Set([3,4]) 

{2, 3, 4} 

sage: Set(ZZ) | Set(QQ) 

Set-theoretic union of Set of elements of Integer Ring and Set of elements of Rational Field 

""" 

 

return self.union(X) 

 

def intersection(self, X): 

r""" 

Return the intersection of ``self`` and ``X``. 

 

EXAMPLES:: 

 

sage: X = Set(ZZ).intersection(Primes()) 

sage: 4 in X 

False 

sage: 3 in X 

True 

 

sage: 2/1 in X 

True 

 

sage: X = Set(GF(9,'b')).intersection(Set(GF(27,'c'))) 

sage: X 

{} 

 

sage: X = Set(GF(9,'b')).intersection(Set(GF(27,'b'))) 

sage: X 

{} 

""" 

if isinstance(X, Set_generic): 

if self is X: 

return self 

return Set_object_intersection(self, X) 

raise TypeError("X (=%s) must be a Set"%X) 

 

 

def difference(self, X): 

r""" 

Return the set difference ``self - X``. 

 

EXAMPLES:: 

 

sage: X = Set(ZZ).difference(Primes()) 

sage: 4 in X 

True 

sage: 3 in X 

False 

 

sage: 4/1 in X 

True 

 

sage: X = Set(GF(9,'b')).difference(Set(GF(27,'c'))) 

sage: X 

{0, 1, 2, b, b + 1, b + 2, 2*b, 2*b + 1, 2*b + 2} 

 

sage: X = Set(GF(9,'b')).difference(Set(GF(27,'b'))) 

sage: X 

{0, 1, 2, b, b + 1, b + 2, 2*b, 2*b + 1, 2*b + 2} 

""" 

if isinstance(X, Set_generic): 

if self is X: 

return Set([]) 

return Set_object_difference(self, X) 

raise TypeError("X (=%s) must be a Set"%X) 

 

def symmetric_difference(self, X): 

r""" 

Returns the symmetric difference of ``self`` and ``X``. 

 

EXAMPLES:: 

 

sage: X = Set([1,2,3]).symmetric_difference(Set([3,4])) 

sage: X 

{1, 2, 4} 

""" 

 

if isinstance(X, Set_generic): 

if self is X: 

return Set([]) 

return Set_object_symmetric_difference(self, X) 

raise TypeError("X (=%s) must be a Set"%X) 

 

 

def __sub__(self, X): 

""" 

Return the difference of ``self`` and ``X``. 

 

EXAMPLES:: 

 

sage: X = Set(ZZ).difference(Primes()) 

sage: Y = Set(ZZ) - Primes() 

sage: X == Y 

True 

""" 

return self.difference(X) 

 

def __and__(self, X): 

""" 

Returns the intersection of ``self`` and ``X``. 

 

EXAMPLES:: 

 

sage: Set([2,3]) & Set([3,4]) 

{3} 

sage: Set(ZZ) & Set(QQ) 

Set-theoretic intersection of Set of elements of Integer Ring and Set of elements of Rational Field 

""" 

 

return self.intersection(X) 

 

def __xor__(self, X): 

""" 

Returns the symmetric difference of ``self`` and ``X``. 

 

EXAMPLES:: 

 

sage: X = Set([1,2,3,4]) 

sage: Y = Set([1,2]) 

sage: X.symmetric_difference(Y) 

{3, 4} 

sage: X.__xor__(Y) 

{3, 4} 

""" 

return self.symmetric_difference(X) 

 

def cardinality(self): 

""" 

Return the cardinality of this set, which is either an integer or 

``Infinity``. 

 

EXAMPLES:: 

 

sage: Set(ZZ).cardinality() 

+Infinity 

sage: Primes().cardinality() 

+Infinity 

sage: Set(GF(5)).cardinality() 

5 

sage: Set(GF(5^2,'a')).cardinality() 

25 

""" 

if not self.is_finite(): 

return sage.rings.infinity.infinity 

 

if self is not self.__object: 

try: 

return self.__object.cardinality() 

except (AttributeError, NotImplementedError): 

pass 

from sage.rings.integer import Integer 

try: 

return Integer(len(self.__object)) 

except TypeError: 

pass 

 

raise NotImplementedError("computation of cardinality of %s not yet implemented"%self.__object) 

 

def is_empty(self): 

""" 

Return boolean representing emptiness of the set. 

 

OUTPUT: 

 

True if the set is empty, false if otherwise. 

 

EXAMPLES:: 

 

sage: Set([]).is_empty() 

True 

sage: Set([0]).is_empty() 

False 

sage: Set([1..100]).is_empty() 

False 

sage: Set(SymmetricGroup(2).list()).is_empty() 

False 

sage: Set(ZZ).is_empty() 

False 

 

TESTS:: 

 

sage: Set([]).is_empty() 

True 

sage: Set([1,2,3]).is_empty() 

False 

sage: Set([1..100]).is_empty() 

False 

sage: Set(DihedralGroup(4).list()).is_empty() 

False 

sage: Set(QQ).is_empty() 

False 

""" 

return not self 

 

def is_finite(self): 

""" 

Return ``True`` if ``self`` is finite. 

 

EXAMPLES:: 

 

sage: Set(QQ).is_finite() 

False 

sage: Set(GF(250037)).is_finite() 

True 

sage: Set(Integers(2^1000000)).is_finite() 

True 

sage: Set([1,'a',ZZ]).is_finite() 

True 

""" 

obj = self.__object 

try: 

is_finite = obj.is_finite 

except AttributeError: 

return has_finite_length(obj) 

else: 

return is_finite() 

 

def object(self): 

""" 

Return underlying object. 

 

EXAMPLES:: 

 

sage: X = Set(QQ) 

sage: X.object() 

Rational Field 

sage: X = Primes() 

sage: X.object() 

Set of all prime numbers: 2, 3, 5, 7, ... 

""" 

return self.__object 

 

def subsets(self,size=None): 

""" 

Return the :class:`Subsets` object representing the subsets of a set. 

If size is specified, return the subsets of that size. 

 

EXAMPLES:: 

 

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

sage: list(X.subsets()) 

[{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}] 

sage: list(X.subsets(2)) 

[{1, 2}, {1, 3}, {2, 3}] 

 

""" 

from sage.combinat.subset import Subsets 

return Subsets(self,size) 

 

 

class Set_object_enumerated(Set_object): 

""" 

A finite enumerated set. 

""" 

def __init__(self, X): 

r""" 

Initialize ``self``. 

 

EXAMPLES:: 

 

sage: S = Set(GF(19)); S 

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18} 

sage: S.category() 

Category of finite sets 

sage: print(latex(S)) 

\left\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18\right\} 

sage: TestSuite(S).run() 

""" 

Set_object.__init__(self, X, category=Sets().Finite()) 

 

def random_element(self): 

r""" 

Return a random element in this set. 

 

EXAMPLES:: 

 

sage: Set([1,2,3]).random_element() # random 

2 

""" 

try: 

return self.object().random_element() 

except AttributeError: 

# TODO: this very slow! 

return choice(self.list()) 

 

def is_finite(self): 

r""" 

Return ``True`` as this is a finite set. 

 

EXAMPLES:: 

 

sage: Set(GF(19)).is_finite() 

True 

""" 

return True 

 

def cardinality(self): 

""" 

Return the cardinality of ``self``. 

 

EXAMPLES:: 

 

sage: Set([1,1]).cardinality() 

1 

""" 

from sage.rings.integer import Integer 

return Integer(len(self.set())) 

 

def __len__(self): 

""" 

EXAMPLES:: 

 

sage: len(Set([1,1])) 

1 

""" 

return len(self.set()) 

 

def __iter__(self): 

r""" 

Iterating through the elements of ``self``. 

 

EXAMPLES:: 

 

sage: S = Set(GF(19)) 

sage: I = iter(S) 

sage: next(I) 

0 

sage: next(I) 

1 

sage: next(I) 

2 

sage: next(I) 

3 

""" 

return iter(self.set()) 

 

def _latex_(self): 

r""" 

Return the LaTeX representation of ``self``. 

 

EXAMPLES:: 

 

sage: S = Set(GF(2)) 

sage: latex(S) 

\left\{0, 1\right\} 

""" 

return '\\left\\{' + ', '.join([latex(x) for x in self.set()]) + '\\right\\}' 

 

def _repr_(self): 

r""" 

Return the string representation of ``self``. 

 

EXAMPLES:: 

 

sage: S = Set(GF(2)) 

sage: S 

{0, 1} 

""" 

s = repr(self.set()) 

if six.PY3: 

return s 

else: 

return "{" + s[5:-2] + "}" 

 

def list(self): 

""" 

Return the elements of ``self``, as a list. 

 

EXAMPLES:: 

 

sage: X = Set(GF(8,'c')) 

sage: X 

{0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1} 

sage: X.list() 

[0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1] 

sage: type(X.list()) 

<... 'list'> 

 

.. TODO:: 

 

FIXME: What should be the order of the result? 

That of ``self.object()``? Or the order given by 

``set(self.object())``? Note that :meth:`__getitem__` is 

currently implemented in term of this list method, which 

is really inefficient ... 

""" 

return list(set(self.object())) 

 

def set(self): 

""" 

Return the Python set object associated to this set. 

 

Python has a notion of finite set, and often Sage sets 

have an associated Python set. This function returns 

that set. 

 

EXAMPLES:: 

 

sage: X = Set(GF(8,'c')) 

sage: X 

{0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1} 

sage: X.set() 

{0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1} 

sage: type(X.set()) 

<... 'set'> 

sage: type(X) 

<class 'sage.sets.set.Set_object_enumerated_with_category'> 

""" 

return set(self.object()) 

 

def frozenset(self): 

""" 

Return the Python frozenset object associated to this set, 

which is an immutable set (hence hashable). 

 

EXAMPLES:: 

 

sage: X = Set(GF(8,'c')) 

sage: X 

{0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1} 

sage: s = X.set(); s 

{0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1} 

sage: hash(s) 

Traceback (most recent call last): 

... 

TypeError: unhashable type: 'set' 

sage: s = X.frozenset(); s 

frozenset({0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1}) 

sage: hash(s) 

-1390224788 # 32-bit 

561411537695332972 # 64-bit 

sage: type(s) 

<... 'frozenset'> 

""" 

return frozenset(self.object()) 

 

def __hash__(self): 

""" 

Return the hash of ``self`` (as a ``frozenset``). 

 

EXAMPLES:: 

 

sage: s = Set(GF(8,'c')) 

sage: hash(s) == hash(s) 

True 

""" 

return hash(self.frozenset()) 

 

def __richcmp__(self, other, op): 

""" 

Compare the sets ``self`` and ``other``. 

 

EXAMPLES:: 

 

sage: X = Set(GF(8,'c')) 

sage: X == Set(GF(8,'c')) 

True 

sage: X == Set(GF(4,'a')) 

False 

sage: Set(QQ) == Set(ZZ) 

False 

""" 

if not isinstance(other, Set_object_enumerated): 

return NotImplemented 

if self.set() == other.set(): 

return rich_to_bool(op, 0) 

return rich_to_bool(op, -1) 

 

def issubset(self, other): 

r""" 

Return whether ``self`` is a subset of ``other``. 

 

INPUT: 

 

- ``other`` -- a finite Set 

 

EXAMPLES:: 

 

sage: X = Set([1,3,5]) 

sage: Y = Set([0,1,2,3,5,7]) 

sage: X.issubset(Y) 

True 

sage: Y.issubset(X) 

False 

sage: X.issubset(X) 

True 

 

TESTS:: 

 

sage: len([Z for Z in Y.subsets() if Z.issubset(X)]) 

8 

""" 

if not isinstance(other, Set_object_enumerated): 

raise NotImplementedError 

return self.set().issubset(other.set()) 

 

def issuperset(self, other): 

r""" 

Return whether ``self`` is a superset of ``other``. 

 

INPUT: 

 

- ``other`` -- a finite Set 

 

EXAMPLES:: 

 

sage: X = Set([1,3,5]) 

sage: Y = Set([0,1,2,3,5]) 

sage: X.issuperset(Y) 

False 

sage: Y.issuperset(X) 

True 

sage: X.issuperset(X) 

True 

 

TESTS:: 

 

sage: len([Z for Z in Y.subsets() if Z.issuperset(X)]) 

4 

""" 

if not isinstance(other, Set_object_enumerated): 

raise NotImplementedError 

return self.set().issuperset(other.set()) 

 

def union(self, other): 

""" 

Return the union of ``self`` and ``other``. 

 

EXAMPLES:: 

 

sage: X = Set(GF(8,'c')) 

sage: Y = Set([GF(8,'c').0, 1, 2, 3]) 

sage: X 

{0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1} 

sage: Y 

{1, c, 3, 2} 

sage: X.union(Y) 

{0, 1, c, c + 1, c^2, c^2 + 1, c^2 + c, c^2 + c + 1, 2, 3} 

""" 

if not isinstance(other, Set_object_enumerated): 

return Set_object.union(self, other) 

return Set_object_enumerated(self.set().union(other.set())) 

 

def intersection(self, other): 

""" 

Return the intersection of ``self`` and ``other``. 

 

EXAMPLES:: 

 

sage: X = Set(GF(8,'c')) 

sage: Y = Set([GF(8,'c').0, 1, 2, 3]) 

sage: X.intersection(Y) 

{1, c} 

""" 

if not isinstance(other, Set_object_enumerated): 

return Set_object.intersection(self, other) 

return Set_object_enumerated(self.set().intersection(other.set())) 

 

def difference(self, other): 

""" 

Return the set difference ``self - other``. 

 

EXAMPLES:: 

 

sage: X = Set([1,2,3,4]) 

sage: Y = Set([1,2]) 

sage: X.difference(Y) 

{3, 4} 

sage: Z = Set(ZZ) 

sage: W = Set([2.5, 4, 5, 6]) 

sage: W.difference(Z) 

{2.50000000000000} 

""" 

if not isinstance(other, Set_object_enumerated): 

return Set([x for x in self if x not in other]) 

return Set_object_enumerated(self.set().difference(other.set())) 

 

def symmetric_difference(self, other): 

""" 

Return the symmetric difference of ``self`` and ``other``. 

 

EXAMPLES:: 

 

sage: X = Set([1,2,3,4]) 

sage: Y = Set([1,2]) 

sage: X.symmetric_difference(Y) 

{3, 4} 

sage: Z = Set(ZZ) 

sage: W = Set([2.5, 4, 5, 6]) 

sage: U = W.symmetric_difference(Z) 

sage: 2.5 in U 

True 

sage: 4 in U 

False 

sage: V = Z.symmetric_difference(W) 

sage: V == U 

True 

sage: 2.5 in V 

True 

sage: 6 in V 

False 

""" 

if not isinstance(other, Set_object_enumerated): 

return Set_object.symmetric_difference(self, other) 

return Set_object_enumerated(self.set().symmetric_difference(other.set())) 

 

class Set_object_binary(Set_object): 

r""" 

An abstract common base class for sets defined by a binary operation (ex. 

:class:`Set_object_union`, :class:`Set_object_intersection`, 

:class:`Set_object_difference`, and 

:class:`Set_object_symmetric_difference`). 

 

INPUT: 

 

- ``X``, ``Y`` -- sets, the operands to ``op`` 

 

- ``op`` -- a string describing the binary operation 

 

- ``latex_op`` -- a string used for rendering this object in LaTeX 

 

EXAMPLES:: 

 

sage: X = Set(QQ^2) 

sage: Y = Set(ZZ) 

sage: from sage.sets.set import Set_object_binary 

sage: S = Set_object_binary(X, Y, "union", "\\cup"); S 

Set-theoretic union of Set of elements of Vector space of dimension 2 

over Rational Field and Set of elements of Integer Ring 

""" 

def __init__(self, X, Y, op, latex_op): 

r""" 

Initialization. 

 

TESTS:: 

 

sage: from sage.sets.set import Set_object_binary 

sage: X = Set(QQ^2) 

sage: Y = Set(ZZ) 

sage: S = Set_object_binary(X, Y, "union", "\\cup") 

sage: type(S) 

<class 'sage.sets.set.Set_object_binary_with_category'> 

""" 

self._X = X 

self._Y = Y 

self._op = op 

self._latex_op = latex_op 

Set_object.__init__(self, self) 

 

def _repr_(self): 

r""" 

Return a string representation of this set. 

 

EXAMPLES:: 

 

sage: Set(ZZ).union(Set(GF(5))) 

Set-theoretic union of Set of elements of Integer Ring and {0, 1, 2, 3, 4} 

""" 

return "Set-theoretic {} of {} and {}".format(self._op, self._X, self._Y) 

 

def _latex_(self): 

r""" 

Return a latex representation of this set. 

 

EXAMPLES:: 

 

sage: latex(Set(ZZ).union(Set(GF(5)))) 

\Bold{Z} \cup \left\{0, 1, 2, 3, 4\right\} 

""" 

return latex(self._X) + self._latex_op + latex(self._Y) 

 

def __hash__(self): 

""" 

The hash value of this set. 

 

EXAMPLES: 

 

The hash values of equal sets are in general not equal since it is not 

decidable whether two sets are equal:: 

 

sage: X = Set(GF(13)).intersection(Set(ZZ)) 

sage: Y = Set(ZZ).intersection(Set(GF(13))) 

sage: hash(X) == hash(Y) 

False 

 

TESTS: 

 

Test that :trac:`14432` has been resolved:: 

 

sage: S = Set(ZZ).union(Set([infinity])) 

sage: T = Set(ZZ).union(Set([infinity])) 

sage: hash(S) == hash(T) 

True 

""" 

return hash((self._X, self._Y, self._op)) 

 

class Set_object_union(Set_object_binary): 

""" 

A formal union of two sets. 

""" 

def __init__(self, X, Y): 

r""" 

Initialize ``self``. 

 

EXAMPLES:: 

 

sage: S = Set(QQ^2) 

sage: T = Set(ZZ) 

sage: X = S.union(T); X 

Set-theoretic union of Set of elements of Vector space of dimension 2 over Rational Field and Set of elements of Integer Ring 

 

sage: latex(X) 

\Bold{Q}^{2} \cup \Bold{Z} 

 

sage: TestSuite(X).run() 

""" 

Set_object_binary.__init__(self, X, Y, "union", "\\cup") 

 

def is_finite(self): 

r""" 

Return whether this set is finite. 

 

EXAMPLES:: 

 

sage: X = Set(range(10)) 

sage: Y = Set(range(-10,0)) 

sage: Z = Set(Primes()) 

sage: X.union(Y).is_finite() 

True 

sage: X.union(Z).is_finite() 

False 

""" 

return self._X.is_finite() and self._Y.is_finite() 

 

def __richcmp__(self, right, op): 

r""" 

Try to compare ``self`` and ``right``. 

 

.. NOTE:: 

 

Comparison is basically not implemented, or rather it could 

say sets are not equal even though they are. I don't know 

how one could implement this for a generic union of sets in 

a meaningful manner. So be careful when using this. 

 

EXAMPLES:: 

 

sage: Y = Set(ZZ^2).union(Set(ZZ^3)) 

sage: X = Set(ZZ^3).union(Set(ZZ^2)) 

sage: X == Y 

True 

sage: Y == X 

True 

 

This illustrates that equality testing for formal unions 

can be misleading in general. 

 

:: 

 

sage: Set(ZZ).union(Set(QQ)) == Set(QQ) 

False 

""" 

if not isinstance(right, Set_generic): 

return rich_to_bool(op, -1) 

if not isinstance(right, Set_object_union): 

return rich_to_bool(op, -1) 

if self._X == right._X and self._Y == right._Y or \ 

self._X == right._Y and self._Y == right._X: 

return rich_to_bool(op, 0) 

return rich_to_bool(op, -1) 

 

def __iter__(self): 

""" 

Return iterator over the elements of ``self``. 

 

EXAMPLES:: 

 

sage: [x for x in Set(GF(3)).union(Set(GF(2)))] 

[0, 1, 2, 0, 1] 

""" 

for x in self._X: 

yield x 

for y in self._Y: 

yield y 

 

def __contains__(self, x): 

""" 

Return ``True`` if ``x`` is an element of ``self``. 

 

EXAMPLES:: 

 

sage: X = Set(GF(3)).union(Set(GF(2))) 

sage: GF(5)(1) in X 

False 

sage: GF(3)(2) in X 

True 

sage: GF(2)(0) in X 

True 

sage: GF(5)(0) in X 

False 

""" 

return x in self._X or x in self._Y 

 

def cardinality(self): 

""" 

Return the cardinality of this set. 

 

EXAMPLES:: 

 

sage: X = Set(GF(3)).union(Set(GF(2))) 

sage: X 

{0, 1, 2, 0, 1} 

sage: X.cardinality() 

5 

 

sage: X = Set(GF(3)).union(Set(ZZ)) 

sage: X.cardinality() 

+Infinity 

""" 

return self._X.cardinality() + self._Y.cardinality() 

 

class Set_object_intersection(Set_object_binary): 

""" 

Formal intersection of two sets. 

""" 

def __init__(self, X, Y): 

r""" 

Initialize ``self``. 

 

EXAMPLES:: 

 

sage: S = Set(QQ^2) 

sage: T = Set(ZZ) 

sage: X = S.intersection(T); X 

Set-theoretic intersection of Set of elements of Vector space of dimension 2 over Rational Field and Set of elements of Integer Ring 

sage: latex(X) 

\Bold{Q}^{2} \cap \Bold{Z} 

 

sage: X = Set(IntegerRange(100)).intersection(Primes()) 

sage: X.is_finite() 

True 

sage: TestSuite(X).run() 

""" 

Set_object_binary.__init__(self, X, Y, "intersection", "\\cap") 

 

def is_finite(self): 

r""" 

Return whether this set is finite. 

 

EXAMPLES:: 

 

sage: X = Set(IntegerRange(100)) 

sage: Y = Set(ZZ) 

sage: X.intersection(Y).is_finite() 

True 

sage: Y.intersection(X).is_finite() 

True 

sage: Y.intersection(Set(QQ)).is_finite() 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

if self._X.is_finite(): 

return True 

elif self._Y.is_finite(): 

return True 

raise NotImplementedError 

 

def __richcmp__(self, right, op): 

r""" 

Try to compare ``self`` and ``right``. 

 

.. NOTE:: 

 

Comparison is basically not implemented, or rather it could 

say sets are not equal even though they are. I don't know 

how one could implement this for a generic intersection of 

sets in a meaningful manner. So be careful when using this. 

 

EXAMPLES:: 

 

sage: Y = Set(ZZ).intersection(Set(QQ)) 

sage: X = Set(QQ).intersection(Set(ZZ)) 

sage: X == Y 

True 

sage: Y == X 

True 

 

This illustrates that equality testing for formal unions 

can be misleading in general. 

 

:: 

 

sage: Set(ZZ).intersection(Set(QQ)) == Set(QQ) 

False 

""" 

if not isinstance(right, Set_generic): 

return rich_to_bool(op, -1) 

if not isinstance(right, Set_object_intersection): 

return rich_to_bool(op, -1) 

if self._X == right._X and self._Y == right._Y or \ 

self._X == right._Y and self._Y == right._X: 

return rich_to_bool(op, 0) 

return rich_to_bool(op, -1) 

 

def __iter__(self): 

""" 

Return iterator through elements of ``self``. 

 

``self`` is a formal intersection of `X` and `Y` and this function is 

implemented by iterating through the elements of `X` and for 

each checking if it is in `Y`, and if yielding it. 

 

EXAMPLES:: 

 

sage: X = Set(ZZ).intersection(Primes()) 

sage: I = X.__iter__() 

sage: next(I) 

2 

 

Check that known finite intersections have finite iterators (see 

:trac:`18159`):: 

 

sage: P = Set(ZZ).intersection(Set(range(10,20))) 

sage: list(P) 

[10, 11, 12, 13, 14, 15, 16, 17, 18, 19] 

""" 

X = self._X 

Y = self._Y 

if not self._X.is_finite() and self._Y.is_finite(): 

X,Y = Y,X 

for x in X: 

if x in Y: 

yield x 

 

def __contains__(self, x): 

""" 

Return ``True`` if ``self`` contains ``x``. 

 

Since ``self`` is a formal intersection of `X` and `Y` this function 

returns ``True`` if both `X` and `Y` contains ``x``. 

 

EXAMPLES:: 

 

sage: X = Set(QQ).intersection(Set(RR)) 

sage: 5 in X 

True 

sage: ComplexField().0 in X 

False 

 

Any specific floating-point number in Sage is to finite precision, 

hence it is rational:: 

 

sage: RR(sqrt(2)) in X 

True 

 

Real constants are not rational:: 

 

sage: pi in X 

False 

""" 

return x in self._X and x in self._Y 

 

class Set_object_difference(Set_object_binary): 

""" 

Formal difference of two sets. 

""" 

def __init__(self, X, Y): 

r""" 

Initialize ``self``. 

 

EXAMPLES:: 

 

sage: S = Set(QQ) 

sage: T = Set(ZZ) 

sage: X = S.difference(T); X 

Set-theoretic difference of Set of elements of Rational Field and Set of elements of Integer Ring 

sage: latex(X) 

\Bold{Q} - \Bold{Z} 

 

sage: TestSuite(X).run() 

""" 

Set_object_binary.__init__(self, X, Y, "difference", "-") 

 

def is_finite(self): 

r""" 

Return whether this set is finite. 

 

EXAMPLES:: 

 

sage: X = Set(range(10)) 

sage: Y = Set(range(-10,5)) 

sage: Z = Set(QQ) 

sage: X.difference(Y).is_finite() 

True 

sage: X.difference(Z).is_finite() 

True 

sage: Z.difference(X).is_finite() 

False 

sage: Z.difference(Set(ZZ)).is_finite() 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

if self._X.is_finite(): 

return True 

elif self._Y.is_finite(): 

return False 

raise NotImplementedError 

 

def __richcmp__(self, right, op): 

r""" 

Try to compare ``self`` and ``right``. 

 

.. NOTE:: 

 

Comparison is basically not implemented, or rather it could 

say sets are not equal even though they are. I don't know 

how one could implement this for a generic intersection of 

sets in a meaningful manner. So be careful when using 

this. 

 

EXAMPLES:: 

 

sage: Y = Set(ZZ).difference(Set(QQ)) 

sage: Y == Set([]) 

False 

sage: X = Set(QQ).difference(Set(ZZ)) 

sage: Y == X 

False 

sage: Z = X.difference(Set(ZZ)) 

sage: Z == X 

False 

 

This illustrates that equality testing for formal unions 

can be misleading in general. 

 

:: 

 

sage: X == Set(QQ).difference(Set(ZZ)) 

True 

""" 

if not isinstance(right, Set_generic): 

return rich_to_bool(op, -1) 

if not isinstance(right, Set_object_difference): 

return rich_to_bool(op, -1) 

if self._X == right._X and self._Y == right._Y: 

return rich_to_bool(op, 0) 

return rich_to_bool(op, -1) 

 

def __iter__(self): 

""" 

Return iterator through elements of ``self``. 

 

``self`` is a formal difference of `X` and `Y` and this function 

is implemented by iterating through the elements of `X` and for 

each checking if it is not in `Y`, and if yielding it. 

 

EXAMPLES:: 

 

sage: X = Set(ZZ).difference(Primes()) 

sage: I = X.__iter__() 

sage: next(I) 

0 

sage: next(I) 

1 

sage: next(I) 

-1 

sage: next(I) 

-2 

sage: next(I) 

-3 

""" 

for x in self._X: 

if x not in self._Y: 

yield x 

 

def __contains__(self, x): 

""" 

Return ``True`` if ``self`` contains ``x``. 

 

Since ``self`` is a formal intersection of `X` and `Y` this function 

returns ``True`` if both `X` and `Y` contains ``x``. 

 

EXAMPLES:: 

 

sage: X = Set(QQ).difference(Set(ZZ)) 

sage: 5 in X 

False 

sage: ComplexField().0 in X 

False 

sage: sqrt(2) in X # since sqrt(2) is not a numerical approx 

False 

sage: sqrt(RR(2)) in X # since sqrt(RR(2)) is a numerical approx 

True 

sage: 5/2 in X 

True 

""" 

return x in self._X and x not in self._Y 

 

class Set_object_symmetric_difference(Set_object_binary): 

""" 

Formal symmetric difference of two sets. 

""" 

def __init__(self, X, Y): 

r""" 

Initialize ``self``. 

 

EXAMPLES:: 

 

sage: S = Set(QQ) 

sage: T = Set(ZZ) 

sage: X = S.symmetric_difference(T); X 

Set-theoretic symmetric difference of Set of elements of Rational Field and Set of elements of Integer Ring 

sage: latex(X) 

\Bold{Q} \bigtriangleup \Bold{Z} 

 

sage: TestSuite(X).run() 

""" 

Set_object_binary.__init__(self, X, Y, "symmetric difference", "\\bigtriangleup") 

 

def is_finite(self): 

r""" 

Return whether this set is finite. 

 

EXAMPLES:: 

 

sage: X = Set(range(10)) 

sage: Y = Set(range(-10,5)) 

sage: Z = Set(QQ) 

sage: X.symmetric_difference(Y).is_finite() 

True 

sage: X.symmetric_difference(Z).is_finite() 

False 

sage: Z.symmetric_difference(X).is_finite() 

False 

sage: Z.symmetric_difference(Set(ZZ)).is_finite() 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

if self._X.is_finite(): 

return self._Y.is_finite() 

elif self._Y.is_finite(): 

return False 

raise NotImplementedError 

 

def __richcmp__(self, right, op): 

r""" 

Try to compare ``self`` and ``right``. 

 

.. NOTE:: 

 

Comparison is basically not implemented, or rather it could 

say sets are not equal even though they are. I don't know 

how one could implement this for a generic symmetric 

difference of sets in a meaningful manner. So be careful 

when using this. 

 

EXAMPLES:: 

 

sage: Y = Set(ZZ).symmetric_difference(Set(QQ)) 

sage: X = Set(QQ).symmetric_difference(Set(ZZ)) 

sage: X == Y 

True 

sage: Y == X 

True 

 

""" 

if not isinstance(right, Set_generic): 

return rich_to_bool(op, -1) 

if not isinstance(right, Set_object_symmetric_difference): 

return rich_to_bool(op, -1) 

if self._X == right._X and self._Y == right._Y or \ 

self._X == right._Y and self._Y == right._X: 

return rich_to_bool(op, 0) 

return rich_to_bool(op, -1) 

 

def __iter__(self): 

""" 

Return iterator through elements of ``self``. 

 

This function is implemented by first iterating through the elements 

of `X` and yielding it if it is not in `Y`. 

Then it will iterate throw all the elements of `Y` and yielding it if 

it is not in `X`. 

 

EXAMPLES:: 

 

sage: X = Set(ZZ).symmetric_difference(Primes()) 

sage: I = X.__iter__() 

sage: next(I) 

0 

sage: next(I) 

1 

sage: next(I) 

-1 

sage: next(I) 

-2 

sage: next(I) 

-3 

""" 

for x in self._X: 

if x not in self._Y: 

yield x 

 

for y in self._Y: 

if y not in self._X: 

yield y 

 

def __contains__(self, x): 

""" 

Return ``True`` if ``self`` contains ``x``. 

 

Since ``self`` is the formal symmetric difference of `X` and `Y` 

this function returns ``True`` if either `X` or `Y` (but not both) 

contains ``x``. 

 

EXAMPLES:: 

 

sage: X = Set(QQ).symmetric_difference(Primes()) 

sage: 4 in X 

True 

sage: ComplexField().0 in X 

False 

sage: sqrt(2) in X # since sqrt(2) is currently symbolic 

False 

sage: sqrt(RR(2)) in X # since sqrt(RR(2)) is currently approximated 

True 

sage: pi in X 

False 

sage: 5/2 in X 

True 

sage: 3 in X 

False 

""" 

return (x in self._X and x not in self._Y) \ 

or (x in self._Y and x not in self._X) 

 

def is_Set(x): 

""" 

Deprecated. Use ``isinstance(x, Set_generic)`` instead. 

 

TESTS:: 

 

sage: from sage.sets.set import is_Set 

sage: is_Set(Primes()) 

doctest:...: DeprecationWarning: Please use isinstance(x, Set_generic) 

See http://trac.sagemath.org/24443 for details. 

True 

""" 

from sage.misc.superseded import deprecation 

deprecation(24443, "Please use isinstance(x, Set_generic)") 

return isinstance(x, Set_generic)