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

1715

1716

1717

1718

1719

1720

1721

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

1952

1953

1954

1955

1956

1957

1958

1959

1960

1961

1962

1963

1964

1965

1966

1967

1968

1969

1970

1971

1972

1973

1974

1975

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

1986

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1998

1999

2000

2001

2002

2003

2004

2005

2006

2007

2008

2009

2010

2011

2012

2013

2014

2015

2016

2017

2018

2019

2020

2021

2022

2023

2024

2025

2026

2027

2028

2029

2030

2031

2032

2033

2034

2035

2036

2037

2038

2039

2040

2041

2042

2043

2044

2045

2046

2047

2048

2049

2050

2051

2052

2053

2054

2055

2056

2057

2058

2059

2060

2061

2062

2063

2064

2065

2066

2067

2068

2069

2070

2071

2072

2073

2074

2075

2076

2077

2078

2079

2080

2081

2082

2083

2084

2085

2086

2087

2088

2089

2090

2091

2092

2093

2094

2095

2096

2097

2098

2099

2100

2101

2102

2103

2104

2105

2106

2107

2108

2109

2110

2111

2112

2113

2114

2115

2116

2117

2118

2119

2120

2121

2122

2123

2124

2125

2126

2127

2128

2129

2130

2131

2132

2133

2134

2135

2136

2137

2138

2139

2140

2141

2142

2143

2144

2145

2146

2147

2148

2149

2150

2151

2152

2153

2154

2155

2156

2157

2158

2159

2160

2161

2162

2163

2164

2165

2166

2167

2168

2169

2170

2171

2172

2173

2174

2175

2176

2177

2178

2179

2180

2181

2182

2183

2184

2185

2186

2187

2188

2189

2190

2191

2192

2193

2194

2195

2196

2197

2198

2199

2200

2201

2202

2203

2204

2205

2206

2207

2208

2209

2210

2211

2212

2213

2214

2215

2216

2217

2218

2219

2220

2221

2222

2223

2224

2225

2226

2227

2228

2229

2230

2231

2232

2233

2234

2235

2236

2237

2238

2239

2240

2241

2242

2243

2244

2245

2246

2247

2248

2249

2250

2251

2252

2253

2254

2255

2256

2257

2258

2259

2260

2261

2262

2263

2264

2265

2266

2267

2268

2269

2270

2271

2272

2273

2274

2275

2276

2277

2278

2279

2280

2281

2282

2283

2284

2285

2286

2287

2288

2289

2290

2291

2292

2293

2294

2295

2296

2297

2298

2299

2300

2301

2302

2303

2304

2305

2306

2307

2308

2309

2310

2311

2312

2313

2314

2315

2316

2317

2318

2319

2320

2321

2322

2323

2324

2325

2326

2327

2328

2329

2330

2331

2332

2333

2334

2335

2336

2337

2338

2339

2340

2341

2342

2343

2344

2345

2346

2347

2348

2349

2350

2351

2352

2353

2354

2355

2356

2357

2358

2359

2360

2361

2362

2363

2364

2365

2366

2367

2368

2369

2370

2371

2372

2373

2374

2375

2376

2377

2378

2379

2380

2381

2382

2383

2384

2385

2386

2387

2388

2389

2390

2391

2392

2393

2394

2395

2396

2397

2398

2399

2400

2401

2402

2403

2404

2405

2406

2407

2408

2409

2410

2411

2412

2413

2414

2415

2416

2417

2418

2419

2420

2421

2422

2423

2424

2425

2426

2427

2428

2429

2430

2431

2432

2433

2434

2435

2436

2437

2438

2439

2440

2441

2442

2443

2444

2445

2446

2447

2448

2449

2450

2451

2452

2453

2454

2455

2456

2457

2458

2459

2460

2461

2462

2463

2464

2465

2466

2467

2468

2469

2470

2471

2472

2473

2474

2475

2476

2477

2478

2479

2480

2481

2482

2483

2484

2485

2486

2487

2488

2489

2490

2491

2492

2493

2494

2495

2496

2497

2498

2499

2500

2501

2502

2503

2504

2505

2506

2507

2508

2509

2510

2511

2512

2513

2514

2515

2516

2517

2518

2519

2520

2521

2522

2523

2524

2525

2526

2527

2528

2529

2530

2531

2532

2533

2534

2535

2536

2537

2538

2539

2540

2541

2542

2543

2544

2545

2546

2547

2548

2549

2550

2551

2552

2553

2554

2555

2556

2557

2558

2559

2560

2561

2562

2563

2564

2565

2566

2567

2568

2569

2570

2571

2572

2573

2574

2575

2576

2577

2578

2579

2580

2581

2582

2583

2584

2585

2586

2587

2588

2589

2590

2591

2592

2593

2594

2595

2596

2597

2598

2599

2600

2601

2602

2603

2604

2605

2606

2607

2608

2609

2610

2611

2612

2613

2614

2615

2616

2617

2618

2619

2620

2621

2622

2623

2624

2625

2626

2627

2628

2629

2630

2631

2632

2633

2634

2635

2636

2637

2638

2639

2640

2641

2642

2643

2644

2645

2646

2647

2648

2649

2650

2651

2652

2653

2654

2655

2656

2657

2658

2659

2660

2661

2662

2663

2664

2665

2666

2667

2668

2669

2670

2671

2672

2673

2674

2675

2676

2677

2678

2679

2680

2681

2682

2683

2684

2685

2686

2687

2688

2689

2690

2691

2692

2693

2694

2695

2696

2697

2698

2699

2700

2701

2702

2703

2704

2705

2706

2707

2708

2709

2710

2711

2712

2713

2714

2715

2716

2717

2718

2719

2720

2721

2722

2723

2724

2725

2726

2727

2728

2729

2730

2731

2732

2733

2734

2735

2736

2737

2738

2739

2740

2741

2742

2743

2744

2745

2746

2747

2748

2749

2750

2751

2752

2753

2754

2755

2756

2757

2758

2759

2760

2761

2762

2763

2764

2765

2766

2767

2768

2769

2770

2771

2772

2773

2774

2775

2776

2777

2778

2779

2780

2781

2782

2783

2784

2785

2786

2787

# -*- coding: utf-8 -*- 

r""" 

Word paths 

 

This module implements word paths, which is an application of Combinatorics 

on Words to Discrete Geometry. A word path is the representation of a word 

as a discrete path in a vector space using a one-to-one correspondence 

between the alphabet and a set of vectors called steps. Many problems 

surrounding 2d lattice polygons (such as questions of self-intersection, 

area, inertia moment, etc.) can be solved in linear time (linear in the 

length of the perimeter) using theory from Combinatorics on Words. 

 

On the square grid, the encoding of a path using a four-letter alphabet 

(for East, North, West and South directions) is also known as the Freeman 

chain code [1,2] (see [3] for further reading). 

 

AUTHORS: 

 

- Arnaud Bergeron (2008) : Initial version, path on the square grid 

 

- Sebastien Labbe (2009-01-14) : New classes and hierarchy, doc and functions. 

 

EXAMPLES: 

 

The combinatorial class of all paths defined over three given steps:: 

 

sage: P = WordPaths('abc', steps=[(1,2), (-3,4), (0,-3)]); P 

Word Paths over 3 steps 

 

This defines a one-to-one correspondence between alphabet and steps:: 

 

sage: d = P.letters_to_steps() 

sage: sorted(d.items()) 

[('a', (1, 2)), ('b', (-3, 4)), ('c', (0, -3))] 

 

Creation of a path from the combinatorial class P defined above:: 

 

sage: p = P('abaccba'); p 

Path: abaccba 

 

Many functions can be used on p: the coordinates of its trajectory, 

ask whether p is a closed path, plot it and many other:: 

 

sage: list(p.points()) 

[(0, 0), (1, 2), (-2, 6), (-1, 8), (-1, 5), (-1, 2), (-4, 6), (-3, 8)] 

sage: p.is_closed() 

False 

sage: p.plot() 

Graphics object consisting of 3 graphics primitives 

 

To obtain a list of all the available word path specific functions, 

use ``help(p)``:: 

 

sage: help(p) 

Help on FiniteWordPath_2d_str in module sage.combinat.words.paths object: 

... 

Methods inherited from FiniteWordPath_2d: 

... 

Methods inherited from FiniteWordPath_all: 

... 

 

Since p is a finite word, many functions from the word library are available:: 

 

sage: p.crochemore_factorization() 

(a, b, a, c, c, ba) 

sage: p.is_palindrome() 

False 

sage: p[:3] 

Path: aba 

sage: len(p) 

7 

 

P also herits many functions from Words:: 

 

sage: P = WordPaths('rs', steps=[(1,2), (-1,4)]); P 

Word Paths over 2 steps 

sage: P.alphabet() 

{'r', 's'} 

sage: list(P.iterate_by_length(3)) 

[Path: rrr, 

Path: rrs, 

Path: rsr, 

Path: rss, 

Path: srr, 

Path: srs, 

Path: ssr, 

Path: sss] 

 

When the number of given steps is half the size of alphabet, the 

opposite of vectors are used:: 

 

sage: P = WordPaths('abcd', [(1,0), (0,1)]) 

sage: sorted(P.letters_to_steps().items()) 

[('a', (1, 0)), ('b', (0, 1)), ('c', (-1, 0)), ('d', (0, -1))] 

 

Some built-in combinatorial classes of paths:: 

 

sage: P = WordPaths('abAB', steps='square_grid'); P 

Word Paths on the square grid 

 

:: 

 

sage: D = WordPaths('()', steps='dyck'); D 

Finite Dyck paths 

sage: d = D('()()()(())'); d 

Path: ()()()(()) 

sage: d.plot() 

Graphics object consisting of 3 graphics primitives 

 

:: 

 

sage: P = WordPaths('abcdef', steps='triangle_grid') 

sage: p = P('babaddefadabcadefaadfafabacdefa') 

sage: p.plot() 

Graphics object consisting of 3 graphics primitives 

 

Vector steps may be in more than 2 dimensions:: 

 

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

sage: P = WordPaths(alphabet='abc', steps=d); P 

Word Paths over 3 steps 

sage: p = P('abcabcabcabcaabacabcababcacbabacacabcaccbcac') 

sage: p.plot() 

Graphics3d Object 

 

:: 

 

sage: d = [(1,3,5,1), (-5,1,-6,0), (0,0,1,9), (4,2,-1,0)] 

sage: P = WordPaths(alphabet='rstu', steps=d); P 

Word Paths over 4 steps 

sage: p = P('rtusuusususuturrsust'); p 

Path: rtusuusususuturrsust 

sage: p.end_point() 

(5, 31, -26, 30) 

 

:: 

 

sage: CubePaths = WordPaths('abcABC', steps='cube_grid'); CubePaths 

Word Paths on the cube grid 

sage: CubePaths('abcabaabcabAAAAA').plot() 

Graphics3d Object 

 

The input data may be a str, a list, a tuple, 

a callable or a finite iterator:: 

 

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

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

Path: 012321232 

sage: P((0,1,2,3,2,1,2,3,2)) 

Path: 012321232 

sage: P(lambda n:n%4, length=10) 

Path: 0123012301 

sage: P(iter([0,3,2,1]), length='finite') 

Path: 0321 

 

REFERENCES: 

 

- [1] Freeman, H.: *On the encoding of arbitrary geometric configurations*. 

IRE Trans. Electronic Computer 10 (1961) 260-268. 

- [2] Freeman, H.: *Boundary encoding and processing*. In Lipkin, B., Rosenfeld, 

A., eds.: Picture Processing and Psychopictorics, Academic Press, New York 

(1970) 241-266. 

- [3] Braquelaire, J.P., Vialard, A.: *Euclidean paths: A new representation of 

boundary of discrete regions*. Graphical Models and Image Processing 61 (1999) 

16-43. 

- [4] :wikipedia:`Regular_tiling` 

- [5] :wikipedia:`Dyck_word` 

 

""" 

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

# Copyright (C) 2008 Arnaud bergeron <abergeron@gmail.coms>, 

# Copyright (C) 2009 Sebastien Labbe <slabqc@gmail.com>, 

# 

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

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

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

# (at your option) any later version. 

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

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

from __future__ import absolute_import 

 

from builtins import zip 

 

from sage.structure.sage_object import SageObject 

from sage.misc.cachefunc import cached_method 

from sage.misc.lazy_attribute import lazy_attribute 

from sage.combinat.words.words import FiniteWords 

from sage.combinat.words.word import FiniteWord_class 

from sage.combinat.words.alphabet import build_alphabet 

from sage.plot.all import arrow, line, polygon, point, Graphics 

from sage.modules.free_module_element import vector 

from sage.rings.all import ZZ, RR, QuadraticField 

from .word_datatypes import (WordDatatype_str, 

WordDatatype_list, 

WordDatatype_tuple) 

#WordDatatype_cpp_basic_string) 

 

from .word_infinite_datatypes import ( 

WordDatatype_iter_with_caching, 

WordDatatype_iter, 

WordDatatype_callable_with_caching, 

WordDatatype_callable) 

from sage.matrix.constructor import vector_on_axis_rotation_matrix 

 

####################################################################### 

# # 

# WordPaths function # 

# # 

####################################################################### 

 

def WordPaths(alphabet, steps=None): 

r""" 

Returns the combinatorial class of paths of the given type of steps. 

 

INPUT: 

 

- ``alphabet`` - ordered alphabet 

 

- ``steps`` - (default is None). It can be one of the following: 

 

- an iterable ordered container of as many vectors as there are 

letters in the alphabet. The vectors are associated to the letters 

according to their order in steps. The vectors can be a tuple or 

anything that can be passed to vector function. 

 

- an iterable ordered container of k vectors where k is half the 

size of alphabet. The vectors and their opposites are associated 

to the letters according to their order in steps (given vectors 

first, opposite vectors after). 

 

- ``None``: In this case, the type of steps are guessed from the 

length of alphabet. 

 

- 'square_grid' or 'square' : (default when size of alphabet is 4) 

The order is : East, North, West, South. 

 

- 'triangle_grid' or 'triangle': 

 

- 'hexagonal_grid' or 'hexagon' :(default when size of alphabet is 6) 

 

- 'cube_grid' or 'cube' : 

 

- 'north_east', 'ne' or 'NE' : (the default when size of alphabet is 2) 

 

- 'dyck': 

 

OUTPUT: 

 

- The combinatorial class of all paths of the given type. 

 

EXAMPLES: 

 

The steps can be given explicitely:: 

 

sage: WordPaths('abc', steps=[(1,2), (-1,4), (0,-3)]) 

Word Paths over 3 steps 

 

Different type of input alphabet:: 

 

sage: WordPaths(range(3), steps=[(1,2), (-1,4), (0,-3)]) 

Word Paths over 3 steps 

sage: WordPaths(['cric','crac','croc'], steps=[(1,2), (1,4), (0,3)]) 

Word Paths over 3 steps 

 

Directions can be in three dimensions as well:: 

 

sage: WordPaths('ab', steps=[(1,2,2),(-1,4,2)]) 

Word Paths over 2 steps 

 

When the number of given steps is half the size of alphabet, the 

opposite of vectors are used:: 

 

sage: P = WordPaths('abcd', [(1,0), (0,1)]) 

sage: P 

Word Paths over 4 steps 

sage: sorted(P.letters_to_steps().items()) 

[('a', (1, 0)), ('b', (0, 1)), ('c', (-1, 0)), ('d', (0, -1))] 

 

When no steps are given, default classes are returned:: 

 

sage: WordPaths('ab') 

Word Paths in North and East steps 

sage: WordPaths(range(4)) 

Word Paths on the square grid 

sage: WordPaths(range(6)) 

Word Paths on the hexagonal grid 

 

There are many type of built-in steps... 

 

On a two letters alphabet:: 

 

sage: WordPaths('ab', steps='north_east') 

Word Paths in North and East steps 

sage: WordPaths('()', steps='dyck') 

Finite Dyck paths 

 

On a four letters alphabet:: 

 

sage: WordPaths('ruld', steps='square_grid') 

Word Paths on the square grid 

 

On a six letters alphabet:: 

 

sage: WordPaths('abcdef', steps='hexagonal_grid') 

Word Paths on the hexagonal grid 

sage: WordPaths('abcdef', steps='triangle_grid') 

Word Paths on the triangle grid 

sage: WordPaths('abcdef', steps='cube_grid') 

Word Paths on the cube grid 

 

TESTS:: 

 

sage: WordPaths(range(5)) 

Traceback (most recent call last): 

... 

TypeError: Unable to make a class WordPaths from {0, 1, 2, 3, 4} 

sage: WordPaths('abAB', steps='square_gridd') 

Traceback (most recent call last): 

... 

TypeError: Unknown type of steps : square_gridd 

""" 

#Construction of the alphabet 

alphabet = build_alphabet(alphabet) 

 

#If no steps are given, they are guessed from the alphabet 

if steps is None: 

if alphabet.cardinality() == 2: 

steps = 'north_east' 

elif alphabet.cardinality() == 4: 

steps = 'square_grid' 

elif alphabet.cardinality() == 6: 

steps = 'hexagonal_grid' 

else: 

raise TypeError("Unable to make a class WordPaths from %s"%alphabet) 

 

#Returns the class of WordPaths according to the given type of paths 

if isinstance(steps, str): 

if steps in ('square_grid', 'square'): 

return WordPaths_square_grid(alphabet=alphabet) 

elif steps in ('triangle_grid', 'triangle'): 

return WordPaths_triangle_grid(alphabet=alphabet) 

elif steps in ('hexagonal_grid', 'hexagon'): 

return WordPaths_hexagonal_grid(alphabet=alphabet) 

elif steps in ('cube_grid', 'cube'): 

return WordPaths_cube_grid(alphabet=alphabet) 

elif steps in ('north_east', 'ne', 'NE'): 

return WordPaths_north_east(alphabet=alphabet) 

elif steps == 'dyck': 

return WordPaths_dyck(alphabet=alphabet) 

else: 

raise TypeError("Unknown type of steps : %s"%steps) 

else: 

return WordPaths_all(alphabet=alphabet, steps=steps) 

 

####################################################################### 

# # 

# Combinatorial classes of word paths # 

# # 

####################################################################### 

 

class WordPaths_all(FiniteWords): 

r""" 

The combinatorial class of all paths, i.e of all words over 

an alphabet where each letter is mapped to a step (a vector). 

""" 

def __init__(self, alphabet, steps): 

r""" 

INPUT: 

 

- ``alphabet`` - an ordered alphabet 

 

- ``steps`` - an iterable (of same length as alphabet or half the 

length of alphabet) of ordered vectors 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.paths import WordPaths_all 

sage: d = ((1,1), (-1,1), (1,-1), (-1,-1)) 

sage: P = WordPaths_all('abAB', d); P 

Word Paths over 4 steps 

sage: P == loads(dumps(P)) 

True 

 

If size of alphabet is twice the number of steps, then opposite 

vectors are used for the second part of the alphabet. 

 

sage: WordPaths('abcd',[(2,1),(2,4)]) 

Word Paths over 4 steps 

sage: _.letters_to_steps() 

{'a': (2, 1), 'b': (2, 4), 'c': (-2, -1), 'd': (-2, -4)} 

 

TESTS:: 

 

sage: from sage.combinat.words.paths import WordPaths_all 

sage: d = ((1,1), (-1,1), (1,-1), (-1,-1)) 

sage: WordPaths_all('abA', d) 

Traceback (most recent call last): 

... 

TypeError: size of steps (=4) must equal the size of alphabet (=3) or half the size of alphabet. 

 

sage: d = ((1,1), 1) 

sage: WordPaths_all('ab', d) 

Traceback (most recent call last): 

... 

ValueError: Can't make vectors from steps 

 

sage: d = ((1,1), (-1,1,0)) 

sage: WordPaths_all('ab', d) 

Traceback (most recent call last): 

... 

ValueError: Can't make summable vectors from steps 

""" 

#Construction of the words class 

FiniteWords.__init__(self, alphabet) 

alphabet = self.alphabet() 

 

#Checking the size of alphabet and steps 

ls = len(steps) 

la = alphabet.cardinality() 

if la != ls and la != 2*ls: 

raise TypeError("size of steps (=%s) must equal the size \ 

of alphabet (=%s) or half the size of alphabet."%(len(steps),alphabet.cardinality())) 

 

#Construction of the steps 

from sage.structure.element import Vector 

if all((isinstance(x, Vector) for x in steps)): 

vsteps = steps 

else: 

try: 

vsteps = [vector(_) for _ in steps] 

except (TypeError): 

raise ValueError("Can't make vectors from steps") 

try: 

s = sum(vsteps) 

except (TypeError, AttributeError): 

raise ValueError("Can't make summable vectors from steps") 

 

#Complete vsteps with the opposite vectors if needed 

if la == 2 * ls: 

vsteps += [-v for v in vsteps] 

 

self._steps = dict(zip(alphabet, vsteps)) 

self._vector_space = s.parent() 

 

def __eq__(self, other): 

r""" 

TESTS:: 

 

sage: W1 = WordPaths(['a','b'], [vector((0,1)), vector((0,2))]) 

sage: W2 = WordPaths(['a','b'], [vector((0,1)), vector((0,2))]) 

sage: W3 = WordPaths(['a','b'], [vector((0,2)), vector((1,0))]) 

sage: W1 == W2 

True 

sage: W1 == W3 

False 

""" 

return self is other or (type(self) == type(other) and \ 

self.alphabet() == other.alphabet() and \ 

self.vector_space() == other.vector_space() and \ 

self.letters_to_steps() == other.letters_to_steps()) 

 

def __ne__(self, other): 

r""" 

TESTS:: 

 

sage: W1 = WordPaths(['a','b'], [vector((0,1)), vector((0,2))]) 

sage: W2 = WordPaths(['a','b'], [vector((0,1)), vector((0,2))]) 

sage: W3 = WordPaths(['a','b'], [vector((0,2)), vector((1,0))]) 

sage: W1 != W2 

False 

sage: W1 != W3 

True 

""" 

return not (self == other) 

 

@lazy_attribute 

def _element_classes(self): 

r""" 

Returns a dictionary that gives the class of the elements of self. 

 

The word may be finite (infinite or of unknown length is not supported 

yet). 

Its data may be str, list, tuple, a callable or an iterable. 

For callable and iterable, the data may be cached. 

The dimension of the path may be 1, 2, 3 or more. 

 

TESTS:: 

 

sage: d = WordPaths('ab',steps=[(1,2),(3,4)])._element_classes 

sage: type(d) 

<... 'dict'> 

sage: len(d) 

7 

sage: d['tuple'] 

<class 'sage.combinat.words.paths.FiniteWordPath_2d_tuple'> 

 

:: 

 

sage: d = WordPaths('ab',steps=[(1,2,3),(3,4,5)])._element_classes 

sage: len(d) 

7 

sage: d['tuple'] 

<class 'sage.combinat.words.paths.FiniteWordPath_3d_tuple'> 

 

:: 

 

sage: steps = [(1,2,3,4),(3,4,5,6)] 

sage: d = WordPaths('ab',steps=steps)._element_classes 

sage: len(d) 

7 

sage: d['tuple'] 

<class 'sage.combinat.words.paths.FiniteWordPath_all_tuple'> 

 

:: 

 

sage: d = WordPaths('ab',steps=[(1,),(3,)])._element_classes 

sage: len(d) 

7 

sage: d['tuple'] 

<class 'sage.combinat.words.paths.FiniteWordPath_all_tuple'> 

""" 

dimension = self._vector_space.dimension() 

if dimension == 2: 

return { 

'list': FiniteWordPath_2d_list, 

'str': FiniteWordPath_2d_str, 

'tuple': FiniteWordPath_2d_tuple, 

'callable_with_caching': FiniteWordPath_2d_callable_with_caching, 

'callable': FiniteWordPath_2d_callable, 

'iter_with_caching': FiniteWordPath_2d_iter_with_caching, 

'iter': FiniteWordPath_2d_iter, 

} 

elif dimension == 3: 

return { 

'list': FiniteWordPath_3d_list, 

'str': FiniteWordPath_3d_str, 

'tuple': FiniteWordPath_3d_tuple, 

'callable_with_caching': FiniteWordPath_3d_callable_with_caching, 

'callable': FiniteWordPath_3d_callable, 

'iter_with_caching': FiniteWordPath_3d_iter_with_caching, 

'iter': FiniteWordPath_3d_iter, 

} 

else: 

return { 

'list': FiniteWordPath_all_list, 

'str': FiniteWordPath_all_str, 

'tuple': FiniteWordPath_all_tuple, 

'callable_with_caching': FiniteWordPath_all_callable_with_caching, 

'callable': FiniteWordPath_all_callable, 

'iter_with_caching': FiniteWordPath_all_iter_with_caching, 

'iter': FiniteWordPath_all_iter, 

} 

 

def __repr__(self): 

r""" 

Returns a string representation of self. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.paths import WordPaths_all 

sage: d = (vector((1,1)), vector((-1,1)), vector((1,-1)), vector((-1,-1))) 

sage: WordPaths_all('abAB',d).__repr__() 

'Word Paths over 4 steps' 

""" 

return "Word Paths over %s steps" % self.alphabet().cardinality() 

 

def letters_to_steps(self): 

r""" 

Returns the dictionary mapping letters to vectors (steps). 

 

EXAMPLES:: 

 

sage: d = WordPaths('ab').letters_to_steps() 

sage: sorted(d.items()) 

[('a', (0, 1)), ('b', (1, 0))] 

sage: d = WordPaths('abcd').letters_to_steps() 

sage: sorted(d.items()) 

[('a', (1, 0)), ('b', (0, 1)), ('c', (-1, 0)), ('d', (0, -1))] 

sage: d = WordPaths('abcdef').letters_to_steps() 

sage: sorted(d.items()) 

[('a', (1, 0)), 

('b', (1/2, 1/2*sqrt3)), 

('c', (-1/2, 1/2*sqrt3)), 

('d', (-1, 0)), 

('e', (-1/2, -1/2*sqrt3)), 

('f', (1/2, -1/2*sqrt3))] 

""" 

return self._steps 

 

def vector_space(self): 

r""" 

Return the vector space over which the steps of the paths are defined. 

 

EXAMPLES:: 

 

sage: WordPaths('ab',steps='dyck').vector_space() 

Ambient free module of rank 2 over the principal ideal domain Integer Ring 

sage: WordPaths('ab',steps='north_east').vector_space() 

Ambient free module of rank 2 over the principal ideal domain Integer Ring 

sage: WordPaths('abcd',steps='square_grid').vector_space() 

Ambient free module of rank 2 over the principal ideal domain Integer Ring 

sage: WordPaths('abcdef',steps='hexagonal_grid').vector_space() 

Vector space of dimension 2 over Number Field in sqrt3 with defining polynomial x^2 - 3 

sage: WordPaths('abcdef',steps='cube_grid').vector_space() 

Ambient free module of rank 3 over the principal ideal domain Integer Ring 

sage: WordPaths('abcdef',steps='triangle_grid').vector_space() 

Vector space of dimension 2 over Number Field in sqrt3 with defining polynomial x^2 - 3 

 

""" 

return self._vector_space 

 

class WordPaths_square_grid(WordPaths_all): 

r""" 

The combinatorial class of all paths on the square grid. 

""" 

def __init__(self, alphabet): 

r""" 

The combinatorial class of all finite paths on the square grid. 

 

INPUT: 

 

- ``alphabet`` - ordered alphabet of length 4. The order for the steps 

is : East, North, West, South. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.paths import WordPaths_square_grid 

sage: P = WordPaths_square_grid('abAB'); P 

Word Paths on the square grid 

sage: P == loads(dumps(P)) 

True 

 

""" 

#Construction of the steps 

d = [(1 ,0), (0,1), (-1,0), (0,-1)] 

 

#Construction of the class 

super(WordPaths_square_grid, self).__init__(alphabet, steps=d) 

 

@lazy_attribute 

def _element_classes(self): 

r""" 

Returns a dictionary that gives the class of the elements of self. 

 

The word may be finite (infinite or of unknown length is not supported 

yet). 

Its data may be str, list, tuple, a callable or an iterable. 

For callable and iterable, the data may be cached. 

 

TESTS:: 

 

sage: d = WordPaths('abcd')._element_classes 

sage: type(d) 

<... 'dict'> 

sage: len(d) 

7 

sage: d['tuple'] 

<class 'sage.combinat.words.paths.FiniteWordPath_square_grid_tuple'> 

""" 

return { 

'list': FiniteWordPath_square_grid_list, 

'str': FiniteWordPath_square_grid_str, 

'tuple': FiniteWordPath_square_grid_tuple, 

'callable_with_caching': FiniteWordPath_square_grid_callable_with_caching, 

'callable': FiniteWordPath_square_grid_callable, 

'iter_with_caching': FiniteWordPath_square_grid_iter_with_caching, 

'iter': FiniteWordPath_square_grid_iter, 

} 

 

def __repr__(self): 

r""" 

EXAMPLES:: 

 

sage: from sage.combinat.words.paths import WordPaths_square_grid 

sage: WordPaths_square_grid('abAB').__repr__() 

'Word Paths on the square grid' 

""" 

return "Word Paths on the square grid" 

 

class WordPaths_triangle_grid(WordPaths_all): 

r""" 

The combinatorial class of all paths on the triangle grid. 

""" 

def __init__(self, alphabet): 

r""" 

The combinatorial class of all finite paths on the triangle grid. 

 

INPUT: 

 

- ``alphabet`` - ordered alphabet of length 6. The order for the steps 

is : Right, Up-Right, Up-Left, Left, Down-Left, Down-Right. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.paths import WordPaths_triangle_grid 

sage: P = WordPaths_triangle_grid('abcdef'); P 

Word Paths on the triangle grid 

sage: P == loads(dumps(P)) 

True 

 

""" 

K = QuadraticField(3, 'sqrt3') 

sqrt3 = K.gen() 

 

#Construction of the steps 

d = (vector(K, (1 ,0 )), 

vector(K, (ZZ(1)/ZZ(2), sqrt3/2)), 

vector(K, (ZZ(-1)/ZZ(2), sqrt3/2)), 

vector(K, (-1 , 0 )), 

vector(K, (ZZ(-1)/ZZ(2), -sqrt3/2 )), 

vector(K, (ZZ(1)/ZZ(2), -sqrt3/2 ))) 

 

#Construction of the class 

super(WordPaths_triangle_grid, self).__init__(alphabet, steps=d) 

 

self._infinite_word_class = None 

self._finite_word_class = FiniteWordPath_triangle_grid 

 

@lazy_attribute 

def _element_classes(self): 

r""" 

Returns a dictionary that gives the class of the elements of self. 

 

The word may be finite (infinite or of unknown length is not supported 

yet). 

Its data may be str, list, tuple, a callable or an iterable. 

For callable and iterable, the data may be cached. 

 

TESTS:: 

 

sage: d = WordPaths('abcdef', steps='triangle')._element_classes 

sage: len(d) 

7 

sage: type(d) 

<... 'dict'> 

sage: d['tuple'] 

<class 'sage.combinat.words.paths.FiniteWordPath_triangle_grid_tuple'> 

""" 

return { 

'list': FiniteWordPath_triangle_grid_list, 

'str': FiniteWordPath_triangle_grid_str, 

'tuple': FiniteWordPath_triangle_grid_tuple, 

'callable_with_caching': FiniteWordPath_triangle_grid_callable_with_caching, 

'callable': FiniteWordPath_triangle_grid_callable, 

'iter_with_caching': FiniteWordPath_triangle_grid_iter_with_caching, 

'iter': FiniteWordPath_triangle_grid_iter, 

} 

 

def __repr__(self): 

r""" 

EXAMPLES:: 

 

sage: from sage.combinat.words.paths import WordPaths_triangle_grid 

sage: WordPaths_triangle_grid('abcdef').__repr__() 

'Word Paths on the triangle grid' 

""" 

return "Word Paths on the triangle grid" 

 

class WordPaths_hexagonal_grid(WordPaths_triangle_grid): 

r""" 

The combinatorial class of all paths on the hexagonal grid. 

""" 

def __init__(self, alphabet): 

r""" 

The combinatorial class of all finite paths on the hexagonal grid. 

 

INPUT: 

 

- ``alphabet`` - ordered alphabet of length 6. The order for the steps 

is : Right, Up-Right, Up-Left, Left, Down-Left, Down-Right. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.paths import WordPaths_hexagonal_grid 

sage: P = WordPaths_hexagonal_grid('abcdef'); P 

Word Paths on the hexagonal grid 

sage: P == loads(dumps(P)) 

True 

 

""" 

#Construction of the class 

super(WordPaths_hexagonal_grid, self).__init__(alphabet) 

 

self._infinite_word_class = None 

self._finite_word_class = FiniteWordPath_hexagonal_grid 

 

@lazy_attribute 

def _element_classes(self): 

r""" 

Returns a dictionary that gives the class of the elements of self. 

 

The word may be finite (infinite or of unknown length is not supported 

yet). 

Its data may be str, list, tuple, a callable or an iterable. 

For callable and iterable, the data may be cached. 

 

TESTS:: 

 

sage: d = WordPaths('abcdef', steps='hexagon')._element_classes 

sage: type(d) 

<... 'dict'> 

sage: len(d) 

7 

sage: d['tuple'] 

<class 'sage.combinat.words.paths.FiniteWordPath_hexagonal_grid_tuple'> 

""" 

return { 

'list': FiniteWordPath_hexagonal_grid_list, 

'str': FiniteWordPath_hexagonal_grid_str, 

'tuple': FiniteWordPath_hexagonal_grid_tuple, 

'callable_with_caching': FiniteWordPath_hexagonal_grid_callable_with_caching, 

'callable': FiniteWordPath_hexagonal_grid_callable, 

'iter_with_caching': FiniteWordPath_hexagonal_grid_iter_with_caching, 

'iter': FiniteWordPath_hexagonal_grid_iter, 

} 

 

def __repr__(self): 

r""" 

EXAMPLES:: 

 

sage: from sage.combinat.words.paths import WordPaths_hexagonal_grid 

sage: WordPaths_hexagonal_grid('abcdef').__repr__() 

'Word Paths on the hexagonal grid' 

""" 

return "Word Paths on the hexagonal grid" 

 

class WordPaths_cube_grid(WordPaths_all): 

r""" 

The combinatorial class of all paths on the cube grid. 

""" 

def __init__(self, alphabet): 

r""" 

The combinatorial class of all finite paths on the cube grid. 

 

INPUT: 

 

- ``alphabet`` -- ordered alphabet of length 6. The order for 

the steps is `e_x, e_y, e_z, -e_x, -e_y, -e_z`, where `e_v` 

denotes the canonical basis. 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.paths import WordPaths_cube_grid 

sage: P = WordPaths_cube_grid('abcABC'); P 

Word Paths on the cube grid 

sage: P == loads(dumps(P)) 

True 

""" 

#Construction of the class 

d = [(1,0,0), (0,1,0), (0,0,1), (-1,0,0), (0,-1,0), (0,0,-1)] 

super(WordPaths_cube_grid, self).__init__(alphabet, steps=d) 

self._infinite_word_class = None 

self._finite_word_class = FiniteWordPath_cube_grid 

 

@lazy_attribute 

def _element_classes(self): 

r""" 

Returns a dictionary that gives the class of the elements of self. 

 

The word may be finite (infinite or of unknown length is not supported 

yet). 

Its data may be str, list, tuple, a callable or an iterable. 

For callable and iterable, the data may be cached. 

 

TESTS:: 

 

sage: d = WordPaths('abcdef', steps='cube')._element_classes 

sage: type(d) 

<... 'dict'> 

sage: len(d) 

7 

sage: d['tuple'] 

<class 'sage.combinat.words.paths.FiniteWordPath_cube_grid_tuple'> 

""" 

return {'list': FiniteWordPath_cube_grid_list, 

'str': FiniteWordPath_cube_grid_str, 

'tuple': FiniteWordPath_cube_grid_tuple, 

'callable_with_caching': FiniteWordPath_cube_grid_callable_with_caching, 

'callable': FiniteWordPath_cube_grid_callable, 

'iter_with_caching': FiniteWordPath_cube_grid_iter_with_caching, 

'iter': FiniteWordPath_cube_grid_iter, 

} 

 

def __repr__(self): 

r""" 

EXAMPLES:: 

 

sage: from sage.combinat.words.paths import WordPaths_cube_grid 

sage: WordPaths_cube_grid('abcABC').__repr__() 

'Word Paths on the cube grid' 

""" 

return "Word Paths on the cube grid" 

 

class WordPaths_dyck(WordPaths_all): 

r""" 

The combinatorial class of all Dyck paths. 

""" 

def __init__(self, alphabet): 

r""" 

The combinatorial class of all finite Dyck paths. 

 

INPUT: 

 

- ``alphabet`` - ordered alphabet of length 2. The order for the steps 

is : (1,1), (1,-1) 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.paths import WordPaths_dyck 

sage: P = WordPaths_dyck('[]'); P 

Finite Dyck paths 

sage: P == loads(dumps(P)) 

True 

""" 

#Construction of the class 

d = [(1,1), (1,-1)] 

super(WordPaths_dyck, self).__init__(alphabet, steps=d) 

 

self._infinite_word_class = None 

self._finite_word_class = FiniteWordPath_dyck 

 

@lazy_attribute 

def _element_classes(self): 

r""" 

Returns a dictionary that gives the class of the elements of self. 

 

The word may be finite (infinite or of unknown length is not supported 

yet). 

Its data may be str, list, tuple, a callable or an iterable. 

For callable and iterable, the data may be cached. 

 

TESTS:: 

 

sage: d = WordPaths('ab', steps='dyck')._element_classes 

sage: type(d) 

<... 'dict'> 

sage: len(d) 

7 

sage: d['tuple'] 

<class 'sage.combinat.words.paths.FiniteWordPath_dyck_tuple'> 

""" 

return {'list': FiniteWordPath_dyck_list, 

'str': FiniteWordPath_dyck_str, 

'tuple': FiniteWordPath_dyck_tuple, 

'callable_with_caching': FiniteWordPath_dyck_callable_with_caching, 

'callable': FiniteWordPath_dyck_callable, 

'iter_with_caching': FiniteWordPath_dyck_iter_with_caching, 

'iter': FiniteWordPath_dyck_iter, 

} 

 

def __repr__(self): 

r""" 

EXAMPLES:: 

 

sage: from sage.combinat.words.paths import WordPaths_dyck 

sage: WordPaths_dyck('()').__repr__() 

'Finite Dyck paths' 

""" 

return "Finite Dyck paths" 

 

class WordPaths_north_east(WordPaths_all): 

r""" 

The combinatorial class of all paths using North and East directions. 

""" 

def __init__(self, alphabet): 

r""" 

The combinatorial class of all finite paths using only north and east 

steps on the square grid. 

 

INPUT: 

 

- ``alphabet`` - ordered alphabet of length 2. The order for the steps 

is North, East 

 

EXAMPLES:: 

 

sage: from sage.combinat.words.paths import WordPaths_north_east 

sage: P = WordPaths_north_east('ab'); P 

Word Paths in North and East steps 

sage: P == loads(dumps(P)) 

True 

""" 

#Construction of the class 

d = [(0,1), (1,0)] 

super(WordPaths_north_east, self).__init__(alphabet, steps=d) 

self._infinite_word_class = None 

self._finite_word_class = FiniteWordPath_north_east 

 

@lazy_attribute 

def _element_classes(self): 

r""" 

Returns a dictionary that gives the class of the elements of self. 

 

The word may be finite (infinite or of unknown length is not supported 

yet). 

Its data may be str, list, tuple, a callable or an iterable. 

For callable and iterable, the data may be cached. 

 

TESTS:: 

 

sage: d = WordPaths('ab', steps='NE')._element_classes 

sage: type(d) 

<... 'dict'> 

sage: len(d) 

7 

sage: d['tuple'] 

<class 'sage.combinat.words.paths.FiniteWordPath_north_east_tuple'> 

""" 

return {'list': FiniteWordPath_north_east_list, 

'str': FiniteWordPath_north_east_str, 

'tuple': FiniteWordPath_north_east_tuple, 

'callable_with_caching': FiniteWordPath_north_east_callable_with_caching, 

'callable': FiniteWordPath_north_east_callable, 

'iter_with_caching': FiniteWordPath_north_east_iter_with_caching, 

'iter': FiniteWordPath_north_east_iter, 

} 

 

def __repr__(self): 

r""" 

EXAMPLES:: 

 

sage: from sage.combinat.words.paths import WordPaths_north_east 

sage: WordPaths_north_east('ab').__repr__() 

'Word Paths in North and East steps' 

""" 

return "Word Paths in North and East steps" 

 

####################################################################### 

# # 

# Abstract word path classes # 

# (all, 2d, 3d, ...) # 

# # 

####################################################################### 

 

class FiniteWordPath_all(SageObject): 

def _repr_(self): 

r""" 

Returns a string representation of this path. 

 

EXAMPLES:: 

 

sage: F = WordPaths('ab',[(1,0,0,0),(0,1,0,0)]); F 

Word Paths over 2 steps 

sage: f = F('ababab') 

sage: f._repr_() 

'Path: ababab' 

""" 

return "Path: %s"%self.string_rep() 

 

def points(self, include_last=True): 

r""" 

Returns an iterator yielding a list of points used to draw the path 

represented by this word. 

 

INPUT: 

 

- ``include_last`` - bool (default: True) whether to include the 

last point 

 

EXAMPLES: 

 

A simple closed square:: 

 

sage: P = WordPaths('abAB') 

sage: list(P('abAB').points()) 

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

 

A simple closed square without the last point:: 

 

sage: list(P('abAB').points(include_last=False)) 

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

 

:: 

 

sage: list(P('abaB').points()) 

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

""" 

curpt = self.start_point() 

yield curpt 

end = len(self) if include_last else -1 

for l in self[:end]: 

curpt += self.parent().letters_to_steps()[l] 

yield curpt 

 

def start_point(self): 

r""" 

Return the starting point of self. 

 

OUTPUT: 

 

vector 

 

EXAMPLES:: 

 

sage: WordPaths('abcdef')('abcdef').start_point() 

(0, 0) 

sage: WordPaths('abcdef', steps='cube_grid')('abcdef').start_point() 

(0, 0, 0) 

sage: P = WordPaths('ab', steps=[(1,0,0,0),(0,1,0,0)]) 

sage: P('abbba').start_point() 

(0, 0, 0, 0) 

""" 

return self.parent().vector_space()(0) 

 

@cached_method 

def end_point(self): 

r""" 

Returns the end point of the path. 

 

EXAMPLES:: 

 

sage: WordPaths('abcdef')('abababab').end_point() 

(6, 2*sqrt3) 

sage: WordPaths('abAB')('abababab').end_point() 

(4, 4) 

sage: P = WordPaths('abcABC', steps='cube_grid') 

sage: P('ababababCC').end_point() 

(4, 4, -2) 

sage: WordPaths('abcdef')('abcdef').end_point() 

(0, 0) 

sage: P = WordPaths('abc', steps=[(1,3,7,9),(-4,1,0,0),(0,32,1,8)]) 

sage: P('abcabababacaacccbbcac').end_point() 

(-16, 254, 63, 128) 

""" 

last = None 

for pt in self.points(): 

last = pt 

return last 

 

def directive_vector(self): 

r""" 

Returns the directive vector of self. 

 

The directive vector is the vector starting at the start point 

and ending at the end point of the path self. 

 

EXAMPLES:: 

 

sage: WordPaths('abcdef')('abababab').directive_vector() 

(6, 2*sqrt3) 

sage: WordPaths('abAB')('abababab').directive_vector() 

(4, 4) 

sage: P = WordPaths('abcABC', steps='cube_grid') 

sage: P('ababababCC').directive_vector() 

(4, 4, -2) 

sage: WordPaths('abcdef')('abcdef').directive_vector() 

(0, 0) 

sage: P = WordPaths('abc', steps=[(1,3,7,9),(-4,1,0,0),(0,32,1,8)]) 

sage: P('abcabababacaacccbbcac').directive_vector() 

(-16, 254, 63, 128) 

""" 

return self.end_point() - self.start_point() 

 

def is_closed(self): 

r""" 

Returns True if the path is closed, i.e. if the origin and the end of 

the path are equal. 

 

EXAMPLES:: 

 

sage: P = WordPaths('abcd', steps=[(1,0),(0,1),(-1,0),(0,-1)]) 

sage: P('abcd').is_closed() 

True 

sage: P('abc').is_closed() 

False 

sage: P().is_closed() 

True 

sage: P('aacacc').is_closed() 

True 

""" 

return self.start_point() == self.end_point() 

 

def is_simple(self): 

r""" 

Returns True if the path is simple, i.e. if all its points are 

distincts. 

 

If the path is closed, the last point is not considered. 

 

EXAMPLES:: 

 

sage: P = WordPaths('abcdef',steps='triangle_grid');P 

Word Paths on the triangle grid 

sage: P('abc').is_simple() 

True 

sage: P('abcde').is_simple() 

True 

sage: P('abcdef').is_simple() 

True 

sage: P('ad').is_simple() 

True 

sage: P('aabdee').is_simple() 

False 

""" 

n = 0 

s = set() 

include_last = not self.is_closed() 

for p in self.points(include_last=include_last): 

# We need the elements to have a common parent, 

# so we convert the points to immutable vectors. 

v = vector(p) 

v.set_immutable() 

s.add(v) 

n += 1 

if len(s) != n: 

return False 

else: 

return True 

 

def tikz_trajectory(self): 

r""" 

Returns the trajectory of self as a tikz str. 

 

EXAMPLES:: 

 

sage: P = WordPaths('abcdef') 

sage: p = P('abcde') 

sage: p.tikz_trajectory() 

'(0.000, 0.000) -- (1.00, 0.000) -- (1.50, 0.866) -- (1.00, 1.73) -- (0.000, 1.73) -- (-0.500, 0.866)' 

 

""" 

from sage.all import n 

f = lambda x: n(x,digits=3) 

l = [str(tuple(map(f, pt))) for pt in self.points()] 

return ' -- '.join(l) 

 

def projected_point_iterator(self, v=None, ring=None): 

r""" 

Return an iterator of the projection of the orbit points of the 

path into the space orthogonal to the given vector. 

 

INPUT: 

 

- ``v`` - vector (optional, default: None) If None, the directive 

vector (i.e. the end point minus starting point) of the path is 

considered. 

 

- ``ring`` - ring (optional, default: None) where to do the 

computations. If None, RealField(53) is used. 

 

OUTPUT: 

 

iterator of points 

 

EXAMPLES: 

 

Projected points of the Rauzy fractal:: 

 

sage: s = WordMorphism('1->12,2->13,3->1') 

sage: D = s.fixed_point('1') 

sage: v = s.pisot_eigenvector_right() 

sage: P = WordPaths('123',[(1,0,0),(0,1,0),(0,0,1)]) 

sage: w = P(D[:200]) 

sage: it = w.projected_point_iterator(v) 

sage: for i in range(6): next(it) 

(0.000000000000000, 0.000000000000000) 

(-0.526233343362516, 0.000000000000000) 

(0.220830337618112, -0.477656250512816) 

(-0.305403005744404, -0.477656250512816) 

(0.100767309386062, 0.400890564600664) 

(-0.425466033976454, 0.400890564600664) 

 

Projected points of a 2d path:: 

 

sage: P = WordPaths('ab','ne') 

sage: p = P('aabbabbab') 

sage: it = p.projected_point_iterator(ring=RealField(20)) 

sage: for i in range(8): next(it) 

(0.00000) 

(0.78087) 

(1.5617) 

(0.93704) 

(0.31235) 

(1.0932) 

(0.46852) 

(-0.15617) 

""" 

if v is None: 

v = self.directive_vector() 

if ring is None: 

ring = RR 

R = vector_on_axis_rotation_matrix(v, 0, ring=ring)[1:] 

for q in self.points(): 

yield R * q 

 

def plot_projection(self, v=None, letters=None, color=None, ring=None, 

size=12, kind='right'): 

r""" 

Return an image of the projection of the successive points of the 

path into the space orthogonal to the given vector. 

 

INPUT: 

 

- ``self`` - a word path in a 3 or 4 dimension vector space 

 

- ``v`` - vector (optional, default: None) If None, the directive 

vector (i.e. the end point minus starting point) of the path is 

considered. 

 

- ``letters`` - iterable (optional, default: None) of the letters 

to be projected. If None, then all the letters are considered. 

 

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

mapped to colors. If None, automatic colors are chosen. 

 

- ``ring`` - ring (optional, default: None) where to do the 

computations. If None, RealField(53) is used. 

 

- ``size`` - number (optional, default: ``12``) size of the points. 

 

- ``kind`` - string (optional, default ``'right'``) either 

``'right'`` or ``'left'``. The color of a letter is given to the 

projected prefix to the right or the left of the letter. 

 

OUTPUT: 

 

2d or 3d Graphic object. 

 

EXAMPLES: 

 

The Rauzy fractal:: 

 

sage: s = WordMorphism('1->12,2->13,3->1') 

sage: D = s.fixed_point('1') 

sage: v = s.pisot_eigenvector_right() 

sage: P = WordPaths('123',[(1,0,0),(0,1,0),(0,0,1)]) 

sage: w = P(D[:200]) 

sage: w.plot_projection(v) # long time (2s) 

Graphics object consisting of 200 graphics primitives 

 

In this case, the abelianized vector doesn't give a good 

projection:: 

 

sage: w.plot_projection() # long time (2s) 

Graphics object consisting of 200 graphics primitives 

 

You can project only the letters you want:: 

 

sage: w.plot_projection(v, letters='12') # long time (2s) 

Graphics object consisting of 168 graphics primitives 

 

You can increase or decrease the precision of the computations by 

changing the ring of the projection matrix:: 

 

sage: w.plot_projection(v, ring=RealField(20)) # long time (2s) 

Graphics object consisting of 200 graphics primitives 

 

You can change the size of the points:: 

 

sage: w.plot_projection(v, size=30) # long time (2s) 

Graphics object consisting of 200 graphics primitives 

 

You can assign the color of a letter to the projected prefix to the 

right or the left of the letter:: 

 

sage: w.plot_projection(v, kind='left') # long time (2s) 

Graphics object consisting of 200 graphics primitives 

 

To remove the axis, do like this:: 

 

sage: r = w.plot_projection(v) 

sage: r.axes(False) 

sage: r # long time (2s) 

Graphics object consisting of 200 graphics primitives 

 

You can assign different colors to each letter:: 

 

sage: color = {'1':'purple', '2':(.2,.3,.4), '3': 'magenta'} 

sage: w.plot_projection(v, color=color) # long time (2s) 

Graphics object consisting of 200 graphics primitives 

 

The 3d-Rauzy fractal:: 

 

sage: s = WordMorphism('1->12,2->13,3->14,4->1') 

sage: D = s.fixed_point('1') 

sage: v = s.pisot_eigenvector_right() 

sage: P = WordPaths('1234',[(1,0,0,0), (0,1,0,0), (0,0,1,0), (0,0,0,1)]) 

sage: w = P(D[:200]) 

sage: w.plot_projection(v) 

Graphics3d Object 

 

The dimension of vector space of the parent must be 3 or 4:: 

 

sage: P = WordPaths('ab', [(1, 0), (0, 1)]) 

sage: p = P('aabbabbab') 

sage: p.plot_projection() 

Traceback (most recent call last): 

... 

TypeError: The dimension of the vector space (=2) must be 3 or 4 

""" 

dimension = self.parent().vector_space().dimension() 

if not dimension in (3, 4): 

msg = "The dimension of the vector space (=%s) must be 3 or 4"%dimension 

raise TypeError(msg) 

if letters is None: 

letters = self.parent().alphabet() 

if color is None: 

from sage.plot.all import hue 

A = self.parent().alphabet() 

color = dict( (a, hue(A.rank(a)/float(A.cardinality()))) for a in A ) 

it = self.projected_point_iterator(v, ring=ring) 

if kind == 'right': 

start = next(it) 

elif kind != 'left': 

raise ValueError('unknown value for kind (=%s)'%kind) 

tout = [point([c], color=color[a], size=size) for a, c in zip(self, it) if a in letters] 

return sum(tout) 

 

def projected_path(self, v=None, ring=None): 

r""" 

Return the path projected into the space orthogonal to the given 

vector. 

 

INPUT: 

 

- ``v`` - vector (optional, default: None) If None, the directive 

vector (i.e. the end point minus starting point) of the path is 

considered. 

 

- ``ring`` - ring (optional, default: None) where to do the 

computations. If None, RealField(53) is used. 

 

OUTPUT: 

 

word path 

 

EXAMPLES: 

 

The projected path of the tribonacci word:: 

 

sage: s = WordMorphism('1->12,2->13,3->1') 

sage: D = s.fixed_point('1') 

sage: v = s.pisot_eigenvector_right() 

sage: P = WordPaths('123',[(1,0,0),(0,1,0),(0,0,1)]) 

sage: w = P(D[:1000]) 

sage: p = w.projected_path(v) 

sage: p 

Path: 1213121121312121312112131213121121312121... 

sage: p[:20].plot() 

Graphics object consisting of 3 graphics primitives 

 

The ``ring`` argument allows to change the precision of the 

projected steps:: 

 

sage: p = w.projected_path(v, RealField(10)) 

sage: p 

Path: 1213121121312121312112131213121121312121... 

sage: p.parent().letters_to_steps() 

{'1': (-0.53, 0.00), '2': (0.75, -0.48), '3': (0.41, 0.88)} 

""" 

if v is None: 

v = self.directive_vector() 

if ring is None: 

ring = RR 

R = vector_on_axis_rotation_matrix(v, 0, ring=ring)[1:] 

d = self.parent().letters_to_steps() 

A = self.parent().alphabet() 

nvvectors = [R*d[a] for a in A] 

projected_parent = WordPaths(A, nvvectors) 

return projected_parent(self) 

 

def is_tangent(self): 

r""" 

The is_tangent() method, which is implemented for words, has 

an extended meaning for word paths, which is not implemented yet. 

 

TESTS:: 

 

sage: WordPaths('ab')('abbab').is_tangent() 

Traceback (most recent call last): 

... 

NotImplementedError 

 

AUTHOR: 

 

- Thierry Monteil 

""" 

raise NotImplementedError 

 

class FiniteWordPath_2d(FiniteWordPath_all): 

def plot(self, pathoptions=dict(rgbcolor='red',thickness=3), 

fill=True, filloptions=dict(rgbcolor='red',alpha=0.2), 

startpoint=True, startoptions=dict(rgbcolor='red',pointsize=100), 

endarrow=True, arrowoptions=dict(rgbcolor='red',arrowsize=20,width=3), 

gridlines=False, gridoptions=dict()): 

r""" 

Returns a 2d Graphics illustrating the path. 

 

INPUT: 

 

- ``pathoptions`` - (dict, 

default:dict(rgbcolor='red',thickness=3)), options for the 

path drawing 

 

- ``fill`` - (boolean, default: True), if fill is True and if 

the path is closed, the inside is colored 

 

- ``filloptions`` - (dict, 

default:dict(rgbcolor='red',alpha=0.2)), options for the 

inside filling 

 

- ``startpoint`` - (boolean, default: True), draw the start point? 

 

- ``startoptions`` - (dict, 

default:dict(rgbcolor='red',pointsize=100)) options for the 

start point drawing 

 

- ``endarrow`` - (boolean, default: True), draw an arrow end at the end? 

 

- ``arrowoptions`` - (dict, 

default:dict(rgbcolor='red',arrowsize=20, width=3)) options 

for the end point arrow 

 

- ``gridlines``- (boolean, default: False), show gridlines? 

 

- ``gridoptions`` - (dict, default: {}), options for the gridlines 

 

 

EXAMPLES: 

 

A non closed path on the square grid:: 

 

sage: P = WordPaths('abAB') 

sage: P('abababAABAB').plot() 

Graphics object consisting of 3 graphics primitives 

 

A closed path on the square grid:: 

 

sage: P('abababAABABB').plot() 

Graphics object consisting of 4 graphics primitives 

 

A Dyck path:: 

 

sage: P = WordPaths('()', steps='dyck') 

sage: P('()()()((()))').plot() 

Graphics object consisting of 3 graphics primitives 

 

A path in the triangle grid:: 

 

sage: P = WordPaths('abcdef', steps='triangle_grid') 

sage: P('abcdedededefab').plot() 

Graphics object consisting of 3 graphics primitives 

 

A polygon of length 220 that tiles the plane in two ways:: 

 

sage: P = WordPaths('abAB') 

sage: P('aBababAbabaBaBABaBabaBaBABAbABABaBabaBaBABaBababAbabaBaBABaBabaBaBABAbABABaBABAbAbabAbABABaBABAbABABaBabaBaBABAbABABaBABAbAbabAbABAbAbabaBababAbABAbAbabAbABABaBABAbAbabAbABAbAbabaBababAbabaBaBABaBababAbabaBababAbABAbAbab').plot() 

Graphics object consisting of 4 graphics primitives 

 

With gridlines:: 

 

sage: P('ababababab').plot(gridlines=True) 

 

TESTS:: 

 

sage: P = WordPaths('abAB') 

sage: P().plot() 

Graphics object consisting of 3 graphics primitives 

sage: sum(map(plot,map(P,['a','A','b','B']))) 

Graphics object consisting of 12 graphics primitives 

""" 

G = Graphics() 

pts = list(self.points()) 

 

#################### 

#################### 

#Bug: plot needs float for coordinates 

#################### 

#################### 

pts = [[RR(_) for _ in x] for x in pts] 

 

#Inside 

if fill and self.is_closed(): 

G += polygon(pts, **filloptions) 

 

#Startpoint 

if startpoint: 

G += point(pts[0], **startoptions) 

 

#The path itself 

if endarrow and not self.is_empty(): 

G += line(pts[:-1], **pathoptions) 

G += arrow(pts[-2], pts[-1], **arrowoptions) 

else: 

G += line(pts, **pathoptions) 

 

G.axes(False) 

G.set_aspect_ratio(1) 

 

#gridlines 

###############BUG############## 

#Gridlines doesn't work fine. 

#It should be gridlines="integers" 

###############BUG############## 

if gridlines: 

G = G.show(gridlines=True, **gridoptions) 

 

return G 

 

def animate(self): 

r""" 

Returns an animation object illustrating the path growing step by step. 

 

EXAMPLES:: 

 

sage: P = WordPaths('abAB') 

sage: p = P('aaababbb') 

sage: a = p.animate(); a # optional -- ImageMagick 

Animation with 9 frames 

sage: show(a) # optional -- ImageMagick 

sage: a.gif(delay=35, iterations=3) # optional -- ImageMagick 

doctest:...: DeprecationWarning: use tmp_filename instead 

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

 

:: 

 

sage: P = WordPaths('abcdef',steps='triangle') 

sage: p = P('abcdef') 

sage: p.animate() # optional -- ImageMagick 

Animation with 8 frames 

 

If the path is closed, the plain polygon is added at the end of the 

animation:: 

 

sage: P = WordPaths('abAB') 

sage: p = P('ababAbABABaB') 

sage: a = p.animate(); a # optional -- ImageMagick 

Animation with 14 frames 

 

Another example illustrating a Fibonacci tile:: 

 

sage: w = words.fibonacci_tile(2) 

sage: show(w.animate()) # optional -- ImageMagick 

 

The first 4 Fibonacci tiles in an animation:: 

 

sage: a = words.fibonacci_tile(0).animate() 

sage: b = words.fibonacci_tile(1).animate() 

sage: c = words.fibonacci_tile(2).animate() 

sage: d = words.fibonacci_tile(3).animate() 

sage: (a*b*c*d).show() # optional -- ImageMagick 

 

.. note:: 

 

If ImageMagick is not installed, you will get an error 

message like this:: 

 

/usr/local/share/sage/local/bin/sage-native-execute: 8: convert: 

not found 

 

Error: ImageMagick does not appear to be installed. Saving an 

animation to a GIF file or displaying an animation requires 

ImageMagick, so please install it and try again. 

 

See www.imagemagick.org, for example. 

 

""" 

from sage.plot.all import line, polygon, animate 

 

pts = list(self.points()) 

 

#################### 

#################### 

#Bug: plot needs float for coordinates 

#################### 

#################### 

pts = [[RR(_) for _ in x] for x in pts] 

 

images = [line(pts[:i]) for i in range(1,len(pts)+1)] 

 

if self.is_closed(): 

images.append(polygon(pts)) 

 

#Get the window of the last image 

last_image = images[-1] 

kwds = {} 

kwds['xmin'] = last_image.xmin() 

kwds['xmax'] = last_image.xmax() 

kwds['ymin'] = last_image.ymin() 

kwds['ymax'] = last_image.ymax() 

kwds['aspect_ratio'] = 1 

kwds['axes'] = False 

 

return animate(images, **kwds) 

 

def plot_directive_vector(self, options=dict(rgbcolor='blue')): 

r""" 

Returns an arrow 2d graphics that goes from the start of the path 

to the end. 

 

INPUT: 

 

- ``options`` - dictionary, default: {'rgbcolor': 'blue'} graphic 

options for the arrow 

 

If the start is the same as the end, a single point is returned. 

 

EXAMPLES:: 

 

sage: P = WordPaths('abcd'); P 

Word Paths on the square grid 

sage: p = P('aaaccaccacacacaccccccbbdd'); p 

Path: aaaccaccacacacaccccccbbdd 

sage: R = p.plot() + p.plot_directive_vector() 

sage: R.axes(False) 

sage: R.set_aspect_ratio(1) 

sage: R.plot() 

Graphics object consisting of 4 graphics primitives 

 

TESTS: 

 

A closed path:: 

 

sage: P('acbd').plot_directive_vector() 

Graphics object consisting of 1 graphics primitive 

""" 

start = self.start_point() 

end = self.end_point() 

if (start == end): 

G = point(start, pointsize=10, **options) 

else: 

G = arrow(start, end, **options) 

G.axes(False) 

G.set_aspect_ratio(1) 

return G 

 

def area(self): 

r""" 

Returns the area of a closed path. 

 

INPUT: 

 

- ``self`` - a closed path 

 

EXAMPLES:: 

 

sage: P = WordPaths('abcd',steps=[(1,1),(-1,1),(-1,-1),(1,-1)]) 

sage: p = P('abcd') 

sage: p.area() #todo: not implemented 

2 

 

""" 

if not self.is_closed(): 

raise TypeError("the path must be closed to compute its area") 

return NotImplemented 

 

def height(self): 

r""" 

Returns the height of self. 

 

The height of a `2d`-path is merely the difference 

between the highest and the lowest `y`-coordinate of each 

points traced by it. 

 

OUTPUT: 

 

non negative real number 

 

EXAMPLES:: 

 

sage: Freeman = WordPaths('abAB') 

sage: Freeman('aababaabbbAA').height() 

5 

 

The function is well-defined if self is not simple or close:: 

 

sage: Freeman('aabAAB').height() 

1 

sage: Freeman('abbABa').height() 

2 

 

This works for any `2d`-paths:: 

 

sage: Paths = WordPaths('ab', steps=[(1,0),(1,1)]) 

sage: p = Paths('abbaa') 

sage: p.height() 

2 

sage: DyckPaths = WordPaths('ab', steps='dyck') 

sage: p = DyckPaths('abaabb') 

sage: p.height() 

2 

sage: w = WordPaths('abcABC', steps='triangle')('ababcaaBC') 

sage: w.height() 

2.59807621135332 

""" 

return self.ymax() - self.ymin() 

 

def width(self): 

r""" 

Returns the width of self. 

 

The height of a `2d`-path is merely the difference 

between the rightmost and the leftmost `x`-coordinate of each 

points traced by it. 

 

OUTPUT: 

 

non negative real number 

 

EXAMPLES:: 

 

sage: Freeman = WordPaths('abAB') 

sage: Freeman('aababaabbbAA').width() 

5 

 

The function is well-defined if self is not simple or close:: 

 

sage: Freeman('aabAAB').width() 

2 

sage: Freeman('abbABa').width() 

1 

 

This works for any `2d`-paths:: 

 

sage: Paths = WordPaths('ab', steps=[(1,0),(1,1)]) 

sage: p = Paths('abbaa') 

sage: p.width() 

5 

sage: DyckPaths = WordPaths('ab', steps='dyck') 

sage: p = DyckPaths('abaabb') 

sage: p.width() 

6 

sage: w = WordPaths('abcABC', steps='triangle')('ababcaaBC') 

sage: w.width() 

4.50000000000000 

""" 

return self.xmax() - self.xmin() 

 

def xmin(self): 

r""" 

Returns the minimum of the x-coordinates of the path. 

 

EXAMPLES:: 

 

sage: P = WordPaths('0123') 

sage: p = P('0101013332') 

sage: p.xmin() 

0 

 

This works for any `2d`-paths:: 

 

sage: Paths = WordPaths('ab', steps=[(1,0),(-1,1)]) 

sage: p = Paths('abbba') 

sage: p.xmin() 

-2 

sage: DyckPaths = WordPaths('ab', steps='dyck') 

sage: p = DyckPaths('abaabb') 

sage: p.xmin() 

0 

sage: w = WordPaths('abcABC', steps='triangle')('ababcaaBC') 

sage: w.xmin() 

0.000000000000000 

""" 

return min(x for (x,_) in self.points()) 

 

def ymin(self): 

r""" 

Returns the minimum of the y-coordinates of the path. 

 

EXAMPLES:: 

 

sage: P = WordPaths('0123') 

sage: p = P('0101013332') 

sage: p.ymin() 

0 

 

This works for any `2d`-paths:: 

 

sage: Paths = WordPaths('ab', steps=[(1,-1),(-1,1)]) 

sage: p = Paths('ababa') 

sage: p.ymin() 

-1 

sage: DyckPaths = WordPaths('ab', steps='dyck') 

sage: p = DyckPaths('abaabb') 

sage: p.ymin() 

0 

sage: w = WordPaths('abcABC', steps='triangle')('ababcaaBC') 

sage: w.ymin() 

0.000000000000000 

""" 

return min(y for (_,y) in self.points()) 

 

def xmax(self): 

r""" 

Returns the maximum of the x-coordinates of the path. 

 

EXAMPLES:: 

 

sage: P = WordPaths('0123') 

sage: p = P('0101013332') 

sage: p.xmax() 

3 

 

This works for any `2d`-paths:: 

 

sage: Paths = WordPaths('ab', steps=[(1,-1),(-1,1)]) 

sage: p = Paths('ababa') 

sage: p.xmax() 

1 

sage: DyckPaths = WordPaths('ab', steps='dyck') 

sage: p = DyckPaths('abaabb') 

sage: p.xmax() 

6 

sage: w = WordPaths('abcABC', steps='triangle')('ababcaaBC') 

sage: w.xmax() 

4.50000000000000 

""" 

return max(x for (x,_) in self.points()) 

 

def ymax(self): 

r""" 

Returns the maximum of the y-coordinates of the path. 

 

EXAMPLES:: 

 

sage: P = WordPaths('0123') 

sage: p = P('0101013332') 

sage: p.ymax() 

3 

 

This works for any `2d`-paths:: 

 

sage: Paths = WordPaths('ab', steps=[(1,-1),(-1,1)]) 

sage: p = Paths('ababa') 

sage: p.ymax() 

0 

sage: DyckPaths = WordPaths('ab', steps='dyck') 

sage: p = DyckPaths('abaabb') 

sage: p.ymax() 

2 

sage: w = WordPaths('abcABC', steps='triangle')('ababcaaBC') 

sage: w.ymax() 

2.59807621135332 

""" 

return max(y for (_,y) in self.points()) 

 

 

class FiniteWordPath_3d(FiniteWordPath_all): 

def plot(self, pathoptions=dict(rgbcolor='red',arrow_head=True,thickness=3), 

startpoint=True, startoptions=dict(rgbcolor='red',size=10)): 

r""" 

INPUT: 

 

- ``pathoptions`` - (dict, default:dict(rgbcolor='red',arrow_head=True, 

thickness=3)), options for the path drawing 

 

- ``startpoint`` - (boolean, default: True), draw the start point? 

 

- ``startoptions`` - (dict, default:dict(rgbcolor='red',size=10)) 

options for the start point drawing 

 

EXAMPLES:: 

 

sage: d = ( vector((1,3,2)), vector((2,-4,5)) ) 

sage: P = WordPaths(alphabet='ab', steps=d); P 

Word Paths over 2 steps 

sage: p = P('ababab'); p 

Path: ababab 

sage: p.plot() 

Graphics3d Object 

 

sage: P = WordPaths('abcABC', steps='cube_grid') 

sage: p = P('abcabcAABBC') 

sage: p.plot() 

Graphics3d Object 

 

""" 

#The following line seems not to work for 3d 

#G = Graphics() 

#so we draw to start a small almost invisible point instead: 

G = point([self.start_point()], size=1) 

pts = list(self.points()) 

if startpoint: 

G += point([pts[0]], **startoptions) 

G += line(pts, **pathoptions) 

return G 

 

####################################################################### 

# # 

# Abstract word path classes # 

# (square grid, hexagonal grid, etc.) # 

# # 

####################################################################### 

 

class FiniteWordPath_square_grid(FiniteWordPath_2d): 

def is_closed(self): 

r""" 

Returns True if self represents a closed path and False otherwise. 

 

EXAMPLES:: 

 

sage: P = WordPaths('abAB', steps='square_grid') 

sage: P('aA').is_closed() 

True 

sage: P('abAB').is_closed() 

True 

sage: P('ababAABB').is_closed() 

True 

sage: P('aaabbbAABB').is_closed() 

False 

sage: P('ab').is_closed() 

False 

""" 

tab = self.abelian_vector() 

return tab[0] == tab[2] and tab[1] == tab[3] 

 

def area(self): 

r""" 

Returns the area of a closed path. 

 

INPUT: 

 

- ``self`` - a closed path 

 

EXAMPLES:: 

 

sage: P = WordPaths('abAB', steps='square_grid') 

sage: P('abAB').area() 

1 

sage: P('aabbAABB').area() 

4 

sage: P('aabbABAB').area() 

3 

 

The area of the Fibonacci tiles:: 

 

sage: [words.fibonacci_tile(i).area() for i in range(6)] 

[1, 5, 29, 169, 985, 5741] 

sage: [words.dual_fibonacci_tile(i).area() for i in range(6)] 

[1, 5, 29, 169, 985, 5741] 

sage: oeis(_)[0] # optional -- internet 

A001653: Numbers n such that 2*n^2 - 1 is a square. 

sage: _.first_terms() # optional -- internet 

(1, 

5, 

29, 

169, 

985, 

5741, 

33461, 

195025, 

1136689, 

6625109, 

38613965, 

225058681, 

1311738121, 

7645370045, 

44560482149, 

259717522849, 

1513744654945, 

8822750406821, 

51422757785981, 

299713796309065, 

1746860020068409, 

10181446324101389, 

59341817924539925) 

 

TESTS:: 

 

sage: P = WordPaths('abAB', steps='square_grid') 

sage: P('a').area() 

Traceback (most recent call last): 

... 

TypeError: the path must be closed to compute its area 

 

""" 

if not self.is_closed(): 

raise TypeError("the path must be closed to compute its area") 

return abs(self._area_vh()) 

 

def _area_vh(path, x=0, y=0): 

r""" 

Returns the area of path, with starting point (x,y) using VH algorithm. 

 

INPUT: 

x, y -- starting point 

 

EXAMPLES:: 

 

sage: P = WordPaths('abAB', steps='square_grid') 

sage: P('abAB')._area_vh() 

-1 

sage: P('aabbAABB')._area_vh() 

-4 

sage: P('aabbABAB')._area_vh() 

-3 

 

REFERENCES: 

Annie Lacasse Memoire. 

""" 

area = 0 

a,b,A,B = path.parent().alphabet() 

 

for move in path: 

if move == b: 

area -= x 

y += 1 

elif move == B: 

area += x 

y -= 1 

elif move == a: 

area += y 

x += 1 

elif move == A: 

area -= y 

x -= 1 

return area // 2 

 

def is_simple(self): 

r""" 

Returns True if the path is simple, i.e. if all its points are 

distincts. 

 

If the path is closed, the last point is not considered. 

 

.. note:: 

 

The linear algorithm described in the thesis of Xavier Provençal 

should be implemented here. 

 

EXAMPLES:: 

 

sage: P = WordPaths('abAB', steps='square_grid') 

sage: P('abab').is_simple() 

True 

sage: P('abAB').is_simple() 

True 

sage: P('abA').is_simple() 

True 

sage: P('aabABB').is_simple() 

False 

sage: P().is_simple() 

True 

sage: P('A').is_simple() 

True 

sage: P('aA').is_simple() 

True 

sage: P('aaA').is_simple() 

False 

 

REFERENCES: 

 

- Provençal, X., Combinatoires des mots, geometrie discrete et 

pavages, These de doctorat en Mathematiques, Montreal, UQAM, 

septembre 2008, 115 pages. 

""" 

return super(FiniteWordPath_square_grid,self).is_simple() 

 

def tikz_trajectory(self): 

r""" 

Returns the trajectory of self as a tikz str. 

 

EXAMPLES:: 

 

sage: f = words.fibonacci_tile(1) 

sage: f.tikz_trajectory() 

'(0, 0) -- (0, -1) -- (-1, -1) -- (-1, -2) -- (0, -2) -- (0, -3) -- (1, -3) -- (1, -2) -- (2, -2) -- (2, -1) -- (1, -1) -- (1, 0) -- (0, 0)' 

""" 

return ' -- '.join(map(str,self.points())) 

 

class FiniteWordPath_triangle_grid(FiniteWordPath_2d): 

# Triangle grid paths are implemented with quadratic fields, 

# and the ordering of such elements is currently problematic: 

# 

# sage: Q.<sqrt3> = QuadraticField(3) 

# sage: sqrt3 > 0 

# True 

# sage: 0 < sqrt3 

# False 

# sage: max(2*sqrt3, sqrt3/10) 

# 1/10*sqrt3 

# 

# Therefore, the functions xmin(), xmax(), ymin() and ymax() are 

# redefined here with conversion to RR in order to avoid this problem 

def xmin(self): 

r""" 

Returns the minimum of the x-coordinates of the path. 

 

EXAMPLES:: 

 

sage: w = WordPaths('abcABC', steps='triangle')('ababcaaBC') 

sage: w.xmin() 

0.000000000000000 

sage: w = WordPaths('abcABC', steps='triangle')('ABAcacacababababcbcbAC') 

sage: w.xmin() 

-3.00000000000000 

""" 

return min(RR(x) for (x,_) in self.points()) 

 

def ymin(self): 

r""" 

Returns the minimum of the y-coordinates of the path. 

 

EXAMPLES:: 

 

sage: w = WordPaths('abcABC', steps='triangle')('ababcaaBC') 

sage: w.ymin() 

0.000000000000000 

sage: w = WordPaths('abcABC', steps='triangle')('ABAcacacababababcbcbAC') 

sage: w.ymin() 

-0.866025403784439 

""" 

return min(RR(y) for (_,y) in self.points()) 

 

def xmax(self): 

r""" 

Returns the maximum of the x-coordinates of the path. 

 

EXAMPLES:: 

 

sage: w = WordPaths('abcABC', steps='triangle')('ababcaaBC') 

sage: w.xmax() 

4.50000000000000 

sage: w = WordPaths('abcABC', steps='triangle')('ABAcacacababababcbcbAC') 

sage: w.xmax() 

4.00000000000000 

""" 

return max(RR(x) for (x,_) in self.points()) 

 

def ymax(self): 

r""" 

Returns the maximum of the y-coordinates of the path. 

 

EXAMPLES:: 

 

sage: w = WordPaths('abcABC', steps='triangle')('ababcaaBC') 

sage: w.ymax() 

2.59807621135332 

sage: w = WordPaths('abcABC', steps='triangle')('ABAcacacababababcbcbAC') 

sage: w.ymax() 

8.66025403784439 

""" 

return max(RR(y) for (_,y) in self.points()) 

 

#TODO: faire une verification du mot pour etre sur hexagonal grid 

class FiniteWordPath_hexagonal_grid(FiniteWordPath_triangle_grid): 

def __init__(self, parent, *args, **kwds): 

r""" 

INPUT: 

 

- ``parent`` - a parent object inheriting from Words_all 

that has the alphabet attribute defined 

 

- ``*args, **kwds`` - arguments accepted by AbstractWord 

 

EXAMPLES:: 

 

sage: F = WordPaths('abcdef', steps='hexagon'); F 

Word Paths on the hexagonal grid 

sage: f = F('aaabbbccddef'); f 

Path: aaabbbccddef 

 

:: 

 

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

True 

 

""" 

super(FiniteWordPath_hexagonal_grid, self).__init__(parent, *args, **kwds) 

 

class FiniteWordPath_cube_grid(FiniteWordPath_3d): 

pass 

 

class FiniteWordPath_north_east(FiniteWordPath_2d): 

pass 

 

class FiniteWordPath_dyck(FiniteWordPath_2d): 

pass 

 

####################################################################### 

# # 

# Concrete word path classes # 

# # 

# It would be nice if those were created inline... # 

# We must ask if Nicolas Thiery was able to convince Sage # 

# people about this. # 

# # 

####################################################################### 

 

##### Finite paths ##### 

 

class FiniteWordPath_all_list(WordDatatype_list, FiniteWordPath_all, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths(['a','b'],[(1,2,0,0),(3,4,0,0)]) 

sage: p = P(['a','b','a']);p 

Path: aba 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_all_list'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_all_str(WordDatatype_str, FiniteWordPath_all, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths('ab',[(1,2,0,0),(3,4,0,0)]) 

sage: p = P('aabbb'); p 

Path: aabbb 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_all_str'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_all_tuple(WordDatatype_tuple, FiniteWordPath_all, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths('ab',[(1,2,0,0),(3,4,0,0)]) 

sage: p = P( ('a','b','b') ); p 

Path: abb 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_all_tuple'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_all_iter_with_caching(WordDatatype_iter_with_caching, FiniteWordPath_all, FiniteWord_class): 

pass 

 

class FiniteWordPath_all_iter(WordDatatype_iter, FiniteWordPath_all, FiniteWord_class): 

pass 

 

class FiniteWordPath_all_callable_with_caching(WordDatatype_callable_with_caching, FiniteWordPath_all, FiniteWord_class): 

pass 

 

class FiniteWordPath_all_callable(WordDatatype_callable, FiniteWordPath_all, FiniteWord_class): 

pass 

 

##### Finite paths on 2d ##### 

 

class FiniteWordPath_2d_list(WordDatatype_list, FiniteWordPath_2d, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths(['a','b'],[(1,2),(3,4)]) 

sage: p = P(['a','b','a']);p 

Path: aba 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_2d_list'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_2d_str(WordDatatype_str, FiniteWordPath_2d, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths(['a','b'],[(1,2),(3,4)]) 

sage: p = P('aba'); p 

Path: aba 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_2d_str'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_2d_tuple(WordDatatype_tuple, FiniteWordPath_2d, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths(['a','b'],[(1,2),(3,4)]) 

sage: p = P(('a','b','a'));p 

Path: aba 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_2d_tuple'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_2d_iter_with_caching(WordDatatype_iter_with_caching, FiniteWordPath_2d, FiniteWord_class): 

pass 

 

class FiniteWordPath_2d_iter(WordDatatype_iter, FiniteWordPath_2d, FiniteWord_class): 

pass 

 

class FiniteWordPath_2d_callable_with_caching(WordDatatype_callable_with_caching, FiniteWordPath_2d, FiniteWord_class): 

pass 

 

class FiniteWordPath_2d_callable(WordDatatype_callable, FiniteWordPath_2d, FiniteWord_class): 

pass 

 

##### Finite paths on 3d ##### 

 

class FiniteWordPath_3d_list(WordDatatype_list, FiniteWordPath_3d, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths(['a','b'],[(1,2,0),(3,4,0)]) 

sage: p = P(['a','b','a']);p 

Path: aba 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_3d_list'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_3d_str(WordDatatype_str, FiniteWordPath_3d, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths(['a','b'],[(1,2,0),(3,4,0)]) 

sage: p = P('aba'); p 

Path: aba 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_3d_str'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_3d_tuple(WordDatatype_tuple, FiniteWordPath_3d, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths(['a','b'],[(1,2,0),(3,4,0)]) 

sage: p = P(('a','b','a'));p 

Path: aba 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_3d_tuple'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_3d_iter_with_caching(WordDatatype_iter_with_caching, FiniteWordPath_3d, FiniteWord_class): 

pass 

 

class FiniteWordPath_3d_iter(WordDatatype_iter, FiniteWordPath_3d, FiniteWord_class): 

pass 

 

class FiniteWordPath_3d_callable_with_caching(WordDatatype_callable_with_caching, FiniteWordPath_3d, FiniteWord_class): 

pass 

 

class FiniteWordPath_3d_callable(WordDatatype_callable, FiniteWordPath_3d, FiniteWord_class): 

pass 

 

##### Finite paths on square grid ##### 

 

class FiniteWordPath_square_grid_list(WordDatatype_list, FiniteWordPath_square_grid, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths('abcd', steps='square') 

sage: p = P(['a','b','b']); p 

Path: abb 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_square_grid_list'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_square_grid_str(WordDatatype_str, FiniteWordPath_square_grid, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths('abcd', steps='square') 

sage: p = P('abccc'); p 

Path: abccc 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_square_grid_str'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_square_grid_tuple(WordDatatype_tuple, FiniteWordPath_square_grid, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths('abcd', steps='square') 

sage: p = P(('a','b','b')); p 

Path: abb 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_square_grid_tuple'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_square_grid_iter_with_caching(WordDatatype_iter_with_caching, FiniteWordPath_square_grid, FiniteWord_class): 

pass 

 

class FiniteWordPath_square_grid_iter(WordDatatype_iter, FiniteWordPath_square_grid, FiniteWord_class): 

pass 

 

class FiniteWordPath_square_grid_callable_with_caching(WordDatatype_callable_with_caching, FiniteWordPath_square_grid, FiniteWord_class): 

pass 

 

class FiniteWordPath_square_grid_callable(WordDatatype_callable, FiniteWordPath_square_grid, FiniteWord_class): 

pass 

 

##### Unknown length paths on square grid (experimental) ##### 

 

#class WordPath_square_grid_iter_with_caching(WordDatatype_iter_with_caching, FiniteWordPath_square_grid, Word_class): 

# pass 

 

##### Finite paths on triangle grid ##### 

 

class FiniteWordPath_triangle_grid_list(WordDatatype_list, FiniteWordPath_triangle_grid, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths('abcdef', steps='triangle') 

sage: p = P(['a','b','b']); p 

Path: abb 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_triangle_grid_list'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_triangle_grid_str(WordDatatype_str, FiniteWordPath_triangle_grid, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths('abcdef', steps='triangle') 

sage: p = P('abb'); p 

Path: abb 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_triangle_grid_str'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_triangle_grid_tuple(WordDatatype_tuple, FiniteWordPath_triangle_grid, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths('abcdef', steps='triangle') 

sage: p = P(('a','b','b')); p 

Path: abb 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_triangle_grid_tuple'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_triangle_grid_iter_with_caching(WordDatatype_iter_with_caching, FiniteWordPath_triangle_grid, FiniteWord_class): 

pass 

 

class FiniteWordPath_triangle_grid_iter(WordDatatype_iter, FiniteWordPath_triangle_grid, FiniteWord_class): 

pass 

 

class FiniteWordPath_triangle_grid_callable_with_caching(WordDatatype_callable_with_caching, FiniteWordPath_triangle_grid, FiniteWord_class): 

pass 

 

class FiniteWordPath_triangle_grid_callable(WordDatatype_callable, FiniteWordPath_triangle_grid, FiniteWord_class): 

pass 

 

##### Finite paths on hexagonal grid ##### 

 

class FiniteWordPath_hexagonal_grid_list(WordDatatype_list, FiniteWordPath_hexagonal_grid, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths('abcdef', steps='hexagon') 

sage: p = P(['a','b','b']); p 

Path: abb 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_hexagonal_grid_list'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_hexagonal_grid_str(WordDatatype_str, FiniteWordPath_hexagonal_grid, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths('abcdef', steps='hexagon') 

sage: p = P('abb'); p 

Path: abb 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_hexagonal_grid_str'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_hexagonal_grid_tuple(WordDatatype_tuple, FiniteWordPath_hexagonal_grid, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths('abcdef', steps='hexagon') 

sage: p = P(('a','b','b')); p 

Path: abb 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_hexagonal_grid_tuple'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_hexagonal_grid_iter_with_caching(WordDatatype_iter_with_caching, FiniteWordPath_hexagonal_grid, FiniteWord_class): 

pass 

 

class FiniteWordPath_hexagonal_grid_iter(WordDatatype_iter, FiniteWordPath_hexagonal_grid, FiniteWord_class): 

pass 

 

class FiniteWordPath_hexagonal_grid_callable_with_caching(WordDatatype_callable_with_caching, FiniteWordPath_hexagonal_grid, FiniteWord_class): 

pass 

 

class FiniteWordPath_hexagonal_grid_callable(WordDatatype_callable, FiniteWordPath_hexagonal_grid, FiniteWord_class): 

pass 

 

##### Finite paths on cube grid ##### 

 

class FiniteWordPath_cube_grid_list(WordDatatype_list, FiniteWordPath_cube_grid, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths('abcdef', steps='cube') 

sage: p = P(['a','b','b']); p 

Path: abb 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_cube_grid_list'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_cube_grid_str(WordDatatype_str, FiniteWordPath_cube_grid, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths('abcdef', steps='cube') 

sage: p = P('abb'); p 

Path: abb 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_cube_grid_str'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_cube_grid_tuple(WordDatatype_tuple, FiniteWordPath_cube_grid, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths('abcdef', steps='cube') 

sage: p = P(('a','b','b')); p 

Path: abb 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_cube_grid_tuple'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_cube_grid_iter_with_caching(WordDatatype_iter_with_caching, FiniteWordPath_cube_grid, FiniteWord_class): 

pass 

 

class FiniteWordPath_cube_grid_iter(WordDatatype_iter, FiniteWordPath_cube_grid, FiniteWord_class): 

pass 

 

class FiniteWordPath_cube_grid_callable_with_caching(WordDatatype_callable_with_caching, FiniteWordPath_cube_grid, FiniteWord_class): 

pass 

 

class FiniteWordPath_cube_grid_callable(WordDatatype_callable, FiniteWordPath_cube_grid, FiniteWord_class): 

pass 

 

##### Finite paths on north_east ##### 

 

class FiniteWordPath_north_east_list(WordDatatype_list, FiniteWordPath_north_east, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths('ab', steps='ne') 

sage: p = P(['a','b','b']); p 

Path: abb 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_north_east_list'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_north_east_str(WordDatatype_str, FiniteWordPath_north_east, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths('ab', steps='ne') 

sage: p = P('abb'); p 

Path: abb 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_north_east_str'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_north_east_tuple(WordDatatype_tuple, FiniteWordPath_north_east, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths('ab', steps='ne') 

sage: p = P(('a','b','b')); p 

Path: abb 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_north_east_tuple'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_north_east_iter_with_caching(WordDatatype_iter_with_caching, FiniteWordPath_north_east, FiniteWord_class): 

pass 

 

class FiniteWordPath_north_east_iter(WordDatatype_iter, FiniteWordPath_north_east, FiniteWord_class): 

pass 

 

class FiniteWordPath_north_east_callable_with_caching(WordDatatype_callable_with_caching, FiniteWordPath_north_east, FiniteWord_class): 

pass 

 

class FiniteWordPath_north_east_callable(WordDatatype_callable, FiniteWordPath_north_east, FiniteWord_class): 

pass 

 

##### Finite paths on dyck ##### 

 

class FiniteWordPath_dyck_list(WordDatatype_list, FiniteWordPath_dyck, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths('ab', steps='dyck') 

sage: p = P(['a','b','b']); p 

Path: abb 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_dyck_list'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_dyck_str(WordDatatype_str, FiniteWordPath_dyck, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths('ab', steps='dyck') 

sage: p = P('abb'); p 

Path: abb 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_dyck_str'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_dyck_tuple(WordDatatype_tuple, FiniteWordPath_dyck, FiniteWord_class): 

r""" 

TESTS:: 

 

sage: P = WordPaths('ab', steps='dyck') 

sage: p = P(('a','b','b')); p 

Path: abb 

sage: type(p) 

<class 'sage.combinat.words.paths.FiniteWordPath_dyck_tuple'> 

sage: p == loads(dumps(p)) 

True 

""" 

pass 

 

class FiniteWordPath_dyck_iter_with_caching(WordDatatype_iter_with_caching, FiniteWordPath_dyck, FiniteWord_class): 

pass 

 

class FiniteWordPath_dyck_iter(WordDatatype_iter, FiniteWordPath_dyck, FiniteWord_class): 

pass 

 

class FiniteWordPath_dyck_callable_with_caching(WordDatatype_callable_with_caching, FiniteWordPath_dyck, FiniteWord_class): 

pass 

 

class FiniteWordPath_dyck_callable(WordDatatype_callable, FiniteWordPath_dyck, FiniteWord_class): 

pass