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

""" 

PolyDict engine for generic multivariate polynomial rings 

  

This module provides an implementation of the underlying arithmetic for 

multi-variate polynomial rings using Python dicts. 

  

This class is not meant for end users, but instead for implementing 

multivariate polynomial rings over a completely general base. It does 

not do strong type checking or have parents, etc. For speed, it has been 

implemented in Cython. 

  

The functions in this file use the 'dictionary representation' of multivariate 

polynomials 

  

``{(e1,...,er):c1,...} <-> c1*x1^e1*...*xr^er+...``, 

  

which we call a polydict. The exponent tuple ``(e1,...,er)`` in this 

representation is an instance of the class :class:`ETuple`. This class behaves 

like a normal Python tuple but also offers advanced access methods for sparse 

monomials like positions of non-zero exponents etc. 

  

AUTHORS: 

  

- William Stein 

- David Joyner 

- Martin Albrecht (ETuple) 

- Joel B. Mohler (2008-03-17) -- ETuple rewrite as sparse C array 

""" 

  

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

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

# 

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

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

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

# (at your option) any later version. 

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

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

from __future__ import print_function, absolute_import 

  

from libc.string cimport memcpy 

from cpython.dict cimport * 

from cpython.object cimport (PyObject_RichCompare, Py_EQ, Py_NE, 

Py_LT, Py_LE, Py_GT, Py_GE) 

from cysignals.memory cimport sig_malloc, sig_free 

  

import copy 

from functools import reduce 

from sage.arith.power import generic_power 

from pprint import pformat 

  

from sage.misc.misc import cputime 

from sage.misc.latex import latex 

from sage.misc.superseded import deprecation 

  

  

cdef class PolyDict: 

def __init__(PolyDict self, pdict, zero=0, remove_zero=False, force_int_exponents=True, force_etuples=True): 

""" 

INPUT: 

  

- ``pdict`` -- list, which represents a multi-variable polynomial with 

the distribute representation (a copy is not made) 

  

- ``zero`` -- (optional) zero in the base ring 

  

- ``force_int_exponents`` -- bool (optional) arithmetic with int 

exponents is much faster than some of the alternatives, so this is 

True by default. 

  

- ``force_etuples`` -- bool (optional) enforce that the exponent tuples 

are instances of ETuple class 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: PolyDict({(2,3):2, (1,2):3, (2,1):4}) 

PolyDict with representation {(1, 2): 3, (2, 1): 4, (2, 3): 2} 

  

# I've removed fractional exponent support in ETuple when moving to a sparse C integer array 

#sage: PolyDict({(2/3,3,5):2, (1,2,1):3, (2,1):4}, force_int_exponents=False) 

#PolyDict with representation {(2, 1): 4, (1, 2, 1): 3, (2/3, 3, 5): 2} 

  

sage: PolyDict({(2,3):0, (1,2):3, (2,1):4}, remove_zero=True) 

PolyDict with representation {(1, 2): 3, (2, 1): 4} 

  

sage: PolyDict({(0,0):RIF(-1,1)}, remove_zero=True) 

PolyDict with representation {(0, 0): 0.?} 

""" 

if not isinstance(pdict, dict): 

if isinstance(pdict, list): 

v = {} 

for w in pdict: 

if w[0] != 0: 

v[ETuple(w[1])] = w[0] 

remove_zero = False 

pdict = v 

else: 

raise TypeError("pdict must be a list.") 

  

if isinstance(pdict, dict) and force_etuples is True: 

pdict2 = [] 

for k, v in pdict.iteritems(): 

pdict2.append((ETuple(k), v)) 

  

pdict = dict(pdict2) 

  

if force_int_exponents: 

new_pdict = {} 

if remove_zero: 

for k, c in pdict.iteritems(): 

if not c == zero: 

new_pdict[ETuple(list(map(int, k)))] = c 

else: 

for k, c in pdict.iteritems(): 

new_pdict[ETuple(list(map(int, k)))] = c 

pdict = new_pdict 

else: 

if remove_zero: 

for k in pdict.keys(): 

if pdict[k] == zero: 

del pdict[k] 

self.__repn = pdict 

self.__zero = zero 

  

def __hash__(self): 

""" 

Return the hash. 

  

The hash of two PolyDicts is the same whether or not they use ETuples 

for their keys since that's just an implementation detail. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: PD1 = PolyDict({(2,3):0, (1,2):3, (2,1):4}) 

sage: PD2 = PolyDict({(2,3):0, (1,2):3, (2,1):4}, remove_zero=True) 

sage: PD3 = PolyDict({(2,3):0, (1,2):3, (2,1):4}, 

....: force_etuples=False, force_int_exponents=False) 

sage: PD4 = PolyDict({(2,3):0, (1,2):3, (2,1):4}, zero=4) 

sage: hash(PD1) == hash(PD2) 

False 

sage: hash(PD1) == hash(PolyDict({(2,3):0, (1,2):3, (2,1):4})) 

True 

sage: hash(PD1) == hash(PD3) 

True 

sage: hash(PD3) == hash(PolyDict({(2,3):0, (1,2):3, (2,1):4}, 

....: force_etuples=False)) 

True 

sage: hash(PD1) == hash(PD4) 

False 

sage: hash(PD4) == hash(PolyDict({(2,3):0, (1,2):3, (2,1):4}, 

....: zero=4)) 

True 

""" 

  

repn = frozenset((tuple(key), val) for key, val in self.__repn.items()) 

return hash((type(self), repn, self.__zero)) 

  

def __richcmp__(PolyDict self, PolyDict right, int op): 

return PyObject_RichCompare(self.__repn, right.__repn, op) 

  

def compare(PolyDict self, PolyDict other, key=None): 

if key is not None: 

# start with biggest 

left = iter(sorted(self.__repn, key=key, reverse=True)) 

right = iter(sorted(other.__repn, key=key, reverse=True)) 

else: 

# in despair, do that 

raise ValueError('no key provided') 

  

for m in left: 

try: 

n = next(right) 

except StopIteration: 

return 1 # left has terms, right does not 

  

# first compare the leading monomials 

keym = key(m) 

keyn = key(n) 

if keym > keyn: 

return 1 

elif keym < keyn: 

return -1 

  

# same leading monomial, compare their coefficients 

coefm = self.__repn[m] 

coefn = other.__repn[n] 

if coefm > coefn: 

return 1 

elif coefm < coefn: 

return -1 

  

# try next pair 

try: 

n = next(right) 

return -1 # right has terms, left does not 

except StopIteration: 

return 0 # both have no terms 

  

def list(PolyDict self): 

""" 

Return a list that defines ``self``. It is safe to change this. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: f = PolyDict({(2,3):2, (1,2):3, (2,1):4}) 

sage: f.list() 

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

""" 

ret = [] 

for e, c in self.__repn.iteritems(): 

ret.append([c, list(e)]) 

return ret 

  

def dict(PolyDict self): 

""" 

Return a copy of the dict that defines self. It is 

safe to change this. For a reference, use dictref. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: f = PolyDict({(2,3):2, (1,2):3, (2,1):4}) 

sage: f.dict() 

{(1, 2): 3, (2, 1): 4, (2, 3): 2} 

""" 

return self.__repn.copy() 

  

def coefficients(PolyDict self): 

""" 

Return the coefficients of self. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: f = PolyDict({(2,3):2, (1,2):3, (2,1):4}) 

sage: f.coefficients() 

[3, 2, 4] 

""" 

return self.__repn.values() 

  

def exponents(PolyDict self): 

""" 

Return the exponents of self. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: f = PolyDict({(2,3):2, (1,2):3, (2,1):4}) 

sage: f.exponents() 

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

""" 

return self.__repn.keys() 

  

def __len__(PolyDict self): 

""" 

Return the number of terms of the polynomial. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: f = PolyDict({(2,3):2, (1,2):3, (2,1):4}) 

sage: len(f) 

3 

""" 

return len(self.__repn) 

  

def __getitem__(PolyDict self, e): 

""" 

Return a coefficient of the polynomial. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: f = PolyDict({(2,3):2, (1,2):3, (2,1):4}) 

sage: f[1,2] 

3 

sage: f[(2,1)] 

4 

""" 

return self.__repn[ETuple(e)] 

  

def __repr__(PolyDict self): 

repn = ' '.join(pformat(self.__repn).splitlines()) 

return 'PolyDict with representation %s' % repn 

  

def degree(PolyDict self, PolyDict x=None): 

if x is None: 

return self.total_degree() 

L = x.__repn.keys() 

if len(L) != 1: 

raise TypeError("x must be one of the generators of the parent.") 

L = L[0] 

nonzero_positions = L.nonzero_positions() 

if len(nonzero_positions) != 1: 

raise TypeError("x must be one of the generators of the parent.") 

i = nonzero_positions[0] 

if L[i] != 1: 

raise TypeError("x must be one of the generators of the parent.") 

_max = [] 

for v in self.__repn.keys(): 

_max.append(v[i]) 

return max(_max or [-1]) 

  

def valuation(PolyDict self, PolyDict x=None): 

L = x.__repn.keys() 

if x is None: 

_min = [] 

negative = False 

for k in self.__repn.keys(): 

_sum = 0 

for m in self.__repn[k].nonzero_values(sort=False): 

if m < 0: 

negative = True 

break 

_sum += m 

if negative: 

break 

_min.append(_sum) 

else: 

return min(_min) 

for k in self.__repn.keys(): 

_min.append(sum(m for m in self.__repn[k].nonzero_values(sort=False) if m < 0)) 

return min(_min) 

L = x.__repn.keys() 

if len(L) != 1: 

raise TypeError("x must be one of the generators of the parent.") 

L = L[0] 

nonzero_positions = L.nonzero_positions() 

if len(nonzero_positions) != 1: 

raise TypeError("x must be one of the generators of the parent.") 

i = nonzero_positions[0] 

if L[i] != 1: 

raise TypeError("x must be one of the generators of the parent.") 

_min = [] 

for v in self.__repn.keys(): 

_min.append(v[i]) 

return min(_min) 

  

def total_degree(PolyDict self): 

return max([-1] + [sum(k) for k in self.__repn.keys()]) 

  

def monomial_coefficient(PolyDict self, mon): 

""" 

INPUT: 

  

a PolyDict with a single key 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: f = PolyDict({(2,3):2, (1,2):3, (2,1):4}) 

sage: f.monomial_coefficient(PolyDict({(2,1):1}).dict()) 

4 

""" 

K, = mon.keys() 

if K not in self.__repn: 

return 0 

return self.__repn[K] 

  

def polynomial_coefficient(PolyDict self, degrees): 

""" 

Return a polydict that defines the coefficient in the current 

polynomial viewed as a tower of polynomial extensions. 

  

INPUT: 

  

- ``degrees`` -- a list of degree restrictions; list elements are None 

if the variable in that position should be unrestricted 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: f = PolyDict({(2,3):2, (1,2):3, (2,1):4}) 

sage: f.polynomial_coefficient([2,None]) 

PolyDict with representation {(0, 1): 4, (0, 3): 2} 

sage: f = PolyDict({(0,3):2, (0,2):3, (2,1):4}) 

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

PolyDict with representation {(0, 2): 3, (0, 3): 2} 

""" 

nz = [] 

cdef int i 

for i from 0<=i<len(degrees): 

if degrees[i] is not None: 

nz.append(i) 

ans = {} 

for S in self.__repn.keys(): 

exactly_divides = True 

for j in nz: 

if S[j] != degrees[j]: 

exactly_divides = False 

break 

if exactly_divides: 

t = list(S) 

for m in nz: 

t[m] = 0 

ans[ETuple(t)] = self.__repn[S] 

return PolyDict(ans, force_etuples=False) 

  

def coefficient(PolyDict self, mon): 

""" 

Return a polydict that defines a polynomial in 1 less number 

of variables that gives the coefficient of mon in this 

polynomial. 

  

The coefficient is defined as follows. If f is this 

polynomial, then the coefficient is the sum T/mon where the 

sum is over terms T in f that are exactly divisible by mon. 

""" 

K, = mon.keys() 

nz = K.nonzero_positions() # set([i for i in range(len(K)) if K[i] != 0]) 

ans = {} 

for S in self.__repn.keys(): 

exactly_divides = True 

for j in nz: 

if S[j] != K[j]: 

exactly_divides = False 

break 

if exactly_divides: 

t = list(S) 

for m in nz: 

t[m] = 0 

ans[ETuple(t)] = self.__repn[S] 

return PolyDict(ans, force_etuples=False) 

  

def is_homogeneous(PolyDict self): 

K = self.__repn.keys() 

if len(K) == 0: 

return True 

# A polynomial is homogeneous if the number of different 

# exponent sums is at most 1. 

return len(set(map(sum, K))) <= 1 

  

def homogenize(PolyDict self, var): 

R = self.__repn 

H = {} 

deg = self.degree() 

for e, val in R.iteritems(): 

i = deg - sum(e) 

f = list(e) 

f[var] += i 

H[ETuple(f)] = val 

return PolyDict(H, zero=self.__zero, force_etuples=False) 

  

def latex(PolyDict self, vars, atomic_exponents=True, 

atomic_coefficients=True, sortkey=None): 

r""" 

Return a nice polynomial latex representation of this PolyDict, where 

the vars are substituted in. 

  

INPUT: 

  

- ``vars`` -- list 

- ``atomic_exponents`` -- bool (default: ``True``) 

- ``atomic_coefficients`` -- bool (default: ``True``) 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: f = PolyDict({(2,3):2, (1,2):3, (2,1):4}) 

sage: f.latex(['a', 'WW']) 

'2 a^{2} WW^{3} + 4 a^{2} WW + 3 a WW^{2}' 

  

When ``atomic_exponents`` is False, the exponents are surrounded in 

parenthesis, since ^ has such high precedence:: 

  

# I've removed fractional exponent support in ETuple when moving to a sparse C integer array 

#sage: f = PolyDict({(2/3,3,5):2, (1,2,1):3, (2,1,1):4}, force_int_exponents=False) 

#sage: f.latex(['a', 'b', 'c'], atomic_exponents=False) 

#'4 a^{2}bc + 3 ab^{2}c + 2 a^{2/3}b^{3}c^{5}' 

  

TESTS: 

  

We check that the issue on :trac:`9478` is resolved:: 

  

sage: R2.<a> = QQ[] 

sage: R3.<xi, x> = R2[] 

sage: print(latex(xi*x)) 

\xi x 

""" 

n = len(vars) 

poly = "" 

E = list(self.__repn) 

if sortkey: 

E.sort(key=sortkey, reverse=True) 

else: 

E.sort(reverse=True) 

try: 

pos_one = self.__zero.parent()(1) 

neg_one = -pos_one 

except AttributeError: 

# probably self.__zero is not a ring element 

pos_one = 1 

neg_one = -1 

for e in E: 

c = self.__repn[e] 

if not c == self.__zero: 

sign_switch = False 

# First determine the multinomial: 

multi = " ".join([vars[j] + 

("^{%s}" % e[j] if e[j] != 1 else "") 

for j in e.nonzero_positions(sort=True)]) 

# Next determine coefficient of multinomial 

if len(multi) == 0: 

multi = latex(c) 

elif c == neg_one: 

# handle -1 specially because it's a pain 

if len(poly) > 0: 

sign_switch = True 

else: 

multi = "-%s" % multi 

elif c != pos_one: 

if not atomic_coefficients: 

c = latex(c) 

if c.find("+") != -1 or c.find("-") != -1 or c.find(" ") != -1: 

c = "(%s)" % c 

multi = "%s %s" % (c, multi) 

  

# Now add on coefficiented multinomials 

if len(poly) > 0: 

if sign_switch: 

poly = poly + " - " 

else: 

poly = poly + " + " 

poly = poly + multi 

poly = poly.lstrip().rstrip() 

poly = poly.replace("+ -", "- ") 

if len(poly) == 0: 

return "0" 

return poly 

  

def poly_repr(PolyDict self, vars, atomic_exponents=True, 

atomic_coefficients=True, sortkey=None): 

""" 

Return a nice polynomial string representation of this PolyDict, where 

the vars are substituted in. 

  

INPUT: 

  

- ``vars`` -- list 

- ``atomic_exponents`` -- bool (default: ``True``) 

- ``atomic_coefficients`` -- bool (default: ``True``) 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: f = PolyDict({(2,3):2, (1,2):3, (2,1):4}) 

sage: f.poly_repr(['a', 'WW']) 

'2*a^2*WW^3 + 4*a^2*WW + 3*a*WW^2' 

  

When atomic_exponents is ``False``, the exponents are surrounded 

in parenthesis, since ^ has such high precedence. :: 

  

# I've removed fractional exponent support in ETuple when moving to a sparse C integer array 

#sage: f = PolyDict({(2/3,3,5):2, (1,2,1):3, (2,1,1):4}, force_int_exponents=False) 

#sage: f.poly_repr(['a', 'b', 'c'], atomic_exponents=False) 

#'4*a^(2)*b*c + 3*a*b^(2)*c + 2*a^(2/3)*b^(3)*c^(5)' 

  

We check to make sure that when we are in characteristic two, we 

don't put negative signs on the generators. :: 

  

sage: Integers(2)['x, y'].gens() 

(x, y) 

  

We make sure that intervals are correctly represented. :: 

  

sage: f = PolyDict({(2,3):RIF(1/2,3/2), (1,2):RIF(-1,1)}) 

sage: f.poly_repr(['x', 'y']) 

'1.?*x^2*y^3 + 0.?*x*y^2' 

""" 

n = len(vars) 

poly = "" 

E = list(self.__repn) 

if sortkey: 

E.sort(key=sortkey, reverse=True) 

else: 

E.sort(reverse=True) 

try: 

pos_one = self.__zero.parent()(1) 

neg_one = -pos_one 

except AttributeError: 

# probably self.__zero is not a ring element 

pos_one = 1 

neg_one = -1 

  

is_characteristic_2 = bool(pos_one == neg_one) 

  

for e in E: 

c = self.__repn[e] 

if not c == self.__zero: 

sign_switch = False 

# First determine the multinomial: 

multi = "" 

for j in e.nonzero_positions(sort=True): 

if len(multi) > 0: 

multi = multi + "*" 

multi = multi + vars[j] 

if e[j] != 1: 

if atomic_exponents: 

multi = multi + "^%s" % e[j] 

else: 

multi = multi + "^(%s)" % e[j] 

# Next determine coefficient of multinomial 

if len(multi) == 0: 

multi = str(c) 

elif c == neg_one and not is_characteristic_2: 

# handle -1 specially because it's a pain 

if len(poly) > 0: 

sign_switch = True 

else: 

multi = "-%s" % multi 

elif not c == pos_one: 

if not atomic_coefficients: 

c = str(c) 

if c.find("+") != -1 or c.find("-") != -1 or c.find(" ") != -1: 

c = "(%s)" % c 

multi = "%s*%s" % (c, multi) 

  

# Now add on coefficiented multinomials 

if len(poly) > 0: 

if sign_switch: 

poly = poly + " - " 

else: 

poly = poly + " + " 

poly = poly + multi 

poly = poly.lstrip().rstrip() 

poly = poly.replace("+ -", "- ") 

if len(poly) == 0: 

return "0" 

return poly 

  

def __add__(PolyDict self, PolyDict other): 

""" 

Add two PolyDict's in the same number of variables. 

  

EXAMPLES: 

  

We add two polynomials in 2 variables:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: f = PolyDict({(2,3):2, (1,2):3, (2,1):4}) 

sage: g = PolyDict({(1,5):-3, (2,3):-2, (1,1):3}) 

sage: f + g 

PolyDict with representation {(1, 1): 3, (1, 2): 3, (1, 5): -3, (2, 1): 4} 

  

Next we add two polynomials with fractional exponents in 3 variables:: 

  

# I've removed fractional exponent support in ETuple when moving to a sparse C integer array 

#sage: f = PolyDict({(2/3,3,5):2, (1,2,1):3, (2,1,1):4}, force_int_exponents=False) 

#sage: g = PolyDict({(2/3,3,5):3}, force_int_exponents=False) 

#sage: f+g 

#PolyDict with representation {(1, 2, 1): 3, (2/3, 3, 5): 5, (2, 1, 1): 4} 

""" 

zero = self.__zero 

# D = copy.copy(self.__repn) 

D = self.__repn.copy() # faster! 

R = other.__repn 

for e, c in R.iteritems(): 

try: 

D[e] += c 

except KeyError: 

D[e] = c 

return PolyDict(D, zero=zero, remove_zero=True, 

force_int_exponents=False, force_etuples=False) 

  

def __mul__(PolyDict self, PolyDict right): 

""" 

Multiply two PolyDict's in the same number of variables. 

  

EXAMPLES: 

We multiply two polynomials in 2 variables:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: f = PolyDict({(2,3):2, (1,2):3, (2,1):4}) 

sage: g = PolyDict({(1,5):-3, (2,3):-2, (1,1):3}) 

sage: f*g 

PolyDict with representation {(2, 3): 9, (2, 7): -9, (3, 2): 12, (3, 4): 6, (3, 5): -6, (3, 6): -12, (3, 8): -6, (4, 4): -8, (4, 6): -4} 

""" 

cdef PyObject *cc 

newpoly = {} 

if len(self.__repn) == 0: # product is zero anyways 

return self 

for e0, c0 in self.__repn.iteritems(): 

for e1, c1 in right.__repn.iteritems(): 

e = (<ETuple>e0).eadd(<ETuple>e1) 

c = c0*c1 

cc = PyDict_GetItem(newpoly, e) 

if cc == <PyObject*>0: 

PyDict_SetItem(newpoly, e, c) 

else: 

PyDict_SetItem(newpoly, e, <object>cc+c) 

F = PolyDict(newpoly, self.__zero, force_int_exponents=False, remove_zero=True, force_etuples=False) 

return F 

  

def scalar_rmult(PolyDict self, s): 

""" 

Right Scalar Multiplication 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: x, y = FreeMonoid(2, 'x, y').gens() # a strange object to live in a polydict, but non-commutative! 

sage: f = PolyDict({(2,3):x}) 

sage: f.scalar_rmult(y) 

PolyDict with representation {(2, 3): x*y} 

sage: f = PolyDict({(2,3):2, (1,2):3, (2,1):4}) 

sage: f.scalar_rmult(-2) 

PolyDict with representation {(1, 2): -6, (2, 1): -8, (2, 3): -4} 

sage: f.scalar_rmult(RIF(-1,1)) 

PolyDict with representation {(1, 2): 0.?e1, (2, 1): 0.?e1, (2, 3): 0.?e1} 

""" 

v = {} 

# if s is 0, then all the products will be zero 

if not s == self.__zero: 

for e, c in self.__repn.iteritems(): 

v[e] = c*s 

return PolyDict(v, self.__zero, force_int_exponents=False, force_etuples=False) 

  

def scalar_lmult(PolyDict self, s): 

""" 

Left Scalar Multiplication 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: x, y = FreeMonoid(2, 'x, y').gens() # a strange object to live in a polydict, but non-commutative! 

sage: f = PolyDict({(2,3):x}) 

sage: f.scalar_lmult(y) 

PolyDict with representation {(2, 3): y*x} 

sage: f = PolyDict({(2,3):2, (1,2):3, (2,1):4}) 

sage: f.scalar_lmult(-2) 

PolyDict with representation {(1, 2): -6, (2, 1): -8, (2, 3): -4} 

sage: f.scalar_lmult(RIF(-1,1)) 

PolyDict with representation {(1, 2): 0.?e1, (2, 1): 0.?e1, (2, 3): 0.?e1} 

""" 

v = {} 

# if s is 0, then all the products will be zero 

if not s == self.__zero: 

for e, c in self.__repn.iteritems(): 

v[e] = s*c 

return PolyDict(v, self.__zero, force_int_exponents=False, force_etuples=False) 

  

def __sub__(PolyDict self, PolyDict other): 

""" 

Subtract two PolyDict's. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: f = PolyDict({(2,3):2, (1,2):3, (2,1):4}) 

sage: g = PolyDict({(2,3):2, (1,1):-10}) 

sage: f - g 

PolyDict with representation {(1, 1): 10, (1, 2): 3, (2, 1): 4} 

sage: g - f 

PolyDict with representation {(1, 1): -10, (1, 2): -3, (2, 1): -4} 

""" 

  

# TODO: should refactor add, make abstract operator, so can do both +/-; or copy code. 

return self + other.scalar_lmult(-1) 

  

def __one(PolyDict self): 

one = self.__zero + 1 

if len(self.__repn) == 0: 

v = {(0):one} 

else: 

v = {ETuple({}, len(next(iter(self.__repn)))): one} 

return PolyDict(v, self.__zero, force_int_exponents=False, force_etuples=False) 

  

def __pow__(PolyDict self, n, ignored): 

""" 

Return the n-th nonnegative power of this PolyDict. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: f = PolyDict({(2,3):2, (1,2):3, (2,1):4}) 

sage: f**2 

PolyDict with representation {(2, 4): 9, (3, 3): 24, (3, 5): 12, (4, 2): 16, (4, 4): 16, (4, 6): 4} 

sage: f**0 

PolyDict with representation {(0, 0): 1} 

sage: (f-f)**0 

PolyDict with representation {0: 1} 

""" 

if not n: 

return self.__one() 

return generic_power(self, n) 

  

def lcmt(PolyDict self, greater_etuple): 

""" 

Provides functionality of lc, lm, and lt by calling the tuple 

compare function on the provided term order T. 

  

INPUT: 

  

- ``greater_etuple`` -- a term order 

""" 

try: 

return ETuple(reduce(greater_etuple, self.__repn.keys())) 

except KeyError: 

raise ArithmeticError("%s not supported", greater_etuple) 

  

def __reduce__(PolyDict self): 

""" 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: f = PolyDict({(2,3):2, (1,2):3, (2,1):4}) 

sage: loads(dumps(f)) == f 

True 

""" 

return make_PolyDict, (self.__repn,) 

  

def min_exp(self): 

""" 

Returns an ETuple containing the minimum exponents appearing. If 

there are no terms at all in the PolyDict, it returns None. 

  

The nvars parameter is necessary because a PolyDict doesn't know it 

from the data it has (and an empty PolyDict offers no clues). 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: f = PolyDict({(2,3):2, (1,2):3, (2,1):4}) 

sage: f.min_exp() 

(1, 1) 

sage: PolyDict({}).min_exp() # returns None 

""" 

cdef ETuple r 

ETuples = self.__repn.keys() 

if len(ETuples)>0: 

r = <ETuple>ETuples[0] 

for e in ETuples: 

r = r.emin(e) 

return r 

else: 

return None 

  

def max_exp(self): 

""" 

Returns an ETuple containing the maximum exponents appearing. If 

there are no terms at all in the PolyDict, it returns None. 

  

The nvars parameter is necessary because a PolyDict doesn't know it 

from the data it has (and an empty PolyDict offers no clues). 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import PolyDict 

sage: f = PolyDict({(2,3):2, (1,2):3, (2,1):4}) 

sage: f.max_exp() 

(2, 3) 

sage: PolyDict({}).max_exp() # returns None 

""" 

cdef ETuple r 

ETuples = self.__repn.keys() 

if len(ETuples)>0: 

r = <ETuple>ETuples[0] 

for e in ETuples: 

r = r.emax(e) 

return r 

else: 

return None 

  

cdef class ETupleIter: 

cdef int _i 

cdef int _length 

cdef object _data 

  

def __init__(self, data, length): 

self._data = data 

self._length = length 

self._i = -1 

  

def __next__(self): 

self._i = self._i + 1 

  

if self._i == self._length: 

self._i = -1 

raise StopIteration 

  

return self._data.get(self._i, 0) 

  

cdef inline bint dual_etuple_iter(ETuple self, ETuple other, size_t *ind1, size_t *ind2, size_t *index, int *exp1, int *exp2): 

""" 

This function is a crucial helper function for a number of methods of 

the ETuple class. 

  

This is a rather fragile function. Perhaps some Cython guru could make 

it appear a little less stilted -- a principal difficulty is passing 

C types by reference. In any case, the complicated features of looping 

through two ETuple _data members is all packaged up right here and 

shouldn't be allowed to spread. 

""" 

if ind1[0] >= self._nonzero and ind2[0] >= other._nonzero: 

return 0 

if ind1[0] < self._nonzero and ind2[0] < other._nonzero: 

if self._data[2*ind1[0]] == other._data[2*ind2[0]]: 

exp1[0] = self._data[2*ind1[0]+1] 

exp2[0] = other._data[2*ind2[0]+1] 

index[0] = self._data[2*ind1[0]] 

ind1[0] += 1 

ind2[0] += 1 

elif self._data[2*ind1[0]] > other._data[2*ind2[0]]: 

exp1[0] = 0 

exp2[0] = other._data[2*ind2[0]+1] 

index[0] = other._data[2*ind2[0]] 

ind2[0] += 1 

else: 

exp1[0] = self._data[2*ind1[0]+1] 

exp2[0] = 0 

index[0] = self._data[2*ind1[0]] 

ind1[0] += 1 

else: 

if ind2[0] >= other._nonzero: 

exp1[0] = self._data[2*ind1[0]+1] 

exp2[0] = 0 

index[0] = self._data[2*ind1[0]] 

ind1[0] += 1 

elif ind1[0] >= self._nonzero: 

exp1[0] = 0 

exp2[0] = other._data[2*ind2[0]+1] 

index[0] = other._data[2*ind2[0]] 

ind2[0] += 1 

return 1 

  

cdef class ETuple: 

""" 

Representation of the exponents of a polydict monomial. If 

(0,0,3,0,5) is the exponent tuple of x_2^3*x_4^5 then this class 

only stores {2:3, 4:5} instead of the full tuple. This sparse 

information may be obtained by provided methods. 

  

The index/value data is all stored in the _data C int array member 

variable. For the example above, the C array would contain 

2,3,4,5. The indices are interlaced with the values. 

  

This data structure is very nice to work with for some functions 

implemented in this class, but tricky for others. One reason that 

I really like the format is that it requires a single memory 

allocation for all of the values. A hash table would require more 

allocations and presumably be slower. I didn't benchmark this 

question (although, there is no question that this is much faster 

than the prior use of python dicts). 

""" 

cdef ETuple _new(ETuple self): 

""" 

Quickly creates a new initialized ETuple with the 

same length as self. 

""" 

cdef type t = type(self) 

cdef ETuple x = <ETuple>t.__new__(t) 

x._length = self._length 

return x 

  

def __init__(ETuple self, data=None, length=None): 

""" 

- ``ETuple()`` -> an empty ETuple 

- ``ETuple(sequence)`` -> ETuple initialized from sequence's items 

  

If the argument is an ETuple, the return value is the same object. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: ETuple([1,1,0]) 

(1, 1, 0) 

sage: ETuple({int(1):int(2)}, int(3)) 

(0, 2, 0) 

  

TESTS: 

  

Iterators are not accepted:: 

  

sage: ETuple(iter([2,3,4])) 

Traceback (most recent call last): 

... 

TypeError: Error in ETuple((),<listiterator object at ...>,None) 

""" 

if data is None: 

return 

cdef size_t ind 

cdef int v 

if isinstance(data, ETuple): 

self._length = (<ETuple>data)._length 

self._nonzero = (<ETuple>data)._nonzero 

self._data = <int*>sig_malloc(sizeof(int)*self._nonzero*2) 

memcpy(self._data, (<ETuple>data)._data, sizeof(int)*self._nonzero*2) 

elif isinstance(data, dict) and isinstance(length, int): 

self._length = length 

self._nonzero = len(data) 

self._data = <int*>sig_malloc(sizeof(int)*self._nonzero*2) 

nz_elts = sorted(data.items()) 

ind = 0 

for index, exp in nz_elts: 

self._data[2*ind] = index 

self._data[2*ind+1] = exp 

ind += 1 

elif isinstance(data, list) or isinstance(data, tuple): 

self._length = len(data) 

self._nonzero = 0 

for v in data: 

if v != 0: 

self._nonzero += 1 

ind = 0 

self._data = <int*>sig_malloc(sizeof(int)*self._nonzero*2) 

for i from 0 <= i < self._length: 

v = data[i] 

if v != 0: 

self._data[ind] = i 

self._data[ind+1] = v 

ind += 2 

else: 

raise TypeError("Error in ETuple(%s,%s,%s)" % (self, data, length)) 

  

def __cinit__(ETuple self): 

self._data = <int*>0 

  

def __dealloc__(self): 

if self._data != <int*>0: 

sig_free(self._data) 

  

# methods to simulate tuple 

  

def __add__(ETuple self, ETuple other): 

""" 

x.__add__(n) <==> x+n 

  

concatenates two ETuples 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: ETuple([1,1,0]) + ETuple({int(1):int(2)}, int(3)) 

(1, 1, 0, 0, 2, 0) 

""" 

cdef size_t index = 0 

cdef ETuple result = <ETuple>ETuple.__new__(ETuple) 

result._length = self._length+other._length 

result._nonzero = self._nonzero+other._nonzero 

result._data = <int*>sig_malloc(sizeof(int)*result._nonzero*2) 

for index from 0 <= index < self._nonzero: 

result._data[2*index] = self._data[2*index] 

result._data[2*index+1] = self._data[2*index+1] 

for index from 0 <= index < other._nonzero: 

result._data[2*(index+self._nonzero)] = other._data[2*index]+self._length # offset the second tuple (append to end!) 

result._data[2*(index+self._nonzero)+1] = other._data[2*index+1] 

return result 

  

def __mul__(ETuple self, factor): 

""" 

x.__mul__(n) <==> x*n 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: ETuple([1,2,3])*2 

(1, 2, 3, 1, 2, 3) 

""" 

cdef int _factor = factor 

cdef ETuple result = <ETuple>ETuple.__new__(ETuple) 

if factor <= 0: 

result._length = 0 

result._nonzero = 0 

return result 

cdef size_t index 

cdef size_t f 

result._length = self._length * factor 

result._nonzero = self._nonzero * factor 

result._data = <int*>sig_malloc(sizeof(int)*result._nonzero*2) 

for index from 0 <= index < self._nonzero: 

for f from 0 <= f < factor: 

result._data[2*(f*self._nonzero+index)] = self._data[2*index]+f*self._length 

result._data[2*(f*self._nonzero+index)+1] = self._data[2*index+1] 

return result 

  

def __getitem__(ETuple self, i): 

""" 

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: m = ETuple([1,2,0,3]) 

sage: m[2] 

0 

sage: m[1] 

2 

sage: e = ETuple([1,2,3]) 

sage: e[1:] 

(2, 3) 

sage: e[:1] 

(1,) 

""" 

cdef size_t ind 

if isinstance(i, slice): 

start, stop = i.start, i.stop 

if start is None: 

start = 0 

elif start < 0: 

start = start % self._length 

elif start > self._length: 

start = self._length 

  

if stop > self._length or stop is None: 

stop = self._length 

elif stop < 0: 

stop = stop % self._length 

  

# this is not particularly fast, but I doubt many people care 

# if you do, feel free to tweak! 

d = [self[ind] for ind from start <= ind < stop] 

return ETuple(d) 

else: 

ind = 0 

for ind from 0 <= ind < self._nonzero: 

if self._data[2*ind] == i: 

return self._data[2*ind+1] 

elif self._data[2*ind] > i: 

# the indices are sorted in _data, we are beyond, so quit 

return 0 

return 0 

  

def __hash__(ETuple self): 

""" 

x.__hash__() <==> hash(x) 

""" 

cdef int i 

cdef int result = 0 

for i from 0 <= i < self._nonzero: 

result += (1000003 * result) ^ self._data[2*i] 

result += (1000003 * result) ^ self._data[2*i+1] 

result = (1000003 * result) ^ self._length 

return result 

  

def __len__(ETuple self): 

""" 

x.__len__() <==> len(x) 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: e=ETuple([1,0,2,0,3]) 

sage: len(e) 

5 

""" 

return self._length 

  

def __contains__(ETuple self, elem): 

""" 

x.__contains__(n) <==> n in x 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: e = ETuple({int(1):int(2)}, int(3)); e 

(0, 2, 0) 

sage: 1 in e 

False 

sage: 2 in e 

True 

""" 

if elem==0: 

return self._length > self._nonzero 

  

cdef size_t ind = 0 

for ind from 0 <= ind < self._nonzero: 

if elem == self._data[2*ind+1]: 

return True 

return False 

  

def __richcmp__(ETuple self, ETuple other, op): 

""" 

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: ETuple([1,1,0])<ETuple([1,1,0]) 

False 

sage: ETuple([1,1,0])<ETuple([1,0,0]) 

False 

sage: ETuple([1,1,0])<ETuple([1,2,0]) 

True 

sage: ETuple([1,1,0])<ETuple([1,-1,0]) 

False 

sage: ETuple([0,-2,0])<ETuple([1,-1,0]) 

True 

sage: ETuple([1,1,0])>ETuple([1,1,0]) 

False 

sage: ETuple([1,1,0])>ETuple([1,0,0]) 

True 

sage: ETuple([1,1,0])>ETuple([1,2,0]) 

False 

sage: ETuple([1,1,0])>ETuple([1,-1,0]) 

True 

sage: ETuple([0,-2,0])>ETuple([1,-1,0]) 

False 

""" 

cdef size_t ind = 0 

if op == Py_EQ: # == 

if self._nonzero != other._nonzero: 

return False 

for ind from 0 <= ind < self._nonzero: 

if self._data[2*ind] != other._data[2*ind]: 

return False 

if self._data[2*ind+1] != other._data[2*ind+1]: 

return False 

return self._length == other._length 

  

if op == Py_LT: # < 

while ind < self._nonzero and ind < other._nonzero: 

if self._data[2*ind] < other._data[2*ind]: 

return self._data[2*ind+1] < 0 

if self._data[2*ind] > other._data[2*ind]: 

return other._data[2*ind+1] > 0 

if self._data[2*ind] == other._data[2*ind] and self._data[2*ind+1] != other._data[2*ind+1]: 

return self._data[2*ind+1] < other._data[2*ind+1] 

ind += 1 

if ind < self._nonzero and ind == other._nonzero: 

return self._data[2*ind+1] < 0 

if ind < other._nonzero and ind == self._nonzero: 

return other._data[2*ind+1] > 0 

return self._length < other._length 

  

if op == Py_GT: # > 

while ind < self._nonzero and ind < other._nonzero: 

if self._data[2*ind] < other._data[2*ind]: 

return self._data[2*ind+1] > 0 

if self._data[2*ind] > other._data[2*ind]: 

return other._data[2*ind+1] < 0 

if self._data[2*ind] == other._data[2*ind] and self._data[2*ind+1] != other._data[2*ind+1]: 

return self._data[2*ind+1] > other._data[2*ind+1] 

ind += 1 

if ind < self._nonzero and ind == other._nonzero: 

return self._data[2*ind+1] > 0 

if ind < other._nonzero and ind == self._nonzero: 

return other._data[2*ind+1] < 0 

return self._length < other._length 

  

# the rest of these are not particularly fast 

  

if op == Py_LE: # <= 

return tuple(self) <= tuple(other) 

  

if op == Py_NE: # != 

return tuple(self) != tuple(other) 

  

if op == Py_GE: # >= 

return tuple(self) >= tuple(other) 

  

def __iter__(ETuple self): 

""" 

x.__iter__() <==> iter(x) 

""" 

cdef size_t ind 

# this is not particularly fast, but I doubt many people care 

# if you do, feel free to tweak! 

d = dict([(self._data[2*ind], self._data[2*ind+1]) for ind from 0<=ind<self._nonzero]) 

return ETupleIter(d, self._length) 

  

def __str__(ETuple self): 

return repr(self) 

  

def __repr__(ETuple self): 

res = [0,]*self._length 

cdef size_t ind = 0 

for ind from 0 <= ind < self._nonzero: 

res[self._data[2*ind]] = self._data[2*ind+1] 

return str(tuple(res)) 

  

def __reduce__(ETuple self): 

""" 

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: e = ETuple([1,1,0]) 

sage: bool(e == loads(dumps(e))) 

True 

""" 

cdef size_t ind 

# this is not particularly fast, but I doubt many people care 

# if you do, feel free to tweak! 

d = {self._data[2*ind]: self._data[2*ind+1] 

for ind from 0 <= ind < self._nonzero} 

return make_ETuple, (d, int(self._length)) 

  

# additional methods 

  

cpdef ETuple eadd(ETuple self, ETuple other): 

""" 

Vector addition of ``self`` with ``other``. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: e = ETuple([1,0,2]) 

sage: f = ETuple([0,1,1]) 

sage: e.eadd(f) 

(1, 1, 3) 

  

Verify that :trac:`6428` has been addressed:: 

  

sage: R.<y, z> = Frac(QQ['x'])[] 

sage: type(y) 

<class 'sage.rings.polynomial.multi_polynomial_element.MPolynomial_polydict'> 

sage: y^(2^32) 

Traceback (most recent call last): 

... 

OverflowError: exponent overflow (2147483648) 

""" 

if self._length!=other._length: 

raise ArithmeticError 

  

cdef size_t ind1 = 0 

cdef size_t ind2 = 0 

cdef size_t index 

cdef int exp1 

cdef int exp2 

cdef int s # sum 

cdef size_t alloc_len = self._nonzero + other._nonzero # we simply guesstimate the length -- there might be double the correct amount allocated -- who cares? 

if alloc_len > self._length: 

alloc_len = self._length 

cdef ETuple result = <ETuple>self._new() 

result._nonzero = 0 # we don't know the correct length quite yet 

result._data = <int*>sig_malloc(sizeof(int)*alloc_len*2) 

while dual_etuple_iter(self, other, &ind1, &ind2, &index, &exp1, &exp2): 

s = exp1 + exp2 

# Check for overflow and underflow 

if (exp2 > 0 and s < exp1) or (exp2 < 0 and s > exp1): 

raise OverflowError("exponent overflow (%s)" % (int(exp1)+int(exp2))) 

if s != 0: 

result._data[2*result._nonzero] = index 

result._data[2*result._nonzero+1] = s 

result._nonzero += 1 

return result 

  

cpdef ETuple eadd_p(ETuple self, int other, int pos): 

""" 

Add ``other`` to ``self`` at position ``pos``. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: e = ETuple([1,0,2]) 

sage: e.eadd_p(5, 1) 

(1, 5, 2) 

sage: e = ETuple([0]*7) 

sage: e.eadd_p(5,4) 

(0, 0, 0, 0, 5, 0, 0) 

  

sage: ETuple([0,1]).eadd_p(1, 0) == ETuple([1,1]) 

True 

  

""" 

cdef size_t index = 0 

cdef size_t rindex = 0 

cdef int new_value 

cdef int need_to_add = 1 

if pos < 0 or pos >= self._length: 

raise ValueError("pos must be between 0 and %s" % self._length) 

  

cdef size_t alloc_len = self._nonzero + 1 

  

cdef ETuple result = <ETuple>self._new() 

result._nonzero = self._nonzero 

result._data = <int*>sig_malloc(sizeof(int)*alloc_len*2) 

  

for index from 0 <= index < self._nonzero: 

if self._data[2*index] == pos: 

new_value = self._data[2*index+1] + other 

if new_value != 0: 

result._data[2*rindex] = pos 

result._data[2*rindex+1] = new_value 

else: 

result._nonzero -= 1 

rindex -= 1 

need_to_add = 0 

else: 

result._data[2*rindex] = self._data[2*index] 

result._data[2*rindex+1] = self._data[2*index+1] 

  

rindex += 1 

  

rindex = 0 

if need_to_add: 

for index from 0 <= index < self._nonzero: 

if self._data[2*index] > pos: 

result._data[2*rindex] = pos 

result._data[2*rindex+1] = other 

rindex += 1 

result._nonzero += 1 

result._data[2*rindex] = self._data[2*index] 

result._data[2*rindex+1] = self._data[2*index+1] 

rindex += 1 

  

if rindex == index and other: 

result._data[2*rindex] = pos 

result._data[2*rindex+1] = other 

result._nonzero += 1 

  

return result 

  

cpdef ETuple esub(ETuple self, ETuple other): 

""" 

Vector subtraction of ``self`` with ``other``. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: e = ETuple([1,0,2]) 

sage: f = ETuple([0,1,1]) 

sage: e.esub(f) 

(1, -1, 1) 

""" 

if self._length!=other._length: 

raise ArithmeticError 

  

cdef size_t ind1 = 0 

cdef size_t ind2 = 0 

cdef size_t index 

cdef int exp1 

cdef int exp2 

cdef int d # difference 

cdef size_t alloc_len = self._nonzero + other._nonzero # we simply guesstimate the length -- there might be double the correct amount allocated -- who cares? 

if alloc_len > self._length: 

alloc_len = self._length 

cdef ETuple result = <ETuple>self._new() 

result._nonzero = 0 # we don't know the correct length quite yet 

result._data = <int*>sig_malloc(sizeof(int)*alloc_len*2) 

while dual_etuple_iter(self, other, &ind1, &ind2, &index, &exp1, &exp2): 

# Check for overflow and underflow 

d = exp1 - exp2 

if (exp2 > 0 and d > exp1) or (exp2 < 0 and d < exp1): 

raise OverflowError("Exponent overflow (%s)." % (int(exp1)-int(exp2))) 

if d != 0: 

result._data[2*result._nonzero] = index 

result._data[2*result._nonzero+1] = d 

result._nonzero += 1 

return result 

  

cpdef ETuple emul(ETuple self, int factor): 

""" 

Scalar Vector multiplication of ``self``. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: e = ETuple([1,0,2]) 

sage: e.emul(2) 

(2, 0, 4) 

""" 

cdef size_t ind 

cdef ETuple result = <ETuple>self._new() 

if factor == 0: 

result._nonzero = 0 # all zero, no non-zero entries! 

result._data = <int*>sig_malloc(sizeof(int)*result._nonzero*2) 

else: 

result._nonzero = self._nonzero 

result._data = <int*>sig_malloc(sizeof(int)*result._nonzero*2) 

for ind from 0 <= ind < self._nonzero: 

result._data[2*ind] = self._data[2*ind] 

result._data[2*ind+1] = self._data[2*ind+1]*factor 

return result 

  

cpdef ETuple emax(ETuple self, ETuple other): 

""" 

Vector of maximum of components of ``self`` and ``other``. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: e = ETuple([1,0,2]) 

sage: f = ETuple([0,1,1]) 

sage: e.emax(f) 

(1, 1, 2) 

sage: e = ETuple((1,2,3,4)) 

sage: f = ETuple((4,0,2,1)) 

sage: f.emax(e) 

(4, 2, 3, 4) 

sage: e = ETuple((1,-2,-2,4)) 

sage: f = ETuple((4,0,0,0)) 

sage: f.emax(e) 

(4, 0, 0, 4) 

sage: f.emax(e).nonzero_positions() 

[0, 3] 

""" 

if self._length!=other._length: 

raise ArithmeticError 

  

cdef size_t ind1 = 0 

cdef size_t ind2 = 0 

cdef size_t index 

cdef int exp1 

cdef int exp2 

cdef size_t alloc_len = self._nonzero + other._nonzero # we simply guesstimate the length -- there might be double the correct amount allocated -- who cares? 

if alloc_len > self._length: 

alloc_len = self._length 

cdef ETuple result = <ETuple>self._new() 

result._nonzero = 0 # we don't know the correct length quite yet 

result._data = <int*>sig_malloc(sizeof(int)*alloc_len*2) 

while dual_etuple_iter(self, other, &ind1, &ind2, &index, &exp1, &exp2): 

if exp1 >= exp2 and exp1 != 0: 

result._data[2*result._nonzero] = index 

result._data[2*result._nonzero+1] = exp1 

result._nonzero += 1 

elif exp2 >= exp1 and exp2 != 0: 

result._data[2*result._nonzero] = index 

result._data[2*result._nonzero+1] = exp2 

result._nonzero += 1 

return result 

  

cpdef ETuple emin(ETuple self, ETuple other): 

""" 

Vector of minimum of components of ``self`` and ``other``. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: e = ETuple([1,0,2]) 

sage: f = ETuple([0,1,1]) 

sage: e.emin(f) 

(0, 0, 1) 

sage: e = ETuple([1,0,-1]) 

sage: f = ETuple([0,-2,1]) 

sage: e.emin(f) 

(0, -2, -1) 

""" 

if self._length != other._length: 

raise ArithmeticError 

  

cdef size_t ind1 = 0 

cdef size_t ind2 = 0 

cdef size_t index 

cdef int exp1 

cdef int exp2 

cdef size_t alloc_len = self._nonzero + other._nonzero # we simply guesstimate the length -- there might be double the correct amount allocated -- who cares? 

if alloc_len > self._length: 

alloc_len = self._length 

cdef ETuple result = <ETuple>self._new() 

result._nonzero = 0 # we don't know the correct length quite yet 

result._data = <int*>sig_malloc(sizeof(int)*alloc_len*2) 

while dual_etuple_iter(self, other, &ind1, &ind2, &index, &exp1, &exp2): 

if exp1 <= exp2 and exp1 != 0: 

result._data[2*result._nonzero] = index 

result._data[2*result._nonzero+1] = exp1 

result._nonzero += 1 

elif exp2 <= exp1 and exp2 != 0: 

result._data[2*result._nonzero] = index 

result._data[2*result._nonzero+1] = exp2 

result._nonzero += 1 

return result 

  

cpdef bint is_constant(ETuple self): 

""" 

Return if all exponents are zero in the tuple. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: e = ETuple([1,0,2]) 

sage: e.is_constant() 

False 

sage: e = ETuple([0,0]) 

sage: e.is_constant() 

True 

""" 

return self._nonzero == 0 

  

cpdef list nonzero_positions(ETuple self, bint sort=False): 

""" 

Return the positions of non-zero exponents in the tuple. 

  

INPUT: 

  

- ``sort`` -- (default: ``False``) if ``True`` a sorted list is 

returned; if ``False`` an unsorted list is returned 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: e = ETuple([1,0,2]) 

sage: e.nonzero_positions() 

[0, 2] 

""" 

cdef size_t ind 

return [self._data[2*ind] for ind from 0 <= ind < self._nonzero] 

  

cpdef common_nonzero_positions(ETuple self, ETuple other, bint sort=False): 

""" 

Returns an optionally sorted list of non zero positions either 

in self or other, i.e. the only positions that need to be 

considered for any vector operation. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: e = ETuple([1,0,2]) 

sage: f = ETuple([0,0,1]) 

sage: e.common_nonzero_positions(f) 

{0, 2} 

sage: e.common_nonzero_positions(f, sort=True) 

[0, 2] 

""" 

# TODO: we should probably make a fast version of this! 

res = set(self.nonzero_positions()).union(other.nonzero_positions()) 

if sort: 

return sorted(res) 

else: 

return res 

  

cpdef list nonzero_values(ETuple self, bint sort=True): 

""" 

Return the non-zero values of the tuple. 

  

INPUT: 

  

- ``sort`` -- (default: ``True``) if ``True`` the values are sorted 

by their indices; otherwise the values are returned unsorted 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: e = ETuple([2,0,1]) 

sage: e.nonzero_values() 

[2, 1] 

sage: f = ETuple([0,-1,1]) 

sage: f.nonzero_values(sort=True) 

[-1, 1] 

""" 

cdef size_t ind 

return [self._data[2*ind+1] for ind from 0 <= ind < self._nonzero] 

  

cpdef ETuple reversed(ETuple self): 

""" 

Return the reversed ETuple of ``self``. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: e = ETuple([1,2,3]) 

sage: e.reversed() 

(3, 2, 1) 

""" 

cdef size_t ind 

cdef ETuple result = <ETuple>self._new() 

result._nonzero = self._nonzero 

result._data = <int*>sig_malloc(sizeof(int)*result._nonzero*2) 

for ind from 0 <= ind < self._nonzero: 

result._data[2*(result._nonzero-ind-1)] = self._length - self._data[2*ind] - 1 

result._data[2*(result._nonzero-ind-1)+1] = self._data[2*ind+1] 

return result 

  

def sparse_iter(ETuple self): 

""" 

Iterator over the elements of ``self`` where the elements are returned 

as ``(i, e)`` where ``i`` is the position of ``e`` in the tuple. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: e = ETuple([1,0,2,0,3]) 

sage: list(e.sparse_iter()) 

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

""" 

cdef size_t ind 

for ind from 0 <= ind < self._nonzero: 

yield (self._data[2*ind], self._data[2*ind+1]) 

  

def combine_to_positives(ETuple self, ETuple other): 

""" 

Given a pair of ETuples (self, other), returns a triple of 

ETuples (a, b, c) so that self = a + b, other = a + c and b and c 

have all positive entries. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.polydict import ETuple 

sage: e = ETuple([-2,1,-5, 3, 1,0]) 

sage: f = ETuple([1,-3,-3,4,0,2]) 

sage: e.combine_to_positives(f) 

((-2, -3, -5, 3, 0, 0), (0, 4, 0, 0, 1, 0), (3, 0, 2, 1, 0, 2)) 

""" 

m = self.emin(other) 

return m, self.esub(m), other.esub(m) 

  

  

def make_PolyDict(data): 

return PolyDict(data, remove_zero=False, force_int_exponents=False, 

force_etuples=False) 

  

  

def make_ETuple(data, length): 

return ETuple(data, length)