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

"""Square-free decomposition algorithms and related tools. """ 

 

from __future__ import print_function, division 

 

from sympy.polys.densebasic import ( 

dup_strip, 

dup_LC, dmp_ground_LC, 

dmp_zero_p, 

dmp_ground, 

dup_degree, dmp_degree, 

dmp_raise, dmp_inject, 

dup_convert) 

 

from sympy.polys.densearith import ( 

dup_neg, dmp_neg, 

dup_sub, dmp_sub, 

dup_mul, 

dup_quo, dmp_quo, 

dup_mul_ground, dmp_mul_ground) 

 

from sympy.polys.densetools import ( 

dup_diff, dmp_diff, 

dup_shift, dmp_compose, 

dup_monic, dmp_ground_monic, 

dup_primitive, dmp_ground_primitive) 

 

from sympy.polys.euclidtools import ( 

dup_inner_gcd, dmp_inner_gcd, 

dup_gcd, dmp_gcd, 

dmp_resultant) 

 

from sympy.polys.galoistools import ( 

gf_sqf_list, gf_sqf_part) 

 

from sympy.polys.polyerrors import ( 

MultivariatePolynomialError, 

DomainError) 

 

def dup_sqf_p(f, K): 

""" 

Return ``True`` if ``f`` is a square-free polynomial in ``K[x]``. 

 

Examples 

======== 

 

>>> from sympy.polys import ring, ZZ 

>>> R, x = ring("x", ZZ) 

 

>>> R.dup_sqf_p(x**2 - 2*x + 1) 

False 

>>> R.dup_sqf_p(x**2 - 1) 

True 

 

""" 

if not f: 

return True 

else: 

return not dup_degree(dup_gcd(f, dup_diff(f, 1, K), K)) 

 

 

def dmp_sqf_p(f, u, K): 

""" 

Return ``True`` if ``f`` is a square-free polynomial in ``K[X]``. 

 

Examples 

======== 

 

>>> from sympy.polys import ring, ZZ 

>>> R, x,y = ring("x,y", ZZ) 

 

>>> R.dmp_sqf_p(x**2 + 2*x*y + y**2) 

False 

>>> R.dmp_sqf_p(x**2 + y**2) 

True 

 

""" 

if dmp_zero_p(f, u): 

return True 

else: 

return not dmp_degree(dmp_gcd(f, dmp_diff(f, 1, u, K), u, K), u) 

 

 

def dup_sqf_norm(f, K): 

""" 

Square-free norm of ``f`` in ``K[x]``, useful over algebraic domains. 

 

Returns ``s``, ``f``, ``r``, such that ``g(x) = f(x-sa)`` and ``r(x) = Norm(g(x))`` 

is a square-free polynomial over K, where ``a`` is the algebraic extension of ``K``. 

 

Examples 

======== 

 

>>> from sympy.polys import ring, QQ 

>>> from sympy import sqrt 

 

>>> K = QQ.algebraic_field(sqrt(3)) 

>>> R, x = ring("x", K) 

>>> _, X = ring("x", QQ) 

 

>>> s, f, r = R.dup_sqf_norm(x**2 - 2) 

 

>>> s == 1 

True 

>>> f == x**2 + K([QQ(-2), QQ(0)])*x + 1 

True 

>>> r == X**4 - 10*X**2 + 1 

True 

 

""" 

if not K.is_Algebraic: 

raise DomainError("ground domain must be algebraic") 

 

s, g = 0, dmp_raise(K.mod.rep, 1, 0, K.dom) 

 

while True: 

h, _ = dmp_inject(f, 0, K, front=True) 

r = dmp_resultant(g, h, 1, K.dom) 

 

if dup_sqf_p(r, K.dom): 

break 

else: 

f, s = dup_shift(f, -K.unit, K), s + 1 

 

return s, f, r 

 

 

def dmp_sqf_norm(f, u, K): 

""" 

Square-free norm of ``f`` in ``K[X]``, useful over algebraic domains. 

 

Returns ``s``, ``f``, ``r``, such that ``g(x) = f(x-sa)`` and ``r(x) = Norm(g(x))`` 

is a square-free polynomial over K, where ``a`` is the algebraic extension of ``K``. 

 

Examples 

======== 

 

>>> from sympy.polys import ring, QQ 

>>> from sympy import I 

 

>>> K = QQ.algebraic_field(I) 

>>> R, x, y = ring("x,y", K) 

>>> _, X, Y = ring("x,y", QQ) 

 

>>> s, f, r = R.dmp_sqf_norm(x*y + y**2) 

 

>>> s == 1 

True 

>>> f == x*y + y**2 + K([QQ(-1), QQ(0)])*y 

True 

>>> r == X**2*Y**2 + 2*X*Y**3 + Y**4 + Y**2 

True 

 

""" 

if not u: 

return dup_sqf_norm(f, K) 

 

if not K.is_Algebraic: 

raise DomainError("ground domain must be algebraic") 

 

g = dmp_raise(K.mod.rep, u + 1, 0, K.dom) 

F = dmp_raise([K.one, -K.unit], u, 0, K) 

 

s = 0 

 

while True: 

h, _ = dmp_inject(f, u, K, front=True) 

r = dmp_resultant(g, h, u + 1, K.dom) 

 

if dmp_sqf_p(r, u, K.dom): 

break 

else: 

f, s = dmp_compose(f, F, u, K), s + 1 

 

return s, f, r 

 

 

def dup_gf_sqf_part(f, K): 

"""Compute square-free part of ``f`` in ``GF(p)[x]``. """ 

f = dup_convert(f, K, K.dom) 

g = gf_sqf_part(f, K.mod, K.dom) 

return dup_convert(g, K.dom, K) 

 

 

def dmp_gf_sqf_part(f, K): 

"""Compute square-free part of ``f`` in ``GF(p)[X]``. """ 

raise NotImplementedError('multivariate polynomials over finite fields') 

 

 

def dup_sqf_part(f, K): 

""" 

Returns square-free part of a polynomial in ``K[x]``. 

 

Examples 

======== 

 

>>> from sympy.polys import ring, ZZ 

>>> R, x = ring("x", ZZ) 

 

>>> R.dup_sqf_part(x**3 - 3*x - 2) 

x**2 - x - 2 

 

""" 

if K.is_FiniteField: 

return dup_gf_sqf_part(f, K) 

 

if not f: 

return f 

 

if K.is_negative(dup_LC(f, K)): 

f = dup_neg(f, K) 

 

gcd = dup_gcd(f, dup_diff(f, 1, K), K) 

sqf = dup_quo(f, gcd, K) 

 

if K.has_Field: 

return dup_monic(sqf, K) 

else: 

return dup_primitive(sqf, K)[1] 

 

 

def dmp_sqf_part(f, u, K): 

""" 

Returns square-free part of a polynomial in ``K[X]``. 

 

Examples 

======== 

 

>>> from sympy.polys import ring, ZZ 

>>> R, x,y = ring("x,y", ZZ) 

 

>>> R.dmp_sqf_part(x**3 + 2*x**2*y + x*y**2) 

x**2 + x*y 

 

""" 

if not u: 

return dup_sqf_part(f, K) 

 

if K.is_FiniteField: 

return dmp_gf_sqf_part(f, u, K) 

 

if dmp_zero_p(f, u): 

return f 

 

if K.is_negative(dmp_ground_LC(f, u, K)): 

f = dmp_neg(f, u, K) 

 

gcd = dmp_gcd(f, dmp_diff(f, 1, u, K), u, K) 

sqf = dmp_quo(f, gcd, u, K) 

 

if K.has_Field: 

return dmp_ground_monic(sqf, u, K) 

else: 

return dmp_ground_primitive(sqf, u, K)[1] 

 

 

def dup_gf_sqf_list(f, K, all=False): 

"""Compute square-free decomposition of ``f`` in ``GF(p)[x]``. """ 

f = dup_convert(f, K, K.dom) 

 

coeff, factors = gf_sqf_list(f, K.mod, K.dom, all=all) 

 

for i, (f, k) in enumerate(factors): 

factors[i] = (dup_convert(f, K.dom, K), k) 

 

return K.convert(coeff, K.dom), factors 

 

 

def dmp_gf_sqf_list(f, u, K, all=False): 

"""Compute square-free decomposition of ``f`` in ``GF(p)[X]``. """ 

raise NotImplementedError('multivariate polynomials over finite fields') 

 

 

def dup_sqf_list(f, K, all=False): 

""" 

Return square-free decomposition of a polynomial in ``K[x]``. 

 

Examples 

======== 

 

>>> from sympy.polys import ring, ZZ 

>>> R, x = ring("x", ZZ) 

 

>>> f = 2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16 

 

>>> R.dup_sqf_list(f) 

(2, [(x + 1, 2), (x + 2, 3)]) 

>>> R.dup_sqf_list(f, all=True) 

(2, [(1, 1), (x + 1, 2), (x + 2, 3)]) 

 

""" 

if K.is_FiniteField: 

return dup_gf_sqf_list(f, K, all=all) 

 

if K.has_Field: 

coeff = dup_LC(f, K) 

f = dup_monic(f, K) 

else: 

coeff, f = dup_primitive(f, K) 

 

if K.is_negative(dup_LC(f, K)): 

f = dup_neg(f, K) 

coeff = -coeff 

 

if dup_degree(f) <= 0: 

return coeff, [] 

 

result, i = [], 1 

 

h = dup_diff(f, 1, K) 

g, p, q = dup_inner_gcd(f, h, K) 

 

while True: 

d = dup_diff(p, 1, K) 

h = dup_sub(q, d, K) 

 

if not h: 

result.append((p, i)) 

break 

 

g, p, q = dup_inner_gcd(p, h, K) 

 

if all or dup_degree(g) > 0: 

result.append((g, i)) 

 

i += 1 

 

return coeff, result 

 

 

def dup_sqf_list_include(f, K, all=False): 

""" 

Return square-free decomposition of a polynomial in ``K[x]``. 

 

Examples 

======== 

 

>>> from sympy.polys import ring, ZZ 

>>> R, x = ring("x", ZZ) 

 

>>> f = 2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16 

 

>>> R.dup_sqf_list_include(f) 

[(2, 1), (x + 1, 2), (x + 2, 3)] 

>>> R.dup_sqf_list_include(f, all=True) 

[(2, 1), (x + 1, 2), (x + 2, 3)] 

 

""" 

coeff, factors = dup_sqf_list(f, K, all=all) 

 

if factors and factors[0][1] == 1: 

g = dup_mul_ground(factors[0][0], coeff, K) 

return [(g, 1)] + factors[1:] 

else: 

g = dup_strip([coeff]) 

return [(g, 1)] + factors 

 

 

def dmp_sqf_list(f, u, K, all=False): 

""" 

Return square-free decomposition of a polynomial in ``K[X]``. 

 

Examples 

======== 

 

>>> from sympy.polys import ring, ZZ 

>>> R, x,y = ring("x,y", ZZ) 

 

>>> f = x**5 + 2*x**4*y + x**3*y**2 

 

>>> R.dmp_sqf_list(f) 

(1, [(x + y, 2), (x, 3)]) 

>>> R.dmp_sqf_list(f, all=True) 

(1, [(1, 1), (x + y, 2), (x, 3)]) 

 

""" 

if not u: 

return dup_sqf_list(f, K, all=all) 

 

if K.is_FiniteField: 

return dmp_gf_sqf_list(f, u, K, all=all) 

 

if K.has_Field: 

coeff = dmp_ground_LC(f, u, K) 

f = dmp_ground_monic(f, u, K) 

else: 

coeff, f = dmp_ground_primitive(f, u, K) 

 

if K.is_negative(dmp_ground_LC(f, u, K)): 

f = dmp_neg(f, u, K) 

coeff = -coeff 

 

if dmp_degree(f, u) <= 0: 

return coeff, [] 

 

result, i = [], 1 

 

h = dmp_diff(f, 1, u, K) 

g, p, q = dmp_inner_gcd(f, h, u, K) 

 

while True: 

d = dmp_diff(p, 1, u, K) 

h = dmp_sub(q, d, u, K) 

 

if dmp_zero_p(h, u): 

result.append((p, i)) 

break 

 

g, p, q = dmp_inner_gcd(p, h, u, K) 

 

if all or dmp_degree(g, u) > 0: 

result.append((g, i)) 

 

i += 1 

 

return coeff, result 

 

 

def dmp_sqf_list_include(f, u, K, all=False): 

""" 

Return square-free decomposition of a polynomial in ``K[x]``. 

 

Examples 

======== 

 

>>> from sympy.polys import ring, ZZ 

>>> R, x,y = ring("x,y", ZZ) 

 

>>> f = x**5 + 2*x**4*y + x**3*y**2 

 

>>> R.dmp_sqf_list_include(f) 

[(1, 1), (x + y, 2), (x, 3)] 

>>> R.dmp_sqf_list_include(f, all=True) 

[(1, 1), (x + y, 2), (x, 3)] 

 

""" 

if not u: 

return dup_sqf_list_include(f, K, all=all) 

 

coeff, factors = dmp_sqf_list(f, u, K, all=all) 

 

if factors and factors[0][1] == 1: 

g = dmp_mul_ground(factors[0][0], coeff, u, K) 

return [(g, 1)] + factors[1:] 

else: 

g = dmp_ground(coeff, u) 

return [(g, 1)] + factors 

 

 

def dup_gff_list(f, K): 

""" 

Compute greatest factorial factorization of ``f`` in ``K[x]``. 

 

Examples 

======== 

 

>>> from sympy.polys import ring, ZZ 

>>> R, x = ring("x", ZZ) 

 

>>> R.dup_gff_list(x**5 + 2*x**4 - x**3 - 2*x**2) 

[(x, 1), (x + 2, 4)] 

 

""" 

if not f: 

raise ValueError("greatest factorial factorization doesn't exist for a zero polynomial") 

 

f = dup_monic(f, K) 

 

if not dup_degree(f): 

return [] 

else: 

g = dup_gcd(f, dup_shift(f, K.one, K), K) 

H = dup_gff_list(g, K) 

 

for i, (h, k) in enumerate(H): 

g = dup_mul(g, dup_shift(h, -K(k), K), K) 

H[i] = (h, k + 1) 

 

f = dup_quo(f, g, K) 

 

if not dup_degree(f): 

return H 

else: 

return [(f, 1)] + H 

 

 

def dmp_gff_list(f, u, K): 

""" 

Compute greatest factorial factorization of ``f`` in ``K[X]``. 

 

Examples 

======== 

 

>>> from sympy.polys import ring, ZZ 

>>> R, x,y = ring("x,y", ZZ) 

 

""" 

if not u: 

return dup_gff_list(f, K) 

else: 

raise MultivariatePolynomialError(f)