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

""" 

libGAP element wrapper 

  

This document describes the individual wrappers for various GAP 

elements. For general information about libGAP, you should read the 

:mod:`~sage.libs.gap.libgap` module documentation. 

""" 

  

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

# Copyright (C) 2012 Volker Braun <vbraun.name@gmail.com> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

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

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

  

from __future__ import absolute_import, print_function 

  

from cpython.object cimport * 

from cysignals.signals cimport sig_on, sig_off 

  

from sage.misc.cachefunc import cached_method 

from sage.structure.sage_object cimport SageObject 

from sage.structure.parent import Parent 

from sage.rings.all import ZZ, QQ, RDF 

  

decode_type_number = { 

libGAP_T_INT: 'T_INT (integer)', 

libGAP_T_INTPOS: 'T_INTPOS (positive integer)', 

libGAP_T_INTNEG: 'T_INTNEG (negative integer)', 

libGAP_T_RAT: 'T_RAT (rational number)', 

libGAP_T_CYC: 'T_CYC (universal cylotomic)', 

libGAP_T_FFE: 'T_FFE (finite field element)', 

libGAP_T_PERM2: 'T_PERM2', 

libGAP_T_PERM4: 'T_PERM4', 

libGAP_T_BOOL: 'T_BOOL', 

libGAP_T_CHAR: 'T_CHAR', 

libGAP_T_FUNCTION: 'T_FUNCTION', 

libGAP_T_PLIST: 'T_PLIST', 

libGAP_T_PLIST_CYC: 'T_PLIST_CYC', 

libGAP_T_BLIST: 'T_BLIST', 

libGAP_T_STRING: 'T_STRING', 

libGAP_T_MACFLOAT: 'T_MACFLOAT (hardware floating point number)', 

libGAP_T_COMOBJ: 'T_COMOBJ (component object)', 

libGAP_T_POSOBJ: 'T_POSOBJ (positional object)', 

libGAP_T_DATOBJ: 'T_DATOBJ (data object)', 

libGAP_T_WPOBJ: 'T_WPOBJ (weak pointer object)', 

} 

  

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

### helper functions to construct lists and records ######################## 

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

  

cdef libGAP_Obj make_gap_list(sage_list) except NULL: 

""" 

Convert Sage lists into Gap lists 

  

INPUT: 

  

- ``a`` -- list of :class:`GapElement`. 

  

OUTPUT: 

  

The list of the elements in ``a`` as a Gap ``Obj``. 

""" 

# FIXME slow -- to make fast directly use ADD_LIST in Gap's C code. 

from sage.libs.gap.libgap import libgap 

cdef GapElement l = libgap.eval('[]') 

for x in sage_list: 

l.Add(x) 

return l.value 

  

  

cdef libGAP_Obj make_gap_record(sage_dict) except NULL: 

""" 

Convert Sage lists into Gap lists 

  

INPUT: 

  

- ``a`` -- list of :class:`GapElement`. 

  

OUTPUT: 

  

The list of the elements in ``a`` as a Gap ``Obj``. 

  

TESTS:: 

  

sage: libgap({'a': 1, 'b':123}) # indirect doctest 

rec( a := 1, b := 123 ) 

""" 

from sage.libs.gap.libgap import libgap 

data = [ (str(key), libgap(value)) for key, value in sage_dict.iteritems() ] 

  

libgap_enter() 

cdef libGAP_Obj rec = libGAP_NEW_PREC(len(data)) 

cdef GapElement val 

cdef libGAP_UInt rnam 

for d in data: 

key, val = d 

rnam = libGAP_RNamName(key) 

libGAP_AssPRec(rec, rnam, val.value) 

libgap_exit() 

return rec 

  

  

cdef libGAP_Obj make_gap_integer(sage_int) except NULL: 

""" 

Convert Sage integer into Gap integer 

  

INPUT: 

  

- ``sage_int`` -- a Sage integer. 

  

OUTPUT: 

  

The integer as a GAP ``Obj``. 

  

TESTS:: 

  

sage: libgap(1) # indirect doctest 

1 

""" 

libgap_enter() 

cdef libGAP_Obj result = libGAP_INTOBJ_INT(<int>sage_int) 

libgap_exit() 

return result 

  

  

cdef libGAP_Obj make_gap_string(sage_string) except NULL: 

""" 

Convert a Sage string to a Gap string 

  

INPUT: 

  

- ``sage_string`` -- a Sage integer. 

  

OUTPUT: 

  

The string as a GAP ``Obj``. 

  

TESTS:: 

  

sage: libgap('string') # indirect doctest 

"string" 

""" 

libgap_enter() 

cdef libGAP_Obj result 

libGAP_C_NEW_STRING(result, len(sage_string), <char*>sage_string) 

libgap_exit() 

return result 

  

  

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

### generic construction of GapElements #################################### 

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

  

cdef GapElement make_any_gap_element(parent, libGAP_Obj obj): 

""" 

Return the libGAP element wrapper of ``obj`` 

  

The most suitable subclass of GapElement is determined 

automatically. Use this function to wrap GAP objects unless you 

know exactly which type it is (then you can use the specialized 

``make_GapElement_...``) 

  

TESTS:: 

  

sage: T_CHAR = libgap.eval("'c'"); T_CHAR 

"c" 

sage: type(T_CHAR) 

<type 'sage.libs.gap.element.GapElement_String'> 

  

sage: libgap.eval("['a', 'b', 'c']") # gap strings are also lists of chars 

"abc" 

sage: t = libgap.UnorderedTuples('abc', 2); t 

[ "aa", "ab", "ac", "bb", "bc", "cc" ] 

sage: t[1] 

"ab" 

sage: t[1].sage() 

'ab' 

sage: t.sage() 

['aa', 'ab', 'ac', 'bb', 'bc', 'cc'] 

  

Check that :trac:`18158` is fixed:: 

  

sage: S = SymmetricGroup(5) 

sage: irr = libgap.Irr(S)[3] 

sage: irr[0] 

6 

sage: irr[1] 

0 

""" 

if obj is NULL: 

return make_GapElement(parent, obj) 

cdef int num = libGAP_TNUM_OBJ(obj) 

if num == libGAP_T_INT or num == libGAP_T_INTPOS or num == libGAP_T_INTNEG: 

return make_GapElement_Integer(parent, obj) 

elif num == libGAP_T_MACFLOAT: 

return make_GapElement_Float(parent, obj) 

elif num == libGAP_T_CYC: 

return make_GapElement_Cyclotomic(parent, obj) 

elif num == libGAP_T_FFE: 

return make_GapElement_FiniteField(parent, obj) 

elif num == libGAP_T_RAT: 

return make_GapElement_Rational(parent, obj) 

elif num == libGAP_T_BOOL: 

return make_GapElement_Boolean(parent, obj) 

elif num == libGAP_T_FUNCTION: 

return make_GapElement_Function(parent, obj) 

elif num == libGAP_T_PERM2 or num == libGAP_T_PERM4: 

return make_GapElement_Permutation(parent, obj) 

elif libGAP_FIRST_RECORD_TNUM <= num <= libGAP_LAST_RECORD_TNUM: 

return make_GapElement_Record(parent, obj) 

elif libGAP_FIRST_LIST_TNUM <= num <= libGAP_LAST_LIST_TNUM and libGAP_LEN_PLIST(obj) == 0: 

# Empty lists are lists and not strings in Python 

return make_GapElement_List(parent, obj) 

elif libGAP_IsStringConv(obj): 

# GAP strings are lists, too. Make sure this comes before non-empty make_GapElement_List 

return make_GapElement_String(parent, obj) 

elif libGAP_IS_LIST(obj): 

return make_GapElement_List(parent, obj) 

elif num == libGAP_T_CHAR: 

ch = make_GapElement(parent, obj).IntChar().sage() 

return make_GapElement_String(parent, make_gap_string(chr(ch))) 

result = make_GapElement(parent, obj) 

if num == libGAP_T_POSOBJ: 

if result.IsZmodnZObj(): 

return make_GapElement_IntegerMod(parent, obj) 

if num == libGAP_T_COMOBJ: 

if result.IsRing(): 

return make_GapElement_Ring(parent, obj) 

return result 

  

  

  

  

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

### GapElement ############################################################# 

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

  

cdef GapElement make_GapElement(parent, libGAP_Obj obj): 

r""" 

Turn a Gap C object (of type ``Obj``) into a Cython ``GapElement``. 

  

INPUT: 

  

- ``parent`` -- the parent of the new :class:`GapElement` 

  

- ``obj`` -- a GAP object. 

  

OUTPUT: 

  

A :class:`GapElement_Function` instance, or one of its derived 

classes if it is a better fit for the GAP object. 

  

EXAMPLES:: 

  

sage: libgap(0) 

0 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_Integer'> 

  

sage: libgap.eval('') 

NULL 

  

sage: libgap(None) 

Traceback (most recent call last): 

... 

AttributeError: 'NoneType' object has no attribute '_gap_init_' 

""" 

cdef GapElement r = GapElement.__new__(GapElement) 

r._initialize(parent, obj) 

return r 

  

  

cdef class GapElement(RingElement): 

r""" 

Wrapper for all Gap objects. 

  

.. NOTE:: 

  

In order to create ``GapElements`` you should use the 

``libgap`` instance (the parent of all Gap elements) to 

convert things into ``GapElement``. You must not create 

``GapElement`` instances manually. 

  

EXAMPLES:: 

  

sage: libgap(0) 

0 

  

If Gap finds an error while evaluating, a corresponding assertion is raised:: 

  

sage: libgap.eval('1/0') 

Traceback (most recent call last): 

... 

ValueError: libGAP: Error, Rational operations: <divisor> must not be zero 

  

Also, a ``ValueError`` is raised if the input is not a simple expression:: 

  

sage: libgap.eval('1; 2; 3') 

Traceback (most recent call last): 

... 

ValueError: can only evaluate a single statement 

""" 

  

def __cinit__(self): 

""" 

The Cython constructor. 

  

EXAMPLES:: 

  

sage: libgap.eval('1') 

1 

""" 

self.value = NULL 

self._compare_by_id = False 

  

  

def __init__(self): 

""" 

The ``GapElement`` constructor 

  

Users must use the ``libgap`` instance to construct instances 

of :class:`GapElement`. Cython programmers must use 

:func:`make_GapElement` factory function. 

  

TESTS:: 

  

sage: from sage.libs.gap.element import GapElement 

sage: GapElement() 

Traceback (most recent call last): 

... 

TypeError: this class cannot be instantiated from Python 

""" 

raise TypeError('this class cannot be instantiated from Python') 

  

cdef _initialize(self, parent, libGAP_Obj obj): 

r""" 

Initialize the GapElement. 

  

This Cython method is called from :func:`make_GapElement` to 

initialize the newly-constructed object. You must never call 

it manually. 

  

TESTS:: 

  

sage: n_before = libgap.count_GAP_objects() 

sage: a = libgap.eval('123') 

sage: b = libgap.eval('456') 

sage: c = libgap.eval('CyclicGroup(3)') 

sage: d = libgap.eval('"a string"') 

sage: libgap.collect() 

sage: del c 

sage: libgap.collect() 

sage: n_after = libgap.count_GAP_objects() 

sage: n_after - n_before 

3 

""" 

assert self.value is NULL 

self._parent = parent 

self.value = obj 

if obj is NULL: 

return 

reference_obj(obj) 

  

  

def __dealloc__(self): 

r""" 

The Cython destructor 

  

TESTS:: 

  

sage: pre_refcount = libgap.count_GAP_objects() 

sage: def f(): 

....: local_variable = libgap.eval('"This is a new string"') 

sage: f() 

sage: f() 

sage: f() 

sage: post_refcount = libgap.count_GAP_objects() 

sage: post_refcount - pre_refcount 

0 

""" 

if self.value is NULL: 

return 

dereference_obj(self.value) 

  

def __copy__(self): 

r""" 

TESTS:: 

  

sage: a = libgap(1) 

sage: a.__copy__() is a 

True 

  

sage: a = libgap(1/3) 

sage: a.__copy__() is a 

True 

  

sage: a = libgap([1,2]) 

sage: b = a.__copy__() 

sage: a is b 

False 

sage: a[0] = 3 

sage: a 

[ 3, 2 ] 

sage: b 

[ 1, 2 ] 

  

sage: a = libgap([[0,1],[2,3,4]]) 

sage: b = a.__copy__() 

sage: b[0][1] = -2 

sage: b 

[ [ 0, -2 ], [ 2, 3, 4 ] ] 

sage: a 

[ [ 0, -2 ], [ 2, 3, 4 ] ] 

""" 

if libGAP_IS_MUTABLE_OBJ(self.value): 

return make_any_gap_element(self.parent(), libGAP_SHALLOW_COPY_OBJ(self.value)) 

else: 

return self 

  

cpdef GapElement deepcopy(self, bint mut): 

r""" 

Return a deepcopy of this Gap object 

  

Note that this is the same thing as calling ``StructuralCopy`` but much 

faster. 

  

INPUT: 

  

- ``mut`` - (boolean) wheter to return an mutable copy 

  

EXAMPLES:: 

  

sage: a = libgap([[0,1],[2,3]]) 

sage: b = a.deepcopy(1) 

sage: b[0,0] = 5 

sage: a 

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

sage: b 

[ [ 5, 1 ], [ 2, 3 ] ] 

  

sage: l = libgap([0,1]) 

sage: l.deepcopy(0).IsMutable() 

false 

sage: l.deepcopy(1).IsMutable() 

true 

""" 

if libGAP_IS_MUTABLE_OBJ(self.value): 

return make_any_gap_element(self.parent(), libGAP_CopyObj(self.value, mut)) 

else: 

return self 

  

def __deepcopy__(self, memo): 

r""" 

TESTS:: 

  

sage: a = libgap([[0,1],[2]]) 

sage: b = deepcopy(a) 

sage: a[0,0] = -1 

sage: a 

[ [ -1, 1 ], [ 2 ] ] 

sage: b 

[ [ 0, 1 ], [ 2 ] ] 

""" 

return self.deepcopy(0) 

  

def __contains__(self, other): 

r""" 

TESTS:: 

  

sage: libgap(1) in libgap.eval('Integers') 

True 

sage: 1 in libgap.eval('Integers') 

True 

  

sage: 3 in libgap([1,5,3,2]) 

True 

sage: -5 in libgap([1,5,3,2]) 

False 

  

sage: libgap.eval('Integers') in libgap(1) 

Traceback (most recent call last): 

... 

ValueError: libGAP: Error, no method found! Error, no 1st choice method found for `in' on 2 arguments 

""" 

from sage.libs.gap.libgap import libgap 

GAP_IN = libgap.eval(r'\in') 

return GAP_IN(other, self).sage() 

  

cpdef _type_number(self): 

""" 

Return the GAP internal type number. 

  

This is only useful for libgap development purposes. 

  

OUTPUT: 

  

Integer. 

  

EXAMPLES:: 

  

sage: x = libgap(1) 

sage: x._type_number() 

(0L, 'T_INT (integer)') 

""" 

n = libGAP_TNUM_OBJ(self.value) 

global decode_type_number 

name = decode_type_number.get(n, 'unknown') 

return (n, name) 

  

def __dir__(self): 

""" 

Customize tab completion 

  

EXAMPLES:: 

  

sage: G = libgap.DihedralGroup(4) 

sage: 'GeneratorsOfMagmaWithInverses' in dir(G) 

True 

sage: 'GeneratorsOfGroup' in dir(G) # known bug 

False 

sage: x = libgap(1) 

sage: len(dir(x)) > 100 

True 

""" 

from sage.libs.gap.operations import OperationInspector 

ops = OperationInspector(self).op_names() 

return dir(self.__class__) + ops 

  

def __getattr__(self, name): 

r""" 

Return functionoid implementing the function ``name``. 

  

EXAMPLES:: 

  

sage: lst = libgap([]) 

sage: 'Add' in dir(lst) # This is why tab-completion works 

True 

sage: lst.Add(1) # this is the syntactic sugar 

sage: lst 

[ 1 ] 

  

The above is equivalent to the following calls:: 

  

sage: lst = libgap.eval('[]') 

sage: libgap.eval('Add') (lst, 1) 

sage: lst 

[ 1 ] 

  

TESTS:: 

  

sage: lst.Adddddd(1) 

Traceback (most recent call last): 

... 

AttributeError: name "Adddddd" is not defined in GAP. 

  

sage: libgap.eval('some_name := 1') 

1 

sage: lst.some_name 

Traceback (most recent call last): 

... 

AttributeError: name "some_name" does not define a GAP function. 

""" 

if name in ('__dict__', '_getAttributeNames', '__custom_name', 'keys'): 

raise AttributeError('Python special name, not a GAP function.') 

try: 

proxy = make_GapElement_MethodProxy\ 

(self.parent(), gap_eval(name), self) 

except ValueError: 

raise AttributeError('name "'+str(name)+'" is not defined in GAP.') 

if not proxy.is_function(): 

raise AttributeError('name "'+str(name)+'" does not define a GAP function.') 

return proxy 

  

def _repr_(self): 

r""" 

Return a string representation of ``self``. 

  

EXAMPLES:: 

  

sage: libgap(0) 

0 

sage: libgap.eval('') 

NULL 

sage: libgap(0) 

0 

sage: libgap(0)._repr_() 

'0' 

""" 

if self.value == NULL: 

return 'NULL' 

try: 

libgap_enter() 

libgap_start_interaction('') 

libGAP_ViewObjHandler(self.value) 

s = libgap_get_output() 

return s.strip() 

finally: 

libgap_finish_interaction() 

libgap_exit() 

  

cpdef _set_compare_by_id(self): 

""" 

Set comparison to compare by ``id`` 

  

By default, GAP is used to compare libGAP objects. However, 

this is not defined for all GAP objects. To have libGAP play 

nice with ``UniqueRepresentation``, comparison must always 

work. This method allows one to override the comparison to 

sort by the (unique) Python ``id``. 

  

Obviously it is a bad idea to change the comparison of objects 

after you have inserted them into a set/dict. You also must 

not mix libGAP objects with different sort methods in the same 

container. 

  

EXAMPLES:: 

  

sage: F1 = libgap.FreeGroup(['a']) 

sage: F2 = libgap.FreeGroup(['a']) 

sage: F1 < F2 

Traceback (most recent call last): 

... 

ValueError: libGAP: cannot compare less than: Error, no method found! 

Error, no 1st choice method found for `<' on 2 arguments 

  

sage: F1._set_compare_by_id() 

sage: F1 != F2 

Traceback (most recent call last): 

... 

ValueError: comparison style must be the same for both operands 

  

sage: F1._set_compare_by_id() 

sage: F2._set_compare_by_id() 

sage: F1 != F2 

True 

""" 

self._compare_by_id = True 

  

cpdef _assert_compare_by_id(self): 

""" 

Ensure that comparison is by ``id`` 

  

See :meth:`_set_compare_by_id`. 

  

OUTPUT: 

  

This method returns nothing. A ``ValueError`` is raised if 

:meth:`_set_compare_by_id` has not been called on this libgap 

object. 

  

EXAMPLES:: 

  

sage: x = libgap.FreeGroup(1) 

sage: x._assert_compare_by_id() 

Traceback (most recent call last): 

... 

ValueError: requires a libGAP objects whose comparison is by "id" 

  

sage: x._set_compare_by_id() 

sage: x._assert_compare_by_id() 

""" 

if not self._compare_by_id: 

raise ValueError('requires a libGAP objects whose comparison is by "id"') 

  

def __hash__(self): 

""" 

Make hashable. 

  

EXAMPLES:: 

  

sage: hash(libgap(123)) # random output 

163512108404620371 

""" 

return hash(str(self)) 

  

cpdef _richcmp_(self, other, int op): 

""" 

Compare ``self`` with ``other``. 

  

Uses the GAP comparison by default, or the Python ``id`` if 

:meth:`_set_compare_by_id` was called. 

  

OUTPUT: 

  

Boolean, depending on the comparison of ``self`` and 

``other``. Raises a ``ValueError`` if GAP does not support 

comparison of ``self`` and ``other``, unless 

:meth:`_set_compare_by_id` was called on both ``self`` and 

``other``. 

  

EXAMPLES:: 

  

sage: a = libgap(123) 

sage: a == a 

True 

sage: b = libgap('string') 

sage: a._richcmp_(b, 0) 

1 

sage: (a < b) or (a > b) 

True 

sage: a._richcmp_(libgap(123), 2) 

True 

  

GAP does not have a comparison function for two ``FreeGroup`` 

objects. LibGAP signals this by raising a ``ValueError`` :: 

  

sage: F1 = libgap.FreeGroup(['a']) 

sage: F2 = libgap.FreeGroup(['a']) 

sage: F1 < F2 

Traceback (most recent call last): 

... 

ValueError: libGAP: cannot compare less than: Error, no method found! 

Error, no 1st choice method found for `<' on 2 arguments 

  

sage: F1._set_compare_by_id() 

sage: F1 < F2 

Traceback (most recent call last): 

... 

ValueError: comparison style must be the same for both operands 

  

sage: F1._set_compare_by_id() 

sage: F2._set_compare_by_id() 

sage: F1 < F2 or F1 > F2 

True 

""" 

if self._compare_by_id != (<GapElement>other)._compare_by_id: 

raise ValueError('comparison style must be the same for both operands') 

if op==Py_LT: 

return self._compare_less(other) 

elif op==Py_LE: 

return self._compare_equal(other) or self._compare_less(other) 

elif op == Py_EQ: 

return self._compare_equal(other) 

elif op == Py_GT: 

return not self._compare_less(other) 

elif op == Py_GE: 

return self._compare_equal(other) or not self._compare_less(other) 

elif op == Py_NE: 

return not self._compare_equal(other) 

else: 

assert False # unreachable 

  

cdef bint _compare_equal(self, Element other) except -2: 

""" 

Compare ``self`` with ``other``. 

  

Helper for :meth:`_richcmp_` 

  

EXAMPLES:: 

  

sage: libgap(1) == libgap(1) # indirect doctest 

True 

""" 

if self._compare_by_id: 

return id(self) == id(other) 

cdef GapElement c_other = <GapElement>other 

cdef bint result 

libgap_enter() 

try: 

sig_on() 

result = libGAP_EQ(self.value, c_other.value) 

sig_off() 

except RuntimeError as msg: 

raise ValueError('libGAP: cannot compare equality: '+str(msg)) 

finally: 

libgap_exit() 

return result 

  

cdef bint _compare_less(self, Element other) except -2: 

""" 

Compare ``self`` with ``other``. 

  

Helper for :meth:`_richcmp_` 

  

EXAMPLES:: 

  

sage: libgap(1) < libgap(2) # indirect doctest 

True 

""" 

if self._compare_by_id: 

return id(self) < id(other) 

cdef bint result 

cdef GapElement c_other = <GapElement>other 

libgap_enter() 

try: 

sig_on() 

result = libGAP_LT(self.value, c_other.value) 

sig_off() 

except RuntimeError as msg: 

raise ValueError('libGAP: cannot compare less than: '+str(msg)) 

finally: 

libgap_exit() 

return result 

  

cpdef _add_(self, right): 

r""" 

Add two GapElement objects. 

  

EXAMPLES:: 

  

sage: g1 = libgap(1) 

sage: g2 = libgap(2) 

sage: g1._add_(g2) 

3 

sage: g1 + g2 # indirect doctest 

3 

  

sage: libgap(1) + libgap.CyclicGroup(2) 

Traceback (most recent call last): 

... 

ValueError: libGAP: Error, no method found! 

Error, no 1st choice method found for `+' on 2 arguments 

""" 

cdef libGAP_Obj result 

try: 

libgap_enter() 

sig_on() 

result = libGAP_SUM(self.value, (<GapElement>right).value) 

sig_off() 

except RuntimeError as msg: 

libGAP_ClearError() 

raise ValueError('libGAP: '+str(msg)) 

finally: 

libgap_exit() 

return make_any_gap_element(self.parent(), result) 

  

  

cpdef _sub_(self, right): 

r""" 

Subtract two GapElement objects. 

  

EXAMPLES:: 

  

sage: g1 = libgap(1) 

sage: g2 = libgap(2) 

sage: g1._sub_(g2) 

-1 

sage: g1 - g2 # indirect doctest 

-1 

  

sage: libgap(1) - libgap.CyclicGroup(2) 

Traceback (most recent call last): 

... 

ValueError: libGAP: Error, no method found! 

Error, no 1st choice method found for `-' on 2 arguments 

""" 

cdef libGAP_Obj result 

try: 

libgap_enter() 

sig_on() 

result = libGAP_DIFF(self.value, (<GapElement>right).value) 

sig_off() 

except RuntimeError as msg: 

libGAP_ClearError() 

raise ValueError('libGAP: {}'.format(msg)) 

finally: 

libgap_exit() 

return make_any_gap_element(self.parent(), result) 

  

  

cpdef _mul_(self, right): 

r""" 

Multiply two GapElement objects. 

  

EXAMPLES:: 

  

sage: g1 = libgap(3) 

sage: g2 = libgap(5) 

sage: g1._mul_(g2) 

15 

sage: g1 * g2 # indirect doctest 

15 

  

sage: libgap(1) * libgap.CyclicGroup(2) 

Traceback (most recent call last): 

... 

ValueError: libGAP: Error, no method found! 

Error, no 1st choice method found for `*' on 2 arguments 

""" 

cdef libGAP_Obj result 

try: 

libgap_enter() 

sig_on() 

result = libGAP_PROD(self.value, (<GapElement>right).value) 

sig_off() 

except RuntimeError as msg: 

libGAP_ClearError() 

raise ValueError('libGAP: {}'.format(msg)) 

finally: 

libgap_exit() 

return make_any_gap_element(self.parent(), result) 

  

  

cpdef _div_(self, right): 

r""" 

Divide two GapElement objects. 

  

EXAMPLES:: 

  

sage: g1 = libgap(3) 

sage: g2 = libgap(5) 

sage: g1._div_(g2) 

3/5 

sage: g1 / g2 # indirect doctest 

3/5 

  

sage: libgap(1) / libgap.CyclicGroup(2) 

Traceback (most recent call last): 

... 

ValueError: libGAP: Error, no method found! 

Error, no 1st choice method found for `/' on 2 arguments 

  

sage: libgap(1) / libgap(0) 

Traceback (most recent call last): 

... 

ValueError: libGAP: Error, Rational operations: <divisor> must not be zero 

""" 

cdef libGAP_Obj result 

try: 

libgap_enter() 

sig_on() 

result = libGAP_QUO(self.value, (<GapElement>right).value) 

sig_off() 

except RuntimeError as msg: 

libGAP_ClearError() 

raise ValueError('libGAP: '+str(msg)) 

finally: 

libgap_exit() 

return make_any_gap_element(self.parent(), result) 

  

cpdef _mod_(self, right): 

r""" 

Modulus of two GapElement objects. 

  

EXAMPLES:: 

  

sage: g1 = libgap(5) 

sage: g2 = libgap(2) 

sage: g1 % g2 

1 

  

sage: libgap(1) % libgap.CyclicGroup(2) 

Traceback (most recent call last): 

... 

ValueError: libGAP: Error, no method found! 

Error, no 1st choice method found for `mod' on 2 arguments 

""" 

cdef libGAP_Obj result 

try: 

libgap_enter() 

sig_on() 

result = libGAP_MOD(self.value, (<GapElement>right).value) 

sig_off() 

except RuntimeError as msg: 

libGAP_ClearError() 

raise ValueError('libGAP: '+str(msg)) 

finally: 

libgap_exit() 

return make_any_gap_element(self.parent(), result) 

  

  

def __pow__(GapElement self, right, dummy): 

r""" 

Exponentiation of two GapElement objects. 

  

EXAMPLES:: 

  

sage: g1 = libgap(5) 

sage: g2 = libgap(2) 

sage: g1 ^ g2 

25 

  

sage: libgap.CyclicGroup(2) ^ 2 

Traceback (most recent call last): 

... 

ValueError: libGAP: Error, no method found! 

Error, no 1st choice method found for `^' on 2 arguments 

  

sage: libgap(3) ^ Infinity 

Traceback (most recent call last): 

... 

ValueError: libGAP: Error, no method found! Error, no 1st choice 

method found for `InverseMutable' on 1 arguments 

""" 

if not isinstance(right, GapElement): 

libgap = self.parent() 

right = libgap(right) 

cdef libGAP_Obj result 

try: 

libgap_enter() 

sig_on() 

result = libGAP_POW(self.value, (<GapElement>right).value) 

sig_off() 

except RuntimeError as msg: 

libGAP_ClearError() 

raise ValueError('libGAP: ' + str(msg)) 

finally: 

libgap_exit() 

return make_any_gap_element(self.parent(), result) 

  

  

def is_function(self): 

""" 

Return whether the wrapped GAP object is a function. 

  

OUTPUT: 

  

Boolean. 

  

EXAMPLES:: 

  

sage: a = libgap.eval("NormalSubgroups") 

sage: a.is_function() 

True 

sage: a = libgap(2/3) 

sage: a.is_function() 

False 

""" 

return libGAP_IS_FUNC(self.value) 

  

  

def is_list(self): 

r""" 

Return whether the wrapped GAP object is a GAP List. 

  

OUTPUT: 

  

Boolean. 

  

EXAMPLES:: 

  

sage: libgap.eval('[1, 2,,,, 5]').is_list() 

True 

sage: libgap.eval('3/2').is_list() 

False 

""" 

return libGAP_IS_LIST(self.value) 

  

  

def is_record(self): 

r""" 

Return whether the wrapped GAP object is a GAP record. 

  

OUTPUT: 

  

Boolean. 

  

EXAMPLES:: 

  

sage: libgap.eval('[1, 2,,,, 5]').is_record() 

False 

sage: libgap.eval('rec(a:=1, b:=3)').is_record() 

True 

""" 

return libGAP_IS_REC(self.value) 

  

  

cpdef is_bool(self): 

r""" 

Return whether the wrapped GAP object is a GAP boolean. 

  

OUTPUT: 

  

Boolean. 

  

EXAMPLES:: 

  

sage: libgap(True).is_bool() 

True 

""" 

libgap = self.parent() 

cdef GapElement r_sage = libgap.IsBool(self) 

cdef libGAP_Obj r_gap = r_sage.value 

return r_gap == libGAP_True 

  

  

def is_string(self): 

r""" 

Return whether the wrapped GAP object is a GAP string. 

  

OUTPUT: 

  

Boolean. 

  

EXAMPLES:: 

  

sage: libgap('this is a string').is_string() 

True 

""" 

return libGAP_IS_STRING(self.value) 

  

  

def is_permutation(self): 

r""" 

Return whether the wrapped GAP object is a GAP permutation. 

  

OUTPUT: 

  

Boolean. 

  

EXAMPLES:: 

  

sage: perm = libgap.PermList( libgap([1,5,2,3,4]) ); perm 

(2,5,4,3) 

sage: perm.is_permutation() 

True 

sage: libgap('this is a string').is_permutation() 

False 

""" 

return (libGAP_TNUM_OBJ(self.value) == libGAP_T_PERM2 or 

libGAP_TNUM_OBJ(self.value) == libGAP_T_PERM4) 

  

  

def sage(self): 

r""" 

Return the Sage equivalent of the :class:`GapElement` 

  

EXAMPLES:: 

  

sage: libgap(1).sage() 

1 

sage: type(_) 

<type 'sage.rings.integer.Integer'> 

  

sage: libgap(3/7).sage() 

3/7 

sage: type(_) 

<type 'sage.rings.rational.Rational'> 

  

sage: libgap.eval('5 + 7*E(3)').sage() 

7*zeta3 + 5 

  

sage: libgap(Infinity).sage() 

+Infinity 

sage: libgap(-Infinity).sage() 

-Infinity 

  

sage: libgap(True).sage() 

True 

sage: libgap(False).sage() 

False 

sage: type(_) 

<... 'bool'> 

  

sage: libgap('this is a string').sage() 

'this is a string' 

sage: type(_) 

<... 'str'> 

""" 

if self.value is NULL: 

return None 

libgap = self.parent() 

  

if self.IsInfinity(): 

from sage.rings.infinity import Infinity 

return Infinity 

elif self.IsNegInfinity(): 

from sage.rings.infinity import Infinity 

return -Infinity 

  

raise NotImplementedError('cannot construct equivalent Sage object') 

  

  

  

  

  

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

### GapElement_Integer ##################################################### 

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

  

cdef GapElement_Integer make_GapElement_Integer(parent, libGAP_Obj obj): 

r""" 

Turn a Gap integer object into a GapElement_Integer Sage object 

  

EXAMPLES:: 

  

sage: libgap(123) 

123 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_Integer'> 

""" 

cdef GapElement_Integer r = GapElement_Integer.__new__(GapElement_Integer) 

r._initialize(parent, obj) 

return r 

  

  

cdef class GapElement_Integer(GapElement): 

r""" 

Derived class of GapElement for GAP integers. 

  

EXAMPLES:: 

  

sage: i = libgap(123) 

sage: type(i) 

<type 'sage.libs.gap.element.GapElement_Integer'> 

sage: ZZ(i) 

123 

""" 

  

def is_C_int(self): 

r""" 

Return whether the wrapped GAP object is a immediate GAP integer. 

  

An immediate integer is one that is stored as a C integer, and 

is subject to the usual size limits. Larger integers are 

stored in GAP as GMP integers. 

  

OUTPUT: 

  

Boolean. 

  

EXAMPLES:: 

  

sage: n = libgap(1) 

sage: type(n) 

<type 'sage.libs.gap.element.GapElement_Integer'> 

sage: n.is_C_int() 

True 

sage: n.IsInt() 

true 

  

sage: N = libgap(2^130) 

sage: type(N) 

<type 'sage.libs.gap.element.GapElement_Integer'> 

sage: N.is_C_int() 

False 

sage: N.IsInt() 

true 

""" 

return libGAP_IS_INTOBJ(self.value) 

  

def _rational_(self): 

r""" 

EXAMPLES:: 

  

sage: QQ(libgap(1)) # indirect doctest 

1 

sage: QQ(libgap(-2**200)) == -2**200 

True 

""" 

return self.sage(ring=QQ) 

  

def sage(self, ring=None): 

r""" 

Return the Sage equivalent of the :class:`GapElement_Integer` 

  

- ``ring`` -- Integer ring or ``None`` (default). If not 

specified, a the default Sage integer ring is used. 

  

OUTPUT: 

  

A Sage integer 

  

EXAMPLES:: 

  

sage: libgap([ 1, 3, 4 ]).sage() 

[1, 3, 4] 

sage: all( x in ZZ for x in _ ) 

True 

  

sage: libgap(132).sage(ring=IntegerModRing(13)) 

2 

sage: parent(_) 

Ring of integers modulo 13 

  

TESTS:: 

  

sage: large = libgap.eval('2^130'); large 

1361129467683753853853498429727072845824 

sage: large.sage() 

1361129467683753853853498429727072845824 

  

sage: huge = libgap.eval('10^9999'); huge # gap abbreviates very long ints 

<integer 100...000 (10000 digits)> 

sage: huge.sage().ndigits() 

10000 

""" 

if ring is None: 

ring = ZZ 

if self.is_C_int(): 

return ring(libGAP_INT_INTOBJ(self.value)) 

else: 

# TODO: waste of time! 

# gap integers are stored as a mp_limb_t and we have a much more direct 

# conversion implemented in mpz_get_pylong(mpz_srcptr z) 

# (see sage.libs.gmp.pylong) 

string = self.String().sage() 

return ring(string) 

  

_integer_ = sage 

  

def __int__(self): 

r""" 

TESTS:: 

  

sage: int(libgap(3)) 

3 

sage: type(_) 

<... 'int'> 

  

sage: int(libgap(2)**128) 

340282366920938463463374607431768211456L 

sage: type(_) 

<type 'long'> 

""" 

return self.sage(ring=int) 

  

def __index__(self): 

r""" 

TESTS: 

  

Check that gap integers can be used as indices (:trac:`23878`):: 

  

sage: s = 'abcd' 

sage: s[libgap(1)] 

'b' 

""" 

return int(self) 

  

  

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

### GapElement_Float ##################################################### 

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

  

cdef GapElement_Float make_GapElement_Float(parent, libGAP_Obj obj): 

r""" 

Turn a Gap macfloat object into a GapElement_Float Sage object 

  

EXAMPLES:: 

  

sage: libgap(123.5) 

123.5 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_Float'> 

""" 

cdef GapElement_Float r = GapElement_Float.__new__(GapElement_Float) 

r._initialize(parent, obj) 

return r 

  

cdef class GapElement_Float(GapElement): 

r""" 

Derived class of GapElement for GAP floating point numbers. 

  

EXAMPLES:: 

  

sage: i = libgap(123.5) 

sage: type(i) 

<type 'sage.libs.gap.element.GapElement_Float'> 

sage: RDF(i) 

123.5 

sage: float(i) 

123.5 

  

TESTS:: 

  

sage: a = RDF.random_element() 

sage: libgap(a).sage() == a 

True 

""" 

def sage(self, ring=None): 

r""" 

Return the Sage equivalent of the :class:`GapElement_Float` 

  

- ``ring`` -- a floating point field or ``None`` (default). If not 

specified, the default Sage ``RDF`` is used. 

  

OUTPUT: 

  

A Sage double precision floating point number 

  

EXAMPLES:: 

  

sage: a = libgap.eval("Float(3.25)").sage() 

sage: a 

3.25 

sage: parent(a) 

Real Double Field 

""" 

if ring is None: 

ring = RDF 

return ring(libGAP_VAL_MACFLOAT(self.value)) 

  

def __float__(self): 

r""" 

TESTS:: 

  

sage: float(libgap.eval("Float(3.5)")) 

3.5 

""" 

return libGAP_VAL_MACFLOAT(self.value) 

  

  

  

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

### GapElement_IntegerMod ##################################################### 

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

  

cdef GapElement_IntegerMod make_GapElement_IntegerMod(parent, libGAP_Obj obj): 

r""" 

Turn a Gap integer object into a :class:`GapElement_IntegerMod` Sage object 

  

EXAMPLES:: 

  

sage: n = IntegerModRing(123)(13) 

sage: libgap(n) 

ZmodnZObj( 13, 123 ) 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_IntegerMod'> 

""" 

cdef GapElement_IntegerMod r = GapElement_IntegerMod.__new__(GapElement_IntegerMod) 

r._initialize(parent, obj) 

return r 

  

cdef class GapElement_IntegerMod(GapElement): 

r""" 

Derived class of GapElement for GAP integers modulo an integer. 

  

EXAMPLES:: 

  

sage: n = IntegerModRing(123)(13) 

sage: i = libgap(n) 

sage: type(i) 

<type 'sage.libs.gap.element.GapElement_IntegerMod'> 

""" 

  

cpdef GapElement_Integer lift(self): 

""" 

Return an integer lift. 

  

OUTPUT: 

  

A :class:`GapElement_Integer` that equals ``self`` in the 

integer mod ring. 

  

EXAMPLES:: 

  

sage: n = libgap.eval('One(ZmodnZ(123)) * 13') 

sage: n.lift() 

13 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_Integer'> 

""" 

return self.Int() 

  

  

def sage(self, ring=None): 

r""" 

Return the Sage equivalent of the :class:`GapElement_IntegerMod` 

  

INPUT: 

  

- ``ring`` -- Sage integer mod ring or ``None`` (default). If 

not specified, a suitable integer mod ringa is used 

automatically. 

  

OUTPUT: 

  

A Sage integer modulo another integer. 

  

EXAMPLES:: 

  

sage: n = libgap.eval('One(ZmodnZ(123)) * 13') 

sage: n.sage() 

13 

sage: parent(_) 

Ring of integers modulo 123 

""" 

if ring is None: 

# ring = self.DefaultRing().sage() 

characteristic = self.Characteristic().sage() 

ring = ZZ.quotient_ring(characteristic) 

return self.lift().sage(ring=ring) 

  

  

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

### GapElement_FiniteField ##################################################### 

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

  

cdef GapElement_FiniteField make_GapElement_FiniteField(parent, libGAP_Obj obj): 

r""" 

Turn a GAP finite field object into a :class:`GapElement_FiniteField` Sage object 

  

EXAMPLES:: 

  

sage: libgap.eval('Z(5)^2') 

Z(5)^2 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_FiniteField'> 

""" 

cdef GapElement_FiniteField r = GapElement_FiniteField.__new__(GapElement_FiniteField) 

r._initialize(parent, obj) 

return r 

  

  

cdef class GapElement_FiniteField(GapElement): 

r""" 

Derived class of GapElement for GAP finite field elements. 

  

EXAMPLES:: 

  

sage: libgap.eval('Z(5)^2') 

Z(5)^2 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_FiniteField'> 

""" 

  

cpdef GapElement_Integer lift(self): 

""" 

Return an integer lift. 

  

OUTPUT: 

  

The smallest positive :class:`GapElement_Integer` that equals 

``self`` in the prime finite field. 

  

EXAMPLES:: 

  

sage: n = libgap.eval('Z(5)^2') 

sage: n.lift() 

4 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_Integer'> 

  

sage: n = libgap.eval('Z(25)') 

sage: n.lift() 

Traceback (most recent call last): 

TypeError: not in prime subfield 

""" 

degree = self.DegreeFFE().sage() 

if degree == 1: 

return self.IntFFE() 

else: 

raise TypeError('not in prime subfield') 

  

  

def sage(self, ring=None, var='a'): 

r""" 

Return the Sage equivalent of the :class:`GapElement_FiniteField`. 

  

INPUT: 

  

- ``ring`` -- a Sage finite field or ``None`` (default). The 

field to return ``self`` in. If not specified, a suitable 

finite field will be constructed. 

  

OUTPUT: 

  

An Sage finite field element. The isomorphism is chosen such 

that the Gap ``PrimitiveRoot()`` maps to the Sage 

:meth:`~sage.rings.finite_rings.finite_field_prime_modn.multiplicative_generator`. 

  

EXAMPLES:: 

  

sage: n = libgap.eval('Z(25)^2') 

sage: n.sage() 

a + 3 

sage: parent(_) 

Finite Field in a of size 5^2 

  

sage: n.sage(ring=GF(5)) 

Traceback (most recent call last): 

... 

ValueError: the given ring is incompatible ... 

  

TESTS:: 

  

sage: n = libgap.eval('Z(2^4)^2 + Z(2^4)^1 + Z(2^4)^0') 

sage: n 

Z(2^2)^2 

sage: n.sage() 

a + 1 

sage: parent(_) 

Finite Field in a of size 2^2 

sage: n.sage(ring=ZZ) 

Traceback (most recent call last): 

... 

ValueError: the given ring is incompatible ... 

sage: n.sage(ring=CC) 

Traceback (most recent call last): 

... 

ValueError: the given ring is incompatible ... 

sage: n.sage(ring=GF(5)) 

Traceback (most recent call last): 

... 

ValueError: the given ring is incompatible ... 

sage: n.sage(ring=GF(2^3)) 

Traceback (most recent call last): 

... 

ValueError: the given ring is incompatible ... 

sage: n.sage(ring=GF(2^2, 'a')) 

a + 1 

sage: n.sage(ring=GF(2^4, 'a')) 

a^2 + a + 1 

sage: n.sage(ring=GF(2^8, 'a')) 

a^7 + a^6 + a^4 + a^2 + a + 1 

  

Check that :trac:`23153` is fixed:: 

  

sage: n = libgap.eval('Z(2^4)^2 + Z(2^4)^1 + Z(2^4)^0') 

sage: n.sage(ring=GF(2^4, 'a')) 

a^2 + a + 1 

""" 

deg = self.DegreeFFE().sage() 

char = self.Characteristic().sage() 

if ring is None: 

from sage.rings.finite_rings.finite_field_constructor import GF 

ring = GF(char**deg, name=var) 

elif not (ring.is_field() and ring.is_finite() and \ 

ring.characteristic() == char and ring.degree() % deg == 0): 

raise ValueError(('the given ring is incompatible (must be a ' 

'finite field of characteristic {} and degree ' 

'divisible by {})').format(char, deg)) 

  

if self.IsOne(): 

return ring.one() 

if deg == 1 and char == ring.characteristic(): 

return ring(self.lift().sage()) 

else: 

gap_field = make_GapElement_Ring(self.parent(), gap_eval(ring._gap_init_())) 

exp = self.LogFFE(gap_field.PrimitiveRoot()) 

return ring.multiplicative_generator() ** exp.sage() 

  

def __int__(self): 

r""" 

TESTS:: 

  

sage: int(libgap.eval("Z(53)")) 

2 

""" 

return int(self.Int()) 

  

def _integer_(self, R): 

r""" 

TESTS:: 

  

sage: ZZ(libgap.eval("Z(53)")) 

2 

""" 

return R(self.Int()) 

  

  

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

### GapElement_Cyclotomic ##################################################### 

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

  

cdef GapElement_Cyclotomic make_GapElement_Cyclotomic(parent, libGAP_Obj obj): 

r""" 

Turn a Gap cyclotomic object into a :class:`GapElement_Cyclotomic` Sage 

object. 

  

EXAMPLES:: 

  

sage: libgap.eval('E(3)') 

E(3) 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_Cyclotomic'> 

""" 

cdef GapElement_Cyclotomic r = GapElement_Cyclotomic.__new__(GapElement_Cyclotomic) 

r._initialize(parent, obj) 

return r 

  

  

cdef class GapElement_Cyclotomic(GapElement): 

r""" 

Derived class of GapElement for GAP universal cyclotomics. 

  

EXAMPLES:: 

  

sage: libgap.eval('E(3)') 

E(3) 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_Cyclotomic'> 

""" 

  

def sage(self, ring=None): 

r""" 

Return the Sage equivalent of the :class:`GapElement_Cyclotomic`. 

  

INPUT: 

  

- ``ring`` -- a Sage cyclotomic field or ``None`` 

(default). If not specified, a suitable minimal cyclotomic 

field will be constructed. 

  

OUTPUT: 

  

A Sage cyclotomic field element. 

  

EXAMPLES:: 

  

sage: n = libgap.eval('E(3)') 

sage: n.sage() 

zeta3 

sage: parent(_) 

Cyclotomic Field of order 3 and degree 2 

  

sage: n.sage(ring=CyclotomicField(6)) 

zeta6 - 1 

  

sage: libgap.E(3).sage(ring=CyclotomicField(3)) 

zeta3 

sage: libgap.E(3).sage(ring=CyclotomicField(6)) 

zeta6 - 1 

  

TESTS: 

  

Check that :trac:`15204` is fixed:: 

  

sage: libgap.E(3).sage(ring=UniversalCyclotomicField()) 

E(3) 

sage: libgap.E(3).sage(ring=CC) 

-0.500000000000000 + 0.866025403784439*I 

""" 

if ring is None: 

conductor = self.Conductor() 

from sage.rings.number_field.number_field import CyclotomicField 

ring = CyclotomicField(conductor.sage()) 

else: 

try: 

conductor = ring._n() 

except AttributeError: 

from sage.rings.number_field.number_field import CyclotomicField 

conductor = self.Conductor() 

cf = CyclotomicField(conductor.sage()) 

return ring(cf(self.CoeffsCyc(conductor).sage())) 

coeff = self.CoeffsCyc(conductor).sage() 

return ring(coeff) 

  

  

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

### GapElement_Rational #################################################### 

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

  

cdef GapElement_Rational make_GapElement_Rational(parent, libGAP_Obj obj): 

r""" 

Turn a Gap Rational number (of type ``Obj``) into a Cython ``GapElement_Rational``. 

  

EXAMPLES:: 

  

sage: libgap(123/456) 

41/152 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_Rational'> 

""" 

cdef GapElement_Rational r = GapElement_Rational.__new__(GapElement_Rational) 

r._initialize(parent, obj) 

return r 

  

  

cdef class GapElement_Rational(GapElement): 

r""" 

Derived class of GapElement for GAP rational numbers. 

  

EXAMPLES:: 

  

sage: r = libgap(123/456) 

sage: type(r) 

<type 'sage.libs.gap.element.GapElement_Rational'> 

""" 

def _rational_(self): 

r""" 

EXAMPLES:: 

  

sage: r = libgap(-1/3) 

sage: QQ(r) # indirect doctest 

-1/3 

sage: QQ(libgap(2**300 / 3**300)) == 2**300 / 3**300 

True 

""" 

return self.sage(ring=QQ) 

  

def sage(self, ring=None): 

r""" 

Return the Sage equivalent of the :class:`GapElement`. 

  

INPUT: 

  

- ``ring`` -- the Sage rational ring or ``None`` (default). If 

not specified, the rational ring is used automatically. 

  

OUTPUT: 

  

A Sage rational number. 

  

EXAMPLES:: 

  

sage: r = libgap(123/456); r 

41/152 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_Rational'> 

sage: r.sage() 

41/152 

sage: type(_) 

<type 'sage.rings.rational.Rational'> 

""" 

if ring is None: 

ring = ZZ 

libgap = self.parent() 

return libgap.NumeratorRat(self).sage(ring=ring) / libgap.DenominatorRat(self).sage(ring=ring) 

  

  

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

### GapElement_Ring ##################################################### 

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

  

cdef GapElement_Ring make_GapElement_Ring(parent, libGAP_Obj obj): 

r""" 

Turn a Gap integer object into a :class:`GapElement_Ring` Sage 

object. 

  

EXAMPLES:: 

  

sage: libgap(GF(5)) 

GF(5) 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_Ring'> 

""" 

cdef GapElement_Ring r = GapElement_Ring.__new__(GapElement_Ring) 

r._initialize(parent, obj) 

return r 

  

  

cdef class GapElement_Ring(GapElement): 

r""" 

Derived class of GapElement for GAP rings (parents of ring elements). 

  

EXAMPLES:: 

  

sage: i = libgap(ZZ) 

sage: type(i) 

<type 'sage.libs.gap.element.GapElement_Ring'> 

""" 

  

def ring_integer(self): 

""" 

Construct the Sage integers. 

  

EXAMPLES:: 

  

sage: libgap.eval('Integers').ring_integer() 

Integer Ring 

""" 

return ZZ 

  

  

def ring_rational(self): 

""" 

Construct the Sage rationals. 

  

EXAMPLES:: 

  

sage: libgap.eval('Rationals').ring_rational() 

Rational Field 

""" 

return ZZ.fraction_field() 

  

  

def ring_integer_mod(self): 

""" 

Construct a Sage integer mod ring. 

  

EXAMPLES:: 

  

sage: libgap.eval('ZmodnZ(15)').ring_integer_mod() 

Ring of integers modulo 15 

""" 

characteristic = self.Characteristic().sage() 

return ZZ.quotient_ring(characteristic) 

  

  

def ring_finite_field(self, var='a'): 

""" 

Construct an integer ring. 

  

EXAMPLES:: 

  

sage: libgap.GF(3,2).ring_finite_field(var='A') 

Finite Field in A of size 3^2 

""" 

size = self.Size().sage() 

from sage.rings.finite_rings.finite_field_constructor import GF 

return GF(size, name=var) 

  

  

def ring_cyclotomic(self): 

""" 

Construct an integer ring. 

  

EXAMPLES:: 

  

sage: libgap.CyclotomicField(6).ring_cyclotomic() 

Cyclotomic Field of order 3 and degree 2 

""" 

conductor = self.Conductor() 

from sage.rings.number_field.number_field import CyclotomicField 

return CyclotomicField(conductor.sage()) 

  

  

def sage(self, **kwds): 

r""" 

Return the Sage equivalent of the :class:`GapElement_Ring`. 

  

INPUT: 

  

- ``**kwds`` -- keywords that are passed on to the ``ring_`` 

method. 

  

OUTPUT: 

  

A Sage ring. 

  

EXAMPLES:: 

  

sage: libgap.eval('Integers').sage() 

Integer Ring 

  

sage: libgap.eval('Rationals').sage() 

Rational Field 

  

sage: libgap.eval('ZmodnZ(15)').sage() 

Ring of integers modulo 15 

  

sage: libgap.GF(3,2).sage(var='A') 

Finite Field in A of size 3^2 

  

sage: libgap.CyclotomicField(6).sage() 

Cyclotomic Field of order 3 and degree 2 

""" 

if self.IsField(): 

if self.IsRationals(): 

return self.ring_rational(**kwds) 

if self.IsCyclotomicField(): 

return self.ring_cyclotomic(**kwds) 

if self.IsFinite(): 

return self.ring_finite_field(**kwds) 

else: 

if self.IsIntegers(): 

return self.ring_integer(**kwds) 

if self.IsFinite(): 

return self.ring_integer_mod(**kwds) 

raise NotImplementedError('cannot convert GAP ring to Sage') 

  

  

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

### GapElement_Boolean ##################################################### 

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

  

cdef GapElement_Boolean make_GapElement_Boolean(parent, libGAP_Obj obj): 

r""" 

Turn a Gap Boolean number (of type ``Obj``) into a Cython ``GapElement_Boolean``. 

  

EXAMPLES:: 

  

sage: libgap(True) 

true 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_Boolean'> 

""" 

cdef GapElement_Boolean r = GapElement_Boolean.__new__(GapElement_Boolean) 

r._initialize(parent, obj) 

return r 

  

  

cdef class GapElement_Boolean(GapElement): 

r""" 

Derived class of GapElement for GAP boolean values. 

  

EXAMPLES:: 

  

sage: b = libgap(True) 

sage: type(b) 

<type 'sage.libs.gap.element.GapElement_Boolean'> 

""" 

  

def sage(self): 

r""" 

Return the Sage equivalent of the :class:`GapElement` 

  

OUTPUT: 

  

A Python boolean if the values is either true or false. GAP 

booleans can have the third value ``Fail``, in which case a 

``ValueError`` is raised. 

  

EXAMPLES:: 

  

sage: b = libgap.eval('true'); b 

true 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_Boolean'> 

sage: b.sage() 

True 

sage: type(_) 

<... 'bool'> 

  

sage: libgap.eval('fail') 

fail 

sage: _.sage() 

Traceback (most recent call last): 

... 

ValueError: the GAP boolean value "fail" cannot be represented in Sage 

""" 

if self.value == libGAP_True: return True 

if self.value == libGAP_False: return False 

raise ValueError('the GAP boolean value "fail" cannot be represented in Sage') 

  

  

def __nonzero__(self): 

""" 

Check that the boolean is "true". 

  

This is syntactic sugar for using libgap. See the examples below. 

  

OUTPUT: 

  

Boolean. 

  

EXAMPLES:: 

  

sage: gap_bool = [libgap.eval('true'), libgap.eval('false'), libgap.eval('fail')] 

sage: for x in gap_bool: 

....: if x: # this calls __nonzero__ 

....: print("{} {}".format(x, type(x))) 

true <type 'sage.libs.gap.element.GapElement_Boolean'> 

  

sage: for x in gap_bool: 

....: if not x: # this calls __nonzero__ 

....: print("{} {}".format( x, type(x))) 

false <type 'sage.libs.gap.element.GapElement_Boolean'> 

fail <type 'sage.libs.gap.element.GapElement_Boolean'> 

""" 

return self.value == libGAP_True 

  

  

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

### GapElement_String #################################################### 

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

  

cdef GapElement_String make_GapElement_String(parent, libGAP_Obj obj): 

r""" 

Turn a Gap String (of type ``Obj``) into a Cython ``GapElement_String``. 

  

EXAMPLES:: 

  

sage: libgap('this is a string') 

"this is a string" 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_String'> 

""" 

cdef GapElement_String r = GapElement_String.__new__(GapElement_String) 

r._initialize(parent, obj) 

return r 

  

  

cdef class GapElement_String(GapElement): 

r""" 

Derived class of GapElement for GAP strings. 

  

EXAMPLES:: 

  

sage: s = libgap('string') 

sage: type(s) 

<type 'sage.libs.gap.element.GapElement_String'> 

sage: s 

"string" 

sage: print(s) 

string 

""" 

def __str__(self): 

r""" 

Convert this :class:`GapElement_String` to a Python string. 

  

OUTPUT: 

  

A Python string. 

  

EXAMPLES:: 

  

sage: s = libgap.eval(' "string" '); s 

"string" 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_String'> 

sage: str(s) 

'string' 

sage: s.sage() 

'string' 

sage: type(_) 

<... 'str'> 

""" 

libgap_enter() 

s = libGAP_CSTR_STRING(self.value) 

libgap_exit() 

return s 

  

sage = __str__ 

  

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

### GapElement_Function #################################################### 

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

  

cdef GapElement_Function make_GapElement_Function(parent, libGAP_Obj obj): 

r""" 

Turn a Gap C function object (of type ``Obj``) into a Cython ``GapElement_Function``. 

  

INPUT: 

  

- ``parent`` -- the parent of the new :class:`GapElement` 

  

- ``obj`` -- a GAP function object. 

  

OUTPUT: 

  

A :class:`GapElement_Function` instance. 

  

EXAMPLES:: 

  

sage: libgap.CycleLength 

<Gap function "CycleLength"> 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_Function'> 

""" 

cdef GapElement_Function r = GapElement_Function.__new__(GapElement_Function) 

r._initialize(parent, obj) 

return r 

  

  

cdef class GapElement_Function(GapElement): 

r""" 

Derived class of GapElement for GAP functions. 

  

EXAMPLES:: 

  

sage: f = libgap.Cycles 

sage: type(f) 

<type 'sage.libs.gap.element.GapElement_Function'> 

""" 

  

  

def __repr__(self): 

r""" 

Return a string representation 

  

OUTPUT: 

  

String. 

  

EXAMPLES:: 

  

sage: libgap.Orbits 

<Gap function "Orbits"> 

""" 

libgap = self.parent() 

name = libgap.NameFunction(self) 

s = '<Gap function "'+name.sage()+'">' 

return s 

  

  

def __call__(self, *args): 

""" 

Call syntax for functions. 

  

INPUT: 

  

- ``*args`` -- arguments. Will be converted to `GapElement` if 

they are not already of this type. 

  

OUTPUT: 

  

A :class:`GapElement` encapsulating the functions return 

value, or ``None`` if it does not return anything. 

  

EXAMPLES:: 

  

sage: a = libgap.NormalSubgroups 

sage: b = libgap.SymmetricGroup(4) 

sage: libgap.collect() 

sage: a 

<Gap function "NormalSubgroups"> 

sage: b 

Sym( [ 1 .. 4 ] ) 

sage: a(b) 

[ Group(()), 

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

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

Sym( [ 1 .. 4 ] ) ] 

  

sage: libgap.eval("a := NormalSubgroups") 

<Gap function "NormalSubgroups"> 

sage: libgap.eval("b := SymmetricGroup(4)") 

Sym( [ 1 .. 4 ] ) 

sage: libgap.collect() 

sage: libgap.eval('a') (libgap.eval('b')) 

[ Group(()), 

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

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

Sym( [ 1 .. 4 ] ) ] 

sage: a = libgap.eval('a') 

sage: b = libgap.eval('b') 

sage: libgap.collect() 

sage: a(b) 

[ Group(()), 

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

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

Sym( [ 1 .. 4 ] ) ] 

  

Not every ``GapElement`` is callable:: 

  

sage: f = libgap(3) 

sage: f() 

Traceback (most recent call last): 

... 

TypeError: 'sage.libs.gap.element.GapElement_Integer' object is not callable 

  

We illustrate appending to a list which returns None:: 

  

sage: a = libgap([]); a 

[ ] 

sage: a.Add(5); a 

[ 5 ] 

sage: a.Add(10); a 

[ 5, 10 ] 

  

TESTS:: 

  

sage: s = libgap.Sum 

sage: s(libgap([1,2])) 

3 

sage: s(libgap(1), libgap(2)) 

Traceback (most recent call last): 

... 

ValueError: libGAP: Error, no method found! 

Error, no 1st choice method found for `SumOp' on 2 arguments 

  

sage: for i in range(0,100): 

....: rnd = [ randint(-10,10) for i in range(0,randint(0,7)) ] 

....: # compute the sum in GAP 

....: _ = libgap.Sum(rnd) 

....: try: 

....: libgap.Sum(*rnd) 

....: print('This should have triggered a ValueError') 

....: print('because Sum needs a list as argument') 

....: except ValueError: 

....: pass 

  

sage: libgap_exec = libgap.eval("Exec") 

sage: libgap_exec('echo hello from the shell') 

hello from the shell 

""" 

cdef libGAP_Obj result = NULL 

cdef libGAP_Obj arg_list 

cdef int i, n = len(args) 

  

if n > 0: 

libgap = self.parent() 

a = [x if isinstance(x,GapElement) else libgap(x) for x in args] 

  

try: 

libgap_enter() 

sig_on() 

if n == 0: 

result = libGAP_CALL_0ARGS(self.value) 

elif n == 1: 

result = libGAP_CALL_1ARGS(self.value, 

(<GapElement>a[0]).value) 

elif n == 2: 

result = libGAP_CALL_2ARGS(self.value, 

(<GapElement>a[0]).value, 

(<GapElement>a[1]).value) 

elif n == 3: 

result = libGAP_CALL_3ARGS(self.value, 

(<GapElement>a[0]).value, 

(<GapElement>a[1]).value, 

(<GapElement>a[2]).value) 

elif n == 4: 

result = libGAP_CALL_4ARGS(self.value, 

(<GapElement>a[0]).value, 

(<GapElement>a[1]).value, 

(<GapElement>a[2]).value, 

(<GapElement>a[3]).value) 

elif n == 5: 

result = libGAP_CALL_5ARGS(self.value, 

(<GapElement>a[0]).value, 

(<GapElement>a[1]).value, 

(<GapElement>a[2]).value, 

(<GapElement>a[3]).value, 

(<GapElement>a[4]).value) 

elif n == 6: 

result = libGAP_CALL_6ARGS(self.value, 

(<GapElement>a[0]).value, 

(<GapElement>a[1]).value, 

(<GapElement>a[2]).value, 

(<GapElement>a[3]).value, 

(<GapElement>a[4]).value, 

(<GapElement>a[5]).value) 

elif n >= 7: 

libgap_exit() 

arg_list = make_gap_list(args) 

libgap_enter() 

result = libGAP_CALL_XARGS(self.value, arg_list) 

sig_off() 

except RuntimeError as msg: 

raise ValueError('libGAP: ' + str(msg)) 

finally: 

libgap_exit() 

  

if result == NULL: 

# We called a procedure that does not return anything 

return None 

  

return make_any_gap_element(self.parent(), result) 

  

  

def _instancedoc_(self): 

r""" 

Return the help string 

  

EXAMPLES:: 

  

sage: f = libgap.CyclicGroup 

sage: 'constructs the cyclic group' in f.__doc__ 

True 

  

You would get the full help by typing ``f?`` in the command line. 

""" 

libgap = self.parent() 

from sage.interfaces.gap import gap 

return gap.help(libgap.NameFunction(self).sage(), pager=False) 

  

  

  

  

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

### GapElement_MethodProxy ################################################# 

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

  

cdef GapElement_MethodProxy make_GapElement_MethodProxy(parent, libGAP_Obj function, GapElement base_object): 

r""" 

Turn a Gap C rec object (of type ``Obj``) into a Cython ``GapElement_Record``. 

  

This class implement syntactic sugar so that you can write 

``gapelement.f()`` instead of ``libgap.f(gapelement)`` for any GAP 

function ``f``. 

  

INPUT: 

  

- ``parent`` -- the parent of the new :class:`GapElement` 

  

- ``obj`` -- a GAP function object. 

  

- ``base_object`` -- The first argument to be inserted into the function. 

  

OUTPUT: 

  

A :class:`GapElement_MethodProxy` instance. 

  

EXAMPLES:: 

  

sage: lst = libgap([]) 

sage: type( lst.Add ) 

<type 'sage.libs.gap.element.GapElement_MethodProxy'> 

""" 

cdef GapElement_MethodProxy r = GapElement_MethodProxy.__new__(GapElement_MethodProxy) 

r._initialize(parent, function) 

r.first_argument = base_object 

return r 

  

  

cdef class GapElement_MethodProxy(GapElement_Function): 

r""" 

Helper class returned by ``GapElement.__getattr__``. 

  

Derived class of GapElement for GAP functions. Like its parent, 

you can call instances to implement function call syntax. The only 

difference is that a fixed first argument is prepended to the 

argument list. 

  

EXAMPLES:: 

  

sage: lst = libgap([]) 

sage: lst.Add 

<Gap function "Add"> 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_MethodProxy'> 

sage: lst.Add(1) 

sage: lst 

[ 1 ] 

""" 

  

def __call__(self, *args): 

""" 

Call syntax for methods. 

  

This method is analogous to 

:meth:`GapElement_Function.__call__`, except that it inserts a 

fixed :class:`GapElement` in the first slot of the function. 

  

INPUT: 

  

- ``*args`` -- arguments. Will be converted to `GapElement` if 

they are not already of this type. 

  

OUTPUT: 

  

A :class:`GapElement` encapsulating the functions return 

value, or ``None`` if it does not return anything. 

  

EXAMPLES:: 

  

sage: lst = libgap.eval('[1,,3]') 

sage: lst.Add.__call__(4) 

sage: lst.Add(5) 

sage: lst 

[ 1,, 3, 4, 5 ] 

""" 

if len(args) > 0: 

return GapElement_Function.__call__(self, * ([self.first_argument] + list(args))) 

else: 

return GapElement_Function.__call__(self, self.first_argument) 

  

  

  

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

### GapElement_List ######################################################## 

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

  

cdef GapElement_List make_GapElement_List(parent, libGAP_Obj obj): 

r""" 

Turn a Gap C List object (of type ``Obj``) into a Cython ``GapElement_List``. 

  

EXAMPLES:: 

  

sage: libgap([0, 2, 3]) 

[ 0, 2, 3 ] 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_List'> 

""" 

cdef GapElement_List r = GapElement_List.__new__(GapElement_List) 

r._initialize(parent, obj) 

return r 

  

  

cdef class GapElement_List(GapElement): 

r""" 

Derived class of GapElement for GAP Lists. 

  

.. NOTE:: 

  

Lists are indexed by `0..len(l)-1`, as expected from 

Python. This differs from the GAP convention where lists start 

at `1`. 

  

EXAMPLES:: 

  

sage: lst = libgap.SymmetricGroup(3).List(); lst 

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

sage: type(lst) 

<type 'sage.libs.gap.element.GapElement_List'> 

sage: len(lst) 

6 

sage: lst[3] 

(2,3) 

  

We can easily convert a Gap ``List`` object into a Python ``list``:: 

  

sage: list(lst) 

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

sage: type(_) 

<... 'list'> 

  

Range checking is performed:: 

  

sage: lst[10] 

Traceback (most recent call last): 

... 

IndexError: index out of range. 

""" 

  

def __len__(self): 

r""" 

Return the length of the list. 

  

OUTPUT: 

  

Integer. 

  

EXAMPLES:: 

  

sage: lst = libgap.eval('[1,,,4]') # a sparse list 

sage: len(lst) 

4 

""" 

return libGAP_LEN_LIST(self.value) 

  

def __getitem__(self, i): 

r""" 

Return the ``i``-th element of the list. 

  

As usual in Python, indexing starts at `0` and not at `1` (as 

in GAP). This can also be used with multi-indices. 

  

INPUT: 

  

- ``i`` -- integer. 

  

OUTPUT: 

  

The ``i``-th element as a :class:`GapElement`. 

  

EXAMPLES:: 

  

sage: lst = libgap.eval('["first",,,"last"]') # a sparse list 

sage: lst[0] 

"first" 

  

sage: l = libgap.eval('[ [0, 1], [2, 3] ]') 

sage: l[0,0] 

0 

sage: l[0,1] 

1 

sage: l[1,0] 

2 

sage: l[0,2] 

Traceback (most recent call last): 

... 

IndexError: index out of range 

sage: l[2,0] 

Traceback (most recent call last): 

... 

IndexError: index out of range 

sage: l[0,0,0] 

Traceback (most recent call last): 

... 

ValueError: too many indices 

""" 

cdef int j 

cdef libGAP_Obj obj = self.value 

  

if isinstance(i, tuple): 

for j in i: 

if not libGAP_IS_LIST(obj): 

raise ValueError('too many indices') 

if j < 0 or j >= libGAP_LEN_LIST(obj): 

raise IndexError('index out of range') 

obj = libGAP_ELM_LIST(obj, j+1) 

  

else: 

j = i 

if j < 0 or j >= libGAP_LEN_LIST(obj): 

raise IndexError('index out of range.') 

obj = libGAP_ELM_LIST(obj, j+1) 

  

return make_any_gap_element(self.parent(), obj) 

  

def __setitem__(self, i, elt): 

r""" 

Set the ``i``-th item of this list 

  

EXAMPLES:: 

  

sage: l = libgap.eval('[0, 1]') 

sage: l 

[ 0, 1 ] 

sage: l[0] = 3 

sage: l 

[ 3, 1 ] 

  

Contrarily to Python lists, setting an element beyond the limit extends the list:: 

  

sage: l[12] = -2 

sage: l 

[ 3, 1,,,,,,,,,,, -2 ] 

  

This function also handles multi-indices:: 

  

sage: l = libgap.eval('[[[0,1],[2,3]],[[4,5], [6,7]]]') 

sage: l[0,1,0] = -18 

sage: l 

[ [ [ 0, 1 ], [ -18, 3 ] ], [ [ 4, 5 ], [ 6, 7 ] ] ] 

sage: l[0,0,0,0] 

Traceback (most recent call last): 

... 

ValueError: too many indices 

  

Assignment to immutable objects gives error:: 

  

sage: l = libgap([0,1]) 

sage: u = l.deepcopy(0) 

sage: u[0] = 5 

Traceback (most recent call last): 

... 

TypeError: immutable Gap object does not support item assignment 

  

TESTS:: 

  

sage: m = libgap.eval('[[0,0],[0,0]]') 

sage: m[0,0] = 1 

sage: m[0,1] = 2 

sage: m[1,0] = 3 

sage: m[1,1] = 4 

sage: m 

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

""" 

if not libGAP_IS_MUTABLE_OBJ(self.value): 

raise TypeError('immutable Gap object does not support item assignment') 

  

cdef int j 

cdef libGAP_Obj obj = self.value 

  

if isinstance(i, tuple): 

for j in i[:-1]: 

if not libGAP_IS_LIST(obj): 

raise ValueError('too many indices') 

if j < 0 or j >= libGAP_LEN_LIST(obj): 

raise IndexError('index out of range') 

obj = libGAP_ELM_LIST(obj, j+1) 

if not libGAP_IS_LIST(obj): 

raise ValueError('too many indices') 

j = i[-1] 

else: 

j = i 

  

if j < 0: 

raise IndexError('index out of range.') 

  

cdef GapElement celt 

if isinstance(elt, GapElement): 

celt = <GapElement> elt 

else: 

celt= self.parent()(elt) 

  

libGAP_ASS_LIST(obj, j+1, celt.value) 

  

def sage(self, **kwds): 

r""" 

Return the Sage equivalent of the :class:`GapElement` 

  

OUTPUT: 

  

A Python list. 

  

EXAMPLES:: 

  

sage: libgap([ 1, 3, 4 ]).sage() 

[1, 3, 4] 

sage: all( x in ZZ for x in _ ) 

True 

""" 

return [ x.sage(**kwds) for x in self ] 

  

  

def matrix(self, ring=None): 

""" 

Return the list as a matrix. 

  

GAP does not have a special matrix data type, they are just 

lists of lists. This function converts a GAP list of lists to 

a Sage matrix. 

  

OUTPUT: 

  

A Sage matrix. 

  

EXAMPLES:: 

  

sage: F = libgap.GF(4) 

sage: a = F.PrimitiveElement() 

sage: m = libgap([[a,a^0],[0*a,a^2]]); m 

[ [ Z(2^2), Z(2)^0 ], 

[ 0*Z(2), Z(2^2)^2 ] ] 

sage: m.IsMatrix() 

true 

sage: matrix(m) 

[ a 1] 

[ 0 a + 1] 

sage: matrix(GF(4,'B'), m) 

[ B 1] 

[ 0 B + 1] 

  

sage: M = libgap.eval('SL(2,GF(5))').GeneratorsOfGroup()[1] 

sage: type(M) 

<type 'sage.libs.gap.element.GapElement_List'> 

sage: M[0][0] 

Z(5)^2 

sage: M.IsMatrix() 

true 

sage: M.matrix() 

[4 1] 

[4 0] 

""" 

if not self.IsMatrix(): 

raise ValueError('not a GAP matrix') 

entries = self.Flat() 

n = self.Length().sage() 

m = len(entries) // n 

if len(entries) % n != 0: 

raise ValueError('not a rectangular list of lists') 

from sage.matrix.matrix_space import MatrixSpace 

if ring is None: 

ring = entries.DefaultRing().sage() 

MS = MatrixSpace(ring, n, m) 

return MS([x.sage(ring=ring) for x in entries]) 

  

_matrix_ = matrix 

  

def vector(self, ring=None): 

""" 

Return the list as a vector. 

  

GAP does not have a special vector data type, they are just 

lists. This function converts a GAP list to a Sage vector. 

  

OUTPUT: 

  

A Sage vector. 

  

EXAMPLES:: 

  

sage: F = libgap.GF(4) 

sage: a = F.PrimitiveElement() 

sage: m = libgap([0*a, a, a^3, a^2]); m 

[ 0*Z(2), Z(2^2), Z(2)^0, Z(2^2)^2 ] 

sage: type(m) 

<type 'sage.libs.gap.element.GapElement_List'> 

sage: m[3] 

Z(2^2)^2 

sage: vector(m) 

(0, a, 1, a + 1) 

sage: vector(GF(4,'B'), m) 

(0, B, 1, B + 1) 

""" 

if not self.IsVector(): 

raise ValueError('not a GAP vector') 

from sage.modules.all import vector 

entries = self.Flat() 

n = self.Length().sage() 

if ring is None: 

ring = entries.DefaultRing().sage() 

return vector(ring, n, self.sage(ring=ring)) 

  

_vector_ = vector 

  

  

  

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

### GapElement_Permutation ################################################# 

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

  

  

cdef GapElement_Permutation make_GapElement_Permutation(parent, libGAP_Obj obj): 

r""" 

Turn a Gap C permutation object (of type ``Obj``) into a Cython ``GapElement_Permutation``. 

  

EXAMPLES:: 

  

sage: libgap.eval('(1,3,2)(4,5,8)') 

(1,3,2)(4,5,8) 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_Permutation'> 

""" 

cdef GapElement_Permutation r = GapElement_Permutation.__new__(GapElement_Permutation) 

r._initialize(parent, obj) 

return r 

  

  

cdef class GapElement_Permutation(GapElement): 

r""" 

Derived class of GapElement for GAP permutations. 

  

.. NOTE:: 

  

Permutations in GAP act on the numbers starting with 1. 

  

EXAMPLES:: 

  

sage: perm = libgap.eval('(1,5,2)(4,3,8)') 

sage: type(perm) 

<type 'sage.libs.gap.element.GapElement_Permutation'> 

""" 

  

def sage(self): 

r""" 

Return the Sage equivalent of the :class:`GapElement` 

  

EXAMPLES:: 

  

sage: perm_gap = libgap.eval('(1,5,2)(4,3,8)'); perm_gap 

(1,5,2)(3,8,4) 

sage: perm_gap.sage() 

(1,5,2)(3,8,4) 

sage: type(_) 

<type 'sage.groups.perm_gps.permgroup_element.PermutationGroupElement'> 

""" 

from sage.groups.perm_gps.permgroup_element import PermutationGroupElement 

libgap = self.parent() 

return PermutationGroupElement(libgap.ListPerm(self).sage()) 

  

  

  

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

### GapElement_Record ###################################################### 

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

  

cdef GapElement_Record make_GapElement_Record(parent, libGAP_Obj obj): 

r""" 

Turn a Gap C rec object (of type ``Obj``) into a Cython ``GapElement_Record``. 

  

EXAMPLES:: 

  

sage: libgap.eval('rec(a:=0, b:=2, c:=3)') 

rec( a := 0, b := 2, c := 3 ) 

sage: type(_) 

<type 'sage.libs.gap.element.GapElement_Record'> 

""" 

cdef GapElement_Record r = GapElement_Record.__new__(GapElement_Record) 

r._initialize(parent, obj) 

return r 

  

  

cdef class GapElement_Record(GapElement): 

r""" 

Derived class of GapElement for GAP records. 

  

EXAMPLES:: 

  

sage: rec = libgap.eval('rec(a:=123, b:=456)') 

sage: type(rec) 

<type 'sage.libs.gap.element.GapElement_Record'> 

sage: len(rec) 

2 

sage: rec['a'] 

123 

  

We can easily convert a Gap ``rec`` object into a Python ``dict``:: 

  

sage: dict(rec) 

{'a': 123, 'b': 456} 

sage: type(_) 

<... 'dict'> 

  

Range checking is performed:: 

  

sage: rec['no_such_element'] 

Traceback (most recent call last): 

... 

IndexError: libGAP: Error, Record: '<rec>.no_such_element' must have an assigned value 

""" 

  

def __len__(self): 

r""" 

Return the length of the record. 

  

OUTPUT: 

  

Integer. The number of entries in the record. 

  

EXAMPLES:: 

  

sage: rec = libgap.eval('rec(a:=123, b:=456, S3:=SymmetricGroup(3))') 

sage: len(rec) 

3 

""" 

return libGAP_LEN_PREC(self.value) 

  

  

def __iter__(self): 

r""" 

Iterate over the elements of the record. 

  

OUTPUT: 

  

A :class:`GapElement_RecordIterator`. 

  

EXAMPLES:: 

  

sage: rec = libgap.eval('rec(a:=123, b:=456)') 

sage: iter = rec.__iter__() 

sage: type(iter) 

<type 'sage.libs.gap.element.GapElement_RecordIterator'> 

sage: list(rec) 

[('a', 123), ('b', 456)] 

""" 

return GapElement_RecordIterator(self) 

  

  

cpdef libGAP_UInt record_name_to_index(self, bytes py_name): 

r""" 

Convert string to GAP record index. 

  

INPUT: 

  

- ``py_name`` -- a python string. 

  

OUTPUT: 

  

A ``UInt``, which is a GAP hash of the string. If this is the 

first time the string is encountered, a new integer is 

returned(!) 

  

EXAMPLES:: 

  

sage: rec = libgap.eval('rec(first:=123, second:=456)') 

sage: rec.record_name_to_index('first') # random output 

1812L 

sage: rec.record_name_to_index('no_such_name') # random output 

3776L 

""" 

cdef char* c_name = py_name 

try: 

libgap_enter() 

return libGAP_RNamName(c_name) 

finally: 

libgap_exit() 

  

def __getitem__(self, name): 

r""" 

Return the ``name``-th element of the GAP record. 

  

INPUT: 

  

- ``name`` -- string. 

  

OUTPUT: 

  

The record element labelled by ``name`` as a :class:`GapElement`. 

  

EXAMPLES:: 

  

sage: rec = libgap.eval('rec(first:=123, second:=456)') 

sage: rec['first'] 

123 

""" 

cdef libGAP_UInt i = self.record_name_to_index(name) 

cdef libGAP_Obj result 

try: 

sig_on() 

result = libGAP_ELM_REC(self.value, i) 

sig_off() 

except RuntimeError as msg: 

raise IndexError('libGAP: ' + str(msg)) 

return make_any_gap_element(self.parent(), result) 

  

  

def sage(self): 

r""" 

Return the Sage equivalent of the :class:`GapElement` 

  

EXAMPLES:: 

  

sage: libgap.eval('rec(a:=1, b:=2)').sage() 

{'a': 1, 'b': 2} 

sage: all( isinstance(key,str) and val in ZZ for key,val in _.items() ) 

True 

  

sage: rec = libgap.eval('rec(a:=123, b:=456, Sym3:=SymmetricGroup(3))') 

sage: rec.sage() 

{'Sym3': NotImplementedError('cannot construct equivalent Sage object',), 

'a': 123, 

'b': 456} 

""" 

result = dict() 

for key, val in self: 

try: 

val = val.sage() 

except Exception as ex: 

val = ex 

result[key] = val 

return result 

  

  

cdef class GapElement_RecordIterator(object): 

r""" 

Iterator for :class:`GapElement_Record` 

  

Since Cython does not support generators yet, we implement the 

older iterator specification with this auxiliary class. 

  

INPUT: 

  

- ``rec`` -- the :class:`GapElement_Record` to iterate over. 

  

EXAMPLES:: 

  

sage: rec = libgap.eval('rec(a:=123, b:=456)') 

sage: list(rec) 

[('a', 123), ('b', 456)] 

sage: dict(rec) 

{'a': 123, 'b': 456} 

""" 

  

def __cinit__(self, rec): 

r""" 

The Cython constructor. 

  

INPUT: 

  

- ``rec`` -- the :class:`GapElement_Record` to iterate over. 

  

EXAMPLES:: 

  

sage: libgap.eval('rec(a:=123, b:=456)') 

rec( a := 123, b := 456 ) 

""" 

self.rec = rec 

self.i = 1 

  

  

def __next__(self): 

r""" 

Return the next element in the record. 

  

OUTPUT: 

  

A tuple ``(key, value)`` where ``key`` is a string and 

``value`` is the corresponding :class:`GapElement`. 

  

EXAMPLES:: 

  

sage: rec = libgap.eval('rec(a:=123, b:=456)') 

sage: iter = rec.__iter__() 

sage: iter.__next__() 

('a', 123) 

sage: next(iter) 

('b', 456) 

""" 

cdef libGAP_UInt i = self.i 

if i>len(self.rec): 

raise StopIteration 

# note the abs: negative values mean the rec keys are not sorted 

libgap_enter() 

key_index = abs(libGAP_GET_RNAM_PREC(self.rec.value, i)) 

key = libGAP_NAME_RNAM(key_index) 

cdef libGAP_Obj result = libGAP_GET_ELM_PREC(self.rec.value,i) 

libgap_exit() 

val = make_any_gap_element(self.rec.parent(), result) 

self.i += 1 

return (key, val) 

  

  

# Add support for _instancedoc_ 

from sage.docs.instancedoc import instancedoc 

instancedoc(GapElement_Function) 

instancedoc(GapElement_MethodProxy)