Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

1715

1716

1717

1718

1719

1720

1721

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

1952

1953

1954

1955

1956

1957

1958

1959

1960

1961

1962

1963

1964

1965

1966

1967

1968

1969

1970

1971

1972

1973

1974

1975

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

1986

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1998

1999

2000

2001

2002

2003

2004

2005

2006

2007

2008

2009

2010

2011

2012

2013

2014

2015

2016

2017

2018

2019

2020

2021

2022

2023

2024

2025

2026

2027

2028

2029

2030

2031

2032

2033

2034

2035

2036

2037

2038

2039

2040

2041

2042

2043

2044

2045

2046

2047

2048

2049

2050

2051

2052

2053

2054

2055

2056

2057

2058

2059

2060

2061

2062

2063

2064

2065

2066

2067

2068

2069

2070

2071

2072

2073

2074

2075

2076

2077

2078

2079

2080

2081

2082

2083

2084

2085

2086

2087

2088

2089

2090

2091

2092

2093

2094

2095

2096

2097

2098

2099

2100

2101

2102

2103

2104

2105

2106

2107

2108

2109

2110

2111

2112

2113

2114

2115

2116

2117

2118

2119

2120

2121

2122

2123

2124

2125

2126

2127

2128

2129

2130

2131

2132

2133

2134

2135

2136

2137

2138

2139

2140

2141

2142

2143

2144

2145

2146

2147

2148

2149

2150

2151

2152

2153

2154

2155

2156

2157

2158

2159

2160

2161

2162

2163

2164

2165

2166

2167

2168

2169

2170

2171

2172

2173

2174

2175

2176

2177

2178

2179

2180

2181

2182

2183

2184

2185

2186

2187

2188

2189

2190

2191

2192

2193

2194

2195

2196

2197

2198

2199

2200

2201

2202

2203

2204

2205

2206

2207

2208

2209

2210

2211

2212

2213

2214

2215

2216

2217

2218

2219

2220

2221

2222

2223

2224

2225

2226

2227

2228

2229

2230

2231

2232

2233

2234

2235

2236

2237

2238

2239

2240

2241

2242

2243

2244

2245

2246

2247

2248

2249

2250

2251

2252

2253

2254

2255

2256

2257

2258

2259

2260

2261

2262

2263

2264

2265

2266

2267

2268

2269

2270

2271

2272

2273

2274

2275

2276

2277

2278

2279

2280

2281

2282

2283

2284

2285

2286

2287

2288

2289

2290

2291

2292

2293

2294

2295

2296

2297

2298

2299

2300

2301

2302

2303

2304

2305

2306

2307

2308

2309

2310

2311

2312

2313

2314

2315

2316

2317

2318

2319

2320

2321

2322

2323

2324

2325

2326

2327

2328

2329

2330

2331

2332

2333

2334

2335

2336

2337

2338

2339

2340

2341

2342

2343

2344

2345

2346

2347

2348

2349

2350

2351

2352

2353

2354

2355

2356

2357

2358

2359

2360

2361

2362

2363

2364

2365

2366

2367

2368

2369

2370

2371

2372

2373

2374

2375

2376

2377

2378

2379

2380

2381

2382

2383

2384

2385

2386

2387

2388

2389

2390

2391

2392

2393

2394

2395

2396

2397

2398

2399

2400

2401

2402

2403

2404

2405

2406

2407

2408

2409

2410

2411

2412

2413

2414

2415

2416

2417

2418

2419

2420

2421

2422

2423

2424

2425

2426

2427

2428

2429

2430

2431

2432

2433

2434

2435

2436

2437

2438

2439

2440

2441

2442

2443

2444

r""" 

Univariate polynomials over `\QQ` implemented via FLINT 

  

AUTHOR: 

  

- Sebastian Pancratz 

""" 

  

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

# Copyright (C) 2010 Sebastian Pancratz <sfp@pancratz.org> 

# 

# 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 cysignals.memory cimport check_allocarray, check_malloc, sig_free 

from cysignals.signals cimport sig_on, sig_str, sig_off 

  

from cpython.int cimport PyInt_AS_LONG 

from sage.arith.long cimport pyobject_to_long 

  

from sage.libs.gmp.mpz cimport * 

from sage.libs.gmp.mpq cimport * 

from sage.libs.flint.fmpz cimport * 

from sage.libs.flint.fmpq cimport * 

from sage.libs.flint.fmpz_poly cimport * 

from sage.libs.flint.fmpq_poly cimport * 

  

from sage.interfaces.all import singular as singular_default 

  

from cypari2.gen import Gen as pari_gen 

  

from sage.rings.integer cimport Integer, smallInteger 

from sage.rings.integer_ring import ZZ 

from sage.rings.fraction_field_element import FractionFieldElement 

from sage.rings.rational cimport Rational 

from sage.rings.rational_field import QQ 

from sage.rings.polynomial.polynomial_element cimport Polynomial 

from sage.rings.polynomial.polynomial_integer_dense_flint cimport Polynomial_integer_dense_flint 

  

from sage.structure.parent cimport Parent 

from sage.structure.element cimport Element, ModuleElement, RingElement 

from sage.structure.element import coerce_binop 

  

from sage.misc.cachefunc import cached_method 

  

cdef inline bint _do_sig(fmpq_poly_t op): 

""" 

Returns 1 when signal handling should be carried out for an operation 

on this polynomial and 0 otherwise. 

  

Strictly speaking, whether or not signal handling should be carried 

ought to depend on the operation as well as the operands in question. 

For simplicity we carry out signal handling for all but the simplest 

of operands regardless of the operation. 

  

TESTS:: 

  

sage: R.<t> = QQ[] 

sage: f = 1 + t/2 

sage: g = 2/3 + t^2 

sage: _ = f * g # indirect doctest 

""" 

# Trac #12173: check that the degree is greater than 1000 before computing 

# the max limb size 

return (fmpq_poly_length(op) > 0 and 

(fmpq_poly_degree(op) > 1000 or 

sage_fmpq_poly_max_limbs(op) > 1)) 

  

cdef class Polynomial_rational_flint(Polynomial): 

""" 

Univariate polynomials over the rationals, implemented via FLINT. 

  

Internally, we represent rational polynomial as the quotient of an integer 

polynomial and a positive denominator which is coprime to the content of 

the numerator. 

  

TESTS:: 

  

sage: f = QQ['x'].random_element() 

sage: from sage.rings.polynomial.polynomial_rational_flint import Polynomial_rational_flint 

sage: isinstance(f, Polynomial_rational_flint) 

True 

  

.. automethod:: _add_ 

.. automethod:: _sub_ 

.. automethod:: _lmul_ 

.. automethod:: _rmul_ 

.. automethod:: _mul_ 

.. automethod:: _mul_trunc_ 

""" 

  

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

# Allocation & initialisation # 

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

  

cdef Polynomial_rational_flint _new(self): 

""" 

Quickly creates a new polynomial object in this class. 

  

OUTPUT: 

  

- Polynomial of type Polynomial_rational_flint 

  

TESTS:: 

  

sage: R.<t> = QQ[] 

sage: f = 2/3*t^2 

sage: g = -1/2*t + 2 

sage: f + g # indirect doctest 

2/3*t^2 - 1/2*t + 2 

""" 

cdef Polynomial_rational_flint res = Polynomial_rational_flint.__new__(Polynomial_rational_flint) 

res._parent = self._parent 

res._is_gen = 0 

return res 

  

cpdef Polynomial _new_constant_poly(self, x, Parent P): 

r""" 

Quickly creates a new constant polynomial with value x in parent P 

  

ASSUMPTION: 

  

x must be a rational or convertible to an int. 

  

EXAMPLES:: 

  

sage: R.<x> = QQ[] 

sage: x._new_constant_poly(2/1,R) 

2 

sage: x._new_constant_poly(2,R) 

2 

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

2 

sage: x._new_constant_poly("2.1",R) 

Traceback (most recent call last): 

... 

ValueError: invalid literal for int() with base 10: '2.1' 

""" 

cdef Polynomial_rational_flint res = Polynomial_rational_flint.__new__(Polynomial_rational_flint) 

res._parent = P 

res._is_gen = <char>0 

if isinstance(x, int): 

fmpq_poly_set_si(res.__poly, <int> x) 

  

elif isinstance(x, Integer): 

fmpq_poly_set_mpz(res.__poly, (<Integer> x).value) 

  

elif isinstance(x, Rational): 

fmpq_poly_set_mpq(res.__poly, (<Rational> x).value) 

  

else: 

fmpq_poly_set_si(res.__poly, int(x)) 

return res 

  

  

def __cinit__(self): 

""" 

Initialises the underlying data structure. 

  

TESTS:: 

  

sage: R.<t> = QQ[] 

sage: f = 2/3 * t - 7 #indirect doctest 

""" 

fmpq_poly_init(self.__poly) 

  

def __dealloc__(self): 

""" 

Deallocates the underlying data structure. 

  

TESTS:: 

  

sage: R.<t> = QQ[] 

sage: f = 1/3 * t 

sage: del f # untested 

""" 

fmpq_poly_clear(self.__poly) 

  

def __init__(self, parent, x=None, check=True, is_gen=False, construct=False): 

""" 

Initialises the associated data for the polynomial self. 

  

INPUT: 

  

- ``parent`` - Polynomial ring, the parent of self 

- ``x`` - Data for the new polynomial self, e.g. a polynomial, an 

integer, a rational, a list of rationals, a dictionary with keys 

the degrees and the rational coefficients, etc (default: ``None``) 

- `check`` - Whether the integrity of the data needs to be verified, 

largely ignored by this method (default: ``True``) 

- ``is_gen`` - Whether self shall be initialised as the generator of 

the parent polynomial ring 

- ``construct`` - Whether the element shall always be constructed 

as an independent copy of any input data (default: ``False``) 

  

TESTS:: 

  

sage: R.<t> = QQ[] 

sage: f = -4 * t^2 + 1/3 * t - 1/7 # indirect doctest 

  

sage: f = ZZ['x']([1..10^6]) 

sage: g = f.change_ring(QQ) 

sage: g[:10] 

10*x^9 + 9*x^8 + 8*x^7 + 7*x^6 + 6*x^5 + 5*x^4 + 4*x^3 + 3*x^2 + 2*x + 1 

""" 

cdef long deg 

cdef unsigned long n 

cdef Rational c 

cdef list L1 

cdef mpq_t * L2 

  

Polynomial.__init__(self, parent, is_gen=is_gen) 

  

if is_gen: 

fmpq_poly_set_coeff_si(self.__poly, 1, 1) 

  

elif isinstance(x, Polynomial_rational_flint): 

fmpq_poly_set(self.__poly, (<Polynomial_rational_flint> x).__poly) 

  

elif isinstance(x, int): 

fmpq_poly_set_si(self.__poly, <int> x) 

  

elif isinstance(x, Integer): 

fmpq_poly_set_mpz(self.__poly, (<Integer> x).value) 

  

elif isinstance(x, Rational): 

fmpq_poly_set_mpq(self.__poly, (<Rational> x).value) 

  

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

  

if len(x) == 0: 

return 

elif len(x) == 1: 

Polynomial_rational_flint.__init__(self, parent, x[0], 

check=check, is_gen=False, construct=construct) 

return 

  

L1 = [e if isinstance(e, Rational) else Rational(e) for e in x] 

n = <unsigned long> len(x) 

sig_on() 

L2 = <mpq_t *> check_allocarray(n, sizeof(mpq_t)) 

for deg from 0 <= deg < n: 

mpq_init(L2[deg]) 

mpq_set(L2[deg], (<Rational> L1[deg]).value) 

fmpq_poly_set_array_mpq(self.__poly, L2, n) 

for deg from 0 <= deg < n: 

mpq_clear(L2[deg]) 

sig_free(L2) 

sig_off() 

  

# deg = 0 

# for e in x: 

# c = Rational(e) 

# fmpq_poly_set_coeff_mpq(self.__poly, deg, c.value) 

# deg += 1 

  

elif isinstance(x, dict): 

for deg, e in x.iteritems(): 

c = Rational(e) 

fmpq_poly_set_coeff_mpq(self.__poly, deg, c.value) 

  

elif isinstance(x, pari_gen): 

k = self._parent.base_ring() 

x = [k(w) for w in x.list()] 

Polynomial_rational_flint.__init__(self, parent, x, check=True, 

is_gen=False, construct=construct) 

  

elif isinstance(x, Polynomial_integer_dense_flint): 

fmpq_poly_set_fmpz_poly(self.__poly, (<Polynomial_integer_dense_flint>x).__poly) 

  

elif isinstance(x, Polynomial): 

k = self._parent.base_ring() 

x = [k(w) for w in list(x)] 

Polynomial_rational_flint.__init__(self, parent, x, check=True, 

is_gen=False, construct=construct) 

  

elif isinstance(x, FractionFieldElement) and (x.parent().base() is parent or x.parent().base() == parent) and x.denominator() == 1: 

x = x.numerator() 

Polynomial_rational_flint.__init__(self, parent, x, check=check, 

is_gen=is_gen, construct=construct) 

  

else: 

x = parent.base_ring()(x) 

Polynomial_rational_flint.__init__(self, parent, x, check=check, 

is_gen=is_gen, construct=construct) 

  

def __reduce__(self): 

""" 

This is used when pickling polynomials. 

  

TESTS:: 

  

sage: R.<t> = QQ[] 

sage: f = 2/3 * t^2 + 1 

sage: r = f.__reduce__(); r 

(<type 'sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint'>, (Univariate Polynomial Ring in t over Rational Field, [1, 0, 2/3], False, False)) 

sage: r[0](*r[1]) 

2/3*t^2 + 1 

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

True 

""" 

return (Polynomial_rational_flint, 

(self.parent(), self.list(), False, self.is_gen())) 

  

def __copy__(self): 

""" 

Returns a copy of self. 

  

TESTS:: 

  

sage: R.<t> = QQ[] 

sage: f = 4/5 * t^3 - 1/17 

sage: copy(f) == f 

True 

""" 

cdef Polynomial_rational_flint res = self._new() 

fmpq_poly_set(res.__poly, self.__poly) 

return res 

  

def _singular_(self, singular=singular_default, have_ring=False): 

""" 

Returns a Singular representation of self. 

  

INPUT: 

  

- ``singular`` - Singular interpreter (default: default interpreter) 

- ``have_ring`` - set to True if the ring was already set in Singular 

  

EXAMPLES:: 

  

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

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

sage: singular(f) 

3*x^2+2*x+5 

""" 

if not have_ring: 

self._parent._singular_(singular).set_ring() # Expensive! 

return singular(self._singular_init_()) 

  

cpdef list list(self, bint copy=True): 

""" 

Return a list with the coefficients of ``self``. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = 1 + t + t^2/2 + t^3/3 + t^4/4 

sage: f.list() 

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

sage: g = R(0) 

sage: g.list() 

[] 

""" 

cdef unsigned long length = fmpq_poly_length(self.__poly) 

return [self.get_unsafe(n) for n in range(length)] 

  

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

# Basis access # 

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

  

def degree(self): 

""" 

Return the degree of ``self``. 

  

By convention, the degree of the zero polynomial is -1. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = 1 + t + t^2/2 + t^3/3 + t^4/4 

sage: f.degree() 

4 

sage: g = R(0) 

sage: g.degree() 

-1 

  

TESTS:: 

  

sage: type(f.degree()) 

<type 'sage.rings.integer.Integer'> 

""" 

return smallInteger(fmpq_poly_degree(self.__poly)) 

  

cdef get_unsafe(self, Py_ssize_t n): 

""" 

Return the `n`-th coefficient of ``self``. 

  

INPUT: 

  

- ``n`` -- Degree of the monomial whose coefficient is to be 

returned. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = 1 + t + t^2/2 + t^3/3 + t^4/4 

sage: f[-1], f[0], f[3], f[5] # indirect doctest 

(0, 1, 1/3, 0) 

sage: f[:3] # indirect doctest 

1/2*t^2 + t + 1 

""" 

cdef Rational z = Rational.__new__(Rational) 

fmpq_poly_get_coeff_mpq(z.value, self.__poly, n) 

return z 

  

cpdef _unsafe_mutate(self, unsigned long n, value): 

""" 

Sets the `n`-th coefficient of self to value. 

  

TESTS:: 

  

sage: R.<t> = QQ[] 

sage: f = 1 + t + t^2/2 + t^3/3 + t^4/4 

sage: f._unsafe_mutate(4, 1/5) 

sage: f 

1/5*t^4 + 1/3*t^3 + 1/2*t^2 + t + 1 

  

WARNING: 

  

Polynomials in Sage are meant to be immutable, and some methods may 

rely on this convention. This method should be used only with the 

utmost care. 

""" 

cdef bint do_sig = _do_sig(self.__poly) 

  

if isinstance(value, int): 

if do_sig: sig_str("FLINT exception") 

fmpq_poly_set_coeff_si(self.__poly, n, value) 

if do_sig: sig_off() 

elif isinstance(value, Integer): 

if do_sig: sig_str("FLINT exception") 

fmpq_poly_set_coeff_mpz(self.__poly, n, (<Integer> value).value) 

if do_sig: sig_off() 

elif isinstance(value, Rational): 

if do_sig: sig_str("FLINT exception") 

fmpq_poly_set_coeff_mpq(self.__poly, n, (<Rational> value).value) 

if do_sig: sig_off() 

else: 

value = Rational(value) 

if do_sig: sig_str("FLINT exception") 

fmpq_poly_set_coeff_mpq(self.__poly, n, (<Rational> value).value) 

if do_sig: sig_off() 

  

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

""" 

Calls this polynomial with the given parameters, which can be 

interpreted as polynomial composition or evaluation by this 

method. 

  

If the argument is not simply an integer, a rational, or a 

polynomial, the call is passed on to the generic implementation 

in the Polynomial class. 

  

EXAMPLES: 

  

The first example illustrates polynomial composition:: 

  

sage: R.<t> = QQ[] 

sage: f = t^2 - 1 

sage: g = t + 1 

sage: f(g) # indirect doctest 

t^2 + 2*t 

  

Now we illustrate how a polynomial can be evaluated at a rational 

number:: 

  

sage: f(-2/3) # indirect doctest 

-5/9 

  

TESTS: 

  

sage: t(-sys.maxint-1r) == t(-sys.maxint-1) 

True 

""" 

cdef Polynomial_rational_flint f 

cdef Rational r 

cdef mpz_t tmpz 

  

if len(x) == 1: 

a = x[0] 

if isinstance(a, Polynomial_rational_flint): 

f = (<Polynomial_rational_flint> a)._new() 

sig_str("FLINT exception") 

fmpq_poly_compose(f.__poly, self.__poly, 

(<Polynomial_rational_flint> a).__poly) 

sig_off() 

return f 

elif isinstance(a, Rational): 

r = Rational.__new__(Rational) 

sig_str("FLINT exception") 

fmpq_poly_evaluate_mpq(r.value, self.__poly, (<Rational> a).value) 

sig_off() 

return r 

elif isinstance(a, Integer): 

r = Rational.__new__(Rational) 

sig_str("FLINT exception") 

fmpq_poly_evaluate_mpz(r.value, self.__poly, (<Integer> a).value) 

sig_off() 

return r 

elif isinstance(a, int): 

r = Rational.__new__(Rational) 

sig_str("FLINT exception") 

mpz_init(tmpz) 

mpz_set_si(tmpz, PyInt_AS_LONG(a)) 

fmpq_poly_evaluate_mpz(r.value, self.__poly, tmpz) 

mpz_clear(tmpz) 

sig_off() 

return r 

  

return Polynomial.__call__(self, *x, **kwds) 

  

cpdef Polynomial truncate(self, long n): 

""" 

Returns self truncated modulo `t^n`. 

  

INPUT: 

  

- ``n`` - The power of `t` modulo which self is truncated 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = 1 - t + 1/2*t^2 - 1/3*t^3 

sage: f.truncate(0) 

0 

sage: f.truncate(2) 

-t + 1 

""" 

cdef Polynomial_rational_flint res 

cdef bint do_sig 

  

if (n >= fmpq_poly_length(self.__poly)): 

return self 

else: 

res = self._new() 

if n > 0: 

do_sig = _do_sig(self.__poly) 

if do_sig: sig_str("FLINT exception") 

fmpq_poly_get_slice(res.__poly, self.__poly, 0, n) 

if do_sig: sig_off() 

return res 

  

def reverse(self, degree=None): 

""" 

Reverse the coefficients of this polynomial (thought of as a polynomial 

of degree ``degree``). 

  

INPUT: 

  

- ``degree`` (``None`` or integral value that fits in an ``unsigned 

long``, default: degree of ``self``) - if specified, truncate or zero 

pad the list of coefficients to this degree before reversing it. 

  

EXAMPLES: 

  

We first consider the simplest case, where we reverse all coefficients 

of a polynomial and obtain a polynomial of the same degree:: 

  

sage: R.<t> = QQ[] 

sage: f = 1 + t + t^2 / 2 + t^3 / 3 + t^4 / 4 

sage: f.reverse() 

t^4 + t^3 + 1/2*t^2 + 1/3*t + 1/4 

  

Next, an example we the returned polynomial has lower degree because 

the original polynomial has low coefficients equal to zero:: 

  

sage: R.<t> = QQ[] 

sage: f = 3/4*t^2 + 6*t^7 

sage: f.reverse() 

3/4*t^5 + 6 

  

The next example illustrates the passing of a value for ``degree`` less 

than the length of self, notationally resulting in truncation prior to 

reversing:: 

  

sage: R.<t> = QQ[] 

sage: f = 1 + t + t^2 / 2 + t^3 / 3 + t^4 / 4 

sage: f.reverse(2) 

t^2 + t + 1/2 

  

Now we illustrate the passing of a value for ``degree`` greater than 

the length of self, notationally resulting in zero padding at the top 

end prior to reversing:: 

  

sage: R.<t> = QQ[] 

sage: f = 1 + t + t^2 / 2 + t^3 / 3 

sage: f.reverse(4) 

t^4 + t^3 + 1/2*t^2 + 1/3*t 

  

TESTS: 

  

We illustrate two ways in which the interpretation of ``degree`` as an 

unsigned long int may fail. Firstly, an integral value which is 

too large, yielding an ``OverflowError``:: 

  

sage: R.<t> = QQ[] 

sage: f = 1 + t/2 

sage: f.reverse(2**64 - 1) 

Traceback (most recent call last): 

... 

OverflowError: ... int too large to convert 

  

Secondly, a value which cannot be converted to an integral value, 

resulting in a ValueError:: 

  

sage: R.<t> = QQ[] 

sage: f = 1 + t/2 

sage: f.reverse(I) 

Traceback (most recent call last): 

... 

ValueError: degree must be convertible to long 

  

We check that this specialized implementation is compatible with the 

generic one:: 

  

sage: all((t + 2*t^2).reverse(degree=d) 

....: == Polynomial.reverse(t + 2*t^2, degree=d) 

....: for d in [None, 0, 1, 2, 3, 4, 5]) 

True 

""" 

cdef unsigned long len 

cdef Polynomial_rational_flint res 

cdef bint do_sig 

  

if degree is None: 

len = fmpq_poly_length(self.__poly) 

else: 

try: 

len = <unsigned long> (degree + 1) 

except ValueError: 

raise ValueError('degree must be convertible to long') 

  

res = self._new() 

do_sig = _do_sig(self.__poly) 

if do_sig: sig_str("FLINT exception") 

fmpq_poly_reverse(res.__poly, self.__poly, len) 

if do_sig: sig_off() 

return res 

  

def revert_series(self, n): 

r""" 

Return a polynomial `f` such that `f(self(x)) = self(f(x)) = x mod x^n`. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = t - t^3/6 + t^5/120 

sage: f.revert_series(6) 

3/40*t^5 + 1/6*t^3 + t 

  

sage: f.revert_series(-1) 

Traceback (most recent call last): 

ValueError: argument n must be a non-negative integer, got -1 

  

sage: g = - t^3/3 + t^5/5 

sage: g.revert_series(6) 

Traceback (most recent call last): 

... 

ValueError: self must have constant coefficient 0 and a unit for coefficient t^1 

""" 

  

cdef Polynomial_rational_flint res = self._new() 

cdef unsigned long m 

if n < 0: 

raise ValueError("argument n must be a non-negative integer, got {}".format(n)) 

m = n 

if not self[0].is_zero() or not self[1].is_unit(): 

raise ValueError("self must have constant coefficient 0 and a unit for coefficient {}^1".format(self.parent().gen())) 

  

sig_str("FLINT exception") 

fmpq_poly_revert_series(res.__poly, self.__poly, m) 

sig_off() 

  

return res 

  

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

# Comparisons # 

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

  

cpdef bint is_zero(self): 

""" 

Returns whether or not self is the zero polynomial. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = 1 - t + 1/2*t^2 - 1/3*t^3 

sage: f.is_zero() 

False 

sage: R(0).is_zero() 

True 

""" 

return fmpq_poly_is_zero(self.__poly) 

  

cpdef bint is_one(self): 

r""" 

Returns whether or not this polynomial is one. 

  

EXAMPLES:: 

  

sage: R.<x> = QQ[] 

sage: R([0,1]).is_one() 

False 

sage: R([1]).is_one() 

True 

sage: R([0]).is_one() 

False 

sage: R([-1]).is_one() 

False 

sage: R([1,1]).is_one() 

False 

""" 

return fmpq_poly_is_one(self.__poly) 

  

def __nonzero__(self): 

""" 

Returns whether or not self is non-zero. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = 1 - t + 1/2*t^2 - 1/3*t^3 

sage: bool(f) 

True 

sage: bool(R(0)) 

False 

""" 

return not fmpq_poly_is_zero(self.__poly) 

  

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

# Shifting # 

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

  

def __lshift__(self, n): 

""" 

Notationally multiplies self by `t^n`. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: t << 10 # indirect doctest 

t^11 

  

TESTS:: 

  

sage: R.<t> = QQ[] 

sage: f = R.random_element(1000) 

sage: (f << 23) >> 23 == f # indirect doctest 

True 

""" 

cdef unsigned long k = <unsigned long> n 

cdef Polynomial_rational_flint f = <Polynomial_rational_flint> self 

cdef Polynomial_rational_flint res 

cdef bint do_sig 

  

if k == 0 or fmpq_poly_is_zero(f.__poly): 

return self 

else: 

res = f._new() 

do_sig = fmpq_poly_length(f.__poly) > 5000 or n > 5000 

  

if do_sig: sig_str("FLINT exception") 

fmpq_poly_shift_left(res.__poly, f.__poly, k) 

if do_sig: sig_off() 

return res 

  

def __rshift__(self, n): 

""" 

Notationally returns the quotient of Euclidean division of self 

by `t^n`. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = 1 + t + t^2/2 + t^3/3 + t^4/4 

sage: f >> 2 

1/4*t^2 + 1/3*t + 1/2 

""" 

cdef unsigned long k = <unsigned long> n 

cdef Polynomial_rational_flint f = <Polynomial_rational_flint> self 

cdef Polynomial_rational_flint res 

cdef bint do_sig 

  

if k == 0 or fmpq_poly_is_zero(f.__poly): 

return self 

else: 

res = f._new() 

do_sig = _do_sig(f.__poly) 

  

if do_sig: sig_str("FLINT exception") 

fmpq_poly_shift_right(res.__poly, f.__poly, k) 

if do_sig: sig_off() 

return res 

  

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

# Arithmetic # 

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

  

cpdef _add_(self, right): 

""" 

Returns the sum of two rational polynomials. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = 2/3 + t + 2*t^3 

sage: g = -1 + t/3 - 10/11*t^4 

sage: f + g 

-10/11*t^4 + 2*t^3 + 4/3*t - 1/3 

  

TESTS:: 

  

sage: R.<t> = QQ[] 

sage: f = R.random_element(2000) 

sage: f + f == 2 * f # indirect doctest 

True 

""" 

cdef Polynomial_rational_flint op2 = <Polynomial_rational_flint> right 

cdef Polynomial_rational_flint res = self._new() 

cdef bint do_sig = _do_sig(self.__poly) or _do_sig(op2.__poly) 

  

if do_sig: sig_str("FLINT exception") 

fmpq_poly_add(res.__poly, self.__poly, op2.__poly) 

if do_sig: sig_off() 

return res 

  

cpdef _sub_(self, right): 

""" 

Returns the difference of two rational polynomials. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = -10/11*t^4 + 2*t^3 + 4/3*t - 1/3 

sage: g = 2*t^3 

sage: f - g # indirect doctest 

-10/11*t^4 + 4/3*t - 1/3 

  

TESTS:: 

  

sage: R.<t> = QQ[] 

sage: f = R.random_element(2000) 

sage: f - f/2 == 1/2 * f # indirect doctest 

True 

""" 

cdef Polynomial_rational_flint op2 = <Polynomial_rational_flint> right 

cdef Polynomial_rational_flint res = self._new() 

cdef bint do_sig = _do_sig(self.__poly) or _do_sig(op2.__poly) 

  

if do_sig: sig_str("FLINT exception") 

fmpq_poly_sub(res.__poly, self.__poly, op2.__poly) 

if do_sig: sig_off() 

return res 

  

cpdef _neg_(self): 

""" 

Returns the difference of two rational polynomials. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = 3*t/2 

sage: -f # indirect doctest 

-3/2*t 

  

TESTS:: 

  

sage: R.<t> = QQ[] 

sage: f = R.random_element(2000) 

sage: f + (-f) == 0 # indirect doctest 

True 

""" 

cdef Polynomial_rational_flint res = self._new() 

cdef bint do_sig = _do_sig(self.__poly) 

  

if do_sig: sig_str("FLINT exception") 

fmpq_poly_neg(res.__poly, self.__poly) 

if do_sig: sig_off() 

return res 

  

@coerce_binop 

def quo_rem(self, right): 

""" 

Returns the quotient and remainder of the Euclidean division of 

self and right. 

  

Raises a ZerodivisionError if right is zero. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = R.random_element(2000) 

sage: g = R.random_element(1000) 

sage: q, r = f.quo_rem(g) 

sage: f == q*g + r 

True 

""" 

if right.is_zero(): 

raise ZeroDivisionError("division by zero polynomial") 

if self.is_zero(): 

return self, self 

  

cdef Polynomial_rational_flint qq = self._new() 

cdef Polynomial_rational_flint rr = self._new() 

  

sig_str("FLINT exception") 

fmpq_poly_divrem(qq.__poly, rr.__poly, self.__poly, 

(<Polynomial_rational_flint> right).__poly) 

sig_off() 

return qq, rr 

  

@coerce_binop 

def gcd(self, right): 

""" 

Returns the (monic) greatest common divisor of self and right. 

  

Corner cases: if self and right are both zero, returns zero. If 

only one of them is zero, returns the other polynomial, up to 

normalisation. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = -2 + 3*t/2 + 4*t^2/7 - t^3 

sage: g = 1/2 + 4*t + 2*t^4/3 

sage: f.gcd(g) 

1 

sage: f = (-3*t + 1/2) * f 

sage: g = (-3*t + 1/2) * (4*t^2/3 - 1) * g 

sage: f.gcd(g) 

t - 1/6 

""" 

cdef Polynomial_rational_flint res = self._new() 

  

sig_str("FLINT exception") 

fmpq_poly_gcd(res.__poly, self.__poly, 

(<Polynomial_rational_flint> right).__poly) 

sig_off() 

return res 

  

@coerce_binop 

def lcm(self, right): 

""" 

Returns the monic (or zero) least common multiple of self and right. 

  

Corner cases: if either of self and right are zero, returns zero. 

This behaviour is ensures that the relation lcm(a,b) gcd(a,b) == a b 

holds up to multiplication by rationals. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = -2 + 3*t/2 + 4*t^2/7 - t^3 

sage: g = 1/2 + 4*t + 2*t^4/3 

sage: f.lcm(g) 

t^7 - 4/7*t^6 - 3/2*t^5 + 8*t^4 - 75/28*t^3 - 66/7*t^2 + 87/8*t + 3/2 

sage: f.lcm(g) * f.gcd(g) // (f * g) 

-3/2 

""" 

cdef Polynomial_rational_flint res = self._new() 

  

sig_str("FLINT exception") 

fmpq_poly_lcm(res.__poly, self.__poly, 

(<Polynomial_rational_flint> right).__poly) 

sig_off() 

return res 

  

@coerce_binop 

def xgcd(self, right): 

""" 

Returns polynomials d, s, and t such that d == s * self + t * right, 

where d is the (monic) greatest common divisor of self and right. 

The choice of s and t is not specified any further. 

  

Corner cases: if self and right are zero, returns zero polynomials. 

Otherwise, if only self is zero, returns (d, s, t) = (right, 0, 1) up 

to normalisation, and similarly if only right is zero. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = 2/3 + 3/4 * t - t^2 

sage: g = -3 + 1/7 * t 

sage: f.xgcd(g) 

(1, -12/5095, -84/5095*t - 1701/5095) 

  

TESTS: 

  

The following example used to crash (cf. :trac:`11771`):: 

  

sage: R.<t> = QQ[] 

sage: f = 10**383 * (t+1) 

sage: g = 10**445 * t^2 + 1 

sage: r = f.xgcd(g) 

sage: r[0] == f.gcd(g) 

True 

sage: r[1]*f + r[2]*g == r[0] 

True 

""" 

cdef Polynomial_rational_flint d = self._new() 

cdef Polynomial_rational_flint s = self._new() 

cdef Polynomial_rational_flint t = self._new() 

  

sig_str("FLINT exception") 

fmpq_poly_xgcd(d.__poly, s.__poly, t.__poly, self.__poly, (<Polynomial_rational_flint>right).__poly) 

sig_off() 

return d, s, t 

  

cpdef _mul_(self, right): 

""" 

Returns the product of self and right. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = -1 + 3*t/2 - t^3 

sage: g = 2/3 + 7/3*t + 3*t^2 

sage: f * g # indirect doctest 

-3*t^5 - 7/3*t^4 + 23/6*t^3 + 1/2*t^2 - 4/3*t - 2/3 

  

TESTS:: 

  

sage: R.<t> = QQ[] 

sage: f = R.random_element(2000) 

sage: g = R.random_element(2000) 

sage: (f + g) * (f - g) == f^2 - g^2 # indirect doctest 

True 

""" 

cdef Polynomial_rational_flint op2 = <Polynomial_rational_flint> right 

cdef Polynomial_rational_flint res = self._new() 

cdef bint do_sig = _do_sig(self.__poly) or _do_sig(op2.__poly) 

  

if do_sig: sig_str("FLINT exception") 

fmpq_poly_mul(res.__poly, self.__poly, op2.__poly) 

if do_sig: sig_off() 

return res 

  

cpdef Polynomial _mul_trunc_(self, Polynomial right, long n): 

r""" 

Truncated multiplication. 

  

EXAMPLES:: 

  

sage: x = polygen(QQ) 

sage: p1 = 1/2 - 3*x + 2/7*x**3 

sage: p2 = x + 2/5*x**5 + x**7 

sage: p1._mul_trunc_(p2, 5) 

2/7*x^4 - 3*x^2 + 1/2*x 

sage: (p1*p2).truncate(5) 

2/7*x^4 - 3*x^2 + 1/2*x 

  

sage: p1._mul_trunc_(p2, 1) 

0 

sage: p1._mul_trunc_(p2, 0) 

Traceback (most recent call last): 

... 

ValueError: n must be > 0 

  

ALGORITHM: 

  

Call the FLINT method ``fmpq_poly_mullow``. 

""" 

cdef Polynomial_rational_flint op2 = <Polynomial_rational_flint> right 

cdef Polynomial_rational_flint res = self._new() 

cdef bint do_sig = _do_sig(self.__poly) or _do_sig(op2.__poly) 

  

if n <= 0: 

raise ValueError("n must be > 0") 

  

if do_sig: sig_str("FLINT exception") 

fmpq_poly_mullow(res.__poly, self.__poly, op2.__poly, n) 

if do_sig: sig_off() 

return res 

  

cpdef _rmul_(self, Element left): 

r""" 

Returns left * self, where left is a rational number. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = 3/2*t^3 - t + 1/3 

sage: 6 * f # indirect doctest 

9*t^3 - 6*t + 2 

""" 

cdef Polynomial_rational_flint res = self._new() 

cdef bint do_sig = _do_sig(self.__poly) 

  

if do_sig: sig_str("FLINT exception") 

fmpq_poly_scalar_mul_mpq(res.__poly, self.__poly, 

(<Rational> left).value) 

if do_sig: sig_off() 

return res 

  

cpdef _lmul_(self, Element right): 

r""" 

Returns self * right, where right is a rational number. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = 3/2*t^3 - t + 1/3 

sage: f * 6 # indirect doctest 

9*t^3 - 6*t + 2 

""" 

cdef Polynomial_rational_flint res = self._new() 

cdef bint do_sig = _do_sig(self.__poly) 

  

if do_sig: sig_str("FLINT exception") 

fmpq_poly_scalar_mul_mpq(res.__poly, self.__poly, 

(<Rational> right).value) 

if do_sig: sig_off() 

return res 

  

def __pow__(Polynomial_rational_flint self, exp, ignored): 

""" 

Returns self raised to the power of exp. 

  

The corner case of ``exp == 0`` is handled by returning the constant 

polynomial 1. Note that this includes the case ``0^0 == 1``. 

  

This method only supports integral values for exp that fit into 

a signed long int (except when this is a constant polynomial). 

  

INPUT: 

  

- ``exp`` - Exponent 

  

OUTPUT: 

  

Polynomial; this polynomial raised to the power of ``exp`` 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = 1/2 + 2*t - t^2/3 

sage: f^0 

1 

sage: f^3 

-1/27*t^6 + 2/3*t^5 - 23/6*t^4 + 6*t^3 + 23/4*t^2 + 3/2*t + 1/8 

sage: f^(-3) 

1/(-1/27*t^6 + 2/3*t^5 - 23/6*t^4 + 6*t^3 + 23/4*t^2 + 3/2*t + 1/8) 

  

TESTS:: 

  

sage: R.<t> = QQ[] 

sage: t^0 

1 

sage: R(0)^0 

1 

sage: R(0)^(-1) 

Traceback (most recent call last): 

... 

ZeroDivisionError: negative exponent in power of zero 

  

We verify the checking of the exponent:: 

  

sage: R.<t> = QQ[] 

sage: (1 + t)^(2/3) 

Traceback (most recent call last): 

... 

ValueError: not a 3rd power 

sage: (1 + t)^(2^63) 

Traceback (most recent call last): 

... 

OverflowError: Sage Integer too large to convert to C long 

  

FLINT memory errors do not crash Sage (:trac:`17629`):: 

  

sage: t^(sys.maxsize//2) 

Traceback (most recent call last): 

... 

RuntimeError: FLINT exception 

  

Test fractional powers (:trac:`20086`):: 

  

sage: P.<R> = QQ[] 

sage: (1/27*R^3 + 2/3*R^2 + 4*R + 8)^(2/3) 

1/9*R^2 + 4/3*R + 4 

sage: _.parent() 

Univariate Polynomial Ring in R over Rational Field 

sage: P(1/4)^(1/2) 

1/2 

sage: _.parent() 

Univariate Polynomial Ring in R over Rational Field 

  

sage: (R+2)^(2/5) 

Traceback (most recent call last): 

... 

ValueError: not a 5th power 

  

sage: P(1/3)^(1/2) 

Traceback (most recent call last): 

... 

ValueError: not a perfect 2nd power 

sage: P(4)^P(1/2) 

Traceback (most recent call last): 

... 

TypeError: no canonical coercion from Univariate Polynomial 

Ring in R over Rational Field to Rational Field 

sage: (R + 1)^P(2) 

Traceback (most recent call last): 

... 

TypeError: no canonical coercion from Univariate Polynomial 

Ring in R over Rational Field to Rational Field 

sage: (R + 1)^R 

Traceback (most recent call last): 

... 

TypeError: no canonical coercion from Univariate Polynomial 

Ring in R over Rational Field to Rational Field 

sage: 2^R 

Traceback (most recent call last): 

... 

TypeError: no canonical coercion from Univariate Polynomial 

Ring in R over Rational Field to Rational Field 

""" 

cdef Polynomial_rational_flint res 

cdef long n 

  

try: 

n = pyobject_to_long(exp) 

except TypeError: 

r = QQ.coerce(exp) 

num = r.numerator() 

den = r.denominator() 

  

if fmpq_poly_degree(self.__poly) == 0: 

return self.parent()(self[0].nth_root(den) ** num) 

  

return self.nth_root(den) ** num 

  

else: 

if n < 0: 

if fmpq_poly_is_zero(self.__poly): 

raise ZeroDivisionError("negative exponent in power of zero") 

res = self._new() 

sig_str("FLINT exception") 

fmpq_poly_pow(res.__poly, self.__poly, -n) 

sig_off() 

return ~res 

else: 

res = self._new() 

sig_str("FLINT exception") 

if self._is_gen: 

fmpq_poly_set_coeff_si(res.__poly, n, 1) 

else: 

fmpq_poly_pow(res.__poly, self.__poly, n) 

sig_off() 

return res 

  

def __floordiv__(Polynomial_rational_flint self, right): 

""" 

Returns the quotient of self and right obtain by Euclidean division. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = t^3 - t/2 + 1/5 

sage: g = 2/3*t - 1 

sage: f // g # indirect doctest 

3/2*t^2 + 9/4*t + 21/8 

  

TESTS:: 

  

sage: R.<t> = QQ[] 

sage: f = R.random_element(1000) 

sage: g = R.random_element(500) 

sage: if g == 0: g = R(1) 

sage: qr = f.quo_rem(g) 

sage: q = f // g # indirect doctest 

sage: qr[0] == q 

True 

""" 

cdef Polynomial_rational_flint res 

cdef bint do_sig 

  

if right == 0: 

raise ZeroDivisionError("division by zero polynomial") 

  

if not isinstance(right, Polynomial_rational_flint): 

if right in QQ: 

res = self._new() 

do_sig = _do_sig(self.__poly) 

  

if do_sig: sig_str("FLINT exception") 

fmpq_poly_scalar_div_mpq(res.__poly, self.__poly, 

(<Rational> QQ(right)).value) 

if do_sig: sig_off() 

return res 

  

right = self._parent(right) 

  

res = self._new() 

sig_str("FLINT exception") 

fmpq_poly_div(res.__poly, self.__poly, 

(<Polynomial_rational_flint>right).__poly) 

sig_off() 

return res 

  

cpdef Polynomial inverse_series_trunc(self, long prec): 

r""" 

Return a polynomial approximation of precision ``prec`` of the inverse 

series of this polynomial. 

  

EXAMPLES:: 

  

sage: x = polygen(QQ) 

sage: p = 2 + x - 3/5*x**2 

sage: q5 = p.inverse_series_trunc(5) 

sage: q5 

151/800*x^4 - 17/80*x^3 + 11/40*x^2 - 1/4*x + 1/2 

sage: q5 * p 

-453/4000*x^6 + 253/800*x^5 + 1 

  

sage: q100 = p.inverse_series_trunc(100) 

sage: (q100 * p).truncate(100) 

1 

  

TESTS:: 

  

sage: (0*x).inverse_series_trunc(4) 

Traceback (most recent call last): 

... 

ValueError: constant term is zero 

sage: x.inverse_series_trunc(4) 

Traceback (most recent call last): 

... 

ValueError: constant term is zero 

sage: (x+1).inverse_series_trunc(0) 

Traceback (most recent call last): 

... 

ValueError: the precision must be positive, got 0 

""" 

if prec <= 0: 

raise ValueError("the precision must be positive, got {}".format(prec)) 

if fmpq_poly_degree(self.__poly) == -1 or \ 

fmpz_is_zero(fmpq_poly_numref(self.__poly)): 

raise ValueError("constant term is zero") 

  

cdef Polynomial_rational_flint res = self._new() 

if prec <= 0: 

return res 

sig_on() 

fmpq_poly_inv_series(res.__poly, self.__poly, prec) 

sig_off() 

return res 

  

cpdef _mod_(self, right): 

""" 

Returns the remainder of self and right obtain by Euclidean division. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = t^3 - t/2 + 1/5 

sage: g = 2/3*t - 1 

sage: f % g # indirect doctest 

113/40 

  

TESTS:: 

  

sage: R.<t> = QQ[] 

sage: f = R.random_element(1000) 

sage: g = R.random_element(500) 

sage: if g == 0: g = R(1) 

sage: qr = f.quo_rem(g) 

sage: r = f % g # indirect doctest 

sage: qr[1] == r 

True 

""" 

cdef Polynomial_rational_flint res 

  

if right == 0: 

raise ZeroDivisionError("division by zero polynomial") 

  

res = self._new() 

sig_str("FLINT exception") 

fmpq_poly_rem(res.__poly, self.__poly, 

(<Polynomial_rational_flint>right).__poly) 

sig_off() 

return res 

  

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

# Further methods # 

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

  

def numerator(self): 

""" 

Returns the numerator of self. 

  

Representing self as the quotient of an integer polynomial and 

a positive integer denominator (coprime to the content of the 

polynomial), returns the integer polynomial. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = (3 * t^3 + 1) / -3 

sage: f.numerator() 

-3*t^3 - 1 

""" 

cdef Polynomial_integer_dense_flint num 

num = Polynomial_integer_dense_flint.__new__(Polynomial_integer_dense_flint) 

parent = ZZ[self.variable_name()] 

Polynomial_integer_dense_flint.__init__(num, parent, x=None, 

check=False, is_gen=False, construct=False) 

sig_str("FLINT exception") 

fmpq_poly_get_numerator(num.__poly, self.__poly) 

sig_off() 

return num 

  

def denominator(self): 

""" 

Returns the denominator of self. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = (3 * t^3 + 1) / -3 

sage: f.denominator() 

3 

""" 

cdef Integer den = Integer.__new__(Integer) 

if fmpq_poly_denref(self.__poly) is NULL: 

mpz_set_ui(den.value, 1) 

else: 

fmpz_get_mpz(den.value, <fmpz *> fmpq_poly_denref(self.__poly)) 

return den 

  

def _derivative(self, var = None): 

""" 

Returns the derivative of self with respect to ``var``. 

  

INPUT: 

  

- ``var`` - Must be either (equal to) the generator of the polynomial 

ring to which this polynomial belongs, or ``None``; either way the 

behaviour is the same. 

  

OUTPUT: 

  

- Derivative as a ``Polynomial_rational_flint`` 

  

.. SEEALSO:: :meth:`~Polynomial.derivative` 

  

EXAMPLES:: 

  

sage: R.<x> = QQ[] 

sage: f = x^4 - x - 1 

sage: f._derivative() 

4*x^3 - 1 

sage: f._derivative(None) 

4*x^3 - 1 

sage: f._derivative(2*x) 

Traceback (most recent call last): 

... 

ValueError: Cannot differentiate with respect to 2*x 

sage: y = var("y") 

sage: f._derivative(y) 

Traceback (most recent call last): 

... 

ValueError: Cannot differentiate with respect to y 

""" 

cdef Polynomial_rational_flint der 

cdef bint do_sig 

  

if var is not None and var != self._parent.gen(): 

raise ValueError("Cannot differentiate with respect to %s" % var) 

  

der = self._new() 

do_sig = _do_sig(self.__poly) 

  

if do_sig: sig_str("FLINT exception") 

fmpq_poly_derivative(der.__poly, self.__poly) 

if do_sig: sig_off() 

return der 

  

def real_root_intervals(self): 

""" 

Returns isolating intervals for the real roots of self. 

  

EXAMPLES: 

  

We compute the roots of the characteristic polynomial of some 

Salem numbers:: 

  

sage: R.<t> = QQ[] 

sage: f = 1 - t^2 - t^3 - t^4 + t^6 

sage: f.real_root_intervals() 

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

""" 

from sage.rings.polynomial.real_roots import real_roots 

return real_roots(self) 

  

@coerce_binop 

def resultant(Polynomial_rational_flint self, right): 

r""" 

Returns the resultant of self and right. 

  

Enumerating the roots over `\QQ` as `r_1, \cdots, r_m` and 

`s_1, \cdots, s_n` and letting `x` and `y` denote the leading 

coefficients of `f` and `g`, the resultant of the two polynomials 

is defined by 

  

.. MATH:: 

  

x^{\deg g} y^{\deg f} \prod_{i,j} (r_i - s_j). 

  

Corner cases: if one of the polynomials is zero, the resultant 

is zero. Note that otherwise if one of the polynomials is constant, 

the last term in the above is the empty product. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: f = (t - 2/3) * (t + 4/5) * (t - 1) 

sage: g = (t - 1/3) * (t + 1/2) * (t + 1) 

sage: f.resultant(g) 

119/1350 

sage: h = (t - 1/3) * (t + 1/2) * (t - 1) 

sage: f.resultant(h) 

0 

""" 

cdef Rational res = Rational.__new__(Rational) 

cdef fmpq_t t 

fmpq_init(t) 

sig_str("FLINT exception") 

fmpq_poly_resultant(t, self.__poly, 

(<Polynomial_rational_flint>right).__poly) 

fmpq_get_mpq(res.value, t) 

sig_off() 

fmpq_clear(t) 

return res 

  

@cached_method 

def is_irreducible(self): 

r""" 

Return whether this polynomial is irreducible. 

  

This method computes the primitive part as an element of `\ZZ[t]` and 

calls the method ``is_irreducible`` for elements of that polynomial 

ring. 

  

By definition, over any integral domain, an element `r` is irreducible 

if and only if it is non-zero, not a unit and whenever `r = ab` then 

`a` or `b` is a unit. 

  

EXAMPLES:: 

  

sage: R.<t> = QQ[] 

sage: (t^2 + 2).is_irreducible() 

True 

sage: (t^2 - 1).is_irreducible() 

False 

  

TESTS:: 

  

sage: R.<t> = QQ[] 

sage: R(0).is_irreducible() 

False 

sage: R(-1/2).is_irreducible() 

False 

sage: (t+1).is_irreducible() 

True 

  

Test that caching works:: 

  

sage: R.<t> = QQ[] 

sage: f = t + 1 

sage: f.is_irreducible() 

True 

sage: f.is_irreducible.cache 

True 

  

""" 

cdef Polynomial_integer_dense_flint primitive 

cdef unsigned long length = fmpq_poly_length(self.__poly) 

  

if length < 2: 

return False 

elif length == 2: 

return True 

else: 

primitive = Polynomial_integer_dense_flint.__new__(Polynomial_integer_dense_flint) 

parent = ZZ[self.variable_name()] 

Polynomial_integer_dense_flint.__init__(primitive, parent, 

x=None, check=True, is_gen=False, construct=False) 

  

sig_str("FLINT exception") 

fmpq_poly_get_numerator(primitive.__poly, self.__poly) 

fmpz_poly_primitive_part(primitive.__poly, primitive.__poly) 

sig_off() 

return primitive.is_irreducible() 

  

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

# Transcendental functions (return truncated series) # 

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

  

def _log_series(self, long prec): 

r""" 

Return the logarithm of this polynomial up to precision ``prec``. 

  

EXAMPLES:: 

  

sage: x = polygen(QQ) 

sage: (1+x)._log_series(5) 

-1/4*x^4 + 1/3*x^3 - 1/2*x^2 + x 

  

sage: (1/3*x^3 - 2*x^2 + x + 1)._log_series(10)._exp_series(10) 

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

  

TESTS:: 

  

sage: x._log_series(5) 

Traceback (most recent call last): 

... 

ValueError: constant term should be 1 in order to take logarithm 

sage: (0*x)._log_series(5) 

Traceback (most recent call last): 

... 

ValueError: constant term should be 1 in order to take logarithm 

""" 

if fmpq_poly_degree(self.__poly) == -1 or \ 

fmpz_cmp(fmpq_poly_numref(self.__poly), 

fmpq_poly_denref(self.__poly)): 

raise ValueError("constant term should be 1 in order to take logarithm") 

  

cdef Polynomial_rational_flint res = self._new() 

sig_on() 

fmpq_poly_log_series(res.__poly, self.__poly, prec) 

sig_off() 

return res 

  

def _exp_series(self, long prec): 

r""" 

Return the exponential of this polynomial up to precision ``prec``. 

  

EXAMPLES:: 

  

sage: x = polygen(QQ) 

sage: x._exp_series(5) 

1/24*x^4 + 1/6*x^3 + 1/2*x^2 + x + 1 

sage: (1/3*x^4 - 3*x^2 - 1/2*x)._exp_series(5)._log_series(5) 

1/3*x^4 - 3*x^2 - 1/2*x 

  

TESTS:: 

  

sage: (x+1)._exp_series(5) 

Traceback (most recent call last): 

... 

ValueError: constant term should be 0 in order to take exponential 

sage: (0*x)._exp_series(5) 

1 

sage: _.parent() 

Univariate Polynomial Ring in x over Rational Field 

""" 

if fmpq_poly_degree(self.__poly) == -1: 

return self._parent.one() 

elif not fmpz_is_zero(fmpq_poly_numref(self.__poly)): 

raise ValueError("constant term should be 0 in order to take exponential") 

  

cdef Polynomial_rational_flint res = self._new() 

sig_on() 

fmpq_poly_exp_series(res.__poly, self.__poly, prec) 

sig_off() 

return res 

  

def _atan_series(self, long prec): 

r""" 

Return the arctangent of this polynomial up to precision ``prec``. 

  

EXAMPLES:: 

  

sage: x = polygen(QQ) 

sage: x._atan_series(7) 

1/5*x^5 - 1/3*x^3 + x 

sage: (1/5*x^3 + 2*x^2 - x)._atan_series(10)._tan_series(10) 

1/5*x^3 + 2*x^2 - x 

  

TESTS:: 

  

sage: (1+x)._atan_series(3) 

Traceback (most recent call last): 

... 

ValueError: constant term should be 0 in order to take arctangent 

sage: (0*x)._atan_series(10) 

0 

sage: _.parent() 

Univariate Polynomial Ring in x over Rational Field 

""" 

if fmpq_poly_degree(self.__poly) == -1: 

return self._parent.zero() 

elif not fmpz_is_zero(fmpq_poly_numref(self.__poly)): 

raise ValueError("constant term should be 0 in order to take arctangent") 

  

cdef Polynomial_rational_flint res = self._new() 

sig_on() 

fmpq_poly_atan_series(res.__poly, self.__poly, prec) 

sig_off() 

return res 

  

def _atanh_series(self, long prec): 

r""" 

Return the hyperbolic arctangent of this polynomial up to precision 

``prec``. 

  

EXAMPLES:: 

  

sage: x = polygen(QQ) 

sage: x._atanh_series(7) 

1/5*x^5 + 1/3*x^3 + x 

sage: (1/5*x^3 + 2*x^2 - x)._atanh_series(10)._tanh_series(10) 

1/5*x^3 + 2*x^2 - x 

  

TESTS:: 

  

sage: (0*x)._atanh_series(10) 

0 

sage: _.parent() 

Univariate Polynomial Ring in x over Rational Field 

""" 

if fmpq_poly_degree(self.__poly) == -1: 

return self._parent.zero() 

elif not fmpz_is_zero(fmpq_poly_numref(self.__poly)): 

raise ValueError("constant term should be 0 in order to take hyperbolic arctangent") 

  

cdef Polynomial_rational_flint res = self._new() 

sig_on() 

fmpq_poly_atanh_series(res.__poly, self.__poly, prec) 

sig_off() 

return res 

  

def _asin_series(self, long prec): 

r""" 

Return the arcsine of this polynomial up to precision ``prec``. 

  

EXAMPLES:: 

  

sage: x = polygen(QQ) 

sage: x._asin_series(7) 

3/40*x^5 + 1/6*x^3 + x 

sage: (1/5*x^3 + 2*x^2 - x)._asin_series(10)._sin_series(10) 

1/5*x^3 + 2*x^2 - x 

  

TESTS:: 

  

sage: (x+1)._asin_series(5) 

Traceback (most recent call last): 

... 

ValueError: constant term should be 0 in order to take arcsine 

sage: (0*x)._asin_series(5) 

0 

sage: _.parent() 

Univariate Polynomial Ring in x over Rational Field 

""" 

if fmpq_poly_degree(self.__poly) == -1: 

return self._parent.zero() 

elif not fmpz_is_zero(fmpq_poly_numref(self.__poly)): 

raise ValueError("constant term should be 0 in order to take arcsine") 

  

cdef Polynomial_rational_flint res = self._new() 

sig_on() 

fmpq_poly_asin_series(res.__poly, self.__poly, prec) 

sig_off() 

return res 

  

def _asinh_series(self, long prec): 

r""" 

Return the hyperbolic arcsine of this polynomial up to precision 

``prec``. 

  

EXAMPLES:: 

  

sage: x = polygen(QQ) 

sage: x._asinh_series(7) 

3/40*x^5 - 1/6*x^3 + x 

sage: (1/5*x^3 + 2*x^2 - x)._asinh_series(10)._sinh_series(10) 

1/5*x^3 + 2*x^2 - x 

  

TESTS:: 

  

sage: (x+1)._asinh_series(5) 

Traceback (most recent call last): 

... 

ValueError: constant term should be 0 in order to take hyperbolic arcsine 

sage: (0*x)._asinh_series(5) 

0 

sage: _.parent() 

Univariate Polynomial Ring in x over Rational Field 

""" 

if fmpq_poly_degree(self.__poly) == -1: 

return self._parent.zero() 

elif not fmpz_is_zero(fmpq_poly_numref(self.__poly)): 

raise ValueError("constant term should be 0 in order to take hyperbolic arcsine") 

  

cdef Polynomial_rational_flint res = self._new() 

sig_on() 

fmpq_poly_asinh_series(res.__poly, self.__poly, prec) 

sig_off() 

return res 

  

def _tan_series(self, long prec): 

r""" 

Return the tangent of this polynomial up to precision ``prec``. 

  

EXAMPLES:: 

  

sage: x = polygen(QQ) 

sage: x._tan_series(8) 

17/315*x^7 + 2/15*x^5 + 1/3*x^3 + x 

sage: (1/5*x^3 + 2*x^2 - x)._tan_series(10)._atan_series(10) 

1/5*x^3 + 2*x^2 - x 

  

TESTS:: 

  

sage: (x+1)._tan_series(10) 

Traceback (most recent call last): 

... 

ValueError: constant term should be 0 in order to take tangent 

sage: (0*x)._tan_series(5) 

0 

sage: _.parent() 

Univariate Polynomial Ring in x over Rational Field 

""" 

if fmpq_poly_degree(self.__poly) == -1: 

return self._parent.zero() 

elif not fmpz_is_zero(fmpq_poly_numref(self.__poly)): 

raise ValueError("constant term should be 0 in order to take tangent") 

  

cdef Polynomial_rational_flint res = self._new() 

sig_on() 

fmpq_poly_tan_series(res.__poly, self.__poly, prec) 

sig_off() 

return res 

  

def _sin_series(self, long prec): 

r""" 

Return the sine of this polynomial up to precision ``prec``. 

  

EXAMPLES:: 

  

sage: x = polygen(QQ) 

sage: x._sin_series(8) 

-1/5040*x^7 + 1/120*x^5 - 1/6*x^3 + x 

sage: (1/5*x^3 - 2*x^2 + 1/2*x)._sin_series(10)._asin_series(10) 

1/5*x^3 - 2*x^2 + 1/2*x 

  

TESTS:: 

  

sage: (x+1)._sin_series(10) 

Traceback (most recent call last): 

... 

ValueError: constant term should be 0 in order to take sine 

sage: (0*x)._sin_series(5) 

0 

sage: _.parent() 

Univariate Polynomial Ring in x over Rational Field 

""" 

if fmpq_poly_degree(self.__poly) == -1: 

return self._parent.zero() 

elif not fmpz_is_zero(fmpq_poly_numref(self.__poly)): 

raise ValueError("constant term should be 0 in order to take sine") 

  

cdef Polynomial_rational_flint res = self._new() 

sig_on() 

fmpq_poly_sin_series(res.__poly, self.__poly, prec) 

sig_off() 

return res 

  

def _cos_series(self, long prec): 

r""" 

Return the cosine of this polynomial up to precision ``prec`` 

  

EXAMPLES:: 

  

sage: x = polygen(QQ) 

sage: x._cos_series(10) 

1/40320*x^8 - 1/720*x^6 + 1/24*x^4 - 1/2*x^2 + 1 

  

TESTS:: 

  

sage: (x+1)._cos_series(5) 

Traceback (most recent call last): 

... 

ValueError: constant term should be 0 in order to take cosine 

sage: (0*x)._cos_series(5) 

0 

sage: _.parent() 

Univariate Polynomial Ring in x over Rational Field 

""" 

if fmpq_poly_degree(self.__poly) == -1: 

return self._parent.zero() 

elif not fmpz_is_zero(fmpq_poly_numref(self.__poly)): 

raise ValueError("constant term should be 0 in order to take cosine") 

  

cdef Polynomial_rational_flint res = self._new() 

sig_on() 

fmpq_poly_cos_series(res.__poly, self.__poly, prec) 

sig_off() 

return res 

  

def _sinh_series(self, long prec): 

r""" 

Return the hyperbolic sine of this polynomial up to precision ``prec``. 

  

EXAMPLES:: 

  

sage: x = polygen(QQ) 

sage: x._sinh_series(8) 

1/5040*x^7 + 1/120*x^5 + 1/6*x^3 + x 

  

TESTS:: 

  

sage: (x+1)._sinh_series(5) 

Traceback (most recent call last): 

... 

ValueError: constant term should be 0 in order to take hyperbolic sine 

sage: (0*x)._sinh_series(5) 

0 

sage: _.parent() 

Univariate Polynomial Ring in x over Rational Field 

""" 

if fmpq_poly_degree(self.__poly) == -1: 

return self._parent.zero() 

elif not fmpz_is_zero(fmpq_poly_numref(self.__poly)): 

raise ValueError("constant term should be 0 in order to take hyperbolic sine") 

  

cdef Polynomial_rational_flint res = self._new() 

sig_on() 

fmpq_poly_sinh_series(res.__poly, self.__poly, prec) 

sig_off() 

return res 

  

def _cosh_series(self, long prec): 

r""" 

Return the hyperbolic cosine of this polynomial up to precision 

``prec``. 

  

EXAMPLES:: 

  

sage: x = polygen(QQ) 

sage: x._cosh_series(8) 

1/720*x^6 + 1/24*x^4 + 1/2*x^2 + 1 

  

A trigonometric identity:: 

  

sage: x._cosh_series(8) + x._sinh_series(8) == x._exp_series(8) 

True 

  

TESTS:: 

  

sage: (x+1)._cosh_series(5) 

Traceback (most recent call last): 

... 

ValueError: constant term should be 0 in order to take hyperbolic cosine 

sage: (0*x)._cosh_series(5) 

1 

sage: _.parent() 

Univariate Polynomial Ring in x over Rational Field 

""" 

if fmpq_poly_degree(self.__poly) == -1: 

return self._parent.one() 

elif not fmpz_is_zero(fmpq_poly_numref(self.__poly)): 

raise ValueError("constant term should be 0 in order to take hyperbolic cosine") 

  

cdef Polynomial_rational_flint res = self._new() 

sig_on() 

fmpq_poly_cosh_series(res.__poly, self.__poly, prec) 

sig_off() 

return res 

  

def _tanh_series(self, long prec): 

r""" 

Return the hyperbolic tangent of this polynomial up to precision 

``prec``. 

  

EXAMPLES:: 

  

sage: x = polygen(QQ) 

sage: x._tanh_series(8) 

-17/315*x^7 + 2/15*x^5 - 1/3*x^3 + x 

  

TESTS:: 

  

sage: (x+1)._tanh_series(5) 

Traceback (most recent call last): 

... 

ValueError: constant term should be 0 in order to take hyperbolic tangent 

sage: (0*x)._tanh_series(5) 

0 

sage: _.parent() 

Univariate Polynomial Ring in x over Rational Field 

""" 

if fmpq_poly_degree(self.__poly) == -1: 

return self._parent.zero() 

elif not fmpz_is_zero(fmpq_poly_numref(self.__poly)): 

raise ValueError("constant term should be 0 in order to take hyperbolic tangent") 

  

cdef Polynomial_rational_flint res = self._new() 

sig_on() 

fmpq_poly_tanh_series(res.__poly, self.__poly, prec) 

sig_off() 

return res 

  

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

# Methods using PARI # 

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

  

def galois_group(self, pari_group = False, algorithm = 'pari'): 

""" 

Returns the Galois group of self as a permutation group. 

  

INPUT: 

  

- ``self`` - Irreducible polynomial 

  

- ``pari_group`` - bool (default: ``False``); if ``True`` instead 

return the Galois group as a PARI group. This has a useful label 

in it, and may be slightly faster since it doesn't require looking 

up a group in Gap. To get a permutation group from a PARI 

group ``P``, type ``PermutationGroup(P)``. 

  

- ``algorithm`` - ``'pari'``, ``'kash'``, ``'magma'`` (default: 

``'pari'``, except when the degree is at least 12 in which case 

``'kash'`` is tried). 

  

OUTPUT: 

  

- Galois group 

  

ALGORITHM: 

  

The Galois group is computed using PARI in C library mode, or possibly 

KASH or MAGMA. 

  

.. NOTE:: 

  

The PARI documentation contains the following warning: The method 

used is that of resolvent polynomials and is sensitive to the 

current precision. The precision is updated internally but, in very 

rare cases, a wrong result may be returned if the initial precision 

was not sufficient. 

  

MAGMA does not return a provably correct result. Please see the 

MAGMA documentation for how to obtain a provably correct result. 

  

EXAMPLES:: 

  

sage: R.<x> = QQ[] 

sage: f = x^4 - 17*x^3 - 2*x + 1 

sage: G = f.galois_group(); G # optional - database_gap 

Transitive group number 5 of degree 4 

sage: G.gens() # optional - database_gap 

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

sage: G.order() # optional - database_gap 

24 

  

It is potentially useful to instead obtain the corresponding PARI 

group, which is little more than a 4-tuple. See the PARI manual for 

the exact details. (Note that the third entry in the tuple is in the 

new standard ordering.) 

  

:: 

  

sage: f = x^4 - 17*x^3 - 2*x + 1 

sage: G = f.galois_group(pari_group=True); G 

PARI group [24, -1, 5, "S4"] of degree 4 

sage: PermutationGroup(G) # optional - database_gap 

Transitive group number 5 of degree 4 

  

You can use KASH to compute Galois groups as well. The advantage is 

that KASH can compute Galois groups of fields up to degree 21, whereas 

PARI only goes to degree 11. (In my not-so-thorough experiments PARI 

is faster than KASH.) 

  

:: 

  

sage: f = x^4 - 17*x^3 - 2*x + 1 

sage: f.galois_group(algorithm='kash') # optional - kash 

Transitive group number 5 of degree 4 

  

sage: f = x^4 - 17*x^3 - 2*x + 1 

sage: f.galois_group(algorithm='magma') # optional - magma database_gap 

Transitive group number 5 of degree 4 

  

TESTS: 

  

We illustrate the behaviour in the case of reducible polynomials:: 

  

sage: R.<t> = QQ[] 

sage: f = (1 + t)^2 

sage: f.galois_group() 

Traceback (most recent call last): 

... 

ValueError: The polynomial must be irreducible 

  

Variable names that are reserved in PARI, such as ``zeta``, 

are supported (see :trac:`20631`):: 

  

sage: R.<zeta> = QQ[] 

sage: (zeta^2 + zeta + 1).galois_group(pari_group=True) 

PARI group [2, -1, 1, "S2"] of degree 2 

  

""" 

from sage.groups.all import PariGroup, PermutationGroup, TransitiveGroup 

  

if not self.is_irreducible(): 

raise ValueError("The polynomial must be irreducible") 

  

if self.degree() > 11 and algorithm == 'pari': 

algorithm = 'kash' 

  

if self.degree() > 21 and algorithm == 'kash': 

raise NotImplementedError("Galois group computation is " 

"supported for degrees up to 11 using PARI, or up to 21 " 

"if the optional package KASH is installed. Try " 

"algorithm='magma' if you have magma.") 

  

if algorithm == 'pari': 

G = self._pari_with_name().Polrev().polgalois() 

H = PariGroup(G, self.degree()) 

if pari_group: 

return H 

else: 

return PermutationGroup(H) 

  

elif algorithm == 'kash': 

try: 

from sage.interfaces.all import kash 

kash.eval('X := PolynomialRing(RationalField()).1') 

s = self._repr(name='X') 

G = kash('Galois(%s)'%s) 

d = int(kash.eval('%s.ext1'%G.name())) 

n = int(kash.eval('%s.ext2'%G.name())) 

return TransitiveGroup(d, n) 

except RuntimeError as msg: 

raise NotImplementedError(str(msg) + "\nSorry, " + 

"computation of Galois groups of fields of degree " + 

"bigger than 11 is not yet implemented. Try installing " + 

"the optional free (closed source) KASH package, which " + 

"supports degrees up to 21, or use algorithm='magma' if " + 

"you have magma.") 

  

elif algorithm == 'magma': 

from sage.interfaces.all import magma 

X = magma(self).GaloisGroup() 

try: 

n, d = X.TransitiveGroupIdentification(nvals=2) 

d = int(d) 

n = int(n) 

except RuntimeError as msg: 

raise RuntimeError(str(msg) + "\nUnable to lookup " + 

"description of Galois group as a transitive " + 

"group.\n%s" % X) 

return TransitiveGroup(d, n) 

  

else: 

raise ValueError("Algorithm %s not supported." % algorithm) 

  

def factor_mod(self, p): 

""" 

Returns the factorization of self modulo the prime ``p``. 

  

Assumes that the degree of this polynomial is at least one, and raises 

a ``ValueError`` otherwise. 

  

INPUT: 

  

- ``p`` - Prime number 

  

OUTPUT: 

  

- Factorization of this polynomial modulo ``p`` 

  

EXAMPLES:: 

  

sage: R.<x> = QQ[] 

sage: (x^5 + 17*x^3 + x+ 3).factor_mod(3) 

x * (x^2 + 1)^2 

sage: (x^5 + 2).factor_mod(5) 

(x + 2)^5 

  

Variable names that are reserved in PARI, such as ``zeta``, 

are supported (see :trac:`20631`):: 

  

sage: R.<zeta> = QQ[] 

sage: (zeta^2 + zeta + 1).factor_mod(7) 

(zeta + 3) * (zeta + 5) 

  

""" 

from sage.rings.finite_rings.finite_field_constructor import FiniteField 

  

p = Integer(p) 

if not p.is_prime(): 

raise ValueError("p must be prime") 

  

if self.degree() < 1: 

raise ValueError("The polynomial must have degree at least 1") 

  

G = self._pari_with_name().factormod(p) 

K = FiniteField(p) 

R = K[self.parent().variable_name()] 

return R(1)._factor_pari_helper(G, unit=R(self).leading_coefficient()) 

  

def factor_padic(self, p, prec=10): 

""" 

Return the `p`-adic factorization of this polynomial to the given 

precision. 

  

INPUT: 

  

- ``p`` - Prime number 

  

- ``prec`` - Integer; the precision 

  

OUTPUT: 

  

- factorization of ``self`` viewed as a `p`-adic polynomial 

  

EXAMPLES:: 

  

sage: R.<x> = QQ[] 

sage: f = x^3 - 2 

sage: f.factor_padic(2) 

(1 + O(2^10))*x^3 + (O(2^10))*x^2 + (O(2^10))*x + (2 + 2^2 + 2^3 + 2^4 + 2^5 + 2^6 + 2^7 + 2^8 + 2^9 + O(2^10)) 

sage: f.factor_padic(3) 

(1 + O(3^10))*x^3 + (O(3^10))*x^2 + (O(3^10))*x + (1 + 2*3 + 2*3^2 + 2*3^3 + 2*3^4 + 2*3^5 + 2*3^6 + 2*3^7 + 2*3^8 + 2*3^9 + O(3^10)) 

sage: f.factor_padic(5) 

((1 + O(5^10))*x + (2 + 4*5 + 2*5^2 + 2*5^3 + 5^4 + 3*5^5 + 4*5^7 + 2*5^8 + 5^9 + O(5^10))) * ((1 + O(5^10))*x^2 + (3 + 2*5^2 + 2*5^3 + 3*5^4 + 5^5 + 4*5^6 + 2*5^8 + 3*5^9 + O(5^10))*x + (4 + 5 + 2*5^2 + 4*5^3 + 4*5^4 + 3*5^5 + 3*5^6 + 4*5^7 + 4*5^9 + O(5^10))) 

  

The input polynomial is considered to have "infinite" precision, 

therefore the `p`-adic factorization of the polynomial is not 

the same as first coercing to `Q_p` and then factoring 

(see also :trac:`15422`):: 

  

sage: f = x^2 - 3^6 

sage: f.factor_padic(3,5) 

((1 + O(3^5))*x + (3^3 + O(3^5))) * ((1 + O(3^5))*x + (2*3^3 + 2*3^4 + O(3^5))) 

sage: f.change_ring(Qp(3,5)).factor() 

Traceback (most recent call last): 

... 

PrecisionError: p-adic factorization not well-defined since the discriminant is zero up to the requestion p-adic precision 

  

A more difficult example:: 

  

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

sage: f.factor_padic(5, 10) 

(4*5^4 + O(5^14)) * ((1 + O(5^9))*x + (5^-1 + O(5^9)))^2 * ((1 + O(5^10))*x + (5 + O(5^10)))^2 

  

Try some bogus inputs:: 

  

sage: f.factor_padic(3,-1) 

Traceback (most recent call last): 

... 

ValueError: prec_cap must be non-negative. 

sage: f.factor_padic(6,10) 

Traceback (most recent call last): 

... 

ValueError: p must be prime 

sage: f.factor_padic('hello', 'world') 

Traceback (most recent call last): 

... 

TypeError: unable to convert 'hello' to an integer 

""" 

from sage.rings.padics.factory import Qp 

  

p = Integer(p) 

prec = Integer(prec) 

  

# Parent field for coefficients and polynomial 

K = Qp(p, prec, type='capped-rel') 

R = K[self.parent().variable_name()] 

  

# Factor the *exact* polynomial using factorpadic() 

G = self._pari_with_name().factorpadic(p, prec) 

  

from sage.rings.polynomial.padics.polynomial_padic import _pari_padic_factorization_to_sage 

return _pari_padic_factorization_to_sage(G, R, self.leading_coefficient()) 

  

def hensel_lift(self, p, e): 

r""" 

Assuming that this polynomial factors modulo `p` into distinct 

monic factors, computes the Hensel lifts of these factors modulo 

`p^e`. We assume that ``self`` has integer coefficients. 

  

Returns an empty list if this polynomial has degree less than one. 

  

INPUT: 

  

- ``p`` - Prime number; coerceable to ``Integer`` 

- ``e`` - Exponent; coerceable to ``Integer`` 

  

OUTPUT: 

  

- Hensel lifts; list of polynomials over `\ZZ / p^e \ZZ` 

  

EXAMPLES:: 

  

sage: R.<x> = QQ[] 

sage: R((x-1)*(x+1)).hensel_lift(7, 2) 

[x + 1, x + 48] 

  

If the input polynomial `f` is not monic, we get a factorization of 

`f / lc(f)`:: 

  

sage: R(2*x^2 - 2).hensel_lift(7, 2) 

[x + 1, x + 48] 

  

TESTS:: 

  

sage: R.<x> = QQ[] 

sage: R(0).hensel_lift(7, 2) 

[] 

sage: R(x).hensel_lift(7, 2) 

[x] 

sage: R(x-1).hensel_lift(7, 2) 

[x + 48] 

  

Variable names that are reserved in PARI, such as ``I``, are 

supported (see :trac:`20631`):: 

  

sage: R.<I> = QQ[] 

sage: (I^2 + 1).hensel_lift(5, 3) 

[I + 57, I + 68] 

sage: (I^2 + 1).hensel_lift(2, 3) 

Traceback (most recent call last): 

... 

ValueError: I^2 + 1 is not square-free modulo 2 

  

""" 

from sage.rings.finite_rings.integer_mod_ring import IntegerModRing 

  

p = Integer(p) 

if not p.is_prime(): 

raise ValueError("p must be prime") 

e = Integer(e) 

if e < 1: 

raise ValueError("e must be at least 1") 

  

# The relevant PARI method doesn't seem to play well with constant and 

# linear polynomials, so we handle these separately. 

# 

if self.degree() < 1: 

return [ ] 

elif self.degree() == 1: 

R = IntegerModRing(p**e) 

S = R[self.parent().variable_name()] 

return [S(self)] 

  

f = self._pari_with_name() 

F = f.factormod(p) 

if any(n > 1 for n in F[1]): 

raise ValueError("{} is not square-free modulo {}".format(self, p)) 

H = f.polhensellift(F[0].liftint(), p, e) 

R = IntegerModRing(p**e) 

S = R[self.parent().variable_name()] 

return [S(m) for m in H] 

  

def discriminant(self): 

r""" 

Returns the discriminant of this polynomial. 

  

The discriminant `R_n` is defined as 

  

.. MATH:: 

  

R_n = a_n^{2 n-2} \prod_{1 \le i < j \le n} (r_i - r_j)^2, 

  

where `n` is the degree of this polynomial, `a_n` is the leading 

coefficient and the roots over `\QQbar` are `r_1, \ldots, r_n`. 

  

The discriminant of constant polynomials is defined to be 0. 

  

OUTPUT: 

  

- Discriminant, an element of the base ring of the polynomial ring 

  

.. NOTE:: 

  

Note the identity `R_n(f) := (-1)^(n (n-1)/2) R(f,f') a_n^(n-k-2)`, 

where `n` is the degree of this polynomial, `a_n` is the leading 

coefficient, `f'` is the derivative of `f`, and `k` is the degree 

of `f'`. Calls :meth:`.resultant`. 

  

ALGORITHM: 

  

Use PARI. 

  

EXAMPLES: 

  

In the case of elliptic curves in special form, the discriminant is 

easy to calculate:: 

  

sage: R.<t> = QQ[] 

sage: f = t^3 + t + 1 

sage: d = f.discriminant(); d 

-31 

sage: d.parent() is QQ 

True 

sage: EllipticCurve([1, 1]).discriminant() / 16 

-31 

  

:: 

  

sage: R.<t> = QQ[] 

sage: f = 2*t^3 + t + 1 

sage: d = f.discriminant(); d 

-116 

  

:: 

  

sage: R.<t> = QQ[] 

sage: f = t^3 + 3*t - 17 

sage: f.discriminant() 

-7911 

  

TESTS:: 

  

sage: R.<t> = QQ[] 

sage: R(0).discriminant() 

0 

sage: R(2/3).discriminant() 

0 

sage: (t + 1/2).discriminant() 

1 

  

Variable names that are reserved in PARI, such as ``I``, are 

supported (see :trac:`20631`):: 

  

sage: R.<I> = QQ[] 

sage: (I^2 + 1).discriminant() 

-4 

  

""" 

return QQ(self._pari_with_name().poldisc()) 

  

# Alias for discriminant 

disc = discriminant