Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

1715

1716

1717

1718

1719

1720

1721

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

1952

1953

1954

1955

1956

1957

1958

1959

1960

1961

1962

1963

1964

1965

1966

1967

1968

1969

1970

1971

1972

1973

1974

1975

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

1986

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1998

1999

2000

2001

2002

2003

2004

2005

2006

2007

2008

2009

2010

2011

2012

2013

2014

2015

2016

2017

2018

2019

2020

2021

2022

2023

2024

2025

2026

2027

2028

2029

2030

2031

2032

2033

2034

2035

2036

2037

2038

2039

2040

2041

2042

2043

2044

2045

2046

2047

2048

2049

2050

2051

2052

2053

2054

2055

2056

2057

2058

2059

2060

2061

2062

2063

2064

2065

2066

2067

2068

2069

2070

2071

2072

2073

2074

2075

2076

2077

2078

2079

2080

2081

2082

2083

2084

2085

2086

2087

2088

2089

2090

2091

2092

2093

2094

2095

2096

2097

2098

2099

2100

2101

2102

2103

2104

2105

2106

2107

2108

2109

2110

2111

2112

2113

2114

2115

2116

2117

2118

2119

2120

2121

2122

2123

2124

2125

2126

2127

2128

2129

2130

2131

2132

2133

2134

2135

2136

2137

2138

2139

2140

2141

2142

2143

2144

2145

2146

2147

2148

2149

2150

2151

2152

2153

2154

2155

2156

2157

2158

2159

2160

2161

2162

2163

2164

2165

2166

2167

2168

2169

2170

2171

2172

2173

2174

2175

2176

2177

2178

2179

2180

2181

2182

2183

2184

2185

2186

2187

2188

2189

2190

2191

2192

2193

2194

2195

2196

2197

2198

2199

2200

2201

2202

2203

2204

2205

2206

2207

2208

2209

2210

2211

2212

2213

2214

2215

2216

2217

2218

2219

2220

2221

2222

2223

2224

2225

2226

2227

2228

2229

2230

2231

2232

2233

2234

2235

2236

2237

2238

2239

2240

2241

2242

2243

2244

2245

2246

2247

2248

2249

2250

2251

2252

2253

2254

2255

2256

2257

2258

2259

2260

2261

2262

2263

2264

2265

2266

2267

2268

2269

2270

2271

2272

2273

2274

2275

2276

2277

2278

2279

2280

2281

2282

2283

2284

2285

2286

2287

2288

2289

2290

2291

2292

2293

2294

2295

2296

2297

2298

2299

2300

2301

2302

2303

2304

2305

2306

2307

2308

2309

2310

2311

2312

2313

2314

2315

2316

2317

2318

2319

2320

2321

2322

2323

2324

2325

2326

2327

2328

2329

2330

2331

2332

2333

2334

2335

2336

2337

2338

2339

2340

2341

2342

2343

2344

2345

2346

2347

2348

2349

2350

2351

2352

2353

2354

2355

2356

2357

2358

2359

2360

2361

2362

2363

2364

2365

2366

2367

2368

2369

2370

2371

2372

2373

2374

2375

2376

2377

2378

2379

2380

2381

2382

2383

2384

2385

2386

2387

2388

2389

2390

2391

2392

2393

2394

2395

2396

2397

2398

2399

2400

2401

2402

2403

2404

2405

2406

2407

2408

2409

2410

2411

2412

2413

2414

2415

2416

2417

2418

2419

2420

2421

2422

2423

2424

2425

2426

2427

2428

2429

2430

2431

2432

2433

2434

2435

2436

2437

2438

2439

2440

2441

2442

2443

2444

2445

2446

2447

2448

2449

2450

2451

2452

2453

2454

2455

2456

2457

2458

2459

2460

2461

2462

2463

2464

2465

2466

2467

2468

2469

2470

2471

2472

2473

2474

2475

2476

2477

2478

2479

2480

2481

2482

2483

2484

2485

2486

2487

2488

2489

2490

2491

2492

2493

2494

2495

2496

2497

2498

2499

2500

2501

2502

2503

2504

2505

2506

2507

2508

2509

2510

2511

2512

2513

2514

2515

2516

2517

2518

2519

2520

2521

2522

2523

2524

2525

2526

2527

2528

2529

2530

2531

2532

2533

2534

2535

2536

2537

2538

2539

2540

2541

2542

2543

2544

2545

2546

2547

2548

2549

2550

2551

2552

2553

2554

2555

2556

2557

2558

2559

2560

2561

2562

2563

2564

2565

2566

2567

2568

2569

2570

2571

2572

2573

2574

2575

2576

2577

2578

2579

2580

2581

2582

2583

2584

2585

2586

2587

2588

2589

2590

2591

2592

2593

2594

2595

2596

2597

2598

2599

2600

2601

2602

2603

2604

2605

2606

2607

2608

2609

2610

2611

2612

2613

2614

2615

2616

2617

2618

2619

2620

2621

2622

2623

2624

2625

2626

2627

2628

2629

2630

2631

2632

2633

2634

2635

2636

2637

2638

2639

2640

2641

2642

2643

2644

2645

2646

2647

2648

2649

2650

2651

2652

2653

2654

2655

2656

2657

2658

2659

2660

2661

2662

2663

2664

2665

2666

2667

2668

2669

2670

2671

2672

2673

2674

2675

2676

2677

2678

2679

2680

2681

2682

2683

2684

2685

2686

2687

2688

2689

2690

2691

2692

2693

2694

2695

2696

2697

2698

2699

2700

2701

2702

2703

2704

2705

2706

2707

2708

2709

2710

2711

2712

2713

2714

2715

2716

2717

2718

2719

2720

2721

2722

2723

2724

2725

2726

2727

2728

2729

2730

2731

2732

2733

2734

2735

2736

2737

2738

2739

2740

2741

2742

2743

2744

2745

2746

2747

2748

2749

2750

2751

2752

2753

2754

2755

2756

2757

2758

2759

2760

2761

2762

2763

2764

2765

2766

2767

2768

2769

2770

2771

2772

2773

2774

2775

2776

2777

2778

2779

2780

2781

2782

2783

2784

2785

2786

2787

2788

2789

2790

2791

2792

2793

2794

2795

2796

2797

2798

2799

2800

2801

2802

2803

2804

2805

2806

2807

2808

2809

2810

2811

2812

2813

2814

2815

2816

2817

2818

2819

2820

2821

2822

2823

2824

2825

2826

2827

2828

2829

2830

2831

2832

2833

2834

2835

2836

2837

2838

2839

2840

2841

2842

2843

2844

2845

2846

2847

2848

2849

2850

2851

2852

2853

2854

2855

2856

2857

2858

2859

2860

2861

2862

2863

2864

2865

2866

2867

2868

2869

2870

2871

2872

2873

2874

2875

2876

2877

2878

2879

2880

2881

2882

2883

2884

2885

2886

2887

2888

2889

2890

2891

2892

2893

2894

2895

2896

2897

2898

2899

2900

2901

2902

2903

2904

2905

2906

2907

2908

2909

2910

2911

2912

2913

2914

2915

2916

2917

2918

2919

2920

2921

2922

2923

2924

2925

2926

2927

2928

2929

2930

2931

2932

2933

2934

2935

2936

2937

2938

2939

2940

2941

2942

2943

2944

2945

2946

2947

2948

2949

2950

2951

2952

2953

2954

2955

2956

2957

2958

2959

2960

2961

2962

2963

2964

2965

2966

2967

2968

2969

2970

2971

2972

2973

2974

2975

2976

2977

2978

2979

2980

2981

2982

2983

2984

2985

2986

2987

2988

2989

2990

2991

2992

2993

2994

2995

2996

2997

2998

2999

3000

3001

3002

3003

3004

3005

3006

3007

3008

3009

3010

3011

3012

3013

3014

3015

3016

3017

3018

3019

3020

3021

3022

3023

3024

3025

3026

3027

3028

3029

3030

3031

3032

3033

3034

3035

3036

3037

3038

3039

3040

3041

3042

3043

3044

3045

3046

3047

3048

3049

3050

3051

3052

3053

3054

3055

3056

3057

3058

3059

3060

3061

3062

3063

3064

3065

3066

3067

3068

3069

3070

3071

3072

3073

3074

3075

3076

3077

3078

3079

3080

3081

3082

3083

3084

3085

3086

3087

3088

3089

3090

3091

3092

3093

3094

3095

3096

3097

3098

3099

3100

3101

3102

3103

3104

3105

3106

3107

3108

3109

3110

3111

3112

3113

3114

3115

3116

3117

3118

3119

3120

3121

3122

3123

3124

3125

3126

3127

3128

3129

3130

3131

3132

3133

3134

3135

3136

3137

3138

3139

3140

3141

3142

3143

3144

3145

3146

3147

3148

3149

3150

3151

3152

3153

3154

3155

3156

3157

3158

3159

3160

3161

3162

3163

3164

3165

3166

3167

3168

3169

3170

3171

3172

3173

3174

3175

3176

3177

3178

3179

3180

3181

3182

3183

3184

3185

3186

3187

3188

3189

3190

3191

3192

3193

3194

3195

3196

3197

3198

3199

3200

3201

3202

3203

3204

3205

3206

3207

3208

3209

3210

3211

3212

3213

3214

3215

3216

3217

3218

3219

3220

3221

3222

3223

3224

3225

3226

3227

3228

3229

3230

3231

3232

3233

3234

3235

3236

3237

3238

3239

3240

3241

3242

3243

3244

3245

3246

3247

3248

3249

3250

3251

3252

3253

3254

3255

3256

3257

3258

3259

3260

3261

3262

3263

3264

3265

3266

3267

3268

3269

3270

3271

3272

3273

3274

3275

3276

3277

3278

3279

3280

3281

3282

3283

3284

3285

3286

3287

3288

3289

3290

3291

3292

3293

3294

3295

3296

3297

3298

3299

3300

3301

3302

3303

3304

3305

3306

3307

3308

3309

3310

3311

3312

3313

3314

3315

3316

3317

3318

3319

3320

3321

3322

3323

3324

3325

3326

3327

3328

3329

3330

3331

3332

3333

3334

3335

3336

3337

3338

3339

3340

3341

3342

3343

3344

3345

3346

3347

3348

3349

3350

3351

3352

3353

3354

3355

3356

3357

3358

3359

3360

3361

3362

3363

3364

3365

3366

3367

3368

3369

3370

3371

3372

3373

3374

3375

3376

3377

3378

3379

3380

3381

3382

3383

3384

3385

3386

3387

3388

3389

3390

3391

3392

3393

3394

3395

3396

3397

3398

3399

3400

3401

3402

3403

3404

3405

3406

3407

3408

3409

3410

3411

3412

3413

3414

3415

3416

3417

3418

3419

3420

3421

3422

3423

3424

3425

3426

3427

3428

3429

3430

3431

3432

3433

3434

3435

3436

3437

3438

3439

3440

3441

3442

3443

3444

3445

3446

3447

3448

3449

3450

3451

3452

3453

3454

3455

3456

3457

3458

3459

3460

3461

3462

3463

3464

3465

3466

3467

3468

3469

3470

3471

3472

3473

3474

3475

3476

3477

3478

3479

3480

3481

3482

3483

3484

3485

3486

3487

3488

3489

3490

3491

3492

3493

3494

3495

3496

3497

3498

3499

3500

3501

3502

3503

3504

3505

3506

3507

3508

3509

3510

3511

3512

3513

3514

3515

3516

3517

3518

3519

3520

3521

3522

3523

3524

3525

3526

3527

3528

3529

3530

3531

3532

3533

3534

3535

3536

3537

3538

3539

3540

3541

3542

3543

3544

3545

3546

3547

3548

3549

3550

3551

3552

3553

3554

3555

3556

3557

3558

3559

3560

3561

3562

3563

3564

3565

3566

3567

3568

3569

3570

3571

3572

3573

3574

3575

3576

3577

3578

3579

3580

3581

3582

3583

3584

3585

3586

3587

3588

3589

3590

3591

3592

3593

3594

3595

3596

3597

3598

3599

3600

3601

3602

3603

3604

3605

3606

3607

3608

3609

3610

3611

3612

3613

3614

3615

3616

3617

3618

3619

3620

3621

3622

3623

3624

3625

3626

3627

3628

3629

3630

3631

3632

3633

3634

3635

3636

3637

3638

3639

3640

3641

3642

3643

3644

3645

3646

3647

3648

3649

3650

3651

3652

3653

3654

3655

3656

3657

3658

3659

3660

3661

3662

3663

3664

3665

3666

3667

3668

3669

3670

3671

3672

3673

3674

3675

3676

3677

3678

3679

3680

3681

3682

3683

3684

3685

3686

3687

3688

3689

3690

3691

3692

3693

3694

3695

3696

3697

3698

3699

3700

3701

3702

3703

3704

3705

3706

3707

3708

3709

3710

3711

3712

3713

3714

3715

3716

3717

3718

3719

3720

3721

3722

3723

3724

3725

3726

3727

3728

3729

3730

3731

3732

3733

3734

3735

3736

3737

3738

3739

3740

3741

3742

3743

3744

3745

3746

3747

3748

3749

3750

3751

3752

3753

3754

3755

3756

3757

3758

3759

3760

3761

3762

3763

3764

3765

3766

3767

3768

3769

3770

3771

3772

3773

3774

3775

3776

3777

3778

3779

3780

3781

3782

3783

3784

3785

3786

3787

3788

3789

3790

3791

3792

3793

3794

3795

3796

3797

3798

3799

3800

3801

3802

3803

3804

3805

3806

3807

3808

3809

3810

3811

3812

3813

3814

3815

3816

3817

3818

3819

3820

3821

3822

3823

3824

3825

3826

3827

3828

3829

3830

3831

3832

3833

3834

3835

3836

3837

3838

3839

3840

3841

3842

3843

3844

3845

3846

3847

3848

3849

3850

3851

3852

3853

3854

3855

3856

3857

3858

3859

3860

3861

3862

3863

3864

3865

3866

3867

3868

3869

3870

3871

3872

3873

3874

3875

3876

3877

3878

3879

3880

3881

3882

3883

3884

3885

3886

3887

3888

3889

3890

3891

3892

3893

3894

3895

3896

3897

3898

3899

3900

3901

3902

3903

3904

3905

3906

3907

3908

3909

3910

3911

3912

3913

3914

3915

3916

3917

3918

3919

3920

3921

3922

3923

3924

3925

3926

3927

3928

3929

3930

3931

3932

3933

3934

3935

3936

3937

3938

3939

3940

3941

3942

3943

3944

3945

3946

3947

3948

3949

3950

3951

3952

3953

3954

3955

3956

3957

3958

3959

3960

3961

3962

3963

3964

3965

3966

3967

3968

3969

3970

3971

3972

3973

3974

3975

3976

3977

3978

3979

3980

3981

3982

3983

3984

3985

3986

3987

3988

3989

3990

3991

3992

3993

3994

3995

3996

3997

3998

3999

4000

4001

4002

4003

4004

4005

4006

4007

4008

4009

4010

4011

4012

4013

4014

4015

4016

4017

4018

4019

4020

4021

4022

4023

4024

4025

4026

4027

4028

4029

4030

4031

4032

4033

4034

4035

4036

4037

4038

4039

4040

4041

4042

4043

4044

4045

4046

4047

4048

4049

4050

4051

4052

4053

4054

4055

4056

4057

4058

4059

4060

4061

4062

4063

4064

4065

4066

4067

4068

4069

4070

4071

4072

4073

4074

4075

4076

4077

4078

4079

4080

4081

4082

4083

4084

4085

4086

4087

4088

4089

4090

4091

4092

4093

4094

4095

4096

4097

4098

4099

4100

4101

4102

4103

4104

4105

4106

4107

4108

4109

4110

4111

4112

4113

4114

4115

4116

4117

4118

4119

4120

4121

4122

4123

4124

4125

4126

4127

4128

4129

4130

4131

4132

4133

4134

4135

4136

4137

4138

4139

4140

4141

4142

4143

4144

4145

4146

4147

4148

4149

4150

4151

4152

4153

4154

4155

4156

4157

4158

4159

4160

4161

4162

4163

4164

4165

4166

4167

4168

4169

4170

4171

4172

4173

4174

4175

4176

4177

4178

4179

4180

4181

4182

4183

4184

4185

4186

4187

4188

4189

4190

4191

4192

4193

4194

4195

4196

4197

4198

4199

4200

4201

4202

4203

4204

4205

4206

4207

4208

4209

4210

4211

4212

4213

4214

4215

4216

4217

4218

4219

4220

4221

4222

4223

4224

4225

4226

4227

4228

4229

4230

4231

4232

4233

4234

4235

4236

4237

4238

4239

4240

4241

4242

4243

4244

4245

4246

4247

4248

4249

4250

4251

4252

4253

4254

4255

4256

4257

4258

4259

4260

4261

4262

4263

4264

4265

4266

4267

4268

4269

4270

4271

4272

4273

4274

4275

4276

4277

4278

4279

4280

4281

4282

4283

4284

4285

4286

4287

4288

4289

4290

4291

4292

4293

4294

4295

4296

4297

4298

4299

4300

4301

4302

4303

4304

4305

4306

4307

4308

4309

4310

4311

4312

4313

4314

4315

4316

4317

4318

4319

4320

4321

4322

4323

4324

4325

4326

4327

4328

4329

4330

4331

4332

4333

4334

4335

4336

4337

4338

4339

4340

4341

4342

4343

4344

4345

4346

4347

4348

4349

4350

4351

4352

4353

4354

4355

4356

4357

4358

4359

4360

4361

4362

4363

4364

4365

4366

4367

4368

4369

4370

4371

4372

4373

4374

4375

4376

4377

4378

4379

4380

4381

4382

4383

4384

4385

4386

4387

4388

4389

4390

4391

4392

4393

4394

4395

4396

4397

4398

4399

4400

4401

4402

4403

4404

4405

4406

4407

4408

4409

4410

4411

4412

4413

4414

4415

4416

4417

4418

4419

4420

4421

4422

4423

4424

4425

4426

4427

4428

4429

4430

4431

4432

4433

4434

4435

4436

4437

4438

4439

4440

4441

4442

4443

4444

4445

4446

4447

4448

4449

4450

4451

4452

4453

4454

4455

4456

4457

4458

4459

4460

4461

4462

4463

4464

4465

4466

4467

4468

4469

4470

4471

4472

4473

4474

4475

4476

4477

4478

4479

4480

4481

4482

4483

4484

4485

4486

4487

4488

4489

4490

4491

4492

4493

4494

4495

4496

4497

4498

4499

4500

4501

4502

4503

4504

4505

4506

4507

4508

4509

4510

4511

4512

4513

4514

4515

4516

4517

4518

4519

4520

4521

4522

4523

4524

4525

4526

4527

4528

4529

4530

4531

4532

4533

4534

4535

4536

4537

4538

4539

4540

4541

4542

4543

4544

4545

4546

4547

4548

4549

4550

4551

4552

4553

4554

4555

4556

4557

4558

4559

4560

4561

4562

4563

4564

4565

4566

4567

4568

4569

4570

4571

4572

4573

4574

4575

4576

4577

4578

4579

4580

4581

4582

4583

4584

4585

4586

4587

4588

4589

4590

4591

4592

4593

4594

4595

4596

4597

4598

4599

4600

4601

4602

4603

4604

4605

4606

4607

4608

4609

4610

4611

4612

4613

4614

4615

4616

4617

4618

4619

4620

4621

4622

4623

4624

4625

4626

4627

4628

4629

4630

4631

4632

4633

4634

4635

4636

4637

4638

4639

4640

4641

4642

4643

4644

4645

4646

4647

4648

4649

4650

4651

4652

4653

4654

4655

4656

4657

4658

4659

4660

4661

4662

4663

4664

4665

4666

4667

4668

4669

4670

4671

4672

4673

4674

4675

4676

4677

4678

4679

4680

4681

4682

4683

4684

4685

4686

4687

4688

4689

4690

4691

4692

4693

4694

4695

4696

4697

4698

4699

4700

4701

4702

4703

4704

4705

4706

4707

4708

4709

4710

4711

4712

4713

4714

4715

4716

4717

4718

4719

4720

4721

4722

4723

4724

4725

4726

4727

4728

4729

4730

4731

4732

4733

4734

4735

4736

4737

4738

4739

4740

4741

4742

4743

4744

4745

4746

4747

4748

4749

4750

4751

4752

4753

4754

4755

4756

4757

4758

4759

4760

4761

4762

4763

4764

4765

4766

4767

4768

4769

4770

4771

4772

4773

4774

4775

4776

4777

4778

4779

4780

4781

4782

4783

4784

4785

4786

4787

4788

4789

4790

4791

4792

4793

4794

4795

4796

4797

4798

4799

4800

4801

4802

4803

4804

4805

4806

4807

4808

4809

4810

4811

4812

4813

4814

4815

4816

4817

4818

4819

4820

4821

4822

4823

4824

4825

4826

4827

4828

4829

4830

4831

4832

4833

4834

4835

4836

4837

4838

4839

4840

4841

4842

4843

4844

4845

4846

4847

4848

4849

4850

4851

4852

4853

4854

4855

4856

4857

4858

4859

4860

4861

4862

4863

4864

4865

4866

4867

4868

4869

4870

4871

4872

4873

4874

4875

4876

4877

4878

4879

4880

4881

4882

4883

4884

4885

4886

4887

4888

4889

4890

4891

4892

4893

4894

4895

4896

4897

4898

4899

4900

4901

4902

4903

4904

4905

4906

4907

4908

4909

4910

4911

4912

4913

4914

4915

4916

4917

4918

4919

4920

4921

4922

4923

4924

4925

4926

4927

4928

4929

4930

4931

4932

4933

4934

4935

4936

4937

4938

4939

4940

4941

4942

4943

4944

4945

4946

4947

4948

4949

4950

4951

4952

4953

4954

4955

4956

4957

4958

4959

4960

4961

4962

4963

4964

4965

4966

4967

4968

4969

4970

4971

4972

4973

4974

4975

4976

4977

4978

4979

4980

4981

4982

4983

4984

4985

4986

4987

4988

4989

4990

4991

4992

4993

4994

4995

4996

4997

4998

4999

5000

5001

5002

5003

5004

5005

5006

5007

5008

5009

5010

5011

5012

5013

5014

5015

5016

5017

5018

5019

5020

5021

5022

5023

5024

5025

5026

5027

5028

5029

5030

5031

5032

5033

5034

5035

5036

5037

5038

5039

5040

5041

5042

5043

5044

5045

5046

5047

5048

5049

5050

5051

5052

5053

5054

5055

5056

5057

5058

5059

5060

5061

5062

5063

5064

5065

5066

5067

5068

5069

5070

5071

5072

5073

5074

5075

5076

5077

5078

5079

5080

5081

5082

5083

5084

5085

5086

5087

5088

5089

5090

5091

5092

5093

5094

5095

5096

5097

5098

5099

5100

5101

5102

5103

5104

5105

5106

5107

5108

5109

5110

5111

5112

5113

5114

5115

5116

5117

5118

5119

5120

5121

5122

5123

5124

5125

5126

5127

5128

5129

5130

5131

5132

5133

5134

5135

5136

5137

5138

5139

5140

5141

5142

5143

5144

5145

5146

5147

5148

5149

5150

5151

5152

5153

5154

5155

5156

5157

5158

5159

5160

5161

5162

5163

5164

5165

5166

5167

5168

5169

5170

5171

5172

5173

5174

5175

5176

5177

5178

5179

5180

5181

5182

5183

5184

5185

5186

5187

5188

5189

5190

5191

5192

5193

5194

5195

5196

5197

5198

5199

5200

5201

5202

5203

5204

5205

5206

5207

5208

5209

5210

5211

5212

5213

5214

5215

5216

5217

5218

5219

5220

5221

5222

5223

5224

5225

5226

5227

5228

5229

5230

5231

5232

5233

5234

5235

5236

5237

5238

5239

5240

5241

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

r""" 

ClusterSeed 

 

A *cluster seed* is a pair `(B,\mathbf{x})` with `B` being a *skew-symmetrizable* `(n+m \times n)` *-matrix* 

and with `\mathbf{x}` being an `n`-tuple of *independent elements* in the field of rational functions in `n` variables. 

 

For the compendium on the cluster algebra and quiver package see 

[MS2011]_. 

 

AUTHORS: 

 

- Gregg Musiker: Initial Version 

- Christian Stump: Initial Version 

- Aram Dermenjian (2015-07-01): Updating ability to not rely solely on clusters 

- Jesse Levitt (2015-07-01): Updating ability to not rely solely on clusters 

 

REFERENCES: 

 

- [FZ2007]_ 

 

- [BDP2013]_ 

 

.. SEEALSO:: For mutation types of cluster seeds, see :meth:`sage.combinat.cluster_algebra_quiver.quiver_mutation_type.QuiverMutationType`. Cluster seeds are closely related to :meth:`sage.combinat.cluster_algebra_quiver.quiver.ClusterQuiver`. 

""" 

 

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

# Copyright (C) 2011 Gregg Musiker <musiker@math.mit.edu> 

# Christian Stump <christian.stump@univie.ac.at> 

# 

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

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

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

from __future__ import print_function 

from six.moves import range 

 

import itertools 

import time 

from operator import pos 

from itertools import islice 

from sage.structure.sage_object import SageObject 

from copy import copy 

from sage.rings.all import QQ, infinity 

from sage.rings.integer_ring import ZZ 

from sage.rings.all import FractionField, PolynomialRing 

from sage.rings.fraction_field_element import FractionFieldElement 

from sage.sets.all import Set 

from sage.graphs.digraph import DiGraph 

from sage.combinat.cluster_algebra_quiver.quiver_mutation_type import QuiverMutationType_Irreducible, QuiverMutationType_Reducible 

from sage.combinat.cluster_algebra_quiver.mutation_type import is_mutation_finite 

from random import randint 

from sage.misc.all import prod 

from sage.matrix.all import identity_matrix 

from sage.matrix.constructor import matrix 

from sage.combinat.cluster_algebra_quiver.quiver import ClusterQuiver 

from sage.rings.integer import Integer 

from copy import deepcopy 

 

from sage.misc.decorators import rename_keyword 

 

 

class ClusterSeed(SageObject): 

r""" 

The *cluster seed* associated to an *exchange matrix*. 

 

INPUT: 

 

- ``data`` -- can be any of the following:: 

 

* QuiverMutationType 

* str - a string representing a QuiverMutationType or a common quiver type (see Examples) 

* ClusterQuiver 

* Matrix - a skew-symmetrizable matrix 

* DiGraph - must be the input data for a quiver 

* List of edges - must be the edge list of a digraph for a quiver 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',5]); S 

A seed for a cluster algebra of rank 5 of type ['A', 5] 

 

sage: S = ClusterSeed(['A',[2,5],1]); S 

A seed for a cluster algebra of rank 7 of type ['A', [2, 5], 1] 

 

sage: T = ClusterSeed( S ); T 

A seed for a cluster algebra of rank 7 of type ['A', [2, 5], 1] 

 

sage: T = ClusterSeed( S._M ); T 

A seed for a cluster algebra of rank 7 

 

sage: T = ClusterSeed( S.quiver()._digraph ); T 

A seed for a cluster algebra of rank 7 

 

sage: T = ClusterSeed( S.quiver()._digraph.edges() ); T 

A seed for a cluster algebra of rank 7 

 

sage: S = ClusterSeed(['B',2]); S 

A seed for a cluster algebra of rank 2 of type ['B', 2] 

 

sage: S = ClusterSeed(['C',2]); S 

A seed for a cluster algebra of rank 2 of type ['B', 2] 

 

sage: S = ClusterSeed(['A', [5,0],1]); S 

A seed for a cluster algebra of rank 5 of type ['D', 5] 

 

sage: S = ClusterSeed(['GR',[3,7]]); S 

A seed for a cluster algebra of rank 6 of type ['E', 6] 

 

sage: S = ClusterSeed(['F', 4, [2,1]]); S 

A seed for a cluster algebra of rank 6 of type ['F', 4, [1, 2]] 

 

sage: S = ClusterSeed(['A',4]); S._use_fpolys 

True 

 

sage: S._use_d_vec 

True 

 

sage: S._use_g_vec 

True 

 

sage: S._use_c_vec 

True 

 

sage: S = ClusterSeed(['A', 4]); S.use_fpolys(False); S._use_fpolys 

False 

 

sage: S = ClusterSeed(DiGraph([['a', 'b'], ['c', 'b'], ['c', 'd'], ['e', 'd']]), frozen = \ 

['c']); S 

A seed for a cluster algebra of rank 4 with 1 frozen variable 

 

sage: S = ClusterSeed(['D', 4],user_labels = [-1, 0, 1, 2]);S 

A seed for a cluster algebra of rank 4 of type ['D', 4] 

""" 

def __init__(self, data, frozen=None, is_principal=False, user_labels=None, user_labels_prefix='x'): 

r""" 

 

Initializes the ClusterSeed ``self`` with the following range of possible attributes: 

 

* self._n - the number of mutable elements of the cluster seed. 

* self._m - the number of immutable elements of the cluster seed. 

* self._nlist - a list of mutable elements of the cluster seed. 

* self._mlist - a list of immutable elements of the cluster seed. 

* self._M - the 'n + m' x 'n' exchange matrix associated to the cluster seed. 

* self._B - the mutable part of self._M. 

* self._b_initial - the initial exchange matrix 

* self._description - the description of the ClusterSeed 

* self._use_fpolys - a boolean tracking whether F-polynomials and cluster variables will be tracked as part of every mutation. 

* self._cluster - a list tracking the current names of cluster elements. 

* self._user_labels_prefix - the prefix for every named cluster element. Defaults to 'x'. 

* self._user_labels - an optional dictionary or list of user 

defined names for all cluster elements. Defaults to ``'x_i'`` 

for mutable elements and ``'y_i'`` for immutable elements. 

All labels should be integers or alphanumeric strings. 

* self._init_vars - an internal list for defining ambient the algebraic setting and naming quiver vertices. 

* self._init_exch - the dictionary storing the initial mutable cluster variable names. 

* self._U - the coefficient tuple of the initial cluster seed. 

* self._F - the dictionary of F-polynomials. 

* self._R - the ambient polynomial ring. 

* self._y - the coefficient tuple for the current cluster seed. 

* self._yhat - the mixed coefficient tuple appearing in Proposition 3.9 of [FZ2007] 

* self._use_g_vec - a boolean stating if g-vectors for the cluster seed are being tracked. User input overridden as needed. 

* self._G - the matrix containing all g-vectors. 

* self._use_d_vec - a boolean stating if d-vectors for the cluster seed are being tracked. 

* self._D - the matrix containing all d-vectors. 

* self._bot_is_c - a boolean stating if the c-vectors are stored on the bottom of the exchange matrix M. 

* self._use_c_vec - a boolean stating if c-vectors for the cluster seed are being tracked. User input overridden as needed. 

* self._C - the matrix containing all c-vectors. 

* self._BC - an extended matrix involving the B and C matrices used for simplifying mutation calculations. 

* self._is_principal - a boolean tracking whether the ClusterSeed contains immutable elements coming from a principal extension of the mutable vertices. To be deprecated in future versions. 

 

* self._quiver - the ClusterQuiver corresponding to the exchange matrix self._M . 

* self._mutation_type - the mutation type of self._quiver . 

 

* self._track_mut - a boolean tracking whether the a ClusterSeed's mutation path is being recorded. 

* self._mut_path - the list of integers recording the mutation path of a seed - with consecutive repeats deleted since mutations is an involution. 

 

TESTS:: 

 

sage: S = ClusterSeed(['A',4]) 

sage: TestSuite(S).run() 

""" 

#initialize a null state ClusterSeed object so all tests run and fail as appropriate. 

# numerous doctests if this null state is not first initialized. 

self._n = 0 

self._m = 0 

self._nlist = None 

self._mlist = None 

self._M = None 

self._B = None 

self._b_initial = None 

self._description = None 

self._use_fpolys = None 

self._cluster = None 

self._user_labels_prefix = None 

self._user_labels = None 

self._init_vars = None 

self._init_exch = None 

self._U = None 

self._F = None 

self._R = None 

self._y = None 

self._yhat = None 

 

self._use_g_vec = None 

self._G = None 

 

self._use_d_vec = None 

self._D = None 

 

self._bot_is_c = None 

self._use_c_vec = None 

self._C = None 

self._BC = None 

self._is_principal = None 

 

self._quiver = None 

self._mutation_type = None 

 

self._track_mut = None 

self._mut_path = None 

 

# ensures user_labels are immutable 

if isinstance(user_labels, list): 

user_labels = [tuple(x) if isinstance(x, list) else x for x in user_labels] 

elif isinstance(user_labels, dict): 

values = [tuple(user_labels[x]) if isinstance(user_labels[x], list) else user_labels[x] for x in user_labels] 

keys = list(user_labels) 

user_labels = {keys[i]: v for i, v in enumerate(values)} 

 

# constructs a cluster seed from a cluster seed 

if isinstance(data, ClusterSeed): 

if frozen: 

print("The input \'frozen\' is ignored") 

 

# Copy the following attributes from data 

self._M = copy( data._M ) 

self._M.set_immutable() 

self._B = copy( data._B ) 

self._n = data._n 

self._m = data._m 

self._nlist = list(data._nlist) 

self._mlist = list(data._mlist) 

 

# initialize matrix of g-vectors if desired and possible 

if data._use_g_vec and (data._G or data._cluster or (data._B.is_skew_symmetric() and data._C) or data._track_mut): 

self._G = data.g_matrix() 

 

# initialize matrix of c-vectors if desired and possible 

if data._use_c_vec and (data._C or (data._B.is_skew_symmetric() and (data._cluster or (data._use_g_vec and data._G)) or data._track_mut)): 

self._C = data.c_matrix() 

self._BC = copy(self._M).stack(copy(self._C)) 

else: 

self._BC = copy(self._M) 

 

# initialize matrix of d-vectors if desired and possible 

if data._use_d_vec and (data._D or data._cluster or data._track_mut): 

self._D = data.d_matrix() 

 

self._cluster = copy( data._cluster) 

 

self._b_initial = copy( data._b_initial) 

 

self._mutation_type = copy( data._mutation_type) 

self._description = copy( data._description) 

self._quiver = ClusterQuiver( data._quiver ) if data._quiver else None 

 

# copy all previous booleans 

self._use_fpolys = data._use_fpolys 

self._use_g_vec = data._use_g_vec 

self._use_d_vec = data._use_d_vec 

self._bot_is_c = data._bot_is_c 

self._use_c_vec = data._use_c_vec 

self._track_mut = data._track_mut 

self._is_principal = data._is_principal 

 

# copy all previous dictionaries, names and data 

self._user_labels = copy(data._user_labels) 

self._user_labels_prefix = copy(data._user_labels_prefix) 

self._init_vars = copy(data._init_vars) 

self._init_exch = copy(data._init_exch) 

self._U = copy(data._U) 

self._F = copy(data._F) 

self._R = copy(data._R) 

self._y = copy(data._y) 

self._yhat = copy(data._yhat) 

self._mut_path = copy(data._mut_path) 

 

# constructs a cluster seed from a quiver 

elif isinstance(data, ClusterQuiver): 

quiver = ClusterQuiver(data) 

 

self._M = copy(quiver._M) # B-tilde exchange matrix 

self._M.set_immutable() 

self._n = quiver._n 

self._m = quiver._m 

self._nlist = copy(quiver._nlist) 

self._mlist = copy(quiver._mlist) 

self._B = copy(self._M[:self._n,:self._n]) # Square Part of the B_matrix 

 

# If initializing from a ClusterQuiver rather than a ClusterSeed, the initial B-matrix is reset to be the input B-matrix. 

self._b_initial = copy(self._M) 

self._mutation_type = copy(quiver._mutation_type) 

self._description = 'A seed for a cluster algebra of rank %d' % (self._n) 

self._quiver = quiver 

 

# Sets ``user_labels`` to existing vertex labels 

if not user_labels and set(self._nlist + self._mlist) != set(range(self._n+self._m)): 

user_labels = self._nlist + self._mlist 

if user_labels: 

if isinstance(user_labels, dict): 

labelset = set(user_labels) 

else: 

labelset = set(user_labels) 

# Sanitizes our ``user_labels`` to use Integers instead of ints 

user_labels = [ZZ(x) if x in ZZ else x for x in user_labels] 

if labelset != set(self._nlist + self._mlist) and labelset != set(range(self._n + self._m)): 

raise ValueError('user_labels conflict with both the given' 

' vertex labels and the default labels') 

 

# We are now updating labels from user's most recent choice. 

self._is_principal = is_principal 

self._user_labels = user_labels 

self._user_labels_prefix = user_labels_prefix 

 

# initialize the rest 

 

self._C = matrix.identity(self._n) 

self._use_c_vec = True 

 

self._G = matrix.identity(self._n) 

self._use_g_vec = True 

 

self._BC = copy(self._M).stack(self.c_matrix()) 

self._bot_is_c=False 

 

self._D = -matrix.identity(self._n) 

self._use_d_vec = True 

 

self._mut_path = [ ] 

self._track_mut = True 

 

if user_labels: 

self._sanitize_init_vars(user_labels, user_labels_prefix) 

else: 

xs = {i:'x%s'%i for i in range(self._n)} 

ys = {(i+self._n):'y%s'%i for i in range(self._n+self._m)} 

self._init_vars = copy(xs) 

self._init_vars.update(ys) 

 

self._init_exch = dict(islice(self._init_vars.items(), self._n)) 

self._U = PolynomialRing(QQ,['y%s' % i for i in range(self._n)]) 

self._F = dict([(i,self._U(1)) for i in self._init_exch.values()]) 

self._R = PolynomialRing(QQ,[val for val in self._init_vars.values()]) 

self._y = dict([ (self._U.gen(j),prod([self._R.gen(i)**self._M[i,j] for i in range(self._n,self._n+self._m)])) for j in range(self._n)]) 

self._yhat = dict([ (self._U.gen(j),prod([self._R.gen(i)**self._M[i,j] for i in range(self._n+self._m)])) for j in range(self._n)]) 

#self._cluster = None 

self._use_fpolys = True 

 

# in all other cases, we construct the corresponding ClusterQuiver first 

else: 

quiver = ClusterQuiver(data, frozen=frozen, user_labels=user_labels) 

self.__init__(quiver, frozen=frozen, 

is_principal=is_principal, 

user_labels=user_labels, 

user_labels_prefix=user_labels_prefix) 

 

def use_c_vectors(self, use=True, bot_is_c=False, force=False): 

r""" 

Reconstruct c vectors from other data or initialize if no usable data exists. 

 

Warning: Initialization may lead to inconsistent data. 

 

INPUT: 

 

- ``use`` -- (default:True) If True, will use c vectors 

- ``bot_is_c`` -- (default:False) If True and ClusterSeed self has self._m == self._n, then will assume bottom half of the extended exchange matrix is the c-matrix. If true, lets the ClusterSeed know c-vectors can be calculated. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',4]); 

sage: S.use_c_vectors(False); S.use_g_vectors(False); S.use_fpolys(False); S.track_mutations(False) 

sage: S.use_c_vectors(True) 

Warning: Initializing c-vectors at this point could lead to inconsistent seed data. 

 

sage: S.use_c_vectors(True, force=True) 

sage: S.c_matrix() 

[1 0 0 0] 

[0 1 0 0] 

[0 0 1 0] 

[0 0 0 1] 

 

sage: S = ClusterSeed(['A',4]); 

sage: S.use_c_vectors(False); S.use_g_vectors(False); S.use_fpolys(False); S.track_mutations(False) 

sage: S.mutate(1); 

sage: S.use_c_vectors(True, force=True) 

sage: S.c_matrix() 

[1 0 0 0] 

[0 1 0 0] 

[0 0 1 0] 

[0 0 0 1] 

 

""" 

if self._use_c_vec != use: 

self._use_c_vec = use 

if self._use_c_vec: 

#self._C = matrix.identity(self._n) 

try: 

self._use_c_vec = False # temporarily turns off c-vectors to see if they can be recovered. 

self._C = self.c_matrix() # if not just sets it to be identity matrix, i.e. reinitialized. 

self._BC = copy(self._M).stack(self.c_matrix()) 

self._use_c_vec = True 

except ValueError: 

if not force: 

print("Warning: Initializing c-vectors at this point could lead to inconsistent seed data.") 

else: 

self._use_c_vec = True 

self._C = matrix.identity(self._n) 

self._BC = copy(self._M).stack(self.c_matrix()) 

except AttributeError: 

if not force: 

print("Warning: Initializing c-vectors at this point could lead to inconsistent seed data.") 

else: 

self._use_c_vec = True 

self._C = matrix.identity(self._n) 

self._BC = copy(self._M).stack(self.c_matrix()) 

else: 

self._C = None 

self._BC = copy(self._M) 

if self._bot_is_c != bot_is_c: # If we need to do this. It overrides the previous designations. 

self._bot_is_c = bot_is_c 

if self._bot_is_c: 

self._use_c_vec = True 

if self._m == self._n: # in this case, the second half of a 2n x n matrix is a c-matrix. 

self._C = copy(self._M[self._n:(self._n+self._m),:self._n]) 

self._BC = copy(self._M) 

else: # self._n != self._m 

raise ValueError('There are immutable elements not in the c-matrix. Storing the c-matrix separately.') 

self._C = copy(self._M[self._m:(self._n+self._m),:self._n]) 

self._BC = copy(self._M) 

self._M = self._M[:self._m:self._n] 

self._M.set_immutable() 

self._bot_is_c = False 

 

def use_g_vectors(self, use=True, force=False): 

r""" 

Reconstruct g vectors from other data or initialize if no usable data exists. 

 

Warning: Initialization may lead to inconsistent data. 

 

INPUT: 

 

- ``use`` -- (default:True) If True, will use g vectors 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',4]); 

sage: S.use_g_vectors(False); S.use_fpolys(False) 

sage: S.use_g_vectors(True) 

sage: S.g_matrix() 

[1 0 0 0] 

[0 1 0 0] 

[0 0 1 0] 

[0 0 0 1] 

 

sage: S = ClusterSeed(['A',4]); 

sage: S.use_g_vectors(False); S.use_fpolys(False) 

sage: S.mutate(1); 

sage: S.use_g_vectors(True) 

sage: S.g_matrix() 

[ 1 0 0 0] 

[ 0 -1 0 0] 

[ 0 0 1 0] 

[ 0 0 0 1] 

 

sage: S = ClusterSeed(['A',4]); 

sage: S.use_g_vectors(False); S.use_fpolys(False); S.track_mutations(False) 

sage: S.mutate(1); 

sage: S.use_c_vectors(False) 

sage: S.g_matrix() 

Traceback (most recent call last): 

... 

ValueError: Unable to calculate g-vectors. Need to use g vectors. 

 

sage: S = ClusterSeed(['A',4]); 

sage: S.use_g_vectors(False); S.use_fpolys(False); S.track_mutations(False) 

sage: S.mutate(1); 

sage: S.use_c_vectors(False) 

sage: S.use_g_vectors(True) 

Warning: Initializing g-vectors at this point could lead to inconsistent seed data. 

 

sage: S.use_g_vectors(True, force=True) 

sage: S.g_matrix() 

[1 0 0 0] 

[0 1 0 0] 

[0 0 1 0] 

[0 0 0 1] 

""" 

if self._use_g_vec != use: 

self._use_g_vec = use 

if self._use_g_vec: 

#self._G = matrix.identity(self._n) if self._use_g_vec else None 

try: 

self._use_g_vec = False # temporarily turns off g-vectors to see if they can be recovered. 

self._G = self.g_matrix() # if not just sets it to be identity matrix, i.e. reinitialized. 

self._use_g_vec = True 

except ValueError: 

if not force: 

print("Warning: Initializing g-vectors at this point could lead to inconsistent seed data.") 

else: 

self._use_g_vec = True 

self._G = matrix.identity(self._n) 

except AttributeError: 

if not force: 

print("Warning: Initializing g-vectors at this point could lead to inconsistent seed data.") 

else: 

self._use_g_vec = True 

self._G = matrix.identity(self._n) 

else: 

self._G = None 

 

# Initially coded so c_vectors would be turned back on but now each of these boolean flags are independent 

#if self._use_g_vec and not self._use_c_vec: 

# self.use_c_vectors(True) 

 

def use_d_vectors(self, use=True, force=False): 

r""" 

Reconstruct d vectors from other data or initialize if no usable data exists. 

 

Warning: Initialization may lead to inconsistent data. 

 

INPUT: 

 

- ``use`` -- (default:True) If True, will use d vectors 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',4]); 

sage: S.use_d_vectors(True) 

sage: S.d_matrix() 

[-1 0 0 0] 

[ 0 -1 0 0] 

[ 0 0 -1 0] 

[ 0 0 0 -1] 

 

sage: S = ClusterSeed(['A',4]); S.use_d_vectors(False); S.track_mutations(False); S.mutate(1); S.d_matrix() 

[-1 0 0 0] 

[ 0 1 0 0] 

[ 0 0 -1 0] 

[ 0 0 0 -1] 

sage: S.use_fpolys(False) 

sage: S.d_matrix() 

Traceback (most recent call last): 

... 

ValueError: Unable to calculate d-vectors. Need to use d vectors. 

 

sage: S = ClusterSeed(['A',4]); S.use_d_vectors(False); S.track_mutations(False); S.mutate(1); S.d_matrix() 

[-1 0 0 0] 

[ 0 1 0 0] 

[ 0 0 -1 0] 

[ 0 0 0 -1] 

sage: S.use_fpolys(False) 

sage: S.use_d_vectors(True) 

Warning: Initializing d-vectors at this point could lead to inconsistent seed data. 

 

sage: S.use_d_vectors(True, force=True) 

sage: S.d_matrix() 

[-1 0 0 0] 

[ 0 -1 0 0] 

[ 0 0 -1 0] 

[ 0 0 0 -1] 

 

sage: S = ClusterSeed(['A',4]); S.mutate(1); S.d_matrix() 

[-1 0 0 0] 

[ 0 1 0 0] 

[ 0 0 -1 0] 

[ 0 0 0 -1] 

sage: S = ClusterSeed(['A',4]); S.use_d_vectors(True); S.mutate(1); S.d_matrix() 

[-1 0 0 0] 

[ 0 1 0 0] 

[ 0 0 -1 0] 

[ 0 0 0 -1] 

""" 

if self._use_d_vec != use: 

self._use_d_vec = use 

if self._use_d_vec: 

try: 

self._use_d_vec = False # temporarily turns off d-vectors to see if they can be recovered. 

self._D = self.d_matrix() 

self._use_d_vec = True 

except ValueError: 

if not force: 

print("Warning: Initializing d-vectors at this point could lead to inconsistent seed data.") 

else: 

self._use_d_vec = True # if not just sets it to be negative identity matrix, i.e. reinitialized. 

self._D = -matrix.identity(self._n) 

except AttributeError: 

if not force: 

print("Warning: Initializing d-vectors at this point could lead to inconsistent seed data.") 

else: 

self._use_d_vec = True # if not just sets it to be negative identity matrix, i.e. reinitialized. 

self._D = -matrix.identity(self._n) 

else: 

self._D = None 

 

def use_fpolys(self, use=True, user_labels=None, user_labels_prefix=None): 

r""" 

Use F-polynomials in our Cluster Seed 

 

Note: This will automatically try to recompute the cluster variables if possible 

 

INPUT: 

 

- ``use`` -- (default:True) If True, will use F-polynomials 

- ``user_labels`` -- (default:None) If set will overwrite the default cluster variable labels 

- ``user_labels_prefix`` -- (default:None) If set will overwrite the default 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',4]); S.use_fpolys(False); S._cluster 

sage: S.use_fpolys(True) 

sage: S.cluster() 

[x0, x1, x2, x3] 

 

sage: S = ClusterSeed(['A',4]); S.use_fpolys(False); S.track_mutations(False); S.mutate(1) 

sage: S.use_fpolys(True) 

Traceback (most recent call last): 

... 

ValueError: F-polynomials and Cluster Variables cannot be reconstructed from given data. 

sage: S.cluster() 

Traceback (most recent call last): 

... 

ValueError: Clusters not being tracked 

 

""" 

if user_labels: 

self._user_labels = user_labels 

if user_labels_prefix: 

self._user_labels_prefix = user_labels_prefix 

if self._use_fpolys != use: 

self._use_fpolys = use 

 

if self._use_fpolys: 

 

if user_labels: 

self._sanitize_init_vars(user_labels, user_labels_prefix) 

else: 

xs = {i:'x%s'%i for i in range(self._n)} 

ys = {(i+self._n):'y%s'%i for i in range(self._n+self._m)} 

self._init_vars = copy(xs) 

self._init_vars.update(ys) 

 

if self._G == matrix.identity(self._n): # If we are at the root 

if not self._use_g_vec: 

self.use_g_vectors(True) 

self._init_exch = dict(islice(self._init_vars.items(), self._n)) 

self._U = PolynomialRing(QQ,['y%s' % i for i in range(self._n)]) 

self._F = dict([(i,self._U(1)) for i in self._init_exch.values()]) 

self._R = PolynomialRing(QQ,[val for val in self._init_vars.values()]) 

self._y = dict([ (self._U.gen(j),prod([self._R.gen(i)**self._M[i,j] for i in range(self._n,self._n+self._m)])) for j in range(self._n)]) 

self._yhat = dict([ (self._U.gen(j),prod([self._R.gen(i)**self._M[i,j] for i in range(self._n+self._m)])) for j in range(self._n)]) 

elif self._cluster: 

raise ValueError("should not be possible to have cluster variables without f-polynomials") # added this as a sanity check. This error should never appear however. 

elif self._track_mut: # If we can navigate from the root to where we are 

if not self._use_g_vec: 

self.use_g_vectors(True) 

catchup = ClusterSeed(self._b_initial, user_labels=user_labels, user_labels_prefix=user_labels_prefix) 

catchup.use_c_vectors(use=self._use_c_vec,bot_is_c=self._bot_is_c) 

catchup.mutate(self.mutations()) 

 

self._init_exch = catchup._init_exch 

self._U = catchup._U 

self._F = catchup._F 

self._R = catchup._R 

self._y = catchup._y 

self._yhat = catchup._yhat 

else: 

self._use_fpolys = False 

self._cluster = None 

raise ValueError("F-polynomials and Cluster Variables cannot be reconstructed from given data.") 

 

# since we have F polynomials, set up clusters properly 

self._cluster = None 

self.cluster() 

else: 

if user_labels: 

print("Warning: since 'use_fpolys' is False, the parameter 'user_labels' is ignored.") 

self._init_vars = None 

self._init_exch = None 

self._U = None 

self._F = None 

self._R = None 

self._y = None 

self._yhat = None 

self._cluster = None 

 

def track_mutations(self, use=True): 

r""" 

Begins tracking the mutation path. 

 

Warning: May initialize all other data to ensure that all c, d, and g vectors agree on the start of mutations. 

 

INPUT: 

 

- ``use`` -- (default:True) If True, will begin filling the mutation path 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',4]); S.track_mutations(False) 

sage: S.mutate(0) 

sage: S.mutations() 

Traceback (most recent call last): 

... 

ValueError: Not recording mutation sequence. Need to track mutations. 

sage: S.track_mutations(True) 

sage: S.g_matrix() 

[1 0 0 0] 

[0 1 0 0] 

[0 0 1 0] 

[0 0 0 1] 

 

sage: S.mutate([0,1]) 

sage: S.mutations() 

[0, 1] 

""" 

 

if self._track_mut != use: 

self._track_mut = use 

if self._track_mut: 

self._b_initial = self.b_matrix() 

self._mut_path = [] 

use_c = self._use_c_vec 

use_d = self._use_d_vec 

use_g = self._use_g_vec 

# use_f = self._use_fpolys #### also reinitialize F polynomials? -J 

# if use_f: 

# self.use_fpolys(False) 

if use_g: 

self.use_g_vectors(False) 

if use_c: 

self.use_c_vectors(False) 

if use_d: 

self.use_d_vectors(False) 

if use_g: 

self.use_g_vectors() 

if use_c: 

self.use_c_vectors() 

if use_d: 

self.use_d_vectors() 

# if use_f: 

# self.use_fpolys() 

else: 

self._mut_path = None 

 

def _sanitize_init_vars(self, user_labels, user_labels_prefix = 'x'): 

r""" 

Rewrite a user-given set of cluster variable names into a format that Sage can utilize. 

 

INPUT: 

 

- ``user_labels`` -- The labels that need sanitizing 

- ``user_labels_prefix`` -- (default:'x') The prefix to use for labels if integers given for labels 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A', 4]); S._init_vars 

{0: 'x0', 1: 'x1', 2: 'x2', 3: 'x3', 4: 'y0', 5: 'y1', 6: 'y2', 7: 'y3'} 

sage: S._sanitize_init_vars([1, 2, 3, 4], 'z') 

sage: S._init_vars 

{0: 'z1', 1: 'z2', 2: 'z3', 3: 'z4'} 

 

sage: S = ClusterSeed(['A', 4]); S._init_vars 

{0: 'x0', 1: 'x1', 2: 'x2', 3: 'x3', 4: 'y0', 5: 'y1', 6: 'y2', 7: 'y3'} 

sage: S._sanitize_init_vars(['a', 'b', 'c', 'd']) 

sage: S._init_vars 

{0: 'a', 1: 'b', 2: 'c', 3: 'd'} 

""" 

self._init_vars = {} 

if isinstance(user_labels, list): 

for i in range(len(user_labels)): 

if isinstance(user_labels[i], Integer): 

if user_labels[i] >= 0: 

self._init_vars[i] = user_labels_prefix + user_labels[i].str() 

else: 

self._init_vars[i] = user_labels_prefix + 'neg' + (-user_labels[i]).str() 

elif isinstance(user_labels[i], (list, tuple)): 

self._user_labels_prefix = user_labels_prefix 

strng = self._user_labels_prefix 

for j in user_labels[i]: 

if isinstance(j, Integer): 

if j >= 0: 

strng = strng + "_" + j.str() 

else: 

strng = strng + "_" + 'neg' + (-j).str() 

else: 

strng = strng + "_" + j 

self._init_vars[i] = strng 

else: 

self._init_vars[i] = user_labels[i] 

elif isinstance(user_labels, dict): 

for key in user_labels: 

if isinstance(user_labels[key], (list, tuple)): 

self._user_labels_prefix = user_labels_prefix 

strng = self._user_labels_prefix 

for j in user_labels[key]: 

if isinstance(j, Integer): 

if j >= 0: 

strng = strng + "_" + j.str() 

else: 

strng = strng + "_" + 'neg' + (-j).str() 

else: 

strng = strng + "_" + j 

self._init_vars[key] = strng 

else: 

self._init_vars[key] = user_labels[key] 

else: 

raise ValueError("the input 'user_labels' must be a dictionary or a list") 

 

if len(self._init_vars.keys()) != self._n + self._m: 

raise ValueError("the number of user-defined labels is not the" 

" number of exchangeable and frozen variables") 

 

def set_c_matrix(self, data): 

r""" 

Will force set the c matrix according to a matrix, a quiver, or a seed. 

 

INPUT: 

 

- ``data`` -- The matrix to set the c matrix to. Also allowed 

to be a quiver or cluster seed, in which case the b_matrix 

is used. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]); 

sage: X = matrix([[0,0,1],[0,1,0],[1,0,0]]) 

sage: S.set_c_matrix(X) 

sage: S.c_matrix() 

[0 0 1] 

[0 1 0] 

[1 0 0] 

 

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

sage: S.set_c_matrix(Y) 

C matrix does not look to be valid - there exists a column containing positive and negative entries. 

Continuing... 

 

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

sage: S.set_c_matrix(Z) 

C matrix does not look to be valid - not a linearly independent set. 

Continuing... 

""" 

if isinstance(data, ClusterQuiver): 

data = data.b_matrix() 

if isinstance(matrix, ClusterSeed): 

data=data.b_matrix() 

 

if data.determinant() == 0: 

print("C matrix does not look to be valid - not a linearly independent set.") 

print("Continuing...") 

 

# Do a quick check to make sure that each column is either all 

# positive or all negative. 

# Can do this through green/red vertices 

greens = Set(get_green_vertices(data)) 

reds = Set(get_red_vertices(data)) 

if greens.intersection(reds) or greens.union(reds).cardinality() < data.ncols(): 

print("C matrix does not look to be valid - there exists a column containing positive and negative entries.") 

print("Continuing...") 

 

self._C = data 

self._BC = copy(self._M.stack(self._C)) 

 

def __eq__(self, other): 

r""" 

Returns True iff ``self`` represent the same cluster seed as ``other`` and all tracked data agrees. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',5]) 

sage: T = S.mutate( 2, inplace=False ) 

sage: S.__eq__( T ) 

False 

 

sage: T.mutate( 2 ) 

sage: S.__eq__( T ) 

True 

 

sage: S = ClusterSeed(['A',2]) 

sage: T = ClusterSeed(S) 

sage: S.__eq__( T ) 

True 

 

sage: S.mutate([0,1,0,1,0]) 

sage: S.__eq__( T ) 

False 

sage: S.cluster() 

[x1, x0] 

sage: T.cluster() 

[x0, x1] 

 

sage: S.mutate([0,1,0,1,0]) 

sage: S.__eq__( T ) 

True 

sage: S.cluster() 

[x0, x1] 

""" 

if not isinstance(other, ClusterSeed): 

return False 

g_vec = True 

c_vec = True 

d_vec = True 

clusters = True 

ExMat = self._M == other._M 

if self._use_fpolys and other._use_fpolys: 

clusters = self.cluster() == other.cluster() and self.ground_field() == other.ground_field() 

elif self._use_g_vec and other._use_g_vec: 

g_vec = self.g_matrix() == other.g_matrix() 

if self._use_c_vec and other._use_c_vec: 

c_vec = self.c_matrix() == other.c_matrix() 

if self._use_d_vec and other._use_d_vec: 

d_vec = self.d_matrix() == other.d_matrix() 

return g_vec and c_vec and d_vec and clusters and ExMat 

 

def __hash__(self): 

""" 

Return a hash of ``self``. 

 

EXAMPLES:: 

 

sage: Q = ClusterSeed(['A',5]) 

sage: hash(Q) # indirect doctest 

-5649412990944896369 # 64-bit 

222337679 # 32-bit 

""" 

# mat_hash = self._M.__hash__() 

if self._use_fpolys: 

return hash(tuple(self.cluster())) 

elif self._use_g_vec: 

return hash(self.g_matrix()) 

elif self._use_c_vec: 

return hash(self.c_matrix()) 

elif self._use_d_vec: 

return hash(self.d_matrix()) 

 

def _repr_(self): 

r""" 

Returns the description of ``self``. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',5]) 

sage: S._repr_() 

"A seed for a cluster algebra of rank 5 of type ['A', 5]" 

 

sage: S=ClusterSeed(['B',2]) 

sage: T=S.principal_extension() 

sage: T._repr_() 

"A seed for a cluster algebra of rank 2 of type ['B', 2] with principal coefficients" 

""" 

name = self._description 

if self._mutation_type: 

if type( self._mutation_type ) in [QuiverMutationType_Irreducible,QuiverMutationType_Reducible]: 

name += ' of type ' + str(self._mutation_type) 

# the following case allows description of 'undetermined finite mutation type' 

else: 

name += ' of ' + self._mutation_type 

if self._is_principal: 

name += ' with principal coefficients' 

elif self._m == 1: 

name += ' with %s frozen variable'%self._m 

elif self._m > 1: 

name += ' with %s frozen variables'%self._m 

return name 

 

def plot(self, circular=False, mark=None, save_pos=False, force_c =False, with_greens=False, add_labels = False): 

r""" 

Returns the plot of the quiver of ``self``. 

 

INPUT: 

 

- ``circular`` -- (default:False) if True, the circular plot is chosen, otherwise >>spring<< is used. 

- ``mark`` -- (default: None) if set to i, the vertex i is highlighted. 

- ``save_pos`` -- (default:False) if True, the positions of the vertices are saved. 

- ``force_c`` -- (default:False) if True, will show the frozen vertices even if they were never initialized 

- ``with_greens`` -- (default:False) if True, will display the green vertices in green 

- ``add_labels`` -- (default:False) if True, will use the initial variables as labels 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',5]) 

sage: pl = S.plot() 

sage: pl = S.plot(circular=True) 

""" 

 

greens = [] 

if with_greens: 

greens = self.green_vertices() 

 

if force_c: 

quiver = ClusterQuiver(self._BC) 

elif add_labels: 

# relabelling multiple times causes errors, so we need to always do it in place 

quiver = self.quiver().relabel(self._init_vars, inplace=True) 

else: 

quiver = self.quiver() 

 

return quiver.plot(circular=circular,mark=mark,save_pos=save_pos, greens=greens) 

 

def show(self, fig_size=1, circular=False, mark=None, save_pos=False, force_c = False, with_greens= False, add_labels = False): 

r""" 

Shows the plot of the quiver of ``self``. 

 

INPUT: 

 

- ``fig_size`` -- (default: 1) factor by which the size of the plot is multiplied. 

- ``circular`` -- (default: False) if True, the circular plot is chosen, otherwise >>spring<< is used. 

- ``mark`` -- (default: None) if set to i, the vertex i is highlighted. 

- ``save_pos`` -- (default:False) if True, the positions of the vertices are saved. 

- ``force_c`` -- (default:False) if True, will show the frozen vertices even if they were never initialized 

- ``with_greens`` -- (default:False) if True, will display the green vertices in green 

- ``add_labels`` -- (default:False) if True, will use the initial variables as labels 

 

TESTS:: 

 

sage: S = ClusterSeed(['A',5]) 

sage: S.show() # long time 

""" 

 

greens = [] 

if with_greens: 

greens = self.green_vertices() 

 

if force_c: 

quiver = ClusterQuiver(self._BC) 

elif add_labels: 

# relabelling multiple times causes errors, so we need to always do it in place 

quiver = self.quiver().relabel(self._init_vars, inplace=True) 

else: 

quiver = self.quiver() 

 

quiver.show(fig_size=fig_size, circular=circular,mark=mark,save_pos=save_pos, greens=greens) 

 

def interact(self, fig_size=1, circular=True): 

r""" 

Only in *notebook mode*. Starts an interactive window for cluster seed mutations. 

 

INPUT: 

 

- ``fig_size`` -- (default: 1) factor by which the size of the plot is multiplied. 

- ``circular`` -- (default: True) if True, the circular plot is chosen, otherwise >>spring<< is used. 

 

TESTS:: 

 

sage: S = ClusterSeed(['A',4]) 

sage: S.interact() # long time 

'The interactive mode only runs in the Sage notebook.' 

""" 

# Also update so works in cloud and not just notebook 

from sage.plot.plot import EMBEDDED_MODE 

from sagenb.notebook.interact import interact, selector 

from sage.misc.all import html,latex 

from sage.repl.rich_output.pretty_print import pretty_print 

 

if not EMBEDDED_MODE: 

return "The interactive mode only runs in the Sage notebook." 

else: 

seq = [] 

sft = [True] 

sss = [True] 

ssv = [True] 

ssm = [True] 

ssl = [True] 

@interact 

def player(k=selector(values=list(range(self._n)), nrows = 1, 

label='Mutate at: '), 

show_seq=("Mutation sequence:", True), 

show_vars=("Cluster variables:", True), 

show_matrix=("B-Matrix:", True), 

show_lastmutation=("Show last mutation:", True)): 

ft, ss, sv, sm, sl = sft.pop(), sss.pop(), ssv.pop(), ssm.pop(), ssl.pop() 

if ft: 

self.show(fig_size=fig_size, circular=circular) 

elif show_seq is not ss or show_vars is not sv or show_matrix is not sm or show_lastmutation is not sl: 

if seq and show_lastmutation: 

self.show(fig_size=fig_size, circular=circular, 

mark=seq[len(seq) - 1]) 

else: 

self.show(fig_size=fig_size, circular=circular ) 

else: 

self.mutate(k) 

seq.append(k) 

if not show_lastmutation: 

self.show(fig_size=fig_size, circular=circular) 

else: 

self.show(fig_size=fig_size, circular=circular, mark=k) 

sft.append(False) 

sss.append(show_seq) 

ssv.append(show_vars) 

ssm.append(show_matrix) 

ssl.append(show_lastmutation) 

if show_seq: 

pretty_print(html("Mutation sequence: $" + str( [ seq[i] for i in range(len(seq)) ] ).strip('[]') + "$")) 

if show_vars: 

pretty_print(html("Cluster variables:")) 

table = "$\\begin{align*}\n" 

for i in range(self._n): 

table += "\tv_{%s} &= " % i + latex(self.cluster_variable(i)) + "\\\\ \\\\\n" 

table += "\\end{align*}$" 

pretty_print(html("$ $")) 

pretty_print(html(table)) 

pretty_print(html("$ $")) 

if show_matrix: 

pretty_print(html("B-Matrix:")) 

pretty_print(html(self._M)) 

pretty_print(html("$ $")) 

 

def save_image(self, filename, circular=False, mark=None, save_pos=False): 

r""" 

Saves the plot of the underlying digraph of the quiver of ``self``. 

 

INPUT: 

 

- ``filename`` -- the filename the image is saved to. 

- ``circular`` -- (default: False) if True, the circular plot is chosen, otherwise >>spring<< is used. 

- ``mark`` -- (default: None) if set to i, the vertex i is highlighted. 

- ``save_pos`` -- (default:False) if True, the positions of the vertices are saved. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['F',4,[1,2]]) 

sage: S.save_image(os.path.join(SAGE_TMP, 'sage.png')) 

""" 

graph_plot = self.plot( circular=circular, mark=mark, save_pos=save_pos) 

graph_plot.save( filename=filename ) 

 

def b_matrix(self): 

r""" 

Returns the `B` *-matrix* of ``self``. 

 

EXAMPLES:: 

 

sage: ClusterSeed(['A',4]).b_matrix() 

[ 0 1 0 0] 

[-1 0 -1 0] 

[ 0 1 0 1] 

[ 0 0 -1 0] 

 

sage: ClusterSeed(['B',4]).b_matrix() 

[ 0 1 0 0] 

[-1 0 -1 0] 

[ 0 1 0 1] 

[ 0 0 -2 0] 

 

sage: ClusterSeed(['D',4]).b_matrix() 

[ 0 1 0 0] 

[-1 0 -1 -1] 

[ 0 1 0 0] 

[ 0 1 0 0] 

 

sage: ClusterSeed(QuiverMutationType([['A',2],['B',2]])).b_matrix() 

[ 0 1 0 0] 

[-1 0 0 0] 

[ 0 0 0 1] 

[ 0 0 -2 0] 

""" 

return copy(self._M) 

 

def ground_field(self): 

r""" 

Returns the *ground field* of the cluster of ``self``. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]) 

sage: S.ground_field() 

Multivariate Polynomial Ring in x0, x1, x2, y0, y1, y2 over Rational Field 

""" 

return self._R 

 

def x(self, k): 

r""" 

Return the `k` *-th initial cluster variable* for the associated cluster seed, 

or the cluster variable of the corresponding vertex in self.quiver. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A', 3]) 

sage: S.mutate([2, 1]) 

sage: S.x(0) 

x0 

 

sage: S.x(1) 

x1 

 

sage: S.x(2) 

x2 

 

sage: S = ClusterSeed(DiGraph([['a', 'b'], ['b', 'c']]), frozen = ['c']) 

sage: S.x(0) 

a 

sage: S.x('a') 

a 

""" 

 

if self._use_fpolys: 

if k in range(self._n): 

x = self._R.gens()[k] 

elif k in self._nlist: 

x = self._R.gens()[self._nlist.index(k)] 

 

return ClusterVariable(FractionField(self._R), 

x.numerator(), x.denominator(), 

mutation_type=self._mutation_type, 

variable_type='cluster variable', 

xdim=self._n) 

else: 

raise ValueError("The input is not in an index of a cluster variable.") 

 

def y(self, k): 

r""" 

Return the `k` *-th initial coefficient (frozen variable)* for the \ 

associated cluster seed, or the cluster variable of the corresponding \ 

vertex in self.quiver. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A', 3]).principal_extension() 

sage: S.mutate([2, 1]) 

sage: S.y(0) 

y0 

 

sage: S.y(1) 

y1 

 

sage: S.y(2) 

y2 

 

sage: S = ClusterSeed(DiGraph([['a', 'b'], ['b', 'c']]), frozen = ['c']) 

sage: S.y(0) 

c 

sage: S.y('c') 

c 

""" 

 

if self._use_fpolys: 

if k in range(self._m): 

x = self._R.gens()[self._n + k] 

elif k in self._mlist: 

x = self._R.gens()[self._mlist.index(k) + self._n] 

return ClusterVariable(FractionField(self._R), 

x.numerator(), x.denominator(), 

mutation_type=self._mutation_type, 

variable_type='frozen variable', 

xdim=self._n) 

else: 

raise ValueError("The input is not in an index of a frozen variable.") 

 

def n(self): 

r""" 

Return the number of *exchangeable variables* of ``self``. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A', 3]) 

sage: S.n() 

3 

""" 

return self._n 

 

def m(self): 

r""" 

Returns the number of *frozen variables* of ``self``. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]) 

sage: S.n() 

3 

 

sage: S.m() 

0 

 

sage: S = S.principal_extension() 

sage: S.m() 

3 

""" 

return self._m 

 

def free_vertices(self): 

r""" 

Return the list of *exchangable vertices* of ``self``. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(DiGraph([['a', 'b'], ['c', 'b'], ['c', 'd'], ['e', 'd']]), \ 

frozen = ['b', 'd']) 

sage: S.free_vertices() 

['a', 'c', 'e'] 

 

sage: S=ClusterSeed(DiGraph([[5, 'b']])) 

sage: S.free_vertices() 

[5, 'b'] 

""" 

return self._nlist 

 

def frozen_vertices(self): 

r""" 

Return the list of *frozen vertices* of ``self``. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(DiGraph([['a', 'b'], ['c', 'b'], ['c', 'd'], ['e', 'd']]), \ 

frozen = ['b', 'd']) 

sage: S.frozen_vertices() 

['b', 'd'] 

""" 

return self._mlist 

 

def mutations(self): 

r""" 

Returns the list of mutations ``self`` has undergone if they are being tracked. 

 

Examples:: 

 

sage: S = ClusterSeed(['A',3]) 

sage: S.mutations() 

[] 

 

sage: S.mutate([0,1,0,2]) 

sage: S.mutations() 

[0, 1, 0, 2] 

 

sage: S.track_mutations(False) 

sage: S.mutations() 

Traceback (most recent call last): 

... 

ValueError: Not recording mutation sequence. Need to track mutations. 

""" 

if self._track_mut: 

return copy(self._mut_path) 

else: 

raise ValueError("Not recording mutation sequence. Need to track mutations.") 

 

def cluster_variable(self, k): 

r""" 

Generates a cluster variable using F-polynomials 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]) 

sage: S.mutate([0,1]) 

sage: S.cluster_variable(0) 

(x1 + 1)/x0 

sage: S.cluster_variable(1) 

(x0*x2 + x1 + 1)/(x0*x1) 

""" 

if self._use_fpolys: 

IE = self._init_exch.values() 

if (k in range(self._n)) or (k in IE): 

if k in range(self._n): 

pass 

elif k in IE: 

k = IE.index(k) 

 

g_mon = prod([self._R.gen(i)**self._G[i,k] for i in range(self._n)]) 

F_num = self._F[IE[k]].subs(self._yhat) 

F_den = self._R(self._F[IE[k]].subs(self._y).denominator()) 

cluster_variable = g_mon*F_num*F_den 

 

return ClusterVariable(FractionField(self._R), cluster_variable.numerator(), cluster_variable.denominator(), mutation_type=self._mutation_type, variable_type='cluster variable',xdim=self._n) 

else: 

raise ValueError('No cluster variable with index or label ' + str(k) + '.') 

elif self._track_mut: # if we can recreate the clusters 

catchup = ClusterSeed(self._b_initial, user_labels=self._user_labels, user_labels_prefix=self._user_labels_prefix) 

catchup.use_c_vectors(use=self._use_c_vec, bot_is_c=self._bot_is_c) 

catchup.mutate(self.mutations()) 

return catchup.cluster_variable(k) 

else: 

raise ValueError('Clusters not being tracked') 

return None 

 

def cluster(self): 

r""" 

Returns a copy of the *cluster* of ``self``. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]) 

sage: S.cluster() 

[x0, x1, x2] 

 

sage: S.mutate(1) 

sage: S.cluster() 

[x0, (x0*x2 + 1)/x1, x2] 

 

sage: S.mutate(2) 

sage: S.cluster() 

[x0, (x0*x2 + 1)/x1, (x0*x2 + x1 + 1)/(x1*x2)] 

 

sage: S.mutate([2,1]) 

sage: S.cluster() 

[x0, x1, x2] 

""" 

 

if not self._use_fpolys: 

if self._track_mut: # if we can recreate the clusters 

catchup = ClusterSeed(self._b_initial, user_labels=self._user_labels, user_labels_prefix=self._user_labels_prefix) 

catchup.use_c_vectors(use=self._use_c_vec, bot_is_c=self._bot_is_c) 

catchup.mutate(self.mutations()) 

return catchup.cluster() 

else: 

raise ValueError('Clusters not being tracked') 

elif self._cluster is None: 

self._cluster = [self.cluster_variable(k) for k in range(self._n)] 

return copy(self._cluster) 

 

def _f_mutate( self, k): 

r""" 

An internal procedure that returns ``self`` with F-polynomials mutated at k. 

 

WARNING: This function assumes you are sending it good data 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]) 

sage: S._f_mutate(0) 

sage: S.f_polynomial(0) 

y0 + 1 

""" 

if self._use_fpolys: 

IE = self._init_exch.values() 

else: 

IE = [] 

 

F = self._F 

B = self.b_matrix() 

C = self.c_matrix() 

 

# F-polynomials 

pos = self._U(1) 

neg = self._U(1) 

 

for j in range(self._n): 

if C[j,k] > 0: 

pos *= self._U.gen(j)**C[j,k] 

else: 

neg *= self._U.gen(j)**(-C[j,k]) 

if B[j,k] > 0: 

pos *= F[IE[j]]**B[j,k] 

else: 

neg *= F[IE[j]]**(-B[j,k]) 

 

# can the following be improved? 

self._F[IE[k]] = (pos+neg)//F[IE[k]] 

 

def f_polynomial(self,k): 

r""" 

Return the ``k``-th *F-polynomial* of ``self``. It is obtained from the 

``k``-th cluster variable by setting all `x_i` to `1`. 

 

Warning: this method assumes the sign-coherence conjecture and that the 

input seed is sign-coherent (has an exchange matrix with columns of like signs). 

Otherwise, computational errors might arise. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]).principal_extension() 

sage: S.mutate([2,1,2]) 

sage: [S.f_polynomial(k) for k in range(3)] 

[1, y1*y2 + y2 + 1, y1 + 1] 

 

sage: S = ClusterSeed(Matrix([[0,1],[-1,0],[1,0],[-1,1]])); S.use_c_vectors(bot_is_c=True); S 

A seed for a cluster algebra of rank 2 with 2 frozen variables 

sage: T = ClusterSeed(Matrix([[0,1],[-1,0]])).principal_extension(); T 

A seed for a cluster algebra of rank 2 with principal coefficients 

sage: S.mutate(0) 

sage: T.mutate(0) 

sage: S.f_polynomials() 

[y0 + y1, 1] 

sage: T.f_polynomials() 

[y0 + 1, 1] 

""" 

if self._use_fpolys: 

IE = self._init_exch.values() 

if k in range(self._n): 

pass 

elif k in IE: 

k = IE.index(k) 

else: 

raise ValueError("The cluster seed does not have a cluster variable of index %s."%k) 

 

return self._F[IE[k]] 

elif self._track_mut: 

catchup = ClusterSeed(self._b_initial, user_labels=self._user_labels, user_labels_prefix=self._user_labels_prefix) 

catchup.use_c_vectors(use=self._use_c_vec, bot_is_c=self._bot_is_c) 

catchup.mutate(self.mutations()) 

 

return catchup.f_polynomial(k) 

else: 

raise ValueError("Turn on use_fpolys to get F polynomial %s."%k) 

 

def f_polynomials(self): 

r""" 

Return all *F-polynomials* of ``self``. These are obtained from the 

cluster variables by setting all `x_i`'s to `1`. 

 

Warning: this method assumes the sign-coherence conjecture and that the 

input seed is sign-coherent (has an exchange matrix with columns of like signs). 

Otherwise, computational errors might arise. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]).principal_extension() 

sage: S.mutate([2,1,2]) 

sage: S.f_polynomials() 

[1, y1*y2 + y2 + 1, y1 + 1] 

""" 

 

return [self.f_polynomial(i) for i in range(self._n)] 

 

def g_vector(self,k): 

r""" 

Return the ``k``-th *g-vector* of ``self``. This is the degree vector 

of the ``k``-th cluster variable after setting all `y_i`'s to `0`. 

 

Warning: this method assumes the sign-coherence conjecture and that the 

input seed is sign-coherent (has an exchange matrix with columns of like signs). 

Otherwise, computational errors might arise. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]).principal_extension() 

sage: S.mutate([2,1,2]) 

sage: [ S.g_vector(k) for k in range(3) ] 

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

""" 

 

if not (self._is_principal or self._use_g_vec or (self._use_fpolys and self._cluster)): 

raise ValueError("Unable to calculate g-vectors. Need to use g vectors.") 

if k not in range(self._n): 

raise ValueError("The cluster seed does not have a cluster variable of index %s."%k) 

 

if self._use_g_vec: # This implies the g-matrix is maintained by the mutate function and will always be up to date 

return copy(self._G.column(k)) 

elif self._use_fpolys and self._cluster: 

f = copy(self.cluster_variable(k)) 

eval_dict = dict( [ ( self.y(i), 0 ) for i in range(self._m) ] ) 

f0 = f.subs(eval_dict) 

d1 = f0.numerator().degrees() 

d2 = f0.denominator().degrees() 

return tuple( d1[i] - d2[i] for i in range(self._n) ) 

else: # in the is_principal=True case 

try: 

# ensure that we cannot create a loop by calling g_matrix() here by filtering out loop causing conditions in the previous if-elif sections 

return self.g_matrix().column(k) 

except ValueError: 

raise ValueError("Unable to calculate g-vectors. Need to use g vectors.") 

 

def g_matrix(self, show_warnings=True): 

r""" 

Return the matrix of all *g-vectors* of ``self``. These are the degree 

vectors of the cluster variables after setting all `y_i`'s to `0`. 

 

Warning: this method assumes the sign-coherence conjecture and that the 

input seed is sign-coherent (has an exchange matrix with columns of like signs). 

Otherwise, computational errors might arise. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]).principal_extension() 

sage: S.mutate([2,1,2]) 

sage: S.g_matrix() 

[ 1 0 0] 

[ 0 0 -1] 

[ 0 -1 0] 

 

sage: S = ClusterSeed(['A',3]) 

sage: S.mutate([0,1]) 

sage: S.g_matrix() 

[-1 -1 0] 

[ 1 0 0] 

[ 0 0 1] 

 

sage: S = ClusterSeed(['A',4]); S.use_g_vectors(False); S.use_fpolys(False); S.g_matrix() 

[1 0 0 0] 

[0 1 0 0] 

[0 0 1 0] 

[0 0 0 1] 

 

sage: S = ClusterSeed(['A',4]) 

sage: S.use_g_vectors(False); S.use_c_vectors(False); S.use_fpolys(False); S.track_mutations(False); S.g_matrix() 

Traceback (most recent call last): 

... 

ValueError: Unable to calculate g-vectors. Need to use g vectors. 

""" 

 

from sage.matrix.all import matrix 

if self._use_g_vec: 

return copy(self._G) 

elif self._use_fpolys and self._cluster: # This only calls g_vector when it will not create a loop. 

return matrix( [ self.g_vector(k) for k in range(self._n) ] ).transpose() 

elif self._use_c_vec: 

if self.b_matrix().is_skew_symmetric(): 

return copy(self._C).inverse().transpose() 

elif self._track_mut: 

BC1 = copy(self._b_initial[0:self._n]) 

BC1 = -BC1.transpose() 

BC1 = BC1.stack(matrix.identity(self._n)) 

seq = iter(self.mutations()) 

for k in seq: 

BC1.mutate(k) 

return copy(BC1[self._n:2*self._n]).inverse().transpose() 

else: 

raise ValueError("Unable to calculate g-vectors. Need to use g vectors.") 

elif self._track_mut: 

catchup = ClusterSeed(self._b_initial) 

catchup.use_fpolys(False) 

catchup.mutate(self.mutations()) 

return catchup.g_matrix() 

elif show_warnings: 

raise ValueError("Unable to calculate g-vectors. Need to use g vectors.") 

else: 

return None 

 

def _g_mutate(self, k): 

r""" 

An internal procedure that returns ``self`` with g-vectors mutated at k. 

 

.. WARNING:: 

 

This function assumes you are sending it good data. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]) 

sage: S._g_mutate(0) 

sage: S.g_vector(0) 

(-1, 1, 0) 

 

REFERENCES: 

 

[NZ2012]_ 

""" 

from sage.matrix.all import identity_matrix 

 

if self._use_fpolys: 

IE = self._init_exch.values() 

else: 

IE = [] 

 

B = self.b_matrix() 

C = self.c_matrix() 

 

# G-matrix 

J = identity_matrix(self._n) 

if any(x > 0 for x in C.column(k)): 

eps = +1 

else: 

eps = -1 

for j in range(self._n): 

J[j,k] += max(0, -eps*B[j,k]) 

J[k,k] = -1 

self._G = self._G*J 

 

def c_vector(self,k): 

r""" 

Return the ``k``-th *c-vector* of ``self``. It is obtained as the 

``k``-th column vector of the bottom part of the ``B``-matrix 

of ``self``. 

 

Warning: this method assumes the sign-coherence conjecture and that the 

input seed is sign-coherent (has an exchange matrix with columns of like signs). 

Otherwise, computational errors might arise. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]).principal_extension() 

sage: S.mutate([2,1,2]) 

sage: [ S.c_vector(k) for k in range(3) ] 

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

 

sage: S = ClusterSeed(Matrix([[0,1],[-1,0],[1,0],[-1,1]])); S 

A seed for a cluster algebra of rank 2 with 2 frozen variables 

sage: S.c_vector(0) 

(1, 0) 

 

sage: S = ClusterSeed(Matrix([[0,1],[-1,0],[1,0],[-1,1]])); S.use_c_vectors(bot_is_c=True); S 

A seed for a cluster algebra of rank 2 with 2 frozen variables 

sage: S.c_vector(0) 

(1, -1) 

 

""" 

if k not in range(self._n): 

raise ValueError("The cluster seed does not have a c-vector of index %s."%k) 

if not (self._is_principal or self._use_c_vec): 

raise ValueError("Requires C vectors to use.") 

if self._use_c_vec: 

return self.c_matrix().column(k) 

else: 

return tuple( self._M[i,k] for i in range(self._n,self._n+self._m) ) 

 

def c_matrix(self,show_warnings=True): 

r""" 

Return all *c-vectors* of ``self``. 

 

Warning: this method assumes the sign-coherence conjecture and that the 

input seed is sign-coherent (has an exchange matrix with columns of like signs). 

Otherwise, computational errors might arise. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]).principal_extension() 

sage: S.mutate([2,1,2]) 

sage: S.c_matrix() 

[ 1 0 0] 

[ 0 0 -1] 

[ 0 -1 0] 

 

sage: S = ClusterSeed(['A',4]); 

sage: S.use_g_vectors(False); S.use_fpolys(False); S.use_c_vectors(False); S.use_d_vectors(False); S.track_mutations(False);  

sage: S.c_matrix() 

Traceback (most recent call last): 

... 

ValueError: Unable to calculate c-vectors. Need to use c vectors. 

""" 

 

if self._bot_is_c: 

return copy(self._M[self._m:(self._n+self._m),:self._n]) 

elif self._use_c_vec: 

return copy(self._C) 

elif self._use_g_vec or self._use_fpolys: #both of these will populate g_matrix() successfully 

if self.b_matrix().is_skew_symmetric(): 

return self.g_matrix().inverse().transpose() 

elif self._track_mut: 

BC1 = copy(self._b_initial[0:self._n]) 

BC1 = BC1.stack(matrix.identity(self._n)) 

seq = iter(self.mutations()) 

for k in seq: 

BC1.mutate(k) 

return copy(BC1[self._n:2*self._n]) 

else: 

raise ValueError("Unable to calculate c-vectors. Need to use c vectors.") 

elif self._track_mut: 

BC1 = copy(self._b_initial[0:self._n]) 

BC1 = BC1.stack(matrix.identity(self._n)) 

seq = iter(self.mutations()) 

for k in seq: 

BC1.mutate(k) 

return copy(BC1[self._n:2*self._n]) 

elif show_warnings: 

raise ValueError("Unable to calculate c-vectors. Need to use c vectors.") 

else: 

return None 

 

def d_vector(self, k): 

r""" 

Return the ``k``-th *d-vector* of ``self``. This is the exponent vector 

of the denominator of the ``k``-th cluster variable. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]) 

sage: S.mutate([2,1,2]) 

sage: [ S.d_vector(k) for k in range(3) ] 

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

""" 

from sage.modules.free_module_element import vector 

 

if self._use_d_vec: 

return copy(self._D).column(k) 

elif self._use_fpolys: 

f = self.cluster_variable(k) 

if f in self._R.gens(): 

return -vector(f.numerator().monomials()[0].exponents()[0][:self._n]) 

return vector(f.denominator().monomials()[0].exponents()[0][:self._n]) 

elif self._track_mut: 

catchup = ClusterSeed(self._b_initial) 

catchup.use_fpolys(False) 

catchup.use_g_vectors(False) 

catchup.use_c_vectors(False) 

 

catchup.mutate(self.mutations()) 

return copy(catchup._D).column(k) 

else: 

raise ValueError("Unable to calculate d-vector %s. Need to use d vectors."%k) 

 

def d_matrix(self, show_warnings=True): 

r""" 

Return the matrix of *d-vectors* of ``self``. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',4]); S.d_matrix() 

[-1 0 0 0] 

[ 0 -1 0 0] 

[ 0 0 -1 0] 

[ 0 0 0 -1] 

sage: S.mutate([1,2,1,0,1,3]); S.d_matrix() 

[1 1 0 1] 

[1 1 1 1] 

[1 0 1 1] 

[0 0 0 1] 

 

 

""" 

if not (self._use_d_vec or self._use_fpolys or self._track_mut): 

#raise ValueError("No d-vectors initialized.") 

raise ValueError("Unable to calculate d-vectors. Need to use d vectors.") 

if self._use_d_vec: 

return copy(self._D) 

elif self._use_fpolys: 

return matrix( [ self.d_vector(k) for k in range(self._n) ] ).transpose() 

elif self._track_mut: 

catchup = ClusterSeed(self._b_initial) 

catchup.use_fpolys(False) 

catchup.use_g_vectors(False) 

catchup.use_c_vectors(False) 

catchup.track_mutations(False) 

 

catchup.mutate(self.mutations()) 

return catchup.d_matrix() 

elif show_warnings: 

raise ValueError("No valid way to calculate d-vectors") 

 

def _d_mutate(self, k): 

r""" 

An internal procedure that returns ``self`` with d-vectors mutated at k. 

 

WARNING: This function assumes you are sending it good data (does not check for sanitized inputs) 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]) 

sage: S._d_mutate(0) 

sage: S.d_matrix() 

[ 1 0 0] 

[ 0 -1 0] 

[ 0 0 -1] 

sage: S.d_vector(0) 

(1, 0, 0) 

 

""" 

if self._use_fpolys: 

IE = self._init_exch.values() 

else: 

IE = [] 

 

B = self.b_matrix() 

D = copy(self._D) 

dnew = copy(-D.column(k)) 

dp = copy( dnew.parent().zero() ) 

dn = copy( dnew.parent().zero() ) 

dmax = copy( dnew.parent().zero() ) 

 

for j in range(self._n): 

if B[j,k] >0: 

dp += B[j,k]*D.column(j) 

elif B[j,k] <0: 

dn -= B[j,k]*D.column(j) 

for i in range(self._n): 

dmax[i] = max(dp[i],dn[i]) 

self._D.set_column(k,dnew+dmax) 

 

def coefficient(self,k): 

r""" 

Return the *coefficient* of ``self`` at index ``k``,  

or vertex ``k`` if ``k`` is not an index. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]).principal_extension() 

sage: S.mutate([2,1,2]) 

sage: [ S.coefficient(k) for k in range(3) ] 

[y0, 1/y2, 1/y1] 

""" 

from sage.misc.all import prod 

 

if k in self._nlist: 

k = self._nlist.index(k) 

if k not in range(self._n): 

raise ValueError("The cluster seed does not have a coefficient of index %s."%k) 

if self._m == 0: 

return self.x(0)**0 

else: 

try: # are c vectors being tracked? 

exp = self.c_vector(k) 

except Exception: # if not try and reconstruct them 

exp = self.c_matrix().column(k) 

 

return prod( self.y(i)**exp[i] for i in range(self._m) ) 

 

def coefficients(self): 

r""" 

Return all *coefficients* of ``self``. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]).principal_extension() 

sage: S.mutate([2,1,2]) 

sage: S.coefficients() 

[y0, 1/y2, 1/y1] 

""" 

# exceptions are caught in the subroutine. 

return [ self.coefficient(k) for k in range(self._n) ] 

 

def quiver(self): 

r""" 

Return the *quiver* associated to ``self``. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]) 

sage: S.quiver() 

Quiver on 3 vertices of type ['A', 3] 

""" 

from sage.combinat.cluster_algebra_quiver.quiver import ClusterQuiver 

if self._quiver is None: 

self._quiver = ClusterQuiver(self._M, user_labels=self._nlist+self._mlist) 

return self._quiver 

 

def is_acyclic(self): 

r""" 

Return True iff self is acyclic (i.e., if the underlying quiver is acyclic). 

 

EXAMPLES:: 

 

sage: ClusterSeed(['A',4]).is_acyclic() 

True 

 

sage: ClusterSeed(['A',[2,1],1]).is_acyclic() 

True 

 

sage: ClusterSeed([[0,1],[1,2],[2,0]]).is_acyclic() 

False 

""" 

 

return self.quiver()._digraph.is_directed_acyclic() 

 

def is_bipartite(self,return_bipartition=False): 

r""" 

Return True iff self is bipartite (i.e., if the underlying quiver is bipartite). 

 

INPUT: 

 

- return_bipartition -- (default:False) if True, the bipartition is returned in the case of ``self`` being bipartite. 

 

EXAMPLES:: 

 

sage: ClusterSeed(['A',[3,3],1]).is_bipartite() 

True 

 

sage: ClusterSeed(['A',[4,3],1]).is_bipartite() 

False 

""" 

 

return self.quiver().is_bipartite(return_bipartition=return_bipartition) 

 

def green_vertices(self): 

r""" 

Return the list of green vertices of ``self``. 

 

A vertex is defined to be green if its c-vector has all non-positive 

entries. More information on green vertices can be found at [BDP2013]_ 

 

OUTPUT: 

 

The green vertices as a list of integers. 

 

EXAMPLES:: 

 

sage: ClusterSeed(['A',3]).principal_extension().green_vertices() 

[0, 1, 2] 

 

sage: ClusterSeed(['A',[3,3],1]).principal_extension().green_vertices() 

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

""" 

 

# Make sure we have c vectors 

if not self._use_c_vec: 

raise ValueError("Must use c vectors to grab the vertices.") 

 

return get_green_vertices(self._C) 

 

def first_green_vertex(self): 

r""" 

Return the first green vertex of ``self``. 

 

A vertex is defined to be green if its c-vector has all non-positive entries. 

More information on green vertices can be found at [BDP2013]_ 

 

EXAMPLES:: 

 

sage: ClusterSeed(['A',3]).principal_extension().first_green_vertex() 

0 

 

sage: ClusterSeed(['A',[3,3],1]).principal_extension().first_green_vertex() 

0 

""" 

# Make sure we have c vectors 

if not self._use_c_vec: 

raise ValueError("Must use c vectors to grab the vertices.") 

 

greens = self.green_vertices() 

if greens: 

return greens[0] 

 

return None 

 

def red_vertices(self): 

r""" 

Return the list of red vertices of ``self``. 

 

A vertex is defined to be red if its c-vector has all non-negative entries. 

More information on red vertices can be found at [BDP2013]_. 

 

OUTPUT: 

 

The red vertices as a list of integers. 

 

EXAMPLES:: 

 

sage: ClusterSeed(['A',3]).principal_extension().red_vertices() 

[] 

 

sage: ClusterSeed(['A',[3,3],1]).principal_extension().red_vertices() 

[] 

 

sage: Q = ClusterSeed(['A',[3,3],1]).principal_extension(); 

sage: Q.mutate(1); 

sage: Q.red_vertices() 

[1] 

 

""" 

# Make sure we have c vectors on 

if not self._use_c_vec: 

raise ValueError("Must use c vectors to grab the vertices.") 

 

return get_red_vertices(self._C) 

 

def first_red_vertex(self): 

r""" 

Return the first red vertex of ``self``. 

 

A vertex is defined to be red if its c-vector has all non-negative entries. 

More information on red vertices can be found at [BDP2013]_. 

 

EXAMPLES:: 

 

sage: ClusterSeed(['A',3]).principal_extension().first_red_vertex() 

 

sage: ClusterSeed(['A',[3,3],1]).principal_extension().first_red_vertex() 

 

sage: Q = ClusterSeed(['A',[3,3],1]).principal_extension(); 

sage: Q.mutate(1); 

sage: Q.first_red_vertex() 

1 

 

""" 

# Make sure we have c vectors 

if not self._use_c_vec: 

raise ValueError("Must use c vectors to grab the vertices.") 

 

reds = self.red_vertices() 

if reds: 

return reds[0] 

 

return None 

 

def urban_renewals(self, return_first=False): 

r""" 

Return the list of the urban renewal vertices of ``self``. 

 

An urban renewal vertex is one in which there are two arrows pointing 

toward the vertex and two arrows pointing away. 

 

INPUT: 

 

- ``return_first`` -- (default:False) if True, will return the first urban renewal 

 

OUTPUT: 

 

A list of vertices (as integers) 

 

EXAMPLES:: 

 

sage: G = ClusterSeed(['GR',[4,9]]); G.urban_renewals() 

[5, 6] 

""" 

vertices = [] 

for i in range(self._n): 

if self.quiver().digraph().in_degree(i) == 2 and self.quiver().digraph().out_degree(i) == 2: 

if return_first: 

return i 

vertices.append(i) 

 

if return_first: 

return None 

return vertices 

 

def first_urban_renewal(self): 

r""" 

Return the first urban renewal vertex. 

 

An urban renewal vertex is one in which there are two arrows pointing 

toward the vertex and two arrows pointing away. 

 

EXAMPLES:: 

 

sage: G = ClusterSeed(['GR',[4,9]]); G.first_urban_renewal() 

5 

""" 

return self.urban_renewals(return_first=True) 

 

def highest_degree_denominator(self, filter=None): 

r""" 

Return the vertex of the cluster polynomial with highest degree in the denominator. 

 

INPUT: 

 

- ``filter`` - Filter should be a list or iterable 

 

OUTPUT: 

 

An integer. 

 

EXAMPLES:: 

 

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

sage: C = ClusterSeed(B).principal_extension(); C.mutate([0,1,2,4,3,2,5,4,3]) 

sage: C.highest_degree_denominator() 

5 

""" 

if filter is None: 

filter = list(range(len(self.cluster()))) 

degree = 0 

vertex_to_mutate = [] 

 

# if we have d vectors use those, else see if we have clusters 

if self._use_d_vec: 

for i in list(enumerate(self.d_matrix().columns())): 

if i[0] not in filter: 

continue 

col = i[1] 

vertex = i[0] 

cur_vertex_degree = sum(col) 

if degree == cur_vertex_degree: 

vertex_to_mutate.append(vertex) 

if degree < cur_vertex_degree: 

degree = cur_vertex_degree 

vertex_to_mutate = [vertex] 

elif self._use_fpolys: 

for i in list(enumerate(self.cluster())): 

if i[0] not in filter: 

continue 

vari = i[1] 

vertex = i[0] 

denom = vari.denominator() 

cur_vertex_degree = denom.degree() 

if degree == cur_vertex_degree: 

vertex_to_mutate.append(vertex) 

if degree < cur_vertex_degree: 

degree = cur_vertex_degree 

vertex_to_mutate = [vertex] 

 

 

return_key = randint(0,len(vertex_to_mutate) - 1) 

return vertex_to_mutate[return_key] 

 

def smallest_c_vector(self): 

r""" 

Return the vertex with the smallest c vector 

 

OUTPUT: 

 

An integer. 

 

EXAMPLES:: 

 

sage: B = matrix([[0,2],[-2,0]]) 

sage: C = ClusterSeed(B).principal_extension(); 

sage: C.mutate(0) 

sage: C.smallest_c_vector() 

0 

 

""" 

min_sum = infinity 

vertex_to_mutate = [] 

 

for i in list(enumerate(self.c_matrix().columns())): 

col = i[1] 

vertex=i[0] 

cur_vertex_sum = abs(sum(col)) 

if min_sum == cur_vertex_sum: 

vertex_to_mutate.append(vertex) 

if min_sum > cur_vertex_sum: 

min_sum = cur_vertex_sum 

vertex_to_mutate = [vertex] 

 

return_key = randint(0,len(vertex_to_mutate) - 1) 

return vertex_to_mutate[return_key] 

 

def most_decreased_edge_after_mutation(self): 

r""" 

 

Return the vertex that will produce the least degrees after mutation 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',5]) 

sage: S.mutate([0,2,3,1,2,3,1,2,0,2,3]) 

sage: S.most_decreased_edge_after_mutation() 

2 

 

""" 

analysis = self.mutation_analysis(['edge_diff']) 

least_edge = infinity 

least_vertex = [] 

for edge,edge_analysis in analysis.items(): 

if least_edge == edge_analysis['edge_diff']: 

least_vertex.append(edge) 

if least_edge > edge_analysis['edge_diff']: 

least_edge = edge_analysis['edge_diff'] 

least_vertex = [edge] 

 

# if we have one vertex, return it 

if len(least_vertex) == 1: 

return least_vertex[0] 

 

# if not then do a test based on which one currently has the highest degree 

return self.highest_degree_denominator(least_vertex) 

 

def most_decreased_denominator_after_mutation(self): 

r""" 

 

Return the vertex that will produce the most decrease in denominator degrees after mutation 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',5]) 

sage: S.mutate([0,2,3,1,2,3,1,2,0,2,3]) 

sage: S.most_decreased_denominator_after_mutation() 

2 

 

""" 

analysis = self.mutation_analysis(['d_matrix']) 

least_change = infinity 

least_vertex = [] 

current_columns = [sum(i) for i in self.d_matrix().columns()] 

for vertex,edge_analysis in analysis.items(): 

mutated_column = sum(edge_analysis['d_matrix'].column(vertex)) 

 

diff = mutated_column - current_columns[vertex] 

if least_change == diff: 

least_vertex.append(vertex) 

if diff < least_change: 

least_change = diff 

least_vertex = [vertex] 

 

return_key = randint(0,len(least_vertex) - 1) 

return least_vertex[return_key] 

 

def mutate(self, sequence, inplace=True, input_type=None): 

r""" 

Mutate ``self`` at a vertex or a sequence of vertices. 

 

INPUT: 

 

- ``sequence`` -- a vertex of ``self``, an iterator of vertices of 

``self``, a function which takes in the :class:`ClusterSeed` 

and returns a vertex or an iterator of vertices, 

or a string representing a type of vertices to mutate 

- ``inplace`` -- (default: ``True``) if ``False``, the result is 

returned, otherwise ``self`` is modified 

- ``input_type`` -- (default: ``None``) indicates the type of data 

contained in the sequence 

 

Possible values for vertex types in ``sequence`` are: 

 

- ``"first_source"``: mutates at first found source vertex, 

- ``"sources"``: mutates at all sources, 

- ``"first_sink"``: mutates at first sink, 

- ``"sinks"``: mutates at all sink vertices, 

- ``"green"``: mutates at the first green vertex, 

- ``"red"``: mutates at the first red vertex, 

- ``"urban_renewal"`` or ``"urban"``: mutates at first urban renewal vertex, 

- ``"all_urban_renewals"`` or ``"all_urban"``: mutates at all urban renewal vertices. 

 

For ``input_type``, if no value is given, preference will 

be given to vertex names, then indices, then cluster variables. 

If all input is not of the same type, an error is given. 

Possible values for ``input_type`` are: 

 

- ``"vertices"``: interprets the input sequence as vertices 

- ``"indices"``: interprets the input sequence as indices 

- ``"cluster_vars"``: interprets the input sequence as cluster variables 

this must be selected if inputing a sequence of cluster variables. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',4]); S.b_matrix() 

[ 0 1 0 0] 

[-1 0 -1 0] 

[ 0 1 0 1] 

[ 0 0 -1 0] 

 

sage: S.mutate(0); S.b_matrix() 

[ 0 -1 0 0] 

[ 1 0 -1 0] 

[ 0 1 0 1] 

[ 0 0 -1 0] 

 

sage: T = S.mutate(0, inplace=False); T 

A seed for a cluster algebra of rank 4 of type ['A', 4] 

 

sage: S.mutate(0) 

sage: S == T 

True 

 

sage: S.mutate([0,1,0]) 

sage: S.b_matrix() 

[ 0 -1 1 0] 

[ 1 0 0 0] 

[-1 0 0 1] 

[ 0 0 -1 0] 

 

sage: S = ClusterSeed(QuiverMutationType([['A',1],['A',3]])) 

sage: S.b_matrix() 

[ 0 0 0 0] 

[ 0 0 1 0] 

[ 0 -1 0 -1] 

[ 0 0 1 0] 

 

sage: T = S.mutate(0,inplace=False) 

sage: S == T 

False 

 

sage: Q = ClusterSeed(['A',3]);Q.b_matrix() 

[ 0 1 0] 

[-1 0 -1] 

[ 0 1 0] 

 

sage: Q.mutate('first_sink');Q.b_matrix() 

[ 0 -1 0] 

[ 1 0 1] 

[ 0 -1 0] 

 

sage: def last_vertex(self): return self._n - 1 

sage: Q.mutate(last_vertex); Q.b_matrix() 

[ 0 -1 0] 

[ 1 0 -1] 

[ 0 1 0] 

 

sage: S = ClusterSeed(['A', 4], user_labels=['a', 'b', 'c', 'd']); 

sage: S.mutate('a'); S.mutate('(b+1)/a') 

sage: S.cluster() 

[a, b, c, d] 

 

sage: S = ClusterSeed(['A', 4], user_labels=['a', 'b', 'c']); 

Traceback (most recent call last): 

... 

ValueError: the number of user-defined labels is not 

the number of exchangeable and frozen variables 

 

sage: S = ClusterSeed(['A', 4], user_labels=['x', 'y', 'w', 'z']) 

sage: S.mutate('x') 

sage: S.cluster() 

[(y + 1)/x, y, w, z] 

sage: S.mutate('(y+1)/x') 

sage: S.cluster() 

[x, y, w, z] 

sage: S.mutate('y') 

sage: S.cluster() 

[x, (x*w + 1)/y, w, z] 

sage: S.mutate('(x*w+1)/y') 

sage: S.cluster() 

[x, y, w, z] 

 

sage: S = ClusterSeed(['A', 4], user_labels=[[1, 2], [2, 3], [4, 5], [5, 6]]); 

sage: S.cluster() 

[x_1_2, x_2_3, x_4_5, x_5_6] 

sage: S.mutate('[1,2]'); 

sage: S.cluster() 

[(x_2_3 + 1)/x_1_2, x_2_3, x_4_5, x_5_6] 

 

sage: S = ClusterSeed(['A', 4], user_labels=[[1, 2], [2, 3], [4, 5], [5, 6]], 

....: user_labels_prefix='P'); 

sage: S.cluster() 

[P_1_2, P_2_3, P_4_5, P_5_6] 

sage: S.mutate('[1,2]') 

sage: S.cluster() 

[(P_2_3 + 1)/P_1_2, P_2_3, P_4_5, P_5_6] 

sage: S.mutate('P_4_5') 

sage: S.cluster() 

[(P_2_3 + 1)/P_1_2, P_2_3, (P_2_3*P_5_6 + 1)/P_4_5, P_5_6] 

 

sage: S = ClusterSeed(['A', 4]) 

sage: S.mutate([0, 1, 0, 1, 0, 2, 1]) 

sage: T = ClusterSeed(S) 

sage: S.use_fpolys(False) 

sage: S.use_g_vectors(False) 

sage: S.use_c_vectors(False) 

sage: S._C 

sage: S._G 

sage: S._F 

sage: S.g_matrix() 

[ 0 -1 0 0] 

[ 1 1 1 0] 

[ 0 0 -1 0] 

[ 0 0 1 1] 

sage: S.c_matrix() 

[ 1 -1 0 0] 

[ 1 0 0 0] 

[ 1 0 -1 1] 

[ 0 0 0 1] 

sage: S.f_polynomials() == T.f_polynomials() 

True 

 

sage: S.cluster() == T.cluster() 

True 

sage: S._mut_path 

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

 

sage: S = ClusterSeed(DiGraph([[1, 2], [2, 'c']])) 

sage: S.mutate(1) 

Input can be ambiguously interpreted as both vertices and indices. 

Mutating at vertices by default. 

sage: S.cluster() 

[(x2 + 1)/x1, x2, c] 

sage: S.mutate(1, input_type="indices") 

sage: S.cluster() 

[(x2 + 1)/x1, (x2*c + x1 + c)/(x1*x2), c] 

 

sage: S = ClusterSeed(DiGraph([['a', 'b'], ['c', 'b'], ['d', 'b']])) 

sage: S.mutate(['a', 'b', 'a', 'b', 'a']) 

sage: S.cluster() 

[b, a, c, d] 

sage: S.mutate('a') 

Input can be ambiguously interpreted as both vertices and cluster variables. 

Mutating at vertices by default. 

sage: S.cluster() 

[(a*c*d + 1)/b, a, c, d] 

sage: S.mutate('a', input_type="cluster_vars") 

sage: S.cluster() 

[(a*c*d + 1)/b, (a*c*d + b + 1)/(a*b), c, d] 

sage: S.mutate(['(a*c*d + 1)/b', 'd']) 

sage: S.cluster() 

[(b + 1)/a, (a*c*d + b + 1)/(a*b), c, (a*c*d + b^2 + 2*b + 1)/(a*b*d)] 

 

sage: S=ClusterSeed(DiGraph([[5, 'b']])) 

sage: S.mutate(5) 

sage: S.cluster() 

[(b + 1)/x5, b] 

sage: S.mutate([5]) 

sage: S.cluster() 

[x5, b] 

sage: S.mutate(0) 

sage: S.cluster() 

[(b + 1)/x5, b] 

 

sage: S=ClusterSeed(DiGraph([[1, 2]])) 

sage: S.cluster() 

[x1, x2] 

sage: S.mutate(1) 

Input can be ambiguously interpreted as both vertices and indices. 

Mutating at vertices by default. 

sage: S.cluster() 

[(x2 + 1)/x1, x2] 

 

sage: S = ClusterSeed(DiGraph([[-1, 0], [0, 1]])) 

sage: S.cluster() 

[xneg1, x0, x1] 

sage: S.mutate(-1);S.cluster() 

[(x0 + 1)/xneg1, x0, x1] 

sage: S.mutate(0, input_type='vertices');S.cluster() 

[(x0 + 1)/xneg1, (x0*x1 + xneg1 + x1)/(xneg1*x0), x1] 

""" 

 

# check for sanitizable data 

if not isinstance(inplace, bool): 

raise ValueError("the second parameter must be boolean; to mutate" 

" at a sequence of length 2, input it as a list") 

 

if inplace: 

seed = self 

else: 

seed = ClusterSeed(self)# change to deepcopy? 

 

# If we get a string, execute as a function 

if isinstance(sequence, str) and len(sequence) > 1 and sequence[0] is not '_': 

if sequence is 'green': 

sequence = self.first_green_vertex() 

elif sequence is 'red': 

sequence = self.first_red_vertex() 

elif sequence is 'urban' or sequence is 'urban_renewal': 

sequence = self.first_urban_renewal() 

elif sequence is 'all_urbans' or sequence is 'all_urban_renewals': 

sequence = self.urban_renewals() 

elif hasattr(self, sequence): 

sequence = getattr(self, sequence)() 

elif hasattr(self.quiver(), sequence): 

sequence = getattr(self.quiver(), sequence)() 

# If we are given a list in string format 

elif sequence[0] == '[' and sequence[-1] == ']': 

# convert to list 

from ast import literal_eval 

temp_list = literal_eval(sequence) 

 

sequence = self._user_labels_prefix 

for j in temp_list: 

if isinstance(j, Integer): 

sequence = sequence + "_" + j.str() 

elif isinstance(j, int): 

sequence = sequence + "_" + repr(j) 

else: 

sequence = sequence + "_" + j 

 

# If we get a function, execute it 

if hasattr(sequence, '__call__'): 

# function should return either integer or sequence 

sequence = sequence(seed) 

 

if sequence is None: 

raise ValueError('not mutating: no vertices given') 

 

if seed._use_fpolys: 

IE = seed._init_exch.values() 

else: 

IE = [] 

 

n, m = seed.n(), seed.m() 

 

V = IE + list(range(n)) 

 

if (sequence in range(n) or sequence in IE 

or isinstance(sequence, str) or sequence in seed._nlist): 

seqq = [sequence] 

else: 

seqq = sequence 

 

if isinstance(seqq, tuple): 

seqq = list(seqq) 

if not isinstance(seqq, list): 

raise ValueError("the quiver can only be mutated at a vertex" 

" or at a sequence of vertices") 

 

# These boolean variables classify the input type 

is_vertices = set(seqq).issubset(set(seed._nlist)) 

is_indices = set(seqq).issubset(set(range(n))) 

 

# Note - this does not guarantee that the sequence consists of 

# cluster variables, it only rules out some possibilities. 

is_cluster_vars = reduce(lambda x, y: isinstance(y, str), seqq, 1) and seed._use_fpolys 

 

# Ensures the sequence has elements of type input_type. 

if input_type: 

if input_type == "vertices" and not is_vertices: 

raise ValueError('input_type set to "vertices" but not everything' 

' in the mutation sequence is a vertex.') 

 

elif input_type == "indices" and not is_indices: 

raise ValueError('input_type set to "indices" but not everything' 

' in the mutation sequence is an index.') 

 

elif input_type == "cluster_vars" and not is_cluster_vars: 

raise ValueError('input_type set to "cluster_vars" but not' 

' everything in the mutation sequence is a' 

' cluster variable.') 

 

elif input_type not in ["vertices", "indices", "cluster_vars"]: 

raise ValueError('input_type must be either "vertices",' 

' "indices", or "cluster_vars"') 

 

# Classifies the input_type. Raises warnings if the input is ambiguous, and errors if the input is not all of the same type. 

else: 

if is_vertices: 

input_type = "vertices" 

for x in seqq: 

if is_indices and seed._nlist[x] != x: 

print("Input can be ambiguously interpreted as both" 

" vertices and indices." 

" Mutating at vertices by default.") 

break 

 

elif is_cluster_vars: 

cluster_var_index = seed.cluster_index(x) 

vertex_index = seed._nlist.index(x) 

if isinstance(cluster_var_index, int) and cluster_var_index != vertex_index: 

print("Input can be ambiguously interpreted as" 

" both vertices and cluster variables." 

" Mutating at vertices by default.") 

break 

 

# It should be impossible to interpret an index as a cluster variable. 

elif is_indices: 

input_type = "indices" 

elif is_cluster_vars: 

input_type = "cluster_vars" 

else: 

raise ValueError('mutation sequences must consist of exactly' 

' one of vertices, indices, or cluster variables') 

 

if input_type == "cluster_vars" and len(seqq) > 1: 

mutation_seed = deepcopy(seed) 

try: 

index_list = [] 

for cluster_var in seqq: 

new_index = mutation_seed.cluster_index(cluster_var) 

mutation_seed.mutate(new_index, input_type="indices") 

index_list.append(new_index) 

except (ValueError, TypeError): 

raise ValueError('input interpreted as cluster variables,' 

' but the input sequence did not consist' 

' of cluster variables') 

 

input_type = "indices" 

seqq = index_list 

 

if not inplace: 

mutation_seed._cluster = None 

mutation_seed._quiver = None 

return mutation_seed 

 

seq = iter(seqq) 

 

for k in seq: 

if input_type == "vertices": 

k = seed._nlist.index(k) 

 

elif input_type == "indices": 

pass 

 

elif input_type == "cluster_vars": 

k = seed.cluster_index(k) 

if k is None: 

raise ValueError("variable provided is not in our cluster") 

else: 

raise ValueError('Why wasnt this caught earlier? Cannot mutate in direction ' + str(k) + '.') 

 

if seed._use_fpolys: 

seed._f_mutate(k) 

 

if seed._use_g_vec: 

seed._g_mutate(k) 

 

if seed._use_d_vec: 

seed._d_mutate(k) 

 

seed._BC.mutate(k) 

seed._M = copy(seed._BC[:n+m,:n]) 

self._M.set_immutable() 

 

if seed._use_c_vec: 

seed._C = seed._BC[n+m:2*n+m,:n+m] 

 

if seed._track_mut: 

# delete involutive mutations 

if len(seed._mut_path) == 0 or seed._mut_path[len(self._mut_path)-1] != k: 

seed._mut_path.append(k) 

else: 

seed._mut_path.pop() 

 

# a mutation invalidates the cluster although it can be recomputed by F-polys and g-vectors 

seed._cluster = None 

seed._quiver = None 

 

if not inplace: 

return seed 

 

def cluster_index(self, cluster_str): 

r""" 

Return the index of a cluster if ``use_fpolys`` is on. 

 

INPUT: 

 

- ``cluster_str`` -- the string to look for in the cluster 

 

OUTPUT: 

 

An integer or ``None`` if the string is not a cluster variable 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A', 4], user_labels=['x', 'y', 'z', 'w']); S.mutate('x') 

sage: S.cluster_index('x') 

sage: S.cluster_index('(y+1)/x') 

0 

 

""" 

if self._use_fpolys and isinstance(cluster_str, str): 

c = FractionField(self._R)(cluster_str) 

cluster_str = ClusterVariable(FractionField(self._R), 

c.numerator(), c.denominator(), 

mutation_type=self._mutation_type, 

variable_type='cluster variable', 

xdim=self._n) 

if cluster_str in self.cluster(): 

return self.cluster().index(cluster_str) 

 

return None 

 

def mutation_sequence(self, sequence, show_sequence=False, 

fig_size=1.2, return_output='seed'): 

r""" 

Return the seeds obtained by mutating ``self`` at all vertices 

in ``sequence``. 

 

INPUT: 

 

- ``sequence`` -- an iterable of vertices of self. 

- ``show_sequence`` -- (default: False) if True, a png containing the associated quivers is shown. 

- ``fig_size`` -- (default: 1.2) factor by which the size of the plot is multiplied. 

- ``return_output`` -- (default: 'seed') determines what output is to be returned:: 

 

* if 'seed', outputs all the cluster seeds obtained by the ``sequence`` of mutations. 

* if 'matrix', outputs a list of exchange matrices. 

* if 'var', outputs a list of new cluster variables obtained at each step. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',2]) 

sage: for T in S.mutation_sequence([0,1,0]): 

....: print(T.b_matrix()) 

[ 0 -1] 

[ 1 0] 

[ 0 1] 

[-1 0] 

[ 0 -1] 

[ 1 0] 

 

sage: S = ClusterSeed(['A',2]) 

sage: S.mutation_sequence([0,1,0,1], return_output='var') 

[(x1 + 1)/x0, (x0 + x1 + 1)/(x0*x1), (x0 + 1)/x1, x0] 

""" 

seed = ClusterSeed(self) 

 

new_clust_var = [] 

seed_sequence = [] 

 

for v in sequence: 

seed = seed.mutate(v, inplace=False) 

new_clust_var.append(seed.cluster()[v]) 

seed_sequence.append(seed) 

 

if show_sequence: 

self.quiver().mutation_sequence2(sequence=sequence, show_sequence=True, fig_size=fig_size ) 

 

if return_output=='seed': 

return seed_sequence 

elif return_output=='matrix': 

return [ seed._M for seed in seed_sequence ] 

elif return_output=='var': 

return new_clust_var 

else: 

raise ValueError('The parameter `return_output` can only be `seed`, `matrix`, or `var`.') 

 

def mutation_analysis(self, options=['all'], filter=None): 

r""" 

Runs an analysis of all potential mutation options. Note that this might take a long time on large seeds. 

 

Notes: Edges are only returned if we have a non-valued quiver. Green and red vertices are only returned if the cluster is principal. 

 

INPUT: 

 

- ``options`` -- (default: ['all']) a list of mutation options. 

- ``filter`` -- (default: None) A vertex or interval of vertices to limit our search to 

 

Possible options are: 

 

- ``"all"`` - All options below 

- ``"edges"`` - Number of edges (works with skew-symmetric quivers) 

- ``"edge_diff"`` - Edges added/deleted (works with skew-symmetric quivers) 

- ``"green_vertices"`` - List of green vertices (works with principals) 

- ``"green_vertices_diff"`` - Green vertices added/removed (works with principals) 

- ``"red_vertices"`` - List of red vertices (works with principals) 

- ``"red_vertices_diff"`` - Red vertices added/removed (works with principals) 

- ``"urban_renewals"`` - List of urban renewal vertices 

- ``"urban_renewals_diff"`` - Urban renewal vertices added/removed 

- ``"sources"`` - List of source vertices 

- ``"sources_diff"`` - Source vertices added/removed 

- ``"sinks"`` - List of sink vertices 

- ``"sinks_diff"`` - Sink vertices added/removed 

- ``"denominators"`` - List of all denominators of the cluster variables 

 

OUTPUT: 

 

Outputs a dictionary indexed by the vertex numbers. Each vertex will itself also be a 

dictionary with each desired option included as a key in the dictionary. As an example 

you would get something similar to: {0: {'edges': 1}, 1: {'edges': 2}}. This represents 

that if you were to do a mutation at the current seed then mutating at vertex 0 would result 

in a quiver with 1 edge and mutating at vertex 0 would result in a quiver with 2 edges. 

 

EXAMPLES:: 

 

sage: B = [[0, 4, 0, -1],[-4,0, 3, 0],[0, -3, 0, 1],[1, 0, -1, 0]] 

sage: S = ClusterSeed(matrix(B)); S.mutate([2,3,1,2,1,3,0,2]) 

sage: S.mutation_analysis() 

{0: {'d_matrix': [ 0 0 1 0] 

[ 0 -1 0 0] 

[ 0 0 0 -1] 

[-1 0 0 0], 

'denominators': [1, 1, x0, 1], 

'edge_diff': 6, 

'edges': 13, 

'green_vertices': [0, 1, 3], 

'green_vertices_diff': {'added': [0], 'removed': []}, 

'red_vertices': [2], 

'red_vertices_diff': {'added': [], 'removed': [0]}, 

'sinks': [], 

'sinks_diff': {'added': [], 'removed': [2]}, 

'sources': [], 

'sources_diff': {'added': [], 'removed': []}, 

'urban_renewals': [], 

'urban_renewals_diff': {'added': [], 'removed': []}}, 

1: {'d_matrix': [ 1 4 1 0] 

[ 0 1 0 0] 

[ 0 0 0 -1] 

[ 1 4 0 0], 

'denominators': [x0*x3, x0^4*x1*x3^4, x0, 1], 

'edge_diff': 2, 

'edges': 9, 

'green_vertices': [0, 3], 

'green_vertices_diff': {'added': [0], 'removed': [1]}, 

'red_vertices': [1, 2], 

'red_vertices_diff': {'added': [1], 'removed': [0]}, 

'sinks': [2], 

'sinks_diff': {'added': [], 'removed': []}, 

'sources': [], 

'sources_diff': {'added': [], 'removed': []}, 

'urban_renewals': [], 

'urban_renewals_diff': {'added': [], 'removed': []}}, 

2: {'d_matrix': [ 1 0 0 0] 

[ 0 -1 0 0] 

[ 0 0 0 -1] 

[ 1 0 1 0], 

'denominators': [x0*x3, 1, x3, 1], 

'edge_diff': 0, 

'edges': 7, 

'green_vertices': [1, 2, 3], 

'green_vertices_diff': {'added': [2], 'removed': []}, 

'red_vertices': [0], 

'red_vertices_diff': {'added': [], 'removed': [2]}, 

'sinks': [], 

'sinks_diff': {'added': [], 'removed': [2]}, 

'sources': [2], 

'sources_diff': {'added': [2], 'removed': []}, 

'urban_renewals': [], 

'urban_renewals_diff': {'added': [], 'removed': []}}, 

3: {'d_matrix': [ 1 0 1 1] 

[ 0 -1 0 0] 

[ 0 0 0 1] 

[ 1 0 0 1], 

'denominators': [x0*x3, 1, x0, x0*x2*x3], 

'edge_diff': -1, 

'edges': 6, 

'green_vertices': [1], 

'green_vertices_diff': {'added': [], 'removed': [3]}, 

'red_vertices': [0, 2, 3], 

'red_vertices_diff': {'added': [3], 'removed': []}, 

'sinks': [2], 

'sinks_diff': {'added': [], 'removed': []}, 

'sources': [1], 

'sources_diff': {'added': [1], 'removed': []}, 

'urban_renewals': [], 

'urban_renewals_diff': {'added': [], 'removed': []}}} 

 

sage: S = ClusterSeed(['A',3]).principal_extension() 

sage: S.mutation_analysis() 

{0: {'d_matrix': [ 1 0 0] 

[ 0 -1 0] 

[ 0 0 -1], 

'denominators': [x0, 1, 1], 

'green_vertices': [1, 2], 

'green_vertices_diff': {'added': [], 'removed': [0]}, 

'red_vertices': [0], 

'red_vertices_diff': {'added': [0], 'removed': []}, 

'sinks': [], 

'sinks_diff': {'added': [], 'removed': [1]}, 

'sources': [4, 5], 

'sources_diff': {'added': [], 'removed': [3]}, 

'urban_renewals': [], 

'urban_renewals_diff': {'added': [], 'removed': []}}, 

1: {'d_matrix': [-1 0 0] 

[ 0 1 0] 

[ 0 0 -1], 

'denominators': [1, x1, 1], 

'green_vertices': [0, 2], 

'green_vertices_diff': {'added': [], 'removed': [1]}, 

'red_vertices': [1], 

'red_vertices_diff': {'added': [1], 'removed': []}, 

'sinks': [0, 2, 4], 

'sinks_diff': {'added': [0, 2, 4], 'removed': [1]}, 

'sources': [1, 3, 5], 

'sources_diff': {'added': [1], 'removed': [4]}, 

'urban_renewals': [], 

'urban_renewals_diff': {'added': [], 'removed': []}}, 

2: {'d_matrix': [-1 0 0] 

[ 0 -1 0] 

[ 0 0 1], 

'denominators': [1, 1, x2], 

'green_vertices': [0, 1], 

'green_vertices_diff': {'added': [], 'removed': [2]}, 

'red_vertices': [2], 

'red_vertices_diff': {'added': [2], 'removed': []}, 

'sinks': [], 

'sinks_diff': {'added': [], 'removed': [1]}, 

'sources': [3, 4], 

'sources_diff': {'added': [], 'removed': [5]}, 

'urban_renewals': [], 

'urban_renewals_diff': {'added': [], 'removed': []}}} 

 

""" 

 

V = list(range(self._n)) 

 

if filter is None: 

filter = V 

if filter in V: 

filter = [filter] 

 

# setup our initial information for differences later on 

if 'edge_diff' in options or ('all' in options and self._M.is_skew_symmetric()): 

initial_edges = self.quiver().number_of_edges() 

if 'green_vertices_diff' in options or ('all' in options and self._use_c_vec): 

initial_green_vertices = self.green_vertices() 

if 'red_vertices_diff' in options or ('all' in options and self._use_c_vec): 

initial_red_vertices = self.red_vertices() 

if 'urban_renewals_diff' in options or 'all' in options: 

initial_urban_renewals= self.urban_renewals() 

if 'sources_diff' in options or 'all' in options: 

initial_sources = self.quiver().sources() 

if 'sinks_diff' in options or 'all' in options: 

initial_sinks = self.quiver().sinks() 

 

#instantiate our dictionary 

analysis = {} 

for i in filter: 

#instantiate our dictionary 

analysis[i] = {} 

 

#run mutations not in place as we just want an analysis 

current_mutation = self.mutate(i,inplace=False) 

 

if ('edges' in options or 'all' in options) and self._M.is_skew_symmetric(): 

analysis[i]['edges'] = current_mutation.quiver().number_of_edges() 

if ('edge_diff' in options or 'all' in options) and self._M.is_skew_symmetric(): 

analysis[i]['edge_diff'] = current_mutation.quiver().number_of_edges() - initial_edges 

 

if ('green_vertices' in options or 'all' in options) and self._use_c_vec: 

analysis[i]['green_vertices'] = current_mutation.green_vertices() 

if ('green_vertices_diff' in options or 'all' in options) and self._use_c_vec: 

analysis[i]['green_vertices_diff'] = {} 

new_green_vertices = current_mutation.green_vertices() 

analysis[i]['green_vertices_diff']['added'] = list(set(new_green_vertices) - set(initial_green_vertices)) 

analysis[i]['green_vertices_diff']['removed'] = list(set(initial_green_vertices) - set(new_green_vertices)) 

 

if ('red_vertices' in options or 'all' in options) and self._use_c_vec: 

analysis[i]['red_vertices'] = current_mutation.red_vertices() 

if ('red_vertices_diff' in options or 'all' in options) and self._use_c_vec: 

analysis[i]['red_vertices_diff'] = {} 

new_red_vertices = current_mutation.red_vertices() 

analysis[i]['red_vertices_diff']['added'] = list(set(new_red_vertices) - set(initial_red_vertices)) 

analysis[i]['red_vertices_diff']['removed'] = list(set(initial_red_vertices) - set(new_red_vertices)) 

 

if 'urban_renewals' in options or 'all' in options: 

analysis[i]['urban_renewals'] = current_mutation.urban_renewals() 

if 'urban_renewals_diff' in options or 'all' in options: 

analysis[i]['urban_renewals_diff'] = {} 

new_urban_renewals = current_mutation.urban_renewals() 

analysis[i]['urban_renewals_diff']['added'] = list(set(new_urban_renewals) - set(initial_urban_renewals)) 

analysis[i]['urban_renewals_diff']['removed'] = list(set(initial_urban_renewals) - set(new_urban_renewals)) 

 

if 'sources' in options or 'all' in options: 

analysis[i]['sources'] = current_mutation.quiver().sources() 

if 'sources_diff' in options or 'all' in options: 

analysis[i]['sources_diff'] = {} 

new_sources = current_mutation.quiver().sources() 

analysis[i]['sources_diff']['added'] = list(set(new_sources) - set(initial_sources)) 

analysis[i]['sources_diff']['removed'] = list(set(initial_sources) - set(new_sources)) 

 

if 'sinks' in options or 'all' in options: 

analysis[i]['sinks'] = current_mutation.quiver().sinks() 

if 'sinks_diff' in options or 'all' in options: 

analysis[i]['sinks_diff'] = {} 

new_sinks = current_mutation.quiver().sinks() 

analysis[i]['sinks_diff']['added'] = list(set(new_sinks) - set(initial_sinks)) 

analysis[i]['sinks_diff']['removed'] = list(set(initial_sinks) - set(new_sinks)) 

 

if ('denominators' in options or 'all' in options) and self._use_fpolys: 

analysis[i]['denominators'] = [] 

for vari in current_mutation.cluster(): 

analysis[i]['denominators'].append(vari.denominator()) 

 

if ('d_matrix' in options or 'all' in options) and (self._use_d_vec or self._use_fpolys): 

analysis[i]['d_matrix'] = current_mutation.d_matrix() 

 

return analysis 

 

def exchangeable_part(self): 

r""" 

Return the restriction to the principal part (i.e. the exchangeable 

variables) of ``self``. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',4]) 

sage: T = ClusterSeed( S.quiver().digraph().edges(), frozen=[3] ) 

sage: T.quiver().digraph().edges() 

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

 

sage: T.exchangeable_part().quiver().digraph().edges() 

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

 

""" 

from sage.combinat.cluster_algebra_quiver.mutation_class import _principal_part 

eval_dict = dict( [ ( self.y(i), 1 ) for i in range(self._m) ] ) 

seed = ClusterSeed(_principal_part(self._M), is_principal=True, 

user_labels=self._nlist, 

user_labels_prefix=self._user_labels_prefix, 

frozen=None) 

seed.use_c_vectors(self._use_c_vec) 

seed.use_fpolys(self._use_fpolys) 

seed.use_g_vectors(self._use_g_vec) 

seed.use_d_vectors(self._use_d_vec) 

seed.track_mutations(self._track_mut) 

if self._use_fpolys: 

self.cluster() 

seed._cluster = [self._cluster[k].subs(eval_dict) 

for k in range(self._n)] 

seed._mutation_type = self._mutation_type 

return seed 

 

def universal_extension(self): 

r""" 

Return the universal extension of ``self``. 

 

This is the initial seed of the associated cluster algebra 

with universal coefficients, as defined in section 12 of 

[FZ2007]_. 

 

This method works only if ``self`` is a bipartite, finite-type seed. 

 

Due to some limitations in the current implementation of 

``CartanType``, we need to construct the set of almost positive 

coroots by hand. As a consequence their ordering is not the 

standard one (the rows of the bottom part of the exchange 

matrix might be a shuffling of those you would expect). 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',2]) 

sage: T = S.universal_extension() 

sage: T.b_matrix() 

[ 0 1] 

[-1 0] 

[-1 0] 

[ 1 0] 

[ 1 -1] 

[ 0 1] 

[ 0 -1] 

 

sage: S = ClusterSeed(['A',3]) 

sage: T = S.universal_extension() 

sage: T.b_matrix() 

[ 0 1 0] 

[-1 0 -1] 

[ 0 1 0] 

[-1 0 0] 

[ 1 0 0] 

[ 1 -1 0] 

[ 1 -1 1] 

[ 0 1 0] 

[ 0 -1 0] 

[ 0 -1 1] 

[ 0 0 -1] 

[ 0 0 1] 

 

sage: S = ClusterSeed(['B',2]) 

sage: T = S.universal_extension() 

sage: T.b_matrix() 

[ 0 1] 

[-2 0] 

[-1 0] 

[ 1 0] 

[ 1 -1] 

[ 2 -1] 

[ 0 1] 

[ 0 -1] 

 

sage: S = ClusterSeed(['A', 5], user_labels = [-2, -1, 0, 1 ,2]) 

sage: U = S.universal_extension() 

sage: U.b_matrix() == ClusterSeed(['A', 5]).universal_extension().b_matrix() 

True 

""" 

if self._m != 0: 

raise ValueError("To have universal coefficients we need " 

"to start from a coefficient-free seed") 

if not self.is_bipartite() or not self.is_finite(): 

raise ValueError("Universal coefficients are defined only " 

"for finite type cluster algebras at a " 

"bipartite initial cluster") 

 

from sage.matrix.all import matrix 

from sage.combinat.root_system.cartan_matrix import CartanMatrix 

 

A = 2 - self.b_matrix().apply_map(abs).transpose() 

 

# We give the indexing set of the Cartan matrix to be [1, 2, ..., n] 

rs = CartanMatrix(A, index_set=list(range(1,A.ncols()+1))).root_space() 

almost_positive_coroots = rs.almost_positive_roots() 

 

sign = [-1 if all(x <= 0 for x in self.b_matrix()[i]) else 1 

for i in range(self._n)] 

C = matrix([[sign[j] * alpha[j + 1] for j in range(self._n)] 

for alpha in almost_positive_coroots]) 

 

M = self._M.stack(C) 

n = C.nrows() 

new_labels = None 

if self._user_labels: 

if isinstance(self._user_labels,list): 

new_labels = self._user_labels + ['y%s'%i for i in range(n)] 

elif isinstance(self._user_labels,dict): 

new_labels = copy(self._user_labels) 

new_labels.update( {(i+self._n):'y%s'%i for i in range(n)} ) 

seed = ClusterSeed(M, is_principal=False, user_labels=new_labels, 

user_labels_prefix=self._user_labels_prefix, 

frozen=None) 

seed.use_c_vectors(self._use_c_vec) 

seed.use_fpolys(self._use_fpolys) 

seed.use_g_vectors(self._use_g_vec) 

seed.use_d_vectors(self._use_d_vec) 

seed.track_mutations(self._track_mut) 

 

seed._mutation_type = self._mutation_type 

return seed 

 

def principal_extension(self): 

r""" 

Return the principal extension of ``self``, yielding a 

`2n \times n` matrix. 

 

Raises an error if the input seed has a non-square exchange matrix. 

In this case, the method instead adds `n` frozen variables to any 

previously frozen variables. I.e., the seed obtained by adding a 

frozen variable to every exchangeable variable of ``self``. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed([[0,1],[1,2],[2,3],[2,4]]); S 

A seed for a cluster algebra of rank 5 

 

sage: T = S.principal_extension(); T 

A seed for a cluster algebra of rank 5 with principal coefficients 

 

sage: T.b_matrix() 

[ 0 1 0 0 0] 

[-1 0 1 0 0] 

[ 0 -1 0 1 1] 

[ 0 0 -1 0 0] 

[ 0 0 -1 0 0] 

[ 1 0 0 0 0] 

[ 0 1 0 0 0] 

[ 0 0 1 0 0] 

[ 0 0 0 1 0] 

[ 0 0 0 0 1] 

 

sage: S = ClusterSeed(['A', 4], user_labels=['a', 'b', 'c', 'd']) 

sage: T = S.principal_extension() 

sage: T.cluster() 

[a, b, c, d] 

sage: T.coefficients() 

[y0, y1, y2, y3] 

sage: S2 = ClusterSeed(['A', 4], user_labels={0:'a', 1:'b', 2:'c', 3:'d'}) 

sage: S2 == S 

True 

sage: T2 = S2.principal_extension() 

sage: T2 == T 

True 

""" 

from sage.matrix.all import identity_matrix 

if self._m != 0: 

raise ValueError("the b-matrix is not square") 

M = self._M.stack(identity_matrix(self._n)) 

is_principal = (self._m == 0) 

new_labels = None 

if self._user_labels: 

if isinstance(self._user_labels, list): 

new_labels = self._user_labels + ['y%s'%i for i in range(self._n)] 

elif isinstance(self._user_labels, dict): 

new_labels = copy(self._user_labels) 

new_labels.update( {(i+self._n): 'y%s'%i for i in range(self._n)} ) 

seed = ClusterSeed(M, is_principal=is_principal, user_labels=new_labels, 

user_labels_prefix=self._user_labels_prefix, frozen=None) 

seed.use_c_vectors(self._use_c_vec) 

seed.use_fpolys(self._use_fpolys) 

seed.use_g_vectors(self._use_g_vec) 

seed.use_d_vectors(self._use_d_vec) 

seed.track_mutations(self._track_mut) 

 

#### This should fix principal_extension resetting boolean flags. Might need to update user labels to include new principals with y's. -G 

seed._mutation_type = self._mutation_type 

return seed 

 

def reorient( self, data ): 

r""" 

Reorients ``self`` with respect to the given total order, 

or with respect to an iterator of ordered pairs. 

 

WARNING: 

 

- This operation might change the mutation type of ``self``. 

- Ignores ordered pairs `(i,j)` for which neither `(i,j)` nor `(j,i)` is an edge of ``self``. 

 

INPUT: 

 

- ``data`` -- an iterator defining a total order on ``self.vertices()``, or an iterator of ordered pairs in ``self`` defining the new orientation of these edges. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',[2,3],1]) 

sage: S.mutation_type() 

['A', [2, 3], 1] 

 

sage: S.reorient([(0,1),(2,3)]) 

sage: S.mutation_type() 

['D', 5] 

 

sage: S.reorient([(1,0),(2,3)]) 

sage: S.mutation_type() 

['A', [1, 4], 1] 

 

sage: S.reorient([0,1,2,3,4]) 

sage: S.mutation_type() 

['A', [1, 4], 1] 

""" 

if not self._quiver: 

self.quiver() 

self._quiver.reorient( data ) 

self._M = self._quiver._M 

self.reset_cluster() 

self._mutation_type = None 

 

def set_cluster( self, cluster, force=False ): 

r""" 

Sets the cluster for ``self`` to ``cluster``. 

 

Warning: Initialization may lead to inconsistent data. 

 

INPUT: 

 

- ``cluster`` -- an iterable defining a cluster for ``self``. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]) 

sage: cluster = S.cluster() 

sage: S.mutate([1,2,1]) 

sage: S.cluster() 

[x0, (x1 + 1)/x2, (x0*x2 + x1 + 1)/(x1*x2)] 

sage: cluster2 = S.cluster() 

 

sage: S.set_cluster(cluster) 

Warning: using set_cluster at this point could lead to inconsistent seed data. 

 

sage: S.set_cluster(cluster, force=True) 

sage: S.cluster() 

[x0, x1, x2] 

sage: S.set_cluster(cluster2, force=True) 

sage: S.cluster() 

[x0, (x1 + 1)/x2, (x0*x2 + x1 + 1)/(x1*x2)] 

 

sage: S = ClusterSeed(['A',3]); S.use_fpolys(False) 

sage: S.set_cluster([1,1,1]) 

Warning: clusters not being tracked so this command is ignored. 

""" 

 

if len(cluster) < self._n+self._m: 

raise ValueError('The number of given cluster variables is wrong') 

if self._use_fpolys: 

if any(c not in FractionField(self._R) for c in cluster): 

raise ValueError('The cluster variables are not all contained in %s'%FractionField(self._R)) 

if not force: # if already have f_polynomials, using set_cluster might yield data inconsistent with them. 

print("Warning: using set_cluster at this point could lead to inconsistent seed data.") 

else: 

self._cluster = [FractionField(self._R)(x) 

for x in cluster][0:self._n] 

self._is_principal = None 

else: 

print("Warning: clusters not being tracked so this command is ignored.") 

 

def reset_cluster( self ): 

r""" 

Resets the cluster of ``self`` to the initial cluster. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]) 

sage: S.mutate([1,2,1]) 

sage: S.cluster() 

[x0, (x1 + 1)/x2, (x0*x2 + x1 + 1)/(x1*x2)] 

 

sage: S.reset_cluster() 

sage: S.cluster() 

[x0, x1, x2] 

 

sage: T = S.principal_extension() 

sage: T.cluster() 

[x0, x1, x2] 

sage: T.mutate([1,2,1]) 

sage: T.cluster() 

[x0, (x1*y2 + x0)/x2, (x1*y1*y2 + x0*y1 + x2)/(x1*x2)] 

 

sage: T.reset_cluster() 

sage: T.cluster() 

[x0, x1, x2] 

 

sage: S = ClusterSeed(['B',3],user_labels=[[1,2],[2,3],[3,4]],user_labels_prefix='p') 

sage: S.mutate([0,1]) 

sage: S.cluster() 

[(p_2_3 + 1)/p_1_2, (p_1_2*p_3_4^2 + p_2_3 + 1)/(p_1_2*p_2_3), p_3_4] 

 

sage: S.reset_cluster() 

sage: S.cluster() 

[p_1_2, p_2_3, p_3_4] 

sage: S.g_matrix() 

[1 0 0] 

[0 1 0] 

[0 0 1] 

sage: S.f_polynomials() 

[1, 1, 1] 

""" 

if self._use_g_vec: 

self._G = matrix.identity(self._n) 

if self._use_fpolys: 

self._F = dict([(i,self._U(1)) for i in self._init_exch.values()]) 

if self._use_fpolys: 

self.set_cluster(self._R.gens(), force=True) 

 

def reset_coefficients( self ): 

r""" 

Resets the coefficients of ``self`` to the frozen variables but keeps the current cluster. 

Raises an error if the number of frozen variables is different than the number of exchangeable variables. 

 

WARNING: This command to be phased out since 'use_c_vectors() does this more effectively. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]).principal_extension() 

sage: S.b_matrix() 

[ 0 1 0] 

[-1 0 -1] 

[ 0 1 0] 

[ 1 0 0] 

[ 0 1 0] 

[ 0 0 1] 

sage: S.mutate([1,2,1]) 

sage: S.b_matrix() 

[ 0 1 -1] 

[-1 0 1] 

[ 1 -1 0] 

[ 1 0 0] 

[ 0 1 -1] 

[ 0 0 -1] 

sage: S.reset_coefficients() 

sage: S.b_matrix() 

[ 0 1 -1] 

[-1 0 1] 

[ 1 -1 0] 

[ 1 0 0] 

[ 0 1 0] 

[ 0 0 1] 

""" 

n, m = self._n, self._m 

if not n == m: 

raise ValueError("The numbers of cluster variables " 

"and of frozen variables do not coincide.") 

newM = copy(self._M) 

for i in range(m): 

for j in range(n): 

if i == j: 

newM[i + n, j] = 1 

else: 

newM[i + n, j] = 0 

self._M = newM 

self._M.set_immutable() 

self._quiver = None 

self._is_principal = None 

 

def mutation_class_iter(self, depth=infinity, show_depth=False, 

return_paths=False, up_to_equivalence=True, 

only_sink_source=False): 

r""" 

Return an iterator for the mutation class of ``self`` with 

respect to certain constrains. 

 

INPUT: 

 

- ``depth`` -- (default: infinity) integer or infinity, only seeds with distance at most ``depth`` from ``self`` are returned. 

- ``show_depth`` -- (default: False) if True, the current depth of the mutation is shown while computing. 

- ``return_paths`` -- (default: False) if True, a shortest path of mutations from ``self`` to the given quiver is returned as well. 

- ``up_to_equivalence`` -- (default: True) if True, only one seed up to simultaneous permutation of rows and columns of the exchange matrix is recorded. 

- ``sink_source`` -- (default: False) if True, only mutations at sinks and sources are applied. 

 

EXAMPLES: 

 

A standard finite type example:: 

 

sage: S = ClusterSeed(['A',3]) 

sage: it = S.mutation_class_iter() 

sage: for T in it: print(T) 

A seed for a cluster algebra of rank 3 of type ['A', 3] 

A seed for a cluster algebra of rank 3 of type ['A', 3] 

A seed for a cluster algebra of rank 3 of type ['A', 3] 

A seed for a cluster algebra of rank 3 of type ['A', 3] 

A seed for a cluster algebra of rank 3 of type ['A', 3] 

A seed for a cluster algebra of rank 3 of type ['A', 3] 

A seed for a cluster algebra of rank 3 of type ['A', 3] 

A seed for a cluster algebra of rank 3 of type ['A', 3] 

A seed for a cluster algebra of rank 3 of type ['A', 3] 

A seed for a cluster algebra of rank 3 of type ['A', 3] 

A seed for a cluster algebra of rank 3 of type ['A', 3] 

A seed for a cluster algebra of rank 3 of type ['A', 3] 

A seed for a cluster algebra of rank 3 of type ['A', 3] 

A seed for a cluster algebra of rank 3 of type ['A', 3] 

 

A finite type example with given depth:: 

 

sage: it = S.mutation_class_iter(depth=1) 

sage: for T in it: print(T) 

A seed for a cluster algebra of rank 3 of type ['A', 3] 

A seed for a cluster algebra of rank 3 of type ['A', 3] 

A seed for a cluster algebra of rank 3 of type ['A', 3] 

A seed for a cluster algebra of rank 3 of type ['A', 3] 

 

A finite type example where the depth is shown while computing:: 

 

sage: it = S.mutation_class_iter(show_depth=True) 

sage: for T in it: pass 

Depth: 0 found: 1 Time: ... s 

Depth: 1 found: 4 Time: ... s 

Depth: 2 found: 9 Time: ... s 

Depth: 3 found: 13 Time: ... s 

Depth: 4 found: 14 Time: ... s 

 

A finite type example with shortest paths returned:: 

 

sage: it = S.mutation_class_iter(return_paths=True) 

sage: for T in it: print(T) 

(A seed for a cluster algebra of rank 3 of type ['A', 3], []) 

(A seed for a cluster algebra of rank 3 of type ['A', 3], [2]) 

(A seed for a cluster algebra of rank 3 of type ['A', 3], [1]) 

(A seed for a cluster algebra of rank 3 of type ['A', 3], [0]) 

(A seed for a cluster algebra of rank 3 of type ['A', 3], [2, 1]) 

(A seed for a cluster algebra of rank 3 of type ['A', 3], [0, 2]) 

(A seed for a cluster algebra of rank 3 of type ['A', 3], [0, 1]) 

(A seed for a cluster algebra of rank 3 of type ['A', 3], [1, 2]) 

(A seed for a cluster algebra of rank 3 of type ['A', 3], [1, 0]) 

(A seed for a cluster algebra of rank 3 of type ['A', 3], [0, 2, 1]) 

(A seed for a cluster algebra of rank 3 of type ['A', 3], [0, 1, 2]) 

(A seed for a cluster algebra of rank 3 of type ['A', 3], [2, 1, 0]) 

(A seed for a cluster algebra of rank 3 of type ['A', 3], [1, 0, 2]) 

(A seed for a cluster algebra of rank 3 of type ['A', 3], [0, 1, 2, 0]) 

 

Finite type examples not considered up to equivalence:: 

 

sage: it = S.mutation_class_iter(up_to_equivalence=False) 

sage: len( [ T for T in it ] ) 

84 

 

sage: it = ClusterSeed(['A',2]).mutation_class_iter(return_paths=True,up_to_equivalence=False) 

sage: for T in it: print(T) 

(A seed for a cluster algebra of rank 2 of type ['A', 2], []) 

(A seed for a cluster algebra of rank 2 of type ['A', 2], [1]) 

(A seed for a cluster algebra of rank 2 of type ['A', 2], [0]) 

(A seed for a cluster algebra of rank 2 of type ['A', 2], [0, 1]) 

(A seed for a cluster algebra of rank 2 of type ['A', 2], [1, 0]) 

(A seed for a cluster algebra of rank 2 of type ['A', 2], [1, 0, 1]) 

(A seed for a cluster algebra of rank 2 of type ['A', 2], [0, 1, 0]) 

(A seed for a cluster algebra of rank 2 of type ['A', 2], [1, 0, 1, 0]) 

(A seed for a cluster algebra of rank 2 of type ['A', 2], [0, 1, 0, 1]) 

(A seed for a cluster algebra of rank 2 of type ['A', 2], [1, 0, 1, 0, 1]) 

 

Check that :trac:`14638` is fixed:: 

 

sage: S = ClusterSeed(['E',6]) 

sage: MC = S.mutation_class(depth=7); len(MC) 

534 

 

Infinite type examples:: 

 

sage: S = ClusterSeed(['A',[1,1],1]) 

sage: it = S.mutation_class_iter() 

sage: next(it) 

A seed for a cluster algebra of rank 2 of type ['A', [1, 1], 1] 

sage: next(it) 

A seed for a cluster algebra of rank 2 of type ['A', [1, 1], 1] 

sage: next(it) 

A seed for a cluster algebra of rank 2 of type ['A', [1, 1], 1] 

sage: next(it) 

A seed for a cluster algebra of rank 2 of type ['A', [1, 1], 1] 

 

sage: it = S.mutation_class_iter(depth=3, return_paths=True) 

sage: for T in it: print(T) 

(A seed for a cluster algebra of rank 2 of type ['A', [1, 1], 1], []) 

(A seed for a cluster algebra of rank 2 of type ['A', [1, 1], 1], [1]) 

(A seed for a cluster algebra of rank 2 of type ['A', [1, 1], 1], [0]) 

(A seed for a cluster algebra of rank 2 of type ['A', [1, 1], 1], [1, 0]) 

(A seed for a cluster algebra of rank 2 of type ['A', [1, 1], 1], [0, 1]) 

(A seed for a cluster algebra of rank 2 of type ['A', [1, 1], 1], [1, 0, 1]) 

(A seed for a cluster algebra of rank 2 of type ['A', [1, 1], 1], [0, 1, 0]) 

""" 

 

# Variable to track the depth 

depth_counter = 0 

n = self._n 

timer = time.time() 

 

# set up our initial cluster and grab variables 

if up_to_equivalence: 

cl = Set( self.cluster() ) 

else: 

cl = tuple( self.cluster() ) 

 

# If we are tracking return paths 

if return_paths: 

yield (self,[]) 

else: 

yield self 

 

 

# instantiate the variables 

clusters = {} 

clusters[ cl ] = [ self, list(range(n)), [] ] 

 

# we get bigger the first time 

gets_bigger = True 

 

# If we are showing depth, show some statistics 

if show_depth: 

timer2 = time.time() 

dc = str(depth_counter) 

dc += ' ' * (5-len(dc)) 

nr = str(len(clusters)) 

nr += ' ' * (10-len(nr)) 

print("Depth: %s found: %s Time: %.2f s" % (dc, nr, timer2-timer)) 

 

# Each time we get bigger and we haven't hit the full depth 

while gets_bigger and depth_counter < depth: 

gets_bigger = False 

 

# set the keys 

keys = clusters.keys() 

 

# Our keys are cluster variables, so for each cluster: 

for key in keys: 

# sd is the cluster data 

sd = clusters[key] 

 

# another way to do a for loop for each item 

while sd[1]: 

i = sd[1].pop() 

 

# If we aren't only sinking the source 

if not only_sink_source or all( entry >= 0 for entry in sd[0]._M.row( i ) ) or all( entry <= 0 for entry in sd[0]._M.row( i ) ): 

# do an inplace mutation on our cluster (sd[0]) 

sd2 = sd[0].mutate(i, inplace=False, input_type="indices") 

 

# set up our new cluster variables 

if up_to_equivalence: 

cl2 = Set(sd2.cluster()) 

else: 

cl2 = tuple(sd2.cluster()) 

if cl2 in clusters: 

if not up_to_equivalence and i in clusters[cl2][1]: 

clusters[cl2][1].remove(i) 

else: 

gets_bigger = True 

if only_sink_source: 

orbits = list(range(n)) 

else: 

orbits = [ index for index in range(n) if index > i or sd2._M[index,i] != 0 ] 

 

clusters[ cl2 ] = [ sd2, orbits, clusters[key][2]+[i] ] 

if return_paths: 

yield (sd2,clusters[cl2][2]) 

else: 

yield sd2 

depth_counter += 1 

if show_depth and gets_bigger: 

timer2 = time.time() 

dc = str(depth_counter) 

dc += ' ' * (5-len(dc)) 

nr = str(len(clusters)) 

nr += ' ' * (10-len(nr)) 

print("Depth: %s found: %s Time: %.2f s" % (dc,nr,timer2-timer)) 

 

def mutation_class(self, depth=infinity, show_depth=False, return_paths=False, 

up_to_equivalence=True, only_sink_source=False): 

r""" 

Return the mutation class of ``self`` with respect to 

certain constraints. 

 

.. NOTE:: 

 

Vertex labels are not tracked in this method. 

 

.. SEEALSO:: 

 

:meth:`mutation_class_iter` 

 

INPUT: 

 

- ``depth`` -- (default: ``infinity`) integer, only seeds with 

distance at most depth from ``self`` are returned 

- ``show_depth`` -- (default: ``False``) if ``True``, the actual depth 

of the mutation is shown 

- ``return_paths`` -- (default: ``False``) if ``True``, a shortest 

path of mutation sequences from self to the given quiver is 

returned as well 

- ``up_to_equivalence`` -- (default: ``True``) if ``True``, only 

seeds up to equivalence are considered 

- ``sink_source`` -- (default: ``False``) if ``True``, only mutations 

at sinks and sources are applied 

 

EXAMPLES: 

 

- for examples see :meth:`mutation_class_iter` 

 

TESTS:: 

 

sage: A = ClusterSeed(['A',3]).mutation_class() 

""" 

if depth is infinity and not self.is_finite(): 

raise ValueError('The mutation class can - for infinite types - only be computed up to a given depth') 

return list( S for S in self.mutation_class_iter( depth=depth, show_depth=show_depth, return_paths=return_paths, up_to_equivalence=up_to_equivalence, only_sink_source=only_sink_source ) ) 

 

def cluster_class_iter(self, depth=infinity, show_depth=False, up_to_equivalence=True): 

r""" 

Returns an iterator through all clusters in the mutation class of ``self``. 

 

INPUT: 

 

- ``depth`` -- (default: infinity) integer or infinity, only seeds with distance at most depth from self are returned 

- ``show_depth`` -- (default False) - if True, ignored if depth is set; returns the depth of the mutation class, i.e., the maximal distance from self of an element in the mutation class 

- ``up_to_equivalence`` -- (default: True) if True, only clusters up to equivalence are considered. 

 

EXAMPLES: 

 

A standard finite type example:: 

 

sage: S = ClusterSeed(['A',3]) 

sage: it = S.cluster_class_iter() 

sage: for T in it: print(T) 

[x0, x1, x2] 

[x0, x1, (x1 + 1)/x2] 

[x0, (x0*x2 + 1)/x1, x2] 

[(x1 + 1)/x0, x1, x2] 

[x0, (x0*x2 + x1 + 1)/(x1*x2), (x1 + 1)/x2] 

[(x1 + 1)/x0, x1, (x1 + 1)/x2] 

[(x1 + 1)/x0, (x0*x2 + x1 + 1)/(x0*x1), x2] 

[x0, (x0*x2 + 1)/x1, (x0*x2 + x1 + 1)/(x1*x2)] 

[(x0*x2 + x1 + 1)/(x0*x1), (x0*x2 + 1)/x1, x2] 

[(x1 + 1)/x0, (x1^2 + x0*x2 + 2*x1 + 1)/(x0*x1*x2), (x1 + 1)/x2] 

[(x1 + 1)/x0, (x0*x2 + x1 + 1)/(x0*x1), (x1^2 + x0*x2 + 2*x1 + 1)/(x0*x1*x2)] 

[(x1^2 + x0*x2 + 2*x1 + 1)/(x0*x1*x2), (x0*x2 + x1 + 1)/(x1*x2), (x1 + 1)/x2] 

[(x0*x2 + x1 + 1)/(x0*x1), (x0*x2 + 1)/x1, (x0*x2 + x1 + 1)/(x1*x2)] 

[(x0*x2 + x1 + 1)/(x1*x2), (x0*x2 + x1 + 1)/(x0*x1), (x1^2 + x0*x2 + 2*x1 + 1)/(x0*x1*x2)] 

 

A finite type example with given depth:: 

 

sage: it = S.cluster_class_iter(depth=1) 

sage: for T in it: print(T) 

[x0, x1, x2] 

[x0, x1, (x1 + 1)/x2] 

[x0, (x0*x2 + 1)/x1, x2] 

[(x1 + 1)/x0, x1, x2] 

 

A finite type example where the depth is returned while computing:: 

 

sage: it = S.cluster_class_iter(show_depth=True) 

sage: for T in it: print(T) 

[x0, x1, x2] 

Depth: 0 found: 1 Time: ... s 

[x0, x1, (x1 + 1)/x2] 

[x0, (x0*x2 + 1)/x1, x2] 

[(x1 + 1)/x0, x1, x2] 

Depth: 1 found: 4 Time: ... s 

[x0, (x0*x2 + x1 + 1)/(x1*x2), (x1 + 1)/x2] 

[(x1 + 1)/x0, x1, (x1 + 1)/x2] 

[(x1 + 1)/x0, (x0*x2 + x1 + 1)/(x0*x1), x2] 

[x0, (x0*x2 + 1)/x1, (x0*x2 + x1 + 1)/(x1*x2)] 

[(x0*x2 + x1 + 1)/(x0*x1), (x0*x2 + 1)/x1, x2] 

Depth: 2 found: 9 Time: ... s 

[(x1 + 1)/x0, (x1^2 + x0*x2 + 2*x1 + 1)/(x0*x1*x2), (x1 + 1)/x2] 

[(x1 + 1)/x0, (x0*x2 + x1 + 1)/(x0*x1), (x1^2 + x0*x2 + 2*x1 + 1)/(x0*x1*x2)] 

[(x1^2 + x0*x2 + 2*x1 + 1)/(x0*x1*x2), (x0*x2 + x1 + 1)/(x1*x2), (x1 + 1)/x2] 

[(x0*x2 + x1 + 1)/(x0*x1), (x0*x2 + 1)/x1, (x0*x2 + x1 + 1)/(x1*x2)] 

Depth: 3 found: 13 Time: ... s 

[(x0*x2 + x1 + 1)/(x1*x2), (x0*x2 + x1 + 1)/(x0*x1), (x1^2 + x0*x2 + 2*x1 + 1)/(x0*x1*x2)] 

Depth: 4 found: 14 Time: ... s 

 

Finite type examples not considered up to equivalence:: 

 

sage: it = S.cluster_class_iter(up_to_equivalence=False) 

sage: len( [ T for T in it ] ) 

84 

 

sage: it = ClusterSeed(['A',2]).cluster_class_iter(up_to_equivalence=False) 

sage: for T in it: print(T) 

[x0, x1] 

[x0, (x0 + 1)/x1] 

[(x1 + 1)/x0, x1] 

[(x1 + 1)/x0, (x0 + x1 + 1)/(x0*x1)] 

[(x0 + x1 + 1)/(x0*x1), (x0 + 1)/x1] 

[(x0 + x1 + 1)/(x0*x1), (x1 + 1)/x0] 

[(x0 + 1)/x1, (x0 + x1 + 1)/(x0*x1)] 

[x1, (x1 + 1)/x0] 

[(x0 + 1)/x1, x0] 

[x1, x0] 

 

Infinite type examples:: 

 

sage: S = ClusterSeed(['A',[1,1],1]) 

sage: it = S.cluster_class_iter() 

sage: next(it) 

[x0, x1] 

sage: next(it) 

[x0, (x0^2 + 1)/x1] 

sage: next(it) 

[(x1^2 + 1)/x0, x1] 

sage: next(it) 

[(x0^4 + 2*x0^2 + x1^2 + 1)/(x0*x1^2), (x0^2 + 1)/x1] 

sage: next(it) 

[(x1^2 + 1)/x0, (x1^4 + x0^2 + 2*x1^2 + 1)/(x0^2*x1)] 

 

sage: it = S.cluster_class_iter(depth=3) 

sage: for T in it: print(T) 

[x0, x1] 

[x0, (x0^2 + 1)/x1] 

[(x1^2 + 1)/x0, x1] 

[(x0^4 + 2*x0^2 + x1^2 + 1)/(x0*x1^2), (x0^2 + 1)/x1] 

[(x1^2 + 1)/x0, (x1^4 + x0^2 + 2*x1^2 + 1)/(x0^2*x1)] 

[(x0^4 + 2*x0^2 + x1^2 + 1)/(x0*x1^2), (x0^6 + 3*x0^4 + 2*x0^2*x1^2 + x1^4 + 3*x0^2 + 2*x1^2 + 1)/(x0^2*x1^3)] 

[(x1^6 + x0^4 + 2*x0^2*x1^2 + 3*x1^4 + 2*x0^2 + 3*x1^2 + 1)/(x0^3*x1^2), (x1^4 + x0^2 + 2*x1^2 + 1)/(x0^2*x1)] 

 

For a cluster seed from an arbitrarily labelled digraph:: 

 

sage: S = ClusterSeed(DiGraph([['a', 'b'], ['b', 'c']]), frozen=['b']) 

sage: S.cluster_class() 

[[a, c], [a, (b + 1)/c], [(b + 1)/a, c], [(b + 1)/a, (b + 1)/c]] 

 

sage: S2 = ClusterSeed(DiGraph([['a', 'b'], ['b', 'c']]), frozen=[]) 

sage: S2.cluster_class() 

[[a, b, c], 

[a, b, (b + 1)/c], 

[a, (a + c)/b, c], 

[(b + 1)/a, b, c], 

[a, (a + c)/b, (a*b + a + c)/(b*c)], 

[(b*c + a + c)/(a*b), (a + c)/b, c], 

[a, (a*b + a + c)/(b*c), (b + 1)/c], 

[(b + 1)/a, b, (b + 1)/c], 

[(b + 1)/a, (b*c + a + c)/(a*b), c], 

[(a*b + b*c + a + c)/(a*b*c), (a*b + a + c)/(b*c), (b + 1)/c], 

[(b + 1)/a, (a*b + b*c + a + c)/(a*b*c), (b + 1)/c], 

[(b + 1)/a, (b*c + a + c)/(a*b), (a*b + b*c + a + c)/(a*b*c)], 

[(b*c + a + c)/(a*b), (a + c)/b, (a*b + b*c + a + c)/(a*b*c)], 

[(a*b + b*c + a + c)/(a*b*c), (a + c)/b, (a*b + a + c)/(b*c)]] 

""" 

mc_iter = self.mutation_class_iter( depth=depth, show_depth=show_depth, up_to_equivalence=up_to_equivalence ) 

for c in mc_iter: 

yield c.cluster() 

 

def cluster_class(self, depth=infinity, show_depth=False, up_to_equivalence=True): 

r""" 

Return the cluster class of ``self`` with respect to certain constraints. 

 

INPUT: 

 

- ``depth`` -- (default: infinity) integer, only seeds with distance at most depth from self are returned 

- ``return_depth`` -- (default False) - if True, ignored if depth is set; returns the depth of the mutation class, i.e., the maximal distance from self of an element in the mutation class 

- ``up_to_equivalence`` -- (default: True) if True, only clusters up to equivalence are considered. 

 

EXAMPLES: 

 

- for examples see :meth:`cluster_class_iter` 

 

TESTS:: 

 

sage: A = ClusterSeed(['A',3]).cluster_class() 

""" 

if depth is infinity and not self.is_finite(): 

raise ValueError('The variable class can - for infinite types - only be computed up to a given depth') 

 

return [ c for c in self.cluster_class_iter(depth=depth, show_depth=show_depth, up_to_equivalence=up_to_equivalence) ] 

 

def b_matrix_class_iter(self, depth=infinity, up_to_equivalence=True): 

r""" 

Returns an iterator through all `B`-matrices in the mutation class of ``self``. 

 

INPUT: 

 

- ``depth`` -- (default:infinity) integer or infinity, only seeds with distance at most depth from self are returned 

- ``up_to_equivalence`` -- (default: True) if True, only 'B'-matrices up to equivalence are considered. 

 

EXAMPLES: 

 

A standard finite type example:: 

 

sage: S = ClusterSeed(['A',4]) 

sage: it = S.b_matrix_class_iter() 

sage: for T in it: print(T) 

[ 0 0 0 1] 

[ 0 0 1 1] 

[ 0 -1 0 0] 

[-1 -1 0 0] 

[ 0 0 0 1] 

[ 0 0 1 0] 

[ 0 -1 0 1] 

[-1 0 -1 0] 

[ 0 0 1 1] 

[ 0 0 0 -1] 

[-1 0 0 0] 

[-1 1 0 0] 

[ 0 0 0 1] 

[ 0 0 -1 1] 

[ 0 1 0 -1] 

[-1 -1 1 0] 

[ 0 0 0 1] 

[ 0 0 -1 0] 

[ 0 1 0 -1] 

[-1 0 1 0] 

[ 0 0 0 -1] 

[ 0 0 -1 1] 

[ 0 1 0 -1] 

[ 1 -1 1 0] 

 

A finite type example with given depth:: 

 

sage: it = S.b_matrix_class_iter(depth=1) 

sage: for T in it: print(T) 

[ 0 0 0 1] 

[ 0 0 1 1] 

[ 0 -1 0 0] 

[-1 -1 0 0] 

[ 0 0 0 1] 

[ 0 0 1 0] 

[ 0 -1 0 1] 

[-1 0 -1 0] 

[ 0 0 1 1] 

[ 0 0 0 -1] 

[-1 0 0 0] 

[-1 1 0 0] 

 

Finite type example not considered up to equivalence:: 

 

sage: S = ClusterSeed(['A',3]) 

sage: it = S.b_matrix_class_iter(up_to_equivalence=False) 

sage: for T in it: print(T) 

[ 0 1 0] 

[-1 0 -1] 

[ 0 1 0] 

[ 0 1 0] 

[-1 0 1] 

[ 0 -1 0] 

[ 0 -1 0] 

[ 1 0 1] 

[ 0 -1 0] 

[ 0 -1 0] 

[ 1 0 -1] 

[ 0 1 0] 

[ 0 -1 1] 

[ 1 0 -1] 

[-1 1 0] 

[ 0 1 -1] 

[-1 0 1] 

[ 1 -1 0] 

[ 0 0 1] 

[ 0 0 -1] 

[-1 1 0] 

[ 0 -1 1] 

[ 1 0 0] 

[-1 0 0] 

[ 0 0 -1] 

[ 0 0 1] 

[ 1 -1 0] 

[ 0 1 -1] 

[-1 0 0] 

[ 1 0 0] 

[ 0 1 1] 

[-1 0 0] 

[-1 0 0] 

[ 0 -1 -1] 

[ 1 0 0] 

[ 1 0 0] 

[ 0 0 -1] 

[ 0 0 -1] 

[ 1 1 0] 

[ 0 0 1] 

[ 0 0 1] 

[-1 -1 0] 

 

Infinite (but finite mutation) type example:: 

 

sage: S = ClusterSeed(['A',[1,2],1]) 

sage: it = S.b_matrix_class_iter() 

sage: for T in it: print(T) 

[ 0 1 1] 

[-1 0 1] 

[-1 -1 0] 

[ 0 -2 1] 

[ 2 0 -1] 

[-1 1 0] 

 

Infinite mutation type example:: 

 

sage: S = ClusterSeed(['E',10]) 

sage: it = S.b_matrix_class_iter(depth=3) 

sage: len ( [T for T in it] ) 

266 

 

For a cluster seed from an arbitrarily labelled digraph:: 

 

sage: S = ClusterSeed(DiGraph([['a', 'b'], ['b', 'c']]), frozen=['b']) 

sage: S.b_matrix_class() 

[ 

[ 0 0] [ 0 0] [0 0] 

[ 0 0] [ 0 0] [0 0] 

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

] 

""" 

Q = self.quiver() 

for M in Q.mutation_class_iter( depth=depth, up_to_equivalence=up_to_equivalence, data_type='matrix' ): 

yield M 

 

def b_matrix_class(self, depth=infinity, up_to_equivalence=True): 

r""" 

Returns all `B`-matrices in the mutation class of ``self``. 

 

INPUT: 

 

- ``depth`` -- (default:infinity) integer or infinity, only seeds with distance at most depth from self are returned 

- ``up_to_equivalence`` -- (default: True) if True, only 'B'-matrices up to equivalence are considered. 

 

EXAMPLES: 

 

- for examples see :meth:`b_matrix_class_iter` 

 

TESTS:: 

 

sage: A = ClusterSeed(['A',3]).b_matrix_class() 

sage: A = ClusterSeed(['A',[2,1],1]).b_matrix_class() 

""" 

if depth is infinity and not self.is_mutation_finite(): 

raise ValueError('The B-matrix class can - for infinite mutation types - only be computed up to a given depth') 

 

return [ M for M in self.b_matrix_class_iter( depth=depth, up_to_equivalence=up_to_equivalence ) ] 

 

def variable_class_iter(self, depth=infinity, ignore_bipartite_belt=False): 

r""" 

Returns an iterator for all cluster variables in the mutation class of ``self``. 

 

INPUT: 

 

- ``depth`` -- (default:infinity) integer, only seeds with distance at most depth from self are returned 

- ``ignore_bipartite_belt`` -- (default:False) if True, the algorithms does not use the bipartite belt 

 

EXAMPLES: 

 

A standard finite type example:: 

 

sage: S = ClusterSeed(['A',3]) 

sage: it = S.variable_class_iter() 

sage: for T in it: print(T) 

x0 

x1 

x2 

(x1 + 1)/x0 

(x1^2 + x0*x2 + 2*x1 + 1)/(x0*x1*x2) 

(x1 + 1)/x2 

(x0*x2 + x1 + 1)/(x0*x1) 

(x0*x2 + 1)/x1 

(x0*x2 + x1 + 1)/(x1*x2) 

 

Finite type examples with given depth:: 

 

sage: it = S.variable_class_iter(depth=1) 

sage: for T in it: print(T) 

Found a bipartite seed - restarting the depth counter at zero and constructing the variable class using its bipartite belt. 

x0 

x1 

x2 

(x1 + 1)/x0 

(x1^2 + x0*x2 + 2*x1 + 1)/(x0*x1*x2) 

(x1 + 1)/x2 

(x0*x2 + x1 + 1)/(x0*x1) 

(x0*x2 + 1)/x1 

(x0*x2 + x1 + 1)/(x1*x2) 

 

Note that the notion of *depth* depends on whether a bipartite seed is found or not, or if it is manually ignored:: 

 

sage: it = S.variable_class_iter(depth=1,ignore_bipartite_belt=True) 

sage: for T in it: print(T) 

x0 

x1 

x2 

(x1 + 1)/x2 

(x0*x2 + 1)/x1 

(x1 + 1)/x0 

 

sage: S.mutate([0,1]) 

sage: it2 = S.variable_class_iter(depth=1) 

sage: for T in it2: print(T) 

(x1 + 1)/x0 

(x0*x2 + x1 + 1)/(x0*x1) 

x2 

(x1^2 + x0*x2 + 2*x1 + 1)/(x0*x1*x2) 

x1 

(x0*x2 + 1)/x1 

 

Infinite type examples:: 

 

sage: S = ClusterSeed(['A',[1,1],1]) 

sage: it = S.variable_class_iter(depth=2) 

sage: for T in it: print(T) 

Found a bipartite seed - restarting the depth counter at zero and constructing the variable class using its bipartite belt. 

x0 

x1 

(x1^2 + 1)/x0 

(x1^4 + x0^2 + 2*x1^2 + 1)/(x0^2*x1) 

(x0^4 + 2*x0^2 + x1^2 + 1)/(x0*x1^2) 

(x0^2 + 1)/x1 

(x1^6 + x0^4 + 2*x0^2*x1^2 + 3*x1^4 + 2*x0^2 + 3*x1^2 + 1)/(x0^3*x1^2) 

(x1^8 + x0^6 + 2*x0^4*x1^2 + 3*x0^2*x1^4 + 4*x1^6 + 3*x0^4 + 6*x0^2*x1^2 + 6*x1^4 + 3*x0^2 + 4*x1^2 + 1)/(x0^4*x1^3) 

(x0^8 + 4*x0^6 + 3*x0^4*x1^2 + 2*x0^2*x1^4 + x1^6 + 6*x0^4 + 6*x0^2*x1^2 + 3*x1^4 + 4*x0^2 + 3*x1^2 + 1)/(x0^3*x1^4) 

(x0^6 + 3*x0^4 + 2*x0^2*x1^2 + x1^4 + 3*x0^2 + 2*x1^2 + 1)/(x0^2*x1^3) 

""" 

mut_iter = self.mutation_class_iter( depth=depth,show_depth=False ) 

var_class = set() 

 

for seed in mut_iter: 

if seed is self: 

seed = ClusterSeed(seed) 

if not ignore_bipartite_belt and seed.is_bipartite(): 

bipartition = seed.is_bipartite(return_bipartition=True) 

bipartition = (list(bipartition[0]),list(bipartition[1])) 

if depth is not infinity: 

print("Found a bipartite seed - restarting the depth counter at zero and constructing the variable class using its bipartite belt.") 

depth_counter = 0 

end = False 

seed2 = ClusterSeed(seed) 

for c in seed.cluster(): 

if c not in var_class: 

yield ClusterVariable( FractionField(seed._R), c.numerator(), c.denominator(), mutation_type=self._mutation_type, variable_type='cluster variable',xdim=seed._n ) 

var_class = var_class.union( seed.cluster()) 

 

init_cluster = set(seed.cluster()) 

while not end and depth_counter < depth: 

depth_counter += 1 

seed.mutate(bipartition[0]) 

seed.mutate(bipartition[1]) 

if set(seed.cluster()) in [set(seed2.cluster()),init_cluster]: 

end = True 

if not end: 

for c in seed.cluster(): 

if c not in var_class: 

yield ClusterVariable( FractionField(seed._R), c.numerator(), c.denominator(), mutation_type=self._mutation_type, variable_type='cluster variable',xdim=seed._n ) 

var_class = var_class.union( seed.cluster() ) 

seed2.mutate(bipartition[1]) 

seed2.mutate(bipartition[0]) 

if set(seed2.cluster()) in [set(seed.cluster()),init_cluster]: 

end = True 

if not end: 

for c in seed2.cluster(): 

if c not in var_class: 

yield ClusterVariable(FractionField(seed._R), c.numerator(), c.denominator(), mutation_type=self._mutation_type, variable_type='cluster variable',xdim=seed._n ) 

var_class = var_class.union(seed2.cluster()) 

return 

else: 

for c in seed.cluster(): 

if c not in var_class: 

yield ClusterVariable( FractionField(seed._R), c.numerator(), c.denominator(), mutation_type=self._mutation_type, variable_type='cluster variable',xdim=seed._n) 

var_class = var_class.union(seed.cluster()) 

 

def variable_class(self, depth=infinity, ignore_bipartite_belt=False): 

r""" 

Returns all cluster variables in the mutation class of ``self``. 

 

INPUT: 

 

- ``depth`` -- (default:infinity) integer, only seeds with distance at most depth from self are returned 

- ``ignore_bipartite_belt`` -- (default:False) if True, the algorithms does not use the bipartite belt 

 

EXAMPLES: 

 

- for examples see :meth:`variable_class_iter` 

 

TESTS:: 

 

sage: A = ClusterSeed(['A',3]).variable_class() 

""" 

if depth is infinity and not self.is_finite(): 

raise ValueError('The variable class can - for infinite types - only be computed up to a given depth') 

 

var_iter = self.variable_class_iter( depth=depth, ignore_bipartite_belt=ignore_bipartite_belt ) 

return sorted(var_iter) 

 

def is_finite(self): 

r""" 

Returns True if ``self`` is of finite type. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]) 

sage: S.is_finite() 

True 

 

sage: S = ClusterSeed(['A',[2,2],1]) 

sage: S.is_finite() 

False 

""" 

mt = self.mutation_type() 

if isinstance(mt, str): 

return False 

else: 

return mt.is_finite() 

 

def is_mutation_finite( self, nr_of_checks=None, return_path=False ): 

r""" 

Returns True if ``self`` is of finite mutation type. 

 

INPUT: 

 

- ``nr_of_checks`` -- (default: None) number of mutations applied. Standard is 500*(number of vertices of self). 

- ``return_path`` -- (default: False) if True, in case of self not being mutation finite, a path from self to a quiver with an edge label (a,-b) and a*b > 4 is returned. 

 

ALGORITHM: 

 

- A cluster seed is mutation infinite if and only if every `b_{ij}*b_{ji} > -4`. Thus, we apply random mutations in random directions 

 

WARNING: 

 

- Uses a non-deterministic method by random mutations in various directions. 

- In theory, it can return a wrong True. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',10]) 

sage: S._mutation_type = None 

sage: S.is_mutation_finite() 

True 

 

sage: S = ClusterSeed([(0,1),(1,2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,8),(2,9)]) 

sage: S.is_mutation_finite() 

False 

""" 

is_finite, path = is_mutation_finite(copy(self._M),nr_of_checks=nr_of_checks) 

if return_path: 

return is_finite, path 

else: 

return is_finite 

 

def mutation_type(self): 

r""" 

Returns the mutation_type of each connected component of ``self``, if it can be determined. 

Otherwise, the mutation type of this component is set to be unknown. 

 

The mutation types of the components are ordered by vertex labels. 

 

WARNING: 

 

- All finite types can be detected, 

- All affine types can be detected, EXCEPT affine type D (the algorithm is not yet implemented) 

- All exceptional types can be detected. 

 

- Might fail to work if it is used within different Sage processes simultaneously (that happened in the doctesting). 

 

EXAMPLES: 

 

- finite types:: 

 

sage: S = ClusterSeed(['A',5]) 

sage: S._mutation_type = S._quiver._mutation_type = None 

sage: S.mutation_type() 

['A', 5] 

 

sage: S = ClusterSeed([(0,1),(1,2),(2,3),(3,4)]) 

sage: S.mutation_type() 

['A', 5] 

 

sage: S = ClusterSeed(DiGraph([['a','b'],['c','b'],['c','d'],['e','d']]), frozen = ['c']) 

sage: S.mutation_type() 

[ ['A', 2], ['A', 2] ] 

 

- affine types:: 

 

sage: S = ClusterSeed(['E',8,[1,1]]); S 

A seed for a cluster algebra of rank 10 of type ['E', 8, [1, 1]] 

sage: S._mutation_type = S._quiver._mutation_type = None; S 

A seed for a cluster algebra of rank 10 

sage: S.mutation_type() # long time 

['E', 8, [1, 1]] 

 

- the not yet working affine type D:: 

 

sage: S = ClusterSeed(['D',4,1]) 

sage: S._mutation_type = S._quiver._mutation_type = None 

sage: S.mutation_type() # todo: not implemented 

['D', 4, 1] 

 

- the exceptional types:: 

 

sage: S = ClusterSeed(['X',6]) 

sage: S._mutation_type = S._quiver._mutation_type = None 

sage: S.mutation_type() # long time 

['X', 6] 

 

- infinite types:: 

 

sage: S = ClusterSeed(['GR',[4,9]]) 

sage: S._mutation_type = S._quiver._mutation_type = None 

sage: S.mutation_type() 

'undetermined infinite mutation type' 

""" 

if self._mutation_type is None: 

if self._quiver is None: 

self.quiver() 

self._mutation_type = self._quiver.mutation_type() 

return self._mutation_type 

 

@rename_keyword(deprecation=19572, method='algorithm') 

def greedy(self, a1, a2, algorithm='by_recursion'): 

r""" 

Returns the greedy element `x[a_1,a_2]` assuming that self is rank two. 

 

The third input can be 'by_recursion', 'by_combinatorics', or 

'just_numbers' to specify if the user wants the element 

computed by the recurrence, combinatorial formula, or wants to 

set `x_1` and `x_2` to be one. 

 

See [LLZ2014]_ for more details. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['R2', [3, 3]]) 

sage: S.greedy(4, 4) 

(x0^12 + x1^12 + 4*x0^9 + 4*x1^9 + 6*x0^6 + 4*x0^3*x1^3 + 6*x1^6 + 4*x0^3 + 4*x1^3 + 1)/(x0^4*x1^4) 

sage: S.greedy(4, 4, 'by_combinatorics') 

(x0^12 + x1^12 + 4*x0^9 + 4*x1^9 + 6*x0^6 + 4*x0^3*x1^3 + 6*x1^6 + 4*x0^3 + 4*x1^3 + 1)/(x0^4*x1^4) 

sage: S.greedy(4, 4, 'just_numbers') 

35 

sage: S = ClusterSeed(['R2', [2, 2]]) 

sage: S.greedy(1, 2) 

(x0^4 + 2*x0^2 + x1^2 + 1)/(x0*x1^2) 

sage: S.greedy(1, 2, 'by_combinatorics') 

(x0^4 + 2*x0^2 + x1^2 + 1)/(x0*x1^2) 

 

TESTS: 

 

We check that :trac:`23688` has been resolved:: 

 

sage: S = ClusterSeed(Matrix([[0,1],[-4,0]])); S 

A seed for a cluster algebra of rank 2 

sage: S.greedy(1,2) 

(x1^4 + x0^2 + 2*x0 + 1)/(x0*x1^2) 

sage: S.greedy(1,2,'by_combinatorics') 

(x1^4 + x0^2 + 2*x0 + 1)/(x0*x1^2) 

""" 

if self.b_matrix().dimensions() == (2, 2): 

b = abs(self.b_matrix()[0, 1]) 

c = abs(self.b_matrix()[1, 0]) 

if algorithm == 'by_recursion': 

ans = self.x(0)**(-a1)*self.x(1)**(-a2) 

for p in range(max(a2, 0)+1): 

for q in range(max(a1, 0)+1): 

if p != 0 or q != 0: 

ans += self._R(coeff_recurs(p, q, a1, a2, b, c))*self.x(0)**(b*p-a1)*self.x(1)**(c*q-a2) 

return(ans) 

elif algorithm == 'by_combinatorics': 

if b == 0: 

S = ClusterSeed([['A', 1], ['A', 1]]) 

else: 

S = ClusterSeed(['R2', [b, b]]) 

ans = 0 

if a1 >= a2: 

PS = PathSubset(a1, a2) 

elif a1 < a2: 

PS = PathSubset(a2, a1) 

from sage.combinat.subset import Subsets 

for T in Subsets(PS): 

if a1 >= a2: 

if is_LeeLiZel_allowable(T, a1, a2, b, c): 

oddT = set(T).intersection(PathSubset(a1, 0)) 

evenT = set(T).symmetric_difference(oddT) 

ans = ans + S.x(0)**(b*len(evenT)) * S.x(1)**(c*len(oddT)) 

elif a1 < a2: 

if is_LeeLiZel_allowable(T, a2, a1, c, b): 

oddT = set(T).intersection(PathSubset(a2, 0)) 

evenT = set(T).symmetric_difference(oddT) 

ans = ans + S.x(0)**(b*len(oddT)) * S.x(1)**(c*len(evenT)) 

ans = ans*S.x(0)**(-a1)*S.x(1)**(-a2) 

return ans 

elif algorithm == 'just_numbers': 

ans = 1 

for p in range(max(a2, 0)+1): 

for q in range(max(a1, 0)+1): 

if p != 0 or q != 0: 

ans += coeff_recurs(p, q, a1, a2, b, c) 

return(ans) 

else: 

raise ValueError("The third input should be 'by_recursion', " 

"'by_combinatorics', or 'just_numbers'.") 

else: 

raise ValueError("Greedy elements are only currently " 

"defined for cluster seeds of rank two.") 

 

def oriented_exchange_graph(self): 

""" 

Return the oriented exchange graph of ``self`` as a directed 

graph. 

 

The seed must be a cluster seed for a cluster algebra of 

finite type with principal coefficients (the corresponding 

quiver must have mutable vertices 0,1,...,n-1). 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A', 2]).principal_extension() 

sage: G = S.oriented_exchange_graph(); G 

Digraph on 5 vertices 

sage: G.out_degree_sequence() 

[2, 1, 1, 1, 0] 

 

sage: S = ClusterSeed(['B', 2]).principal_extension() 

sage: G = S.oriented_exchange_graph(); G 

Digraph on 6 vertices 

sage: G.out_degree_sequence() 

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

 

TESTS:: 

 

sage: S = ClusterSeed(['A',[2,2],1]) 

sage: S.oriented_exchange_graph() 

Traceback (most recent call last): 

... 

TypeError: only works for finite mutation type 

 

sage: S = ClusterSeed(['A', 2]) 

sage: S.oriented_exchange_graph() 

Traceback (most recent call last): 

... 

TypeError: only works for principal coefficients 

""" 

if not self._mutation_type.is_finite(): 

raise TypeError('only works for finite mutation type') 

 

if not self._is_principal: 

raise TypeError('only works for principal coefficients') 

 

covers = [] 

n = self.n() 

stack = [self] 

known_clusters = [] 

while stack: 

i = stack.pop() 

Vari = tuple(sorted(i.cluster())) 

B = i.b_matrix() 

for k in range(n): 

# check if green 

if all(B[i2][k] >= 0 for i2 in range(n, 2 * n)): 

j = i.mutate(k, inplace=False) 

Varj = tuple(sorted(j.cluster())) 

covers.append((Vari, Varj)) 

if not(Varj in known_clusters): 

known_clusters += [Varj] 

stack.append(j) 

 

return DiGraph(covers) 

 

def find_upper_bound(self, verbose=False): 

r""" 

Return the upper bound of the given cluster algebra as a quotient_ring. 

 

The upper bound is the intersection of the Laurent polynomial 

rings of the initial cluster and its neighboring clusters. As 

such, it always contains both the cluster algebra and the 

upper cluster algebra. This function uses the algorithm from 

[MM2015]_. 

 

When the initial seed is totally coprime (for example, when 

the unfrozen part of the exchange matrix has full rank), the 

upper bound is equal to the upper cluster algebra by 

[BFZ2005]_. 

 

.. WARNING:: 

 

The computation time grows rapidly with the size 

of the seed and the number of steps. For most seeds 

larger than four vertices, the algorithm may take an 

infeasible amount of time. Additionally, it will run 

forever without terminating whenever the upper bound is 

infinitely-generated (such as the example in [Spe2013]_). 

 

INPUT: 

 

- ``verbose`` -- (default: ``False``) if ``True``, prints output 

during the computation. 

 

EXAMPLES: 

 

- finite type:: 

 

sage: S = ClusterSeed(['A',3]) 

sage: S.find_upper_bound() 

Quotient of Multivariate Polynomial Ring in x0, x1, x2, x0p, x1p, x2p, z0 over Rational Field by the ideal (x0*x0p - x1 - 1, x1*x1p - x0*x2 - 1, x2*x2p - x1 - 1, x0*z0 - x2p, x1*z0 + z0 - x0p*x2p, x2*z0 - x0p, x1p*z0 + z0 - x0p*x1p*x2p + x1 + 1) 

 

- Markov:: 

 

sage: B = matrix([[0,2,-2],[-2,0,2],[2,-2,0]]) 

sage: S = ClusterSeed(B) 

sage: S.find_upper_bound() 

Quotient of Multivariate Polynomial Ring in x0, x1, x2, x0p, x1p, x2p, z0 over Rational Field by the ideal (x0*x0p - x2^2 - x1^2, x1*x1p - x2^2 - x0^2, x2*x2p - x1^2 - x0^2, x0p*x1p*x2p - x0*x1*x2p - x0*x2*x1p - x1*x2*x0p - 2*x0*x1*x2, x0^3*z0 - x1p*x2p + x1*x2, x0*x1*z0 - x2p - x2, x1^3*z0 - x0p*x2p + x0*x2, x0*x2*z0 - x1p - x1, x1*x2*z0 - x0p - x0, x2^3*z0 - x0p*x1p + x0*x1) 

 

""" 

rank = self.n() 

 

xvars = ['x{}'.format(t) for t in range(rank)] 

xpvars = ['x{}p'.format(t) for t in range(rank)] 

gens = xvars + xpvars 

initial_product = '*'.join(g for g in xvars) 

 

lower_var = self.cluster() 

for t in range(self.b_matrix().nrows()): 

lower_var += [(self.mutate(t, inplace=False)).cluster()[t]] 

 

deep_gens = [initial_product] 

for t in range(rank): 

neighbor_product = '*'.join(xpvars[s] if s == t else xvars[s] 

for s in range(rank)) 

deep_gens += [neighbor_product] 

 

rels = ["-{}*{}+{}".format(gens[t], gens[t + rank], 

lower_var[t + rank].numerator()) 

for t in range(rank)] 

 

while True: 

R = PolynomialRing(QQ, gens, order='invlex') 

I = R.ideal(rels) 

J = R.ideal(initial_product) 

if verbose: 

msg = 'Computing relations among {} generators' 

print(msg.format(len(gens))) 

start = time.time() 

ISat = I.saturation(J)[0] 

spend = time.time() - start 

if verbose: 

msg = 'Computed {} relations in {} seconds' 

print(msg.format(len(ISat.gens()), spend)) 

deep_ideal = R.ideal(deep_gens) + ISat 

initial_product_ideal = R.ideal(initial_product) + ISat 

if verbose: 

print('Attempting to find a new element of the upper bound') 

start = time.time() 

M = initial_product_ideal.saturation(deep_ideal)[0] 

spend = time.time() - start 

if M == initial_product_ideal: 

if verbose: 

print('Verified that there are no new elements in', spend, 'seconds') 

print('Returning a presentation for the upper bound') 

return R.quotient_ring(ISat) 

else: 

gens.append('z' + str(len(gens) - 2 * rank)) 

new_gen_found = False 

i = 0 

M_gens = M.gens() 

while (not new_gen_found) and i < len(M_gens): 

f = initial_product_ideal.reduce(M_gens[i]) 

if f != 0: 

rels.append('z' + str(len(gens) - 2 * rank - 1) + '*' + initial_product + '-(' + str(f) + ')') 

new_gen_found = True 

if verbose: 

print('Found a new element in', spend, 'seconds!') 

print('') 

i += 1 

 

def get_upper_cluster_algebra_element(self,a): 

r""" 

Computes an element in the upper cluster algebra of `B` corresponding to the vector `a \in \mathbb{Z}^n`. 

 

See [LeeLiM]_ for more details.  

 

INPUT: 

 

- `B` -- a skew-symmetric matrix. Must have the same number of columns as the length of the vectors in `vd`. 

- `a` -- a vector in `\mathbb{Z}^n` where `n` is the number of columns in `B`. 

 

OUTPUT: 

 

Returns an element in the upper cluster algebra. Depending on the input it may or may not be irreducible. 

 

EXAMPLES:: 

 

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

sage: C=ClusterSeed(B) 

sage: C.get_upper_cluster_algebra_element([1,1,0]) 

(x0^3*x2^3*x3*x4 + x2^6*x3 + x1^3*x2^3)/(x0*x1) 

sage: C.get_upper_cluster_algebra_element([1,1,1]) 

x0^2*x1^2*x2^2*x3*x4*x5 + x0^2*x1^2*x2^2 

 

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

sage: C=ClusterSeed(B) 

sage: C.get_upper_cluster_algebra_element([1,1,0]) 

(x1^3*x2^3 + x0^3 + x2^3)/(x0*x1) 

sage: C.get_upper_cluster_algebra_element([1,1,1]) 

(x0^3*x1^3 + x1^3*x2^3 + x0^3 + x2^3)/(x0*x1*x2) 

 

sage: B=matrix([[0,2],[-3,0],[4,-5]]) 

sage: C=ClusterSeed(B) 

sage: C.get_upper_cluster_algebra_element([1,1]) 

(x2^9 + x1^3*x2^5 + x0^2*x2^4)/(x0*x1) 

 

sage: B=matrix([[0,3,-5],[-3,0,4],[5,-4,0]]) 

sage: C=ClusterSeed(B) 

sage: C.get_upper_cluster_algebra_element([1,1,1]) 

x0^4*x1^2*x2^3 + x0^2*x1^3*x2^4 

 

 

REFERENCES: 

 

.. [LeeLiM] Lee-Li-Mills, A combinatorial formula for certain elements in the upper cluster algebra, :arxiv:`1409.8177` 

 

""" 

B=self.b_matrix() 

#Checks if the length of the 

if len(a) != B.ncols(): 

raise ValueError('The length of the input vector must be the same as the number of columns of B.') 

#Runs helper functions. 

v=_vector_decomposition(a,B.nrows()) 

c=self._compute_compatible_vectors(v) 

return self._produce_upper_cluster_algebra_element(v,c) 

 

def LLM_gen_set(self,size_limit=-1): 

r""" 

Produce a list of upper cluster algebra elements corresponding to all 

vectors in `\{0,1\}^n`.  

 

INPUT: 

 

- `B` -- a skew-symmetric matrigitx. 

- `size_limit` -- a limit on how many vectors you want the function to return.  

 

OUTPUT: 

 

An array of elements in the upper cluster algebra.  

 

EXAMPLES:: 

 

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

sage: C = ClusterSeed(B) 

sage: C.LLM_gen_set() 

[1, 

(x1 + x3)/x0, 

(x0*x4 + x2)/x1, 

(x0*x3*x4 + x1*x2 + x2*x3)/(x0*x1), 

(x1*x5 + 1)/x2, 

(x1^2*x5 + x1*x3*x5 + x1 + x3)/(x0*x2), 

(x0*x1*x4*x5 + x0*x4 + x2)/(x1*x2), 

(x0*x1*x3*x4*x5 + x0*x3*x4 + x1*x2 + x2*x3)/(x0*x1*x2)] 

""" 

from sage.modules.free_module import VectorSpace 

from sage.rings.finite_rings.finite_field_constructor import GF 

B = self.b_matrix() 

aSet = VectorSpace(GF(2), B.ncols()).list() 

genSet = [] 

for i in range(len(aSet)): 

if i == size_limit: 

break 

a = aSet[i] 

genSet.append(self.get_upper_cluster_algebra_element(a)) 

return (genSet) 

 

def _compute_compatible_vectors(self,vd): 

r""" 

Return a list of compatible vectors of each vector in the vector decomposition `vd`. 

 

Compatibility is defined as in [LLM]_ with respect to the matrix `B`. 

 

INPUT: 

 

- `B` -- a skew-symmetric matrix. Must have the same number of columns as the length of the vectors in `vd`. 

- `vd` -- a collection of tuples `(v,z)` with `v \in \{0,1\}^n` and `z \in \mathbb{Z}`. 

`n` must be the number of columns in `B`. Taken from the output of vector_decomposition. 

 

OUTPUT: 

 

Returns an a 2-dimensional array containing all the vectors compatible with each vector in `vd.`  

 

NOTE: 

 

If the vector in `vd` is negative it will not have any compatible vectors, so it does not contribute to the list. 

 

EXAMPLES:: 

 

sage: from sage.combinat.cluster_algebra_quiver.cluster_seed import _vector_decomposition 

 

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

sage: C=ClusterSeed(B) 

sage: v=_vector_decomposition([3,2,3,4],4) 

sage: C._compute_compatible_vectors(v) 

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

[[0, 0, 0, 0], 

[0, 0, 0, 1], 

[0, 0, 1, 1], 

[1, 0, 0, 0], 

[1, 0, 0, 1], 

[1, 0, 1, 1]], 

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

 

 

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

sage: C=ClusterSeed(B) 

sage: v=_vector_decomposition([2,-1,3,-2],4) 

sage: C._compute_compatible_vectors(v) 

[[], 

[], 

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

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

""" 

from sage.modules.free_module import VectorSpace 

from sage.rings.finite_rings.finite_field_constructor import GF 

B = self.b_matrix() 

# E is the set of 'edges' in the quiver. It records the tuple 

# of indices `(i,j)` if `b_{ij} > 0`. 

E = [] 

# Checks the upper triangular part of the exchange graph. 

num_cols = B.ncols() 

num_rows = B.nrows() 

for j in range(num_cols): 

for i in range(j, num_rows): 

if B[i][j] > 0: 

E.append([i, j]) 

elif B[i][j] < 0: 

E.append([j, i]) 

# Checks for edges to frozen vertices.  

num_frozens = num_rows - num_cols 

for k in range(num_frozens): 

for j in range(i, num_cols): 

if B[k + num_cols][j] > 0: 

E.append([i, j]) 

elif B[i][j] < 0: 

E.append([j, i]) 

 

# For each vector a in vd. check if a vector s in {0,1}^n is compatible. 

compatibleList = [] 

psetvect_temp = list(itertools.product([0,1],repeat=num_cols)) 

psetvect = [] 

for p_tuple in psetvect_temp: 

p = list(p_tuple) 

while len(p) < len(vd[0][0]): 

p.append(0) 

psetvect.append(p) 

 

for a in vd: 

negative = False 

for m in range(len(a)): 

# If the vector a in vd is non-positive it is not compatible 

# with any vector. 0 vector will pass this check but will be 

# handled later. 

if a[m] < 0: 

negative = True 

break 

if negative: 

continue 

clist = [] 

for s in psetvect: 

pass1 = True 

#The first possible failure for compatibility is if any entry in s is larger than the corresponding entry of a. 

#Only checks for the mutable vertices since all entries in a_i i>num_cols are zero.  

for k in range(num_cols): 

if s[k] > a[0][k]: 

pass1 = False 

break 

#The second possible failure is if (s_i,a_j-s_j) = (1,1). 

if pass1: 

for e in E: 

if s[e[0]] == 1 and (a[0][e[1]]-s[e[1]]) == 1: 

pass1 = False 

break 

if pass1: 

clist.append(s) 

compatibleList.append(clist) 

return compatibleList 

 

def _produce_upper_cluster_algebra_element(self, vd, cList): 

r""" 

Takes the compatible vectors and uses them to produce a Laurent polynomial in the upper cluster algebra.  

 

EXAMPLES:: 

 

sage: from sage.combinat.cluster_algebra_quiver.cluster_seed import _vector_decomposition 

 

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

sage: C = ClusterSeed(B) 

sage: v = _vector_decomposition([1,2,1,2],8) 

sage: c = C._compute_compatible_vectors(v) 

sage: C._produce_upper_cluster_algebra_element(v,c) 

(x0^2*x1^3*x4*x5^2*x6*x7^2 + x0*x1^2*x2*x3*x4*x5*x6*x7 + 2*x0^2*x1^2*x4*x5^2*x6*x7 + x0^2*x1^2*x4*x5^2*x7^2 + x0*x1*x2*x3*x4*x5*x6 + x0^2*x1*x4*x5^2*x6 + x0*x1^2*x2*x3*x5*x7 + 2*x0*x1*x2*x3*x4*x5*x7 + 2*x0^2*x1*x4*x5^2*x7 + x1*x2^2*x3^2 + x2^2*x3^2*x4 + x0*x1*x2*x3*x5 + 2*x0*x2*x3*x4*x5 + x0^2*x4*x5^2)/(x0*x1^2*x2*x3^2) 

 

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

sage: C = ClusterSeed(B) 

sage: v = _vector_decomposition([2,-1,3,-2],4) 

sage: c = C._compute_compatible_vectors(v) 

sage: C._produce_upper_cluster_algebra_element(v,c) 

(x0^3*x1^4*x3^2 + 2*x0^2*x1^4*x2*x3^2 + x0*x1^4*x2^2*x3^2 + 3*x0^2*x1^3*x3^2 + 4*x0*x1^3*x2*x3^2 + x1^3*x2^2*x3^2 + 3*x0*x1^2*x3^2 + 2*x1^2*x2*x3^2 + 

x1*x3^2)/(x0^2*x2^3) 

""" 

B = self.b_matrix() 

#Creates a the fraction field of a polynomial ring in which to build the Laurent polynomials. 

num_cols = B.ncols() 

num_rows = B.nrows() 

R = PolynomialRing(QQ, num_rows, 'x') 

#Computes the Laurent Polynomial for each vector in the decomposition. 

finalP = [] 

#Laurent polynomial for each vector in {0,1}^n 

for i in range(len(vd)): 

final = 1 

numerator = 0 

if cList[i] != []: 

#If the vector in vd is negative then it did not contribute any compatible vectors. It will only contribute a Laurent monomial. This is the case when cList[i]=[] 

#Each compatible sequence gives a term in the numerator of the Laurent polynomial. 

for s in cList[i]: 

term = 1 

#Calulates the monomial in the term.  

for j in range(num_rows): 

x = R.gen(j) 

expn = 0 

#The exponent is determined by the vectors a,s, and the matrix B. 

for k in range(num_cols): 

expn += (vd[i][0][k]-s[k])*max(0, B[j][k])+s[k]*max(0, -B[j][k]) 

term *= x ** expn 

numerator += term 

#Gives a numerator for the negative vector, or else the product would be zero.  

else: 

numerator = 1 

 

#Uses the vectors in vd to calculates the denominator of the Laurent.  

denominator = 1 

for l in range(num_cols): 

denominator = denominator * (R.gen(l))**vd[i][0][l] 

#Each copy of a vector in vd contributes a factor of the Laurent polynomial calculated from it.  

final = (numerator/denominator)**vd[i][1] 

finalP.append(final) 

laurentP = 1 

#The UCA element for the vector a is the product of the elements produced from the vectors in its decomposition.  

for p in finalP: 

laurentP *= p 

return laurentP 

 

def _bino(n, k): 

""" 

Binomial coefficient which we define as zero for negative n. 

 

EXAMPLES:: 

 

sage: from sage.combinat.cluster_algebra_quiver.cluster_seed import _bino 

sage: _bino(3, 2) 

3 

sage: _bino(-3, 2) 

0 

""" 

if n >= 0: 

from sage.arith.all import binomial 

return binomial(n, k) 

else: 

return 0 

 

 

def coeff_recurs(p, q, a1, a2, b, c): 

""" 

Coefficients in Laurent expansion of greedy element, as defined by recursion. 

 

EXAMPLES:: 

 

sage: from sage.combinat.cluster_algebra_quiver.cluster_seed import coeff_recurs 

sage: coeff_recurs(1, 1, 5, 5, 3, 3) 

10 

""" 

if p == 0 and q == 0: 

return 1 

elif p < 0 or q < 0: 

return 0 

else: 

if c*a1*q <= b*a2*p: 

return sum((-1)**(k-1)*coeff_recurs(p-k, q, a1, a2, b, c)*_bino(a2-c*q+k-1, k) 

for k in range(1, p+1)) 

else: 

return sum((-1)**(k-1)*coeff_recurs(p, q-k, a1, a2, b, c)*_bino(a1-b*p+k-1, k) 

for k in range(1, q+1)) 

 

def PathSubset(n,m): 

r""" 

Encodes a *maximal* Dyck path from (0,0) to (n,m) (for n >= m >= 0) as a subset of {0,1,2,..., 2n-1}. 

The encoding is given by indexing horizontal edges by odd numbers and vertical edges by evens. 

 

The horizontal between (i,j) and (i+1,j) is indexed by the odd number 2*i+1. 

The vertical between (i,j) and (i,j+1) is indexed by the even number 2*j. 

 

EXAMPLES:: 

 

sage: from sage.combinat.cluster_algebra_quiver.cluster_seed import PathSubset 

sage: PathSubset(4,0) 

{1, 3, 5, 7} 

sage: PathSubset(4,1) 

{1, 3, 5, 6, 7} 

sage: PathSubset(4,2) 

{1, 2, 3, 5, 6, 7} 

sage: PathSubset(4,3) 

{1, 2, 3, 4, 5, 6, 7} 

sage: PathSubset(4,4) 

{0, 1, 2, 3, 4, 5, 6, 7} 

""" 

from sage.misc.misc import union 

from sage.functions.other import floor 

S = [ ] 

for i in range(n): 

S = union(S, [2*i+1]) 

if m > 0: 

for j in range(n): 

if floor((j+1)*m/n) - floor(j*m/n) == 1: 

S = union(S, [2*j]) 

return set(S) 

 

 

def SetToPath(T): 

r""" 

Rearranges the encoding for a *maximal* Dyck path (as a set) so that it is a list in the proper order of the edges. 

 

EXAMPLES:: 

 

sage: from sage.combinat.cluster_algebra_quiver.cluster_seed import PathSubset 

sage: from sage.combinat.cluster_algebra_quiver.cluster_seed import SetToPath 

sage: SetToPath(PathSubset(4,0)) 

[1, 3, 5, 7] 

sage: SetToPath(PathSubset(4,1)) 

[1, 3, 5, 7, 6] 

sage: SetToPath(PathSubset(4,2)) 

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

sage: SetToPath(PathSubset(4,3)) 

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

sage: SetToPath(PathSubset(4,4)) 

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

""" 

n = (max(T)+1) // 2 

ans = [1] 

for i in range(n-1): 

if 2*i in T: 

ans.append(2*i) 

ans.append(2*i+3) 

if 2*n-2 in T: 

ans.append(2*n-2) 

return ans 

 

def is_LeeLiZel_allowable(T,n,m,b,c): 

""" 

Check if the subset T contributes to the computation of the greedy 

element x[m,n] in the rank two (b,c)-cluster algebra. 

 

This uses the conditions of Lee-Li-Zelevinsky's paper [LLZ2014]_. 

 

EXAMPLES:: 

 

sage: from sage.combinat.cluster_algebra_quiver.cluster_seed import is_LeeLiZel_allowable 

sage: is_LeeLiZel_allowable({1,3,2,5,7,6},4,2,6,6) 

False 

sage: is_LeeLiZel_allowable({1,2,5},3,3,1,1) 

True 

""" 

horiz = set(T).intersection( PathSubset(n, 0)) 

vert = set(T).symmetric_difference(horiz) 

if len(horiz) == 0 or len(vert) == 0: 

return True 

else: 

Latt = SetToPath(PathSubset(n, m)) 

for u in horiz: 

from sage.combinat.words.word import Word 

from sage.modules.free_module_element import vector 

WW = Word(Latt) 

LattCycled = vector(WW.conjugate(Latt.index(u))).list() 

for v in vert: 

uv_okay = False 

for A in range(LattCycled.index(v)): 

EA = [] 

AF = copy(LattCycled) 

for i in range(LattCycled.index(v), len(LattCycled)-1): 

AF.pop() 

AF.reverse() 

for i in range(A+1): 

EA.append(LattCycled[i]) 

AF.pop() 

AF.reverse() 

nAF1 = 0 

for i in range(len(AF)): 

if AF[i] % 2 == 1: 

nAF1 += 1 

nAF2 = 0 

for i in range(len(AF)): 

if AF[i] % 2 == 0 and AF[i] in vert: 

nAF2 += 1 

nEA2 = 0 

for i in range(len(EA)): 

if EA[i] % 2 == 0: 

nEA2 += 1 

nEA1 = 0 

for i in range(len(EA)): 

if EA[i] % 2 == 1 and EA[i] in horiz: 

nEA1 += 1 

if nAF1 == b*nAF2 or nEA2 == c*nEA1: 

uv_okay = True 

if not uv_okay: 

return False 

return True 

 

 

def get_green_vertices(C): 

r""" 

Get the green vertices from a matrix. Will go through each column and return 

the ones where no entry is greater than 0. 

 

INPUT: 

 

- ``C`` -- The C matrix to check 

 

EXAMPLES:: 

 

sage: from sage.combinat.cluster_algebra_quiver.cluster_seed import get_green_vertices 

sage: S = ClusterSeed(['A',4]); S.mutate([1,2,3,2,0,1,2,0,3]) 

sage: get_green_vertices(S.c_matrix()) 

[0, 3] 

 

""" 

return [ i for (i,v) in enumerate(C.columns()) if any(x > 0 for x in v) ] 

## old code commented out 

#import numpy as np 

#max_entries = [ np.max(np.array(C.column(i))) for i in range(C.ncols()) ] 

#return [i for i in range(C.ncols()) if max_entries[i] > 0] 

 

 

def get_red_vertices(C): 

r""" 

Get the red vertices from a matrix. 

 

Will go through each column and return 

the ones where no entry is less than 0. 

 

INPUT: 

 

- ``C`` -- The C matrix to check 

 

EXAMPLES:: 

 

sage: from sage.combinat.cluster_algebra_quiver.cluster_seed import get_red_vertices 

sage: S = ClusterSeed(['A',4]); S.mutate([1,2,3,2,0,1,2,0,3]) 

sage: get_red_vertices(S.c_matrix()) 

[1, 2] 

""" 

return [ i for (i,v) in enumerate(C.columns()) if any(x < 0 for x in v) ] 

## old code commented out 

#import numpy as np 

#min_entries = [ np.min(np.array(C.column(i))) for i in range(C.ncols()) ] 

#return [i for i in range(C.ncols()) if min_entries[i] < 0] 

 

 

def _vector_decomposition(a, length): 

r""" 

Decomposes an integer vector. 

 

INPUT: 

 

- `a` -- a vector in `\mathbb{Z}^n.` 

 

OUTPUT: 

 

A decomposition of `a` into vectors `b_i \in \{0,1\}^n` such that `a= \sum c_i b_i` for `c_i \in \mathbb{Z}.` 

Returns an array of tuples `\right[b_i,c_i\left].`  

 

EXAMPLES:: 

 

sage: from sage.combinat.cluster_algebra_quiver.cluster_seed import _vector_decomposition 

sage: _vector_decomposition([2,-1,3,-2],4) 

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

sage: _vector_decomposition([3,2,3,4],4) 

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

""" 

 

multiList = [] 

a_plus=[] 

for i in range(len(a)): 

if a[i]<0: 

a_plus.append(0) 

#create a vector with i-th coordinate -1 

temp=[0]*length;temp[i]=-1 

multiList.append([tuple(temp),-a[i]]) 

else: 

a_plus.append(a[i]) 

 

#Finds the difference between the largest and smallest entry in the vector to determine the how many vectors are in the decomposition 

max = 0 

min = 0 

for i in range(len(a_plus)): 

if a_plus[i] > max: 

max = a_plus[i] 

if a_plus[i] < min: 

min = a_plus[i] 

diff = max - min 

 

#Creates a copy of a that will be edited when decomposing the vector.  

ap = copy(a_plus) 

if max == 0 and min == 0: 

ap = [] 

for i in range(length): 

ap.append(0) 

return [[ap, 1]] 

#Resets the counter i and puts the integer partition of the ith component of a into an array.  

i = 0 

cols = [] 

for i in range(len(a_plus)): 

c = [] 

for j in range(diff): 

if ap[i] > 0: 

c.append(1) 

ap[i] -= 1 

elif ap[i] < 0: 

c.append(-1) 

ap[i] += 1 

elif ap[i] == 0: 

c.append(0) 

cols.append(c) 

#Converts the integer partitions into decomposition vectors. 

i = 0 

for i in range(len(cols)): 

if cols[i][0] < 0: 

cols[i].reverse() 

mat = matrix(cols) 

#Adds a zero to the end of every vector for each frozen vertex.  

froz_mat = matrix(length-mat.nrows(),mat.ncols()) 

mat = mat.stack(froz_mat) 

mat = mat.transpose() 

vects = mat.rows() 

#Collects identical decomposition vectors and counts their multiplicities.  

while(len(vects) > 0): 

vect = vects[0] 

count = vects.count(vect) 

multiList.append([vect, count]) 

i = 0 

for i in range(count): 

vects.remove(vect) 

return multiList 

 

 

def _power_set(n): 

r""" 

Returns an array of all vectors in `\{0,1\}^n`. 

 

INPUT: 

 

- `n` -- an integer. 

 

OUTPUT:  

 

A 2-dimensional array containing all elements of `\{0,1\}^n`. 

 

EXAMPLES:: 

 

sage: from sage.combinat.cluster_algebra_quiver.cluster_seed import _power_set 

 

sage: _power_set(2) 

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

 

sage: _power_set(5) 

[[0, 0, 0, 0, 0], 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

[1, 1, 1, 1, 1]] 

 

""" 

p = _multi_concatenate([[]], [0, 1]) 

for i in range(n - 1): 

p = _multi_concatenate(p, [0, 1]) 

return p 

 

 

def _multi_concatenate(l1, l2): 

r""" 

Each element of `l2` gets added to the end of a copy of each array in `l1`. 

Used to produce the power set. 

 

INPUT: 

 

-`l1` -- a 2-dimensional array. 

-`l2` -- a single array. 

 

OUTPUT: 

 

A 2-dimensional array. 

 

EXAMPLES:: 

 

sage: from sage.combinat.cluster_algebra_quiver.cluster_seed import _multi_concatenate 

 

sage: _multi_concatenate([[0,1,2]],[3,4,5]) 

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

 

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

[[0, 1, 2, 6], 

[0, 1, 2, 7], 

[0, 1, 2, 8], 

[3, 4, 5, 6], 

[3, 4, 5, 7], 

[3, 4, 5, 8]]  

""" 

plist = [] 

for i in l1: 

for j in l2: 

ip = copy(i) 

ip.append(j) 

plist.append(ip) 

return plist 

 

 

class ClusterVariable(FractionFieldElement): 

r""" 

This class is a thin wrapper for cluster variables in cluster seeds. 

 

It provides the extra feature to store if a variable is frozen or not. 

 

- the associated positive root:: 

 

sage: S = ClusterSeed(['A',3]) 

sage: for T in S.variable_class_iter(): 

....: print("{} {}".format(T, T.almost_positive_root())) 

x0 -alpha[1] 

x1 -alpha[2] 

x2 -alpha[3] 

(x1 + 1)/x0 alpha[1] 

(x1^2 + x0*x2 + 2*x1 + 1)/(x0*x1*x2) alpha[1] + alpha[2] + alpha[3] 

(x1 + 1)/x2 alpha[3] 

(x0*x2 + x1 + 1)/(x0*x1) alpha[1] + alpha[2] 

(x0*x2 + 1)/x1 alpha[2] 

(x0*x2 + x1 + 1)/(x1*x2) alpha[2] + alpha[3] 

""" 

def __init__( self, parent, numerator, denominator, coerce=True, reduce=True, mutation_type=None, variable_type=None, xdim=0 ): 

r""" 

Initialize a cluster variable in the same way that elements in the field of rational functions are initialized. 

 

.. SEEALSO:: :class:`Fraction Field of Multivariate Polynomial Ring` 

 

TESTS:: 

 

sage: S = ClusterSeed(['A',2]) 

sage: for f in S.cluster(): 

....: print(type(f)) 

<class 'sage.combinat.cluster_algebra_quiver.cluster_seed.ClusterVariable'> 

<class 'sage.combinat.cluster_algebra_quiver.cluster_seed.ClusterVariable'> 

 

sage: S.variable_class() 

[(x0 + x1 + 1)/(x0*x1), (x1 + 1)/x0, (x0 + 1)/x1, x1, x0] 

""" 

FractionFieldElement.__init__( self, parent, numerator, denominator, coerce=coerce, reduce=reduce ) 

self._n = xdim; 

self._mutation_type = mutation_type 

self._variable_type = variable_type 

 

def almost_positive_root( self ): 

r""" 

Returns the *almost positive root* associated to ``self`` if ``self`` is of finite type. 

 

EXAMPLES:: 

 

sage: S = ClusterSeed(['A',3]) 

sage: for T in S.variable_class_iter(): 

....: print("{} {}".format(T, T.almost_positive_root())) 

x0 -alpha[1] 

x1 -alpha[2] 

x2 -alpha[3] 

(x1 + 1)/x0 alpha[1] 

(x1^2 + x0*x2 + 2*x1 + 1)/(x0*x1*x2) alpha[1] + alpha[2] + alpha[3] 

(x1 + 1)/x2 alpha[3] 

(x0*x2 + x1 + 1)/(x0*x1) alpha[1] + alpha[2] 

(x0*x2 + 1)/x1 alpha[2] 

(x0*x2 + x1 + 1)/(x1*x2) alpha[2] + alpha[3] 

""" 

if self._variable_type == 'frozen variable': 

raise ValueError('The variable is frozen.') 

if isinstance(self._mutation_type, str): 

raise ValueError('The cluster algebra for %s is not of finite type.'%self._repr_()) 

else: 

if self._mutation_type is None: 

self._mutation_type = self.parent().mutation_type() 

if self._mutation_type.is_finite(): 

from sage.combinat.root_system.root_system import RootSystem 

# the import above is used in the line below 

mt = self._mutation_type._repr_() 

# mt is a string of the shape "['A', 15]" 

# where A is a single letter and 15 is an integer 

Phi = RootSystem([mt[2: 3], ZZ(mt[6: -1])]) 

Phiplus = Phi.root_lattice().simple_roots() 

 

if self.denominator() == 1: 

return -Phiplus[ self.numerator().degrees().index(1) + 1 ] 

else: 

root = self.denominator().degrees() 

return sum( [ root[i]*Phiplus[ i+1 ] for i in range(self._n) ] ) 

else: 

raise ValueError('The cluster algebra for %s is not of finite type.'%self._repr_())