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

from __future__ import print_function, division 

 

from random import randrange, choice 

from math import log 

 

from sympy.core import Basic 

from sympy.core.compatibility import range 

from sympy.combinatorics import Permutation 

from sympy.combinatorics.permutations import (_af_commutes_with, _af_invert, 

_af_rmul, _af_rmuln, _af_pow, Cycle) 

from sympy.combinatorics.util import (_check_cycles_alt_sym, 

_distribute_gens_by_base, _orbits_transversals_from_bsgs, 

_handle_precomputed_bsgs, _base_ordering, _strong_gens_from_distr, 

_strip, _strip_af) 

from sympy.functions.combinatorial.factorials import factorial 

from sympy.ntheory import sieve 

from sympy.utilities.iterables import has_variety, is_sequence, uniq 

from sympy.utilities.randtest import _randrange 

from itertools import islice 

 

rmul = Permutation.rmul_with_af 

_af_new = Permutation._af_new 

 

 

class PermutationGroup(Basic): 

"""The class defining a Permutation group. 

 

PermutationGroup([p1, p2, ..., pn]) returns the permutation group 

generated by the list of permutations. This group can be supplied 

to Polyhedron if one desires to decorate the elements to which the 

indices of the permutation refer. 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> Permutation.print_cyclic = True 

>>> from sympy.combinatorics.permutations import Cycle 

>>> from sympy.combinatorics.polyhedron import Polyhedron 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

 

The permutations corresponding to motion of the front, right and 

bottom face of a 2x2 Rubik's cube are defined: 

 

>>> F = Permutation(2, 19, 21, 8)(3, 17, 20, 10)(4, 6, 7, 5) 

>>> R = Permutation(1, 5, 21, 14)(3, 7, 23, 12)(8, 10, 11, 9) 

>>> D = Permutation(6, 18, 14, 10)(7, 19, 15, 11)(20, 22, 23, 21) 

 

These are passed as permutations to PermutationGroup: 

 

>>> G = PermutationGroup(F, R, D) 

>>> G.order() 

3674160 

 

The group can be supplied to a Polyhedron in order to track the 

objects being moved. An example involving the 2x2 Rubik's cube is 

given there, but here is a simple demonstration: 

 

>>> a = Permutation(2, 1) 

>>> b = Permutation(1, 0) 

>>> G = PermutationGroup(a, b) 

>>> P = Polyhedron(list('ABC'), pgroup=G) 

>>> P.corners 

(A, B, C) 

>>> P.rotate(0) # apply permutation 0 

>>> P.corners 

(A, C, B) 

>>> P.reset() 

>>> P.corners 

(A, B, C) 

 

Or one can make a permutation as a product of selected permutations 

and apply them to an iterable directly: 

 

>>> P10 = G.make_perm([0, 1]) 

>>> P10('ABC') 

['C', 'A', 'B'] 

 

See Also 

======== 

 

sympy.combinatorics.polyhedron.Polyhedron, 

sympy.combinatorics.permutations.Permutation 

 

References 

========== 

 

[1] Holt, D., Eick, B., O'Brien, E. 

"Handbook of Computational Group Theory" 

 

[2] Seress, A. 

"Permutation Group Algorithms" 

 

[3] http://en.wikipedia.org/wiki/Schreier_vector 

 

[4] http://en.wikipedia.org/wiki/Nielsen_transformation 

#Product_replacement_algorithm 

 

[5] Frank Celler, Charles R.Leedham-Green, Scott H.Murray, 

Alice C.Niemeyer, and E.A.O'Brien. "Generating Random 

Elements of a Finite Group" 

 

[6] http://en.wikipedia.org/wiki/Block_%28permutation_group_theory%29 

 

[7] http://www.algorithmist.com/index.php/Union_Find 

 

[8] http://en.wikipedia.org/wiki/Multiply_transitive_group#Multiply_transitive_groups 

 

[9] http://en.wikipedia.org/wiki/Center_%28group_theory%29 

 

[10] http://en.wikipedia.org/wiki/Centralizer_and_normalizer 

 

[11] http://groupprops.subwiki.org/wiki/Derived_subgroup 

 

[12] http://en.wikipedia.org/wiki/Nilpotent_group 

 

[13] http://www.math.colostate.edu/~hulpke/CGT/cgtnotes.pdf 

 

""" 

is_group = True 

 

def __new__(cls, *args, **kwargs): 

"""The default constructor. Accepts Cycle and Permutation forms. 

Removes duplicates unless ``dups`` keyword is False. 

""" 

if not args: 

args = [Permutation()] 

else: 

args = list(args[0] if is_sequence(args[0]) else args) 

if any(isinstance(a, Cycle) for a in args): 

args = [Permutation(a) for a in args] 

if has_variety(a.size for a in args): 

degree = kwargs.pop('degree', None) 

if degree is None: 

degree = max(a.size for a in args) 

for i in range(len(args)): 

if args[i].size != degree: 

args[i] = Permutation(args[i], size=degree) 

if kwargs.pop('dups', True): 

args = list(uniq([_af_new(list(a)) for a in args])) 

obj = Basic.__new__(cls, *args, **kwargs) 

obj._generators = args 

obj._order = None 

obj._center = [] 

obj._is_abelian = None 

obj._is_transitive = None 

obj._is_sym = None 

obj._is_alt = None 

obj._is_primitive = None 

obj._is_nilpotent = None 

obj._is_solvable = None 

obj._is_trivial = None 

obj._transitivity_degree = None 

obj._max_div = None 

obj._r = len(obj._generators) 

obj._degree = obj._generators[0].size 

 

# these attributes are assigned after running schreier_sims 

obj._base = [] 

obj._strong_gens = [] 

obj._basic_orbits = [] 

obj._transversals = [] 

 

# these attributes are assigned after running _random_pr_init 

obj._random_gens = [] 

return obj 

 

def __getitem__(self, i): 

return self._generators[i] 

 

def __contains__(self, i): 

"""Return True if `i` is contained in PermutationGroup. 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation, PermutationGroup 

>>> p = Permutation(1, 2, 3) 

>>> Permutation(3) in PermutationGroup(p) 

True 

 

""" 

if not isinstance(i, Permutation): 

raise TypeError("A PermutationGroup contains only Permutations as " 

"elements, not elements of type %s" % type(i)) 

return self.contains(i) 

 

def __len__(self): 

return len(self._generators) 

 

def __eq__(self, other): 

"""Return True if PermutationGroup generated by elements in the 

group are same i.e they represent the same PermutationGroup. 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> p = Permutation(0, 1, 2, 3, 4, 5) 

>>> G = PermutationGroup([p, p**2]) 

>>> H = PermutationGroup([p**2, p]) 

>>> G.generators == H.generators 

False 

>>> G == H 

True 

 

""" 

if not isinstance(other, PermutationGroup): 

return False 

 

set_self_gens = set(self.generators) 

set_other_gens = set(other.generators) 

 

# before reaching the general case there are also certain 

# optimisation and obvious cases requiring less or no actual 

# computation. 

if set_self_gens == set_other_gens: 

return True 

 

# in the most general case it will check that each generator of 

# one group belongs to the other PermutationGroup and vice-versa 

for gen1 in set_self_gens: 

if not other.contains(gen1): 

return False 

for gen2 in set_other_gens: 

if not self.contains(gen2): 

return False 

return True 

 

def __hash__(self): 

return super(PermutationGroup, self).__hash__() 

 

def __mul__(self, other): 

"""Return the direct product of two permutation groups as a permutation 

group. 

 

This implementation realizes the direct product by shifting 

the index set for the generators of the second group: so if we have 

G acting on n1 points and H acting on n2 points, G*H acts on n1 + n2 

points. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> from sympy.combinatorics.named_groups import CyclicGroup 

>>> G = CyclicGroup(5) 

>>> H = G*G 

>>> H 

PermutationGroup([ 

(9)(0 1 2 3 4), 

(5 6 7 8 9)]) 

>>> H.order() 

25 

 

""" 

gens1 = [perm._array_form for perm in self.generators] 

gens2 = [perm._array_form for perm in other.generators] 

n1 = self._degree 

n2 = other._degree 

start = list(range(n1)) 

end = list(range(n1, n1 + n2)) 

for i in range(len(gens2)): 

gens2[i] = [x + n1 for x in gens2[i]] 

gens2 = [start + gen for gen in gens2] 

gens1 = [gen + end for gen in gens1] 

together = gens1 + gens2 

gens = [_af_new(x) for x in together] 

return PermutationGroup(gens) 

 

def _random_pr_init(self, r, n, _random_prec_n=None): 

r"""Initialize random generators for the product replacement algorithm. 

 

The implementation uses a modification of the original product 

replacement algorithm due to Leedham-Green, as described in [1], 

pp. 69-71; also, see [2], pp. 27-29 for a detailed theoretical 

analysis of the original product replacement algorithm, and [4]. 

 

The product replacement algorithm is used for producing random, 

uniformly distributed elements of a group ``G`` with a set of generators 

``S``. For the initialization ``_random_pr_init``, a list ``R`` of 

``\max\{r, |S|\}`` group generators is created as the attribute 

``G._random_gens``, repeating elements of ``S`` if necessary, and the 

identity element of ``G`` is appended to ``R`` - we shall refer to this 

last element as the accumulator. Then the function ``random_pr()`` 

is called ``n`` times, randomizing the list ``R`` while preserving 

the generation of ``G`` by ``R``. The function ``random_pr()`` itself 

takes two random elements ``g, h`` among all elements of ``R`` but 

the accumulator and replaces ``g`` with a randomly chosen element 

from ``\{gh, g(~h), hg, (~h)g\}``. Then the accumulator is multiplied 

by whatever ``g`` was replaced by. The new value of the accumulator is 

then returned by ``random_pr()``. 

 

The elements returned will eventually (for ``n`` large enough) become 

uniformly distributed across ``G`` ([5]). For practical purposes however, 

the values ``n = 50, r = 11`` are suggested in [1]. 

 

Notes 

===== 

 

THIS FUNCTION HAS SIDE EFFECTS: it changes the attribute 

self._random_gens 

 

See Also 

======== 

 

random_pr 

 

""" 

deg = self.degree 

random_gens = [x._array_form for x in self.generators] 

k = len(random_gens) 

if k < r: 

for i in range(k, r): 

random_gens.append(random_gens[i - k]) 

acc = list(range(deg)) 

random_gens.append(acc) 

self._random_gens = random_gens 

 

# handle randomized input for testing purposes 

if _random_prec_n is None: 

for i in range(n): 

self.random_pr() 

else: 

for i in range(n): 

self.random_pr(_random_prec=_random_prec_n[i]) 

 

def _union_find_merge(self, first, second, ranks, parents, not_rep): 

"""Merges two classes in a union-find data structure. 

 

Used in the implementation of Atkinson's algorithm as suggested in [1], 

pp. 83-87. The class merging process uses union by rank as an 

optimization. ([7]) 

 

Notes 

===== 

 

THIS FUNCTION HAS SIDE EFFECTS: the list of class representatives, 

``parents``, the list of class sizes, ``ranks``, and the list of 

elements that are not representatives, ``not_rep``, are changed due to 

class merging. 

 

See Also 

======== 

 

minimal_block, _union_find_rep 

 

References 

========== 

 

[1] Holt, D., Eick, B., O'Brien, E. 

"Handbook of computational group theory" 

 

[7] http://www.algorithmist.com/index.php/Union_Find 

 

""" 

rep_first = self._union_find_rep(first, parents) 

rep_second = self._union_find_rep(second, parents) 

if rep_first != rep_second: 

# union by rank 

if ranks[rep_first] >= ranks[rep_second]: 

new_1, new_2 = rep_first, rep_second 

else: 

new_1, new_2 = rep_second, rep_first 

total_rank = ranks[new_1] + ranks[new_2] 

if total_rank > self.max_div: 

return -1 

parents[new_2] = new_1 

ranks[new_1] = total_rank 

not_rep.append(new_2) 

return 1 

return 0 

 

def _union_find_rep(self, num, parents): 

"""Find representative of a class in a union-find data structure. 

 

Used in the implementation of Atkinson's algorithm as suggested in [1], 

pp. 83-87. After the representative of the class to which ``num`` 

belongs is found, path compression is performed as an optimization 

([7]). 

 

Notes 

===== 

 

THIS FUNCTION HAS SIDE EFFECTS: the list of class representatives, 

``parents``, is altered due to path compression. 

 

See Also 

======== 

 

minimal_block, _union_find_merge 

 

References 

========== 

 

[1] Holt, D., Eick, B., O'Brien, E. 

"Handbook of computational group theory" 

 

[7] http://www.algorithmist.com/index.php/Union_Find 

 

""" 

rep, parent = num, parents[num] 

while parent != rep: 

rep = parent 

parent = parents[rep] 

# path compression 

temp, parent = num, parents[num] 

while parent != rep: 

parents[temp] = rep 

temp = parent 

parent = parents[temp] 

return rep 

 

@property 

def base(self): 

"""Return a base from the Schreier-Sims algorithm. 

 

For a permutation group ``G``, a base is a sequence of points 

``B = (b_1, b_2, ..., b_k)`` such that no element of ``G`` apart 

from the identity fixes all the points in ``B``. The concepts of 

a base and strong generating set and their applications are 

discussed in depth in [1], pp. 87-89 and [2], pp. 55-57. 

 

An alternative way to think of ``B`` is that it gives the 

indices of the stabilizer cosets that contain more than the 

identity permutation. 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation, PermutationGroup 

>>> G = PermutationGroup([Permutation(0, 1, 3)(2, 4)]) 

>>> G.base 

[0, 2] 

 

See Also 

======== 

 

strong_gens, basic_transversals, basic_orbits, basic_stabilizers 

 

""" 

if self._base == []: 

self.schreier_sims() 

return self._base 

 

def baseswap(self, base, strong_gens, pos, randomized=False, 

transversals=None, basic_orbits=None, strong_gens_distr=None): 

r"""Swap two consecutive base points in base and strong generating set. 

 

If a base for a group ``G`` is given by ``(b_1, b_2, ..., b_k)``, this 

function returns a base ``(b_1, b_2, ..., b_{i+1}, b_i, ..., b_k)``, 

where ``i`` is given by ``pos``, and a strong generating set relative 

to that base. The original base and strong generating set are not 

modified. 

 

The randomized version (default) is of Las Vegas type. 

 

Parameters 

========== 

 

base, strong_gens 

The base and strong generating set. 

pos 

The position at which swapping is performed. 

randomized 

A switch between randomized and deterministic version. 

transversals 

The transversals for the basic orbits, if known. 

basic_orbits 

The basic orbits, if known. 

strong_gens_distr 

The strong generators distributed by basic stabilizers, if known. 

 

Returns 

======= 

 

(base, strong_gens) 

``base`` is the new base, and ``strong_gens`` is a generating set 

relative to it. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.named_groups import SymmetricGroup 

>>> from sympy.combinatorics.testutil import _verify_bsgs 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> S = SymmetricGroup(4) 

>>> S.schreier_sims() 

>>> S.base 

[0, 1, 2] 

>>> base, gens = S.baseswap(S.base, S.strong_gens, 1, randomized=False) 

>>> base, gens 

([0, 2, 1], 

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

(2 3), (1 3)]) 

 

check that base, gens is a BSGS 

 

>>> S1 = PermutationGroup(gens) 

>>> _verify_bsgs(S1, base, gens) 

True 

 

See Also 

======== 

 

schreier_sims 

 

Notes 

===== 

 

The deterministic version of the algorithm is discussed in 

[1], pp. 102-103; the randomized version is discussed in [1], p.103, and 

[2], p.98. It is of Las Vegas type. 

Notice that [1] contains a mistake in the pseudocode and 

discussion of BASESWAP: on line 3 of the pseudocode, 

``|\beta_{i+1}^{\left\langle T\right\rangle}|`` should be replaced by 

``|\beta_{i}^{\left\langle T\right\rangle}|``, and the same for the 

discussion of the algorithm. 

 

""" 

# construct the basic orbits, generators for the stabilizer chain 

# and transversal elements from whatever was provided 

transversals, basic_orbits, strong_gens_distr = \ 

_handle_precomputed_bsgs(base, strong_gens, transversals, 

basic_orbits, strong_gens_distr) 

base_len = len(base) 

degree = self.degree 

# size of orbit of base[pos] under the stabilizer we seek to insert 

# in the stabilizer chain at position pos + 1 

size = len(basic_orbits[pos])*len(basic_orbits[pos + 1]) \ 

//len(_orbit(degree, strong_gens_distr[pos], base[pos + 1])) 

# initialize the wanted stabilizer by a subgroup 

if pos + 2 > base_len - 1: 

T = [] 

else: 

T = strong_gens_distr[pos + 2][:] 

# randomized version 

if randomized is True: 

stab_pos = PermutationGroup(strong_gens_distr[pos]) 

schreier_vector = stab_pos.schreier_vector(base[pos + 1]) 

# add random elements of the stabilizer until they generate it 

while len(_orbit(degree, T, base[pos])) != size: 

new = stab_pos.random_stab(base[pos + 1], 

schreier_vector=schreier_vector) 

T.append(new) 

# deterministic version 

else: 

Gamma = set(basic_orbits[pos]) 

Gamma.remove(base[pos]) 

if base[pos + 1] in Gamma: 

Gamma.remove(base[pos + 1]) 

# add elements of the stabilizer until they generate it by 

# ruling out member of the basic orbit of base[pos] along the way 

while len(_orbit(degree, T, base[pos])) != size: 

gamma = next(iter(Gamma)) 

x = transversals[pos][gamma] 

temp = x._array_form.index(base[pos + 1]) # (~x)(base[pos + 1]) 

if temp not in basic_orbits[pos + 1]: 

Gamma = Gamma - _orbit(degree, T, gamma) 

else: 

y = transversals[pos + 1][temp] 

el = rmul(x, y) 

if el(base[pos]) not in _orbit(degree, T, base[pos]): 

T.append(el) 

Gamma = Gamma - _orbit(degree, T, base[pos]) 

# build the new base and strong generating set 

strong_gens_new_distr = strong_gens_distr[:] 

strong_gens_new_distr[pos + 1] = T 

base_new = base[:] 

base_new[pos], base_new[pos + 1] = base_new[pos + 1], base_new[pos] 

strong_gens_new = _strong_gens_from_distr(strong_gens_new_distr) 

for gen in T: 

if gen not in strong_gens_new: 

strong_gens_new.append(gen) 

return base_new, strong_gens_new 

 

@property 

def basic_orbits(self): 

""" 

Return the basic orbits relative to a base and strong generating set. 

 

If ``(b_1, b_2, ..., b_k)`` is a base for a group ``G``, and 

``G^{(i)} = G_{b_1, b_2, ..., b_{i-1}}`` is the ``i``-th basic stabilizer 

(so that ``G^{(1)} = G``), the ``i``-th basic orbit relative to this base 

is the orbit of ``b_i`` under ``G^{(i)}``. See [1], pp. 87-89 for more 

information. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.named_groups import SymmetricGroup 

>>> S = SymmetricGroup(4) 

>>> S.basic_orbits 

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

 

See Also 

======== 

 

base, strong_gens, basic_transversals, basic_stabilizers 

 

""" 

if self._basic_orbits == []: 

self.schreier_sims() 

return self._basic_orbits 

 

@property 

def basic_stabilizers(self): 

""" 

Return a chain of stabilizers relative to a base and strong generating 

set. 

 

The ``i``-th basic stabilizer ``G^{(i)}`` relative to a base 

``(b_1, b_2, ..., b_k)`` is ``G_{b_1, b_2, ..., b_{i-1}}``. For more 

information, see [1], pp. 87-89. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.named_groups import AlternatingGroup 

>>> A = AlternatingGroup(4) 

>>> A.schreier_sims() 

>>> A.base 

[0, 1] 

>>> for g in A.basic_stabilizers: 

... print(g) 

... 

PermutationGroup([ 

(3)(0 1 2), 

(1 2 3)]) 

PermutationGroup([ 

(1 2 3)]) 

 

See Also 

======== 

 

base, strong_gens, basic_orbits, basic_transversals 

 

""" 

 

if self._transversals == []: 

self.schreier_sims() 

strong_gens = self._strong_gens 

base = self._base 

strong_gens_distr = _distribute_gens_by_base(base, strong_gens) 

basic_stabilizers = [] 

for gens in strong_gens_distr: 

basic_stabilizers.append(PermutationGroup(gens)) 

return basic_stabilizers 

 

@property 

def basic_transversals(self): 

""" 

Return basic transversals relative to a base and strong generating set. 

 

The basic transversals are transversals of the basic orbits. They 

are provided as a list of dictionaries, each dictionary having 

keys - the elements of one of the basic orbits, and values - the 

corresponding transversal elements. See [1], pp. 87-89 for more 

information. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.named_groups import AlternatingGroup 

>>> A = AlternatingGroup(4) 

>>> A.basic_transversals 

[{0: (3), 1: (3)(0 1 2), 2: (3)(0 2 1), 3: (0 3 1)}, {1: (3), 2: (1 2 3), 3: (1 3 2)}] 

 

See Also 

======== 

 

strong_gens, base, basic_orbits, basic_stabilizers 

 

""" 

 

if self._transversals == []: 

self.schreier_sims() 

return self._transversals 

 

def center(self): 

r""" 

Return the center of a permutation group. 

 

The center for a group ``G`` is defined as 

``Z(G) = \{z\in G | \forall g\in G, zg = gz \}``, 

the set of elements of ``G`` that commute with all elements of ``G``. 

It is equal to the centralizer of ``G`` inside ``G``, and is naturally a 

subgroup of ``G`` ([9]). 

 

Examples 

======== 

 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> from sympy.combinatorics.named_groups import DihedralGroup 

>>> D = DihedralGroup(4) 

>>> G = D.center() 

>>> G.order() 

2 

 

See Also 

======== 

 

centralizer 

 

Notes 

===== 

 

This is a naive implementation that is a straightforward application 

of ``.centralizer()`` 

 

""" 

return self.centralizer(self) 

 

def centralizer(self, other): 

r""" 

Return the centralizer of a group/set/element. 

 

The centralizer of a set of permutations ``S`` inside 

a group ``G`` is the set of elements of ``G`` that commute with all 

elements of ``S``:: 

 

``C_G(S) = \{ g \in G | gs = sg \forall s \in S\}`` ([10]) 

 

Usually, ``S`` is a subset of ``G``, but if ``G`` is a proper subgroup of 

the full symmetric group, we allow for ``S`` to have elements outside 

``G``. 

 

It is naturally a subgroup of ``G``; the centralizer of a permutation 

group is equal to the centralizer of any set of generators for that 

group, since any element commuting with the generators commutes with 

any product of the generators. 

 

Parameters 

========== 

 

other 

a permutation group/list of permutations/single permutation 

 

Examples 

======== 

 

>>> from sympy.combinatorics.named_groups import (SymmetricGroup, 

... CyclicGroup) 

>>> S = SymmetricGroup(6) 

>>> C = CyclicGroup(6) 

>>> H = S.centralizer(C) 

>>> H.is_subgroup(C) 

True 

 

See Also 

======== 

 

subgroup_search 

 

Notes 

===== 

 

The implementation is an application of ``.subgroup_search()`` with 

tests using a specific base for the group ``G``. 

 

""" 

if hasattr(other, 'generators'): 

if other.is_trivial or self.is_trivial: 

return self 

degree = self.degree 

identity = _af_new(list(range(degree))) 

orbits = other.orbits() 

num_orbits = len(orbits) 

orbits.sort(key=lambda x: -len(x)) 

long_base = [] 

orbit_reps = [None]*num_orbits 

orbit_reps_indices = [None]*num_orbits 

orbit_descr = [None]*degree 

for i in range(num_orbits): 

orbit = list(orbits[i]) 

orbit_reps[i] = orbit[0] 

orbit_reps_indices[i] = len(long_base) 

for point in orbit: 

orbit_descr[point] = i 

long_base = long_base + orbit 

base, strong_gens = self.schreier_sims_incremental(base=long_base) 

strong_gens_distr = _distribute_gens_by_base(base, strong_gens) 

i = 0 

for i in range(len(base)): 

if strong_gens_distr[i] == [identity]: 

break 

base = base[:i] 

base_len = i 

for j in range(num_orbits): 

if base[base_len - 1] in orbits[j]: 

break 

rel_orbits = orbits[: j + 1] 

num_rel_orbits = len(rel_orbits) 

transversals = [None]*num_rel_orbits 

for j in range(num_rel_orbits): 

rep = orbit_reps[j] 

transversals[j] = dict( 

other.orbit_transversal(rep, pairs=True)) 

trivial_test = lambda x: True 

tests = [None]*base_len 

for l in range(base_len): 

if base[l] in orbit_reps: 

tests[l] = trivial_test 

else: 

def test(computed_words, l=l): 

g = computed_words[l] 

rep_orb_index = orbit_descr[base[l]] 

rep = orbit_reps[rep_orb_index] 

im = g._array_form[base[l]] 

im_rep = g._array_form[rep] 

tr_el = transversals[rep_orb_index][base[l]] 

# using the definition of transversal, 

# base[l]^g = rep^(tr_el*g); 

# if g belongs to the centralizer, then 

# base[l]^g = (rep^g)^tr_el 

return im == tr_el._array_form[im_rep] 

tests[l] = test 

 

def prop(g): 

return [rmul(g, gen) for gen in other.generators] == \ 

[rmul(gen, g) for gen in other.generators] 

return self.subgroup_search(prop, base=base, 

strong_gens=strong_gens, tests=tests) 

elif hasattr(other, '__getitem__'): 

gens = list(other) 

return self.centralizer(PermutationGroup(gens)) 

elif hasattr(other, 'array_form'): 

return self.centralizer(PermutationGroup([other])) 

 

def commutator(self, G, H): 

""" 

Return the commutator of two subgroups. 

 

For a permutation group ``K`` and subgroups ``G``, ``H``, the 

commutator of ``G`` and ``H`` is defined as the group generated 

by all the commutators ``[g, h] = hgh^{-1}g^{-1}`` for ``g`` in ``G`` and 

``h`` in ``H``. It is naturally a subgroup of ``K`` ([1], p.27). 

 

Examples 

======== 

 

>>> from sympy.combinatorics.named_groups import (SymmetricGroup, 

... AlternatingGroup) 

>>> S = SymmetricGroup(5) 

>>> A = AlternatingGroup(5) 

>>> G = S.commutator(S, A) 

>>> G.is_subgroup(A) 

True 

 

See Also 

======== 

 

derived_subgroup 

 

Notes 

===== 

 

The commutator of two subgroups ``H, G`` is equal to the normal closure 

of the commutators of all the generators, i.e. ``hgh^{-1}g^{-1}`` for ``h`` 

a generator of ``H`` and ``g`` a generator of ``G`` ([1], p.28) 

 

""" 

ggens = G.generators 

hgens = H.generators 

commutators = [] 

for ggen in ggens: 

for hgen in hgens: 

commutator = rmul(hgen, ggen, ~hgen, ~ggen) 

if commutator not in commutators: 

commutators.append(commutator) 

res = self.normal_closure(commutators) 

return res 

 

def coset_factor(self, g, factor_index=False): 

"""Return ``G``'s (self's) coset factorization of ``g`` 

 

If ``g`` is an element of ``G`` then it can be written as the product 

of permutations drawn from the Schreier-Sims coset decomposition, 

 

The permutations returned in ``f`` are those for which 

the product gives ``g``: ``g = f[n]*...f[1]*f[0]`` where ``n = len(B)`` 

and ``B = G.base``. f[i] is one of the permutations in 

``self._basic_orbits[i]``. 

 

If factor_index==True, 

returns a tuple ``[b[0],..,b[n]]``, where ``b[i]`` 

belongs to ``self._basic_orbits[i]`` 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation, PermutationGroup 

>>> Permutation.print_cyclic = True 

>>> a = Permutation(0, 1, 3, 7, 6, 4)(2, 5) 

>>> b = Permutation(0, 1, 3, 2)(4, 5, 7, 6) 

>>> G = PermutationGroup([a, b]) 

 

Define g: 

 

>>> g = Permutation(7)(1, 2, 4)(3, 6, 5) 

 

Confirm that it is an element of G: 

 

>>> G.contains(g) 

True 

 

Thus, it can be written as a product of factors (up to 

3) drawn from u. See below that a factor from u1 and u2 

and the Identity permutation have been used: 

 

>>> f = G.coset_factor(g) 

>>> f[2]*f[1]*f[0] == g 

True 

>>> f1 = G.coset_factor(g, True); f1 

[0, 4, 4] 

>>> tr = G.basic_transversals 

>>> f[0] == tr[0][f1[0]] 

True 

 

If g is not an element of G then [] is returned: 

 

>>> c = Permutation(5, 6, 7) 

>>> G.coset_factor(c) 

[] 

 

see util._strip 

""" 

if isinstance(g, (Cycle, Permutation)): 

g = g.list() 

if len(g) != self._degree: 

# this could either adjust the size or return [] immediately 

# but we don't choose between the two and just signal a possible 

# error 

raise ValueError('g should be the same size as permutations of G') 

I = list(range(self._degree)) 

basic_orbits = self.basic_orbits 

transversals = self._transversals 

factors = [] 

base = self.base 

h = g 

for i in range(len(base)): 

beta = h[base[i]] 

if beta == base[i]: 

factors.append(beta) 

continue 

if beta not in basic_orbits[i]: 

return [] 

u = transversals[i][beta]._array_form 

h = _af_rmul(_af_invert(u), h) 

factors.append(beta) 

if h != I: 

return [] 

if factor_index: 

return factors 

tr = self.basic_transversals 

factors = [tr[i][factors[i]] for i in range(len(base))] 

return factors 

 

def coset_rank(self, g): 

"""rank using Schreier-Sims representation 

 

The coset rank of ``g`` is the ordering number in which 

it appears in the lexicographic listing according to the 

coset decomposition 

 

The ordering is the same as in G.generate(method='coset'). 

If ``g`` does not belong to the group it returns None. 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> Permutation.print_cyclic = True 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> a = Permutation(0, 1, 3, 7, 6, 4)(2, 5) 

>>> b = Permutation(0, 1, 3, 2)(4, 5, 7, 6) 

>>> G = PermutationGroup([a, b]) 

>>> c = Permutation(7)(2, 4)(3, 5) 

>>> G.coset_rank(c) 

16 

>>> G.coset_unrank(16) 

(7)(2 4)(3 5) 

 

See Also 

======== 

 

coset_factor 

 

""" 

factors = self.coset_factor(g, True) 

if not factors: 

return None 

rank = 0 

b = 1 

transversals = self._transversals 

base = self._base 

basic_orbits = self._basic_orbits 

for i in range(len(base)): 

k = factors[i] 

j = basic_orbits[i].index(k) 

rank += b*j 

b = b*len(transversals[i]) 

return rank 

 

def coset_unrank(self, rank, af=False): 

"""unrank using Schreier-Sims representation 

 

coset_unrank is the inverse operation of coset_rank 

if 0 <= rank < order; otherwise it returns None. 

 

""" 

if rank < 0 or rank >= self.order(): 

return None 

base = self._base 

transversals = self._transversals 

basic_orbits = self._basic_orbits 

m = len(base) 

v = [0]*m 

for i in range(m): 

rank, c = divmod(rank, len(transversals[i])) 

v[i] = basic_orbits[i][c] 

a = [transversals[i][v[i]]._array_form for i in range(m)] 

h = _af_rmuln(*a) 

if af: 

return h 

else: 

return _af_new(h) 

 

@property 

def degree(self): 

"""Returns the size of the permutations in the group. 

 

The number of permutations comprising the group is given by 

len(group); the number of permutations that can be generated 

by the group is given by group.order(). 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> Permutation.print_cyclic = True 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> a = Permutation([1, 0, 2]) 

>>> G = PermutationGroup([a]) 

>>> G.degree 

3 

>>> len(G) 

1 

>>> G.order() 

2 

>>> list(G.generate()) 

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

 

See Also 

======== 

 

order 

""" 

return self._degree 

 

@property 

def elements(self): 

"""Returns all the elements of the permutatio group in 

a list 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

""" 

return set(list(islice(self.generate(), None))) 

 

def derived_series(self): 

r"""Return the derived series for the group. 

 

The derived series for a group ``G`` is defined as 

``G = G_0 > G_1 > G_2 > \ldots`` where ``G_i = [G_{i-1}, G_{i-1}]``, 

i.e. ``G_i`` is the derived subgroup of ``G_{i-1}``, for 

``i\in\mathbb{N}``. When we have ``G_k = G_{k-1}`` for some 

``k\in\mathbb{N}``, the series terminates. 

 

Returns 

======= 

 

A list of permutation groups containing the members of the derived 

series in the order ``G = G_0, G_1, G_2, \ldots``. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.named_groups import (SymmetricGroup, 

... AlternatingGroup, DihedralGroup) 

>>> A = AlternatingGroup(5) 

>>> len(A.derived_series()) 

1 

>>> S = SymmetricGroup(4) 

>>> len(S.derived_series()) 

4 

>>> S.derived_series()[1].is_subgroup(AlternatingGroup(4)) 

True 

>>> S.derived_series()[2].is_subgroup(DihedralGroup(2)) 

True 

 

See Also 

======== 

 

derived_subgroup 

 

""" 

res = [self] 

current = self 

next = self.derived_subgroup() 

while not current.is_subgroup(next): 

res.append(next) 

current = next 

next = next.derived_subgroup() 

return res 

 

def derived_subgroup(self): 

"""Compute the derived subgroup. 

 

The derived subgroup, or commutator subgroup is the subgroup generated 

by all commutators ``[g, h] = hgh^{-1}g^{-1}`` for ``g, h\in G`` ; it is 

equal to the normal closure of the set of commutators of the generators 

([1], p.28, [11]). 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> Permutation.print_cyclic = True 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> a = Permutation([1, 0, 2, 4, 3]) 

>>> b = Permutation([0, 1, 3, 2, 4]) 

>>> G = PermutationGroup([a, b]) 

>>> C = G.derived_subgroup() 

>>> list(C.generate(af=True)) 

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

 

See Also 

======== 

 

derived_series 

 

""" 

r = self._r 

gens = [p._array_form for p in self.generators] 

gens_inv = [_af_invert(p) for p in gens] 

set_commutators = set() 

degree = self._degree 

rng = list(range(degree)) 

for i in range(r): 

for j in range(r): 

p1 = gens[i] 

p2 = gens[j] 

c = list(range(degree)) 

for k in rng: 

c[p2[p1[k]]] = p1[p2[k]] 

ct = tuple(c) 

if not ct in set_commutators: 

set_commutators.add(ct) 

cms = [_af_new(p) for p in set_commutators] 

G2 = self.normal_closure(cms) 

return G2 

 

def generate(self, method="coset", af=False): 

"""Return iterator to generate the elements of the group 

 

Iteration is done with one of these methods:: 

 

method='coset' using the Schreier-Sims coset representation 

method='dimino' using the Dimino method 

 

If af = True it yields the array form of the permutations 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> Permutation.print_cyclic = True 

>>> from sympy.combinatorics import PermutationGroup 

>>> from sympy.combinatorics.polyhedron import tetrahedron 

 

The permutation group given in the tetrahedron object is also 

true groups: 

 

>>> G = tetrahedron.pgroup 

>>> G.is_group 

True 

 

Also the group generated by the permutations in the tetrahedron 

pgroup -- even the first two -- is a proper group: 

 

>>> H = PermutationGroup(G[0], G[1]) 

>>> J = PermutationGroup(list(H.generate())); J 

PermutationGroup([ 

(0 1)(2 3), 

(3), 

(1 2 3), 

(1 3 2), 

(0 3 1), 

(0 2 3), 

(0 3)(1 2), 

(0 1 3), 

(3)(0 2 1), 

(0 3 2), 

(3)(0 1 2), 

(0 2)(1 3)]) 

>>> _.is_group 

True 

""" 

if method == "coset": 

return self.generate_schreier_sims(af) 

elif method == "dimino": 

return self.generate_dimino(af) 

else: 

raise NotImplementedError('No generation defined for %s' % method) 

 

def generate_dimino(self, af=False): 

"""Yield group elements using Dimino's algorithm 

 

If af == True it yields the array form of the permutations 

 

References 

========== 

 

[1] The Implementation of Various Algorithms for Permutation Groups in 

the Computer Algebra System: AXIOM, N.J. Doye, M.Sc. Thesis 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> Permutation.print_cyclic = True 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> a = Permutation([0, 2, 1, 3]) 

>>> b = Permutation([0, 2, 3, 1]) 

>>> g = PermutationGroup([a, b]) 

>>> list(g.generate_dimino(af=True)) 

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

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

 

""" 

idn = list(range(self.degree)) 

order = 0 

element_list = [idn] 

set_element_list = {tuple(idn)} 

if af: 

yield idn 

else: 

yield _af_new(idn) 

gens = [p._array_form for p in self.generators] 

 

for i in range(len(gens)): 

# D elements of the subgroup G_i generated by gens[:i] 

D = element_list[:] 

N = [idn] 

while N: 

A = N 

N = [] 

for a in A: 

for g in gens[:i + 1]: 

ag = _af_rmul(a, g) 

if tuple(ag) not in set_element_list: 

# produce G_i*g 

for d in D: 

order += 1 

ap = _af_rmul(d, ag) 

if af: 

yield ap 

else: 

p = _af_new(ap) 

yield p 

element_list.append(ap) 

set_element_list.add(tuple(ap)) 

N.append(ap) 

self._order = len(element_list) 

 

def generate_schreier_sims(self, af=False): 

"""Yield group elements using the Schreier-Sims representation 

in coset_rank order 

 

If af = True it yields the array form of the permutations 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> Permutation.print_cyclic = True 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> a = Permutation([0, 2, 1, 3]) 

>>> b = Permutation([0, 2, 3, 1]) 

>>> g = PermutationGroup([a, b]) 

>>> list(g.generate_schreier_sims(af=True)) 

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

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

""" 

 

n = self._degree 

u = self.basic_transversals 

basic_orbits = self._basic_orbits 

if len(u) == 0: 

for x in self.generators: 

if af: 

yield x._array_form 

else: 

yield x 

return 

if len(u) == 1: 

for i in basic_orbits[0]: 

if af: 

yield u[0][i]._array_form 

else: 

yield u[0][i] 

return 

 

u = list(reversed(u)) 

basic_orbits = basic_orbits[::-1] 

# stg stack of group elements 

stg = [list(range(n))] 

posmax = [len(x) for x in u] 

n1 = len(posmax) - 1 

pos = [0]*n1 

h = 0 

while 1: 

# backtrack when finished iterating over coset 

if pos[h] >= posmax[h]: 

if h == 0: 

return 

pos[h] = 0 

h -= 1 

stg.pop() 

continue 

p = _af_rmul(u[h][basic_orbits[h][pos[h]]]._array_form, stg[-1]) 

pos[h] += 1 

stg.append(p) 

h += 1 

if h == n1: 

if af: 

for i in basic_orbits[-1]: 

p = _af_rmul(u[-1][i]._array_form, stg[-1]) 

yield p 

else: 

for i in basic_orbits[-1]: 

p = _af_rmul(u[-1][i]._array_form, stg[-1]) 

p1 = _af_new(p) 

yield p1 

stg.pop() 

h -= 1 

 

@property 

def generators(self): 

"""Returns the generators of the group. 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> Permutation.print_cyclic = True 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> a = Permutation([0, 2, 1]) 

>>> b = Permutation([1, 0, 2]) 

>>> G = PermutationGroup([a, b]) 

>>> G.generators 

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

 

""" 

return self._generators 

 

def contains(self, g, strict=True): 

"""Test if permutation ``g`` belong to self, ``G``. 

 

If ``g`` is an element of ``G`` it can be written as a product 

of factors drawn from the cosets of ``G``'s stabilizers. To see 

if ``g`` is one of the actual generators defining the group use 

``G.has(g)``. 

 

If ``strict`` is not True, ``g`` will be resized, if necessary, 

to match the size of permutations in ``self``. 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> Permutation.print_cyclic = True 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

 

>>> a = Permutation(1, 2) 

>>> b = Permutation(2, 3, 1) 

>>> G = PermutationGroup(a, b, degree=5) 

>>> G.contains(G[0]) # trivial check 

True 

>>> elem = Permutation([[2, 3]], size=5) 

>>> G.contains(elem) 

True 

>>> G.contains(Permutation(4)(0, 1, 2, 3)) 

False 

 

If strict is False, a permutation will be resized, if 

necessary: 

 

>>> H = PermutationGroup(Permutation(5)) 

>>> H.contains(Permutation(3)) 

False 

>>> H.contains(Permutation(3), strict=False) 

True 

 

To test if a given permutation is present in the group: 

 

>>> elem in G.generators 

False 

>>> G.has(elem) 

False 

 

See Also 

======== 

 

coset_factor, has, in 

 

""" 

if not isinstance(g, Permutation): 

return False 

if g.size != self.degree: 

if strict: 

return False 

g = Permutation(g, size=self.degree) 

if g in self.generators: 

return True 

return bool(self.coset_factor(g.array_form, True)) 

 

@property 

def is_abelian(self): 

"""Test if the group is Abelian. 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> Permutation.print_cyclic = True 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> a = Permutation([0, 2, 1]) 

>>> b = Permutation([1, 0, 2]) 

>>> G = PermutationGroup([a, b]) 

>>> G.is_abelian 

False 

>>> a = Permutation([0, 2, 1]) 

>>> G = PermutationGroup([a]) 

>>> G.is_abelian 

True 

 

""" 

if self._is_abelian is not None: 

return self._is_abelian 

 

self._is_abelian = True 

gens = [p._array_form for p in self.generators] 

for x in gens: 

for y in gens: 

if y <= x: 

continue 

if not _af_commutes_with(x, y): 

self._is_abelian = False 

return False 

return True 

 

def is_alt_sym(self, eps=0.05, _random_prec=None): 

r"""Monte Carlo test for the symmetric/alternating group for degrees 

>= 8. 

 

More specifically, it is one-sided Monte Carlo with the 

answer True (i.e., G is symmetric/alternating) guaranteed to be 

correct, and the answer False being incorrect with probability eps. 

 

Notes 

===== 

 

The algorithm itself uses some nontrivial results from group theory and 

number theory: 

1) If a transitive group ``G`` of degree ``n`` contains an element 

with a cycle of length ``n/2 < p < n-2`` for ``p`` a prime, ``G`` is the 

symmetric or alternating group ([1], pp. 81-82) 

2) The proportion of elements in the symmetric/alternating group having 

the property described in 1) is approximately ``\log(2)/\log(n)`` 

([1], p.82; [2], pp. 226-227). 

The helper function ``_check_cycles_alt_sym`` is used to 

go over the cycles in a permutation and look for ones satisfying 1). 

 

Examples 

======== 

 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> from sympy.combinatorics.named_groups import DihedralGroup 

>>> D = DihedralGroup(10) 

>>> D.is_alt_sym() 

False 

 

See Also 

======== 

 

_check_cycles_alt_sym 

 

""" 

if _random_prec is None: 

n = self.degree 

if n < 8: 

return False 

if not self.is_transitive(): 

return False 

if n < 17: 

c_n = 0.34 

else: 

c_n = 0.57 

d_n = (c_n*log(2))/log(n) 

N_eps = int(-log(eps)/d_n) 

for i in range(N_eps): 

perm = self.random_pr() 

if _check_cycles_alt_sym(perm): 

return True 

return False 

else: 

for i in range(_random_prec['N_eps']): 

perm = _random_prec[i] 

if _check_cycles_alt_sym(perm): 

return True 

return False 

 

@property 

def is_nilpotent(self): 

"""Test if the group is nilpotent. 

 

A group ``G`` is nilpotent if it has a central series of finite length. 

Alternatively, ``G`` is nilpotent if its lower central series terminates 

with the trivial group. Every nilpotent group is also solvable 

([1], p.29, [12]). 

 

Examples 

======== 

 

>>> from sympy.combinatorics.named_groups import (SymmetricGroup, 

... CyclicGroup) 

>>> C = CyclicGroup(6) 

>>> C.is_nilpotent 

True 

>>> S = SymmetricGroup(5) 

>>> S.is_nilpotent 

False 

 

See Also 

======== 

 

lower_central_series, is_solvable 

 

""" 

if self._is_nilpotent is None: 

lcs = self.lower_central_series() 

terminator = lcs[len(lcs) - 1] 

gens = terminator.generators 

degree = self.degree 

identity = _af_new(list(range(degree))) 

if all(g == identity for g in gens): 

self._is_solvable = True 

self._is_nilpotent = True 

return True 

else: 

self._is_nilpotent = False 

return False 

else: 

return self._is_nilpotent 

 

def is_normal(self, gr, strict=True): 

"""Test if G=self is a normal subgroup of gr. 

 

G is normal in gr if 

for each g2 in G, g1 in gr, g = g1*g2*g1**-1 belongs to G 

It is sufficient to check this for each g1 in gr.generator and 

g2 g2 in G.generator 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> Permutation.print_cyclic = True 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> a = Permutation([1, 2, 0]) 

>>> b = Permutation([1, 0, 2]) 

>>> G = PermutationGroup([a, b]) 

>>> G1 = PermutationGroup([a, Permutation([2, 0, 1])]) 

>>> G1.is_normal(G) 

True 

 

""" 

d_self = self.degree 

d_gr = gr.degree 

new_self = self.copy() 

if not strict and d_self != d_gr: 

if d_self < d_gr: 

new_self = PermGroup(new_self.generators + [Permutation(d_gr - 1)]) 

else: 

gr = PermGroup(gr.generators + [Permutation(d_self - 1)]) 

gens2 = [p._array_form for p in new_self.generators] 

gens1 = [p._array_form for p in gr.generators] 

for g1 in gens1: 

for g2 in gens2: 

p = _af_rmuln(g1, g2, _af_invert(g1)) 

if not new_self.coset_factor(p, True): 

return False 

return True 

 

def is_primitive(self, randomized=True): 

"""Test if a group is primitive. 

 

A permutation group ``G`` acting on a set ``S`` is called primitive if 

``S`` contains no nontrivial block under the action of ``G`` 

(a block is nontrivial if its cardinality is more than ``1``). 

 

Notes 

===== 

 

The algorithm is described in [1], p.83, and uses the function 

minimal_block to search for blocks of the form ``\{0, k\}`` for ``k`` 

ranging over representatives for the orbits of ``G_0``, the stabilizer of 

``0``. This algorithm has complexity ``O(n^2)`` where ``n`` is the degree 

of the group, and will perform badly if ``G_0`` is small. 

 

There are two implementations offered: one finds ``G_0`` 

deterministically using the function ``stabilizer``, and the other 

(default) produces random elements of ``G_0`` using ``random_stab``, 

hoping that they generate a subgroup of ``G_0`` with not too many more 

orbits than G_0 (this is suggested in [1], p.83). Behavior is changed 

by the ``randomized`` flag. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> from sympy.combinatorics.named_groups import DihedralGroup 

>>> D = DihedralGroup(10) 

>>> D.is_primitive() 

False 

 

See Also 

======== 

 

minimal_block, random_stab 

 

""" 

if self._is_primitive is not None: 

return self._is_primitive 

n = self.degree 

if randomized: 

random_stab_gens = [] 

v = self.schreier_vector(0) 

for i in range(len(self)): 

random_stab_gens.append(self.random_stab(0, v)) 

stab = PermutationGroup(random_stab_gens) 

else: 

stab = self.stabilizer(0) 

orbits = stab.orbits() 

for orb in orbits: 

x = orb.pop() 

if x != 0 and self.minimal_block([0, x]) != [0]*n: 

self._is_primitive = False 

return False 

self._is_primitive = True 

return True 

 

@property 

def is_solvable(self): 

"""Test if the group is solvable. 

 

``G`` is solvable if its derived series terminates with the trivial 

group ([1], p.29). 

 

Examples 

======== 

 

>>> from sympy.combinatorics.named_groups import SymmetricGroup 

>>> S = SymmetricGroup(3) 

>>> S.is_solvable 

True 

 

See Also 

======== 

 

is_nilpotent, derived_series 

 

""" 

if self._is_solvable is None: 

ds = self.derived_series() 

terminator = ds[len(ds) - 1] 

gens = terminator.generators 

degree = self.degree 

identity = _af_new(list(range(degree))) 

if all(g == identity for g in gens): 

self._is_solvable = True 

return True 

else: 

self._is_solvable = False 

return False 

else: 

return self._is_solvable 

 

def is_subgroup(self, G, strict=True): 

"""Return True if all elements of self belong to G. 

 

If ``strict`` is False then if ``self``'s degree is smaller 

than ``G``'s, the elements will be resized to have the same degree. 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation, PermutationGroup 

>>> from sympy.combinatorics.named_groups import (SymmetricGroup, 

... CyclicGroup) 

 

Testing is strict by default: the degree of each group must be the 

same: 

 

>>> p = Permutation(0, 1, 2, 3, 4, 5) 

>>> G1 = PermutationGroup([Permutation(0, 1, 2), Permutation(0, 1)]) 

>>> G2 = PermutationGroup([Permutation(0, 2), Permutation(0, 1, 2)]) 

>>> G3 = PermutationGroup([p, p**2]) 

>>> assert G1.order() == G2.order() == G3.order() == 6 

>>> G1.is_subgroup(G2) 

True 

>>> G1.is_subgroup(G3) 

False 

>>> G3.is_subgroup(PermutationGroup(G3[1])) 

False 

>>> G3.is_subgroup(PermutationGroup(G3[0])) 

True 

 

To ignore the size, set ``strict`` to False: 

 

>>> S3 = SymmetricGroup(3) 

>>> S5 = SymmetricGroup(5) 

>>> S3.is_subgroup(S5, strict=False) 

True 

>>> C7 = CyclicGroup(7) 

>>> G = S5*C7 

>>> S5.is_subgroup(G, False) 

True 

>>> C7.is_subgroup(G, 0) 

False 

""" 

if not isinstance(G, PermutationGroup): 

return False 

if self == G: 

return True 

if G.order() % self.order() != 0: 

return False 

if self.degree == G.degree or \ 

(self.degree < G.degree and not strict): 

gens = self.generators 

else: 

return False 

return all(G.contains(g, strict=strict) for g in gens) 

 

def is_transitive(self, strict=True): 

"""Test if the group is transitive. 

 

A group is transitive if it has a single orbit. 

 

If ``strict`` is False the group is transitive if it has 

a single orbit of length different from 1. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.permutations import Permutation 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> a = Permutation([0, 2, 1, 3]) 

>>> b = Permutation([2, 0, 1, 3]) 

>>> G1 = PermutationGroup([a, b]) 

>>> G1.is_transitive() 

False 

>>> G1.is_transitive(strict=False) 

True 

>>> c = Permutation([2, 3, 0, 1]) 

>>> G2 = PermutationGroup([a, c]) 

>>> G2.is_transitive() 

True 

>>> d = Permutation([1, 0, 2, 3]) 

>>> e = Permutation([0, 1, 3, 2]) 

>>> G3 = PermutationGroup([d, e]) 

>>> G3.is_transitive() or G3.is_transitive(strict=False) 

False 

""" 

if self._is_transitive: # strict or not, if True then True 

return self._is_transitive 

if strict: 

if self._is_transitive is not None: # we only store strict=True 

return self._is_transitive 

 

ans = len(self.orbit(0)) == self.degree 

self._is_transitive = ans 

return ans 

 

got_orb = False 

for x in self.orbits(): 

if len(x) > 1: 

if got_orb: 

return False 

got_orb = True 

return got_orb 

 

@property 

def is_trivial(self): 

"""Test if the group is the trivial group. 

 

This is true if the group contains only the identity permutation. 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> G = PermutationGroup([Permutation([0, 1, 2])]) 

>>> G.is_trivial 

True 

 

""" 

if self._is_trivial is None: 

self._is_trivial = len(self) == 1 and self[0].is_Identity 

return self._is_trivial 

 

def lower_central_series(self): 

r"""Return the lower central series for the group. 

 

The lower central series for a group ``G`` is the series 

``G = G_0 > G_1 > G_2 > \ldots`` where 

``G_k = [G, G_{k-1}]``, i.e. every term after the first is equal to the 

commutator of ``G`` and the previous term in ``G1`` ([1], p.29). 

 

Returns 

======= 

 

A list of permutation groups in the order 

``G = G_0, G_1, G_2, \ldots`` 

 

Examples 

======== 

 

>>> from sympy.combinatorics.named_groups import (AlternatingGroup, 

... DihedralGroup) 

>>> A = AlternatingGroup(4) 

>>> len(A.lower_central_series()) 

2 

>>> A.lower_central_series()[1].is_subgroup(DihedralGroup(2)) 

True 

 

See Also 

======== 

 

commutator, derived_series 

 

""" 

res = [self] 

current = self 

next = self.commutator(self, current) 

while not current.is_subgroup(next): 

res.append(next) 

current = next 

next = self.commutator(self, current) 

return res 

 

@property 

def max_div(self): 

"""Maximum proper divisor of the degree of a permutation group. 

 

Notes 

===== 

 

Obviously, this is the degree divided by its minimal proper divisor 

(larger than ``1``, if one exists). As it is guaranteed to be prime, 

the ``sieve`` from ``sympy.ntheory`` is used. 

This function is also used as an optimization tool for the functions 

``minimal_block`` and ``_union_find_merge``. 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> G = PermutationGroup([Permutation([0, 2, 1, 3])]) 

>>> G.max_div 

2 

 

See Also 

======== 

 

minimal_block, _union_find_merge 

 

""" 

if self._max_div is not None: 

return self._max_div 

n = self.degree 

if n == 1: 

return 1 

for x in sieve: 

if n % x == 0: 

d = n//x 

self._max_div = d 

return d 

 

def minimal_block(self, points): 

r"""For a transitive group, finds the block system generated by 

``points``. 

 

If a group ``G`` acts on a set ``S``, a nonempty subset ``B`` of ``S`` 

is called a block under the action of ``G`` if for all ``g`` in ``G`` 

we have ``gB = B`` (``g`` fixes ``B``) or ``gB`` and ``B`` have no 

common points (``g`` moves ``B`` entirely). ([1], p.23; [6]). 

 

The distinct translates ``gB`` of a block ``B`` for ``g`` in ``G`` 

partition the set ``S`` and this set of translates is known as a block 

system. Moreover, we obviously have that all blocks in the partition 

have the same size, hence the block size divides ``|S|`` ([1], p.23). 

A ``G``-congruence is an equivalence relation ``~`` on the set ``S`` 

such that ``a ~ b`` implies ``g(a) ~ g(b)`` for all ``g`` in ``G``. 

For a transitive group, the equivalence classes of a ``G``-congruence 

and the blocks of a block system are the same thing ([1], p.23). 

 

The algorithm below checks the group for transitivity, and then finds 

the ``G``-congruence generated by the pairs ``(p_0, p_1), (p_0, p_2), 

..., (p_0,p_{k-1})`` which is the same as finding the maximal block 

system (i.e., the one with minimum block size) such that 

``p_0, ..., p_{k-1}`` are in the same block ([1], p.83). 

 

It is an implementation of Atkinson's algorithm, as suggested in [1], 

and manipulates an equivalence relation on the set ``S`` using a 

union-find data structure. The running time is just above 

``O(|points||S|)``. ([1], pp. 83-87; [7]). 

 

Examples 

======== 

 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> from sympy.combinatorics.named_groups import DihedralGroup 

>>> D = DihedralGroup(10) 

>>> D.minimal_block([0, 5]) 

[0, 6, 2, 8, 4, 0, 6, 2, 8, 4] 

>>> D.minimal_block([0, 1]) 

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 

 

See Also 

======== 

 

_union_find_rep, _union_find_merge, is_transitive, is_primitive 

 

""" 

if not self.is_transitive(): 

return False 

n = self.degree 

gens = self.generators 

# initialize the list of equivalence class representatives 

parents = list(range(n)) 

ranks = [1]*n 

not_rep = [] 

k = len(points) 

# the block size must divide the degree of the group 

if k > self.max_div: 

return [0]*n 

for i in range(k - 1): 

parents[points[i + 1]] = points[0] 

not_rep.append(points[i + 1]) 

ranks[points[0]] = k 

i = 0 

len_not_rep = k - 1 

while i < len_not_rep: 

temp = not_rep[i] 

i += 1 

for gen in gens: 

# find has side effects: performs path compression on the list 

# of representatives 

delta = self._union_find_rep(temp, parents) 

# union has side effects: performs union by rank on the list 

# of representatives 

temp = self._union_find_merge(gen(temp), gen(delta), ranks, 

parents, not_rep) 

if temp == -1: 

return [0]*n 

len_not_rep += temp 

for i in range(n): 

# force path compression to get the final state of the equivalence 

# relation 

self._union_find_rep(i, parents) 

return parents 

 

def normal_closure(self, other, k=10): 

r"""Return the normal closure of a subgroup/set of permutations. 

 

If ``S`` is a subset of a group ``G``, the normal closure of ``A`` in ``G`` 

is defined as the intersection of all normal subgroups of ``G`` that 

contain ``A`` ([1], p.14). Alternatively, it is the group generated by 

the conjugates ``x^{-1}yx`` for ``x`` a generator of ``G`` and ``y`` a 

generator of the subgroup ``\left\langle S\right\rangle`` generated by 

``S`` (for some chosen generating set for ``\left\langle S\right\rangle``) 

([1], p.73). 

 

Parameters 

========== 

 

other 

a subgroup/list of permutations/single permutation 

k 

an implementation-specific parameter that determines the number 

of conjugates that are adjoined to ``other`` at once 

 

Examples 

======== 

 

>>> from sympy.combinatorics.named_groups import (SymmetricGroup, 

... CyclicGroup, AlternatingGroup) 

>>> S = SymmetricGroup(5) 

>>> C = CyclicGroup(5) 

>>> G = S.normal_closure(C) 

>>> G.order() 

60 

>>> G.is_subgroup(AlternatingGroup(5)) 

True 

 

See Also 

======== 

 

commutator, derived_subgroup, random_pr 

 

Notes 

===== 

 

The algorithm is described in [1], pp. 73-74; it makes use of the 

generation of random elements for permutation groups by the product 

replacement algorithm. 

 

""" 

if hasattr(other, 'generators'): 

degree = self.degree 

identity = _af_new(list(range(degree))) 

 

if all(g == identity for g in other.generators): 

return other 

Z = PermutationGroup(other.generators[:]) 

base, strong_gens = Z.schreier_sims_incremental() 

strong_gens_distr = _distribute_gens_by_base(base, strong_gens) 

basic_orbits, basic_transversals = \ 

_orbits_transversals_from_bsgs(base, strong_gens_distr) 

 

self._random_pr_init(r=10, n=20) 

 

_loop = True 

while _loop: 

Z._random_pr_init(r=10, n=10) 

for i in range(k): 

g = self.random_pr() 

h = Z.random_pr() 

conj = h^g 

res = _strip(conj, base, basic_orbits, basic_transversals) 

if res[0] != identity or res[1] != len(base) + 1: 

gens = Z.generators 

gens.append(conj) 

Z = PermutationGroup(gens) 

strong_gens.append(conj) 

temp_base, temp_strong_gens = \ 

Z.schreier_sims_incremental(base, strong_gens) 

base, strong_gens = temp_base, temp_strong_gens 

strong_gens_distr = \ 

_distribute_gens_by_base(base, strong_gens) 

basic_orbits, basic_transversals = \ 

_orbits_transversals_from_bsgs(base, 

strong_gens_distr) 

_loop = False 

for g in self.generators: 

for h in Z.generators: 

conj = h^g 

res = _strip(conj, base, basic_orbits, 

basic_transversals) 

if res[0] != identity or res[1] != len(base) + 1: 

_loop = True 

break 

if _loop: 

break 

return Z 

elif hasattr(other, '__getitem__'): 

return self.normal_closure(PermutationGroup(other)) 

elif hasattr(other, 'array_form'): 

return self.normal_closure(PermutationGroup([other])) 

 

def orbit(self, alpha, action='tuples'): 

r"""Compute the orbit of alpha ``\{g(\alpha) | g \in G\}`` as a set. 

 

The time complexity of the algorithm used here is ``O(|Orb|*r)`` where 

``|Orb|`` is the size of the orbit and ``r`` is the number of generators of 

the group. For a more detailed analysis, see [1], p.78, [2], pp. 19-21. 

Here alpha can be a single point, or a list of points. 

 

If alpha is a single point, the ordinary orbit is computed. 

if alpha is a list of points, there are three available options: 

 

'union' - computes the union of the orbits of the points in the list 

'tuples' - computes the orbit of the list interpreted as an ordered 

tuple under the group action ( i.e., g((1,2,3)) = (g(1), g(2), g(3)) ) 

'sets' - computes the orbit of the list interpreted as a sets 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> a = Permutation([1, 2, 0, 4, 5, 6, 3]) 

>>> G = PermutationGroup([a]) 

>>> G.orbit(0) 

set([0, 1, 2]) 

>>> G.orbit([0, 4], 'union') 

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

 

See Also 

======== 

 

orbit_transversal 

 

""" 

return _orbit(self.degree, self.generators, alpha, action) 

 

def orbit_rep(self, alpha, beta, schreier_vector=None): 

"""Return a group element which sends ``alpha`` to ``beta``. 

 

If ``beta`` is not in the orbit of ``alpha``, the function returns 

``False``. This implementation makes use of the schreier vector. 

For a proof of correctness, see [1], p.80 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> Permutation.print_cyclic = True 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> from sympy.combinatorics.named_groups import AlternatingGroup 

>>> G = AlternatingGroup(5) 

>>> G.orbit_rep(0, 4) 

(0 4 1 2 3) 

 

See Also 

======== 

 

schreier_vector 

 

""" 

if schreier_vector is None: 

schreier_vector = self.schreier_vector(alpha) 

if schreier_vector[beta] is None: 

return False 

k = schreier_vector[beta] 

gens = [x._array_form for x in self.generators] 

a = [] 

while k != -1: 

a.append(gens[k]) 

beta = gens[k].index(beta) # beta = (~gens[k])(beta) 

k = schreier_vector[beta] 

if a: 

return _af_new(_af_rmuln(*a)) 

else: 

return _af_new(list(range(self._degree))) 

 

def orbit_transversal(self, alpha, pairs=False): 

r"""Computes a transversal for the orbit of ``alpha`` as a set. 

 

For a permutation group ``G``, a transversal for the orbit 

``Orb = \{g(\alpha) | g \in G\}`` is a set 

``\{g_\beta | g_\beta(\alpha) = \beta\}`` for ``\beta \in Orb``. 

Note that there may be more than one possible transversal. 

If ``pairs`` is set to ``True``, it returns the list of pairs 

``(\beta, g_\beta)``. For a proof of correctness, see [1], p.79 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> Permutation.print_cyclic = True 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> from sympy.combinatorics.named_groups import DihedralGroup 

>>> G = DihedralGroup(6) 

>>> G.orbit_transversal(0) 

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

 

See Also 

======== 

 

orbit 

 

""" 

return _orbit_transversal(self._degree, self.generators, alpha, pairs) 

 

def orbits(self, rep=False): 

"""Return the orbits of self, ordered according to lowest element 

in each orbit. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.permutations import Permutation 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> a = Permutation(1, 5)(2, 3)(4, 0, 6) 

>>> b = Permutation(1, 5)(3, 4)(2, 6, 0) 

>>> G = PermutationGroup([a, b]) 

>>> G.orbits() 

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

""" 

return _orbits(self._degree, self._generators) 

 

def order(self): 

"""Return the order of the group: the number of permutations that 

can be generated from elements of the group. 

 

The number of permutations comprising the group is given by 

len(group); the length of each permutation in the group is 

given by group.size. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.permutations import Permutation 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

 

>>> a = Permutation([1, 0, 2]) 

>>> G = PermutationGroup([a]) 

>>> G.degree 

3 

>>> len(G) 

1 

>>> G.order() 

2 

>>> list(G.generate()) 

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

 

>>> a = Permutation([0, 2, 1]) 

>>> b = Permutation([1, 0, 2]) 

>>> G = PermutationGroup([a, b]) 

>>> G.order() 

6 

 

See Also 

======== 

 

degree 

""" 

if self._order != None: 

return self._order 

if self._is_sym: 

n = self._degree 

self._order = factorial(n) 

return self._order 

if self._is_alt: 

n = self._degree 

self._order = factorial(n)/2 

return self._order 

 

basic_transversals = self.basic_transversals 

m = 1 

for x in basic_transversals: 

m *= len(x) 

self._order = m 

return m 

 

def pointwise_stabilizer(self, points, incremental=True): 

r"""Return the pointwise stabilizer for a set of points. 

 

For a permutation group ``G`` and a set of points 

``\{p_1, p_2,\ldots, p_k\}``, the pointwise stabilizer of 

``p_1, p_2, \ldots, p_k`` is defined as 

``G_{p_1,\ldots, p_k} = 

\{g\in G | g(p_i) = p_i \forall i\in\{1, 2,\ldots,k\}\} ([1],p20). 

It is a subgroup of ``G``. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.named_groups import SymmetricGroup 

>>> S = SymmetricGroup(7) 

>>> Stab = S.pointwise_stabilizer([2, 3, 5]) 

>>> Stab.is_subgroup(S.stabilizer(2).stabilizer(3).stabilizer(5)) 

True 

 

See Also 

======== 

 

stabilizer, schreier_sims_incremental 

 

Notes 

===== 

 

When incremental == True, 

rather than the obvious implementation using successive calls to 

.stabilizer(), this uses the incremental Schreier-Sims algorithm 

to obtain a base with starting segment - the given points. 

 

""" 

if incremental: 

base, strong_gens = self.schreier_sims_incremental(base=points) 

stab_gens = [] 

degree = self.degree 

for gen in strong_gens: 

if [gen(point) for point in points] == points: 

stab_gens.append(gen) 

if not stab_gens: 

stab_gens = _af_new(list(range(degree))) 

return PermutationGroup(stab_gens) 

else: 

gens = self._generators 

degree = self.degree 

for x in points: 

gens = _stabilizer(degree, gens, x) 

return PermutationGroup(gens) 

 

def make_perm(self, n, seed=None): 

""" 

Multiply ``n`` randomly selected permutations from 

pgroup together, starting with the identity 

permutation. If ``n`` is a list of integers, those 

integers will be used to select the permutations and they 

will be applied in L to R order: make_perm((A, B, C)) will 

give CBA(I) where I is the identity permutation. 

 

``seed`` is used to set the seed for the random selection 

of permutations from pgroup. If this is a list of integers, 

the corresponding permutations from pgroup will be selected 

in the order give. This is mainly used for testing purposes. 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> Permutation.print_cyclic = True 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> a, b = [Permutation([1, 0, 3, 2]), Permutation([1, 3, 0, 2])] 

>>> G = PermutationGroup([a, b]) 

>>> G.make_perm(1, [0]) 

(0 1)(2 3) 

>>> G.make_perm(3, [0, 1, 0]) 

(0 2 3 1) 

>>> G.make_perm([0, 1, 0]) 

(0 2 3 1) 

 

See Also 

======== 

 

random 

""" 

if is_sequence(n): 

if seed is not None: 

raise ValueError('If n is a sequence, seed should be None') 

n, seed = len(n), n 

else: 

try: 

n = int(n) 

except TypeError: 

raise ValueError('n must be an integer or a sequence.') 

randrange = _randrange(seed) 

 

# start with the identity permutation 

result = Permutation(list(range(self.degree))) 

m = len(self) 

for i in range(n): 

p = self[randrange(m)] 

result = rmul(result, p) 

return result 

 

def random(self, af=False): 

"""Return a random group element 

""" 

rank = randrange(self.order()) 

return self.coset_unrank(rank, af) 

 

def random_pr(self, gen_count=11, iterations=50, _random_prec=None): 

"""Return a random group element using product replacement. 

 

For the details of the product replacement algorithm, see 

``_random_pr_init`` In ``random_pr`` the actual 'product replacement' 

is performed. Notice that if the attribute ``_random_gens`` 

is empty, it needs to be initialized by ``_random_pr_init``. 

 

See Also 

======== 

 

_random_pr_init 

 

""" 

if self._random_gens == []: 

self._random_pr_init(gen_count, iterations) 

random_gens = self._random_gens 

r = len(random_gens) - 1 

 

# handle randomized input for testing purposes 

if _random_prec is None: 

s = randrange(r) 

t = randrange(r - 1) 

if t == s: 

t = r - 1 

x = choice([1, 2]) 

e = choice([-1, 1]) 

else: 

s = _random_prec['s'] 

t = _random_prec['t'] 

if t == s: 

t = r - 1 

x = _random_prec['x'] 

e = _random_prec['e'] 

 

if x == 1: 

random_gens[s] = _af_rmul(random_gens[s], _af_pow(random_gens[t], e)) 

random_gens[r] = _af_rmul(random_gens[r], random_gens[s]) 

else: 

random_gens[s] = _af_rmul(_af_pow(random_gens[t], e), random_gens[s]) 

random_gens[r] = _af_rmul(random_gens[s], random_gens[r]) 

return _af_new(random_gens[r]) 

 

def random_stab(self, alpha, schreier_vector=None, _random_prec=None): 

"""Random element from the stabilizer of ``alpha``. 

 

The schreier vector for ``alpha`` is an optional argument used 

for speeding up repeated calls. The algorithm is described in [1], p.81 

 

See Also 

======== 

 

random_pr, orbit_rep 

 

""" 

if schreier_vector is None: 

schreier_vector = self.schreier_vector(alpha) 

if _random_prec is None: 

rand = self.random_pr() 

else: 

rand = _random_prec['rand'] 

beta = rand(alpha) 

h = self.orbit_rep(alpha, beta, schreier_vector) 

return rmul(~h, rand) 

 

def schreier_sims(self): 

"""Schreier-Sims algorithm. 

 

It computes the generators of the chain of stabilizers 

G > G_{b_1} > .. > G_{b1,..,b_r} > 1 

in which G_{b_1,..,b_i} stabilizes b_1,..,b_i, 

and the corresponding ``s`` cosets. 

An element of the group can be written as the product 

h_1*..*h_s. 

 

We use the incremental Schreier-Sims algorithm. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.permutations import Permutation 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> a = Permutation([0, 2, 1]) 

>>> b = Permutation([1, 0, 2]) 

>>> G = PermutationGroup([a, b]) 

>>> G.schreier_sims() 

>>> G.basic_transversals 

[{0: (2)(0 1), 1: (2), 2: (1 2)}, 

{0: (2), 2: (0 2)}] 

""" 

if self._transversals: 

return 

base, strong_gens = self.schreier_sims_incremental() 

self._base = base 

self._strong_gens = strong_gens 

if not base: 

self._transversals = [] 

self._basic_orbits = [] 

return 

 

strong_gens_distr = _distribute_gens_by_base(base, strong_gens) 

basic_orbits, transversals = _orbits_transversals_from_bsgs(base,\ 

strong_gens_distr) 

self._transversals = transversals 

self._basic_orbits = [sorted(x) for x in basic_orbits] 

 

def schreier_sims_incremental(self, base=None, gens=None): 

"""Extend a sequence of points and generating set to a base and strong 

generating set. 

 

Parameters 

========== 

 

base 

The sequence of points to be extended to a base. Optional 

parameter with default value ``[]``. 

gens 

The generating set to be extended to a strong generating set 

relative to the base obtained. Optional parameter with default 

value ``self.generators``. 

 

Returns 

======= 

 

(base, strong_gens) 

``base`` is the base obtained, and ``strong_gens`` is the strong 

generating set relative to it. The original parameters ``base``, 

``gens`` remain unchanged. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.named_groups import AlternatingGroup 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> from sympy.combinatorics.testutil import _verify_bsgs 

>>> A = AlternatingGroup(7) 

>>> base = [2, 3] 

>>> seq = [2, 3] 

>>> base, strong_gens = A.schreier_sims_incremental(base=seq) 

>>> _verify_bsgs(A, base, strong_gens) 

True 

>>> base[:2] 

[2, 3] 

 

Notes 

===== 

 

This version of the Schreier-Sims algorithm runs in polynomial time. 

There are certain assumptions in the implementation - if the trivial 

group is provided, ``base`` and ``gens`` are returned immediately, 

as any sequence of points is a base for the trivial group. If the 

identity is present in the generators ``gens``, it is removed as 

it is a redundant generator. 

The implementation is described in [1], pp. 90-93. 

 

See Also 

======== 

 

schreier_sims, schreier_sims_random 

 

""" 

if base is None: 

base = [] 

if gens is None: 

gens = self.generators[:] 

degree = self.degree 

id_af = list(range(degree)) 

# handle the trivial group 

if len(gens) == 1 and gens[0].is_Identity: 

return base, gens 

# prevent side effects 

_base, _gens = base[:], gens[:] 

# remove the identity as a generator 

_gens = [x for x in _gens if not x.is_Identity] 

# make sure no generator fixes all base points 

for gen in _gens: 

if all(x == gen._array_form[x] for x in _base): 

for new in id_af: 

if gen._array_form[new] != new: 

break 

else: 

assert None # can this ever happen? 

_base.append(new) 

# distribute generators according to basic stabilizers 

strong_gens_distr = _distribute_gens_by_base(_base, _gens) 

# initialize the basic stabilizers, basic orbits and basic transversals 

orbs = {} 

transversals = {} 

base_len = len(_base) 

for i in range(base_len): 

transversals[i] = dict(_orbit_transversal(degree, strong_gens_distr[i], 

_base[i], pairs=True, af=True)) 

orbs[i] = list(transversals[i].keys()) 

# main loop: amend the stabilizer chain until we have generators 

# for all stabilizers 

i = base_len - 1 

while i >= 0: 

# this flag is used to continue with the main loop from inside 

# a nested loop 

continue_i = False 

# test the generators for being a strong generating set 

db = {} 

for beta, u_beta in list(transversals[i].items()): 

for gen in strong_gens_distr[i]: 

gb = gen._array_form[beta] 

u1 = transversals[i][gb] 

g1 = _af_rmul(gen._array_form, u_beta) 

if g1 != u1: 

# test if the schreier generator is in the i+1-th 

# would-be basic stabilizer 

y = True 

try: 

u1_inv = db[gb] 

except KeyError: 

u1_inv = db[gb] = _af_invert(u1) 

schreier_gen = _af_rmul(u1_inv, g1) 

h, j = _strip_af(schreier_gen, _base, orbs, transversals, i) 

if j <= base_len: 

# new strong generator h at level j 

y = False 

elif h: 

# h fixes all base points 

y = False 

moved = 0 

while h[moved] == moved: 

moved += 1 

_base.append(moved) 

base_len += 1 

strong_gens_distr.append([]) 

if y is False: 

# if a new strong generator is found, update the 

# data structures and start over 

h = _af_new(h) 

for l in range(i + 1, j): 

strong_gens_distr[l].append(h) 

transversals[l] =\ 

dict(_orbit_transversal(degree, strong_gens_distr[l], 

_base[l], pairs=True, af=True)) 

orbs[l] = list(transversals[l].keys()) 

i = j - 1 

# continue main loop using the flag 

continue_i = True 

if continue_i is True: 

break 

if continue_i is True: 

break 

if continue_i is True: 

continue 

i -= 1 

# build the strong generating set 

strong_gens = list(uniq(i for gens in strong_gens_distr for i in gens)) 

return _base, strong_gens 

 

def schreier_sims_random(self, base=None, gens=None, consec_succ=10, 

_random_prec=None): 

r"""Randomized Schreier-Sims algorithm. 

 

The randomized Schreier-Sims algorithm takes the sequence ``base`` 

and the generating set ``gens``, and extends ``base`` to a base, and 

``gens`` to a strong generating set relative to that base with 

probability of a wrong answer at most ``2^{-consec\_succ}``, 

provided the random generators are sufficiently random. 

 

Parameters 

========== 

 

base 

The sequence to be extended to a base. 

gens 

The generating set to be extended to a strong generating set. 

consec_succ 

The parameter defining the probability of a wrong answer. 

_random_prec 

An internal parameter used for testing purposes. 

 

Returns 

======= 

 

(base, strong_gens) 

``base`` is the base and ``strong_gens`` is the strong generating 

set relative to it. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> from sympy.combinatorics.testutil import _verify_bsgs 

>>> from sympy.combinatorics.named_groups import SymmetricGroup 

>>> S = SymmetricGroup(5) 

>>> base, strong_gens = S.schreier_sims_random(consec_succ=5) 

>>> _verify_bsgs(S, base, strong_gens) #doctest: +SKIP 

True 

 

Notes 

===== 

 

The algorithm is described in detail in [1], pp. 97-98. It extends 

the orbits ``orbs`` and the permutation groups ``stabs`` to 

basic orbits and basic stabilizers for the base and strong generating 

set produced in the end. 

The idea of the extension process 

is to "sift" random group elements through the stabilizer chain 

and amend the stabilizers/orbits along the way when a sift 

is not successful. 

The helper function ``_strip`` is used to attempt 

to decompose a random group element according to the current 

state of the stabilizer chain and report whether the element was 

fully decomposed (successful sift) or not (unsuccessful sift). In 

the latter case, the level at which the sift failed is reported and 

used to amend ``stabs``, ``base``, ``gens`` and ``orbs`` accordingly. 

The halting condition is for ``consec_succ`` consecutive successful 

sifts to pass. This makes sure that the current ``base`` and ``gens`` 

form a BSGS with probability at least ``1 - 1/\text{consec\_succ}``. 

 

See Also 

======== 

 

schreier_sims 

 

""" 

if base is None: 

base = [] 

if gens is None: 

gens = self.generators 

base_len = len(base) 

n = self.degree 

# make sure no generator fixes all base points 

for gen in gens: 

if all(gen(x) == x for x in base): 

new = 0 

while gen._array_form[new] == new: 

new += 1 

base.append(new) 

base_len += 1 

# distribute generators according to basic stabilizers 

strong_gens_distr = _distribute_gens_by_base(base, gens) 

# initialize the basic stabilizers, basic transversals and basic orbits 

transversals = {} 

orbs = {} 

for i in range(base_len): 

transversals[i] = dict(_orbit_transversal(n, strong_gens_distr[i], 

base[i], pairs=True)) 

orbs[i] = list(transversals[i].keys()) 

# initialize the number of consecutive elements sifted 

c = 0 

# start sifting random elements while the number of consecutive sifts 

# is less than consec_succ 

while c < consec_succ: 

if _random_prec is None: 

g = self.random_pr() 

else: 

g = _random_prec['g'].pop() 

h, j = _strip(g, base, orbs, transversals) 

y = True 

# determine whether a new base point is needed 

if j <= base_len: 

y = False 

elif not h.is_Identity: 

y = False 

moved = 0 

while h(moved) == moved: 

moved += 1 

base.append(moved) 

base_len += 1 

strong_gens_distr.append([]) 

# if the element doesn't sift, amend the strong generators and 

# associated stabilizers and orbits 

if y is False: 

for l in range(1, j): 

strong_gens_distr[l].append(h) 

transversals[l] = dict(_orbit_transversal(n, 

strong_gens_distr[l], base[l], pairs=True)) 

orbs[l] = list(transversals[l].keys()) 

c = 0 

else: 

c += 1 

# build the strong generating set 

strong_gens = strong_gens_distr[0][:] 

for gen in strong_gens_distr[1]: 

if gen not in strong_gens: 

strong_gens.append(gen) 

return base, strong_gens 

 

def schreier_vector(self, alpha): 

"""Computes the schreier vector for ``alpha``. 

 

The Schreier vector efficiently stores information 

about the orbit of ``alpha``. It can later be used to quickly obtain 

elements of the group that send ``alpha`` to a particular element 

in the orbit. Notice that the Schreier vector depends on the order 

in which the group generators are listed. For a definition, see [3]. 

Since list indices start from zero, we adopt the convention to use 

"None" instead of 0 to signify that an element doesn't belong 

to the orbit. 

For the algorithm and its correctness, see [2], pp.78-80. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> from sympy.combinatorics.permutations import Permutation 

>>> a = Permutation([2, 4, 6, 3, 1, 5, 0]) 

>>> b = Permutation([0, 1, 3, 5, 4, 6, 2]) 

>>> G = PermutationGroup([a, b]) 

>>> G.schreier_vector(0) 

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

 

See Also 

======== 

 

orbit 

 

""" 

n = self.degree 

v = [None]*n 

v[alpha] = -1 

orb = [alpha] 

used = [False]*n 

used[alpha] = True 

gens = self.generators 

r = len(gens) 

for b in orb: 

for i in range(r): 

temp = gens[i]._array_form[b] 

if used[temp] is False: 

orb.append(temp) 

used[temp] = True 

v[temp] = i 

return v 

 

def stabilizer(self, alpha): 

r"""Return the stabilizer subgroup of ``alpha``. 

 

The stabilizer of ``\alpha`` is the group ``G_\alpha = 

\{g \in G | g(\alpha) = \alpha\}``. 

For a proof of correctness, see [1], p.79. 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> Permutation.print_cyclic = True 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> from sympy.combinatorics.named_groups import DihedralGroup 

>>> G = DihedralGroup(6) 

>>> G.stabilizer(5) 

PermutationGroup([ 

(5)(0 4)(1 3), 

(5)]) 

 

See Also 

======== 

 

orbit 

 

""" 

return PermGroup(_stabilizer(self._degree, self._generators, alpha)) 

 

@property 

def strong_gens(self): 

"""Return a strong generating set from the Schreier-Sims algorithm. 

 

A generating set ``S = \{g_1, g_2, ..., g_t\}`` for a permutation group 

``G`` is a strong generating set relative to the sequence of points 

(referred to as a "base") ``(b_1, b_2, ..., b_k)`` if, for 

``1 \leq i \leq k`` we have that the intersection of the pointwise 

stabilizer ``G^{(i+1)} := G_{b_1, b_2, ..., b_i}`` with ``S`` generates 

the pointwise stabilizer ``G^{(i+1)}``. The concepts of a base and 

strong generating set and their applications are discussed in depth 

in [1], pp. 87-89 and [2], pp. 55-57. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.named_groups import DihedralGroup 

>>> D = DihedralGroup(4) 

>>> D.strong_gens 

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

>>> D.base 

[0, 1] 

 

See Also 

======== 

 

base, basic_transversals, basic_orbits, basic_stabilizers 

 

""" 

if self._strong_gens == []: 

self.schreier_sims() 

return self._strong_gens 

 

def subgroup_search(self, prop, base=None, strong_gens=None, tests=None, 

init_subgroup=None): 

"""Find the subgroup of all elements satisfying the property ``prop``. 

 

This is done by a depth-first search with respect to base images that 

uses several tests to prune the search tree. 

 

Parameters 

========== 

 

prop 

The property to be used. Has to be callable on group elements 

and always return ``True`` or ``False``. It is assumed that 

all group elements satisfying ``prop`` indeed form a subgroup. 

base 

A base for the supergroup. 

strong_gens 

A strong generating set for the supergroup. 

tests 

A list of callables of length equal to the length of ``base``. 

These are used to rule out group elements by partial base images, 

so that ``tests[l](g)`` returns False if the element ``g`` is known 

not to satisfy prop base on where g sends the first ``l + 1`` base 

points. 

init_subgroup 

if a subgroup of the sought group is 

known in advance, it can be passed to the function as this 

parameter. 

 

Returns 

======= 

 

res 

The subgroup of all elements satisfying ``prop``. The generating 

set for this group is guaranteed to be a strong generating set 

relative to the base ``base``. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.named_groups import (SymmetricGroup, 

... AlternatingGroup) 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> from sympy.combinatorics.testutil import _verify_bsgs 

>>> S = SymmetricGroup(7) 

>>> prop_even = lambda x: x.is_even 

>>> base, strong_gens = S.schreier_sims_incremental() 

>>> G = S.subgroup_search(prop_even, base=base, strong_gens=strong_gens) 

>>> G.is_subgroup(AlternatingGroup(7)) 

True 

>>> _verify_bsgs(G, base, G.generators) 

True 

 

Notes 

===== 

 

This function is extremely lenghty and complicated and will require 

some careful attention. The implementation is described in 

[1], pp. 114-117, and the comments for the code here follow the lines 

of the pseudocode in the book for clarity. 

 

The complexity is exponential in general, since the search process by 

itself visits all members of the supergroup. However, there are a lot 

of tests which are used to prune the search tree, and users can define 

their own tests via the ``tests`` parameter, so in practice, and for 

some computations, it's not terrible. 

 

A crucial part in the procedure is the frequent base change performed 

(this is line 11 in the pseudocode) in order to obtain a new basic 

stabilizer. The book mentiones that this can be done by using 

``.baseswap(...)``, however the current imlementation uses a more 

straightforward way to find the next basic stabilizer - calling the 

function ``.stabilizer(...)`` on the previous basic stabilizer. 

 

""" 

# initialize BSGS and basic group properties 

def get_reps(orbits): 

# get the minimal element in the base ordering 

return [min(orbit, key = lambda x: base_ordering[x]) \ 

for orbit in orbits] 

 

def update_nu(l): 

temp_index = len(basic_orbits[l]) + 1 -\ 

len(res_basic_orbits_init_base[l]) 

# this corresponds to the element larger than all points 

if temp_index >= len(sorted_orbits[l]): 

nu[l] = base_ordering[degree] 

else: 

nu[l] = sorted_orbits[l][temp_index] 

 

if base is None: 

base, strong_gens = self.schreier_sims_incremental() 

base_len = len(base) 

degree = self.degree 

identity = _af_new(list(range(degree))) 

base_ordering = _base_ordering(base, degree) 

# add an element larger than all points 

base_ordering.append(degree) 

# add an element smaller than all points 

base_ordering.append(-1) 

# compute BSGS-related structures 

strong_gens_distr = _distribute_gens_by_base(base, strong_gens) 

basic_orbits, transversals = _orbits_transversals_from_bsgs(base, 

strong_gens_distr) 

# handle subgroup initialization and tests 

if init_subgroup is None: 

init_subgroup = PermutationGroup([identity]) 

if tests is None: 

trivial_test = lambda x: True 

tests = [] 

for i in range(base_len): 

tests.append(trivial_test) 

# line 1: more initializations. 

res = init_subgroup 

f = base_len - 1 

l = base_len - 1 

# line 2: set the base for K to the base for G 

res_base = base[:] 

# line 3: compute BSGS and related structures for K 

res_base, res_strong_gens = res.schreier_sims_incremental( 

base=res_base) 

res_strong_gens_distr = _distribute_gens_by_base(res_base, 

res_strong_gens) 

res_generators = res.generators 

res_basic_orbits_init_base = \ 

[_orbit(degree, res_strong_gens_distr[i], res_base[i])\ 

for i in range(base_len)] 

# initialize orbit representatives 

orbit_reps = [None]*base_len 

# line 4: orbit representatives for f-th basic stabilizer of K 

orbits = _orbits(degree, res_strong_gens_distr[f]) 

orbit_reps[f] = get_reps(orbits) 

# line 5: remove the base point from the representatives to avoid 

# getting the identity element as a generator for K 

orbit_reps[f].remove(base[f]) 

# line 6: more initializations 

c = [0]*base_len 

u = [identity]*base_len 

sorted_orbits = [None]*base_len 

for i in range(base_len): 

sorted_orbits[i] = basic_orbits[i][:] 

sorted_orbits[i].sort(key=lambda point: base_ordering[point]) 

# line 7: initializations 

mu = [None]*base_len 

nu = [None]*base_len 

# this corresponds to the element smaller than all points 

mu[l] = degree + 1 

update_nu(l) 

# initialize computed words 

computed_words = [identity]*base_len 

# line 8: main loop 

while True: 

# apply all the tests 

while l < base_len - 1 and \ 

computed_words[l](base[l]) in orbit_reps[l] and \ 

base_ordering[mu[l]] < \ 

base_ordering[computed_words[l](base[l])] < \ 

base_ordering[nu[l]] and \ 

tests[l](computed_words): 

# line 11: change the (partial) base of K 

new_point = computed_words[l](base[l]) 

res_base[l] = new_point 

new_stab_gens = _stabilizer(degree, res_strong_gens_distr[l], 

new_point) 

res_strong_gens_distr[l + 1] = new_stab_gens 

# line 12: calculate minimal orbit representatives for the 

# l+1-th basic stabilizer 

orbits = _orbits(degree, new_stab_gens) 

orbit_reps[l + 1] = get_reps(orbits) 

# line 13: amend sorted orbits 

l += 1 

temp_orbit = [computed_words[l - 1](point) for point 

in basic_orbits[l]] 

temp_orbit.sort(key=lambda point: base_ordering[point]) 

sorted_orbits[l] = temp_orbit 

# lines 14 and 15: update variables used minimality tests 

new_mu = degree + 1 

for i in range(l): 

if base[l] in res_basic_orbits_init_base[i]: 

candidate = computed_words[i](base[i]) 

if base_ordering[candidate] > base_ordering[new_mu]: 

new_mu = candidate 

mu[l] = new_mu 

update_nu(l) 

# line 16: determine the new transversal element 

c[l] = 0 

temp_point = sorted_orbits[l][c[l]] 

gamma = computed_words[l - 1]._array_form.index(temp_point) 

u[l] = transversals[l][gamma] 

# update computed words 

computed_words[l] = rmul(computed_words[l - 1], u[l]) 

# lines 17 & 18: apply the tests to the group element found 

g = computed_words[l] 

temp_point = g(base[l]) 

if l == base_len - 1 and \ 

base_ordering[mu[l]] < \ 

base_ordering[temp_point] < base_ordering[nu[l]] and \ 

temp_point in orbit_reps[l] and \ 

tests[l](computed_words) and \ 

prop(g): 

# line 19: reset the base of K 

res_generators.append(g) 

res_base = base[:] 

# line 20: recalculate basic orbits (and transversals) 

res_strong_gens.append(g) 

res_strong_gens_distr = _distribute_gens_by_base(res_base, 

res_strong_gens) 

res_basic_orbits_init_base = \ 

[_orbit(degree, res_strong_gens_distr[i], res_base[i]) \ 

for i in range(base_len)] 

# line 21: recalculate orbit representatives 

# line 22: reset the search depth 

orbit_reps[f] = get_reps(orbits) 

l = f 

# line 23: go up the tree until in the first branch not fully 

# searched 

while l >= 0 and c[l] == len(basic_orbits[l]) - 1: 

l = l - 1 

# line 24: if the entire tree is traversed, return K 

if l == -1: 

return PermutationGroup(res_generators) 

# lines 25-27: update orbit representatives 

if l < f: 

# line 26 

f = l 

c[l] = 0 

# line 27 

temp_orbits = _orbits(degree, res_strong_gens_distr[f]) 

orbit_reps[f] = get_reps(temp_orbits) 

# line 28: update variables used for minimality testing 

mu[l] = degree + 1 

temp_index = len(basic_orbits[l]) + 1 - \ 

len(res_basic_orbits_init_base[l]) 

if temp_index >= len(sorted_orbits[l]): 

nu[l] = base_ordering[degree] 

else: 

nu[l] = sorted_orbits[l][temp_index] 

# line 29: set the next element from the current branch and update 

# accorndingly 

c[l] += 1 

if l == 0: 

gamma = sorted_orbits[l][c[l]] 

else: 

gamma = computed_words[l - 1]._array_form.index(sorted_orbits[l][c[l]]) 

 

u[l] = transversals[l][gamma] 

if l == 0: 

computed_words[l] = u[l] 

else: 

computed_words[l] = rmul(computed_words[l - 1], u[l]) 

 

@property 

def transitivity_degree(self): 

"""Compute the degree of transitivity of the group. 

 

A permutation group ``G`` acting on ``\Omega = \{0, 1, ..., n-1\}`` is 

``k``-fold transitive, if, for any k points 

``(a_1, a_2, ..., a_k)\in\Omega`` and any k points 

``(b_1, b_2, ..., b_k)\in\Omega`` there exists ``g\in G`` such that 

``g(a_1)=b_1, g(a_2)=b_2, ..., g(a_k)=b_k`` 

The degree of transitivity of ``G`` is the maximum ``k`` such that 

``G`` is ``k``-fold transitive. ([8]) 

 

Examples 

======== 

 

>>> from sympy.combinatorics.perm_groups import PermutationGroup 

>>> from sympy.combinatorics.permutations import Permutation 

>>> a = Permutation([1, 2, 0]) 

>>> b = Permutation([1, 0, 2]) 

>>> G = PermutationGroup([a, b]) 

>>> G.transitivity_degree 

3 

 

See Also 

======== 

is_transitive, orbit 

 

""" 

if self._transitivity_degree is None: 

n = self.degree 

G = self 

# if G is k-transitive, a tuple (a_0,..,a_k) 

# can be brought to (b_0,...,b_(k-1), b_k) 

# where b_0,...,b_(k-1) are fixed points; 

# consider the group G_k which stabilizes b_0,...,b_(k-1) 

# if G_k is transitive on the subset excluding b_0,...,b_(k-1) 

# then G is (k+1)-transitive 

for i in range(n): 

orb = G.orbit((i)) 

if len(orb) != n - i: 

self._transitivity_degree = i 

return i 

G = G.stabilizer(i) 

self._transitivity_degree = n 

return n 

else: 

return self._transitivity_degree 

 

 

def _orbit(degree, generators, alpha, action='tuples'): 

r"""Compute the orbit of alpha ``\{g(\alpha) | g \in G\}`` as a set. 

 

The time complexity of the algorithm used here is ``O(|Orb|*r)`` where 

``|Orb|`` is the size of the orbit and ``r`` is the number of generators of 

the group. For a more detailed analysis, see [1], p.78, [2], pp. 19-21. 

Here alpha can be a single point, or a list of points. 

 

If alpha is a single point, the ordinary orbit is computed. 

if alpha is a list of points, there are three available options: 

 

'union' - computes the union of the orbits of the points in the list 

'tuples' - computes the orbit of the list interpreted as an ordered 

tuple under the group action ( i.e., g((1, 2, 3)) = (g(1), g(2), g(3)) ) 

'sets' - computes the orbit of the list interpreted as a sets 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> from sympy.combinatorics.perm_groups import PermutationGroup, _orbit 

>>> a = Permutation([1, 2, 0, 4, 5, 6, 3]) 

>>> G = PermutationGroup([a]) 

>>> _orbit(G.degree, G.generators, 0) 

set([0, 1, 2]) 

>>> _orbit(G.degree, G.generators, [0, 4], 'union') 

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

 

See Also 

======== 

 

orbit, orbit_transversal 

 

""" 

if not hasattr(alpha, '__getitem__'): 

alpha = [alpha] 

 

gens = [x._array_form for x in generators] 

if len(alpha) == 1 or action == 'union': 

orb = alpha 

used = [False]*degree 

for el in alpha: 

used[el] = True 

for b in orb: 

for gen in gens: 

temp = gen[b] 

if used[temp] == False: 

orb.append(temp) 

used[temp] = True 

return set(orb) 

elif action == 'tuples': 

alpha = tuple(alpha) 

orb = [alpha] 

used = {alpha} 

for b in orb: 

for gen in gens: 

temp = tuple([gen[x] for x in b]) 

if temp not in used: 

orb.append(temp) 

used.add(temp) 

return set(orb) 

elif action == 'sets': 

alpha = frozenset(alpha) 

orb = [alpha] 

used = {alpha} 

for b in orb: 

for gen in gens: 

temp = frozenset([gen[x] for x in b]) 

if temp not in used: 

orb.append(temp) 

used.add(temp) 

return {tuple(x) for x in orb} 

 

def _orbits(degree, generators): 

"""Compute the orbits of G. 

 

If rep=False it returns a list of sets else it returns a list of 

representatives of the orbits 

 

Examples 

======== 

 

>>> from sympy.combinatorics.permutations import Permutation 

>>> from sympy.combinatorics.perm_groups import PermutationGroup, _orbits 

>>> a = Permutation([0, 2, 1]) 

>>> b = Permutation([1, 0, 2]) 

>>> _orbits(a.size, [a, b]) 

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

""" 

 

seen = set() # elements that have already appeared in orbits 

orbs = [] 

sorted_I = list(range(degree)) 

I = set(sorted_I) 

while I: 

i = sorted_I[0] 

orb = _orbit(degree, generators, i) 

orbs.append(orb) 

# remove all indices that are in this orbit 

I -= orb 

sorted_I = [i for i in sorted_I if i not in orb] 

return orbs 

 

def _orbit_transversal(degree, generators, alpha, pairs, af=False): 

r"""Computes a transversal for the orbit of ``alpha`` as a set. 

 

generators generators of the group ``G`` 

 

For a permutation group ``G``, a transversal for the orbit 

``Orb = \{g(\alpha) | g \in G\}`` is a set 

``\{g_\beta | g_\beta(\alpha) = \beta\}`` for ``\beta \in Orb``. 

Note that there may be more than one possible transversal. 

If ``pairs`` is set to ``True``, it returns the list of pairs 

``(\beta, g_\beta)``. For a proof of correctness, see [1], p.79 

 

if af is True, the transversal elements are given in array form 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> Permutation.print_cyclic = True 

>>> from sympy.combinatorics.named_groups import DihedralGroup 

>>> from sympy.combinatorics.perm_groups import _orbit_transversal 

>>> G = DihedralGroup(6) 

>>> _orbit_transversal(G.degree, G.generators, 0, False) 

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

""" 

 

tr = [(alpha, list(range(degree)))] 

used = [False]*degree 

used[alpha] = True 

gens = [x._array_form for x in generators] 

for x, px in tr: 

for gen in gens: 

temp = gen[x] 

if used[temp] == False: 

tr.append((temp, _af_rmul(gen, px))) 

used[temp] = True 

if pairs: 

if not af: 

tr = [(x, _af_new(y)) for x, y in tr] 

return tr 

 

if af: 

return [y for _, y in tr] 

 

return [_af_new(y) for _, y in tr] 

 

def _stabilizer(degree, generators, alpha): 

r"""Return the stabilizer subgroup of ``alpha``. 

 

The stabilizer of ``\alpha`` is the group ``G_\alpha = 

\{g \in G | g(\alpha) = \alpha\}``. 

For a proof of correctness, see [1], p.79. 

 

degree degree of G 

generators generators of G 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Permutation 

>>> Permutation.print_cyclic = True 

>>> from sympy.combinatorics.perm_groups import _stabilizer 

>>> from sympy.combinatorics.named_groups import DihedralGroup 

>>> G = DihedralGroup(6) 

>>> _stabilizer(G.degree, G.generators, 5) 

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

 

See Also 

======== 

 

orbit 

 

""" 

orb = [alpha] 

table = {alpha: list(range(degree))} 

table_inv = {alpha: list(range(degree))} 

used = [False]*degree 

used[alpha] = True 

gens = [x._array_form for x in generators] 

stab_gens = [] 

for b in orb: 

for gen in gens: 

temp = gen[b] 

if used[temp] is False: 

gen_temp = _af_rmul(gen, table[b]) 

orb.append(temp) 

table[temp] = gen_temp 

table_inv[temp] = _af_invert(gen_temp) 

used[temp] = True 

else: 

schreier_gen = _af_rmuln(table_inv[temp], gen, table[b]) 

if schreier_gen not in stab_gens: 

stab_gens.append(schreier_gen) 

return [_af_new(x) for x in stab_gens] 

 

PermGroup = PermutationGroup