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

r""" 

Fully packed loops 

 

AUTHORS: 

 

- Vincent Knight, James Campbell, Kevin Dilks, Emily Gunawan (2015): Initial version 

- Vincent Delecroix (2017): cleaning and enhanced plotting function 

""" 

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

# Copyright (C) 2015 Vincent Knight <vincent.knight@gmail.com> 

# James Campbell <james.campbell@tanti.org.uk> 

# Kevin Dilks <kdilks@gmail.com> 

# Emily Gunawan <egunawan@umn.edu> 

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

# 

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

# 

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

# WITHOUT ANY WARRANTY; without even the implied warranty of 

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

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

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

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

# python3 

from __future__ import division, print_function 

from six import add_metaclass 

 

from sage.misc.inherit_comparison import InheritComparisonClasscallMetaclass 

from sage.structure.unique_representation import UniqueRepresentation 

from sage.structure.parent import Parent 

from sage.structure.element import parent, Element 

 

from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets 

from sage.combinat.six_vertex_model import (SquareIceModel, 

SixVertexConfiguration, 

SixVertexModel) 

from sage.combinat.alternating_sign_matrix import AlternatingSignMatrix 

 

from sage.misc.decorators import options 

from sage.matrix.constructor import matrix 

from sage.arith.all import factorial 

from sage.rings.integer import Integer 

from sage.misc.all import prod 

 

# edges of a fpl in terms of the six vertex possible configurations 

R = (1, 0) 

L = (-1, 0) 

U = (0, 1) 

D = (0, -1) 

 

FPL_edges = ( 

# 0 UD 1 RD, 2 UR, 3 LR, 4 LD 5 LU 

((D,U), (L,D), (D,R), (R,L), (L,U), (R,U)), # even 

((R,L), (R,U), (L,U), (D,U), (D,R), (L,D)) # odd 

) 

 

FPL_turns = ( 

# 0 UD 1 RD 2 UR 3 LR 4 LD 5 LU 

({U: U, D: D}, {R: D, U: L}, {U: R, L: D}, {L: L, R: R}, {R: U, D: L}, {L: U, D: R}), # even 

({L: L, R: R}, {L: U, D: R}, {R: U, D: L}, {U: U, D: D}, {U: R, L: D}, {R: D, U: L}) # odd 

) 

 

def _make_color_list(n, colors=None, color_map=None, randomize=False): 

r""" 

TESTS:: 

 

sage: from sage.combinat.fully_packed_loop import _make_color_list 

sage: _make_color_list(5) 

sage: _make_color_list(5, ['blue', 'red']) 

['blue', 'red', 'blue', 'red', 'blue'] 

sage: _make_color_list(5, color_map='summer') 

[(0.0, 0.5, 0.40000000000000002), 

(0.25098039215686274, 0.62549019607843137, 0.40000000000000002), 

(0.50196078431372548, 0.75098039215686274, 0.40000000000000002), 

(0.75294117647058822, 0.87647058823529411, 0.40000000000000002), 

(1.0, 1.0, 0.40000000000000002)] 

sage: _make_color_list(8, ['blue', 'red'], randomize=True) 

['blue', 'blue', 'red', 'blue', 'red', 'red', 'red', 'blue'] 

""" 

if colors: 

dim = len(colors) 

colors = [colors[i % dim] for i in range(n)] 

 

elif color_map: 

from matplotlib import cm 

if not color_map in cm.datad: 

raise ValueError('unknown color map %s' % color_map) 

cmap = cm.__dict__[color_map] 

colors = [cmap(i/float(n-1))[:3] for i in range(n)] 

 

if colors and randomize: 

from sage.misc.prandom import shuffle 

shuffle(colors) 

 

return colors 

 

@add_metaclass(InheritComparisonClasscallMetaclass) 

class FullyPackedLoop(Element): 

r""" 

A class for fully packed loops. 

 

A fully packed loop is a collection of non-intersecting lattice paths on a square 

grid such that every vertex is part of some path, and the paths are either closed 

internal loops or have endpoints corresponding to alternate points on the 

boundary [Propp2001]_. They are known to be in bijection with alternating sign 

matrices. 

 

.. SEEALSO:: 

 

:class:`AlternatingSignMatrix` 

 

To each fully packed loop, we assign a link pattern, which is the non-crossing 

matching attained by seeing which points on the boundary are connected 

by open paths in the fully packed loop. 

 

We can create a fully packed loop using the corresponding alternating sign 

matrix and also extract the link pattern:: 

 

sage: A = AlternatingSignMatrix([[0, 0, 1], [0, 1, 0], [1, 0, 0]]) 

sage: fpl = FullyPackedLoop(A) 

sage: fpl.link_pattern() 

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

sage: fpl 

| | 

| | 

+ -- + + 

| | 

| | 

-- + + + -- 

| | 

| | 

+ + -- + 

| | 

| | 

sage: B = AlternatingSignMatrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) 

sage: fplb = FullyPackedLoop(B) 

sage: fplb.link_pattern() 

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

sage: fplb 

| | 

| | 

+ + -- + 

| | 

| | 

-- + + + -- 

| | 

| | 

+ -- + + 

| | 

| | 

 

The class also has a plot method:: 

 

sage: fpl.plot() 

Graphics object consisting of 3 graphics primitives 

 

which gives: 

 

.. PLOT:: 

:width: 200 px 

 

A = AlternatingSignMatrix([[0, 0, 1], [0, 1, 0], [1, 0, 0]]) 

fpl = FullyPackedLoop(A) 

p = fpl.plot() 

sphinx_plot(p) 

 

Note that we can also create a fully packed loop from a six vertex model configuration:: 

 

sage: S = SixVertexModel(3, boundary_conditions='ice').from_alternating_sign_matrix(A) 

sage: S 

^ ^ ^ 

| | | 

--> # -> # -> # <-- 

^ ^ | 

| | V 

--> # -> # <- # <-- 

^ | | 

| V V 

--> # <- # <- # <-- 

| | | 

V V V 

sage: fpl = FullyPackedLoop(S) 

sage: fpl 

| | 

| | 

+ -- + + 

| | 

| | 

-- + + + -- 

| | 

| | 

+ + -- + 

| | 

| | 

 

Once we have a fully packed loop we can obtain the corresponding alternating sign matrix:: 

 

sage: fpl.to_alternating_sign_matrix() 

[0 0 1] 

[0 1 0] 

[1 0 0] 

 

Here are some more examples using bigger ASMs:: 

 

sage: A = AlternatingSignMatrix([[0,1,0,0],[0,0,1,0],[1,-1,0,1],[0,1,0,0]]) 

sage: S = SixVertexModel(4, boundary_conditions='ice').from_alternating_sign_matrix(A) 

sage: fpl = FullyPackedLoop(S) 

sage: fpl.link_pattern() 

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

sage: fpl 

| | 

| | 

+ -- + -- + + -- 

| 

| 

-- + + -- + -- + 

| | 

| | 

+ + + -- + -- 

| | | 

| | | 

-- + + + -- + 

| | 

| | 

 

sage: m = AlternatingSignMatrix([[0,0,1,0,0,0], 

....: [1,0,-1,0,1,0], 

....: [0,0,0,1,0,0], 

....: [0,1,0,0,-1,1], 

....: [0,0,0,0,1,0], 

....: [0,0,1,0,0,0]]) 

sage: fpl = FullyPackedLoop(m) 

sage: fpl.link_pattern() 

[(1, 12), (2, 7), (3, 4), (5, 6), (8, 9), (10, 11)] 

sage: fpl 

| | | 

| | | 

+ -- + + + -- + + -- 

| | | | 

| | | | 

-- + -- + + + -- + -- + 

| 

| 

+ -- + + -- + -- + + -- 

| | | | 

| | | | 

-- + + + -- + + + 

| | | | | 

| | | | | 

+ -- + + -- + + + -- 

| | 

| | 

-- + + -- + -- + + -- + 

| | | 

| | | 

 

sage: m = AlternatingSignMatrix([[0,1,0,0,0,0,0], 

....: [1,-1,0,0,1,0,0], 

....: [0,0,0,1,0,0,0], 

....: [0,1,0,0,-1,1,0], 

....: [0,0,0,0,1,0,0], 

....: [0,0,1,0,-1,0,1], 

....: [0,0,0,0,1,0,0]]) 

sage: fpl = FullyPackedLoop(m) 

sage: fpl.link_pattern() 

[(1, 2), (3, 4), (5, 6), (7, 8), (9, 14), (10, 11), (12, 13)] 

sage: fpl 

| | | | 

| | | | 

+ -- + -- + + -- + + -- + 

| | 

| | 

-- + -- + -- + + -- + -- + + -- 

| | 

| | 

+ -- + + -- + -- + + -- + 

| | | | 

| | | | 

-- + + + -- + + + + -- 

| | | | | | 

| | | | | | 

+ -- + + -- + + + -- + 

| | 

| | 

-- + + -- + -- + + + -- + -- 

| | | | 

| | | | 

+ -- + + -- + + + -- + 

| | | | 

| | | | 

 

Gyration on an alternating sign matrix/fully packed loop ``fpl`` 

of the link pattern corresponding to ``fpl``:: 

 

sage: ASMs = AlternatingSignMatrices(3).list() 

sage: ncp = FullyPackedLoop(ASMs[1]).link_pattern() # fpl's gyration orbit size is 2 

sage: rotated_ncp=[] 

sage: for (a,b) in ncp: 

....: for i in range(0,5): 

....: a,b=a%6+1,b%6+1; 

....: rotated_ncp.append((a,b)) 

sage: PerfectMatching(ASMs[1].gyration().to_fully_packed_loop().link_pattern()) ==\ 

PerfectMatching(rotated_ncp) 

True 

 

sage: fpl = FullyPackedLoop(ASMs[0]) 

sage: ncp = fpl.link_pattern() # fpl's gyration size is 3 

sage: rotated_ncp=[] 

sage: for (a,b) in ncp: 

....: for i in range(0,5): 

....: a,b=a%6+1,b%6+1; 

....: rotated_ncp.append((a,b)) 

sage: PerfectMatching(ASMs[0].gyration().to_fully_packed_loop().link_pattern()) ==\ 

PerfectMatching(rotated_ncp) 

True 

 

sage: mat = AlternatingSignMatrix([[0,0,1,0,0,0,0],[1,0,-1,0,1,0,0],\ 

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

sage: fpl = FullyPackedLoop(mat) # n=7 

sage: ncp = fpl.link_pattern() 

sage: rotated_ncp=[] 

sage: for (a,b) in ncp: 

....: for i in range(0,13): 

....: a,b=a%14+1,b%14+1; 

....: rotated_ncp.append((a,b)) 

sage: PerfectMatching(mat.gyration().to_fully_packed_loop().link_pattern()) ==\ 

PerfectMatching(rotated_ncp) 

True 

 

sage: mat = AlternatingSignMatrix([[0,0,0,1,0,0], [0,0,1,-1,1,0],\ 

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

sage: fpl = FullyPackedLoop(mat) # n =6 

sage: ncp = fpl.link_pattern() 

sage: rotated_ncp=[] 

sage: for (a,b) in ncp: 

....: for i in range(0,11): 

....: a,b=a%12+1,b%12+1; 

....: rotated_ncp.append((a,b)) 

sage: PerfectMatching(mat.gyration().to_fully_packed_loop().link_pattern()) ==\ 

PerfectMatching(rotated_ncp) 

True 

 

More examples:: 

 

We can initiate a fully packed loop using an alternating sign matrix:: 

 

sage: A = AlternatingSignMatrix([[0, 0, 1], [0, 1, 0], [1, 0, 0]]) 

sage: fpl = FullyPackedLoop(A) 

sage: fpl 

| | 

| | 

+ -- + + 

| | 

| | 

-- + + + -- 

| | 

| | 

+ + -- + 

| | 

| | 

sage: FullyPackedLoops(3)(A) == fpl 

True 

 

We can also input a matrix:: 

 

sage: FullyPackedLoop([[0, 0, 1], [0, 1, 0], [1, 0, 0]]) 

| | 

| | 

+ -- + + 

| | 

| | 

-- + + + -- 

| | 

| | 

+ + -- + 

| | 

| | 

sage: FullyPackedLoop([[0, 0, 1], [0, 1, 0], [1, 0, 0]]) ==\ 

....: FullyPackedLoops(3)([[0, 0, 1], [0, 1, 0], [1, 0, 0]]) 

True 

 

Otherwise we initiate a fully packed loop using a six vertex model:: 

 

sage: S = SixVertexModel(3, boundary_conditions='ice').from_alternating_sign_matrix(A) 

sage: fpl = FullyPackedLoop(S) 

sage: fpl 

| | 

| | 

+ -- + + 

| | 

| | 

-- + + + -- 

| | 

| | 

+ + -- + 

| | 

| | 

 

sage: FullyPackedLoops(3)(S) == FullyPackedLoop(S) 

True 

 

sage: fpl.six_vertex_model().to_alternating_sign_matrix() 

[0 0 1] 

[0 1 0] 

[1 0 0] 

 

We can also input the matrix associated to a six vertex model:: 

 

sage: SixVertexModel(2)([[3,1],[5,3]]) 

^ ^ 

| | 

--> # <- # <-- 

| ^ 

V | 

--> # -> # <-- 

| | 

V V 

 

sage: FullyPackedLoop([[3,1],[5,3]]) 

| 

| 

+ + -- 

| | 

| | 

-- + + 

| 

| 

 

sage: FullyPackedLoops(2)([[3,1],[5,3]]) == FullyPackedLoop([[3,1],[5,3]]) 

True 

 

Note that the matrix corresponding to a six vertex model without 

the ice boundary condition is not allowed:: 

 

sage: SixVertexModel(2)([[3,1],[5,5]]) 

^ ^ 

| | 

--> # <- # <-- 

| ^ 

V V 

--> # -> # --> 

| | 

V V 

 

sage: FullyPackedLoop([[3,1],[5,5]]) 

Traceback (most recent call last): 

... 

ValueError: invalid alternating sign matrix 

 

sage: FullyPackedLoops(2)([[3,1],[5,5]]) 

Traceback (most recent call last): 

... 

ValueError: invalid alternating sign matrix 

 

Note that if anything else is used to generate the fully packed loop an error will occur:: 

 

sage: fpl = FullyPackedLoop(5) 

Traceback (most recent call last): 

... 

ValueError: invalid alternating sign matrix 

 

sage: fpl = FullyPackedLoop((1, 2, 3)) 

Traceback (most recent call last): 

... 

ValueError: The alternating sign matrices must be square 

 

sage: SVM = SixVertexModel(3)[0] 

sage: FullyPackedLoop(SVM) 

Traceback (most recent call last): 

... 

ValueError: invalid alternating sign matrix 

 

REFERENCES: 

 

.. [Propp2001] James Propp. 

*The Many Faces of Alternating Sign Matrices*, 

Discrete Mathematics and Theoretical Computer Science 43 (2001): 58 

:arxiv:`math/0208125` 

 

.. [Striker2015] Jessica Striker. 

*The toggle group, homomesy, and the Razumov-Stroganov correspondence*, 

Electron. J. Combin. 22 (2015) no. 2 

:arxiv:`1503.08898` 

""" 

@staticmethod 

def __classcall_private__(cls, generator): 

""" 

Create a FPL. 

 

EXAMPLES:: 

 

sage: A = AlternatingSignMatrix([[1, 0, 0],[0, 1, 0],[0, 0, 1]]) 

sage: FullyPackedLoop(A) 

| | 

| | 

+ + -- + 

| | 

| | 

-- + + + -- 

| | 

| | 

+ -- + + 

| | 

| | 

 

sage: SVM = SixVertexModel(4, boundary_conditions='ice')[0] 

sage: FullyPackedLoop(SVM) 

| | 

| | 

+ + -- + + -- 

| | | 

| | | 

-- + + + -- + 

| | 

| | 

+ -- + + + -- 

| | | 

| | | 

-- + + -- + + 

| | 

| | 

""" 

if isinstance(generator, AlternatingSignMatrix): 

SVM = generator.to_six_vertex_model() 

elif isinstance(generator, SquareIceModel.Element): 

SVM = generator 

elif isinstance(generator, SixVertexConfiguration): 

# Check that this is an ice square model 

generator = SixVertexModel(generator.parent()._nrows, \ 

boundary_conditions='ice')(generator) 

M = generator.to_alternating_sign_matrix().to_matrix() 

M = AlternatingSignMatrix(M) 

SVM = generator 

else: # Not ASM nor SVM 

try: 

SVM = AlternatingSignMatrix(generator).to_six_vertex_model() 

except (TypeError, ValueError): 

generator = matrix(generator) 

generator = SixVertexModel(generator.nrows(), boundary_conditions='ice')(generator) 

# Check that this is an ice square model 

M = generator.to_alternating_sign_matrix() 

SVM = generator 

 

if not SVM: 

raise TypeError('generator for FullyPackedLoop must either be an \ 

AlternatingSignMatrix or a SquareIceModel.Element') 

FPLs = FullyPackedLoops(len(SVM)) 

return FPLs(generator) 

 

def __init__(self, parent, generator): 

""" 

Initialise object, can take ASM of FPL as generator. 

 

TESTS:: 

 

sage: A = AlternatingSignMatrix([[0, 0, 1], [0, 1, 0], [1, 0, 0]]) 

sage: fpl = FullyPackedLoop(A) 

sage: TestSuite(fpl).run() 

 

""" 

if isinstance(generator, AlternatingSignMatrix): 

self._six_vertex_model = generator.to_six_vertex_model() 

elif isinstance(generator, SquareIceModel.Element): 

self._six_vertex_model = generator 

 

Element.__init__(self, parent) 

 

def _repr_(self): 

""" 

Return a string representation of ``self``. 

 

EXAMPLES:: 

 

sage: A = AlternatingSignMatrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) 

sage: fpl = FullyPackedLoop(A) 

sage: fpl 

| | 

| | 

+ + -- + 

| | 

| | 

-- + + + -- 

| | 

| | 

+ -- + + 

| | 

| | 

 

sage: A = AlternatingSignMatrix([[0,1,0,0],[0,0,1,0],[1,-1,0,1],[0,1,0,0]]) 

sage: S = SixVertexModel(4, boundary_conditions='ice').from_alternating_sign_matrix(A) 

sage: fpl = FullyPackedLoop(S) 

sage: fpl 

| | 

| | 

+ -- + -- + + -- 

| 

| 

-- + + -- + -- + 

| | 

| | 

+ + + -- + -- 

| | | 

| | | 

-- + + + -- + 

| | 

| | 

 

""" 

# List are in the order of URDL 

# One set of rules for how to draw around even vertex, one set of rules for odd vertex 

n = len(self._six_vertex_model) - 1 

ascii1 = [[r' ', ' -', r' ', '- '], # LR 

[r' | ', ' ', r' ', '- '], # LU 

[r' ', ' ', r' | ', '- '], # LD 

[r' | ', ' ', r' | ', ' '], # UD 

[r' | ', ' -', r' ', ' '], # UR 

[r' ', ' -', r' | ', ' ']] # RD 

 

ascii2 = [[r' | ', ' ', r' | ', ' '], # LR 

[r' ', ' -', r' | ', ' '], # LU 

[r' | ', ' -', r' ', ' '], # LD 

[r' ', ' -', r' ', '- '], # UD 

[r' ', ' ', r' | ', '- '], # UR 

[r' | ', ' ', r' ', '- ']] # RD 

ret = ' ' 

# Do the top line 

for i,entry in enumerate(self._six_vertex_model[0]): 

if i % 2 == 0: 

ret += ' | ' 

else: 

ret += ' ' 

 

plus_sign = '+' 

 

# Do the meat of the ascii art 

for j,row in enumerate(self._six_vertex_model): 

ret += '\n ' 

# Do the top row 

for i,entry in enumerate(row): 

if (i+j) % 2 == 0: 

ret += ascii1[entry][0] 

else: 

ret += ascii2[entry][0] 

ret += '\n' 

 

# Do the left-most entry 

if (j) % 2 == 0: 

ret += ' ' 

else: 

ret += ' -' 

 

# Do the middle row 

for i,entry in enumerate(row): 

if (i+j) % 2 == 0: 

ret += ascii1[entry][3] + plus_sign + ascii1[entry][1] 

else: 

ret += ascii2[entry][3] + plus_sign + ascii2[entry][1] 

 

# Do the right-most entry 

if (j+n) % 2 ==0: 

ret += ' ' 

else: 

ret += '- ' 

 

# Do the bottom row 

ret += '\n ' 

for i,entry in enumerate(row): 

if (i+j) % 2 ==0: 

ret += ascii1[entry][2] 

else: 

ret += ascii2[entry][2] 

 

# Do the bottom line 

ret += '\n ' 

for i,entry in enumerate(self._six_vertex_model[-1]): 

if (i+n+1) % 2 ==0: 

ret += ' ' 

else: 

ret += ' | ' 

 

return ret 

 

def _richcmp_(self, other, op): 

""" 

Check equality or inequality. 

 

EXAMPLES:: 

 

sage: A = AlternatingSignMatrices(3) 

sage: M = A.random_element() 

sage: FullyPackedLoop(M) == M.to_fully_packed_loop() 

True 

 

sage: FullyPackedLoop(A([[1, 0, 0],[0, 1, 0],[0, 0, 1]])) ==\ 

FullyPackedLoop(A([[1, 0, 0],[0, 0, 1],[0, 1, 0]])) 

False 

 

sage: FullyPackedLoop(M) == M 

False 

 

sage: M = A.random_element() 

sage: FullyPackedLoop(M) != M.to_fully_packed_loop() 

False 

 

sage: f0 = FullyPackedLoop(A([[1, 0, 0],[0, 1, 0],[0, 0, 1]])) 

sage: f1 = FullyPackedLoop(A([[1, 0, 0],[0, 0, 1],[0, 1, 0]])) 

sage: f0 != f1 

True 

""" 

return self._six_vertex_model._richcmp_(other._six_vertex_model, op) 

 

def to_alternating_sign_matrix(self): 

""" 

Return the alternating sign matrix corresponding to this class. 

 

.. SEEALSO:: 

 

:class:`AlternatingSignMatrix` 

 

EXAMPLES:: 

 

sage: A = AlternatingSignMatrix([[0, 1, 0], [1, -1, 1], [0, 1, 0]]) 

sage: S = SixVertexModel(3, boundary_conditions='ice').from_alternating_sign_matrix(A) 

sage: fpl = FullyPackedLoop(S) 

sage: fpl.to_alternating_sign_matrix() 

[ 0 1 0] 

[ 1 -1 1] 

[ 0 1 0] 

sage: A = AlternatingSignMatrix([[0,1,0,0],[0,0,1,0],[1,-1,0,1],[0,1,0,0]]) 

sage: S = SixVertexModel(4, boundary_conditions='ice').from_alternating_sign_matrix(A) 

sage: fpl = FullyPackedLoop(S) 

sage: fpl.to_alternating_sign_matrix() 

[ 0 1 0 0] 

[ 0 0 1 0] 

[ 1 -1 0 1] 

[ 0 1 0 0] 

""" 

return self._six_vertex_model.to_alternating_sign_matrix() 

 

 

@options(link=True, loop=True, loop_fill=False) 

def plot(self, **options): 

r""" 

Return a graphical object of the Fully Packed Loop. 

 

Each option can be specified separately for links (the curves that join 

boundary points) and the loops. In order to do so, you need to prefix 

its name with either ``'link_'`` or ``'loop_'``. As an example, setting 

``color='red'`` will color both links and loops in red while setting 

``link_color='red'`` will only apply the color option for the links. 

 

INPUT: 

 

- ``link``, ``loop`` - (boolean, default ``True``) whether to plot the links 

or the loops 

 

- ``color``, ``link_color``, ``loop_color`` - (optional, a string or a 

RGB triple) 

 

- ``colors``, ``link_colors``, ``loop_colors`` - (optional, list) a list of 

colors 

 

- ``color_map``, ``link_color_map``, ``loop_color_map`` - (string, 

optional) a name of a matplotlib color map for the link or the loop 

 

- ``link_color_randomize`` - (boolean, default ``False``) when 

``link_colors`` or ``link_color_map`` is specified it randomizes 

its order. Setting this option to ``True`` makes it unlikely to 

have two neighboring links with the same color. 

 

- ``loop_fill`` - (boolean, optional) whether to fill the interior of the loops 

 

EXAMPLES: 

 

To plot the fully packed loop associated to the following alternating sign 

matrix 

 

.. MATH:: 

 

\begin{pmatrix} 0&1&1 \\ 1&-1&1 \\ 0&1&0 \end{pmatrix} 

 

simply do:: 

 

sage: A = AlternatingSignMatrix([[0, 1, 0], [1, -1, 1], [0, 1, 0]]) 

sage: fpl = FullyPackedLoop(A) 

sage: fpl.plot() 

Graphics object consisting of 3 graphics primitives 

 

The resulting graphics is as follows 

 

.. PLOT:: 

:width: 200 px 

 

A = AlternatingSignMatrix([[0, 1, 0], [1, -1, 1], [0, 1, 0]]) 

fpl = FullyPackedLoop(A) 

p = fpl.plot() 

sphinx_plot(p) 

 

You can also have the three links in different colors with:: 

 

sage: A = AlternatingSignMatrix([[0, 1, 0], [1, -1, 1], [0, 1, 0]]) 

sage: fpl = FullyPackedLoop(A) 

sage: fpl.plot(link_color_map='rainbow') 

Graphics object consisting of 3 graphics primitives 

 

.. PLOT:: 

:width: 200 px 

 

A = AlternatingSignMatrix([[0, 1, 0], [1, -1, 1], [0, 1, 0]]) 

fpl = FullyPackedLoop(A) 

p = fpl.plot(link_color_map='rainbow') 

sphinx_plot(p) 

 

You can plot the 42 fully packed loops of size `4 \times 4` using:: 

 

sage: G = [fpl.plot(link_color_map='winter', loop_color='black') for fpl in FullyPackedLoops(4)] 

sage: graphics_array(G, 7, 6) 

Graphics Array of size 7 x 6 

 

.. PLOT:: 

:width: 600 px 

 

G = [fpl.plot(link_color_map='winter', loop_color='black') for fpl in FullyPackedLoops(4)] 

p = graphics_array(G, 7, 6) 

sphinx_plot(p) 

 

Here is an example of a `20 \times 20` fully packed loop:: 

 

sage: s = "00000000000+0000000000000000+00-0+00000000000+00-00+0-+00000\ 

....: 0000+-00+00-+00000000+00-0000+0000-+00000000+000-0+0-+0-+000\ 

....: 000+-000+-00+0000000+-+-000+00-+0-000+000+-000+-0+0000000-0+\ 

....: 0000+0-+0-+00000-+00000+-+0-0+-00+0000000000+-0000+0-00+0000\ 

....: 000000+0-000+000000000000000+0000-00+00000000+0000-000+00000\ 

....: 00+0-00+0000000000000000+-0000+000000-+000000+00-0000+-00+00\ 

....: 00000000+-0000+00000000000000+0000000000" 

sage: a = matrix(20, [{'0':0, '+':1, '-': -1}[i] for i in s]) 

sage: fpl = FullyPackedLoop(a) 

sage: fpl.plot(loop_fill=True, loop_color_map='rainbow') 

Graphics object consisting of 27 graphics primitives 

 

.. PLOT:: 

:width: 400 px 

 

s = "00000000000+0000000000000000+00-0+00000000000+00-00+0-+00000\ 

0000+-00+00-+00000000+00-0000+0000-+00000000+000-0+0-+0-+000\ 

000+-000+-00+0000000+-+-000+00-+0-000+000+-000+-0+0000000-0+\ 

0000+0-+0-+00000-+00000+-+0-0+-00+0000000000+-0000+0-00+0000\ 

000000+0-000+000000000000000+0000-00+00000000+0000-000+00000\ 

00+0-00+0000000000000000+-0000+000000-+000000+00-0000+-00+00\ 

00000000+-0000+00000000000000+0000000000" 

a = matrix(20, [{'0':0, '+':1, '-': -1}[i] for i in s]) 

p = FullyPackedLoop(a).plot(loop_fill=True, loop_color_map='rainbow') 

sphinx_plot(p) 

""" 

from sage.plot.graphics import Graphics 

from sage.plot.line import line2d 

from sage.plot.polygon import polygon2d 

 

link_options = {} 

loop_options = {} 

extra_options = {} 

for k,v in options.items(): 

if k == 'link': 

link = v 

elif k == 'loop': 

loop = v 

elif k.startswith('link_'): 

link_options[k[5:]] = v 

elif k.startswith('loop_'): 

loop_options[k[5:]] = v 

else: 

link_options[k] = v 

loop_options[k] = v 

 

sv = self._six_vertex_model 

n = len(sv) 

 

# LR boudaries => odd sum 

# UD boundaries => even sum 

rank = self.parent()._boundary_index 

unrank = self.parent()._boundary 

seen = [False] * (2*n) 

 

squares = set((i,j) for i in range(n) for j in range(n)) 

 

colors = _make_color_list(2*n, 

colors = link_options.pop('colors', None), 

color_map = link_options.pop('color_map', None), 

randomize = link_options.pop('color_randomize', False)) 

 

G = Graphics() 

for i in range(2*n): 

if seen[i]: 

continue 

orbit = self._link_or_loop_from(unrank(i)) 

j = rank(orbit[-1]) 

seen[i] = seen[j] = True 

squares.difference_update(orbit) 

 

if link: 

if colors: 

link_options['color'] = colors.pop() 

 

# make it upside down 

orbit = [(j, n - i - 1) for i, j in orbit] 

G += line2d(orbit, **link_options) 

 

loops = [] 

while squares: 

orbit = self._link_or_loop_from(squares.pop()) 

loops.append(orbit) 

squares.difference_update(orbit) 

 

if loop: 

colors = _make_color_list(len(loops), 

colors = loop_options.pop('colors', None), 

color_map = loop_options.pop('color_map', None), 

randomize = loop_options.pop('color_randomize', False)) 

 

fill = loop_options.pop('fill') 

 

for orbit in loops: 

if colors: 

loop_options['color'] = colors.pop() 

 

# make it upside down 

orbit = [(j, n - i - 1) for i,j in orbit] 

 

if fill: 

G += polygon2d(orbit, **loop_options) 

else: 

G += line2d(orbit, **loop_options) 

 

G.axes(False) 

G.set_aspect_ratio(1) 

return G 

 

def gyration(self): 

r""" 

Return the fully packed loop obtained by applying gyration 

to the alternating sign matrix in bijection with ``self``. 

 

Gyration was first defined in [Wieland00]_ as an action on 

fully-packed loops. 

 

REFERENCES: 

 

.. [Wieland00] \B. Wieland. *A large dihedral symmetry of the set of 

alternating sign matrices*. Electron. J. Combin. 7 (2000). 

 

EXAMPLES:: 

 

sage: A = AlternatingSignMatrix([[1, 0, 0],[0, 1, 0],[0, 0, 1]]) 

sage: fpl = FullyPackedLoop(A) 

sage: fpl.gyration().to_alternating_sign_matrix() 

[0 0 1] 

[0 1 0] 

[1 0 0] 

sage: asm = AlternatingSignMatrix([[0, 0, 1],[1, 0, 0],[0, 1, 0]]) 

sage: f = FullyPackedLoop(asm) 

sage: f.gyration().to_alternating_sign_matrix() 

[0 1 0] 

[0 0 1] 

[1 0 0] 

""" 

return FullyPackedLoop(self.to_alternating_sign_matrix().gyration()) 

 

def _link_or_loop_from(self, pos, d0=None): 

r""" 

Return the coordinates of the line passing through ``pos``. 

 

EXAMPLES: 

 

A link:: 

 

sage: fpl = FullyPackedLoops(4).first() 

sage: fpl._link_or_loop_from((2,2)) 

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

sage: fpl._link_or_loop_from((-1, 0)) 

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

 

A loop:: 

 

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

sage: fpl = FullyPackedLoop(a) 

sage: fpl._link_or_loop_from((1,1)) 

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

""" 

global R, L, U, D, FPL_turns, FPL_edges 

 

orbit = [pos] 

sv = self._six_vertex_model 

n = len(sv) 

i,j = pos 

 

# deal with boundary cases 

if i < -1 or i > n or j < -1 or j > n: 

raise ValueError('indices out of range') 

if (i == -1 or i == n) and (i+j)%2 != 1: 

raise ValueError('left and right boundary values must have odd sum') 

if (j == -1 or j == n) and (i+j)%2 != 0: 

raise ValueError('up and down boundary values must have even sum') 

 

if i == -1: 

d = R 

elif i == n: 

d = L 

elif j == -1: 

d = U 

elif j == n: 

d = D 

elif d0 is None: 

d = FPL_edges[(i + j)%2][sv[i][j]][0] 

elif d0 in FPL_edges[(i+j)%2][sv[i][j]]: 

d = d0 

else: 

raise ValueError('invalid direction') 

 

# compute the link or loop 

while True: 

i += d[0] 

j += d[1] 

orbit.append((i,j)) 

if (i,j) == orbit[0] or \ 

i == -1 or j == -1 or \ 

i == n or j == n: 

break 

 

conf = sv[i][j] 

parity = (i + j) % 2 

 

d = FPL_turns[parity][conf][d] 

if d is None: 

raise RuntimeError 

 

if i == -1 or j == -1 or i == n or j == n: 

i0,j0 = orbit[0] 

if d0 is None and i0 != -1 and i0 != n and j0 != -1 and j0 != n: 

# only half of a link -> compute the other half 

i1,j1 = orbit[1] 

d = (i0-i1, j0-j1) 

orbit2 = self._link_or_loop_from(orbit[1], d) 

assert orbit2[0] == (i1,j1) and orbit2[1] == (i0,j0) 

return orbit2[:1:-1] + orbit 

return orbit 

else: 

return orbit 

 

 

def link_pattern(self): 

""" 

Return a link pattern corresponding to a fully packed loop. 

 

Here we define a link pattern `LP` to be a partition of the list 

`[1, ..., 2k]` into 2-element sets (such a partition is also known as 

a perfect matching) such that the following non-crossing condition holds: 

Let the numbers `1, ..., 2k` be written on the perimeter of a circle. 

For every 2-element set `(a,b)` of the partition `LP`, draw an arc 

linking the two numbers `a` and `b`. We say that `LP` is non-crossing 

if every arc can be drawn so that no two arcs intersect. 

 

Since every endpoint of a fully packed loop `fpl` is connected to a different 

endpoint, there is a natural surjection from the fully packed loops on an 

nxn grid onto the link patterns on the list `[1, \dots, 2n]`. 

The pairs of connected endpoints of a fully packed loop `fpl` correspond to 

the 2-element tuples of the corresponding link pattern. 

 

.. SEEALSO:: 

 

:class:`PerfectMatching` 

 

.. NOTE:: 

 

by convention, we choose the top left vertex to be even. 

See [Propp2001]_ and [Striker2015]_. 

 

EXAMPLES: 

 

We can extract the underlying link pattern (a non-crossing 

partition) from a fully packed loop:: 

 

sage: A = AlternatingSignMatrix([[0, 1, 0], [1, -1, 1], [0, 1, 0]]) 

sage: fpl = FullyPackedLoop(A) 

sage: fpl.link_pattern() 

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

 

sage: B = AlternatingSignMatrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) 

sage: fpl = FullyPackedLoop(B) 

sage: fpl.link_pattern() 

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

 

Gyration on an alternating sign matrix/fully packed loop ``fpl`` 

corresponds to a rotation (i.e. a becomes a-1 mod 2n) 

of the link pattern corresponding to ``fpl``:: 

 

sage: ASMs = AlternatingSignMatrices(3).list() 

sage: ncp = FullyPackedLoop(ASMs[1]).link_pattern() 

sage: rotated_ncp=[] 

sage: for (a,b) in ncp: 

....: for i in range(0,5): 

....: a,b=a%6+1,b%6+1; 

....: rotated_ncp.append((a,b)) 

sage: PerfectMatching(ASMs[1].gyration().to_fully_packed_loop().link_pattern()) ==\ 

PerfectMatching(rotated_ncp) 

True 

 

sage: fpl = FullyPackedLoop(ASMs[0]) 

sage: ncp = fpl.link_pattern() 

sage: rotated_ncp=[] 

sage: for (a,b) in ncp: 

....: for i in range(0,5): 

....: a,b=a%6+1,b%6+1; 

....: rotated_ncp.append((a,b)) 

sage: PerfectMatching(ASMs[0].gyration().to_fully_packed_loop().link_pattern()) ==\ 

PerfectMatching(rotated_ncp) 

True 

 

sage: mat = AlternatingSignMatrix([[0,0,1,0,0,0,0],[1,0,-1,0,1,0,0],[0,0,1,0,0,0,0],\ 

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

sage: fpl = FullyPackedLoop(mat) # n=7 

sage: ncp = fpl.link_pattern() 

sage: rotated_ncp=[] 

sage: for (a,b) in ncp: 

....: for i in range(0,13): 

....: a,b=a%14+1,b%14+1; 

....: rotated_ncp.append((a,b)) 

sage: PerfectMatching(mat.gyration().to_fully_packed_loop().link_pattern()) ==\ 

PerfectMatching(rotated_ncp) 

True 

 

sage: mat = AlternatingSignMatrix([[0,0,0,1,0,0], [0,0,1,-1,1,0], [0,1,0,0,-1,1], [1,0,-1,1,0,0], \ 

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

sage: fpl = FullyPackedLoop(mat) 

sage: ncp = fpl.link_pattern() 

sage: rotated_ncp=[] 

sage: for (a,b) in ncp: 

....: for i in range(0,11): 

....: a,b=a%12+1,b%12+1; 

....: rotated_ncp.append((a,b)) 

sage: PerfectMatching(mat.gyration().to_fully_packed_loop().link_pattern()) ==\ 

PerfectMatching(rotated_ncp) 

True 

 

TESTS: 

 

We test previous two bugs which showed up when this method is called twice:: 

 

sage: A = AlternatingSignMatrices(6) 

sage: B = A.random_element() 

sage: C = FullyPackedLoop(B) 

sage: D = C.link_pattern() 

sage: E = C.link_pattern() 

sage: D == E 

True 

""" 

global L, R, U, D, FPL_turns 

 

link_pattern = [] 

n = len(self._six_vertex_model) 

seen = [False] * (2*n) 

unrank = self.parent()._boundary 

rank = self.parent()._boundary_index 

sv = self._six_vertex_model 

 

for k in range(2*n): 

if seen[k]: 

continue 

 

i,j = unrank(k) 

 

# initial direction 

if i == -1: d = R 

elif i == n: d = L 

elif j == -1: d = U 

elif j == n: d = D 

 

# go through the link 

while True: 

i += d[0] 

j += d[1] 

if i == -1 or j == -1 or i == n or j == n: 

break 

 

conf = sv[i][j] 

parity = (i + j) % 2 

d = FPL_turns[parity][conf][d] 

 

# update seen and link_pattern 

l = rank((i,j)) 

seen[k] = seen[l] = True 

link_pattern.append((k+1, l+1)) 

 

return link_pattern 

 

def six_vertex_model(self): 

""" 

Return the underlying six vertex model configuration. 

 

EXAMPLES:: 

 

sage: B = AlternatingSignMatrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) 

sage: fpl = FullyPackedLoop(B) 

sage: fpl 

| | 

| | 

+ + -- + 

| | 

| | 

-- + + + -- 

| | 

| | 

+ -- + + 

| | 

| | 

sage: fpl.six_vertex_model() 

^ ^ ^ 

| | | 

--> # <- # <- # <-- 

| ^ ^ 

V | | 

--> # -> # <- # <-- 

| | ^ 

V V | 

--> # -> # -> # <-- 

| | | 

V V V 

""" 

return self._six_vertex_model 

 

class FullyPackedLoops(Parent, UniqueRepresentation): 

r""" 

Class of all fully packed loops on an `n \times n` grid. 

 

They are known to be in bijection with alternating sign matrices. 

 

.. SEEALSO:: 

 

:class:`AlternatingSignMatrices` 

 

INPUT: 

 

- ``n`` -- the number of row (and column) or grid 

 

EXAMPLES: 

 

This will create an instance to manipulate the fully packed loops of size 3:: 

 

sage: FPLs = FullyPackedLoops(3) 

sage: FPLs 

Fully packed loops on a 3x3 grid 

sage: FPLs.cardinality() 

7 

 

When using the square ice model, it is known that the number of 

configurations is equal to the number of alternating sign matrices:: 

 

sage: M = FullyPackedLoops(1) 

sage: len(M) 

1 

sage: M = FullyPackedLoops(4) 

sage: len(M) 

42 

sage: all(len(SixVertexModel(n, boundary_conditions='ice')) 

....: == FullyPackedLoops(n).cardinality() for n in range(1, 7)) 

True 

""" 

def __init__(self, n): 

r""" 

Initialize ``self``. 

 

TESTS:: 

 

sage: FPLs = FullyPackedLoops(3) 

sage: TestSuite(FPLs).run() 

""" 

self._n = n 

Parent.__init__(self, category=FiniteEnumeratedSets()) 

 

def __iter__(self): 

""" 

Iterate through ``self``. 

 

EXAMPLES:: 

 

sage: FPLs = FullyPackedLoops(2) 

sage: len(FPLs) 

2 

""" 

for X in SixVertexModel(self._n, boundary_conditions='ice'): 

yield self.element_class(self, X) 

 

def _repr_(self): 

r""" 

Return a string representation of ``self``. 

 

TESTS:: 

 

sage: FPLs = FullyPackedLoops(4); FPLs 

Fully packed loops on a 4x4 grid 

""" 

return "Fully packed loops on a %sx%s grid" % (self._n,self._n) 

 

def __contains__(self, fpl): 

""" 

Check if ``fpl`` is in ``self``. 

 

TESTS:: 

 

sage: FPLs = FullyPackedLoops(3) 

sage: FullyPackedLoop(AlternatingSignMatrix([[0,1,0],[1,0,0],[0,0,1]])) in FPLs 

True 

sage: FullyPackedLoop(AlternatingSignMatrix([[0,1,0],[1,-1,1],[0,1,0]])) in FPLs 

True 

sage: FullyPackedLoop(AlternatingSignMatrix([[0, 1],[1,0]])) in FPLs 

False 

sage: FullyPackedLoop(AlternatingSignMatrix([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]])) in FPLs 

False 

sage: [1,2,3] in FPLs 

False 

""" 

return parent(fpl) is self 

 

def _element_constructor_(self, generator): 

""" 

Construct an element of ``self``. 

 

EXAMPLES:: 

 

sage: FPLs = FullyPackedLoops(4) 

sage: M = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]] 

sage: A = AlternatingSignMatrix(M) 

sage: elt = FullyPackedLoop(A) 

sage: FPL = FPLs(elt); FPL 

| | 

| | 

+ + -- + + -- 

| | | 

| | | 

-- + + + -- + 

| | 

| | 

+ -- + + + -- 

| | | 

| | | 

-- + + -- + + 

| | 

| | 

 

sage: FPLs(A) == FPL 

True 

 

sage: FPLs(M) == FPL 

True 

 

sage: FPLs(FPL._six_vertex_model) == FPL 

True 

 

sage: FPL.parent() is FPLs 

True 

 

sage: FPL = FullyPackedLoops(2) 

sage: FPL([[3,1],[5,3]]) 

| 

| 

+ + -- 

| | 

| | 

-- + + 

| 

| 

""" 

if isinstance(generator, AlternatingSignMatrix): 

SVM = generator.to_six_vertex_model() 

elif isinstance(generator, SquareIceModel.Element) or \ 

isinstance(generator, SixVertexConfiguration): 

SVM = generator 

else: # Not ASM nor SVM 

try: 

SVM = AlternatingSignMatrix(generator).to_six_vertex_model() 

except (TypeError, ValueError): 

SVM = SixVertexModel(self._n, boundary_conditions='ice')(generator) 

SVM.to_alternating_sign_matrix() 

if len(SVM) != self._n: 

raise ValueError("invalid size") 

return self.element_class(self, SVM) 

 

Element = FullyPackedLoop 

 

def size(self): 

r""" 

Return the size of the matrices in ``self``. 

 

TESTS:: 

 

sage: FPLs = FullyPackedLoops(4) 

sage: FPLs.size() 

4 

""" 

return self._n 

 

def cardinality(self): 

r""" 

Return the cardinality of ``self``. 

 

The number of fully packed loops on `n \times n` grid 

 

.. MATH:: 

 

\prod_{k=0}^{n-1} \frac{(3k+1)!}{(n+k)!} = \frac{1! 4! 7! 10! 

\cdots (3n-2)!}{n! (n+1)! (n+2)! (n+3)! \cdots (2n-1)!}. 

 

EXAMPLES:: 

 

sage: [AlternatingSignMatrices(n).cardinality() for n in range(0, 11)] 

[1, 1, 2, 7, 42, 429, 7436, 218348, 10850216, 911835460, 129534272700] 

""" 

return Integer(prod( [ factorial(3*k+1)/factorial(self._n+k) 

for k in range(self._n)] )) 

 

def _an_element_(self): 

""" 

Return an element of ``self``. 

 

EXAMPLES:: 

 

sage: FPLs = FullyPackedLoops(3) 

sage: FPLs.an_element() 

| | 

| | 

+ + -- + 

| | 

| | 

-- + + + -- 

| | 

| | 

+ -- + + 

| | 

| | 

""" 

#ASM = AlternatingSignMatrix(matrix.identity(self._n)) 

#SVM = ASM.to_six_vertex_model() 

SVM = SixVertexModel(self._n,boundary_conditions='ice').an_element() 

return self.element_class(self, SVM) 

 

def _boundary(self, k): 

r""" 

Return the coordinates of the ``k``-th boundary. 

 

TESTS:: 

 

sage: F = FullyPackedLoops(5) 

sage: [F._boundary(k) for k in range(10)] == F._boundaries() 

True 

sage: all(F._boundary_index(F._boundary(k)) == k for k in range(10)) 

True 

 

sage: F = FullyPackedLoops(6) 

sage: [F._boundary(k) for k in range(12)] == F._boundaries() 

True 

sage: all(F._boundary_index(F._boundary(k)) == k for k in range(12)) 

True 

""" 

n = self._n 

n_LR = n//2 if n%2 == 0 else (n+1) // 2 

n_TB = n//2 if n%2 == 0 else (n-1) // 2 

if k < n_LR: 

return (-1, 2*k) 

k -= n_LR 

if k < n_TB: 

return (n%2 + 2*k, n) 

k -= n_TB 

if k < n_LR: 

return (n, n - 1 - 2*k) 

k -= n_LR 

if k < n_TB: 

return (n - 1 - n%2 - 2*k, -1) 

 

def _boundary_index(self, pos): 

r""" 

Return the index of the boundary at position ``pos``. 

 

TESTS:: 

 

sage: F = FullyPackedLoops(5) 

sage: [F._boundary_index(b) for b in F._boundaries()] 

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

sage: all(F._boundary(F._boundary_index(b)) == b for b in F._boundaries()) 

True 

 

sage: F = FullyPackedLoops(6) 

sage: [F._boundary_index(b) for b in F._boundaries()] 

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] 

sage: all(F._boundary(F._boundary_index(b)) == b for b in F._boundaries()) 

True 

""" 

n = self._n 

i, j = pos 

if i == -1: 

return j//2 

elif j == n: 

return (n + 1) // 2 + i // 2 

elif i == n: 

return n + (n - j) // 2 

elif j == -1: 

return 3 * n // 2 + (n - i) // 2 

 

def _boundaries(self): 

r""" 

Return the list of coordinates for the link in the boundaries. 

 

TESTS:: 

 

sage: FullyPackedLoops(5)._boundaries() 

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

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

 

sage: FullyPackedLoops(6)._boundaries() 

[(-1, 0), (-1, 2), (-1, 4), (0, 6), (2, 6), (4, 6), 

(6, 5), (6, 3), (6, 1), (5, -1), (3, -1), (1, -1)] 

""" 

n = self._n 

boundaries = [] 

# left side: j = 0 mod 2 

boundaries.extend((-1, j) for j in range(0, n, 2)) 

# top side: i = n mod 2 

boundaries.extend((i, n) for i in range(n % 2, n, 2)) 

# right side: j = n+1 mod 2 

boundaries.extend((n, j) for j in range(n - 1, -1, -2)) 

# bottom side: i = 1 mod 2 

boundaries.extend((i, -1) for i in range(n - 1 - n % 2, -1, -2)) 

return boundaries