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

r""" 

Tiling Solver 

 

Tiling a n-dimensional polyomino with n-dimensional polyominoes. 

 

This module defines two classes: 

 

- :class:`sage.combinat.tiling.Polyomino` class, to represent polyominoes 

in arbitrary dimension. The goal of this class is to return all the 

rotated, reflected and/or translated copies of a polyomino that are 

contained in a certain box. 

 

- :class:`sage.combinat.tiling.TilingSolver` class, to solve the problem of 

tiling a `n`-dimensional polyomino with a set of `n`-dimensional 

polyominoes. One can specify if rotations and reflections are allowed or 

not and if pieces can be reused or not. This class convert the tiling 

data into rows of a matrix that are passed to the DLX solver. It also 

allows to compute the number of solutions. 

 

This uses dancing links code which is in Sage. Dancing links were 

originally introduced by Donald Knuth in 2000 [Knuth1]_. Knuth used dancing 

links to solve tilings of a region by 2d pentaminoes. Here we extend the 

method to any dimension. 

 

In particular, the :mod:`sage.games.quantumino` module is based on 

the Tiling Solver and allows to solve the 3d Quantumino puzzle. 

 

AUTHOR: 

 

- Sebastien Labbe, June 2011, initial version 

- Sebastien Labbe, July 2015, count solutions up to rotations 

- Sebastien Labbe, April 2017, tiling a polyomino, not only a rectangular box 

 

EXAMPLES: 

 

2d Easy Example 

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

 

Here is a 2d example. Let us try to fill the `3 \times 2` rectangle with a 

`1 \times 2` rectangle and a `2 \times 2` square. Obviously, there are two 

solutions:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: p = Polyomino([(0,0), (0,1)]) 

sage: q = Polyomino([(0,0), (0,1), (1,0), (1,1)]) 

sage: T = TilingSolver([p,q], box=[3,2]) 

sage: it = T.solve() 

sage: next(it) 

[Polyomino: [(0, 0), (0, 1), (1, 0), (1, 1)], Color: gray, Polyomino: [(2, 0), (2, 1)], Color: gray] 

sage: next(it) 

[Polyomino: [(1, 0), (1, 1), (2, 0), (2, 1)], Color: gray, Polyomino: [(0, 0), (0, 1)], Color: gray] 

sage: next(it) 

Traceback (most recent call last): 

... 

StopIteration 

sage: T.number_of_solutions() 

2 

 

Scott's pentomino problem 

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

 

As mentionned in the introduction of [Knuth1]_, Scott's pentomino problem 

consists in tiling a chessboard leaving the center four squares vacant with 

the 12 distinct pentominoes. 

 

The 12 pentominoes:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: I = Polyomino([(0,0),(1,0),(2,0),(3,0),(4,0)], color='brown') 

sage: N = Polyomino([(1,0),(1,1),(1,2),(0,2),(0,3)], color='yellow') 

sage: L = Polyomino([(0,0),(1,0),(0,1),(0,2),(0,3)], color='magenta') 

sage: U = Polyomino([(0,0),(1,0),(0,1),(0,2),(1,2)], color='violet') 

sage: X = Polyomino([(1,0),(0,1),(1,1),(1,2),(2,1)], color='pink') 

sage: W = Polyomino([(2,0),(2,1),(1,1),(1,2),(0,2)], color='green') 

sage: P = Polyomino([(1,0),(2,0),(0,1),(1,1),(2,1)], color='orange') 

sage: F = Polyomino([(1,0),(1,1),(0,1),(2,1),(2,2)], color='gray') 

sage: Z = Polyomino([(0,0),(1,0),(1,1),(1,2),(2,2)], color='yellow') 

sage: T = Polyomino([(0,0),(0,1),(1,1),(2,1),(0,2)], color='red') 

sage: Y = Polyomino([(0,0),(1,0),(2,0),(3,0),(2,1)], color='green') 

sage: V = Polyomino([(0,0),(0,1),(0,2),(1,0),(2,0)], color='blue') 

 

A `8 \times 8` chessboard leaving the center four squares vacant:: 

 

sage: import itertools 

sage: s = set(itertools.product(range(8), repeat=2)) 

sage: s.difference_update([(3,3), (3,4), (4,3), (4,4)]) 

sage: chessboard = Polyomino(s) 

sage: len(chessboard) 

60 

 

This problem is represented by a matrix made of 1568 rows and 72 columns. 

It has 65 different solutions up to isometries:: 

 

sage: from sage.combinat.tiling import TilingSolver 

sage: T = TilingSolver([I,N,L,U,X,W,P,F,Z,T,Y,V], box=chessboard, reflection=True) 

sage: T 

Tiling solver of 12 pieces into a box of size 60 

Rotation allowed: True 

Reflection allowed: True 

Reusing pieces allowed: False 

sage: len(T.rows()) # long time 

1568 

sage: T.number_of_solutions() # long time 

520 

sage: 520 / 8 

65 

 

Showing one solution:: 

 

sage: solution = next(T.solve()) # long time 

sage: G = sum([piece.show2d() for piece in solution], Graphics()) # long time 

sage: G.show(aspect_ratio=1, axes=False) # long time 

 

1d Easy Example 

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

 

Here is an easy one dimensional example where we try to tile a stick of 

length 6 with three sticks of length 1, 2 and 3. There are six solutions:: 

 

sage: p = Polyomino([[0]]) 

sage: q = Polyomino([[0],[1]]) 

sage: r = Polyomino([[0],[1],[2]]) 

sage: T = TilingSolver([p,q,r], box=[6]) 

sage: len(T.rows()) 

15 

sage: it = T.solve() 

sage: next(it) 

[Polyomino: [(0)], Color: gray, Polyomino: [(1), (2)], Color: gray, Polyomino: [(3), (4), (5)], Color: gray] 

sage: next(it) 

[Polyomino: [(0)], Color: gray, Polyomino: [(1), (2), (3)], Color: gray, Polyomino: [(4), (5)], Color: gray] 

sage: T.number_of_solutions() 

6 

 

2d Puzzle allowing reflections 

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

 

The following is a puzzle owned by Florent Hivert:: 

 

sage: from sage.combinat.tiling import Polyomino, TilingSolver 

sage: L = [] 

sage: L.append(Polyomino([(0,0),(0,1),(0,2),(0,3),(1,0),(1,1),(1,2),(1,3)], 'yellow')) 

sage: L.append(Polyomino([(0,0),(0,1),(0,2),(0,3),(1,0),(1,1),(1,2)], "black")) 

sage: L.append(Polyomino([(0,0),(0,1),(0,2),(0,3),(1,0),(1,1),(1,3)], "gray")) 

sage: L.append(Polyomino([(0,0),(0,1),(0,2),(0,3),(1,0),(1,3)],"cyan")) 

sage: L.append(Polyomino([(0,0),(0,1),(0,2),(0,3),(1,0),(1,1)],"red")) 

sage: L.append(Polyomino([(0,0),(0,1),(0,2),(0,3),(1,1),(1,2)],"blue")) 

sage: L.append(Polyomino([(0,0),(0,1),(0,2),(0,3),(1,1),(1,3)],"green")) 

sage: L.append(Polyomino([(0,1),(0,2),(0,3),(1,0),(1,1),(1,3)],"magenta")) 

sage: L.append(Polyomino([(0,1),(0,2),(0,3),(1,0),(1,1),(1,2)],"orange")) 

sage: L.append(Polyomino([(0,0),(0,1),(0,2),(1,0),(1,1),(1,2)],"pink")) 

 

By default, rotations are allowed and reflections are not. In this case, 

there are no solution for tiling a `8 \times 8` rectangular box:: 

 

sage: T = TilingSolver(L, box=(8,8)) 

sage: T.number_of_solutions() # long time (2.5 s) 

0 

 

If reflections are allowed, there are solutions. Solve the puzzle and show 

one solution:: 

 

sage: T = TilingSolver(L, box=(8,8), reflection=True) 

sage: solution = next(T.solve()) # long time (7s) 

sage: G = sum([piece.show2d() for piece in solution], Graphics()) # long time (<1s) 

sage: G.show(aspect_ratio=1, axes=False) # long time (2s) 

 

Compute the number of solutions:: 

 

sage: T.number_of_solutions() # long time (2.6s) 

328 

 

Create a animation of all the solutions:: 

 

sage: a = T.animate() # not tested 

sage: a # not tested 

Animation with 328 frames 

 

3d Puzzle 

--------- 

 

The same thing done in 3d *without* allowing reflections this time:: 

 

sage: from sage.combinat.tiling import Polyomino, TilingSolver 

sage: L = [] 

sage: L.append(Polyomino([(0,0,0),(0,1,0),(0,2,0),(0,3,0),(1,0,0),(1,1,0),(1,2,0),(1,3,0)])) 

sage: L.append(Polyomino([(0,0,0),(0,1,0),(0,2,0),(0,3,0),(1,0,0),(1,1,0),(1,2,0)])) 

sage: L.append(Polyomino([(0,0,0),(0,1,0),(0,2,0),(0,3,0),(1,0,0),(1,1,0),(1,3,0)])) 

sage: L.append(Polyomino([(0,0,0),(0,1,0),(0,2,0),(0,3,0),(1,0,0),(1,3,0)])) 

sage: L.append(Polyomino([(0,0,0),(0,1,0),(0,2,0),(0,3,0),(1,0,0),(1,1,0)])) 

sage: L.append(Polyomino([(0,0,0),(0,1,0),(0,2,0),(0,3,0),(1,1,0),(1,2,0)])) 

sage: L.append(Polyomino([(0,0,0),(0,1,0),(0,2,0),(0,3,0),(1,1,0),(1,3,0)])) 

sage: L.append(Polyomino([(0,1,0),(0,2,0),(0,3,0),(1,0,0),(1,1,0),(1,3,0)])) 

sage: L.append(Polyomino([(0,1,0),(0,2,0),(0,3,0),(1,0,0),(1,1,0),(1,2,0)])) 

sage: L.append(Polyomino([(0,0,0),(0,1,0),(0,2,0),(1,0,0),(1,1,0),(1,2,0)])) 

 

Solve the puzzle and show one solution:: 

 

sage: T = TilingSolver(L, box=(8,8,1)) 

sage: solution = next(T.solve()) # long time (8s) 

sage: G = sum([p.show3d(size=0.85) for p in solution], Graphics()) # long time (<1s) 

sage: G.show(aspect_ratio=1, viewer='tachyon') # long time (2s) 

 

Let us compute the number of solutions:: 

 

sage: T.number_of_solutions() # long time (3s) 

328 

 

Donald Knuth example : the Y pentamino 

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

 

Donald Knuth [Knuth1]_ considered the problem of packing 45 Y pentaminoes into a 

`15 \times 15` square:: 

 

sage: from sage.combinat.tiling import Polyomino, TilingSolver 

sage: y = Polyomino([(0,0),(1,0),(2,0),(3,0),(2,1)]) 

sage: T = TilingSolver([y], box=(5,10), reusable=True, reflection=True) 

sage: T.number_of_solutions() 

10 

sage: solution = next(T.solve()) 

sage: G = sum([p.show2d() for p in solution], Graphics()) 

sage: G.show(aspect_ratio=1) # long time (2s) 

 

:: 

 

sage: T = TilingSolver([y], box=(15,15), reusable=True, reflection=True) 

sage: T.number_of_solutions() # not tested 

212 

 

Animation of Donald Knuth's dancing links 

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

 

Animation of the solutions:: 

 

sage: from sage.combinat.tiling import Polyomino, TilingSolver 

sage: Y = Polyomino([(0,0),(1,0),(2,0),(3,0),(2,1)], color='yellow') 

sage: T = TilingSolver([Y], box=(15,15), reusable=True, reflection=True) 

sage: a = T.animate(stop=40) # long time # optional -- ImageMagick 

sage: a # long time # optional -- ImageMagick 

Animation with 40 frames 

 

Incremental animation of the solutions (one piece is removed/added at a time):: 

 

sage: a = T.animate('incremental', stop=40) # long time # optional -- ImageMagick 

sage: a # long time # optional -- ImageMagick 

Animation with 40 frames 

sage: a.show(delay=50, iterations=1) # long time # optional -- ImageMagick 

 

5d Easy Example 

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

 

Here is a 5d example. Let us try to fill the `2 \times 2 \times 2 \times 2 

\times 2` rectangle with reusable `1 \times 1 \times 1 \times 1 \times 1` 

rectangles. Obviously, there is one solution:: 

 

sage: from sage.combinat.tiling import Polyomino, TilingSolver 

sage: p = Polyomino([(0,0,0,0,0)]) 

sage: T = TilingSolver([p], box=(2,2,2,2,2), reusable=True) 

sage: rows = T.rows() # long time (3s) 

sage: rows # long time (fast) 

[[0], [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]] 

sage: T.number_of_solutions() # long time (fast) 

1 

 

REFERENCES: 

 

.. [Knuth1] Knuth, Donald (2000). "Dancing links". :arxiv:`cs/0011047`. 

 

""" 

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

# Copyright (C) 2011-2015 Sebastien Labbe <slabqc@gmail.com> 

# 

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

# as published by the Free Software Foundation; either version 2 of 

# the License, or (at your option) any later version. 

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

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

# python3 

from __future__ import division 

 

from builtins import zip 

from six import iteritems 

from six.moves import range 

 

import itertools 

from sage.structure.sage_object import SageObject 

from sage.modules.free_module_element import vector 

from sage.misc.mrange import xmrange 

from sage.misc.cachefunc import cached_method, cached_function 

from sage.misc.superseded import deprecated_function_alias 

 

 

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

# n-cube isometry group transformations 

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

def ncube_isometry_group(n, orientation_preserving=True): 

r""" 

Return the isometry group of the `n`-cube as a list of matrices. 

 

INPUT: 

 

- ``n`` -- positive integer, dimension of the space 

- ``orientation_preserving`` -- bool (optional, default: ``True``), 

whether the orientation is preserved 

 

OUTPUT: 

 

list of matrices 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import ncube_isometry_group 

sage: ncube_isometry_group(2) 

[ 

[1 0] [ 0 1] [-1 0] [ 0 -1] 

[0 1], [-1 0], [ 0 -1], [ 1 0] 

] 

sage: ncube_isometry_group(2, orientation_preserving=False) 

[ 

[1 0] [ 0 -1] [ 1 0] [ 0 1] [0 1] [-1 0] [ 0 -1] [-1 0] 

[0 1], [-1 0], [ 0 -1], [-1 0], [1 0], [ 0 -1], [ 1 0], [ 0 1] 

] 

 

There are 24 orientation preserving isometries of the 3-cube:: 

 

sage: ncube_isometry_group(3) 

[ 

[1 0 0] [ 1 0 0] [-1 0 0] [-1 0 0] [0 0 1] [ 0 0 -1] 

[0 1 0] [ 0 -1 0] [ 0 1 0] [ 0 -1 0] [1 0 0] [ 1 0 0] 

[0 0 1], [ 0 0 -1], [ 0 0 -1], [ 0 0 1], [0 1 0], [ 0 -1 0], 

<BLANKLINE> 

[ 0 0 -1] [ 0 0 1] [0 1 0] [ 0 -1 0] [ 0 1 0] [ 0 -1 0] 

[-1 0 0] [-1 0 0] [0 0 1] [ 0 0 -1] [ 0 0 -1] [ 0 0 1] 

[ 0 1 0], [ 0 -1 0], [1 0 0], [ 1 0 0], [-1 0 0], [-1 0 0], 

<BLANKLINE> 

[ 0 1 0] [ 0 -1 0] [ 0 1 0] [ 0 -1 0] [ 1 0 0] [ 1 0 0] 

[ 1 0 0] [ 1 0 0] [-1 0 0] [-1 0 0] [ 0 0 -1] [ 0 0 1] 

[ 0 0 -1], [ 0 0 1], [ 0 0 1], [ 0 0 -1], [ 0 1 0], [ 0 -1 0], 

<BLANKLINE> 

[-1 0 0] [-1 0 0] [ 0 0 -1] [ 0 0 1] [ 0 0 1] [ 0 0 -1] 

[ 0 0 1] [ 0 0 -1] [ 0 1 0] [ 0 -1 0] [ 0 1 0] [ 0 -1 0] 

[ 0 1 0], [ 0 -1 0], [ 1 0 0], [ 1 0 0], [-1 0 0], [-1 0 0] 

] 

 

TESTS:: 

 

sage: ncube_isometry_group(1) 

[[1]] 

sage: ncube_isometry_group(0) 

Traceback (most recent call last): 

... 

ValueError: ['B', 0] is not a valid Cartan type 

 

Is deprecated:: 

 

sage: from sage.combinat.tiling import orthogonal_transformation 

sage: L = orthogonal_transformation(2) 

doctest:...: DeprecationWarning: orthogonal_transformation is 

deprecated. Please use sage.combinat.tiling.ncube_isometry_group 

instead. See http://trac.sagemath.org/19107 for details. 

""" 

from sage.combinat.root_system.weyl_group import WeylGroup 

L = [w.matrix() for w in WeylGroup(['B', n])] 

if orientation_preserving: 

return [m for m in L if m.det() == 1] 

else: 

return L 

 

orthogonal_transformation = deprecated_function_alias(19107, ncube_isometry_group) 

@cached_function 

def ncube_isometry_group_cosets(n, orientation_preserving=True): 

r""" 

Return the quotient of the isometry group of the `n`-cube by the 

the isometry group of the rectangular parallelepiped. 

 

INPUT: 

 

- ``n`` -- positive integer, dimension of the space 

- ``orientation_preserving`` -- bool (optional, default: ``True``), 

whether the orientation is preserved 

 

OUTPUT: 

 

list of cosets, each coset being a sorted list of matrices 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import ncube_isometry_group_cosets 

sage: sorted(ncube_isometry_group_cosets(2)) 

[[ 

[-1 0] [1 0] 

[ 0 -1], [0 1] 

], [ 

[ 0 -1] [ 0 1] 

[ 1 0], [-1 0] 

]] 

sage: sorted(ncube_isometry_group_cosets(2, False)) 

[[ 

[-1 0] [-1 0] [ 1 0] [1 0] 

[ 0 -1], [ 0 1], [ 0 -1], [0 1] 

], [ 

[ 0 -1] [ 0 -1] [ 0 1] [0 1] 

[-1 0], [ 1 0], [-1 0], [1 0] 

]] 

 

:: 

 

sage: sorted(ncube_isometry_group_cosets(3)) 

[[ 

[-1 0 0] [-1 0 0] [ 1 0 0] [1 0 0] 

[ 0 -1 0] [ 0 1 0] [ 0 -1 0] [0 1 0] 

[ 0 0 1], [ 0 0 -1], [ 0 0 -1], [0 0 1] 

], [ 

[-1 0 0] [-1 0 0] [ 1 0 0] [ 1 0 0] 

[ 0 0 -1] [ 0 0 1] [ 0 0 -1] [ 0 0 1] 

[ 0 -1 0], [ 0 1 0], [ 0 1 0], [ 0 -1 0] 

], [ 

[ 0 -1 0] [ 0 -1 0] [ 0 1 0] [ 0 1 0] 

[-1 0 0] [ 1 0 0] [-1 0 0] [ 1 0 0] 

[ 0 0 -1], [ 0 0 1], [ 0 0 1], [ 0 0 -1] 

], [ 

[ 0 -1 0] [ 0 -1 0] [ 0 1 0] [0 1 0] 

[ 0 0 -1] [ 0 0 1] [ 0 0 -1] [0 0 1] 

[ 1 0 0], [-1 0 0], [-1 0 0], [1 0 0] 

], [ 

[ 0 0 -1] [ 0 0 -1] [ 0 0 1] [0 0 1] 

[-1 0 0] [ 1 0 0] [-1 0 0] [1 0 0] 

[ 0 1 0], [ 0 -1 0], [ 0 -1 0], [0 1 0] 

], [ 

[ 0 0 -1] [ 0 0 -1] [ 0 0 1] [ 0 0 1] 

[ 0 -1 0] [ 0 1 0] [ 0 -1 0] [ 0 1 0] 

[-1 0 0], [ 1 0 0], [ 1 0 0], [-1 0 0] 

]] 

 

TESTS:: 

 

sage: cosets = ncube_isometry_group_cosets(3, False) 

sage: len(cosets) 

6 

sage: [len(c) for c in cosets] 

[8, 8, 8, 8, 8, 8] 

sage: type(cosets[0][0]) 

<... 'sage.matrix.matrix_rational_dense.Matrix_rational_dense'> 

 

""" 

from sage.misc.misc_c import prod 

from sage.matrix.constructor import diagonal_matrix 

G = ncube_isometry_group(n, orientation_preserving) 

 

# Construct the subgroup H of G of diagonal matrices 

it = itertools.product((1,-1), repeat=n) 

if orientation_preserving: 

H = [diagonal_matrix(L) for L in it if prod(L) == 1] 

else: 

H = [diagonal_matrix(L) for L in it] 

 

# Make sure that H is a subset of G 

G_set = set(G) 

for h in H: h.set_immutable() 

assert all(h in G_set for h in H), "H must be a subset of G" 

 

# Construct the cosets 

cosets = [] 

while G_set: 

g = G_set.pop() 

left_coset = sorted(h*g for h in H) 

right_coset = sorted(g*h for h in H) 

assert left_coset == right_coset, "H must be a normal subgroup of G" 

for c in left_coset: c.set_immutable() 

G_set.difference_update(left_coset) 

cosets.append(left_coset) 

return cosets 

 

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

# Class Polyomino 

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

class Polyomino(SageObject): 

r""" 

A polyomino in `\ZZ^d`. 

 

The polyomino is the union of the unit square (or cube, or n-cube) 

centered at those coordinates. Such an object should be connected, but 

the code does not make this assumption. 

 

INPUT: 

 

- ``coords`` -- iterable of integer coordinates in `\ZZ^d` 

- ``color`` -- string (default: ``'gray'``), color for display 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: Polyomino([(0,0,0), (0,1,0), (1,1,0), (1,1,1)], color='blue') 

Polyomino: [(0, 0, 0), (0, 1, 0), (1, 1, 0), (1, 1, 1)], Color: blue 

""" 

def __init__(self, coords, color='gray'): 

r""" 

Constructor. 

 

See :mod:`Polyomino` for full documentation. 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: Polyomino([(0,0,0), (0,1,0), (1,1,0), (1,1,1)], color='blue') 

Polyomino: [(0, 0, 0), (0, 1, 0), (1, 1, 0), (1, 1, 1)], Color: blue 

 

:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: Polyomino([(0,0), (1,0), (2,0)]) 

Polyomino: [(0, 0), (1, 0), (2, 0)], Color: gray 

""" 

from sage.modules.free_module import FreeModule 

from sage.rings.integer_ring import ZZ 

 

if not isinstance(color, str): 

raise TypeError("color = ({!r}) must be a string".format(color)) 

self._color = color 

 

if not isinstance(coords, (tuple,list)): 

coords = list(coords) 

if not coords: 

raise ValueError("Polyomino must be non empty") 

 

self._dimension = ZZ(len(coords[0])) 

self._free_module = FreeModule(ZZ, self._dimension) 

 

self._blocs = coords 

self._blocs = map(self._free_module, self._blocs) 

for b in self._blocs: 

b.set_immutable() 

self._blocs = frozenset(self._blocs) 

 

def _repr_(self): 

r""" 

String representation. 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: Polyomino([(0,0,0), (0,1,0), (1,1,0), (1,1,1)], color='red') 

Polyomino: [(0, 0, 0), (0, 1, 0), (1, 1, 0), (1, 1, 1)], Color: red 

""" 

s = "Polyomino: %s, " % sorted(self._blocs) 

s += "Color: %s" % self._color 

return s 

 

def color(self): 

r""" 

Return the color of the polyomino. 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0,0), (0,1,0), (1,1,0), (1,1,1)], color='blue') 

sage: p.color() 

'blue' 

""" 

return self._color 

 

def frozenset(self): 

r""" 

Return the elements of `\ZZ^d` in the polyomino as a frozenset. 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0,0), (0,1,0), (1,1,0), (1,1,1)], color='red') 

sage: p.frozenset() 

frozenset({(0, 0, 0), (0, 1, 0), (1, 1, 0), (1, 1, 1)}) 

""" 

return self._blocs 

 

@cached_method 

def sorted_list(self): 

r""" 

Return the color of the polyomino. 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0,0), (0,1,0), (1,1,0), (1,1,1)], color='blue') 

sage: p.sorted_list() 

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

""" 

return sorted(self.frozenset()) 

 

def __len__(self): 

r""" 

Return the size of the polyomino, i.e. the number of n-dimensional 

unit cubes. 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0,0), (0,1,0), (1,1,0), (1,1,1)], color='blue') 

sage: len(p) 

4 

""" 

return len(self.frozenset()) 

 

def __iter__(self): 

r""" 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0,0), (0,1,0), (1,1,0), (1,1,1)], color='blue') 

sage: it = iter(p) 

sage: next(it) 

(0, 0, 0) 

""" 

return iter(self.sorted_list()) 

 

@cached_method 

def bounding_box(self): 

r""" 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0,0),(1,0,0),(1,1,0),(1,1,1),(1,2,0)], color='deeppink') 

sage: p.bounding_box() 

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

""" 

zipped_coords = list(zip(*self)) 

return [[min(_) for _ in zipped_coords], 

[max(_) for _ in zipped_coords]] 

 

def __hash__(self): 

r""" 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0,0), (0,1,0), (1,1,0), (1,1,1)], color='blue') 

sage: hash(p) # random 

2059134902 

""" 

return hash(self.frozenset()) 

 

def __eq__(self, other): 

r""" 

Return whether self is equal to other. 

 

INPUT: 

 

- ``other`` - a polyomino 

 

OUTPUT: 

 

boolean 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0,0), (0,1,0), (1,1,0), (1,1,1)], color='blue') 

sage: q = Polyomino([(0,0,0), (0,1,0), (1,1,0), (1,1,1)], color='red') 

sage: p == q 

True 

sage: r = Polyomino([(0,0,0), (0,1,0), (1,1,0)], color='blue') 

sage: p == r 

False 

""" 

return isinstance(other, Polyomino) and self.frozenset() == other.frozenset() 

 

def __ne__(self, other): 

r""" 

Return whether self is not equal to other. 

 

INPUT: 

 

- ``other`` - a polyomino 

 

OUTPUT: 

 

boolean 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0,0), (0,1,0), (1,1,0), (1,1,1)], color='blue') 

sage: q = Polyomino([(0,0,0), (0,1,0), (1,1,0), (1,1,1)], color='red') 

sage: p != q 

False 

sage: r = Polyomino([(0,0,0), (0,1,0), (1,1,0)], color='blue') 

sage: p != r 

True 

""" 

return not self.frozenset() == other.frozenset() 

 

def __le__(self, other): 

r""" 

Return whether self is inside of other. 

 

INPUT: 

 

- ``other`` - a polyomino 

 

OUTPUT: 

 

boolean 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0)]) 

sage: b = Polyomino([(0,0), (0,1), (1,1), (2,1)]) 

sage: p <= b 

True 

sage: b <= p 

False 

""" 

return isinstance(other, Polyomino) and self.frozenset() <= other.frozenset() 

 

def __ge__(self, other): 

r""" 

Return whether self contains other. 

 

INPUT: 

 

- ``other`` - a polyomino 

 

OUTPUT: 

 

boolean 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0)]) 

sage: b = Polyomino([(0,0), (0,1), (1,1), (2,1)]) 

sage: p >= b 

False 

sage: b >= p 

True 

""" 

return isinstance(other, Polyomino) and self.frozenset() >= other.frozenset() 

 

def __sub__(self, v): 

r""" 

Return a translated copy of self by the opposite of the 

vector v. 

 

INPUT: 

 

- ``v`` - tuple 

 

OUTPUT: 

 

polyomino 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0,0),(1,0,0),(1,1,0),(1,1,1),(1,2,0)], color='deeppink') 

sage: p - (2,2,2) 

Polyomino: [(-2, -2, -2), (-1, -2, -2), (-1, -1, -2), (-1, -1, -1), (-1, 0, -2)], Color: deeppink 

""" 

v = self._free_module(v) 

return Polyomino([p-v for p in self], color=self._color) 

 

def __add__(self, v): 

r""" 

Return a translated copy of self by the vector v. 

 

INPUT: 

 

- ``v`` - tuple 

 

OUTPUT: 

 

polyomino 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0,0),(1,0,0),(1,1,0),(1,1,1),(1,2,0)], color='deeppink') 

sage: p + (2,2,2) 

Polyomino: [(2, 2, 2), (3, 2, 2), (3, 3, 2), (3, 3, 3), (3, 4, 2)], Color: deeppink 

""" 

v = self._free_module(v) 

return Polyomino([p+v for p in self], color=self._color) 

 

def __rmul__(self, m): 

r""" 

Return the image of the polyomino under the application of the 

matrix m. 

 

INPUT: 

 

- ``m`` - square matrix, matching the dimension of self. 

 

OUTPUT: 

 

Polyomino 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0,0),(1,0,0),(1,1,0),(1,1,1),(1,2,0)], color='deeppink') 

sage: m = matrix(3, [1,0,0,0,1,0,0,0,1]) 

sage: m * p 

Polyomino: [(0, 0, 0), (1, 0, 0), (1, 1, 0), (1, 1, 1), (1, 2, 0)], Color: deeppink 

sage: m = matrix(3, [1,0,0,0,0,-1,0,1,0]) 

sage: m * p 

Polyomino: [(0, 0, 0), (1, -1, 1), (1, 0, 0), (1, 0, 1), (1, 0, 2)], Color: deeppink 

 

TESTS:: 

 

sage: m = matrix(2, [1,0,0,1]) 

sage: m * p 

Traceback (most recent call last): 

... 

ValueError: Dimension of input matrix must match the dimension of the polyomino 

""" 

if not m.nrows() == m.ncols() == self._dimension: 

raise ValueError("Dimension of input matrix must match the " 

"dimension of the polyomino") 

return Polyomino([m * p for p in self], color=self._color) 

 

def canonical(self): 

r""" 

Returns the translated copy of self having minimal and nonnegative 

coordinates 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0,0),(1,0,0),(1,1,0),(1,1,1),(1,2,0)], color='deeppink') 

sage: p 

Polyomino: [(0, 0, 0), (1, 0, 0), (1, 1, 0), (1, 1, 1), (1, 2, 0)], Color: deeppink 

sage: p.canonical() 

Polyomino: [(0, 0, 0), (1, 0, 0), (1, 1, 0), (1, 1, 1), (1, 2, 0)], Color: deeppink 

 

TESTS:: 

 

sage: p 

Polyomino: [(0, 0, 0), (1, 0, 0), (1, 1, 0), (1, 1, 1), (1, 2, 0)], Color: deeppink 

sage: p + (3,4,5) 

Polyomino: [(3, 4, 5), (4, 4, 5), (4, 5, 5), (4, 5, 6), (4, 6, 5)], Color: deeppink 

sage: (p + (3,4,5)).canonical() 

Polyomino: [(0, 0, 0), (1, 0, 0), (1, 1, 0), (1, 1, 1), (1, 2, 0)], Color: deeppink 

""" 

minxyz, maxxyz = self.bounding_box() 

return self - minxyz 

 

def canonical_isometric_copies(self, orientation_preserving=True, 

mod_box_isometries=False): 

r""" 

Return the set of image of self under isometries of the `n`-cube 

where the coordinates are all nonnegative and minimal. 

 

INPUT: 

 

- ``orientation_preserving`` -- bool (optional, default: ``True``), 

If True, the group of isometries of the `n`-cube is restricted to 

those that preserve the orientation, i.e. of determinant 1. 

 

- ``mod_box_isometries`` -- bool (default: ``False``), whether to 

quotient the group of isometries of the `n`-cube by the 

subgroup of isometries of the `a_1\times a_2\cdots \times a_n` 

rectangular box where are the `a_i` are assumed to be distinct. 

 

OUTPUT: 

 

set of Polyomino 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0,0), (0,1,0), (1,1,0), (1,1,1)], color='blue') 

sage: s = p.canonical_isometric_copies() 

sage: len(s) 

12 

 

With the non orientation-preserving:: 

 

sage: s = p.canonical_isometric_copies(orientation_preserving=False) 

sage: len(s) 

24 

 

Modulo rotation by angle 180 degrees:: 

 

sage: s = p.canonical_isometric_copies(mod_box_isometries=True) 

sage: len(s) 

3 

 

TESTS:: 

 

sage: from sage.games.quantumino import pentaminos 

sage: [len(p.canonical_isometric_copies((5,8,2), mod_box_isometries=False)) for p in pentaminos] 

[24, 24, 24, 24, 24, 24, 12, 12, 24, 24, 24, 24, 12, 12, 24, 24, 12] 

sage: [len(p.canonical_isometric_copies((5,8,2), mod_box_isometries=True)) for p in pentaminos] 

[6, 6, 6, 6, 6, 6, 3, 3, 6, 6, 6, 6, 3, 3, 6, 6, 3] 

""" 

if mod_box_isometries: 

L = ncube_isometry_group_cosets(self._dimension, orientation_preserving) 

P_cosets = set(frozenset((m * self).canonical() for m in coset) for coset in L) 

return set(next(iter(s)) for s in P_cosets) 

else: 

L = ncube_isometry_group(self._dimension, orientation_preserving) 

return set((m * self).canonical() for m in L) 

 

def translated_copies(self, box): 

r""" 

Returns an iterator over the translated images of self inside a 

polyomino. 

 

INPUT: 

 

- ``box`` -- Polyomino or tuple of integers (size of a box) 

 

OUTPUT: 

 

iterator of 3d polyominoes 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0,0),(1,0,0),(1,1,0),(1,1,1),(1,2,0)], color='deeppink') 

sage: for t in p.translated_copies(box=(5,8,2)): t 

Polyomino: [(0, 0, 0), (1, 0, 0), (1, 1, 0), (1, 1, 1), (1, 2, 0)], Color: deeppink 

Polyomino: [(0, 1, 0), (1, 1, 0), (1, 2, 0), (1, 2, 1), (1, 3, 0)], Color: deeppink 

Polyomino: [(0, 2, 0), (1, 2, 0), (1, 3, 0), (1, 3, 1), (1, 4, 0)], Color: deeppink 

Polyomino: [(0, 3, 0), (1, 3, 0), (1, 4, 0), (1, 4, 1), (1, 5, 0)], Color: deeppink 

Polyomino: [(0, 4, 0), (1, 4, 0), (1, 5, 0), (1, 5, 1), (1, 6, 0)], Color: deeppink 

Polyomino: [(0, 5, 0), (1, 5, 0), (1, 6, 0), (1, 6, 1), (1, 7, 0)], Color: deeppink 

Polyomino: [(1, 0, 0), (2, 0, 0), (2, 1, 0), (2, 1, 1), (2, 2, 0)], Color: deeppink 

Polyomino: [(1, 1, 0), (2, 1, 0), (2, 2, 0), (2, 2, 1), (2, 3, 0)], Color: deeppink 

Polyomino: [(1, 2, 0), (2, 2, 0), (2, 3, 0), (2, 3, 1), (2, 4, 0)], Color: deeppink 

Polyomino: [(1, 3, 0), (2, 3, 0), (2, 4, 0), (2, 4, 1), (2, 5, 0)], Color: deeppink 

Polyomino: [(1, 4, 0), (2, 4, 0), (2, 5, 0), (2, 5, 1), (2, 6, 0)], Color: deeppink 

Polyomino: [(1, 5, 0), (2, 5, 0), (2, 6, 0), (2, 6, 1), (2, 7, 0)], Color: deeppink 

Polyomino: [(2, 0, 0), (3, 0, 0), (3, 1, 0), (3, 1, 1), (3, 2, 0)], Color: deeppink 

Polyomino: [(2, 1, 0), (3, 1, 0), (3, 2, 0), (3, 2, 1), (3, 3, 0)], Color: deeppink 

Polyomino: [(2, 2, 0), (3, 2, 0), (3, 3, 0), (3, 3, 1), (3, 4, 0)], Color: deeppink 

Polyomino: [(2, 3, 0), (3, 3, 0), (3, 4, 0), (3, 4, 1), (3, 5, 0)], Color: deeppink 

Polyomino: [(2, 4, 0), (3, 4, 0), (3, 5, 0), (3, 5, 1), (3, 6, 0)], Color: deeppink 

Polyomino: [(2, 5, 0), (3, 5, 0), (3, 6, 0), (3, 6, 1), (3, 7, 0)], Color: deeppink 

Polyomino: [(3, 0, 0), (4, 0, 0), (4, 1, 0), (4, 1, 1), (4, 2, 0)], Color: deeppink 

Polyomino: [(3, 1, 0), (4, 1, 0), (4, 2, 0), (4, 2, 1), (4, 3, 0)], Color: deeppink 

Polyomino: [(3, 2, 0), (4, 2, 0), (4, 3, 0), (4, 3, 1), (4, 4, 0)], Color: deeppink 

Polyomino: [(3, 3, 0), (4, 3, 0), (4, 4, 0), (4, 4, 1), (4, 5, 0)], Color: deeppink 

Polyomino: [(3, 4, 0), (4, 4, 0), (4, 5, 0), (4, 5, 1), (4, 6, 0)], Color: deeppink 

Polyomino: [(3, 5, 0), (4, 5, 0), (4, 6, 0), (4, 6, 1), (4, 7, 0)], Color: deeppink 

 

This method is independant of the translation of the polyomino:: 

 

sage: q = Polyomino([(0,0,0), (1,0,0)]) 

sage: list(q.translated_copies((2,2,1))) 

[Polyomino: [(0, 0, 0), (1, 0, 0)], Color: gray, Polyomino: [(0, 1, 0), (1, 1, 0)], Color: gray] 

sage: q = Polyomino([(34,7,-9), (35,7,-9)]) 

sage: list(q.translated_copies((2,2,1))) 

[Polyomino: [(0, 0, 0), (1, 0, 0)], Color: gray, Polyomino: [(0, 1, 0), (1, 1, 0)], Color: gray] 

 

Inside smaller boxes:: 

 

sage: list(p.translated_copies(box=(2,2,3))) 

[] 

sage: list(p.translated_copies(box=(2,3,2))) 

[Polyomino: [(0, 0, 0), (1, 0, 0), (1, 1, 0), (1, 1, 1), (1, 2, 0)], Color: deeppink] 

sage: list(p.translated_copies(box=(3,2,2))) 

[] 

sage: list(p.translated_copies(box=(1,1,1))) 

[] 

 

Using a Polyomino as input:: 

 

sage: b = Polyomino([(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)]) 

sage: p = Polyomino([(0,0)]) 

sage: list(p.translated_copies(b)) 

[Polyomino: [(0, 0)], Color: gray, 

Polyomino: [(0, 1)], Color: gray, 

Polyomino: [(0, 2)], Color: gray, 

Polyomino: [(1, 0)], Color: gray, 

Polyomino: [(1, 1)], Color: gray, 

Polyomino: [(1, 2)], Color: gray] 

 

:: 

 

sage: p = Polyomino([(0,0), (1,0), (0,1)]) 

sage: b = Polyomino([(0,0), (1,0), (2,0), (0,1), (1,1), (0,2)]) 

sage: list(p.translated_copies(b)) 

[Polyomino: [(0, 0), (0, 1), (1, 0)], Color: gray, 

Polyomino: [(0, 1), (0, 2), (1, 1)], Color: gray, 

Polyomino: [(1, 0), (1, 1), (2, 0)], Color: gray] 

""" 

if not isinstance(box, Polyomino): 

ranges = [range(a) for a in box] 

box = Polyomino(itertools.product(*ranges)) 

if not box._dimension == self._dimension: 

raise ValueError("Dimension of input box must match the " 

"dimension of the polyomino") 

minxyz, maxxyz = map(vector, self.bounding_box()) 

size = maxxyz - minxyz 

boxminxyz, boxmaxxyz = box.bounding_box() 

ranges = [range(a, b-c+1) for (a,b,c) in zip(boxminxyz, 

boxmaxxyz, 

size)] 

cano = self.canonical() 

for v in itertools.product(*ranges): 

translated = cano + v 

if translated <= box: 

yield translated 

 

def isometric_copies(self, box, orientation_preserving=True, 

mod_box_isometries=False): 

r""" 

Return the translated and isometric images of self that lies in the box. 

 

INPUT: 

 

- ``box`` -- Polyomino or tuple of integers (size of a box) 

 

- ``orientation_preserving`` -- bool (optional, default: ``True``), 

If True, the group of isometries of the `n`-cube is restricted to 

those that preserve the orientation, i.e. of determinant 1. 

 

- ``mod_box_isometries`` -- bool (default: ``False``), whether to 

quotient the group of isometries of the `n`-cube by the 

subgroup of isometries of the `a_1\times a_2\cdots \times a_n` 

rectangular box where are the `a_i` are assumed to be distinct. 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0,0),(1,0,0),(1,1,0),(1,1,1),(1,2,0)], color='deeppink') 

sage: L = list(p.isometric_copies(box=(5,8,2))) 

sage: len(L) 

360 

 

:: 

 

sage: p = Polyomino([(0,0,0),(1,0,0),(1,1,0),(1,2,0),(1,2,1)], color='orange') 

sage: L = list(p.isometric_copies(box=(5,8,2))) 

sage: len(L) 

180 

sage: L = list(p.isometric_copies((5,8,2), False)) 

sage: len(L) 

360 

sage: L = list(p.isometric_copies((5,8,2), mod_box_isometries=True)) 

sage: len(L) 

45 

 

:: 

 

sage: p = Polyomino([(0,0), (1,0), (0,1)]) 

sage: b = Polyomino([(0,0), (1,0), (2,0), (0,1), (1,1), (0,2)]) 

sage: list(p.isometric_copies(b)) 

[Polyomino: [(0, 0), (1, 0), (1, 1)], Color: gray, 

Polyomino: [(0, 0), (0, 1), (1, 0)], Color: gray, 

Polyomino: [(0, 1), (0, 2), (1, 1)], Color: gray, 

Polyomino: [(1, 0), (1, 1), (2, 0)], Color: gray, 

Polyomino: [(0, 0), (0, 1), (1, 1)], Color: gray, 

Polyomino: [(0, 1), (1, 0), (1, 1)], Color: gray] 

""" 

if not isinstance(box, Polyomino): 

ranges = [range(a) for a in box] 

box = Polyomino(itertools.product(*ranges)) 

if not box._dimension == self._dimension: 

raise ValueError("Dimension of input box must match the " 

"dimension of the polyomino") 

box_min_coords, box_max_coords = box.bounding_box() 

if mod_box_isometries and len(set(b-a for (a,b) in zip(box_min_coords, 

box_max_coords))) < box._dimension: 

raise NotImplementedError("The code below assumes that the" 

" sizes of the box (={}) are all distinct when" 

" argument `mod_box_isometries` is True.".format(box)) 

all_distinct_cano = self.canonical_isometric_copies(orientation_preserving, 

mod_box_isometries) 

for cano in all_distinct_cano: 

for t in cano.translated_copies(box=box): 

yield t 

 

def neighbor_edges(self): 

r""" 

Return an iterator over the pairs of neighbor coordinates inside of 

the polyomino. 

 

Two points `P` and `Q` in the polyomino are neighbor if `P - Q` has 

one coordinate equal to `+1` or `-1` and zero everywhere else. 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0,0),(0,0,1)]) 

sage: list(sorted(edge) for edge in p.neighbor_edges()) 

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

 

In 3d:: 

 

sage: p = Polyomino([(0,0,0),(1,0,0),(1,1,0),(1,1,1),(1,2,0)], color='deeppink') 

sage: L = sorted(sorted(edge) for edge in p.neighbor_edges()) 

sage: for a in L: a 

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

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

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

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

 

In 2d:: 

 

sage: p = Polyomino([(0,0),(1,0),(1,1),(1,2)]) 

sage: L = sorted(sorted(edge) for edge in p.neighbor_edges()) 

sage: for a in L: a 

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

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

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

""" 

for P, Q in itertools.combinations(self, 2): 

s = sorted(map(abs, Q-P)) 

firsts = s[:-1] 

last = s[-1] 

if last == 1 and all(f == 0 for f in firsts): 

yield P, Q 

 

def center(self): 

r""" 

Return the center of the polyomino. 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0,0),(0,0,1)]) 

sage: p.center() 

(0, 0, 1/2) 

 

In 3d:: 

 

sage: p = Polyomino([(0,0,0),(1,0,0),(1,1,0),(1,1,1),(1,2,0)], color='deeppink') 

sage: p.center() 

(4/5, 4/5, 1/5) 

 

In 2d:: 

 

sage: p = Polyomino([(0,0),(1,0),(1,1),(1,2)]) 

sage: p.center() 

(3/4, 3/4) 

""" 

return sum(self) / len(self) 

 

def boundary(self): 

r""" 

Return the boundary of a 2d polyomino. 

 

INPUT: 

 

- ``self`` - a 2d polyomino 

 

OUTPUT: 

 

- list of edges (an edge is a pair of adjacent 2d coordinates) 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0), (1,0), (0,1), (1,1)]) 

sage: p.boundary() 

[((0.5, 1.5), (1.5, 1.5)), ((-0.5, -0.5), (0.5, -0.5)), ((0.5, -0.5), (1.5, -0.5)), ((-0.5, 1.5), (0.5, 1.5)), ((-0.5, 0.5), (-0.5, 1.5)), ((-0.5, -0.5), (-0.5, 0.5)), ((1.5, 0.5), (1.5, 1.5)), ((1.5, -0.5), (1.5, 0.5))] 

sage: len(_) 

8 

sage: p = Polyomino([(5,5)]) 

sage: p.boundary() 

[((4.5, 5.5), (5.5, 5.5)), ((4.5, 4.5), (5.5, 4.5)), ((4.5, 4.5), (4.5, 5.5)), ((5.5, 4.5), (5.5, 5.5))] 

""" 

if self._dimension != 2: 

raise NotImplementedError("The method boundary is currently " 

"implemented " 

"only for dimension 2") 

from collections import defaultdict 

horizontal = defaultdict(int) 

vertical = defaultdict(int) 

for a in self: 

x, y = a = tuple(a) 

horizontal[a] += 1 

vertical[a] += 1 

horizontal[(x, y+1)] -= 1 

vertical[(x+1, y)] -= 1 

edges = [] 

h = 0.5 

for (x, y), coeff in iteritems(horizontal): 

if coeff: 

edges.append(((x-h, y-h), (x+h, y-h))) 

for (x, y), coeff in iteritems(vertical): 

if coeff: 

edges.append(((x-h, y-h), (x-h, y+h))) 

return edges 

 

def show3d(self, size=1): 

r""" 

Returns a 3d Graphic object representing the polyomino. 

 

INPUT: 

 

- ``self`` - a polyomino of dimension 3 

- ``size`` - number (optional, default: ``1``), the size of each 

``1 \times 1 \times 1`` cube. This does a homothety with respect 

to the center of the polyomino. 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0,0), (0,1,0), (1,1,0), (1,1,1)], color='blue') 

sage: p.show3d() # long time (2s) 

Graphics3d Object 

""" 

assert self._dimension == 3, "Dimension of the polyomino must be 3." 

from sage.plot.graphics import Graphics 

from sage.plot.plot3d.platonic import cube 

G = Graphics() 

for p in self: 

G += cube(p, color=self._color) 

center = self.center() 

G = G.translate(-center) 

G = G.scale(size) 

G = G.translate(center) 

return G 

 

def show2d(self, size=0.7, color='black', thickness=1): 

r""" 

Returns a 2d Graphic object representing the polyomino. 

 

INPUT: 

 

- ``self`` - a polyomino of dimension 2 

- ``size`` - number (optional, default: ``0.7``), the size of each 

square. 

- ``color`` - color (optional, default: ``'black'``), color of 

the boundary line. 

- ``thickness`` - number (optional, default: ``1``), how thick the 

boundary line is. 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino 

sage: p = Polyomino([(0,0),(1,0),(1,1),(1,2)], color='deeppink') 

sage: p.show2d() # long time (0.5s) 

Graphics object consisting of 17 graphics primitives 

""" 

assert self._dimension == 2, "Dimension of the polyomino must be 2." 

from sage.plot.graphics import Graphics 

from sage.plot.circle import circle 

from sage.plot.line import line 

from sage.plot.polygon import polygon 

h = size / 2.0 

G = Graphics() 

for a, b in self: 

G += circle((a, b), h, fill=True, color=self._color) 

k = h / 2.0 

for P, Q in self.neighbor_edges(): 

a, b = (P + Q) / 2.0 

G += polygon([(a-k, b-k), (a+k, b-k), (a+k, b+k), (a-k, b+k), 

(a-k, b-k)], color=self._color) 

for edge in self.boundary(): 

G += line(edge, color=color, thickness=thickness) 

return G 

 

canonical_orthogonals = deprecated_function_alias(19107, canonical_isometric_copies) 

translated = deprecated_function_alias(19107, translated_copies) 

translated_orthogonals = deprecated_function_alias(19107, isometric_copies) 

 

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

# General tiling solver 

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

class TilingSolver(SageObject): 

r""" 

Tiling solver 

 

Solve the problem of tiling a rectangular box with a certain number 

of pieces, called polyominoes, where each polyomino must be used 

exactly once. 

 

INPUT: 

 

- ``pieces`` -- iterable of Polyominoes 

- ``box`` -- Polyomino or tuple of integers (size of a box) 

- ``rotation`` -- bool (optional, default: ``True``), whether to allow 

rotations 

- ``reflection`` -- bool (optional, default: ``False``), whether to allow 

reflections 

- ``reusable`` -- bool (optional, default: ``False``), whether to allow 

the pieces to be reused 

 

EXAMPLES: 

 

By default, rotations are allowed and reflections are not allowed:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: p = Polyomino([(0,0,0)]) 

sage: q = Polyomino([(0,0,0), (0,0,1)]) 

sage: r = Polyomino([(0,0,0), (0,0,1), (0,0,2)]) 

sage: T = TilingSolver([p,q,r], box=(1,1,6)) 

sage: T 

Tiling solver of 3 pieces into a box of size 6 

Rotation allowed: True 

Reflection allowed: False 

Reusing pieces allowed: False 

 

Solutions are given by an iterator:: 

 

sage: it = T.solve() 

sage: for p in next(it): p 

Polyomino: [(0, 0, 0)], Color: gray 

Polyomino: [(0, 0, 1), (0, 0, 2)], Color: gray 

Polyomino: [(0, 0, 3), (0, 0, 4), (0, 0, 5)], Color: gray 

 

Another solution:: 

 

sage: for p in next(it): p 

Polyomino: [(0, 0, 0)], Color: gray 

Polyomino: [(0, 0, 1), (0, 0, 2), (0, 0, 3)], Color: gray 

Polyomino: [(0, 0, 4), (0, 0, 5)], Color: gray 

 

Tiling of a polyomino by polyominoes:: 

 

sage: b = Polyomino([(0,0), (1,0), (1,1), (2,1), (1,2), (2,2), (0,3), (1,3)]) 

sage: p = Polyomino([(0,0), (1,0)]) 

sage: T = TilingSolver([p], box=b, reusable=True) 

sage: T.number_of_solutions() 

2 

 

TESTS:: 

 

sage: T = TilingSolver([p,q,r], box=(1,1,6), rotation=False, reflection=True) 

Traceback (most recent call last): 

... 

NotImplementedError: When reflection is allowed and rotation is not allowed 

""" 

def __init__(self, pieces, box, rotation=True, 

reflection=False, reusable=False): 

r""" 

Constructor. 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: p = Polyomino([(0,0,0)]) 

sage: q = Polyomino([(0,0,0), (0,0,1)]) 

sage: r = Polyomino([(0,0,0), (0,0,1), (0,0,2)]) 

sage: T = TilingSolver([p,q,r], box=(1,1,6)) 

sage: T 

Tiling solver of 3 pieces into a box of size 6 

Rotation allowed: True 

Reflection allowed: False 

Reusing pieces allowed: False 

""" 

self._pieces = pieces 

self._free_module = self._pieces[0]._free_module 

if isinstance(box, Polyomino): 

self._box = box 

else: 

ranges = [range(a) for a in box] 

self._box = Polyomino(itertools.product(*ranges)) 

self._rotation = rotation 

self._reflection = reflection 

if not self._rotation and self._reflection: 

raise NotImplementedError("When reflection is allowed and " 

"rotation is not allowed") 

self._reusable = reusable 

 

def _repr_(self): 

r""" 

String representation 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: p = Polyomino([(0,0,0)]) 

sage: q = Polyomino([(0,0,0), (0,0,1)]) 

sage: r = Polyomino([(0,0,0), (0,0,1), (0,0,2)]) 

sage: TilingSolver([p,q,r], box=(1,1,6)) 

Tiling solver of 3 pieces into a box of size 6 

Rotation allowed: True 

Reflection allowed: False 

Reusing pieces allowed: False 

 

""" 

s = "Tiling solver of %s pieces " % len(self._pieces) 

s += "into a box of size %s\n" % len(self._box) 

s += "Rotation allowed: %s\n" % self._rotation 

s += "Reflection allowed: %s\n" % self._reflection 

s += "Reusing pieces allowed: %s" % self._reusable 

return s 

 

def is_suitable(self): 

r""" 

Return whether the volume of the box is equal to sum of the volume 

of the polyominoes and the number of rows sent to the DLX solver is 

larger than zero. 

 

If these conditions are not verified, then the problem is not suitable 

in the sense that there are no solution. 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: p = Polyomino([(0,0,0)]) 

sage: q = Polyomino([(0,0,0), (0,0,1)]) 

sage: r = Polyomino([(0,0,0), (0,0,1), (0,0,2)]) 

sage: T = TilingSolver([p,q,r], box=(1,1,6)) 

sage: T.is_suitable() 

True 

sage: T = TilingSolver([p,q,r], box=(1,1,7)) 

sage: T.is_suitable() 

False 

""" 

if self._reusable: 

return len(self.rows()) != 0 

else: 

from sage.misc.misc_c import prod 

return (sum(len(p) for p in self.pieces()) == len(self._box) 

and len(self.rows()) != 0) 

 

def pieces(self): 

r""" 

Return the list of pieces. 

 

OUTPUT: 

 

list of 3d polyominoes 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: p = Polyomino([(0,0,0)]) 

sage: q = Polyomino([(0,0,0), (0,0,1)]) 

sage: r = Polyomino([(0,0,0), (0,0,1), (0,0,2)]) 

sage: T = TilingSolver([p,q,r], box=(1,1,6)) 

sage: for p in T._pieces: p 

Polyomino: [(0, 0, 0)], Color: gray 

Polyomino: [(0, 0, 0), (0, 0, 1)], Color: gray 

Polyomino: [(0, 0, 0), (0, 0, 1), (0, 0, 2)], Color: gray 

""" 

return self._pieces 

 

def space(self): 

r""" 

Returns an iterator over all the non negative integer coordinates 

contained in the space to tile. 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: p = Polyomino([(0,0,0)]) 

sage: q = Polyomino([(0,0,0), (0,0,1)]) 

sage: r = Polyomino([(0,0,0), (0,0,1), (0,0,2)]) 

sage: T = TilingSolver([p,q,r], box=(1,1,6)) 

sage: list(T.space()) 

[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 0, 4), (0, 0, 5)] 

""" 

return iter(self._box) 

 

@cached_method 

def coord_to_int_dict(self): 

r""" 

Returns a dictionary mapping coordinates to integers. 

 

OUTPUT: 

 

dict 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: p = Polyomino([(0,0,0)]) 

sage: q = Polyomino([(0,0,0), (0,0,1)]) 

sage: r = Polyomino([(0,0,0), (0,0,1), (0,0,2)]) 

sage: T = TilingSolver([p,q,r], box=(1,1,6)) 

sage: A = T.coord_to_int_dict() 

sage: sorted(A.items()) 

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

 

Reusable pieces:: 

 

sage: p = Polyomino([(0,0), (0,1)]) 

sage: q = Polyomino([(0,0), (0,1), (1,0), (1,1)]) 

sage: T = TilingSolver([p,q], box=[3,2], reusable=True) 

sage: B = T.coord_to_int_dict() 

sage: sorted(B.items()) 

[((0, 0), 0), ((0, 1), 1), ((1, 0), 2), ((1, 1), 3), ((2, 0), 4), ((2, 1), 5)] 

""" 

if self._reusable: 

return dict((c, i) for i, c in enumerate(self.space())) 

else: 

number_of_pieces = len(self._pieces) 

return dict((c, i+number_of_pieces) 

for i, c in enumerate(self.space())) 

 

@cached_method 

def int_to_coord_dict(self): 

r""" 

Returns a dictionary mapping integers to coordinates. 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: p = Polyomino([(0,0,0)]) 

sage: q = Polyomino([(0,0,0), (0,0,1)]) 

sage: r = Polyomino([(0,0,0), (0,0,1), (0,0,2)]) 

sage: T = TilingSolver([p,q,r], box=(1,1,6)) 

sage: B = T.int_to_coord_dict() 

sage: sorted(B.items()) 

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

 

Reusable pieces:: 

 

sage: from sage.combinat.tiling import Polyomino, TilingSolver 

sage: p = Polyomino([(0,0), (0,1)]) 

sage: q = Polyomino([(0,0), (0,1), (1,0), (1,1)]) 

sage: T = TilingSolver([p,q], box=[3,2], reusable=True) 

sage: B = T.int_to_coord_dict() 

sage: sorted(B.items()) 

[(0, (0, 0)), (1, (0, 1)), (2, (1, 0)), (3, (1, 1)), (4, (2, 0)), (5, (2, 1))] 

 

TESTS: 

 

The methods ``int_to_coord_dict`` and ``coord_to_int_dict`` returns 

dictionary that are inverse of each other:: 

 

sage: A = T.coord_to_int_dict() 

sage: B = T.int_to_coord_dict() 

sage: all(A[B[i]] == i for i in B) 

True 

sage: all(B[A[i]] == i for i in A) 

True 

 

""" 

if self._reusable: 

return dict((i, c) for i, c in enumerate(self.space())) 

else: 

number_of_pieces = len(self._pieces) 

return dict((i+number_of_pieces, c) 

for i, c in enumerate(self.space())) 

 

@cached_method 

def rows_for_piece(self, i, mod_box_isometries=False): 

r""" 

Return the rows for the i-th piece. 

 

INPUT: 

 

- ``i`` -- integer, the i-th piece 

 

- ``mod_box_isometries`` -- bool (default: ``False``), whether to 

consider only rows for positions up to the action of the 

quotient the group of isometries of the `n`-cube by the 

subgroup of isometries of the `a_1\times a_2\cdots \times a_n` 

rectangular box where are the `a_i` are assumed to be distinct. 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: p = Polyomino([(0,0,0)]) 

sage: q = Polyomino([(0,0,0), (0,0,1)]) 

sage: r = Polyomino([(0,0,0), (0,0,1), (0,0,2)]) 

sage: T = TilingSolver([p,q,r], box=(1,1,6)) 

sage: T.rows_for_piece(0) 

[[0, 3], [0, 4], [0, 5], [0, 6], [0, 7], [0, 8]] 

sage: T.rows_for_piece(1) 

[[1, 3, 4], [1, 4, 5], [1, 5, 6], [1, 6, 7], [1, 7, 8]] 

sage: T.rows_for_piece(2) 

[[2, 3, 4, 5], [2, 4, 5, 6], [2, 5, 6, 7], [2, 6, 7, 8]] 

 

Less rows when using ``mod_box_isometries=True``:: 

 

sage: a = Polyomino([(0,0,0), (0,0,1), (1,0,0)]) 

sage: b = Polyomino([(0,0,0), (1,0,0), (0,1,0)]) 

sage: T = TilingSolver([a,b], box=(2,1,3)) 

sage: T.rows_for_piece(0) 

[[0, 3, 5, 6], 

[0, 4, 6, 7], 

[0, 2, 3, 6], 

[0, 3, 4, 7], 

[0, 2, 3, 5], 

[0, 3, 4, 6], 

[0, 2, 5, 6], 

[0, 3, 6, 7]] 

sage: T.rows_for_piece(0, mod_box_isometries=True) 

[[0, 2, 3, 6], [0, 3, 4, 7]] 

sage: T.rows_for_piece(1, mod_box_isometries=True) 

[[1, 2, 3, 6], [1, 3, 4, 7]] 

""" 

p = self._pieces[i] 

if self._rotation: 

if self._reflection: 

orientation_preserving = False 

else: 

orientation_preserving = True 

it = p.isometric_copies(self._box, 

orientation_preserving=orientation_preserving, 

mod_box_isometries=mod_box_isometries) 

else: 

if self._reflection: 

raise NotImplementedError("Reflection allowed, Rotation not " 

"allowed is not implemented") 

else: 

it = p.translated_copies(self._box) 

coord_to_int = self.coord_to_int_dict() 

rows = [] 

for q in it: 

L = [] if self._reusable else [i] 

L.extend(coord_to_int[coord] for coord in q) 

rows.append(L) 

return rows 

 

@cached_method 

def rows(self): 

r""" 

Creation of the rows 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: p = Polyomino([(0,0,0)]) 

sage: q = Polyomino([(0,0,0), (0,0,1)]) 

sage: r = Polyomino([(0,0,0), (0,0,1), (0,0,2)]) 

sage: T = TilingSolver([p,q,r], box=(1,1,6)) 

sage: rows = T.rows() 

sage: for row in rows: row 

[0, 3] 

[0, 4] 

[0, 5] 

[0, 6] 

[0, 7] 

[0, 8] 

[1, 3, 4] 

[1, 4, 5] 

[1, 5, 6] 

[1, 6, 7] 

[1, 7, 8] 

[2, 3, 4, 5] 

[2, 4, 5, 6] 

[2, 5, 6, 7] 

[2, 6, 7, 8] 

""" 

rows = [] 

for i in range(len(self._pieces)): 

rows.extend(self.rows_for_piece(i)) 

return rows 

 

def _rows_mod_box_isometries(self, i): 

r""" 

Return a list of rows representing the solutions up to isometries of 

the box. 

 

The positions of the ``i``-th pieces are chosen up to isometries of 

the box. In dimension 3, there are four times less rows for that 

piece. 

 

It is currently implemented only when the pieces are not reusable. 

 

INPUT: 

 

- ``i`` - integer, the i-th piece to consider, that piece must not 

be isometric to itself by a isometry that preserve the box. 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: p = Polyomino([(0,0,0), (1,0,0), (1,1,0), (1,0,1), (2,0,1)], color='red') 

sage: T = TilingSolver([p], box=(3,4,2)) 

sage: T._rows_mod_box_isometries(0) 

[[0, 2, 3, 4, 5, 11], 

[0, 4, 5, 6, 7, 13], 

[0, 10, 11, 12, 13, 19], 

[0, 12, 13, 14, 15, 21], 

[0, 1, 9, 10, 12, 18], 

[0, 3, 11, 12, 14, 20], 

[0, 5, 13, 14, 16, 22], 

[0, 2, 9, 10, 12, 20], 

[0, 4, 11, 12, 14, 22], 

[0, 6, 13, 14, 16, 24], 

[0, 1, 3, 4, 11, 13], 

[0, 3, 5, 6, 13, 15], 

[0, 9, 11, 12, 19, 21], 

[0, 11, 13, 14, 21, 23]] 

 

We test that there are four times less rows for that polyomino:: 

 

sage: len(T.rows()) == 4 * len(T._rows_mod_box_isometries(0)) 

True 

 

Now, a real use case. A solution of the game Quantumino is a tiling 

of a `5 \times 8 \times 2` box. Since a `5 \times 8 \times 2` box 

has four orientation preserving isometries, each solution up to 

rotation is counted four times by this dancing links solver:: 

 

sage: from sage.games.quantumino import QuantuminoSolver 

sage: from sage.combinat.matrices.dancing_links import dlx_solver 

sage: q = QuantuminoSolver(0) 

sage: T = q.tiling_solver() 

sage: dlx_solver(T.rows()) # long time (10s) 

Dancing links solver for 96 columns and 5484 rows 

 

It is possible to avoid to compute 4 times each solution up to 

rotations. This is done by choosing a piece (here the 0-th) and 

considering 4 times less positions for that piece. To be precise, 

90 positions instead of 360, therefore the dancing links solver 

below has 270 less rows:: 

 

sage: dlx_solver(T._rows_mod_box_isometries(0)) # long time (10s) 

Dancing links solver for 96 columns and 5214 rows 

""" 

assert not self._reusable, ("this code assumes the pieces are not reusable") 

len_pieces = len(self._pieces) 

if not 0 <= i < len_pieces: 

raise ValueError("i(={}) must be 0 <= i < {}".format(i,len_pieces)) 

rows = [] 

for j in range(len_pieces): 

if j == i: 

rows.extend(self.rows_for_piece(j, mod_box_isometries=True)) 

else: 

rows.extend(self.rows_for_piece(j)) 

return rows 

 

def nrows_per_piece(self): 

r""" 

Return the number of rows necessary by each piece. 

 

OUTPUT: 

 

list 

 

EXAMPLES:: 

 

sage: from sage.games.quantumino import QuantuminoSolver 

sage: q = QuantuminoSolver(0) 

sage: T = q.tiling_solver() 

sage: T.nrows_per_piece() # long time (10s) 

[360, 360, 360, 360, 360, 180, 180, 672, 672, 360, 360, 180, 180, 360, 360, 180] 

""" 

return [len(self.rows_for_piece(i)) for i in range(len(self._pieces))] 

 

def starting_rows(self): 

r""" 

Return the starting rows for each piece. 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: p = Polyomino([(0,0,0)]) 

sage: q = Polyomino([(0,0,0), (0,0,1)]) 

sage: r = Polyomino([(0,0,0), (0,0,1), (0,0,2)]) 

sage: T = TilingSolver([p,q,r], box=(1,1,6)) 

sage: T.starting_rows() 

[0, 6, 11, 15] 

""" 

s = 0 

S = [s] 

for a in self.nrows_per_piece(): 

s += a 

S.append(s) 

return S 

 

def row_to_polyomino(self, row_number): 

r""" 

Return a polyomino associated to a row. 

 

INPUT: 

 

- ``row_number`` -- integer, the i-th row 

 

OUTPUT: 

 

polyomino 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: a = Polyomino([(0,0,0), (0,0,1), (1,0,0)], color='blue') 

sage: b = Polyomino([(0,0,0), (1,0,0), (0,1,0)], color='red') 

sage: T = TilingSolver([a,b], box=(2,1,3)) 

sage: len(T.rows()) 

16 

 

:: 

 

sage: T.row_to_polyomino(7) 

Polyomino: [(0, 0, 1), (1, 0, 1), (1, 0, 2)], Color: blue 

 

:: 

 

sage: T.row_to_polyomino(13) 

Polyomino: [(0, 0, 1), (0, 0, 2), (1, 0, 1)], Color: red 

""" 

row = self.rows()[row_number] 

if self._reusable: 

starting_rows = self.starting_rows() 

no = -1 

while starting_rows[no] < row_number: 

no += 1 

indices = row 

else: 

no = row[0] 

indices = row[1:] 

int_to_coord = self.int_to_coord_dict() 

coords = [int_to_coord[i] for i in indices] 

return Polyomino(coords, color=self._pieces[no].color()) 

 

def dlx_solver(self): 

r""" 

Return the sage DLX solver of that tiling problem. 

 

OUTPUT: 

 

DLX Solver 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: p = Polyomino([(0,0,0)]) 

sage: q = Polyomino([(0,0,0), (0,0,1)]) 

sage: r = Polyomino([(0,0,0), (0,0,1), (0,0,2)]) 

sage: T = TilingSolver([p,q,r], box=(1,1,6)) 

sage: T.dlx_solver() 

Dancing links solver for 9 columns and 15 rows 

""" 

from sage.combinat.matrices.dancing_links import dlx_solver 

return dlx_solver(self.rows()) 

 

def _dlx_solutions_iterator(self): 

r""" 

Return an iterator over the row indices of the solutions. 

 

OUTPUT: 

 

iterator 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: p = Polyomino([(0,0,0)]) 

sage: q = Polyomino([(0,0,0), (0,0,1)]) 

sage: r = Polyomino([(0,0,0), (0,0,1), (0,0,2)]) 

sage: T = TilingSolver([p,q,r], box=(1,1,6)) 

sage: list(T._dlx_solutions_iterator()) 

[[0, 7, 14], [0, 12, 10], [6, 13, 5], [6, 14, 2], [11, 9, 5], [11, 10, 3]] 

""" 

if len(self.rows()) == 0: 

return 

 

x = self.dlx_solver() 

while x.search() == 1: 

yield x.get_solution() 

 

def _dlx_common_prefix_solutions_iterator(self): 

r""" 

Return an iterator over the row indices of solutions and of partial 

solutions, i.e. the common prefix of two consecutive solutions. 

 

The purpose is to illustrate the backtracking and construct an 

animation of the evolution of solutions. 

 

OUTPUT: 

 

iterator 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: p = Polyomino([(0,0,0)]) 

sage: q = Polyomino([(0,0,0), (0,0,1)]) 

sage: r = Polyomino([(0,0,0), (0,0,1), (0,0,2)]) 

sage: T = TilingSolver([p,q,r], box=(1,1,6)) 

sage: list(T._dlx_solutions_iterator()) 

[[0, 7, 14], [0, 12, 10], [6, 13, 5], [6, 14, 2], [11, 9, 5], [11, 10, 3]] 

sage: list(T._dlx_common_prefix_solutions_iterator()) 

[[0, 7, 14], [0], [0, 12, 10], [], [6, 13, 5], [6], [6, 14, 2], [], [11, 9, 5], [11], [11, 10, 3]] 

 

:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: y = Polyomino([(0,0),(1,0),(2,0),(3,0),(2,1)], color='yellow') 

sage: T = TilingSolver([y], box=(5,10), reusable=True, reflection=True) 

sage: for a in T._dlx_common_prefix_solutions_iterator(): a 

[64, 83, 149, 44, 179, 62, 35, 162, 132, 101] 

[64, 83, 149, 44, 179] 

[64, 83, 149, 44, 179, 154, 35, 162, 132, 175] 

[64, 83, 149] 

[64, 83, 149, 97, 39, 162, 35, 62, 48, 106] 

[64] 

[64, 157, 149, 136, 179, 62, 35, 162, 132, 101] 

[64, 157, 149, 136, 179] 

[64, 157, 149, 136, 179, 154, 35, 162, 132, 175] 

[] 

[82, 119, 58, 97, 38, 87, 8, 63, 48, 107] 

[82, 119, 58, 97, 38] 

[82, 119, 58, 97, 38, 161, 8, 63, 140, 107] 

[82, 119] 

[82, 119, 150, 136, 180, 63, 8, 161, 131, 175] 

[82, 119, 150] 

[82, 119, 150, 171, 38, 87, 8, 63, 48, 107] 

[82, 119, 150, 171, 38] 

[82, 119, 150, 171, 38, 161, 8, 63, 140, 107] 

""" 

it = self._dlx_solutions_iterator() 

B = next(it) 

while True: 

yield B 

A, B = B, next(it) 

common_prefix = [] 

for a, b in zip(A, B): 

if a == b: 

common_prefix.append(a) 

else: 

break 

yield common_prefix 

 

def _dlx_incremental_solutions_iterator(self): 

r""" 

Return an iterator over the row indices of the incremental 

solutions. 

 

Between two incremental solution, either one piece is added or one 

piece is removed. 

 

The purpose is to illustrate the backtracking and construct an 

animation of the evolution of solutions. 

 

OUTPUT: 

 

iterator 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: p = Polyomino([(0,0,0)]) 

sage: q = Polyomino([(0,0,0), (0,0,1)]) 

sage: r = Polyomino([(0,0,0), (0,0,1), (0,0,2)]) 

sage: T = TilingSolver([p,q,r], box=(1,1,6)) 

sage: list(T._dlx_solutions_iterator()) 

[[0, 7, 14], [0, 12, 10], [6, 13, 5], [6, 14, 2], [11, 9, 5], [11, 10, 3]] 

sage: list(T._dlx_incremental_solutions_iterator()) 

[[0, 7, 14], [0, 7], [0], [0, 12], [0, 12, 10], [0, 12], [0], [], [6], [6, 13], [6, 13, 5], [6, 13], [6], [6, 14], [6, 14, 2], [6, 14], [6], [], [11], [11, 9], [11, 9, 5], [11, 9], [11], [11, 10], [11, 10, 3]] 

 

:: 

 

sage: y = Polyomino([(0,0),(1,0),(2,0),(3,0),(2,1)], color='yellow') 

sage: T = TilingSolver([y], box=(5,10), reusable=True, reflection=True) 

sage: for a in T._dlx_solutions_iterator(): a 

[64, 83, 149, 44, 179, 62, 35, 162, 132, 101] 

[64, 83, 149, 44, 179, 154, 35, 162, 132, 175] 

[64, 83, 149, 97, 39, 162, 35, 62, 48, 106] 

[64, 157, 149, 136, 179, 62, 35, 162, 132, 101] 

[64, 157, 149, 136, 179, 154, 35, 162, 132, 175] 

[82, 119, 58, 97, 38, 87, 8, 63, 48, 107] 

[82, 119, 58, 97, 38, 161, 8, 63, 140, 107] 

[82, 119, 150, 136, 180, 63, 8, 161, 131, 175] 

[82, 119, 150, 171, 38, 87, 8, 63, 48, 107] 

[82, 119, 150, 171, 38, 161, 8, 63, 140, 107] 

sage: len(list(T._dlx_incremental_solutions_iterator())) 

123 

""" 

it = self._dlx_solutions_iterator() 

B = next(it) 

while True: 

yield B 

A, B = B, next(it) 

common_prefix = 0 

for a, b in zip(A, B): 

if a == b: 

common_prefix += 1 

else: 

break 

for i in range(1, len(A)-common_prefix): 

yield A[:-i] 

for j in range(common_prefix, len(B)): 

yield B[:j] 

 

def solve(self, partial=None): 

r""" 

Returns an iterator of list of polyominoes that are an exact cover 

of the box. 

 

INPUT: 

 

- ``partial`` - string (optional, default: ``None``), whether to 

include partial (incomplete) solutions. It can be one of the 

following: 

 

- ``None`` - include only complete solution 

- ``'common_prefix'`` - common prefix between two consecutive solutions 

- ``'incremental'`` - one piece change at a time 

 

OUTPUT: 

 

iterator of list of polyominoes 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: p = Polyomino([(0,0,0)]) 

sage: q = Polyomino([(0,0,0), (0,0,1)]) 

sage: r = Polyomino([(0,0,0), (0,0,1), (0,0,2)]) 

sage: T = TilingSolver([p,q,r], box=(1,1,6)) 

sage: it = T.solve() 

sage: for p in next(it): p 

Polyomino: [(0, 0, 0)], Color: gray 

Polyomino: [(0, 0, 1), (0, 0, 2)], Color: gray 

Polyomino: [(0, 0, 3), (0, 0, 4), (0, 0, 5)], Color: gray 

sage: for p in next(it): p 

Polyomino: [(0, 0, 0)], Color: gray 

Polyomino: [(0, 0, 1), (0, 0, 2), (0, 0, 3)], Color: gray 

Polyomino: [(0, 0, 4), (0, 0, 5)], Color: gray 

sage: for p in next(it): p 

Polyomino: [(0, 0, 0), (0, 0, 1)], Color: gray 

Polyomino: [(0, 0, 2), (0, 0, 3), (0, 0, 4)], Color: gray 

Polyomino: [(0, 0, 5)], Color: gray 

 

Including the partial solutions:: 

 

sage: it = T.solve(partial='common_prefix') 

sage: for p in next(it): p 

Polyomino: [(0, 0, 0)], Color: gray 

Polyomino: [(0, 0, 1), (0, 0, 2)], Color: gray 

Polyomino: [(0, 0, 3), (0, 0, 4), (0, 0, 5)], Color: gray 

sage: for p in next(it): p 

Polyomino: [(0, 0, 0)], Color: gray 

sage: for p in next(it): p 

Polyomino: [(0, 0, 0)], Color: gray 

Polyomino: [(0, 0, 1), (0, 0, 2), (0, 0, 3)], Color: gray 

Polyomino: [(0, 0, 4), (0, 0, 5)], Color: gray 

sage: for p in next(it): p 

sage: for p in next(it): p 

Polyomino: [(0, 0, 0), (0, 0, 1)], Color: gray 

Polyomino: [(0, 0, 2), (0, 0, 3), (0, 0, 4)], Color: gray 

Polyomino: [(0, 0, 5)], Color: gray 

 

Colors are preserved when the polyomino can be reused:: 

 

sage: p = Polyomino([(0,0,0)], color='yellow') 

sage: q = Polyomino([(0,0,0), (0,0,1)], color='yellow') 

sage: r = Polyomino([(0,0,0), (0,0,1), (0,0,2)], color='yellow') 

sage: T = TilingSolver([p,q,r], box=(1,1,6), reusable=True) 

sage: it = T.solve() 

sage: for p in next(it): p 

Polyomino: [(0, 0, 0)], Color: yellow 

Polyomino: [(0, 0, 1)], Color: yellow 

Polyomino: [(0, 0, 2)], Color: yellow 

Polyomino: [(0, 0, 3)], Color: yellow 

Polyomino: [(0, 0, 4)], Color: yellow 

Polyomino: [(0, 0, 5)], Color: yellow 

 

TESTS:: 

 

sage: T = TilingSolver([p,q,r], box=(1,1,7)) 

sage: next(T.solve()) 

Traceback (most recent call last): 

... 

StopIteration 

 

""" 

if not self.is_suitable(): 

return 

if partial is None: 

it = self._dlx_solutions_iterator() 

elif partial == 'common_prefix': 

it = self._dlx_common_prefix_solutions_iterator() 

elif partial == 'incremental': 

it = self._dlx_incremental_solutions_iterator() 

else: 

raise ValueError("Unknown value for partial (=%s)" % partial) 

for solution in it: 

yield map(self.row_to_polyomino, solution) 

 

def number_of_solutions(self): 

r""" 

Return the number of distinct solutions. 

 

OUTPUT: 

 

integer 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import TilingSolver, Polyomino 

sage: p = Polyomino([(0,0)]) 

sage: q = Polyomino([(0,0), (0,1)]) 

sage: r = Polyomino([(0,0), (0,1), (0,2)]) 

sage: T = TilingSolver([p,q,r], box=(1,6)) 

sage: T.number_of_solutions() 

6 

 

:: 

 

sage: T = TilingSolver([p,q,r], box=(1,7)) 

sage: T.number_of_solutions() 

0 

""" 

if not self.is_suitable(): 

return 0 

x = self.dlx_solver() 

N = 0 

while x.search() == 1: 

N += 1 

return N 

 

def animate(self, partial=None, stop=None, size=0.75, axes=False): 

r""" 

Return an animation of evolving solutions. 

 

INPUT: 

 

- ``partial`` - string (optional, default: ``None``), whether to 

include partial (incomplete) solutions. It can be one of the 

following: 

 

- ``None`` - include only complete solutions 

- ``'common_prefix'`` - common prefix between two consecutive solutions 

- ``'incremental'`` - one piece change at a time 

 

- ``stop`` - integer (optional, default:``None``), number of frames 

 

- ``size`` - number (optional, default: ``0.75``), the size of each 

``1 \times 1`` square. This does a homothety with respect 

to the center of each polyomino. 

 

- ``axes`` - bool (optional, default:``False``), whether the x and 

y axes are shown. 

 

EXAMPLES:: 

 

sage: from sage.combinat.tiling import Polyomino, TilingSolver 

sage: y = Polyomino([(0,0),(1,0),(2,0),(3,0),(2,1)], color='cyan') 

sage: T = TilingSolver([y], box=(5,10), reusable=True, reflection=True) 

sage: a = T.animate() 

sage: a # optional -- ImageMagick 

Animation with 10 frames 

 

Include partial solutions (common prefix between two consecutive 

solutions):: 

 

sage: a = T.animate('common_prefix') 

sage: a # optional -- ImageMagick 

Animation with 19 frames 

 

Incremental solutions (one piece removed or added at a time):: 

 

sage: a = T.animate('incremental') # long time (2s) 

sage: a # long time (2s) # optional -- ImageMagick 

Animation with 123 frames 

 

:: 

 

sage: a.show() # optional -- ImageMagick 

 

The ``show`` function takes arguments to specify the delay between 

frames (measured in hundredths of a second, default value 20) and 

the number of iterations (default value 0, which means to iterate 

forever). To iterate 4 times with half a second between each frame:: 

 

sage: a.show(delay=50, iterations=4) # optional -- ImageMagick 

 

Limit the number of frames:: 

 

sage: a = T.animate('incremental', stop=13) # not tested 

sage: a # not tested 

Animation with 13 frames 

""" 

dimension = self._box._dimension 

if dimension == 2: 

from sage.plot.graphics import Graphics 

from sage.plot.animate import Animation 

it = self.solve(partial=partial) 

it = itertools.islice(it, stop) 

L = [sum([piece.show2d(size) 

for piece in solution], Graphics()) for solution in it] 

(xmin,ymin), (xmax,ymax) = self._box.bounding_box() 

xmax = xmax+0.5 

ymax = ymax+0.5 

a = Animation(L, xmin=xmin-0.5, ymin=ymin-0.5, 

xmax=xmax, ymax=ymax, aspect_ratio=1, axes=axes) 

return a 

elif dimension == 3: 

raise NotImplementedError("3d Animation must be implemented " 

"in Jmol first") 

else: 

raise NotImplementedError("Dimension must be 2 or 3 in order " 

"to make an animation")