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

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

r""" 

Ideals in multivariate polynomial rings. 

 

Sage has a powerful system to compute with multivariate polynomial 

rings. Most algorithms dealing with these ideals are centered on the 

computation of *Groebner bases*. Sage mainly uses Singular to 

implement this functionality. Singular is widely regarded as the best 

open-source system for Groebner basis calculation in multivariate 

polynomial rings over fields. 

 

AUTHORS: 

 

- William Stein 

 

- Kiran S. Kedlaya (2006-02-12): added Macaulay2 analogues of some 

Singular features 

 

- Martin Albrecht (2008,2007): refactoring, many Singular related 

functions 

 

- Martin Albrecht (2009): added Groebner basis over rings 

functionality from Singular 3.1 

 

- John Perry (2012): bug fixing equality & containment of ideals 

 

EXAMPLES: 

 

We compute a Groebner basis for some given ideal. The type returned by 

the ``groebner_basis`` method is ``PolynomialSequence``, i.e. it is not a 

:class:`MPolynomialIdeal`:: 

 

sage: x,y,z = QQ['x,y,z'].gens() 

sage: I = ideal(x^5 + y^4 + z^3 - 1, x^3 + y^3 + z^2 - 1) 

sage: B = I.groebner_basis() 

sage: type(B) 

<class 'sage.rings.polynomial.multi_polynomial_sequence.PolynomialSequence_generic'> 

 

Groebner bases can be used to solve the ideal membership problem:: 

 

sage: f,g,h = B 

sage: (2*x*f + g).reduce(B) 

0 

 

sage: (2*x*f + g) in I 

True 

 

sage: (2*x*f + 2*z*h + y^3).reduce(B) 

y^3 

 

sage: (2*x*f + 2*z*h + y^3) in I 

False 

 

We compute a Groebner basis for Cyclic 6, which is a standard 

benchmark and test ideal. :: 

 

sage: R.<x,y,z,t,u,v> = QQ['x,y,z,t,u,v'] 

sage: I = sage.rings.ideal.Cyclic(R,6) 

sage: B = I.groebner_basis() 

sage: len(B) 

45 

 

We compute in a quotient of a polynomial ring over `\ZZ/17\ZZ`:: 

 

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

sage: S.<a,b> = R.quotient((x^2 + y^2, 17)) 

sage: S 

Quotient of Multivariate Polynomial Ring in x, y over Integer Ring 

by the ideal (x^2 + y^2, 17) 

 

sage: a^2 + b^2 == 0 

True 

sage: a^3 - b^2 

-a*b^2 - b^2 

 

Note that the result of a computation is not necessarily reduced:: 

 

sage: (a+b)^17 

256*a*b^16 + 256*b^17 

sage: S(17) == 0 

True 

 

Or we can work with `\ZZ/17\ZZ` directly:: 

 

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

sage: S.<a,b> = R.quotient((x^2 + y^2,)) 

sage: S 

Quotient of Multivariate Polynomial Ring in x, y over Ring of 

integers modulo 17 by the ideal (x^2 + y^2) 

 

sage: a^2 + b^2 == 0 

True 

sage: a^3 - b^2 == -a*b^2 - b^2 == 16*a*b^2 + 16*b^2 

True 

sage: (a+b)^17 

a*b^16 + b^17 

sage: S(17) == 0 

True 

 

 

Working with a polynomial ring over `\ZZ`:: 

 

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

sage: I = ideal(x^2 + y^2 - z^2 - w^2, x-y) 

sage: J = I^2 

sage: J.groebner_basis() 

[4*y^4 - 4*y^2*z^2 + z^4 - 4*y^2*w^2 + 2*z^2*w^2 + w^4, 

2*x*y^2 - 2*y^3 - x*z^2 + y*z^2 - x*w^2 + y*w^2, 

x^2 - 2*x*y + y^2] 

 

sage: y^2 - 2*x*y + x^2 in J 

True 

sage: 0 in J 

True 

 

We do a Groebner basis computation over a number field:: 

 

sage: K.<zeta> = CyclotomicField(3) 

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

Multivariate Polynomial Ring in x, y, z over Cyclotomic Field of order 3 and degree 2 

 

sage: i = ideal(x - zeta*y + 1, x^3 - zeta*y^3); i 

Ideal (x + (-zeta)*y + 1, x^3 + (-zeta)*y^3) of Multivariate 

Polynomial Ring in x, y, z over Cyclotomic Field of order 3 and degree 2 

 

sage: i.groebner_basis() 

[y^3 + (2*zeta + 1)*y^2 + (zeta - 1)*y + (-1/3*zeta - 2/3), x + (-zeta)*y + 1] 

 

sage: S = R.quotient(i); S 

Quotient of Multivariate Polynomial Ring in x, y, z over 

Cyclotomic Field of order 3 and degree 2 by the ideal (x + 

(-zeta)*y + 1, x^3 + (-zeta)*y^3) 

 

sage: S.0 - zeta*S.1 

-1 

sage: S.0^3 - zeta*S.1^3 

0 

 

Two examples from the Mathematica documentation (done in Sage): 

 

We compute a Groebner basis:: 

 

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

sage: ideal(x^2 - 2*y^2, x*y - 3).groebner_basis() 

[x - 2/3*y^3, y^4 - 9/2] 

 

We show that three polynomials have no common root:: 

 

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

sage: ideal(x+y, x^2 - 1, y^2 - 2*x).groebner_basis() 

[1] 

 

The next example shows how we can use Groebner bases over `\ZZ` to 

find the primes modulo which a system of equations has a solution, 

when the system has no solutions over the rationals. 

 

We first form a certain ideal `I` in `\ZZ[x, y, z]`, and note that 

the Groebner basis of `I` over `\QQ` contains 1, so there are no 

solutions over `\QQ` or an algebraic closure of it (this is not 

surprising as there are 4 equations in 3 unknowns). :: 

 

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

sage: I = ideal(-y^2 - 3*y + z^2 + 3, -2*y*z + z^2 + 2*z + 1, \ 

x*z + y*z + z^2, -3*x*y + 2*y*z + 6*z^2) 

sage: I.change_ring(P.change_ring(QQ)).groebner_basis() 

[1] 

 

However, when we compute the Groebner basis of I (defined over 

`\ZZ`), we note that there is a certain integer in the ideal 

which is not 1. :: 

 

sage: I.groebner_basis() 

[x + 130433*y + 59079*z, y^2 + 3*y + 17220, y*z + 5*y + 14504, 2*y + 158864, z^2 + 17223, 2*z + 41856, 164878] 

 

Now for each prime `p` dividing this integer 164878, the Groebner 

basis of I modulo `p` will be non-trivial and will thus give a 

solution of the original system modulo `p`. :: 

 

 

sage: factor(164878) 

2 * 7 * 11777 

 

sage: I.change_ring(P.change_ring( GF(2) )).groebner_basis() 

[x + y + z, y^2 + y, y*z + y, z^2 + 1] 

sage: I.change_ring(P.change_ring( GF(7) )).groebner_basis() 

[x - 1, y + 3, z - 2] 

sage: I.change_ring(P.change_ring( GF(11777 ))).groebner_basis() 

[x + 5633, y - 3007, z - 2626] 

 

The Groebner basis modulo any product of the prime factors is also non-trivial:: 

 

sage: I.change_ring(P.change_ring( IntegerModRing(2*7) )).groebner_basis() 

[x + 9*y + 13*z, y^2 + 3*y, y*z + 7*y + 6, 2*y + 6, z^2 + 3, 2*z + 10] 

 

Modulo any other prime the Groebner basis is trivial so there are 

no other solutions. For example:: 

 

sage: I.change_ring( P.change_ring( GF(3) ) ).groebner_basis() 

[1] 

 

TESTS:: 

 

sage: x,y,z = QQ['x,y,z'].gens() 

sage: I = ideal(x^5 + y^4 + z^3 - 1, x^3 + y^3 + z^2 - 1) 

sage: I == loads(dumps(I)) 

True 

 

.. NOTE:: 

 

Sage distinguishes between lists or sequences of polynomials and 

ideals. Thus an ideal is not identified with a particular set of 

generators. For sequences of multivariate polynomials see 

:class:`sage.rings.polynomial.multi_polynomial_sequence.PolynomialSequence_generic`. 

 

""" 

 

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

# 

# Sage 

# 

# Copyright (C) 2005 William Stein <wstein@gmail.com> 

# Copyright (C) 2008,2009 Martin Albrecht <malb@informatik.uni-bremen.de> 

# 

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

# 

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

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

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

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

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

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

from __future__ import print_function 

from six import iteritems 

from six.moves import range 

 

from sage.interfaces.all import (singular as singular_default, 

macaulay2 as macaulay2_default, 

magma as magma_default) 

 

from sage.interfaces.expect import StdOutContext 

 

from sage.rings.ideal import Ideal_generic 

from sage.rings.noncommutative_ideals import Ideal_nc 

from sage.rings.integer import Integer 

from sage.structure.sequence import Sequence 

from sage.structure.richcmp import (richcmp_method, op_EQ, op_NE, 

op_LT, op_GT, op_LE, op_GE, rich_to_bool) 

from sage.misc.cachefunc import cached_method 

from sage.misc.all import prod, verbose, get_verbose 

from sage.misc.method_decorator import MethodDecorator 

 

from sage.rings.integer_ring import ZZ 

import sage.rings.polynomial.toy_buchberger as toy_buchberger 

import sage.rings.polynomial.toy_variety as toy_variety 

import sage.rings.polynomial.toy_d_basis as toy_d_basis 

 

from warnings import warn 

 

from sage.interfaces.magma import magma_gb_standard_options 

from sage.interfaces.singular import singular_gb_standard_options 

from sage.libs.singular.standard_options import libsingular_gb_standard_options 

 

class RequireField(MethodDecorator): 

""" 

Decorator which throws an exception if a computation over a 

coefficient ring which is not a field is attempted. 

 

.. NOTE:: 

 

This decorator is used automatically internally so the user 

does not need to use it manually. 

""" 

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

""" 

EXAMPLES:: 

 

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

sage: I = ideal( x^2 - 3*y, y^3 - x*y, z^3 - x, x^4 - y*z + 1 ) 

sage: from sage.rings.polynomial.multi_polynomial_ideal import RequireField 

sage: class Foo(I.__class__): 

....: @RequireField 

....: def bar(I): 

....: return I.groebner_basis() 

....: 

sage: J = Foo(I.ring(), I.gens()) 

sage: J.bar() 

Traceback (most recent call last): 

... 

ValueError: Coefficient ring must be a field for function 'bar'. 

""" 

R = self._instance.ring() 

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

raise ValueError("Coefficient ring must be a field for function '%s'."%(self.f.__name__)) 

return self.f(self._instance, *args, **kwds) 

 

require_field = RequireField 

 

def is_MPolynomialIdeal(x): 

""" 

Return ``True`` if the provided argument ``x`` is an ideal in the 

multivariate polynomial ring. 

 

INPUT: 

 

- ``x`` - an arbitrary object 

 

EXAMPLES:: 

 

sage: from sage.rings.polynomial.multi_polynomial_ideal import is_MPolynomialIdeal 

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

sage: I = [x + 2*y + 2*z - 1, x^2 + 2*y^2 + 2*z^2 - x, 2*x*y + 2*y*z - y] 

 

Sage distinguishes between a list of generators for an ideal and 

the ideal itself. This distinction is inconsistent with Singular 

but matches Magma's behavior. :: 

 

sage: is_MPolynomialIdeal(I) 

False 

 

:: 

 

sage: I = Ideal(I) 

sage: is_MPolynomialIdeal(I) 

True 

""" 

return isinstance(x, MPolynomialIdeal) 

 

class MPolynomialIdeal_magma_repr: 

def _magma_init_(self, magma): 

""" 

Returns a Magma ideal matching this ideal if the base ring 

coerces to Magma and Magma is available. 

 

INPUT: 

 

- ``magma`` - Magma instance 

 

EXAMPLES:: 

 

sage: R.<a,b,c,d,e,f,g,h,i,j> = PolynomialRing(GF(127),10) 

sage: I = sage.rings.ideal.Cyclic(R,4) # indirect doctest 

sage: magma(I) # optional - magma 

Ideal of Polynomial ring of rank 10 over GF(127) 

Order: Graded Reverse Lexicographical 

Variables: a, b, c, d, e, f, g, h, i, j 

Basis: 

[ 

a + b + c + d, 

a*b + b*c + a*d + c*d, 

a*b*c + a*b*d + a*c*d + b*c*d, 

a*b*c*d + 126 

] 

""" 

P = magma(self.ring()) 

G = magma(self.gens()) 

return 'ideal<%s|%s>'%(P.name(), G._ref()) 

 

@magma_gb_standard_options 

def _groebner_basis_magma(self, deg_bound=None, prot=False, magma=magma_default): 

""" 

Computes a Groebner Basis for this ideal using Magma if 

available. 

 

INPUT: 

 

- ``deg_bound`` - only compute to degree ``deg_bound``, that 

is, ignore all S-polynomials of higher degree. (default: 

``None``) 

 

- ``prot`` - if ``True`` Magma's protocol is printed to 

stdout. 

 

- ``magma`` - Magma instance or None (default instance) (default: None) 

 

EXAMPLES:: 

 

sage: R.<a,b,c,d,e,f,g,h,i,j> = PolynomialRing(GF(127),10) 

sage: I = sage.rings.ideal.Cyclic(R,6) 

sage: gb = I.groebner_basis('magma:GroebnerBasis') # indirect doctest; optional - magma 

sage: len(gb) # optional - magma 

45 

 

We may also pass a degree bound to Magma:: 

 

sage: R.<a,b,c,d,e,f,g,h,i,j> = PolynomialRing(GF(127),10) 

sage: I = sage.rings.ideal.Cyclic(R,6) 

sage: gb = I.groebner_basis('magma:GroebnerBasis', deg_bound=4) # indirect doctest; optional - magma 

sage: len(gb) # optional - magma 

7 

""" 

R = self.ring() 

if not deg_bound: 

mself = magma(self) 

else: 

mself = magma(list(self.gens())) # PolynomialSequence converts to a Magma Ideal too, so we force a list 

 

if get_verbose() >= 2: 

prot = True 

 

from sage.interfaces.magma import MagmaGBLogPrettyPrinter 

 

if prot: 

log_parser = MagmaGBLogPrettyPrinter(verbosity=get_verbose()+ 1, style="sage" if prot=="sage" else "magma") 

else: 

log_parser = None 

 

ctx = StdOutContext(magma, silent=False if prot else True, stdout=log_parser) 

if prot: 

magma.SetVerbose('Groebner',1) 

with ctx: 

if deg_bound: 

mgb = mself.GroebnerBasis(deg_bound) 

else: 

mgb = mself.GroebnerBasis() 

 

if prot == "sage": 

print("") 

print("Highest degree reached during computation: %2d." % log_parser.max_deg) 

 

# TODO: rewrite this to be much more sophisticated in multi-level nested cases. 

mgb = [str(mgb[i+1]) for i in range(len(mgb))] 

if R.base_ring().degree() > 1: 

a = str(R.base_ring().gen()) 

mgb = [e.replace("$.1",a) for e in mgb] 

 

from sage.rings.polynomial.multi_polynomial_sequence import PolynomialSequence 

 

B = PolynomialSequence([R(e) for e in mgb], R, immutable=True) 

return B 

 

class MPolynomialIdeal_singular_base_repr: 

@require_field 

def syzygy_module(self): 

r""" 

Computes the first syzygy (i.e., the module of relations of the 

given generators) of the ideal. 

 

EXAMPLES:: 

 

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

sage: f = 2*x^2 + y 

sage: g = y 

sage: h = 2*f + g 

sage: I = Ideal([f,g,h]) 

sage: M = I.syzygy_module(); M 

[ -2 -1 1] 

[ -y 2*x^2 + y 0] 

sage: G = vector(I.gens()) 

sage: M*G 

(0, 0) 

 

ALGORITHM: Uses Singular's syz command 

""" 

import sage.libs.singular.function_factory 

syz = sage.libs.singular.function_factory.ff.syz 

from sage.matrix.constructor import matrix 

 

#return self._singular_().syz().transpose().sage_matrix(self.ring()) 

S = syz(self) 

return matrix(self.ring(), S) 

 

@libsingular_gb_standard_options 

def _groebner_basis_libsingular(self, algorithm="groebner", *args, **kwds): 

""" 

Return the reduced Groebner basis of this ideal. 

 

If the Groebner basis for this ideal has been calculated 

before, the cached Groebner basis is returned regardless 

of the requested algorithm. 

 

INPUT: 

 

- ``algorithm`` -- see below for available algorithms 

- ``redsb`` -- (default: ``True``) return a reduced Groebner basis 

- ``red_tail`` -- (default: ``True``) perform tail reduction 

 

ALGORITHMS: 

 

- ``'groebner'`` -- Singular's heuristic script (default) 

- ``'std'`` -- Buchberger's algorithm 

- ``'slimgb'`` -- the *SlimGB* algorithm 

- ``'stdhilb'`` -- Hilbert Basis driven Groebner basis 

- ``'stdfglm'`` -- Buchberger and FGLM 

 

EXAMPLES: 

 

We compute a Groebner basis of 'cyclic 4' relative to 

lexicographic ordering. :: 

 

sage: R.<a,b,c,d> = PolynomialRing(QQ, 4, order='lex') 

sage: I = sage.rings.ideal.Cyclic(R,4); I 

Ideal (a + b + c + d, a*b + a*d + b*c + c*d, a*b*c + a*b*d 

+ a*c*d + b*c*d, a*b*c*d - 1) of Multivariate Polynomial 

Ring in a, b, c, d over Rational Field 

 

:: 

 

sage: I._groebner_basis_libsingular() 

[c^2*d^6 - c^2*d^2 - d^4 + 1, c^3*d^2 + c^2*d^3 - c - d, 

b*d^4 - b + d^5 - d, b*c - b*d + c^2*d^4 + c*d - 2*d^2, 

b^2 + 2*b*d + d^2, a + b + c + d] 

 

TESTS: 

 

We check that :trac:`17676` is fixed:: 

 

sage: R.<x,y,z> = PolynomialRing(ZZ, 3, order='lex') 

sage: I = Ideal(13*x*y*z+6*x*y+78*x*z+36*x-11*y^2*z-66*y*z, 

....: 168*x*y*z+84*x*y+1008*x*z+504*x+12*y^3 

....: -154*y^2*z+72*y^2-924*y*z, 

....: -168*x^2*y*z^2-84*x^2*y*z-1008*x^2*z^2-504*x^2*z 

....: +x*y^3*z+6*x*y^3+154*x*y^2*z^2+6*x*y^2*z 

....: +36*x*y^2+924*x*y*z^2-11*y^4*z-66*y^3*z); 

sage: gI = R * (I.groebner_basis()) 

sage: len(gI.gens()) 

3 

""" 

from sage.rings.polynomial.multi_polynomial_ideal_libsingular import std_libsingular, slimgb_libsingular 

from sage.libs.singular.function import singular_function 

from sage.libs.singular.option import opt 

from sage.misc.stopgap import stopgap 

 

import sage.libs.singular.function_factory 

groebner = sage.libs.singular.function_factory.ff.groebner 

 

if get_verbose()>=2: 

opt['prot'] = True 

for name, value in iteritems(kwds): 

if value is not None: 

opt[name] = value 

 

T = self.ring().term_order() 

 

if algorithm == "std": 

S = std_libsingular(self) 

elif algorithm == "slimgb": 

S = slimgb_libsingular(self) 

elif algorithm == "groebner": 

S = groebner(self) 

else: 

try: 

fnc = singular_function(algorithm) 

S = fnc(self) 

except NameError: 

raise NameError("Algorithm '%s' unknown"%algorithm) 

return S 

 

 

class MPolynomialIdeal_singular_repr( 

MPolynomialIdeal_singular_base_repr): 

""" 

An ideal in a multivariate polynomial ring, which has an 

underlying Singular ring associated to it. 

""" 

def _singular_(self, singular=singular_default): 

""" 

Return Singular ideal corresponding to this ideal. 

 

EXAMPLES:: 

 

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

sage: I = R.ideal([x^3 + y, y]) 

sage: S = I._singular_() 

sage: S 

x^3+y, 

y 

""" 

try: 

self.ring()._singular_(singular).set_ring() 

I = self.__singular 

if not (I.parent() is singular): 

raise ValueError 

I._check_valid() 

return I 

except (AttributeError, ValueError): 

self.ring()._singular_(singular).set_ring() 

try: 

# this may fail for quotient ring elements, but is 

# faster 

gens = [str(x) for x in self.gens()] 

if len(gens) == 0: 

gens = ['0'] 

self.__singular = singular.ideal(gens) 

except TypeError: 

gens = [str(x.lift()) for x in self.gens()] 

if len(gens) == 0: 

gens = ['0'] 

self.__singular = singular.ideal(gens) 

return self.__singular 

 

@cached_method 

def _groebner_strategy(self): 

""" 

Return Singular's Groebner Strategy object for the Groebner 

basis of this ideal which implements some optimized functions. 

 

EXAMPLES:: 

 

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

sage: I = R.ideal([y^3 - x^2]) 

sage: I._groebner_strategy() 

Groebner Strategy for ideal generated by 1 elements over 

Multivariate Polynomial Ring in x, y over Rational Field 

 

.. NOTE:: 

 

This function is mainly used internally. 

""" 

from sage.libs.singular.groebner_strategy import GroebnerStrategy 

 

return GroebnerStrategy(MPolynomialIdeal(self.ring(), self.groebner_basis())) 

 

def plot(self, singular=singular_default): 

r""" 

If you somehow manage to install surf, perhaps you can use 

this function to implicitly plot the real zero locus of this 

ideal (if principal). 

 

INPUT: 

 

 

- ``self`` - must be a principal ideal in 2 or 3 vars 

over `\QQ`. 

 

 

EXAMPLES: 

 

Implicit plotting in 2-d:: 

 

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

sage: I = R.ideal([y^3 - x^2]) 

sage: I.plot() # cusp 

Graphics object consisting of 1 graphics primitive 

sage: I = R.ideal([y^2 - x^2 - 1]) 

sage: I.plot() # hyperbola 

Graphics object consisting of 1 graphics primitive 

sage: I = R.ideal([y^2 + x^2*(1/4) - 1]) 

sage: I.plot() # ellipse 

Graphics object consisting of 1 graphics primitive 

sage: I = R.ideal([y^2-(x^2-1)*(x-2)]) 

sage: I.plot() # elliptic curve 

Graphics object consisting of 1 graphics primitive 

 

Implicit plotting in 3-d:: 

 

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

sage: I = R.ideal([y^2 + x^2*(1/4) - z]) 

sage: I.plot() # a cone; optional - surf 

sage: I = R.ideal([y^2 + z^2*(1/4) - x]) 

sage: I.plot() # same code, from a different angle; optional - surf 

sage: I = R.ideal([x^2*y^2+x^2*z^2+y^2*z^2-16*x*y*z]) 

sage: I.plot() # Steiner surface; optional - surf 

 

AUTHORS: 

 

- David Joyner (2006-02-12) 

""" 

if self.ring().characteristic() != 0: 

raise TypeError("base ring must have characteristic 0") 

if not self.is_principal(): 

raise TypeError("self must be principal") 

singular.lib('surf') 

I = singular(self) 

I.plot() 

 

@require_field 

@libsingular_gb_standard_options 

def complete_primary_decomposition(self, algorithm="sy"): 

r""" 

Return a list of primary ideals such that their intersection 

is ``self``, together with the associated prime ideals. 

 

An ideal `Q` is called primary if it is a proper ideal of the 

ring `R`, and if whenever `ab \in Q` and `a \not\in Q`, then 

`b^n \in Q` for some `n \in \ZZ`. 

 

If `Q` is a primary ideal of the ring `R`, then the radical 

ideal `P` of `Q` (i.e. the ideal consisting of all `a \in R` 

with a^n \in Q` for some `n \in \ZZ`), is called the 

associated prime of `Q`. 

 

If `I` is a proper ideal of a Noetherian ring `R`, then there 

exists a finite collection of primary ideals `Q_i` such that 

the following hold: 

 

- the intersection of the `Q_i` is `I`; 

 

- none of the `Q_i` contains the intersection of the others; 

 

- the associated prime ideals `P_i` of the `Q_i` are pairwise 

distinct. 

 

INPUT: 

 

- ``algorithm`` -- string: 

 

- ``'sy'`` -- (default) use the Shimoyama-Yokoyama 

algorithm 

 

- ``'gtz'`` -- use the Gianni-Trager-Zacharias algorithm 

 

OUTPUT: 

 

- a list of pairs `(Q_i, P_i)`, where the `Q_i` form a primary 

decomposition of ``self`` and `P_i` is the associated prime 

of `Q_i`. 

 

EXAMPLES:: 

 

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

sage: p = z^2 + 1; q = z^3 + 2 

sage: I = (p*q^2, y-z^2)*R 

sage: pd = I.complete_primary_decomposition(); pd 

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

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

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

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

 

sage: I.primary_decomposition_complete(algorithm = 'gtz') 

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

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

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

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

 

sage: from functools import reduce 

sage: reduce(lambda Qi,Qj: Qi.intersection(Qj), [Qi for (Qi,radQi) in pd]) == I 

True 

 

sage: [Qi.radical() == radQi for (Qi,radQi) in pd] 

[True, True] 

 

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

sage: I = ideal( x^2 - 3*y, y^3 - x*y, z^3 - x, x^4 - y*z + 1 ) 

sage: I.complete_primary_decomposition() 

Traceback (most recent call last): 

... 

ValueError: Coefficient ring must be a field for function 'complete_primary_decomposition'. 

 

ALGORITHM: 

 

Uses Singular. 

 

.. NOTE:: 

 

See [BW93]_ for an introduction to primary decomposition. 

 

TESTS: 

 

Check that :trac:`15745` is fixed:: 

 

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

sage: I = Ideal(R(1)) 

sage: I.complete_primary_decomposition() 

[] 

sage: I.is_prime() 

False 

 

""" 

try: 

return self.__complete_primary_decomposition[algorithm] 

except AttributeError: 

self.__complete_primary_decomposition = {} 

except KeyError: 

pass 

 

# Avoid a bug in Singular (see #15745). 

if self.is_one(): 

return [] 

 

import sage.libs.singular.function_factory 

 

if algorithm == 'sy': 

primdecSY = sage.libs.singular.function_factory.ff.primdec__lib.primdecSY 

P = primdecSY(self) 

elif algorithm == 'gtz': 

primdecGTZ = sage.libs.singular.function_factory.ff.primdec__lib.primdecGTZ 

P = primdecGTZ(self) 

 

R = self.ring() 

V = [(R.ideal(X[0]), R.ideal(X[1])) for X in P] 

V = Sequence(V) 

self.__complete_primary_decomposition[algorithm] = V 

return self.__complete_primary_decomposition[algorithm] 

 

# Seems useful for Tab-Completion 

primary_decomposition_complete = complete_primary_decomposition 

 

@require_field 

def primary_decomposition(self, algorithm='sy'): 

r""" 

Return a list of primary ideals such that their intersection 

is ``self``. 

 

An ideal `Q` is called primary if it is a proper ideal of the 

ring `R`, and if whenever `ab \in Q` and `a \not\in Q`, then 

`b^n \in Q` for some `n \in \ZZ`. 

 

If `Q` is a primary ideal of the ring `R`, then the radical 

ideal `P` of `Q` (i.e. the ideal consisting of all `a \in R` 

with a^n \in Q` for some `n \in \ZZ`), is called the 

associated prime of `Q`. 

 

If `I` is a proper ideal of a Noetherian ring `R`, then there 

exists a finite collection of primary ideals `Q_i` such that 

the following hold: 

 

- the intersection of the `Q_i` is `I`; 

 

- none of the `Q_i` contains the intersection of the others; 

 

- the associated prime ideals of the `Q_i` are pairwise 

distinct. 

 

INPUT: 

 

- ``algorithm`` -- string: 

 

- ``'sy'`` -- (default) use the Shimoyama-Yokoyama 

algorithm 

 

- ``'gtz'`` -- use the Gianni-Trager-Zacharias algorithm 

 

OUTPUT: 

 

- a list of primary ideals `Q_i` forming a primary 

decomposition of ``self``. 

 

EXAMPLES:: 

 

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

sage: p = z^2 + 1; q = z^3 + 2 

sage: I = (p*q^2, y-z^2)*R 

sage: pd = I.primary_decomposition(); pd 

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

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

 

:: 

 

sage: from functools import reduce 

sage: reduce(lambda Qi,Qj: Qi.intersection(Qj), pd) == I 

True 

 

ALGORITHM: 

 

Uses Singular. 

 

REFERENCES: 

 

- Thomas Becker and Volker Weispfenning. Groebner Bases - A 

Computational Approach To Commutative Algebra. Springer, New 

York 1993. 

""" 

return [I for I, _ in self.complete_primary_decomposition(algorithm)] 

 

@require_field 

def associated_primes(self, algorithm='sy'): 

r""" 

Return a list of the associated primes of primary ideals of 

which the intersection is `I` = ``self``. 

 

An ideal `Q` is called primary if it is a proper ideal of 

the ring `R` and if whenever `ab \in Q` and 

`a \not\in Q` then `b^n \in Q` for some 

`n \in \ZZ`. 

 

If `Q` is a primary ideal of the ring `R`, then the 

radical ideal `P` of `Q`, i.e. 

`P = \{a \in R, a^n \in Q\}` for some 

`n \in \ZZ`, is called the 

*associated prime* of `Q`. 

 

If `I` is a proper ideal of the ring `R` then there 

exists a decomposition in primary ideals `Q_i` such that 

 

- their intersection is `I` 

 

- none of the `Q_i` contains the intersection of the 

rest, and 

 

- the associated prime ideals of `Q_i` are pairwise 

different. 

 

 

This method returns the associated primes of the `Q_i`. 

 

INPUT: 

 

 

- ``algorithm`` - string: 

 

- ``'sy'`` - (default) use the Shimoyama-Yokoyama algorithm 

 

- ``'gtz'`` - use the Gianni-Trager-Zacharias algorithm 

 

 

OUTPUT: 

 

- ``list`` - a list of associated primes 

 

EXAMPLES:: 

 

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

sage: p = z^2 + 1; q = z^3 + 2 

sage: I = (p*q^2, y-z^2)*R 

sage: pd = I.associated_primes(); pd 

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

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

 

ALGORITHM: 

 

Uses Singular. 

 

REFERENCES: 

 

- Thomas Becker and Volker Weispfenning. Groebner Bases - A 

Computational Approach To Commutative Algebra. Springer, New 

York 1993. 

""" 

return [P for _,P in self.complete_primary_decomposition(algorithm)] 

 

def is_prime(self, **kwds): 

r""" 

Return ``True`` if this ideal is prime. 

 

INPUT: 

 

- keyword arguments are passed on to 

``complete_primary_decomposition``; in this way you can 

specify the algorithm to use. 

 

EXAMPLES:: 

 

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

sage: I = (x^2 - y^2 - 1)*R 

sage: I.is_prime() 

True 

sage: (I^2).is_prime() 

False 

 

sage: J = (x^2 - y^2)*R 

sage: J.is_prime() 

False 

sage: (J^3).is_prime() 

False 

 

sage: (I * J).is_prime() 

False 

 

The following is :trac:`5982`. Note that the quotient ring 

is not recognized as being a field at this time, so the 

fraction field is not the quotient ring itself:: 

 

sage: Q = R.quotient(I); Q 

Quotient of Multivariate Polynomial Ring in x, y over Rational Field by the ideal (x^2 - y^2 - 1) 

sage: Q.fraction_field() 

Fraction Field of Quotient of Multivariate Polynomial Ring in x, y over Rational Field by the ideal (x^2 - y^2 - 1) 

""" 

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

raise NotImplementedError 

CPD = self.complete_primary_decomposition(**kwds) 

if len(CPD) != 1: 

return False 

_, P = CPD[0] 

return self == P 

 

@require_field 

@singular_gb_standard_options 

@libsingular_gb_standard_options 

def triangular_decomposition(self, algorithm=None, singular=singular_default): 

""" 

Decompose zero-dimensional ideal ``self`` into triangular 

sets. 

 

This requires that the given basis is reduced w.r.t. to the 

lexicographical monomial ordering. If the basis of self does 

not have this property, the required Groebner basis is 

computed implicitly. 

 

INPUT: 

 

- ``algorithm`` - string or None (default: None) 

 

ALGORITHMS: 

 

- ``singular:triangL`` - decomposition of self into triangular 

systems (Lazard). 

 

- ``singular:triangLfak`` - decomp. of self into tri. systems 

plus factorization. 

 

- ``singular:triangM`` - decomposition of self into 

triangular systems (Moeller). 

 

OUTPUT: a list `T` of lists `t` such that the variety of 

``self`` is the union of the varieties of `t` in `L` and each 

`t` is in triangular form. 

 

EXAMPLES:: 

 

sage: P.<e,d,c,b,a> = PolynomialRing(QQ,5,order='lex') 

sage: I = sage.rings.ideal.Cyclic(P) 

sage: GB = Ideal(I.groebner_basis('libsingular:stdfglm')) 

sage: GB.triangular_decomposition('singular:triangLfak') 

[Ideal (a - 1, b - 1, c - 1, d^2 + 3*d + 1, e + d + 3) of Multivariate Polynomial Ring in e, d, c, b, a over Rational Field, 

Ideal (a - 1, b - 1, c^2 + 3*c + 1, d + c + 3, e - 1) of Multivariate Polynomial Ring in e, d, c, b, a over Rational Field, 

Ideal (a - 1, b^2 + 3*b + 1, c + b + 3, d - 1, e - 1) of Multivariate Polynomial Ring in e, d, c, b, a over Rational Field, 

Ideal (a - 1, b^4 + b^3 + b^2 + b + 1, -c + b^2, -d + b^3, e + b^3 + b^2 + b + 1) of Multivariate Polynomial Ring in e, d, c, b, a over Rational Field, 

Ideal (a^2 + 3*a + 1, b - 1, c - 1, d - 1, e + a + 3) of Multivariate Polynomial Ring in e, d, c, b, a over Rational Field, 

Ideal (a^2 + 3*a + 1, b + a + 3, c - 1, d - 1, e - 1) of Multivariate Polynomial Ring in e, d, c, b, a over Rational Field, 

Ideal (a^4 - 4*a^3 + 6*a^2 + a + 1, -11*b^2 + 6*b*a^3 - 26*b*a^2 + 41*b*a - 4*b - 8*a^3 + 31*a^2 - 40*a - 24, 11*c + 3*a^3 - 13*a^2 + 26*a - 2, 11*d + 3*a^3 - 13*a^2 + 26*a - 2, -11*e - 11*b + 6*a^3 - 26*a^2 + 41*a - 4) of Multivariate Polynomial Ring in e, d, c, b, a over Rational Field, 

Ideal (a^4 + a^3 + a^2 + a + 1, b - 1, c + a^3 + a^2 + a + 1, -d + a^3, -e + a^2) of Multivariate Polynomial Ring in e, d, c, b, a over Rational Field, 

Ideal (a^4 + a^3 + a^2 + a + 1, b - a, c - a, d^2 + 3*d*a + a^2, e + d + 3*a) of Multivariate Polynomial Ring in e, d, c, b, a over Rational Field, 

Ideal (a^4 + a^3 + a^2 + a + 1, b - a, c^2 + 3*c*a + a^2, d + c + 3*a, e - a) of Multivariate Polynomial Ring in e, d, c, b, a over Rational Field, 

Ideal (a^4 + a^3 + a^2 + a + 1, b^2 + 3*b*a + a^2, c + b + 3*a, d - a, e - a) of Multivariate Polynomial Ring in e, d, c, b, a over Rational Field, 

Ideal (a^4 + a^3 + a^2 + a + 1, b^3 + b^2*a + b^2 + b*a^2 + b*a + b + a^3 + a^2 + a + 1, c + b^2*a^3 + b^2*a^2 + b^2*a + b^2, -d + b^2*a^2 + b^2*a + b^2 + b*a^2 + b*a + a^2, -e + b^2*a^3 - b*a^2 - b*a - b - a^2 - a) of Multivariate Polynomial Ring in e, d, c, b, a over Rational Field, 

Ideal (a^4 + a^3 + 6*a^2 - 4*a + 1, -11*b^2 + 6*b*a^3 + 10*b*a^2 + 39*b*a + 2*b + 16*a^3 + 23*a^2 + 104*a - 24, 11*c + 3*a^3 + 5*a^2 + 25*a + 1, 11*d + 3*a^3 + 5*a^2 + 25*a + 1, -11*e - 11*b + 6*a^3 + 10*a^2 + 39*a + 2) of Multivariate Polynomial Ring in e, d, c, b, a over Rational Field] 

 

sage: R.<x1,x2> = PolynomialRing(QQ, 2, order='lex') 

sage: f1 = 1/2*((x1^2 + 2*x1 - 4)*x2^2 + 2*(x1^2 + x1)*x2 + x1^2) 

sage: f2 = 1/2*((x1^2 + 2*x1 + 1)*x2^2 + 2*(x1^2 + x1)*x2 - 4*x1^2) 

sage: I = Ideal(f1,f2) 

sage: I.triangular_decomposition() 

[Ideal (x2, x1^2) of Multivariate Polynomial Ring in x1, x2 over Rational Field, 

Ideal (x2, x1^2) of Multivariate Polynomial Ring in x1, x2 over Rational Field, 

Ideal (x2, x1^2) of Multivariate Polynomial Ring in x1, x2 over Rational Field, 

Ideal (x2^4 + 4*x2^3 - 6*x2^2 - 20*x2 + 5, 8*x1 - x2^3 + x2^2 + 13*x2 - 5) of Multivariate Polynomial Ring in x1, x2 over Rational Field] 

 

TESTS:: 

 

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

sage: J = Ideal(x^2+y^2-2, y^2-1) 

sage: J.triangular_decomposition() 

[Ideal (y^2 - 1, x^2 - 1) of Multivariate Polynomial Ring in x, y over Rational Field] 

""" 

P = self.ring() 

 

is_groebner = self.basis_is_groebner() 

 

# make sure to work w.r.t. 'lex' 

if P.term_order() != 'lex': 

Q = P.change_ring(order='lex') 

else: 

Q = P 

 

# the Singular routines are quite picky about their input. 

if is_groebner: 

if Q == P: 

I = MPolynomialIdeal(P, self.interreduced_basis()[::-1]) 

else: 

I = self 

I = MPolynomialIdeal(P, I.transformed_basis('fglm')[::-1]) # -> 'lex' 

I = I.change_ring(Q) # transform to 'lex' GB 

else: 

if Q == P: 

I = MPolynomialIdeal(P, self.groebner_basis()[::-1]) 

else: 

I = self.change_ring(Q) # transform to 'lex' GB 

I = MPolynomialIdeal(Q, I.groebner_basis()[::-1]) 

 

if I.dimension() != 0: 

raise TypeError("dimension must be zero") 

 

from sage.libs.singular.function import singular_function 

from sage.libs.singular.function import lib as singular_lib 

 

singular_lib('triang.lib') 

 

if algorithm is None: 

algorithm = "singular:triangL" 

 

if algorithm in ("singular:triangL","singular:triangLfak","singular:triangM"): 

f = singular_function(algorithm[9:]) 

Tbar = f(I, attributes={I:{'isSB':1}}) 

else: 

raise TypeError("algorithm '%s' unknown"%algorithm) 

 

T = Sequence([ MPolynomialIdeal(Q,t) for t in Tbar]) 

return sorted(T, key=lambda x: x.gens()) 

 

@require_field 

def dimension(self, singular=singular_default): 

""" 

The dimension of the ring modulo this ideal. 

 

EXAMPLES:: 

 

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

sage: I = ideal(x^2-y,x^3) 

sage: I.dimension() 

1 

 

If the ideal is the total ring, the dimension is `-1` by convention. 

 

For polynomials over a finite field of order too large for Singular, 

this falls back on a toy implementation of Buchberger to compute 

the Groebner basis, then uses the algorithm described in Chapter 9, 

Section 1 of Cox, Little, and O'Shea's "Ideals, Varieties, and Algorithms". 

 

EXAMPLES:: 

 

sage: R.<x,y> = PolynomialRing(GF(2147483659),order='lex') 

sage: I = R.ideal([x*y,x*y+1]) 

sage: I.dimension() 

verbose 0 (...: multi_polynomial_ideal.py, dimension) Warning: falling back to very slow toy implementation. 

-1 

sage: I=ideal([x*(x*y+1),y*(x*y+1)]) 

sage: I.dimension() 

verbose 0 (...: multi_polynomial_ideal.py, dimension) Warning: falling back to very slow toy implementation. 

1 

sage: I = R.ideal([x^3*y,x*y^2]) 

sage: I.dimension() 

verbose 0 (...: multi_polynomial_ideal.py, dimension) Warning: falling back to very slow toy implementation. 

1 

sage: R.<x,y> = PolynomialRing(GF(2147483659),order='lex') 

sage: I = R.ideal(0) 

sage: I.dimension() 

verbose 0 (...: multi_polynomial_ideal.py, dimension) Warning: falling back to very slow toy implementation. 

2 

 

ALGORITHM: 

 

Uses Singular, unless the characteristic is too large. 

 

.. NOTE:: 

 

Requires computation of a Groebner basis, which can be a 

very expensive operation. 

 

""" 

try: 

return self.__dimension 

except AttributeError: 

try: 

import sage.libs.singular.function_factory 

dim = sage.libs.singular.function_factory.ff.dim 

v = MPolynomialIdeal(self.ring(),self.groebner_basis()) 

self.__dimension = Integer(dim(v, attributes={v:{'isSB':1}})) 

except TypeError: 

try: 

v = self._groebner_basis_singular_raw() 

self.__dimension = Integer(v.dim()) 

except TypeError: 

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

raise NotImplementedError("dimension() is implemented only over fields.") 

if self.ring().term_order().is_global(): 

verbose("Warning: falling back to very slow toy implementation.", level=0) 

# See Chapter 9, Section 1 of Cox, Little, O'Shea's "Ideals, Varieties, 

# and Algorithms". 

from sage.sets.set import Set 

gb = toy_buchberger.buchberger_improved(self) 

if self.ring().one() in gb: 

return Integer(-1) 

ring_vars = self.ring().gens() 

n = len(ring_vars) 

lms = [each.lm() for each in gb] 

# compute M_j, denoted by var_lms 

var_lms = [Set([]) for each in lms] 

for j in range(len(ring_vars)): 

for i in range(len(lms)): 

if lms[i].degree(ring_vars[j]) > 0: 

var_lms[i] += Set([j+1]) 

# compute intersections of M_j and J 

# we assume that the iterator starts with the empty set, 

# then iterates through all subsets of order 1, 

# then through all subsets of order 2, etc... 

# the way Sage currently operates 

all_J = Set([each + 1 for each in range(n)]).subsets() 

min_dimension = -1 

all_J = iter(all_J) 

while min_dimension == -1: 

try: 

J = next(all_J) 

except StopIteration: 

min_dimension = n 

break 

J_intersects_all = True 

i = 0 

while J_intersects_all and i < len(var_lms): 

J_intersects_all = J.intersection(var_lms[i]) != Set([]) 

i += 1 

if J_intersects_all: 

min_dimension = len(J) 

return Integer(n - min_dimension) 

else: 

raise TypeError("Local/unknown orderings not supported by 'toy_buchberger' implementation.") 

return self.__dimension 

 

@require_field 

def vector_space_dimension(self): 

""" 

Return the vector space dimension of the ring modulo this ideal. If 

the ideal is not zero-dimensional, a TypeError is raised. 

 

ALGORITHM: 

 

Uses Singular. 

 

EXAMPLES:: 

 

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

sage: g = u^4 + v^4 + u^3 + v^3 

sage: I = ideal(g) + ideal(g.gradient()) 

sage: I.dimension() 

0 

sage: I.vector_space_dimension() 

4 

 

When the ideal is not zero-dimensional, we return infinity:: 

 

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

sage: I = R.ideal(x) 

sage: I.dimension() 

1 

sage: I.vector_space_dimension() 

+Infinity 

""" 

R = self.ring() 

gb = R.ideal(self.groebner_basis()) 

 

import sage.libs.singular.function_factory 

vdim = sage.libs.singular.function_factory.ff.vdim 

vd = Integer(vdim(gb, attributes={gb:{'isSB':1}})) 

 

if vd == -1: 

from sage.rings.infinity import Infinity 

return Infinity 

else: 

return vd 

 

@require_field 

def _groebner_basis_ginv(self, algorithm="TQ", criteria='CritPartially', division_interface="Janet"): 

r""" 

Compute a Groebner basis using GINV. 

 

INPUT: 

 

- ``algorithm`` - "TQ", "TQBlockHigh", "TQBlockLow" or "TQDegree" 

- ``criteria`` - "Without" (without any criteria) 

- "C1", "CritPartially" (partial involutive criteria) 

- "C1C2C3", "C1C2C3C4" (full involutive criteria) 

 

- ``division_interface`` - either "Janet" or "JanetLike" 

 

EXAMPLES: 

 

Currently, only `\GF{p}` and `\QQ` are supported as base fields:: 

 

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

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

sage: I.groebner_basis(algorithm='ginv') # optional - ginv 

[z^3 - 79/210*z^2 + 1/30*y + 1/70*z, y^2 - 3/5*z^2 - 1/5*y + 1/5*z, y*z + 6/5*z^2 - 1/10*y - 2/5*z, x + 2*y + 2*z - 1] 

 

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

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

sage: I.groebner_basis(algorithm='ginv') # optional - ginv 

... 

[z^3 + 22*z^2 - 55*y + 49*z, y^2 - 26*z^2 - 51*y + 51*z, y*z + 52*z^2 + 38*y + 25*z, x + 2*y + 2*z - 1] 

 

.. NOTE:: 

 

Criterion C1 is Buchberger's co-prime criterion. Criteria 

C2, C3 and C4 in the aggregate are equivalent to the second 

(chain) Buchberger's criterion. Supported term orderings are 

'lex' and 'degrevlex', supported base rings are `\GF{p}` with 

`p < 2^16` and `\QQ`. 

""" 

P = self.ring() 

T = P.term_order() 

K = P.base_ring() 

 

try: 

import ginv 

except ImportError: 

from sage.misc.package import PackageNotFoundError 

raise PackageNotFoundError("ginv") 

 

st = ginv.SystemType("Polynomial") 

 

term_order_map = {'degrevlex':"DegRevLex",'lex':"Lex"} 

try: 

im = ginv.MonomInterface(term_order_map[T.name()], st, list(P.variable_names())) 

except KeyError: 

raise NotImplementedError("Term order '%s' not supported by Sage's GINV interface or GINV"%T.term_order()) 

 

from sage.all import QQ 

if K is QQ: 

ic = ginv.CoeffInterface("GmpQ", st) 

elif K.order() <= 2**16 and K.order().is_prime(): 

ic = ginv.CoeffInterface("ModularShort", st, modularShort=K.order()) 

else: 

raise NotImplementedError("GINV interface for base ring '%s' is not implemented."%K) 

 

ip = ginv.PolyInterface("PolyList", st, im, ic) 

iw = ginv.WrapInterface(criteria, ip) 

iD = ginv.DivisionInterface(division_interface, iw) 

 

system = [ginv.Poly(ip, str(f)) for f in self.gens()] 

G = ginv.basisBuild(algorithm, iD, system) 

G = Sequence([P(str(f)) for f in G.iterGB()]) 

return G 

 

@singular_gb_standard_options 

def _groebner_basis_singular(self, algorithm="groebner", *args, **kwds): 

""" 

Return the reduced Groebner basis of this ideal. If the 

Groebner basis for this ideal has been calculated before, the 

cached Groebner basis is returned regardless of the requested 

algorithm. 

 

INPUT: 

 

- ``algorithm`` - see below for available algorithms 

 

 

ALGORITHMS: 

 

'groebner' 

use Singular's groebner heuristic to choose an algorithm (default) 

 

'std' 

Buchberger's algorithm 

 

'stdhilb' 

computes the standard basis of the homogeneous ideal in the 

base ring, via a Hilbert driven standard basis computation. 

 

'stdfglm' 

computes the standard basis of the ideal in the base ring via fglm 

(from the degrevlex ordering to the ordering of the base ring). 

 

'slimgb' 

the *SlimGB* algorithm 

 

EXAMPLES: 

 

We compute a Groebner basis of 'cyclic 4' relative to 

lexicographic ordering. :: 

 

sage: R.<a,b,c,d> = PolynomialRing(QQ, 4, order='lex') 

sage: I = sage.rings.ideal.Cyclic(R,4); I 

Ideal (a + b + c + d, a*b + a*d + b*c + c*d, a*b*c + a*b*d 

+ a*c*d + b*c*d, a*b*c*d - 1) of Multivariate Polynomial 

Ring in a, b, c, d over Rational Field 

 

:: 

 

sage: I._groebner_basis_singular() 

[c^2*d^6 - c^2*d^2 - d^4 + 1, c^3*d^2 + c^2*d^3 - c - d, 

b*d^4 - b + d^5 - d, b*c - b*d + c^2*d^4 + c*d - 2*d^2, 

b^2 + 2*b*d + d^2, a + b + c + d] 

 

ALGORITHM: 

 

Uses Singular. 

 

.. NOTE:: 

 

This method is called by the :meth:`.groebner_basis` method 

and the user usually doesn't need to bother with this one. 

""" 

from sage.rings.polynomial.multi_polynomial_sequence import PolynomialSequence 

 

R = self.ring() 

S = self._groebner_basis_singular_raw(algorithm=algorithm, *args, **kwds) 

S = PolynomialSequence([R(S[i+1]) for i in range(len(S))], R, immutable=True) 

return S 

 

@cached_method 

def _groebner_basis_singular_raw(self, algorithm="groebner", singular=singular_default, *args, **kwds): 

r""" 

Return a Groebner basis in Singular format. 

 

INPUT: 

 

- ``algorithm`` - Singular function to call (default: ``groebner``) 

 

- ``singular`` - Singular instance to use (default: ``singular_default``) 

 

- ``args`` - ignored 

 

- ``kwds`` - Singular options 

 

EXAMPLES:: 

 

sage: R.<a,b,c,d> = PolynomialRing(QQ, 4, order='lex') 

sage: I = sage.rings.ideal.Cyclic(R,4) 

sage: I._groebner_basis_singular() # indirect doctest 

[c^2*d^6 - c^2*d^2 - d^4 + 1, c^3*d^2 + c^2*d^3 - c - d, 

b*d^4 - b + d^5 - d, b*c - b*d + c^2*d^4 + c*d - 2*d^2, 

b^2 + 2*b*d + d^2, a + b + c + d] 

""" 

#try: 

# return self.__gb_singular 

#except AttributeError: 

# pass 

# singular options are preserved by @singular_gb_standard_options, 

# so we don't need to do that here too 

from sage.libs.singular.option import _options_py_to_singular 

S = self._singular_() # for degBound, we need to ensure 

# that a ring is defined 

 

if get_verbose() >= 2: 

kwds['prot'] = True 

 

for o, v in iteritems(kwds): 

o = _options_py_to_singular.get(o,o) 

if v: 

if o in ['degBound','multBound']: 

singular.eval(o+'=%d'%v) 

else: 

singular.option(o) 

else: 

if o in ['degBound','multBound']: 

singular.eval(o+'=0') 

else: 

singular.option("no"+o) 

 

obj = self._singular_() 

 

prot = kwds.get('prot',False) 

 

if prot == "sage": 

if algorithm == 'slimgb': 

warn("'slimgb' does not print sufficient information for prot='sage' to work reliably, the highest degree reached might be too low.") 

from sage.interfaces.singular import SingularGBLogPrettyPrinter 

log_parser = SingularGBLogPrettyPrinter(verbosity=get_verbose()+1) 

else: 

log_parser = None 

 

ctx = StdOutContext(singular, silent=False if prot else True, stdout=log_parser) 

 

with ctx: 

if algorithm=="groebner": 

S = obj.groebner() 

elif algorithm=="std": 

S = obj.std() 

elif algorithm=="slimgb": 

S = obj.slimgb() 

elif algorithm=="stdhilb": 

S = obj.stdhilb() 

elif algorithm=="stdfglm": 

S = obj.stdfglm() 

else: 

raise TypeError("algorithm '%s' unknown"%algorithm) 

self.__gb_singular = S 

if prot == "sage": 

print("") 

print("Highest degree reached during computation: %2d." % log_parser.max_deg) 

return S 

 

@require_field 

def genus(self): 

""" 

Return the genus of the projective curve defined by this ideal, 

which must be 1 dimensional. 

 

EXAMPLES: 

 

Consider the hyperelliptic curve `y^2 = 4x^5 - 30x^3 + 45x - 

22` over `\QQ`, it has genus 2:: 

 

sage: P.<x> = QQ[] 

sage: f = 4*x^5 - 30*x^3 + 45*x - 22 

sage: C = HyperellipticCurve(f); C 

Hyperelliptic Curve over Rational Field defined by y^2 = 4*x^5 - 30*x^3 + 45*x - 22 

sage: C.genus() 

2 

 

:: 

 

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

sage: f = y^2 - 4*x^5 - 30*x^3 + 45*x - 22 

sage: I = Ideal([f]) 

sage: I.genus() 

2 

 

TESTS: 

 

Check that the answer is correct for reducible curves:: 

 

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

sage: C = Curve(x^2 - 2*y^2) 

sage: C.is_singular() 

True 

sage: C.genus() 

-1 

sage: Ideal(x^4+y^2*x+x).genus() 

0 

sage: T.<t1,t2,u1,u2> = QQ[] 

sage: TJ = Ideal([t1^2 + u1^2 - 1,t2^2 + u2^2 - 1, (t1-t2)^2 + (u1-u2)^2 -1]) 

sage: TJ.genus() 

-1 

""" 

try: 

return self.__genus 

except AttributeError: 

import sage.libs.singular.function_factory 

genus = sage.libs.singular.function_factory.ff.normal__lib.genus 

self.__genus = Integer(genus(self)) 

return self.__genus 

 

@libsingular_gb_standard_options 

def intersection(self, *others): 

""" 

Return the intersection of the arguments with this ideal. 

 

EXAMPLES:: 

 

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

sage: I = x*R 

sage: J = y*R 

sage: I.intersection(J) 

Ideal (x*y) of Multivariate Polynomial Ring in x, y over Rational Field 

 

The following simple example illustrates that the product need 

not equal the intersection. :: 

 

sage: I = (x^2, y)*R 

sage: J = (y^2, x)*R 

sage: K = I.intersection(J); K 

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

sage: IJ = I*J; IJ 

Ideal (x^2*y^2, x^3, y^3, x*y) of Multivariate Polynomial Ring in x, y over Rational Field 

sage: IJ == K 

False 

 

Intersection of several ideals:: 

 

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

sage: I1 = x*R 

sage: I2 = y*R 

sage: I3 = (x, y)*R 

sage: I4 = (x^2 + x*y*z, y^2 - z^3*y, z^3 + y^5*x*z)*R 

sage: I1.intersection(I2, I3, I4) 

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

 

The ideals must share the same ring:: 

 

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

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

sage: I2 = x*R2 

sage: I3 = x*R3 

sage: I2.intersection(I3) 

Traceback (most recent call last): 

... 

TypeError: Intersection is only available for ideals of the same ring. 

 

""" 

R = self.ring() 

 

 

for other in others: 

if not isinstance(other, MPolynomialIdeal_singular_repr) or other.ring() != R: 

raise TypeError("Intersection is only available for ideals of the same ring.") 

 

import sage.libs.singular.function_factory 

intersect = sage.libs.singular.function_factory.ff.intersect 

 

K = intersect(self, *others) 

return R.ideal(K) 

 

@require_field 

@libsingular_gb_standard_options 

def minimal_associated_primes(self): 

""" 

OUTPUT: 

 

- ``list`` - a list of prime ideals 

 

EXAMPLES:: 

 

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

sage: p = z^2 + 1; q = z^3 + 2 

sage: I = (p*q^2, y-z^2)*R 

sage: I.minimal_associated_primes () 

[Ideal (z^2 + 1, -z^2 + y) of Multivariate Polynomial Ring 

in x, y, z over Rational Field, Ideal (z^3 + 2, -z^2 + y) 

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

Field] 

 

ALGORITHM: 

 

Uses Singular. 

""" 

import sage.libs.singular.function_factory 

minAssGTZ = sage.libs.singular.function_factory.ff.primdec__lib.minAssGTZ 

 

M = minAssGTZ(self) 

R = self.ring() 

return [R.ideal(J) for J in M] 

 

@require_field 

@libsingular_gb_standard_options 

def radical(self): 

r""" 

The radical of this ideal. 

 

EXAMPLES: 

 

This is an obviously not radical ideal:: 

 

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

sage: I = (x^2, y^3, (x*z)^4 + y^3 + 10*x^2)*R 

sage: I.radical() 

Ideal (y, x) of Multivariate Polynomial Ring in x, y, z over Rational Field 

 

That the radical is correct is clear from the Groebner basis. :: 

 

sage: I.groebner_basis() 

[y^3, x^2] 

 

This is the example from the Singular manual:: 

 

sage: p = z^2 + 1; q = z^3 + 2 

sage: I = (p*q^2, y-z^2)*R 

sage: I.radical() 

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

 

.. NOTE:: 

 

From the Singular manual: A combination of the algorithms 

of Krick/Logar and Kemper is used. Works also in positive 

characteristic (Kempers algorithm). 

 

:: 

 

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

sage: p = z^2 + 1; q = z^3 + 2 

sage: I = (p*q^2, y - z^2)*R 

sage: I.radical() 

Ideal (z^2 - y, y^2*z + y*z + 2*y + 2) of Multivariate Polynomial Ring in x, y, z over Finite Field of size 37 

""" 

import sage.libs.singular.function_factory 

radical = sage.libs.singular.function_factory.ff.primdec__lib.radical 

r = radical(self) 

 

S = self.ring() 

 

#I = self._singular_() 

#I.parent().lib('primdec.lib') 

#r = I.radical() 

 

return S.ideal(r) 

 

@require_field 

@libsingular_gb_standard_options 

def integral_closure(self, p=0, r=True, singular=singular_default): 

""" 

Let `I` = ``self``. 

 

Returns the integral closure of `I, ..., I^p`, where `sI` is 

an ideal in the polynomial ring `R=k[x(1),...x(n)]`. If `p` is 

not given, or `p=0`, compute the closure of all powers up to 

the maximum degree in t occurring in the closure of `R[It]` 

(so this is the last power whose closure is not just the 

sum/product of the smaller). If `r` is given and ``r is 

True``, ``I.integral_closure()`` starts with a check whether I 

is already a radical ideal. 

 

INPUT: 

 

- ``p`` - powers of I (default: 0) 

 

- ``r`` - check whether self is a radical ideal first (default: ``True``) 

 

EXAMPLES:: 

 

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

sage: I = ideal([x^2,x*y^4,y^5]) 

sage: I.integral_closure() 

[x^2, x*y^4, y^5, x*y^3] 

 

ALGORITHM: 

 

Uses libSINGULAR. 

""" 

from sage.rings.polynomial.multi_polynomial_sequence import PolynomialSequence 

 

R = self.ring() 

import sage.libs.singular.function_factory 

normalI = sage.libs.singular.function_factory.ff.reesclos__lib.normalI 

ret = PolynomialSequence(normalI(self, p, int(r))[0], R, immutable=True) 

return ret 

 

@require_field 

def syzygy_module(self): 

r""" 

Computes the first syzygy (i.e., the module of relations of the 

given generators) of the ideal. 

 

EXAMPLES:: 

 

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

sage: f = 2*x^2 + y 

sage: g = y 

sage: h = 2*f + g 

sage: I = Ideal([f,g,h]) 

sage: M = I.syzygy_module(); M 

[ -2 -1 1] 

[ -y 2*x^2 + y 0] 

sage: G = vector(I.gens()) 

sage: M*G 

(0, 0) 

 

ALGORITHM: 

 

Uses Singular's syz command. 

""" 

import sage.libs.singular.function_factory 

syz = sage.libs.singular.function_factory.ff.syz 

from sage.matrix.constructor import matrix 

 

#return self._singular_().syz().transpose().sage_matrix(self.ring()) 

S = syz(self) 

return matrix(self.ring(), S) 

 

@singular_gb_standard_options 

@libsingular_gb_standard_options 

def interreduced_basis(self): 

r""" 

If this ideal is spanned by `(f_1, ..., f_n)` this method 

returns `(g_1, ..., g_s)` such that: 

 

- `(f_1,...,f_n) = (g_1,...,g_s)` 

 

- `LT(g_i) != LT(g_j)` for all `i != j` 

 

- `LT(g_i)` does not divide `m` for all monomials `m` of 

`\{g_1,...,g_{i-1},g_{i+1},...,g_s\}` 

 

- `LC(g_i) == 1` for all `i` if the coefficient ring is a field. 

 

 

EXAMPLES:: 

 

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

sage: I = Ideal([z*x+y^3,z+y^3,z+x*y]) 

sage: I.interreduced_basis() 

[y^3 + z, x*y + z, x*z - z] 

 

Note that tail reduction for local orderings is not well-defined:: 

 

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

sage: I = Ideal([z*x+y^3,z+y^3,z+x*y]) 

sage: I.interreduced_basis() 

[z + x*y, x*y - y^3, x^2*y - y^3] 

 

A fixed error with nonstandard base fields:: 

 

sage: R.<t>=QQ['t'] 

sage: K.<x,y>=R.fraction_field()['x,y'] 

sage: I=t*x*K 

sage: I.interreduced_basis() 

[x] 

 

The interreduced basis of 0 is 0:: 

 

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

sage: Ideal(P(0)).interreduced_basis() 

[0] 

 

ALGORITHM: 

 

Uses Singular's interred command or 

:func:`sage.rings.polynomial.toy_buchberger.inter_reduction` 

if conversion to Singular fails. 

""" 

return self.basis.reduced() 

 

@cached_method 

@singular_gb_standard_options 

def basis_is_groebner(self, singular=singular_default): 

r""" 

Returns ``True`` if the generators of this ideal 

(``self.gens()``) form a Groebner basis. 

 

Let `I` be the set of generators of this ideal. The check is 

performed by trying to lift `Syz(LM(I))` to `Syz(I)` as `I` 

forms a Groebner basis if and only if for every element `S` in 

`Syz(LM(I))`: 

 

.. MATH:: 

 

S * G = \sum_{i=0}^{m} h_ig_i ---->_G 0. 

 

ALGORITHM: 

 

Uses Singular. 

 

EXAMPLES:: 

 

sage: R.<a,b,c,d,e,f,g,h,i,j> = PolynomialRing(GF(127),10) 

sage: I = sage.rings.ideal.Cyclic(R,4) 

sage: I.basis_is_groebner() 

False 

sage: I2 = Ideal(I.groebner_basis()) 

sage: I2.basis_is_groebner() 

True 

 

A more complicated example:: 

 

sage: R.<U6,U5,U4,U3,U2, u6,u5,u4,u3,u2, h> = PolynomialRing(GF(7583)) 

sage: l = [u6 + u5 + u4 + u3 + u2 - 3791*h, \ 

U6 + U5 + U4 + U3 + U2 - 3791*h, \ 

U2*u2 - h^2, U3*u3 - h^2, U4*u4 - h^2, \ 

U5*u4 + U5*u3 + U4*u3 + U5*u2 + U4*u2 + U3*u2 - 3791*U5*h - 3791*U4*h - 3791*U3*h - 3791*U2*h - 2842*h^2, \ 

U4*u5 + U3*u5 + U2*u5 + U3*u4 + U2*u4 + U2*u3 - 3791*u5*h - 3791*u4*h - 3791*u3*h - 3791*u2*h - 2842*h^2, \ 

U5*u5 - h^2, U4*U2*u3 + U5*U3*u2 + U4*U3*u2 + U3^2*u2 - 3791*U5*U3*h - 3791*U4*U3*h - 3791*U3^2*h - 3791*U5*U2*h \ 

- 3791*U4*U2*h + U3*U2*h - 3791*U2^2*h - 3791*U4*u3*h - 3791*U4*u2*h - 3791*U3*u2*h - 2843*U5*h^2 + 1897*U4*h^2 - 946*U3*h^2 - 947*U2*h^2 + 2370*h^3, \ 

U3*u5*u4 + U2*u5*u4 + U3*u4^2 + U2*u4^2 + U2*u4*u3 - 3791*u5*u4*h - 3791*u4^2*h - 3791*u4*u3*h - 3791*u4*u2*h + u5*h^2 - 2842*u4*h^2, \ 

U2*u5*u4*u3 + U2*u4^2*u3 + U2*u4*u3^2 - 3791*u5*u4*u3*h - 3791*u4^2*u3*h - 3791*u4*u3^2*h - 3791*u4*u3*u2*h + u5*u4*h^2 + u4^2*h^2 + u5*u3*h^2 - 2842*u4*u3*h^2, \ 

U5^2*U4*u3 + U5*U4^2*u3 + U5^2*U4*u2 + U5*U4^2*u2 + U5^2*U3*u2 + 2*U5*U4*U3*u2 + U5*U3^2*u2 - 3791*U5^2*U4*h - 3791*U5*U4^2*h - 3791*U5^2*U3*h \ 

+ U5*U4*U3*h - 3791*U5*U3^2*h - 3791*U5^2*U2*h + U5*U4*U2*h + U5*U3*U2*h - 3791*U5*U2^2*h - 3791*U5*U3*u2*h - 2842*U5^2*h^2 + 1897*U5*U4*h^2 \ 

- U4^2*h^2 - 947*U5*U3*h^2 - U4*U3*h^2 - 948*U5*U2*h^2 - U4*U2*h^2 - 1422*U5*h^3 + 3791*U4*h^3, \ 

u5*u4*u3*u2*h + u4^2*u3*u2*h + u4*u3^2*u2*h + u4*u3*u2^2*h + 2*u5*u4*u3*h^2 + 2*u4^2*u3*h^2 + 2*u4*u3^2*h^2 + 2*u5*u4*u2*h^2 + 2*u4^2*u2*h^2 \ 

+ 2*u5*u3*u2*h^2 + 1899*u4*u3*u2*h^2, \ 

U5^2*U4*U3*u2 + U5*U4^2*U3*u2 + U5*U4*U3^2*u2 - 3791*U5^2*U4*U3*h - 3791*U5*U4^2*U3*h - 3791*U5*U4*U3^2*h - 3791*U5*U4*U3*U2*h \ 

+ 3791*U5*U4*U3*u2*h + U5^2*U4*h^2 + U5*U4^2*h^2 + U5^2*U3*h^2 - U4^2*U3*h^2 - U5*U3^2*h^2 - U4*U3^2*h^2 - U5*U4*U2*h^2 \ 

- U5*U3*U2*h^2 - U4*U3*U2*h^2 + 3791*U5*U4*h^3 + 3791*U5*U3*h^3 + 3791*U4*U3*h^3, \ 

u4^2*u3*u2*h^2 + 1515*u5*u3^2*u2*h^2 + u4*u3^2*u2*h^2 + 1515*u5*u4*u2^2*h^2 + 1515*u5*u3*u2^2*h^2 + u4*u3*u2^2*h^2 \ 

+ 1521*u5*u4*u3*h^3 - 3028*u4^2*u3*h^3 - 3028*u4*u3^2*h^3 + 1521*u5*u4*u2*h^3 - 3028*u4^2*u2*h^3 + 1521*u5*u3*u2*h^3 + 3420*u4*u3*u2*h^3, \ 

U5^2*U4*U3*U2*h + U5*U4^2*U3*U2*h + U5*U4*U3^2*U2*h + U5*U4*U3*U2^2*h + 2*U5^2*U4*U3*h^2 + 2*U5*U4^2*U3*h^2 + 2*U5*U4*U3^2*h^2 \ 

+ 2*U5^2*U4*U2*h^2 + 2*U5*U4^2*U2*h^2 + 2*U5^2*U3*U2*h^2 - 2*U4^2*U3*U2*h^2 - 2*U5*U3^2*U2*h^2 - 2*U4*U3^2*U2*h^2 \ 

- 2*U5*U4*U2^2*h^2 - 2*U5*U3*U2^2*h^2 - 2*U4*U3*U2^2*h^2 - U5*U4*U3*h^3 - U5*U4*U2*h^3 - U5*U3*U2*h^3 - U4*U3*U2*h^3] 

 

sage: Ideal(l).basis_is_groebner() 

False 

sage: gb = Ideal(l).groebner_basis() 

sage: Ideal(gb).basis_is_groebner() 

True 

 

.. NOTE:: 

 

From the Singular Manual for the reduce function we use in 

this method: 'The result may have no meaning if the second 

argument (``self``) is not a standard basis'. I (malb) 

believe this refers to the mathematical fact that the 

results may have no meaning if self is no standard basis, 

i.e., Singular doesn't 'add' any additional 'nonsense' to 

the result. So we may actually use reduce to determine if 

self is a Groebner basis. 

""" 

from sage.matrix.constructor import matrix 

from sage.libs.singular.option import opt_verb_ctx 

import sage.libs.singular.function_factory 

sing_reduce = sage.libs.singular.function_factory.ff.reduce 

syz = sage.libs.singular.function_factory.ff.syz 

 

R = self.ring() 

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

raise ValueError("Coefficient ring must be a field for function 'basis_is_groebner'.") 

 

try: 

F = matrix(R, 1, self.ngens(), self.gens()) 

LTF = matrix(R, 1, self.ngens(), [f.lt() for f in self.gens()]) 

 

with opt_verb_ctx(notWarnSB=True): 

M = F * matrix(R, syz(LTF)).transpose() 

M.set_immutable() 

M = sing_reduce(M, self) 

 

if any(M): 

return False 

return True 

except TypeError: 

R._singular_().set_ring() 

F = singular( tuple(self.gens()), "module" ) 

LTF = singular( [f.lt() for f in self.gens()] , "module" ) 

 

M = (F * LTF.syz()).reduce(self._singular_()) 

 

for i in range(M.ncols()): 

if int(singular.eval("%s[1,%s+1]!=0"%(M.name(),i))): 

return False 

self._singular_().attrib('isSB',1) 

return True 

 

@require_field 

@singular_gb_standard_options 

@libsingular_gb_standard_options 

def transformed_basis(self, algorithm="gwalk", other_ring=None, singular=singular_default): 

""" 

Returns a lex or ``other_ring`` Groebner Basis for this ideal. 

 

INPUT: 

 

- ``algorithm`` - see below for options. 

 

- ``other_ring`` - only valid for algorithm 'fglm', if 

provided conversion will be performed to this 

ring. Otherwise a lex Groebner basis will be returned. 

 

 

ALGORITHMS: 

 

- ``fglm`` - FGLM algorithm. The input ideal must be given with a reduced 

Groebner Basis of a zero-dimensional ideal 

 

- ``gwalk`` - Groebner Walk algorithm (*default*) 

 

- ``awalk1`` - 'first alternative' algorithm 

 

- ``awalk2`` - 'second alternative' algorithm 

 

- ``twalk`` - Tran algorithm 

 

- ``fwalk`` - Fractal Walk algorithm 

 

EXAMPLES:: 

 

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

sage: I = Ideal([y^3+x^2,x^2*y+x^2, x^3-x^2, z^4-x^2-y]) 

sage: I = Ideal(I.groebner_basis()) 

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

sage: J = Ideal(I.transformed_basis('fglm',S)) 

sage: J 

Ideal (z^4 + y^3 - y, x^2 + y^3, x*y^3 - y^3, y^4 + y^3) 

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

 

:: 

 

sage: R.<z,y,x>=PolynomialRing(GF(32003),3,order='lex') 

sage: I=Ideal([y^3+x*y*z+y^2*z+x*z^3,3+x*y+x^2*y+y^2*z]) 

sage: I.transformed_basis('gwalk') 

[z*y^2 + y*x^2 + y*x + 3, 

z*x + 8297*y^8*x^2 + 8297*y^8*x + 3556*y^7 - 8297*y^6*x^4 + 15409*y^6*x^3 - 8297*y^6*x^2 

- 8297*y^5*x^5 + 15409*y^5*x^4 - 8297*y^5*x^3 + 3556*y^5*x^2 + 3556*y^5*x + 3556*y^4*x^3 

+ 3556*y^4*x^2 - 10668*y^4 - 10668*y^3*x - 8297*y^2*x^9 - 1185*y^2*x^8 + 14224*y^2*x^7 

- 1185*y^2*x^6 - 8297*y^2*x^5 - 14223*y*x^7 - 10666*y*x^6 - 10666*y*x^5 - 14223*y*x^4 

+ x^5 + 2*x^4 + x^3, 

y^9 - y^7*x^2 - y^7*x - y^6*x^3 - y^6*x^2 - 3*y^6 - 3*y^5*x - y^3*x^7 - 3*y^3*x^6 

- 3*y^3*x^5 - y^3*x^4 - 9*y^2*x^5 - 18*y^2*x^4 - 9*y^2*x^3 - 27*y*x^3 - 27*y*x^2 - 27*x] 

 

ALGORITHM: 

 

Uses Singular. 

""" 

from sage.rings.polynomial.multi_polynomial_sequence import PolynomialSequence 

R = self.ring() 

 

if self.basis_is_groebner(): 

I = R.ideal(self.interreduced_basis()) 

else: 

I = R.ideal(self.groebner_basis()) 

 

if algorithm in ("gwalk","awalk1","awalk2","twalk","fwalk"): 

from sage.libs.singular.function import lib 

from sage.libs.singular.function import singular_function 

lib("grwalk.lib") 

gb = singular_function(algorithm)(I) 

return PolynomialSequence(R, sorted(gb,reverse=True), immutable=True) 

 

elif algorithm == "fglm": 

# new ring 

if other_ring is None: 

nR = R.change_ring(order='lex') 

else: 

nR = other_ring 

Rs = singular(R) 

Is = singular(I) 

Is.attrib('isSB',1) 

singular(nR).set_ring() 

nIs = singular.fglm(Rs,Is) 

 

return PolynomialSequence(nR, sorted([nR(f) for f in nIs],reverse=True), immutable=True) 

 

else: 

raise TypeError("Cannot convert basis with given algorithm") 

 

@libsingular_gb_standard_options 

def elimination_ideal(self, variables): 

r""" 

Return the elimination ideal of this ideal with respect to the 

variables given in ``variables``. 

 

INPUT: 

 

- ``variables`` -- a list or tuple of variables in ``self.ring()`` 

 

EXAMPLES:: 

 

sage: R.<x,y,t,s,z> = PolynomialRing(QQ,5) 

sage: I = R * [x-t,y-t^2,z-t^3,s-x+y^3] 

sage: I.elimination_ideal([t,s]) 

Ideal (y^2 - x*z, x*y - z, x^2 - y) of Multivariate 

Polynomial Ring in x, y, t, s, z over Rational Field 

 

ALGORITHM: 

 

Uses Singular. 

 

.. NOTE:: 

 

Requires computation of a Groebner basis, which can be a very 

expensive operation. 

""" 

import sage.libs.singular.function_factory 

eliminate = sage.libs.singular.function_factory.ff.eliminate 

 

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

variables = (variables,) 

 

R = self.ring() 

Is = MPolynomialIdeal(R,self.groebner_basis()) 

return MPolynomialIdeal(R, eliminate(Is, prod(variables)) ) 

 

@libsingular_gb_standard_options 

def quotient(self, J): 

r""" 

Given ideals `I` = ``self`` and `J` in the same polynomial 

ring `P`, return the ideal quotient of `I` by `J` consisting 

of the polynomials `a` of `P` such that `\{aJ \subset I\}`. 

 

This is also referred to as the colon ideal 

(`I`:`J`). 

 

INPUT: 

 

- ``J`` - multivariate polynomial ideal 

 

EXAMPLES:: 

 

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

sage: I = Ideal([x^2+x*y*z,y^2-z^3*y,z^3+y^5*x*z]) 

sage: J = Ideal([x]) 

sage: Q = I.quotient(J) 

sage: y*z + x in I 

False 

sage: x in J 

True 

sage: x * (y*z + x) in I 

True 

 

TESTS: 

 

This example checks :trac:`16301`:: 

 

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

sage: I = Ideal(R(2), x*y, x*z + x) 

sage: eD = Ideal(x, z^2-1) 

sage: I.quotient(eD).gens() 

[2, x*z + x, x*y] 

""" 

from sage.misc.stopgap import stopgap 

R = self.ring() 

 

if not isinstance(J, MPolynomialIdeal): 

raise TypeError("J needs to be a multivariate polynomial ideal") 

 

if not R is J.ring() and not R == J.ring(): 

raise TypeError("base rings do not match") 

 

import sage.libs.singular.function_factory 

if self.base_ring() == ZZ: 

stopgap("Singular's quotient()-routine for rings over ZZ contains bugs and may be mathematically unreliable", 12803) 

quotient = sage.libs.singular.function_factory.ff.quotient 

return R.ideal(quotient(self, J)) 

 

def saturation(self, other): 

r""" 

Returns the saturation (and saturation exponent) of the ideal ``self`` with respect to the ideal ``other`` 

 

INPUT: 

 

- ``other`` -- another ideal in the same ring 

 

OUTPUT: 

 

- a pair (ideal, integer) 

 

EXAMPLES:: 

 

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

sage: I = R.ideal(x^5*z^3, x*y*z, y*z^4) 

sage: J = R.ideal(z) 

sage: I.saturation(J) 

(Ideal (y, x^5) of Multivariate Polynomial Ring in x, y, z over Rational Field, 4) 

""" 

from sage.libs.singular.function_factory import ff 

sat = ff.elim__lib.sat 

R = self.ring() 

ideal, expo = sat(self, other) 

return (R.ideal(ideal), ZZ(expo)) 

 

@require_field 

def variety(self, ring=None): 

r""" 

Return the variety of this ideal. 

 

Given a zero-dimensional ideal `I` (== ``self``) of a 

polynomial ring `P` whose order is lexicographic, return the 

variety of `I` as a list of dictionaries with ``(variable, value)`` 

pairs. By default, the variety of the ideal over its 

coefficient field `K` is returned; ``ring`` can be specified 

to find the variety over a different ring. 

 

These dictionaries have cardinality equal to the number of 

variables in `P` and represent assignments of values to these 

variables such that all polynomials in `I` vanish. 

 

If ``ring`` is specified, then a triangular decomposition of 

``self`` is found over the original coefficient field `K`; 

then the triangular systems are solved using root-finding over 

``ring``. This is particularly useful when `K` is ``QQ`` (to 

allow fast symbolic computation of the triangular 

decomposition) and ``ring`` is ``RR``, ``AA``, ``CC``, or 

``QQbar`` (to compute the whole real or complex variety of the 

ideal). 

 

Note that with ``ring=RR`` or ``CC``, computation is done 

numerically and potentially inaccurately; in particular, the 

number of points in the real variety may be miscomputed. With 

``ring=AA`` or ``QQbar``, computation is done exactly 

(which may be much slower, of course). 

 

INPUT: 

 

- ``ring`` - return roots in the ``ring`` instead of the base 

ring of this ideal (default: ``None``) 

- ``proof`` - return a provably correct result (default: ``True``) 

 

EXAMPLES:: 

 

sage: K.<w> = GF(27) # this example is from the MAGMA handbook 

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

sage: I = Ideal([ x^8 + y + 2, y^6 + x*y^5 + x^2 ]) 

sage: I = Ideal(I.groebner_basis()); I 

Ideal (x - y^47 - y^45 + y^44 - y^43 + y^41 - y^39 - y^38 

- y^37 - y^36 + y^35 - y^34 - y^33 + y^32 - y^31 + y^30 + 

y^28 + y^27 + y^26 + y^25 - y^23 + y^22 + y^21 - y^19 - 

y^18 - y^16 + y^15 + y^13 + y^12 - y^10 + y^9 + y^8 + y^7 

- y^6 + y^4 + y^3 + y^2 + y - 1, y^48 + y^41 - y^40 + y^37 

- y^36 - y^33 + y^32 - y^29 + y^28 - y^25 + y^24 + y^2 + y 

+ 1) of Multivariate Polynomial Ring in x, y over Finite 

Field in w of size 3^3 

 

sage: V = I.variety(); V 

[{y: w^2 + 2, x: 2*w}, {y: w^2 + w, x: 2*w + 1}, {y: w^2 + 2*w, x: 2*w + 2}] 

 

sage: [f.subs(v) for f in I.gens() for v in V] # check that all polynomials vanish 

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

sage: [I.subs(v).is_zero() for v in V] # same test, but nicer syntax 

[True, True, True] 

 

However, we only account for solutions in the ground field and not 

in the algebraic closure:: 

 

sage: I.vector_space_dimension() 

48 

 

Here we compute the points of intersection of a hyperbola and a 

circle, in several fields:: 

 

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

sage: I = Ideal([ x*y - 1, (x-2)^2 + (y-1)^2 - 1]) 

sage: I = Ideal(I.groebner_basis()); I 

Ideal (x + y^3 - 2*y^2 + 4*y - 4, y^4 - 2*y^3 + 4*y^2 - 4*y + 1) 

of Multivariate Polynomial Ring in x, y over Rational Field 

 

These two curves have one rational intersection:: 

 

sage: I.variety() 

[{y: 1, x: 1}] 

 

There are two real intersections:: 

 

sage: I.variety(ring=RR) 

[{y: 0.361103080528647, x: 2.76929235423863}, 

{y: 1.00000000000000, x: 1.00000000000000}] 

sage: I.variety(ring=AA) 

[{x: 2.769292354238632?, y: 0.3611030805286474?}, 

{x: 1, y: 1}] 

 

and a total of four intersections:: 

 

sage: I.variety(ring=CC) 

[{y: 0.31944845973567... - 1.6331702409152...*I, 

x: 0.11535382288068... + 0.58974280502220...*I}, 

{y: 0.31944845973567... + 1.6331702409152...*I, 

x: 0.11535382288068... - 0.58974280502220...*I}, 

{y: 0.36110308052864..., x: 2.7692923542386...}, 

{y: 1.00000000000000, x: 1.00000000000000}] 

sage: I.variety(ring=QQbar) 

[{y: 0.3194484597356763? - 1.633170240915238?*I, 

x: 0.11535382288068429? + 0.5897428050222055?*I}, 

{y: 0.3194484597356763? + 1.633170240915238?*I, 

x: 0.11535382288068429? - 0.5897428050222055?*I}, 

{y: 0.3611030805286474?, x: 2.769292354238632?}, 

{y: 1, x: 1}] 

 

Computation over floating point numbers may compute only a partial solution, 

or even none at all. Notice that x values are missing from the following variety:: 

 

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

sage: I = ideal([x^2+y^2-1,x*y-1]) 

sage: I.variety() 

verbose 0 (...: multi_polynomial_ideal.py, variety) Warning: computations in the complex field are inexact; variety may be computed partially or incorrectly. 

verbose 0 (...: multi_polynomial_ideal.py, variety) Warning: falling back to very slow toy implementation. 

[{y: -0.86602540378443... - 0.500000000000000*I}, 

{y: -0.86602540378443... + 0.500000000000000*I}, 

{y: 0.86602540378443... - 0.500000000000000*I}, 

{y: 0.86602540378443... + 0.500000000000000*I}] 

 

This is due to precision error, 

which causes the computation of an intermediate Groebner basis to fail. 

 

If the ground field's characteristic is too large for 

Singular, we resort to a toy implementation:: 

 

sage: R.<x,y> = PolynomialRing(GF(2147483659),order='lex') 

sage: I=ideal([x^3-2*y^2,3*x+y^4]) 

sage: I.variety() 

verbose 0 (...: multi_polynomial_ideal.py, groebner_basis) Warning: falling back to very slow toy implementation. 

verbose 0 (...: multi_polynomial_ideal.py, dimension) Warning: falling back to very slow toy implementation. 

verbose 0 (...: multi_polynomial_ideal.py, variety) Warning: falling back to very slow toy implementation. 

[{y: 0, x: 0}] 

 

The dictionary expressing the variety will be indexed by generators 

of the polynomial ring after changing to the target field. 

But the mapping will also accept generators of the original ring, 

or even generator names as strings, when provided as keys:: 

 

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

sage: I = ideal([x^2+2*y-5,x+y+3]) 

sage: v = I.variety(AA)[0]; v 

{x: 4.464101615137755?, y: -7.464101615137755?} 

sage: v.keys()[0].parent() 

Multivariate Polynomial Ring in x, y over Algebraic Real Field 

sage: v[x] 

4.464101615137755? 

sage: v["y"] 

-7.464101615137755? 

 

TESTS:: 

 

sage: K.<w> = GF(27) 

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

sage: I = Ideal([ x^8 + y + 2, y^6 + x*y^5 + x^2 ]) 

 

Testing the robustness of the Singular interface:: 

 

sage: T = I.triangular_decomposition('singular:triangLfak') 

sage: I.variety() 

[{y: w^2 + 2, x: 2*w}, {y: w^2 + w, x: 2*w + 1}, {y: w^2 + 2*w, x: 2*w + 2}] 

 

Testing that a bug is indeed fixed :: 

 

sage: R = PolynomialRing(GF(2), 30, ['x%d'%(i+1) for i in range(30)], order='lex') 

sage: R.inject_variables() 

Defining... 

sage: I = Ideal([x1 + 1, x2, x3 + 1, x5*x10 + x10 + x18, x5*x11 + x11, \ 

x5*x18, x6, x7 + 1, x9, x10*x11 + x10 + x18, x10*x18 + x18, \ 

x11*x18, x12, x13, x14, x15, x16 + 1, x17 + x18 + 1, x19, x20, \ 

x21 + 1, x22, x23, x24, x25 + 1, x28 + 1, x29 + 1, x30, x8, \ 

x26, x1^2 + x1, x2^2 + x2, x3^2 + x3, x4^2 + x4, x5^2 + x5, \ 

x6^2 + x6, x7^2 + x7, x8^2 + x8, x9^2 + x9, x10^2 + x10, \ 

x11^2 + x11, x12^2 + x12, x13^2 + x13, x14^2 + x14, x15^2 + x15, \ 

x16^2 + x16, x17^2 + x17, x18^2 + x18, x19^2 + x19, x20^2 + x20, \ 

x21^2 + x21, x22^2 + x22, x23^2 + x23, x24^2 + x24, x25^2 + x25, \ 

x26^2 + x26, x27^2 + x27, x28^2 + x28, x29^2 + x29, x30^2 + x30]) 

sage: I.basis_is_groebner() 

True 

sage: sorted("".join(str(V[g]) for g in R.gens()) for V in I.variety()) # long time (6s on sage.math, 2011) 

['101000100000000110001000100110', 

'101000100000000110001000101110', 

'101000100100000101001000100110', 

'101000100100000101001000101110', 

'101010100000000110001000100110', 

'101010100000000110001000101110', 

'101010100010000110001000100110', 

'101010100010000110001000101110', 

'101010100110000110001000100110', 

'101010100110000110001000101110', 

'101100100000000110001000100110', 

'101100100000000110001000101110', 

'101100100100000101001000100110', 

'101100100100000101001000101110', 

'101110100000000110001000100110', 

'101110100000000110001000101110', 

'101110100010000110001000100110', 

'101110100010000110001000101110', 

'101110100110000110001000100110', 

'101110100110000110001000101110'] 

 

Check that the issue at :trac:`7425` is fixed:: 

 

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

sage: I = R.ideal([x^2-y^3*z, x+y*z]) 

sage: I.dimension() 

1 

sage: I.variety() 

Traceback (most recent call last): 

... 

ValueError: The dimension of the ideal is 1, but it should be 0 

 

Check that the issue at :trac:`7425` is fixed:: 

 

sage: S.<t>=PolynomialRing(QQ) 

sage: F.<q>=QQ.extension(t^4+1) 

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

sage: I=R.ideal(x,y^4+1) 

sage: I.variety() 

[...{y: -q^3, x: 0}...] 

 

Check that computing the variety of the ``[1]`` ideal is allowed (:trac:`13977`):: 

 

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

sage: I = R.ideal(1) 

sage: I.variety() 

[] 

 

Check that the issue at :trac:`16485` is fixed:: 

 

sage: R.<a,b,c> = PolynomialRing(QQ, order='lex') 

sage: I = R.ideal(c^2-2, b-c, a) 

sage: I.variety(QQbar) 

[...a: 0...] 

 

ALGORITHM: 

 

Uses triangular decomposition. 

""" 

def _variety(T, V, v=None): 

""" 

Return variety ``V`` for one triangular set of 

polynomials ``T``. 

""" 

if v is None: v = {} 

found = False 

for f in T: 

if f.is_univariate() and not f.is_constant(): 

T.remove(f); found = True; break 

 

if found is False: 

V.append(v) 

return V 

 

variable = f.variable(0) 

roots = f.univariate_polynomial().roots(ring=ring, multiplicities=False) 

 

for root in roots: 

vbar = v.copy() 

vbar[variable] = root 

Tbar = [ f.subs({variable:root}) for f in T ] 

_variety(Tbar,V,vbar) 

 

return V 

 

d = self.dimension() 

if d > 0: 

raise ValueError("The dimension of the ideal is %s, but it should be 0"%d) 

if d == -1: 

return [] 

 

import sage.rings.complex_field as CCmod 

if isinstance(self.base_ring(), CCmod.ComplexField_class): 

verbose("Warning: computations in the complex field are inexact; variety may be computed partially or incorrectly.", level=0) 

P = self.ring() 

if ring is not None: P = P.change_ring(ring) 

try: 

TI = self.triangular_decomposition('singular:triangLfak') 

T = [list(each.gens()) for each in TI] 

except TypeError as msg: # conversion to Singular not supported 

if self.ring().term_order().is_global(): 

verbose("Warning: falling back to very slow toy implementation.", level=0) 

T = toy_variety.triangular_factorization(self.groebner_basis()) 

else: 

raise TypeError("Local/unknown orderings not supported by 'toy_buchberger' implementation.") 

 

from sage.misc.converting_dict import KeyConvertingDict 

V = [] 

for t in T: 

Vbar = _variety([P(f) for f in t], []) 

#Vbar = _variety(list(t.gens()),[]) 

 

for v in Vbar: 

V.append(KeyConvertingDict(P, v)) 

V.sort() 

return V 

 

@require_field 

def hilbert_polynomial(self): 

r""" 

Return the Hilbert polynomial of this ideal. 

 

Let `I` = ``self`` be a homogeneous ideal and 

`R` = ``self.ring()`` be a graded commutative 

algebra (`R = \oplus R_d`) over a field `K`. The 

Hilbert polynomial is the unique polynomial `HP(t)` with 

rational coefficients such that `HP(d) = dim_K R_d` for 

all but finitely many positive integers `d`. 

 

EXAMPLES:: 

 

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

sage: I = Ideal([x^3*y^2 + 3*x^2*y^2*z + y^3*z^2 + z^5]) 

sage: I.hilbert_polynomial() 

5*t - 5 

""" 

if not self.is_homogeneous(): 

raise TypeError("Ideal must be homogeneous.") 

 

import sage.libs.singular.function_factory 

hilbPoly = sage.libs.singular.function_factory.ff.poly__lib.hilbPoly 

 

hp = hilbPoly(self) 

t = ZZ['t'].gen() 

fp = ZZ(len(hp)-1).factorial() 

return sum(ZZ(hp[i]) * t ** i for i in range(len(hp))) / fp 

 

@require_field 

def hilbert_series(self, singular=singular_default, grading=None): 

r""" 

Return the Hilbert series of this ideal. 

 

Let `I` = ``self`` be a homogeneous ideal and 

`R` = ``self.ring()`` be a graded commutative 

algebra (`R = \oplus R_d`) over a field 

`K`. Then the Hilbert function is defined as 

`H(d) = dim_K R_d` and the Hilbert series of `I` 

is defined as the formal power series 

`HS(t) = \sum_0^{\infty} H(d) t^d`. 

 

This power series can be expressed as 

`HS(t) = Q(t)/(1-t)^n` where `Q(t)` is a polynomial 

over `Z` and `n` the number of variables in 

`R`. This method returns `Q(t)/(1-t)^n`. 

 

An optional ``grading`` can be given, in which case 

the graded (or weighted) Hilbert series is given. 

 

EXAMPLES:: 

 

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

sage: I = Ideal([x^3*y^2 + 3*x^2*y^2*z + y^3*z^2 + z^5]) 

sage: I.hilbert_series() 

(-t^4 - t^3 - t^2 - t - 1)/(-t^2 + 2*t - 1) 

sage: R.<a,b> = PolynomialRing(QQ) 

sage: J = R.ideal([a^2*b,a*b^2]) 

sage: J.hilbert_series() 

(t^3 - t^2 - t - 1)/(t - 1) 

sage: J.hilbert_series(grading=(10,3)) 

(t^25 + t^24 + t^23 - t^15 - t^14 - t^13 - t^12 - t^11 

- t^10 - t^9 - t^8 - t^7 - t^6 - t^5 - t^4 - t^3 - t^2 

- t - 1)/(t^12 + t^11 + t^10 - t^2 - t - 1) 

 

sage: J = R.ideal([a^2*b^3, a*b^4 + a^3*b^2]) 

sage: J.hilbert_series(grading=[1,2]) 

(t^11 + t^8 - t^6 - t^5 - t^4 - t^3 - t^2 - t - 1)/(t^2 - 1) 

sage: J.hilbert_series(grading=[2,1]) 

(2*t^7 - t^6 - t^4 - t^2 - 1)/(t - 1) 

 

TESTS:: 

 

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

sage: I = Ideal([x^3*y^2 + 3*x^2*y^2*z + y^3*z^2 + z^5]) 

sage: I.hilbert_series(grading=5) 

Traceback (most recent call last): 

... 

TypeError: Grading must be a list or a tuple of integers. 

""" 

if not self.is_homogeneous(): 

raise TypeError("Ideal must be homogeneous.") 

 

t = ZZ['t'].gen() 

n = self.ring().ngens() 

 

if grading is None: 

return self.hilbert_numerator(singular) / (1-t)**n 

 

# The check that ``grading`` is valid input is done by ``hilbert_numerator()`` 

return (self.hilbert_numerator(singular, grading) 

/ prod((1 - t**a) for a in grading)) 

 

@require_field 

def hilbert_numerator(self, singular=singular_default, grading=None): 

r""" 

Return the Hilbert numerator of this ideal. 

 

Let `I` = ``self`` be a homogeneous ideal and 

`R` = ``self.ring()`` be a graded commutative 

algebra (`R = \oplus R_d`) over a field 

`K`. Then the Hilbert function is defined as 

`H(d) = dim_K R_d` and the Hilbert series of `I` 

is defined as the formal power series 

`H(d) = dim_K R_d` and the Hilbert series of `I` 

is defined as the formal power series 

`HS(t) = \sum_0^{\infty} H(d) t^d`. 

 

This power series can be expressed as 

`HS(t) = Q(t)/(1-t)^n` where `Q(t)` is a polynomial 

over `Z` and `n` the number of variables in 

`R`. This method returns `Q(t)`, the numerator; 

hence the name, `hilbert_numerator`. 

 

An optional ``grading`` can be given, in which case 

the graded (or weighted) Hilbert numerator is given. 

 

EXAMPLES:: 

 

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

sage: I = Ideal([x^3*y^2 + 3*x^2*y^2*z + y^3*z^2 + z^5]) 

sage: I.hilbert_numerator() 

-t^5 + 1 

sage: R.<a,b> = PolynomialRing(QQ) 

sage: J = R.ideal([a^2*b,a*b^2]) 

sage: J.hilbert_numerator() 

t^4 - 2*t^3 + 1 

sage: J.hilbert_numerator(grading=(10,3)) 

t^26 - t^23 - t^16 + 1 

""" 

if not self.is_homogeneous(): 

raise TypeError("Ideal must be homogeneous.") 

 

import sage.libs.singular.function_factory 

hilb = sage.libs.singular.function_factory.ff.hilb 

 

gb = self.groebner_basis() 

t = ZZ['t'].gen() 

n = self.ring().ngens() 

gb = MPolynomialIdeal(self.ring(), gb) 

if grading is not None: 

if not isinstance(grading, (list, tuple)) or any(a not in ZZ for a in grading): 

raise TypeError("Grading must be a list or a tuple of integers.") 

 

hs = hilb(gb, 1, tuple(grading), attributes={gb: {'isSB': 1}}) 

else: 

hs = hilb(gb, 1, attributes={gb: {'isSB': 1}}) 

return sum(ZZ(hs[i]) * t ** i for i in range(len(hs)-1)) 

 

 

@require_field 

def _normal_basis_libsingular(self): 

r""" 

Returns the normal basis for a given groebner basis. It will use 

the Groebner Basis as computed by 

``MPolynomialIdeal._groebner_basis_libsingular()``. 

 

EXAMPLES:: 

 

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

sage: I = R.ideal(x^2-2*x*z+5, x*y^2+y*z+1, 3*y^2-8*x*z) 

sage: I.normal_basis() #indirect doctest 

[z^2, y*z, x*z, z, x*y, y, x, 1] 

""" 

from sage.rings.polynomial.multi_polynomial_ideal_libsingular import kbase_libsingular 

from sage.rings.polynomial.multi_polynomial_sequence import PolynomialSequence 

gb = self._groebner_basis_libsingular() 

 

return PolynomialSequence(self.ring(), kbase_libsingular(self.ring().ideal(gb)), immutable=True) 

 

@require_field 

def normal_basis(self, algorithm='libsingular', singular=singular_default): 

""" 

Returns a vector space basis (consisting of monomials) of the 

quotient ring by the ideal, resp. of a free module by the module, 

in case it is finite dimensional and if the input is a standard 

basis with respect to the ring ordering. 

 

INPUT: 

 

``algorithm`` - defaults to use libsingular, if it is anything 

else we will use the ``kbase()`` command 

 

EXAMPLES:: 

 

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

sage: I = R.ideal(x^2+y^2+z^2-4, x^2+2*y^2-5, x*z-1) 

sage: I.normal_basis() 

[y*z^2, z^2, y*z, z, x*y, y, x, 1] 

sage: I.normal_basis(algorithm='singular') 

[y*z^2, z^2, y*z, z, x*y, y, x, 1] 

""" 

from sage.rings.polynomial.multi_polynomial_sequence import PolynomialSequence 

 

if algorithm == 'libsingular': 

return self._normal_basis_libsingular() 

else: 

gb = self.groebner_basis() 

R = self.ring() 

return PolynomialSequence(R, [R(f) for f in singular.kbase(R.ideal(gb))], immutable=True) 

 

 

class MPolynomialIdeal_macaulay2_repr: 

""" 

An ideal in a multivariate polynomial ring, which has an underlying 

Macaulay2 ring associated to it. 

 

EXAMPLES:: 

 

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

sage: I = ideal(x*y-z^2, y^2-w^2) 

sage: I 

Ideal (x*y - z^2, y^2 - w^2) of Multivariate Polynomial Ring in x, y, z, w over Integer Ring 

""" 

def _macaulay2_(self, macaulay2=None): 

""" 

Return Macaulay2 ideal corresponding to this ideal. 

 

EXAMPLES:: 

 

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

sage: I = ideal(x*y-z^2, y^2-w^2) #indirect doctest 

sage: macaulay2(I) # optional - macaulay2 

2 2 2 

ideal (x*y - z , y - w ) 

""" 

if macaulay2 is None: macaulay2 = macaulay2_default 

try: 

I = self.__macaulay2[macaulay2] 

I._check_valid() 

return I 

except KeyError: 

pass 

except AttributeError: 

self.__macaulay2 = {} 

except ValueError: 

pass 

 

R = self.ring() 

R._macaulay2_set_ring(macaulay2) 

 

gens = [repr(x) for x in self.gens()] 

if len(gens) == 0: 

gens = ['0'] 

z = macaulay2.ideal(gens) 

self.__macaulay2[macaulay2] = z 

return z 

 

def _groebner_basis_macaulay2(self): 

r""" 

Return the Groebner basis for this ideal, computed using 

Macaulay2. 

 

ALGORITHM: 

 

Computed using Macaulay2. A big advantage of Macaulay2 is that 

it can compute the Groebner basis of ideals in polynomial 

rings over the integers. 

 

EXAMPLES:: 

 

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

sage: I = ideal(x*y-z^2, y^2-w^2) 

sage: I.groebner_basis('macaulay2') # indirect doctest; optional - macaulay2 

[z^4 - x^2*w^2, y*z^2 - x*w^2, x*y - z^2, y^2 - w^2] 

 

The Groebner basis can be used to compute in 

`\ZZ/n\ZZ[x,\ldots]`. 

 

:: 

 

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

sage: I = ideal([y^2*z - x^3 - 19*x*z, y^2, 19^2]) 

sage: I.groebner_basis('macaulay2') # optional - macaulay2 

[x^3 + 19*x*z, y^2, 361] 

sage: I = ideal([y^2*z - x^3 - 19^2*x*z, y^2, 19^2]) 

sage: I.groebner_basis('macaulay2') # optional - macaulay2 

[x^3, y^2, 361] 

""" 

from sage.rings.polynomial.multi_polynomial_sequence import PolynomialSequence 

 

I = self._macaulay2_() 

G = str(I.gb().generators().external_string()).replace('\n','') 

i = G.rfind('{{') 

j = G.rfind('}}') 

G = G[i+2:j].split(',') 

R = self.ring() 

B = [R(f) for f in G] 

B = PolynomialSequence(self.ring(), B, immutable=True) 

return B 

 

def _reduce_using_macaulay2(self, f): 

""" 

EXAMPLES:: 

 

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

sage: I = ideal(x*y-z^2, y^2-w^2) 

sage: I._reduce_using_macaulay2(x*y-z^2 + y^2) # optional - macaulay2 

w^2 

""" 

I = self._macaulay2_() 

M2 = I.parent() 

k = M2('(%r) %% %s'%(f, I.name())) 

R = self.ring() 

return R(k) 

 

class NCPolynomialIdeal(MPolynomialIdeal_singular_repr, Ideal_nc): 

def __init__(self, ring, gens, coerce=True, side = "left"): 

r""" 

Creates a non-commutative polynomial ideal. 

 

INPUT: 

 

- ``ring`` - the g-algebra to which this ideal belongs 

- ``gens`` - the generators of this ideal 

- ``coerce`` (optional - default True) - generators are 

coerced into the ring before creating the ideal 

- ``side`` - optional string, either "left" (default) 

or "twosided"; defines whether this ideal is left 

of two-sided. 

 

EXAMPLES:: 

 

sage: A.<x,y,z> = FreeAlgebra(QQ, 3) 

sage: H = A.g_algebra({y*x:x*y-z, z*x:x*z+2*x, z*y:y*z-2*y}) 

sage: H.inject_variables() 

Defining x, y, z 

sage: I = H.ideal([y^2, x^2, z^2-H.one()],coerce=False) # indirect doctest 

sage: I #random 

Left Ideal (y^2, x^2, z^2 - 1) of Noncommutative Multivariate Polynomial Ring in x, y, z over Rational Field, nc-relations: {z*x: x*z + 2*x, z*y: y*z - 2*y, y*x: x*y - z} 

sage: sorted(I.gens(),key=str) 

[x^2, y^2, z^2 - 1] 

sage: H.ideal([y^2, x^2, z^2-H.one()], side="twosided") #random 

Twosided Ideal (y^2, x^2, z^2 - 1) of Noncommutative Multivariate Polynomial Ring in x, y, z over Rational Field, nc-relations: {z*x: x*z + 2*x, z*y: y*z - 2*y, y*x: x*y - z} 

sage: sorted(H.ideal([y^2, x^2, z^2-H.one()], side="twosided").gens(),key=str) 

[x^2, y^2, z^2 - 1] 

sage: H.ideal([y^2, x^2, z^2-H.one()], side="right") 

Traceback (most recent call last): 

... 

ValueError: Only left and two-sided ideals are allowed. 

 

""" 

if side == "right": 

raise ValueError("Only left and two-sided ideals are allowed.") 

Ideal_nc.__init__(self, ring, gens, coerce=coerce, side=side) 

 

def __call_singular(self, cmd, arg = None): 

""" 

Internal function for calling a Singular function. 

 

INPUT: 

 

- ``cmd`` - string, representing a Singular function 

- ``arg`` (Default: None) - arguments for which cmd is called 

 

OUTPUT: 

 

- result of the Singular function call 

 

EXAMPLES:: 

 

sage: A.<x,y,z> = FreeAlgebra(QQ, 3) 

sage: H = A.g_algebra({y*x:x*y-z, z*x:x*z+2*x, z*y:y*z-2*y}) 

sage: H.inject_variables() 

Defining x, y, z 

sage: id = H.ideal(x + y, y + z) 

sage: id.std() # indirect doctest # random 

Left Ideal (z, y, x) of Noncommutative Multivariate Polynomial Ring in x, y, z over Rational Field, nc-relations: {z*x: x*z + 2*x, z*y: y*z - 2*y, y*x: x*y - z} 

sage: sorted(id.std().gens(),key=str) 

[x, y, z] 

""" 

from sage.libs.singular.function import singular_function 

fun = singular_function(cmd) 

if arg is None: 

return fun(self, ring=self.ring()) 

 

return fun(self, arg, ring=self.ring()) 

 

@cached_method 

def std(self): 

r""" 

Computes a GB of the ideal. It is two-sided if and only if the ideal is two-sided. 

 

EXAMPLES:: 

 

sage: A.<x,y,z> = FreeAlgebra(QQ, 3) 

sage: H = A.g_algebra({y*x:x*y-z, z*x:x*z+2*x, z*y:y*z-2*y}) 

sage: H.inject_variables() 

Defining x, y, z 

sage: I = H.ideal([y^2, x^2, z^2-H.one()],coerce=False) 

sage: I.std() #random 

Left Ideal (z^2 - 1, y*z - y, x*z + x, y^2, 2*x*y - z - 1, x^2) of Noncommutative Multivariate Polynomial Ring in x, y, z over Rational Field, nc-relations: {z*x: x*z + 2*x, z*y: y*z - 2*y, y*x: x*y - z} 

sage: sorted(I.std().gens(),key=str) 

[2*x*y - z - 1, x*z + x, x^2, y*z - y, y^2, z^2 - 1] 

 

 

If the ideal is a left ideal, then std returns a left 

Groebner basis. But if it is a two-sided ideal, then 

the output of std and :meth:`twostd` coincide:: 

 

sage: JL = H.ideal([x^3, y^3, z^3 - 4*z]) 

sage: JL #random 

Left Ideal (x^3, y^3, z^3 - 4*z) of Noncommutative Multivariate Polynomial Ring in x, y, z over Rational Field, nc-relations: {z*x: x*z + 2*x, z*y: y*z - 2*y, y*x: x*y - z} 

sage: sorted(JL.gens(),key=str) 

[x^3, y^3, z^3 - 4*z] 

sage: JL.std() #random 

Left Ideal (z^3 - 4*z, y*z^2 - 2*y*z, x*z^2 + 2*x*z, 2*x*y*z - z^2 - 2*z, y^3, x^3) of Noncommutative Multivariate Polynomial Ring in x, y, z over Rational Field, nc-relations: {z*x: x*z + 2*x, z*y: y*z - 2*y, y*x: x*y - z} 

sage: sorted(JL.std().gens(),key=str) 

[2*x*y*z - z^2 - 2*z, x*z^2 + 2*x*z, x^3, y*z^2 - 2*y*z, y^3, z^3 - 4*z] 

sage: JT = H.ideal([x^3, y^3, z^3 - 4*z], side='twosided') 

sage: JT #random 

Twosided Ideal (x^3, y^3, z^3 - 4*z) of Noncommutative Multivariate Polynomial Ring in x, y, z over Rational Field, nc-relations: {z*x: x*z + 2*x, z*y: y*z - 2*y, y*x: x*y - z} 

sage: sorted(JT.gens(),key=str) 

[x^3, y^3, z^3 - 4*z] 

sage: JT.std() #random 

Twosided Ideal (z^3 - 4*z, y*z^2 - 2*y*z, x*z^2 + 2*x*z, y^2*z - 2*y^2, 2*x*y*z - z^2 - 2*z, x^2*z + 2*x^2, y^3, x*y^2 - y*z, x^2*y - x*z - 2*x, x^3) of Noncommutative Multivariate Polynomial Ring in x, y, z over Rational Field, nc-relations: {z*x: x*z + 2*x, z*y: y*z - 2*y, y*x: x*y - z} 

sage: sorted(JT.std().gens(),key=str) 

[2*x*y*z - z^2 - 2*z, x*y^2 - y*z, x*z^2 + 2*x*z, x^2*y - x*z - 2*x, x^2*z + 2*x^2, x^3, y*z^2 - 2*y*z, y^2*z - 2*y^2, y^3, z^3 - 4*z] 

sage: JT.std() == JL.twostd() 

True 

 

ALGORITHM: Uses Singular's std command 

""" 

if self.side() == 'twosided': 

return self.twostd() 

return self.ring().ideal( self.__call_singular('std'), side=self.side()) 

# return self.__call_singular('std') 

 

@cached_method 

def twostd(self): 

r""" 

Computes a two-sided GB of the ideal (even if it is a left ideal). 

 

EXAMPLES:: 

 

sage: A.<x,y,z> = FreeAlgebra(QQ, 3) 

sage: H = A.g_algebra({y*x:x*y-z, z*x:x*z+2*x, z*y:y*z-2*y}) 

sage: H.inject_variables() 

Defining x, y, z 

sage: I = H.ideal([y^2, x^2, z^2-H.one()],coerce=False) 

sage: I.twostd() #random 

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

sage: sorted(I.twostd().gens(),key=str) 

[2*x*y - z - 1, x*z + x, x^2, y*z - y, y^2, z^2 - 1] 

 

ALGORITHM: Uses Singular's twostd command 

""" 

return self.ring().ideal( self.__call_singular('twostd'), side='twosided') 

# return self.__call_singular('twostd') 

 

# def syz(self): 

# return self.__call_singular('syz') 

 

@cached_method 

def _groebner_strategy(self): 

""" 

Return Singular's Groebner Strategy object for the Groebner 

basis of this ideal which implements some optimized functions. 

 

EXAMPLES:: 

 

sage: A.<x,y,z> = FreeAlgebra(QQ, 3) 

sage: H.<x,y,z> = A.g_algebra({y*x:x*y-z, z*x:x*z+2*x, z*y:y*z-2*y}) 

sage: I = H.ideal([y^2, x^2, z^2-H.one()],coerce=False) 

sage: I._groebner_strategy() #random 

Groebner Strategy for ideal generated by 6 elements over 

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

Field, nc-relations: {z*x: x*z + 2*x, z*y: y*z - 2*y, y*x: x*y - z} 

 

.. NOTE:: 

 

This function is mainly used internally. 

""" 

from sage.libs.singular.groebner_strategy import NCGroebnerStrategy 

return NCGroebnerStrategy(self.std()) 

 

def reduce(self,p): 

""" 

Reduce an element modulo a Groebner basis for this ideal. 

 

It returns 0 if and only if the element is in this ideal. In any 

case, this reduction is unique up to monomial orders. 

 

NOTE: 

 

There are left and two-sided ideals. Hence, 

 

EXAMPLES:: 

 

sage: A.<x,y,z> = FreeAlgebra(QQ, 3) 

sage: H.<x,y,z> = A.g_algebra({y*x:x*y-z, z*x:x*z+2*x, z*y:y*z-2*y}) 

sage: I = H.ideal([y^2, x^2, z^2-H.one()],coerce=False, side='twosided') 

sage: Q = H.quotient(I); Q #random 

Quotient of Noncommutative Multivariate Polynomial Ring in x, y, z 

over Rational Field, nc-relations: {z*x: x*z + 2*x, 

z*y: y*z - 2*y, y*x: x*y - z} by the ideal (y^2, x^2, z^2 - 1) 

sage: Q.2^2 == Q.one() # indirect doctest 

True 

 

Here, we see that the relation that we just found in the quotient 

is actually a consequence of the given relations:: 

 

sage: H.2^2-H.one() in I.std().gens() 

True 

 

Here is the corresponding direct test:: 

 

sage: I.reduce(z^2) 

1 

 

""" 

return self._groebner_strategy().normal_form(p) 

 

def _contains_(self,p): 

""" 

EXAMPLES: 

 

We define a left and a two-sided ideal:: 

 

sage: A.<x,y,z> = FreeAlgebra(QQ, 3) 

sage: H.<x,y,z> = A.g_algebra({y*x:x*y-z, z*x:x*z+2*x, z*y:y*z-2*y}) 

sage: JL = H.ideal([x^3, y^3, z^3 - 4*z]) 

sage: JL.std() #random 

Left Ideal (z^3 - 4*z, y*z^2 - 2*y*z, x*z^2 + 2*x*z, 2*x*y*z - z^2 - 2*z, y^3, x^3) of Noncommutative Multivariate Polynomial Ring in x, y, z over Rational Field, nc-relations: {z*x: x*z + 2*x, z*y: y*z - 2*y, y*x: x*y - z} 

sage: JT = H.ideal([x^3, y^3, z^3 - 4*z], side='twosided') 

sage: JT.std() #random 

Twosided Ideal (z^3 - 4*z, y*z^2 - 2*y*z, x*z^2 + 2*x*z, y^2*z - 2*y^2, 2*x*y*z - z^2 - 2*z, x^2*z + 2*x^2, y^3, x*y^2 - y*z, x^2*y - x*z - 2*x, x^3) of Noncommutative Multivariate Polynomial Ring in x, y, z over Rational Field, nc-relations: {z*x: x*z + 2*x, z*y: y*z - 2*y, y*x: x*y - z} 

 

Apparently, ``x*y^2-y*z`` should be in the two-sided, but not 

in the left ideal:: 

 

sage: x*y^2-y*z in JL #indirect doctest 

False 

sage: x*y^2-y*z in JT 

True 

 

""" 

return self.reduce(p).is_zero() 

 

@require_field 

def syzygy_module(self): 

r""" 

Computes the first syzygy (i.e., the module of relations of the 

given generators) of the ideal. 

 

NOTE: 

 

Only left syzygies can be computed. So, even if the ideal is 

two-sided, then the syzygies are only one-sided. In that case, 

a warning is printed. 

 

EXAMPLES:: 

 

sage: A.<x,y,z> = FreeAlgebra(QQ, 3) 

sage: H = A.g_algebra({y*x:x*y-z, z*x:x*z+2*x, z*y:y*z-2*y}) 

sage: H.inject_variables() 

Defining x, y, z 

sage: I = H.ideal([y^2, x^2, z^2-H.one()],coerce=False) 

sage: G = vector(I.gens()); G 

d...: UserWarning: You are constructing a free module 

over a noncommutative ring. Sage does not have a concept 

of left/right and both sided modules, so be careful. 

It's also not guaranteed that all multiplications are 

done from the right side. 

d...: UserWarning: You are constructing a free module 

over a noncommutative ring. Sage does not have a concept 

of left/right and both sided modules, so be careful. 

It's also not guaranteed that all multiplications are 

done from the right side. 

(y^2, x^2, z^2 - 1) 

sage: M = I.syzygy_module(); M 

[ -z^2 - 8*z - 15 0 y^2] 

[ 0 -z^2 + 8*z - 15 x^2] 

[ x^2*z^2 + 8*x^2*z + 15*x^2 -y^2*z^2 + 8*y^2*z - 15*y^2 -4*x*y*z + 2*z^2 + 2*z] 

[ x^2*y*z^2 + 9*x^2*y*z - 6*x*z^3 + 20*x^2*y - 72*x*z^2 - 282*x*z - 360*x -y^3*z^2 + 7*y^3*z - 12*y^3 6*y*z^2] 

[ x^3*z^2 + 7*x^3*z + 12*x^3 -x*y^2*z^2 + 9*x*y^2*z - 4*y*z^3 - 20*x*y^2 + 52*y*z^2 - 224*y*z + 320*y -6*x*z^2] 

[ x^2*y^2*z + 4*x^2*y^2 - 8*x*y*z^2 - 48*x*y*z + 12*z^3 - 64*x*y + 108*z^2 + 312*z + 288 -y^4*z + 4*y^4 0] 

[ 2*x^3*y*z + 8*x^3*y + 9*x^2*z + 27*x^2 -2*x*y^3*z + 8*x*y^3 - 12*y^2*z^2 + 99*y^2*z - 195*y^2 -36*x*y*z + 24*z^2 + 18*z] 

[ x^4*z + 4*x^4 -x^2*y^2*z + 4*x^2*y^2 - 4*x*y*z^2 + 32*x*y*z - 6*z^3 - 64*x*y + 66*z^2 - 240*z + 288 0] 

[x^3*y^2*z + 4*x^3*y^2 + 18*x^2*y*z - 36*x*z^3 + 66*x^2*y - 432*x*z^2 - 1656*x*z - 2052*x -x*y^4*z + 4*x*y^4 - 8*y^3*z^2 + 62*y^3*z - 114*y^3 48*y*z^2 - 36*y*z] 

 

sage: M*G 

(0, 0, 0, 0, 0, 0, 0, 0, 0) 

 

ALGORITHM: Uses Singular's syz command 

""" 

if self.side() == 'twosided': 

warn("The result of this Syzygy computation is one-sided (left)!") 

import sage.libs.singular.function_factory 

syz = sage.libs.singular.function_factory.ff.syz 

from sage.matrix.constructor import matrix 

 

#return self._singular_().syz().transpose().sage_matrix(self.ring()) 

S = syz(self) 

return matrix(self.ring(), S) 

 

def res(self, length): 

r""" 

Compute the resolution up to a given length of the ideal. 

 

NOTE: 

 

Only left syzygies can be computed. So, even if the ideal is 

two-sided, then the resolution is only one-sided. In that case, 

a warning is printed. 

 

EXAMPLES:: 

 

sage: A.<x,y,z> = FreeAlgebra(QQ, 3) 

sage: H = A.g_algebra({y*x:x*y-z, z*x:x*z+2*x, z*y:y*z-2*y}) 

sage: H.inject_variables() 

Defining x, y, z 

sage: I = H.ideal([y^2, x^2, z^2-H.one()],coerce=False) 

sage: I.res(3) 

<Resolution> 

""" 

if self.side() == 'twosided': 

warn("The resulting resolution is one-sided (left)!") 

return self.__call_singular('res', length) 

 

 

@richcmp_method 

class MPolynomialIdeal( MPolynomialIdeal_singular_repr, \ 

MPolynomialIdeal_macaulay2_repr, \ 

MPolynomialIdeal_magma_repr, \ 

Ideal_generic ): 

def __init__(self, ring, gens, coerce=True): 

r""" 

Create an ideal in a multivariate polynomial ring. 

 

INPUT: 

 

- ``ring`` - the ring the ideal is defined in 

 

- ``gens`` - a list of generators for the ideal 

 

- ``coerce`` - coerce elements to the ring ``ring``? 

 

EXAMPLES:: 

 

sage: R.<x,y> = PolynomialRing(IntegerRing(), 2, order='lex') 

sage: R.ideal([x, y]) 

Ideal (x, y) of Multivariate Polynomial Ring in x, y over Integer Ring 

sage: R.<x0,x1> = GF(3)[] 

sage: R.ideal([x0^2, x1^3]) 

Ideal (x0^2, x1^3) of Multivariate Polynomial Ring in x0, x1 over Finite Field of size 3 

""" 

Ideal_generic.__init__(self, ring, gens, coerce=coerce) 

self._gb_by_ordering = dict() 

 

def __hash__(self): 

r""" 

Stupid constant hash function! 

 

TESTS:: 

 

sage: R.<x,y> = PolynomialRing(IntegerRing(), 2, order='lex') 

sage: hash(R.ideal([x, y])) 

0 

""" 

return 0 

 

@cached_method 

def gens(self): 

""" 

Return a set of generators / a basis of this ideal. This is usually the 

set of generators provided during object creation. 

 

EXAMPLES:: 

 

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

sage: I = Ideal([x,y+1]); I 

Ideal (x, y + 1) of Multivariate Polynomial Ring in x, y over Rational Field 

sage: I.gens() 

[x, y + 1] 

""" 

from sage.rings.polynomial.multi_polynomial_sequence import PolynomialSequence 

return PolynomialSequence(self.ring(), Ideal_generic.gens(self), immutable=True) 

 

@property 

def basis(self): 

""" 

Shortcut to ``gens()``. 

 

EXAMPLES:: 

 

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

sage: I = Ideal([x,y+1]) 

sage: I.basis 

[x, y + 1] 

 

""" 

return self.gens() 

 

def __richcmp__(self, other, op): 

""" 

Compare ``self`` and ``other``. 

 

INPUT: 

 

- ``other`` -- a polynomial ideal 

 

OUTPUT: 

 

boolean 

 

ALGORITHM: 

 

Comparison for ``==`` and ``!=`` compares two Groebner bases. 

 

Comparison for ``<=` and ``>=`` tests the inclusion of ideals 

using the usual ideal membership test, namely all generators 

of one ideal must reduce to zero in the other ideal's Groebner 

basis. 

 

Comparison for ``<`` and ``>`` tests for inclusion and different 

Groebner bases. 

 

EXAMPLES:: 

 

sage: R.<x,y> = ZZ[]; I = R*[x^2 + y, 2*y]; J = R*[x^2 + y] 

sage: I > J 

True 

sage: J < I 

True 

sage: I == I 

True 

 

:: 

 

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

sage: I = R*[x^2 + x, y] 

sage: J = R*[x + 1, y] 

sage: J < I 

False 

sage: I < J 

True 

 

:: 

 

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

sage: I = R*[x^2 + x, y] 

sage: J = R*[x + 1, y] 

sage: J > I 

True 

sage: I > J 

False 

 

:: 

 

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

sage: I = R.ideal() 

sage: I == R.ideal() 

True 

 

:: 

 

sage: R = PolynomialRing(QQ, names=[]) 

sage: R.ideal(0) == R.ideal(0) 

True 

 

:: 

 

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

sage: I = (x^3 + y, y)*R 

sage: J = (x^3 + y, y, y*x^3 + y^2)*R 

sage: I == J 

True 

 

:: 

 

sage: R = PolynomialRing(QQ, 'x,y,z', order='degrevlex') 

sage: S = PolynomialRing(QQ, 'x,y,z', order='invlex') 

sage: I = R.ideal([R.0,R.1]) 

sage: J = S.ideal([S.0,S.1]) 

sage: I == J 

True 

 

TESTS: 

 

We test to make sure that pickling works with the cached 

Groebner basis:: 

 

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

sage: I = R*[x^2 + x, y] 

sage: J = R*[x + 1, y] 

sage: J >= I 

True 

sage: I >= J 

False 

 

sage: loads(dumps(I)).__getstate__() 

(Monoid of ideals of Multivariate Polynomial Ring in x, y over Finite Field of size 32003, 

{'_Ideal_generic__gens': (x^2 + x, y), 

'_Ideal_generic__ring': Multivariate Polynomial Ring in x, y over Finite Field of size 32003, 

'_cache__groebner_basis': {}, 

'_gb_by_ordering': {'degrevlex': [x^2 + x, y]}, 

'gens': Pickle of the cached method "gens", 

'groebner_basis': Pickle of the cached method "groebner_basis"}) 

 

This example checks :trac:`12802`:: 

 

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

sage: I = R * [ x^2 + y, 2*y ] 

sage: J = R * [ x^2 - y, 2*y ] 

sage: I == J 

True 

 

Another good test from the discussion in :trac:`12802`:: 

 

sage: Rx = PolynomialRing(QQ, 2, "x") 

sage: Ix = Rx.ideal(Rx.0) 

sage: Ry = PolynomialRing(QQ, 2, "y") 

sage: Iy = Ry.ideal(Ry.0) 

sage: Ix == Iy 

False 

 

However, this should work if only the orderings are different:: 

 

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

sage: S = PolynomialRing(QQ, 'x', 2, order='lex') 

sage: R == S 

False 

sage: I = R*[R.0^2 + R.1, R.1] 

sage: J = S*[S.0^2 + S.1, S.1] 

sage: I == J 

True 

""" 

# first check the type 

if not isinstance(other, MPolynomialIdeal): 

return NotImplemented 

 

if self is other: 

return rich_to_bool(op, 0) 

 

# comparison for >= and > : swap the arguments 

if op == op_GE: 

return other.__richcmp__(self, op_LE) 

elif op == op_GT: 

return other.__richcmp__(self, op_LT) 

 

# the ideals may be defined w.r.t. to different term orders 

# but are still the same. 

R = self.ring() 

S = other.ring() 

 

# separate next two tests to avoid unnecessary creation of 

# Groebner basis 

if S is not R: 

if S.change_ring(order=R.term_order()) != R: # rings are unique 

return NotImplemented 

else: 

# at this point, the rings are the same, but for the term order, 

# and we can fix that easily 

other_new = other.change_ring(R) 

else: 

other_new = other 

 

if set(self.gens()) == set(other_new.gens()): 

return rich_to_bool(op, 0) 

 

# comparison for <= 

# needs just the Groebner basis for other 

if op == op_LE: 

l = self.gens() 

try: 

# first check whether the GB is cached already 

if other_new.groebner_basis.is_in_cache(): 

r = other_new.groebner_basis() 

elif len(other_new._gb_by_ordering): 

o, r = next(iteritems(other_new._gb_by_ordering)) 

l = self.change_ring(R.change_ring(order=o)).gens() 

else: # use easy GB otherwise 

newR = R.change_ring(order="degrevlex") 

l = self.change_ring(newR).gens() 

r = other_new.change_ring(newR).groebner_basis() 

# remember this Groebner basis for future reference 

other_new._gb_by_ordering['degrevlex'] = r 

except AttributeError: # e.g. quotient rings 

r = other_new.groebner_basis() 

return all(f.reduce(r) == 0 for f in l) 

 

# comparison for == and != and < 

# needs both groebner bases for the same term order 

# first check whether the GBs are cached already 

if op in [op_EQ, op_NE, op_LT]: 

try: 

if (other_new.groebner_basis.is_in_cache() 

or self.groebner_basis().is_in_cache()): 

l = self.groebner_basis() 

r = other_new.groebner_basis() 

else: # use easy GB otherwise 

newR = R.change_ring(order="degrevlex") 

l = self.change_ring(newR).groebner_basis() 

r = other_new.change_ring(newR).groebner_basis() 

except AttributeError: # e.g. quotient rings 

l = self.groebner_basis() 

r = other_new.groebner_basis() 

contained = all(f.reduce(r) == 0 for f in l) 

contains = all(f.reduce(l) == 0 for f in r) 

if op == op_EQ: 

return contained and contains 

elif op == op_NE: 

return not (contained and contains) 

else: # remaining case < 

return contained and not contains 

 

def groebner_fan(self, is_groebner_basis=False, symmetry=None, verbose=False): 

r""" 

Return the Groebner fan of this ideal. 

 

The base ring must be `\QQ` or a finite field 

`\GF{p}` of with `p \leq 32749`. 

 

EXAMPLES:: 

 

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

sage: i = ideal(x^2 - y^2 + 1) 

sage: g = i.groebner_fan() 

sage: g.reduced_groebner_bases() 

[[x^2 - y^2 + 1], [-x^2 + y^2 - 1]] 

 

INPUT: 

 

 

- ``is_groebner_basis`` - bool (default False). if 

True, then I.gens() must be a Groebner basis with respect to the 

standard degree lexicographic term order. 

 

- ``symmetry`` - default: None; if not None, describes 

symmetries of the ideal 

 

- ``verbose`` - default: False; if True, printout 

useful info during computations 

""" 

import sage.rings.polynomial.groebner_fan as groebner_fan 

return groebner_fan.GroebnerFan(self, is_groebner_basis=is_groebner_basis, 

symmetry=symmetry, verbose=verbose) 

 

@cached_method(do_pickle=True) 

def groebner_basis(self, algorithm='', deg_bound=None, mult_bound=None, prot=False, *args, **kwds): 

r""" 

Return the reduced Groebner basis of this ideal. 

 

A Groebner basis `g_1,...,g_n` for an ideal `I` is a 

generating set such that `<LM(g_i)> = LM(I)`, i.e., the 

leading monomial ideal of `I` is spanned by the leading terms 

of `g_1,...,g_n`. Groebner bases are the key concept in 

computational ideal theory in multivariate polynomial rings 

which allows a variety of problems to be solved. 

 

Additionally, a *reduced* Groebner basis `G` is a unique 

representation for the ideal `<G>` with respect to the chosen 

monomial ordering. 

 

INPUT: 

 

- ``algorithm`` - determines the algorithm to use, see below 

for available algorithms. 

 

- ``deg_bound`` - only compute to degree ``deg_bound``, that 

is, ignore all S-polynomials of higher degree. (default: 

``None``) 

 

- ``mult_bound`` - the computation is stopped if the ideal is 

zero-dimensional in a ring with local ordering and its 

multiplicity is lower than ``mult_bound``. Singular 

only. (default: ``None``) 

 

- ``prot`` - if set to ``True`` the computation protocol of 

the underlying implementation is printed. If an algorithm 

from the ``singular:`` or ``magma:`` family is used, 

``prot`` may also be ``sage`` in which case the output is 

parsed and printed in a common format where the amount of 

information printed can be controlled via calls to 

:func:`set_verbose`. 

 

- ``*args`` - additional parameters passed to the respective 

implementations 

 

- ``**kwds`` - additional keyword parameters passed to the 

respective implementations 

 

ALGORITHMS: 

 

'' 

autoselect (default) 

 

'singular:groebner' 

Singular's ``groebner`` command 

 

'singular:std' 

Singular's ``std`` command 

 

'singular:stdhilb' 

Singular's ``stdhib`` command 

 

'singular:stdfglm' 

Singular's ``stdfglm`` command 

 

'singular:slimgb' 

Singular's ``slimgb`` command 

 

'libsingular:groebner' 

libSingular's ``groebner`` command 

 

'libsingular:std' 

libSingular's ``std`` command 

 

'libsingular:slimgb' 

libSingular's ``slimgb`` command 

 

'libsingular:stdhilb' 

libSingular's ``stdhib`` command 

 

'libsingular:stdfglm' 

libSingular's ``stdfglm`` command 

 

'toy:buchberger' 

Sage's toy/educational buchberger without Buchberger criteria 

 

'toy:buchberger2' 

Sage's toy/educational buchberger with Buchberger criteria 

 

'toy:d_basis' 

Sage's toy/educational algorithm for computation over PIDs 

 

'macaulay2:gb' 

Macaulay2's ``gb`` command (if available) 

 

'magma:GroebnerBasis' 

Magma's ``Groebnerbasis`` command (if available) 

 

'ginv:TQ', 'ginv:TQBlockHigh', 'ginv:TQBlockLow' and 'ginv:TQDegree' 

One of GINV's implementations (if available) 

 

'giac:gbasis' 

Giac's ``gbasis`` command (if available) 

 

If only a system is given - e.g. 'magma' - the default algorithm is 

chosen for that system. 

 

.. NOTE:: 

 

The Singular and libSingular versions of the respective 

algorithms are identical, but the former calls an external 

Singular process while the later calls a C function, 

i.e. the calling overhead is smaller. However, the 

libSingular interface does not support pretty printing of 

computation protocols. 

 

EXAMPLES: 

 

Consider Katsura-3 over `\QQ` with lexicographical term 

ordering. We compute the reduced Groebner basis using every 

available implementation and check their equality. 

 

:: 

 

sage: P.<a,b,c> = PolynomialRing(QQ,3, order='lex') 

sage: I = sage.rings.ideal.Katsura(P,3) # regenerate to prevent caching 

sage: I.groebner_basis() 

[a - 60*c^3 + 158/7*c^2 + 8/7*c - 1, b + 30*c^3 - 79/7*c^2 + 3/7*c, c^4 - 10/21*c^3 + 1/84*c^2 + 1/84*c] 

 

:: 

 

sage: I = sage.rings.ideal.Katsura(P,3) # regenerate to prevent caching 

sage: I.groebner_basis('libsingular:groebner') 

[a - 60*c^3 + 158/7*c^2 + 8/7*c - 1, b + 30*c^3 - 79/7*c^2 + 3/7*c, c^4 - 10/21*c^3 + 1/84*c^2 + 1/84*c] 

 

:: 

 

sage: I = sage.rings.ideal.Katsura(P,3) # regenerate to prevent caching 

sage: I.groebner_basis('libsingular:std') 

[a - 60*c^3 + 158/7*c^2 + 8/7*c - 1, b + 30*c^3 - 79/7*c^2 + 3/7*c, c^4 - 10/21*c^3 + 1/84*c^2 + 1/84*c] 

 

:: 

 

sage: I = sage.rings.ideal.Katsura(P,3) # regenerate to prevent caching 

sage: I.groebner_basis('libsingular:stdhilb') 

[a - 60*c^3 + 158/7*c^2 + 8/7*c - 1, b + 30*c^3 - 79/7*c^2 + 3/7*c, c^4 - 10/21*c^3 + 1/84*c^2 + 1/84*c] 

 

:: 

 

sage: I = sage.rings.ideal.Katsura(P,3) # regenerate to prevent caching 

sage: I.groebner_basis('libsingular:stdfglm') 

[a - 60*c^3 + 158/7*c^2 + 8/7*c - 1, b + 30*c^3 - 79/7*c^2 + 3/7*c, c^4 - 10/21*c^3 + 1/84*c^2 + 1/84*c] 

 

:: 

 

sage: I = sage.rings.ideal.Katsura(P,3) # regenerate to prevent caching 

sage: I.groebner_basis('libsingular:slimgb') 

[a - 60*c^3 + 158/7*c^2 + 8/7*c - 1, b + 30*c^3 - 79/7*c^2 + 3/7*c, c^4 - 10/21*c^3 + 1/84*c^2 + 1/84*c] 

 

Giac only supports the degree reverse lexicographical ordering:: 

 

sage: I = sage.rings.ideal.Katsura(P,3) # regenerate to prevent caching 

sage: J = I.change_ring(P.change_ring(order='degrevlex')) 

sage: gb = J.groebner_basis('giac') # optional - giacpy_sage, random 

sage: gb # optional - giacpy_sage 

[c^3 - 79/210*c^2 + 1/30*b + 1/70*c, b^2 - 3/5*c^2 - 1/5*b + 1/5*c, b*c + 6/5*c^2 - 1/10*b - 2/5*c, a + 2*b + 2*c - 1] 

 

sage: J.groebner_basis.set_cache(gb) # optional - giacpy_sage 

sage: ideal(J.transformed_basis()).change_ring(P).interreduced_basis() # testing trac 21884 

[a - 60*c^3 + 158/7*c^2 + 8/7*c - 1, b + 30*c^3 - 79/7*c^2 + 3/7*c, c^4 - 10/21*c^3 + 1/84*c^2 + 1/84*c] 

 

Giac's gbasis over `\QQ` can benefit from a probabilistic lifting and 

multi threaded operations:: 

 

sage: A9=PolynomialRing(QQ,9,'x') # optional - giacpy_sage 

sage: I9=sage.rings.ideal.Katsura(A9) # optional - giacpy_sage 

sage: I9.groebner_basis("giac",proba_epsilon=1e-7) # optional - giacpy_sage, long time (3s) 

Running a probabilistic check for the reconstructed Groebner basis... 

Polynomial Sequence with 143 Polynomials in 9 Variables 

 

The list of available Giac options is provided at :func:`sage.libs.giac.groebner_basis`. 

 

Note that ``toy:buchberger`` does not return the reduced Groebner 

basis, :: 

 

sage: I = sage.rings.ideal.Katsura(P,3) # regenerate to prevent caching 

sage: I.groebner_basis('toy:buchberger') 

[a^2 - a + 2*b^2 + 2*c^2, 

a*b + b*c - 1/2*b, a + 2*b + 2*c - 1, 

b^2 + 3*b*c - 1/2*b + 3*c^2 - c, 

b*c - 1/10*b + 6/5*c^2 - 2/5*c, 

b + 30*c^3 - 79/7*c^2 + 3/7*c, 

c^6 - 79/210*c^5 - 229/2100*c^4 + 121/2520*c^3 + 1/3150*c^2 - 11/12600*c, 

c^4 - 10/21*c^3 + 1/84*c^2 + 1/84*c] 

 

but that ``toy:buchberger2`` does.:: 

 

sage: I = sage.rings.ideal.Katsura(P,3) # regenerate to prevent caching 

sage: I.groebner_basis('toy:buchberger2') 

[a - 60*c^3 + 158/7*c^2 + 8/7*c - 1, b + 30*c^3 - 79/7*c^2 + 3/7*c, c^4 - 10/21*c^3 + 1/84*c^2 + 1/84*c] 

 

:: 

 

sage: I = sage.rings.ideal.Katsura(P,3) # regenerate to prevent caching 

sage: I.groebner_basis('macaulay2:gb') # optional - macaulay2 

[a - 60*c^3 + 158/7*c^2 + 8/7*c - 1, b + 30*c^3 - 79/7*c^2 + 3/7*c, c^4 - 10/21*c^3 + 1/84*c^2 + 1/84*c] 

 

:: 

 

sage: I = sage.rings.ideal.Katsura(P,3) # regenerate to prevent caching 

sage: I.groebner_basis('magma:GroebnerBasis') # optional - magma 

[a - 60*c^3 + 158/7*c^2 + 8/7*c - 1, b + 30*c^3 - 79/7*c^2 + 3/7*c, c^4 - 10/21*c^3 + 1/84*c^2 + 1/84*c] 

 

Singular and libSingular can compute Groebner basis with degree 

restrictions.:: 

 

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

sage: I = R*[x^3+y^2,x^2*y+1] 

sage: I.groebner_basis(algorithm='singular') 

[x^3 + y^2, x^2*y + 1, y^3 - x] 

sage: I.groebner_basis(algorithm='singular',deg_bound=2) 

[x^3 + y^2, x^2*y + 1] 

sage: I.groebner_basis() 

[x^3 + y^2, x^2*y + 1, y^3 - x] 

sage: I.groebner_basis(deg_bound=2) 

[x^3 + y^2, x^2*y + 1] 

 

A protocol is printed, if the verbosity level is at least 2, 

or if the argument ``prot`` is provided. Historically, the 

protocol did not appear during doctests, so, we skip the 

examples with protocol output. :: 

 

sage: set_verbose(2) 

sage: I = R*[x^3+y^2,x^2*y+1] 

sage: I.groebner_basis() # not tested 

std in (QQ),(x,y),(dp(2),C) 

[...:2]3ss4s6 

(S:2)-- 

product criterion:1 chain criterion:0 

[x^3 + y^2, x^2*y + 1, y^3 - x] 

sage: I.groebner_basis(prot=False) 

std in (QQ),(x,y),(dp(2),C) 

[...:2]3ss4s6 

(S:2)-- 

product criterion:1 chain criterion:0 

[x^3 + y^2, x^2*y + 1, y^3 - x] 

sage: set_verbose(0) 

sage: I.groebner_basis(prot=True) # not tested 

std in (QQ),(x,y),(dp(2),C) 

[...:2]3ss4s6 

(S:2)-- 

product criterion:1 chain criterion:0 

[x^3 + y^2, x^2*y + 1, y^3 - x] 

 

The list of available options is provided at 

:class:`~sage.libs.singular.option.LibSingularOptions`. 

 

Note that Groebner bases over `\ZZ` can also be computed.:: 

 

sage: P.<a,b,c> = PolynomialRing(ZZ,3) 

sage: I = P * (a + 2*b + 2*c - 1, a^2 - a + 2*b^2 + 2*c^2, 2*a*b + 2*b*c - b) 

sage: I.groebner_basis() 

[b^3 - 181*b*c^2 + 222*c^3 - 26*b*c - 146*c^2 + 19*b + 24*c, 

2*b*c^2 - 48*c^3 + 3*b*c + 22*c^2 - 2*b - 2*c, 

42*c^3 + 45*b^2 + 54*b*c + 22*c^2 - 13*b - 12*c, 

2*b^2 + 6*b*c + 6*c^2 - b - 2*c, 

10*b*c + 12*c^2 - b - 4*c, 

a + 2*b + 2*c - 1] 

 

:: 

 

sage: I.groebner_basis('macaulay2') # optional - macaulay2 

[b^3 + b*c^2 + 12*c^3 + b^2 + b*c - 4*c^2, 

2*b*c^2 - 6*c^3 + b^2 + 5*b*c + 8*c^2 - b - 2*c, 

42*c^3 + b^2 + 2*b*c - 14*c^2 + b, 

2*b^2 - 4*b*c - 6*c^2 + 2*c, 10*b*c + 12*c^2 - b - 4*c, 

a + 2*b + 2*c - 1] 

 

Groebner bases over `\ZZ/n\ZZ` are also supported:: 

 

sage: P.<a,b,c> = PolynomialRing(Zmod(1000),3) 

sage: I = P * (a + 2*b + 2*c - 1, a^2 - a + 2*b^2 + 2*c^2, 2*a*b + 2*b*c - b) 

sage: I.groebner_basis() 

[b*c^2 + 732*b*c + 808*b, 

2*c^3 + 884*b*c + 666*c^2 + 320*b, 

b^2 + 438*b*c + 281*b, 

5*b*c + 156*c^2 + 112*b + 948*c, 

50*c^2 + 600*b + 650*c, 

a + 2*b + 2*c + 999, 

125*b] 

 

:: 

 

sage: R.<x,y,z> = PolynomialRing(Zmod(2233497349584)) 

sage: I = R.ideal([z*(x-3*y), 3^2*x^2-y*z, z^2+y^2]) 

sage: I.groebner_basis() 

[2*z^4, y*z^2 + 81*z^3, 248166372176*z^3, 9*x^2 - y*z, y^2 + z^2, x*z + 

2233497349581*y*z, 248166372176*y*z] 

 

Sage also supports local orderings:: 

 

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

sage: I = P * ( x*y*z + z^5, 2*x^2 + y^3 + z^7, 3*z^5 +y ^5 ) 

sage: I.groebner_basis() 

[x^2 + 1/2*y^3, x*y*z + z^5, y^5 + 3*z^5, y^4*z - 2*x*z^5, z^6] 

 

We can represent every element in the ideal as a combination 

of the generators using the :meth:`~sage.rings.polynomial.multi_polynomial_element.MPolynomial_polydict.lift` method:: 

 

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

sage: I = P * ( x*y*z + z^5, 2*x^2 + y^3 + z^7, 3*z^5 +y ^5 ) 

sage: J = Ideal(I.groebner_basis()) 

sage: f = sum(P.random_element(terms=2)*f for f in I.gens()) 

sage: f 

1/2*y^2*z^7 - 1/4*y*z^8 + 2*x*z^5 + 95*z^6 + 1/2*y^5 - 1/4*y^4*z + x^2*y^2 + 3/2*x^2*y*z + 95*x*y*z^2 

sage: f.lift(I.gens()) 

[2*x + 95*z, 1/2*y^2 - 1/4*y*z, 0] 

sage: l = f.lift(J.gens()); l 

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1/2*y^2 + 1/4*y*z, 1/2*y^2*z^2 - 1/4*y*z^3 + 2*x + 95*z] 

sage: sum(map(mul, zip(l,J.gens()))) == f 

True 

 

Groebner bases over fraction fields of polynomial rings are also supported:: 

 

sage: P.<t> = QQ[] 

sage: F = Frac(P) 

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

sage: I = Ideal([f + P.random_element() for f in sage.rings.ideal.Katsura(R).gens()]) 

sage: I.groebner_basis() 

[Z^3 + (79/105*t^2 - 79/105*t + 79/630)*Z^2 + (-11/105*t^4 + 22/105*t^3 - 17/45*t^2 + 197/630*t + 557/1890)*Y + ..., 

Y^2 + (-3/5)*Z^2 + (2/5*t^2 - 2/5*t + 1/15)*Y + (-2/5*t^2 + 2/5*t - 1/15)*Z - 1/10*t^4 + 1/5*t^3 - 7/30*t^2 + 2/5*t + 11/90, 

Y*Z + 6/5*Z^2 + (1/5*t^2 - 1/5*t + 1/30)*Y + (4/5*t^2 - 4/5*t + 2/15)*Z + 1/5*t^4 - 2/5*t^3 + 7/15*t^2 - 3/10*t - 11/45, X + 2*Y + 2*Z + t^2 - t - 1/3] 

 

In cases where a characteristic cannot be determined, we use a toy implementation of Buchberger's algorithm 

(see :trac:`6581`):: 

 

sage: R.<a,b> = QQ[]; I = R.ideal(a^2+b^2-1) 

sage: Q = QuotientRing(R,I); K = Frac(Q) 

sage: R2.<x,y> = K[]; J = R2.ideal([(a^2+b^2)*x + y, x+y]) 

sage: J.groebner_basis() 

verbose 0 (...: multi_polynomial_ideal.py, groebner_basis) Warning: falling back to very slow toy implementation. 

[x + y] 

 

ALGORITHM: 

 

Uses Singular, Magma (if available), Macaulay2 (if available), 

Giac (if available), or a toy implementation. 

""" 

from sage.rings.finite_rings.integer_mod_ring import is_IntegerModRing 

from sage.rings.polynomial.multi_polynomial_sequence import PolynomialSequence 

 

if algorithm.lower() == "magma": 

algorithm = "magma:GroebnerBasis" 

elif algorithm.lower() == "singular": 

algorithm = "singular:groebner" 

elif algorithm.lower() == "libsingular": 

algorithm = "libsingular:groebner" 

elif algorithm.lower() == "macaulay2": 

algorithm = "macaulay2:gb" 

elif algorithm.lower() == "toy": 

algorithm = "toy:buchberger2" 

elif algorithm.lower() == "giac": 

algorithm = "giac:gbasis" 

 

if not algorithm: 

try: 

gb = self._groebner_basis_libsingular("groebner", deg_bound=deg_bound, mult_bound=mult_bound, *args, **kwds) 

except (TypeError, NameError) as msg: # conversion to Singular not supported 

try: 

gb = self._groebner_basis_singular("groebner", deg_bound=deg_bound, mult_bound=mult_bound, *args, **kwds) 

except (TypeError, NameError, NotImplementedError) as msg: # conversion to Singular not supported 

if self.ring().term_order().is_global() and is_IntegerModRing(self.ring().base_ring()) and not self.ring().base_ring().is_field(): 

verbose("Warning: falling back to very slow toy implementation.", level=0) 

 

ch = self.ring().base_ring().characteristic() 

R = self.ring().change_ring(ZZ) 

I = R.ideal([R(f) for f in self.gens()] + [R(ch)]) 

 

gb = toy_d_basis.d_basis(I, *args, **kwds) 

 

R = self.ring() 

gb = [r for r in (R(f) for f in gb) if r] 

else: 

if self.ring().term_order().is_global(): 

verbose("Warning: falling back to very slow toy implementation.", level=0) 

gb = toy_buchberger.buchberger_improved(self, *args, **kwds) 

else: 

raise TypeError("Local/unknown orderings not supported by 'toy_buchberger' implementation.") 

 

elif algorithm.startswith('singular:'): 

gb = self._groebner_basis_singular(algorithm[9:], deg_bound=deg_bound, mult_bound=mult_bound, prot=prot, *args, **kwds) 

elif algorithm.startswith('libsingular:'): 

if prot == "sage": 

warn("The libsingular interface does not support prot='sage', reverting to 'prot=True'.") 

gb = self._groebner_basis_libsingular(algorithm[len('libsingular:'):], deg_bound=deg_bound, mult_bound=mult_bound, prot=prot, *args, **kwds) 

elif algorithm == 'macaulay2:gb': 

gb = self._groebner_basis_macaulay2(*args, **kwds) 

elif algorithm == 'magma:GroebnerBasis': 

gb = self._groebner_basis_magma(prot=prot, deg_bound=deg_bound, *args, **kwds) 

elif algorithm == 'toy:buchberger': 

gb = toy_buchberger.buchberger(self, *args, **kwds) 

elif algorithm == 'toy:buchberger2': 

gb = toy_buchberger.buchberger_improved(self, *args, **kwds) 

elif algorithm == 'toy:d_basis': 

gb = toy_d_basis.d_basis(self, *args, **kwds) 

elif algorithm.startswith('ginv'): 

if algorithm == 'ginv': 

gb = self._groebner_basis_ginv(*args, **kwds) 

elif ":" in algorithm: 

ginv,alg = algorithm.split(":") 

gb = self._groebner_basis_ginv(algorithm=alg,*args, **kwds) 

else: 

raise NameError("Algorithm '%s' unknown."%algorithm) 

elif algorithm == 'giac:gbasis': 

from sage.libs.giac import groebner_basis as groebner_basis_libgiac 

gb = groebner_basis_libgiac(self, prot=prot, *args, **kwds) 

 

else: 

raise NameError("Algorithm '%s' unknown."%algorithm) 

 

gb = sorted(gb, reverse=True) 

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

_gb = [] 

for f in gb: 

if f.lc(): 

_gb.append(f*f.lc()**(-1)) 

else: 

_gb.append(f) 

gb = _gb 

elif self.ring().base_ring() is ZZ: 

if gb[-1].degree() == 0: 

gb = [f % gb[-1] for f in gb[:-1]] + [gb[-1]] 

 

gb = PolynomialSequence(self.ring(), gb, immutable=True) 

return gb 

 

def change_ring(self, P): 

r""" 

Return the ideal ``I`` in ``P`` spanned by 

the generators `g_1, ..., g_n` of self as returned by 

``self.gens()``. 

 

INPUT: 

 

 

- ``P`` - a multivariate polynomial ring 

 

 

EXAMPLES:: 

 

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

sage: I = sage.rings.ideal.Cyclic(P) 

sage: I 

Ideal (x + y + z, x*y + x*z + y*z, x*y*z - 1) of 

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

 

:: 

 

sage: I.groebner_basis() 

[x + y + z, y^2 + y*z + z^2, z^3 - 1] 

 

:: 

 

sage: Q.<x,y,z> = P.change_ring(order='degrevlex'); Q 

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

sage: Q.term_order() 

Degree reverse lexicographic term order 

 

:: 

 

sage: J = I.change_ring(Q); J 

Ideal (x + y + z, x*y + x*z + y*z, x*y*z - 1) of 

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

 

:: 

 

sage: J.groebner_basis() 

[z^3 - 1, y^2 + y*z + z^2, x + y + z] 

""" 

return P.ideal([P(f) for f in self.gens()]) 

 

def subs(self, in_dict=None, **kwds): 

""" 

Substitute variables. 

 

This method substitutes some variables in the polynomials that 

generate the ideal with given values. Variables that are not 

specified in the input remain unchanged. 

 

INPUT: 

 

- ``in_dict`` -- (optional) dictionary of inputs 

 

- ``**kwds`` -- named parameters 

 

OUTPUT: 

 

A new ideal with modified generators. If possible, in the same 

polynomial ring. Raises a ``TypeError`` if no common 

polynomial ring of the substituted generators can be found. 

 

EXAMPLES:: 

 

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

sage: I = R.ideal(x^5+y^5, x^2 + y + x^2*y^2 + 5); I 

Ideal (x^5 + y^5, x^2*y^2 + x^2 + y + 5) of Multivariate Polynomial Ring in x, y over Integer Ring 

sage: I.subs(x=y) 

Ideal (2*y^5, y^4 + y^2 + y + 5) of Multivariate Polynomial Ring in x, y over Integer Ring 

sage: I.subs({x:y}) # same substitution but with dictionary 

Ideal (2*y^5, y^4 + y^2 + y + 5) of Multivariate Polynomial Ring in x, y over Integer Ring 

 

The new ideal can be in a different ring:: 

 

sage: R.<a,b> = PolynomialRing(QQ,2) 

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

sage: I = R.ideal(a^2+b^2+a-b+2); I 

Ideal (a^2 + b^2 + a - b + 2) of Multivariate Polynomial Ring in a, b over Rational Field 

sage: I.subs(a=x, b=y) 

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

 

The resulting ring need not be a multivariate polynomial ring:: 

 

sage: T.<t> = PolynomialRing(QQ) 

sage: I.subs(a=t, b=t) 

Principal ideal (t^2 + 1) of Univariate Polynomial Ring in t over Rational Field 

sage: var("z") 

z 

sage: I.subs(a=z, b=z) 

Principal ideal (2*z^2 + 2) of Symbolic Ring 

 

Variables that are not substituted remain unchanged:: 

 

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

sage: I = R.ideal(x^2+y^2+x-y+2); I 

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

sage: I.subs(x=1) 

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

""" 

ring = self.ring() 

generators = [f.subs(in_dict, **kwds) for f in self.gens()] 

if not all(gen in ring for gen in generators): 

ring = Sequence(generators).universe() 

try: 

return ring.ideal(generators) 

except AttributeError: 

raise TypeError('Cannot construct an ideal from the substituted generators!') 

 

def reduce(self, f): 

""" 

Reduce an element modulo the reduced Groebner basis for this ideal. 

This returns 0 if and only if the element is in this ideal. In any 

case, this reduction is unique up to monomial orders. 

 

EXAMPLES:: 

 

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

sage: I = (x^3 + y, y)*R 

sage: I.reduce(y) 

0 

sage: I.reduce(x^3) 

0 

sage: I.reduce(x - y) 

x 

 

sage: I = (y^2 - (x^3 + x))*R 

sage: I.reduce(x^3) 

y^2 - x 

sage: I.reduce(x^6) 

y^4 - 2*x*y^2 + x^2 

sage: (y^2 - x)^2 

y^4 - 2*x*y^2 + x^2 

 

.. NOTE:: 

 

Requires computation of a Groebner basis, which can be a 

very expensive operation. 

""" 

try: 

strat = self._groebner_strategy() 

return strat.normal_form(f) 

except (TypeError, NotImplementedError, ValueError): 

pass 

 

gb = self.groebner_basis() 

return f.reduce(gb) 

 

def _contains_(self, f): 

r""" 

Returns ``True`` if ``f`` is in this ideal, 

``False`` otherwise. 

 

EXAMPLES:: 

 

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

sage: I = (x^3 + y, y)*R 

sage: x in I # indirect doctest 

False 

sage: y in I 

True 

sage: x^3 + 2*y in I 

True 

 

.. NOTE:: 

 

Requires computation of a Groebner basis, which can be a very 

expensive operation. 

""" 

g = f.reduce(self.groebner_basis()) 

return self.ring()(g).is_zero() 

 

def homogenize(self, var='h'): 

""" 

Return homogeneous ideal spanned by the homogeneous polynomials 

generated by homogenizing the generators of this ideal. 

 

INPUT: 

 

 

- ``h`` - variable name or variable in cover ring 

(default: 'h') 

 

 

EXAMPLES:: 

 

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

sage: I = Ideal([x^2*y + z + 1, x + y^2 + 1]); I 

Ideal (x^2*y + z + 1, y^2 + x + 1) of Multivariate 

Polynomial Ring in x, y, z over Finite Field of size 2 

 

:: 

 

sage: I.homogenize() 

Ideal (x^2*y + z*h^2 + h^3, y^2 + x*h + h^2) of 

Multivariate Polynomial Ring in x, y, z, h over Finite 

Field of size 2 

 

:: 

 

sage: I.homogenize(y) 

Ideal (x^2*y + y^3 + y^2*z, x*y) of Multivariate 

Polynomial Ring in x, y, z over Finite Field of size 2 

 

:: 

 

sage: I = Ideal([x^2*y + z^3 + y^2*x, x + y^2 + 1]) 

sage: I.homogenize() 

Ideal (x^2*y + x*y^2 + z^3, y^2 + x*h + h^2) of 

Multivariate Polynomial Ring in x, y, z, h over Finite 

Field of size 2 

""" 

I = [f.homogenize(var) for f in self.gens()] 

P = max(I, key=lambda x: x.parent().ngens()).parent() 

return P.ideal([P(f) for f in I]) 

 

def is_homogeneous(self): 

r""" 

Return ``True`` if this ideal is spanned by homogeneous 

polynomials, i.e. if it is a homogeneous ideal. 

 

EXAMPLES:: 

 

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

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

sage: I 

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: I.is_homogeneous() 

False 

 

:: 

 

sage: J = I.homogenize() 

sage: J 

Ideal (x + 2*y + 2*z - h, x^2 + 2*y^2 + 2*z^2 - x*h, 2*x*y 

+ 2*y*z - y*h) of Multivariate Polynomial Ring in x, y, z, 

h over Rational Field 

 

:: 

 

sage: J.is_homogeneous() 

True 

""" 

for f in self.gens(): 

if not f.is_homogeneous(): 

return False 

return True 

 

def degree_of_semi_regularity(self): 

r""" 

Return the degree of semi-regularity of this ideal under the 

assumption that it is semi-regular. 

 

Let `\{f_1, ... , f_m\} \subset K[x_1 , ... , x_n]` be 

homogeneous polynomials of degrees `d_1,... ,d_m` 

respectively. This sequence is semi-regular if: 

 

* `\{f_1, ... , f_m\} \neq K[x_1 , ... , x_n]` 

 

* for all `1 \leq i \leq m` and `g \in K[x_1,\dots,x_n]`: 

`deg(g \cdot pi ) < D` and 

`g \cdot f_i \in <f_1 , \dots , f_{i-1}>` implies that 

`g \in <f_1, ..., f_{i-1}>` where `D` is the degree of regularity. 

 

This notion can be extended to affine polynomials by 

considering their homogeneous components of highest degree. 

 

The degree of regularity of a semi-regular sequence 

`f_1, ...,f_m` of respective degrees `d_1,...,d_m` is given by the 

index of the first non-positive coefficient of: 

 

`\sum c_k z^k = \frac{\prod (1 - z^{d_i})}{(1-z)^n}` 

 

EXAMPLES: 

 

We consider a homogeneous example:: 

 

sage: n = 8 

sage: K = GF(127) 

sage: P = PolynomialRing(K,n,'x') 

sage: s = [K.random_element() for _ in range(n)] 

sage: L = [] 

sage: for i in range(2*n): 

....: f = P.random_element(degree=2, terms=binomial(n,2)) 

....: f -= f(*s) 

....: L.append(f.homogenize()) 

sage: I = Ideal(L) 

sage: I.degree_of_semi_regularity() 

4 

 

From this, we expect a Groebner basis computation to reach at 

most degree 4. For homogeneous systems this is equivalent to 

the largest degree in the Groebner basis:: 

 

sage: max(f.degree() for f in I.groebner_basis()) 

4 

 

We increase the number of polynomials and observe a decrease 

the degree of regularity:: 

 

sage: for i in range(2*n): 

....: f = P.random_element(degree=2, terms=binomial(n,2)) 

....: f -= f(*s) 

....: L.append(f.homogenize()) 

sage: I = Ideal(L) 

sage: I.degree_of_semi_regularity() 

3 

 

sage: max(f.degree() for f in I.groebner_basis()) 

3 

 

The degree of regularity approaches 2 for quadratic systems as 

the number of polynomials approaches `n^2`:: 

 

sage: for i in range((n-4)*n): 

....: f = P.random_element(degree=2, terms=binomial(n,2)) 

....: f -= f(*s) 

....: L.append(f.homogenize()) 

sage: I = Ideal(L) 

sage: I.degree_of_semi_regularity() 

2 

 

sage: max(f.degree() for f in I.groebner_basis()) 

2 

 

.. NOTE:: 

 

It is unknown whether semi-regular sequences 

exist. However, it is expected that random systems are 

semi-regular sequences. For more details about 

semi-regular sequences see [BFS04]_. 

 

REFERENCES: 

 

.. [BFS04] Magali Bardet, Jean-Charles Faugère, and Bruno 

Salvy, On the complexity of Groebner basis computation of 

semi-regular overdetermined algebraic equations. 

Proc. International Conference on Polynomial System Solving 

(ICPSS), pp. 71-75, 2004. 

 

""" 

degs = [f.degree() for f in self.gens() if f!=0] # we ignore zeroes 

m, n = self.ngens(), len(set(sum([f.variables() for f in self.gens()],()))) 

if m <= n: 

raise ValueError("This function requires an overdefined system of polynomials.") 

 

from sage.rings.all import QQ 

from sage.misc.misc_c import prod 

from sage.rings.power_series_ring import PowerSeriesRing 

 

R = PowerSeriesRing(QQ,'z', default_prec=sum(degs)) 

z = R.gen() 

dreg = 0 

s = prod([1-z**d for d in degs]) / (1-z)**n 

for dreg in range(sum(degs)): 

if s[dreg] < 0: 

return ZZ(dreg) 

else: 

raise ValueError("BUG: Could not compute the degree of semi-regularity") 

 

def plot(self, *args, **kwds): 

""" 

Plot the real zero locus of this principal ideal. 

 

INPUT: 

 

- ``self`` - a principal ideal in 2 variables 

 

- ``algorithm`` - set this to 'surf' if you want 'surf' to 

plot the ideal (default: None) 

 

- ``*args`` - optional tuples ``(variable, minimum, maximum)`` 

for plotting dimensions 

 

- ``**kwds`` - optional keyword arguments passed on to 

``implicit_plot`` 

 

EXAMPLES: 

 

Implicit plotting in 2-d:: 

 

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

sage: I = R.ideal([y^3 - x^2]) 

sage: I.plot() # cusp 

Graphics object consisting of 1 graphics primitive 

 

:: 

 

sage: I = R.ideal([y^2 - x^2 - 1]) 

sage: I.plot((x,-3, 3), (y, -2, 2)) # hyperbola 

Graphics object consisting of 1 graphics primitive 

 

:: 

 

sage: I = R.ideal([y^2 + x^2*(1/4) - 1]) 

sage: I.plot() # ellipse 

Graphics object consisting of 1 graphics primitive 

 

:: 

 

sage: I = R.ideal([y^2-(x^2-1)*(x-2)]) 

sage: I.plot() # elliptic curve 

Graphics object consisting of 1 graphics primitive 

 

:: 

 

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

sage: I = R.ideal(f) 

sage: I.plot() # the Singular logo 

Graphics object consisting of 1 graphics primitive 

 

This used to be :trac:`5267`:: 

 

sage: I = R.ideal([-x^2*y+1]) 

sage: I.plot() 

Graphics object consisting of 1 graphics primitive 

 

AUTHORS: 

 

- Martin Albrecht (2008-09) 

""" 

from sage.rings.rational_field import QQ 

from sage.rings.real_mpfr import RR 

from sage.plot.all import implicit_plot 

 

 

K = self.base_ring() 

try: 

RR._coerce_(K(1)) 

except TypeError: 

raise NotImplementedError("Plotting of curves over %s not implemented yet"%K) 

 

if not self.is_principal(): 

raise TypeError("Ideal must be principal.") 

 

 

f = self.gens()[0] 

 

variables = sorted(f.variables(), reverse=True) 

 

if len(variables) == 2 and kwds.get('algorithm','') != 'surf': 

V = [(variables[0], None, None), (variables[1], None, None)] 

 

if len(args) > 2: 

raise TypeError("Expected up to 2 optional parameters but got %d."%len(args)) 

 

# first check whether user supplied boundaries 

for e in args: 

if not isinstance(e, (tuple, list)) or len(e) != 3: 

raise TypeError("Optional parameter must be list or tuple or length 3.") 

v,mi,ma = e 

 

if v not in variables: 

raise TypeError("Optional parameter must contain variable of ideal generator.") 

 

vi = variables.index(v) 

V[vi] = v,mi,ma 

 

# now check whether we should find boundaries 

for var_index in range(2): 

if V[var_index][1] is None: 

v, mi, ma = variables[var_index], -10, 10 

for i in range(mi, ma): 

roots = f.subs({v:i}).univariate_polynomial().change_ring(RR).roots() 

if len(roots) > 0: 

mi = i - 1 

break 

 

for i in range(ma, mi, -1): 

roots = f.subs({v:i}).univariate_polynomial().change_ring(RR).roots() 

if len(roots) > 0: 

ma = i + 1 

break 

V[var_index] = variables[var_index], mi, ma 

 

kwds.setdefault("plot_points",200) 

kwds.pop('algorithm', '') 

return implicit_plot(f, V[0], V[1], **kwds) 

 

elif len(variables) == 3 or kwds.get('algorithm','') == 'surf': 

MPolynomialIdeal_singular_repr.plot(self, kwds.get("singular",singular_default)) 

else: 

raise TypeError("Ideal generator may not have either 2 or 3 variables.") 

 

def random_element(self, degree, compute_gb=False, *args, **kwds): 

""" 

Return a random element in this ideal as `r = \sum h_i·f_i`. 

 

INPUT: 

 

- ``compute_gb`` - if ``True`` then a Gröbner basis is computed first 

and `f_i` are the elements in the Gröbner basis. Otherwise whatever 

basis is returned by ``self.gens()`` is used. 

 

- ``*args`` and ``**kwds`` are passed to ``R.random_element()`` with 

``R = self.ring()``. 

 

EXAMPLES: 

 

We compute a uniformly random element up to the provided degree.:: 

 

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

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

sage: I.random_element(degree=4, compute_gb=True, terms=infinity) 

34*x^4 - 33*x^3*y + 45*x^2*y^2 - 51*x*y^3 - 55*y^4 + 43*x^3*z ... - 28*y - 33*z + 45 

 

Note that sampling uniformly at random from the ideal at some large enough degree is 

equivalent to computing a Gröbner basis. We give an example showing how to compute a Gröbner 

basis if we can sample uniformly at random from an ideal:: 

 

sage: n = 3; d = 4 

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

sage: I = sage.rings.ideal.Cyclic(P) 

 

1. We sample `n^d` uniformly random elements in the ideal:: 

 

sage: F = Sequence(I.random_element(degree=d, compute_gb=True, terms=infinity) for _ in range(n^d)) 

 

2. We linearize and compute the echelon form:: 

 

sage: A,v = F.coefficient_matrix() 

sage: A.echelonize() 

 

3. The result is the desired Gröbner basis:: 

 

sage: G = Sequence((A*v).list()) 

sage: G.is_groebner() 

True 

sage: Ideal(G) == I 

True 

 

We return some element in the ideal with no guarantee on the distribution:: 

 

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

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

sage: I.random_element(degree=3) 

-25*x0^2*x1 + 14*x1^3 + 57*x0*x1*x2 + ... + 19*x7*x9 + 40*x8*x9 + 49*x1 

 

We show that the default method does not sample uniformly at random from the ideal:: 

 

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

sage: G = Sequence([x+7, y-2, z+110]) 

sage: I = Ideal([sum(P.random_element() * g for g in G) for _ in range(4)]) 

sage: all(I.random_element(degree=1) == 0 for _ in range(100)) 

True 

 

If degree equals the degree of the generators a random linear 

combination of the generators is returned:: 

 

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

sage: I = P.ideal([x^2,y^2]) 

sage: I.random_element(degree=2) 

-x^2 

 

""" 

if compute_gb: 

gens = self.groebner_basis() 

else: 

gens = self.basis 

 

R = self.ring() 

 

r = R(0) 

 

for f in gens: 

d = degree - f.degree() 

if d >= 0: 

h = R.random_element(degree=d, *args, **kwds) 

r += h*f 

return r 

 

@require_field 

def weil_restriction(self): 

""" 

Compute the Weil restriction of this ideal over some extension 

field. If the field is a finite field, then this computes 

the Weil restriction to the prime subfield. 

 

A Weil restriction of scalars - denoted `Res_{L/k}` - is a 

functor which, for any finite extension of fields `L/k` and 

any algebraic variety `X` over `L`, produces another 

corresponding variety `Res_{L/k}(X)`, defined over `k`. It is 

useful for reducing questions about varieties over large 

fields to questions about more complicated varieties over 

smaller fields. 

 

This function does not compute this Weil restriction directly 

but computes on generating sets of polynomial ideals: 

 

Let `d` be the degree of the field extension `L/k`, let `a` a 

generator of `L/k` and `p` the minimal polynomial of 

`L/k`. Denote this ideal by `I`. 

 

Specifically, this function first maps each variable `x` to 

its representation over `k`: `\sum_{i=0}^{d-1} a^i x_i`. Then 

each generator of `I` is evaluated over these representations 

and reduced modulo the minimal polynomial `p`. The result is 

interpreted as a univariate polynomial in `a` and its 

coefficients are the new generators of the returned ideal. 

 

If the input and the output ideals are radical, this is 

equivalent to the statement about algebraic varieties above. 

 

OUTPUT: MPolynomial Ideal 

 

EXAMPLES:: 

 

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

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

sage: I = Ideal([x*y + 1, a*x + 1]) 

sage: I.variety() 

[{y: a, x: a + 1}] 

sage: J = I.weil_restriction() 

sage: J 

Ideal (x0*y0 + x1*y1 + 1, x1*y0 + x0*y1 + x1*y1, x1 + 1, x0 + x1) of 

Multivariate Polynomial Ring in x0, x1, y0, y1 over Finite Field of size 

2 

sage: J += sage.rings.ideal.FieldIdeal(J.ring()) # ensure radical ideal 

sage: J.variety() 

[{y1: 1, x1: 1, x0: 1, y0: 0}] 

 

sage: J.weil_restriction() # returns J 

Ideal (x0*y0 + x1*y1 + 1, x1*y0 + x0*y1 + x1*y1, x1 + 1, x0 + x1, x0^2 + 

x0, x1^2 + x1, y0^2 + y0, y1^2 + y1) of Multivariate Polynomial Ring in 

x0, x1, y0, y1 over Finite Field of size 2 

 

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

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

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

sage: I.dimension() 

0 

sage: I.variety() 

[{y: 0, z: 0, x: 1}] 

 

sage: J = I.weil_restriction(); J 

Ideal (x0 - y0 - z0 - 1, x1 - y1 - z1, x2 - y2 - z2, x3 - y3 - z3, x4 - 

y4 - z4, x0^2 + x2*x3 + x1*x4 - y0^2 - y2*y3 - y1*y4 - z0^2 - z2*z3 - 

z1*z4 - x0, -x0*x1 - x2*x3 - x3^2 - x1*x4 + x2*x4 + y0*y1 + y2*y3 + y3^2 

+ y1*y4 - y2*y4 + z0*z1 + z2*z3 + z3^2 + z1*z4 - z2*z4 - x1, x1^2 - 

x0*x2 + x3^2 - x2*x4 + x3*x4 - y1^2 + y0*y2 - y3^2 + y2*y4 - y3*y4 - 

z1^2 + z0*z2 - z3^2 + z2*z4 - z3*z4 - x2, -x1*x2 - x0*x3 - x3*x4 - x4^2 

+ y1*y2 + y0*y3 + y3*y4 + y4^2 + z1*z2 + z0*z3 + z3*z4 + z4^2 - x3, x2^2 

- x1*x3 - x0*x4 + x4^2 - y2^2 + y1*y3 + y0*y4 - y4^2 - z2^2 + z1*z3 + 

z0*z4 - z4^2 - x4, -x0*y0 + x4*y1 + x3*y2 + x2*y3 + x1*y4 - y0*z0 + 

y4*z1 + y3*z2 + y2*z3 + y1*z4 - y0, -x1*y0 - x0*y1 - x4*y1 - x3*y2 + 

x4*y2 - x2*y3 + x3*y3 - x1*y4 + x2*y4 - y1*z0 - y0*z1 - y4*z1 - y3*z2 + 

y4*z2 - y2*z3 + y3*z3 - y1*z4 + y2*z4 - y1, -x2*y0 - x1*y1 - x0*y2 - 

x4*y2 - x3*y3 + x4*y3 - x2*y4 + x3*y4 - y2*z0 - y1*z1 - y0*z2 - y4*z2 - 

y3*z3 + y4*z3 - y2*z4 + y3*z4 - y2, -x3*y0 - x2*y1 - x1*y2 - x0*y3 - 

x4*y3 - x3*y4 + x4*y4 - y3*z0 - y2*z1 - y1*z2 - y0*z3 - y4*z3 - y3*z4 + 

y4*z4 - y3, -x4*y0 - x3*y1 - x2*y2 - x1*y3 - x0*y4 - x4*y4 - y4*z0 - 

y3*z1 - y2*z2 - y1*z3 - y0*z4 - y4*z4 - y4) of Multivariate Polynomial 

Ring in x0, x1, x2, x3, x4, y0, y1, y2, y3, y4, z0, z1, z2, z3, z4 over 

Finite Field of size 3 

sage: J += sage.rings.ideal.FieldIdeal(J.ring()) # ensure radical ideal 

sage: from sage.doctest.fixtures import reproducible_repr 

sage: print(reproducible_repr(J.variety())) 

[{x0: 1, x1: 0, x2: 0, x3: 0, x4: 0, y0: 0, y1: 0, y2: 0, y3: 0, y4: 0, z0: 0, z1: 0, z2: 0, z3: 0, z4: 0}] 

 

 

Weil restrictions are often used to study elliptic curves over 

extension fields so we give a simple example involving those:: 

 

sage: K.<a> = QuadraticField(1/3) 

sage: E = EllipticCurve(K,[1,2,3,4,5]) 

 

We pick a point on ``E``:: 

 

sage: p = E.lift_x(1); p 

(1 : 2 : 1) 

 

sage: I = E.defining_ideal(); I 

Ideal (-x^3 - 2*x^2*z + x*y*z + y^2*z - 4*x*z^2 + 3*y*z^2 - 5*z^3) 

of Multivariate Polynomial Ring in x, y, z over Number Field in a with defining polynomial x^2 - 1/3 

 

Of course, the point ``p`` is a root of all generators of ``I``:: 

 

sage: I.subs(x=1,y=2,z=1) 

Ideal (0) of Multivariate Polynomial Ring in x, y, z over 

Number Field in a with defining polynomial x^2 - 1/3 

 

``I`` is also radical:: 

 

sage: I.radical() == I 

True 

 

So we compute its Weil restriction:: 

 

sage: J = I.weil_restriction() 

sage: J 

Ideal (-x0^3 - x0*x1^2 - 2*x0^2*z0 - 2/3*x1^2*z0 + x0*y0*z0 + y0^2*z0 + 

1/3*x1*y1*z0 + 1/3*y1^2*z0 - 4*x0*z0^2 + 3*y0*z0^2 - 5*z0^3 - 

4/3*x0*x1*z1 + 1/3*x1*y0*z1 + 1/3*x0*y1*z1 + 2/3*y0*y1*z1 - 8/3*x1*z0*z1 

+ 2*y1*z0*z1 - 4/3*x0*z1^2 + y0*z1^2 - 5*z0*z1^2, -3*x0^2*x1 - 1/3*x1^3 

- 4*x0*x1*z0 + x1*y0*z0 + x0*y1*z0 + 2*y0*y1*z0 - 4*x1*z0^2 + 3*y1*z0^2 

- 2*x0^2*z1 - 2/3*x1^2*z1 + x0*y0*z1 + y0^2*z1 + 1/3*x1*y1*z1 + 

1/3*y1^2*z1 - 8*x0*z0*z1 + 6*y0*z0*z1 - 15*z0^2*z1 - 4/3*x1*z1^2 + 

y1*z1^2 - 5/3*z1^3) of Multivariate Polynomial Ring in x0, x1, y0, y1, 

z0, z1 over Rational Field 

 

We can check that the point ``p`` is still a root of all generators of ``J``:: 

 

sage: J.subs(x0=1,y0=2,z0=1,x1=0,y1=0,z1=0) 

Ideal (0, 0) of Multivariate Polynomial Ring in x0, x1, y0, y1, z0, z1 over Rational Field 

 

Example for relative number fields:: 

 

sage: R.<x> = QQ[] 

sage: K.<w> = NumberField(x^5-2) 

sage: R.<x> = K[] 

sage: L.<v> = K.extension(x^2+1) 

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

sage: I = S.ideal([y^2-x^3-1]) 

sage: I.weil_restriction() 

Ideal (-x0^3 + 3*x0*x1^2 + y0^2 - y1^2 - 1, -3*x0^2*x1 + x1^3 + 2*y0*y1) 

of Multivariate Polynomial Ring in x0, x1, y0, y1 over Number Field in w 

with defining polynomial x^5 - 2 

 

.. NOTE:: 

 

Based on a Singular implementation by Michael Brickenstein 

""" 

from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing 

 

R = self.ring() 

nvars = R.ngens() 

L = R.base_ring() 

if L.is_finite(): 

k = L.prime_subfield() 

d = L.degree() 

poly = L.polynomial() 

else: 

k = L.base_field() 

d = L.relative_degree() 

poly = L.relative_polynomial() 

 

if d == 1: 

return self 

 

helper = PolynomialRing(k, nvars + 1, (L.variable_name(),) + R.variable_names(), order='lex') 

myminpoly = poly.subs(helper.gen(0)) 

 

l = [helper(str(f)) for f in self.gens()] 

r = myminpoly.degree() 

intermediate_ring = PolynomialRing(k, nvars*r+1, 'x') 

a = intermediate_ring.gen(0) 

 

# map e.g. x -> a^2*x_2 + a*x_1 + x_0, where x_0,..,x_2 

# represent the components of x if viewed as a vector in k^r 

map_ideal = [a] 

 

variables = iter(intermediate_ring.gens()[1:]) 

for _ in range(nvars): 

map_ideal.append(sum([a**i * next(variables) for i in range(r)])) 

 

myminpoly = myminpoly(*map_ideal) 

l = [f(*map_ideal).reduce([myminpoly]) for f in l] 

 

result = [] 

# split e.g. a^2*x2+a*x1+x0 to x0,x1,x2 

for f in l: 

t = [] 

for i in reversed(range(r)): 

g = f.coefficient(a**i) 

f = f - a**i * g 

t.append(g) 

result += reversed(t) 

 

# eliminate parameter 

new_var_names = [str(var) + "%d"%i for var in R.gens() for i in range(r)] 

 

result_ring = PolynomialRing(k, nvars*r, new_var_names) 

 

map_ideal = (0,) + result_ring.gens() 

result = [f(*map_ideal) for f in result] 

 

return result_ring.ideal(result)