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

""" 

Symmetric Reduction of Infinite Polynomials 

  

:class:`~sage.rings.polynomial.symmetric_reduction.SymmetricReductionStrategy` 

provides a framework for efficient symmetric reduction of Infinite 

Polynomials, see :mod:`~sage.rings.polynomial.infinite_polynomial_element`. 

  

AUTHORS: 

  

- Simon King <simon.king@nuigalway.ie> 

  

THEORY: 

  

According to M. Aschenbrenner and C. Hillar [AB2007]_, Symmetric 

Reduction of an element `p` of an Infinite Polynomial Ring `X` by some 

other element `q` means the following: 

  

1. Let `M` and `N` be the leading terms of `p` and `q`. 

2. Test whether there is a permutation `P` that does not 

diminish the variable indices occurring in `N` 

and preserves their order, so that there is some term 

`T\in X` with `T N^P = M`. If there is no such permutation, 

return `p`. 

3. Replace `p` by `p-T q^P` and continue with step 1. 

  

  

When reducing one polynomial `p` with respect to a list `L` of other 

polynomials, there usually is a choice of order on which the 

efficiency crucially depends. Also it helps to modify the polynomials 

on the list in order to simplify the basic reduction steps. 

  

The preparation of `L` may be expensive. Hence, if the same list is 

used many times then it is reasonable to perform the preparation only 

once. This is the background of 

:class:`~sage.rings.polynomial.symmetric_reduction.SymmetricReductionStrategy`. 

  

Our current strategy is to keep the number of terms in the polynomials 

as small as possible. For this, we sort `L` by increasing number of 

terms. If several elements of `L` allow for a reduction of `p`, we 

choose the one with the smallest number of terms. Later on, it should 

be possible to implement further strategies for choice. 

  

When adding a new polynomial `q` to `L`, we first reduce `q` with 

respect to `L`. Then, we test heuristically whether it is possible to 

reduce the number of terms of the elements of `L` by reduction modulo 

`q`. That way, we see best chances to keep the number of terms in 

intermediate reduction steps relatively small. 

  

EXAMPLES: 

  

First, we create an infinite polynomial ring and one of its elements:: 

  

sage: X.<x,y> = InfinitePolynomialRing(QQ) 

sage: p = y[1]*y[3]+y[1]^2*x[3] 

  

We want to symmetrically reduce it by another polynomial. So, we put 

this other polynomial into a list and create a Symmetric Reduction 

Strategy object:: 

  

sage: from sage.rings.polynomial.symmetric_reduction import SymmetricReductionStrategy 

sage: S = SymmetricReductionStrategy(X, [y[2]^2*x[1]]) 

sage: S 

Symmetric Reduction Strategy in Infinite polynomial ring in x, y over Rational Field, modulo 

x_1*y_2^2 

sage: S.reduce(p) 

x_3*y_1^2 + y_3*y_1 

  

The preceding is correct, since any permutation that turns 

``y[2]^2*x[1]`` into a factor of ``y[1]^2*x[3]`` interchanges the 

variable indices 1 and 2 -- which is not allowed in a symmetric 

reduction. However, reduction by ``y[1]^2*x[2]`` works, since one can 

change variable index 1 into 2 and 2 into 3. So, we add this to 

``S``:: 

  

sage: S.add_generator(y[1]^2*x[2]) 

sage: S 

Symmetric Reduction Strategy in Infinite polynomial ring in x, y over Rational Field, modulo 

x_2*y_1^2, 

x_1*y_2^2 

sage: S.reduce(p) 

y_3*y_1 

  

The next example shows that tail reduction is not done, unless it is 

explicitly advised:: 

  

sage: S.reduce(x[3] + 2*x[2]*y[1]^2 + 3*y[2]^2*x[1]) 

x_3 + 2*x_2*y_1^2 + 3*x_1*y_2^2 

sage: S.tailreduce(x[3] + 2*x[2]*y[1]^2 + 3*y[2]^2*x[1]) 

x_3 

  

However, it is possible to ask for tailreduction already when the 

Symmetric Reduction Strategy is created:: 

  

sage: S2 = SymmetricReductionStrategy(X, [y[2]^2*x[1],y[1]^2*x[2]], tailreduce=True) 

sage: S2 

Symmetric Reduction Strategy in Infinite polynomial ring in x, y over Rational Field, modulo 

x_2*y_1^2, 

x_1*y_2^2 

with tailreduction 

sage: S2.reduce(x[3] + 2*x[2]*y[1]^2 + 3*y[2]^2*x[1]) 

x_3 

  

""" 

  

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

# Copyright (C) 2009 Simon King <king@mathematik.nuigalway.ie> 

# 

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

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

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

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

from __future__ import print_function, absolute_import 

  

import copy 

import operator 

import sys 

from sage.structure.richcmp cimport richcmp, Py_NE, Py_EQ 

  

  

cdef class SymmetricReductionStrategy: 

""" 

A framework for efficient symmetric reduction of InfinitePolynomial, see 

:mod:`~sage.rings.polynomial.infinite_polynomial_element`. 

  

INPUT: 

  

- ``Parent`` -- an Infinite Polynomial Ring, see 

:mod:`~sage.rings.polynomial.infinite_polynomial_element`. 

- ``L`` -- (list, default the empty list) List of elements of ``Parent`` 

with respect to which will be reduced. 

- ``good_input`` -- (bool, default ``None``) If this optional parameter 

is true, it is assumed that each element of ``L`` is symmetrically 

reduced with respect to the previous elements of ``L``. 

  

EXAMPLES:: 

  

sage: X.<y> = InfinitePolynomialRing(QQ) 

sage: from sage.rings.polynomial.symmetric_reduction import SymmetricReductionStrategy 

sage: S = SymmetricReductionStrategy(X, [y[2]^2*y[1],y[1]^2*y[2]], good_input=True) 

sage: S.reduce(y[3] + 2*y[2]*y[1]^2 + 3*y[2]^2*y[1]) 

y_3 + 3*y_2^2*y_1 + 2*y_2*y_1^2 

sage: S.tailreduce(y[3] + 2*y[2]*y[1]^2 + 3*y[2]^2*y[1]) 

y_3 

  

""" 

def __init__(self, Parent, L=None, tailreduce=False, good_input=None): 

""" 

EXAMPLES:: 

  

sage: X.<y> = InfinitePolynomialRing(QQ) 

sage: from sage.rings.polynomial.symmetric_reduction import SymmetricReductionStrategy 

sage: S = SymmetricReductionStrategy(X, [y[2]^2*y[1],y[1]^2*y[2]], good_input=True) 

sage: S == loads(dumps(S)) 

True 

  

""" 

self._parent = Parent 

if hasattr(Parent, '_P'): 

self._R = Parent._P 

else: 

self._R = None 

self._lm = [] 

self._lengths = [] 

self._min_lm = None 

self._tail = int(tailreduce) 

if not (L is None): 

for p in L: 

self.add_generator(p, good_input=good_input) 

  

def __getinitargs__(self): 

r""" 

Used for pickling. 

  

EXAMPLES:: 

  

sage: X.<y> = InfinitePolynomialRing(QQ) 

sage: from sage.rings.polynomial.symmetric_reduction import SymmetricReductionStrategy 

sage: S = SymmetricReductionStrategy(X, [y[2]^2*y[1],y[1]^2*y[2]], good_input=True) 

sage: S.__getinitargs__() 

(Infinite polynomial ring in y over Rational Field, [], 0, None) 

""" 

return (self._parent, [], self._tail, None) 

  

def __getstate__(self): 

r""" 

Used for pickling. 

  

EXAMPLES:: 

  

sage: X.<y> = InfinitePolynomialRing(QQ) 

sage: from sage.rings.polynomial.symmetric_reduction import SymmetricReductionStrategy 

sage: S = SymmetricReductionStrategy(X, [y[2]^2*y[1],y[1]^2*y[2]], good_input=True) 

sage: S.__getstate__() 

([y_2*y_1^2, y_2^2*y_1], [1, 1], y_2*y_1^2, 0, Infinite polynomial ring in y over Rational Field) 

""" 

# Apparently, for pickling it is needed to update self._lm and 

# self._min_lm before calling dumps... 

R = self._parent 

self._lm = [R(x) for x in self._lm] # I have no idea why -- but it seems needed 

  

self._min_lm = R(self._min_lm) 

return (self._lm, self._lengths, self._min_lm, 

self._tail, self._parent) 

  

def __setstate__(self, L): # (lm, lengths, min_lm, tail) 

r""" 

Used for pickling. 

  

EXAMPLES:: 

  

sage: X.<y> = InfinitePolynomialRing(QQ) 

sage: from sage.rings.polynomial.symmetric_reduction import SymmetricReductionStrategy 

sage: S = SymmetricReductionStrategy(X, [y[2]^2*y[1],y[1]^2*y[2]], good_input=True) 

sage: S == loads(dumps(S)) # indirect doctest 

True 

""" 

self._lm = L[0] 

self._lengths = L[1] 

self._min_lm = L[2] 

self._tail = L[3] 

self._parent = L[4] 

if hasattr(self._parent, '_P'): 

self._R = self._parent._P 

else: 

self._R = None 

  

def __richcmp__(self, other, op): 

r""" 

Standard comparison function. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.symmetric_reduction import SymmetricReductionStrategy 

sage: X.<x,y> = InfinitePolynomialRing(QQ) 

sage: S = SymmetricReductionStrategy(X, [y[2]^2*y[1],y[1]^2*y[2]], tailreduce=True) 

sage: S == 17 

False 

sage: S == SymmetricReductionStrategy(X, [y[2]^2*y[1],y[1]^2*y[2]], tailreduce=False) 

False 

sage: S == SymmetricReductionStrategy(X, [y[2]^2*y[1],y[1]^2*y[2]], tailreduce=True) 

True 

""" 

if not isinstance(other, SymmetricReductionStrategy): 

if op in [Py_NE, Py_EQ]: 

return (op == Py_NE) 

else: 

return NotImplemented 

cdef SymmetricReductionStrategy left = self 

cdef SymmetricReductionStrategy right = other 

return richcmp((left._parent, left._lm, left._tail), 

(right._parent, right._lm, right._tail), op) 

  

def gens(self): 

""" 

Return the list of Infinite Polynomials modulo which self reduces. 

  

EXAMPLES:: 

  

sage: X.<y> = InfinitePolynomialRing(QQ) 

sage: from sage.rings.polynomial.symmetric_reduction import SymmetricReductionStrategy 

sage: S = SymmetricReductionStrategy(X, [y[2]^2*y[1],y[1]^2*y[2]]) 

sage: S 

Symmetric Reduction Strategy in Infinite polynomial ring in y over Rational Field, modulo 

y_2*y_1^2, 

y_2^2*y_1 

sage: S.gens() 

[y_2*y_1^2, y_2^2*y_1] 

  

""" 

return self._lm 

  

def setgens(self, L): 

""" 

Define the list of Infinite Polynomials modulo which self reduces. 

  

INPUT: 

  

``L`` -- a list of elements of the underlying infinite polynomial ring. 

  

.. NOTE:: 

  

It is not tested if ``L`` is a good input. That method simply 

assigns a *copy* of ``L`` to the generators of self. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.symmetric_reduction import SymmetricReductionStrategy 

sage: X.<y> = InfinitePolynomialRing(QQ) 

sage: S = SymmetricReductionStrategy(X, [y[2]^2*y[1],y[1]^2*y[2]]) 

sage: R = SymmetricReductionStrategy(X) 

sage: R.setgens(S.gens()) 

sage: R 

Symmetric Reduction Strategy in Infinite polynomial ring in y over Rational Field, modulo 

y_2*y_1^2, 

y_2^2*y_1 

sage: R.gens() is S.gens() 

False 

sage: R.gens() == S.gens() 

True 

  

""" 

self._lm = [X for X in L] 

  

def reset(self): 

""" 

Remove all polynomials from ``self``. 

  

EXAMPLES:: 

  

sage: X.<y> = InfinitePolynomialRing(QQ) 

sage: from sage.rings.polynomial.symmetric_reduction import SymmetricReductionStrategy 

sage: S = SymmetricReductionStrategy(X, [y[2]^2*y[1],y[1]^2*y[2]]) 

sage: S 

Symmetric Reduction Strategy in Infinite polynomial ring in y over Rational Field, modulo 

y_2*y_1^2, 

y_2^2*y_1 

sage: S.reset() 

sage: S 

Symmetric Reduction Strategy in Infinite polynomial ring in y over Rational Field 

  

""" 

self._lm = [] 

self._lengths = [] 

self._min_lm = None 

  

def __repr__(self): 

""" 

String representation of ``self``. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.symmetric_reduction import SymmetricReductionStrategy 

sage: X.<x,y> = InfinitePolynomialRing(QQ) 

sage: S = SymmetricReductionStrategy(X, [y[2]^2*y[1],y[1]^2*y[2]], tailreduce=True) 

sage: S # indirect doctest 

Symmetric Reduction Strategy in Infinite polynomial ring in x, y over Rational Field, modulo 

y_2*y_1^2, 

y_2^2*y_1 

with tailreduction 

  

""" 

s = "Symmetric Reduction Strategy in %s" % self._parent 

if self._lm: 

s += ", modulo\n %s" % (',\n '.join(str(X) for X in self._lm)) 

if self._tail: 

s += '\nwith tailreduction' 

return s 

  

def __call__(self, p): 

""" 

INPUT: 

  

A polynomial or an infinite polynomial 

  

OUTPUT: 

  

A polynomial whose parent ring allows for coercion of any generator of self 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.symmetric_reduction import SymmetricReductionStrategy 

sage: X.<x,y> = InfinitePolynomialRing(QQ, implementation='sparse') 

sage: a, b = y[2]^2*y[1], y[1]^2*y[2] 

sage: p = y[3]*x[2]*x[1] 

sage: S = SymmetricReductionStrategy(X, [a,b]) 

sage: p._p.parent().has_coerce_map_from(a._p.parent()) 

False 

sage: q = S(p) 

sage: q.parent().has_coerce_map_from(a._p.parent()) 

True 

sage: S(p) == S(p._p) 

True 

  

""" 

if hasattr(p, '_p'): 

p = p._p 

if self._R is None: 

self._R = p.parent() 

if hasattr(self._parent, '_P'): 

self._parent._P = self._R 

return p 

if self._R.has_coerce_map_from(p.parent()): 

return self._R(p) 

if p.parent().has_coerce_map_from(self._R): 

self._R = p.parent() 

if hasattr(self._parent, '_P'): 

self._parent._P = self._R 

return p 

# now we really need to work... 

R = self._R 

VarList = list(set(list(R.variable_names()) + list(p.parent().variable_names()))) 

VarList.sort(key=self._parent.varname_key, reverse=True) 

from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing 

self._R = PolynomialRing(self._parent.base_ring(), VarList, 

order=self._parent._order) 

if hasattr(self._parent, '_P'): 

self._parent._P = self._R 

return self._R(p) 

  

def add_generator(self, p, good_input=None): 

""" 

Add another polynomial to ``self``. 

  

INPUT: 

  

- ``p`` -- An element of the underlying infinite polynomial ring. 

- ``good_input`` -- (bool, default ``None``) If ``True``, it is 

assumed that ``p`` is reduced with respect to ``self``. Otherwise, 

this reduction will be done first (which may cost some time). 

  

.. NOTE:: 

  

Previously added polynomials may be modified. All input is 

prepared in view of an efficient symmetric reduction. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.symmetric_reduction import SymmetricReductionStrategy 

sage: X.<x,y> = InfinitePolynomialRing(QQ) 

sage: S = SymmetricReductionStrategy(X) 

sage: S 

Symmetric Reduction Strategy in Infinite polynomial ring in x, y over Rational Field 

sage: S.add_generator(y[3] + y[1]*(x[3]+x[1])) 

sage: S 

Symmetric Reduction Strategy in Infinite polynomial ring in x, y over Rational Field, modulo 

x_3*y_1 + x_1*y_1 + y_3 

  

Note that the first added polynomial will be simplified when 

adding a suitable second polynomial:: 

  

sage: S.add_generator(x[2]+x[1]) 

sage: S 

Symmetric Reduction Strategy in Infinite polynomial ring in x, y over Rational Field, modulo 

y_3, 

x_2 + x_1 

  

By default, reduction is applied to any newly added 

polynomial. This can be avoided by specifying the optional 

parameter 'good_input':: 

  

sage: S.add_generator(y[2]+y[1]*x[2]) 

sage: S 

Symmetric Reduction Strategy in Infinite polynomial ring in x, y over Rational Field, modulo 

y_3, 

x_1*y_1 - y_2, 

x_2 + x_1 

sage: S.reduce(x[3]+x[2]) 

-2*x_1 

sage: S.add_generator(x[3]+x[2], good_input=True) 

sage: S 

Symmetric Reduction Strategy in Infinite polynomial ring in x, y over Rational Field, modulo 

y_3, 

x_3 + x_2, 

x_1*y_1 - y_2, 

x_2 + x_1 

  

In the previous example, ``x[3] + x[2]`` is added without 

being reduced to zero. 

  

""" 

from sage.rings.polynomial.infinite_polynomial_element import InfinitePolynomial 

p = InfinitePolynomial(self._parent, self(p)) 

cdef SymmetricReductionStrategy tmpStrategy 

if good_input is None: 

p = self.reduce(p) 

if p._p == 0: 

return 

cdef int i = 0 

cdef int l = len(self._lm) 

cdef int newLength = len(p._p.coefficients()) 

p = p / p.lc() 

if (self._min_lm is None) or (p.lm() < self._min_lm): 

self._min_lm = p.lm() 

while (i < l) and (self._lengths[i] < newLength): 

i += 1 

self._lm.insert(i, p) 

self._lengths.insert(i, newLength) 

# return 

i += 1 

l += 1 

if i < l: 

tmpStrategy = SymmetricReductionStrategy(self._parent, [p], 

tailreduce=False, 

good_input=True) 

else: 

return 

cdef int j 

while i < l: 

q = tmpStrategy.reduce(self._lm[i].lm()) + tmpStrategy.reduce(self._lm[i].tail()) 

if q._p == 0: 

self._lm.pop(i) 

self._lengths.pop(i) 

l -= 1 

i -= 1 

else: 

q_len = len(q._p.coefficients()) 

if q_len < self._lengths[i]: 

self._lm.pop(i) 

self._lengths.pop(i) 

j = 0 

while (j < i) and (self._lengths[j] < q_len): 

j += 1 

self._lm.insert(j, q) 

self._lengths.insert(j, q_len) 

i += 1 

  

def reduce(self, p, notail=False, report=None): 

""" 

Symmetric reduction of an infinite polynomial. 

  

INPUT: 

  

- ``p`` -- an element of the underlying infinite polynomial ring. 

- ``notail`` -- (bool, default ``False``) If ``True``, tail reduction 

is avoided (but there is no guarantee that there will be no tail 

reduction at all). 

- ``report`` -- (object, default ``None``) If not ``None``, print 

information on the progress of the computation. 

  

OUTPUT: 

  

Reduction of ``p`` with respect to ``self``. 

  

.. NOTE:: 

  

If tail reduction shall be forced, use :meth:`.tailreduce`. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.symmetric_reduction import SymmetricReductionStrategy 

sage: X.<x,y> = InfinitePolynomialRing(QQ) 

sage: S = SymmetricReductionStrategy(X, [y[3]], tailreduce=True) 

sage: S.reduce(y[4]*x[1] + y[1]*x[4]) 

x_4*y_1 

sage: S.reduce(y[4]*x[1] + y[1]*x[4], notail=True) 

x_4*y_1 + x_1*y_4 

  

Last, we demonstrate the 'report' option:: 

  

sage: S = SymmetricReductionStrategy(X, [x[2]+y[1],x[2]*y[3]+x[1]*y[2]+y[4],y[3]+y[2]]) 

sage: S 

Symmetric Reduction Strategy in Infinite polynomial ring in x, y over Rational Field, modulo 

y_3 + y_2, 

x_2 + y_1, 

x_1*y_2 + y_4 - y_3*y_1 

sage: S.reduce(x[3] + x[1]*y[3] + x[1]*y[1],report=True) 

:::> 

x_1*y_1 + y_4 - y_3*y_1 - y_1 

  

Each ':' indicates that one reduction of the leading monomial 

was performed. Eventually, the '>' indicates that the 

computation is finished. 

  

""" 

from sage.rings.polynomial.infinite_polynomial_element import InfinitePolynomial 

cdef list lml = self._lm 

if not lml: 

if report is not None: 

print('>') 

return p 

if p.lm() < self._min_lm: 

if report is not None: 

print('>') 

return p 

cdef list REDUCTOR 

while True: 

REDUCTOR = [] 

for q in lml: 

c, P, w = q.symmetric_cancellation_order(p) 

if (c is not None) and (c <= 0): 

REDUCTOR = [self(q ** P)] 

break 

if not REDUCTOR: 

new_p = p 

break 

p = self(p) # now this is a usual polynomial 

R = self._R 

if hasattr(p, 'reduce'): 

new_p = InfinitePolynomial(self._parent, 

p.reduce([R(X) for X in REDUCTOR])) 

else: 

new_p = InfinitePolynomial(self._parent, p % (REDUCTOR * R)) 

if report is not None: 

sys.stdout.write(':') 

sys.stdout.flush() 

if (new_p._p == p) or (new_p._p == 0): 

break 

p = new_p # now this is an infinite polynomial 

p = new_p 

if (not self._tail) or notail or (p._p == 0): 

if report is not None: 

print('>') 

return p 

# there remains to perform tail reduction 

REM = p.lt() 

p = p.tail() 

p = self.tailreduce(p, report=report) 

if report is not None: 

print('>') 

return p + REM 

  

def tailreduce(self, p, report=None): 

""" 

Symmetric reduction of an infinite polynomial, with forced tail reduction. 

  

INPUT: 

  

- ``p`` -- an element of the underlying infinite polynomial ring. 

- ``report`` -- (object, default ``None``) If not ``None``, print 

information on the progress of the computation. 

  

OUTPUT: 

  

Reduction (including the non-leading elements) of ``p`` with respect to ``self``. 

  

EXAMPLES:: 

  

sage: from sage.rings.polynomial.symmetric_reduction import SymmetricReductionStrategy 

sage: X.<x,y> = InfinitePolynomialRing(QQ) 

sage: S = SymmetricReductionStrategy(X, [y[3]]) 

sage: S.reduce(y[4]*x[1] + y[1]*x[4]) 

x_4*y_1 + x_1*y_4 

sage: S.tailreduce(y[4]*x[1] + y[1]*x[4]) 

x_4*y_1 

  

Last, we demonstrate the 'report' option:: 

  

sage: S = SymmetricReductionStrategy(X, [x[2]+y[1],x[2]*x[3]+x[1]*y[2]+y[4],y[3]+y[2]]) 

sage: S 

Symmetric Reduction Strategy in Infinite polynomial ring in x, y over Rational Field, modulo 

y_3 + y_2, 

x_2 + y_1, 

x_1*y_2 + y_4 + y_1^2 

sage: S.tailreduce(x[3] + x[1]*y[3] + x[1]*y[1],report=True) 

T[3]:::> 

T[3]:> 

x_1*y_1 - y_2 + y_1^2 - y_1 

  

The protocol means the following. 

* 'T[3]' means that we currently do tail reduction for a polynomial 

with three terms. 

* ':::>' means that there were three reductions of leading terms. 

* The tail of the result of the preceding reduction still has three 

terms. One reduction of leading terms was possible, and then the 

final result was obtained. 

""" 

if not self._lm: 

return p 

OUT = p.parent()(0) 

while p._p != 0: 

if report is not None: 

sys.stdout.write('T[%d]' % len(p._p.coefficients())) 

sys.stdout.flush() 

p = self.reduce(p, notail=True, report=report) 

OUT = OUT + p.lt() 

p = p.tail() 

if p.lm() < self._min_lm: 

return OUT + p 

return OUT