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

""" 

Polynomial Template for C/C++ Library Interfaces 

""" 

  

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

# Copyright (C) 2008 Martin Albrecht <M.R.Albrecht@rhul.ac.uk> 

# Copyright (C) 2008 Robert Bradshaw 

# 

# 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 sage.rings.polynomial.polynomial_element cimport Polynomial 

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

from sage.structure.element import coerce_binop, bin_op 

from sage.structure.richcmp cimport rich_to_bool 

from sage.rings.fraction_field_element import FractionFieldElement 

from sage.rings.integer cimport Integer 

from sage.libs.all import pari_gen 

  

import operator 

  

from sage.interfaces.all import singular as singular_default 

  

def make_element(parent, args): 

return parent(*args) 

  

cdef inline Polynomial_template element_shift(self, int n): 

if not isinstance(self, Polynomial_template): 

if n > 0: 

error_msg = "Cannot shift %s << %n."%(self, n) 

else: 

error_msg = "Cannot shift %s >> %n."%(self, n) 

raise TypeError(error_msg) 

  

if n == 0: 

return self 

  

cdef celement *gen = celement_new((<Polynomial_template>self)._cparent) 

celement_gen(gen, 0, (<Polynomial_template>self)._cparent) 

celement_pow(gen, gen, abs(n), NULL, (<Polynomial_template>self)._cparent) 

cdef type T = type(self) 

cdef Polynomial_template r = <Polynomial_template>T.__new__(T) 

celement_construct(&r.x, (<Polynomial_template>self)._cparent) 

r._parent = (<Polynomial_template>self)._parent 

r._cparent = (<Polynomial_template>self)._cparent 

  

if n > 0: 

celement_mul(&r.x, &(<Polynomial_template>self).x, gen, (<Polynomial_template>self)._cparent) 

else: 

celement_floordiv(&r.x, &(<Polynomial_template>self).x, gen, (<Polynomial_template>self)._cparent) 

  

celement_delete(gen, (<Polynomial_template>self)._cparent) 

return r 

  

cdef class Polynomial_template(Polynomial): 

r""" 

Template for interfacing to external C / C++ libraries for implementations of polynomials. 

  

AUTHORS: 

  

- Robert Bradshaw (2008-10): original idea for templating 

- Martin Albrecht (2008-10): initial implementation 

  

This file implements a simple templating engine for linking univariate 

polynomials to their C/C++ library implementations. It requires a 

'linkage' file which implements the ``celement_`` functions (see 

:mod:`sage.libs.ntl.ntl_GF2X_linkage` for an example). Both parts are 

then plugged together by inclusion of the linkage file when inheriting from 

this class. See :mod:`sage.rings.polynomial.polynomial_gf2x` for an 

example. 

  

We illustrate the generic glueing using univariate polynomials over 

`\mathop{\mathrm{GF}}(2)`. 

  

.. note:: 

  

Implementations using this template MUST implement coercion from base 

ring elements and :meth:`get_unsafe`. See 

:class:`~sage.rings.polynomial.polynomial_gf2x.Polynomial_GF2X` for an 

example. 

""" 

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

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: P(0) 

0 

sage: P(GF(2)(1)) 

1 

sage: P(3) 

1 

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

x^2 + 1 

sage: P(list(map(GF(2),[1,0,1]))) 

x^2 + 1 

""" 

cdef celement *gen 

cdef celement *monomial 

cdef Py_ssize_t deg 

  

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

  

(<Polynomial_template>self)._cparent = get_cparent(self._parent) 

  

if is_gen: 

celement_construct(&self.x, (<Polynomial_template>self)._cparent) 

celement_gen(&self.x, 0, (<Polynomial_template>self)._cparent) 

  

elif isinstance(x, Polynomial_template): 

try: 

celement_construct(&self.x, (<Polynomial_template>self)._cparent) 

celement_set(&self.x, &(<Polynomial_template>x).x, (<Polynomial_template>self)._cparent) 

except NotImplementedError: 

raise TypeError("%s not understood."%x) 

  

elif isinstance(x, int) or isinstance(x, Integer): 

try: 

celement_construct(&self.x, (<Polynomial_template>self)._cparent) 

celement_set_si(&self.x, int(x), (<Polynomial_template>self)._cparent) 

except NotImplementedError: 

raise TypeError("%s not understood."%x) 

  

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

celement_construct(&self.x, (<Polynomial_template>self)._cparent) 

gen = celement_new((<Polynomial_template>self)._cparent) 

monomial = celement_new((<Polynomial_template>self)._cparent) 

  

celement_set_si(&self.x, 0, (<Polynomial_template>self)._cparent) 

celement_gen(gen, 0, (<Polynomial_template>self)._cparent) 

  

deg = 0 

for e in x: 

# r += parent(e)*power 

celement_pow(monomial, gen, deg, NULL, (<Polynomial_template>self)._cparent) 

celement_mul(monomial, &(<Polynomial_template>self.__class__(parent, e)).x, monomial, (<Polynomial_template>self)._cparent) 

celement_add(&self.x, &self.x, monomial, (<Polynomial_template>self)._cparent) 

deg += 1 

  

celement_delete(gen, (<Polynomial_template>self)._cparent) 

celement_delete(monomial, (<Polynomial_template>self)._cparent) 

  

elif isinstance(x, dict): 

celement_construct(&self.x, (<Polynomial_template>self)._cparent) 

gen = celement_new((<Polynomial_template>self)._cparent) 

monomial = celement_new((<Polynomial_template>self)._cparent) 

  

celement_set_si(&self.x, 0, (<Polynomial_template>self)._cparent) 

celement_gen(gen, 0, (<Polynomial_template>self)._cparent) 

  

for deg, coef in x.iteritems(): 

celement_pow(monomial, gen, deg, NULL, (<Polynomial_template>self)._cparent) 

celement_mul(monomial, &(<Polynomial_template>self.__class__(parent, coef)).x, monomial, (<Polynomial_template>self)._cparent) 

celement_add(&self.x, &self.x, monomial, (<Polynomial_template>self)._cparent) 

  

celement_delete(gen, (<Polynomial_template>self)._cparent) 

celement_delete(monomial, (<Polynomial_template>self)._cparent) 

  

elif isinstance(x, pari_gen): 

k = (<Polynomial_template>self)._parent.base_ring() 

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

self.__class__.__init__(self, parent, x, check=True, is_gen=False, construct=construct) 

elif isinstance(x, Polynomial): 

k = (<Polynomial_template>self)._parent.base_ring() 

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

Polynomial_template.__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() 

self.__class__.__init__(self, parent, x, check=check, is_gen=is_gen, construct=construct) 

else: 

x = parent.base_ring()(x) 

self.__class__.__init__(self, parent, x, check=check, is_gen=is_gen, construct=construct) 

  

def get_cparent(self): 

return <long> self._cparent 

  

def __reduce__(self): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

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

True 

""" 

return make_element, ((<Polynomial_template>self)._parent, (self.list(), False, self.is_gen())) 

  

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

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: x.list() 

[0, 1] 

sage: list(x) 

[0, 1] 

""" 

cdef Py_ssize_t i 

return [self[i] for i in range(celement_len(&self.x, (<Polynomial_template>self)._cparent))] 

  

def __dealloc__(self): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: del x 

  

TESTS: 

  

The following has been a problem in a preliminary version of 

:trac:`12313`:: 

  

sage: K.<z> = GF(4) 

sage: P.<x> = K[] 

sage: del P 

sage: del x 

sage: import gc 

sage: _ = gc.collect() 

""" 

celement_destruct(&self.x, (<Polynomial_template>self)._cparent) 

  

cpdef _add_(self, right): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: x + 1 

x + 1 

""" 

cdef type T = type(self) 

cdef Polynomial_template r = <Polynomial_template>T.__new__(T) 

  

celement_construct(&r.x, (<Polynomial_template>self)._cparent) 

r._parent = (<Polynomial_template>self)._parent 

r._cparent = (<Polynomial_template>self)._cparent 

celement_add(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x, (<Polynomial_template>self)._cparent) 

#assert(r._parent(pari(self) + pari(right)) == r) 

return r 

  

cpdef _sub_(self, right): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: x - 1 

x + 1 

""" 

cdef type T = type(self) 

cdef Polynomial_template r = <Polynomial_template>T.__new__(T) 

celement_construct(&r.x, (<Polynomial_template>self)._cparent) 

r._parent = (<Polynomial_template>self)._parent 

r._cparent = (<Polynomial_template>self)._cparent 

celement_sub(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x, (<Polynomial_template>self)._cparent) 

#assert(r._parent(pari(self) - pari(right)) == r) 

return r 

  

def __neg__(self): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: -x 

x 

""" 

cdef type T = type(self) 

cdef Polynomial_template r = <Polynomial_template>T.__new__(T) 

celement_construct(&r.x, (<Polynomial_template>self)._cparent) 

r._parent = (<Polynomial_template>self)._parent 

r._cparent = (<Polynomial_template>self)._cparent 

celement_neg(&r.x, &self.x, (<Polynomial_template>self)._cparent) 

#assert(r._parent(-pari(self)) == r) 

return r 

  

cpdef _lmul_(self, Element left): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: t = x^2 + x + 1 

sage: 0*t 

0 

sage: 1*t 

x^2 + x + 1 

  

sage: R.<y> = GF(5)[] 

sage: u = y^2 + y + 1 

sage: 3*u 

3*y^2 + 3*y + 3 

sage: 5*u 

0 

sage: (2^81)*u 

2*y^2 + 2*y + 2 

sage: (-2^81)*u 

3*y^2 + 3*y + 3 

  

:: 

  

sage: P.<x> = GF(2)[] 

sage: t = x^2 + x + 1 

sage: t*0 

0 

sage: t*1 

x^2 + x + 1 

  

sage: R.<y> = GF(5)[] 

sage: u = y^2 + y + 1 

sage: u*3 

3*y^2 + 3*y + 3 

sage: u*5 

0 

""" 

cdef type T = type(self) 

cdef Polynomial_template r = <Polynomial_template>T.__new__(T) 

celement_construct(&r.x, (<Polynomial_template>self)._cparent) 

r._parent = (<Polynomial_template>self)._parent 

r._cparent = (<Polynomial_template>self)._cparent 

celement_mul_scalar(&r.x, &(<Polynomial_template>self).x, left, (<Polynomial_template>self)._cparent) 

return r 

  

cpdef _mul_(self, right): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: x*(x+1) 

x^2 + x 

""" 

cdef type T = type(self) 

cdef Polynomial_template r = <Polynomial_template>T.__new__(T) 

celement_construct(&r.x, (<Polynomial_template>self)._cparent) 

r._parent = (<Polynomial_template>self)._parent 

r._cparent = (<Polynomial_template>self)._cparent 

celement_mul(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x, (<Polynomial_template>self)._cparent) 

#assert(r._parent(pari(self) * pari(right)) == r) 

return r 

  

@coerce_binop 

def gcd(self, Polynomial_template other): 

""" 

Return the greatest common divisor of self and other. 

  

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: f = x*(x+1) 

sage: f.gcd(x+1) 

x + 1 

sage: f.gcd(x^2) 

x 

""" 

if(celement_is_zero(&self.x, (<Polynomial_template>self)._cparent)): 

return other 

if(celement_is_zero(&other.x, (<Polynomial_template>self)._cparent)): 

return self 

  

cdef type T = type(self) 

cdef Polynomial_template r = <Polynomial_template>T.__new__(T) 

celement_construct(&r.x, (<Polynomial_template>self)._cparent) 

r._parent = (<Polynomial_template>self)._parent 

r._cparent = (<Polynomial_template>self)._cparent 

celement_gcd(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>other).x, (<Polynomial_template>self)._cparent) 

#assert(r._parent(pari(self).gcd(pari(other))) == r) 

return r 

  

@coerce_binop 

def xgcd(self, Polynomial_template other): 

""" 

Computes extended gcd of self and other. 

  

EXAMPLES:: 

  

sage: P.<x> = GF(7)[] 

sage: f = x*(x+1) 

sage: f.xgcd(x+1) 

(x + 1, 0, 1) 

sage: f.xgcd(x^2) 

(x, 1, 6) 

""" 

if(celement_is_zero(&self.x, (<Polynomial_template>self)._cparent)): 

return other, self._parent(0), self._parent(1) 

if(celement_is_zero(&other.x, (<Polynomial_template>self)._cparent)): 

return self, self._parent(1), self._parent(0) 

  

cdef type T = type(self) 

cdef Polynomial_template r = <Polynomial_template>T.__new__(T) 

celement_construct(&r.x, (<Polynomial_template>self)._cparent) 

r._parent = (<Polynomial_template>self)._parent 

r._cparent = (<Polynomial_template>self)._cparent 

  

cdef Polynomial_template s = <Polynomial_template>T.__new__(T) 

celement_construct(&s.x, (<Polynomial_template>self)._cparent) 

s._parent = (<Polynomial_template>self)._parent 

s._cparent = (<Polynomial_template>self)._cparent 

  

cdef Polynomial_template t = <Polynomial_template>T.__new__(T) 

celement_construct(&t.x, (<Polynomial_template>self)._cparent) 

t._parent = (<Polynomial_template>self)._parent 

t._cparent = (<Polynomial_template>self)._cparent 

  

celement_xgcd(&r.x, &s.x, &t.x, &(<Polynomial_template>self).x, &(<Polynomial_template>other).x, (<Polynomial_template>self)._cparent) 

#rp, sp, tp = pari(self).xgcd(pari(other)) 

#assert(r._parent(rp) == r) 

#assert(s._parent(sp) == s) 

#assert(t._parent(tp) == t) 

return r,s,t 

  

cpdef _floordiv_(self, right): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: x//(x + 1) 

1 

sage: (x + 1)//x 

1 

sage: F = GF(47) 

sage: R.<x> = F[] 

sage: x // 1 

x 

sage: x // F(1) 

x 

sage: 1 // x 

0 

sage: parent(x // 1) 

Univariate Polynomial Ring in x over Finite Field of size 47 

sage: parent(1 // x) 

Univariate Polynomial Ring in x over Finite Field of size 47 

""" 

cdef Polynomial_template _right = <Polynomial_template>right 

  

if celement_is_zero(&_right.x, (<Polynomial_template>self)._cparent): 

raise ZeroDivisionError 

cdef type T = type(self) 

cdef Polynomial_template r = <Polynomial_template>T.__new__(T) 

celement_construct(&r.x, (<Polynomial_template>self)._cparent) 

r._parent = (<Polynomial_template>self)._parent 

r._cparent = (<Polynomial_template>self)._cparent 

#assert(r._parent(pari(self) // pari(right)) == r) 

celement_floordiv(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x, (<Polynomial_template>self)._cparent) 

return r 

  

cpdef _mod_(self, other): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: (x^2 + 1) % x^2 

1 

  

  

TESTS:: 

  

We test that :trac:`10578` is fixed:: 

  

sage: P.<x> = GF(2)[] 

sage: x % 1r 

0 

""" 

cdef Polynomial_template _other = <Polynomial_template>other 

  

if celement_is_zero(&_other.x, (<Polynomial_template>self)._cparent): 

raise ZeroDivisionError 

  

cdef type T = type(self) 

cdef Polynomial_template r = <Polynomial_template>T.__new__(T) 

celement_construct(&r.x, (<Polynomial_template>self)._cparent) 

r._parent = (<Polynomial_template>self)._parent 

r._cparent = (<Polynomial_template>self)._cparent 

celement_mod(&r.x, &(<Polynomial_template>self).x, &_other.x, (<Polynomial_template>self)._cparent) 

#assert(r._parent(pari(self) % pari(other)) == r) 

return r 

  

@coerce_binop 

def quo_rem(self, Polynomial_template right): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: f = x^2 + x + 1 

sage: f.quo_rem(x + 1) 

(x, 1) 

""" 

if celement_is_zero(&right.x, (<Polynomial_template>self)._cparent): 

raise ZeroDivisionError 

  

cdef type T = type(self) 

cdef Polynomial_template q = <Polynomial_template>T.__new__(T) 

celement_construct(&q.x, (<Polynomial_template>self)._cparent) 

q._parent = (<Polynomial_template>self)._parent 

q._cparent = (<Polynomial_template>self)._cparent 

  

cdef Polynomial_template r = <Polynomial_template>T.__new__(T) 

celement_construct(&r.x, (<Polynomial_template>self)._cparent) 

r._parent = (<Polynomial_template>self)._parent 

r._cparent = (<Polynomial_template>self)._cparent 

  

celement_quorem(&q.x, &r.x, &(<Polynomial_template>self).x, &right.x, (<Polynomial_template>self)._cparent) 

return q,r 

  

def __long__(self): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: int(x) 

Traceback (most recent call last): 

... 

ValueError: Cannot coerce polynomial with degree 1 to integer. 

  

sage: int(P(1)) 

1 

""" 

if celement_len(&self.x, (<Polynomial_template>self)._cparent) > 1: 

raise ValueError("Cannot coerce polynomial with degree %d to integer."%(self.degree())) 

return int(self[0]) 

  

def __nonzero__(self): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: bool(x), x.is_zero() 

(True, False) 

sage: bool(P(0)), P(0).is_zero() 

(False, True) 

""" 

return not celement_is_zero(&self.x, (<Polynomial_template>self)._cparent) 

  

cpdef _richcmp_(self, other, int op): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: x != 1 

True 

sage: x < 1 

False 

sage: x > 1 

True 

""" 

cdef int c 

c = celement_cmp(&self.x, &(<Polynomial_template>other).x, self._cparent) 

return rich_to_bool(op, c) 

  

def __hash__(self): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: {x:1} 

{x: 1} 

""" 

cdef long result = 0 # store it in a c-int and just let the overflowing additions wrap 

cdef long result_mon 

cdef long c_hash 

cdef long var_name_hash 

cdef int i 

for i from 0<= i <= self.degree(): 

if i == 1: 

# we delay the hashing until now to not waste it one a constant poly 

var_name_hash = hash(self.variable_name()) 

# I'm assuming (incorrectly) that hashes of zero indicate that the element is 0. 

# This assumption is not true, but I think it is true enough for the purposes and it 

# it allows us to write fast code that omits terms with 0 coefficients. This is 

# important if we want to maintain the '==' relationship with sparse polys. 

c_hash = hash(self[i]) 

if c_hash != 0: 

if i == 0: 

result += c_hash 

else: 

# Hash (self[i], generator, i) as a tuple according to the algorithm. 

result_mon = c_hash 

result_mon = (1000003 * result_mon) ^ var_name_hash 

result_mon = (1000003 * result_mon) ^ i 

result += result_mon 

if result == -1: 

return -2 

return result 

  

  

def __pow__(self, ee, modulus): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: P.<x> = GF(2)[] 

sage: x^1000 

x^1000 

sage: (x+1)^2 

x^2 + 1 

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

1/(x^2 + 1) 

sage: f = x^9 + x^7 + x^6 + x^5 + x^4 + x^2 + x 

sage: h = x^10 + x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + 1 

sage: (f^2) % h 

x^9 + x^8 + x^7 + x^5 + x^3 

sage: pow(f, 2, h) 

x^9 + x^8 + x^7 + x^5 + x^3 

""" 

if not isinstance(self, Polynomial_template): 

raise NotImplementedError("%s^%s not defined."%(ee,self)) 

cdef bint recip = 0, do_sig 

  

cdef long e 

try: 

e = ee 

except OverflowError: 

return Polynomial.__pow__(self, ee, modulus) 

if e != ee: 

raise TypeError("Only integral powers defined.") 

elif e < 0: 

recip = 1 # delay because powering frac field elements is slow 

e = -e 

if not self: 

if e == 0: 

raise ArithmeticError("0^0 is undefined.") 

cdef type T = type(self) 

cdef Polynomial_template r = <Polynomial_template>T.__new__(T) 

  

celement_construct(&r.x, (<Polynomial_template>self)._cparent) 

parent = (<Polynomial_template>self)._parent 

r._parent = parent 

r._cparent = (<Polynomial_template>self)._cparent 

  

if modulus is None: 

celement_pow(&r.x, &(<Polynomial_template>self).x, e, NULL, (<Polynomial_template>self)._cparent) 

else: 

if parent is not (<Polynomial_template>modulus)._parent and parent != (<Polynomial_template>modulus)._parent: 

modulus = parent._coerce_(modulus) 

celement_pow(&r.x, &(<Polynomial_template>self).x, e, &(<Polynomial_template>modulus).x, (<Polynomial_template>self)._cparent) 

  

#assert(r._parent(pari(self)**ee) == r) 

if recip: 

return ~r 

else: 

return r 

  

def __copy__(self): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: copy(x) is x 

False 

sage: copy(x) == x 

True 

""" 

cdef type T = type(self) 

cdef Polynomial_template r = <Polynomial_template>T.__new__(T) 

celement_construct(&r.x, (<Polynomial_template>self)._cparent) 

r._parent = (<Polynomial_template>self)._parent 

r._cparent = (<Polynomial_template>self)._cparent 

celement_set(&r.x, &self.x, (<Polynomial_template>self)._cparent) 

return r 

  

def is_gen(self): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: x.is_gen() 

True 

sage: (x+1).is_gen() 

False 

""" 

cdef celement *gen = celement_new((<Polynomial_template>self)._cparent) 

celement_gen(gen, 0, (<Polynomial_template>self)._cparent) 

cdef bint r = celement_equal(&self.x, gen, (<Polynomial_template>self)._cparent) 

celement_delete(gen, (<Polynomial_template>self)._cparent) 

return r 

  

def shift(self, int n): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: f = x^3 + x^2 + 1 

sage: f.shift(1) 

x^4 + x^3 + x 

sage: f.shift(-1) 

x^2 + x 

""" 

return element_shift(self, n) 

  

def __lshift__(self, int n): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: f = x^3 + x^2 + 1 

sage: f << 1 

x^4 + x^3 + x 

sage: f << -1 

x^2 + x 

""" 

return element_shift(self, n) 

  

def __rshift__(self, int n): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: x>>1 

1 

sage: (x^2 + x)>>1 

x + 1 

sage: (x^2 + x) >> -1 

x^3 + x^2 

""" 

return element_shift(self, -n) 

  

cpdef bint is_zero(self): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: x.is_zero() 

False 

""" 

return celement_is_zero(&self.x, (<Polynomial_template>self)._cparent) 

  

cpdef bint is_one(self): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: P(1).is_one() 

True 

""" 

return celement_is_one(&self.x, (<Polynomial_template>self)._cparent) 

  

def degree(self): 

""" 

EXAMPLES:: 

  

sage: P.<x> = GF(2)[] 

sage: x.degree() 

1 

sage: P(1).degree() 

0 

sage: P(0).degree() 

-1 

""" 

return Integer(celement_len(&self.x, (<Polynomial_template>self)._cparent)-1) 

  

cpdef Polynomial truncate(self, long n): 

r""" 

Returns this polynomial mod `x^n`. 

  

EXAMPLES:: 

  

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

sage: f = sum(x^n for n in range(10)); f 

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

sage: f.truncate(6) 

x^5 + x^4 + x^3 + x^2 + x + 1 

  

If the precision is higher than the degree of the polynomial then 

the polynomial itself is returned:: 

  

sage: f.truncate(10) is f 

True 

""" 

if n >= celement_len(&self.x, (<Polynomial_template>self)._cparent): 

return self 

  

cdef type T = type(self) 

cdef Polynomial_template r = <Polynomial_template>T.__new__(T) 

celement_construct(&r.x, (<Polynomial_template>self)._cparent) 

r._parent = (<Polynomial_template>self)._parent 

r._cparent = (<Polynomial_template>self)._cparent 

  

if n <= 0: 

return r 

  

cdef celement *gen = celement_new((<Polynomial_template>self)._cparent) 

celement_gen(gen, 0, (<Polynomial_template>self)._cparent) 

celement_pow(gen, gen, n, NULL, (<Polynomial_template>self)._cparent) 

  

celement_mod(&r.x, &self.x, gen, (<Polynomial_template>self)._cparent) 

celement_delete(gen, (<Polynomial_template>self)._cparent) 

return r 

  

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

r""" 

Return Singular representation of this polynomial 

  

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(GF(7)) 

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

sage: singular(f) 

3*x^2+2*x-2 

""" 

if not have_ring: 

self.parent()._singular_(singular).set_ring() #this is expensive 

return singular(self._singular_init_()) 

  

def _derivative(self, var=None): 

r""" 

Returns the formal derivative of self with respect to var. 

  

var must be either the generator of the polynomial ring to which 

this polynomial belongs, or None (either way the behaviour is the 

same). 

  

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

  

EXAMPLES:: 

  

sage: R.<x> = Integers(77)[] 

sage: f = x^4 - x - 1 

sage: f._derivative() 

4*x^3 + 76 

sage: f._derivative(None) 

4*x^3 + 76 

  

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 

""" 

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

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

  

P = self.parent() 

x = P.gen() 

res = P(0) 

for i,c in enumerate(self.list()[1:]): 

res += (i+1)*c*x**i 

return res