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

""" 

Infinite Polynomial Rings. 

 

By Infinite Polynomial Rings, we mean polynomial rings in a countably 

infinite number of variables. The implementation consists of a wrapper 

around the current *finite* polynomial rings in Sage. 

 

AUTHORS: 

 

- Simon King <simon.king@nuigalway.ie> 

- Mike Hansen <mhansen@gmail.com> 

 

An Infinite Polynomial Ring has finitely many generators `x_\\ast, 

y_\\ast,...` and infinitely many variables of the form `x_0, x_1, x_2, 

..., y_0, y_1, y_2,...,...`. We refer to the natural number `n` as 

the *index* of the variable `x_n`. 

 

INPUT: 

 

- ``R``, the base ring. It has to be a commutative ring, and in some 

applications it must even be a field 

- ``names``, a list of generator names. Generator names must be alpha-numeric. 

- ``order`` (optional string). The default order is ``'lex'`` (lexicographic). 

``'deglex'`` is degree lexicographic, and ``'degrevlex'`` (degree reverse 

lexicographic) is possible but discouraged. 

 

Each generator ``x`` produces an infinite sequence of variables 

``x[1], x[2], ...`` which are printed on screen as ``x_1, x_2, ...`` 

and are latex typeset as `x_{1}, x_{2}`. Then, the Infinite 

Polynomial Ring is formed by polynomials in these variables. 

 

By default, the monomials are ordered lexicographically. Alternatively, 

degree (reverse) lexicographic ordering is possible as well. However, we 

do not guarantee that the computation of Groebner bases will terminate 

in this case. 

 

In either case, the variables of a Infinite Polynomial Ring X are ordered 

according to the following rule: 

 

``X.gen(i)[m] > X.gen(j)[n]`` if and only if ``i<j or (i==j and m>n)`` 

 

We provide a 'dense' and a 'sparse' implementation. In the dense 

implementation, the Infinite Polynomial Ring carries a finite 

polynomial ring that comprises *all* variables up to the maximal index 

that has been used so far. This is potentially a very big ring and may 

also comprise many variables that are not used. 

 

In the sparse implementation, we try to keep the underlying finite 

polynomial rings small, using only those variables that are really 

needed. By default, we use the dense implementation, since it usually 

is much faster. 

 

EXAMPLES:: 

 

sage: X.<x,y> = InfinitePolynomialRing(ZZ, implementation='sparse') 

sage: A.<alpha,beta> = InfinitePolynomialRing(QQ, order='deglex') 

 

sage: f = x[5] + 2; f 

x_5 + 2 

sage: g = 3*y[1]; g 

3*y_1 

 

It has some advantages to have an underlying ring that is not 

univariate. Hence, we always have at least two variables:: 

 

sage: g._p.parent() 

Multivariate Polynomial Ring in y_1, y_0 over Integer Ring 

 

sage: f2 = alpha[5] + 2; f2 

alpha_5 + 2 

sage: g2 = 3*beta[1]; g2 

3*beta_1 

sage: A.polynomial_ring() 

Multivariate Polynomial Ring in alpha_5, alpha_4, alpha_3, alpha_2, alpha_1, alpha_0, beta_5, beta_4, beta_3, beta_2, beta_1, beta_0 over Rational Field 

 

Of course, we provide the usual polynomial arithmetic:: 

 

sage: f+g 

x_5 + 3*y_1 + 2 

sage: p = x[10]^2*(f+g); p 

x_10^2*x_5 + 3*x_10^2*y_1 + 2*x_10^2 

sage: p2 = alpha[10]^2*(f2+g2); p2 

alpha_10^2*alpha_5 + 3*alpha_10^2*beta_1 + 2*alpha_10^2 

 

There is a permutation action on the variables, by permuting positive 

variable indices:: 

 

sage: P = Permutation(((10,1))) 

sage: p^P 

x_5*x_1^2 + 3*x_1^2*y_10 + 2*x_1^2 

sage: p2^P 

alpha_5*alpha_1^2 + 3*alpha_1^2*beta_10 + 2*alpha_1^2 

 

Note that `x_0^P = x_0`, since the permutations only change *positive* 

variable indices. 

 

We also implemented ideals of Infinite Polynomial Rings. Here, it is 

thoroughly assumed that the ideals are set-wise invariant under the 

permutation action. We therefore refer to these ideals as *Symmetric 

Ideals*. Symmetric Ideals are finitely generated modulo addition, 

multiplication by ring elements and permutation of variables. If the 

base ring is a field, one can compute Symmetric Groebner Bases:: 

 

sage: J = A*(alpha[1]*beta[2]) 

sage: J.groebner_basis() 

[alpha_1*beta_2, alpha_2*beta_1] 

 

For more details, see :class:`~sage.rings.polynomial.symmetric_ideal.SymmetricIdeal`. 

 

Infinite Polynomial Rings can have any commutative base ring. If the 

base ring of an Infinite Polynomial Ring is a (classical or infinite) 

Polynomial Ring, then our implementation tries to merge everything 

into *one* ring. The basic requirement is that the monomial orders 

match. In the case of two Infinite Polynomial Rings, the 

implementations must match. Moreover, name conflicts should be 

avoided. An overlap is only accepted if the order of variables can be 

uniquely inferred, as in the following example:: 

 

sage: A.<a,b,c> = InfinitePolynomialRing(ZZ) 

sage: B.<b,c,d> = InfinitePolynomialRing(A) 

sage: B 

Infinite polynomial ring in a, b, c, d over Integer Ring 

 

This is also allowed if finite polynomial rings are involved:: 

 

sage: A.<a_3,a_1,b_1,c_2,c_0> = ZZ[] 

sage: B.<b,c,d> = InfinitePolynomialRing(A, order='degrevlex') 

sage: B 

Infinite polynomial ring in b, c, d over Multivariate Polynomial Ring in a_3, a_1 over Integer Ring 

 

It is no problem if one generator of the Infinite Polynomial Ring is 

called ``x`` and one variable of the base ring is also called 

``x``. This is since no *variable* of the Infinite Polynomial Ring 

will be called ``x``. However, a problem arises if the underlying 

classical Polynomial Ring has a variable ``x_1``, since this can be 

confused with a variable of the Infinite Polynomial Ring. In this 

case, an error will be raised:: 

 

sage: X.<x,y_1> = ZZ[] 

sage: Y.<x,z> = InfinitePolynomialRing(X) 

 

Note that ``X`` is not merged into ``Y``; this is since the monomial 

order of ``X`` is 'degrevlex', but of ``Y`` is 'lex'. 

:: 

 

sage: Y 

Infinite polynomial ring in x, z over Multivariate Polynomial Ring in x, y_1 over Integer Ring 

 

The variable ``x`` of ``X`` can still be interpreted in ``Y``, 

although the first generator of ``Y`` is called ``x`` as well:: 

 

sage: x 

x_* 

sage: X('x') 

x 

sage: Y(X('x')) 

x 

sage: Y('x') 

x 

 

But there is only merging if the resulting monomial order is uniquely 

determined. This is not the case in the following examples, and thus 

an error is raised:: 

 

sage: X.<y_1,x> = ZZ[] 

sage: Y.<y,z> = InfinitePolynomialRing(X) 

Traceback (most recent call last): 

... 

CoercionException: Overlapping variables (('y', 'z'),['y_1']) are incompatible 

sage: Y.<z,y> = InfinitePolynomialRing(X) 

Traceback (most recent call last): 

... 

CoercionException: Overlapping variables (('z', 'y'),['y_1']) are incompatible 

sage: X.<x_3,y_1,y_2> = PolynomialRing(ZZ,order='lex') 

sage: # y_1 and y_2 would be in opposite order in an Infinite Polynomial Ring 

sage: Y.<y> = InfinitePolynomialRing(X) 

Traceback (most recent call last): 

... 

CoercionException: Overlapping variables (('y',),['y_1', 'y_2']) are incompatible 

 

 

If the type of monomial orderings (e.g., 'degrevlex' versus 'lex') or 

if the implementations don't match, there is no simplified 

construction available:: 

 

sage: X.<x,y> = InfinitePolynomialRing(ZZ) 

sage: Y.<z> = InfinitePolynomialRing(X,order='degrevlex') 

sage: Y 

Infinite polynomial ring in z over Infinite polynomial ring in x, y over Integer Ring 

sage: Y.<z> = InfinitePolynomialRing(X,implementation='sparse') 

sage: Y 

Infinite polynomial ring in z over Infinite polynomial ring in x, y over Integer Ring 

 

TESTS: 

 

Infinite Polynomial Rings are part of Sage's coercion system. Hence, 

we can do arithmetic, so that the result lives in a ring into which 

all constituents coerce. 

:: 

 

sage: R.<a,b> = InfinitePolynomialRing(ZZ) 

sage: X.<x> = InfinitePolynomialRing(R) 

sage: x[2]/2+(5/3)*a[3]*x[4] + 1 

5/3*a_3*x_4 + 1/2*x_2 + 1 

 

sage: R.<a,b> = InfinitePolynomialRing(ZZ,implementation='sparse') 

sage: X.<x> = InfinitePolynomialRing(R) 

sage: x[2]/2+(5/3)*a[3]*x[4] + 1 

5/3*a_3*x_4 + 1/2*x_2 + 1 

 

sage: R.<a,b> = InfinitePolynomialRing(ZZ,implementation='sparse') 

sage: X.<x> = InfinitePolynomialRing(R,implementation='sparse') 

sage: x[2]/2+(5/3)*a[3]*x[4] + 1 

5/3*a_3*x_4 + 1/2*x_2 + 1 

 

sage: R.<a,b> = InfinitePolynomialRing(ZZ) 

sage: X.<x> = InfinitePolynomialRing(R,implementation='sparse') 

sage: x[2]/2+(5/3)*a[3]*x[4] + 1 

5/3*a_3*x_4 + 1/2*x_2 + 1 

 

""" 

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

# Copyright (C) 2009 Simon King <simon.king@nuigalway.ie> and 

# Mike Hansen <mhansen@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 six.moves import range 

 

import six 

from sage.rings.ring import CommutativeRing 

from sage.structure.all import SageObject, parent 

from sage.structure.factory import UniqueFactory 

from sage.misc.cachefunc import cached_method 

import operator, re 

from functools import reduce 

 

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

## Ring Factory framework 

 

class InfinitePolynomialRingFactory(UniqueFactory): 

""" 

A factory for creating infinite polynomial ring elements. It 

handles making sure that they are unique as well as handling 

pickling. For more details, see 

:class:`~sage.structure.factory.UniqueFactory` and 

:mod:`~sage.rings.polynomial.infinite_polynomial_ring`. 

 

EXAMPLES:: 

 

sage: A.<a> = InfinitePolynomialRing(QQ) 

sage: B.<b> = InfinitePolynomialRing(A) 

sage: B.construction() 

[InfPoly{[a,b], "lex", "dense"}, Rational Field] 

sage: R.<a,b> = InfinitePolynomialRing(QQ) 

sage: R is B 

True 

sage: X.<x> = InfinitePolynomialRing(QQ) 

sage: X2.<x> = InfinitePolynomialRing(QQ, implementation='sparse') 

sage: X is X2 

False 

 

sage: X is loads(dumps(X)) 

True 

 

""" 

def create_key(self, R, names=('x',), order='lex', implementation='dense'): 

""" 

Creates a key which uniquely defines the infinite polynomial ring. 

 

TESTS:: 

 

sage: InfinitePolynomialRing.create_key(QQ, ('y1',)) 

(InfPoly{[y1], "lex", "dense"}(FractionField(...)), Integer Ring) 

sage: _[0].all 

[FractionField, InfPoly{[y1], "lex", "dense"}] 

sage: InfinitePolynomialRing.create_key(QQ, names=['beta'], order='deglex', implementation='sparse') 

(InfPoly{[beta], "deglex", "sparse"}(FractionField(...)), Integer Ring) 

sage: _[0].all 

[FractionField, InfPoly{[beta], "deglex", "sparse"}] 

sage: InfinitePolynomialRing.create_key(QQ, names=['x','y'], implementation='dense') 

(InfPoly{[x,y], "lex", "dense"}(FractionField(...)), Integer Ring) 

sage: _[0].all 

[FractionField, InfPoly{[x,y], "lex", "dense"}] 

 

If no generator name is provided, a generator named 'x', 

lexicographic order and the dense implementation are assumed:: 

 

sage: InfinitePolynomialRing.create_key(QQ) 

(InfPoly{[x], "lex", "dense"}(FractionField(...)), Integer Ring) 

sage: _[0].all 

[FractionField, InfPoly{[x], "lex", "dense"}] 

 

If it is attempted to use no generator, a ValueError is raised:: 

 

sage: InfinitePolynomialRing.create_key(ZZ, names=[]) 

Traceback (most recent call last): 

... 

ValueError: Infinite Polynomial Rings must have at least one generator 

 

""" 

if isinstance(names, list): 

names = tuple(names) 

if not names: 

raise ValueError("Infinite Polynomial Rings must have at least one generator") 

if len(names)>len(set(names)): 

raise ValueError("The variable names must be distinct") 

F = InfinitePolynomialFunctor(names,order,implementation) 

while hasattr(R,'construction'): 

C = R.construction() 

if C is None: 

break 

F = F*C[0] 

R = C[1] 

return (F,R) 

 

 

def create_object(self, version, key): 

""" 

Returns the infinite polynomial ring corresponding to the key ``key``. 

 

TESTS:: 

 

sage: InfinitePolynomialRing.create_object('1.0', InfinitePolynomialRing.create_key(ZZ, ('x3',))) 

Infinite polynomial ring in x3 over Integer Ring 

 

""" 

if len(key)>2: 

# We got an old pickle. By calling the ring constructor, it will automatically 

# be transformed into the new scheme 

return InfinitePolynomialRing(*key) 

# By now, we have different unique keys, based on construction functors 

C,R = key 

from sage.categories.pushout import CompositeConstructionFunctor, InfinitePolynomialFunctor 

if isinstance(C,CompositeConstructionFunctor): 

F = C.all[-1] 

if len(C.all)>1: 

R = CompositeConstructionFunctor(*C.all[:-1])(R) 

else: 

F = C 

if not isinstance(F, InfinitePolynomialFunctor): 

raise TypeError("We expected an InfinitePolynomialFunctor, not %s"%type(F)) 

if F._imple=='sparse': 

return InfinitePolynomialRing_sparse(R, F._gens, order=F._order) 

return InfinitePolynomialRing_dense(R, F._gens, order=F._order) 

 

InfinitePolynomialRing = InfinitePolynomialRingFactory('InfinitePolynomialRing') 

 

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

## The Construction Functor 

 

from sage.categories.pushout import InfinitePolynomialFunctor 

 

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

## An auxiliary dictionary-like class that returns variables 

 

class InfiniteGenDict: 

""" 

A dictionary-like class that is suitable for usage in ``sage_eval``. 

 

The generators of an Infinite Polynomial Ring are not 

variables. Variables of an Infinite Polynomial Ring are returned 

by indexing a generator. The purpose of this class is to return a 

variable of an Infinite Polynomial Ring, given its string 

representation. 

 

EXAMPLES:: 

 

sage: R.<a,b> = InfinitePolynomialRing(ZZ) 

sage: D = R.gens_dict() # indirect doctest 

sage: D._D 

[InfiniteGenDict defined by ['a', 'b'], {'1': 1}] 

sage: D._D[0]['a_15'] 

a_15 

sage: type(_) 

<class 'sage.rings.polynomial.infinite_polynomial_element.InfinitePolynomial_dense'> 

sage: sage_eval('3*a_3*b_5-1/2*a_7', D._D[0]) 

-1/2*a_7 + 3*a_3*b_5 

 

""" 

def __init__(self, Gens): 

""" 

INPUT: 

 

``Gens`` -- a list of generators of an infinite polynomial ring. 

 

EXAMPLES:: 

 

sage: R.<a,b> = InfinitePolynomialRing(ZZ) 

sage: D = R.gens_dict() # indirect doctest 

sage: D._D 

[InfiniteGenDict defined by ['a', 'b'], {'1': 1}] 

sage: D._D == loads(dumps(D._D)) # indirect doctest 

True 

 

""" 

self._D = dict(zip([(hasattr(X,'_name') and X._name) or repr(X) for X in Gens],Gens)) 

 

def __eq__(self, other): 

""" 

Check whether ``self`` is equal to ``other``. 

 

EXAMPLES:: 

 

sage: R.<a,b> = InfinitePolynomialRing(ZZ) 

sage: D = R.gens_dict() # indirect doctest 

sage: D._D 

[InfiniteGenDict defined by ['a', 'b'], {'1': 1}] 

sage: D._D == loads(dumps(D._D)) # indirect doctest 

True 

""" 

if isinstance(other, InfiniteGenDict): 

return self._D == other._D 

return False 

 

def __ne__(self, other): 

""" 

Check whether ``self`` is not equal to ``other``. 

 

EXAMPLES:: 

 

sage: R.<a,b> = InfinitePolynomialRing(ZZ) 

sage: D = R.gens_dict() # indirect doctest 

sage: D._D 

[InfiniteGenDict defined by ['a', 'b'], {'1': 1}] 

sage: D._D != loads(dumps(D._D)) # indirect doctest 

False 

""" 

return not (self == other) 

 

def __repr__(self): 

""" 

EXAMPLES:: 

 

sage: R.<a,b> = InfinitePolynomialRing(ZZ) 

sage: D = R.gens_dict() 

sage: D._D # indirect doctest 

[InfiniteGenDict defined by ['a', 'b'], {'1': 1}] 

""" 

return "InfiniteGenDict defined by %s"%repr(self._D.keys()) 

 

def __getitem__(self, k): 

""" 

EXAMPLES:: 

 

sage: R.<a,b> = InfinitePolynomialRing(ZZ) 

sage: D = R.gens_dict() # indirect doctest 

sage: D._D 

[InfiniteGenDict defined by ['a', 'b'], {'1': 1}] 

sage: D._D[0]['a_15'] 

a_15 

sage: type(_) 

<class 'sage.rings.polynomial.infinite_polynomial_element.InfinitePolynomial_dense'> 

""" 

 

if not isinstance(k, six.string_types): 

raise KeyError("String expected") 

L = k.split('_') 

try: 

if len(L)==2: 

return self._D[L[0]][int(L[1])] 

except Exception: 

pass 

raise KeyError("%s is not a variable name"%k) 

 

class GenDictWithBasering: 

""" 

A dictionary-like class that is suitable for usage in ``sage_eval``. 

 

This pseudo-dictionary accepts strings as index, and then walks down 

a chain of base rings of (infinite) polynomial rings until it finds 

one ring that has the given string as variable name, which is then 

returned. 

 

EXAMPLES:: 

 

sage: R.<a,b> = InfinitePolynomialRing(ZZ) 

sage: D = R.gens_dict() # indirect doctest 

sage: D 

GenDict of Infinite polynomial ring in a, b over Integer Ring 

sage: D['a_15'] 

a_15 

sage: type(_) 

<class 'sage.rings.polynomial.infinite_polynomial_element.InfinitePolynomial_dense'> 

sage: sage_eval('3*a_3*b_5-1/2*a_7', D) 

-1/2*a_7 + 3*a_3*b_5 

 

""" 

 

def __init__(self,parent, start): 

""" 

INPUT: 

 

``parent`` -- a ring. 

``start`` -- some dictionary, usually the dictionary of variables of ``parent``. 

 

EXAMPLES:: 

 

sage: R.<a,b> = InfinitePolynomialRing(ZZ) 

sage: D = R.gens_dict() # indirect doctest 

sage: D 

GenDict of Infinite polynomial ring in a, b over Integer Ring 

sage: D['a_15'] 

a_15 

sage: type(_) 

<class 'sage.rings.polynomial.infinite_polynomial_element.InfinitePolynomial_dense'> 

sage: sage_eval('3*a_3*b_5-1/2*a_7', D) 

-1/2*a_7 + 3*a_3*b_5 

 

TESTS:: 

 

sage: from sage.rings.polynomial.infinite_polynomial_ring import GenDictWithBasering 

sage: R = ZZ['x']['y']['a','b']['c'] 

sage: D = GenDictWithBasering(R,R.gens_dict()) 

sage: R.gens_dict()['a'] 

Traceback (most recent call last): 

... 

KeyError: 'a' 

sage: D['a'] 

a 

 

""" 

P = self._P = parent 

if isinstance(start,list): 

self._D = start 

return 

self._D = [start] 

while hasattr(P,'base_ring') and (P.base_ring() is not P): 

P = P.base_ring() 

D = P.gens_dict() 

if isinstance(D, GenDictWithBasering): 

self._D.extend(D._D) 

break 

else: 

self._D.append(D) 

def __next__(self): 

""" 

Return a dictionary that can be used to interprete strings in the base ring of ``self``. 

 

EXAMPLES:: 

 

sage: R.<a,b> = InfinitePolynomialRing(QQ['t']) 

sage: D = R.gens_dict() 

sage: D 

GenDict of Infinite polynomial ring in a, b over Univariate Polynomial Ring in t over Rational Field 

sage: next(D) 

GenDict of Univariate Polynomial Ring in t over Rational Field 

sage: sage_eval('t^2', next(D)) 

t^2 

 

""" 

if len(self._D)<=1: 

raise ValueError("No next term for %s available"%self) 

return GenDictWithBasering(self._P.base_ring(), self._D[1:]) 

 

next = __next__ 

 

def __repr__(self): 

""" 

TESTS:: 

 

sage: R.<a,b> = InfinitePolynomialRing(ZZ) 

sage: D = R.gens_dict() # indirect doctest 

sage: D 

GenDict of Infinite polynomial ring in a, b over Integer Ring 

""" 

return "GenDict of "+repr(self._P) 

 

def __getitem__(self, k): 

""" 

TESTS:: 

 

sage: R.<a,b> = InfinitePolynomialRing(ZZ) 

sage: D = R.gens_dict() # indirect doctest 

sage: D 

GenDict of Infinite polynomial ring in a, b over Integer Ring 

sage: D['a_15'] 

a_15 

sage: type(_) 

<class 'sage.rings.polynomial.infinite_polynomial_element.InfinitePolynomial_dense'> 

""" 

for D in self._D: 

try: 

return D[k] 

except KeyError: 

pass 

raise KeyError("%s is not a variable name of %s or its iterated base rings"%(k,self._P)) 

 

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

## The sparse implementation 

 

class InfinitePolynomialRing_sparse(CommutativeRing): 

""" 

Sparse implementation of Infinite Polynomial Rings. 

 

An Infinite Polynomial Ring with generators `x_\\ast, y_\\ast, 

...` over a field `F` is a free commutative `F`-algebra generated 

by `x_0, x_1, x_2, ..., y_0, y_1, y_2, ..., ...` and is equipped 

with a permutation action on the generators, namely `x_n^P = 

x_{P(n)}, y_{n}^P=y_{P(n)}, ...` for any permutation `P` (note 

that variables of index zero are invariant under such 

permutation). 

 

It is known that any permutation invariant ideal in an Infinite 

Polynomial Ring is finitely generated modulo the permutation 

action -- see :class:`~sage.rings.polynomial.symmetric_ideal.SymmetricIdeal` 

for more details. 

 

Usually, an instance of this class is created using 

``InfinitePolynomialRing`` with the optional parameter 

``implementation='sparse'``. This takes care of uniqueness of 

parent structures. However, a direct construction is possible, in 

principle:: 

 

sage: X.<x,y> = InfinitePolynomialRing(QQ, implementation='sparse') 

sage: Y.<x,y> = InfinitePolynomialRing(QQ, implementation='sparse') 

sage: X is Y 

True 

sage: from sage.rings.polynomial.infinite_polynomial_ring import InfinitePolynomialRing_sparse 

sage: Z = InfinitePolynomialRing_sparse(QQ, ['x','y'], 'lex') 

 

Nevertheless, since infinite polynomial rings are supposed to be unique 

parent structures, they do not evaluate equal. 

 

sage: Z == X 

False 

 

The last parameter ('lex' in the above example) can also be 

'deglex' or 'degrevlex'; this would result in an Infinite 

Polynomial Ring in degree lexicographic or degree reverse 

lexicographic order. 

 

See :mod:`~sage.rings.polynomial.infinite_polynomial_ring` for 

more details. 

 

""" 

def __init__(self, R, names, order): 

""" 

INPUT: 

 

``R`` -- base ring. 

``names`` -- list of generator names. 

``order`` -- string determining the monomial order of the infinite polynomial ring. 

 

EXAMPLES:: 

 

sage: X.<alpha,beta> = InfinitePolynomialRing(ZZ, implementation='sparse') 

 

Infinite Polynomial Rings are unique parent structures:: 

 

sage: X is loads(dumps(X)) 

True 

sage: p=alpha[10]*beta[2]^3+2*alpha[1]*beta[3] 

sage: p 

alpha_10*beta_2^3 + 2*alpha_1*beta_3 

 

We define another Infinite Polynomial Ring with same generator 

names but a different order. These rings are different, but 

allow for coercion:: 

 

sage: Y.<alpha,beta> = InfinitePolynomialRing(QQ, order='deglex', implementation='sparse') 

sage: Y is X 

False 

sage: q=beta[2]^3*alpha[10]+beta[3]*alpha[1]*2 

sage: q 

alpha_10*beta_2^3 + 2*alpha_1*beta_3 

sage: p==q 

True 

sage: X.gen(1)[2]*Y.gen(0)[1] 

alpha_1*beta_2 

 

""" 

if not names: 

names = ['x'] 

for n in names: 

if not (isinstance(n, six.string_types) and n.isalnum() and (not n[0].isdigit())): 

raise ValueError("generator names must be alpha-numeric strings not starting with a digit, but %s isn't"%n) 

if len(names)!=len(set(names)): 

raise ValueError("generator names must be pairwise different") 

self._names = tuple(names) 

if not isinstance(order, six.string_types): 

raise TypeError("The monomial order must be given as a string") 

try: 

if not (hasattr(R,'is_ring') and R.is_ring() and hasattr(R,'is_commutative') and R.is_commutative()): 

raise TypeError 

except Exception: 

raise TypeError("The given 'base ring' (= %s) must be a commutative ring"%(R)) 

 

# now, the input is accepted 

if hasattr(R,'_underlying_ring'): 

self._underlying_ring = R._underlying_ring 

else: 

self._underlying_ring = R.base_ring() 

 

# some basic data 

self._order = order 

self._name_dict = dict([(names[i], i) for i in range(len(names))]) 

from sage.categories.commutative_algebras import CommutativeAlgebras 

CommutativeRing.__init__(self, R, category=CommutativeAlgebras(R)) 

 

# some tools to analyse polynomial string representations. 

self._identify_variable = lambda x, y: (-self._names.index(x), int(y)) 

self._find_maxshift = re.compile('_([0-9]+)') # findall yields stringrep of the shifts 

self._find_variables = re.compile('[a-zA-Z0-9]+_[0-9]+') 

self._find_varpowers = re.compile('([a-zA-Z0-9]+)_([0-9]+)\^?([0-9]*)') # findall yields triple "generator_name", "index", "exponent" 

from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing 

# Create some small underlying polynomial ring. 

# It is used to ensure that the parent of the underlying 

# polynomial of an element of self is actually a *multi*variate 

# polynomial ring. 

from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing 

if len(names)==1: 

VarList = [names[0]+'_0',names[0]+'_1'] 

else: 

VarList = [X+'_0' for X in names] 

VarList.sort(key=self.varname_key, reverse=True) 

self._minP = PolynomialRing(R, len(VarList), VarList) 

self._populate_coercion_lists_() 

 

def __repr__(self): 

""" 

EXAMPLES:: 

 

sage: InfinitePolynomialRing(QQ) # indirect doctest 

Infinite polynomial ring in x over Rational Field 

 

sage: X.<alpha,beta> = InfinitePolynomialRing(ZZ, order='deglex'); X 

Infinite polynomial ring in alpha, beta over Integer Ring 

 

""" 

return "Infinite polynomial ring in %s over %s"%(", ".join(self._names), self._base) 

 

def _latex_(self): 

r""" 

EXAMPLES:: 

 

sage: from sage.misc.latex import latex 

sage: X.<x,y> = InfinitePolynomialRing(QQ) 

sage: latex(X) # indirect doctest 

\Bold{Q}[x_{\ast}, y_{\ast}] 

""" 

from sage.misc.latex import latex 

vars = ', '.join([latex(X) for X in self.gens()]) 

return "%s[%s]"%(latex(self.base_ring()), vars) 

 

@cached_method 

def _an_element_(self): 

""" 

Returns an element of this ring. 

 

EXAMPLES:: 

 

sage: R.<x> = InfinitePolynomialRing(QQ) 

sage: R.an_element() # indirect doctest 

x_1 

""" 

x = self.gen(0) 

return x[1] 

 

@cached_method 

def one(self): 

""" 

TESTS:: 

 

sage: X.<x,y> = InfinitePolynomialRing(QQ) 

sage: X.one() 

1 

""" 

from sage.rings.polynomial.infinite_polynomial_element import InfinitePolynomial 

return InfinitePolynomial(self,self._base(1)) 

 

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

## coercion 

 

def construction(self): 

""" 

Return the construction of ``self``. 

 

OUTPUT: 

 

A pair ``F,R``, where ``F`` is a construction functor and ``R`` is a ring, 

so that ``F(R) is self``. 

 

EXAMPLES:: 

 

sage: R.<x,y> = InfinitePolynomialRing(GF(5)) 

sage: R.construction() 

[InfPoly{[x,y], "lex", "dense"}, Finite Field of size 5] 

 

""" 

return [InfinitePolynomialFunctor(self._names, self._order, 'sparse'), self._base] 

 

def _coerce_map_from_(self, S): 

""" 

Coerce things into ``self``. 

 

NOTE: 

 

Any coercion will preserve variable names. 

 

EXAMPLES:: 

 

Here, we check to see that elements of a *finitely* generated 

polynomial ring with appropriate variable names coerce 

correctly into the Infinite Polynomial Ring:: 

 

sage: X.<x> = InfinitePolynomialRing(QQ) 

sage: px0 = PolynomialRing(QQ,'x_0').gen(0) 

sage: px0 + x[0] # indirect doctest 

2*x_0 

sage: px0==x[0] 

True 

 

It is possible to construct an Infinite Polynomial Ring whose 

base ring is another Infinite Polynomial Ring:: 

 

sage: R.<a,b> = InfinitePolynomialRing(ZZ) 

sage: X.<x> = InfinitePolynomialRing(R) 

sage: a[2]*x[3]+x[1]*a[4]^2 

a_4^2*x_1 + a_2*x_3 

 

""" 

# Use Construction Functors! 

from sage.categories.pushout import pushout, construction_tower 

try: 

# the following line should not test "pushout is self", but 

# only "pushout == self", since we also allow coercion from 

# dense to sparse implementation! 

P = pushout(self,S) 

# We don't care about the orders. But base ring and generators 

# of the pushout should remain the same as in self. 

return (P._names == self._names and P._base == self._base) 

except Exception: 

return False 

 

def _element_constructor_(self, x): 

""" 

Return an element of ``self``. 

 

INPUT: 

 

``x`` -- any object that can be interpreted in ``self``. 

 

TESTS:: 

 

sage: X = InfinitePolynomialRing(QQ) 

sage: a = X(2); a # indirect doctest 

2 

sage: a.parent() 

Infinite polynomial ring in x over Rational Field 

sage: R=PolynomialRing(ZZ,['x_3']) 

sage: b = X(R.gen()); b 

x_3 

sage: b.parent() 

Infinite polynomial ring in x over Rational Field 

sage: X('(x_1^2+2/3*x_4)*(x_2+x_5)') 

2/3*x_5*x_4 + x_5*x_1^2 + 2/3*x_4*x_2 + x_2*x_1^2 

 

""" 

# if x is in self, there's nothing left to do 

if parent(x) is self: 

return x 

from sage.rings.polynomial.infinite_polynomial_element import InfinitePolynomial 

# In many cases, the easiest solution is to "simply" evaluate 

# the string representation. 

from sage.misc.sage_eval import sage_eval 

if isinstance(x, six.string_types): 

try: 

return sage_eval(x, self.gens_dict()) 

except Exception: 

raise ValueError("Can't convert %s into an element of %s" % (x, self)) 

 

if isinstance(parent(x), InfinitePolynomialRing_sparse): 

# the easy case - parent == self - is already past 

if x.parent() is self._base: # another easy case 

return InfinitePolynomial(self,x) 

xmaxind = x.max_index() # save for later 

x = x._p 

else: 

xmaxind = -1 

 

# Now, we focus on the underlying classical polynomial ring. 

# First, try interpretation in the base ring. 

try: 

from sage.rings.polynomial.multi_polynomial_ring import MPolynomialRing_polydict 

if isinstance(self._base, MPolynomialRing_polydict): 

x = sage_eval(repr(), next(self.gens_dict())) 

else: 

x = self._base(x) 

# remark: Conversion to self._P (if applicable) 

# is done in InfinitePolynomial() 

return InfinitePolynomial(self, x) 

except Exception: 

pass 

 

# By now, we can assume that x has a parent, because 

# types like int have already been done in the previous step; 

# and also it is not an InfinitePolynomial. 

# If it isn't a polynomial (duck typing: we need 

# the variables attribute), we fall back to using strings 

if not hasattr(x,'variables'): 

try: 

return sage_eval(repr(x), self.gens_dict()) 

except Exception: 

raise ValueError("Can't convert %s into an element of %s" % (x, self)) 

 

# direct conversion will only be used if the underlying polynomials are libsingular. 

from sage.rings.polynomial.multi_polynomial_libsingular import MPolynomial_libsingular, MPolynomialRing_libsingular 

# try interpretation in self._P, if we have a dense implementation 

if hasattr(self,'_P'): 

if x.parent() is self._P: 

return InfinitePolynomial(self,x) 

# It's a shame to use sage_eval. However, it's even more of a shame 

# that MPolynomialRing_polydict doesn't work in complicated settings. 

# So, if self._P is libsingular (and this will be the case in many 

# applications!), we do it "nicely". Otherwise, we have to use sage_eval. 

if isinstance(x, MPolynomial_libsingular) and isinstance(self._P,MPolynomialRing_libsingular): 

if xmaxind == -1: # Otherwise, x has been an InfinitePolynomial 

# We infer the correct variable shift. 

# Note: Since we are in the "libsingular" case, there are 

# no further "variables" hidden in the base ring of x.parent() 

try: 

VarList = [repr(v) for v in x.variables()] 

# since interpretation in base ring 

# was impossible, it *must* have 

# variables 

# This tests admissibility on the fly: 

VarList.sort(key=self.varname_key, reverse=True) 

except ValueError: 

raise ValueError("Can't convert %s into an element of %s - variables aren't admissible"%(x,self)) 

xmaxind = max([int(v.split('_')[1]) for v in VarList]) 

try: 

# Apparently, in libsingular, the polynomial conversion is not done by 

# name but by position, if the number of variables in the parents coincide. 

# So, we shift self._P to achieve xmaxind, and if the number of variables is 

# the same then we shift further. We then *must* be 

# able to convert x into self._P, or conversion to self is 

# impossible (and will be done in InfinitePolynomial(...) 

if self._max < xmaxind: 

self.gen()[xmaxind] 

if self._P.ngens() == x.parent().ngens(): 

self.gen()[self._max+1] 

# conversion to self._P will be done in InfinitePolynomial.__init__ 

return InfinitePolynomial(self, x) 

except (ValueError, TypeError, NameError): 

raise ValueError("Can't convert %s (from %s, but variables %s) into an element of %s - no conversion into underlying polynomial ring %s"%(x,x.parent(),x.variables(),self,self._P)) 

# By now, x or self._P are not libsingular. Since MPolynomialRing_polydict 

# is too buggy, we use string evaluation 

try: 

return sage_eval(repr(x), self.gens_dict()) 

except (ValueError, TypeError, NameError): 

raise ValueError("Can't convert %s into an element of %s - no conversion into underlying polynomial ring"%(x,self)) 

 

# By now, we are in the sparse case. 

try: 

VarList = [repr(v) for v in x.variables()] 

# since interpretation in base ring 

# was impossible, it *must* have 

# variables 

# This tests admissibility on the fly: 

VarList.sort(key=self.varname_key, reverse=True) 

except ValueError: 

raise ValueError("Can't convert %s into an element of %s - variables aren't admissible"%(x,self)) 

 

if len(VarList)==1: 

# univariate polynomial rings are crab. So, make up another variable. 

if VarList[0]==self._names[0]+'_0': 

VarList.append(self._names[0]+'_1') 

else: 

VarList.append(self._names[0]+'_0') 

# We ensure that polynomial conversion is done by names; 

# the problem is that it is done by names if the number of variables coincides. 

if len(VarList)==x.parent().ngens(): 

BigList = x.parent().variable_names() 

ind = 2 

while self._names[0]+'_'+str(ind) in BigList: 

ind+=1 

VarList.append(self._names[0]+'_'+str(ind)) 

try: 

VarList.sort(key=self.varname_key, reverse=True) 

except ValueError: 

raise ValueError("Can't convert %s into an element of %s; the variables aren't admissible"%(x,self)) 

 

from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing 

R = PolynomialRing(self._base, VarList, order=self._order) 

if isinstance(R, MPolynomialRing_libsingular) and isinstance(x,MPolynomial_libsingular): # everything else is so buggy that it's even not worth to try. 

try: 

# Problem: If there is only a partial overlap in the variables 

# of x.parent() and R, then R(x) raises an error (which, I think, 

# is a bug, since we talk here about conversion, not coercion). 

# Hence, for being on the safe side, we coerce into a pushout ring: 

x = R(1)*x 

return InfinitePolynomial(self,x) 

except Exception: 

# OK, last resort, to be on the safe side 

try: 

return sage_eval(repr(x), self.gens_dict()) 

except (ValueError,TypeError,NameError): 

raise ValueError("Can't convert %s into an element of %s; conversion of the underlying polynomial failed"%(x,self)) 

else: 

try: 

return sage_eval(repr(x), self.gens_dict()) 

except (ValueError,TypeError,NameError): 

raise ValueError("Can't convert %s into an element of %s"%(x,self)) 

 

def tensor_with_ring(self, R): 

""" 

Return the tensor product of ``self`` with another ring. 

 

INPUT: 

 

``R`` - a ring. 

 

OUTPUT: 

 

An infinite polynomial ring that, mathematically, can be seen as the 

tensor product of ``self`` with ``R``. 

 

NOTE: 

 

It is required that the underlying ring of self coerces into ``R``. 

Hence, the tensor product is in fact merely an extension of the base 

ring. 

 

EXAMPLES:: 

 

sage: R.<a,b> = InfinitePolynomialRing(ZZ) 

sage: R.tensor_with_ring(QQ) 

Infinite polynomial ring in a, b over Rational Field 

sage: R 

Infinite polynomial ring in a, b over Integer Ring 

 

The following tests against a bug that was fixed at :trac:`10468`:: 

 

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

sage: R.tensor_with_ring(QQ) is R 

True 

""" 

if not R.has_coerce_map_from(self._underlying_ring): 

raise TypeError("We can't tensor with "+repr(R)) 

B = self.base_ring() 

if hasattr(B,'tensor_with_ring'): 

return InfinitePolynomialRing(B.tensor_with_ring(R), self._names, self._order, implementation='sparse') 

if hasattr(B,'change_ring'): # e.g., polynomial rings 

return InfinitePolynomialRing(B.change_ring(R), self._names, self._order, implementation='sparse') 

# try to find the correct base ring in other ways: 

try: 

o = B.one()*R.one() 

except Exception: 

raise TypeError("We can't tensor with "+repr(R)) 

return InfinitePolynomialRing(o.parent(), self._names, self._order, implementation='sparse') 

 

## Basic Ring Properties 

# -- some stuff that is useful for quotient rings etc. 

def is_noetherian(self): 

""" 

Since Infinite Polynomial Rings must have at least one 

generator, they have infinitely many variables and are thus 

not noetherian, as a ring. 

 

NOTE: 

 

Infinite Polynomial Rings over a field `F` are noetherian as 

`F(G)` modules, where `G` is the symmetric group of the 

natural numbers. But this is not what the method 

``is_noetherian()`` is answering. 

 

TESTS:: 

 

sage: R = InfinitePolynomialRing(GF(2)) 

sage: R 

Infinite polynomial ring in x over Finite Field of size 2 

sage: R.is_noetherian() 

False 

 

""" 

return False 

 

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

""" 

Return ``False``: Since Infinite Polynomial Rings must have at 

least one generator, they have infinitely many variables and thus 

never are fields. 

 

EXAMPLES:: 

 

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

sage: R.is_field() 

False 

 

 

TESTS:: 

 

sage: R = InfinitePolynomialRing(GF(2)) 

sage: R 

Infinite polynomial ring in x over Finite Field of size 2 

sage: R.is_field() 

False 

 

:trac:`9443`:: 

 

sage: W = PowerSeriesRing(InfinitePolynomialRing(QQ,'a'),'x') 

sage: W.is_field() 

False 

 

 

""" 

return False 

 

def varname_key(self, x): 

""" 

Key for comparison of variable names. 

 

INPUT: 

 

``x`` -- a string of the form ``a+'_'+str(n)``, where a is the 

name of a generator, and n is an integer 

 

RETURN: 

 

a key used to sort the variables 

 

THEORY: 

 

The order is defined as follows: 

 

x<y `\\iff` the string ``x.split('_')[0]`` is later in the list of 

generator names of self than ``y.split('_')[0]``, or 

(``x.split('_')[0]==y.split('_')[0]`` and 

``int(x.split('_')[1])<int(y.split('_')[1])``) 

 

EXAMPLES:: 

 

sage: X.<alpha,beta> = InfinitePolynomialRing(ZZ) 

sage: X.varname_key('alpha_1') 

(0, 1) 

sage: X.varname_key('beta_10') 

(-1, 10) 

sage: X.varname_key('beta_1') 

(-1, 1) 

sage: X.varname_key('alpha_10') 

(0, 10) 

sage: X.varname_key('alpha_1') 

(0, 1) 

sage: X.varname_key('alpha_10') 

(0, 10) 

""" 

try: 

return self._identify_variable(*x.split('_', 1)) 

except (KeyError, ValueError, TypeError): 

raise ValueError("%s is not a valid variable name" % x) 

 

def ngens(self): 

""" 

Returns the number of generators for this ring. Since there 

are countably infinitely many variables in this polynomial 

ring, by 'generators' we mean the number of infinite families 

of variables. See :mod:`~sage.rings.polynomial.infinite_polynomial_ring` 

for more details. 

 

EXAMPLES:: 

 

sage: X.<x> = InfinitePolynomialRing(ZZ) 

sage: X.ngens() 

1 

 

sage: X.<x1,x2> = InfinitePolynomialRing(QQ) 

sage: X.ngens() 

2 

 

""" 

return len(self._names) 

 

@cached_method 

def gen(self, i=None): 

""" 

Returns the `i^{th}` 'generator' (see the description in :meth:`.ngens`) 

of this infinite polynomial ring. 

 

EXAMPLES:: 

 

sage: X = InfinitePolynomialRing(QQ) 

sage: x = X.gen() 

sage: x[1] 

x_1 

sage: X.gen() is X.gen(0) 

True 

sage: XX = InfinitePolynomialRing(GF(5)) 

sage: XX.gen(0) is XX.gen() 

True 

 

""" 

if i > len(self._names): 

raise ValueError 

j = i if i is not None else 0 

res = InfinitePolynomialGen(self, self._names[j]) 

if i is None: 

key = ((0,), ()) 

if key in self._cache__gen: 

return self._cache__gen[key] 

else: 

self._cache__gen[key] = res 

return res 

 

def _first_ngens(self, n): 

""" 

Used by the preparser for R.<x> = ... 

 

EXAMPLES:: 

 

sage: InfinitePolynomialRing(ZZ, 'a')._first_ngens(1) 

(a_*,) 

""" 

# It may be that we merge variables. If this is the case, 

# the new variables (as used by R.<x> = ...) come *last*, 

# but in order. 

return self.gens()[-n:] 

 

@cached_method 

def gens_dict(self): 

""" 

Return a dictionary-like object containing the infinitely many 

``{var_name:variable}`` pairs. 

 

EXAMPLES:: 

 

sage: R = InfinitePolynomialRing(ZZ, 'a') 

sage: D = R.gens_dict() 

sage: D 

GenDict of Infinite polynomial ring in a over Integer Ring 

sage: D['a_5'] 

a_5 

""" 

return GenDictWithBasering(self, InfiniteGenDict(self.gens())) 

 

def _ideal_class_(self, n=0): 

""" 

Return :class:`SymmetricIdeals` (see there for further details). 

 

TESTS:: 

 

sage: R.<a,b> = InfinitePolynomialRing(ZZ) 

sage: R._ideal_class_() 

<class 'sage.rings.polynomial.symmetric_ideal.SymmetricIdeal'> 

 

""" 

import sage.rings.polynomial.symmetric_ideal 

return sage.rings.polynomial.symmetric_ideal.SymmetricIdeal 

 

def characteristic(self): 

""" 

Return the characteristic of the base field. 

 

EXAMPLES:: 

 

sage: X.<x,y> = InfinitePolynomialRing(GF(25,'a')) 

sage: X 

Infinite polynomial ring in x, y over Finite Field in a of size 5^2 

sage: X.characteristic() 

5 

 

""" 

return self._base.characteristic() 

 

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

""" 

An infinite polynomial ring is an integral domain if and only if 

the base ring is. Arguments are passed to is_integral_domain 

method of base ring. 

 

EXAMPLES:: 

 

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

sage: R.is_integral_domain() 

True 

 

TESTS: 

 

:trac:`9443`:: 

 

sage: W = PolynomialRing(InfinitePolynomialRing(QQ,'a'),2,'x,y') 

sage: W.is_integral_domain() 

True 

""" 

return self._base.is_integral_domain(*args, **kwds) 

 

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

""" 

Return ``False``, since polynomial rings in infinitely many 

variables are never Noetherian rings. 

 

Note, however, that they are noetherian modules over the group 

ring of the symmetric group of the natural numbers 

 

EXAMPLES:: 

 

sage: R.<x> = InfinitePolynomialRing(QQ) 

sage: R.is_noetherian() 

False 

 

""" 

return False 

 

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

""" 

Return ``Infinity``, since polynomial rings in infinitely many 

variables have infinite Krull dimension. 

 

EXAMPLES:: 

 

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

sage: R.krull_dimension() 

+Infinity 

""" 

from sage.rings.all import Infinity 

return Infinity 

 

def order(self): 

""" 

Return ``Infinity``, since polynomial rings have infinitely 

many elements. 

 

EXAMPLES:: 

 

sage: R.<x> = InfinitePolynomialRing(GF(2)) 

sage: R.order() 

+Infinity 

""" 

from sage.rings.all import Infinity 

return Infinity 

 

 

class InfinitePolynomialGen(SageObject): 

""" 

This class provides the object which is responsible for returning 

variables in an infinite polynomial ring (implemented in 

:meth:`.__getitem__`). 

 

EXAMPLES:: 

 

sage: X.<x1,x2> = InfinitePolynomialRing(RR) 

sage: x1 

x1_* 

sage: x1[5] 

x1_5 

sage: x1 == loads(dumps(x1)) 

True 

 

""" 

 

def __init__(self, parent, name): 

""" 

EXAMPLES:: 

 

sage: X.<x> = InfinitePolynomialRing(QQ) 

sage: loads(dumps(x)) 

x_* 

 

""" 

self._name = name 

self._parent = parent 

self._output = {} 

 

def __eq__(self, other): 

""" 

Check whether ``self`` is equal to ``other``. 

 

EXAMPLES:: 

 

sage: X.<x,y> = InfinitePolynomialRing(QQ) 

sage: from sage.rings.polynomial.infinite_polynomial_ring import InfinitePolynomialGen 

sage: x2 = InfinitePolynomialGen(X, 'x') 

sage: x2 == x 

True 

""" 

if not isinstance(other, InfinitePolynomialGen): 

return False 

return (self._name, self._parent) == (other._name, other._parent) 

 

def __ne__(self, other): 

""" 

Check whether ``self`` is not equal to ``other``. 

 

EXAMPLES:: 

 

sage: X.<x,y> = InfinitePolynomialRing(QQ) 

sage: from sage.rings.polynomial.infinite_polynomial_ring import InfinitePolynomialGen 

sage: x2 = InfinitePolynomialGen(X, 'x') 

sage: x2 != x 

False 

""" 

return not (self == other) 

 

def _latex_(self): 

r""" 

EXAMPLES:: 

 

sage: from sage.misc.latex import latex 

sage: X.<x,x1,xx> = InfinitePolynomialRing(QQ) 

sage: latex(x) # indirect doctest 

x_{\ast} 

sage: latex(x1) # indirect doctest 

\mathit{x1}_{\ast} 

sage: latex(xx) # indirect doctest 

\mathit{xx}_{\ast} 

sage: latex(x[2]) # indirect doctest 

x_{2} 

sage: latex(x1[3]) # indirect doctest 

\mathit{x1}_{3} 

""" 

from sage.misc.latex import latex_variable_name 

return latex_variable_name(self._name + '_ast') 

 

def __getitem__(self, i): 

""" 

Return the variable ``x[i]`` where ``x`` is this 

:class:`sage.rings.polynomial.infinite_polynomial_ring.InfinitePolynomialGen`, 

and i is a non-negative integer. 

 

EXAMPLES:: 

 

sage: X.<alpha> = InfinitePolynomialRing(QQ) 

sage: alpha[1] 

alpha_1 

""" 

if int(i) != i: 

raise ValueError("The index (= %s) must be an integer" % i) 

i = int(i) 

if i < 0: 

raise ValueError("The index (= %s) must be non-negative" % i) 

P = self._parent 

from sage.rings.polynomial.infinite_polynomial_element import InfinitePolynomial_dense, InfinitePolynomial_sparse 

from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing 

OUT = self._output.get(i) 

if hasattr(P,'_P'): 

if i <= P._max: 

#return InfinitePolynomial_dense(P, P._P.gen(P._P.variable_names().index(self._name+'_'+str(i)))) 

if OUT is None: 

self._output[i] = InfinitePolynomial_dense(P, P._P.gen(P._P.variable_names().index(self._name+'_'+str(i)))) 

else: 

if OUT._p.parent() is not P._P: 

OUT._p = P._P(OUT._p) 

return self._output[i] 

#Calculate all of the new names needed 

try: 

names = [ [name+'_'+str(j) for name in P._names] for j in range(i+1)] 

except OverflowError: 

raise IndexError("Variable index is too big - consider using the sparse implementation") 

names = reduce(operator.add, names) 

names.sort(key=P.varname_key, reverse=True) 

#Create the new polynomial ring 

P._P = PolynomialRing(P.base_ring(), names, order = P._order) 

##Get the generators 

P._max = i 

#return InfinitePolynomial_dense(P, P._P.gen(P._P.variable_names().index(self._name+'_'+str(i)))) 

self._output[i] = InfinitePolynomial_dense(P, P._P.gen(P._P.variable_names().index(self._name+'_'+str(i)))) 

return self._output[i] 

# Now, we are in the sparse implementation 

if OUT is not None: # in the sparse implementation, this is ok 

return OUT 

if i==0: 

names = [self._name+'_0',self._name+'_1'] 

else: 

names = [self._name+'_0',self._name+'_'+str(i)] 

names.sort(key=P.varname_key, reverse=True) 

Pol = PolynomialRing(P.base_ring(), names, order=P._order) 

#return InfinitePolynomial_sparse(P, Pol.gen(names.index(self._name+'_'+str(i)))) 

self._output[i] = InfinitePolynomial_sparse(P, Pol.gen(names.index(self._name+'_'+str(i)))) 

return self._output[i] 

 

def _repr_(self): 

""" 

EXAMPLES:: 

 

sage: X.<x,y> = InfinitePolynomialRing(QQ) 

sage: x # indirect doctest 

x_* 

 

""" 

return self._name+'_*' 

 

def __str__(self): 

""" 

EXAMPLES:: 

 

sage: X.<x,y> = InfinitePolynomialRing(QQ) 

sage: print(x) # indirect doctest 

Generator for the x's in Infinite polynomial ring in x, y over Rational Field 

 

""" 

return "Generator for the %s's in %s"%(self._name, self._parent) 

 

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

## The dense implementation 

 

class InfinitePolynomialRing_dense(InfinitePolynomialRing_sparse): 

""" 

Dense implementation of Infinite Polynomial Rings 

 

Compared with :class:`~sage.rings.polynomial.infinite_polynomial_ring.InfinitePolynomialRing_sparse`, 

from which this class inherits, it keeps a polynomial ring that comprises all elements that have 

been created so far. 

""" 

def __init__(self, R, names, order): 

""" 

EXAMPLES:: 

 

sage: X.<x2,alpha,y4> = InfinitePolynomialRing(ZZ, implementation='dense') 

sage: X == loads(dumps(X)) 

True 

 

""" 

if not names: 

names = ['x'] 

#Generate the initial polynomial ring 

self._max = 0 

InfinitePolynomialRing_sparse.__init__(self, R, names, order) 

self._P = self._minP 

#self._pgens = self._P.gens() 

 

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

## Coercion 

 

def construction(self): 

""" 

Return the construction of ``self``. 

 

OUTPUT: 

 

A pair ``F,R``, where ``F`` is a construction functor and ``R`` is a ring, 

so that ``F(R) is self``. 

 

EXAMPLES:: 

 

sage: R.<x,y> = InfinitePolynomialRing(GF(5)) 

sage: R.construction() 

[InfPoly{[x,y], "lex", "dense"}, Finite Field of size 5] 

""" 

return [InfinitePolynomialFunctor(self._names, self._order, 'dense'), self._base] 

 

def tensor_with_ring(self, R): 

""" 

Return the tensor product of ``self`` with another ring. 

 

INPUT: 

 

``R`` - a ring. 

 

OUTPUT: 

 

An infinite polynomial ring that, mathematically, can be seen as the 

tensor product of ``self`` with ``R``. 

 

NOTE: 

 

It is required that the underlying ring of self coerces into ``R``. 

Hence, the tensor product is in fact merely an extension of the base 

ring. 

 

EXAMPLES:: 

 

sage: R.<a,b> = InfinitePolynomialRing(ZZ, implementation='sparse') 

sage: R.tensor_with_ring(QQ) 

Infinite polynomial ring in a, b over Rational Field 

sage: R 

Infinite polynomial ring in a, b over Integer Ring 

 

The following tests against a bug that was fixed at :trac:`10468`:: 

 

sage: R.<x,y> = InfinitePolynomialRing(QQ, implementation='sparse') 

sage: R.tensor_with_ring(QQ) is R 

True 

 

""" 

if not R.has_coerce_map_from(self._underlying_ring): 

raise TypeError("We can't tensor with "+repr(R)) 

B = self.base_ring() 

if hasattr(B,'tensor_with_ring'): 

return InfinitePolynomialRing(B.tensor_with_ring(R), self._names, self._order, implementation='dense') 

if hasattr(B,'change_ring'): # e.g., polynomial rings 

return InfinitePolynomialRing(B.change_ring(R), self._names, self._order, implementation='dense') 

# try to find the correct base ring in other ways: 

try: 

o = B.one()*R.one() 

except Exception: 

raise TypeError("We can't tensor with "+repr(R)) 

return InfinitePolynomialRing(o.parent(), self._names, self._order, implementation='dense') 

 

def polynomial_ring(self): 

""" 

Returns the underlying *finite* polynomial ring. 

 

.. note:: 

 

The ring returned can change over time as more variables 

are used. 

 

Since the rings are cached, we create here a ring with variable 

names that do not occur in other doc tests, so that we avoid 

side effects. 

 

EXAMPLES:: 

 

sage: X.<xx, yy> = InfinitePolynomialRing(ZZ) 

sage: X.polynomial_ring() 

Multivariate Polynomial Ring in xx_0, yy_0 over Integer Ring 

sage: a = yy[3] 

sage: X.polynomial_ring() 

Multivariate Polynomial Ring in xx_3, xx_2, xx_1, xx_0, yy_3, yy_2, yy_1, yy_0 over Integer Ring 

 

""" 

return self._P