Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

1715

1716

1717

1718

1719

1720

1721

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

1952

1953

1954

1955

1956

1957

1958

1959

1960

1961

1962

1963

1964

1965

1966

1967

1968

1969

1970

1971

1972

1973

1974

1975

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

1986

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1998

1999

2000

2001

2002

2003

2004

2005

2006

2007

2008

2009

2010

2011

2012

2013

2014

2015

2016

2017

2018

2019

2020

2021

2022

2023

2024

2025

2026

2027

2028

2029

2030

2031

2032

""" 

Generic Multivariate Polynomials 

 

AUTHORS: 

 

- David Joyner: first version 

 

- William Stein: use dict's instead of lists 

 

- Martin Albrecht malb@informatik.uni-bremen.de: some functions added 

 

- William Stein (2006-02-11): added better __div__ behavior. 

 

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

Singular features 

 

- William Stein (2006-04-19): added e.g., 

``f[1,3]`` to get coeff of `xy^3`; added examples of the new 

``R.x,y = PolynomialRing(QQ,2)`` notation. 

 

- Martin Albrecht: improved singular coercions (restructured class 

hierarchy) and added ETuples 

 

- Robert Bradshaw (2007-08-14): added support for coercion of 

polynomials in a subset of variables (including multi-level 

univariate rings) 

 

- Joel B. Mohler (2008-03): Refactored interactions with ETuples. 

 

EXAMPLES: 

 

We verify Lagrange's four squares identity:: 

 

sage: R.<a0,a1,a2,a3,b0,b1,b2,b3> = QQbar[] 

sage: (a0^2 + a1^2 + a2^2 + a3^2)*(b0^2 + b1^2 + b2^2 + b3^2) == (a0*b0 - a1*b1 - a2*b2 - a3*b3)^2 + (a0*b1 + a1*b0 + a2*b3 - a3*b2)^2 + (a0*b2 - a1*b3 + a2*b0 + a3*b1)^2 + (a0*b3 + a1*b2 - a2*b1 + a3*b0)^2 

True 

""" 

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

# 

# Sage: System for Algebra and Geometry Experimentation 

# 

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

# 

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

# 

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

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

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

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

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

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

from __future__ import absolute_import 

from six.moves import range 

from six import iteritems, integer_types 

 

from sage.structure.element import CommutativeRingElement, canonical_coercion, coerce_binop 

from sage.misc.all import prod 

import sage.rings.integer 

from . import polydict 

from sage.structure.factorization import Factorization 

from sage.rings.polynomial.polynomial_singular_interface import Polynomial_singular_repr 

from sage.structure.sequence import Sequence 

from .multi_polynomial import MPolynomial 

from sage.categories.morphism import Morphism 

 

def is_MPolynomial(x): 

return isinstance(x, MPolynomial) 

 

class MPolynomial_element(MPolynomial): 

def __init__(self, parent, x): 

""" 

EXAMPLES:: 

 

sage: K.<cuberoot2> = NumberField(x^3 - 2) 

sage: L.<cuberoot3> = K.extension(x^3 - 3) 

sage: S.<sqrt2> = L.extension(x^2 - 2) 

sage: S 

Number Field in sqrt2 with defining polynomial x^2 - 2 over its base field 

sage: P.<x,y,z> = PolynomialRing(S) # indirect doctest 

""" 

CommutativeRingElement.__init__(self, parent) 

self.__element = x 

 

def _repr_(self): 

""" 

EXAMPLES:: 

 

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

sage: x + QQbar.random_element() # indirect doctest 

x - 4 

""" 

return "%s"%self.__element 

 

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

 

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

""" 

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

`x` is either the tuple of values to substitute in, or one 

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

evaluate `f` with the ith variable replaced by 

`a_i`. 

 

EXAMPLES:: 

 

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

sage: f = x^2 + y^2 

sage: f(1,2) 

5.00000000000000 

sage: f((1,2)) 

5.00000000000000 

 

:: 

 

sage: x = PolynomialRing(CC,3,'x').gens() 

sage: f = x[0] + x[1] - 2*x[1]*x[2] 

sage: f 

(-2.00000000000000)*x1*x2 + x0 + x1 

sage: f(1,2,0) 

3.00000000000000 

sage: f(1,2,5) 

-17.0000000000000 

 

AUTHORS: 

 

- David Kohel (2005-09-27) 

""" 

if len(kwds) > 0: 

f = self.subs(**kwds) 

if len(x) > 0: 

return f(*x) 

else: 

return f 

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

x = x[0] 

n = self.parent().ngens() 

if len(x) != n: 

raise TypeError("x must be of correct length") 

if n == 0: 

return self 

try: 

K = x[0].parent() 

except AttributeError: 

K = self.parent().base_ring() 

y = K(0) 

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

y += c*prod([ x[i]**m[i] for i in range(n) if m[i] != 0]) 

return y 

 

def __cmp__(self, right): 

""" 

Compares right to self with respect to the term order of 

self.parent(). 

 

EXAMPLES:: 

 

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

sage: x^1*y^2 > y^3*z^4 

True 

sage: x^3*y^2*z^4 < x^3*y^2*z^1 

False 

 

:: 

 

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

sage: x^1*y^2*z^3 > x^3*y^2*z^0 

True 

sage: x^1*y^2*z^4 < x^1*y^1*z^5 

False 

 

:: 

 

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

sage: x^1*y^5*z^2 > x^4*y^1*z^3 

True 

sage: x^4*y^7*z^1 < x^4*y^2*z^3 

False 

""" 

try: 

return self.__element.compare(right.__element, 

self.parent().term_order().sortkey) 

except AttributeError: 

return self.__element.compare(right.__element) 

 

def _im_gens_(self, codomain, im_gens): 

""" 

EXAMPLES:: 

 

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

sage: f = R.hom([y,x], R) 

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

3*x^5 + y^2 

""" 

n = self.parent().ngens() 

if n == 0: 

return codomain._coerce_(self) 

y = codomain(0) 

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

y += codomain(c)*prod([ im_gens[i]**m[i] for i in range(n) if m[i] ]) 

return y 

 

def number_of_terms(self): 

""" 

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

 

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

 

EXAMPLES:: 

 

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

sage: f = x^3 - y 

sage: f.number_of_terms() 

2 

sage: R(0).number_of_terms() 

0 

sage: f = (x+y)^100 

sage: f.number_of_terms() 

101 

 

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

 

sage: f.hamming_weight() 

101 

""" 

return len(self.element().dict()) 

 

hamming_weight = number_of_terms 

 

def _add_(self, right): 

#return self.parent()(self.__element + right.__element) 

return self.__class__(self.parent(),self.__element + right.__element) 

 

def _sub_(self, right): 

# return self.parent()(self.__element - right.__element) 

return self.__class__(self.parent(),self.__element - right.__element) 

 

def _mul_(self, right): 

#return self.parent()(self.__element * right.__element) 

return self.__class__(self.parent(),self.__element * right.__element) 

 

def _lmul_(self, a): 

""" 

Left Scalar Multiplication 

 

EXAMPLES: 

 

Note that it is not really possible to do a meaningful 

example since sage mpoly rings refuse to have non-commutative 

bases. 

 

:: 

 

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

sage: f = (x + y) 

sage: 3*f 

3*x + 3*y 

""" 

return self.__class__(self.parent(),self.__element.scalar_lmult(a)) 

 

def _rmul_(self, a): 

""" 

Right Scalar Multiplication 

 

EXAMPLES: 

 

Note that it is not really possible to do a meaningful 

example since sage mpoly rings refuse to have non-commutative 

bases. 

 

:: 

 

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

sage: f = (x + y) 

sage: f*3 

3*x + 3*y 

""" 

return self.__class__(self.parent(),self.__element.scalar_rmult(a)) 

 

def _div_(self, right): 

r""" 

EXAMPLES:: 

 

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

sage: f = (x + y)/x; f 

(x + y)/x 

sage: f.parent() 

Fraction Field of Multivariate Polynomial Ring in x, y over 

Complex Field with 53 bits of precision 

 

If dividing by a scalar, there is no need to go to the fraction 

field of the polynomial ring:: 

 

sage: f = (x + y)/2; f 

0.500000000000000*x + 0.500000000000000*y 

sage: f.parent() 

Multivariate Polynomial Ring in x, y over Complex Field with 

53 bits of precision 

 

TESTS: 

 

Ensure that :trac:`13704` is fixed.:: 

 

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

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

sage: x/S(2) 

1/2*x 

""" 

if right in self.base_ring(): 

inv = self.base_ring().one()/self.base_ring()(right) 

return inv*self 

return self.parent().fraction_field()(self, right, coerce=False) 

 

def __rpow__(self, n): 

if not isinstance(n, integer_types + (sage.rings.integer.Integer,)): 

raise TypeError("The exponent must be an integer.") 

return self.parent()(self.__element**n) 

 

def element(self): 

return self.__element 

 

def change_ring(self, R): 

r""" 

Change the base ring of this polynomial to ``R``. 

 

INPUT: 

 

- ``R`` -- ring or morphism. 

 

OUTPUT: a new polynomial converted to ``R``. 

 

EXAMPLES:: 

 

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

sage: f = x^2 + 5*y 

sage: f.change_ring(GF(5)) 

x^2 

 

:: 

 

sage: K.<w> = CyclotomicField(5) 

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

sage: f = x^2 + w*y 

sage: f.change_ring(K.embeddings(QQbar)[1]) 

x^2 + (-0.8090169943749474? + 0.5877852522924731?*I)*y 

""" 

if isinstance(R, Morphism): 

#if we're given a hom of the base ring extend to a poly hom 

if R.domain() == self.base_ring(): 

R = self.parent().hom(R, self.parent().change_ring(R.codomain())) 

return R(self) 

else: 

return self.parent().change_ring(R)(self) 

 

 

class MPolynomial_polydict(Polynomial_singular_repr, MPolynomial_element): 

r""" 

Multivariate polynomials implemented in pure python using 

polydicts. 

""" 

def __init__(self, parent, x): 

""" 

EXAMPLES:: 

 

sage: R, x = PolynomialRing(QQbar, 10, 'x').objgens() 

sage: x 

(x0, x1, x2, x3, x4, x5, x6, x7, x8, x9) 

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

True 

""" 

if not isinstance(x, polydict.PolyDict): 

x = polydict.PolyDict(x, parent.base_ring()(0), remove_zero=True) 

MPolynomial_element.__init__(self, parent, x) 

 

def _new_constant_poly(self, x, P): 

""" 

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

 

ASSUMPTION: 

 

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

not verified. 

 

EXAMPLES:: 

 

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

sage: x._new_constant_poly(R.base_ring()(2),R) 

2 

 

""" 

return MPolynomial_polydict(P, {P._zero_tuple:x}) 

 

def __neg__(self): 

""" 

EXAMPLES:: 

 

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

sage: -x 

-x 

sage: -(y-1) 

-y + 1 

""" 

return self*(-1) 

 

def _repr_(self): 

""" 

EXAMPLES:: 

 

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

sage: repr(-x^2-y+1) # indirect doc-test 

'-x^2 - y + 1' 

sage: K.<I>=QuadraticField(-1) 

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

sage: repr(-I*y-x^2) # indirect doc-test 

'-x^2 + (-I)*y' 

""" 

try: 

key = self.parent().term_order().sortkey 

except AttributeError: 

key = None 

atomic = self.parent().base_ring()._repr_option('element_is_atomic') 

return self.element().poly_repr(self.parent().variable_names(), 

atomic_coefficients=atomic, 

sortkey=key) 

 

def _latex_(self): 

r""" 

EXAMPLES:: 

 

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

sage: latex(-x^2-y+1) 

-x^{2} - y + 1 

sage: K.<I>=QuadraticField(-1) 

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

sage: latex(-I*y+I*x^2) 

\left(\sqrt{-1}\right) x^{2} + \left(-\sqrt{-1}\right) y 

""" 

try: 

key = self.parent().term_order().sortkey 

except AttributeError: 

key = None 

atomic = self.parent().base_ring()._repr_option('element_is_atomic') 

return self.element().latex(self.parent().latex_variable_names(), 

atomic_coefficients=atomic, sortkey=key) 

 

def _repr_with_changed_varnames(self, varnames): 

""" 

EXAMPLES:: 

 

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

sage: f=-x^2-y+1 

sage: f._repr_with_changed_varnames(['jack','jill']) 

'-jack^2 - jill + 1' 

""" 

try: 

key = self.parent().term_order().sortkey 

except AttributeError: 

key = None 

atomic = self.parent().base_ring()._repr_option('element_is_atomic') 

return self.element().poly_repr(varnames, 

atomic_coefficients=atomic, sortkey=key) 

 

def degrees(self): 

r""" 

Returns a tuple (precisely - an ``ETuple``) with the 

degree of each variable in this polynomial. The list of degrees is, 

of course, ordered by the order of the generators. 

 

EXAMPLES:: 

 

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

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

sage: f.degrees() 

(2, 2, 0) 

sage: f = x^2+z^2 

sage: f.degrees() 

(2, 0, 2) 

sage: f.total_degree() # this simply illustrates that total degree is not the sum of the degrees 

2 

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

sage: f=(1-x)*(1+y+z+x^3)^5 

sage: f.degrees() 

(16, 5, 5, 0) 

sage: R(0).degrees() 

(0, 0, 0, 0) 

""" 

if self.is_zero(): 

return polydict.ETuple({},self.parent().ngens()) 

else: 

return self._MPolynomial_element__element.max_exp() 

 

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

""" 

Return the degree of self in x, where x must be one of the 

generators for the parent of self. 

 

INPUT: 

 

- ``x`` - multivariate polynomial (a generator of the parent 

of self). If ``x`` is not specified (or is None), return 

the total degree, which is the maximum degree of any 

monomial. Note that a weighted term ordering alters the 

grading of the generators of the ring; see the tests below. 

To avoid this behavior, set the optional argument ``std_grading=True``. 

 

OUTPUT: integer 

 

EXAMPLES:: 

 

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

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

sage: f.degree(x) 

9 

sage: f.degree(y) 

2 

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

3 

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

10 

 

Note that total degree takes into account if we are working in a polynomial 

ring with a weighted term order. 

 

:: 

 

sage: R = PolynomialRing(QQ,'x,y',order=TermOrder('wdeglex',(2,3))) 

sage: x,y = R.gens() 

sage: x.degree() 

2 

sage: y.degree() 

3 

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

(0, 1, 0, 1) 

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

sage: f.degree(x) 

2 

sage: f.degree(y) 

2 

sage: f.degree() 

8 

sage: f.degree(std_grading=True) 

3 

 

Note that if ``x`` is not a generator of the parent of self, 

for example if it is a generator of a polynomial algebra which 

maps naturally to this one, then it is converted to an element 

of this algebra. (This fixes the problem reported in 

:trac:`17366`.) 

 

:: 

 

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

sage: GF(3037000453)['x','y'].gen(0).degree(x) 

1 

 

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

sage: GF(3037000453)['x','y'].gen(0).degree(x0) 

Traceback (most recent call last): 

... 

TypeError: x must canonically coerce to parent 

 

sage: GF(3037000453)['x','y'].gen(0).degree(x^2) 

Traceback (most recent call last): 

... 

TypeError: x must be one of the generators of the parent 

 

TESTS:: 

 

sage: R = PolynomialRing(GF(2)['t'],'x,y',order=TermOrder('wdeglex',(2,3))) 

sage: x,y = R.gens() 

sage: x.degree() 

2 

sage: y.degree() 

3 

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

(0, 1, 0, 1) 

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

sage: f.degree(x) 

2 

sage: f.degree(y) 

2 

sage: f.degree() 

8 

sage: f.degree(std_grading=True) 

3 

sage: R(0).degree() 

-1 

 

Degree of zero polynomial for other implementation :trac:`20048` :: 

 

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

sage: R.zero().degree(x) 

-1 

""" 

if x is None: 

if std_grading or not self.parent().term_order().is_weighted_degree_order(): 

return self.element().degree(None) 

return self.weighted_degree(self.parent().term_order().weights()) 

if isinstance(x, MPolynomial): 

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

try: 

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

except TypeError: 

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

if not x.is_generator(): 

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

else: 

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

return self.element().degree(x.element()) 

 

def total_degree(self): 

""" 

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

monomial in self. 

 

EXAMPLES:: 

 

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

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

sage: f.total_degree() 

6 

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

sage: f.total_degree() 

7 

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

sage: f.total_degree() 

18 

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

sage: f.total_degree() 

10 

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

sage: f.total_degree() 

10 

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

sage: f.total_degree() 

10 

""" 

return self.degree() 

 

def monomial_coefficient(self, mon): 

""" 

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

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

 

This function contrasts with the function 

``coefficient`` which returns the coefficient of a 

monomial viewing this polynomial in a polynomial ring over a base 

ring having fewer variables. 

 

INPUT: 

 

- ``mon`` - a monomial 

 

 

OUTPUT: coefficient in base ring 

 

.. SEEALSO:: 

 

For coefficients in a base ring of fewer variables, look 

at :meth:`coefficient`. 

 

EXAMPLES: 

 

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

 

:: 

 

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

 

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

 

:: 

 

sage: f = 2 * x * y 

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

2 

sage: c.parent() 

Algebraic Field 

 

:: 

 

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

sage: f.monomial_coefficient(y^2) 

1 

sage: f.monomial_coefficient(x*y) 

5 

sage: f.monomial_coefficient(x^9) 

-1 

sage: f.monomial_coefficient(x^10) 

0 

 

:: 

 

sage: var('a') 

a 

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

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

sage: f=(a*x-1)*((a+1)*y-1); f 

-x*y + (-a)*x + (-a - 1)*y + 1 

sage: f.monomial_coefficient(x) 

-a 

""" 

if not (isinstance(mon, MPolynomial) and mon.parent() is self.parent() and mon.is_monomial()): 

raise TypeError("mon must be a monomial in the parent of self.") 

R = self.parent().base_ring() 

return R(self.element().monomial_coefficient(mon.element().dict())) 

 

def dict(self): 

""" 

Return underlying dictionary with keys the exponents and values 

the coefficients of this polynomial. 

""" 

return self.element().dict() 

 

#def __iter__(self): 

# """ 

# Facilitates iterating over the monomials of self, 

# returning tuples of the form (coeff, mon) for each 

# non-zero monomial. 

# 

# EXAMPLES:: 

 

# sage: R = ZZ['t'] 

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

# sage: f = 3*x^3*y + 16*x + 7 

# sage: [(c,m) for c,m in f] 

# [(3, x^3*y), (16, x), (7, 1)] 

# sage: f = P.random_element(10,10) 

# sage: sum(c*m for c,m in f) == f 

# True 

# """ 

# exps = self.exponents() 

# parent = self.parent() 

# for exp in exps: 

# yield self.element()[exp], MPolynomial_polydict(parent, {exp: 1}) 

 

def __getitem__(self, x): 

""" 

INPUT: 

 

 

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

MPolynomial ring x can also be an integer. 

 

 

EXAMPLES:: 

 

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

sage: f = -10*x^3*y + 17*x*y 

sage: f[3,1] 

-10 

sage: f[1,1] 

17 

sage: f[0,1] 

0 

 

:: 

 

sage: R.<x> = PolynomialRing(QQbar,1); R 

Multivariate Polynomial Ring in x over Algebraic Field 

sage: f = 5*x^2 + 3; f 

5*x^2 + 3 

sage: f[2] 

5 

""" 

if isinstance(x, MPolynomial): 

return self.monomial_coefficient(x) 

if not isinstance(x, tuple): 

try: 

x = tuple(x) 

except TypeError: 

x = (x, ) 

try: 

return self.element()[x] 

except KeyError: 

return self.parent().base_ring()(0) 

 

def coefficient(self, degrees): 

""" 

Return the coefficient of the variables with the degrees specified 

in the python dictionary ``degrees``. Mathematically, 

this is the coefficient in the base ring adjoined by the variables 

of this ring not listed in ``degrees``. However, the 

result has the same parent as this polynomial. 

 

This function contrasts with the function 

``monomial_coefficient`` which returns the coefficient 

in the base ring of a monomial. 

 

INPUT: 

 

 

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

 

- a dictionary of degree restrictions 

 

- a list of degree restrictions (with None in 

the unrestricted variables) 

 

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

 

 

OUTPUT: element of the parent of self 

 

.. SEEALSO:: 

 

For coefficients of specific monomials, look at 

:meth:`monomial_coefficient`. 

 

EXAMPLES:: 

 

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

sage: f = 2 * x * y 

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

2 

sage: c.parent() 

Multivariate Polynomial Ring in x, y over Algebraic Field 

sage: c in PolynomialRing(QQbar, 2, names = ['x','y']) 

True 

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

sage: f.coefficient({y:1}) 

5*x 

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

-x^9 + (-7)*x 

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

0 

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

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

y^2 + y + 1 

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

y^2 + y + 1 

sage: f.coefficient(x) 

y^2 + y + 1 

sage: # Be aware that this may not be what you think! 

sage: # The physical appearance of the variable x is deceiving -- particularly if the exponent would be a variable. 

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

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

 

:: 

 

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

sage: f=x*y+5 

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

5.00000000000000 

sage: parent(c) 

Multivariate Polynomial Ring in x, y over Real Field with 53 bits of precision 

 

AUTHORS: 

 

- Joel B. Mohler (2007-10-31) 

""" 

looking_for = None 

if isinstance(degrees, MPolynomial) and degrees.parent() == self.parent() and degrees.is_monomial(): 

looking_for = [e if e > 0 else None for e in degrees.exponents()[0]] 

elif isinstance(degrees, list): 

looking_for = degrees 

elif isinstance(degrees, dict): 

poly_vars = self.parent().gens() 

looking_for = [None] * len(poly_vars) 

for d, exp in degrees.items(): 

for i in range(len(poly_vars)): 

if d == poly_vars[i]: 

looking_for[i] = exp 

if not looking_for: 

raise ValueError("You must pass a dictionary list or monomial.") 

return self.parent()(self.element().polynomial_coefficient(looking_for)) 

 

def exponents(self, as_ETuples=True): 

""" 

Return the exponents of the monomials appearing in self. 

 

INPUT: 

 

- as_ETuples (default: ``True``): return the list of exponents as a list 

of ETuples. 

 

OUTPUT: 

 

Return the list of exponents as a list of ETuples or tuples. 

 

EXAMPLES:: 

 

sage: R.<a,b,c> = PolynomialRing(QQbar, 3) 

sage: f = a^3 + b + 2*b^2 

sage: f.exponents() 

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

 

Be default the list of exponents is a list of ETuples:: 

 

sage: type(f.exponents()[0]) 

<type 'sage.rings.polynomial.polydict.ETuple'> 

sage: type(f.exponents(as_ETuples=False)[0]) 

<... 'tuple'> 

""" 

try: 

exp = self.__exponents 

if as_ETuples: 

return exp 

else: 

return [tuple(e) for e in exp] 

except AttributeError: 

self.__exponents = self.element().dict().keys() 

try: 

self.__exponents.sort(key=self.parent().term_order().sortkey, 

reverse=True) 

except AttributeError: 

pass 

if as_ETuples: 

return self.__exponents 

else: 

return [tuple(e) for e in self.__exponents] 

 

def inverse_of_unit(self): 

d = self.element().dict() 

k = d.keys() 

if self.is_unit(): 

if len(k) != 1: 

raise NotImplementedError 

return ~d[k[0]] 

raise ArithmeticError("is not a unit") 

 

def is_homogeneous(self): 

""" 

Return True if self is a homogeneous polynomial. 

 

EXAMPLES:: 

 

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

sage: (x+y).is_homogeneous() 

True 

sage: (x.parent()(0)).is_homogeneous() 

True 

sage: (x+y^2).is_homogeneous() 

False 

sage: (x^2 + y^2).is_homogeneous() 

True 

sage: (x^2 + y^2*x).is_homogeneous() 

False 

sage: (x^2*y + y^2*x).is_homogeneous() 

True 

""" 

return self.element().is_homogeneous() 

 

def _homogenize(self, var): 

r""" 

Return ``self`` if ``self`` is homogeneous. 

Otherwise return a homogenized polynomial constructed by modifying 

the degree of the variable with index ``var``. 

 

INPUT: 

 

 

- ``var`` - an integer indicating which variable to 

use to homogenize (0 <= var < parent(self).ngens()) 

 

 

OUTPUT: a multivariate polynomial 

 

EXAMPLES:: 

 

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

sage: f = x^2 + y + 1 + 5*x*y^1 

sage: g = f.homogenize('z'); g # indirect doctest 

x^2 + 5*x*y + y*z + z^2 

sage: g.parent() 

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

 

SEE: ``self.homogenize`` 

""" 

if self.is_homogeneous(): 

return self 

X = self.element().homogenize(var) 

R = self.parent() 

return R(X) 

 

def is_generator(self): 

""" 

Returns True if self is a generator of it's parent. 

 

EXAMPLES:: 

 

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

sage: x.is_generator() 

True 

sage: (x+y-y).is_generator() 

True 

sage: (x*y).is_generator() 

False 

""" 

d = self.element().dict() 

if len(d) == 1: 

(e, c), = d.items() 

if c.is_one() and len(e.nonzero_positions()) == 1 and e.nonzero_values()[0] == 1: 

return True 

return False 

 

def is_monomial(self): 

""" 

Returns True if self is a monomial, which we define to be a 

product of generators with coefficient 1. 

 

Use is_term to allow the coefficient to not be 1. 

 

EXAMPLES:: 

 

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

sage: x.is_monomial() 

True 

sage: (x+2*y).is_monomial() 

False 

sage: (2*x).is_monomial() 

False 

sage: (x*y).is_monomial() 

True 

 

To allow a non-1 leading coefficient, use is_term():: 

 

sage: (2*x*y).is_term() 

True 

sage: (2*x*y).is_monomial() 

False 

""" 

term = (len(self.element().dict().keys()) == 1) 

if term: 

if self.coefficients()[0] == 1: 

return True 

else: 

return False 

else: 

return False 

 

def is_term(self): 

""" 

Returns True if self is a term, which we define to be a 

product of generators times some coefficient, which need 

not be 1. 

 

Use :meth:`is_monomial` to require that the coefficient be 1. 

 

EXAMPLES:: 

 

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

sage: x.is_term() 

True 

sage: (x+2*y).is_term() 

False 

sage: (2*x).is_term() 

True 

sage: (7*x^5*y).is_term() 

True 

 

To require leading coefficient 1, use is_monomial():: 

 

sage: (2*x*y).is_monomial() 

False 

sage: (2*x*y).is_term() 

True 

""" 

return len(self.element().dict().keys()) == 1 

 

def subs(self, fixed=None, **kw): 

""" 

Fixes some given variables in a given multivariate polynomial and 

returns the changed multivariate polynomials. The polynomial itself 

is not affected. The variable,value pairs for fixing are to be 

provided as a dictionary of the form {variable:value}. 

 

This is a special case of evaluating the polynomial with some of 

the variables constants and the others the original variables. 

 

INPUT: 

 

 

- ``fixed`` - (optional) dictionary of inputs 

 

- ``**kw`` - named parameters 

 

 

OUTPUT: new MPolynomial 

 

EXAMPLES:: 

 

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

sage: f = x^2 + y + x^2*y^2 + 5 

sage: f((5,y)) 

25*y^2 + y + 30 

sage: f.subs({x:5}) 

25*y^2 + y + 30 

""" 

variables = list(self.parent().gens()) 

for i in range(0,len(variables)): 

if str(variables[i]) in kw: 

variables[i]=kw[str(variables[i])] 

elif fixed and variables[i] in fixed: 

variables[i] = fixed[variables[i]] 

return self(tuple(variables)) 

 

def monomials(self): 

""" 

Returns the list of monomials in self. The returned list is 

decreasingly ordered by the term ordering of self.parent(). 

 

OUTPUT: list of MPolynomials representing Monomials 

 

EXAMPLES:: 

 

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

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

sage: f.monomials() 

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

 

:: 

 

sage: R.<fx,fy,gx,gy> = QQbar[] 

sage: F = ((fx*gy - fy*gx)^3) 

sage: F 

-fy^3*gx^3 + 3*fx*fy^2*gx^2*gy + (-3)*fx^2*fy*gx*gy^2 + fx^3*gy^3 

sage: F.monomials() 

[fy^3*gx^3, fx*fy^2*gx^2*gy, fx^2*fy*gx*gy^2, fx^3*gy^3] 

sage: F.coefficients() 

[-1, 3, -3, 1] 

sage: sum(map(mul,zip(F.coefficients(),F.monomials()))) == F 

True 

""" 

ring = self.parent() 

one = ring.base_ring()(1) 

return [MPolynomial_polydict(ring, polydict.PolyDict({m:one}, force_int_exponents=False, force_etuples=False)) for m in self.exponents()] 

try: 

return self.__monomials 

except AttributeError: 

ring = self.parent() 

one = self.parent().base_ring()(1) 

self.__monomials = sorted([ MPolynomial_polydict(ring, polydict.PolyDict( {m:one}, force_int_exponents=False, force_etuples=False ) ) \ 

for m in self._MPolynomial_element__element.dict().keys() ], reverse=True) 

return self.__monomials 

 

def constant_coefficient(self): 

""" 

Return the constant coefficient of this multivariate polynomial. 

 

EXAMPLES:: 

 

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

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

sage: f.constant_coefficient() 

5 

sage: f = 3*x^2 

sage: f.constant_coefficient() 

0 

""" 

#v = (0,)*int(self.parent().ngens()) 

d = self.element().dict() 

try: 

return d[polydict.ETuple({},self.parent().ngens())] 

except KeyError: 

return self.parent().base_ring()(0) 

 

def is_univariate(self): 

""" 

Returns True if this multivariate polynomial is univariate and 

False otherwise. 

 

EXAMPLES:: 

 

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

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

sage: f.is_univariate() 

False 

sage: g = f.subs({x:10}); g 

700*y^2 + (-2)*y + 305 

sage: g.is_univariate() 

True 

sage: f = x^0 

sage: f.is_univariate() 

True 

""" 

mons = self.element().dict().keys() 

 

found = -1 

for mon in mons: 

for i in mon.nonzero_positions(): 

if found != i: 

if found != -1: 

return False 

else: 

found = i 

return True 

 

def univariate_polynomial(self, R=None): 

""" 

Returns a univariate polynomial associated to this multivariate 

polynomial. 

 

INPUT: 

 

 

- ``R`` - (default: None) PolynomialRing 

 

 

If this polynomial is not in at most one variable, then a 

ValueError exception is raised. This is checked using the 

is_univariate() method. The new Polynomial is over the same base 

ring as the given MPolynomial. 

 

EXAMPLES:: 

 

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

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

sage: f.univariate_polynomial() 

Traceback (most recent call last): 

... 

TypeError: polynomial must involve at most one variable 

sage: g = f.subs({x:10}); g 

700*y^2 + (-2)*y + 305 

sage: g.univariate_polynomial () 

700*y^2 - 2*y + 305 

sage: g.univariate_polynomial(PolynomialRing(QQ,'z')) 

700*z^2 - 2*z + 305 

 

TESTS:: 

 

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

sage: P(5).univariate_polynomial() 

5 

""" 

if self.parent().ngens() == 0: 

if R is None: 

return self.base_ring()(self) 

else: 

return R(self) 

 

if not self.is_univariate(): 

raise TypeError("polynomial must involve at most one variable") 

 

#construct ring if None 

if R is None: 

# constant, we just pick first variable from parent 

if self.is_constant(): 

R = self.base_ring()[self.parent().variable_names()[0]] 

else: 

R = self.base_ring()[str(self.variables()[0])] 

 

monomial_coefficients = self._MPolynomial_element__element.dict() 

 

if( not self.is_constant() ): 

var_idx = self.degrees().nonzero_positions()[0] #variable 

else: 

var_idx = 0; #constant 

if( len(monomial_coefficients.keys())==0 ): 

return R(0) 

 

#construct list 

lookup = [int(0),] * len(next(iter(monomial_coefficients))) 

coefficients = [] 

for degree in range(max(m[var_idx] 

for m in monomial_coefficients.keys()) + 1): 

lookup[var_idx] = int(degree) 

try: 

coefficients.append( monomial_coefficients[ polydict.ETuple(lookup) ] ) #if we find something, add the coefficient 

except KeyError: 

coefficients.append( 0 ) #else add zero 

 

#construct polynomial 

return R(coefficients) 

 

def variables(self): 

""" 

Returns the tuple of variables occurring in this polynomial. 

 

EXAMPLES:: 

 

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

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

sage: f.variables() 

(x, y) 

sage: g = f.subs({x:10}); g 

700*y^2 + (-2)*y + 305 

sage: g.variables() 

(y,) 

 

TESTS: 

 

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

 

sage: x,y,z=polygens(QQ,'x,y,z') 

sage: (x^2).variables() 

(x,) 

""" 

return tuple([self.parent().gen(index) for index in self.degrees().nonzero_positions()]) 

 

def variable(self,i): 

""" 

Returns `i`-th variable occurring in this polynomial. 

 

EXAMPLES:: 

 

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

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

sage: f.variable(0) 

x 

sage: f.variable(1) 

y 

""" 

return self.variables()[int(i)] 

 

def nvariables(self): 

""" 

Number of variables in this polynomial 

 

EXAMPLES:: 

 

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

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

sage: f.nvariables () 

2 

sage: g = f.subs({x:10}); g 

700*y^2 + (-2)*y + 305 

sage: g.nvariables () 

1 

""" 

return len(self.degrees().nonzero_positions()) 

 

def is_constant(self): 

""" 

True if polynomial is constant, and False otherwise. 

 

EXAMPLES:: 

 

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

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

sage: f.is_constant() 

False 

sage: g = 10*x^0 

sage: g.is_constant() 

True 

""" 

if len(self.dict()) <= 1 and self.degrees().is_constant(): 

return True 

else: 

return False 

 

def lm(self): 

""" 

Returns the lead monomial of self with respect to the term order of 

self.parent(). 

 

EXAMPLES:: 

 

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

sage: (x^1*y^2 + y^3*z^4).lm() 

x*y^2 

sage: (x^3*y^2*z^4 + x^3*y^2*z^1).lm() 

x^3*y^2*z^4 

 

:: 

 

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

sage: (x^1*y^2*z^3 + x^3*y^2*z^0).lm() 

x*y^2*z^3 

sage: (x^1*y^2*z^4 + x^1*y^1*z^5).lm() 

x*y^2*z^4 

 

:: 

 

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

sage: (x^1*y^5*z^2 + x^4*y^1*z^3).lm() 

x*y^5*z^2 

sage: (x^4*y^7*z^1 + x^4*y^2*z^3).lm() 

x^4*y^7*z 

 

TESTS:: 

 

sage: from sage.rings.polynomial.multi_polynomial_ring import MPolynomialRing_polydict 

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

sage: f=x+y 

sage: f.lm() 

x 

 

""" 

try: 

return self.__lm 

except AttributeError: 

if self.is_zero(): 

return self 

R = self.parent() 

f = self._MPolynomial_element__element.lcmt( R.term_order().greater_tuple ) 

one = R.base_ring()(1) 

self.__lm = MPolynomial_polydict(R,polydict.PolyDict({f:one},zero=R.base_ring().zero(),force_int_exponents=False, force_etuples=False)) 

return self.__lm 

 

def lc(self): 

""" 

Returns the leading coefficient of self i.e., 

self.coefficient(self.lm()) 

 

EXAMPLES:: 

 

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

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

sage: f.lc() 

3 

""" 

try: 

return self.__lc 

except AttributeError: 

if self.is_zero(): 

return self.base_ring()._zero_element 

R = self.parent() 

f = self._MPolynomial_element__element.dict() 

self.__lc = f[self._MPolynomial_element__element.lcmt( R.term_order().greater_tuple )] 

return self.__lc 

 

def lt(self): 

""" 

Returns the leading term of self i.e., self.lc()\*self.lm(). The 

notion of "leading term" depends on the ordering defined in the 

parent ring. 

 

EXAMPLES:: 

 

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

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

sage: f.lt() 

3*x^2 

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

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

sage: f.lt() 

-y^2 

 

TESTS:: 

 

sage: from sage.rings.polynomial.multi_polynomial_ring import MPolynomialRing_polydict 

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

sage: f=x+y 

sage: f.lt() 

x 

 

""" 

try: 

return self.__lt 

except AttributeError: 

if self.is_zero(): 

return self 

R = self.parent() 

f = self._MPolynomial_element__element.dict() 

res = self._MPolynomial_element__element.lcmt( R.term_order().greater_tuple ) 

self.__lt = MPolynomial_polydict(R,polydict.PolyDict({res:f[res]},zero=R.base_ring().zero(),force_int_exponents=False, force_etuples=False)) 

return self.__lt 

 

def __eq__(self,right): 

if not isinstance(right, MPolynomial_polydict): 

# we want comparison with zero to be fast 

if not right: 

return not self._MPolynomial_element__element.dict() 

return CommutativeRingElement.__eq__(self, right) 

return self._MPolynomial_element__element == right._MPolynomial_element__element 

 

def __ne__(self,right): 

if not isinstance(right, MPolynomial_polydict): 

# we want comparison with zero to be fast 

if not right: 

return not not self._MPolynomial_element__element.dict() 

return CommutativeRingElement.__ne__(self, right) 

return self._MPolynomial_element__element != right._MPolynomial_element__element 

 

def __bool__(self): 

""" 

Returns True if self != 0 

 

.. note:: 

 

This is much faster than actually writing ``self == 0``. 

""" 

return self._MPolynomial_element__element.dict()!={} 

 

__nonzero__ = __bool__ 

 

def _floordiv_(self, right): 

r""" 

Quotient of division of self by other. This is denoted //. 

 

.. note:: 

 

It's not clear to me that this is well-defined if 

``self`` is not exactly divisible by other. 

 

EXAMPLES:: 

 

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

sage: 2*x*y//y 

2*x 

sage: 2*x//y 

0 

sage: 2*x//4 

1/2*x 

sage: type(0//y) 

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

""" 

# handle division by monomials without using Singular 

if len(right.dict()) == 1: 

P = self.parent() 

ret = P(0) 

denC,denM = next(iter(right)) 

for c,m in self: 

t = c*m 

if denC.divides(c) and P.monomial_divides(denM, m): 

ret += P.monomial_quotient(t, right, coeff=True) 

return ret 

 

Q, _ = self.quo_rem(right) 

return Q 

 

def _derivative(self, var=None): 

r""" 

Differentiates ``self`` with respect to variable ``var``. 

 

If ``var`` is not one of the generators of this ring, _derivative(var) 

is called recursively on each coefficient of this polynomial. 

 

.. SEEALSO:: 

 

:meth:`derivative` 

 

EXAMPLES:: 

 

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

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

sage: f = (t^2 + O(t^3))*x^2*y^3 + (37*t^4 + O(t^5))*x^3 

sage: f.parent() 

Multivariate Polynomial Ring in x, y over Power Series Ring in t over Algebraic Field 

sage: f._derivative(x) # with respect to x 

(2*t^2 + O(t^3))*x*y^3 + (111*t^4 + O(t^5))*x^2 

sage: f._derivative(x).parent() 

Multivariate Polynomial Ring in x, y over Power Series Ring in t over Algebraic Field 

sage: f._derivative(y) # with respect to y 

(3*t^2 + O(t^3))*x^2*y^2 

sage: f._derivative(t) # with respect to t (recurses into base ring) 

(2*t + O(t^2))*x^2*y^3 + (148*t^3 + O(t^4))*x^3 

sage: f._derivative(x)._derivative(y) # with respect to x and then y 

(6*t^2 + O(t^3))*x*y^2 

sage: f.derivative(y, 3) # with respect to y three times 

(6*t^2 + O(t^3))*x^2 

sage: f._derivative() # can't figure out the variable 

Traceback (most recent call last): 

... 

ValueError: must specify which variable to differentiate with respect to 

""" 

if var is None: 

raise ValueError("must specify which variable to differentiate with respect to") 

 

gens = list(self.parent().gens()) 

 

# check if var is one of the generators 

try: 

index = gens.index(var) 

except ValueError: 

# var is not a generator; do term-by-term differentiation recursively 

# var may be, for example, a generator of the base ring 

d = dict([(e, x._derivative(var)) for (e, x) in iteritems(self.dict())]) 

d = polydict.PolyDict(d, self.parent().base_ring()(0), remove_zero=True) 

return MPolynomial_polydict(self.parent(), d) 

 

# differentiate w.r.t. indicated variable 

d = {} 

v = polydict.ETuple({index:1}, len(gens)) 

for (exp, coeff) in iteritems(self.dict()): 

if exp[index] > 0: 

d[exp.esub(v)] = coeff * exp[index] 

d = polydict.PolyDict(d, self.parent().base_ring()(0), remove_zero=True) 

return MPolynomial_polydict(self.parent(), d) 

 

def integral(self, var=None): 

r""" 

Integrates ``self`` with respect to variable ``var``. 

 

.. NOTE:: 

 

The integral is always chosen so the constant term is 0. 

 

If ``var`` is not one of the generators of this ring, integral(var) 

is called recursively on each coefficient of this polynomial. 

 

EXAMPLES: 

 

On polynomials with rational coefficients:: 

 

sage: x, y = PolynomialRing(QQ, 'x, y').gens() 

sage: ex = x*y + x - y 

sage: it = ex.integral(x); it 

1/2*x^2*y + 1/2*x^2 - x*y 

sage: it.parent() == x.parent() 

True 

 

On polynomials with coefficients in power series:: 

 

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

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

sage: f = (t^2 + O(t^3))*x^2*y^3 + (37*t^4 + O(t^5))*x^3 

sage: f.parent() 

Multivariate Polynomial Ring in x, y over Power Series Ring in t over Algebraic Field 

sage: f.integral(x) # with respect to x 

(1/3*t^2 + O(t^3))*x^3*y^3 + (37/4*t^4 + O(t^5))*x^4 

sage: f.integral(x).parent() 

Multivariate Polynomial Ring in x, y over Power Series Ring in t over Algebraic Field 

 

sage: f.integral(y) # with respect to y 

(1/4*t^2 + O(t^3))*x^2*y^4 + (37*t^4 + O(t^5))*x^3*y 

sage: f.integral(t) # with respect to t (recurses into base ring) 

(1/3*t^3 + O(t^4))*x^2*y^3 + (37/5*t^5 + O(t^6))*x^3 

 

TESTS:: 

 

sage: f.integral() # can't figure out the variable 

Traceback (most recent call last): 

... 

ValueError: must specify which variable to integrate with respect to 

""" 

if var is None: 

raise ValueError("must specify which variable to integrate " 

"with respect to") 

 

gens = list(self.parent().gens()) 

 

# check if var is one of the generators 

try: 

index = gens.index(var) 

except ValueError: 

# var is not a generator; do term-by-term integration recursively 

# var may be, for example, a generator of the base ring 

d = dict([(e, x.integral(var)) 

for (e, x) in iteritems(self.dict())]) 

d = polydict.PolyDict(d, self.parent().base_ring()(0), 

remove_zero=True) 

return MPolynomial_polydict(self.parent(), d) 

 

# integrate w.r.t. indicated variable 

d = {} 

v = polydict.ETuple({index:1}, len(gens)) 

for (exp, coeff) in iteritems(self.dict()): 

d[exp.eadd(v)] = coeff / (1+exp[index]) 

d = polydict.PolyDict(d, self.parent().base_ring()(0), remove_zero=True) 

return MPolynomial_polydict(self.parent(), d) 

 

def factor(self, proof=True): 

r""" 

Compute the irreducible factorization of this polynomial. 

 

INPUT: 

 

- ``proof'' - insist on provably correct results (ignored, always ``True``) 

 

ALGORITHM: Use univariate factorization code. 

 

If a polynomial is univariate, the appropriate univariate 

factorization code is called:: 

 

sage: R.<z> = PolynomialRing(CC,1) 

sage: f = z^4 - 6*z + 3 

sage: f.factor() 

(z - 1.60443920904349) * (z - 0.511399619393097) * (z + 1.05791941421830 - 1.59281852704435*I) * (z + 1.05791941421830 + 1.59281852704435*I) 

 

TESTS: 

 

Check if we can handle polynomials with no variables, see :trac:`7950`:: 

 

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

sage: res = P(10).factor(); res 

2 * 5 

sage: res[0][0].parent() 

Multivariate Polynomial Ring in no variables over Integer Ring 

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

sage: res = R(10).factor(); res 

10 

sage: res.unit().parent() 

Rational Field 

sage: P(0).factor() 

Traceback (most recent call last): 

... 

ArithmeticError: factorization of 0 is not defined 

 

Check if we can factor a constant polynomial, see :trac:`8207`:: 

 

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

sage: R(1).factor() 

1.00000000000000 

 

Check that we prohibit too large moduli, :trac:`11829`:: 

 

sage: R.<x,y> = GF(previous_prime(2^31))[] 

sage: factor(x+y+1,proof=False) 

Traceback (most recent call last): 

... 

NotImplementedError: Factorization of multivariate polynomials over prime fields with characteristic > 2^29 is not implemented. 

 

We check that the original issue in :trac:`7554` is fixed:: 

 

sage: K.<a> = PolynomialRing(QQ) 

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

sage: factor(x) 

x 

""" 

R = self.parent() 

 

# raise error if trying to factor zero 

if not self: 

raise ArithmeticError("factorization of {!r} is not defined".format(self)) 

 

# if number of variables is zero ... 

if R.ngens() == 0: 

base_ring = self.base_ring() 

if base_ring.is_field(): 

return Factorization([],unit=self.base_ring()(self)) 

else: 

F = base_ring(self).factor() 

return Factorization([(R(f),m) for f,m in F], unit=F.unit()) 

 

# try to use univariate factoring 

try: 

F = self.univariate_polynomial().factor() 

return Factorization([(R(f),m) for f,m in F], unit=F.unit()) 

except TypeError: 

pass 

 

base_ring = self.base_ring() 

if base_ring.is_finite(): 

if base_ring.characteristic() > 1<<29: 

raise NotImplementedError("Factorization of multivariate polynomials over prime fields with characteristic > 2^29 is not implemented.") 

if proof: 

raise NotImplementedError("proof = True factorization not implemented. Call factor with proof=False.") 

 

R._singular_().set_ring() 

S = self._singular_().factorize() 

factors = S[1] 

exponents = S[2] 

v = sorted([(R(factors[i+1]), sage.rings.integer.Integer(exponents[i+1])) \ 

for i in range(len(factors))]) 

unit = R(1) 

for i in range(len(v)): 

if v[i][0].is_unit(): 

unit = unit * v[i][0] 

del v[i] 

break 

F = sorted(Factorization(v, unit=unit)) 

return F 

 

def lift(self,I): 

""" 

given an ideal I = (f_1,...,f_r) and some g (== self) in I, find 

s_1,...,s_r such that g = s_1 f_1 + ... + s_r f_r 

 

ALGORITHM: Use Singular. 

 

EXAMPLES:: 

 

sage: A.<x,y> = PolynomialRing(CC,2,order='degrevlex') 

sage: I = A.ideal([x^10 + x^9*y^2, y^8 - x^2*y^7 ]) 

sage: f = x*y^13 + y^12 

sage: M = f.lift(I) 

sage: M 

[y^7, x^7*y^2 + x^8 + x^5*y^3 + x^6*y + x^3*y^4 + x^4*y^2 + x*y^5 + x^2*y^3 + y^4] 

sage: sum( map( mul , zip( M, I.gens() ) ) ) == f 

True 

""" 

fs = self._singular_() 

Is = I._singular_() 

P = I.ring() 

try: 

M = Is.lift(fs)._sage_(P) 

except TypeError: 

raise ArithmeticError("f is not in I") 

return Sequence(M.list(), P, check=False, immutable=True) 

 

@coerce_binop 

def quo_rem(self, right): 

""" 

Returns quotient and remainder of self and right. 

 

EXAMPLES:: 

 

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

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

sage: f.quo_rem(x) 

(x*y + 1.00000000000000, 1.00000000000000) 

 

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

sage: p1 = R('a + (1+2*b)*x*y + (3-a^2)*z') 

sage: p2 = R('x-1') 

sage: p1.quo_rem(p2) 

((2*b + 1)*y, (2*b + 1)*y + (-a^2 + 3)*z + a) 

 

sage: R.<x,y> = Qp(5)[] 

sage: x.quo_rem(y) 

Traceback (most recent call last): 

... 

TypeError: no conversion of this ring to a Singular ring defined 

 

ALGORITHM: Use Singular. 

""" 

R = self.parent() 

try: 

R._singular_().set_ring() 

except TypeError: 

f = self.parent().flattening_morphism() 

if f.domain() != f.codomain(): 

g = f.section() 

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

return g(q), g(r) 

else: 

raise 

else: 

X = self._singular_().division(right._singular_()) 

return R(X[1][1,1]), R(X[2][1]) 

 

def resultant(self, other, variable=None): 

""" 

Compute the resultant of ``self`` and ``other`` with respect 

to ``variable``. 

 

If a second argument is not provided, the first variable of 

``self.parent()`` is chosen. 

 

INPUT: 

 

- ``other`` -- polynomial in ``self.parent()`` 

 

- ``variable`` -- (optional) variable (of type polynomial) in 

``self.parent()`` 

 

EXAMPLES:: 

 

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

sage: a = x + y 

sage: b = x^3 - y^3 

sage: a.resultant(b) 

-2*y^3 

sage: a.resultant(b, y) 

2*x^3 

 

TESTS:: 

 

sage: from sage.rings.polynomial.multi_polynomial_ring import MPolynomialRing_polydict_domain 

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

sage: a = x + y 

sage: b = x^3 - y^3 

sage: a.resultant(b) 

-2*y^3 

sage: a.resultant(b, y) 

2*x^3 

 

Check that :trac:`15061` is fixed:: 

 

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

sage: (x^2 + 1).resultant(x^2 - y) 

y^2 + 2*y + 1 

 

""" 

R = self.parent() 

if variable is None: 

variable = R.gen(0) 

if R._has_singular: 

rt = self._singular_().resultant(other._singular_(), variable._singular_()) 

r = rt.sage_poly(R) 

else: 

r = self.sylvester_matrix(other, variable).det() 

if R.ngens() <= 1 and r.degree() <= 0: 

return R.base_ring()(r[0]) 

else: 

return r 

 

def reduce(self, I): 

""" 

Reduce this polynomial by the polynomials in `I`. 

 

INPUT: 

 

- ``I`` - a list of polynomials or an ideal 

 

EXAMPLES:: 

 

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

sage: f1 = -2 * x^2 + x^3 

sage: f2 = -2 * y + x* y 

sage: f3 = -x^2 + y^2 

sage: F = Ideal([f1,f2,f3]) 

sage: g = x*y - 3*x*y^2 

sage: g.reduce(F) 

(-6)*y^2 + 2*y 

sage: g.reduce(F.gens()) 

(-6)*y^2 + 2*y 

 

:: 

 

sage: f = 3*x 

sage: f.reduce([2*x,y]) 

0 

 

:: 

 

sage: k.<w> = CyclotomicField(3) 

sage: A.<y9,y12,y13,y15> = PolynomialRing(k) 

sage: J = [ y9 + y12] 

sage: f = y9 - y12; f.reduce(J) 

-2*y12 

sage: f = y13*y15; f.reduce(J) 

y13*y15 

sage: f = y13*y15 + y9 - y12; f.reduce(J) 

y13*y15 - 2*y12 

 

Make sure the remainder returns the correct type, fixing :trac:`13903`:: 

 

sage: R.<y1,y2>=PolynomialRing(Qp(5),2, order='lex') 

sage: G=[y1^2 + y2^2, y1*y2 + y2^2, y2^3] 

sage: type((y2^3).reduce(G)) 

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

""" 

from sage.rings.polynomial.multi_polynomial_ideal import MPolynomialIdeal 

 

k = self.base_ring() 

P = self.parent() 

 

if isinstance(I, MPolynomialIdeal): 

I = I.gens() 

 

if not k.is_field(): 

raise TypeError("Can only reduce polynomials over fields.") 

 

try: 

fs = self._singular_() 

Is = fs.parent().ideal(I) 

return P(fs.reduce(Is)) 

except (NotImplementedError, TypeError): 

pass 

 

lI = len(I) 

I = list(I) 

r = P.zero() 

p = self 

 

while p != 0: 

for i in range(lI): 

gi = I[i] 

plm = p.lm() 

gilm = gi.lm() 

if P.monomial_divides(gilm, plm): 

quot = p.lc()/gi.lc() * P.monomial_quotient(plm, gilm) 

p -= quot*I[i] 

break 

else: 

plt = p.lt() 

r += plt 

p -= plt 

return r 

 

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

# Useful for some geometry code. 

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

 

def degree_lowest_rational_function(r,x): 

r""" 

INPUT: 

 

 

- ``r`` - a multivariate rational function 

 

- ``x`` - a multivariate polynomial ring generator x 

 

 

OUTPUT: 

 

 

- ``integer`` - the degree of r in x and its "leading" 

(in the x-adic sense) coefficient. 

 

 

.. note:: 

 

This function is dependent on the ordering of a python dict. 

Thus, it isn't really mathematically well-defined. I think that 

it should made a method of the FractionFieldElement class and 

rewritten. 

 

EXAMPLES:: 

 

sage: R1 = PolynomialRing(FiniteField(5), 3, names = ["a","b","c"]) 

sage: F = FractionField(R1) 

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

sage: f = 3*a*b^2*c^3+4*a*b*c 

sage: g = a^2*b*c^2+2*a^2*b^4*c^7 

 

Consider the quotient 

`f/g = \frac{4 + 3 bc^{2}}{ac + 2 ab^{3}c^{6}}` (note the 

cancellation). 

 

:: 

 

sage: r = f/g; r 

(-b*c^2 + 2)/(a*b^3*c^6 - 2*a*c) 

sage: degree_lowest_rational_function(r,a) 

(-1, 3) 

sage: degree_lowest_rational_function(r,b) 

(0, 4) 

sage: degree_lowest_rational_function(r,c) 

(-1, 4) 

""" 

from sage.rings.fraction_field import FractionField 

R = r.parent() 

F = FractionField(R) 

r = F(r) 

if r == 0: 

return (0, F(0)) 

L = next(iter(x.dict())) 

for ix in range(len(L)): 

if L[ix] != 0: 

break 

f = r.numerator() 

g = r.denominator() 

M = f.dict() 

keys = list(M.keys()) 

numtermsf = len(M) 

degreesf = [keys[j][ix] for j in range(numtermsf)] 

lowdegf = min(degreesf) 

cf = M[keys[degreesf.index(lowdegf)]] ## constant coeff of lowest degree term 

M = g.dict() 

keys = list(M.keys()) 

numtermsg = len(M) 

degreesg = [keys[j][ix] for j in range(numtermsg)] 

lowdegg = min(degreesg) 

cg = M[keys[degreesg.index(lowdegg)]] ## constant coeff of lowest degree term 

return (lowdegf-lowdegg,cf/cg)