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

""" 

Univariate Polynomials over domains and fields 

 

AUTHORS: 

 

- William Stein: first version 

- Martin Albrecht: Added singular coercion. 

- David Harvey: split off polynomial_integer_dense_ntl.pyx (2007-09) 

- Robert Bradshaw: split off polynomial_modn_dense_ntl.pyx (2007-09) 

 

TESTS: 

 

We test coercion in a particularly complicated situation:: 

 

sage: W.<w>=QQ['w'] 

sage: WZ.<z>=W['z'] 

sage: m = matrix(WZ,2,2,[1,z,z,z^2]) 

sage: a = m.charpoly() 

sage: R.<x> = WZ[] 

sage: R(a) 

x^2 + (-z^2 - 1)*x 

""" 

 

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

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

# 

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

# as published by the Free Software Foundation; either version 2 of 

# the License, or (at your option) any later version. 

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

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

from __future__ import print_function 

import six 

from six.moves import range 

 

from sage.rings.polynomial.polynomial_element import Polynomial, Polynomial_generic_dense, Polynomial_generic_dense_inexact 

from sage.structure.element import IntegralDomainElement, EuclideanDomainElement 

 

from sage.rings.polynomial.polynomial_singular_interface import Polynomial_singular_repr 

 

from sage.libs.pari.all import pari_gen 

from sage.structure.richcmp import richcmp, richcmp_item, rich_to_bool, rich_to_bool_sgn 

from sage.structure.element import coerce_binop 

 

from sage.rings.infinity import infinity, Infinity 

from sage.rings.integer_ring import ZZ 

from sage.rings.integer import Integer 

from sage.structure.factorization import Factorization 

 

 

class Polynomial_generic_sparse(Polynomial): 

""" 

A generic sparse polynomial. 

 

The ``Polynomial_generic_sparse`` class defines functionality for sparse 

polynomials over any base ring. A sparse polynomial is represented using a 

dictionary which maps each exponent to the corresponding coefficient. The 

coefficients must never be zero. 

 

EXAMPLES:: 

 

sage: R.<x> = PolynomialRing(PolynomialRing(QQ, 'y'), sparse=True) 

sage: f = x^3 - x + 17 

sage: type(f) 

<class 'sage.rings.polynomial.polynomial_ring.PolynomialRing_integral_domain_with_category.element_class'> 

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

True 

 

A more extensive example:: 

 

sage: A.<T> = PolynomialRing(Integers(5),sparse=True) ; f = T^2+1 ; B = A.quo(f) 

sage: C.<s> = PolynomialRing(B) 

sage: C 

Univariate Polynomial Ring in s over Univariate Quotient Polynomial Ring in Tbar over Ring of integers modulo 5 with modulus T^2 + 1 

sage: s + T 

s + Tbar 

sage: (s + T)**2 

s^2 + 2*Tbar*s + 4 

 

""" 

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

""" 

TESTS:: 

 

sage: PolynomialRing(RIF, 'z', sparse=True)([RIF(-1, 1), RIF(-1,1)]) 

0.?*z + 0.? 

sage: PolynomialRing(CIF, 'z', sparse=True)([CIF(RIF(-1,1), RIF(-1,1)), RIF(-1,1)]) 

0.?*z + 0.? + 0.?*I 

""" 

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

if x is None: 

self.__coeffs = {} 

return 

R = parent.base_ring() 

if isinstance(x, Polynomial): 

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

x = dict(x.dict()) 

elif x.parent() == R: 

x = {0:x} 

else: 

w = {} 

for n, c in six.iteritems(x.dict()): 

w[n] = R(c) 

# The following line has been added in trac ticket #9944. 

# Apparently, the "else" case has never occured before. 

x = w 

elif isinstance(x, list): 

x = dict((i, c) for (i, c) in enumerate(x) if c) 

elif isinstance(x, pari_gen): 

y = {} 

for i in range(len(x)): 

y[i] = R(x[i]) 

x = y 

check = True 

elif not isinstance(x, dict): 

x = {0:x} # constant polynomials 

if check: 

self.__coeffs = {} 

for i, z in six.iteritems(x): 

self.__coeffs[i] = R(z) 

else: 

self.__coeffs = x 

if check: 

self.__normalize() 

 

def dict(self): 

""" 

Return a new copy of the dict of the underlying 

elements of ``self``. 

 

EXAMPLES:: 

 

sage: R.<w> = PolynomialRing(Integers(8), sparse=True) 

sage: f = 5 + w^1997 - w^10000; f 

7*w^10000 + w^1997 + 5 

sage: d = f.dict(); d 

{0: 5, 1997: 1, 10000: 7} 

sage: d[0] = 10 

sage: f.dict() 

{0: 5, 1997: 1, 10000: 7} 

""" 

return dict(self.__coeffs) 

 

def coefficients(self, sparse=True): 

""" 

Return the coefficients of the monomials appearing in ``self``. 

 

EXAMPLES:: 

 

sage: R.<w> = PolynomialRing(Integers(8), sparse=True) 

sage: f = 5 + w^1997 - w^10000; f 

7*w^10000 + w^1997 + 5 

sage: f.coefficients() 

[5, 1, 7] 

 

TESTS: 

 

Check that all coefficients are in the base ring:: 

 

sage: S.<x> = PolynomialRing(QQ, sparse=True) 

sage: f = x^4 

sage: all(c.parent() is QQ for c in f.coefficients(False)) 

True 

""" 

if sparse: 

return [self.__coeffs[e] for e in self.exponents()] 

else: 

zero = self.parent().base_ring().zero() 

return [self.__coeffs[i] if i in self.__coeffs else zero 

for i in range(self.degree() + 1)] 

 

def exponents(self): 

""" 

Return the exponents of the monomials appearing in ``self``. 

 

EXAMPLES:: 

 

sage: R.<w> = PolynomialRing(Integers(8), sparse=True) 

sage: f = 5 + w^1997 - w^10000; f 

7*w^10000 + w^1997 + 5 

sage: f.exponents() 

[0, 1997, 10000] 

""" 

keys = self.__coeffs.keys() 

keys.sort() 

return keys 

 

def valuation(self): 

""" 

Return the valuation of ``self``. 

 

EXAMPLES:: 

 

sage: R.<w> = PolynomialRing(GF(9,'a'), sparse=True) 

sage: f = w^1997 - w^10000 

sage: f.valuation() 

1997 

sage: R(19).valuation() 

0 

sage: R(0).valuation() 

+Infinity 

""" 

if not self.__coeffs: 

return infinity 

return ZZ(min(self.__coeffs)) 

 

def _derivative(self, var=None): 

""" 

Computes formal derivative of this polynomial with respect to 

the given variable. 

 

If ``var`` is ``None`` or is the generator of this ring, the 

derivative is with respect to the generator. Otherwise, 

_derivative(var) is called recursively for each coefficient of 

this polynomial. 

 

.. SEEALSO:: :meth:`.derivative` 

 

EXAMPLES:: 

 

sage: R.<w> = PolynomialRing(ZZ, sparse=True) 

sage: f = R(range(9)); f 

8*w^8 + 7*w^7 + 6*w^6 + 5*w^5 + 4*w^4 + 3*w^3 + 2*w^2 + w 

sage: f._derivative() 

64*w^7 + 49*w^6 + 36*w^5 + 25*w^4 + 16*w^3 + 9*w^2 + 4*w + 1 

sage: f._derivative(w) 

64*w^7 + 49*w^6 + 36*w^5 + 25*w^4 + 16*w^3 + 9*w^2 + 4*w + 1 

 

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

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

sage: f = x^3*y^4 

sage: f._derivative() 

4*x^3*y^3 

sage: f._derivative(y) 

4*x^3*y^3 

sage: f._derivative(x) 

3*x^2*y^4 

""" 

P = self.parent() 

if var is not None and var is not P.gen(): 

# call _derivative() recursively on coefficients 

return P(dict([(n, c._derivative(var)) \ 

for (n, c) in six.iteritems(self.__coeffs)])) 

 

# compute formal derivative with respect to generator 

d = {} 

for n, c in six.iteritems(self.__coeffs): 

d[n-1] = n*c 

if -1 in d: 

del d[-1] 

return P(d) 

 

def integral(self, var=None): 

""" 

Return the integral of this polynomial. 

 

By default, the integration variable is the variable of the 

polynomial. 

 

Otherwise, the integration variable is the optional parameter ``var`` 

 

.. NOTE:: 

 

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

 

EXAMPLES:: 

 

sage: R.<x> = PolynomialRing(ZZ, sparse=True) 

sage: (1 + 3*x^10 - 2*x^100).integral() 

-2/101*x^101 + 3/11*x^11 + x 

 

TESTS: 

 

Check that :trac:`18600` is fixed:: 

 

sage: R.<x> = PolynomialRing(ZZ, sparse=True) 

sage: (x^2^100).integral() 

1/1267650600228229401496703205377*x^1267650600228229401496703205377 

 

Check the correctness when the base ring is a polynomial ring:: 

 

sage: R.<x> = PolynomialRing(ZZ, sparse=True) 

sage: S.<t> = PolynomialRing(R, sparse=True) 

sage: (x*t+1).integral() 

1/2*x*t^2 + t 

sage: (x*t+1).integral(x) 

1/2*x^2*t + x 

 

Check the correctness when the base ring is not an integral domain:: 

 

sage: R.<x> = PolynomialRing(Zmod(4), sparse=True) 

sage: (x^4 + 2*x^2 + 3).integral() 

x^5 + 2*x^3 + 3*x 

sage: x.integral() 

Traceback (most recent call last): 

... 

ZeroDivisionError: Inverse does not exist. 

""" 

R = self.parent() 

# TODO: 

# calling the coercion model bin_op is much more accurate than using the 

# true division (which is bypassed by polynomials). But it does not work 

# in all cases!! 

from sage.structure.element import coercion_model as cm 

import operator 

try: 

Q = cm.bin_op(R.one(), ZZ.one(), operator.truediv).parent() 

except TypeError: 

F = (R.base_ring().one()/ZZ.one()).parent() 

Q = R.change_ring(F) 

 

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

return Q({k:v.integral(var) for k,v in six.iteritems(self.__coeffs)}, check=False) 

 

return Q({ k+1:v/(k+1) for k,v in six.iteritems(self.__coeffs)}, check=False) 

 

def _dict_unsafe(self): 

""" 

Return unsafe access to the underlying dictionary of coefficients. 

 

** DO NOT use this, unless you really really know what you are doing. ** 

 

EXAMPLES:: 

 

sage: R.<w> = PolynomialRing(ZZ, sparse=True) 

sage: f = w^15 - w*3; f 

w^15 - 3*w 

sage: d = f._dict_unsafe(); d 

{1: -3, 15: 1} 

sage: d[1] = 10; f 

w^15 + 10*w 

""" 

return self.__coeffs 

 

def _repr(self, name=None): 

r""" 

EXAMPLES:: 

 

sage: R.<w> = PolynomialRing(CDF, sparse=True) 

sage: f = CDF(1,2) + w^5 - CDF(pi)*w + CDF(e) 

sage: f._repr() # abs tol 1e-15 

'1.0*w^5 - 3.141592653589793*w + 3.718281828459045 + 2.0*I' 

sage: f._repr(name='z') # abs tol 1e-15 

'1.0*z^5 - 3.141592653589793*z + 3.718281828459045 + 2.0*I' 

 

TESTS:: 

 

sage: pol = RIF['x']([0, 0, (-1,1)]) 

sage: PolynomialRing(RIF, 'x', sparse=True)(pol) 

0.?*x^2 

 

AUTHOR: 

 

- David Harvey (2006-08-05), based on Polynomial._repr() 

- Francis Clarke (2008-09-08) improved for 'negative' coefficients 

""" 

s = " " 

m = self.degree() + 1 

if name is None: 

name = self.parent().variable_name() 

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

coeffs = sorted(six.iteritems(self.__coeffs)) 

for (n, x) in reversed(coeffs): 

if x: 

if n != m-1: 

s += " + " 

x = y = repr(x) 

if y.find("-") == 0: 

y = y[1:] 

if not atomic_repr and n > 0 and (y.find("+") != -1 or y.find("-") != -1): 

x = "(%s)"%x 

if n > 1: 

var = "*%s^%s"%(name,n) 

elif n==1: 

var = "*%s"%name 

else: 

var = "" 

s += "%s%s"%(x,var) 

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

s = s.replace(" 1*"," ") 

s = s.replace(" -1*", " -") 

if s==" ": 

return "0" 

return s[1:] 

 

def __normalize(self): 

x = self.__coeffs 

D = [n for n, z in six.iteritems(x) if not z] 

for n in D: 

del x[n] 

 

def __getitem__(self,n): 

""" 

Return the `n`-th coefficient of this polynomial. 

 

Negative indexes are allowed and always return 0 (so you can 

view the polynomial as embedding Laurent series). 

 

EXAMPLES:: 

 

sage: R.<w> = PolynomialRing(RDF, sparse=True) 

sage: e = RDF(e) 

sage: f = sum(e^n*w^n for n in range(4)); f # abs tol 1.1e-14 

20.085536923187664*w^3 + 7.3890560989306495*w^2 + 2.718281828459045*w + 1.0 

sage: f[1] # abs tol 5e-16 

2.718281828459045 

sage: f[5] 

0.0 

sage: f[-1] 

0.0 

sage: R.<x> = PolynomialRing(RealField(19), sparse=True) 

sage: f = (2-3.5*x)^3; f 

-42.875*x^3 + 73.500*x^2 - 42.000*x + 8.0000 

 

Using slices, we can truncate polynomials:: 

 

sage: f[:2] 

-42.000*x + 8.0000 

 

Any other kind of slicing is deprecated or an error:: 

 

sage: f[1:3] 

doctest:...: DeprecationWarning: polynomial slicing with a start index is deprecated, use list() and slice the resulting list instead 

See http://trac.sagemath.org/18940 for details. 

73.500*x^2 - 42.000*x 

sage: f[1:3:2] 

Traceback (most recent call last): 

... 

NotImplementedError: polynomial slicing with a step is not defined 

sage: f["hello"] 

Traceback (most recent call last): 

... 

TypeError: list indices must be integers, not str 

""" 

if isinstance(n, slice): 

d = self.degree() + 1 

start, stop, step = n.start, n.stop, n.step 

if step is not None: 

raise NotImplementedError("polynomial slicing with a step is not defined") 

if start is None: 

start = 0 

else: 

if start < 0: 

start = 0 

from sage.misc.superseded import deprecation 

deprecation(18940, "polynomial slicing with a start index is deprecated, use list() and slice the resulting list instead") 

if stop is None or stop > d: 

stop = d 

x = self.__coeffs 

v = {k: x[k] for k in x.keys() if start <= k < stop} 

return self.parent()(v) 

 

try: 

n = n.__index__() 

except AttributeError: 

raise TypeError("list indices must be integers, not {0}".format(type(n).__name__)) 

try: 

return self.__coeffs[n] 

except KeyError: 

return self.base_ring().zero() 

 

def _unsafe_mutate(self, n, value): 

r""" 

Change the coefficient of `x^n` to value. 

 

** NEVER USE THIS ** -- unless you really know what you are doing. 

 

EXAMPLES:: 

 

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

sage: f = z^2 + CC.0; f 

1.00000000000000*z^2 + 1.00000000000000*I 

sage: f._unsafe_mutate(0, 10) 

sage: f 

1.00000000000000*z^2 + 10.0000000000000 

 

Much more nasty:: 

 

sage: z._unsafe_mutate(1, 0) 

sage: z 

0 

""" 

n = int(n) 

value = self.base_ring()(value) 

x = self.__coeffs 

if n < 0: 

raise IndexError("polynomial coefficient index must be nonnegative") 

if value == 0: 

if n in x: 

del x[n] 

else: 

x[n] = value 

 

def list(self, copy=True): 

""" 

Return a new copy of the list of the underlying 

elements of ``self``. 

 

EXAMPLES:: 

 

sage: R.<z> = PolynomialRing(Integers(100), sparse=True) 

sage: f = 13*z^5 + 15*z^2 + 17*z 

sage: f.list() 

[0, 17, 15, 0, 0, 13] 

""" 

zero = self.base_ring().zero() 

v = [zero] * (self.degree()+1) 

for n, x in six.iteritems(self.__coeffs): 

v[n] = x 

return v 

 

def degree(self, gen=None): 

""" 

Return the degree of this sparse polynomial. 

 

EXAMPLES:: 

 

sage: R.<z> = PolynomialRing(ZZ, sparse=True) 

sage: f = 13*z^50000 + 15*z^2 + 17*z 

sage: f.degree() 

50000 

""" 

if not self.__coeffs: 

return -1 

return max(self.__coeffs) 

 

def _add_(self, right): 

r""" 

EXAMPLES:: 

 

sage: R.<x> = PolynomialRing(Integers(), sparse=True) 

sage: (x^100000 + 2*x^50000) + (4*x^75000 - 2*x^50000 + 3*x) 

x^100000 + 4*x^75000 + 3*x 

 

AUTHOR: 

 

- David Harvey (2006-08-05) 

""" 

output = dict(self.__coeffs) 

 

for (index, coeff) in six.iteritems(right.__coeffs): 

if index in output: 

output[index] += coeff 

else: 

output[index] = coeff 

 

output = self.parent()(output, check=False) 

output.__normalize() 

return output 

 

def _neg_(self): 

r""" 

EXAMPLES:: 

 

sage: R.<x> = PolynomialRing(Integers(), sparse=True) 

sage: a = x^10000000; a 

x^10000000 

sage: -a 

-x^10000000 

""" 

output = { } 

for (index, coeff) in six.iteritems(self.__coeffs): 

output[index] = -coeff 

output = self.parent()(output, check=False) 

return output 

 

def _mul_(self, right): 

r""" 

EXAMPLES:: 

 

sage: R.<x> = PolynomialRing(ZZ, sparse=True) 

sage: (x^100000 - x^50000) * (x^100000 + x^50000) 

x^200000 - x^100000 

sage: (x^100000 - x^50000) * R(0) 

0 

 

AUTHOR: 

- David Harvey (2006-08-05) 

""" 

output = {} 

 

for (index1, coeff1) in six.iteritems(self.__coeffs): 

for (index2, coeff2) in six.iteritems(right.__coeffs): 

product = coeff1 * coeff2 

index = index1 + index2 

if index in output: 

output[index] += product 

else: 

output[index] = product 

 

output = self.parent()(output, check=False) 

output.__normalize() 

return output 

 

def _rmul_(self, left): 

r""" 

EXAMPLES:: 

 

sage: R.<x> = PolynomialRing(ZZ, sparse=True) 

sage: (x^100000 - x^50000) * (x^100000 + x^50000) 

x^200000 - x^100000 

sage: 7 * (x^100000 - x^50000) # indirect doctest 

7*x^100000 - 7*x^50000 

 

AUTHOR: 

 

- Simon King (2011-03-31) 

""" 

output = {} 

 

for (index, coeff) in six.iteritems(self.__coeffs): 

output[index] = left * coeff 

 

output = self.parent()(output, check=False) 

output.__normalize() 

return output 

 

def _lmul_(self, right): 

r""" 

EXAMPLES:: 

 

sage: R.<x> = PolynomialRing(ZZ, sparse=True) 

sage: (x^100000 - x^50000) * (x^100000 + x^50000) 

x^200000 - x^100000 

sage: (x^100000 - x^50000) * 7 # indirect doctest 

7*x^100000 - 7*x^50000 

 

AUTHOR: 

 

- Simon King (2011-03-31) 

""" 

output = {} 

 

for (index, coeff) in six.iteritems(self.__coeffs): 

output[index] = coeff * right 

 

output = self.parent()(output, check=False) 

output.__normalize() 

return output 

 

def _richcmp_(self, other, op): 

""" 

Compare this polynomial with other. 

 

Polynomials are first compared by degree, then in dictionary order 

starting with the coefficient of largest degree. 

 

EXAMPLES:: 

 

sage: R.<x> = PolynomialRing(ZZ, sparse=True) 

sage: 3*x^100 - 12 > 12*x + 5 

True 

sage: 3*x^100 - 12 > 3*x^100 - x^50 + 5 

True 

sage: 3*x^100 - 12 < 3*x^100 - x^50 + 5 

False 

sage: x^100 + x^10 - 1 < x^100 + x^10 

True 

sage: x^100 < x^100 - x^10 

False 

 

TESTS:: 

 

sage: R.<x> = PolynomialRing(QQ, sparse=True) 

sage: 2*x^2^500 > x^2^500 

True 

 

sage: Rd = PolynomialRing(ZZ, 'x', sparse=False) 

sage: Rs = PolynomialRing(ZZ, 'x', sparse=True) 

sage: for _ in range(100): 

....: pd = Rd.random_element() 

....: qd = Rd.random_element() 

....: assert bool(pd < qd) == bool(Rs(pd) < Rs(qd)) 

""" 

d1 = self.degree() 

d2 = other.degree() 

 

# Special case constant polynomials 

if d1 <= 0 and d2 <= 0: 

return richcmp(self[0], other[0], op) 

 

# For different degrees, compare the degree 

if d1 != d2: 

return rich_to_bool_sgn(op, d1 - d2) 

 

degs = set(self.__coeffs) | set(other.__coeffs) 

for i in sorted(degs, reverse=True): 

x = self[i] 

y = other[i] 

res = richcmp_item(x, y, op) 

if res is not NotImplemented: 

return res 

return rich_to_bool(op, 0) 

 

def shift(self, n): 

r""" 

Returns this polynomial multiplied by the power `x^n`. 

 

If `n` is negative, terms below `x^n` will be discarded. Does 

not change this polynomial. 

 

EXAMPLES:: 

 

sage: R.<x> = PolynomialRing(ZZ, sparse=True) 

sage: p = x^100000 + 2*x + 4 

sage: type(p) 

<class 'sage.rings.polynomial.polynomial_ring.PolynomialRing_integral_domain_with_category.element_class'> 

sage: p.shift(0) 

x^100000 + 2*x + 4 

sage: p.shift(-1) 

x^99999 + 2 

sage: p.shift(-100002) 

0 

sage: p.shift(2) 

x^100002 + 2*x^3 + 4*x^2 

 

TESTS: 

 

Check that :trac:`18600` is fixed:: 

 

sage: R.<x> = PolynomialRing(ZZ, sparse=True) 

sage: p = x^2^100 - 5 

sage: p.shift(10) 

x^1267650600228229401496703205386 - 5*x^10 

sage: p.shift(-10) 

x^1267650600228229401496703205366 

sage: p.shift(1.5) 

Traceback (most recent call last): 

... 

TypeError: Attempt to coerce non-integral RealNumber to Integer 

 

AUTHOR: 

- David Harvey (2006-08-06) 

""" 

n = ZZ(n) 

if n == 0: 

return self 

if n > 0: 

output = {index+n: coeff for index, coeff in six.iteritems(self.__coeffs)} 

return self.parent()(output, check=False) 

if n < 0: 

output = {index+n:coeff for index, coeff in six.iteritems(self.__coeffs) if index + n >= 0} 

return self.parent()(output, check=False) 

 

@coerce_binop 

def quo_rem(self, other): 

""" 

Returns the quotient and remainder of the Euclidean division of 

``self`` and ``other``. 

 

Raises ZerodivisionError if ``other`` is zero. Raises ArithmeticError 

if ``other`` has a nonunit leading coefficient. 

 

EXAMPLES:: 

 

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

sage: R.<y> = PolynomialRing(P,sparse=True) 

sage: f = R.random_element(10) 

sage: g = y^5+R.random_element(4) 

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

sage: f == q*g + r and r.degree() < g.degree() 

True 

sage: g = x*y^5 

sage: f.quo_rem(g) 

Traceback (most recent call last): 

... 

ArithmeticError: Division non exact (consider coercing to polynomials over the fraction field) 

sage: g = 0 

sage: f.quo_rem(g) 

Traceback (most recent call last): 

... 

ZeroDivisionError: Division by zero polynomial 

 

TESTS:: 

 

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

sage: f = x^10-4*x^6-5 

sage: g = 17*x^22+x^15-3*x^5+1 

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

sage: g == f*q + r and r.degree() < f.degree() 

True 

sage: zero = P(0) 

sage: zero.quo_rem(f) 

(0, 0) 

sage: Q.<y> = IntegerModRing(14)[] 

sage: f = y^10-4*y^6-5 

sage: g = 17*y^22+y^15-3*y^5+1 

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

sage: g == f*q + r and r.degree() < f.degree() 

True 

sage: f += 2*y^10 # 3 is invertible mod 14 

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

sage: g == f*q + r and r.degree() < f.degree() 

True 

 

The following shows that :trac:`16649` is indeed fixed. :: 

 

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

sage: (4*x).quo_rem(2*x) 

(2, 0) 

 

AUTHORS: 

 

- Bruno Grenet (2014-07-09) 

""" 

if other.is_zero(): 

raise ZeroDivisionError("Division by zero polynomial") 

if self.is_zero(): 

return self, self 

 

R = self.parent() 

 

d = other.degree() 

if self.degree() < d: 

return R.zero(), self 

 

quo = R.zero() 

rem = self 

 

while rem.degree() >= d: 

try: 

c = R(rem.leading_coefficient() * ~other.leading_coefficient()) 

except TypeError: 

raise ArithmeticError("Division non exact (consider coercing to polynomials over the fraction field)") 

e = rem.degree() - d 

quo += c*R.one().shift(e) 

# we know that the leading coefficient of rem vanishes 

# thus we avoid doing a useless computation 

rem = rem[:rem.degree()] - c*other[:d].shift(e) 

return (quo,rem) 

 

@coerce_binop 

def gcd(self,other,algorithm=None): 

""" 

Return the gcd of this polynomial and ``other`` 

 

INPUT: 

 

- ``other`` -- a polynomial defined over the same ring as this 

polynomial. 

 

ALGORITHM: 

 

Two algorithms are provided: 

 

- ``generic``: Uses the generic implementation, which depends on the 

base ring being a UFD or a field. 

- ``dense``: The polynomials are converted to the dense representation, 

their gcd is computed and is converted back to the sparse 

representation. 

 

Default is ``dense`` for polynomials over ZZ and ``generic`` in the 

other cases. 

 

EXAMPLES:: 

 

sage: R.<x> = PolynomialRing(ZZ,sparse=True) 

sage: p = x^6 + 7*x^5 + 8*x^4 + 6*x^3 + 2*x^2 + x + 2 

sage: q = 2*x^4 - x^3 - 2*x^2 - 4*x - 1 

sage: gcd(p,q) 

x^2 + x + 1 

sage: gcd(p, q, algorithm = "dense") 

x^2 + x + 1 

sage: gcd(p, q, algorithm = "generic") 

x^2 + x + 1 

sage: gcd(p, q, algorithm = "foobar") 

Traceback (most recent call last): 

... 

ValueError: Unknown algorithm 'foobar' 

 

TESTS: 

 

Check that :trac:`19676` is fixed:: 

 

sage: S.<y> = R[] 

sage: x.gcd(y) 

1 

sage: (6*x).gcd(9) 

3 

""" 

 

from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing 

from sage.arith.all import lcm 

 

if algorithm is None: 

if self.base_ring() == ZZ: 

algorithm = "dense" 

else: 

algorithm = "generic" 

if algorithm=="dense": 

S = self.parent() 

# FLINT is faster but a bug makes the conversion extremely slow, 

# so NTL is used in those cases where the conversion is too slow. Cf 

# <https://groups.google.com/d/msg/sage-devel/6qhW90dgd1k/Hoq3N7fWe4QJ> 

sd = self.degree() 

od = other.degree() 

if max(sd,od)<100 or \ 

min(len(self.__coeffs)/sd, len(other.__coeffs)/od)>.06: 

implementation="FLINT" 

else: 

implementation="NTL" 

D = PolynomialRing(S.base_ring(),'x',implementation=implementation) 

g = D(self).gcd(D(other)) 

return S(g) 

elif algorithm=="generic": 

return Polynomial.gcd(self,other) 

else: 

raise ValueError("Unknown algorithm '%s'" % algorithm) 

 

def reverse(self, degree=None): 

""" 

Return this polynomial but with the coefficients reversed. 

 

If an optional degree argument is given the coefficient list will be 

truncated or zero padded as necessary and the reverse polynomial will 

have the specified degree. 

 

EXAMPLES:: 

 

sage: R.<x> = PolynomialRing(ZZ, sparse=True) 

sage: p = x^4 + 2*x^2^100 

sage: p.reverse() 

x^1267650600228229401496703205372 + 2 

sage: p.reverse(10) 

x^6 

""" 

if degree is None: 

degree = self.degree() 

if not isinstance(degree, (int,Integer)): 

raise ValueError("degree argument must be a nonnegative integer, got %s"%degree) 

d = {degree-k: v for k,v in six.iteritems(self.__coeffs) if degree >= k} 

return self.parent()(d, check=False) 

 

def truncate(self, n): 

""" 

Return the polynomial of degree `< n` equal to `self` modulo `x^n`. 

 

EXAMPLES:: 

 

sage: R.<x> = PolynomialRing(ZZ, sparse=True) 

sage: (x^11 + x^10 + 1).truncate(11) 

x^10 + 1 

sage: (x^2^500 + x^2^100 + 1).truncate(2^101) 

x^1267650600228229401496703205376 + 1 

""" 

return self[:n] 

 

def number_of_terms(self): 

""" 

Return the number of nonzero terms. 

 

EXAMPLES:: 

 

sage: R.<x> = PolynomialRing(ZZ,sparse=True) 

sage: p = x^100 - 3*x^10 + 12 

sage: p.number_of_terms() 

3 

""" 

return len(self.__coeffs) 

 

class Polynomial_generic_domain(Polynomial, IntegralDomainElement): 

def __init__(self, parent, is_gen=False, construct=False): 

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

 

def is_unit(self): 

r""" 

Return ``True`` if this polynomial is a unit. 

 

*EXERCISE* (Atiyah-McDonald, Ch 1): Let `A[x]` be a polynomial 

ring in one variable. Then `f=\sum a_i x^i \in A[x]` is a 

unit if and only if `a_0` is a unit and `a_1,\ldots, a_n` are 

nilpotent. 

 

EXAMPLES:: 

 

sage: R.<z> = PolynomialRing(ZZ, sparse=True) 

sage: (2 + z^3).is_unit() 

False 

sage: f = -1 + 3*z^3; f 

3*z^3 - 1 

sage: f.is_unit() 

False 

sage: R(-3).is_unit() 

False 

sage: R(-1).is_unit() 

True 

sage: R(0).is_unit() 

False 

""" 

if self.degree() > 0: 

return False 

return self[0].is_unit() 

 

class Polynomial_generic_field(Polynomial_singular_repr, 

Polynomial_generic_domain, 

EuclideanDomainElement): 

 

@coerce_binop 

def quo_rem(self, other): 

""" 

Returns a tuple (quotient, remainder) where 

self = quotient * other + remainder. 

 

EXAMPLES:: 

 

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

sage: K.<t> = NumberField(y^2 - 2) 

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

sage: x.quo_rem(K(1)) 

(x, 0) 

sage: x.xgcd(K(1)) 

(1, 0, 1) 

""" 

P = self.parent() 

if other.is_zero(): 

raise ZeroDivisionError("other must be nonzero") 

 

# This is algorithm 3.1.1 in Cohen GTM 138 

A = self 

B = other 

R = A 

Q = P.zero() 

while R.degree() >= B.degree(): 

aaa = R.leading_coefficient()/B.leading_coefficient() 

diff_deg=R.degree()-B.degree() 

Q += P(aaa).shift(diff_deg) 

# We know that S*B exactly cancels the leading coefficient of R. 

# Thus, we skip the computation of this leading coefficient. 

# For most exact fields, this doesn't matter much; but for 

# inexact fields, the leading coefficient might not end up 

# exactly equal to zero; and for AA/QQbar, verifying that 

# the coefficient is exactly zero triggers exact computation. 

R = R[:R.degree()] - (aaa*B[:B.degree()]).shift(diff_deg) 

return (Q, R) 

 

 

class Polynomial_generic_sparse_field(Polynomial_generic_sparse, Polynomial_generic_field): 

""" 

EXAMPLES:: 

 

sage: R.<x> = PolynomialRing(Frac(RR['t']), sparse=True) 

sage: f = x^3 - x + 17 

sage: type(f) 

<class 'sage.rings.polynomial.polynomial_ring.PolynomialRing_field_with_category.element_class'> 

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

True 

""" 

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

Polynomial_generic_sparse.__init__(self, parent, x, check, is_gen) 

 

 

class Polynomial_generic_dense_field(Polynomial_generic_dense, Polynomial_generic_field): 

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

Polynomial_generic_dense.__init__(self, parent, x, check, is_gen) 

 

 

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

# Over discrete valuation rings and fields 

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

 

class Polynomial_generic_cdv(Polynomial_generic_domain): 

""" 

A generic class for polynomials over complete discrete 

valuation domains and fields. 

 

AUTHOR: 

 

- Xavier Caruso (2013-03) 

""" 

def newton_slopes(self, repetition=True): 

""" 

Returns a list of the Newton slopes of this polynomial. 

 

These are the valuations of the roots of this polynomial. 

 

If ``repetition`` is ``True``, each slope is repeated a number of 

times equal to its multiplicity. Otherwise it appears only 

one time. 

 

EXAMPLES:: 

 

sage: K = Qp(5) 

sage: R.<t> = K[] 

sage: f = 5 + 3*t + t^4 + 25*t^10 

sage: f.newton_polygon() 

Finite Newton polygon with 4 vertices: (0, 1), (1, 0), (4, 0), (10, 2) 

sage: f.newton_slopes() 

[1, 0, 0, 0, -1/3, -1/3, -1/3, -1/3, -1/3, -1/3] 

 

sage: f.newton_slopes(repetition=False) 

[1, 0, -1/3] 

 

AUTHOR: 

 

- Xavier Caruso (2013-03-20) 

""" 

polygon = self.newton_polygon() 

return [-s for s in polygon.slopes(repetition=repetition)] 

 

def newton_polygon(self): 

r""" 

Returns a list of vertices of the Newton polygon of this polynomial. 

 

.. NOTE:: 

 

If some coefficients have not enough precision an error is raised. 

 

EXAMPLES:: 

 

sage: K = Qp(5) 

sage: R.<t> = K[] 

sage: f = 5 + 3*t + t^4 + 25*t^10 

sage: f.newton_polygon() 

Finite Newton polygon with 4 vertices: (0, 1), (1, 0), (4, 0), (10, 2) 

 

sage: g = f + K(0,0)*t^4; g 

(5^2 + O(5^22))*t^10 + (O(5^0))*t^4 + (3 + O(5^20))*t + (5 + O(5^21)) 

sage: g.newton_polygon() 

Traceback (most recent call last): 

... 

PrecisionError: The coefficient of t^4 has not enough precision 

 

TESTS: 

 

Check that :trac:`22936` is fixed:: 

 

sage: S.<x> = PowerSeriesRing(GF(5)) 

sage: R.<y> = S[] 

sage: p = x^2+y+x*y^2 

sage: p.newton_polygon() 

Finite Newton polygon with 3 vertices: (0, 2), (1, 0), (2, 1) 

 

AUTHOR: 

 

- Xavier Caruso (2013-03-20) 

""" 

d = self.degree() 

from sage.geometry.newton_polygon import NewtonPolygon 

polygon = NewtonPolygon([(x, self[x].valuation()) for x in range(d+1)]) 

polygon_prec = NewtonPolygon([ (x, self[x].precision_absolute()) for x in range(d+1) ]) 

vertices = polygon.vertices(copy=False) 

vertices_prec = polygon_prec.vertices(copy=False) 

if len(vertices_prec) > 0: 

if vertices[0][0] > vertices_prec[0][0]: 

raise PrecisionError("first term with non-infinite valuation must have determined valuation") 

elif vertices[-1][0] < vertices_prec[-1][0]: 

raise PrecisionError("last term with non-infinite valuation must have determined valuation") 

else: 

for (x, y) in vertices: 

if polygon_prec(x) <= y: 

raise PrecisionError("The coefficient of %s^%s has not enough precision" % (self.parent().variable_name(), x)) 

return polygon 

 

def hensel_lift(self, a): 

""" 

Lift `a` to a root of this polynomial (using 

Newton iteration). 

 

If `a` is not close enough to a root (so that 

Newton iteration does not converge), an error 

is raised. 

 

EXAMPLES:: 

 

sage: K = Qp(5, 10) 

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

sage: f = x^2 + 1 

sage: root = f.hensel_lift(2); root 

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

sage: f(root) 

O(5^10) 

 

sage: g = (x^2 + 1)*(x - 7) 

sage: g.hensel_lift(2) # here, 2 is a multiple root modulo p 

Traceback (most recent call last): 

... 

ValueError: a is not close enough to a root of this polynomial 

 

AUTHOR: 

 

- Xavier Caruso (2013-03-23) 

""" 

base = self.base_ring() 

selfa = self(a) 

der = self.derivative() 

dera = der(a) 

if selfa.valuation() <= 2 * dera.valuation(): 

raise ValueError("a is not close enough to a root of this polynomial") 

# Newton iteration 

# Todo: compute everything up to the adequate precision at each step 

b = ~dera 

while(True): 

na = a - selfa * b 

if na == a: return a 

a = na 

selfa = self(a) 

dera = der(a) 

b *= 2 - dera*b 

 

def _factor_of_degree(self, deg): 

""" 

Return a factor of ``self`` of degree ``deg``. 

 

Algorithm is Newton iteration. 

 

This fails if ``deg`` is not a breakpoint in the Newton 

polygon of ``self``. 

 

Only for internal use! 

 

EXAMPLES:: 

 

sage: K = Qp(5) 

sage: R.<x> = K[] 

sage: K = Qp(5) 

sage: R.<t> = K[] 

sage: f = 5 + 3*t + t^4 + 25*t^10 

 

sage: g = f._factor_of_degree(4) 

sage: (f % g).is_zero() 

True 

 

sage: g = f._factor_of_degree(3) # not tested 

Traceback (most recent call last) 

... 

KeyboardInterrupt: 

 

TESTS:: 

 

sage: S.<x> = PowerSeriesRing(GF(5)) 

sage: R.<y> = S[] 

sage: p = x^2+y+x*y^2 

sage: p._factor_of_degree(1) 

(1 + O(x^20))*y + x^2 + x^5 + 2*x^8 + 4*x^14 + 2*x^17 + 2*x^20 + O(x^22) 

 

AUTHOR: 

 

- Xavier Caruso (2013-03-20) 

 

.. TODO:: 

 

Precision is not optimal, and can be improved. 

""" 

coeffs = self.list() 

a = coeffs[:deg+1] 

# The leading coefficient need to be known at finite precision 

# in order to ensure that the while loop below terminates 

if a[deg].precision_absolute() is Infinity: 

a[deg] = a[deg].add_bigoh(self.base_ring().default_prec()) 

 

parent = self.parent() 

a = parent(a) 

b = v = parent(1) 

x = self % a 

while(not x.is_zero()): 

a += (v * x) % a 

b, x = self.quo_rem(a) 

b %= a 

v = (v * (2 - b*v)) % a 

 

return a 

 

def factor_of_slope(self, slope=None): 

""" 

INPUT: 

 

- slope -- a rational number (default: the first slope 

in the Newton polygon of ``self``) 

 

OUTPUT: 

 

The factor of ``self`` corresponding to the slope ``slope`` (i.e. 

the unique monic divisor of ``self`` whose slope is ``slope`` and 

degree is the length of ``slope`` in the Newton polygon). 

 

EXAMPLES:: 

 

sage: K = Qp(5) 

sage: R.<x> = K[] 

sage: K = Qp(5) 

sage: R.<t> = K[] 

sage: f = 5 + 3*t + t^4 + 25*t^10 

sage: f.newton_slopes() 

[1, 0, 0, 0, -1/3, -1/3, -1/3, -1/3, -1/3, -1/3] 

 

sage: g = f.factor_of_slope(0) 

sage: g.newton_slopes() 

[0, 0, 0] 

sage: (f % g).is_zero() 

True 

 

sage: h = f.factor_of_slope() 

sage: h.newton_slopes() 

[1] 

sage: (f % h).is_zero() 

True 

 

If ``slope`` is not a slope of ``self``, the corresponding factor 

is `1`:: 

 

sage: f.factor_of_slope(-1) 

(1 + O(5^20)) 

 

AUTHOR: 

 

- Xavier Caruso (2013-03-20) 

""" 

one = self.parent()(1) 

vertices = self.newton_polygon().vertices(copy=False) 

if len(vertices) < 2: 

if slope is Infinity: 

return self.parent().gen() ** self.degree() 

else: 

return one 

if slope is None: 

deg_first = vertices[0][0] 

deg_last = vertices[1][0] 

else: 

(deg_first, y_first) = vertices[0] 

for i in range(1, len(vertices)): 

(deg_last, y_last) = vertices[i] 

slope_cur = (y_first - y_last) / (deg_last - deg_first) 

if slope_cur == slope: 

break 

elif slope_cur < slope: 

return one 

deg_first = deg_last 

y_first = y_last 

if slope_cur > slope: 

return one 

if deg_last == self.degree(): 

div = self 

else: 

div = self._factor_of_degree(deg_last) 

if deg_first > 0: 

div2 = div._factor_of_degree(deg_first) 

div,_ = div.quo_rem(div2) 

return div.monic() 

 

def slope_factorization(self): 

""" 

Return a factorization of ``self`` into a product of factors 

corresponding to each slope in the Newton polygon. 

 

EXAMPLES:: 

 

sage: K = Qp(5) 

sage: R.<x> = K[] 

sage: K = Qp(5) 

sage: R.<t> = K[] 

sage: f = 5 + 3*t + t^4 + 25*t^10 

sage: f.newton_slopes() 

[1, 0, 0, 0, -1/3, -1/3, -1/3, -1/3, -1/3, -1/3] 

 

sage: F = f.slope_factorization() 

sage: F.prod() == f 

True 

sage: for (f,_) in F: 

....: print(f.newton_slopes()) 

[-1/3, -1/3, -1/3, -1/3, -1/3, -1/3] 

[0, 0, 0] 

[1] 

 

TESTS:: 

 

sage: S.<x> = PowerSeriesRing(GF(5)) 

sage: R.<y> = S[] 

sage: p = x^2+y+x*y^2 

sage: p.slope_factorization() 

(x) * ((x + O(x^22))*y + 1 + 4*x^3 + 4*x^6 + 3*x^9 + x^15 + 3*x^18 + O(x^21)) * ((x^-1 + O(x^20))*y + x + x^4 + 2*x^7 + 4*x^13 + 2*x^16 + 2*x^19 + O(x^22)) 

 

AUTHOR: 

 

- Xavier Caruso (2013-03-20) 

""" 

vertices = self.newton_polygon().vertices(copy=False) 

 

unit = self.leading_coefficient() 

P = ~unit * self 

 

deg_first = vertices[0][0] 

factors = [ ] 

if deg_first > 0: 

P >>= deg_first 

factors.append((self._parent.gen(), deg_first)) 

if len(vertices) > 2: 

for i in range(1, len(vertices)-1): 

deg = vertices[i][0] 

div = P._factor_of_degree(deg-deg_first) 

factors.append((div,1)) 

P,_ = P.quo_rem(div) 

deg_first = deg 

if len(vertices) > 1: 

factors.append((P, 1)) 

factors.reverse() 

return Factorization(factors, sort=False, unit=unit) 

 

class Polynomial_generic_dense_cdv(Polynomial_generic_dense_inexact, Polynomial_generic_cdv): 

pass 

 

class Polynomial_generic_sparse_cdv(Polynomial_generic_sparse, Polynomial_generic_cdv): 

pass 

 

 

class Polynomial_generic_cdvr(Polynomial_generic_cdv): 

pass 

 

class Polynomial_generic_dense_cdvr(Polynomial_generic_dense_cdv, Polynomial_generic_cdvr): 

pass 

 

class Polynomial_generic_sparse_cdvr(Polynomial_generic_sparse_cdv, Polynomial_generic_cdvr): 

pass 

 

 

class Polynomial_generic_cdvf(Polynomial_generic_cdv, Polynomial_generic_field): 

pass 

 

class Polynomial_generic_dense_cdvf(Polynomial_generic_dense_cdv, Polynomial_generic_cdvf): 

pass 

 

class Polynomial_generic_sparse_cdvf(Polynomial_generic_sparse_cdv, Polynomial_generic_cdvf): 

pass 

 

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

# XXX: Ensures that the generic polynomials implemented in SAGE via PARI # 

# until at least until 4.5.0 unpickle correctly as polynomials implemented # 

# via FLINT. # 

from sage.structure.sage_object import register_unpickle_override 

from sage.rings.polynomial.polynomial_rational_flint import Polynomial_rational_flint 

 

register_unpickle_override( \ 

'sage.rings.polynomial.polynomial_element_generic', \ 

'Polynomial_rational_dense', Polynomial_rational_flint)