Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

1715

1716

1717

1718

1719

1720

1721

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

1952

1953

1954

1955

1956

1957

1958

1959

1960

1961

1962

1963

1964

1965

1966

1967

1968

1969

1970

1971

1972

1973

1974

1975

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

1986

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1998

1999

2000

2001

2002

2003

2004

2005

2006

2007

2008

2009

2010

2011

2012

2013

2014

2015

2016

2017

2018

2019

2020

2021

2022

2023

2024

2025

2026

2027

2028

2029

2030

2031

2032

2033

2034

2035

2036

2037

2038

2039

2040

2041

2042

2043

2044

2045

2046

2047

2048

2049

2050

2051

2052

2053

2054

2055

2056

2057

2058

2059

2060

2061

2062

2063

2064

2065

2066

2067

2068

2069

2070

2071

2072

2073

2074

2075

2076

2077

2078

2079

2080

2081

2082

2083

2084

2085

2086

2087

2088

2089

2090

2091

2092

2093

2094

2095

2096

2097

2098

2099

2100

2101

2102

2103

2104

2105

2106

2107

2108

2109

2110

2111

2112

2113

2114

2115

2116

2117

2118

2119

2120

2121

2122

2123

2124

2125

2126

2127

2128

2129

2130

2131

2132

2133

2134

2135

2136

2137

2138

2139

2140

2141

2142

2143

2144

2145

2146

2147

2148

2149

2150

2151

2152

2153

2154

2155

2156

2157

2158

2159

2160

2161

2162

2163

2164

2165

2166

2167

2168

2169

2170

2171

2172

2173

2174

2175

2176

2177

2178

2179

2180

2181

2182

2183

2184

2185

2186

2187

2188

2189

2190

2191

2192

2193

2194

2195

2196

2197

2198

2199

2200

2201

2202

2203

2204

2205

2206

2207

2208

2209

2210

2211

2212

2213

2214

2215

2216

2217

2218

2219

2220

2221

2222

2223

2224

2225

2226

2227

2228

2229

2230

2231

2232

2233

2234

2235

2236

2237

2238

2239

2240

2241

2242

2243

2244

2245

2246

2247

2248

2249

2250

2251

2252

2253

2254

2255

2256

2257

2258

2259

2260

2261

2262

2263

2264

2265

2266

2267

2268

2269

2270

2271

2272

2273

2274

2275

2276

2277

2278

2279

2280

2281

2282

2283

2284

2285

2286

2287

2288

2289

2290

2291

2292

2293

2294

2295

2296

2297

2298

2299

2300

2301

2302

2303

2304

2305

2306

2307

2308

2309

2310

2311

2312

2313

2314

2315

2316

2317

2318

2319

2320

2321

2322

2323

2324

2325

2326

2327

2328

2329

2330

2331

2332

2333

2334

2335

2336

2337

2338

2339

2340

2341

2342

2343

2344

2345

2346

2347

2348

2349

2350

2351

2352

2353

2354

2355

2356

2357

2358

2359

2360

2361

2362

2363

2364

2365

2366

2367

2368

2369

2370

2371

2372

2373

2374

2375

2376

2377

2378

2379

2380

2381

2382

2383

2384

2385

2386

2387

2388

2389

2390

2391

2392

2393

2394

2395

2396

2397

2398

2399

2400

2401

2402

2403

2404

2405

2406

2407

2408

2409

2410

2411

2412

2413

2414

2415

2416

2417

2418

2419

2420

2421

2422

2423

2424

2425

2426

2427

2428

2429

2430

2431

2432

2433

2434

2435

2436

2437

2438

2439

2440

2441

2442

2443

2444

2445

2446

2447

2448

2449

2450

2451

2452

2453

2454

2455

2456

2457

2458

2459

2460

2461

2462

2463

2464

2465

2466

2467

2468

2469

2470

2471

2472

2473

2474

2475

2476

2477

2478

2479

2480

2481

2482

2483

2484

2485

2486

2487

2488

2489

2490

2491

2492

2493

2494

2495

2496

2497

2498

2499

2500

2501

2502

2503

2504

2505

2506

2507

2508

2509

2510

2511

2512

2513

2514

2515

2516

2517

2518

2519

2520

2521

2522

2523

2524

2525

2526

2527

2528

2529

2530

2531

2532

2533

2534

2535

2536

2537

2538

2539

2540

2541

2542

2543

2544

2545

2546

2547

2548

2549

2550

2551

2552

2553

2554

2555

2556

2557

2558

2559

2560

2561

2562

2563

2564

2565

2566

2567

2568

2569

2570

2571

2572

2573

2574

2575

2576

2577

2578

2579

2580

2581

2582

2583

2584

2585

2586

2587

2588

2589

2590

2591

2592

2593

2594

2595

2596

2597

2598

2599

2600

2601

2602

2603

2604

2605

2606

2607

2608

2609

2610

2611

2612

2613

2614

2615

2616

2617

2618

2619

2620

2621

2622

2623

2624

2625

2626

2627

2628

2629

2630

2631

2632

2633

2634

2635

2636

2637

2638

2639

2640

2641

2642

2643

2644

2645

2646

2647

2648

2649

2650

2651

2652

2653

2654

2655

2656

2657

2658

2659

2660

2661

2662

2663

2664

2665

2666

2667

2668

2669

2670

2671

2672

2673

2674

2675

2676

2677

2678

2679

2680

2681

2682

2683

2684

2685

2686

2687

2688

2689

2690

2691

2692

2693

2694

2695

2696

2697

2698

2699

2700

2701

2702

2703

2704

2705

2706

2707

2708

2709

2710

2711

2712

2713

2714

2715

2716

2717

2718

2719

2720

2721

2722

2723

2724

2725

2726

2727

2728

2729

2730

2731

2732

2733

2734

2735

2736

2737

2738

2739

2740

2741

2742

2743

2744

2745

2746

2747

2748

2749

2750

2751

2752

2753

2754

2755

2756

2757

2758

2759

2760

2761

2762

2763

2764

2765

2766

2767

2768

2769

2770

2771

2772

2773

2774

2775

2776

2777

2778

2779

2780

2781

2782

2783

2784

2785

2786

2787

2788

2789

2790

2791

2792

2793

2794

2795

2796

2797

2798

2799

2800

2801

2802

2803

2804

2805

2806

2807

2808

2809

2810

2811

2812

2813

2814

2815

2816

2817

2818

2819

2820

2821

2822

2823

2824

2825

2826

2827

2828

2829

2830

2831

2832

2833

2834

2835

2836

2837

2838

2839

2840

2841

2842

2843

2844

2845

2846

2847

2848

2849

2850

2851

2852

2853

2854

2855

2856

2857

2858

2859

2860

2861

2862

2863

2864

2865

2866

2867

2868

2869

2870

2871

2872

2873

2874

2875

2876

2877

2878

2879

2880

2881

2882

2883

2884

2885

2886

2887

2888

2889

2890

2891

2892

2893

2894

2895

2896

2897

2898

2899

2900

2901

2902

2903

2904

2905

2906

2907

2908

2909

2910

2911

2912

2913

2914

2915

2916

2917

2918

2919

2920

2921

2922

2923

2924

2925

2926

2927

2928

2929

2930

2931

2932

2933

2934

2935

2936

2937

2938

2939

2940

2941

2942

2943

2944

2945

2946

2947

2948

2949

2950

2951

2952

2953

2954

2955

2956

2957

2958

2959

2960

2961

2962

2963

2964

2965

2966

2967

2968

2969

2970

2971

2972

2973

2974

2975

2976

2977

2978

2979

2980

2981

2982

2983

2984

2985

2986

2987

2988

2989

2990

2991

2992

2993

2994

2995

2996

2997

2998

2999

3000

3001

3002

3003

3004

3005

3006

3007

3008

3009

3010

3011

3012

3013

3014

3015

3016

3017

3018

3019

3020

3021

3022

3023

3024

3025

3026

3027

3028

3029

3030

3031

3032

3033

3034

3035

3036

3037

3038

3039

3040

3041

3042

3043

3044

3045

3046

3047

3048

3049

3050

3051

3052

3053

3054

3055

3056

3057

3058

3059

3060

3061

3062

3063

3064

3065

3066

3067

3068

3069

3070

3071

3072

3073

3074

3075

3076

3077

3078

3079

3080

3081

3082

3083

3084

3085

3086

3087

3088

3089

3090

3091

3092

3093

3094

3095

3096

3097

3098

3099

3100

3101

3102

3103

3104

3105

3106

3107

3108

3109

3110

3111

3112

3113

3114

3115

3116

3117

3118

3119

3120

3121

3122

3123

3124

3125

3126

3127

3128

3129

3130

3131

3132

3133

3134

3135

3136

3137

3138

3139

3140

3141

3142

3143

3144

3145

3146

3147

3148

3149

3150

3151

3152

3153

3154

3155

3156

3157

3158

3159

3160

3161

3162

3163

3164

3165

3166

3167

3168

3169

3170

3171

3172

3173

3174

3175

3176

3177

3178

3179

3180

3181

3182

3183

3184

3185

3186

3187

3188

3189

3190

3191

3192

3193

3194

3195

3196

3197

3198

3199

3200

3201

3202

3203

3204

3205

3206

3207

3208

3209

3210

3211

3212

3213

3214

3215

3216

3217

3218

3219

3220

3221

3222

3223

3224

3225

3226

3227

3228

3229

3230

3231

3232

3233

3234

3235

3236

3237

3238

3239

3240

3241

3242

3243

3244

3245

3246

3247

3248

3249

3250

3251

3252

3253

3254

3255

3256

3257

3258

3259

3260

3261

3262

3263

3264

3265

3266

3267

3268

3269

3270

3271

3272

3273

3274

3275

3276

3277

3278

3279

3280

3281

3282

3283

3284

3285

3286

3287

3288

3289

3290

3291

3292

3293

3294

3295

3296

3297

3298

3299

3300

3301

3302

3303

3304

3305

3306

3307

3308

3309

3310

3311

3312

3313

3314

3315

3316

3317

3318

3319

3320

3321

3322

3323

3324

3325

3326

3327

3328

3329

3330

3331

3332

3333

3334

3335

3336

3337

3338

3339

3340

3341

3342

3343

3344

3345

3346

3347

3348

3349

3350

3351

3352

3353

3354

3355

3356

3357

3358

3359

3360

3361

3362

3363

3364

3365

3366

3367

3368

3369

3370

3371

3372

3373

3374

3375

3376

3377

3378

3379

3380

3381

3382

3383

3384

3385

3386

3387

3388

3389

3390

3391

3392

3393

3394

3395

3396

3397

3398

3399

3400

3401

3402

3403

3404

3405

3406

3407

3408

3409

3410

3411

3412

3413

3414

3415

3416

3417

3418

3419

3420

3421

3422

3423

3424

3425

3426

3427

3428

3429

3430

3431

3432

3433

3434

3435

3436

3437

3438

3439

3440

3441

3442

3443

3444

3445

3446

3447

3448

3449

3450

3451

3452

3453

3454

3455

3456

3457

3458

3459

3460

3461

3462

3463

3464

3465

3466

3467

3468

3469

3470

3471

3472

3473

3474

3475

3476

3477

3478

3479

3480

3481

3482

3483

3484

3485

3486

3487

3488

3489

3490

3491

3492

3493

3494

3495

3496

3497

3498

3499

3500

3501

3502

3503

3504

3505

3506

3507

3508

3509

3510

3511

3512

3513

3514

3515

3516

3517

3518

3519

3520

3521

3522

3523

3524

3525

3526

3527

3528

3529

3530

3531

3532

3533

3534

3535

3536

3537

3538

3539

3540

3541

3542

3543

3544

3545

3546

3547

3548

3549

3550

3551

3552

3553

3554

3555

3556

3557

3558

3559

3560

3561

3562

3563

3564

3565

3566

3567

3568

3569

3570

3571

3572

3573

3574

3575

3576

3577

3578

3579

3580

3581

3582

3583

3584

3585

3586

3587

3588

3589

3590

3591

3592

3593

3594

3595

3596

3597

3598

3599

3600

3601

3602

3603

3604

3605

3606

3607

3608

3609

3610

3611

3612

3613

3614

3615

3616

3617

3618

3619

3620

3621

3622

3623

3624

3625

3626

3627

3628

3629

3630

3631

3632

3633

3634

3635

3636

3637

3638

3639

3640

3641

3642

3643

3644

3645

3646

3647

3648

3649

3650

3651

3652

3653

3654

3655

3656

3657

3658

3659

3660

3661

3662

3663

3664

3665

3666

3667

3668

3669

3670

3671

3672

3673

3674

3675

3676

3677

3678

3679

3680

3681

3682

3683

3684

3685

3686

3687

3688

3689

3690

3691

3692

3693

3694

3695

3696

3697

3698

3699

3700

3701

3702

3703

3704

3705

3706

3707

3708

3709

3710

3711

3712

3713

3714

3715

3716

3717

3718

3719

3720

3721

3722

3723

3724

3725

3726

3727

3728

3729

3730

3731

3732

3733

3734

3735

3736

3737

3738

3739

3740

3741

3742

3743

3744

3745

3746

3747

3748

3749

3750

3751

3752

3753

3754

3755

3756

3757

3758

3759

3760

3761

3762

3763

3764

3765

3766

3767

3768

3769

3770

3771

3772

3773

3774

3775

3776

3777

3778

3779

3780

3781

3782

3783

3784

3785

3786

3787

3788

3789

3790

3791

3792

3793

3794

3795

3796

3797

3798

3799

3800

3801

3802

3803

3804

3805

3806

3807

3808

3809

3810

3811

3812

3813

3814

3815

3816

3817

3818

3819

3820

3821

3822

3823

3824

3825

3826

3827

3828

3829

3830

3831

3832

3833

3834

3835

3836

3837

3838

3839

3840

3841

3842

3843

3844

3845

3846

3847

3848

3849

3850

3851

3852

3853

3854

3855

3856

3857

3858

3859

3860

3861

3862

3863

3864

3865

3866

3867

3868

3869

3870

3871

3872

3873

3874

3875

3876

3877

3878

3879

3880

3881

3882

3883

3884

3885

3886

3887

3888

3889

3890

3891

3892

3893

3894

3895

3896

3897

3898

3899

3900

3901

3902

3903

3904

3905

3906

3907

3908

3909

3910

3911

3912

3913

3914

3915

3916

3917

3918

3919

3920

3921

3922

3923

3924

3925

3926

3927

3928

3929

3930

3931

3932

3933

3934

3935

3936

3937

3938

3939

3940

3941

3942

3943

3944

3945

3946

3947

3948

3949

3950

3951

3952

3953

3954

3955

3956

3957

3958

3959

3960

3961

3962

3963

3964

3965

3966

3967

3968

3969

3970

3971

3972

3973

3974

3975

3976

3977

3978

3979

3980

3981

3982

3983

3984

3985

3986

3987

3988

3989

3990

3991

3992

3993

3994

3995

3996

3997

3998

3999

4000

4001

4002

4003

4004

4005

4006

4007

4008

4009

4010

4011

4012

4013

4014

4015

4016

4017

4018

4019

4020

4021

4022

4023

4024

4025

4026

4027

4028

4029

4030

4031

4032

4033

4034

4035

4036

4037

4038

4039

4040

4041

4042

4043

4044

4045

4046

4047

4048

4049

4050

4051

4052

4053

4054

4055

4056

4057

4058

4059

4060

4061

4062

4063

4064

4065

4066

4067

4068

4069

4070

4071

4072

4073

4074

4075

4076

4077

4078

4079

4080

4081

4082

4083

4084

4085

4086

4087

4088

4089

4090

4091

4092

4093

4094

4095

4096

4097

4098

4099

4100

4101

4102

4103

4104

4105

4106

4107

4108

4109

4110

4111

4112

4113

4114

4115

4116

4117

4118

4119

4120

4121

4122

4123

4124

4125

4126

4127

4128

4129

4130

4131

4132

4133

4134

4135

4136

4137

4138

4139

4140

4141

4142

4143

4144

4145

4146

4147

4148

4149

4150

4151

4152

4153

4154

4155

4156

4157

4158

4159

4160

4161

4162

4163

4164

4165

4166

4167

4168

4169

4170

4171

4172

4173

4174

4175

4176

4177

4178

4179

4180

4181

4182

4183

4184

4185

4186

4187

4188

4189

4190

4191

4192

4193

4194

4195

4196

4197

4198

4199

4200

4201

4202

4203

4204

4205

4206

4207

4208

4209

4210

4211

4212

4213

4214

4215

4216

4217

4218

4219

4220

4221

4222

4223

4224

4225

4226

4227

4228

4229

4230

4231

4232

4233

4234

4235

4236

4237

4238

4239

4240

4241

4242

4243

4244

4245

4246

4247

4248

4249

4250

4251

4252

4253

4254

4255

4256

4257

4258

4259

4260

4261

4262

4263

4264

4265

4266

4267

4268

4269

4270

4271

4272

4273

4274

4275

4276

4277

4278

4279

4280

4281

4282

4283

4284

4285

4286

4287

4288

4289

4290

4291

4292

4293

4294

4295

4296

4297

4298

4299

4300

4301

4302

4303

4304

4305

4306

4307

4308

4309

4310

4311

4312

4313

4314

4315

4316

4317

4318

4319

4320

4321

4322

4323

4324

4325

4326

4327

4328

4329

4330

4331

4332

4333

4334

4335

4336

4337

4338

4339

4340

4341

4342

4343

4344

4345

4346

4347

4348

4349

4350

4351

4352

4353

4354

4355

4356

4357

4358

4359

4360

4361

4362

4363

4364

4365

4366

4367

4368

4369

4370

4371

4372

4373

4374

4375

4376

4377

4378

4379

4380

4381

4382

4383

4384

4385

4386

4387

4388

4389

4390

4391

4392

4393

4394

4395

4396

4397

4398

4399

4400

4401

4402

4403

4404

4405

4406

4407

4408

4409

4410

4411

4412

4413

4414

4415

4416

4417

4418

4419

4420

4421

4422

4423

4424

4425

4426

4427

4428

4429

4430

4431

4432

4433

4434

4435

4436

4437

4438

4439

4440

4441

4442

4443

4444

4445

4446

4447

4448

4449

4450

4451

4452

4453

4454

4455

4456

4457

4458

4459

4460

4461

4462

4463

4464

4465

4466

4467

4468

4469

4470

4471

4472

4473

4474

4475

4476

4477

4478

4479

4480

4481

4482

4483

4484

4485

4486

4487

4488

4489

4490

4491

4492

4493

4494

4495

4496

4497

4498

4499

4500

4501

4502

4503

4504

4505

4506

4507

4508

4509

4510

4511

4512

4513

4514

4515

4516

4517

4518

4519

4520

4521

4522

4523

4524

4525

4526

4527

4528

4529

4530

4531

4532

4533

4534

4535

4536

4537

4538

4539

4540

4541

4542

4543

4544

4545

4546

4547

4548

4549

4550

4551

4552

4553

4554

4555

4556

4557

4558

4559

4560

4561

4562

4563

4564

4565

4566

4567

4568

4569

4570

4571

4572

4573

4574

4575

4576

4577

4578

4579

4580

4581

4582

4583

4584

4585

4586

4587

4588

4589

4590

4591

4592

4593

4594

4595

4596

4597

4598

4599

4600

4601

4602

4603

4604

4605

4606

4607

4608

4609

4610

4611

4612

4613

4614

4615

4616

4617

4618

4619

4620

4621

4622

4623

4624

4625

4626

4627

4628

4629

4630

4631

4632

4633

4634

4635

4636

4637

4638

4639

4640

4641

4642

4643

4644

4645

4646

4647

4648

4649

4650

4651

4652

4653

4654

4655

4656

4657

4658

4659

4660

4661

4662

4663

4664

4665

4666

4667

4668

4669

4670

4671

4672

4673

4674

4675

4676

4677

4678

4679

4680

4681

4682

4683

4684

4685

4686

4687

4688

4689

4690

4691

4692

4693

4694

4695

4696

4697

4698

4699

4700

4701

4702

4703

4704

4705

4706

4707

4708

4709

4710

4711

4712

4713

4714

4715

4716

4717

4718

4719

4720

4721

4722

4723

4724

4725

4726

4727

4728

4729

4730

4731

4732

4733

4734

4735

4736

4737

4738

4739

4740

4741

4742

4743

4744

4745

4746

4747

4748

4749

4750

4751

4752

4753

4754

4755

4756

4757

4758

4759

4760

4761

4762

4763

4764

4765

4766

4767

4768

4769

4770

4771

4772

4773

4774

4775

4776

4777

4778

4779

4780

4781

4782

4783

4784

4785

4786

4787

4788

4789

4790

4791

4792

4793

4794

4795

4796

4797

4798

4799

4800

4801

4802

4803

4804

4805

4806

4807

4808

4809

4810

4811

4812

4813

4814

4815

4816

4817

4818

4819

4820

4821

4822

4823

4824

4825

4826

4827

4828

4829

4830

4831

4832

4833

4834

4835

4836

4837

4838

4839

4840

4841

4842

4843

4844

4845

4846

4847

4848

4849

4850

4851

4852

4853

4854

4855

4856

4857

4858

4859

4860

4861

4862

4863

4864

4865

4866

4867

4868

4869

4870

4871

4872

4873

4874

4875

4876

4877

4878

4879

4880

4881

4882

4883

4884

4885

4886

4887

4888

4889

4890

4891

4892

4893

4894

4895

4896

4897

4898

4899

4900

4901

4902

4903

4904

4905

4906

4907

4908

4909

4910

4911

4912

4913

4914

4915

4916

4917

4918

4919

4920

4921

4922

4923

4924

4925

4926

4927

4928

4929

4930

4931

4932

4933

4934

4935

4936

4937

4938

4939

4940

4941

4942

4943

4944

4945

4946

4947

4948

4949

4950

4951

4952

4953

4954

4955

4956

4957

4958

4959

4960

4961

4962

4963

4964

4965

4966

4967

4968

4969

4970

4971

4972

4973

4974

4975

4976

4977

4978

4979

4980

4981

4982

4983

4984

4985

4986

4987

4988

4989

4990

4991

4992

4993

4994

4995

4996

4997

4998

4999

5000

5001

5002

5003

5004

5005

5006

5007

5008

5009

5010

5011

5012

5013

5014

5015

5016

5017

5018

5019

5020

5021

5022

5023

5024

5025

5026

5027

5028

5029

5030

5031

5032

5033

5034

5035

5036

5037

5038

5039

5040

5041

5042

5043

5044

5045

5046

5047

5048

5049

5050

5051

5052

5053

5054

5055

5056

5057

5058

5059

5060

5061

5062

5063

5064

5065

5066

5067

5068

5069

5070

5071

5072

5073

5074

5075

5076

5077

5078

5079

5080

5081

5082

5083

5084

5085

5086

5087

5088

5089

5090

5091

5092

5093

5094

5095

5096

5097

5098

5099

5100

5101

5102

5103

5104

5105

5106

5107

5108

5109

5110

5111

5112

5113

5114

5115

5116

5117

5118

5119

5120

5121

5122

5123

5124

5125

5126

5127

5128

5129

5130

5131

5132

5133

5134

5135

5136

5137

5138

5139

5140

5141

5142

5143

5144

5145

5146

5147

5148

5149

5150

5151

5152

5153

5154

5155

5156

5157

5158

5159

5160

5161

5162

5163

5164

5165

5166

5167

5168

5169

5170

5171

5172

5173

5174

5175

5176

5177

5178

5179

5180

5181

5182

5183

5184

5185

5186

5187

5188

5189

5190

5191

5192

5193

5194

5195

5196

5197

5198

5199

5200

5201

5202

5203

5204

5205

5206

5207

5208

5209

5210

5211

5212

5213

5214

5215

5216

5217

5218

5219

5220

5221

5222

5223

5224

5225

5226

5227

5228

5229

5230

5231

5232

5233

5234

5235

5236

5237

5238

5239

5240

5241

5242

5243

5244

5245

5246

5247

5248

5249

5250

5251

5252

5253

5254

5255

5256

5257

5258

5259

5260

5261

5262

5263

5264

5265

5266

5267

5268

5269

5270

5271

5272

5273

5274

5275

5276

5277

5278

5279

5280

5281

5282

5283

5284

5285

5286

5287

5288

5289

5290

5291

5292

5293

5294

5295

5296

5297

5298

5299

5300

5301

5302

5303

5304

5305

5306

5307

5308

5309

5310

5311

5312

5313

5314

5315

5316

5317

5318

5319

5320

5321

5322

5323

5324

5325

5326

5327

5328

5329

5330

5331

5332

5333

5334

5335

5336

5337

5338

5339

5340

5341

5342

5343

5344

5345

5346

5347

5348

5349

5350

5351

5352

5353

5354

5355

5356

5357

5358

5359

5360

5361

5362

5363

5364

5365

5366

5367

5368

5369

5370

5371

5372

5373

5374

5375

5376

5377

5378

5379

5380

5381

5382

5383

5384

5385

5386

5387

5388

5389

5390

5391

5392

5393

5394

5395

5396

5397

5398

5399

5400

5401

5402

5403

5404

5405

5406

5407

5408

5409

5410

5411

5412

5413

5414

5415

5416

5417

5418

5419

5420

5421

5422

5423

5424

5425

5426

5427

5428

5429

5430

5431

5432

5433

5434

5435

5436

5437

5438

5439

5440

5441

5442

5443

5444

5445

5446

5447

5448

5449

5450

5451

5452

5453

5454

5455

5456

5457

5458

5459

5460

5461

5462

5463

5464

5465

5466

5467

5468

5469

5470

5471

5472

5473

5474

5475

5476

5477

5478

5479

5480

5481

5482

5483

5484

5485

5486

5487

5488

5489

5490

5491

5492

5493

5494

5495

5496

5497

5498

5499

5500

5501

5502

5503

5504

5505

5506

5507

5508

5509

5510

5511

5512

5513

5514

5515

5516

5517

5518

5519

5520

5521

5522

5523

5524

5525

5526

5527

5528

5529

5530

5531

5532

5533

5534

5535

5536

5537

5538

5539

5540

5541

5542

5543

5544

5545

5546

5547

5548

5549

5550

5551

5552

5553

5554

5555

5556

5557

5558

5559

5560

5561

5562

5563

5564

5565

5566

5567

5568

5569

5570

5571

5572

5573

5574

5575

5576

5577

5578

5579

5580

5581

5582

5583

5584

5585

5586

5587

5588

5589

5590

5591

5592

5593

5594

5595

5596

5597

5598

5599

5600

5601

5602

5603

5604

5605

5606

5607

5608

r""" 

Multivariate Polynomials via libSINGULAR 

  

This module implements specialized and optimized implementations for 

multivariate polynomials over many coefficient rings, via a shared library 

interface to SINGULAR. In particular, the following coefficient rings are 

supported by this implementation: 

  

- the rational numbers `\QQ`, 

  

- the ring of integers `\ZZ`, 

  

- `\ZZ/n\ZZ` for any integer `n`, 

  

- finite fields `\GF{p^n}` for `p` prime and `n > 0`, 

  

- and absolute number fields `\QQ(a)`. 

  

AUTHORS: 

  

The libSINGULAR interface was implemented by 

  

- Martin Albrecht (2007-01): initial implementation 

  

- Joel Mohler (2008-01): misc improvements, polishing 

  

- Martin Albrecht (2008-08): added `\QQ(a)` and `\ZZ` support 

  

- Simon King (2009-04): improved coercion 

  

- Martin Albrecht (2009-05): added `\ZZ/n\ZZ` support, refactoring 

  

- Martin Albrecht (2009-06): refactored the code to allow better 

re-use 

  

- Simon King (2011-03): Use a faster way of conversion from the base 

ring. 

  

- Volker Braun (2011-06): Major cleanup, refcount singular rings, bugfixes. 

  

.. TODO:: 

  

Implement Real, Complex coefficient rings via libSINGULAR 

  

EXAMPLES: 

  

We show how to construct various multivariate polynomial rings:: 

  

sage: P.<x,y,z> = QQ[] 

sage: P 

Multivariate Polynomial Ring in x, y, z over Rational Field 

  

sage: f = 27/113 * x^2 + y*z + 1/2; f 

27/113*x^2 + y*z + 1/2 

  

sage: P.term_order() 

Degree reverse lexicographic term order 

  

sage: P = PolynomialRing(GF(127),3,names='abc', order='lex') 

sage: P 

Multivariate Polynomial Ring in a, b, c over Finite Field of size 127 

  

sage: a,b,c = P.gens() 

sage: f = 57 * a^2*b + 43 * c + 1; f 

57*a^2*b + 43*c + 1 

  

sage: P.term_order() 

Lexicographic term order 

  

sage: z = QQ['z'].0 

sage: K.<s> = NumberField(z^2 - 2) 

sage: P.<x,y> = PolynomialRing(K, 2) 

sage: 1/2*s*x^2 + 3/4*s 

(1/2*s)*x^2 + (3/4*s) 

  

sage: P.<x,y,z> = ZZ[]; P 

Multivariate Polynomial Ring in x, y, z over Integer Ring 

  

sage: P.<x,y,z> = Zmod(2^10)[]; P 

Multivariate Polynomial Ring in x, y, z over Ring of integers modulo 1024 

  

sage: P.<x,y,z> = Zmod(3^10)[]; P 

Multivariate Polynomial Ring in x, y, z over Ring of integers modulo 59049 

  

sage: P.<x,y,z> = Zmod(2^100)[]; P 

Multivariate Polynomial Ring in x, y, z over Ring of integers modulo 1267650600228229401496703205376 

  

sage: P.<x,y,z> = Zmod(2521352)[]; P 

Multivariate Polynomial Ring in x, y, z over Ring of integers modulo 2521352 

sage: type(P) 

<type 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomialRing_libsingular'> 

  

sage: P.<x,y,z> = Zmod(25213521351515232)[]; P 

Multivariate Polynomial Ring in x, y, z over Ring of integers modulo 25213521351515232 

sage: type(P) 

<class 'sage.rings.polynomial.multi_polynomial_ring.MPolynomialRing_polydict_with_category'> 

  

We construct the Frobenius morphism on `\GF{5}[x,y,z]` over `\GF{5}`:: 

  

sage: R.<x,y,z> = PolynomialRing(GF(5), 3) 

sage: frob = R.hom([x^5, y^5, z^5]) 

sage: frob(x^2 + 2*y - z^4) 

-z^20 + x^10 + 2*y^5 

sage: frob((x + 2*y)^3) 

x^15 + x^10*y^5 + 2*x^5*y^10 - 2*y^15 

sage: (x^5 + 2*y^5)^3 

x^15 + x^10*y^5 + 2*x^5*y^10 - 2*y^15 

  

We make a polynomial ring in one variable over a polynomial ring in 

two variables:: 

  

sage: R.<x, y> = PolynomialRing(QQ, 2) 

sage: S.<t> = PowerSeriesRing(R) 

sage: t*(x+y) 

(x + y)*t 

  

TESTS:: 

  

sage: P.<x,y,z> = QQ[] 

sage: loads(dumps(P)) == P 

True 

sage: loads(dumps(x)) == x 

True 

sage: P.<x,y,z> = GF(2^8,'a')[] 

sage: loads(dumps(P)) == P 

True 

sage: loads(dumps(x)) == x 

True 

sage: P.<x,y,z> = GF(127)[] 

sage: loads(dumps(P)) == P 

True 

sage: loads(dumps(x)) == x 

True 

sage: P.<x,y,z> = GF(127)[] 

sage: loads(dumps(P)) == P 

True 

sage: loads(dumps(x)) == x 

True 

  

sage: Rt.<t> = PolynomialRing(QQ, implementation="singular") 

sage: p = 1+t 

sage: R.<u,v> = PolynomialRing(QQ, 2) 

sage: p(u/v) 

(u + v)/v 

  

Check if :trac:`6160` is fixed:: 

  

sage: x=var('x') 

sage: K.<j> = NumberField(x-1728) 

sage: R.<b,c> = K[] 

sage: b-j*c 

b - 1728*c 

""" 

  

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

# 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 

  

# The Singular API is as follows: 

# 

# pXXX does assume the currRing to be set 

# p_XXX does not. 

# 

# However, sometimes there are bugs, i.e. you call p_XXX and it 

# crashes unless currRing is set. 

# 

# Notable exceptions: 

# * pNext and pIter don't need currRing 

# * p_Normalize apparently needs currRing 

  

from cpython.object cimport Py_NE 

from cysignals.memory cimport sig_malloc, sig_free 

from cysignals.signals cimport sig_on, sig_off 

  

from sage.cpython.string cimport char_to_str, str_to_bytes 

  

# singular types 

from sage.libs.singular.decl cimport ring, poly, ideal, intvec, number, currRing 

from sage.libs.singular.decl cimport n_unknown, n_Zp, n_Q, n_R, n_GF, n_long_R, n_algExt,n_transExt,n_long_C, n_Z, n_Zn, n_Znm, n_Z2m, n_CF 

  

# singular functions 

from sage.libs.singular.decl cimport ( 

errorreported, 

n_IsUnit, n_Invers, 

p_ISet, rChangeCurrRing, p_Copy, p_Init, p_SetCoeff, p_Setm, p_SetExp, p_Add_q, 

p_NSet, p_GetCoeff, p_Delete, p_GetExp, pNext, rRingVar, omAlloc0, omStrDup, 

omFree, pDivide, p_SetCoeff0, n_Init, p_DivisibleBy, pLcm, p_LmDivisibleBy, 

pDivide, p_IsConstant, p_ExpVectorEqual, p_String, p_LmInit, n_Copy, 

p_IsUnit, p_Series, p_Head, idInit, fast_map_common_subexp, id_Delete, 

p_IsHomogeneous, p_Homogen, p_Totaldegree,pLDeg1_Totaldegree, singclap_pdivide, singclap_factorize, 

idLift, IDELEMS, On, Off, SW_USE_CHINREM_GCD, SW_USE_EZGCD, 

p_LmIsConstant, pTakeOutComp1, singclap_gcd, pp_Mult_qq, p_GetMaxExp, 

pLength, kNF, p_Neg, p_Minus_mm_Mult_qq, p_Plus_mm_Mult_qq, 

pDiff, singclap_resultant, p_Normalize, 

prCopyR, prCopyR_NoSort ) 

  

# singular conversion routines 

from sage.libs.singular.singular cimport si2sa, sa2si, overflow_check 

  

# singular poly arith 

from sage.libs.singular.polynomial cimport ( 

singular_polynomial_call, singular_polynomial_cmp, singular_polynomial_add, 

singular_polynomial_sub, singular_polynomial_neg, singular_polynomial_rmul, 

singular_polynomial_mul, singular_polynomial_div_coeff, singular_polynomial_pow, 

singular_polynomial_str, singular_polynomial_latex, 

singular_polynomial_str_with_changed_varnames, singular_polynomial_deg, 

singular_polynomial_length_bounded, singular_polynomial_subst ) 

  

# singular rings 

from sage.libs.singular.ring cimport singular_ring_new, singular_ring_reference, singular_ring_delete 

  

# polynomial imports 

from sage.rings.polynomial.multi_polynomial_ring import MPolynomialRing_polydict, MPolynomialRing_polydict_domain 

from sage.rings.polynomial.multi_polynomial_element import MPolynomial_polydict 

from sage.rings.polynomial.multi_polynomial_ideal import MPolynomialIdeal 

from sage.rings.polynomial.polydict import ETuple 

from sage.rings.polynomial.polynomial_ring import is_PolynomialRing 

  

# base ring imports 

from sage.rings.finite_rings.finite_field_prime_modn import FiniteField_prime_modn 

from sage.rings.rational cimport Rational 

from sage.rings.rational_field import RationalField 

from sage.rings.complex_field import is_ComplexField 

from sage.rings.real_mpfr import is_RealField 

from sage.rings.integer_ring import is_IntegerRing, ZZ 

from sage.rings.integer cimport Integer 

from sage.rings.integer import GCD_list 

from sage.rings.finite_rings.integer_mod_ring import is_IntegerModRing 

from sage.rings.number_field.number_field_base cimport NumberField 

  

from sage.structure.element import coerce_binop 

  

from sage.structure.parent cimport Parent 

from sage.structure.parent_base cimport ParentWithBase 

from sage.structure.parent_gens cimport ParentWithGens 

from sage.structure.category_object cimport CategoryObject 

  

from sage.structure.element cimport EuclideanDomainElement 

from sage.structure.element cimport RingElement 

from sage.structure.element cimport ModuleElement 

from sage.structure.element cimport Element 

from sage.structure.element cimport CommutativeRingElement 

from sage.structure.element cimport coercion_model 

  

from sage.structure.richcmp cimport rich_to_bool, richcmp 

from sage.structure.factorization import Factorization 

from sage.structure.sequence import Sequence 

  

from sage.interfaces.all import macaulay2 

from sage.interfaces.singular import singular as singular_default, is_SingularElement, SingularElement 

from sage.interfaces.macaulay2 import macaulay2 as macaulay2_default, is_Macaulay2Element 

  

from sage.misc.all import prod as mul 

from sage.misc.sage_eval import sage_eval 

  

cimport cypari2.gen 

from . import polynomial_element 

  

permstore=[] 

cdef class MPolynomialRing_libsingular(MPolynomialRing_generic): 

  

def __cinit__(self): 

""" 

The Cython constructor. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.multi_polynomial_libsingular import MPolynomialRing_libsingular 

sage: MPolynomialRing_libsingular(QQ, 3, ('x', 'y', 'z'), TermOrder('degrevlex', 3)) 

Multivariate Polynomial Ring in x, y, z over Rational Field 

sage: type(_) 

<type 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomialRing_libsingular'> 

  

sage: P.<x,y,z> = QQ[]; P 

Multivariate Polynomial Ring in x, y, z over Rational Field 

sage: type(P) 

<type 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomialRing_libsingular'> 

""" 

self._ring = NULL 

  

def __init__(self, base_ring, n, names, order='degrevlex'): 

""" 

Construct a multivariate polynomial ring subject to the 

following conditions: 

  

INPUT: 

  

- ``base_ring`` - base ring (must be either GF(q), ZZ, ZZ/nZZ, 

QQ or absolute number field) 

  

- ``n`` - number of variables (must be at least 1) 

  

- ``names`` - names of ring variables, may be string of list/tuple 

  

- ``order`` - term order (default: ``degrevlex``) 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: P 

Multivariate Polynomial Ring in x, y, z over Rational Field 

  

sage: f = 27/113 * x^2 + y*z + 1/2; f 

27/113*x^2 + y*z + 1/2 

  

sage: P.term_order() 

Degree reverse lexicographic term order 

  

sage: P = PolynomialRing(GF(127),3,names='abc', order='lex') 

sage: P 

Multivariate Polynomial Ring in a, b, c over Finite Field of size 127 

  

sage: a,b,c = P.gens() 

sage: f = 57 * a^2*b + 43 * c + 1; f 

57*a^2*b + 43*c + 1 

  

sage: P.term_order() 

Lexicographic term order 

  

sage: z = QQ['z'].0 

sage: K.<s> = NumberField(z^2 - 2) 

sage: P.<x,y> = PolynomialRing(K, 2) 

sage: 1/2*s*x^2 + 3/4*s 

(1/2*s)*x^2 + (3/4*s) 

  

sage: P.<x,y,z> = ZZ[]; P 

Multivariate Polynomial Ring in x, y, z over Integer Ring 

  

sage: P.<x,y,z> = Zmod(2^10)[]; P 

Multivariate Polynomial Ring in x, y, z over Ring of integers modulo 1024 

  

sage: P.<x,y,z> = Zmod(3^10)[]; P 

Multivariate Polynomial Ring in x, y, z over Ring of integers modulo 59049 

  

sage: P.<x,y,z> = Zmod(2^100)[]; P 

Multivariate Polynomial Ring in x, y, z over Ring of integers modulo 1267650600228229401496703205376 

  

sage: P.<x,y,z> = Zmod(2521352)[]; P 

Multivariate Polynomial Ring in x, y, z over Ring of integers modulo 2521352 

sage: type(P) 

<type 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomialRing_libsingular'> 

  

sage: P.<x,y,z> = Zmod(25213521351515232)[]; P 

Multivariate Polynomial Ring in x, y, z over Ring of integers modulo 25213521351515232 

sage: type(P) 

<class 'sage.rings.polynomial.multi_polynomial_ring.MPolynomialRing_polydict_with_category'> 

  

sage: P.<x,y,z> = PolynomialRing(Integers(2^32),order='lex') 

sage: P(2^32-1) 

4294967295 

  

TESTS: 

  

Make sure that a faster coercion map from the base ring is used; 

see :trac:`9944`:: 

  

sage: R.<x,y> = PolynomialRing(ZZ) 

sage: R.coerce_map_from(R.base_ring()) 

Polynomial base injection morphism: 

From: Integer Ring 

To: Multivariate Polynomial Ring in x, y over Integer Ring 

  

Check some invalid input:: 

  

sage: from sage.rings.polynomial.multi_polynomial_libsingular import MPolynomialRing_libsingular 

sage: MPolynomialRing_libsingular(Zmod(1), 1, ["x"], "lex") 

Traceback (most recent call last): 

... 

NotImplementedError: polynomials over Ring of integers modulo 1 are not supported in Singular 

sage: MPolynomialRing_libsingular(SR, 1, ["x"], "lex") 

Traceback (most recent call last): 

... 

NotImplementedError: polynomials over Symbolic Ring are not supported in Singular 

sage: MPolynomialRing_libsingular(QQ, 0, [], "lex") 

Traceback (most recent call last): 

... 

NotImplementedError: polynomials in 0 variables are not supported in Singular 

sage: MPolynomialRing_libsingular(QQ, -1, [], "lex") 

Traceback (most recent call last): 

... 

ValueError: Multivariate Polynomial Rings must have more than 0 variables. 

""" 

MPolynomialRing_generic.__init__(self, base_ring, n, names, order) 

self._has_singular = True 

assert(n == len(self._names)) 

self.__ngens = n 

self._ring = singular_ring_new(base_ring, n, self._names, order) 

self._zero_element = new_MP(self, NULL) 

cdef MPolynomial_libsingular one = new_MP(self, p_ISet(1, self._ring)) 

self._one_element = one 

self._one_element_poly = one._poly 

# This polynomial ring should belong to Algebras(base_ring). 

# Algebras(...).parent_class, which was called from MPolynomialRing_generic.__init__, 

# tries to provide a conversion from the base ring, if it does not exist. 

# This is for algebras that only do the generic stuff in their initialisation. 

# But here, we want to use PolynomialBaseringInjection. Hence, we need to 

# wipe the memory and construct the conversion from scratch. 

from sage.rings.polynomial.polynomial_element import PolynomialBaseringInjection 

base_inject = PolynomialBaseringInjection(base_ring, self) 

self.register_coercion(base_inject) 

#permanently store a reference to this ring until deallocation works reliably 

permstore.append(self) 

  

def __dealloc__(self): 

r""" 

Deallocate the ring without changing ``currRing`` 

  

TESTS: 

  

This example caused a segmentation fault with a previous version 

of this method:: 

  

sage: import gc 

sage: from sage.rings.polynomial.multi_polynomial_libsingular import MPolynomialRing_libsingular 

sage: R1 = MPolynomialRing_libsingular(GF(5), 2, ('x', 'y'), TermOrder('degrevlex', 2)) 

sage: R2 = MPolynomialRing_libsingular(GF(11), 2, ('x', 'y'), TermOrder('degrevlex', 2)) 

sage: R3 = MPolynomialRing_libsingular(GF(13), 2, ('x', 'y'), TermOrder('degrevlex', 2)) 

sage: _ = gc.collect() 

sage: foo = R1.gen(0) 

sage: del foo 

sage: del R1 

sage: _ = gc.collect() 

sage: del R2 

sage: _ = gc.collect() 

sage: del R3 

sage: _ = gc.collect() 

""" 

if self._ring != NULL: # the constructor did not raise an exception 

singular_ring_delete(self._ring) 

  

def __copy__(self): 

""" 

Copy ``self``. 

  

The ring is unique and immutable, so we do not copy. 

  

TESTS:: 

  

sage: import gc 

sage: from sage.rings.polynomial.multi_polynomial_libsingular import MPolynomialRing_libsingular 

sage: from sage.libs.singular.ring import ring_refcount_dict 

sage: gc.collect() # random output 

sage: n = len(ring_refcount_dict) 

sage: R = MPolynomialRing_libsingular(GF(547), 2, ('x', 'y'), TermOrder('degrevlex', 2)) 

sage: len(ring_refcount_dict) == n + 1 

True 

  

sage: Q = copy(R) # indirect doctest 

sage: p = R.gen(0) ^2+R.gen(1)^2 

sage: q = copy(p) 

sage: del R 

sage: del Q 

sage: del p 

sage: del q 

sage: gc.collect() # random output 

sage: len(ring_refcount_dict) == n 

False 

""" 

return self 

  

def __deepcopy__(self, memo): 

""" 

Deep copy ``self``. 

  

The ring should be immutable, so we do not copy. 

  

TESTS:: 

  

sage: R.<x,y> = GF(547)[] 

sage: R is deepcopy(R) # indirect doctest 

True 

""" 

memo[id(self)] = self 

return self 

  

cpdef _coerce_map_from_(self, other): 

""" 

Return True if and only if there exists a coercion map from 

``other`` to ``self``. 

  

TESTS:: 

  

sage: R.<x,y> = QQ[] 

sage: type(R) 

<type 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomialRing_libsingular'> 

sage: R.has_coerce_map_from(ZZ['t']) 

False 

sage: R.coerce_map_from(ZZ['x']) 

Coercion map: 

From: Univariate Polynomial Ring in x over Integer Ring 

To: Multivariate Polynomial Ring in x, y over Rational Field 

  

""" 

base_ring = self.base_ring() 

  

if isinstance(other, MPolynomialRing_libsingular): 

if self is other: 

return True 

n = other.ngens() 

if(other.base_ring is base_ring and self.ngens() >= n and 

self.variable_names()[:n] == other.variable_names()): 

return True 

elif base_ring.has_coerce_map_from(other._mpoly_base_ring(self.variable_names())): 

return True 

elif isinstance(other, MPolynomialRing_polydict): 

if self == other: 

return True 

elif other.ngens() == 0: 

return True 

elif base_ring.has_coerce_map_from(other._mpoly_base_ring(self.variable_names())): 

return True 

elif is_PolynomialRing(other): 

if base_ring.has_coerce_map_from(other._mpoly_base_ring(self.variable_names())): 

return True 

elif base_ring.has_coerce_map_from(other): 

return True 

  

Element = MPolynomial_libsingular 

  

def _element_constructor_(self, element): 

""" 

Construct a new element in this polynomial ring by converting 

``element`` into ``self`` if possible. 

  

INPUT: 

  

- ``element`` -- several types are supported, see below 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

  

We can coerce elements of self to self:: 

  

sage: P._coerce_(x*y + 1/2) 

x*y + 1/2 

  

We can coerce elements for a ring with the same algebraic properties:: 

  

sage: from sage.rings.polynomial.multi_polynomial_libsingular import MPolynomialRing_libsingular 

sage: R.<x,y,z> = MPolynomialRing_libsingular(QQ,3) 

sage: P == R 

True 

  

sage: P is R 

False 

  

sage: P._coerce_(x*y + 1) 

x*y + 1 

  

We can coerce base ring elements:: 

  

sage: P._coerce_(3/2) 

3/2 

  

and all kinds of integers:: 

  

sage: P._coerce_(ZZ(1)) 

1 

  

sage: P._coerce_(int(1)) 

1 

  

sage: k.<a> = GF(2^8) 

sage: P.<x,y> = PolynomialRing(k,2) 

sage: P._coerce_(a) 

(a) 

  

sage: z = QQ['z'].0 

sage: K.<s> = NumberField(z^2 - 2) 

sage: P.<x,y> = PolynomialRing(K, 2) 

sage: P._coerce_(1/2*s) 

(1/2*s) 

  

TESTS:: 

  

sage: P.<x,y> = PolynomialRing(GF(127)) 

sage: P("111111111111111111111111111111111111111111111111111111111") 

21 

sage: P.<x,y> = PolynomialRing(QQ) 

sage: P("111111111111111111111111111111111111111111111111111111111") 

111111111111111111111111111111111111111111111111111111111 

sage: P("31367566080") 

31367566080 

  

Check if :trac:`7582` is fixed:: 

  

sage: R.<x,y,z> = PolynomialRing(CyclotomicField(2),3) 

sage: R.coerce(1) 

1 

  

Check if :trac:`6160` is fixed:: 

  

sage: x=var('x') 

sage: K.<j> = NumberField(x-1728) 

sage: R.<b,c> = K[] 

sage: R.coerce(1) 

1 

  

Check if coercion from zero variable polynomial rings work 

(:trac:`7951`):: 

  

sage: P = PolynomialRing(QQ,0,'') 

sage: R.<x,y> = QQ[] 

sage: P(5)*x 

5*x 

sage: P = PolynomialRing(ZZ,0,'') 

sage: R.<x,y> = GF(127)[] 

sage: R.coerce(P(5)) 

5 

  

Conversion from strings:: 

  

sage: P.<x,y,z> = QQ[] 

sage: P('x+y + 1/4') 

x + y + 1/4 

  

Coercion from SINGULAR elements:: 

  

sage: P._singular_() 

polynomial ring, over a field, global ordering 

// coefficients: QQ 

// number of vars : 3 

// block 1 : ordering dp 

// : names x y z 

// block 2 : ordering C 

  

sage: P._singular_().set_ring() 

sage: P(singular('x + 3/4')) 

x + 3/4 

  

Coercion from symbolic variables:: 

  

sage: R = QQ['x,y,z'] 

sage: var('x') 

x 

sage: R(x) 

x 

  

Coercion from 'similar' rings, which maps by index:: 

  

sage: P.<x,y,z> = QQ[] 

sage: R.<a,b,c> = ZZ[] 

sage: P(a) 

x 

  

:: 

  

sage: P.<x,y> = QQ[] 

sage: R.<a,b,c> = QQ[] 

sage: R(x) 

a 

  

Coercion from PARI objects:: 

  

sage: P.<x,y,z> = QQ[] 

sage: P(pari('x^2 + y')) 

x^2 + y 

sage: P(pari('x*y')) 

x*y 

  

Coercion from boolean polynomials, also by index:: 

  

sage: B.<x,y,z> = BooleanPolynomialRing(3) 

sage: P.<x,y,z> = QQ[] 

sage: P(B.gen(0)) 

x 

  

If everything else fails, we try to coerce to the base ring:: 

  

sage: R.<x,y,z> = GF(3)[] 

sage: R(1/2) 

-1 

  

Finally, conversions from other polynomial rings which are not 

coercions are provided. Variables are mapped as follows. Say, 

we are mapping an element from `P` to `Q` (this ring). If the 

variables of `P` are a subset of `Q`, we perform a name 

preserving conversion:: 

  

sage: P.<y_2, y_1, z_3, z_2, z_1> = GF(3)[] 

sage: Q = GF(3)['y_4', 'y_3', 'y_2', 'y_1', 'z_5', 'z_4', 'z_3', 'z_2', 'z_1'] 

sage: Q(y_1*z_2^2*z_1) 

y_1*z_2^2*z_1 

  

Otherwise, if `P` has less than or equal the number of 

variables as `Q`, we perform a conversion by index:: 

  

sage: P.<a,b,c> = GF(2)[] 

sage: Q = GF(2)['c','b','d','e'] 

sage: f = Q.convert_map_from(P) 

sage: f(a), f(b), f(c) 

(c, b, d) 

  

:: 

  

sage: P.<a,b,c> = GF(2)[] 

sage: Q = GF(2)['c','b','d'] 

sage: f = Q.convert_map_from(P) 

sage: f(a),f(b),f(c) 

(c, b, d) 

  

In all other cases, we fail:: 

  

sage: P.<a,b,c,f> = GF(2)[] 

sage: Q = GF(2)['c','d','e'] 

sage: f = Q.convert_map_from(P) 

sage: f(a) 

Traceback (most recent call last): 

... 

TypeError: Could not find a mapping of the passed element to this ring. 

  

Coerce in a polydict where a coefficient reduces to 0 but isn't 0. :: 

  

sage: R.<x,y> = QQ[]; S.<xx,yy> = GF(5)[]; S( (5*x*y + x + 17*y)._mpoly_dict_recursive() ) 

xx + 2*yy 

  

Coerce in a polynomial one of whose coefficients reduces to 0. :: 

  

sage: R.<x,y> = QQ[]; S.<xx,yy> = GF(5)[]; S(5*x*y + x + 17*y) 

xx + 2*yy 

  

Some other examples that illustrate the same coercion idea:: 

  

sage: R.<x,y> = ZZ[] 

sage: S.<xx,yy> = GF(25,'a')[] 

sage: S(5*x*y + x + 17*y) 

xx + 2*yy 

  

sage: S.<xx,yy> = Integers(5)[] 

sage: S(5*x*y + x + 17*y) 

xx + 2*yy 

  

See :trac:`5292`:: 

  

sage: R.<x> = QQ[]; S.<q,t> = QQ[]; F = FractionField(S); 

sage: x in S 

False 

sage: x in F 

False 

  

Check if :trac:`8228` is fixed:: 

  

sage: P.<x,y> = Zmod(10)[]; P(0) 

0 

sage: P.<x,y> = Zmod(2^10)[]; P(0) 

0 

  

And :trac:`7597` is fixed if this does not segfault:: 

  

sage: F2 = GF(2) 

sage: F.<x> = GF(2^8) 

sage: R4.<a,b> = PolynomialRing(F) 

sage: R.<u,v> = PolynomialRing(F2) 

sage: P = a 

sage: (P(0,0).polynomial()[0])*u 

0 

sage: P(a,b) 

a 

  

Check that :trac:`15746` is fixed:: 

  

sage: R.<x,y> = GF(7)[] 

sage: R(2^31) 

2 

  

Check that :trac:`17964` is fixed:: 

  

sage: K.<a> = QuadraticField(17) 

sage: Q.<x,y> = K[] 

sage: f = (-3*a)*y + (5*a) 

sage: p = K.primes_above(5)[0] 

sage: R = K.residue_field(p) 

sage: S = R['x','y'] 

sage: S(f) 

(2*abar)*y 

  

""" 

cdef poly *_p 

cdef poly *mon 

cdef poly *El_poly 

cdef ring *_ring = self._ring 

cdef number *_n 

cdef ring *El_ring 

cdef long mpos 

cdef MPolynomial_libsingular Element 

cdef MPolynomialRing_libsingular El_parent 

cdef int i, j 

cdef list ind_map = [] 

cdef int e 

if _ring!=currRing: rChangeCurrRing(_ring) 

  

base_ring = self.base_ring() 

  

if isinstance(element, MPolynomial_libsingular): 

n = (<MPolynomial_libsingular>element)._parent.ngens() 

if element.parent() is self: 

return element 

elif(base_ring is element.base_ring() and 

self.ngens() >= n and 

self.variable_names()[:n] == (<MPolynomial_libsingular>element)._parent.variable_names()): 

if self.term_order() == (<MPolynomial_libsingular>element)._parent.term_order(): 

_p = prCopyR_NoSort((<MPolynomial_libsingular>element)._poly, 

(<MPolynomial_libsingular>element)._parent_ring, 

_ring) 

else: 

_p = prCopyR((<MPolynomial_libsingular>element)._poly, 

(<MPolynomial_libsingular>element)._parent_ring, _ring) 

return new_MP(self, _p) 

elif base_ring.has_coerce_map_from(element.parent()._mpoly_base_ring(self.variable_names())): 

return self(element._mpoly_dict_recursive(self.variable_names(), base_ring)) 

  

elif isinstance(element, MPolynomial_polydict): 

if element.parent() == self: 

_p = p_ISet(0, _ring) 

for (m,c) in element.element().dict().iteritems(): 

mon = p_Init(_ring) 

p_SetCoeff(mon, sa2si(c, _ring), _ring) 

for pos in m.nonzero_positions(): 

overflow_check(m[pos], _ring) 

p_SetExp(mon, pos+1, m[pos], _ring) 

p_Setm(mon, _ring) 

_p = p_Add_q(_p, mon, _ring) 

return new_MP(self, _p) 

elif element.parent().ngens() == 0: 

# zero variable polynomials 

_p = p_NSet(sa2si(base_ring(element[tuple()]), _ring), 

_ring) 

return new_MP(self, _p) 

elif base_ring.has_coerce_map_from(element.parent()._mpoly_base_ring(self.variable_names())): 

return self(element._mpoly_dict_recursive(self.variable_names(), base_ring)) 

  

elif isinstance(element, polynomial_element.Polynomial): 

if base_ring.has_coerce_map_from(element.parent()._mpoly_base_ring(self.variable_names())): 

return self(element._mpoly_dict_recursive(self.variable_names(), base_ring)) 

  

if isinstance(element, CommutativeRingElement): 

# base ring elements 

if element.parent() is base_ring: 

# shortcut for GF(p) 

if isinstance(base_ring, FiniteField_prime_modn): 

_p = p_ISet(int(element) % _ring.cf.ch, _ring) 

else: 

_n = sa2si(element,_ring) 

_p = p_NSet(_n, _ring) 

return new_MP(self, _p) 

# also accepting ZZ 

elif is_IntegerRing(element.parent()): 

if isinstance(base_ring, FiniteField_prime_modn): 

_p = p_ISet(int(element),_ring) 

else: 

_n = sa2si(base_ring(element),_ring) 

_p = p_NSet(_n, _ring) 

return new_MP(self, _p) 

# fall back to base ring 

try: 

element = base_ring._coerce_c(element) 

_n = sa2si(element,_ring) 

_p = p_NSet(_n, _ring) 

return new_MP(self, _p) 

except TypeError: 

pass 

  

elif isinstance(element, int) or isinstance(element, long): 

if isinstance(base_ring, FiniteField_prime_modn): 

_p = p_ISet(element % _ring.cf.ch, _ring) 

else: 

_n = sa2si(base_ring(element), _ring) 

_p = p_NSet(_n, _ring) 

return new_MP(self, _p) 

  

if isinstance(element, (SingularElement, cypari2.gen.Gen)): 

element = str(element) 

  

if isinstance(element, MPolynomial_libsingular) and element.parent() is not self and element.parent() != self: 

variable_names_s = element.parent().variable_names() 

variable_names_t = self.variable_names() 

  

if set(variable_names_s).issubset(variable_names_t): 

for v in variable_names_s: 

ind_map.append(variable_names_t.index(v)+1) 

else: 

ind_map = [i+1 for i in range(_ring.N)] 

  

if element.parent().ngens() <= self.ngens(): 

# Map the variables by indices 

_p = p_ISet(0, _ring) 

K = self.base_ring() 

base = self._base 

Element = <MPolynomial_libsingular>element 

El_poly = Element._poly 

El_parent = Element._parent 

El_ring = Element._parent_ring 

El_base = El_parent._base 

  

while El_poly: 

c = si2sa(p_GetCoeff(El_poly, El_ring), El_ring, El_base) 

try: 

c = K(c) 

except TypeError: 

p_Delete(&_p, _ring) 

raise 

if c: 

mon = p_Init(_ring) 

p_SetCoeff(mon, sa2si(c, _ring), _ring) 

for j from 1 <= j <= El_ring.N: 

e = p_GetExp(El_poly, j, El_ring) 

if e: 

p_SetExp(mon, ind_map[j-1], e, _ring) 

p_Setm(mon, _ring) 

_p = p_Add_q(_p, mon, _ring) 

El_poly = pNext(El_poly) 

return new_MP(self, _p) 

  

if isinstance(element, MPolynomial_polydict): 

variable_names_s = element.parent().variable_names() 

variable_names_t = self.variable_names() 

  

if set(variable_names_s).issubset(variable_names_t): 

for v in variable_names_s: 

ind_map.append(variable_names_t.index(v)+1) 

else: 

ind_map = [i+1 for i in range(_ring.N)] 

  

if element.parent().ngens() <= self.ngens(): 

# Map variables by indices 

_p = p_ISet(0, _ring) 

K = self.base_ring() 

for (m,c) in element.element().dict().iteritems(): 

try: 

c = K(c) 

if not c: continue 

except TypeError: 

p_Delete(&_p, _ring) 

raise 

mon = p_Init(_ring) 

p_SetCoeff(mon, sa2si(c , _ring), _ring) 

for pos in m.nonzero_positions(): 

overflow_check(m[pos], _ring) 

p_SetExp(mon, ind_map[pos], m[pos], _ring) 

p_Setm(mon, _ring) 

_p = p_Add_q(_p, mon, _ring) 

return new_MP(self, _p) 

  

from sage.rings.polynomial.pbori import BooleanPolynomial 

if isinstance(element, BooleanPolynomial): 

if element.constant(): 

if element: 

return self._one_element 

else: 

return self._zero_element 

  

variable_names_s = set(element.parent().variable_names()) 

variable_names_t = self.variable_names() 

  

if variable_names_s.issubset(variable_names_t): 

return eval(str(element),self.gens_dict()) 

  

elif element.parent().ngens() <= self.ngens(): 

Q = element.parent() 

gens_map = dict(zip(Q.variable_names(),self.gens()[:Q.ngens()])) 

return eval(str(element),gens_map) 

  

if isinstance(element, str): 

# let python do the parsing 

d = self.gens_dict() 

if self.base_ring().gen() != 1: 

d[str(self.base_ring().gen())]=self.base_ring().gen() 

try: 

if '/' in element: 

element = sage_eval(element,d) 

else: 

element = element.replace("^","**") 

element = eval(element, d, {}) 

except (SyntaxError, NameError): 

raise TypeError("Could not find a mapping of the passed element to this ring.") 

  

# we need to do this, to make sure that we actually get an 

# element in self. 

return self._coerce_c(element) 

  

if isinstance(element, dict): 

_p = p_ISet(0, _ring) 

K = self.base_ring() 

for (m,c) in element.iteritems(): 

try: 

c = K(c) 

if not c: continue 

except TypeError: 

p_Delete(&_p, _ring) 

raise 

mon = p_Init(_ring) 

p_SetCoeff(mon, sa2si(c , _ring), _ring) 

if len(m) != self.ngens(): 

raise TypeError("tuple key must have same length as ngens") 

for pos from 0 <= pos < len(m): 

if m[pos]: 

overflow_check(m[pos], _ring) 

p_SetExp(mon, pos+1, m[pos], _ring) 

p_Setm(mon, _ring) 

_p = p_Add_q(_p, mon, _ring) 

  

return new_MP(self, _p) 

  

try: #if hasattr(element,'_polynomial_'): 

# SymbolicVariable 

return element._polynomial_(self) 

except AttributeError: 

pass 

  

if is_Macaulay2Element(element): 

return self(element.external_string()) 

try: 

return self(str(element)) 

except TypeError: 

pass 

  

try: 

# now try calling the base ring's __call__ methods 

element = self.base_ring()(element) 

_p = p_NSet(sa2si(element,_ring), _ring) 

return new_MP(self,_p) 

except (TypeError, ValueError): 

raise TypeError("Could not find a mapping of the passed element to this ring.") 

  

def _repr_(self): 

""" 

EXAMPLES:: 

  

sage: P.<x,y> = QQ[] 

sage: P # indirect doctest 

Multivariate Polynomial Ring in x, y over Rational Field 

""" 

varstr = ", ".join(char_to_str(rRingVar(i,self._ring)) 

for i in range(self.__ngens)) 

return "Multivariate Polynomial Ring in %s over %s" % (varstr, self.base_ring()) 

  

def ngens(self): 

""" 

Returns the number of variables in this multivariate polynomial ring. 

  

EXAMPLES:: 

  

sage: P.<x,y> = QQ[] 

sage: P.ngens() 

2 

  

sage: k.<a> = GF(2^16) 

sage: P = PolynomialRing(k,1000,'x') 

sage: P.ngens() 

1000 

""" 

return int(self.__ngens) 

  

def gen(self, int n=0): 

""" 

Returns the ``n``-th generator of this multivariate polynomial 

ring. 

  

INPUT: 

  

- ``n`` -- an integer ``>= 0`` 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: P.gen(),P.gen(1) 

(x, y) 

  

sage: P = PolynomialRing(GF(127),1000,'x') 

sage: P.gen(500) 

x500 

  

sage: P.<SAGE,SINGULAR> = QQ[] # weird names 

sage: P.gen(1) 

SINGULAR 

""" 

cdef poly *_p 

cdef ring *_ring = self._ring 

  

if n < 0 or n >= self.__ngens: 

raise ValueError("Generator not defined.") 

  

rChangeCurrRing(_ring) 

_p = p_ISet(1, _ring) 

p_SetExp(_p, n+1, 1, _ring) 

p_Setm(_p, _ring); 

  

return new_MP(self, _p) 

  

def ideal(self, *gens, **kwds): 

""" 

Create an ideal in this polynomial ring. 

  

INPUT: 

  

- ``*gens`` - list or tuple of generators (or several input arguments) 

  

- ``coerce`` - bool (default: ``True``); this must be a 

keyword argument. Only set it to ``False`` if you are certain 

that each generator is already in the ring. 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: sage.rings.ideal.Katsura(P) 

Ideal (x + 2*y + 2*z - 1, x^2 + 2*y^2 + 2*z^2 - x, 2*x*y + 2*y*z - y) of Multivariate Polynomial Ring in x, y, z over Rational Field 

  

sage: P.ideal([x + 2*y + 2*z-1, 2*x*y + 2*y*z-y, x^2 + 2*y^2 + 2*z^2-x]) 

Ideal (x + 2*y + 2*z - 1, 2*x*y + 2*y*z - y, x^2 + 2*y^2 + 2*z^2 - x) of Multivariate Polynomial Ring in x, y, z over Rational Field 

""" 

coerce = kwds.get('coerce', True) 

if len(gens) == 1: 

gens = gens[0] 

from sage.rings.ideal import is_Ideal 

if is_Ideal(gens): 

if gens.ring() is self: 

return gens 

gens = gens.gens() 

if is_SingularElement(gens): 

gens = list(gens) 

coerce = True 

elif is_Macaulay2Element(gens): 

gens = list(gens) 

coerce = True 

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

gens = [gens] 

if coerce: 

gens = [self(x) for x in gens] # this will even coerce from singular ideals correctly! 

return MPolynomialIdeal(self, gens, coerce=False) 

  

def _macaulay2_(self, macaulay2=macaulay2_default): 

""" 

Create an M2 representation of this polynomial ring if 

Macaulay2 is installed. 

  

INPUT: 

  

- ``macaulay2`` - M2 interpreter (default: ``macaulay2_default``) 

  

EXAMPLES:: 

  

sage: R.<x,y> = ZZ[] 

sage: macaulay2(R) # optional - macaulay2 

ZZ [x, y, MonomialOrder => GRevLex, MonomialSize => 16] 

  

sage: R.<x,y> = QQ[] 

sage: macaulay2(R) # optional - macaulay2, indirect doctest 

QQ [x, y, MonomialOrder => GRevLex, MonomialSize => 16] 

  

sage: R.<x,y> = GF(17)[] 

sage: print(macaulay2(R)) # optional - macaulay2 

ZZ 

-- [x, y, MonomialOrder => GRevLex, MonomialSize => 16] 

17 

""" 

try: 

R = self.__macaulay2 

if R is None or not (R.parent() is macaulay2): 

raise ValueError 

R._check_valid() 

return R 

except (AttributeError, ValueError): 

self.__macaulay2 = self._macaulay2_set_ring(macaulay2) 

return self.__macaulay2 

  

def _macaulay2_set_ring(self, macaulay2=macaulay2_default): 

""" 

Set the associated M2 ring. 

  

INPUT: 

  

- ``macaulay2`` - M2 instance 

  

EXAMPLES:: 

  

sage: P.<x,y> = PolynomialRing(QQ) 

sage: M2 = P._macaulay2_set_ring() # optional - macaulay2 

""" 

if not self.__m2_set_ring_cache is None: 

base_str, gens, order = self.__m2_set_ring_cache 

else: 

if self.base_ring().is_prime_field(): 

if self.characteristic() == 0: 

base_str = "QQ" 

else: 

base_str = "ZZ/" + str(self.characteristic()) 

elif is_IntegerRing(self.base_ring()): 

base_str = "ZZ" 

else: 

raise TypeError("no conversion of to a Macaulay2 ring defined") 

gens = str(self.gens()) 

order = self.term_order().macaulay2_str() 

self.__m2_set_ring_cache = (base_str, gens, order) 

return macaulay2.ring(base_str, gens, order) 

  

def _singular_(self, singular=singular_default): 

""" 

Create a SINGULAR (as in the computer algebra system) 

representation of this polynomial ring. The result is cached. 

  

INPUT: 

  

- ``singular`` - SINGULAR interpreter (default: ``singular_default``) 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: P._singular_() 

polynomial ring, over a field, global ordering 

// coefficients: QQ 

// number of vars : 3 

// block 1 : ordering dp 

// : names x y z 

// block 2 : ordering C 

  

sage: P._singular_() is P._singular_() 

True 

  

sage: P._singular_().name() == P._singular_().name() 

True 

  

sage: k.<a> = GF(3^3) 

sage: P.<x,y,z> = PolynomialRing(k,3) 

sage: P._singular_() 

polynomial ring, over a field, global ordering 

// coefficients: ZZ/3[a]/(a^3-a+1) 

// number of vars : 3 

// block 1 : ordering dp 

// : names x y z 

// block 2 : ordering C 

  

sage: P._singular_() is P._singular_() 

True 

  

sage: P._singular_().name() == P._singular_().name() 

True 

  

TESTS: 

sage: P.<x> = QQ[] 

sage: P._singular_() 

polynomial ring, over a field, global ordering 

// coefficients: QQ 

// number of vars : 1 

// block 1 : ordering lp 

// : names x 

// block 2 : ordering C 

""" 

try: 

R = self.__singular 

if R is None or not (R.parent() is singular): 

raise ValueError 

R._check_valid() 

if not self.base_ring().is_field(): 

return R 

if self.base_ring().is_prime_field(): 

return R 

if self.base_ring().is_finite() \ 

or (isinstance(self.base_ring(), NumberField) and self.base_ring().is_absolute()): 

R.set_ring() #sorry for that, but needed for minpoly 

if singular.eval('minpoly') != "(" + self.__minpoly + ")": 

singular.eval("minpoly=%s"%(self.__minpoly)) 

self.__minpoly = singular.eval('minpoly')[1:-1] # store in correct format 

return R 

except (AttributeError, ValueError): 

return self._singular_init_(singular) 

  

def _singular_init_(self, singular=singular_default): 

""" 

Create a SINGULAR (as in the computer algebra system) 

representation of this polynomial ring. The result is NOT 

cached. 

  

INPUT: 

  

- ``singular`` - SINGULAR interpreter (default: ``singular_default``) 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: P._singular_init_() 

polynomial ring, over a field, global ordering 

// coefficients: QQ 

// number of vars : 3 

// block 1 : ordering dp 

// : names x y z 

// block 2 : ordering C 

sage: P._singular_init_() is P._singular_init_() 

False 

  

sage: P._singular_init_().name() == P._singular_init_().name() 

False 

  

sage: w = var('w') 

sage: R.<x,y> = PolynomialRing(NumberField(w^2+1,'s')) 

sage: singular(R) 

polynomial ring, over a field, global ordering 

// coefficients: QQ[s]/(s^2+1) 

// number of vars : 2 

// block 1 : ordering dp 

// : names x y 

// block 2 : ordering C 

  

sage: R = PolynomialRing(GF(2**8,'a'),10,'x', order='invlex') 

sage: singular(R) 

polynomial ring, over a field, global ordering 

// coefficients: ZZ/2[a]/(a^8+a^4+a^3+a^2+1) 

// number of vars : 10 

// block 1 : ordering rp 

// : names x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 

// block 2 : ordering C 

  

sage: R = PolynomialRing(GF(127),2,'x', order='invlex') 

sage: singular(R) 

polynomial ring, over a field, global ordering 

// coefficients: ZZ/127 

// number of vars : 2 

// block 1 : ordering rp 

// : names x0 x1 

// block 2 : ordering C 

  

sage: R = PolynomialRing(QQ,2,'x', order='invlex') 

sage: singular(R) 

polynomial ring, over a field, global ordering 

// coefficients: QQ 

// number of vars : 2 

// block 1 : ordering rp 

// : names x0 x1 

// block 2 : ordering C 

  

sage: R = PolynomialRing(QQ,2,'x', order='degneglex') 

sage: singular(R) 

polynomial ring, over a field, global ordering 

// coefficients: QQ 

// number of vars : 2 

// block 1 : ordering a 

// : names x0 x1 

// : weights 1 1 

// block 2 : ordering ls 

// : names x0 x1 

// block 3 : ordering C 

  

sage: R = PolynomialRing(QQ,'x') 

sage: singular(R) 

polynomial ring, over a field, global ordering 

// coefficients: QQ 

// number of vars : 1 

// block 1 : ordering lp 

// : names x 

// block 2 : ordering C 

  

sage: R = PolynomialRing(GF(127),'x') 

sage: singular(R) 

polynomial ring, over a field, global ordering 

// coefficients: ZZ/127 

// number of vars : 1 

// block 1 : ordering lp 

// : names x 

// block 2 : ordering C 

  

sage: R = ZZ['x,y'] 

sage: singular(R) 

polynomial ring, over a domain, global ordering 

// coefficients: ZZ 

// number of vars : 2 

// block 1 : ordering dp 

// : names x y 

// block 2 : ordering C 

  

sage: R = IntegerModRing(1024)['x,y'] 

sage: singular(R) 

polynomial ring, over a ring (with zero-divisors), global ordering 

// coefficients: ZZ/(2^10) 

// number of vars : 2 

// block 1 : ordering dp 

// : names x y 

// block 2 : ordering C 

  

sage: R = IntegerModRing(15)['x,y'] 

sage: singular(R) 

polynomial ring, over a ring (with zero-divisors), global ordering 

// coefficients: ZZ/bigint(15) 

// number of vars : 2 

// block 1 : ordering dp 

// : names x y 

// block 2 : ordering C 

  

TESTS:: 

  

sage: P.<x> = QQ[] 

sage: P._singular_init_() 

polynomial ring, over a field, global ordering 

// coefficients: QQ 

// number of vars : 1 

// block 1 : ordering lp 

// : names x 

// block 2 : ordering C 

  

""" 

from sage.functions.other import ceil 

  

if self.ngens()==1: 

_vars = str(self.gen()) 

if "*" in _vars: # 1.000...000*x 

_vars = _vars.split("*")[1] 

order = 'lp' 

else: 

_vars = str(self.gens()) 

order = self.term_order().singular_str()%dict(ngens=self.ngens()) 

  

base_ring = self.base_ring() 

  

if is_RealField(base_ring): 

# singular converts to bits from base_10 in mpr_complex.cc by: 

# size_t bits = 1 + (size_t) ((float)digits * 3.5); 

precision = base_ring.precision() 

digits = ceil((2*precision - 2)/7.0) 

self.__singular = singular.ring("(real,%d,0)"%digits, _vars, order=order) 

  

elif is_ComplexField(base_ring): 

# singular converts to bits from base_10 in mpr_complex.cc by: 

# size_t bits = 1 + (size_t) ((float)digits * 3.5); 

precision = base_ring.precision() 

digits = ceil((2*precision - 2)/7.0) 

self.__singular = singular.ring("(complex,%d,0,I)"%digits, _vars, order=order) 

  

elif base_ring.is_prime_field(): 

self.__singular = singular.ring(self.characteristic(), _vars, order=order) 

  

elif base_ring.is_field() and base_ring.is_finite(): #must be extension field 

gen = str(base_ring.gen()) 

r = singular.ring( "(%s,%s)"%(self.characteristic(),gen), _vars, order=order) 

self.__minpoly = (str(base_ring.modulus()).replace("x",gen)).replace(" ","") 

if singular.eval('minpoly') != "(" + self.__minpoly + ")": 

singular.eval("minpoly=%s"%(self.__minpoly) ) 

self.__minpoly = singular.eval('minpoly')[1:-1] 

self.__singular = r 

  

elif isinstance(base_ring, NumberField) and base_ring.is_absolute(): 

gen = str(base_ring.gen()) 

poly = base_ring.polynomial() 

poly_gen = str(poly.parent().gen()) 

poly_str = str(poly).replace(poly_gen,gen) 

r = singular.ring( "(%s,%s)"%(self.characteristic(),gen), _vars, order=order, check=False) 

self.__minpoly = (poly_str).replace(" ","") 

if singular.eval('minpoly') != "(" + self.__minpoly + ")": 

singular.eval("minpoly=%s"%(self.__minpoly) ) 

self.__minpoly = singular.eval('minpoly')[1:-1] 

self.__singular = r 

  

elif is_IntegerRing(base_ring): 

self.__singular = singular.ring("(integer)", _vars, order=order) 

  

elif is_IntegerModRing(base_ring): 

ch = base_ring.characteristic() 

if ch.is_power_of(2): 

exp = ch.nbits() -1 

self.__singular = singular.ring("(integer,2,%d)"%(exp,), _vars, order=order, check=False) 

else: 

self.__singular = singular.ring("(integer,%d)"%(ch,), _vars, order=order, check=False) 

  

else: 

raise TypeError("no conversion to a Singular ring defined") 

  

return self.__singular 

  

# It is required in cython to redefine __hash__ when __richcmp__ is 

# overloaded. Also just writing 

# __hash__ = CategoryObject.__hash__ 

# doesn't work. 

def __hash__(self): 

""" 

Return a hash for this ring, that is, a hash of the string 

representation of this polynomial ring. 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: hash(P) # somewhat random output 

967902441410893180 # 64-bit 

-1767675994 # 32-bit 

""" 

return CategoryObject.__hash__(self) 

  

def __richcmp__(left, right, int op): 

r""" 

Multivariate polynomial rings are said to be equal if: 

  

- their base rings match, 

- their generator names match and 

- their term orderings match. 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: R.<x,y,z> = QQ[] 

sage: P == R 

True 

  

sage: R.<x,y,z> = GF(127)[] 

sage: P == R 

False 

  

sage: R.<x,y> = QQ[] 

sage: P == R 

False 

  

sage: R.<x,y,z> = PolynomialRing(QQ,order='invlex') 

sage: P == R 

False 

  

TESTS:: 

  

sage: R = QQ['x', 'y']; R 

Multivariate Polynomial Ring in x, y over Rational Field 

sage: R == R 

True 

sage: R == QQ['x','z'] 

False 

""" 

if left is right: 

return rich_to_bool(op, 0) 

  

if not isinstance(right, Parent) or not isinstance(left, Parent): 

# One is not a parent -- not equal and not ordered 

return op == Py_NE 

  

if not isinstance(right, (MPolynomialRing_libsingular, 

MPolynomialRing_polydict_domain)): 

return op == Py_NE 

  

lx = (left.base_ring(), map(str, left.gens()), left.term_order()) 

rx = (right.base_ring(), map(str, right.gens()), right.term_order()) 

return richcmp(lx, rx, op) 

  

def __reduce__(self): 

""" 

Serializes self. 

  

EXAMPLES: 

sage: P.<x,y,z> = PolynomialRing(QQ, order='degrevlex') 

sage: P == loads(dumps(P)) 

True 

  

sage: P.<x,y,z> = PolynomialRing(ZZ, order='degrevlex') 

sage: P == loads(dumps(P)) 

True 

  

sage: P = PolynomialRing(GF(127), names='abc') 

sage: P == loads(dumps(P)) 

True 

  

sage: P = PolynomialRing(GF(2^8,'F'), names='abc') 

sage: P == loads(dumps(P)) 

True 

  

sage: P = PolynomialRing(GF(2^16,'B'), names='abc') 

sage: P == loads(dumps(P)) 

True 

sage: z = QQ['z'].0 

sage: P = PolynomialRing(NumberField(z^2 + 3,'B'), names='abc') 

sage: P == loads(dumps(P)) 

True 

""" 

return unpickle_MPolynomialRing_libsingular, \ 

(self.base_ring(), self.variable_names(), self.term_order()) 

  

def __temporarily_change_names(self, names, latex_names): 

""" 

This is used by the variable names context manager. 

  

EXAMPLES:: 

  

sage: R.<x,y> = QQ[] # indirect doctest 

sage: with localvars(R, 'z,w'): 

....: print(x^3 + y^3 - x*y) 

z^3 + w^3 - z*w 

""" 

cdef ring *_ring = self._ring 

cdef char **_names 

cdef char **_orig_names 

cdef int i 

  

if len(names) != _ring.N: 

raise TypeError("len(names) doesn't equal self.ngens()") 

  

old = self._names, self._latex_names 

(self._names, self._latex_names) = names, latex_names 

  

_names = <char**>omAlloc0(sizeof(char*)*_ring.N) 

for i from 0 <= i < _ring.N: 

_name = str_to_bytes(names[i]) 

_names[i] = omStrDup(_name) 

  

_orig_names = _ring.names 

_ring.names = _names 

  

for i from 0 <= i < _ring.N: 

omFree(_orig_names[i]) 

omFree(_orig_names) 

  

return old 

  

### The following methods are handy for implementing Groebner 

### basis algorithms. They do only superficial type/sanity checks 

### and should be called carefully. 

  

def monomial_quotient(self, MPolynomial_libsingular f, MPolynomial_libsingular g, coeff=False): 

r""" 

Return ``f/g``, where both ``f`` and`` ``g`` are treated as 

monomials. 

  

Coefficients are ignored by default. 

  

INPUT: 

  

- ``f`` - monomial 

- ``g`` - monomial 

- ``coeff`` - divide coefficients as well (default: ``False``) 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: P.monomial_quotient(3/2*x*y,x) 

y 

  

sage: P.monomial_quotient(3/2*x*y,x,coeff=True) 

3/2*y 

  

Note, that `\ZZ` behaves different if ``coeff=True``:: 

  

sage: P.monomial_quotient(2*x,3*x) 

1 

  

sage: P.<x,y> = PolynomialRing(ZZ) 

sage: P.monomial_quotient(2*x,3*x,coeff=True) 

Traceback (most recent call last): 

... 

ArithmeticError: Cannot divide these coefficients. 

  

TESTS:: 

  

sage: R.<x,y,z> = QQ[] 

sage: P.<x,y,z> = QQ[] 

sage: P.monomial_quotient(x*y,x) 

y 

  

sage: P.monomial_quotient(x*y,R.gen()) 

y 

  

sage: P.monomial_quotient(P(0),P(1)) 

0 

  

sage: P.monomial_quotient(P(1),P(0)) 

Traceback (most recent call last): 

... 

ZeroDivisionError 

  

sage: P.monomial_quotient(P(3/2),P(2/3), coeff=True) 

9/4 

  

sage: P.monomial_quotient(x,y) # Note the wrong result 

x*y^65535*z^65535 

  

sage: P.monomial_quotient(x,P(1)) 

x 

  

.. warning:: 

  

Assumes that the head term of f is a multiple of the head 

term of g and return the multiplicant m. If this rule is 

violated, funny things may happen. 

""" 

cdef poly *res 

cdef ring *r = self._ring 

cdef number *n 

cdef number *denom 

  

if self is not f._parent: 

f = self._coerce_c(f) 

if self is not g._parent: 

g = self._coerce_c(g) 

  

if not f._poly: 

return self._zero_element 

if not g._poly: 

raise ZeroDivisionError 

  

if r!=currRing: rChangeCurrRing(r) # pDivide 

res = pDivide(f._poly, g._poly) 

if coeff: 

if r.cf.type == n_unknown or r.cf.cfDivBy(p_GetCoeff(f._poly, r), p_GetCoeff(g._poly, r), r.cf): 

n = r.cf.cfDiv( p_GetCoeff(f._poly, r) , p_GetCoeff(g._poly, r), r.cf) 

p_SetCoeff0(res, n, r) 

else: 

raise ArithmeticError("Cannot divide these coefficients.") 

else: 

p_SetCoeff0(res, n_Init(1, r), r) 

return new_MP(self, res) 

  

def monomial_divides(self, MPolynomial_libsingular a, MPolynomial_libsingular b): 

""" 

Return ``False`` if a does not divide b and ``True`` 

otherwise. 

  

Coefficients are ignored. 

  

INPUT: 

  

- ``a`` -- monomial 

  

- ``b`` -- monomial 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: P.monomial_divides(x*y*z, x^3*y^2*z^4) 

True 

sage: P.monomial_divides(x^3*y^2*z^4, x*y*z) 

False 

  

TESTS:: 

  

sage: P.<x,y,z> = QQ[] 

sage: P.monomial_divides(P(1), P(0)) 

True 

sage: P.monomial_divides(P(1), x) 

True 

""" 

cdef poly *_a 

cdef poly *_b 

cdef ring *_r 

if a._parent is not b._parent: 

b = a._parent._coerce_c(b) 

  

_a = a._poly 

_b = b._poly 

_r = a._parent_ring 

  

if _a == NULL: 

raise ZeroDivisionError 

if _b == NULL: 

return True 

  

if not p_DivisibleBy(_a, _b, _r): 

return False 

else: 

return True 

  

  

def monomial_lcm(self, MPolynomial_libsingular f, MPolynomial_libsingular g): 

""" 

LCM for monomials. Coefficients are ignored. 

  

INPUT: 

  

- ``f`` - monomial 

  

- ``g`` - monomial 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: P.monomial_lcm(3/2*x*y,x) 

x*y 

  

TESTS:: 

  

sage: R.<x,y,z> = QQ[] 

sage: P.<x,y,z> = QQ[] 

sage: P.monomial_lcm(x*y,R.gen()) 

x*y 

  

sage: P.monomial_lcm(P(3/2),P(2/3)) 

1 

  

sage: P.monomial_lcm(x,P(1)) 

x 

""" 

cdef poly *m = p_ISet(1,self._ring) 

  

if self is not f._parent: 

f = self._coerce_c(f) 

if self is not g._parent: 

g = self._coerce_c(g) 

  

if f._poly == NULL: 

if g._poly == NULL: 

return self._zero_element 

else: 

raise ArithmeticError("Cannot compute LCM of zero and nonzero element.") 

if g._poly == NULL: 

raise ArithmeticError("Cannot compute LCM of zero and nonzero element.") 

  

if(self._ring != currRing): rChangeCurrRing(self._ring) 

  

pLcm(f._poly, g._poly, m) 

p_Setm(m, self._ring) 

return new_MP(self,m) 

  

def monomial_reduce(self, MPolynomial_libsingular f, G): 

""" 

Try to find a ``g`` in ``G`` where ``g.lm()`` divides 

``f``. If found ``(flt,g)`` is returned, ``(0,0)`` otherwise, 

where ``flt`` is ``f/g.lm()``. 

  

It is assumed that ``G`` is iterable and contains *only* 

elements in this polynomial ring. 

  

Coefficients are ignored. 

  

INPUT: 

  

- ``f`` - monomial 

- ``G`` - list/set of mpolynomials 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: f = x*y^2 

sage: G = [ 3/2*x^3 + y^2 + 1/2, 1/4*x*y + 2/7, 1/2 ] 

sage: P.monomial_reduce(f,G) 

(y, 1/4*x*y + 2/7) 

  

TESTS:: 

  

sage: P.<x,y,z> = QQ[] 

sage: f = x*y^2 

sage: G = [ 3/2*x^3 + y^2 + 1/2, 1/4*x*y + 2/7, 1/2 ] 

  

sage: P.monomial_reduce(P(0),G) 

(0, 0) 

  

sage: P.monomial_reduce(f,[P(0)]) 

(0, 0) 

""" 

cdef poly *m = f._poly 

cdef ring *r = self._ring 

cdef poly *flt 

  

if not m: 

return f,f 

  

for g in G: 

if isinstance(g, MPolynomial_libsingular) \ 

and (<MPolynomial_libsingular>g) \ 

and g.parent() is self \ 

and p_LmDivisibleBy((<MPolynomial_libsingular>g)._poly, m, r): 

if r!=currRing: rChangeCurrRing(r) # pDivide 

flt = pDivide(f._poly, (<MPolynomial_libsingular>g)._poly) 

#p_SetCoeff(flt, n_Div( p_GetCoeff(f._poly, r) , p_GetCoeff((<MPolynomial_libsingular>g)._poly, r), r), r) 

p_SetCoeff(flt, n_Init(1, r), r) 

return new_MP(self,flt), g 

return self._zero_element,self._zero_element 

  

def monomial_pairwise_prime(self, MPolynomial_libsingular g, MPolynomial_libsingular h): 

""" 

Return ``True`` if ``h`` and ``g`` are pairwise prime. Both 

are treated as monomials. 

  

Coefficients are ignored. 

  

INPUT: 

  

- ``h`` - monomial 

- ``g`` - monomial 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: P.monomial_pairwise_prime(x^2*z^3, y^4) 

True 

  

sage: P.monomial_pairwise_prime(1/2*x^3*y^2, 3/4*y^3) 

False 

  

TESTS:: 

  

sage: Q.<x,y,z> = QQ[] 

sage: P.<x,y,z> = QQ[] 

sage: P.monomial_pairwise_prime(x^2*z^3, Q('y^4')) 

True 

  

sage: P.monomial_pairwise_prime(1/2*x^3*y^2, Q(0)) 

True 

  

sage: P.monomial_pairwise_prime(P(1/2),x) 

False 

""" 

cdef int i 

cdef ring *r 

cdef poly *p 

cdef poly *q 

  

if h._parent is not g._parent: 

g = h._parent._coerce_c(g) 

  

r = h._parent_ring 

p = g._poly 

q = h._poly 

  

if p == NULL: 

if q == NULL: 

return False #GCD(0,0) = 0 

else: 

return True #GCD(x,0) = 1 

elif q == NULL: 

return True # GCD(0,x) = 1 

elif p_IsConstant(p,r) or p_IsConstant(q,r): # assuming a base field 

return False 

  

for i from 1 <= i <= r.N: 

if p_GetExp(p,i,r) and p_GetExp(q,i,r): 

return False 

return True 

  

def monomial_all_divisors(self, MPolynomial_libsingular t): 

""" 

Return a list of all monomials that divide ``t``. 

  

Coefficients are ignored. 

  

INPUT: 

  

- ``t`` - a monomial 

  

OUTPUT: 

a list of monomials 

  

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: P.monomial_all_divisors(x^2*z^3) 

[x, x^2, z, x*z, x^2*z, z^2, x*z^2, x^2*z^2, z^3, x*z^3, x^2*z^3] 

  

ALGORITHM: addwithcarry idea by Toon Segers 

""" 

  

M = list() 

  

cdef ring *_ring = self._ring 

cdef poly *maxvector = t._poly 

cdef poly *tempvector = p_ISet(1, _ring) 

  

pos = 1 

  

while not p_ExpVectorEqual(tempvector, maxvector, _ring): 

tempvector = addwithcarry(tempvector, maxvector, pos, _ring) 

M.append(new_MP(self, p_Copy(tempvector, _ring))) 

return M 

  

def unpickle_MPolynomialRing_libsingular(base_ring, names, term_order): 

""" 

inverse function for ``MPolynomialRing_libsingular.__reduce__`` 

  

EXAMPLES:: 

  

sage: P.<x,y> = PolynomialRing(QQ) 

sage: loads(dumps(P)) is P # indirect doctest 

True 

""" 

from sage.rings.polynomial.polynomial_ring_constructor import _multi_variate 

# If libsingular would be replaced by a different implementation in future 

# sage version, the unpickled ring will belong the new implementation. 

return _multi_variate(base_ring, tuple(names), None, term_order, None) 

  

  

cdef class MPolynomial_libsingular(MPolynomial): 

""" 

A multivariate polynomial implemented using libSINGULAR. 

""" 

def __init__(self, MPolynomialRing_libsingular parent): 

""" 

Construct a zero element in parent. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.multi_polynomial_libsingular import MPolynomial_libsingular 

sage: P = PolynomialRing(GF(32003),3,'x') 

sage: MPolynomial_libsingular(P) 

0 

""" 

self._poly = NULL 

self._parent = parent 

self._parent_ring = singular_ring_reference(parent._ring) 

  

def __dealloc__(self): 

# WARNING: the Cython class self._parent is now no longer accessible! 

if self._poly==NULL: 

# e.g. MPolynomialRing_libsingular._zero_element 

singular_ring_delete(self._parent_ring) 

return 

assert self._parent_ring != NULL # the constructor has no way to raise an exception 

p_Delete(&self._poly, self._parent_ring) 

singular_ring_delete(self._parent_ring) 

  

def __copy__(self): 

""" 

Copy ``self``. 

  

OUTPUT: 

  

A copy. 

  

EXAMPLES:: 

  

sage: F.<a> = GF(7^2) 

sage: R.<x,y> = F[] 

sage: p = a*x^2 + y + a^3; p 

(a)*x^2 + y + (-2*a - 3) 

sage: q = copy(p) 

sage: p == q 

True 

sage: p is q 

False 

sage: lst = [p,q]; 

sage: matrix(ZZ, 2, 2, lambda i,j: bool(lst[i]==lst[j])) 

[1 1] 

[1 1] 

sage: matrix(ZZ, 2, 2, lambda i,j: bool(lst[i] is lst[j])) 

[1 0] 

[0 1] 

""" 

return new_MP(self._parent, p_Copy(self._poly, self._parent_ring)) 

  

def __deepcopy__(self, memo={}): 

""" 

Deep copy ``self`` 

  

TESTS:: 

  

sage: R.<x,y> = QQ[] 

sage: p = x^2 + y^2 

sage: p is deepcopy(p) 

False 

sage: p == deepcopy(p) 

True 

sage: p.parent() is deepcopy(p).parent() 

True 

""" 

cpy = self.__copy__() 

memo[id(self)] = cpy 

return cpy 

  

cpdef MPolynomial_libsingular _new_constant_poly(self, x, MPolynomialRing_libsingular P): 

r""" 

Quickly create a new constant polynomial with value x in the parent P. 

  

ASSUMPTION: 

  

The value x must be an element of the base ring. That assumption is 

not verified. 

  

EXAMPLES:: 

  

sage: R.<x,y> = QQ[] 

sage: x._new_constant_poly(2/1,R) 

2 

  

""" 

if not x: 

return new_MP(P, NULL) 

cdef poly *_p 

singular_polynomial_rmul(&_p, P._one_element_poly, x, P._ring) 

return new_MP(P, _p) 

  

def __call__(self, *x, **kwds): 

""" 

Evaluate this multi-variate polynomial at ``x``, where ``x`` 

is either the tuple of values to substitute in, or one can use 

functional notation ``f(a_0,a_1,a_2, \ldots)`` to evaluate 

``f`` with the ith variable replaced by ``a_i``. 

  

INPUT: 

  

- ``x`` - a list of elements in ``self.parent()`` 

- or ``**kwds`` - a dictionary of ``variable-name:value`` pairs. 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: f = 3/2*x^2*y + 1/7 * y^2 + 13/27 

sage: f(0,0,0) 

13/27 

  

sage: f(1,1,1) 

803/378 

sage: 3/2 + 1/7 + 13/27 

803/378 

  

sage: f(45/2,19/3,1) 

7281167/1512 

  

sage: f(1,2,3).parent() 

Rational Field 

  

TESTS:: 

  

sage: P.<x,y,z> = QQ[] 

sage: P(0)(1,2,3) 

0 

sage: P(3/2)(1,2,3) 

3/2 

  

sage: R.<a,b,y> = QQ[] 

sage: f = a*y^3 + b*y - 3*a*b*y 

sage: f(a=5,b=3,y=10) 

4580 

sage: f(5,3,10) 

4580 

  

See :trac:`8502`:: 

  

sage: x = polygen(QQ) 

sage: K.<t> = NumberField(x^2+47) 

sage: R.<X,Y,Z> = K[] 

sage: f = X+Y+Z 

sage: a = f(t,t,t); a 

3*t 

sage: a.parent() is K 

True 

  

sage: R.<X,Y,Z> = QQ[] 

sage: f = X+Y+Z 

sage: a = f(2,3,4); a 

9 

sage: a.parent() is QQ 

True 

""" 

if len(kwds) > 0: 

f = self.subs(**kwds) 

if len(x) > 0: 

return f(*x) 

else: 

return f 

  

cdef int l = len(x) 

cdef MPolynomialRing_libsingular parent = self._parent 

cdef ring *_ring = parent._ring 

  

if l == 1 and isinstance(x[0], (list, tuple)): 

x = x[0] 

l = len(x) 

  

if l != parent._ring.N: 

raise TypeError("number of arguments does not match number of variables in parent") 

  

try: 

# Attempt evaluation via singular. 

coerced_x = [parent.coerce(e) for e in x] 

except TypeError: 

# give up, evaluate functional 

y = parent.base_ring()(0) 

for (m,c) in self.dict().iteritems(): 

y += c*mul([ x[i]**m[i] for i in m.nonzero_positions()]) 

return y 

  

cdef poly *res # ownership will be transferred to us in the next line 

singular_polynomial_call(&res, self._poly, _ring, coerced_x, MPolynomial_libsingular_get_element) 

res_parent = coercion_model.common_parent(parent._base, *x) 

  

if res == NULL: 

return res_parent(0) 

if p_LmIsConstant(res, _ring): 

sage_res = si2sa( p_GetCoeff(res, _ring), _ring, parent._base ) 

p_Delete(&res, _ring) # sage_res contains copy 

else: 

sage_res = new_MP(parent, res) # pass on ownership of res to sage_res 

  

if parent(sage_res) is not res_parent: 

sage_res = res_parent(sage_res) 

return sage_res 

  

def __hash__(self): 

""" 

This hash incorporates the variable name in an effort to 

respect the obvious inclusions into multi-variable polynomial 

rings. 

  

The tuple algorithm is borrowed from http://effbot.org/zone/python-hash.htm. 

  

EXAMPLES:: 

  

sage: R.<x>=QQ[] 

sage: S.<x,y>=QQ[] 

sage: hash(S(1/2))==hash(1/2) # respect inclusions of the rationals 

True 

sage: hash(S.0)==hash(R.0) # respect inclusions into mpoly rings 

True 

sage: # the point is to make for more flexible dictionary look ups 

sage: d={S.0:12} 

sage: d[R.0] 

12 

""" 

return self._hash_c() 

  

cpdef int _cmp_(left, right) except -2: 

""" 

Compare left and right and return -1, 0, and 1 for <,==, and > 

respectively. 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = PolynomialRing(QQ,order='degrevlex') 

sage: x == x 

True 

  

sage: x > y 

True 

sage: y^2 > x 

True 

  

sage: (2/3*x^2 + 1/2*y + 3) > (2/3*x^2 + 1/4*y + 10) 

True 

  

TESTS:: 

  

sage: P.<x,y,z> = PolynomialRing(QQ, order='degrevlex') 

sage: x > P(0) 

True 

  

sage: P(0) == P(0) 

True 

  

sage: P(0) < P(1) 

True 

  

sage: x > P(1) 

True 

  

sage: 1/2*x < 3/4*x 

True 

  

sage: (x+1) > x 

True 

  

sage: f = 3/4*x^2*y + 1/2*x + 2/7 

sage: f > f 

False 

sage: f < f 

False 

sage: f == f 

True 

  

sage: P.<x,y,z> = PolynomialRing(GF(127), order='degrevlex') 

sage: (66*x^2 + 23) > (66*x^2 + 2) 

True 

""" 

if left is right: 

return 0 

cdef poly *p = (<MPolynomial_libsingular>left)._poly 

cdef poly *q = (<MPolynomial_libsingular>right)._poly 

cdef ring *r = (<MPolynomial_libsingular>left)._parent_ring 

return singular_polynomial_cmp(p, q, r) 

  

cpdef _add_(left, right): 

""" 

Add left and right. 

  

EXAMPLES:: 

  

sage: P.<x,y,z>=PolynomialRing(QQ,3) 

sage: 3/2*x + 1/2*y + 1 #indirect doctest 

3/2*x + 1/2*y + 1 

""" 

cdef ring *r = (<MPolynomial_libsingular>left)._parent_ring 

cdef poly *_p 

singular_polynomial_add(&_p, left._poly, 

(<MPolynomial_libsingular>right)._poly, r) 

return new_MP((<MPolynomial_libsingular>left)._parent, _p) 

  

cpdef _sub_(left, right): 

""" 

Subtract left and right. 

  

EXAMPLES:: 

  

sage: P.<x,y,z>=PolynomialRing(QQ,3) 

sage: 3/2*x - 1/2*y - 1 #indirect doctest 

3/2*x - 1/2*y - 1 

""" 

cdef ring *_ring = (<MPolynomial_libsingular>left)._parent_ring 

cdef poly *_p 

singular_polynomial_sub(&_p, left._poly, 

(<MPolynomial_libsingular>right)._poly, 

_ring) 

return new_MP((<MPolynomial_libsingular>left)._parent, _p) 

  

cpdef _lmul_(self, Element left): 

""" 

Multiply self with a base ring element. 

  

EXAMPLES:: 

  

sage: P.<x,y,z>=PolynomialRing(QQ,3) 

sage: 3/2*x # indirect doctest 

3/2*x 

  

:: 

  

sage: P.<x,y,z>=PolynomialRing(QQ,3) 

sage: (3/2*x - 1/2*y - 1) * (3/2) # indirect doctest 

9/4*x - 3/4*y - 3/2 

""" 

  

cdef ring *_ring = (<MPolynomial_libsingular>self)._parent_ring 

if not left: 

return (<MPolynomial_libsingular>self)._parent._zero_element 

cdef poly *_p 

singular_polynomial_rmul(&_p, self._poly, left, _ring) 

return new_MP((<MPolynomial_libsingular>self)._parent, _p) 

  

cpdef _mul_(left, right): 

""" 

Multiply left and right. 

  

EXAMPLES:: 

  

sage: P.<x,y,z>=PolynomialRing(QQ,3) 

sage: (3/2*x - 1/2*y - 1) * (3/2*x + 1/2*y + 1) # indirect doctest 

9/4*x^2 - 1/4*y^2 - y - 1 

  

sage: P.<x,y> = PolynomialRing(QQ,order='lex') 

sage: (x^2^15) * x^2^15 

Traceback (most recent call last): 

... 

OverflowError: exponent overflow (...) 

""" 

# all currently implemented rings are commutative 

cdef poly *_p 

singular_polynomial_mul(&_p, left._poly, 

(<MPolynomial_libsingular>right)._poly, 

(<MPolynomial_libsingular>left)._parent_ring) 

return new_MP((<MPolynomial_libsingular>left)._parent,_p) 

  

cpdef _div_(left, right_ringelement): 

""" 

Divide left by right 

  

EXAMPLES:: 

  

sage: R.<x,y>=PolynomialRing(QQ,2) 

sage: f = (x + y)/3 # indirect doctest 

sage: f.parent() 

Multivariate Polynomial Ring in x, y over Rational Field 

  

Note that / is still a constructor for elements of the 

fraction field in all cases as long as both arguments have the 

same parent and right is not constant. :: 

  

sage: R.<x,y>=PolynomialRing(QQ,2) 

sage: f = x^3 + y 

sage: g = x 

sage: h = f/g; h 

(x^3 + y)/x 

sage: h.parent() 

Fraction Field of Multivariate Polynomial Ring in x, y over Rational Field 

  

If we divide over `\ZZ` the result is the same as multiplying 

by 1/3 (i.e. base extension). :: 

  

sage: R.<x,y> = ZZ[] 

sage: f = (x + y)/3 

sage: f.parent() 

Multivariate Polynomial Ring in x, y over Rational Field 

sage: f = (x + y) * 1/3 

sage: f.parent() 

Multivariate Polynomial Ring in x, y over Rational Field 

  

But we get a true fraction field if the denominator is not in 

the fraction field of the base ring."" 

  

sage: f = x/y 

sage: f.parent() 

Fraction Field of Multivariate Polynomial Ring in x, y over Integer Ring 

  

Division will fail for non-integral domains:: 

  

sage: P.<x,y> = Zmod(1024)[] 

sage: x/P(3) 

Traceback (most recent call last): 

... 

TypeError: self must be an integral domain. 

  

sage: x/3 

Traceback (most recent call last): 

... 

TypeError: self must be an integral domain. 

  

TESTS:: 

  

sage: R.<x,y>=PolynomialRing(QQ,2) 

sage: x/0 

Traceback (most recent call last): 

... 

ZeroDivisionError: rational division by zero 

""" 

cdef poly *p 

cdef MPolynomial_libsingular right = <MPolynomial_libsingular>right_ringelement 

cdef bint is_field = left._parent._base.is_field() 

if p_IsConstant(right._poly, right._parent_ring): 

if is_field: 

singular_polynomial_div_coeff(&p, left._poly, right._poly, right._parent_ring) 

return new_MP(left._parent, p) 

else: 

return left.change_ring(left.base_ring().fraction_field())/right_ringelement 

else: 

return (left._parent).fraction_field()(left,right_ringelement) 

  

def __pow__(MPolynomial_libsingular self, exp, ignored): 

""" 

Return ``self**(exp)``. 

  

The exponent must be an integer or a rational such that 

the result lies in the same polynomial ring. 

  

EXAMPLES:: 

  

sage: R.<x,y> = PolynomialRing(QQ,2) 

sage: f = x^3 + y 

sage: f^2 

x^6 + 2*x^3*y + y^2 

sage: g = f^(-1); g 

1/(x^3 + y) 

sage: type(g) 

<type 'sage.rings.fraction_field_element.FractionFieldElement'> 

  

sage: P.<x,y> = PolynomialRing(ZZ) 

sage: P(2)**(-1) 

1/2 

  

sage: P.<u,v> = PolynomialRing(QQ, 2) 

sage: u^(1/2) 

Traceback (most recent call last): 

... 

ValueError: (u)^(1/2) does not lie in Multivariate Polynomial Ring 

in u, v over Rational Field 

  

sage: P.<x,y> = PolynomialRing(QQ,order='lex') 

sage: (x+y^2^15)^10 

Traceback (most recent call last): 

.... 

OverflowError: exponent overflow (...) 

  

Test fractional powers (:trac:`22329`):: 

  

sage: P.<R, S> = ZZ[] 

sage: (R^3 + 6*R^2*S + 12*R*S^2 + 8*S^3)^(1/3) 

R + 2*S 

sage: _.parent() 

Multivariate Polynomial Ring in R, S over Integer Ring 

sage: P(4)^(1/2) 

2 

sage: _.parent() 

Multivariate Polynomial Ring in R, S over Integer Ring 

  

sage: (R^2 + 3)^(1/2) 

Traceback (most recent call last): 

... 

ValueError: (R^2 + 3)^(1/2) does not lie in 

Multivariate Polynomial Ring in R, S over Integer Ring 

sage: P(2)^P(2) 

4 

sage: (R + 1)^P(2) 

R^2 + 2*R + 1 

sage: (R + 1)^R 

Traceback (most recent call last): 

... 

TypeError: R is neither an integer nor a rational 

sage: 2^R 

Traceback (most recent call last): 

... 

TypeError: R is neither an integer nor a rational 

""" 

if type(exp) is not Integer: 

try: 

exp = Integer(exp) 

except TypeError: 

try: 

n = Rational(exp) 

except TypeError: 

raise TypeError("{} is neither an integer nor a rational".format(exp)) 

num = n.numerator() 

den = n.denominator() 

  

if self.degree == 0: 

return self.parent()( 

self.constant_coefficient().nth_root(den) ** num) 

return self.nth_root(den) ** num 

  

if exp < 0: 

return 1/(self**(-exp)) 

elif exp == 0: 

return self._parent._one_element 

  

cdef ring *_ring = self._parent_ring 

cdef poly *_p 

singular_polynomial_pow(&_p, self._poly, exp, _ring) 

return new_MP(self._parent, _p) 

  

def __neg__(self): 

""" 

Return ``-self``. 

  

EXAMPLES:: 

  

sage: R.<x,y>=PolynomialRing(QQ,2) 

sage: f = x^3 + y 

sage: -f 

-x^3 - y 

""" 

cdef ring *_ring = self._parent_ring 

  

cdef poly *p 

singular_polynomial_neg(&p, self._poly, _ring) 

return new_MP(self._parent, p) 

  

def _repr_(self): 

""" 

EXAMPLES:: 

  

sage: R.<x,y>=PolynomialRing(QQ,2) 

sage: f = x^3 + y 

sage: f # indirect doctest 

x^3 + y 

""" 

cdef ring *_ring = self._parent_ring 

s = singular_polynomial_str(self._poly, _ring) 

return s 

  

cpdef _repr_short_(self): 

""" 

This is a faster but less pretty way to print polynomials. If 

available it uses the short SINGULAR notation. 

  

EXAMPLES:: 

  

sage: R.<x,y>=PolynomialRing(QQ,2) 

sage: f = x^3 + y 

sage: f._repr_short_() 

'x3+y' 

""" 

cdef ring *_ring = self._parent_ring 

rChangeCurrRing(_ring) 

if _ring.CanShortOut: 

_ring.ShortOut = 1 

s = p_String(self._poly, _ring, _ring) 

_ring.ShortOut = 0 

else: 

s = p_String(self._poly, _ring, _ring) 

return char_to_str(s) 

  

def _latex_(self): 

""" 

Return a polynomial LaTeX representation of this polynomial. 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: f = - 1*x^2*y - 25/27 * y^3 - z^2 

sage: latex(f) # indirect doctest 

- x^{2} y - \frac{25}{27} y^{3} - z^{2} 

""" 

cdef ring *_ring = self._parent_ring 

gens = self.parent().latex_variable_names() 

base = self.parent().base() 

return singular_polynomial_latex(self._poly, _ring, base, gens) 

  

def _repr_with_changed_varnames(self, varnames): 

""" 

Return string representing this polynomial but change the 

variable names to ``varnames``. 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: f = - 1*x^2*y - 25/27 * y^3 - z^2 

sage: print(f._repr_with_changed_varnames(['FOO', 'BAR', 'FOOBAR'])) 

-FOO^2*BAR - 25/27*BAR^3 - FOOBAR^2 

""" 

return singular_polynomial_str_with_changed_varnames(self._poly, self._parent_ring, varnames) 

  

def degree(self, MPolynomial_libsingular x=None, int std_grading=False): 

""" 

Return the maximal degree of this polynomial in ``x``, where 

``x`` must be one of the generators for the parent of this 

polynomial. 

  

INPUT: 

  

- ``x`` - (default: ``None``) a multivariate polynomial which is (or 

coerces to) a generator of the parent of self. If ``x`` is ``None``, 

return the total degree, which is the maximum degree of any monomial. 

Note that a matrix term ordering alters the grading of the generators 

of the ring; see the tests below. To avoid this behavior, use either 

``exponents()`` for the exponents themselves, or the optional 

argument ``std_grading=False``. 

  

OUTPUT: 

integer 

  

EXAMPLES:: 

  

sage: R.<x, y> = QQ[] 

sage: f = y^2 - x^9 - x 

sage: f.degree(x) 

9 

sage: f.degree(y) 

2 

sage: (y^10*x - 7*x^2*y^5 + 5*x^3).degree(x) 

3 

sage: (y^10*x - 7*x^2*y^5 + 5*x^3).degree(y) 

10 

  

TESTS:: 

  

sage: P.<x, y> = QQ[] 

sage: P(0).degree(x) 

-1 

sage: P(1).degree(x) 

0 

  

With a matrix term ordering, the grading of the generators is 

determined by the first row of the matrix. This affects the behavior 

of ``degree()`` when no variable is specified. 

To evaluate the degree with a standard grading, use the optional 

argument ``std_grading=True``. 

  

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

sage: R.<x,y,z> = PolynomialRing(QQ,3,order=tord) 

sage: (x^3*y+x*z^4).degree() 

9 

sage: (x^3*y+x*z^4).degree(std_grading=True) 

5 

sage: x.degree(x), y.degree(y), z.degree(z) 

(1, 1, 1) 

  

The following example is inspired by :trac:`11652`:: 

  

sage: R.<p,q,t> = ZZ[] 

sage: poly = p+q^2+t^3 

sage: poly = poly.polynomial(t)[0] 

sage: poly 

q^2 + p 

  

There is no canonical coercion from ``R`` to the parent of ``poly``, so 

this doesn't work:: 

  

sage: poly.degree(q) 

Traceback (most recent call last): 

... 

TypeError: argument must canonically coerce to parent 

  

Using a non-canonical coercion does work, but we require this 

to be done explicitly, since it can lead to confusing results 

if done automatically:: 

  

sage: poly.degree(poly.parent()(q)) 

2 

sage: poly.degree(poly.parent()(p)) 

1 

sage: T.<x,y> = ZZ[] 

sage: poly.degree(poly.parent()(x)) # noncanonical coercions can be confusing 

1 

  

The argument to degree has to be a generator:: 

  

sage: pp = poly.parent().gen(0) 

sage: poly.degree(pp) 

1 

sage: poly.degree(pp+1) 

Traceback (most recent call last): 

... 

TypeError: argument must be a generator 

  

Canonical coercions are used:: 

  

sage: S = ZZ['p,q'] 

sage: poly.degree(S.0) 

1 

sage: poly.degree(S.1) 

2 

""" 

cdef ring *r = self._parent_ring 

cdef poly *p = self._poly 

if not x: 

if not std_grading: 

return singular_polynomial_deg(p,NULL,r) 

else: 

return self.total_degree(std_grading=True) 

  

if not x.parent() is self.parent(): 

try: 

x = self.parent().coerce(x) 

except TypeError: 

raise TypeError("argument must canonically coerce to parent") 

if not x.is_generator(): 

raise TypeError("argument must be a generator") 

  

return singular_polynomial_deg(p, x._poly, r) 

  

def total_degree(self, int std_grading=False): 

""" 

Return the total degree of ``self``, which is the maximum degree 

of all monomials in ``self``. 

  

EXAMPLES:: 

  

sage: R.<x,y,z> = QQ[] 

sage: f=2*x*y^3*z^2 

sage: f.total_degree() 

6 

sage: f=4*x^2*y^2*z^3 

sage: f.total_degree() 

7 

sage: f=99*x^6*y^3*z^9 

sage: f.total_degree() 

18 

sage: f=x*y^3*z^6+3*x^2 

sage: f.total_degree() 

10 

sage: f=z^3+8*x^4*y^5*z 

sage: f.total_degree() 

10 

sage: f=z^9+10*x^4+y^8*x^2 

sage: f.total_degree() 

10 

  

TESTS:: 

  

sage: R.<x,y,z> = QQ[] 

sage: R(0).total_degree() 

-1 

sage: R(1).total_degree() 

0 

  

With a matrix term ordering, the grading changes. 

To evaluate the total degree using the standard grading, 

use the optional argument``std_grading=True``:: 

  

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

sage: R.<x,y,z> = PolynomialRing(QQ,3,order=tord) 

sage: (x^2*y).total_degree() 

6 

sage: (x^2*y).total_degree(std_grading=True) 

3 

""" 

cdef int i, result 

cdef poly *p = self._poly 

cdef ring *r = self._parent_ring 

  

if not std_grading: 

return singular_polynomial_deg(p,NULL,r) 

else: 

result = 0 

while p: 

result = max(result, sum([p_GetExp(p,i,r) for i in xrange(1,r.N+1)])) 

p = pNext(p) 

return result 

  

def degrees(self): 

""" 

Returns a tuple with the maximal degree of each variable in 

this polynomial. The list of degrees is ordered by the order 

of the generators. 

  

EXAMPLES:: 

  

sage: R.<y0,y1,y2> = PolynomialRing(QQ,3) 

sage: q = 3*y0*y1*y1*y2; q 

3*y0*y1^2*y2 

sage: q.degrees() 

(1, 2, 1) 

sage: (q + y0^5).degrees() 

(5, 2, 1) 

""" 

cdef poly *p = self._poly 

cdef ring *r = self._parent_ring 

cdef int i 

cdef list d = [0 for _ in range(r.N)] 

while p: 

for i from 0 <= i < r.N: 

d[i] = max(d[i],p_GetExp(p, i+1, r)) 

p = pNext(p) 

return tuple(d) 

  

def coefficient(self, degrees): 

""" 

Return the coefficient of the variables with the degrees 

specified in the python dictionary ``degrees``. 

Mathematically, this is the coefficient in the base ring 

adjoined by the variables of this ring not listed in 

``degrees``. However, the result has the same parent as this 

polynomial. 

  

This function contrasts with the function 

``monomial_coefficient`` which returns the coefficient in the 

base ring of a monomial. 

  

INPUT: 

  

- ``degrees`` - Can be any of: 

- a dictionary of degree restrictions 

- a list of degree restrictions (with None in the unrestricted variables) 

- a monomial (very fast, but not as flexible) 

  

OUTPUT: 

element of the parent of this element. 

  

.. NOTE:: 

  

For coefficients of specific monomials, look at :meth:`monomial_coefficient`. 

  

EXAMPLES:: 

  

sage: R.<x,y> = QQ[] 

sage: f=x*y+y+5 

sage: f.coefficient({x:0,y:1}) 

1 

sage: f.coefficient({x:0}) 

y + 5 

sage: f=(1+y+y^2)*(1+x+x^2) 

sage: f.coefficient({x:0}) 

y^2 + y + 1 

sage: f.coefficient([0,None]) 

y^2 + y + 1 

sage: f.coefficient(x) 

y^2 + y + 1 

  

  

Be aware that this may not be what you think! The physical 

appearance of the variable x is deceiving -- particularly if 

the exponent would be a variable. :: 

  

sage: f.coefficient(x^0) # outputs the full polynomial 

x^2*y^2 + x^2*y + x*y^2 + x^2 + x*y + y^2 + x + y + 1 

sage: R.<x,y> = GF(389)[] 

sage: f=x*y+5 

sage: c=f.coefficient({x:0,y:0}); c 

5 

sage: parent(c) 

Multivariate Polynomial Ring in x, y over Finite Field of size 389 

  

AUTHOR: 

  

- Joel B. Mohler (2007.10.31) 

""" 

cdef poly *_degrees = <poly*>0 

cdef poly *p = self._poly 

cdef ring *r = self._parent_ring 

cdef poly *newp = p_ISet(0,r) 

cdef poly *newptemp 

cdef int i 

cdef int flag 

cdef int gens = self._parent.ngens() 

cdef int *exps = <int*>sig_malloc(sizeof(int)*gens) 

for i from 0<=i<gens: 

exps[i] = -1 

  

if isinstance(degrees, MPolynomial_libsingular) \ 

and self._parent is (<MPolynomial_libsingular>degrees)._parent: 

_degrees = (<MPolynomial_libsingular>degrees)._poly 

if pLength(_degrees) != 1: 

raise TypeError("degrees must be a monomial") 

for i from 0<=i<gens: 

if p_GetExp(_degrees,i+1,r)!=0: 

exps[i] = p_GetExp(_degrees,i+1,r) 

elif isinstance(degrees, list): 

for i from 0<=i<gens: 

if degrees[i] is None: 

exps[i] = -1 

else: 

exps[i] = int(degrees[i]) 

elif isinstance(degrees, dict): 

# Extract the ordered list of degree specifications from the dictionary 

poly_vars = self.parent().gens() 

for i from 0<=i<gens: 

try: 

exps[i] = degrees[poly_vars[i]] 

except KeyError: 

pass 

else: 

raise TypeError("The input degrees must be a dictionary of variables to exponents.") 

  

# Extract the monomials that match the specifications 

while(p): 

flag = 0 

for i from 0<=i<gens: 

if exps[i] != -1 and p_GetExp(p,i+1,r)!=exps[i]: 

flag = 1 

if flag == 0: 

newptemp = p_LmInit(p,r) 

p_SetCoeff(newptemp,n_Copy(p_GetCoeff(p,r),r),r) 

for i from 0<=i<gens: 

if exps[i] != -1: 

p_SetExp(newptemp,i+1,0,r) 

p_Setm(newptemp,r) 

newp = p_Add_q(newp,newptemp,r) 

p = pNext(p) 

  

sig_free(exps) 

return new_MP(self._parent, newp) 

  

def monomial_coefficient(self, MPolynomial_libsingular mon): 

""" 

Return the coefficient in the base ring of the monomial mon in 

``self``, where mon must have the same parent as self. 

  

This function contrasts with the function ``coefficient`` 

which returns the coefficient of a monomial viewing this 

polynomial in a polynomial ring over a base ring having fewer 

variables. 

  

INPUT: 

  

- ``mon`` - a monomial 

  

OUTPUT: 

  

coefficient in base ring 

  

.. SEEALSO:: 

  

For coefficients in a base ring of fewer variables, 

look at ``coefficient``. 

  

EXAMPLES:: 

  

sage: P.<x,y> = QQ[] 

  

The parent of the return is a member of the base ring. 

sage: f = 2 * x * y 

sage: c = f.monomial_coefficient(x*y); c 

2 

sage: c.parent() 

Rational Field 

  

sage: f = y^2 + y^2*x - x^9 - 7*x + 5*x*y 

sage: f.monomial_coefficient(y^2) 

1 

sage: f.monomial_coefficient(x*y) 

5 

sage: f.monomial_coefficient(x^9) 

-1 

sage: f.monomial_coefficient(x^10) 

0 

""" 

cdef poly *p = self._poly 

cdef poly *m = mon._poly 

cdef ring *r = self._parent_ring 

  

if not mon._parent is self._parent: 

raise TypeError("mon must have same parent as self.") 

  

while(p): 

if p_ExpVectorEqual(p, m, r) == 1: 

return si2sa(p_GetCoeff(p, r), r, self._parent._base) 

p = pNext(p) 

  

return self._parent._base._zero_element 

  

def dict(self): 

""" 

Return a dictionary representing self. This dictionary is in 

the same format as the generic MPolynomial: The dictionary 

consists of ``ETuple:coefficient`` pairs. 

  

EXAMPLES:: 

  

sage: R.<x,y,z> = QQ[] 

sage: f=2*x*y^3*z^2 + 1/7*x^2 + 2/3 

sage: f.dict() 

{(0, 0, 0): 2/3, (1, 3, 2): 2, (2, 0, 0): 1/7} 

""" 

cdef poly *p 

cdef ring *r = self._parent_ring 

cdef int n 

cdef int v 

if r!=currRing: rChangeCurrRing(r) 

base = self._parent._base 

p = self._poly 

pd = dict() 

while p: 

d = dict() 

for v from 1 <= v <= r.N: 

n = p_GetExp(p,v,r) 

if n!=0: 

d[v-1] = n 

  

pd[ETuple(d,r.N)] = si2sa(p_GetCoeff(p, r), r, base) 

  

p = pNext(p) 

return pd 

  

cpdef long number_of_terms(self): 

""" 

Return the number of non-zero coefficients of this polynomial. 

  

This is also called weight, :meth:`hamming_weight` or sparsity. 

  

EXAMPLES:: 

  

sage: R.<x, y> = ZZ[] 

sage: f = x^3 - y 

sage: f.number_of_terms() 

2 

sage: R(0).number_of_terms() 

0 

sage: f = (x+y)^100 

sage: f.number_of_terms() 

101 

  

The method :meth:`hamming_weight` is an alias:: 

  

sage: f.hamming_weight() 

101 

""" 

cdef long w = 0 

p = self._poly 

while p: 

p = pNext(p) 

w += 1 

return w 

  

hamming_weight = number_of_terms 

  

cdef long _hash_c(self) except -1: 

""" 

See ``self.__hash__`` 

""" 

cdef poly *p = self._poly 

cdef ring *r = self._parent_ring 

cdef int n 

cdef int v 

if r!=currRing: rChangeCurrRing(r) 

base = self._parent._base 

cdef long result = 0 # store it in a c-int and just let the overflowing additions wrap 

cdef long result_mon 

var_name_hash = [hash(vn) for vn in self._parent.variable_names()] 

cdef long c_hash 

while p: 

c_hash = hash(si2sa(p_GetCoeff(p, r), r, base)) 

if c_hash != 0: # this is always going to be true, because we are sparse (correct?) 

# Hash (self[i], gen_a, exp_a, gen_b, exp_b, gen_c, exp_c, ...) as a tuple according to the algorithm. 

# I omit gen,exp pairs where the exponent is zero. 

result_mon = c_hash 

for v from 1 <= v <= r.N: 

n = p_GetExp(p,v,r) 

if n!=0: 

result_mon = (1000003 * result_mon) ^ var_name_hash[v-1] 

result_mon = (1000003 * result_mon) ^ n 

result += result_mon 

  

p = pNext(p) 

if result == -1: 

return -2 

return result 

  

def __getitem__(self,x): 

""" 

Same as ``self.monomial_coefficient`` but for exponent vectors. 

  

INPUT: 

  

- ``x`` - a tuple or, in case of a single-variable MPolynomial 

ring x can also be an integer. 

  

EXAMPLES:: 

  

sage: R.<x, y> = QQ[] 

sage: f = -10*x^3*y + 17*x*y 

sage: f[3,1] 

-10 

sage: f[1,1] 

17 

sage: f[0,1] 

0 

  

sage: R.<x> = PolynomialRing(GF(7), implementation="singular"); R 

Multivariate Polynomial Ring in x over Finite Field of size 7 

sage: f = 5*x^2 + 3; f 

-2*x^2 + 3 

sage: f[2] 

5 

""" 

cdef poly *m 

cdef poly *p = self._poly 

cdef ring *r = self._parent_ring 

cdef int i 

  

if isinstance(x, MPolynomial_libsingular): 

return self.monomial_coefficient(x) 

if not isinstance(x, tuple): 

try: 

x = tuple(x) 

except TypeError: 

x = (x,) 

  

if len(x) != self._parent.ngens(): 

raise TypeError("x must have length self.ngens()") 

  

m = p_ISet(1,r) 

i = 1 

for e in x: 

overflow_check(e, r) 

p_SetExp(m, i, int(e), r) 

i += 1 

p_Setm(m, r) 

  

while(p): 

if p_ExpVectorEqual(p, m, r) == 1: 

p_Delete(&m,r) 

return si2sa(p_GetCoeff(p, r), r, self._parent._base) 

p = pNext(p) 

  

p_Delete(&m,r) 

return self._parent._base._zero_element 

  

def exponents(self, as_ETuples=True): 

""" 

Return the exponents of the monomials appearing in this 

polynomial. 

  

INPUT: 

  

- ``as_ETuples`` - (default: ``True``) if true returns the result as an list of ETuples 

otherwise returns a list of tuples 

  

  

EXAMPLES:: 

  

sage: R.<a,b,c> = QQ[] 

sage: f = a^3 + b + 2*b^2 

sage: f.exponents() 

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

sage: f.exponents(as_ETuples=False) 

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

""" 

cdef poly *p = self._poly 

cdef ring *r = self._parent_ring 

cdef int v 

cdef list pl, ml 

  

pl = list() 

ml = list(xrange(r.N)) 

while p: 

for v from 1 <= v <= r.N: 

ml[v-1] = p_GetExp(p,v,r) 

  

if as_ETuples: 

pl.append(ETuple(ml)) 

else: 

pl.append(tuple(ml)) 

  

p = pNext(p) 

return pl 

  

def inverse_of_unit(self): 

""" 

Return the inverse of this polynomial if it is a unit. 

  

EXAMPLES:: 

  

sage: R.<x,y> = QQ[] 

sage: x.inverse_of_unit() 

Traceback (most recent call last): 

... 

ArithmeticError: Element is not a unit. 

  

sage: R(1/2).inverse_of_unit() 

2 

""" 

cdef ring *_ring = self._parent_ring 

if(_ring != currRing): rChangeCurrRing(_ring) 

  

if not (p_IsUnit(self._poly,_ring)): 

raise ArithmeticError("Element is not a unit.") 

  

sig_on() 

cdef MPolynomial_libsingular r = new_MP(self._parent, p_NSet(n_Invers(p_GetCoeff(self._poly, _ring),_ring.cf),_ring)) 

sig_off() 

return r 

  

def is_homogeneous(self): 

""" 

Return ``True`` if this polynomial is homogeneous. 

  

EXAMPLES:: 

  

sage: P.<x,y> = PolynomialRing(RationalField(), 2) 

sage: (x+y).is_homogeneous() 

True 

sage: (x.parent()(0)).is_homogeneous() 

True 

sage: (x+y^2).is_homogeneous() 

False 

sage: (x^2 + y^2).is_homogeneous() 

True 

sage: (x^2 + y^2*x).is_homogeneous() 

False 

sage: (x^2*y + y^2*x).is_homogeneous() 

True 

""" 

cdef ring *_ring = self._parent_ring 

if(_ring != currRing): rChangeCurrRing(_ring) 

return bool(p_IsHomogeneous(self._poly,_ring)) 

  

cpdef _homogenize(self, int var): 

""" 

Return ``self`` if ``self`` is homogeneous. Otherwise return 

a homogenized polynomial constructed by modifying the degree 

of the variable with index ``var``. 

  

INPUT: 

  

- ``var`` - an integer indicating which variable to use to 

homogenize (``0 <= var < parent(self).ngens()``) 

  

OUTPUT: 

a multivariate polynomial 

  

EXAMPLES:: 

  

sage: P.<x,y> = QQ[] 

sage: f = x^2 + y + 1 + 5*x*y^10 

sage: g = f.homogenize('z'); g # indirect doctest 

5*x*y^10 + x^2*z^9 + y*z^10 + z^11 

sage: g.parent() 

Multivariate Polynomial Ring in x, y, z over Rational Field 

sage: f._homogenize(0) 

2*x^11 + x^10*y + 5*x*y^10 

  

SEE: ``self.homogenize`` 

""" 

cdef ring *_ring = self._parent_ring 

cdef MPolynomialRing_libsingular parent = self._parent 

cdef MPolynomial_libsingular f 

  

if self.is_homogeneous(): 

return self 

  

if(_ring != currRing): rChangeCurrRing(_ring) 

  

if var < parent._ring.N: 

return new_MP(parent, p_Homogen(self._poly, var+1, _ring)) 

else: 

raise TypeError("var must be < self.parent().ngens()") 

  

def is_monomial(self): 

""" 

Return ``True`` if this polynomial is a monomial. A monomial 

is defined to be a product of generators with coefficient 1. 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = PolynomialRing(QQ) 

sage: x.is_monomial() 

True 

sage: (2*x).is_monomial() 

False 

sage: (x*y).is_monomial() 

True 

sage: (x*y + x).is_monomial() 

False 

""" 

cdef poly *_p 

cdef ring *_ring 

cdef number *_n 

_ring = self._parent_ring 

  

if self._poly == NULL: 

return True 

  

if(_ring != currRing): rChangeCurrRing(_ring) 

  

_p = p_Head(self._poly, _ring) 

_n = p_GetCoeff(_p, _ring) 

  

ret = bool((not self._poly.next) and _ring.cf.cfIsOne(_n,_ring.cf)) 

  

p_Delete(&_p, _ring) 

return ret 

  

def subs(self, fixed=None, **kw): 

""" 

Fixes some given variables in a given multivariate polynomial 

and returns the changed multivariate polynomials. The 

polynomial itself is not affected. The variable,value pairs 

for fixing are to be provided as dictionary of the form 

``{variable:value}``. 

  

This is a special case of evaluating the polynomial with some 

of the variables constants and the others the original 

variables, but should be much faster if only few variables are 

to be fixed. 

  

INPUT: 

  

- ``fixed`` - (optional) dict with variable:value pairs 

- ``**kw`` - names parameters 

  

OUTPUT: 

a new multivariate polynomial 

  

EXAMPLES:: 

  

sage: R.<x,y> = QQ[] 

sage: f = x^2 + y + x^2*y^2 + 5 

sage: f(5,y) 

25*y^2 + y + 30 

sage: f.subs({x:5}) 

25*y^2 + y + 30 

sage: f.subs(x=5) 

25*y^2 + y + 30 

  

sage: P.<x,y,z> = PolynomialRing(GF(2),3) 

sage: f = x + y + 1 

sage: f.subs({x:y+1}) 

0 

sage: f.subs(x=y) 

1 

sage: f.subs(x=x) 

x + y + 1 

sage: f.subs({x:z}) 

y + z + 1 

sage: f.subs(x=z+1) 

y + z 

  

sage: f.subs(x=1/y) 

(y^2 + y + 1)/y 

sage: f.subs({x:1/y}) 

(y^2 + y + 1)/y 

  

The parameters are subsituted in order and without side effects:: 

  

sage: R.<x,y>=QQ[] 

sage: g=x+y 

sage: g.subs({x:x+1,y:x*y}) 

x*y + x + 1 

sage: g.subs({x:x+1}).subs({y:x*y}) 

x*y + x + 1 

sage: g.subs({y:x*y}).subs({x:x+1}) 

x*y + x + y + 1 

  

:: 

  

sage: R.<x,y> = QQ[] 

sage: f = x + 2*y 

sage: f.subs(x=y,y=x) 

2*x + y 

  

TESTS:: 

  

sage: P.<x,y,z> = QQ[] 

sage: f = y 

sage: f.subs({y:x}).subs({x:z}) 

z 

  

We test that we change the ring even if there is nothing to do:: 

  

sage: P = QQ['x,y'] 

sage: x = var('x') 

sage: parent(P.zero() / x) 

Symbolic Ring 

  

We are catching overflows:: 

  

sage: R.<x,y> = QQ[] 

sage: n=100; f = x^n 

sage: try: 

....: f.subs(x = x^n) 

....: print("no overflow") 

....: except OverflowError: 

....: print("overflow") 

x^10000 

no overflow 

  

sage: n=1000; 

sage: try: 

....: f = x^n 

....: f.subs(x = x^n) 

....: print("no overflow") 

....: except OverflowError: 

....: print("overflow") 

overflow 

  

Check that there is no more segmentation fault if the polynomial gets 0 

in the middle of a substitution (:trac:`17785`):: 

  

sage: R.<x,y,z> = QQ[] 

sage: for vx in [0,x,y,z]: 

....: for vy in [0,x,y,z]: 

....: for vz in [0,x,y,z]: 

....: d = {x:vx, y:vy, z:vz} 

....: ds = {'x': vx, 'y': vy, 'z': vz} 

....: assert x.subs(d) == x.subs(**ds) == vx 

....: assert y.subs(d) == y.subs(**ds) == vy 

....: assert z.subs(d) == z.subs(**ds) == vz 

....: assert (x+y).subs(d) == (x+y).subs(**ds) == vx+vy 

""" 

cdef int mi, i, need_map, try_symbolic 

  

cdef unsigned long degree = 0 

cdef MPolynomialRing_libsingular parent = self._parent 

cdef ring *_ring = parent._ring 

  

if(_ring != currRing): rChangeCurrRing(_ring) 

  

cdef poly *_p = p_Copy(self._poly, _ring) 

cdef poly *_f 

  

cdef ideal *to_id = idInit(_ring.N,1) 

cdef ideal *from_id 

cdef ideal *res_id 

need_map = 0 

try_symbolic = 0 

  

if _p == NULL: 

# the polynomial is 0. There is nothing to do except to change the 

# ring 

try_symbolic = 1 

  

if not try_symbolic and fixed is not None: 

for m,v in fixed.iteritems(): 

if isinstance(m, (int, Integer)): 

mi = m+1 

elif isinstance(m,MPolynomial_libsingular) and m.parent() is parent: 

for i from 0 < i <= _ring.N: 

if p_GetExp((<MPolynomial_libsingular>m)._poly, i, _ring) != 0: 

mi = i 

break 

if i > _ring.N: 

id_Delete(&to_id, _ring) 

p_Delete(&_p, _ring) 

raise TypeError("key does not match") 

else: 

id_Delete(&to_id, _ring) 

p_Delete(&_p, _ring) 

raise TypeError("keys do not match self's parent") 

try: 

v = parent._coerce_c(v) 

except TypeError: 

try_symbolic = 1 

break 

_f = (<MPolynomial_libsingular>v)._poly 

if p_IsConstant(_f, _ring): 

singular_polynomial_subst(&_p, mi-1, _f, _ring) 

else: 

need_map = 1 

degree = <unsigned long>p_GetExp(_p, mi, _ring) * <unsigned long>p_GetMaxExp(_f, _ring) 

if degree > _ring.bitmask: 

id_Delete(&to_id, _ring) 

p_Delete(&_p, _ring) 

raise OverflowError("exponent overflow (%d)"%(degree)) 

to_id.m[mi-1] = p_Copy(_f, _ring) 

  

if _p == NULL: 

# polynomial becomes 0 after some substitution 

try_symbolic = 1 

break 

  

cdef dict gd 

  

if not try_symbolic: 

gd = parent.gens_dict() 

for m,v in kw.iteritems(): 

m = gd[m] 

for i from 0 < i <= _ring.N: 

if p_GetExp((<MPolynomial_libsingular>m)._poly, i, _ring) != 0: 

mi = i 

break 

if i > _ring.N: 

id_Delete(&to_id, _ring) 

p_Delete(&_p, _ring) 

raise TypeError("key does not match") 

try: 

v = parent._coerce_c(v) 

except TypeError: 

try_symbolic = 1 

break 

_f = (<MPolynomial_libsingular>v)._poly 

if p_IsConstant(_f, _ring): 

singular_polynomial_subst(&_p, mi-1, _f, _ring) 

else: 

if to_id.m[mi-1] != NULL: 

p_Delete(&to_id.m[mi-1],_ring) 

to_id.m[mi-1] = p_Copy(_f, _ring) 

degree = <unsigned long>p_GetExp(_p, mi, _ring) * <unsigned long>p_GetMaxExp(_f, _ring) 

if degree > _ring.bitmask: 

id_Delete(&to_id, _ring) 

p_Delete(&_p, _ring) 

raise OverflowError("exponent overflow (%d)"%(degree)) 

need_map = 1 

  

if _p == NULL: 

# the polynomial is 0 

try_symbolic = 1 

break 

  

if need_map: 

for mi from 0 <= mi < _ring.N: 

if to_id.m[mi] == NULL: 

to_id.m[mi] = p_ISet(1,_ring) 

p_SetExp(to_id.m[mi], mi+1, 1, _ring) 

p_Setm(to_id.m[mi], _ring) 

  

from_id=idInit(1,1) 

from_id.m[0] = _p 

  

rChangeCurrRing(_ring) 

res_id = fast_map_common_subexp(from_id, _ring, to_id, _ring) 

_p = res_id.m[0] 

  

from_id.m[0] = NULL 

res_id.m[0] = NULL 

  

id_Delete(&from_id, _ring) 

id_Delete(&res_id, _ring) 

  

id_Delete(&to_id, _ring) 

  

if not try_symbolic: 

return new_MP(parent,_p) 

  

# now as everything else failed, try to do it symbolically with call 

  

cdef list g = list(parent.gens()) 

  

if fixed is not None: 

for m,v in fixed.iteritems(): 

if isinstance(m, (int, Integer)): 

mi = m+1 

elif isinstance(m, MPolynomial_libsingular) and m.parent() is parent: 

for i from 0 < i <= _ring.N: 

if p_GetExp((<MPolynomial_libsingular>m)._poly, i, _ring) != 0: 

mi = i 

break 

if i > _ring.N: 

raise TypeError("key does not match") 

else: 

raise TypeError("keys do not match self's parent") 

  

g[mi-1] = v 

  

gd = parent.gens_dict() 

for m,v in kw.iteritems(): 

m = gd[m] 

for i from 0 < i <= _ring.N: 

if p_GetExp((<MPolynomial_libsingular>m)._poly, i, _ring) != 0: 

mi = i 

break 

if i > _ring.N: 

raise TypeError("key does not match") 

  

g[mi-1] = v 

  

return self(*g) 

  

def monomials(self): 

""" 

Return the list of monomials in self. The returned list is 

decreasingly ordered by the term ordering of 

``self.parent()``. 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: f = x + 3/2*y*z^2 + 2/3 

sage: f.monomials() 

[y*z^2, x, 1] 

sage: f = P(3/2) 

sage: f.monomials() 

[1] 

  

TESTS:: 

  

sage: P.<x,y,z> = QQ[] 

sage: f = x 

sage: f.monomials() 

[x] 

  

Check if :trac:`12706` is fixed:: 

  

sage: f = P(0) 

sage: f.monomials() 

[] 

  

Check if :trac:`7152` is fixed:: 

  

sage: x=var('x') 

sage: K.<rho> = NumberField(x**2 + 1) 

sage: R.<x,y> = QQ[] 

sage: p = rho*x 

sage: q = x 

sage: p.monomials() 

[x] 

sage: q.monomials() 

[x] 

sage: p.monomials() 

[x] 

""" 

l = list() 

cdef MPolynomialRing_libsingular parent = self._parent 

cdef ring *_ring = parent._ring 

if(_ring != currRing): rChangeCurrRing(_ring) 

cdef poly *p = p_Copy(self._poly, _ring) 

cdef poly *t 

  

if p == NULL: 

return [] 

  

while p: 

t = pNext(p) 

p.next = NULL 

p_SetCoeff(p, n_Init(1,_ring), _ring) 

p_Setm(p, _ring) 

l.append( new_MP(parent,p) ) 

p = t 

  

return l 

  

def constant_coefficient(self): 

""" 

Return the constant coefficient of this multivariate 

polynomial. 

  

EXAMPLES:: 

  

sage: P.<x, y> = QQ[] 

sage: f = 3*x^2 - 2*y + 7*x^2*y^2 + 5 

sage: f.constant_coefficient() 

5 

sage: f = 3*x^2 

sage: f.constant_coefficient() 

0 

""" 

cdef poly *p = self._poly 

cdef ring *r = self._parent_ring 

if p == NULL: 

return self._parent._base._zero_element 

  

while p.next: 

p = pNext(p) 

  

if p_LmIsConstant(p, r): 

return si2sa(p_GetCoeff(p, r), r, self._parent._base) 

else: 

return self._parent._base._zero_element 

  

def univariate_polynomial(self, R=None): 

""" 

Returns a univariate polynomial associated to this 

multivariate polynomial. 

  

INPUT: 

  

- ``R`` - (default: ``None``) PolynomialRing 

  

If this polynomial is not in at most one variable, then a 

``ValueError`` exception is raised. This is checked using the 

:meth:`is_univariate()` method. The new Polynomial is over 

the same base ring as the given ``MPolynomial`` and in the 

variable ``x`` if no ring ``R`` is provided. 

  

EXAMPLES:: 

  

sage: R.<x, y> = QQ[] 

sage: f = 3*x^2 - 2*y + 7*x^2*y^2 + 5 

sage: f.univariate_polynomial() 

Traceback (most recent call last): 

... 

TypeError: polynomial must involve at most one variable 

sage: g = f.subs({x:10}); g 

700*y^2 - 2*y + 305 

sage: g.univariate_polynomial () 

700*y^2 - 2*y + 305 

sage: g.univariate_polynomial(PolynomialRing(QQ,'z')) 

700*z^2 - 2*z + 305 

  

Here's an example with a constant multivariate polynomial:: 

  

sage: g = R(1) 

sage: h = g.univariate_polynomial(); h 

1 

sage: h.parent() 

Univariate Polynomial Ring in x over Rational Field 

""" 

cdef poly *p = self._poly 

cdef poly *p2 = self._poly 

cdef ring *r = self._parent_ring 

cdef long pTotDegMax 

  

k = self.base_ring() 

  

if not self.is_univariate(): 

raise TypeError("polynomial must involve at most one variable") 

  

#construct ring if none 

if R is None: 

if self.is_constant(): 

R = self.base_ring()['x'] 

else: 

R = self.base_ring()[str(self.variables()[0])] 

  

zero = k(0) 

  

if(r != currRing): rChangeCurrRing(r) 

  

pTotDegMax = -1 

while p2: 

pTotDegMax = max(pTotDegMax, p_Totaldegree(p2, r)) 

p2 = pNext(p2) 

  

coefficients = [zero] * (pTotDegMax + 1) 

while p: 

pTotDeg = p_Totaldegree(p, r) 

if ( pTotDeg >= len(coefficients) or pTotDeg < 0 ): 

raise IndexError("list index("+str(pTotDeg)+" out of range(0-"+str(len(coefficients))+")") 

coefficients[pTotDeg] = si2sa(p_GetCoeff(p, r), r, k) 

p = pNext(p) 

  

return R(coefficients) 

  

def is_univariate(self): 

""" 

Return ``True`` if self is a univariate polynomial, that is if 

self contains only one variable. 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = GF(2)[] 

sage: f = x^2 + 1 

sage: f.is_univariate() 

True 

sage: f = y*x^2 + 1 

sage: f.is_univariate() 

False 

sage: f = P(0) 

sage: f.is_univariate() 

True 

""" 

return bool(len(self._variable_indices_(sort=False))<2) 

  

def _variable_indices_(self, sort=True): 

""" 

Return the indices of all variables occurring in self. This 

index is the index as Sage uses them (starting at zero), not 

as SINGULAR uses them (starting at one). 

  

INPUT: 

  

- ``sort`` - specifies whether the indices shall be sorted 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = GF(2)[] 

sage: f = x*z^2 + z + 1 

sage: f._variable_indices_() 

[0, 2] 

  

""" 

cdef poly *p 

cdef ring *r = self._parent_ring 

cdef int i 

s = set() 

p = self._poly 

while p: 

for i from 1 <= i <= r.N: 

if p_GetExp(p,i,r): 

s.add(i-1) 

p = pNext(p) 

if sort: 

return sorted(s) 

else: 

return list(s) 

  

def variables(self): 

""" 

Return a tuple of all variables occurring in self. 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = GF(2)[] 

sage: f = x*z^2 + z + 1 

sage: f.variables() 

(x, z) 

""" 

cdef poly *p 

cdef poly *v 

cdef ring *r = self._parent_ring 

if(r != currRing): rChangeCurrRing(r) 

cdef int i 

l = list() 

si = set() 

p = self._poly 

while p: 

for i from 1 <= i <= r.N: 

if i not in si and p_GetExp(p,i,r): 

v = p_ISet(1,r) 

p_SetExp(v, i, 1, r) 

p_Setm(v, r) 

l.append(new_MP(self._parent, v)) 

si.add(i) 

p = pNext(p) 

return tuple(sorted(l,reverse=True)) 

  

def variable(self, i=0): 

""" 

  

Return the i-th variable occurring in self. The index i is the 

index in ``self.variables()``. 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = GF(2)[] 

sage: f = x*z^2 + z + 1 

sage: f.variables() 

(x, z) 

sage: f.variable(1) 

z 

""" 

return self.variables()[i] 

  

def nvariables(self): 

""" 

Return the number variables in this polynomial. 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = PolynomialRing(GF(127)) 

sage: f = x*y + z 

sage: f.nvariables() 

3 

sage: f = x + y 

sage: f.nvariables() 

2 

""" 

return len(self._variable_indices_(sort=False)) 

  

cpdef is_constant(self): 

""" 

Return ``True`` if this polynomial is constant. 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = PolynomialRing(GF(127)) 

sage: x.is_constant() 

False 

sage: P(1).is_constant() 

True 

""" 

return bool(p_IsConstant(self._poly, self._parent_ring)) 

  

def lm(MPolynomial_libsingular self): 

""" 

Returns the lead monomial of self with respect to the term 

order of ``self.parent()``. In Sage a monomial is a product of 

variables in some power without a coefficient. 

  

EXAMPLES:: 

  

sage: R.<x,y,z>=PolynomialRing(GF(7),3,order='lex') 

sage: f = x^1*y^2 + y^3*z^4 

sage: f.lm() 

x*y^2 

sage: f = x^3*y^2*z^4 + x^3*y^2*z^1 

sage: f.lm() 

x^3*y^2*z^4 

  

sage: R.<x,y,z>=PolynomialRing(QQ,3,order='deglex') 

sage: f = x^1*y^2*z^3 + x^3*y^2*z^0 

sage: f.lm() 

x*y^2*z^3 

sage: f = x^1*y^2*z^4 + x^1*y^1*z^5 

sage: f.lm() 

x*y^2*z^4 

  

sage: R.<x,y,z>=PolynomialRing(GF(127),3,order='degrevlex') 

sage: f = x^1*y^5*z^2 + x^4*y^1*z^3 

sage: f.lm() 

x*y^5*z^2 

sage: f = x^4*y^7*z^1 + x^4*y^2*z^3 

sage: f.lm() 

x^4*y^7*z 

  

""" 

cdef poly *_p 

cdef ring *_ring = self._parent_ring 

if self._poly == NULL: 

return self._parent._zero_element 

_p = p_Head(self._poly, _ring) 

p_SetCoeff(_p, n_Init(1,_ring), _ring) 

p_Setm(_p,_ring) 

return new_MP(self._parent, _p) 

  

def lc(MPolynomial_libsingular self): 

""" 

Leading coefficient of this polynomial with respect to the 

term order of ``self.parent()``. 

  

EXAMPLES:: 

  

sage: R.<x,y,z>=PolynomialRing(GF(7),3,order='lex') 

sage: f = 3*x^1*y^2 + 2*y^3*z^4 

sage: f.lc() 

3 

  

sage: f = 5*x^3*y^2*z^4 + 4*x^3*y^2*z^1 

sage: f.lc() 

5 

""" 

  

cdef poly *_p 

cdef ring *_ring = self._parent_ring 

cdef number *_n 

  

if self._poly == NULL: 

return self._parent._base._zero_element 

  

if(_ring != currRing): rChangeCurrRing(_ring) 

  

_p = p_Head(self._poly, _ring) 

_n = p_GetCoeff(_p, _ring) 

  

ret = si2sa(_n, _ring, self._parent._base) 

p_Delete(&_p, _ring) 

return ret 

  

def lt(MPolynomial_libsingular self): 

""" 

Leading term of this polynomial. In Sage a term is a product 

of variables in some power and a coefficient. 

  

EXAMPLES:: 

  

sage: R.<x,y,z>=PolynomialRing(GF(7),3,order='lex') 

sage: f = 3*x^1*y^2 + 2*y^3*z^4 

sage: f.lt() 

3*x*y^2 

  

sage: f = 5*x^3*y^2*z^4 + 4*x^3*y^2*z^1 

sage: f.lt() 

-2*x^3*y^2*z^4 

""" 

if self._poly == NULL: 

return self._parent._zero_element 

return new_MP(self._parent, p_Head(self._poly, self._parent_ring)) 

  

def is_zero(self): 

""" 

Return ``True`` if this polynomial is zero. 

  

EXAMPLES:: 

  

sage: P.<x,y> = PolynomialRing(QQ) 

sage: x.is_zero() 

False 

sage: (x-x).is_zero() 

True 

""" 

if self._poly is NULL: 

return True 

else: 

return False 

  

def __nonzero__(self): 

""" 

EXAMPLES:: 

  

sage: P.<x,y> = PolynomialRing(QQ) 

sage: bool(x) # indirect doctest 

True 

sage: bool(x-x) 

False 

""" 

if self._poly: 

return True 

else: 

return False 

  

cpdef _floordiv_(self, right): 

""" 

Perform division with remainder and return the quotient. 

  

INPUT: 

  

- ``right`` - something coercible to an MPolynomial_libsingular 

in ``self.parent()`` 

  

EXAMPLES:: 

  

sage: R.<x,y,z> = GF(32003)[] 

sage: f = y*x^2 + x + 1 

sage: f//x 

x*y + 1 

sage: f//y 

x^2 

  

sage: P.<x,y> = ZZ[] 

sage: x//y 

0 

sage: (x+y)//y 

1 

  

sage: P.<x,y> = QQ[] 

sage: (x+y)//y 

1 

sage: (x)//y 

0 

  

sage: P.<x,y> = Zmod(1024)[] 

sage: (x+y)//x 

1 

sage: (x+y)//(2*x) 

Traceback (most recent call last): 

... 

NotImplementedError: Division of multivariate polynomials over non fields by non-monomials not implemented. 

""" 

cdef MPolynomialRing_libsingular parent = self._parent 

cdef ring *r = self._parent_ring 

if(r != currRing): rChangeCurrRing(r) 

cdef MPolynomial_libsingular _self, _right 

cdef poly *quo 

cdef poly *temp 

cdef poly *p 

  

if right.is_zero(): 

raise ZeroDivisionError 

  

if self._parent._base.is_finite() and self._parent._base.characteristic() > 1<<29: 

raise NotImplementedError("Division of multivariate polynomials over prime fields with characteristic > 2^29 is not implemented.") 

  

_self = <MPolynomial_libsingular>self 

_right = <MPolynomial_libsingular>right 

  

if r.cf.type != n_unknown: 

if r.cf.type == n_Z: 

P = parent.change_ring(RationalField()) 

f = P(self)//P(right) 

CM = list(f) 

return parent(sum([c.floor()*m for c,m in CM])) 

if _right.is_monomial(): 

p = _self._poly 

quo = p_ISet(0,r) 

if r != currRing: rChangeCurrRing(r) # pDivide 

while p: 

if p_DivisibleBy(_right._poly, p, r): 

temp = pDivide(p, _right._poly) 

p_SetCoeff0(temp, n_Copy(p_GetCoeff(p, r), r), r) 

quo = p_Add_q(quo, temp, r) 

p = pNext(p) 

return new_MP(parent, quo) 

if r.cf.type == n_Znm or r.cf.type == n_Zn or r.cf.type == n_Z2m : 

raise NotImplementedError("Division of multivariate polynomials over non fields by non-monomials not implemented.") 

  

cdef int count = singular_polynomial_length_bounded(_self._poly,15) 

if count >= 15: # note that _right._poly must be of shorter length than self._poly for us to care about this call 

sig_on() 

if r!=currRing: rChangeCurrRing(r) # singclap_pdivide 

quo = singclap_pdivide( _self._poly, _right._poly, r ) 

if count >= 15: 

sig_off() 

f = new_MP(parent, quo) 

  

return f 

  

def factor(self, proof=True): 

""" 

Return the factorization of this polynomial. 

  

INPUT: 

  

- ``proof`` - ignored. 

  

EXAMPLES:: 

  

sage: R.<x, y> = QQ[] 

sage: f = (x^3 + 2*y^2*x) * (x^2 + x + 1); f 

x^5 + 2*x^3*y^2 + x^4 + 2*x^2*y^2 + x^3 + 2*x*y^2 

sage: F = f.factor() 

sage: F 

x * (x^2 + x + 1) * (x^2 + 2*y^2) 

  

Next we factor the same polynomial, but over the finite field 

of order 3.:: 

  

sage: R.<x, y> = GF(3)[] 

sage: f = (x^3 + 2*y^2*x) * (x^2 + x + 1); f 

x^5 - x^3*y^2 + x^4 - x^2*y^2 + x^3 - x*y^2 

sage: F = f.factor() 

sage: F # order is somewhat random 

(-1) * x * (-x + y) * (x + y) * (x - 1)^2 

  

Next we factor a polynomial, but over a finite field of order 9.:: 

  

sage: K.<a> = GF(3^2) 

sage: R.<x, y> = K[] 

sage: f = (x^3 + 2*a*y^2*x) * (x^2 + x + 1); f 

x^5 + (-a)*x^3*y^2 + x^4 + (-a)*x^2*y^2 + x^3 + (-a)*x*y^2 

sage: F = f.factor() 

sage: F 

((-a)) * x * (x - 1)^2 * ((-a + 1)*x^2 + y^2) 

sage: f - F 

0 

  

Next we factor a polynomial over a number field.:: 

  

sage: p = var('p') 

sage: K.<s> = NumberField(p^3-2) 

sage: KXY.<x,y> = K[] 

sage: factor(x^3 - 2*y^3) 

(x + (-s)*y) * (x^2 + (s)*x*y + (s^2)*y^2) 

sage: k = (x^3-2*y^3)^5*(x+s*y)^2*(2/3 + s^2) 

sage: k.factor() 

((s^2 + 2/3)) * (x + (s)*y)^2 * (x + (-s)*y)^5 * (x^2 + (s)*x*y + (s^2)*y^2)^5 

  

This shows that ticket :trac:`2780` is fixed, i.e. that the unit 

part of the factorization is set correctly:: 

  

sage: x = var('x') 

sage: K.<a> = NumberField(x^2 + 1) 

sage: R.<y, z> = PolynomialRing(K) 

sage: f = 2*y^2 + 2*z^2 

sage: F = f.factor(); F.unit() 

2 

  

Another example:: 

  

sage: R.<x,y,z> = GF(32003)[] 

sage: f = 9*(x-1)^2*(y+z) 

sage: f.factor() 

(9) * (y + z) * (x - 1)^2 

  

sage: R.<x,w,v,u> = QQ['x','w','v','u'] 

sage: p = (4*v^4*u^2 - 16*v^2*u^4 + 16*u^6 - 4*v^4*u + 8*v^2*u^3 + v^4) 

sage: p.factor() 

(-2*v^2*u + 4*u^3 + v^2)^2 

sage: R.<a,b,c,d> = QQ[] 

sage: f = (-2) * (a - d) * (-a + b) * (b - d) * (a - c) * (b - c) * (c - d) 

sage: F = f.factor(); F 

(-2) * (c - d) * (-b + c) * (b - d) * (-a + c) * (-a + b) * (a - d) 

sage: F[0][0] 

c - d 

sage: F.unit() 

-2 

  

Constant elements are factorized in the base rings. :: 

  

sage: P.<x,y> = ZZ[] 

sage: P(2^3*7).factor() 

2^3 * 7 

sage: P.<x,y> = GF(2)[] 

sage: P(1).factor() 

1 

  

Factorization for finite prime fields with characteristic 

`> 2^{29}` is not supported :: 

  

sage: q = 1073741789 

sage: T.<aa, bb> = PolynomialRing(GF(q)) 

sage: f = aa^2 + 12124343*bb*aa + 32434598*bb^2 

sage: f.factor() 

Traceback (most recent call last): 

... 

NotImplementedError: Factorization of multivariate polynomials over prime fields with characteristic > 2^29 is not implemented. 

  

Factorization over the integers is now supported, see :trac:`17840`:: 

  

sage: P.<x,y> = PolynomialRing(ZZ) 

sage: f = 12 * (3*x*y + 4) * (5*x - 2) * (2*y + 7)^2 

sage: f.factor() 

2^2 * 3 * (2*y + 7)^2 * (5*x - 2) * (3*x*y + 4) 

sage: g = -12 * (x^2 - y^2) 

sage: g.factor() 

(-1) * 2^2 * 3 * (x - y) * (x + y) 

sage: factor(-4*x*y - 2*x + 2*y + 1) 

(-1) * (2*y + 1) * (2*x - 1) 

  

Factorization over non-integral domains is not supported :: 

  

sage: R.<x,y> = PolynomialRing(Zmod(4)) 

sage: f = (2*x + 1) * (x^2 + x + 1) 

sage: f.factor() 

Traceback (most recent call last): 

... 

NotImplementedError: Factorization of multivariate polynomials over Ring of integers modulo 4 is not implemented. 

  

TESTS: 

  

This shows that :trac:`10270` is fixed:: 

  

sage: R.<x,y,z> = GF(3)[] 

sage: f = x^2*z^2+x*y*z-y^2 

sage: f.factor() 

x^2*z^2 + x*y*z - y^2 

  

This checks that :trac:`11838` is fixed:: 

  

sage: K = GF(4,'a') 

sage: a = K.gens()[0] 

sage: R.<x,y> = K[] 

sage: p=x^8*y^3 + x^2*y^9 + a*x^9 + a*x*y^4 

sage: q=y^11 + (a)*y^10 + (a + 1)*x*y^3 

sage: f = p*q 

sage: f.factor() 

x * y^3 * (y^8 + (a)*y^7 + (a + 1)*x) * (x^7*y^3 + x*y^9 + (a)*x^8 + (a)*y^4) 

  

We test several examples which were known to return wrong 

results in the past (see :trac:`10902`):: 

  

sage: R.<x,y> = GF(2)[] 

sage: p = x^3*y^7 + x^2*y^6 + x^2*y^3 

sage: q = x^3*y^5 

sage: f = p*q 

sage: p.factor()*q.factor() 

x^5 * y^8 * (x*y^4 + y^3 + 1) 

sage: f.factor() 

x^5 * y^8 * (x*y^4 + y^3 + 1) 

sage: f.factor().expand() == f 

True 

  

:: 

  

sage: R.<x,y> = GF(2)[] 

sage: p=x^8 + y^8; q=x^2*y^4 + x 

sage: f=p*q 

sage: lf = f.factor() 

sage: f-lf 

0 

  

:: 

  

sage: R.<x,y> = GF(3)[] 

sage: p = -x*y^9 + x 

sage: q = -x^8*y^2 

sage: f = p*q 

sage: f 

x^9*y^11 - x^9*y^2 

sage: f.factor() 

y^2 * (y - 1)^9 * x^9 

sage: f - f.factor() 

0 

  

:: 

  

sage: R.<x,y> = GF(5)[] 

sage: p=x^27*y^9 + x^32*y^3 + 2*x^20*y^10 - x^4*y^24 - 2*x^17*y 

sage: q=-2*x^10*y^24 + x^9*y^24 - 2*x^3*y^30 

sage: f=p*q; f-f.factor() 

0 

  

:: 

  

sage: R.<x,y> = GF(7)[] 

sage: p=-3*x^47*y^24 

sage: q=-3*x^47*y^37 - 3*x^24*y^49 + 2*x^56*y^8 + 3*x^29*y^15 - x^2*y^33 

sage: f=p*q 

sage: f-f.factor() 

0 

  

The following examples used to give a Segmentation Fault, see 

:trac:`12918` and :trac:`13129`:: 

  

sage: R.<x,y> = GF(2)[] 

sage: f = x^6 + x^5 + y^5 + y^4 

sage: f.factor() 

x^6 + x^5 + y^5 + y^4 

sage: f = x^16*y + x^10*y + x^9*y + x^6*y + x^5 + x*y + y^2 

sage: f.factor() 

x^16*y + x^10*y + x^9*y + x^6*y + x^5 + x*y + y^2 

  

Test :trac:`12928`:: 

  

sage: R.<x,y> = GF(2)[] 

sage: p = x^2 + y^2 + x + 1 

sage: q = x^4 + x^2*y^2 + y^4 + x*y^2 + x^2 + y^2 + 1 

sage: factor(p*q) 

(x^2 + y^2 + x + 1) * (x^4 + x^2*y^2 + y^4 + x*y^2 + x^2 + y^2 + 1) 

  

Check that :trac:`13770` is fixed:: 

  

sage: U.<y,t> = GF(2)[] 

sage: f = y*t^8 + y^5*t^2 + y*t^6 + t^7 + y^6 + y^5*t + y^2*t^4 + y^2*t^2 + y^2*t + t^3 + y^2 + t^2 

sage: l = f.factor() 

sage: l[0][0]==t^2 + y + t + 1 or l[1][0]==t^2 + y + t + 1 

True 

  

The following used to sometimes take a very long time or get 

stuck, see :trac:`12846`. These 100 iterations should take less 

than 1 second:: 

  

sage: K.<a> = GF(4) 

sage: R.<x,y> = K[] 

sage: f = (a + 1)*x^145*y^84 + (a + 1)*x^205*y^17 + x^32*y^112 + x^92*y^45 

sage: for i in range(100): 

....: assert len(f.factor()) == 4 

  

Test for :trac:`20435`:: 

  

sage: x,y = polygen(ZZ,'x,y') 

sage: p = x**2-y**2 

sage: z = factor(p); z 

(x - y) * (x + y) 

sage: z[0][0].parent() 

Multivariate Polynomial Ring in x, y over Integer Ring 

  

Test for :trac:`17680`:: 

  

sage: R.<a,r,v,n,g,f,h,o> = QQ[] 

sage: f = 248301045*a^2*r^10*n^2*o^10+570807000*a^2*r^9*n*o^9-137945025*a^2*r^8*n^2*o^8+328050000*a^2*r^8*o^8-253692000*a^2*r^7*n*o^7+30654450*a^2*r^6*n^2*o^6-109350000*a^2*r^6*o^6+42282000*a^2*r^5*n*o^5-3406050*a^2*r^4*n^2*o^4-22457088*a*r^2*v*n^2*o^6+12150000*a^2*r^4*o^4-3132000*a^2*r^3*n*o^3+189225*a^2*r^2*n^2*o^2+2495232*a*v*n^2*o^4-450000*a^2*r^2*o^2+87000*a^2*r*n*o-4205*a^2*n^2 

sage: len(factor(f)) 

4 

  

Test for :trac:`17251`:: 

  

sage: R.<z,a,b> = PolynomialRing(QQ) 

sage: N = -a^4*z^8 + 2*a^2*b^2*z^8 - b^4*z^8 - 16*a^3*b*z^7 + 16*a*b^3*z^7 + 28*a^4*z^6 - 56*a^2*b^2*z^6 + 28*b^4*z^6 + 112*a^3*b*z^5 - 112*a*b^3*z^5 - 70*a^4*z^4 + 140*a^2*b^2*z^4 - 70*b^4*z^4 - 112*a^3*b*z^3 + 112*a*b^3*z^3 + 28*a^4*z^2 - 56*a^2*b^2*z^2 + 28*b^4*z^2 + 16*a^3*b*z - 16*a*b^3*z - a^4 + 2*a^2*b^2 - b^4 

sage: N.factor() 

(-1) * (-a + b) * (a + b) * (-z^4*a + z^4*b - 4*z^3*a - 4*z^3*b + 6*z^2*a - 6*z^2*b + 4*z*a + 4*z*b - a + b) * (z^4*a + z^4*b - 4*z^3*a + 4*z^3*b - 6*z^2*a - 6*z^2*b + 4*z*a - 4*z*b + a + b) 

""" 

cdef ring *_ring = self._parent_ring 

cdef poly *ptemp 

cdef intvec *iv 

cdef int *ivv 

cdef ideal *I 

cdef MPolynomialRing_libsingular parent = self._parent 

cdef int i 

  

if _ring!=currRing: rChangeCurrRing(_ring) 

  

if p_IsConstant(self._poly, _ring): 

return self.constant_coefficient().factor() 

  

if not self._parent._base.is_field(): 

try: 

frac_field = self._parent._base.fraction_field() 

F = self.change_ring(frac_field).factor() 

FF = [(self._parent(f[0]), f[1]) for f in F] 

U = self._parent._base(F.unit()).factor() 

return Factorization(list(U) + FF, unit=U.unit()) 

except Exception: 

raise NotImplementedError("Factorization of multivariate polynomials over %s is not implemented."%self._parent._base) 

  

if self._parent._base.is_finite(): 

if self._parent._base.characteristic() > 1<<29: 

raise NotImplementedError("Factorization of multivariate polynomials over prime fields with characteristic > 2^29 is not implemented.") 

  

# I make a temporary copy of the poly in self because singclap_factorize appears to modify it's parameter 

ptemp = p_Copy(self._poly,_ring) 

iv = NULL 

sig_on() 

if _ring!=currRing: rChangeCurrRing(_ring) # singclap_factorize 

I = singclap_factorize ( ptemp, &iv , 0, _ring) 

sig_off() 

  

ivv = iv.ivGetVec() 

v = [(new_MP(parent, p_Copy(I.m[i],_ring)) , ivv[i]) for i in range(1,I.ncols)] 

v = [(f,m) for f,m in v if f!=0] # we might have zero in there 

unit = new_MP(parent, p_Copy(I.m[0],_ring)) 

  

F = Factorization(v,unit) 

F.sort() 

  

del iv 

id_Delete(&I,_ring) 

  

return F 

  

def lift(self, I): 

""" 

given an ideal ``I = (f_1,...,f_r)`` and some ``g (== self)`` in ``I``, 

find ``s_1,...,s_r`` such that ``g = s_1 f_1 + ... + s_r f_r``. 

  

A ``ValueError`` exception is raised if ``g (== self)`` does not belong to ``I``. 

  

EXAMPLES:: 

  

sage: A.<x,y> = PolynomialRing(QQ,2,order='degrevlex') 

sage: I = A.ideal([x^10 + x^9*y^2, y^8 - x^2*y^7 ]) 

sage: f = x*y^13 + y^12 

sage: M = f.lift(I) 

sage: M 

[y^7, x^7*y^2 + x^8 + x^5*y^3 + x^6*y + x^3*y^4 + x^4*y^2 + x*y^5 + x^2*y^3 + y^4] 

sage: sum( map( mul , zip( M, I.gens() ) ) ) == f 

True 

  

Check that :trac:`13671` is fixed:: 

  

sage: R.<x1,x2> = QQ[] 

sage: I = R.ideal(x2**2 + x1 - 2, x1**2 - 1) 

sage: f = I.gen(0) + x2*I.gen(1) 

sage: f.lift(I) 

[1, x2] 

sage: (f+1).lift(I) 

Traceback (most recent call last): 

... 

ValueError: polynomial is not in the ideal 

sage: f.lift(I) 

[1, x2] 

  

TESTS: 

  

Check that :trac:`13714` is fixed:: 

  

sage: R.<x1,x2> = QQ[] 

sage: I = R.ideal(x2**2 + x1 - 2, x1**2 - 1) 

sage: R.one().lift(I) 

Traceback (most recent call last): 

... 

ValueError: polynomial is not in the ideal 

sage: foo = I.complete_primary_decomposition() # indirect doctest 

sage: foo[0][0] 

Ideal (x1 + 1, x2^2 - 3) of Multivariate Polynomial Ring in x1, x2 over Rational Field 

  

""" 

global errorreported 

if not self._parent._base.is_field(): 

raise NotImplementedError("Lifting of multivariate polynomials over non-fields is not implemented.") 

  

cdef ideal *fI = idInit(1,1) 

cdef ideal *_I 

cdef MPolynomialRing_libsingular parent = self._parent 

cdef int i = 0 

cdef int j 

cdef ring *r = self._parent_ring 

cdef ideal *res 

  

if isinstance(I, MPolynomialIdeal): 

I = I.gens() 

  

_I = idInit(len(I),1) 

  

for f in I: 

if not (isinstance(f,MPolynomial_libsingular) \ 

and (<MPolynomial_libsingular>f)._parent is parent): 

try: 

f = parent._coerce_c(f) 

except TypeError as msg: 

id_Delete(&fI,r) 

id_Delete(&_I,r) 

raise TypeError(msg) 

  

_I.m[i] = p_Copy((<MPolynomial_libsingular>f)._poly, r) 

i+=1 

  

fI.m[0]= p_Copy(self._poly, r) 

  

if r!=currRing: rChangeCurrRing(r) # idLift 

res = idLift(_I, fI, NULL, 0, 0, 0) 

if errorreported != 0 : 

errorcode = errorreported 

errorreported = 0 

if errorcode == 1: 

raise ValueError("polynomial is not in the ideal") 

raise RuntimeError 

  

l = [] 

for i from 0 <= i < IDELEMS(res): 

for j from 1 <= j <= IDELEMS(_I): 

l.append( new_MP(parent, pTakeOutComp1(&res.m[i], j)) ) 

  

id_Delete(&fI, r) 

id_Delete(&_I, r) 

id_Delete(&res, r) 

return Sequence(l, check=False, immutable=True) 

  

def reduce(self,I): 

""" 

Return a remainder of this polynomial modulo the  

polynomials in ``I``. 

 

INPUT: 

  

- ``I`` - an ideal or a list/set/iterable of polynomials.  

  

OUTPUT: 

  

A polynomial ``r`` such that: 

  

- ``self`` - ``r`` is in the ideal generated by ``I``. 

  

- No term in ``r`` is divisible by any of the leading monomials 

of ``I``. 

  

The result ``r`` is canonical if: 

  

- ``I`` is an ideal, and Sage can compute a Groebner basis of it. 

  

- ``I`` is a list/set/iterable that is a (strong) Groebner basis 

for the term order of ``self``. (A strong Groebner basis is  

such that for every leading term ``t`` of the ideal generated 

by ``I``, there exists an element ``g`` of ``I`` such that the 

leading term of ``g`` divides ``t``.) 

  

The result ``r`` is implementation-dependent (and possibly  

order-dependent) otherwise. If ``I`` is an ideal and no Groebner 

basis can be computed, its list of generators ``I.gens()`` is 

used for the reduction. 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: f1 = -2 * x^2 + x^3 

sage: f2 = -2 * y + x* y 

sage: f3 = -x^2 + y^2 

sage: F = Ideal([f1,f2,f3]) 

sage: g = x*y - 3*x*y^2 

sage: g.reduce(F) 

-6*y^2 + 2*y 

sage: g.reduce(F.gens()) 

-6*y^2 + 2*y 

  

`\ZZ` is also supported. :: 

  

sage: P.<x,y,z> = ZZ[] 

sage: f1 = -2 * x^2 + x^3 

sage: f2 = -2 * y + x* y 

sage: f3 = -x^2 + y^2 

sage: F = Ideal([f1,f2,f3]) 

sage: g = x*y - 3*x*y^2 

sage: g.reduce(F) 

-6*y^2 + 2*y 

sage: g.reduce(F.gens()) 

-6*y^2 + 2*y 

  

sage: f = 3*x 

sage: f.reduce([2*x,y]) 

3*x 

  

The reduction is not canonical when ``I`` is not a Groebner  

basis:: 

  

sage: A.<x,y> = QQ[] 

sage: (x+y).reduce([x+y, x-y]) 

2*y 

sage: (x+y).reduce([x-y, x+y]) 

0 

  

  

""" 

cdef ideal *_I 

cdef MPolynomialRing_libsingular parent = self._parent 

cdef int i = 0 

cdef ring *r = self._parent_ring 

cdef poly *res 

  

if r!=currRing: rChangeCurrRing(r) 

  

if isinstance(I, MPolynomialIdeal): 

try: 

strat = I._groebner_strategy() 

return strat.normal_form(self) 

except (TypeError, NotImplementedError): 

pass 

I = I.gens() 

  

_I = idInit(len(I),1) 

for f in I: 

if not (isinstance(f,MPolynomial_libsingular) \ 

and (<MPolynomial_libsingular>f)._parent is parent): 

try: 

f = parent._coerce_c(f) 

except TypeError as msg: 

id_Delete(&_I,r) 

raise TypeError(msg) 

  

_I.m[i] = p_Copy((<MPolynomial_libsingular>f)._poly, r) 

i+=1 

  

#the second parameter would be qring! 

if r!=currRing: rChangeCurrRing(r) # kNF 

res = kNF(_I, NULL, self._poly) 

id_Delete(&_I,r) 

return new_MP(parent,res) 

  

def divides(self, other): 

""" 

Return ``True`` if this polynomial divides ``other``. 

  

EXAMPLES:: 

  

sage: R.<x,y,z> = QQ[] 

sage: p = 3*x*y + 2*y*z + x*z 

sage: q = x + y + z + 1 

sage: r = p * q 

sage: p.divides(r) 

True 

sage: q.divides(p) 

False 

sage: r.divides(0) 

True 

sage: R.zero().divides(r) 

False 

sage: R.zero().divides(0) 

True 

""" 

if self.is_zero(): 

return other.is_zero() 

cdef ideal *_I 

cdef MPolynomialRing_libsingular parent = self._parent 

cdef ring *r = self._parent_ring 

cdef poly *rem 

  

if r != currRing: 

rChangeCurrRing(r) 

  

_I = idInit(1, 1) 

if not (isinstance(other,MPolynomial_libsingular) \ 

and (<MPolynomial_libsingular>other)._parent is parent): 

try: 

other = parent._coerce_c(other) 

except TypeError as msg: 

id_Delete(&_I,r) 

raise TypeError(msg) 

  

_I.m[0] = p_Copy(self._poly, r) 

  

if r != currRing: 

rChangeCurrRing(r) 

sig_on() 

rem = kNF(_I, NULL, (<MPolynomial_libsingular>other)._poly, 0, 1) 

sig_off() 

id_Delete(&_I, r) 

res = new_MP(parent, rem).is_zero() 

return res 

  

@coerce_binop 

def gcd(self, right, algorithm=None, **kwds): 

""" 

Return the greatest common divisor of self and right. 

  

INPUT: 

  

- ``right`` - polynomial 

- ``algorithm`` 

- ``ezgcd`` - EZGCD algorithm 

- ``modular`` - multi-modular algorithm (default) 

- ``**kwds`` - ignored 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: f = (x*y*z)^6 - 1 

sage: g = (x*y*z)^4 - 1 

sage: f.gcd(g) 

x^2*y^2*z^2 - 1 

sage: GCD([x^3 - 3*x + 2, x^4 - 1, x^6 -1]) 

x - 1 

  

sage: R.<x,y> = QQ[] 

sage: f = (x^3 + 2*y^2*x)^2 

sage: g = x^2*y^2 

sage: f.gcd(g) 

x^2 

  

We compute a gcd over a finite field:: 

  

sage: F.<u> = GF(31^2) 

sage: R.<x,y,z> = F[] 

sage: p = x^3 + (1+u)*y^3 + z^3 

sage: q = p^3 * (x - y + z*u) 

sage: gcd(p,q) 

x^3 + (u + 1)*y^3 + z^3 

sage: gcd(p,q) # yes, twice -- tests that singular ring is properly set. 

x^3 + (u + 1)*y^3 + z^3 

  

We compute a gcd over a number field:: 

  

sage: x = polygen(QQ) 

sage: F.<u> = NumberField(x^3 - 2) 

sage: R.<x,y,z> = F[] 

sage: p = x^3 + (1+u)*y^3 + z^3 

sage: q = p^3 * (x - y + z*u) 

sage: gcd(p,q) 

x^3 + (u + 1)*y^3 + z^3 

  

TESTS:: 

  

sage: Q.<x,y,z> = QQ[] 

sage: P.<x,y,z> = QQ[] 

sage: P(0).gcd(Q(0)) 

0 

sage: x.gcd(1) 

1 

  

sage: k.<a> = GF(9) 

sage: R.<x,y> = PolynomialRing(k) 

sage: f = R.change_ring(GF(3)).gen() 

sage: g = x+y 

sage: g.gcd(f) 

1 

sage: x.gcd(R.change_ring(GF(3)).gen()) 

x 

""" 

cdef MPolynomial_libsingular _right 

cdef poly *_res 

cdef ring *_ring = self._parent_ring 

  

if algorithm is None: 

algorithm = "modular" 

  

if algorithm == "ezgcd": 

Off(SW_USE_CHINREM_GCD) 

On(SW_USE_EZGCD) 

elif algorithm == "modular": 

On(SW_USE_CHINREM_GCD) 

Off(SW_USE_EZGCD) 

else: 

raise TypeError("algorithm %s not supported" % algorithm) 

  

if _ring.cf.type != n_unknown: 

if _ring.cf.type == n_Z: 

P = self._parent.change_ring(RationalField()) 

res = P(self).gcd(P(right)) 

coef = GCD_list(self.coefficients() + right.coefficients()) 

return self._parent(coef*res) 

  

#TODO: 

if _ring.cf.type == n_Znm or _ring.cf.type == n_Zn or _ring.cf.type == n_Z2m : 

raise NotImplementedError("GCD over rings not implemented.") 

#raise NotImplementedError("GCD over rings not implemented.") 

  

if self._parent._base.is_finite() and self._parent._base.characteristic() > 1<<29: 

raise NotImplementedError("GCD of multivariate polynomials over prime fields with characteristic > 2^29 is not implemented.") 

  

if(_ring != currRing): rChangeCurrRing(_ring) 

  

if not (isinstance(right, MPolynomial_libsingular) \ 

and (<MPolynomial_libsingular>right)._parent is self._parent): 

_right = self._parent._coerce_c(right) 

else: 

_right = <MPolynomial_libsingular>right 

  

cdef int count = singular_polynomial_length_bounded(self._poly,20) \ 

+ singular_polynomial_length_bounded(_right._poly,20) 

if count >= 20: 

sig_on() 

if _ring!=currRing: rChangeCurrRing(_ring) # singclap_gcd 

_res = singclap_gcd(p_Copy(self._poly, _ring), p_Copy(_right._poly, _ring), _ring ) 

if count >= 20: 

sig_off() 

  

res = new_MP(self._parent, _res) 

return res 

  

@coerce_binop 

def lcm(self, MPolynomial_libsingular g): 

""" 

Return the least common multiple of ``self`` and `g`. 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = QQ[] 

sage: p = (x+y)*(y+z) 

sage: q = (z^4+2)*(y+z) 

sage: lcm(p,q) 

x*y*z^4 + y^2*z^4 + x*z^5 + y*z^5 + 2*x*y + 2*y^2 + 2*x*z + 2*y*z 

  

sage: P.<x,y,z> = ZZ[] 

sage: p = 2*(x+y)*(y+z) 

sage: q = 3*(z^4+2)*(y+z) 

sage: lcm(p,q) 

6*x*y*z^4 + 6*y^2*z^4 + 6*x*z^5 + 6*y*z^5 + 12*x*y + 12*y^2 + 12*x*z + 12*y*z 

  

sage: r.<x,y> = PolynomialRing(GF(2**8, 'a'), 2) 

sage: a = r.base_ring().0 

sage: f = (a^2+a)*x^2*y + (a^4+a^3+a)*y + a^5 

sage: f.lcm(x^4) 

(a^2 + a)*x^6*y + (a^4 + a^3 + a)*x^4*y + (a^5)*x^4 

  

sage: w = var('w') 

sage: r.<x,y> = PolynomialRing(NumberField(w^4 + 1, 'a'), 2) 

sage: a = r.base_ring().0 

sage: f = (a^2+a)*x^2*y + (a^4+a^3+a)*y + a^5 

sage: f.lcm(x^4) 

(a^2 + a)*x^6*y + (a^3 + a - 1)*x^4*y + (-a)*x^4 

""" 

cdef ring *_ring = self._parent_ring 

cdef poly *ret 

cdef poly *prod 

cdef poly *gcd 

cdef MPolynomial_libsingular _g 

if _ring!=currRing: rChangeCurrRing(_ring) 

  

if _ring.cf.type != n_unknown: 

if _ring.cf.type == n_Z: 

P = self.parent().change_ring(RationalField()) 

py_gcd = P(self).gcd(P(g)) 

py_prod = P(self*g) 

return self.parent(py_prod//py_gcd) 

else: 

if _ring.cf.type == n_Znm or _ring.cf.type == n_Zn or _ring.cf.type == n_Z2m : 

raise TypeError("LCM over non-integral domains not available.") 

  

if self._parent is not g._parent: 

_g = self._parent._coerce_c(g) 

else: 

_g = <MPolynomial_libsingular>g 

  

if self._parent._base.is_finite() and self._parent._base.characteristic() > 1<<29: 

raise NotImplementedError("LCM of multivariate polynomials over prime fields with characteristic > 2^29 is not implemented.") 

  

cdef int count = singular_polynomial_length_bounded(self._poly,20) \ 

+ singular_polynomial_length_bounded(_g._poly,20) 

if count >= 20: 

sig_on() 

if _ring!=currRing: rChangeCurrRing(_ring) # singclap_gcd 

gcd = singclap_gcd(p_Copy(self._poly, _ring), p_Copy(_g._poly, _ring), _ring ) 

prod = pp_Mult_qq(self._poly, _g._poly, _ring) 

ret = singclap_pdivide(prod , gcd , _ring) 

p_Delete(&prod, _ring) 

p_Delete(&gcd, _ring) 

if count >= 20: 

sig_off() 

return new_MP(self._parent, ret) 

  

def is_squarefree(self): 

""" 

Return ``True`` if this polynomial is square free. 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = PolynomialRing(QQ) 

sage: f= x^2 + 2*x*y + 1/2*z 

sage: f.is_squarefree() 

True 

sage: h = f^2 

sage: h.is_squarefree() 

False 

""" 

# TODO: Use Singular (4.x) intrinsics. (Temporary solution from #17254.) 

return all([ e == 1 for (f, e) in self.factor() ]) 

  

@coerce_binop 

def quo_rem(self, MPolynomial_libsingular right): 

""" 

Returns quotient and remainder of self and right. 

  

EXAMPLES:: 

  

sage: R.<x,y> = QQ[] 

sage: f = y*x^2 + x + 1 

sage: f.quo_rem(x) 

(x*y + 1, 1) 

sage: f.quo_rem(y) 

(x^2, x + 1) 

  

sage: R.<x,y> = ZZ[] 

sage: f = 2*y*x^2 + x + 1 

sage: f.quo_rem(x) 

(2*x*y + 1, 1) 

sage: f.quo_rem(y) 

(2*x^2, x + 1) 

sage: f.quo_rem(3*x) 

(2*x*y + 1, -4*x^2*y - 2*x + 1) 

  

TESTS:: 

  

sage: R.<x,y> = QQ[] 

sage: R(0).quo_rem(R(1)) 

(0, 0) 

sage: R(1).quo_rem(R(0)) 

Traceback (most recent call last): 

... 

ZeroDivisionError 

  

""" 

cdef poly *quo 

cdef poly *rem 

cdef MPolynomialRing_libsingular parent = self._parent 

cdef ring *r = self._parent_ring 

if r!=currRing: rChangeCurrRing(r) 

  

if right.is_zero(): 

raise ZeroDivisionError 

  

if not self._parent._base.is_field() and not is_IntegerRing(self._parent._base): 

py_quo = self//right 

py_rem = self - right*py_quo 

return py_quo, py_rem 

  

if self._parent._base.is_finite() and self._parent._base.characteristic() > 1<<29: 

raise NotImplementedError("Division of multivariate polynomials over prime fields with characteristic > 2^29 is not implemented.") 

  

cdef int count = singular_polynomial_length_bounded(self._poly,15) 

if count >= 15: # note that _right._poly must be of shorter length than self._poly for us to care about this call 

sig_on() 

if r!=currRing: rChangeCurrRing(r) # singclap_pdivide 

quo = singclap_pdivide( self._poly, right._poly, r ) 

rem = p_Add_q(p_Copy(self._poly, r), p_Neg(pp_Mult_qq(right._poly, quo, r), r), r) 

if count >= 15: 

sig_off() 

return new_MP(parent, quo), new_MP(parent, rem) 

  

def _singular_init_(self, singular=singular_default): 

""" 

Return a SINGULAR (as in the computer algebra system) string 

representation for this element. 

  

INPUT: 

  

- ``singular`` - interpreter (default: ``singular_default``) 

  

- ``have_ring`` - should the correct ring not be set in 

SINGULAR first (default: ``False``) 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = PolynomialRing(GF(127),3) 

sage: x._singular_init_() 

'x' 

sage: (x^2+37*y+128)._singular_init_() 

'x2+37y+1' 

  

TESTS:: 

  

sage: P(0)._singular_init_() 

'0' 

""" 

self._parent._singular_().set_ring() 

return self._repr_short_() 

  

def sub_m_mul_q(self, MPolynomial_libsingular m, MPolynomial_libsingular q): 

""" 

Return ``self - m*q``, where ``m`` must be a monomial and 

``q`` a polynomial. 

  

INPUT: 

  

- ``m`` - a monomial 

- ``q`` - a polynomial 

  

EXAMPLES:: 

  

sage: P.<x,y,z>=PolynomialRing(QQ,3) 

sage: x.sub_m_mul_q(y,z) 

-y*z + x 

  

TESTS:: 

  

sage: Q.<x,y,z>=PolynomialRing(QQ,3) 

sage: P.<x,y,z>=PolynomialRing(QQ,3) 

sage: P(0).sub_m_mul_q(P(0),P(1)) 

0 

sage: x.sub_m_mul_q(Q.gen(1),Q.gen(2)) 

-y*z + x 

""" 

cdef ring *r = self._parent_ring 

  

if not self._parent is m._parent: 

m = self._parent._coerce_c(m) 

if not self._parent is q._parent: 

q = self._parent._coerce_c(q) 

  

if m._poly and m._poly.next: 

raise ArithmeticError("m must be a monomial.") 

elif not m._poly: 

return self 

  

cdef int le = p_GetMaxExp(m._poly, r) 

cdef int lr = p_GetMaxExp(q._poly, r) 

cdef int esum = le + lr 

  

overflow_check(esum, r) 

  

return new_MP(self._parent, p_Minus_mm_Mult_qq(p_Copy(self._poly, r), m._poly, q._poly, r)) 

  

def _macaulay2_(self, macaulay2=macaulay2): 

""" 

Return a Macaulay2 string representation of this polynomial. 

  

.. NOTE:: 

  

Two identical rings are not canonically isomorphic in M2, 

so we require the user to explicitly set the ring, since 

there is no way to know if the ring has been set or not, 

and setting it twice screws everything up. 

  

EXAMPLES:: 

  

sage: R.<x,y> = PolynomialRing(GF(7), 2) 

sage: f = (x^3 + 2*y^2*x)^7; f # indirect doctest 

x^21 + 2*x^7*y^14 

  

sage: h = macaulay2(f); h # optional - macaulay2 

21 7 14 

x + 2x y 

sage: k = macaulay2(x+y); k # optional - macaulay2 

x + y 

sage: k + h # optional - macaulay2 

21 7 14 

x + 2x y + x + y 

sage: R(h) # optional - macaulay2 

x^21 + 2*x^7*y^14 

sage: R(h^20) == f^20 # optional - macaulay2 

True 

""" 

m2_parent = macaulay2(self.parent()) 

macaulay2.use(m2_parent) 

return macaulay2(repr(self)) 

  

def add_m_mul_q(self, MPolynomial_libsingular m, MPolynomial_libsingular q): 

""" 

Return ``self + m*q``, where ``m`` must be a monomial and 

``q`` a polynomial. 

  

INPUT: 

  

- ``m`` - a monomial 

- ``q`` - a polynomial 

  

EXAMPLES:: 

  

sage: P.<x,y,z>=PolynomialRing(QQ,3) 

sage: x.add_m_mul_q(y,z) 

y*z + x 

  

TESTS:: 

  

sage: R.<x,y,z>=PolynomialRing(QQ,3) 

sage: P.<x,y,z>=PolynomialRing(QQ,3) 

sage: P(0).add_m_mul_q(P(0),P(1)) 

0 

sage: x.add_m_mul_q(R.gen(),R.gen(1)) 

x*y + x 

""" 

cdef ring *r = self._parent_ring 

  

if not self._parent is m._parent: 

m = self._parent._coerce_c(m) 

if not self._parent is q._parent: 

q = self._parent._coerce_c(q) 

  

if m._poly and m._poly.next: 

raise ArithmeticError("m must be a monomial.") 

elif not m._poly: 

return self 

  

cdef int le = p_GetMaxExp(m._poly, r) 

cdef int lr = p_GetMaxExp(q._poly, r) 

cdef int esum = le + lr 

  

overflow_check(esum, r) 

  

return new_MP(self._parent, p_Plus_mm_Mult_qq(p_Copy(self._poly, r), m._poly, q._poly, r)) 

  

def __reduce__(self): 

""" 

Serialize this polynomial. 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = PolynomialRing(QQ,3, order='degrevlex') 

sage: f = 27/113 * x^2 + y*z + 1/2 

sage: f == loads(dumps(f)) 

True 

  

sage: P = PolynomialRing(GF(127),3,names='abc') 

sage: a,b,c = P.gens() 

sage: f = 57 * a^2*b + 43 * c + 1 

sage: f == loads(dumps(f)) 

True 

  

TESTS: 

  

Verify that :trac:`9220` is fixed. 

  

sage: R=QQ['x'] 

sage: S=QQ['x','y'] 

sage: h=S.0^2 

sage: parent(h(R.0,0)) 

Univariate Polynomial Ring in x over Rational Field 

""" 

return unpickle_MPolynomial_libsingular, (self._parent, self.dict()) 

  

def _im_gens_(self, codomain, im_gens): 

""" 

INPUT: 

  

- ``codomain`` 

- ``im_gens`` 

  

EXAMPLES:: 

  

sage: R.<x,y> = PolynomialRing(QQ, 2) 

sage: f = R.hom([y,x], R) 

sage: f(x^2 + 3*y^5) 

3*x^5 + y^2 

  

sage: R.<a,b,c,d> = QQ[] 

sage: S.<u> = QQ[] 

sage: h = R.hom([0,0,0,u], S) # indirect doctest 

sage: h((a+d)^3) 

u^3 

""" 

#TODO: very slow 

n = self.parent().ngens() 

if n == 0: 

return codomain._coerce_(self) 

y = codomain(0) 

for (m,c) in self.dict().iteritems(): 

y += codomain(c)*mul([ im_gens[i]**m[i] for i in range(n) if m[i]]) 

return y 

  

  

def _derivative(self, MPolynomial_libsingular var): 

""" 

Differentiates this polynomial with respect to the provided 

variable. This is completely symbolic so it is also defined 

over finite fields. 

  

INPUT: 

  

- ``variable`` - the derivative is taken with respect to variable 

  

- ``have_ring`` - ignored, accepted for compatibility reasons 

  

.. NOTE:: See also :meth:`derivative` 

  

EXAMPLES:: 

  

sage: R.<x,y> = PolynomialRing(QQ,2) 

sage: f = 3*x^3*y^2 + 5*y^2 + 3*x + 2 

sage: f._derivative(x) 

9*x^2*y^2 + 3 

sage: f._derivative(y) 

6*x^3*y + 10*y 

  

The derivative is also defined over finite fields:: 

  

sage: R.<x,y> = PolynomialRing(GF(2**8, 'a'),2) 

sage: f = x^3*y^2 + y^2 + x + 2 

sage: f._derivative(x) 

x^2*y^2 + 1 

  

""" 

if var is None: 

raise ValueError("you must specify which variable with respect to which to differentiate") 

  

cdef int i, var_i 

  

cdef poly *p 

if var._parent is not self._parent: 

raise TypeError("provided variable is not in same ring as self") 

cdef ring *_ring = self._parent_ring 

if _ring != currRing: 

rChangeCurrRing(_ring) 

  

var_i = -1 

for i from 0 <= i <= _ring.N: 

if p_GetExp(var._poly, i, _ring): 

if var_i == -1: 

var_i = i 

else: 

raise TypeError("provided variable is not univariate") 

  

if var_i == -1: 

raise TypeError("provided variable is constant") 

  

p = pDiff(self._poly, var_i) 

return new_MP(self._parent,p) 

  

def integral(self, MPolynomial_libsingular var): 

""" 

Integrates this polynomial with respect to the provided 

variable. 

  

One requires that `\QQ` is contained in the ring. 

  

INPUT: 

  

- ``variable`` - the integral is taken with respect to variable 

  

EXAMPLES:: 

  

sage: R.<x, y> = PolynomialRing(QQ, 2) 

sage: f = 3*x^3*y^2 + 5*y^2 + 3*x + 2 

sage: f.integral(x) 

3/4*x^4*y^2 + 5*x*y^2 + 3/2*x^2 + 2*x 

sage: f.integral(y) 

x^3*y^3 + 5/3*y^3 + 3*x*y + 2*y 

  

Check that :trac:`15896` is solved:: 

  

sage: s = x+y 

sage: s.integral(x)+x 

1/2*x^2 + x*y + x 

sage: s.integral(x)*s 

1/2*x^3 + 3/2*x^2*y + x*y^2 

  

TESTS:: 

  

sage: z, w = polygen(QQ, 'z, w') 

sage: f.integral(z) 

Traceback (most recent call last): 

... 

TypeError: the variable is not in the same ring as self 

  

sage: f.integral(y**2) 

Traceback (most recent call last): 

... 

TypeError: not a variable in the same ring as self 

  

sage: x,y = polygen(ZZ,'x,y') 

sage: y.integral(x) 

Traceback (most recent call last): 

... 

TypeError: the ring must contain the rational numbers 

""" 

cdef int index 

  

ambient_ring = var.parent() 

if ambient_ring is not self._parent: 

raise TypeError("the variable is not in the same ring as self") 

  

if not ambient_ring.has_coerce_map_from(RationalField()): 

raise TypeError("the ring must contain the rational numbers") 

  

gens = ambient_ring.gens() 

try: 

index = gens.index(var) 

except ValueError: 

raise TypeError("not a variable in the same ring as self") 

  

cdef poly *_p 

cdef poly *mon 

cdef ring *_ring = self._parent_ring 

if _ring != currRing: 

rChangeCurrRing(_ring) 

  

v = ETuple({index: 1}, len(gens)) 

  

_p = p_ISet(0, _ring) 

for (exp, coeff) in self.dict().iteritems(): 

nexp = exp.eadd(v) # new exponent 

mon = p_Init(_ring) 

p_SetCoeff(mon, sa2si(coeff / (1 + exp[index]), _ring), _ring) 

for pos in nexp.nonzero_positions(): 

overflow_check(nexp[pos], _ring) 

p_SetExp(mon, pos + 1, nexp[pos], _ring) 

p_Setm(mon, _ring) 

_p = p_Add_q(_p, mon, _ring) 

return new_MP(self._parent, _p) 

  

def resultant(self, MPolynomial_libsingular other, variable=None): 

""" 

Compute the resultant of this polynomial and the first 

argument with respect to the variable given as the second 

argument. 

  

If a second argument is not provide the first variable of 

the parent is chosen. 

  

INPUT: 

  

- ``other`` - polynomial 

  

- ``variable`` - optional variable (default: ``None``) 

  

EXAMPLES:: 

  

sage: P.<x,y> = PolynomialRing(QQ,2) 

sage: a = x+y 

sage: b = x^3-y^3 

sage: c = a.resultant(b); c 

-2*y^3 

sage: d = a.resultant(b,y); d 

2*x^3 

  

The SINGULAR example:: 

  

sage: R.<x,y,z> = PolynomialRing(GF(32003),3) 

sage: f = 3 * (x+2)^3 + y 

sage: g = x+y+z 

sage: f.resultant(g,x) 

3*y^3 + 9*y^2*z + 9*y*z^2 + 3*z^3 - 18*y^2 - 36*y*z - 18*z^2 + 35*y + 36*z - 24 

  

Resultants are also supported over the Integers:: 

  

sage: R.<x,y,a,b,u>=PolynomialRing(ZZ, 5, order='lex') 

sage: r = (x^4*y^2+x^2*y-y).resultant(x*y-y*a-x*b+a*b+u,x) 

sage: r 

y^6*a^4 - 4*y^5*a^4*b - 4*y^5*a^3*u + y^5*a^2 - y^5 + 6*y^4*a^4*b^2 + 12*y^4*a^3*b*u - 4*y^4*a^2*b + 6*y^4*a^2*u^2 - 2*y^4*a*u + 4*y^4*b - 4*y^3*a^4*b^3 - 12*y^3*a^3*b^2*u + 6*y^3*a^2*b^2 - 12*y^3*a^2*b*u^2 + 6*y^3*a*b*u - 4*y^3*a*u^3 - 6*y^3*b^2 + y^3*u^2 + y^2*a^4*b^4 + 4*y^2*a^3*b^3*u - 4*y^2*a^2*b^3 + 6*y^2*a^2*b^2*u^2 - 6*y^2*a*b^2*u + 4*y^2*a*b*u^3 + 4*y^2*b^3 - 2*y^2*b*u^2 + y^2*u^4 + y*a^2*b^4 + 2*y*a*b^3*u - y*b^4 + y*b^2*u^2 

  

TESTS:: 

  

sage: P.<x,y> = PolynomialRing(QQ, order='degrevlex') 

sage: a = x+y 

sage: b = x^3-y^3 

sage: c = a.resultant(b); c 

-2*y^3 

sage: d = a.resultant(b,y); d 

2*x^3 

  

  

sage: P.<x,y> = PolynomialRing(ZZ,2) 

sage: f = x+y 

sage: g=y^2+x 

sage: f.resultant(g,y) 

x^2 + x 

""" 

cdef ring *_ring = self._parent_ring 

cdef poly *rt 

  

if variable is None: 

variable = self.parent().gen(0) 

  

if not self._parent is other._parent: 

raise TypeError("first parameter needs to be an element of self.parent()") 

  

if not variable.parent() is self.parent(): 

raise TypeError("second parameter needs to be an element of self.parent() or None") 

  

  

if self._parent._base.is_finite() and self._parent._base.characteristic() > 1<<29: 

raise NotImplementedError("Resultants of multivariate polynomials over prime fields with characteristic > 2^29 is not implemented.") 

  

if is_IntegerRing(self._parent._base): 

ret = self.change_ring(RationalField()).resultant(other.change_ring(RationalField()), 

variable.change_ring(RationalField())) 

return ret.change_ring(ZZ) 

elif not self._parent._base.is_field(): 

raise ValueError("Resultants require base fields or integer base ring.") 

  

cdef int count = singular_polynomial_length_bounded(self._poly,20) \ 

+ singular_polynomial_length_bounded(other._poly,20) 

if count >= 20: 

sig_on() 

if _ring != currRing: rChangeCurrRing(_ring) # singclap_resultant 

rt = singclap_resultant(p_Copy(self._poly, _ring), p_Copy(other._poly, _ring), p_Copy((<MPolynomial_libsingular>variable)._poly , _ring ), _ring) 

if count >= 20: 

sig_off() 

return new_MP(self._parent, rt) 

  

def discriminant(self,variable): 

""" 

Returns the discriminant of self with respect to the given variable. 

  

INPUT: 

  

- ``variable`` - The variable with respect to which we compute 

the discriminant 

  

OUTPUT: 

  

- An element of the base ring of the polynomial ring. 

  

  

EXAMPLES:: 

  

sage: R.<x,y,z>=QQ[] 

sage: f=4*x*y^2 + 1/4*x*y*z + 3/2*x*z^2 - 1/2*z^2 

sage: f.discriminant(x) 

1 

sage: f.discriminant(y) 

-383/16*x^2*z^2 + 8*x*z^2 

sage: f.discriminant(z) 

-383/16*x^2*y^2 + 8*x*y^2 

  

Note that, unlike the univariate case, the result lives in 

the same ring as the polynomial:: 

  

sage: R.<x,y>=QQ[] 

sage: f=x^5*y+3*x^2*y^2-2*x+y-1 

sage: f.discriminant(y) 

x^10 + 2*x^5 + 24*x^3 + 12*x^2 + 1 

sage: f.polynomial(y).discriminant() 

x^10 + 2*x^5 + 24*x^3 + 12*x^2 + 1 

sage: f.discriminant(y).parent()==f.polynomial(y).discriminant().parent() 

False 

  

AUTHOR: 

Miguel Marco 

""" 

if self.is_zero(): 

return self.parent().zero() 

n = self.degree(variable) 

d = self.derivative(variable) 

k = d.degree(variable) 

  

r = n % 4 

u = -1 # (-1)**(n*(n-1)/2) 

if r == 0 or r == 1: 

u = 1 

an = self.coefficient(variable**n)**(n - k - 2) 

return self.parent()(u * self.resultant(d, variable) * an) 

  

def coefficients(self): 

""" 

Return the nonzero coefficients of this polynomial in a list. 

The returned list is decreasingly ordered by the term ordering 

of the parent. 

  

EXAMPLES:: 

  

sage: R.<x,y,z> = PolynomialRing(QQ, order='degrevlex') 

sage: f=23*x^6*y^7 + x^3*y+6*x^7*z 

sage: f.coefficients() 

[23, 6, 1] 

  

sage: R.<x,y,z> = PolynomialRing(QQ, order='lex') 

sage: f=23*x^6*y^7 + x^3*y+6*x^7*z 

sage: f.coefficients() 

[6, 23, 1] 

  

AUTHOR: 

  

- Didier Deshommes 

""" 

cdef poly *p 

cdef ring *r = self._parent_ring 

if r!=currRing: rChangeCurrRing(r) 

base = self._parent._base 

p = self._poly 

coeffs = list() 

while p: 

coeffs.append(si2sa(p_GetCoeff(p, r), r, base)) 

p = pNext(p) 

return coeffs 

  

def gradient(self): 

""" 

Return a list of partial derivatives of this polynomial, 

ordered by the variables of the parent. 

  

EXAMPLES:: 

  

sage: P.<x,y,z> = PolynomialRing(QQ,3) 

sage: f= x*y + 1 

sage: f.gradient() 

[y, x, 0] 

""" 

cdef ring *r = self._parent_ring 

cdef int k 

  

if r!=currRing: rChangeCurrRing(r) 

i = [] 

for k from 0 < k <= r.N: 

i.append( new_MP(self._parent, pDiff(self._poly, k))) 

return i 

  

  

def numerator(self): 

""" 

Return a numerator of self computed as self * self.denominator() 

  

If the base_field of self is the Rational Field then the 

numerator is a polynomial whose base_ring is the Integer Ring, 

this is done for compatibility to the univariate case. 

  

.. warning:: 

  

This is not the numerator of the rational function 

defined by self, which would always be self since self is a 

polynomial. 

  

EXAMPLES: 

  

First we compute the numerator of a polynomial with 

integer coefficients, which is of course self. 

  

:: 

  

sage: R.<x, y> = ZZ[] 

sage: f = x^3 + 17*y + 1 

sage: f.numerator() 

x^3 + 17*y + 1 

sage: f == f.numerator() 

True 

  

Next we compute the numerator of a polynomial with rational 

coefficients. 

  

:: 

  

sage: R.<x,y> = PolynomialRing(QQ) 

sage: f = (1/17)*x^19 - (2/3)*y + 1/3; f 

1/17*x^19 - 2/3*y + 1/3 

sage: f.numerator() 

3*x^19 - 34*y + 17 

sage: f == f.numerator() 

False 

sage: f.numerator().base_ring() 

Integer Ring 

  

We check that the computation of numerator and denominator 

is valid. 

  

:: 

  

sage: K=QQ['x,y'] 

sage: f=K.random_element() 

sage: f.numerator() / f.denominator() == f 

True 

  

The following tests against a bug fixed in :trac:`11780`:: 

  

sage: P.<foo,bar> = ZZ[] 

sage: Q.<foo,bar> = QQ[] 

sage: f = Q.random_element() 

sage: f.numerator().parent() is P 

True 

""" 

if self.base_ring() == RationalField(): 

#This part is for compatibility with the univariate case, 

#where the numerator of a polynomial over RationalField 

#is a polynomial over IntegerRing 

# 

# Trac ticket #11780: Create the polynomial ring over 

# the integers using the (cached) polynomial ring constructor: 

from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing 

integer_polynomial_ring = PolynomialRing(ZZ,\ 

self.parent().ngens(), self.parent().gens(), order =\ 

self.parent().term_order()) 

return integer_polynomial_ring(self * self.denominator()) 

else: 

return self * self.denominator() 

  

  

def unpickle_MPolynomial_libsingular(MPolynomialRing_libsingular R, d): 

""" 

Deserialize an ``MPolynomial_libsingular`` object 

  

INPUT: 

  

- ``R`` - the base ring 

- ``d`` - a Python dictionary as returned by :meth:`MPolynomial_libsingular.dict` 

  

EXAMPLES:: 

  

sage: P.<x,y> = PolynomialRing(QQ) 

sage: loads(dumps(x)) == x # indirect doctest 

True 

""" 

cdef ring *r = R._ring 

cdef poly *m 

cdef poly *p 

cdef int _i, _e 

p = p_ISet(0,r) 

rChangeCurrRing(r) 

for mon,c in d.iteritems(): 

m = p_Init(r) 

for i,e in mon.sparse_iter(): 

_i = i 

if _i >= r.N: 

p_Delete(&p, r) 

p_Delete(&m, r) 

raise TypeError("variable index too big") 

_e = e 

if _e <= 0: 

p_Delete(&p, r) 

p_Delete(&m, r) 

raise TypeError("exponent too small") 

overflow_check(_e, r) 

p_SetExp(m, _i+1, _e, r) 

p_SetCoeff(m, sa2si(c, r), r) 

p_Setm(m, r) 

p = p_Add_q(p, m, r) 

return new_MP(R, p) 

  

  

cdef inline poly *addwithcarry(poly *tempvector, poly *maxvector, int pos, ring *_ring): 

if p_GetExp(tempvector, pos, _ring) < p_GetExp(maxvector, pos, _ring): 

p_SetExp(tempvector, pos, p_GetExp(tempvector, pos, _ring)+1, _ring) 

else: 

p_SetExp(tempvector, pos, 0, _ring) 

tempvector = addwithcarry(tempvector, maxvector, pos + 1, _ring) 

p_Setm(tempvector, _ring) 

return tempvector 

  

  

cdef inline MPolynomial_libsingular new_MP(MPolynomialRing_libsingular parent, poly *juice): 

""" 

Construct MPolynomial_libsingular from parent and SINGULAR poly. 

  

INPUT: 

  

- ``parent`` -- a :class:`MPolynomialRing_libsingular`` 

instance. The parent of the polynomial to create. 

  

- ``juice`` -- a pointer to a Singular ``poly`` C struct. 

  

OUTPUT: 

  

A Python object :class:`MPolynomial_libsingular`. 

  

The ownership of ``juice`` will be transferred to the Python 

object. You must not free it yourself. Singular will modify the 

polynomial, so it is your responsibility to make a copy if the 

Singular data structure is used elsewhere. 

""" 

cdef MPolynomial_libsingular p = MPolynomial_libsingular.__new__(MPolynomial_libsingular) 

p._parent = parent 

p._parent_ring = singular_ring_reference(parent._ring) 

p._poly = juice 

p_Normalize(p._poly, p._parent_ring) 

return p 

  

  

cdef poly *MPolynomial_libsingular_get_element(object self): 

return (<MPolynomial_libsingular>self)._poly