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

""" 

FLINT fmpz_poly class wrapper 

  

AUTHORS: 

  

- Robert Bradshaw (2007-09-15) Initial version. 

- William Stein (2007-10-02) update for new flint; add arithmetic and creation 

of coefficients of arbitrary size. 

""" 

  

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

# Copyright (C) 2007 Robert Bradshaw <robertwb@math.washington.edu> 

# 

# 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 cpython.sequence cimport * 

  

from cysignals.memory cimport sig_free 

  

from sage.arith.long cimport pyobject_to_long 

from sage.cpython.string cimport char_to_str, str_to_bytes 

from sage.structure.sage_object cimport SageObject 

from sage.rings.integer cimport Integer 

from sage.libs.flint.fmpz_poly cimport * 

  

cdef class Fmpz_poly(SageObject): 

  

def __cinit__(self): 

fmpz_poly_init(self.poly) 

  

def __init__(self, v): 

""" 

Construct a new fmpz_poly from a sequence, constant coefficient, 

or string (in the same format as it prints). 

  

EXAMPLES:: 

  

sage: from sage.libs.flint.fmpz_poly import Fmpz_poly 

sage: Fmpz_poly([1,2,3]) 

3 1 2 3 

sage: Fmpz_poly(5) 

1 5 

sage: Fmpz_poly(str(Fmpz_poly([3,5,7]))) 

3 3 5 7 

""" 

cdef Py_ssize_t i 

cdef long c 

cdef Integer w 

if isinstance(v, str): 

if not fmpz_poly_set_str(self.poly, str_to_bytes(v)): 

return 

else: 

raise ValueError("Unable to create Fmpz_poly from that string.") 

if not PySequence_Check(v): 

v = [v] 

try: 

fmpz_poly_set_coeff_si(self.poly, 0, 1) 

fmpz_poly_set_coeff_si(self.poly, 0, 0) 

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

#fmpz_poly_set_coeff_si(self.poly, i, v[i]) 

w = Integer(v[i]) 

fmpz_poly_set_coeff_mpz(self.poly, i, w.value) 

except OverflowError: 

raise ValueError("No fmpz_poly_set_coeff_mpz() method.") 

  

def __dealloc__(self): 

fmpz_poly_clear(self.poly) 

  

def __setitem__(self, i, value): 

""" 

Set the $i$-th item of self, which is the coefficient of the $x^i$ term. 

  

EXAMPLES:: 

  

sage: from sage.libs.flint.fmpz_poly import Fmpz_poly 

sage: f = Fmpz_poly(range(10)) 

sage: f[7] = 100; f 

10 0 1 2 3 4 5 6 100 8 9 

sage: f[2] = 10**100000 

sage: f[2] == 10**100000 

True 

""" 

if isinstance(value, Integer) : 

fmpz_poly_set_coeff_mpz(self.poly, i, (<Integer>value).value) 

else : 

fmpz_poly_set_coeff_si(self.poly, i, value) 

  

def __getitem__(self, i): 

""" 

Return the $i$-th item of self, which is the coefficient of the $x^i$ term. 

  

EXAMPLES:: 

  

sage: from sage.libs.flint.fmpz_poly import Fmpz_poly 

sage: f = Fmpz_poly(range(100)) 

sage: f[13] 

13 

sage: f[200] 

0 

""" 

cdef Integer res = Integer.__new__(Integer) 

fmpz_poly_get_coeff_mpz(res.value, self.poly, i) 

return res 

  

def __repr__(self): 

""" 

Print self according to the native FLINT format. 

  

EXAMPLES:: 

  

sage: from sage.libs.flint.fmpz_poly import Fmpz_poly 

sage: f = Fmpz_poly([0,1]); f^7 

8 0 0 0 0 0 0 0 1 

""" 

cdef char* ss = fmpz_poly_get_str(self.poly) 

cdef object s = char_to_str(ss) 

sig_free(ss) 

return s 

  

def degree(self): 

""" 

The degree of self. 

  

EXAMPLES:: 

  

sage: from sage.libs.flint.fmpz_poly import Fmpz_poly 

sage: f = Fmpz_poly([1,2,3]); f 

3 1 2 3 

sage: f.degree() 

2 

sage: Fmpz_poly(range(1000)).degree() 

999 

sage: Fmpz_poly([2,0]).degree() 

0 

""" 

return fmpz_poly_degree(self.poly) 

  

def list(self): 

""" 

Return self as a list of coefficients, lowest terms first. 

  

EXAMPLES:: 

  

sage: from sage.libs.flint.fmpz_poly import Fmpz_poly 

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

sage: f.list() 

[2, 1, 0, -1] 

""" 

return [self[i] for i in xrange(self.degree() + 1)] 

  

def __add__(left, right): 

""" 

Add together two Flint polynomials. 

  

EXAMPLES:: 

  

sage: from sage.libs.flint.fmpz_poly import Fmpz_poly 

sage: Fmpz_poly([1,2,3]) + Fmpz_poly(range(6)) 

6 1 3 5 3 4 5 

""" 

if not isinstance(left, Fmpz_poly) or not isinstance(right, Fmpz_poly): 

raise TypeError 

cdef Fmpz_poly res = <Fmpz_poly>Fmpz_poly.__new__(Fmpz_poly) 

fmpz_poly_add(res.poly, (<Fmpz_poly>left).poly, (<Fmpz_poly>right).poly) 

return res 

  

def __sub__(left, right): 

""" 

Subtract two Flint polynomials. 

  

EXAMPLES:: 

  

sage: from sage.libs.flint.fmpz_poly import Fmpz_poly 

sage: Fmpz_poly([10,2,3]) - Fmpz_poly([4,-2,1]) 

3 6 4 2 

""" 

if not isinstance(left, Fmpz_poly) or not isinstance(right, Fmpz_poly): 

raise TypeError 

cdef Fmpz_poly res = <Fmpz_poly>Fmpz_poly.__new__(Fmpz_poly) 

fmpz_poly_sub(res.poly, (<Fmpz_poly>left).poly, (<Fmpz_poly>right).poly) 

return res 

  

def __neg__(self): 

""" 

Return the negative of self. 

  

EXAMPLES:: 

  

sage: from sage.libs.flint.fmpz_poly import Fmpz_poly 

sage: -Fmpz_poly([2,10,2,3,18,-5]) 

6 -2 -10 -2 -3 -18 5 

""" 

cdef Fmpz_poly res = <Fmpz_poly>Fmpz_poly.__new__(Fmpz_poly) 

fmpz_poly_neg(res.poly, self.poly) 

return res 

  

def __mul__(left, right): 

""" 

Return the product of left and right. 

  

EXAMPLES:: 

  

sage: from sage.libs.flint.fmpz_poly import Fmpz_poly 

sage: f = Fmpz_poly([0,1]); g = Fmpz_poly([2,3,4]) 

sage: f*g 

4 0 2 3 4 

sage: f = Fmpz_poly([1,0,-1]); g = Fmpz_poly([2,3,4]) 

sage: f*g 

5 2 3 2 -3 -4 

  

Scalar multiplication 

sage: f * 3 

3 3 0 -3 

sage: f * 5r 

3 5 0 -5 

""" 

cdef Fmpz_poly res = <Fmpz_poly>Fmpz_poly.__new__(Fmpz_poly) 

if not isinstance(left, Fmpz_poly) or not isinstance(right, Fmpz_poly): 

if isinstance(left, int) : 

fmpz_poly_scalar_mul_si(res.poly, (<Fmpz_poly>right).poly, left) 

elif isinstance(left, Integer) : 

fmpz_poly_scalar_mul_mpz(res.poly, (<Fmpz_poly>right).poly, (<Integer>left).value) 

elif isinstance(right, int) : 

fmpz_poly_scalar_mul_si(res.poly, (<Fmpz_poly>left).poly, right) 

elif isinstance(right, Integer) : 

fmpz_poly_scalar_mul_mpz(res.poly, (<Fmpz_poly>left).poly, (<Integer>right).value) 

else: 

raise TypeError 

else: 

fmpz_poly_mul(res.poly, (<Fmpz_poly>left).poly, (<Fmpz_poly>right).poly) 

return res 

  

def __pow__(self, n, dummy): 

""" 

Return self raised to the power of n. 

  

EXAMPLES:: 

  

sage: from sage.libs.flint.fmpz_poly import Fmpz_poly 

sage: f = Fmpz_poly([1,1]) 

sage: f**6 

7 1 6 15 20 15 6 1 

sage: f = Fmpz_poly([2]) 

sage: f^150 

1 1427247692705959881058285969449495136382746624 

sage: 2^150 

1427247692705959881058285969449495136382746624 

  

sage: f**(3/2) 

Traceback (most recent call last): 

... 

TypeError: rational is not an integer 

""" 

cdef long nn = pyobject_to_long(n) 

if not isinstance(self, Fmpz_poly): 

raise TypeError 

cdef Fmpz_poly res = <Fmpz_poly>Fmpz_poly.__new__(Fmpz_poly) 

fmpz_poly_pow(res.poly, (<Fmpz_poly>self).poly, nn) 

return res 

  

def pow_truncate(self, exp, n): 

""" 

Return self raised to the power of exp mod x^n. 

  

EXAMPLES:: 

  

sage: from sage.libs.flint.fmpz_poly import Fmpz_poly 

sage: f = Fmpz_poly([1,2]) 

sage: f.pow_truncate(10,3) 

3 1 20 180 

sage: f.pow_truncate(1000,3) 

3 1 2000 1998000 

""" 

if exp < 0: 

raise ValueError("Exponent must be at least 0") 

if n < 0: 

raise ValueError("Exponent must be at least 0") 

cdef long exp_c = exp, nn = n 

cdef Fmpz_poly res = <Fmpz_poly>Fmpz_poly.__new__(Fmpz_poly) 

fmpz_poly_pow_trunc(res.poly, (<Fmpz_poly>self).poly, exp_c, nn) 

return res 

  

def __floordiv__(left, right): 

""" 

Return left // right, truncated. 

  

EXAMPLES:: 

  

sage: from sage.libs.flint.fmpz_poly import Fmpz_poly 

sage: f = Fmpz_poly([3,4,5]) 

sage: g = f^5; g 

11 243 1620 6345 16560 32190 47224 53650 46000 29375 12500 3125 

sage: g // f 

9 81 432 1404 2928 4486 4880 3900 2000 625 

sage: f^4 

9 81 432 1404 2928 4486 4880 3900 2000 625 

""" 

if not isinstance(left, Fmpz_poly) or not isinstance(right, Fmpz_poly): 

raise TypeError 

cdef Fmpz_poly res = <Fmpz_poly>Fmpz_poly.__new__(Fmpz_poly) 

fmpz_poly_div(res.poly, (<Fmpz_poly>left).poly, (<Fmpz_poly>right).poly) 

return res 

  

def div_rem(self, Fmpz_poly other): 

""" 

Return self / other, self, % other. 

  

EXAMPLES:: 

  

sage: from sage.libs.flint.fmpz_poly import Fmpz_poly 

sage: f = Fmpz_poly([1,3,4,5]) 

sage: g = f^23 

sage: g.div_rem(f)[1] 

0 

sage: g.div_rem(f)[0] - f^22 

0 

sage: f = Fmpz_poly([1..10]) 

sage: g = Fmpz_poly([1,3,5]) 

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

sage: q*f+r 

17 1 2 3 4 4 4 10 11 17 18 22 26 30 23 26 18 20 

sage: g 

3 1 3 5 

sage: q*g+r 

10 1 2 3 4 5 6 7 8 9 10 

""" 

cdef Fmpz_poly Q = <Fmpz_poly>Fmpz_poly.__new__(Fmpz_poly) 

cdef Fmpz_poly R = <Fmpz_poly>Fmpz_poly.__new__(Fmpz_poly) 

fmpz_poly_divrem(Q.poly, R.poly, self.poly, other.poly) 

return Q, R 

  

def left_shift(self, unsigned long n) : 

""" 

Left shift self by n. 

  

EXAMPLES:: 

  

sage: from sage.libs.flint.fmpz_poly import Fmpz_poly 

sage: f = Fmpz_poly([1,2]) 

sage: f.left_shift(1).list() == [0,1,2] 

True 

""" 

cdef Fmpz_poly res = <Fmpz_poly>Fmpz_poly.__new__(Fmpz_poly) 

  

fmpz_poly_shift_left(res.poly, self.poly, n) 

  

return res 

  

def right_shift(self, unsigned long n) : 

""" 

Right shift self by n. 

  

EXAMPLES:: 

  

sage: from sage.libs.flint.fmpz_poly import Fmpz_poly 

sage: f = Fmpz_poly([1,2]) 

sage: f.right_shift(1).list() == [2] 

True 

""" 

cdef Fmpz_poly res = <Fmpz_poly>Fmpz_poly.__new__(Fmpz_poly) 

  

fmpz_poly_shift_right(res.poly, self.poly, n) 

  

return res 

  

def pseudo_div(self, Fmpz_poly other): 

cdef ulong d 

cdef Fmpz_poly Q = <Fmpz_poly>Fmpz_poly.__new__(Fmpz_poly) 

fmpz_poly_pseudo_div(Q.poly, &d, self.poly, other.poly) 

return Q, d 

  

def pseudo_div_rem(self, Fmpz_poly other): 

cdef ulong d 

cdef Fmpz_poly Q = <Fmpz_poly>Fmpz_poly.__new__(Fmpz_poly) 

cdef Fmpz_poly R = <Fmpz_poly>Fmpz_poly.__new__(Fmpz_poly) 

fmpz_poly_pseudo_divrem(Q.poly, R.poly, &d, self.poly, other.poly) 

return Q, R, d 

  

def derivative(self) : 

""" 

Return the derivative of self. 

  

EXAMPLES:: 

  

sage: from sage.libs.flint.fmpz_poly import Fmpz_poly 

sage: f = Fmpz_poly([1,2,6]) 

sage: f.derivative().list() == [2, 12] 

True 

""" 

cdef Fmpz_poly res = <Fmpz_poly>Fmpz_poly.__new__(Fmpz_poly) 

  

fmpz_poly_derivative(res.poly, self.poly) 

  

return res 

  

def __copy__(self): 

cdef Fmpz_poly res = <Fmpz_poly>Fmpz_poly.__new__(Fmpz_poly) 

fmpz_poly_set(res.poly, self.poly) 

return res 

  

def truncate(self, n): 

""" 

Return the truncation of self at degree n. 

  

EXAMPLES:: 

  

sage: from sage.libs.flint.fmpz_poly import Fmpz_poly 

sage: f = Fmpz_poly([1,1]) 

sage: g = f**10; g 

11 1 10 45 120 210 252 210 120 45 10 1 

sage: g.truncate(5) 

5 1 10 45 120 210 

""" 

cdef Fmpz_poly g = self.__copy__() 

fmpz_poly_truncate(g.poly, n) 

return g 

  

def _unsafe_mutate_truncate(self, n): 

""" 

Return the truncation of self at degree n. 

  

Don't do this unless you know there are no other references to 

this polynomial!!!!! 

  

EXAMPLES:: 

  

sage: from sage.libs.flint.fmpz_poly import Fmpz_poly 

sage: f = Fmpz_poly([1,1]) 

sage: g = f**10; g 

11 1 10 45 120 210 252 210 120 45 10 1 

sage: g._unsafe_mutate_truncate(5); g 

5 1 10 45 120 210 

""" 

cdef long nn = n 

fmpz_poly_truncate(self.poly, nn) # mutating! 

  

  

def _sage_(self, var='x'): 

""" 

Return self as an element of the sage ZZ[var]. 

  

EXAMPLES:: 

  

sage: from sage.libs.flint.fmpz_poly import Fmpz_poly 

sage: f = Fmpz_poly([1,1]) 

sage: f._sage_('t') 

t + 1 

sage: Fmpz_poly([-1,0,0,1])._sage_() 

x^3 - 1 

""" 

from sage.rings.all import ZZ 

return ZZ[var](self.list())