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

r""" 

Multivariate Polynomial Rings over Generic Rings 

 

Sage implements multivariate polynomial rings through several 

backends. This generic implementation uses the classes ``PolyDict`` 

and ``ETuple`` to construct a dictionary with exponent tuples as keys 

and coefficients as values. 

 

AUTHORS: 

 

- David Joyner and William Stein 

 

- Kiran S. Kedlaya (2006-02-12): added Macaulay2 analogues of Singular 

features 

 

- Martin Albrecht (2006-04-21): reorganize class hierarchy for singular 

rep 

 

- Martin Albrecht (2007-04-20): reorganized class hierarchy to support 

Pyrex implementations 

 

- Robert Bradshaw (2007-08-15): Coercions from rings in a subset of 

the variables. 

 

EXAMPLES: 

 

We construct the Frobenius morphism on `\GF{5}[x,y,z]` over 

`\GF{5}`:: 

 

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

sage: frob = R.hom([x^5, y^5, z^5]) 

sage: frob(x^2 + 2*y - z^4) 

-z^20 + x^10 + 2*y^5 

sage: frob((x + 2*y)^3) 

x^15 + x^10*y^5 + 2*x^5*y^10 - 2*y^15 

sage: (x^5 + 2*y^5)^3 

x^15 + x^10*y^5 + 2*x^5*y^10 - 2*y^15 

 

We make a polynomial ring in one variable over a polynomial ring in 

two variables:: 

 

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

sage: S.<t> = PowerSeriesRing(R) 

sage: t*(x+y) 

(x + y)*t 

 

TESTS:: 

 

sage: PolynomialRing(GF(5), 3, 'xyz').objgens() 

(Multivariate Polynomial Ring in x, y, z over Finite Field of size 5, 

(x, y, z)) 

""" 

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

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

# 

# 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 __future__ import absolute_import 

from six.moves import range 

from six import iteritems, iterkeys, itervalues 

 

from sage.rings.ring import IntegralDomain 

import sage.rings.fraction_field_element as fraction_field_element 

 

from sage.rings.integer_ring import is_IntegerRing 

 

import sage.rings.polynomial.multi_polynomial_ideal as multi_polynomial_ideal 

 

from sage.rings.polynomial.multi_polynomial_ring_generic import MPolynomialRing_generic, is_MPolynomialRing 

from sage.rings.polynomial.polynomial_singular_interface import PolynomialRing_singular_repr 

from sage.rings.polynomial.polydict import PolyDict, ETuple 

from sage.rings.polynomial.term_order import TermOrder 

 

from sage.interfaces.singular import is_SingularElement 

from sage.interfaces.all import macaulay2 as macaulay2_default 

from sage.interfaces.macaulay2 import is_Macaulay2Element 

from sage.libs.pari.all import pari_gen 

 

from sage.structure.element import Element 

 

class MPolynomialRing_macaulay2_repr: 

""" 

 

""" 

def _macaulay2_(self, macaulay2=None): 

if macaulay2 is None: 

macaulay2 = macaulay2_default 

try: 

R = self.__macaulay2 

if not (R.parent() is macaulay2): 

raise ValueError 

R._check_valid() 

return R 

except (AttributeError, ValueError): 

base_str = self._macaulay2_base_str() 

self.__macaulay2 = macaulay2.ring(base_str, str(self.gens()), \ 

self.term_order().macaulay2_str()) 

return self.__macaulay2 

 

def _macaulay2_base_str(self): 

if self.base_ring().is_prime_field(): 

if self.characteristic() == 0: 

return "QQ" 

else: 

return "ZZ/" + str(self.characteristic()) 

elif is_IntegerRing(self.base_ring()): 

return "ZZ" 

else: 

raise TypeError("no conversion of to a Macaulay2 ring defined") 

 

def _macaulay2_set_ring(self, macaulay2): 

macaulay2.ring(self._macaulay2_base_str(), str(self.gens()), \ 

self.term_order().macaulay2_str()) 

 

def is_exact(self): 

return self.base_ring().is_exact() 

 

 

class MPolynomialRing_polydict( MPolynomialRing_macaulay2_repr, PolynomialRing_singular_repr, MPolynomialRing_generic): 

""" 

Multivariable polynomial ring. 

 

EXAMPLES:: 

 

sage: R = PolynomialRing(Integers(12), 'x', 5); R 

Multivariate Polynomial Ring in x0, x1, x2, x3, x4 over Ring of integers modulo 12 

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

True 

""" 

def __init__(self, base_ring, n, names, order): 

from sage.rings.polynomial.polynomial_singular_interface import can_convert_to_singular 

order = TermOrder(order,n) 

MPolynomialRing_generic.__init__(self, base_ring, n, names, order) 

# Construct the generators 

v = [0] * n 

one = base_ring(1); 

self._gens = [] 

C = self._poly_class() 

for i in range(n): 

v[i] = 1 # int's! 

self._gens.append(C(self, {tuple(v):one})) 

v[i] = 0 

self._gens = tuple(self._gens) 

self._zero_tuple = tuple(v) 

self._has_singular = can_convert_to_singular(self) 

# This polynomial ring should belong to Algebras(base_ring). 

# Algebras(...).parent_class, which was called from MPolynomialRing_generic.__init__, 

# tries to provide a conversion from the base ring, if it does not exist. 

# This is for algebras that only do the generic stuff in their initialisation. 

# But here, we want to use PolynomialBaseringInjection. Hence, we need to 

# wipe the memory and construct the conversion from scratch. 

if n: 

from sage.rings.polynomial.polynomial_element import PolynomialBaseringInjection 

base_inject = PolynomialBaseringInjection(base_ring, self) 

self.register_coercion(base_inject) 

 

def _monomial_order_function(self): 

return self.__monomial_order_function 

 

def _poly_class(self): 

from sage.rings.polynomial.multi_polynomial_element import MPolynomial_polydict 

return MPolynomial_polydict 

 

def __eq__(left, right): 

""" 

Check whether ``left`` is equal to ``right``. 

 

EXAMPLES:: 

 

sage: R = PolynomialRing(Integers(10), 'x', 4) 

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

True 

""" 

if not is_MPolynomialRing(right): 

return False 

return ((left.base_ring(), left.ngens(), 

left.variable_names(), left.term_order()) == 

(right.base_ring(), right.ngens(), 

right.variable_names(), right.term_order())) 

 

def __ne__(self , other): 

""" 

Check whether ``self`` is not equal to ``other``. 

 

EXAMPLES:: 

 

sage: R = PolynomialRing(Integers(8), 'x', 3) 

sage: loads(R.dumps()) != R 

False 

""" 

return not (self == other) 

 

def __call__(self, x, check=True): 

""" 

Convert ``x`` to an element of this multivariate polynomial ring, 

possibly non-canonically. 

 

EXAMPLES: 

 

We create a Macaulay2 multivariate polynomial via ideal 

arithmetic, then convert it into R. 

 

:: 

 

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

sage: I = R.ideal([x^3 + y, y]) 

sage: S = I._macaulay2_() # optional - macaulay2 

sage: T = S*S*S # optional - macaulay2 

sage: U = T.gens().entries().flatten() # optional - macaulay2 

sage: f = U[2]; f # optional - macaulay2 

x^6*y+2*x^3*y^2+y^3 

sage: R(repr(f)) # optional - macaulay2 

x^6*y + 2*x^3*y^2 + y^3 

 

Some other subtle conversions. We create polynomial rings in 2 

variables over the rationals, integers, and a finite field. 

 

:: 

 

sage: R.<x,y> = QQ[] 

sage: S.<x,y> = ZZ[] 

sage: T.<x,y> = GF(7)[] 

 

We convert from integer polynomials to rational polynomials, 

and back:: 

 

sage: f = R(S.0^2 - 4*S.1^3); f 

-4*y^3 + x^2 

sage: parent(f) 

Multivariate Polynomial Ring in x, y over Rational Field 

sage: parent(S(f)) 

Multivariate Polynomial Ring in x, y over Integer Ring 

 

We convert from polynomials over the finite field. 

 

:: 

 

sage: f = R(T.0^2 - 4*T.1^3); f 

3*y^3 + x^2 

sage: parent(f) 

Multivariate Polynomial Ring in x, y over Rational Field 

 

We dump and load the polynomial ring S:: 

 

sage: S2 = loads(dumps(S)) 

sage: S2 == S 

True 

 

Coerce works and gets the right parent. 

 

:: 

 

sage: parent(S2._coerce_(S.0)) is S2 

True 

 

Conversion to reduce modulo a prime between rings with different 

variable names:: 

 

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

sage: S.<a,b> = PolynomialRing(GF(7),2) 

sage: f = x^2 + 2/3*y^3 

sage: S(f) 

3*b^3 + a^2 

 

Conversion from symbolic variables:: 

 

sage: x,y,z = var('x,y,z') 

sage: R = QQ['x,y,z'] 

sage: type(x) 

<type 'sage.symbolic.expression.Expression'> 

sage: type(R(x)) 

<type 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular'> 

sage: f = R(x^3 + y^3 - z^3); f 

x^3 + y^3 - z^3 

sage: type(f) 

<type 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular'> 

sage: parent(f) 

Multivariate Polynomial Ring in x, y, z over Rational Field 

 

A more complicated symbolic and computational mix. Behind the 

scenes Singular and Maxima are doing the real work. 

 

:: 

 

sage: R = QQ['x,y,z'] 

sage: f = (x^3 + y^3 - z^3)^10; f 

(x^3 + y^3 - z^3)^10 

sage: g = R(f); parent(g) 

Multivariate Polynomial Ring in x, y, z over Rational Field 

sage: (f - g).expand() 

0 

 

It intelligently handles conversions from polynomial rings in a subset 

of the variables too. 

 

:: 

 

sage: R = GF(5)['x,y,z'] 

sage: S = ZZ['y'] 

sage: R(7*S.0) 

2*y 

sage: T = ZZ['x,z'] 

sage: R(2*T.0 + 6*T.1 + T.0*T.1^2) 

x*z^2 + 2*x + z 

 

:: 

 

sage: R = QQ['t,x,y,z'] 

sage: S.<x> = ZZ['x'] 

sage: T.<z> = S['z'] 

sage: T 

Univariate Polynomial Ring in z over Univariate Polynomial Ring in x over Integer Ring 

sage: f = (x+3*z+5)^2; f 

9*z^2 + (6*x + 30)*z + x^2 + 10*x + 25 

sage: R(f) 

x^2 + 6*x*z + 9*z^2 + 10*x + 30*z + 25 

 

Arithmetic with a constant from a base ring:: 

 

sage: R.<u,v> = QQ[] 

sage: S.<x,y> = R[] 

sage: u^3*x^2 + v*y 

u^3*x^2 + v*y 

 

Stacked polynomial rings convert into constants if possible. First, 

the univariate case:: 

 

sage: R.<x> = QQ[] 

sage: S.<u,v> = R[] 

sage: S(u + 2) 

u + 2 

sage: S(u + 2).degree() 

1 

sage: S(x + 3) 

x + 3 

sage: S(x + 3).degree() 

0 

 

Second, the multivariate case:: 

 

sage: R.<x,y> = QQ[] 

sage: S.<u,v> = R[] 

sage: S(x + 2*y) 

x + 2*y 

sage: S(u + 2*v) 

u + 2*v 

 

Conversion from strings:: 

 

sage: R.<x,y> = QQ[] 

sage: R('x+(1/2)*y^2') 

1/2*y^2 + x 

sage: S.<u,v> = ZZ[] 

sage: S('u^2 + u*v + v^2') 

u^2 + u*v + v^2 

 

Foreign polynomial rings convert into the highest ring; the point 

here is that an element of T could convert to an element of R or an 

element of S; it is anticipated that an element of T is more likely 

to be "the right thing" and is historically consistent. 

 

:: 

 

sage: R.<x,y> = QQ[] 

sage: S.<u,v> = R[] 

sage: T.<a,b> = QQ[] 

sage: S(a + b) 

u + v 

 

TESTS: 

 

Check if we still allow nonsense (see :trac:`7951`):: 

 

sage: P = PolynomialRing(QQ, 0, '') 

sage: P('pi') 

Traceback (most recent call last): 

... 

TypeError: unable to convert pi to a rational 

 

Check that it is possible to convert strings to iterated 

polynomial rings (see :trac:`13327`):: 

 

sage: Rm = QQ["a"]["b, c"] 

sage: Rm("a*b") 

a*b 

sage: parent(_) is Rm 

True 

 

Check that conversion from PARI works correctly (see 

:trac:`17974`):: 

 

sage: A.<a> = PolynomialRing(QQ) 

sage: B.<d,e> = PolynomialRing(A) 

sage: f = pari(a*d) 

sage: B(f) 

a*d 

 

sage: A.<a,b> = PolynomialRing(QQ) 

sage: B.<d,e> = PolynomialRing(A) 

sage: f = pari(a*d) 

sage: B(f) 

a*d 

 

It is possible to convert `f` into `B` by using ``f.sage()``, 

but this requires specifying a ``locals`` argument:: 

 

sage: f 

d*a 

sage: f.sage() 

Traceback (most recent call last): 

... 

NameError: name 'd' is not defined 

sage: f.sage(locals={'a': a, 'd': d}) 

a*d 

 

Check that :trac:`21999` is fixed:: 

 

sage: R = QQbar['s,t'] 

sage: type(R({(1,2): 3}).coefficients()[0]) 

<class 'sage.rings.qqbar.AlgebraicNumber'> 

""" 

from sage.rings.polynomial.multi_polynomial_element import MPolynomial_polydict 

import sage.rings.polynomial.polynomial_element as polynomial_element 

 

# handle constants that coerce into self.base_ring() first, if possible 

if isinstance(x, Element) and x.parent() is self.base_ring(): 

# A Constant multi-polynomial 

return self({self._zero_tuple:x}) 

 

try: 

y = self.base_ring()._coerce_(x) 

return MPolynomial_polydict(self, {self._zero_tuple:y}) 

except TypeError: 

pass 

 

from .multi_polynomial_libsingular import MPolynomial_libsingular 

 

if isinstance(x, MPolynomial_polydict): 

P = x.parent() 

 

if P is self: 

return x 

elif P == self: 

return MPolynomial_polydict(self, x.element().dict()) 

elif self.base_ring().has_coerce_map_from(P): 

# it might be in the base ring (i.e. a poly ring over a poly ring) 

c = self.base_ring()(x) 

return MPolynomial_polydict(self, {self._zero_tuple:c}) 

elif len(P.variable_names()) == len(self.variable_names()): 

# Map the variables in some crazy way (but in order, 

# of course). This is here since R(blah) is supposed 

# to be "make an element of R if at all possible with 

# no guarantees that this is mathematically solid." 

K = self.base_ring() 

D = x.element().dict() 

for i, a in iteritems(D): 

D[i] = K(a) 

return MPolynomial_polydict(self, D) 

elif set(P.variable_names()).issubset(set(self.variable_names())) and self.base_ring().has_coerce_map_from(P.base_ring()): 

# If the named variables are a superset of the input, map the variables by name 

return MPolynomial_polydict(self, self._extract_polydict(x)) 

else: 

return MPolynomial_polydict(self, x._mpoly_dict_recursive(self.variable_names(), self.base_ring())) 

 

elif isinstance(x, MPolynomial_libsingular): 

P = x.parent() 

if P == self: 

return MPolynomial_polydict(self, x.dict()) 

elif self.base_ring().has_coerce_map_from(P): 

# it might be in the base ring (i.e. a poly ring over a poly ring) 

c = self.base_ring()(x) 

return MPolynomial_polydict(self, {self._zero_tuple:c}) 

elif len(P.variable_names()) == len(self.variable_names()): 

# Map the variables in some crazy way (but in order, 

# of course). This is here since R(blah) is supposed 

# to be "make an element of R if at all possible with 

# no guarantees that this is mathematically solid." 

K = self.base_ring() 

D = x.dict() 

for i, a in iteritems(D): 

D[i] = K(a) 

return MPolynomial_polydict(self, D) 

elif set(P.variable_names()).issubset(set(self.variable_names())) and self.base_ring().has_coerce_map_from(P.base_ring()): 

# If the named variables are a superset of the input, map the variables by name 

return MPolynomial_polydict(self, self._extract_polydict(x)) 

else: 

return MPolynomial_polydict(self, x._mpoly_dict_recursive(self.variable_names(), self.base_ring())) 

 

elif isinstance(x, polynomial_element.Polynomial): 

return MPolynomial_polydict(self, x._mpoly_dict_recursive(self.variable_names(), self.base_ring())) 

 

elif isinstance(x, PolyDict): 

return MPolynomial_polydict(self, x) 

 

elif isinstance(x, dict): 

K = self.base_ring() 

return MPolynomial_polydict(self, {i: K(a) for i, a in iteritems(x)}) 

 

elif isinstance(x, fraction_field_element.FractionFieldElement) and x.parent().ring() == self: 

if x.denominator() == 1: 

return x.numerator() 

else: 

raise TypeError("unable to coerce since the denominator is not 1") 

 

elif is_SingularElement(x) and self._has_singular: 

self._singular_().set_ring() 

try: 

return x.sage_poly(self) 

except TypeError: 

raise TypeError("unable to coerce singular object") 

 

elif hasattr(x, '_polynomial_'): 

return x._polynomial_(self) 

 

elif isinstance(x, str): 

from sage.misc.sage_eval import sage_eval 

try: 

x = sage_eval(x, self.gens_dict_recursive()) 

except NameError: 

raise TypeError("unable to evaluate {!r} in {}".format(x, self)) 

return self(x) 

 

elif is_Macaulay2Element(x): 

try: 

s = x.sage_polystring() 

if len(s) == 0: 

raise TypeError 

# NOTE: It's CRUCIAL to use the eval command as follows, 

# i.e., with the gen dict as the third arg and the second 

# empty. Otherwise pickling won't work after calls to this eval!!! 

# This took a while to figure out! 

return self(eval(s, {}, self.gens_dict())) 

except (AttributeError, TypeError, NameError, SyntaxError): 

raise TypeError("Unable to coerce macaulay2 object") 

return MPolynomial_polydict(self, x) 

 

elif isinstance(x, pari_gen) and x.type() == 't_POL': 

# This recursive approach is needed because PARI 

# represents multivariate polynomials as iterated 

# univariate polynomials. Below, v is the variable 

# with highest priority, and the x[i] are expressions 

# in the remaining variables. 

v = self.gens_dict_recursive()[str(x.variable())] 

return sum(self(x[i]) * v ** i for i in range(x.poldegree() + 1)) 

 

if isinstance(x, dict): 

return MPolynomial_polydict(self, x) 

else: 

c = self.base_ring()(x) 

return MPolynomial_polydict(self, {self._zero_tuple:c}) 

 

### The following methods are handy for implementing Groebner 

### basis algorithms. They do only superficial type/sanity checks 

### and should be called carefully. 

 

def monomial_quotient(self, f, g, coeff=False): 

r""" 

Return ``f/g``, where both ``f`` and ``g`` are treated as monomials. 

 

Coefficients are ignored by default. 

 

INPUT: 

 

- ``f`` - monomial. 

 

- ``g`` - monomial. 

 

- ``coeff`` - divide coefficients as well (default: 

False). 

 

OUTPUT: monomial. 

 

EXAMPLES:: 

 

sage: from sage.rings.polynomial.multi_polynomial_ring import MPolynomialRing_polydict_domain 

sage: P.<x,y,z> = MPolynomialRing_polydict_domain(QQ, 3, order='degrevlex') 

sage: P.monomial_quotient(3/2*x*y, x) 

y 

 

:: 

 

sage: P.monomial_quotient(3/2*x*y, 2*x, coeff=True) 

3/4*y 

 

TESTS:: 

 

sage: from sage.rings.polynomial.multi_polynomial_ring import MPolynomialRing_polydict_domain 

sage: R.<x,y,z> = MPolynomialRing_polydict_domain(QQ,3, order='degrevlex') 

sage: P.<x,y,z> = MPolynomialRing_polydict_domain(QQ,3, order='degrevlex') 

sage: P.monomial_quotient(x*y, x) 

y 

 

:: 

 

sage: P.monomial_quotient(x*y, R.gen()) 

y 

 

:: 

 

sage: P.monomial_quotient(P(0), P(1)) 

0 

 

:: 

 

sage: P.monomial_quotient(P(1), P(0)) 

Traceback (most recent call last): 

... 

ZeroDivisionError 

 

:: 

 

sage: P.monomial_quotient(P(3/2), P(2/3), coeff=True) 

9/4 

 

:: 

 

sage: P.monomial_quotient(x, y) # Note the wrong result 

x*y^-1 

 

:: 

 

sage: P.monomial_quotient(x, P(1)) 

x 

 

.. note:: 

 

Assumes that the head term of f is a multiple of the head 

term of g and return the multiplicant m. If this rule is 

violated, funny things may happen. 

""" 

from sage.rings.polynomial.multi_polynomial_element import MPolynomial_polydict 

 

if not f: 

return f 

if not g: 

raise ZeroDivisionError 

 

if not coeff: 

coeff = self.base_ring().one() 

else: 

coeff = self.base_ring()(next(itervalues(f.dict())) / next(itervalues(g.dict()))) 

 

f = next(iterkeys(f.dict())) 

g = next(iterkeys(g.dict())) 

 

res = f.esub(g) 

 

return MPolynomial_polydict(self, PolyDict({res:coeff}, 

force_int_exponents=False, 

force_etuples=False)) 

 

def monomial_lcm(self, f, g): 

""" 

LCM for monomials. Coefficients are ignored. 

 

INPUT: 

 

- ``f`` - monomial. 

 

- ``g`` - monomial. 

 

OUTPUT: monomial. 

 

EXAMPLES:: 

 

sage: from sage.rings.polynomial.multi_polynomial_ring import MPolynomialRing_polydict_domain 

sage: P.<x,y,z> = MPolynomialRing_polydict_domain(QQ,3, order='degrevlex') 

sage: P.monomial_lcm(3/2*x*y, x) 

x*y 

 

TESTS:: 

 

sage: from sage.rings.polynomial.multi_polynomial_ring import MPolynomialRing_polydict_domain 

sage: R.<x,y,z> = MPolynomialRing_polydict_domain(QQ,3, order='degrevlex') 

sage: P.<x,y,z> = MPolynomialRing_polydict_domain(QQ,3, order='degrevlex') 

sage: P.monomial_lcm(x*y, R.gen()) 

x*y 

 

:: 

 

sage: P.monomial_lcm(P(3/2), P(2/3)) 

1 

 

:: 

 

sage: P.monomial_lcm(x, P(1)) 

x 

""" 

one = self.base_ring().one() 

 

f, = f.dict() 

g, = g.dict() 

 

length = len(f) 

 

res = {i: max(f[i], g[i]) 

for i in f.common_nonzero_positions(g)} 

 

return self(PolyDict({ETuple(res, length): one}, 

force_int_exponents=False, force_etuples=False)) 

 

def monomial_reduce(self, f, G): 

r""" 

Try to find a ``g`` in ``G`` where ``g.lm()`` divides ``f``. 

 

If found, ``(flt,g)`` is returned, ``(0,0)`` otherwise, where 

``flt`` is ``f/g.lm()``. It is assumed that ``G`` is iterable and contains 

ONLY elements in this ring. 

 

INPUT: 

 

 

- ``f`` - monomial 

 

- ``G`` - list/set of mpolynomials 

 

 

EXAMPLES:: 

 

sage: from sage.rings.polynomial.multi_polynomial_ring import MPolynomialRing_polydict_domain 

sage: P.<x,y,z>=MPolynomialRing_polydict_domain(QQ,3, order='degrevlex') 

sage: f = x*y^2 

sage: G = [3/2*x^3 + y^2 + 1/2, 1/4*x*y + 2/7, P(1/2)] 

sage: P.monomial_reduce(f,G) 

(y, 1/4*x*y + 2/7) 

 

:: 

 

sage: from sage.rings.polynomial.multi_polynomial_ring import MPolynomialRing_polydict_domain 

sage: P.<x,y,z> = MPolynomialRing_polydict_domain(Zmod(23432),3, order='degrevlex') 

sage: f = x*y^2 

sage: G = [3*x^3 + y^2 + 2, 4*x*y + 7, P(2)] 

sage: P.monomial_reduce(f,G) 

(y, 4*x*y + 7) 

 

TESTS:: 

 

sage: from sage.rings.polynomial.multi_polynomial_ring import MPolynomialRing_polydict_domain 

sage: P.<x,y,z>=MPolynomialRing_polydict_domain(QQ,3, order='degrevlex') 

sage: f = x*y^2 

sage: G = [3/2*x^3 + y^2 + 1/2, 1/4*x*y + 2/7, P(1/2)] 

 

:: 

 

sage: P.monomial_reduce(P(0),G) 

(0, 0) 

 

:: 

 

sage: P.monomial_reduce(f,[P(0)]) 

(0, 0) 

""" 

if not f: 

return 0,0 

for g in G: 

t = g.lm() 

try: 

if self.monomial_divides(t,f): 

return self.monomial_quotient(f,t),g 

except ZeroDivisionError: 

return 0,0 

return 0,0 

 

def monomial_divides(self, a, b): 

""" 

Return ``False`` if ``a`` does not divide ``b`` and ``True`` otherwise. 

 

INPUT: 

 

- ``a`` -- monomial 

 

- ``b`` -- monomial 

 

OUTPUT: Boolean 

 

EXAMPLES:: 

 

sage: P.<x,y,z> = PolynomialRing(ZZ,3, order='degrevlex') 

sage: P.monomial_divides(x*y*z, x^3*y^2*z^4) 

True 

sage: P.monomial_divides(x^3*y^2*z^4, x*y*z) 

False 

 

TESTS:: 

 

sage: P.<x,y,z> = PolynomialRing(ZZ,3, order='degrevlex') 

sage: P.monomial_divides(P(1), P(0)) 

True 

sage: P.monomial_divides(P(1), x) 

True 

""" 

if not b: 

return True 

if not a: 

raise ZeroDivisionError 

 

a, = a.dict() 

b, = b.dict() 

 

for i in b.common_nonzero_positions(a): 

if b[i] - a[i] < 0: 

return False 

return True 

 

def monomial_pairwise_prime(self, h, g): 

r""" 

Return True if ``h`` and ``g`` are pairwise prime. 

 

Both are treated as monomials. 

 

INPUT: 

 

- ``h`` - monomial. 

 

- ``g`` - monomial. 

 

OUTPUT: Boolean. 

 

EXAMPLES:: 

 

sage: from sage.rings.polynomial.multi_polynomial_ring import MPolynomialRing_polydict_domain 

sage: P.<x,y,z> = MPolynomialRing_polydict_domain(QQ,3, order='degrevlex') 

sage: P.monomial_pairwise_prime(x^2*z^3, y^4) 

True 

 

:: 

 

sage: P.monomial_pairwise_prime(1/2*x^3*y^2, 3/4*y^3) 

False 

 

TESTS:: 

 

sage: from sage.rings.polynomial.multi_polynomial_ring import MPolynomialRing_polydict_domain 

sage: P.<x,y,z> = MPolynomialRing_polydict_domain(QQ,3, order='degrevlex') 

sage: Q.<x,y,z> = MPolynomialRing_polydict_domain(QQ,3, order='degrevlex') 

sage: P.monomial_pairwise_prime(x^2*z^3, Q('y^4')) 

True 

 

:: 

 

sage: P.monomial_pairwise_prime(1/2*x^3*y^2, Q(0)) 

True 

 

:: 

 

sage: P.monomial_pairwise_prime(P(1/2),x) 

False 

""" 

if not g: 

if not h: 

return False #GCD(0,0) = 0 

else: 

return True #GCD(x,0) = 1 

 

elif not h: 

return True # GCD(0,x) = 1 

 

return self.monomial_lcm(g,h) == g*h 

 

def monomial_all_divisors(self, t): 

r""" 

Return a list of all monomials that divide ``t``, coefficients are 

ignored. 

 

INPUT: 

 

- ``t`` - a monomial. 

 

OUTPUT: a list of monomials. 

 

EXAMPLES:: 

 

sage: from sage.rings.polynomial.multi_polynomial_ring import MPolynomialRing_polydict_domain 

sage: P.<x,y,z> = MPolynomialRing_polydict_domain(QQ,3, order='degrevlex') 

sage: P.monomial_all_divisors(x^2*z^3) 

[x, x^2, z, x*z, x^2*z, z^2, x*z^2, x^2*z^2, z^3, x*z^3, x^2*z^3] 

 

ALGORITHM: addwithcarry idea by Toon Segers 

""" 

 

def addwithcarry(tempvector, maxvector, pos): 

if tempvector[pos] < maxvector[pos]: 

tempvector[pos] += 1 

else: 

tempvector[pos] = 0 

tempvector = addwithcarry(tempvector, maxvector, pos + 1) 

return tempvector 

 

if not t.is_monomial(): 

raise TypeError("only monomials are supported") 

 

R = self 

one = self.base_ring().one() 

M = list() 

 

v, = t.dict() 

maxvector = list(v) 

 

tempvector =[0,] * len(maxvector) 

 

pos = 0 

 

while tempvector != maxvector: 

tempvector = addwithcarry(list(tempvector) , maxvector, pos) 

M.append(R(PolyDict({ETuple(tempvector): one}, 

force_int_exponents=False, 

force_etuples=False))) 

return M 

 

 

class MPolynomialRing_polydict_domain(IntegralDomain, 

MPolynomialRing_polydict): 

def __init__(self, base_ring, n, names, order): 

order = TermOrder(order,n) 

MPolynomialRing_polydict.__init__(self, base_ring, n, names, order) 

 

def is_integral_domain(self, proof = True): 

return True 

 

def is_field(self, proof = True): 

if self.ngens() == 0: 

return self.base_ring().is_field(proof) 

return False 

 

def ideal(self, *gens, **kwds): 

""" 

Create an ideal in this polynomial ring. 

""" 

do_coerce = False 

if len(gens) == 1: 

from sage.rings.ideal import is_Ideal 

if is_Ideal(gens[0]): 

if gens[0].ring() is self: 

return gens[0] 

gens = gens[0].gens() 

elif isinstance(gens[0], (list, tuple)): 

gens = gens[0] 

if not self._has_singular: 

# pass through 

MPolynomialRing_generic.ideal(self,gens,**kwds) 

if is_SingularElement(gens): 

gens = list(gens) 

do_coerce = True 

if is_Macaulay2Element(gens): 

gens = list(gens) 

do_coerce = True 

elif not isinstance(gens, (list, tuple)): 

gens = [gens] 

if ('coerce' in kwds and kwds['coerce']) or do_coerce: 

gens = [self(x) for x in gens] # this will even coerce from singular ideals correctly! 

return multi_polynomial_ideal.MPolynomialIdeal(self, gens, **kwds)