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

r""" 

Knutson-Tao Puzzles 

 

This module implements a generic algorithm to solve Knutson-Tao puzzles. An 

instance of this class will be callable: the arguments are the labels of 

north-east and north-west sides of the puzzle boundary; the output is the list 

of the fillings of the puzzle with the specified pieces. 

 

Acknowledgements 

---------------- 

 

This code was written during Sage Days 45 at ICERM with Franco Saliola, Anne Schilling, and Avinash Dalal in discussions with Allen Knutson. 

The code was tested afterwards by Liz Beazley and Ed Richmond. 

 

.. TODO:: 

 

Functionality to add: 

 

- plotter will not plot edge labels higher than 2; e.g. in BK puzzles, the labels are 

1,..., n and so in 3-step examples, none of the edge labels with 3 appear 

 

- we should also have a 3-step puzzle pieces constructor, taken from p22 of 

:arXiv:`math/0610538` 

 

- implement the bijection from puzzles to tableaux; see for example 

R. Vakil, A geometric Littlewood-Richardson rule, :arXiv:`math/0302294` 

or K. Purbhoo, Puzzles, Tableaux and Mosaics, :arXiv:`0705.1184`. 

""" 

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

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

# 2013 Allen Knutson, 

# 2013 Avinash Dalal, 

# 2013 Anne Schilling, 

# 2013 Elizabeth Beazley, 

# 2013 Ed Richmond 

# 

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

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

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

from sage.plot.graphics import Graphics 

from sage.plot.polygon import polygon 

from sage.plot.line import line 

from sage.plot.text import text 

from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing 

from sage.rings.finite_rings.integer_mod_ring import Integers 

from sage.plot.plot import graphics_array 

from sage.misc.cachefunc import cached_method 

from sage.structure.unique_representation import UniqueRepresentation 

 

 

class PuzzlePiece(object): 

r""" 

Abstract class for puzzle pieces. 

 

This abstract class contains information on how to test equality of 

puzzle pieces, and sets color and plotting options. 

""" 

 

def __eq__(self, other): 

r""" 

TESTS:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece 

sage: delta = DeltaPiece('a','b','c') 

sage: delta1 = DeltaPiece('a','b','c') 

sage: delta == delta1 

True 

sage: delta1 = DeltaPiece('A','b','c') 

sage: delta == delta1 

False 

""" 

if isinstance(other, type(self)): 

return self.border() == other.border() 

else: 

return False 

 

def __hash__(self): 

r""" 

TESTS:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece 

sage: delta = DeltaPiece('a','b','c') 

sage: hash(delta) == hash(delta) 

True 

""" 

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

 

def border(self): 

r""" 

Returns the border of ``self``. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece 

sage: delta = DeltaPiece('a','b','c') 

sage: delta.border() 

('a', 'b', 'c') 

""" 

return tuple(self.edge_label(edge) for edge in self.edges()) 

 

def color(self): 

r""" 

Returns the color of ``self``. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece 

sage: delta = DeltaPiece('a','b','c') 

sage: delta.color() 

'white' 

sage: delta = DeltaPiece('0','0','0') 

sage: delta.color() 

'red' 

sage: delta = DeltaPiece('1','1','1') 

sage: delta.color() 

'blue' 

sage: delta = DeltaPiece('2','2','2') 

sage: delta.color() 

'green' 

sage: delta = DeltaPiece('2','K','2') 

sage: delta.color() 

'orange' 

sage: delta = DeltaPiece('2','T1/2','2') 

sage: delta.color() 

'yellow' 

""" 

colors = {('0','0','0'): 'red', 

('1','1','1'): 'blue', 

('2','2','2'): 'green'} 

border = self.border() 

if border in colors: 

color = colors[border] 

elif 'K' in border: 

color = 'orange' 

elif '10' in border: 

color = 'white' 

elif any(label.startswith('T') for label in border): 

color = 'yellow' 

else: 

color = 'white' 

return color 

 

def _plot_label(self, label, coords, fontcolor=(0.3,0.3,0.3), fontsize=15, rotation=0): 

r""" 

TESTS:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece 

sage: delta = DeltaPiece('2','K','2') 

sage: delta._plot_label('1',(1,1)) # not tested 

""" 

if label in ('0', '1', '2'): 

return text(label, coords, color=fontcolor, fontsize=fontsize, rotation=rotation) 

else: 

return Graphics() 

 

def _plot_piece(self, coords, border_color=(0.5,0.5,0.5), border_thickness=1, style='fill'): 

r""" 

TESTS:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece 

sage: delta = DeltaPiece('2','K','2') 

sage: delta._plot_piece([(1,1),(1,2),(2,2)]) # not tested 

""" 

if style == 'fill': 

P = polygon(coords, color=self.color()) 

P += polygon(coords, fill=False, color=border_color, thickness=border_thickness) 

return P 

elif style == 'edges': 

if isinstance(self, DeltaPiece): 

edges = ('north_west', 'south', 'north_east') 

elif isinstance(self, NablaPiece): 

edges = ('south_west', 'north', 'south_east') 

else: 

edges = self.edges() 

P = Graphics() 

for (i, edge) in enumerate(edges): 

P += line([coords[i], coords[(i+1)%3]], color=self.edge_color(edge), thickness=border_thickness) 

return P 

else: 

return NotImplemented 

 

def edge_color(self, edge): 

r""" 

Color of the specified edge of ``self`` (to be used when plotting the 

piece). 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece 

sage: delta = DeltaPiece('1','0','10') 

sage: delta.edge_color('south') 

'blue' 

sage: delta.edge_color('north_west') 

'red' 

sage: delta.edge_color('north_east') 

'white' 

""" 

edge_label = self.edge_label(edge) 

colors = {'1': 'blue', '0': 'red'} 

if edge_label in colors: 

color = colors[edge_label] 

elif 'K' in edge_label: 

color = 'orange' 

elif edge_label.startswith('T'): 

color = 'yellow' 

else: 

color = 'white' 

return color 

 

def edge_label(self, edge): 

r""" 

Return the edge label of ``edge``. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece 

sage: delta = DeltaPiece('2','K','2') 

sage: delta.edge_label('south') 

'2' 

sage: delta.edge_label('north_east') 

'2' 

sage: delta.edge_label('north_west') 

'K' 

""" 

return self._edge_labels[edge] 

 

__getitem__ = edge_label 

 

class NablaPiece(PuzzlePiece): 

r""" 

Nabla Piece takes as input three labels, inputted as strings. They label 

the North, Southeast and Southwest edges, respectively. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import NablaPiece 

sage: NablaPiece('a','b','c') 

c\a/b 

""" 

 

def __init__(self, north, south_east, south_west): 

r""" 

INPUT: 

 

- ``north``, ``south_east``, ``south_west`` -- strings, which label the edges 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import NablaPiece 

sage: NablaPiece('1','2','3') 

3\1/2 

""" 

self._edge_labels = dict(north=north, south_east=south_east, south_west=south_west) 

 

def __repr__(self): 

r""" 

Prints the labels of the Nabla piece. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import NablaPiece 

sage: NablaPiece('1','2','3') 

3\1/2 

""" 

return "%s\%s/%s" % (self['south_west'], 

self['north'], 

self['south_east']) 

 

def clockwise_rotation(self): 

r""" 

Rotates the Nabla piece by 120 degree clockwise. 

 

OUTPUT: 

 

- Nabla piece 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import NablaPiece 

sage: nabla = NablaPiece('1','2','3') 

sage: nabla.clockwise_rotation() 

2\3/1 

""" 

return NablaPiece(north=self['south_west'], 

south_east=self['north'], 

south_west=self['south_east']) 

 

def half_turn_rotation(self): 

r""" 

Rotates the Nabla piece by 180 degree. 

 

OUTPUT: 

 

- Delta piece 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import NablaPiece 

sage: nabla = NablaPiece('1','2','3') 

sage: nabla.half_turn_rotation() 

2/1\3 

""" 

return DeltaPiece(south=self['north'], 

north_west=self['south_east'], 

north_east=self['south_west']) 

 

def edges(self): 

r""" 

Return the tuple of edge names. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import NablaPiece 

sage: nabla = NablaPiece('1','2','3') 

sage: nabla.edges() 

('north', 'south_east', 'south_west') 

""" 

return ('north', 'south_east', 'south_west') 

 

class DeltaPiece(PuzzlePiece): 

r""" 

Delta Piece takes as input three labels, inputted as strings. They label 

the South, Northwest and Northeast edges, respectively. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece 

sage: DeltaPiece('a','b','c') 

b/a\c 

""" 

 

def __init__(self, south, north_west, north_east): 

r""" 

INPUT: 

 

- ``south``, ``north_west``, ``north_east`` -- strings, which label the edges 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece 

sage: DeltaPiece('1','2','3') 

2/1\3 

""" 

self._edge_labels = dict(south=south, north_west=north_west, north_east=north_east) 

 

def __repr__(self): 

r""" 

Prints the labels of the Delta piece. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece 

sage: DeltaPiece('1','2','3') 

2/1\3 

""" 

return "%s/%s\%s" % (self['north_west'], 

self['south'], 

self['north_east']) 

 

def clockwise_rotation(self): 

r""" 

Rotates the Delta piece by 120 degree clockwise. 

 

OUTPUT: 

 

- Delta piece 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece 

sage: delta = DeltaPiece('1','2','3') 

sage: delta.clockwise_rotation() 

1/3\2 

""" 

return DeltaPiece(south=self['north_east'], 

north_west=self['south'], 

north_east=self['north_west']) 

 

def half_turn_rotation(self): 

r""" 

Rotates the Delta piece by 180 degree. 

 

OUTPUT: 

 

- Nabla piece 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece 

sage: delta = DeltaPiece('1','2','3') 

sage: delta.half_turn_rotation() 

3\1/2 

""" 

return NablaPiece(north=self['south'], 

south_east=self['north_west'], 

south_west=self['north_east']) 

 

def edges(self): 

r""" 

Return the tuple of edge names. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece 

sage: delta = DeltaPiece('1','2','3') 

sage: delta.edges() 

('south', 'north_west', 'north_east') 

""" 

return ('south', 'north_west', 'north_east') 

 

class RhombusPiece(PuzzlePiece): 

r""" 

Class of rhombi pieces. 

 

To construct a rhombus piece we input a delta and a nabla piece. 

The delta and nabla pieces are joined along the south and north edge, 

respectively. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece, NablaPiece, RhombusPiece 

sage: delta = DeltaPiece('1','2','3') 

sage: nabla = NablaPiece('4','5','6') 

sage: RhombusPiece(delta,nabla) 

2/\3 6\/5 

""" 

def __init__(self, north_piece, south_piece): 

r""" 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece, NablaPiece, RhombusPiece 

sage: delta = DeltaPiece('1','2','3') 

sage: nabla = NablaPiece('4','5','6') 

sage: RhombusPiece(delta,nabla) 

2/\3 6\/5 

""" 

self._north_piece = north_piece 

self._south_piece = south_piece 

self._edge_labels = dict(north_west=north_piece['north_west'], 

north_east=north_piece['north_east'], 

south_east=south_piece['south_east'], 

south_west=south_piece['south_west']) 

 

def __iter__(self): 

r""" 

Return the list of the north and south piece. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece, NablaPiece, RhombusPiece 

sage: delta = DeltaPiece('1','2','3') 

sage: nabla = NablaPiece('4','5','6') 

sage: r = RhombusPiece(delta,nabla) 

sage: list(r) 

[2/1\3, 6\4/5] 

""" 

yield self._north_piece 

yield self._south_piece 

 

def north_piece(self): 

r""" 

Return the north piece. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece, NablaPiece, RhombusPiece 

sage: delta = DeltaPiece('1','2','3') 

sage: nabla = NablaPiece('4','5','6') 

sage: r = RhombusPiece(delta,nabla) 

sage: r.north_piece() 

2/1\3 

""" 

return self._north_piece 

 

def south_piece(self): 

r""" 

Return the south piece. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece, NablaPiece, RhombusPiece 

sage: delta = DeltaPiece('1','2','3') 

sage: nabla = NablaPiece('4','5','6') 

sage: r = RhombusPiece(delta,nabla) 

sage: r.south_piece() 

6\4/5 

""" 

return self._south_piece 

 

def __repr__(self): 

r""" 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece, NablaPiece, RhombusPiece 

sage: delta = DeltaPiece('1','2','3') 

sage: nabla = NablaPiece('4','5','6') 

sage: RhombusPiece(delta,nabla) 

2/\3 6\/5 

""" 

return "%s/\%s %s\/%s" % (self['north_west'], self['north_east'], 

self['south_west'], self['south_east']) 

 

def edges(self): 

r""" 

Return the tuple of edge names. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece, NablaPiece, RhombusPiece 

sage: delta = DeltaPiece('1','2','3') 

sage: nabla = NablaPiece('4','5','6') 

sage: RhombusPiece(delta,nabla).edges() 

('north_west', 'north_east', 'south_east', 'south_west') 

""" 

return ('north_west', 'north_east', 'south_east', 'south_west') 

 

class PuzzlePieces(object): 

r""" 

Construct a valid set of puzzle pieces. 

 

This class constructs the set of valid puzzle pieces. It can take a list of 

forbidden border labels as input. These labels are forbidden from appearing 

on the south edge of a puzzle filling. The user can add valid nabla or 

delta pieces and specify which rotations of these pieces are legal. For 

example, ``rotations=0`` does not add any additional pieces (only the piece 

itself), ``rotations=60`` adds six pieces (the pieces and its rotations by 

60, 120, 180, 240, 300), etc.. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import PuzzlePieces, NablaPiece 

sage: forbidden_border_labels = ['10'] 

sage: pieces = PuzzlePieces(forbidden_border_labels) 

sage: pieces.add_piece(NablaPiece('0','0','0'), rotations=60) 

sage: pieces.add_piece(NablaPiece('1','1','1'), rotations=60) 

sage: pieces.add_piece(NablaPiece('1','0','10'), rotations=60) 

sage: pieces 

Nablas : [0\0/0, 0\10/1, 10\1/0, 1\0/10, 1\1/1] 

Deltas : [0/0\0, 0/1\10, 1/10\0, 1/1\1, 10/0\1] 

 

The user can obtain the list of valid rhombi pieces as follows:: 

 

sage: sorted([p for p in pieces.rhombus_pieces()], key=str) 

[0/\0 0\/0, 0/\0 1\/10, 0/\10 10\/0, 0/\10 1\/1, 1/\0 0\/1, 

1/\1 10\/0, 1/\1 1\/1, 10/\1 0\/0, 10/\1 1\/10] 

""" 

def __init__(self, forbidden_border_labels=None): 

r""" 

INPUT: 

 

- ``forbidden_border_labels`` -- list of forbidden border labels given as strings 

 

TESTS:: 

 

sage: from sage.combinat.knutson_tao_puzzles import PuzzlePieces 

sage: forbidden_border_labels = ['10'] 

sage: pieces = PuzzlePieces(forbidden_border_labels) 

sage: pieces 

Nablas : [] 

Deltas : [] 

 

sage: PuzzlePieces('10') 

Traceback (most recent call last): 

... 

TypeError: Input must be a list 

""" 

self._nabla_pieces = set([]) 

self._delta_pieces = set([]) 

if forbidden_border_labels is None: 

forbidden_border_labels = [] 

if not isinstance(forbidden_border_labels,list): 

raise TypeError("Input must be a list") 

self._forbidden_border_labels = forbidden_border_labels 

 

def __eq__(self, other): 

r""" 

TESTS:: 

 

sage: from sage.combinat.knutson_tao_puzzles import H_grassmannian_pieces 

sage: x = H_grassmannian_pieces() 

sage: y = H_grassmannian_pieces() 

sage: x == y 

True 

""" 

if isinstance(other, type(self)): 

return self.__dict__ == other.__dict__ 

else: 

return False 

 

def __hash__(self): 

r""" 

TESTS:: 

 

sage: from sage.combinat.knutson_tao_puzzles import H_grassmannian_pieces 

sage: x = H_grassmannian_pieces() 

sage: hash(x) == hash(x) 

True 

""" 

return hash((type(self), repr(self))) 

 

def add_piece(self, piece, rotations=120): 

r""" 

Adds ``piece`` to the list of pieces. 

 

INPUT: 

 

- ``piece`` -- a nabla piece or a delta piece 

- ``rotations`` -- (default: 120) 0, 60, 120, 180 

 

The user can add valid nabla or delta pieces and specify 

which rotations of these pieces are legal. For example, ``rotations=0`` 

does not add any additional pieces (only the piece itself), ``rotations=60`` adds 

six pieces (namely three delta and three nabla pieces), while 

``rotations=120`` adds only delta or nabla (depending on which piece ``self`` is). 

``rotations=180`` adds the piece and its 180 degree rotation, i.e. one delta and one 

nabla piece. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import PuzzlePieces, DeltaPiece 

sage: delta = DeltaPiece('a','b','c') 

sage: pieces = PuzzlePieces() 

sage: pieces 

Nablas : [] 

Deltas : [] 

sage: pieces.add_piece(delta) 

sage: pieces 

Nablas : [] 

Deltas : [a/c\b, b/a\c, c/b\a] 

 

sage: pieces = PuzzlePieces() 

sage: pieces.add_piece(delta,rotations=0) 

sage: pieces 

Nablas : [] 

Deltas : [b/a\c] 

 

sage: pieces = PuzzlePieces() 

sage: pieces.add_piece(delta,rotations=60) 

sage: pieces 

Nablas : [a\b/c, b\c/a, c\a/b] 

Deltas : [a/c\b, b/a\c, c/b\a] 

""" 

if isinstance(piece, NablaPiece): 

pieces_list = self._nabla_pieces 

else: 

pieces_list = self._delta_pieces 

pieces_list.add(piece) 

if rotations == 120: 

pieces_list.add(piece.clockwise_rotation()) 

pieces_list.add(piece.clockwise_rotation().clockwise_rotation()) 

elif rotations == 180: 

self.add_piece(piece.half_turn_rotation(), rotations=0) 

elif rotations == 60: 

self.add_piece(piece, rotations=120) 

self.add_piece(piece.half_turn_rotation(), rotations=120) 

 

def add_forbidden_label(self, label): 

r""" 

Adds forbidden border labels. 

 

INPUT: 

 

- ``label`` -- string specifying a new forbidden label 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import PuzzlePieces 

sage: pieces = PuzzlePieces() 

sage: pieces.add_forbidden_label('1') 

sage: pieces._forbidden_border_labels 

['1'] 

sage: pieces.add_forbidden_label('2') 

sage: pieces._forbidden_border_labels 

['1', '2'] 

""" 

self._forbidden_border_labels.append(label) 

 

def add_T_piece(self, label1, label2): 

r""" 

Adds a nabla and delta piece with ``label1`` and ``label2``. 

 

This method adds a nabla piece with edges ``label2``\ T``label1``|``label2`` / ``label1``. 

and a delta piece with edges ``label1``/ T``label1``|``label2`` \ ``label2``. 

It also adds T``label1``|``label2`` to the forbidden list. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import PuzzlePieces 

sage: pieces = PuzzlePieces() 

sage: pieces.add_T_piece('1','3') 

sage: pieces 

Nablas : [3\T1|3/1] 

Deltas : [1/T1|3\3] 

sage: pieces._forbidden_border_labels 

['T1|3'] 

""" 

self.add_forbidden_label('T%s|%s' % (label1, label2)) 

self.add_piece(NablaPiece('T%s|%s' % (label1, label2), label1, label2), rotations=180) 

 

def __repr__(self): 

r""" 

TESTS:: 

 

sage: from sage.combinat.knutson_tao_puzzles import PuzzlePieces, DeltaPiece 

sage: pieces = PuzzlePieces() 

sage: delta = DeltaPiece('a','b','c') 

sage: pieces.add_piece(delta,rotations=60) 

sage: pieces 

Nablas : [a\b/c, b\c/a, c\a/b] 

Deltas : [a/c\b, b/a\c, c/b\a] 

""" 

s = "Nablas : %s\n" % sorted([p for p in self._nabla_pieces], key=str) 

s += "Deltas : %s" % sorted([p for p in self._delta_pieces], key=str) 

return s 

 

def delta_pieces(self): 

r""" 

Returns the delta pieces as a set. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import PuzzlePieces, DeltaPiece 

sage: pieces = PuzzlePieces() 

sage: delta = DeltaPiece('a','b','c') 

sage: pieces.add_piece(delta,rotations=60) 

sage: sorted([p for p in pieces.delta_pieces()], key=str) 

[a/c\b, b/a\c, c/b\a] 

""" 

return self._delta_pieces 

 

def nabla_pieces(self): 

r""" 

Returns the nabla pieces as a set. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import PuzzlePieces, DeltaPiece 

sage: pieces = PuzzlePieces() 

sage: delta = DeltaPiece('a','b','c') 

sage: pieces.add_piece(delta,rotations=60) 

sage: sorted([p for p in pieces.nabla_pieces()], key=str) 

[a\b/c, b\c/a, c\a/b] 

""" 

return self._nabla_pieces 

 

def rhombus_pieces(self): 

r""" 

Returns a list of all allowable rhombus pieces. 

 

Allowable rhombus pieces are those where the south edge of the delta 

piece equals the north edge of the nabla piece. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import PuzzlePieces, DeltaPiece 

sage: pieces = PuzzlePieces() 

sage: delta = DeltaPiece('a','b','c') 

sage: pieces.add_piece(delta,rotations=60) 

sage: sorted([p for p in pieces.rhombus_pieces()], key=str) 

[a/\b b\/a, b/\c c\/b, c/\a a\/c] 

""" 

rhombi = set([]) 

for nabla in self._nabla_pieces: 

for delta in self._delta_pieces: 

if delta['south'] == nabla['north']: 

rhombi.add(RhombusPiece(delta, nabla)) 

return rhombi 

 

def boundary_deltas(self): 

r""" 

Returns deltas with south edges not in the forbidden list. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import PuzzlePieces, DeltaPiece 

sage: pieces = PuzzlePieces(['a']) 

sage: delta = DeltaPiece('a','b','c') 

sage: pieces.add_piece(delta,rotations=60) 

sage: sorted([p for p in pieces.boundary_deltas()], key=str) 

[a/c\b, c/b\a] 

""" 

return tuple(delta for delta in self.delta_pieces() 

if delta['south'] not in self._forbidden_border_labels) 

 

def H_grassmannian_pieces(): 

r""" 

Defines the puzzle pieces used in computing the cohomology of the Grassmannian. 

 

REFERENCES: 

 

.. [KTW] Allen Knutson, Terence Tao, Christopher Woodward, 

The honeycomb model of GL(n) tensor products II: Puzzles determine facets of the Littlewood-Richardson cone, 

:arXiv:`math/0107011` 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import H_grassmannian_pieces 

sage: H_grassmannian_pieces() 

Nablas : [0\0/0, 0\10/1, 10\1/0, 1\0/10, 1\1/1] 

Deltas : [0/0\0, 0/1\10, 1/10\0, 1/1\1, 10/0\1] 

""" 

forbidden_border_labels = ['10'] 

pieces = PuzzlePieces(forbidden_border_labels) 

pieces.add_piece(NablaPiece('0','0','0'), rotations=60) 

pieces.add_piece(NablaPiece('1','1','1'), rotations=60) 

pieces.add_piece(NablaPiece('1','0','10'), rotations=60) 

return pieces 

 

def HT_grassmannian_pieces(): 

r""" 

Defines the puzzle pieces used in computing the torus-equivariant cohomology of the Grassmannian. 

 

REFERENCES: 

 

.. [KT2003] Allen Knutson, Terence Tao, Puzzles and (equivariant) cohomology of Grassmannians, 

Duke Math. J. 119 (2003) 221 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import HT_grassmannian_pieces 

sage: HT_grassmannian_pieces() 

Nablas : [0\0/0, 0\10/1, 10\1/0, 1\0/10, 1\1/1, 1\T0|1/0] 

Deltas : [0/0\0, 0/1\10, 0/T0|1\1, 1/10\0, 1/1\1, 10/0\1] 

""" 

pieces = H_grassmannian_pieces() 

pieces.add_T_piece('0', '1') 

return pieces 

 

def K_grassmannian_pieces(): 

r""" 

Defines the puzzle pieces used in computing the K-theory of the Grassmannian. 

 

REFERENCES: 

 

.. [Buch00] \A. Buch, A Littlewood-Richardson rule for the K-theory of Grassmannians, :arXiv:`math.AG/0004137` 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import K_grassmannian_pieces 

sage: K_grassmannian_pieces() 

Nablas : [0\0/0, 0\10/1, 0\K/1, 10\1/0, 1\0/10, 1\0/K, 1\1/1, K\1/0] 

Deltas : [0/0\0, 0/1\10, 1/10\0, 1/1\1, 10/0\1, K/K\K] 

""" 

pieces = H_grassmannian_pieces() 

pieces.add_forbidden_label('K') 

pieces.add_piece(NablaPiece('0','K','1'), rotations=120) 

pieces.add_piece(DeltaPiece('K','K','K'), rotations=0) 

return pieces 

 

def H_two_step_pieces(): 

r""" 

Defines the puzzle pieces used in two step flags. 

 

This rule is currently only conjecturally true. See [BuchKreschTamvakis03]_. 

 

REFERENCES: 

 

.. [BuchKreschTamvakis03] \A. Buch, A. Kresch, H. Tamvakis, Gromov-Witten invariants on Grassmannian, :arXiv:`math/0306388` 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import H_two_step_pieces 

sage: H_two_step_pieces() 

Nablas : [(21)0\21/0, 0\(21)0/21, 0\0/0, 0\10/1, 0\20/2, 10\1/0, 10\2(10)/2, 1\0/10, 1\1/1, 1\21/2, 

2(10)\2/10, 20\2/0, 21\0/(21)0, 21\2/1, 2\0/20, 2\1/21, 2\10/2(10), 2\2/2] 

Deltas : [(21)0/0\21, 0/0\0, 0/1\10, 0/21\(21)0, 0/2\20, 1/10\0, 1/1\1, 1/2\21, 10/0\1, 10/2\2(10), 

2(10)/10\2, 2/2(10)\10, 2/20\0, 2/21\1, 2/2\2, 20/0\2, 21/(21)0\0, 21/1\2] 

""" 

forbidden_border_labels = ['10', '20', '21', '(21)0', '2(10)'] 

pieces = PuzzlePieces(forbidden_border_labels) 

for i in ('0', '1', '2'): 

pieces.add_piece(DeltaPiece(i, i, i), rotations=60) 

for i, j in (('1','0'), ('2','0'), ('2','1')): 

pieces.add_piece(DeltaPiece(i+j, i, j), rotations=60) 

pieces.add_piece(DeltaPiece('(21)0','21','0'), rotations=60) 

pieces.add_piece(DeltaPiece('2(10)','2','10'), rotations=60) 

return pieces 

 

def HT_two_step_pieces(): 

r""" 

Defines the puzzle pieces used in computing the equivariant two step puzzle pieces. 

 

For the puzzle pieces, see Figure 26 on page 22 of [CoskunVakil06]_. 

 

REFERENCES: 

 

.. [CoskunVakil06] \I. Coskun, R. Vakil, Geometric positivity in the cohomology of homogeneous spaces 

and generalized Schubert calculus, :arXiv:`math/0610538` 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import HT_two_step_pieces 

sage: HT_two_step_pieces() 

Nablas : [(21)0\21/0, 0\(21)0/21, 0\0/0, 0\10/1, 0\20/2, 10\1/0, 10\2(10)/2, 

1\0/10, 1\1/1, 1\21/2, 1\T0|1/0, 2(10)\2/10, 20\2/0, 21\0/(21)0, 21\2/1, 21\T0|21/0, 

21\T10|21/10, 2\0/20, 2\1/21, 2\10/2(10), 2\2/2, 2\T0|2/0, 2\T10|2/10, 2\T1|2/1] 

Deltas : [(21)0/0\21, 0/0\0, 0/1\10, 0/21\(21)0, 0/2\20, 0/T0|1\1, 0/T0|21\21, 0/T0|2\2, 

1/10\0, 1/1\1, 1/2\21, 1/T1|2\2, 10/0\1, 10/2\2(10), 10/T10|21\21, 10/T10|2\2, 2(10)/10\2, 

2/2(10)\10, 2/20\0, 2/21\1, 2/2\2, 20/0\2, 21/(21)0\0, 21/1\2] 

""" 

pieces = H_two_step_pieces() 

for label1, label2 in (('0','1'), ('0','2'), ('1','2'), ('10','2'), ('0','21'), ('10','21')): 

pieces.add_T_piece(label1, label2) 

return pieces 

 

 

def BK_pieces(max_letter): 

r""" 

The puzzle pieces used in computing the Belkale-Kumar coefficients for any 

partial flag variety in type `A`. 

 

There are two types of puzzle pieces: 

 

- a triangle, with each edge labeled with the same letter; 

- a rhombus, with edges labeled `i`, `j`, `i`, `j` in clockwise order with 

`i > j`. 

 

Each of these is rotated by 60 degrees, but not reflected. 

 

We model the rhombus pieces as two triangles: a delta piece north-west 

label `i`, north-east label `j` and south label `i(j)`; and a nabla piece 

with south-east label `i`, south-west label `j` and north label `i(j)`. 

 

INPUT: 

 

- ``max_letter`` -- positive integer specifying the number of steps in the 

partial flag variety, equivalently, the number of elements in the 

alphabet for the edge labels. The smallest label is `1`. 

 

REFERENCES: 

 

.. [KnutsonPurbhoo10] \A. Knutson, K. Purbhoo, Product and puzzle formulae 

for `GL_n` Belkale-Kumar coefficients, :arXiv:`1008.4979` 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import BK_pieces 

sage: BK_pieces(3) 

Nablas : [1\1/1, 1\2(1)/2, 1\3(1)/3, 2(1)\2/1, 2\1/2(1), 2\2/2, 2\3(2)/3, 3(1)\3/1, 3(2)\3/2, 3\1/3(1), 3\2/3(2), 3\3/3] 

Deltas : [1/1\1, 1/2\2(1), 1/3\3(1), 2(1)/1\2, 2/2(1)\1, 2/2\2, 2/3\3(2), 3(1)/1\3, 3(2)/2\3, 3/3(1)\1, 3/3(2)\2, 3/3\3] 

 

""" 

forbidden_border_labels = ['%s(%s)' % (i, j) 

for i in range(1, max_letter + 1) 

for j in range(1, i)] 

pieces = PuzzlePieces(forbidden_border_labels) 

for i in range(1, max_letter + 1): 

piece = DeltaPiece('%s'%i, '%s'%i, '%s'%i) 

pieces.add_piece(piece, rotations=60) 

for j in range(1, i): 

piece = DeltaPiece(north_west='%s'%i, north_east='%s'%j, south='%s(%s)'%(i,j)) 

pieces.add_piece(piece, rotations=60) 

return pieces 

 

class PuzzleFilling(object): 

r""" 

Create partial puzzles and provides methods to build puzzles from them. 

""" 

def __init__(self, north_west_labels, north_east_labels): 

r""" 

TESTS:: 

 

sage: from sage.combinat.knutson_tao_puzzles import PuzzleFilling 

sage: P = PuzzleFilling('0101','0101') 

sage: P 

{} 

""" 

self._nw_labels = tuple(north_west_labels) 

self._ne_labels = tuple(north_east_labels) 

self._squares = {} 

self._n = len(self._nw_labels) 

self._kink_coordinates = (1, self._n) 

 

def __getitem__(self, key): 

r""" 

TESTS:: 

 

sage: from sage.combinat.knutson_tao_puzzles import KnutsonTaoPuzzleSolver 

sage: ps = KnutsonTaoPuzzleSolver("H") 

sage: puzzle = ps('0101','1001')[0] 

sage: puzzle 

{(1, 2): 1/\1 10\/0, (1, 3): 0/\10 1\/1, (3, 3): 1/1\1, (4, 4): 10/0\1, 

(1, 4): 1/\1 10\/0, (1, 1): 0/1\10, (2, 3): 1/\0 0\/1, (2, 2): 0/0\0, (3, 4): 0/\0 1\/10, (2, 4): 0/\0 0\/0} 

sage: puzzle[(1,2)] # indirect doctest 

1/\1 10\/0 

""" 

return self._squares[key] 

 

def kink_coordinates(self): 

r""" 

Provides the coordinates of the kinks. 

 

The kink coordinates are the coordinates up to which the puzzle has already 

been built. The kink starts in the north corner and then moves down the diagonals 

as the puzzles is built. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import PuzzleFilling 

sage: P = PuzzleFilling('0101','0101') 

sage: P 

{} 

sage: P.kink_coordinates() 

(1, 4) 

""" 

return self._kink_coordinates 

 

def is_in_south_edge(self): 

r""" 

Checks whether kink coordinates of partial puzzle is in south corner. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import PuzzleFilling 

sage: P = PuzzleFilling('0101','0101') 

sage: P.is_in_south_edge() 

False 

""" 

i, j = self.kink_coordinates() 

return i == j 

 

def north_west_label_of_kink(self): 

r""" 

Return north-west label of kink. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import PuzzleFilling 

sage: P = PuzzleFilling('0101','0101') 

sage: P.north_west_label_of_kink() 

'1' 

""" 

(i, j) = self.kink_coordinates() 

if i == 1: 

return self._nw_labels[j-1] 

else: 

return self._squares[i-1,j]['south_east'] 

 

def north_east_label_of_kink(self): 

r""" 

Return north east label of kink. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import PuzzleFilling 

sage: P = PuzzleFilling('0101','0101') 

sage: P.north_east_label_of_kink() 

'0' 

""" 

(i, j) = self.kink_coordinates() 

if j == self._n: 

return self._ne_labels[i-1] 

else: 

return self._squares[i,j+1]['south_west'] 

 

def is_completed(self): 

r""" 

Whether partial puzzle is complete (completely filled) or not. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import PuzzleFilling 

sage: P = PuzzleFilling('0101','0101') 

sage: P.is_completed() 

False 

 

sage: from sage.combinat.knutson_tao_puzzles import KnutsonTaoPuzzleSolver 

sage: ps = KnutsonTaoPuzzleSolver("H") 

sage: puzzle = ps('0101','1001')[0] 

sage: puzzle.is_completed() 

True 

""" 

(i, j) = self.kink_coordinates() 

return i == self._n + 1 

 

def south_labels(self): 

r""" 

Return south labels for completed puzzle. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import KnutsonTaoPuzzleSolver 

sage: ps = KnutsonTaoPuzzleSolver("H") 

sage: ps('0101','1001')[0].south_labels() 

('1', '0', '1', '0') 

""" 

# TODO: return ''.join(self[i, i]['south'] for i in range(1, self._n + 1)) 

return tuple([self[i,i]['south'] for i in range(1, self._n+1)]) 

 

def add_piece(self, piece): 

r""" 

Adds ``piece`` to partial puzzle. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece, PuzzleFilling 

sage: piece = DeltaPiece('0','1','0') 

sage: P = PuzzleFilling('0101','0101'); P 

{} 

sage: P.add_piece(piece); P 

{(1, 4): 1/0\0} 

""" 

(i, j) = self.kink_coordinates() 

self._squares[i, j] = piece 

if isinstance(piece, DeltaPiece): 

i += 1 

j = self._n 

else: 

j -= 1 

self._kink_coordinates = (i, j) 

 

def add_pieces(self, pieces): 

r""" 

Adds ``piece`` to partial puzzle. 

 

INPUT: 

 

- ``pieces`` -- tuple of pieces 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece, PuzzleFilling 

sage: P = PuzzleFilling('0101','0101'); P 

{} 

sage: piece = DeltaPiece('0','1','0') 

sage: pieces = [piece,piece] 

sage: P.add_pieces(pieces) 

sage: P 

{(2, 4): 1/0\0, (1, 4): 1/0\0} 

""" 

(i, j) = self.kink_coordinates() 

for piece in pieces: 

self._squares[i, j] = piece 

if isinstance(piece, DeltaPiece): 

i += 1 

j = self._n 

else: 

j -= 1 

self._kink_coordinates = (i, j) 

 

def copy(self): 

r""" 

Return copy of ``self``. 

 

EXAMPLES:: 

 

 

sage: from sage.combinat.knutson_tao_puzzles import DeltaPiece, PuzzleFilling 

sage: piece = DeltaPiece('0','1','0') 

sage: P = PuzzleFilling('0101','0101'); P 

{} 

sage: PP = P.copy() 

sage: P.add_piece(piece); P 

{(1, 4): 1/0\0} 

sage: PP 

{} 

""" 

PP = PuzzleFilling(self._nw_labels, self._ne_labels) 

PP._squares = self._squares.copy() 

PP._kink_coordinates = self._kink_coordinates 

PP._n = self._n 

return PP 

 

def contribution(self): 

r""" 

Return equivariant contributions from ``self`` in polynomial ring. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import KnutsonTaoPuzzleSolver 

sage: ps = KnutsonTaoPuzzleSolver("HT") 

sage: puzzles = ps('0101','1001') 

sage: sorted([p.contribution() for p in puzzles], key=str) 

[1, y1 - y3] 

""" 

R = PolynomialRing(Integers(), 'y', self._n+1) 

y = R.gens() 

z = R.one() 

for i in range(1, self._n + 1): 

for j in range(i+1, self._n + 1): 

if self[i, j].north_piece()['south'].startswith('T'): 

z *= y[i] - y[j] 

if self[i, j].north_piece()['south'].startswith('K'): 

z *= -1 

return z 

 

def __repr__(self): 

r""" 

TESTS:: 

 

sage: from sage.combinat.knutson_tao_puzzles import H_grassmannian_pieces, PuzzleFilling 

sage: P = PuzzleFilling('0101','0101'); P 

{} 

sage: P.__repr__() 

'{}' 

""" 

return str(self._squares) 

 

def __iter__(self): 

r""" 

Iterator. 

 

TESTS:: 

 

sage: from sage.combinat.knutson_tao_puzzles import KnutsonTaoPuzzleSolver 

sage: ps = KnutsonTaoPuzzleSolver("H") 

sage: puzzle = ps('0101','1001')[0] 

sage: puzzle 

{(1, 2): 1/\1 10\/0, (1, 3): 0/\10 1\/1, (3, 3): 1/1\1, (4, 4): 10/0\1, 

(1, 4): 1/\1 10\/0, (1, 1): 0/1\10, (2, 3): 1/\0 0\/1, (2, 2): 0/0\0, 

(3, 4): 0/\0 1\/10, (2, 4): 0/\0 0\/0} 

sage: [p for p in puzzle] 

[1/\1 10\/0, 

0/\10 1\/1, 

0/\0 0\/0, 

1/\1 10\/0, 

1/\0 0\/1, 

0/\0 1\/10, 

0/1\10, 

0/0\0, 

1/1\1, 

10/0\1] 

""" 

for d in range(self._n): 

for k in range(d + 1): 

yield self[k + 1, self._n - d + k] 

 

def plot(self, labels=True, style="fill"): 

r""" 

Plots completed puzzle. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import KnutsonTaoPuzzleSolver 

sage: ps = KnutsonTaoPuzzleSolver("H") 

sage: puzzle = ps('0101','1001')[0] 

sage: puzzle.plot() #not tested 

sage: puzzle.plot(style='fill') #not tested 

sage: puzzle.plot(style='edges') #not tested 

""" 

P = Graphics() 

coords = [(k, -d) for d in range(self._n) for k in range(-d, d + 1, 2)] 

for ((k, d), piece) in zip(coords, self): 

if isinstance(piece, RhombusPiece): 

for (i, triangle) in enumerate(piece): 

P += triangle._plot_piece([(k, d - 2*i), (k - 1, d - 1), (k + 1, d - 1)], style=style) 

if labels: 

P += piece._plot_label(piece['north_west'], (k - 0.5, d - 0.5), rotation=60) 

P += piece._plot_label(piece['north_east'], (k + 0.5, d - 0.5), rotation=-60) 

P += piece._plot_label(piece.north_piece()['south'], (k, d - 1)) 

else: 

P += piece._plot_piece([(k, d), (k - 1, d - 1), (k + 1, d - 1)], style=style) 

if labels: 

P += piece._plot_label(piece['north_west'], (k - 0.5, d - 0.5), rotation=60) 

P += piece._plot_label(piece['north_east'], (k + 0.5, d - 0.5), rotation=-60) 

P += piece._plot_label(piece['south'], (k, d - 1)) 

P.set_aspect_ratio(1.73) 

P.axes(False) 

return P 

 

def _latex_(self): 

r""" 

Return latex version of ``self``. 

 

Note that you might need to add tikz to the preamble:: 

 

sage: latex.extra_preamble(r'''\usepackage{tikz}''') 

sage: from sage.combinat.knutson_tao_puzzles import * 

 

sage: ps = KnutsonTaoPuzzleSolver(H_grassmannian_pieces()) 

sage: solns = ps('0101', '0101') 

sage: view(solns[0], viewer='pdf') # not tested 

 

sage: ps = KnutsonTaoPuzzleSolver(HT_two_step_pieces()) 

sage: solns = ps(list('10212'), list('12012')) 

sage: view(solns[0], viewer='pdf') # not tested 

 

sage: ps = KnutsonTaoPuzzleSolver(K_grassmannian_pieces()) 

sage: solns = ps('0101', '0101') 

sage: view(solns[0], viewer='pdf') # not tested 

 

""" 

from collections import defaultdict 

label_colors = defaultdict(lambda : None) 

label_colors.update({'0': 'red', '1': 'blue', '2': 'green'}) 

edge_colors = defaultdict(lambda : None) 

edge_colors.update({'0': 'red', '1': 'blue', '2': 'green', 'K':'orange'}) 

 

s = r"""\begin{tikzpicture}[yscale=1.73]""" 

coords = [(k, -d) for d in range(self._n) for k in range(-d, d + 1, 2)] 

 

def tikztriangle_fill(color, k, d, i, *args): 

s = r"""\path[color=%s, fill=%s!10]""" % (color, color) 

s += r"""(%s, %s) -- (%s, %s)""" % (k, d-2*i, k-1, d-1) 

s += r"""-- (%s, %s)""" % (k+1, d-1) 

s += r"""-- (%s, %s)""" % (k, d-2*i) 

s += ";\n" 

return s 

 

def tikztriangle_edges(color, k, d, i, label1, label2, label3): 

s = "" 

if i == 1: return s 

tikzcmd = r"""\draw[color=%s, fill=none] (%s, %s) -- (%s, %s);""" + "\n" 

if edge_colors[label1]: 

s += tikzcmd % (edge_colors[label1], k-1, d-1, k+1, d-1) 

if edge_colors[label2]: 

s += tikzcmd % (edge_colors[label2], k, d-2*i, k-1, d-1) 

if edge_colors[label3]: 

s += tikzcmd % (edge_colors[label3], k+1, d-1, k, d-2*i) 

return s 

 

def tikzlabels(color, k, d, i, label1, label2, label3): 

s = r"""\path[] (%s, %s)""" % (k, d-2*i) 

s += r"""-- (%s, %s) """ % (k-1, d-1) 

if label_colors[label2]: 

s += r"""node[midway, color=%s] {$%s$} """ % (label_colors[label2], label2) 

s += r"""-- (%s, %s) """ % (k+1, d-1) 

if label_colors[label1]: 

s += r"""node[midway, color=%s] {$%s$} """ % (label_colors[label1], label1) 

s += r"""-- (%s, %s) """ % (k, d-2*i) 

if label_colors[label3]: 

s += r"""node[midway, color=%s] {$%s$} """ % (label_colors[label3], label3) 

s += ";\n" 

return s 

 

for ((k, d), piece) in zip(coords, self): 

for tikzcmd in (tikztriangle_fill, tikztriangle_edges, tikzlabels): 

if isinstance(piece, RhombusPiece): 

for (i, triangle) in enumerate([piece.north_piece(), piece.south_piece()]): 

if i == 0: 

s += tikzcmd(triangle.color(), k, d, i, *triangle.border()) 

else: 

s += tikzcmd(triangle.color(), k, d, i, "", "", "") 

else: 

color = piece.color() 

s += tikzcmd(color, k, d, 0, *piece.border()) 

 

s += r"""\end{tikzpicture}""" 

 

return s 

 

class KnutsonTaoPuzzleSolver(UniqueRepresentation): 

r""" 

Returns puzzle solver function used to create all puzzles with given boundary conditions. 

 

This class implements a generic algorithm to solve Knutson-Tao puzzles. 

An instance of this class will be callable: the arguments are the 

labels of north-east and north-west sides of the puzzle boundary; the 

output is the list of the fillings of the puzzle with the specified 

pieces. 

 

INPUT: 

 

- ``puzzle_pieces`` -- takes either a collection of puzzle pieces or 

a string indicating a pre-programmed collection of puzzle pieces: 

 

- ``H`` -- cohomology of the Grassmannian 

- ``HT`` -- equivariant cohomology of the Grassmannian 

- ``K`` -- K-theory 

- ``H2step`` -- cohomology of the *2-step* Grassmannian 

- ``HT2step`` -- equivariant cohomology of the *2-step* Grassmannian 

- ``BK`` -- Belkale-Kumar puzzle pieces 

 

- ``max_letter`` -- (default: None) None or a positive integer. This is 

only required only for Belkale-Kumar puzzles. 

 

EXAMPLES: 

 

Each puzzle piece is an edge-labelled triangle oriented in such a way 

that it has a south edge (called a *delta* piece) or a north edge 

(called a *nabla* piece). For example, the puzzle pieces corresponding 

to the cohomology of the Grassmannian are the following:: 

 

sage: from sage.combinat.knutson_tao_puzzles import H_grassmannian_pieces 

sage: H_grassmannian_pieces() 

Nablas : [0\0/0, 0\10/1, 10\1/0, 1\0/10, 1\1/1] 

Deltas : [0/0\0, 0/1\10, 1/10\0, 1/1\1, 10/0\1] 

 

In the string representation, the nabla pieces are depicted as 

``c\a/b``, where `a` is the label of the north edge, `b` is the label 

of the south-east edge, `c` is the label of the south-west edge. 

A similar string representation exists for the delta pieces. 

 

To create a puzzle solver, one specifies a collection of puzzle pieces:: 

 

sage: KnutsonTaoPuzzleSolver(H_grassmannian_pieces()) 

Knutson-Tao puzzle solver with pieces: 

Nablas : [0\0/0, 0\10/1, 10\1/0, 1\0/10, 1\1/1] 

Deltas : [0/0\0, 0/1\10, 1/10\0, 1/1\1, 10/0\1] 

 

The following shorthand to create the above puzzle solver is also supported:: 

 

sage: KnutsonTaoPuzzleSolver('H') 

Knutson-Tao puzzle solver with pieces: 

Nablas : [0\0/0, 0\10/1, 10\1/0, 1\0/10, 1\1/1] 

Deltas : [0/0\0, 0/1\10, 1/10\0, 1/1\1, 10/0\1] 

 

The solver will compute all fillings of the puzzle with the given 

puzzle pieces. The user specifies the labels of north-east and 

north-west sides of the puzzle boundary and the output is a list of the 

fillings of the puzzle with the specified pieces. For example, there is 

one solution to the puzzle whose north-west and north-east edges are 

both labeled '0':: 

 

sage: ps = KnutsonTaoPuzzleSolver('H') 

sage: ps('0', '0') 

[{(1, 1): 0/0\0}] 

 

There are two solutions to the puzzle whose north-west and north-east 

edges are both labeled '0101':: 

 

sage: ps = KnutsonTaoPuzzleSolver('H') 

sage: solns = ps('0101', '0101') 

sage: len(solns) 

2 

sage: solns.sort(key=str) 

sage: solns 

[{(1, 2): 1/\0 0\/1, (1, 3): 0/\0 0\/0, (3, 3): 1/1\1, (4, 4): 10/0\1, (1, 4): 1/\0 0\/1, (1, 1): 0/0\0, (2, 3): 0/\10 1\/1, (2, 2): 1/1\1, (3, 4): 0/\0 1\/10, (2, 4): 1/\1 10\/0}, 

{(1, 2): 1/\1 10\/0, (1, 3): 0/\0 1\/10, (3, 3): 0/0\0, (4, 4): 1/1\1, (1, 4): 1/\0 0\/1, (1, 1): 0/1\10, (2, 3): 10/\1 0\/0, (2, 2): 0/0\0, (3, 4): 1/\0 0\/1, (2, 4): 1/\1 1\/1}] 

 

The pieces in a puzzle filling are indexed by pairs of non-negative 

integers `(i, j)` with `1 \leq i \leq j \leq n`, where `n` is the 

length of the word labelling the triangle edge. The pieces indexed by 

`(i, i)` are the triangles along the south edge of the puzzle. :: 

 

sage: f = solns[0] 

sage: [f[i, i] for i in range(1,5)] 

[0/0\0, 1/1\1, 1/1\1, 10/0\1] 

 

The pieces indexed by `(i, j)` for `j > i` are a pair consisting of 

a delta piece and nabla piece glued together along the south edge and 

north edge, respectively (these pairs are called *rhombi*). :: 

 

sage: f = solns[0] 

sage: f[1, 2] 

1/\0 0\/1 

 

There are various methods and options to display puzzle solutions. 

A single puzzle can be displayed using the plot method of the puzzle:: 

 

sage: ps = KnutsonTaoPuzzleSolver("H") 

sage: puzzle = ps('0101','1001')[0] 

sage: puzzle.plot() #not tested 

sage: puzzle.plot(style='fill') #not tested 

sage: puzzle.plot(style='edges') #not tested 

 

To plot several puzzle solutions, use the plot method of the puzzle 

solver:: 

 

sage: ps = KnutsonTaoPuzzleSolver('K') 

sage: solns = ps('0101', '0101') 

sage: ps.plot(solns) # not tested 

 

The code can also generate a PDF of a puzzle (using LaTeX and *tikz*):: 

 

sage: latex.extra_preamble(r'''\usepackage{tikz}''') 

sage: ps = KnutsonTaoPuzzleSolver('H') 

sage: solns = ps('0101', '0101') 

sage: view(solns[0], viewer='pdf') # not tested 

 

 

Below are examples of using each of the currently supported puzzles. 

 

Cohomology of the Grassmannian:: 

 

sage: ps = KnutsonTaoPuzzleSolver("H") 

sage: solns = ps('0101', '0101') 

sage: sorted(solns, key=str) 

[{(1, 2): 1/\0 0\/1, (1, 3): 0/\0 0\/0, (3, 3): 1/1\1, (4, 4): 10/0\1, (1, 4): 1/\0 0\/1, (1, 1): 0/0\0, (2, 3): 0/\10 1\/1, (2, 2): 1/1\1, (3, 4): 0/\0 1\/10, (2, 4): 1/\1 10\/0}, 

{(1, 2): 1/\1 10\/0, (1, 3): 0/\0 1\/10, (3, 3): 0/0\0, (4, 4): 1/1\1, (1, 4): 1/\0 0\/1, (1, 1): 0/1\10, (2, 3): 10/\1 0\/0, (2, 2): 0/0\0, (3, 4): 1/\0 0\/1, (2, 4): 1/\1 1\/1}] 

 

Equivariant puzzles:: 

 

sage: ps = KnutsonTaoPuzzleSolver("HT") 

sage: solns = ps('0101', '0101') 

sage: sorted(solns, key=str) 

[{(1, 2): 1/\0 0\/1, (1, 3): 0/\0 0\/0, (3, 3): 0/0\0, (4, 4): 1/1\1, (1, 4): 1/\0 0\/1, (1, 1): 0/0\0, (2, 3): 0/\1 1\/0, (2, 2): 1/1\1, (3, 4): 1/\0 0\/1, (2, 4): 1/\1 1\/1}, 

{(1, 2): 1/\0 0\/1, (1, 3): 0/\0 0\/0, (3, 3): 1/1\1, (4, 4): 10/0\1, (1, 4): 1/\0 0\/1, (1, 1): 0/0\0, (2, 3): 0/\10 1\/1, (2, 2): 1/1\1, (3, 4): 0/\0 1\/10, (2, 4): 1/\1 10\/0}, 

{(1, 2): 1/\1 10\/0, (1, 3): 0/\0 1\/10, (3, 3): 0/0\0, (4, 4): 1/1\1, (1, 4): 1/\0 0\/1, (1, 1): 0/1\10, (2, 3): 10/\1 0\/0, (2, 2): 0/0\0, (3, 4): 1/\0 0\/1, (2, 4): 1/\1 1\/1}] 

 

K-Theory puzzles:: 

 

sage: ps = KnutsonTaoPuzzleSolver("K") 

sage: solns = ps('0101', '0101') 

sage: sorted(solns, key=str) 

[{(1, 2): 1/\0 0\/1, (1, 3): 0/\0 0\/0, (3, 3): 1/1\1, (4, 4): 10/0\1, (1, 4): 1/\0 0\/1, (1, 1): 0/0\0, (2, 3): 0/\10 1\/1, (2, 2): 1/1\1, (3, 4): 0/\0 1\/10, (2, 4): 1/\1 10\/0}, 

{(1, 2): 1/\1 10\/0, (1, 3): 0/\0 1\/10, (3, 3): 0/0\0, (4, 4): 1/1\1, (1, 4): 1/\0 0\/1, (1, 1): 0/1\10, (2, 3): 10/\1 0\/0, (2, 2): 0/0\0, (3, 4): 1/\0 0\/1, (2, 4): 1/\1 1\/1}, 

{(1, 2): 1/\1 10\/0, (1, 3): 0/\0 1\/K, (3, 3): 1/1\1, (4, 4): 10/0\1, (1, 4): 1/\0 0\/1, (1, 1): 0/1\10, (2, 3): K/\K 0\/1, (2, 2): 0/0\0, (3, 4): 0/\0 1\/10, (2, 4): 1/\1 K\/0}] 

 

Two-step puzzles:: 

 

sage: ps = KnutsonTaoPuzzleSolver("H2step") 

sage: solns = ps('01201', '01021') 

sage: sorted(solns, key=str) 

[{(1, 2): 1/\0 0\/1, (1, 3): 2/\0 0\/2, (3, 3): 1/1\1, (4, 5): 20/\2 1\/10, (4, 4): 1/1\1, (5, 5): 10/0\1, (1, 4): 0/\0 0\/0, (1, 1): 0/0\0, (1, 5): 1/\0 0\/1, (2, 3): 2/\2 21\/1, (2, 2): 1/2\21, (2, 5): 1/\1 10\/0, (3, 4): 21/\2 1\/1, (2, 4): 0/\10 2\/21, (3, 5): 0/\0 2\/20}, 

{(1, 2): 1/\1 10\/0, (1, 3): 2/\1 1\/2, (3, 3): 0/0\0, (4, 5): 2(10)/\2 0\/1, (4, 4): 0/0\0, (5, 5): 1/1\1, (1, 4): 0/\0 1\/10, (1, 1): 0/1\10, (1, 5): 1/\0 0\/1, (2, 3): 2/\2 20\/0, (2, 2): 0/2\20, (2, 5): 1/\1 1\/1, (3, 4): 20/\2 0\/0, (2, 4): 10/\1 2\/20, (3, 5): 1/\0 2\/2(10)}, 

{(1, 2): 1/\21 20\/0, (1, 3): 2/\2 21\/1, (3, 3): 1/1\1, (4, 5): 21/\2 1\/1, (4, 4): 10/0\1, (5, 5): 1/1\1, (1, 4): 0/\0 2\/20, (1, 1): 0/2\20, (1, 5): 1/\0 0\/1, (2, 3): 1/\0 0\/1, (2, 2): 0/0\0, (2, 5): 1/\1 2\/21, (3, 4): 0/\0 1\/10, (2, 4): 20/\2 0\/0, (3, 5): 21/\0 0\/21}] 

 

Two-step equivariant puzzles:: 

 

sage: ps = KnutsonTaoPuzzleSolver("HT2step") 

sage: solns = ps('10212', '12012') 

sage: sorted(solns, key=str) 

[{(1, 2): 0/\(21)0 1\/2, (1, 3): 2/\1 (21)0\/0, (3, 3): 0/0\0, (4, 5): 1/\1 2\/21, (4, 4): 2/2\2, (5, 5): 21/1\2, (1, 4): 1/\1 1\/1, (1, 1): 1/1\1, (1, 5): 2/\1 1\/2, (2, 3): 0/\2 2\/0, (2, 2): 2/2\2, (2, 5): 2/\2 21\/1, (3, 4): 2/\0 0\/2, (2, 4): 1/\21 2\/2, (3, 5): 1/\0 0\/1}, 

{(1, 2): 0/\(21)0 1\/2, (1, 3): 2/\1 (21)0\/0, (3, 3): 0/0\0, (4, 5): 2/\1 1\/2, (4, 4): 1/1\1, (5, 5): 2/2\2, (1, 4): 1/\1 1\/1, (1, 1): 1/1\1, (1, 5): 2/\1 1\/2, (2, 3): 0/\2 2\/0, (2, 2): 2/2\2, (2, 5): 2/\2 2\/2, (3, 4): 1/\0 0\/1, (2, 4): 1/\2 2\/1, (3, 5): 2/\0 0\/2}, 

{(1, 2): 0/\(21)0 1\/2, (1, 3): 2/\1 (21)0\/0, (3, 3): 2/2\2, (4, 5): 1/\1 2\/21, (4, 4): 20/0\2, (5, 5): 21/1\2, (1, 4): 1/\1 1\/1, (1, 1): 1/1\1, (1, 5): 2/\1 1\/2, (2, 3): 0/\20 2\/2, (2, 2): 2/2\2, (2, 5): 2/\2 21\/1, (3, 4): 0/\0 2\/20, (2, 4): 1/\21 20\/0, (3, 5): 1/\0 0\/1}, 

{(1, 2): 0/\1 1\/0, (1, 3): 2/\1 1\/2, (3, 3): 0/0\0, (4, 5): 1/\1 2\/21, (4, 4): 2/2\2, (5, 5): 21/1\2, (1, 4): 1/\1 1\/1, (1, 1): 1/1\1, (1, 5): 2/\1 1\/2, (2, 3): 2/\2 20\/0, (2, 2): 0/2\20, (2, 5): 2/\2 21\/1, (3, 4): 2/\0 0\/2, (2, 4): 1/\21 2\/2, (3, 5): 1/\0 0\/1}, 

{(1, 2): 0/\1 1\/0, (1, 3): 2/\1 1\/2, (3, 3): 0/0\0, (4, 5): 2/\1 1\/2, (4, 4): 1/1\1, (5, 5): 2/2\2, (1, 4): 1/\1 1\/1, (1, 1): 1/1\1, (1, 5): 2/\1 1\/2, (2, 3): 2/\2 20\/0, (2, 2): 0/2\20, (2, 5): 2/\2 2\/2, (3, 4): 1/\0 0\/1, (2, 4): 1/\2 2\/1, (3, 5): 2/\0 0\/2}, 

{(1, 2): 0/\10 1\/1, (1, 3): 2/\10 10\/2, (3, 3): 1/1\1, (4, 5): 10/\1 2\/20, (4, 4): 2/2\2, (5, 5): 20/0\2, (1, 4): 1/\1 10\/0, (1, 1): 1/1\1, (1, 5): 2/\1 1\/2, (2, 3): 2/\2 21\/1, (2, 2): 1/2\21, (2, 5): 2/\2 20\/0, (3, 4): 2/\1 1\/2, (2, 4): 0/\20 2\/2, (3, 5): 0/\0 1\/10}, 

{(1, 2): 0/\10 1\/1, (1, 3): 2/\10 10\/2, (3, 3): 1/1\1, (4, 5): 2/\1 1\/2, (4, 4): 10/0\1, (5, 5): 2/2\2, (1, 4): 1/\1 10\/0, (1, 1): 1/1\1, (1, 5): 2/\1 1\/2, (2, 3): 2/\2 21\/1, (2, 2): 1/2\21, (2, 5): 2/\2 2\/2, (3, 4): 0/\0 1\/10, (2, 4): 0/\2 2\/0, (3, 5): 2/\0 0\/2}, 

{(1, 2): 0/\20 21\/1, (1, 3): 2/\2 20\/0, (3, 3): 0/0\0, (4, 5): 2/\1 1\/2, (4, 4): 1/1\1, (5, 5): 2/2\2, (1, 4): 1/\1 2\/21, (1, 1): 1/2\21, (1, 5): 2/\1 1\/2, (2, 3): 0/\1 1\/0, (2, 2): 1/1\1, (2, 5): 2/\2 2\/2, (3, 4): 1/\0 0\/1, (2, 4): 21/\2 1\/1, (3, 5): 2/\0 0\/2}, 

{(1, 2): 0/\20 21\/1, (1, 3): 2/\2 20\/0, (3, 3): 1/1\1, (4, 5): 2/\1 1\/2, (4, 4): 10/0\1, (5, 5): 2/2\2, (1, 4): 1/\1 2\/21, (1, 1): 1/2\21, (1, 5): 2/\1 1\/2, (2, 3): 0/\10 1\/1, (2, 2): 1/1\1, (2, 5): 2/\2 2\/2, (3, 4): 0/\0 1\/10, (2, 4): 21/\2 10\/0, (3, 5): 2/\0 0\/2}, 

{(1, 2): 0/\21 21\/0, (1, 3): 2/\2 21\/1, (3, 3): 0/0\0, (4, 5): 2/\1 1\/2, (4, 4): 1/1\1, (5, 5): 2/2\2, (1, 4): 1/\1 2\/21, (1, 1): 1/2\21, (1, 5): 2/\1 1\/2, (2, 3): 1/\1 10\/0, (2, 2): 0/1\10, (2, 5): 2/\2 2\/2, (3, 4): 1/\0 0\/1, (2, 4): 21/\2 1\/1, (3, 5): 2/\0 0\/2}] 

 

 

Belkale-Kumar puzzles (the following example is Figure 2 of [KnutsonPurbhoo10]_):: 

 

sage: ps = KnutsonTaoPuzzleSolver('BK', 3) 

sage: solns = ps('12132', '23112') 

sage: len(solns) 

1 

sage: solns[0].south_labels() 

('3', '2', '1', '2', '1') 

sage: solns 

[{(1, 2): 2/\3(2) 3(1)\/1, (1, 3): 1/\3(1) 3(2)\/2, (3, 3): 1/1\1, (4, 5): 1/\1 2\/2(1), (4, 4): 2/2\2, (5, 5): 2(1)/1\2, (1, 4): 3/\3 3(1)\/1, (1, 1): 1/3\3(1), (1, 5): 2/\2 3\/3(2), (2, 3): 2/\2 2(1)\/1, (2, 2): 1/2\2(1), (2, 5): 3(2)/\3 2(1)\/1, (3, 4): 2/\1 1\/2, (2, 4): 1/\2(1) 2\/2, (3, 5): 1/\1 1\/1}] 

""" 

def __init__(self, puzzle_pieces): 

r""" 

Knutson-Tao puzzle solver. 

 

TESTS: 

 

Check that UniqueRepresentation works:: 

 

sage: from sage.combinat.knutson_tao_puzzles import KnutsonTaoPuzzleSolver, H_grassmannian_pieces 

sage: ps = KnutsonTaoPuzzleSolver(H_grassmannian_pieces()) 

sage: qs = KnutsonTaoPuzzleSolver("H") 

sage: ps 

Knutson-Tao puzzle solver with pieces: 

Nablas : [0\0/0, 0\10/1, 10\1/0, 1\0/10, 1\1/1] 

Deltas : [0/0\0, 0/1\10, 1/10\0, 1/1\1, 10/0\1] 

sage: qs 

Knutson-Tao puzzle solver with pieces: 

Nablas : [0\0/0, 0\10/1, 10\1/0, 1\0/10, 1\1/1] 

Deltas : [0/0\0, 0/1\10, 1/10\0, 1/1\1, 10/0\1] 

sage: ps == qs 

True 

""" 

self._puzzle_pieces = puzzle_pieces 

self._rhombus_pieces = tuple(puzzle_pieces.rhombus_pieces()) 

self._bottom_deltas = tuple(puzzle_pieces.boundary_deltas()) 

 

@staticmethod 

def __classcall_private__(cls, puzzle_pieces, max_letter=None): 

r""" 

TESTS:: 

 

sage: from sage.combinat.knutson_tao_puzzles import * 

sage: KnutsonTaoPuzzleSolver(H_grassmannian_pieces()) == KnutsonTaoPuzzleSolver("H") # indirect doctest 

True 

sage: KnutsonTaoPuzzleSolver(HT_grassmannian_pieces()) == KnutsonTaoPuzzleSolver("HT") 

True 

sage: KnutsonTaoPuzzleSolver(K_grassmannian_pieces()) == KnutsonTaoPuzzleSolver("K") 

True 

sage: KnutsonTaoPuzzleSolver(H_two_step_pieces()) == KnutsonTaoPuzzleSolver("H2step") 

True 

sage: KnutsonTaoPuzzleSolver(HT_two_step_pieces()) == KnutsonTaoPuzzleSolver("HT2step") 

True 

sage: KnutsonTaoPuzzleSolver(BK_pieces(3)) == KnutsonTaoPuzzleSolver("BK",3) 

True 

""" 

if isinstance(puzzle_pieces, str): 

if puzzle_pieces == "H": 

puzzle_pieces = H_grassmannian_pieces() 

elif puzzle_pieces == "HT": 

puzzle_pieces = HT_grassmannian_pieces() 

elif puzzle_pieces == "K": 

puzzle_pieces = K_grassmannian_pieces() 

elif puzzle_pieces == "H2step": 

puzzle_pieces = H_two_step_pieces() 

elif puzzle_pieces == "HT2step": 

puzzle_pieces = HT_two_step_pieces() 

elif puzzle_pieces == "BK": 

if max_letter is not None: 

puzzle_pieces = BK_pieces(max_letter) 

else: 

raise ValueError("max_letter needs to be specified") 

return super(KnutsonTaoPuzzleSolver, cls).__classcall__(cls, puzzle_pieces) 

 

def __call__(self, lamda, mu, algorithm='strips'): 

r""" 

TESTS:: 

 

sage: from sage.combinat.knutson_tao_puzzles import KnutsonTaoPuzzleSolver 

sage: ps = KnutsonTaoPuzzleSolver("H") 

sage: ps('0101','1001') 

[{(1, 2): 1/\1 10\/0, (1, 3): 0/\10 1\/1, (3, 3): 1/1\1, (4, 4): 10/0\1, (1, 4): 1/\1 10\/0, 

(1, 1): 0/1\10, (2, 3): 1/\0 0\/1, (2, 2): 0/0\0, (3, 4): 0/\0 1\/10, (2, 4): 0/\0 0\/0}] 

sage: ps('0101','1001',algorithm='pieces') 

[{(1, 2): 1/\1 10\/0, (1, 3): 0/\10 1\/1, (3, 3): 1/1\1, (4, 4): 10/0\1, (1, 4): 1/\1 10\/0, 

(2, 4): 0/\0 0\/0, (2, 3): 1/\0 0\/1, (2, 2): 0/0\0, (3, 4): 0/\0 1\/10, (1, 1): 0/1\10}] 

""" 

lamda, mu = tuple(lamda), tuple(mu) 

if algorithm == 'pieces': 

return list(self._fill_puzzle_by_pieces(lamda, mu)) 

elif algorithm == 'strips': 

return list(self._fill_puzzle_by_strips(lamda, mu)) 

 

solutions = __call__ 

 

def __repr__(self): 

r""" 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import KnutsonTaoPuzzleSolver 

sage: KnutsonTaoPuzzleSolver('H') 

Knutson-Tao puzzle solver with pieces: 

Nablas : [0\0/0, 0\10/1, 10\1/0, 1\0/10, 1\1/1] 

Deltas : [0/0\0, 0/1\10, 1/10\0, 1/1\1, 10/0\1] 

""" 

return "Knutson-Tao puzzle solver with pieces:\n%s" % self._puzzle_pieces 

 

def puzzle_pieces(self): 

r""" 

The puzzle pieces used for filling in the puzzles. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import KnutsonTaoPuzzleSolver 

sage: ps = KnutsonTaoPuzzleSolver('H') 

sage: ps.puzzle_pieces() 

Nablas : [0\0/0, 0\10/1, 10\1/0, 1\0/10, 1\1/1] 

Deltas : [0/0\0, 0/1\10, 1/10\0, 1/1\1, 10/0\1] 

""" 

return self._puzzle_pieces 

 

def _fill_piece(self, nw_label, ne_label, pieces): 

r""" 

Fillings of a piece. 

 

INPUT: 

 

- ``nw_label``, ``nw_label`` -- label 

- ``pieces`` -- puzzle pieces used for the filling 

 

OUTPUT: 

 

- list of the fillings 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import KnutsonTaoPuzzleSolver 

sage: ps = KnutsonTaoPuzzleSolver('H') 

sage: ps._fill_piece('0', '0', ps._bottom_deltas) 

[0/0\0] 

""" 

output = [] 

for piece in pieces: 

if ( piece['north_west'] == nw_label 

and piece['north_east'] == ne_label ): 

output.append(piece) 

return output 

 

@cached_method 

def _fill_strip(self, nw_labels, ne_label, pieces, final_pieces=None): 

r""" 

Fillings of a strip of height 1. 

 

INPUT: 

 

- ``nw_labels`` -- tuple of labels 

- ``nw_label`` -- label 

- ``pieces`` -- puzzle pieces used for the filling 

- ``final_pieces`` -- pieces used for the last piece to be filled in 

 

OUTPUT: 

 

- list of lists of the fillings 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import KnutsonTaoPuzzleSolver 

sage: ps = KnutsonTaoPuzzleSolver('H') 

sage: ps._fill_strip(('0',), '0', ps._rhombus_pieces, ps._bottom_deltas) 

[[0/0\0]] 

sage: ps._fill_strip(('0','0'), '0', ps._rhombus_pieces, ps._bottom_deltas) 

[[0/\0 0\/0, 0/0\0]] 

sage: sorted(ps._fill_strip(('0',), '0', ps._rhombus_pieces), key=str) 

[[0/\0 0\/0], [0/\0 1\/10]] 

sage: sorted(ps._fill_strip(('0','1'), '0', ps._rhombus_pieces), key =str) 

[[1/\0 0\/1, 0/\0 0\/0], [1/\0 0\/1, 0/\0 1\/10]] 

 

TESTS:: 

 

sage: from sage.combinat.knutson_tao_puzzles import KnutsonTaoPuzzleSolver 

sage: ps = KnutsonTaoPuzzleSolver('H') 

sage: ps._fill_strip(('0',), 'goo', ps._rhombus_pieces) 

[] 

""" 

if final_pieces is None: 

final_pieces = pieces 

 

output = [] 

if len(nw_labels) == 1: 

X = self._fill_piece(nw_labels[0], ne_label, final_pieces) 

if X: 

output = [[x] for x in X] 

else: 

partial_fillings = self._fill_strip(nw_labels[1:], ne_label, pieces) 

for partial_filling in partial_fillings: 

ne_label = partial_filling[-1]['south_west'] 

for piece in self._fill_piece(nw_labels[0], ne_label, final_pieces): 

output.append(partial_filling + [piece]) 

return output 

 

def _fill_puzzle_by_pieces(self, lamda, mu): 

r""" 

Fill puzzle pieces for given outer labels ``lambda`` and ``mu``. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import KnutsonTaoPuzzleSolver 

sage: ps = KnutsonTaoPuzzleSolver('H') 

sage: list(ps._fill_puzzle_by_pieces('0', '0')) 

[{(1, 1): 0/0\0}] 

""" 

queue = [PuzzleFilling(lamda, mu)] 

while queue: 

PP = queue.pop() 

ne_label = PP.north_east_label_of_kink() 

nw_label = PP.north_west_label_of_kink() 

if PP.is_in_south_edge(): 

pieces = self._bottom_deltas 

else: 

pieces = self._rhombus_pieces 

for piece in self._fill_piece(nw_label, ne_label, pieces): 

PPcopy = PP.copy() 

PPcopy.add_piece(piece) 

if PPcopy.is_completed(): 

yield PPcopy 

else: 

queue.append(PPcopy) 

 

def _fill_puzzle_by_strips(self, lamda, mu): 

r""" 

Fill puzzle pieces by strips for given outer labels ``lambda`` and ``mu``. 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import KnutsonTaoPuzzleSolver 

sage: ps = KnutsonTaoPuzzleSolver('H') 

sage: list(ps._fill_puzzle_by_strips('0', '0')) 

[{(1, 1): 0/0\0}] 

sage: list(ps._fill_puzzle_by_strips('01', '01')) 

[{(1, 2): 1/\0 0\/1, (1, 1): 0/0\0, (2, 2): 1/1\1}] 

""" 

queue = [PuzzleFilling(lamda, mu)] 

while queue: 

PP = queue.pop() 

(i, j) = PP.kink_coordinates() 

 

# grab nw labels 

if i == 1: 

nw_labels = PP._nw_labels 

else: 

nw_labels = tuple(PP._squares[i-1, k]['south_east'] 

for k in range(i, len(lamda)+1)) 

 

# grab ne labels 

ne_label = PP._ne_labels[i-1] 

 

deltas = self._bottom_deltas 

rhombi = self._rhombus_pieces 

for row in self._fill_strip(nw_labels, ne_label, rhombi, deltas): 

PPcopy = PP.copy() 

PPcopy.add_pieces(row) 

if PPcopy.is_completed(): 

yield PPcopy 

else: 

queue.append(PPcopy) 

 

def plot(self, puzzles): 

r""" 

Return plot of puzzles. 

 

INPUT: 

 

- ``puzzles`` -- list of puzzles 

 

EXAMPLES:: 

 

sage: from sage.combinat.knutson_tao_puzzles import KnutsonTaoPuzzleSolver 

sage: ps = KnutsonTaoPuzzleSolver('K') 

sage: solns = ps('0101', '0101') 

sage: ps.plot(solns) # not tested 

""" 

g = [p.plot() for p in puzzles] 

m = len([gg.axes(False) for gg in g]) 

return graphics_array(g, (m+3)/4, 4) 

 

def structure_constants(self, lamda, mu, nu=None): 

r""" 

Compute cohomology structure coefficients from puzzles. 

 

INPUT: 

 

- ``pieces`` -- puzzle pieces to be used 

- ``lambda``, ``mu`` -- edge labels of puzzle for northwest and north east side 

- ``nu`` -- (default: ``None``) If ``nu`` is not specified a dictionary is returned with 

the structure coefficients corresponding to all south labels; if ``nu`` is given, only 

the coefficients with the specified label is returned. 

 

OUTPUT: dictionary 

 

EXAMPLES: 

 

Note: In order to standardize the output of the following examples, 

we output a sorted list of items from the dictionary instead of the 

dictionary itself. 

 

Grassmannian cohomology:: 

 

sage: ps = KnutsonTaoPuzzleSolver('H') 

sage: cp = ps.structure_constants('0101', '0101') 

sage: sorted(cp.items(), key=str) 

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

sage: ps.structure_constants('001001', '001010', '010100') 

1 

 

Equivariant cohomology:: 

 

sage: ps = KnutsonTaoPuzzleSolver('HT') 

sage: cp = ps.structure_constants('0101', '0101') 

sage: sorted(cp.items(), key=str) 

[(('0', '1', '0', '1'), y2 - y3), 

(('0', '1', '1', '0'), 1), 

(('1', '0', '0', '1'), 1)] 

 

K-theory:: 

 

sage: ps = KnutsonTaoPuzzleSolver('K') 

sage: cp = ps.structure_constants('0101', '0101') 

sage: sorted(cp.items(), key=str) 

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

 

Two-step:: 

 

sage: ps = KnutsonTaoPuzzleSolver('H2step') 

sage: cp = ps.structure_constants('01122', '01122') 

sage: sorted(cp.items(), key=str) 

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

sage: cp = ps.structure_constants('01201', '01021') 

sage: sorted(cp.items(), key=str) 

[(('0', '2', '1', '1', '0'), 1), 

(('1', '2', '0', '0', '1'), 1), 

(('2', '0', '1', '0', '1'), 1)] 

 

Two-step equivariant:: 

 

sage: ps = KnutsonTaoPuzzleSolver('HT2step') 

sage: cp = ps.structure_constants('10212', '12012') 

sage: sorted(cp.items(), key=str) 

[(('1', '2', '0', '1', '2'), y1*y2 - y2*y3 - y1*y4 + y3*y4), 

(('1', '2', '0', '2', '1'), y1 - y3), 

(('1', '2', '1', '0', '2'), y2 - y4), 

(('1', '2', '1', '2', '0'), 1), 

(('1', '2', '2', '0', '1'), 1), 

(('2', '1', '0', '1', '2'), y1 - y3), 

(('2', '1', '1', '0', '2'), 1)] 

""" 

from collections import defaultdict 

R = PolynomialRing(Integers(), 'y', len(lamda)+1) 

z = defaultdict(R.zero) 

for p in self(lamda, mu): 

z[p.south_labels()] += p.contribution() 

if nu is None: 

return dict(z) 

else: 

return z[tuple(nu)]