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

r""" 

Substitutions over unit cube faces (Rauzy fractals) 

 

This module implements the `E_1^*(\sigma)` substitution 

associated with a one-dimensional substitution `\sigma`, 

that acts on unit faces of dimension `(d-1)` in `\RR^d`. 

 

This module defines the following classes and functions: 

 

- ``Face`` - a class to model a face 

 

- ``Patch`` - a class to model a finite set of faces 

 

- ``E1Star`` - a class to model the `E_1^*(\sigma)` application 

defined by the substitution sigma 

 

See the documentation of these objects for more information. 

 

The convention for the choice of the unit faces and the 

definition of `E_1^*(\sigma)` varies from article to article. 

Here, unit faces are defined by 

 

.. MATH:: 

 

\begin{array}{ccc} 

\,[x, 1]^* & = & \{x + \lambda e_2 + \mu e_3 : \lambda, \mu \in [0,1]\} \\ 

\,[x, 2]^* & = & \{x + \lambda e_1 + \mu e_3 : \lambda, \mu \in [0,1]\} \\ 

\,[x, 3]^* & = & \{x + \lambda e_1 + \mu e_2 : \lambda, \mu \in [0,1]\} 

\end{array} 

 

and the dual substitution `E_1^*(\sigma)` is defined by 

 

.. MATH:: 

 

E_1^*(\sigma)([x,i]^*) = 

\bigcup_{k = 1,2,3} \; \bigcup_{s | \sigma(k) = pis} 

[M^{-1}(x + \ell(s)), k]^*, 

 

where `\ell(s)` is the abelianized of `s`, and `M` is the matrix of `\sigma`. 

 

AUTHORS: 

 

- Franco Saliola (2009): initial version 

- Vincent Delecroix, Timo Jolivet, Stepan Starosta, Sebastien Labbe (2010-05): redesign 

- Timo Jolivet (2010-08, 2010-09, 2011): redesign 

 

REFERENCES: 

 

.. [AI] \P. Arnoux, S. Ito, 

Pisot substitutions and Rauzy fractals, 

Bull. Belg. Math. Soc. 8 (2), 2001, pp. 181--207 

 

.. [SAI] \Y. Sano, P. Arnoux, S. Ito, 

Higher dimensional extensions of substitutions and their dual maps, 

J. Anal. Math. 83, 2001, pp. 183--206 

 

EXAMPLES: 

 

We start by drawing a simple three-face patch:: 

 

sage: from sage.combinat.e_one_star import E1Star, Face, Patch 

sage: x = [Face((0,0,0),1), Face((0,0,0),2), Face((0,0,0),3)] 

sage: P = Patch(x) 

sage: P 

Patch: [[(0, 0, 0), 1]*, [(0, 0, 0), 2]*, [(0, 0, 0), 3]*] 

sage: P.plot() #not tested 

 

We apply a substitution to this patch, and draw the result:: 

 

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1]}) 

sage: E = E1Star(sigma) 

sage: E(P) 

Patch: [[(0, 0, 0), 1]*, [(0, 0, 0), 2]*, [(0, 0, 0), 3]*, [(0, 1, -1), 2]*, [(1, 0, -1), 1]*] 

sage: E(P).plot() #not tested 

 

.. NOTE:: 

 

- The type of a face is given by an integer in ``[1, ..., d]`` 

where ``d`` is the length of the vector of the face. 

 

- The alphabet of the domain and the codomain of `\sigma` must be 

equal, and they must be of the form ``[1, ..., d]``, where ``d`` 

is a positive integer corresponding to the length of the vectors 

of the faces on which `E_1^*(\sigma)` will act. 

 

:: 

 

sage: P = Patch([Face((0,0,0),1), Face((0,0,0),2), Face((0,0,0),3)]) 

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1]}) 

sage: E = E1Star(sigma) 

sage: E(P) 

Patch: [[(0, 0, 0), 1]*, [(0, 0, 0), 2]*, [(0, 0, 0), 3]*, [(0, 1, -1), 2]*, [(1, 0, -1), 1]*] 

 

The application of an ``E1Star`` substitution assigns to each new face the color of its preimage. 

The ``repaint`` method allows us to repaint the faces of a patch. 

A single color can also be assigned to every face, by specifying a list of a single color:: 

 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: P = E(P, 5) 

sage: P.repaint(['green']) 

sage: P.plot() #not tested 

 

A list of colors allows us to color the faces sequentially:: 

 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: P = E(P) 

sage: P.repaint(['red', 'yellow', 'green', 'blue', 'black']) 

sage: P = E(P, 3) 

sage: P.plot() #not tested 

 

All the color schemes from ``list(matplotlib.cm.datad)`` can be used:: 

 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: P.repaint(cmap='summer') 

sage: P = E(P, 3) 

sage: P.plot() #not tested 

sage: P.repaint(cmap='hsv') 

sage: P = E(P, 2) 

sage: P.plot() #not tested 

 

It is also possible to specify a dictionary to color the faces according to their type:: 

 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: P = E(P, 5) 

sage: P.repaint({1:(0.7, 0.7, 0.7), 2:(0.5,0.5,0.5), 3:(0.3,0.3,0.3)}) 

sage: P.plot() #not tested 

sage: P.repaint({1:'red', 2:'yellow', 3:'green'}) 

sage: P.plot() #not tested 

 

Let us look at a nice big patch in 3D:: 

 

sage: sigma = WordMorphism({1:[1,2], 2:[3], 3:[1]}) 

sage: E = E1Star(sigma) 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: P = P + P.translate([-1,1,0]) 

sage: P = E(P, 11) 

sage: P.plot3d() #not tested 

 

Plotting with TikZ pictures is possible:: 

 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: s = P.plot_tikz() 

sage: print(s) #not tested 

\begin{tikzpicture} 

[x={(-0.216506cm,-0.125000cm)}, y={(0.216506cm,-0.125000cm)}, z={(0.000000cm,0.250000cm)}] 

\definecolor{facecolor}{rgb}{0.000,1.000,0.000} 

\fill[fill=facecolor, draw=black, shift={(0,0,0)}] 

(0, 0, 0) -- (0, 0, 1) -- (1, 0, 1) -- (1, 0, 0) -- cycle; 

\definecolor{facecolor}{rgb}{1.000,0.000,0.000} 

\fill[fill=facecolor, draw=black, shift={(0,0,0)}] 

(0, 0, 0) -- (0, 1, 0) -- (0, 1, 1) -- (0, 0, 1) -- cycle; 

\definecolor{facecolor}{rgb}{0.000,0.000,1.000} 

\fill[fill=facecolor, draw=black, shift={(0,0,0)}] 

(0, 0, 0) -- (1, 0, 0) -- (1, 1, 0) -- (0, 1, 0) -- cycle; 

\end{tikzpicture} 

 

Plotting patches made of unit segments instead of unit faces:: 

 

sage: P = Patch([Face([0,0], 1), Face([0,0], 2)]) 

sage: E = E1Star(WordMorphism({1:[1,2],2:[1]})) 

sage: F = E1Star(WordMorphism({1:[1,1,2],2:[2,1]})) 

sage: E(P,5).plot() 

Graphics object consisting of 21 graphics primitives 

sage: F(P,3).plot() 

Graphics object consisting of 34 graphics primitives 

 

Everything works in any dimension (except for the plotting features 

which only work in dimension two or three):: 

 

sage: P = Patch([Face((0,0,0,0),1), Face((0,0,0,0),4)]) 

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1,4], 4:[1]}) 

sage: E = E1Star(sigma) 

sage: E(P) 

Patch: [[(0, 0, 0, 0), 3]*, [(0, 0, 0, 0), 4]*, [(0, 0, 1, -1), 3]*, [(0, 1, 0, -1), 2]*, [(1, 0, 0, -1), 1]*] 

 

:: 

 

sage: sigma = WordMorphism({1:[1,2],2:[1,3],3:[1,4],4:[1,5],5:[1,6],6:[1,7],7:[1,8],8:[1,9],9:[1,10],10:[1,11],11:[1,12],12:[1]}) 

sage: E = E1Star(sigma) 

sage: E 

E_1^*(1->12, 10->1,11, 11->1,12, 12->1, 2->13, 3->14, 4->15, 5->16, 6->17, 7->18, 8->19, 9->1,10) 

sage: P = Patch([Face((0,0,0,0,0,0,0,0,0,0,0,0),t) for t in [1,2,3]]) 

sage: for x in sorted(list(E(P)), key=lambda x : (x.vector(),x.type())): print(x) 

[(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 1]* 

[(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 2]* 

[(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 12]* 

[(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1), 11]* 

[(0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1), 10]* 

[(0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, -1), 9]* 

[(0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, -1), 8]* 

[(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1), 7]* 

[(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, -1), 6]* 

[(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -1), 5]* 

[(0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -1), 4]* 

[(0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, -1), 3]* 

[(0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1), 2]* 

[(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1), 1]* 

""" 

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

# Copyright (C) 2010 Franco Saliola <saliola@gmail.com> 

# Vincent Delecroix <20100.delecroix@gmail.com> 

# Timo Jolivet <timo.jolivet@gmail.com> 

# Stepan Starosta <stepan.starosta@gmail.com> 

# Sebastien Labbe <slabqc at gmail.com> 

# 

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

# 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/ 

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

from six.moves import range 

 

from sage.misc.functional import det 

from sage.structure.sage_object import SageObject 

from sage.combinat.words.morphism import WordMorphism 

from sage.matrix.constructor import matrix 

from sage.modules.free_module_element import vector 

from sage.plot.all import Graphics 

from sage.plot.colors import Color 

from sage.plot.polygon import polygon 

from sage.plot.line import line 

from sage.rings.integer_ring import ZZ 

from sage.misc.latex import LatexExpr 

from sage.misc.cachefunc import cached_method 

from sage.structure.richcmp import richcmp_by_eq_and_lt, richcmp_method 

 

# matplotlib color maps, loaded on-demand 

cm = None 

 

 

@richcmp_method 

class Face(SageObject): 

r""" 

A class to model a unit face of arbitrary dimension. 

 

A unit face in dimension `d` is represented by 

a `d`-dimensional vector ``v`` and a type ``t`` in `\{1, \ldots, d\}`. 

The type of the face corresponds to the canonical unit vector 

to which the face is orthogonal. 

The optional ``color`` argument is used in plotting functions. 

 

INPUT: 

 

- ``v`` - tuple of integers 

- ``t`` - integer in ``[1, ..., len(v)]``, type of the face. The face of type `i` 

is orthogonal to the canonical vector `e_i`. 

- ``color`` - color (optional, default: ``None``) color of the face, 

used for plotting only. If ``None``, its value is guessed from the 

face type. 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face 

sage: f = Face((0,2,0), 3) 

sage: f.vector() 

(0, 2, 0) 

sage: f.type() 

3 

 

:: 

 

sage: f = Face((0,2,0), 3, color=(0.5, 0.5, 0.5)) 

sage: f.color() 

RGB color (0.5, 0.5, 0.5) 

""" 

def __init__(self, v, t, color=None): 

r""" 

Face constructor. See class doc for more information. 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face 

sage: f = Face((0,2,0), 3) 

sage: f.vector() 

(0, 2, 0) 

sage: f.type() 

3 

 

TESTS: 

 

We test that types can be given by an int (see :trac:`10699`):: 

 

sage: f = Face((0,2,0), int(1)) 

""" 

self._vector = (ZZ**len(v))(v) 

self._vector.set_immutable() 

 

if not((t in ZZ) and 1 <= t <= len(v)): 

raise ValueError('The type must be an integer between 1 and len(v)') 

self._type = t 

 

if color is None: 

if self._type == 1: 

color = Color((1,0,0)) 

elif self._type == 2: 

color = Color((0,1,0)) 

elif self._type == 3: 

color = Color((0,0,1)) 

else: 

color = Color() 

self._color = Color(color) 

 

def __repr__(self): 

r""" 

String representation of a face. 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face 

sage: f = Face((0,0,0,3), 3) 

sage: f 

[(0, 0, 0, 3), 3]* 

 

:: 

 

sage: f = Face((0,0,0,3), 3) 

sage: f 

[(0, 0, 0, 3), 3]* 

""" 

return "[%s, %s]*" % (self.vector(), self.type()) 

 

__richcmp__ = richcmp_by_eq_and_lt('_eq', '_lt') 

 

def _eq(self, other): 

r""" 

Equality of faces. 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face 

sage: f = Face((0,0,0,3), 3) 

sage: g = Face((0,0,0,3), 3) 

sage: f == g 

True 

""" 

return (isinstance(other, Face) and 

self.vector() == other.vector() and 

self.type() == other.type() ) 

 

def _lt(self, other): 

r""" 

Compare ``self`` and ``other``. 

 

The vectors of the faces are first compared, 

and the types of the faces are compared if the vectors are equal. 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face 

sage: Face([-2,1,0], 2) < Face([-1,2,2],3) 

True 

sage: Face([-2,1,0], 2) < Face([-2,1,0],3) 

True 

sage: Face([-2,1,0], 2) < Face([-2,1,0],2) 

False 

""" 

if self.vector() < other.vector(): 

return True 

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

return self.type() < other.type() 

 

def __hash__(self): 

r""" 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face 

sage: f = Face((0,0,0,3), 3) 

sage: g = Face((0,0,0,3), 3) 

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

True 

""" 

return hash((self.vector(), self.type())) 

 

def __add__(self, other): 

r""" 

Addition of self with a Face, a Patch or a finite iterable of faces. 

 

INPUT: 

 

- ``other`` - a Patch or a Face or a finite iterable of faces 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face, Patch 

sage: f = Face([0,0,0], 3) 

sage: g = Face([0,1,-1], 2) 

sage: f + g 

Patch: [[(0, 0, 0), 3]*, [(0, 1, -1), 2]*] 

sage: P = Patch([Face([0,0,0], 1), Face([0,0,0], 2)]) 

sage: f + P 

Patch: [[(0, 0, 0), 1]*, [(0, 0, 0), 2]*, [(0, 0, 0), 3]*] 

 

Adding a finite iterable of faces:: 

 

sage: from sage.combinat.e_one_star import Face 

sage: f = Face([0,0,0], 3) 

sage: f + [f,f] 

Patch: [[(0, 0, 0), 3]*] 

""" 

if isinstance(other, Face): 

return Patch([self, other]) 

else: 

return Patch(other).union(self) 

 

def vector(self): 

r""" 

Return the vector of the face. 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face 

sage: f = Face((0,2,0), 3) 

sage: f.vector() 

(0, 2, 0) 

""" 

return self._vector 

 

def type(self): 

r""" 

Return the type of the face. 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face 

sage: f = Face((0,2,0), 3) 

sage: f.type() 

3 

 

:: 

 

sage: f = Face((0,2,0), 3) 

sage: f.type() 

3 

""" 

return self._type 

 

def color(self, color=None): 

r""" 

Return or change the color of the face. 

 

INPUT: 

 

- ``color`` - string, rgb tuple, color (optional, default: ``None``) 

the new color to assign to the face. If ``None``, it returns the 

color of the face. 

 

OUTPUT: 

 

color 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face 

sage: f = Face((0,2,0), 3) 

sage: f.color() 

RGB color (0.0, 0.0, 1.0) 

sage: f.color('red') 

sage: f.color() 

RGB color (1.0, 0.0, 0.0) 

 

""" 

if color is None: 

return self._color 

else: 

self._color = Color(color) 

 

def _plot(self, projmat, face_contour, opacity): 

r""" 

Return a 2D graphic object representing the face. 

 

INPUT: 

 

- ``projmat`` - 2*3 projection matrix (used only for faces in three dimensions) 

- ``face_contour`` - dict, maps the face type to vectors describing 

the contour of unit faces (used only for faces in three dimensions) 

- ``opacity`` - the alpha value for the color of the face 

 

OUTPUT: 

 

2D graphic object 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face 

sage: f = Face((0,0,3), 3) 

sage: projmat = matrix(2, [-1.7320508075688772*0.5, 1.7320508075688772*0.5, 0, -0.5, -0.5, 1]) 

sage: face_contour = {} 

sage: face_contour[1] = map(vector, [(0,0,0),(0,1,0),(0,1,1),(0,0,1)]) 

sage: face_contour[2] = map(vector, [(0,0,0),(0,0,1),(1,0,1),(1,0,0)]) 

sage: face_contour[3] = map(vector, [(0,0,0),(1,0,0),(1,1,0),(0,1,0)]) 

sage: G = f._plot(projmat, face_contour, 0.75) 

 

:: 

 

sage: f = Face((0,0), 2) 

sage: f._plot(None, None, 1) 

Graphics object consisting of 1 graphics primitive 

""" 

v = self.vector() 

t = self.type() 

G = Graphics() 

 

if len(v) == 2: 

if t == 1: 

G += line([v, v + vector([0,1])], rgbcolor=self.color(), thickness=1.5, alpha=opacity) 

elif t == 2: 

G += line([v, v + vector([1,0])], rgbcolor=self.color(), thickness=1.5, alpha=opacity) 

 

elif len(v) == 3: 

G += polygon([projmat*(u+v) for u in face_contour[t]], alpha=opacity, 

thickness=1, rgbcolor=self.color()) 

 

else: 

raise NotImplementedError("Plotting is implemented only for patches in two or three dimensions.") 

 

return G 

 

def _plot3d(self, face_contour): 

r""" 

3D representation of a unit face (Jmol). 

 

INPUT: 

 

- ``face_contour`` - dict, maps the face type to vectors describing 

the contour of unit faces 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face 

sage: f = Face((0,0,3), 3) 

sage: face_contour = {1: map(vector, [(0,0,0),(0,1,0),(0,1,1),(0,0,1)]), 2: map(vector, [(0,0,0),(0,0,1),(1,0,1),(1,0,0)]), 3: map(vector, [(0,0,0),(1,0,0),(1,1,0),(0,1,0)])} 

sage: G = f._plot3d(face_contour) #not tested 

""" 

v = self.vector() 

t = self.type() 

c = self.color() 

G = polygon([u+v for u in face_contour[t]], rgbcolor=c) 

return G 

 

class Patch(SageObject): 

r""" 

A class to model a collection of faces. A patch is represented by an immutable set of Faces. 

 

.. NOTE:: 

 

The dimension of a patch is the length of the vectors of the faces in the patch, 

which is assumed to be the same for every face in the patch. 

 

.. NOTE:: 

 

Since version 4.7.1, Patches are immutable, except for the colors of the faces, 

which are not taken into account for equality tests and hash functions. 

 

INPUT: 

 

- ``faces`` - finite iterable of faces 

- ``face_contour`` - dict (optional, default:``None``) maps the face 

type to vectors describing the contour of unit faces. If None, 

defaults contour are assumed for faces of type 1, 2, 3 or 1, 2, 3. 

Used in plotting methods only. 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face, Patch 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: P 

Patch: [[(0, 0, 0), 1]*, [(0, 0, 0), 2]*, [(0, 0, 0), 3]*] 

 

:: 

 

sage: face_contour = {} 

sage: face_contour[1] = map(vector, [(0,0,0),(0,1,0),(0,1,1),(0,0,1)]) 

sage: face_contour[2] = map(vector, [(0,0,0),(0,0,1),(1,0,1),(1,0,0)]) 

sage: face_contour[3] = map(vector, [(0,0,0),(1,0,0),(1,1,0),(0,1,0)]) 

sage: Patch([Face((0,0,0),t) for t in [1,2,3]], face_contour=face_contour) 

Patch: [[(0, 0, 0), 1]*, [(0, 0, 0), 2]*, [(0, 0, 0), 3]*] 

""" 

def __init__(self, faces, face_contour=None): 

r""" 

Constructor of a patch (set of faces). See class doc for more information. 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face, Patch 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: P 

Patch: [[(0, 0, 0), 1]*, [(0, 0, 0), 2]*, [(0, 0, 0), 3]*] 

 

TESTS: 

 

We test that colors are not anymore mixed up between 

Patches (see :trac:`11255`):: 

 

sage: P = Patch([Face([0,0,0],2)]) 

sage: Q = Patch(P) 

sage: next(iter(P)).color() 

RGB color (0.0, 1.0, 0.0) 

sage: next(iter(Q)).color('yellow') 

sage: next(iter(P)).color() 

RGB color (0.0, 1.0, 0.0) 

 

""" 

self._faces = frozenset(Face(f.vector(), f.type(), f.color()) for f in faces) 

 

try: 

f0 = next(iter(self._faces)) 

except StopIteration: 

self._dimension = None 

else: 

self._dimension = len(f0.vector()) 

 

if not face_contour is None: 

self._face_contour = face_contour 

 

else: 

self._face_contour = { 

1: [vector(_) for _ in [(0,0,0),(0,1,0),(0,1,1),(0,0,1)]], 

2: [vector(_) for _ in [(0,0,0),(0,0,1),(1,0,1),(1,0,0)]], 

3: [vector(_) for _ in [(0,0,0),(1,0,0),(1,1,0),(0,1,0)]] 

} 

 

def __eq__(self, other): 

r""" 

Equality test for Patch. 

 

INPUT: 

 

- ``other`` - an object 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import E1Star, Face, Patch 

sage: P = Patch([Face((0,0,0),1), Face((0,0,0),2), Face((0,0,0),3)]) 

sage: Q = Patch([Face((0,1,0),1), Face((0,0,0),3)]) 

sage: P == P 

True 

sage: P == Q 

False 

sage: P == 4 

False 

 

:: 

 

sage: s = WordMorphism({1:[1,3], 2:[1,2,3], 3:[3]}) 

sage: t = WordMorphism({1:[1,2,3], 2:[2,3], 3:[3]}) 

sage: P = Patch([Face((0,0,0), 1), Face((0,0,0), 2), Face((0,0,0), 3)]) 

sage: E1Star(s)(P) == E1Star(t)(P) 

False 

sage: E1Star(s*t)(P) == E1Star(t)(E1Star(s)(P)) 

True 

""" 

return (isinstance(other, Patch) and self._faces == other._faces) 

 

def __hash__(self): 

r""" 

Hash function of Patch. 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face, Patch 

sage: x = [Face((0,0,0),t) for t in [1,2,3]] 

sage: P = Patch(x) 

sage: hash(P) #random 

-4839605361791007520 

 

TESTS: 

 

We test that two equal patches have the same hash (see :trac:`11255`):: 

 

sage: P = Patch([Face([0,0,0],1), Face([0,0,0],2)]) 

sage: Q = Patch([Face([0,0,0],2), Face([0,0,0],1)]) 

sage: P == Q 

True 

sage: hash(P) == hash(Q) 

True 

 

Changing the color does not affect the hash value:: 

 

sage: p = Patch([Face((0,0,0), t) for t in [1,2,3]]) 

sage: H1 = hash(p) 

sage: p.repaint(['blue']) 

sage: H2 = hash(p) 

sage: H1 == H2 

True 

""" 

return hash(self._faces) 

 

def __len__(self): 

r""" 

Return the number of faces contained in the patch. 

 

OUTPUT: 

 

integer 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face, Patch 

sage: x = [Face((0,0,0),t) for t in [1,2,3]] 

sage: P = Patch(x) 

sage: len(P) #indirect doctest 

3 

""" 

return len(self._faces) 

 

def __iter__(self): 

r""" 

Return an iterator over the faces of the patch. 

 

OUTPUT: 

 

iterator 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face, Patch 

sage: x = [Face((0,0,0),t) for t in [1,2,3]] 

sage: P = Patch(x) 

sage: it = iter(P) 

sage: type(next(it)) 

<class 'sage.combinat.e_one_star.Face'> 

sage: type(next(it)) 

<class 'sage.combinat.e_one_star.Face'> 

sage: type(next(it)) 

<class 'sage.combinat.e_one_star.Face'> 

sage: type(next(it)) 

Traceback (most recent call last): 

... 

StopIteration 

""" 

return iter(self._faces) 

 

def __add__(self, other): 

r""" 

Addition of patches (union). 

 

INPUT: 

 

- ``other`` - a Patch or a Face or a finite iterable of faces 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face, Patch 

sage: P = Patch([Face([0,0,0], 1), Face([0,0,0], 2)]) 

sage: Q = P.translate([1,-1,0]) 

sage: P + Q 

Patch: [[(0, 0, 0), 1]*, [(0, 0, 0), 2]*, [(1, -1, 0), 1]*, [(1, -1, 0), 2]*] 

sage: P + Face([0,0,0],3) 

Patch: [[(0, 0, 0), 1]*, [(0, 0, 0), 2]*, [(0, 0, 0), 3]*] 

sage: P + [Face([0,0,0],3), Face([1,1,1],2)] 

Patch: [[(0, 0, 0), 1]*, [(0, 0, 0), 2]*, [(0, 0, 0), 3]*, [(1, 1, 1), 2]*] 

""" 

return self.union(other) 

 

def __sub__(self, other): 

r""" 

Subtraction of patches (difference). 

 

INPUT: 

 

- ``other`` - a Patch or a Face or a finite iterable of faces 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face, Patch 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: P - Face([0,0,0],2) 

Patch: [[(0, 0, 0), 1]*, [(0, 0, 0), 3]*] 

sage: P - P 

Patch: [] 

""" 

return self.difference(other) 

 

def __repr__(self): 

r""" 

String representation of a patch. 

 

Displays all the faces if there less than 20, 

otherwise displays only the number of faces. 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face, Patch 

sage: x = [Face((0,0,0),t) for t in [1,2,3]] 

sage: P = Patch(x) 

sage: P 

Patch: [[(0, 0, 0), 1]*, [(0, 0, 0), 2]*, [(0, 0, 0), 3]*] 

 

:: 

 

sage: x = [Face((0,0,a),1) for a in range(25)] 

sage: P = Patch(x) 

sage: P 

Patch of 25 faces 

""" 

if len(self) <= 20: 

L = list(self) 

L.sort(key=lambda x : (x.vector(),x.type())) 

return "Patch: %s"%L 

else: 

return "Patch of %s faces"%len(self) 

 

def union(self, other): 

r""" 

Return a Patch consisting of the union of self and other. 

 

INPUT: 

 

- ``other`` - a Patch or a Face or a finite iterable of faces 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face, Patch 

sage: P = Patch([Face((0,0,0),1), Face((0,0,0),2)]) 

sage: P.union(Face((1,2,3), 3)) 

Patch: [[(0, 0, 0), 1]*, [(0, 0, 0), 2]*, [(1, 2, 3), 3]*] 

sage: P.union([Face((1,2,3), 3), Face((2,3,3), 2)]) 

Patch: [[(0, 0, 0), 1]*, [(0, 0, 0), 2]*, [(1, 2, 3), 3]*, [(2, 3, 3), 2]*] 

""" 

if isinstance(other, Face): 

return Patch(self._faces.union([other])) 

else: 

return Patch(self._faces.union(other)) 

 

def difference(self, other): 

r""" 

Return the difference of self and other. 

 

INPUT: 

 

- ``other`` - a finite iterable of faces or a single face 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face, Patch 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: P.difference(Face([0,0,0],2)) 

Patch: [[(0, 0, 0), 1]*, [(0, 0, 0), 3]*] 

sage: P.difference(P) 

Patch: [] 

""" 

if isinstance(other, Face): 

return Patch(self._faces.difference([other])) 

else: 

return Patch(self._faces.difference(other)) 

 

def dimension(self): 

r""" 

Return the dimension of the vectors of the faces of self 

 

It returns ``None`` if self is the empty patch. 

 

The dimension of a patch is the length of the vectors of the faces in the patch, 

which is assumed to be the same for every face in the patch. 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face, Patch 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: P.dimension() 

3 

 

TESTS:: 

 

sage: from sage.combinat.e_one_star import Patch 

sage: p = Patch([]) 

sage: p.dimension() is None 

True 

 

It works when the patch is created from an iterator:: 

 

sage: p = Patch(Face((0,0,0),t) for t in [1,2,3]) 

sage: p.dimension() 

3 

""" 

return self._dimension 

 

def faces_of_vector(self, v): 

r""" 

Return a list of the faces whose vector is ``v``. 

 

INPUT: 

 

- ``v`` - a vector 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face, Patch 

sage: P = Patch([Face((0,0,0),1), Face((1,2,0),3), Face((1,2,0),1)]) 

sage: P.faces_of_vector([1,2,0]) 

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

""" 

v = vector(v) 

return [f for f in self if f.vector() == v] 

 

def faces_of_type(self, t): 

r""" 

Return a list of the faces that have type ``t``. 

 

INPUT: 

 

- ``t`` - integer or any other type 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face, Patch 

sage: P = Patch([Face((0,0,0),1), Face((1,2,0),3), Face((1,2,0),1)]) 

sage: P.faces_of_type(1) 

[[(0, 0, 0), 1]*, [(1, 2, 0), 1]*] 

""" 

return [f for f in self if f.type() == t] 

 

def faces_of_color(self, color): 

r""" 

Return a list of the faces that have the given color. 

 

INPUT: 

 

- ``color`` - color 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face, Patch 

sage: P = Patch([Face((0,0,0),1, 'red'), Face((1,2,0),3, 'blue'), Face((1,2,0),1, 'red')]) 

sage: P.faces_of_color('red') 

[[(0, 0, 0), 1]*, [(1, 2, 0), 1]*] 

""" 

color = tuple(Color(color)) 

return [f for f in self if tuple(f.color()) == color] 

 

def translate(self, v): 

r""" 

Return a translated copy of self by vector ``v``. 

 

INPUT: 

 

- ``v`` - vector or tuple 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face, Patch 

sage: P = Patch([Face((0,0,0),1), Face((1,2,0),3), Face((1,2,0),1)]) 

sage: P.translate([-1,-2,0]) 

Patch: [[(-1, -2, 0), 1]*, [(0, 0, 0), 1]*, [(0, 0, 0), 3]*] 

""" 

v = vector(v) 

return Patch(Face(f.vector()+v, f.type(), f.color()) for f in self) 

 

def occurrences_of(self, other): 

r""" 

Return all positions at which other appears in self, that is, 

all vectors v such that ``set(other.translate(v)) <= set(self)``. 

 

INPUT: 

 

- ``other`` - a Patch 

 

OUTPUT: 

 

a list of vectors 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import Face, Patch, E1Star 

sage: P = Patch([Face([0,0,0], 1), Face([0,0,0], 2), Face([0,0,0], 3)]) 

sage: Q = Patch([Face([0,0,0], 1), Face([0,0,0], 2)]) 

sage: P.occurrences_of(Q) 

[(0, 0, 0)] 

sage: Q = Q.translate([1,2,3]) 

sage: P.occurrences_of(Q) 

[(-1, -2, -3)] 

 

:: 

 

sage: E = E1Star(WordMorphism({1:[1,2], 2:[1,3], 3:[1]})) 

sage: P = Patch([Face([0,0,0], 1), Face([0,0,0], 2), Face([0,0,0], 3)]) 

sage: P = E(P,4) 

sage: Q = Patch([Face([0,0,0], 1), Face([0,0,0], 2)]) 

sage: L = P.occurrences_of(Q) 

sage: sorted(L) 

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

""" 

f0 = next(iter(other)) 

x = f0.vector() 

t = f0.type() 

L = self.faces_of_type(t) 

positions = [] 

for f in L: 

y = f.vector() 

if other.translate(y-x)._faces.issubset(self._faces): 

positions.append(y-x) 

return positions 

 

def repaint(self, cmap='Set1'): 

r""" 

Repaints all the faces of self from the given color map. 

 

This only changes the colors of the faces of self. 

 

INPUT: 

 

- ``cmap`` - color map (default: ``'Set1'``). It can be one of the 

following : 

 

- string -- A coloring map. For available coloring map names type: 

``sorted(colormaps)`` 

- list -- a list of colors to assign cyclically to the faces. 

A list of a single color colors all the faces with the same color. 

- dict -- a dict of face types mapped to colors, to color the 

faces according to their type. 

- ``{}``, the empty dict - shortcut for 

``{1:'red', 2:'green', 3:'blue'}``. 

 

EXAMPLES: 

 

Using a color map:: 

 

sage: from sage.combinat.e_one_star import Face, Patch 

sage: color = (0, 0, 0) 

sage: P = Patch([Face((0,0,0),t,color) for t in [1,2,3]]) 

sage: for f in P: f.color() 

RGB color (0.0, 0.0, 0.0) 

RGB color (0.0, 0.0, 0.0) 

RGB color (0.0, 0.0, 0.0) 

sage: P.repaint() 

sage: next(iter(P)).color() #random 

RGB color (0.498..., 0.432..., 0.522...) 

 

Using a list of colors:: 

 

sage: P = Patch([Face((0,0,0),t,color) for t in [1,2,3]]) 

sage: P.repaint([(0.9, 0.9, 0.9), (0.65,0.65,0.65), (0.4,0.4,0.4)]) 

sage: for f in P: f.color() 

RGB color (0.9, 0.9, 0.9) 

RGB color (0.65, 0.65, 0.65) 

RGB color (0.4, 0.4, 0.4) 

 

Using a dictionary to color faces according to their type:: 

 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: P.repaint({1:'black', 2:'yellow', 3:'green'}) 

sage: P.plot() #not tested 

sage: P.repaint({}) 

sage: P.plot() #not tested 

""" 

if cmap == {}: 

cmap = {1: 'red', 2:'green', 3:'blue'} 

 

if isinstance(cmap, dict): 

for f in self: 

f.color(cmap[f.type()]) 

 

elif isinstance(cmap, list): 

L = len(cmap) 

for i, f in enumerate(self): 

f.color(cmap[i % L]) 

 

elif isinstance(cmap, str): 

# matplotlib color maps 

global cm 

if not cm: 

from matplotlib import cm 

 

if not cmap in cm.datad: 

raise RuntimeError("Color map %s not known (type sorted(colors) for valid names)" % cmap) 

cmap = cm.__dict__[cmap] 

dim = float(len(self)) 

for i,f in enumerate(self): 

f.color(cmap(i/dim)[:3]) 

 

else: 

raise TypeError("Type of cmap (=%s) must be dict, list or str" %cmap) 

 

def plot(self, projmat=None, opacity=0.75): 

r""" 

Return a 2D graphic object depicting the patch. 

 

INPUT: 

 

- ``projmat`` - matrix (optional, default: ``None``) the projection 

matrix. Its number of lines must be two. Its number of columns 

must equal the dimension of the ambient space of the faces. If 

``None``, the isometric projection is used by default. 

 

- ``opacity`` - float between ``0`` and ``1`` (optional, default: ``0.75``) 

opacity of the face 

 

.. WARNING:: 

 

Plotting is implemented only for patches in two or three dimensions. 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import E1Star, Face, Patch 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: P.plot() 

Graphics object consisting of 3 graphics primitives 

 

:: 

 

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1]}) 

sage: E = E1Star(sigma) 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: P = E(P, 5) 

sage: P.plot() 

Graphics object consisting of 57 graphics primitives 

 

Plot with a different projection matrix:: 

 

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1]}) 

sage: E = E1Star(sigma) 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: M = matrix(2, 3, [1,0,-1,0.3,1,-3]) 

sage: P = E(P, 3) 

sage: P.plot(projmat=M) 

Graphics object consisting of 17 graphics primitives 

 

Plot patches made of unit segments:: 

 

sage: P = Patch([Face([0,0], 1), Face([0,0], 2)]) 

sage: E = E1Star(WordMorphism({1:[1,2],2:[1]})) 

sage: F = E1Star(WordMorphism({1:[1,1,2],2:[2,1]})) 

sage: E(P,5).plot() 

Graphics object consisting of 21 graphics primitives 

sage: F(P,3).plot() 

Graphics object consisting of 34 graphics primitives 

""" 

if self.dimension() == 2: 

G = Graphics() 

for face in self: 

G += face._plot(None, None, 1) 

G.set_aspect_ratio(1) 

return G 

 

if self.dimension() == 3: 

if projmat is None: 

projmat = matrix(2, [-1.7320508075688772*0.5, 1.7320508075688772*0.5, 0, -0.5, -0.5, 1]) 

 

G = Graphics() 

for face in self: 

G += face._plot(projmat, self._face_contour, opacity) 

G.set_aspect_ratio(1) 

return G 

 

else: 

raise NotImplementedError("Plotting is implemented only for patches in two or three dimensions.") 

 

def plot3d(self): 

r""" 

Return a 3D graphics object depicting the patch. 

 

.. WARNING:: 

 

3D plotting is implemented only for patches in three dimensions. 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import E1Star, Face, Patch 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: P.plot3d() #not tested 

 

:: 

 

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1]}) 

sage: E = E1Star(sigma) 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: P = E(P, 5) 

sage: P.repaint() 

sage: P.plot3d() #not tested 

""" 

if self.dimension() != 3: 

raise NotImplementedError("3D plotting is implemented only for patches in three dimensions.") 

 

face_list = [face._plot3d(self._face_contour) for face in self] 

G = sum(face_list) 

return G 

 

def plot_tikz(self, projmat=None, print_tikz_env=True, edgecolor='black', 

scale=0.25, drawzero=False, extra_code_before='', extra_code_after=''): 

r""" 

Return a string containing some TikZ code to be included into 

a LaTeX document, depicting the patch. 

 

.. WARNING:: 

 

Tikz Plotting is implemented only for patches in three dimensions. 

 

INPUT: 

 

- ``projmat`` - matrix (optional, default: ``None``) the projection 

matrix. Its number of lines must be two. Its number of columns 

must equal the dimension of the ambient space of the faces. If 

``None``, the isometric projection is used by default. 

- ``print_tikz_env`` - bool (optional, default: ``True``) if ``True``, 

the tikzpicture environment are printed 

- ``edgecolor`` - string (optional, default: ``'black'``) either 

``'black'`` or ``'facecolor'`` (color of unit face edges) 

- ``scale`` - real number (optional, default: ``0.25``) scaling 

constant for the whole figure 

- ``drawzero`` - bool (optional, default: ``False``) if ``True``, 

mark the origin by a black dot 

- ``extra_code_before`` - string (optional, default: ``''``) extra code to 

include in the tikz picture 

- ``extra_code_after`` - string (optional, default: ``''``) extra code to 

include in the tikz picture 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import E1Star, Face, Patch 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: s = P.plot_tikz() 

sage: len(s) 

602 

sage: print(s) #not tested 

\begin{tikzpicture} 

[x={(-0.216506cm,-0.125000cm)}, y={(0.216506cm,-0.125000cm)}, z={(0.000000cm,0.250000cm)}] 

\definecolor{facecolor}{rgb}{0.000,1.000,0.000} 

\fill[fill=facecolor, draw=black, shift={(0,0,0)}] 

(0, 0, 0) -- (0, 0, 1) -- (1, 0, 1) -- (1, 0, 0) -- cycle; 

\definecolor{facecolor}{rgb}{1.000,0.000,0.000} 

\fill[fill=facecolor, draw=black, shift={(0,0,0)}] 

(0, 0, 0) -- (0, 1, 0) -- (0, 1, 1) -- (0, 0, 1) -- cycle; 

\definecolor{facecolor}{rgb}{0.000,0.000,1.000} 

\fill[fill=facecolor, draw=black, shift={(0,0,0)}] 

(0, 0, 0) -- (1, 0, 0) -- (1, 1, 0) -- (0, 1, 0) -- cycle; 

\end{tikzpicture} 

 

:: 

 

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1]}) 

sage: E = E1Star(sigma) 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: P = E(P, 4) 

sage: from sage.misc.latex import latex #not tested 

sage: latex.add_to_preamble('\\usepackage{tikz}') #not tested 

sage: view(P) #not tested 

 

Plot using shades of gray (useful for article figures):: 

 

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1]}) 

sage: E = E1Star(sigma) 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: P.repaint([(0.9, 0.9, 0.9), (0.65,0.65,0.65), (0.4,0.4,0.4)]) 

sage: P = E(P, 4) 

sage: s = P.plot_tikz() 

 

Plotting with various options:: 

 

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1]}) 

sage: E = E1Star(sigma) 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: M = matrix(2,3,[float(u) for u in [1,0,-0.7071,0,1,-0.7071]]) 

sage: P = E(P, 3) 

sage: s = P.plot_tikz(projmat=M, edgecolor='facecolor', scale=0.6, drawzero=True) 

 

Adding X, Y, Z axes using the extra code feature:: 

 

sage: length = 1.5 

sage: space = 0.3 

sage: axes = '' 

sage: axes += "\\draw[->, thick, black] (0,0,0) -- (%s, 0, 0);\n" % length 

sage: axes += "\\draw[->, thick, black] (0,0,0) -- (0, %s, 0);\n" % length 

sage: axes += "\\node at (%s,0,0) {$x$};\n" % (length + space) 

sage: axes += "\\node at (0,%s,0) {$y$};\n" % (length + space) 

sage: axes += "\\node at (0,0,%s) {$z$};\n" % (length + space) 

sage: axes += "\\draw[->, thick, black] (0,0,0) -- (0, 0, %s);\n" % length 

sage: cube = Patch([Face((0,0,0),1), Face((0,0,0),2), Face((0,0,0),3)]) 

sage: options = dict(scale=0.5,drawzero=True,extra_code_before=axes) 

sage: s = cube.plot_tikz(**options) 

sage: len(s) 

986 

sage: print(s) #not tested 

\begin{tikzpicture} 

[x={(-0.433013cm,-0.250000cm)}, y={(0.433013cm,-0.250000cm)}, z={(0.000000cm,0.500000cm)}] 

\draw[->, thick, black] (0,0,0) -- (1.50000000000000, 0, 0); 

\draw[->, thick, black] (0,0,0) -- (0, 1.50000000000000, 0); 

\node at (1.80000000000000,0,0) {$x$}; 

\node at (0,1.80000000000000,0) {$y$}; 

\node at (0,0,1.80000000000000) {$z$}; 

\draw[->, thick, black] (0,0,0) -- (0, 0, 1.50000000000000); 

\definecolor{facecolor}{rgb}{0.000,1.000,0.000} 

\fill[fill=facecolor, draw=black, shift={(0,0,0)}] 

(0, 0, 0) -- (0, 0, 1) -- (1, 0, 1) -- (1, 0, 0) -- cycle; 

\definecolor{facecolor}{rgb}{1.000,0.000,0.000} 

\fill[fill=facecolor, draw=black, shift={(0,0,0)}] 

(0, 0, 0) -- (0, 1, 0) -- (0, 1, 1) -- (0, 0, 1) -- cycle; 

\definecolor{facecolor}{rgb}{0.000,0.000,1.000} 

\fill[fill=facecolor, draw=black, shift={(0,0,0)}] 

(0, 0, 0) -- (1, 0, 0) -- (1, 1, 0) -- (0, 1, 0) -- cycle; 

\node[circle,fill=black,draw=black,minimum size=1.5mm,inner sep=0pt] at (0,0,0) {}; 

\end{tikzpicture} 

""" 

if self.dimension() != 3: 

raise NotImplementedError("Tikz Plotting is implemented only for patches in three dimensions.") 

 

if projmat is None: 

projmat = matrix(2, [-1.7320508075688772*0.5, 1.7320508075688772*0.5, 0, -0.5, -0.5, 1])*scale 

 

e1 = projmat*vector([1,0,0]) 

e2 = projmat*vector([0,1,0]) 

e3 = projmat*vector([0,0,1]) 

face_contour = self._face_contour 

color = () 

 

# string s contains the TiKZ code of the patch 

s = '' 

 

if print_tikz_env: 

s += '\\begin{tikzpicture}\n' 

s += '[x={(%fcm,%fcm)}, y={(%fcm,%fcm)}, z={(%fcm,%fcm)}]\n'%(e1[0], e1[1], e2[0], e2[1], e3[0], e3[1]) 

 

s += extra_code_before 

 

for f in self: 

t = f.type() 

x, y, z = f.vector() 

 

if tuple(color) != tuple(f.color()): #tuple is needed, comparison for RGB fails 

color = f.color() 

s += '\\definecolor{facecolor}{rgb}{%.3f,%.3f,%.3f}\n'%(color[0], color[1], color[2]) 

 

s += '\\fill[fill=facecolor, draw=%s, shift={(%d,%d,%d)}]\n'%(edgecolor, x, y, z) 

s += ' -- '.join(map(str, face_contour[t])) + ' -- cycle;\n' 

 

s += extra_code_after 

 

if drawzero: 

s += '\\node[circle,fill=black,draw=black,minimum size=1.5mm,inner sep=0pt] at (0,0,0) {};\n' 

 

if print_tikz_env: 

s += '\\end{tikzpicture}' 

 

return LatexExpr(s) 

 

_latex_ = plot_tikz 

 

class E1Star(SageObject): 

r""" 

A class to model the `E_1^*(\sigma)` map associated with 

a unimodular substitution `\sigma`. 

 

INPUT: 

 

- ``sigma`` - unimodular ``WordMorphism``, i.e. such that its incidence 

matrix has determinant `\pm 1`. 

 

- ``method`` - 'prefix' or 'suffix' (optional, default: 'suffix') 

Enables to use an alternative definition `E_1^*(\sigma)` substitutions, 

where the abelianized of the prefix` is used instead of the suffix. 

 

.. NOTE:: 

 

The alphabet of the domain and the codomain of `\sigma` must be 

equal, and they must be of the form ``[1, ..., d]``, where ``d`` 

is a positive integer corresponding to the length of the vectors 

of the faces on which `E_1^*(\sigma)` will act. 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import E1Star, Face, Patch 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1]}) 

sage: E = E1Star(sigma) 

sage: E(P) 

Patch: [[(0, 0, 0), 1]*, [(0, 0, 0), 2]*, [(0, 0, 0), 3]*, [(0, 1, -1), 2]*, [(1, 0, -1), 1]*] 

 

:: 

 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1]}) 

sage: E = E1Star(sigma, method='prefix') 

sage: E(P) 

Patch: [[(0, 0, 0), 1]*, [(0, 0, 0), 2]*, [(0, 0, 0), 3]*, [(0, 0, 1), 1]*, [(0, 0, 1), 2]*] 

 

:: 

 

sage: x = [Face((0,0,0,0),1), Face((0,0,0,0),4)] 

sage: P = Patch(x) 

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1,4], 4:[1]}) 

sage: E = E1Star(sigma) 

sage: E(P) 

Patch: [[(0, 0, 0, 0), 3]*, [(0, 0, 0, 0), 4]*, [(0, 0, 1, -1), 3]*, [(0, 1, 0, -1), 2]*, [(1, 0, 0, -1), 1]*] 

""" 

def __init__(self, sigma, method='suffix'): 

r""" 

E1Star constructor. See class doc for more information. 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import E1Star, Face, Patch 

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1]}) 

sage: E = E1Star(sigma) 

sage: E 

E_1^*(1->12, 2->13, 3->1) 

""" 

if not isinstance(sigma, WordMorphism): 

raise TypeError("sigma (=%s) must be an instance of WordMorphism"%sigma) 

 

if sigma.domain().alphabet() != sigma.codomain().alphabet(): 

raise ValueError("The domain and codomain of (%s) must be the same."%sigma) 

 

if abs(det(matrix(sigma))) != 1: 

raise ValueError("The substitution (%s) must be unimodular."%sigma) 

 

first_letter = sigma.codomain().alphabet()[0] 

if not (first_letter in ZZ) or (first_letter < 1): 

raise ValueError("The substitution (%s) must be defined on positive integers."%sigma) 

 

self._sigma = WordMorphism(sigma) 

self._d = self._sigma.domain().alphabet().cardinality() 

 

# self._base_iter is a base for the iteration of the application of self on set 

# of faces. (Exploits the linearity of `E_1^*(\sigma)` to optimize computation.) 

alphabet = self._sigma.domain().alphabet() 

X = {} 

for k in alphabet: 

subst_im = self._sigma.image(k) 

for n, letter in enumerate(subst_im): 

if method == 'suffix': 

image_word = subst_im[n+1:] 

elif method == 'prefix': 

image_word = subst_im[:n] 

else: 

raise ValueError("Option 'method' can only be 'prefix' or 'suffix'.") 

if not letter in X: 

X[letter] = [] 

v = self.inverse_matrix()*vector(image_word.abelian_vector()) 

X[letter].append((v, k)) 

self._base_iter = X 

 

def __eq__(self, other): 

r""" 

Equality test for E1Star morphisms. 

 

INPUT: 

 

- ``other`` - an object 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import E1Star, Face, Patch 

sage: s = WordMorphism({1:[1,3], 2:[1,2,3], 3:[3]}) 

sage: t = WordMorphism({1:[1,2,3], 2:[2,3], 3:[3]}) 

sage: S = E1Star(s) 

sage: T = E1Star(t) 

sage: S == T 

False 

sage: S2 = E1Star(s, method='prefix') 

sage: S == S2 

False 

""" 

return (isinstance(other, E1Star) and self._base_iter == other._base_iter) 

 

def __call__(self, patch, iterations=1): 

r""" 

Applies a generalized substitution to a Patch; this returns a new object. 

 

The color of every new face in the image is given the same color as its preimage. 

 

INPUT: 

 

- ``patch`` - a patch 

- ``iterations`` - integer (optional, default: 1) number of iterations 

 

OUTPUT: 

 

a patch 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import E1Star, Face, Patch 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1]}) 

sage: E = E1Star(sigma) 

sage: E(P) 

Patch: [[(0, 0, 0), 1]*, [(0, 0, 0), 2]*, [(0, 0, 0), 3]*, [(0, 1, -1), 2]*, [(1, 0, -1), 1]*] 

sage: E(P, iterations=4) 

Patch of 31 faces 

 

TESTS: 

 

We test that iterations=0 works (see :trac:`10699`):: 

 

sage: P = Patch([Face((0,0,0),t) for t in [1,2,3]]) 

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1]}) 

sage: E = E1Star(sigma) 

sage: E(P, iterations=0) 

Patch: [[(0, 0, 0), 1]*, [(0, 0, 0), 2]*, [(0, 0, 0), 3]*] 

""" 

if iterations == 0: 

return Patch(patch) 

elif iterations < 0: 

raise ValueError("iterations (=%s) must be >= 0." % iterations) 

else: 

old_faces = patch 

for i in range(iterations): 

new_faces = [] 

for f in old_faces: 

new_faces.extend(self._call_on_face(f, color=f.color())) 

old_faces = new_faces 

return Patch(new_faces) 

 

def __mul__(self, other): 

r""" 

Return the product of self and other. 

 

The product satisfies the following rule : 

`E_1^*(\sigma\circ\sigma') = E_1^*(\sigma')` \circ E_1^*(\sigma)` 

 

INPUT: 

 

- ``other`` - an instance of E1Star 

 

OUTPUT: 

 

an instance of E1Star 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import E1Star, Face, Patch 

sage: s = WordMorphism({1:[2],2:[3],3:[1,2]}) 

sage: t = WordMorphism({1:[1,3,1],2:[1],3:[1,1,3,2]}) 

sage: E1Star(s) * E1Star(t) 

E_1^*(1->1, 2->1132, 3->1311) 

sage: E1Star(t * s) 

E_1^*(1->1, 2->1132, 3->1311) 

""" 

if not isinstance(other, E1Star): 

raise TypeError("other (=%s) must be an instance of E1Star" % other) 

return E1Star(other.sigma() * self.sigma()) 

 

def __repr__(self): 

r""" 

String representation of a patch. 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import E1Star, Face, Patch 

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1]}) 

sage: E = E1Star(sigma) 

sage: E 

E_1^*(1->12, 2->13, 3->1) 

""" 

return "E_1^*(%s)" % str(self._sigma) 

 

def _call_on_face(self, face, color=None): 

r""" 

Return an iterator of faces obtained by applying self on the face. 

 

INPUT: 

 

- ``face`` - a face 

- ``color`` - string, RGB tuple or color, (optional, default: None) 

RGB color 

 

OUTPUT: 

 

iterator of faces 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import E1Star, Face, Patch 

sage: f = Face((0,2,0), 1) 

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1]}) 

sage: E = E1Star(sigma) 

sage: list(E._call_on_face(f)) 

[[(3, 0, -3), 1]*, [(2, 1, -3), 2]*, [(2, 0, -2), 3]*] 

""" 

if len(face.vector()) != self._d: 

raise ValueError("The dimension of the faces must be equal to the size of the alphabet of the substitution.") 

x_new = self.inverse_matrix() * face.vector() 

t = face.type() 

return (Face(x_new + v, k, color=color) for v, k in self._base_iter[t]) 

 

@cached_method 

def matrix(self): 

r""" 

Return the matrix associated with self. 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import E1Star, Face, Patch 

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1]}) 

sage: E = E1Star(sigma) 

sage: E.matrix() 

[1 1 1] 

[1 0 0] 

[0 1 0] 

""" 

return self._sigma.incidence_matrix() 

 

@cached_method 

def inverse_matrix(self): 

r""" 

Return the inverse of the matrix associated with self. 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import E1Star, Face, Patch 

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1]}) 

sage: E = E1Star(sigma) 

sage: E.inverse_matrix() 

[ 0 1 0] 

[ 0 0 1] 

[ 1 -1 -1] 

 

""" 

return self.matrix().inverse() 

 

def sigma(self): 

r""" 

Return the ``WordMorphism`` associated with self. 

 

EXAMPLES:: 

 

sage: from sage.combinat.e_one_star import E1Star, Face, Patch 

sage: sigma = WordMorphism({1:[1,2], 2:[1,3], 3:[1]}) 

sage: E = E1Star(sigma) 

sage: E.sigma() 

WordMorphism: 1->12, 2->13, 3->1 

""" 

return self._sigma