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

r""" 

Ideals of commutative rings 

 

Sage provides functionality for computing with ideals. One can create 

an ideal in any commutative or non-commutative ring `R` by giving a 

list of generators, using the notation ``R.ideal([a,b,...])``. The case 

of non-commutative rings is implemented in 

:mod:`~sage.rings.noncommutative_ideals`. 

 

A more convenient notation may be ``R*[a,b,...]`` or ``[a,b,...]*R``. 

If `R` is non-commutative, the former creates a left and the latter 

a right ideal, and ``R*[a,b,...]*R`` creates a two-sided ideal. 

""" 

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

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

# 

# 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 absolute_import 

 

from types import GeneratorType 

 

import sage.misc.latex as latex 

import sage.rings.ring 

from sage.structure.element import MonoidElement 

from sage.structure.richcmp import rich_to_bool, richcmp 

from sage.interfaces.singular import singular as singular_default 

import sage.rings.infinity 

from sage.structure.sequence import Sequence 

 

def Ideal(*args, **kwds): 

r""" 

Create the ideal in ring with given generators. 

 

There are some shorthand notations for creating an ideal, in 

addition to using the :func:`Ideal` function: 

 

- ``R.ideal(gens, coerce=True)`` 

- ``gens*R`` 

- ``R*gens`` 

 

INPUT: 

 

- ``R`` - A ring (optional; if not given, will try to infer it from 

``gens``) 

 

- ``gens`` - list of elements generating the ideal 

 

- ``coerce`` - bool (optional, default: ``True``); 

whether ``gens`` need to be coerced into the ring. 

 

 

OUTPUT: The ideal of ring generated by ``gens``. 

 

EXAMPLES:: 

 

sage: R.<x> = ZZ[] 

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

sage: I 

Ideal (x^2 + 3*x + 4, x^2 + 1) of Univariate Polynomial Ring in x over Integer Ring 

sage: Ideal(R, [4 + 3*x + x^2, 1 + x^2]) 

Ideal (x^2 + 3*x + 4, x^2 + 1) of Univariate Polynomial Ring in x over Integer Ring 

sage: Ideal((4 + 3*x + x^2, 1 + x^2)) 

Ideal (x^2 + 3*x + 4, x^2 + 1) of Univariate Polynomial Ring in x over Integer Ring 

 

:: 

 

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

Ideal (x^2 - 2*x + 1, x^2 - 1) of Univariate Polynomial Ring in x over Integer Ring 

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

Ideal (x^2 - 2*x + 1, x^2 - 1) of Univariate Polynomial Ring in x over Integer Ring 

sage: l = [x^2-2*x+1, x^2-1] 

sage: ideal(f^2 for f in l) 

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

Univariate Polynomial Ring in x over Integer Ring 

 

This example illustrates how Sage finds a common ambient ring for 

the ideal, even though 1 is in the integers (in this case). 

 

:: 

 

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

sage: i = ideal(1,t,t^2) 

sage: i 

Ideal (1, t, t^2) of Univariate Polynomial Ring in t over Integer Ring 

sage: ideal(1/2,t,t^2) 

Principal ideal (1) of Univariate Polynomial Ring in t over Rational Field 

 

This shows that the issues at :trac:`1104` are resolved:: 

 

sage: Ideal(3, 5) 

Principal ideal (1) of Integer Ring 

sage: Ideal(ZZ, 3, 5) 

Principal ideal (1) of Integer Ring 

sage: Ideal(2, 4, 6) 

Principal ideal (2) of Integer Ring 

 

You have to provide enough information that Sage can figure out 

which ring to put the ideal in. 

 

:: 

 

sage: I = Ideal([]) 

Traceback (most recent call last): 

... 

ValueError: unable to determine which ring to embed the ideal in 

 

sage: I = Ideal() 

Traceback (most recent call last): 

... 

ValueError: need at least one argument 

 

Note that some rings use different ideal implementations than the standard, 

even if they are PIDs.:: 

 

sage: R.<x> = GF(5)[] 

sage: I = R*(x^2+3) 

sage: type(I) 

<class 'sage.rings.polynomial.ideal.Ideal_1poly_field'> 

 

You can also pass in a specific ideal type:: 

 

sage: from sage.rings.ideal import Ideal_pid 

sage: I = Ideal(x^2+3,ideal_class=Ideal_pid) 

sage: type(I) 

<class 'sage.rings.ideal.Ideal_pid'> 

 

TESTS:: 

 

sage: R.<x> = ZZ[] 

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

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

True 

 

:: 

 

sage: I = Ideal(R, [4 + 3*x + x^2, 1 + x^2]) 

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

True 

 

:: 

 

sage: I = Ideal((4 + 3*x + x^2, 1 + x^2)) 

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

True 

 

This shows that the issue at :trac:`5477` is fixed:: 

 

sage: R.<x> = QQ[] 

sage: I = R.ideal([x + x^2]) 

sage: J = R.ideal([2*x + 2*x^2]) 

sage: J 

Principal ideal (x^2 + x) of Univariate Polynomial Ring in x over Rational Field 

sage: S = R.quotient_ring(I) 

sage: U = R.quotient_ring(J) 

sage: I == J 

True 

sage: S == U 

True 

""" 

if len(args) == 0: 

raise ValueError("need at least one argument") 

if len(args) == 1 and args[0] == []: 

raise ValueError("unable to determine which ring to embed the ideal in") 

 

first = args[0] 

 

if not isinstance(first, sage.rings.ring.Ring): 

if isinstance(first, Ideal_generic) and len(args) == 1: 

R = first.ring() 

gens = first.gens() 

else: 

if isinstance(first, (list, tuple, GeneratorType)) and len(args) == 1: 

gens = first 

else: 

gens = args 

gens = Sequence(gens) 

R = gens.universe() 

else: 

R = first 

gens = args[1:] 

 

if not isinstance(R, sage.rings.ring.CommutativeRing): 

raise TypeError("R must be a commutative ring") 

 

return R.ideal(*gens, **kwds) 

 

def is_Ideal(x): 

r""" 

Return ``True`` if object is an ideal of a ring. 

 

EXAMPLES: 

 

A simple example involving the ring of integers. Note 

that Sage does not interpret rings objects themselves as ideals. 

However, one can still explicitly construct these ideals:: 

 

sage: from sage.rings.ideal import is_Ideal 

sage: R = ZZ 

sage: is_Ideal(R) 

False 

sage: 1*R; is_Ideal(1*R) 

Principal ideal (1) of Integer Ring 

True 

sage: 0*R; is_Ideal(0*R) 

Principal ideal (0) of Integer Ring 

True 

 

Sage recognizes ideals of polynomial rings as well:: 

 

sage: R = PolynomialRing(QQ, 'x'); x = R.gen() 

sage: I = R.ideal(x^2 + 1); I 

Principal ideal (x^2 + 1) of Univariate Polynomial Ring in x over Rational Field 

sage: is_Ideal(I) 

True 

sage: is_Ideal((x^2 + 1)*R) 

True 

""" 

return isinstance(x, Ideal_generic) 

 

 

class Ideal_generic(MonoidElement): 

""" 

An ideal. 

 

See :func:`Ideal()`. 

""" 

def __init__(self, ring, gens, coerce=True): 

""" 

Initialize this ideal. 

 

INPUT: 

 

- ``ring`` -- A ring 

 

- ``gens`` -- The generators for this ideal 

 

- ``coerce`` -- (default: ``True``) If ``gens`` needs to be coerced 

into ``ring``. 

 

EXAMPLES:: 

 

sage: R.<x> = ZZ[] 

sage: R.ideal([4 + 3*x + x^2, 1 + x^2]) 

Ideal (x^2 + 3*x + 4, x^2 + 1) of Univariate Polynomial Ring in x over Integer Ring 

""" 

self.__ring = ring 

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

gens = [gens] 

if coerce: 

gens = [ring(x) for x in gens] 

 

gens = tuple(gens) 

if len(gens)==0: gens=(ring.zero(),) 

self.__gens = gens 

MonoidElement.__init__(self, ring.ideal_monoid()) 

 

def _repr_short(self): 

""" 

Represent the list of generators. 

 

EXAMPLES:: 

 

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

sage: P*[a^2,a*b+c,c^3] 

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

sage: (P*[a^2,a*b+c,c^3])._repr_short() 

'(a^2, a*b + c, c^3)' 

 

If the string representation of a generator contains a line break, 

the generators are not represented from left to right but from 

top to bottom. This is the case, e.g., for matrices:: 

 

sage: MS = MatrixSpace(QQ,2,2) 

sage: MS*[MS.1,2] 

Left Ideal 

( 

[0 1] 

[0 0], 

<BLANKLINE> 

[2 0] 

[0 2] 

) 

of Full MatrixSpace of 2 by 2 dense matrices over Rational Field 

 

 

""" 

L = [] 

has_return = False 

for x in self.gens(): 

s = repr(x) 

if '\n' in s: 

has_return = True 

s = s.replace('\n','\n ') 

L.append(s) 

if has_return: 

return '\n(\n %s\n)\n'%(',\n\n '.join(L)) 

return '(%s)'%(', '.join(L)) 

 

def __repr__(self): 

""" 

Return a string representation of ``self``. 

 

EXAMPLES:: 

 

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

sage: P*[a^2,a*b+c,c^3] # indirect doctest 

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

""" 

return "Ideal %s of %s"%(self._repr_short(), self.ring()) 

 

def _richcmp_(self, other, op): 

""" 

Compares the generators of two ideals. 

 

INPUT: 

 

- ``other`` -- an ideal 

 

OUTPUT: 

 

boolean 

 

EXAMPLES:: 

 

sage: R = ZZ; I = ZZ*2; J = ZZ*(-2) 

sage: I == J 

True 

""" 

S = set(self.gens()) 

T = set(other.gens()) 

if S == T: 

return rich_to_bool(op, 0) 

return richcmp(self.gens(), other.gens(), op) 

 

def __contains__(self, x): 

""" 

Check if ``x`` is in ``self``. 

 

EXAMPLES:: 

 

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

sage: I = P*[a, b] 

sage: a + b in I 

True 

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

sage: x + 2*y + w*z in I 

False 

""" 

try: 

return self._contains_(self.__ring(x)) 

except TypeError: 

return False 

 

def _contains_(self, x): 

""" 

Check if ``x``, which is assumed to be in the ambient ring, is in 

this ideal. 

 

.. TODO:: 

 

Implement this method. 

 

EXAMPLES:: 

 

sage: P.<a> = ZZ[] 

sage: I = P*[a] 

sage: I._contains_(a) 

Traceback (most recent call last): 

... 

NotImplementedError 

 

Note that calling ``in`` does not call this method:: 

 

sage: a in I 

True 

""" 

raise NotImplementedError 

 

def __bool__(self): 

r""" 

Return ``True`` if this ideal is not `(0)`. 

 

TESTS:: 

 

sage: I = ZZ.ideal(5) 

sage: bool(I) 

True 

 

:: 

 

sage: I = ZZ['x'].ideal(0) 

sage: bool(I) 

False 

 

:: 

 

sage: I = ZZ['x'].ideal(ZZ['x'].gen()^2) 

sage: bool(I) 

True 

 

:: 

 

sage: I = QQ['x', 'y'].ideal(0) 

sage: bool(I) 

False 

""" 

for g in self.gens(): 

if not g.is_zero(): 

return True 

return False 

 

__nonzero__ = __bool__ 

 

def base_ring(self): 

r""" 

Returns the base ring of this ideal. 

 

EXAMPLES:: 

 

sage: R = ZZ 

sage: I = 3*R; I 

Principal ideal (3) of Integer Ring 

sage: J = 2*I; J 

Principal ideal (6) of Integer Ring 

sage: I.base_ring(); J.base_ring() 

Integer Ring 

Integer Ring 

 

We construct an example of an ideal of a quotient ring:: 

 

sage: R = PolynomialRing(QQ, 'x'); x = R.gen() 

sage: I = R.ideal(x^2 - 2) 

sage: I.base_ring() 

Rational Field 

 

And `p`-adic numbers:: 

 

sage: R = Zp(7, prec=10); R 

7-adic Ring with capped relative precision 10 

sage: I = 7*R; I 

Principal ideal (7 + O(7^11)) of 7-adic Ring with capped relative precision 10 

sage: I.base_ring() 

7-adic Ring with capped relative precision 10 

""" 

return self.ring().base_ring() 

 

def apply_morphism(self, phi): 

r""" 

Apply the morphism ``phi`` to every element of this ideal. 

Returns an ideal in the domain of ``phi``. 

 

EXAMPLES:: 

 

sage: psi = CC['x'].hom([-CC['x'].0]) 

sage: J = ideal([CC['x'].0 + 1]); J 

Principal ideal (x + 1.00000000000000) of Univariate Polynomial Ring in x over Complex Field with 53 bits of precision 

sage: psi(J) 

Principal ideal (x - 1.00000000000000) of Univariate Polynomial Ring in x over Complex Field with 53 bits of precision 

sage: J.apply_morphism(psi) 

Principal ideal (x - 1.00000000000000) of Univariate Polynomial Ring in x over Complex Field with 53 bits of precision 

 

:: 

 

sage: psi = ZZ['x'].hom([-ZZ['x'].0]) 

sage: J = ideal([ZZ['x'].0, 2]); J 

Ideal (x, 2) of Univariate Polynomial Ring in x over Integer Ring 

sage: psi(J) 

Ideal (-x, 2) of Univariate Polynomial Ring in x over Integer Ring 

sage: J.apply_morphism(psi) 

Ideal (-x, 2) of Univariate Polynomial Ring in x over Integer Ring 

 

TESTS:: 

 

sage: K.<a> = NumberField(x^2 + 1) 

sage: A = K.ideal(a) 

sage: taus = K.embeddings(K) 

sage: A.apply_morphism(taus[0]) # identity 

Fractional ideal (a) 

sage: A.apply_morphism(taus[1]) # complex conjugation 

Fractional ideal (-a) 

sage: A.apply_morphism(taus[0]) == A.apply_morphism(taus[1]) 

True 

 

:: 

 

sage: K.<a> = NumberField(x^2 + 5) 

sage: B = K.ideal([2, a + 1]); B 

Fractional ideal (2, a + 1) 

sage: taus = K.embeddings(K) 

sage: B.apply_morphism(taus[0]) # identity 

Fractional ideal (2, a + 1) 

 

Since 2 is totally ramified, complex conjugation fixes it:: 

 

sage: B.apply_morphism(taus[1]) # complex conjugation 

Fractional ideal (2, a + 1) 

sage: taus[1](B) 

Fractional ideal (2, a + 1) 

""" 

from sage.categories.morphism import is_Morphism 

if not is_Morphism(phi): 

raise TypeError("phi must be a morphism") 

# delegate: morphisms know how to apply themselves to ideals 

return phi(self) 

 

def _latex_(self): 

r""" 

Return a latex representation of ``self``. 

 

EXAMPLES:: 

 

sage: latex(3*ZZ) # indirect doctest 

\left(3\right)\Bold{Z} 

""" 

return '\\left(%s\\right)%s'%(", ".join([latex.latex(g) for g in \ 

self.gens()]), 

latex.latex(self.ring())) 

 

def ring(self): 

""" 

Returns the ring containing this ideal. 

 

EXAMPLES:: 

 

sage: R = ZZ 

sage: I = 3*R; I 

Principal ideal (3) of Integer Ring 

sage: J = 2*I; J 

Principal ideal (6) of Integer Ring 

sage: I.ring(); J.ring() 

Integer Ring 

Integer Ring 

 

Note that ``self.ring()`` is different from 

``self.base_ring()`` 

 

:: 

 

sage: R = PolynomialRing(QQ, 'x'); x = R.gen() 

sage: I = R.ideal(x^2 - 2) 

sage: I.base_ring() 

Rational Field 

sage: I.ring() 

Univariate Polynomial Ring in x over Rational Field 

 

Another example using polynomial rings:: 

 

sage: R = PolynomialRing(QQ, 'x'); x = R.gen() 

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

sage: I.ring() 

Univariate Polynomial Ring in x over Rational Field 

sage: Rbar = R.quotient(I, names='a') 

sage: S = PolynomialRing(Rbar, 'y'); y = Rbar.gen(); S 

Univariate Polynomial Ring in y over Univariate Quotient Polynomial Ring in a over Rational Field with modulus x^2 - 3 

sage: J = S.ideal(y^2 + 1) 

sage: J.ring() 

Univariate Polynomial Ring in y over Univariate Quotient Polynomial Ring in a over Rational Field with modulus x^2 - 3 

""" 

return self.__ring 

 

def reduce(self, f): 

r""" 

Return the reduction of the element of `f` modulo ``self``. 

 

This is an element of `R` that is equivalent modulo `I` to `f` where 

`I` is ``self``. 

 

EXAMPLES:: 

 

sage: ZZ.ideal(5).reduce(17) 

2 

sage: parent(ZZ.ideal(5).reduce(17)) 

Integer Ring 

""" 

return f # default 

 

def gens(self): 

""" 

Return a set of generators / a basis of ``self``. 

 

This is the set of generators provided during creation of this ideal. 

 

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] 

 

:: 

 

sage: ZZ.ideal(5,10).gens() 

(5,) 

""" 

return self.__gens 

 

def gen(self, i): 

""" 

Return the ``i``-th generator in the current basis of this ideal. 

 

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.gen(1) 

y + 1 

 

sage: ZZ.ideal(5,10).gen() 

5 

""" 

return self.__gens[i] 

 

def ngens(self): 

""" 

Return the number of generators in the basis. 

 

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.ngens() 

2 

 

sage: ZZ.ideal(5,10).ngens() 

1 

""" 

return len(self.__gens) 

 

def gens_reduced(self): 

r""" 

Same as :meth:`gens()` for this ideal, since there is currently no 

special ``gens_reduced`` algorithm implemented for this ring. 

 

This method is provided so that ideals in `\ZZ` have the method 

``gens_reduced()``, just like ideals of number fields. 

 

EXAMPLES:: 

 

sage: ZZ.ideal(5).gens_reduced() 

(5,) 

""" 

return self.gens() 

 

def is_maximal(self): 

r""" 

Return ``True`` if the ideal is maximal in the ring containing the 

ideal. 

 

.. TODO:: 

 

This is not implemented for many rings. Implement it! 

 

EXAMPLES:: 

 

sage: R = ZZ 

sage: I = R.ideal(7) 

sage: I.is_maximal() 

True 

sage: R.ideal(16).is_maximal() 

False 

sage: S = Integers(8) 

sage: S.ideal(0).is_maximal() 

False 

sage: S.ideal(2).is_maximal() 

True 

sage: S.ideal(4).is_maximal() 

False 

""" 

from sage.rings.all import ZZ 

R = self.ring() 

if hasattr(R, 'cover_ring') and R.cover_ring() is ZZ: 

# The following test only works for quotients of Z/nZ: for 

# many other rings in Sage, testing whether R/I is a field 

# is done by testing whether I is maximal, so this would 

# result in a loop. 

return R.quotient(self).is_field() 

kd = R.krull_dimension() 

if kd == 0 or (kd == 1 and R.is_integral_domain()): 

# For rings of Krull dimension 0, or for integral domains of 

# Krull dimension 1, every nontrivial prime ideal is maximal. 

return self.is_prime() 

else: 

raise NotImplementedError 

 

def is_primary(self, P=None): 

r""" 

Returns ``True`` if this ideal is primary (or `P`-primary, if 

a prime ideal `P` is specified). 

 

Recall that an ideal `I` is primary if and only if `I` has a 

unique associated prime (see page 52 in [AtiMac]_). If this 

prime is `P`, then `I` is said to be `P`-primary. 

 

INPUT: 

 

- ``P`` - (default: ``None``) a prime ideal in the same ring 

 

EXAMPLES:: 

 

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

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

sage: I.is_primary() 

False 

sage: J = I.primary_decomposition()[1]; J 

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

sage: J.is_primary() 

True 

sage: J.is_prime() 

False 

 

Some examples from the Macaulay2 documentation:: 

 

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

sage: I = R.ideal([y^6]) 

sage: I.is_primary() 

True 

sage: I.is_primary(R.ideal([y])) 

True 

sage: I = R.ideal([x^4, y^7]) 

sage: I.is_primary() 

True 

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

sage: I.is_primary() 

False 

 

.. NOTE:: 

 

This uses the list of associated primes. 

 

REFERENCES: 

 

.. [AtiMac] Atiyah and Macdonald, "Introduction to commutative 

algebra", Addison-Wesley, 1969. 

 

""" 

try: 

ass = self.associated_primes() 

except (NotImplementedError, ValueError): 

raise NotImplementedError 

if P is None: 

return (len(ass) == 1) 

else: 

return (len(ass) == 1) and (ass[0] == P) 

 

 

def primary_decomposition(self): 

r""" 

Return a decomposition of this ideal into primary ideals. 

 

EXAMPLES:: 

 

sage: R = ZZ['x'] 

sage: I = R.ideal(7) 

sage: I.primary_decomposition() 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

raise NotImplementedError 

 

def is_prime(self): 

r""" 

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

 

EXAMPLES:: 

 

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

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

sage: I.is_prime() # a maximal ideal 

True 

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

sage: I.is_prime() # a non-maximal prime ideal 

True 

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

sage: I.is_prime() # a non-prime primary ideal 

False 

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

sage: I.is_prime() # a non-prime non-primary ideal 

False 

 

sage: S = Integers(8) 

sage: S.ideal(0).is_prime() 

False 

sage: S.ideal(2).is_prime() 

True 

sage: S.ideal(4).is_prime() 

False 

 

Note that this method is not implemented for all rings where it 

could be:: 

 

sage: R.<x> = ZZ[] 

sage: I = R.ideal(7) 

sage: I.is_prime() # when implemented, should be True 

Traceback (most recent call last): 

... 

NotImplementedError 

 

.. NOTE:: 

 

For general rings, uses the list of associated primes. 

""" 

from sage.rings.all import ZZ 

R = self.ring() 

if hasattr(R, 'cover_ring') and R.cover_ring() is ZZ and R.is_finite(): 

# For quotient rings of ZZ, prime is the same as maximal. 

return self.is_maximal() 

try: 

ass = self.associated_primes() 

except (NotImplementedError, ValueError): 

raise NotImplementedError 

if len(ass) != 1: 

return False 

if self == ass[0]: 

return True 

else: 

return False 

 

def associated_primes(self): 

r""" 

Return the list of associated prime ideals of this ideal. 

 

EXAMPLES:: 

 

sage: R = ZZ['x'] 

sage: I = R.ideal(7) 

sage: I.associated_primes() 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

raise NotImplementedError 

 

def minimal_associated_primes(self): 

r""" 

Return the list of minimal associated prime ideals of this ideal. 

 

EXAMPLES:: 

 

sage: R = ZZ['x'] 

sage: I = R.ideal(7) 

sage: I.minimal_associated_primes() 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

raise NotImplementedError 

 

def embedded_primes(self): 

r""" 

Return the list of embedded primes of this ideal. 

 

EXAMPLES:: 

 

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

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

sage: I.embedded_primes() 

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

""" 

# by definition, embedded primes are associated primes that 

# are not minimal (under inclusion) 

ass = self.associated_primes() 

min_ass = self.minimal_associated_primes() 

emb = [] 

for p in ass: 

try: 

i = min_ass.index(p) 

except ValueError: 

emb.append(p) 

emb.sort() 

return emb 

 

def is_principal(self): 

r""" 

Returns ``True`` if the ideal is principal in the ring containing the 

ideal. 

 

.. TODO:: 

 

Code is naive. Only keeps track of ideal generators as set 

during initialization of the ideal. (Can the base ring change? See 

example below.) 

 

EXAMPLES:: 

 

sage: R = ZZ['x'] 

sage: I = R.ideal(2,x) 

sage: I.is_principal() 

Traceback (most recent call last): 

... 

NotImplementedError 

sage: J = R.base_extend(QQ).ideal(2,x) 

sage: J.is_principal() 

True 

""" 

if len(self.gens()) <= 1: 

return True 

raise NotImplementedError 

 

def is_trivial(self): 

r""" 

Return ``True`` if this ideal is `(0)` or `(1)`. 

 

TESTS:: 

 

sage: I = ZZ.ideal(5) 

sage: I.is_trivial() 

False 

 

:: 

 

sage: I = ZZ['x'].ideal(-1) 

sage: I.is_trivial() 

True 

 

:: 

 

sage: I = ZZ['x'].ideal(ZZ['x'].gen()^2) 

sage: I.is_trivial() 

False 

 

:: 

 

sage: I = QQ['x', 'y'].ideal(-5) 

sage: I.is_trivial() 

True 

 

:: 

 

sage: I = CC['x'].ideal(0) 

sage: I.is_trivial() 

True 

 

This test addresses ticket :trac:`20514`:: 

 

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

sage: I = R.ideal(R.gens()) 

sage: I.is_trivial() 

False 

""" 

return self.is_zero() or self == self.ring().unit_ideal() 

 

def category(self): 

""" 

Return the category of this ideal. 

 

.. NOTE:: 

 

category is dependent on the ring of the ideal. 

 

EXAMPLES:: 

 

sage: P.<x> = ZZ[] 

sage: I = ZZ.ideal(7) 

sage: J = P.ideal(7,x) 

sage: K = P.ideal(7) 

sage: I.category() 

Category of ring ideals in Integer Ring 

sage: J.category() 

Category of ring ideals in Univariate Polynomial Ring in x 

over Integer Ring 

sage: K.category() 

Category of ring ideals in Univariate Polynomial Ring in x 

over Integer Ring 

""" 

import sage.categories.all 

return sage.categories.all.Ideals(self.__ring) 

 

def __add__(self, other): 

""" 

Add ``self`` on the left to ``other``. 

 

This makes sure that ``other`` and ``self`` are in the same rings. 

 

EXAMPLES:: 

 

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

sage: I = [x + y]*P 

sage: I + [y + z] 

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

""" 

if not isinstance(other, Ideal_generic): 

other = self.ring().ideal(other) 

return self.ring().ideal(self.gens() + other.gens()) 

 

def __radd__(self, other): 

""" 

Add ``self`` on the right to ``other``. 

 

This makes sure that ``other`` and ``self`` are in the same rings. 

 

EXAMPLES:: 

 

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

sage: I = [x + y]*P 

sage: [y + z] + I 

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

""" 

if not isinstance(other, Ideal_generic): 

other = self.ring().ideal(other) 

return self.ring().ideal(self.gens() + other.gens()) 

 

def __mul__(self, other): 

""" 

This method just makes sure that ``self`` and other are ideals in the 

same ring and then calls :meth:`_mul_`. If you want to change the 

behaviour of ideal multiplication in a subclass of 

:class:`Ideal_generic` please overwrite :meth:`_mul_` and not 

:meth:`__mul__`. 

 

EXAMPLES:: 

 

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

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

sage: I * 2 # indirect doctest 

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

""" 

if not isinstance(other, Ideal_generic): 

try: 

if self.ring().has_coerce_map_from(other): 

return self 

except (TypeError,ArithmeticError,ValueError): 

pass 

other = self.ring().ideal(other) 

return self._mul_(other) 

 

def _mul_(self, other): 

""" 

This is a very general implementation of Ideal multiplication. 

 

This method assumes that ``self`` and ``other`` are Ideals of the 

same ring. 

 

The number of generators of ```self * other` will be 

``self.ngens() * other.ngens()``. So if used repeatedly this method 

will create an ideal with a uselessly large amount of generators. 

Therefore it is advisable to overwrite this method with a method that 

takes advantage of the structure of the ring your working in. 

 

Example:: 

 

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

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

sage: J=P.ideal([x^2, x*y]) 

sage: I._mul_(J) 

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

""" 

return self.ring().ideal([z for z in [x*y for x in self.gens() for y in other.gens()] if z]) 

 

def __rmul__(self, other): 

""" 

Multiply ``self`` on the right with ``other``. 

 

EXAMPLES:: 

 

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

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

sage: [2]*I # indirect doctest 

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

 

""" 

if not isinstance(other, Ideal_generic): 

try: 

if self.ring().has_coerce_map_from(other): 

return self 

except (TypeError,ArithmeticError,ValueError): 

pass 

other = self.ring().ideal(other) 

return self.ring().ideal([z for z in [y*x for x in self.gens() for y in other.gens()] if z]) 

 

def norm(self): 

""" 

Returns the norm of this ideal. 

 

In the general case, this is just the ideal itself, since the ring it 

lies in can't be implicitly assumed to be an extension of anything. 

 

We include this function for compatibility with cases such as ideals in 

number fields. 

 

EXAMPLES:: 

 

sage: R.<t> = GF(8, names='a')[] 

sage: I = R.ideal(t^4 + t + 1) 

sage: I.norm() 

Principal ideal (t^4 + t + 1) of Univariate Polynomial Ring in t over Finite Field in a of size 2^3 

""" 

return self 

 

def absolute_norm(self): 

""" 

Returns the absolute norm of this ideal. 

 

In the general case, this is just the ideal itself, since the ring it 

lies in can't be implicitly assumed to be an extension of anything. 

 

We include this function for compatibility with cases such as ideals in 

number fields. 

 

.. TODO:: 

 

Implement this method. 

 

EXAMPLES:: 

 

sage: R.<t> = GF(9, names='a')[] 

sage: I = R.ideal(t^4 + t + 1) 

sage: I.absolute_norm() 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

raise NotImplementedError 

 

class Ideal_principal(Ideal_generic): 

""" 

A principal ideal. 

 

See :func:`Ideal()`. 

""" 

# now Ideal_principal takes a list. 

#def __init__(self, ring, gen): 

# Ideal_generic.__init__(self, ring, [gen]) 

 

def __repr__(self): 

""" 

Return a string representation of ``self``. 

 

EXAMPLES:: 

 

sage: R.<x> = ZZ[] 

sage: I = R.ideal(x) 

sage: I # indirect doctest 

Principal ideal (x) of Univariate Polynomial Ring in x over Integer Ring 

""" 

return "Principal ideal (%s) of %s"%(self.gen(), self.ring()) 

 

def is_principal(self): 

r""" 

Returns ``True`` if the ideal is principal in the ring containing the 

ideal. When the ideal construction is explicitly principal (i.e. 

when we define an ideal with one element) this is always the case. 

 

EXAMPLES: 

 

Note that Sage automatically coerces ideals into 

principal ideals during initialization:: 

 

sage: R.<x> = ZZ[] 

sage: I = R.ideal(x) 

sage: J = R.ideal(2,x) 

sage: K = R.base_extend(QQ).ideal(2,x) 

sage: I 

Principal ideal (x) of Univariate Polynomial Ring in x 

over Integer Ring 

sage: J 

Ideal (2, x) of Univariate Polynomial Ring in x over Integer Ring 

sage: K 

Principal ideal (1) of Univariate Polynomial Ring in x 

over Rational Field 

sage: I.is_principal() 

True 

sage: K.is_principal() 

True 

""" 

return True 

 

def gen(self): 

r""" 

Returns the generator of the principal ideal. The generators are 

elements of the ring containing the ideal. 

 

EXAMPLES: 

 

A simple example in the integers:: 

 

sage: R = ZZ 

sage: I = R.ideal(7) 

sage: J = R.ideal(7, 14) 

sage: I.gen(); J.gen() 

7 

7 

 

Note that the generator belongs to the ring from which the ideal 

was initialized:: 

 

sage: R.<x> = ZZ[] 

sage: I = R.ideal(x) 

sage: J = R.base_extend(QQ).ideal(2,x) 

sage: a = I.gen(); a 

x 

sage: b = J.gen(); b 

1 

sage: a.base_ring() 

Integer Ring 

sage: b.base_ring() 

Rational Field 

""" 

return self.gens()[0] 

 

def __contains__(self, x): 

""" 

Return ``True`` if ``x`` is in ``self``. 

 

EXAMPLES:: 

 

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

sage: I = P.ideal(x^2-2) 

sage: x^2 in I 

False 

sage: x^2-2 in I 

True 

sage: x^2-3 in I 

False 

""" 

if self.gen().is_zero(): 

return x.is_zero() 

return self.gen().divides(x) 

 

def __hash__(self): 

r""" 

Very stupid constant hash function! 

 

TESTS:: 

 

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

sage: I = P.ideal(x^2) 

sage: J = [x, y^2 + x*y]*P 

sage: hash(I) 

0 

sage: hash(J) 

0 

""" 

return 0 

 

def _richcmp_(self, other, op): 

""" 

Compare the two ideals. 

 

EXAMPLES: 

 

Comparison with non-principal ideal:: 

 

sage: R.<x> = ZZ[] 

sage: I = R.ideal([x^3 + 4*x - 1, x + 6]) 

sage: J = [x^2] * R 

sage: I > J # indirect doctest 

True 

sage: J < I # indirect doctest 

True 

 

Between two principal ideals:: 

 

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

sage: I = P.ideal(x^2-2) 

sage: I2 = P.ideal(0) 

sage: I2.is_zero() 

True 

sage: I2 < I 

True 

sage: I3 = P.ideal(x) 

sage: I > I3 

True 

""" 

if not isinstance(other, Ideal_generic): 

other = self.ring().ideal(other) 

 

try: 

if not other.is_principal(): 

return rich_to_bool(op, -1) 

except NotImplementedError: 

# If we do not know if the other is principal or not, then we 

# fallback to the generic implementation 

return Ideal_generic._richcmp_(self, other, op) 

 

if self.is_zero(): 

if not other.is_zero(): 

return rich_to_bool(op, -1) 

return rich_to_bool(op, 0) 

 

# is other.gen() / self.gen() a unit in the base ring? 

g0 = other.gen() 

g1 = self.gen() 

if g0.divides(g1) and g1.divides(g0): 

return rich_to_bool(op, 0) 

return rich_to_bool(op, 1) 

 

def divides(self, other): 

""" 

Return ``True`` if ``self`` divides ``other``. 

 

EXAMPLES:: 

 

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

sage: I = P.ideal(x) 

sage: J = P.ideal(x^2) 

sage: I.divides(J) 

True 

sage: J.divides(I) 

False 

""" 

if isinstance(other, Ideal_principal): 

return self.gen().divides(other.gen()) 

raise NotImplementedError 

 

class Ideal_pid(Ideal_principal): 

""" 

An ideal of a principal ideal domain. 

 

See :func:`Ideal()`. 

""" 

def __init__(self, ring, gen): 

""" 

Initialize ``self``. 

 

EXAMPLES:: 

 

sage: I = 8*ZZ 

sage: I 

Principal ideal (8) of Integer Ring 

""" 

Ideal_principal.__init__(self, ring, gen) 

 

def __add__(self, other): 

""" 

Add the two ideals. 

 

EXAMPLES:: 

 

sage: I = 8*ZZ 

sage: I2 = 3*ZZ 

sage: I + I2 

Principal ideal (1) of Integer Ring 

""" 

if not isinstance(other, Ideal_generic): 

other = self.ring().ideal(other) 

return self.ring().ideal(self.gcd(other)) 

 

def reduce(self, f): 

""" 

Return the reduction of `f` modulo ``self``. 

 

EXAMPLES:: 

 

sage: I = 8*ZZ 

sage: I.reduce(10) 

2 

sage: n = 10; n.mod(I) 

2 

""" 

f = self.ring()(f) 

if self.gen() == 0: 

return f 

q, r = f.quo_rem(self.gen()) 

return r 

 

def gcd(self, other): 

r""" 

Returns the greatest common divisor of the principal ideal with the 

ideal ``other``; that is, the largest principal ideal 

contained in both the ideal and ``other`` 

 

.. TODO:: 

 

This is not implemented in the case when ``other`` is neither 

principal nor when the generator of ``self`` is contained in 

``other``. Also, it seems that this class is used only in PIDs--is 

this redundant? 

 

.. NOTE:: 

 

The second example is broken. 

 

EXAMPLES: 

 

An example in the principal ideal domain `\ZZ`:: 

 

sage: R = ZZ 

sage: I = R.ideal(42) 

sage: J = R.ideal(70) 

sage: I.gcd(J) 

Principal ideal (14) of Integer Ring 

sage: J.gcd(I) 

Principal ideal (14) of Integer Ring 

 

TESTS: 

 

We cannot take the gcd of a principal ideal with a 

non-principal ideal as well: ( ``gcd(I,J)`` should be `(7)` ) 

 

:: 

 

sage: R.<x> = ZZ[] 

sage: I = ZZ.ideal(7) 

sage: J = R.ideal(7,x) 

sage: I.gcd(J) 

Traceback (most recent call last): 

... 

NotImplementedError 

sage: J.gcd(I) 

Traceback (most recent call last): 

... 

AttributeError: 'Ideal_generic' object has no attribute 'gcd' 

 

Note:: 

 

sage: type(I) 

<class 'sage.rings.ideal.Ideal_pid'> 

sage: type(J) 

<class 'sage.rings.ideal.Ideal_generic'> 

""" 

if isinstance(other, Ideal_principal): 

return self.ring().ideal(self.gen().gcd(other.gen())) 

elif self.gen() in other: 

return other 

else: 

raise NotImplementedError 

 

def is_prime(self): 

""" 

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

 

This relies on the ring elements having a method ``is_irreducible()`` 

implemented, since an ideal `(a)` is prime iff `a` is irreducible 

(or 0). 

 

EXAMPLES:: 

 

sage: ZZ.ideal(2).is_prime() 

True 

sage: ZZ.ideal(-2).is_prime() 

True 

sage: ZZ.ideal(4).is_prime() 

False 

sage: ZZ.ideal(0).is_prime() 

True 

sage: R.<x> = QQ[] 

sage: P = R.ideal(x^2+1); P 

Principal ideal (x^2 + 1) of Univariate Polynomial Ring in x over Rational Field 

sage: P.is_prime() 

True 

 

In fields, only the zero ideal is prime:: 

 

sage: RR.ideal(0).is_prime() 

True 

sage: RR.ideal(7).is_prime() 

False 

""" 

if self.is_zero(): # PIDs are integral domains by definition 

return True 

g = self.gen() 

if g.is_one(): # The ideal (1) is never prime 

return False 

if hasattr(g, 'is_irreducible'): 

return g.is_irreducible() 

 

raise NotImplementedError 

 

def is_maximal(self): 

""" 

Returns whether this ideal is maximal. 

 

Principal ideal domains have Krull dimension 1 (or 0), so an ideal is 

maximal if and only if it's prime (and nonzero if the ring is not a 

field). 

 

EXAMPLES:: 

 

sage: R.<t> = GF(5)[] 

sage: p = R.ideal(t^2 + 2) 

sage: p.is_maximal() 

True 

sage: p = R.ideal(t^2 + 1) 

sage: p.is_maximal() 

False 

sage: p = R.ideal(0) 

sage: p.is_maximal() 

False 

sage: p = R.ideal(1) 

sage: p.is_maximal() 

False 

""" 

if not self.ring().is_field() and self.is_zero(): 

return False 

return self.is_prime() 

 

def residue_field(self): 

r""" 

Return the residue class field of this ideal, which must be prime. 

 

.. TODO:: 

 

Implement this for more general rings. Currently only defined 

for `\ZZ` and for number field orders. 

 

EXAMPLES:: 

 

sage: P = ZZ.ideal(61); P 

Principal ideal (61) of Integer Ring 

sage: F = P.residue_field(); F 

Residue field of Integers modulo 61 

sage: pi = F.reduction_map(); pi 

Partially defined reduction map: 

From: Rational Field 

To: Residue field of Integers modulo 61 

sage: pi(123/234) 

6 

sage: pi(1/61) 

Traceback (most recent call last): 

... 

ZeroDivisionError: Cannot reduce rational 1/61 modulo 61: it has negative valuation 

sage: lift = F.lift_map(); lift 

Lifting map: 

From: Residue field of Integers modulo 61 

To: Integer Ring 

sage: lift(F(12345/67890)) 

33 

sage: (12345/67890) % 61 

33 

 

TESTS:: 

 

sage: ZZ.ideal(96).residue_field() 

Traceback (most recent call last): 

... 

ValueError: The ideal (Principal ideal (96) of Integer Ring) is not prime 

 

:: 

 

sage: R.<x>=QQ[] 

sage: I=R.ideal(x^2+1) 

sage: I.is_prime() 

True 

sage: I.residue_field() 

Traceback (most recent call last): 

... 

TypeError: residue fields only supported for polynomial rings over finite fields. 

""" 

if not self.is_prime(): 

raise ValueError("The ideal (%s) is not prime"%self) 

from sage.rings.integer_ring import ZZ 

if self.ring() is ZZ: 

return ZZ.residue_field(self, check = False) 

raise NotImplementedError("residue_field() is only implemented for ZZ and rings of integers of number fields.") 

 

class Ideal_fractional(Ideal_generic): 

""" 

Fractional ideal of a ring. 

 

See :func:`Ideal()`. 

""" 

def __repr__(self): 

""" 

Return a string representation of ``self``. 

 

EXAMPLES:: 

 

sage: from sage.rings.ideal import Ideal_fractional 

sage: K.<a> = NumberField(x^2 + 1) 

sage: Ideal_fractional(K, [a]) # indirect doctest 

Fractional ideal (a) of Number Field in a with defining polynomial x^2 + 1 

""" 

return "Fractional ideal %s of %s"%(self._repr_short(), self.ring()) 

 

# constructors for standard (benchmark) ideals, written uppercase as 

# these are constructors 

 

def Cyclic(R, n=None, homog=False, singular=singular_default): 

""" 

Ideal of cyclic ``n``-roots from 1-st ``n`` variables of ``R`` if ``R`` is 

coercible to :class:`Singular <sage.interfaces.singular.Singular>`. 

 

INPUT: 

 

- ``R`` -- base ring to construct ideal for 

 

- ``n`` -- number of cyclic roots (default: ``None``). If ``None``, then 

``n`` is set to ``R.ngens()``. 

 

- ``homog`` -- (default: ``False``) if ``True`` a homogeneous ideal is 

returned using the last variable in the ideal 

 

- ``singular`` -- singular instance to use 

 

.. NOTE:: 

 

``R`` will be set as the active ring in 

:class:`Singular <sage.interfaces.singular.Singular>` 

 

EXAMPLES: 

 

An example from a multivariate polynomial ring over the 

rationals:: 

 

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] 

 

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 

""" 

from .rational_field import RationalField 

 

if n: 

if n > R.ngens(): 

raise ArithmeticError("n must be <= R.ngens()") 

else: 

n = R.ngens() 

 

singular.lib("poly") 

R2 = R.change_ring(RationalField()) 

R2._singular_().set_ring() 

 

if not homog: 

I = singular.cyclic(n) 

else: 

I = singular.cyclic(n).homog(R2.gen(n-1)) 

return R2.ideal(I).change_ring(R) 

 

def Katsura(R, n=None, homog=False, singular=singular_default): 

""" 

``n``-th katsura ideal of ``R`` if ``R`` is coercible to 

:class:`Singular <sage.interfaces.singular.Singular>`. 

 

INPUT: 

 

- ``R`` -- base ring to construct ideal for 

 

- ``n`` -- (default: ``None``) which katsura ideal of ``R``. If ``None``, 

then ``n`` is set to ``R.ngens()``. 

 

- ``homog`` -- if ``True`` a homogeneous ideal is returned 

using the last variable in the ideal (default: ``False``) 

 

- ``singular`` -- singular instance to use 

 

 

EXAMPLES:: 

 

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

sage: I = sage.rings.ideal.Katsura(P,3); 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: Q.<x> = PolynomialRing(QQ, implementation="singular") 

sage: J = sage.rings.ideal.Katsura(Q,1); J 

Ideal (x - 1) of Multivariate Polynomial Ring in x over Rational Field 

""" 

from .rational_field import RationalField 

if n: 

if n > R.ngens(): 

raise ArithmeticError("n must be <= R.ngens().") 

else: 

n = R.ngens() 

singular.lib("poly") 

R2 = R.change_ring(RationalField()) 

R2._singular_().set_ring() 

 

if not homog: 

I = singular.katsura(n) 

else: 

I = singular.katsura(n).homog(R2.gen(n-1)) 

return R2.ideal(I).change_ring(R) 

 

def FieldIdeal(R): 

r""" 

Let ``q = R.base_ring().order()`` and `(x_0,...,x_n)` ``= R.gens()`` then 

if `q` is finite this constructor returns 

 

.. MATH:: 

 

\langle x_0^q - x_0, ... , x_n^q - x_n \rangle. 

 

We call this ideal the field ideal and the generators the field 

equations. 

 

EXAMPLES: 

 

The field ideal generated from the polynomial ring over 

two variables in the finite field of size 2:: 

 

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

sage: I = sage.rings.ideal.FieldIdeal(P); I 

Ideal (x^2 + x, y^2 + y) of Multivariate Polynomial Ring in x, y over 

Finite Field of size 2 

 

Another, similar example:: 

 

sage: Q.<x1,x2,x3,x4> = PolynomialRing(GF(2^4,name='alpha'), 4) 

sage: J = sage.rings.ideal.FieldIdeal(Q); J 

Ideal (x1^16 + x1, x2^16 + x2, x3^16 + x3, x4^16 + x4) of 

Multivariate Polynomial Ring in x1, x2, x3, x4 over Finite 

Field in alpha of size 2^4 

""" 

 

q = R.base_ring().order() 

 

if q is sage.rings.infinity.infinity: 

raise TypeError("Cannot construct field ideal for R.base_ring().order()==infinity") 

 

return R.ideal([x**q - x for x in R.gens() ])