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

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

r""" 

Rings 

""" 

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

# Copyright (C) 2005 David Kohel <kohel@maths.usyd.edu> 

# William Stein <wstein@math.ucsd.edu> 

# 2008 Teresa Gomez-Diaz (CNRS) <Teresa.Gomez-Diaz@univ-mlv.fr> 

# 2008-2011 Nicolas M. Thiery <nthiery at users.sf.net> 

# 

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

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

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

 

from sage.misc.cachefunc import cached_method 

from sage.misc.lazy_import import LazyImport 

from sage.categories.category_with_axiom import CategoryWithAxiom 

from sage.categories.rngs import Rngs 

from sage.structure.element import Element 

from functools import reduce 

from sage.misc.cachefunc import cached_method 

 

class Rings(CategoryWithAxiom): 

""" 

The category of rings 

 

Associative rings with unit, not necessarily commutative 

 

EXAMPLES:: 

 

sage: Rings() 

Category of rings 

sage: sorted(Rings().super_categories(), key=str) 

[Category of rngs, Category of semirings] 

 

sage: sorted(Rings().axioms()) 

['AdditiveAssociative', 'AdditiveCommutative', 'AdditiveInverse', 

'AdditiveUnital', 'Associative', 'Distributive', 'Unital'] 

 

sage: Rings() is (CommutativeAdditiveGroups() & Monoids()).Distributive() 

True 

sage: Rings() is Rngs().Unital() 

True 

sage: Rings() is Semirings().AdditiveInverse() 

True 

 

TESTS:: 

 

sage: TestSuite(Rings()).run() 

 

.. TODO:: 

 

(see: http://trac.sagemath.org/sage_trac/wiki/CategoriesRoadMap) 

 

- Make Rings() into a subcategory or alias of Algebras(ZZ); 

 

- A parent P in the category ``Rings()`` should automatically be 

in the category ``Algebras(P)``. 

""" 

 

_base_category_class_and_axiom = (Rngs, "Unital") 

 

class MorphismMethods: 

@cached_method 

def is_injective(self): 

""" 

Return whether or not this morphism is injective. 

 

EXAMPLES: 

 

This often raises a ``NotImplementedError`` as many homomorphisms do 

not implement this method:: 

 

sage: R.<x> = QQ[] 

sage: f = R.hom([x + 1]); f 

Ring endomorphism of Univariate Polynomial Ring in x over Rational Field 

Defn: x |--> x + 1 

sage: f.is_injective() 

Traceback (most recent call last): 

... 

NotImplementedError 

 

If the domain is a field, the homomorphism is injective:: 

 

sage: K.<x> = FunctionField(QQ) 

sage: L.<y> = FunctionField(QQ) 

sage: f = K.hom([y]); f 

Function Field morphism: 

From: Rational function field in x over Rational Field 

To: Rational function field in y over Rational Field 

Defn: x |--> y 

sage: f.is_injective() 

True 

 

Unless the codomain is the zero ring:: 

 

sage: codomain = Integers(1) 

sage: f = QQ.hom([Zmod(1)(0)], check=False) 

sage: f.is_injective() 

False 

 

Homomorphism from rings of characteristic zero to rings of positive 

characteristic can not be injective:: 

 

sage: R.<x> = ZZ[] 

sage: f = R.hom([GF(3)(1)]); f 

Ring morphism: 

From: Univariate Polynomial Ring in x over Integer Ring 

To: Finite Field of size 3 

Defn: x |--> 1 

sage: f.is_injective() 

False 

 

A morphism whose domain is an order in a number field is injective if 

the codomain has characteristic zero:: 

 

sage: K.<x> = FunctionField(QQ) 

sage: f = ZZ.hom(K); f 

Composite map: 

From: Integer Ring 

To: Rational function field in x over Rational Field 

Defn: Conversion via FractionFieldElement_1poly_field map: 

From: Integer Ring 

To: Fraction Field of Univariate Polynomial Ring in x over Rational Field 

then 

Isomorphism morphism: 

From: Fraction Field of Univariate Polynomial Ring in x over Rational Field 

To: Rational function field in x over Rational Field 

sage: f.is_injective() 

True 

 

A coercion to the fraction field is injective:: 

 

sage: R = ZpFM(3) 

sage: R.fraction_field().coerce_map_from(R).is_injective() 

True 

 

""" 

if self.domain().is_zero(): 

return True 

if self.codomain().is_zero(): 

# the only map to the zero ring that is injective is the map from itself 

return False 

 

from sage.categories.fields import Fields 

if self.domain() in Fields(): 

# A ring homomorphism from a field to a ring is injective 

# (unless the codomain is the zero ring.) Note that ring 

# homomorphism must send the 1 element to the 1 element 

return True 

 

if self.domain().characteristic() == 0: 

if self.codomain().characteristic() != 0: 

return False 

else: 

from sage.categories.integral_domains import IntegralDomains 

if self.domain() in IntegralDomains(): 

# if all elements of the domain are algebraic over ZZ, 

# then the homomorphism must be injective (in 

# particular if the domain is ZZ) 

from sage.categories.number_fields import NumberFields 

if self.domain().fraction_field() in NumberFields(): 

return True 

 

if self._is_coercion: 

try: 

K = self.domain().fraction_field() 

except (TypeError, AttributeError, ValueError): 

pass 

else: 

if K is self.codomain(): 

return True 

 

if self.domain().cardinality() > self.codomain().cardinality(): 

return False 

 

raise NotImplementedError 

 

def _is_nonzero(self): 

r""" 

Return whether this is not the zero morphism. 

 

.. NOTE:: 

 

We can not override ``is_zero()`` from the category framework 

and we can not implement ``__nonzero__`` because it is a 

special method. That this is why this has a cumbersome name. 

 

EXAMPLES:: 

 

sage: ZZ.hom(ZZ)._is_nonzero() 

True 

sage: ZZ.hom(Zmod(1))._is_nonzero() 

False 

 

""" 

return bool(self.codomain().one()) 

 

class SubcategoryMethods: 

 

def NoZeroDivisors(self): 

""" 

Return the full subcategory of the objects of ``self`` having 

no nonzero zero divisors. 

 

A *zero divisor* in a ring `R` is an element `x \in R` such 

that there exists a nonzero element `y \in R` such that 

`x \cdot y = 0` or `y \cdot x = 0` 

(see :wikipedia:`Zero_divisor`). 

 

EXAMPLES:: 

 

sage: Rings().NoZeroDivisors() 

Category of domains 

 

.. NOTE:: 

 

This could be generalized to 

:class:`MagmasAndAdditiveMagmas.Distributive.AdditiveUnital`. 

 

TESTS:: 

 

sage: TestSuite(Rings().NoZeroDivisors()).run() 

sage: Algebras(QQ).NoZeroDivisors.__module__ 

'sage.categories.rings' 

""" 

return self._with_axiom('NoZeroDivisors') 

 

def Division(self): 

""" 

Return the full subcategory of the division objects of ``self``. 

 

A ring satisfies the *division axiom* if all non-zero 

elements have multiplicative inverses. 

 

.. NOTE:: 

 

This could be generalized to 

:class:`MagmasAndAdditiveMagmas.Distributive.AdditiveUnital`. 

 

EXAMPLES:: 

 

sage: Rings().Division() 

Category of division rings 

sage: Rings().Commutative().Division() 

Category of fields 

 

TESTS:: 

 

sage: TestSuite(Rings().Division()).run() 

sage: Algebras(QQ).Division.__module__ 

'sage.categories.rings' 

""" 

return self._with_axiom('Division') 

 

 

NoZeroDivisors = LazyImport('sage.categories.domains', 'Domains', at_startup=True) 

Division = LazyImport('sage.categories.division_rings', 'DivisionRings', at_startup=True) 

Commutative = LazyImport('sage.categories.commutative_rings', 'CommutativeRings', at_startup=True) 

 

class ParentMethods: 

def is_ring(self): 

""" 

Return True, since this in an object of the category of rings. 

 

EXAMPLES:: 

 

sage: Parent(QQ,category=Rings()).is_ring() 

True 

 

""" 

return True 

 

def is_zero(self): 

""" 

Return ``True`` if this is the zero ring. 

 

EXAMPLES:: 

 

sage: Integers(1).is_zero() 

True 

sage: Integers(2).is_zero() 

False 

sage: QQ.is_zero() 

False 

sage: R.<x> = ZZ[] 

sage: R.quo(1).is_zero() 

True 

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

sage: R.quo(77).is_zero() 

True 

sage: R.quo(x^2+1).is_zero() 

False 

""" 

return self.one() == self.zero() 

 

def bracket(self, x, y): 

""" 

Returns the Lie bracket `[x, y] = x y - y x` of `x` and `y`. 

 

INPUT: 

 

- ``x``, ``y`` -- elements of ``self`` 

 

EXAMPLES:: 

 

sage: F = AlgebrasWithBasis(QQ).example() 

sage: F 

An example of an algebra with basis: the free algebra on the generators ('a', 'b', 'c') over Rational Field 

sage: a,b,c = F.algebra_generators() 

sage: F.bracket(a,b) 

B[word: ab] - B[word: ba] 

 

This measures the default of commutation between `x` and `y`. 

`F` endowed with the bracket operation is a Lie algebra; 

in particular, it satisfies Jacobi's identity:: 

 

sage: F.bracket( F.bracket(a,b), c) + F.bracket(F.bracket(b,c),a) + F.bracket(F.bracket(c,a),b) 

0 

""" 

return x*y - y*x 

 

def _Hom_(self, Y, category): 

r""" 

Returns the homset from ``self`` to ``Y`` in the category ``category`` 

 

INPUT: 

 

- ``Y`` -- a ring 

- ``category`` -- a subcategory of :class:`Rings`() or None 

 

The sole purpose of this method is to construct the homset 

as a :class:`~sage.rings.homset.RingHomset`. If 

``category`` is specified and is not a subcategory of 

:class:`Rings`, a ``TypeError`` is raised instead 

 

This method is not meant to be called directly. Please use 

:func:`sage.categories.homset.Hom` instead. 

 

EXAMPLES:: 

 

sage: H = QQ._Hom_(QQ, category = Rings()); H 

Set of Homomorphisms from Rational Field to Rational Field 

sage: H.__class__ 

<class 'sage.rings.homset.RingHomset_generic_with_category'> 

 

TESTS:: 

 

sage: Hom(QQ, QQ, category = Rings()).__class__ 

<class 'sage.rings.homset.RingHomset_generic_with_category'> 

 

sage: Hom(CyclotomicField(3), QQ, category = Rings()).__class__ 

<class 'sage.rings.number_field.morphism.CyclotomicFieldHomset_with_category'> 

 

sage: TestSuite(Hom(QQ, QQ, category = Rings())).run() # indirect doctest 

 

""" 

if category is not None and not category.is_subcategory(Rings()): 

raise TypeError("%s is not a subcategory of Rings()"%category) 

if Y not in Rings(): 

raise TypeError("%s is not a ring"%Y) 

from sage.rings.homset import RingHomset 

return RingHomset(self, Y, category = category) 

 

# this is already in sage.rings.ring.Ring, 

# but not all rings descend from that class, 

# e.g., matrix spaces. 

def _mul_(self, x, switch_sides=False): 

""" 

Multiplication of rings with, e.g., lists. 

 

NOTE: 

 

This method is used to create ideals. It is 

the same as the multiplication method for 

:class:`~sage.rings.ring.Ring`. However, not 

all parents that belong to the category of 

rings also inherits from the base class of 

rings. Therefore, we implemented a ``__mul__`` 

method for parents, that calls a ``_mul_`` 

method implemented here. See :trac:`7797`. 

 

INPUT: 

 

- `x`, an object to multiply with. 

- `switch_sides` (optional bool): If ``False``, 

the product is ``self*x``; if ``True``, the 

product is ``x*self``. 

 

EXAMPLES: 

 

As we mentioned above, this method is called 

when a ring is involved that does not inherit 

from the base class of rings. This is the case, 

e.g., for matrix algebras:: 

 

sage: MS = MatrixSpace(QQ,2,2) 

sage: isinstance(MS,Ring) 

False 

sage: MS in Rings() 

True 

sage: MS*2 # indirect doctest 

Left Ideal 

( 

[2 0] 

[0 2] 

) 

of Full MatrixSpace of 2 by 2 dense matrices over Rational Field 

 

In the next example, the ring and the other factor switch sides 

in the product:: 

 

sage: [MS.2]*MS 

Right Ideal 

( 

[0 0] 

[1 0] 

) 

of Full MatrixSpace of 2 by 2 dense matrices over Rational Field 

 

AUTHOR: 

 

- Simon King (2011-03-22) 

 

""" 

try: 

if self.is_commutative(): 

return self.ideal(x) 

except (AttributeError,NotImplementedError): 

pass 

try: 

side = x.side() 

except AttributeError: 

return self.ideal(x, side='right' if switch_sides else 'left') 

# presumably x is an ideal... 

try: 

x = x.gens() 

except (AttributeError, NotImplementedError): 

pass # ... not an ideal 

if switch_sides: 

if side in ['right','twosided']: 

return self.ideal(x,side=side) 

elif side=='left': 

return self.ideal(x,side='twosided') 

else: 

if side in ['left','twosided']: 

return self.ideal(x,side=side) 

elif side=='right': 

return self.ideal(x,side='twosided') 

# duck typing failed 

raise TypeError("Don't know how to transform %s into an ideal of %s"%(x,self)) 

 

@cached_method 

def ideal_monoid(self): 

""" 

The monoid of the ideals of this ring. 

 

NOTE: 

 

The code is copied from the base class of rings. 

This is since there are rings that do not inherit 

from that class, such as matrix algebras. See 

:trac:`7797`. 

 

EXAMPLES:: 

 

sage: MS = MatrixSpace(QQ,2,2) 

sage: isinstance(MS,Ring) 

False 

sage: MS in Rings() 

True 

sage: MS.ideal_monoid() 

Monoid of ideals of Full MatrixSpace of 2 by 2 dense matrices 

over Rational Field 

 

Note that the monoid is cached:: 

 

sage: MS.ideal_monoid() is MS.ideal_monoid() 

True 

 

""" 

try: 

from sage.rings.ideal_monoid import IdealMonoid 

return IdealMonoid(self) 

except TypeError: 

from sage.rings.noncommutative_ideals import IdealMonoid_nc 

return IdealMonoid_nc(self) 

 

def characteristic(self): 

""" 

Return the characteristic of this ring. 

 

EXAMPLES:: 

 

sage: QQ.characteristic() 

0 

sage: GF(19).characteristic() 

19 

sage: Integers(8).characteristic() 

8 

sage: Zp(5).characteristic() 

0 

""" 

from sage.rings.infinity import infinity 

from sage.rings.integer_ring import ZZ 

order_1 = self.one().additive_order() 

return ZZ.zero() if order_1 is infinity else order_1 

 

def _test_characteristic(self, **options): 

""" 

Run generic tests on the method :meth:`characteristic`. 

 

See also: :class:`TestSuite`. 

 

EXAMPLES:: 

 

sage: ZZ._test_characteristic() 

""" 

tester = self._tester(**options) 

try: 

characteristic = self.characteristic() 

except AttributeError: 

return # raised when self.one() does not have a additive_order() 

except NotImplementedError: 

return 

 

# test that #12988 is fixed 

from sage.rings.integer import Integer 

tester.assertIsInstance(characteristic, Integer) 

 

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

""" 

Create an ideal of this ring. 

 

NOTE: 

 

The code is copied from the base class 

:class:`~sage.rings.ring.Ring`. This is 

because there are rings that do not inherit 

from that class, such as matrix algebras. 

See :trac:`7797`. 

 

INPUT: 

 

- An element or a list/tuple/sequence of elements. 

- ``coerce`` (optional bool, default ``True``): 

First coerce the elements into this ring. 

- ``side``, optional string, one of ``"twosided"`` 

(default), ``"left"``, ``"right"``: determines 

whether the resulting ideal is twosided, a left 

ideal or a right ideal. 

 

EXAMPLES:: 

 

sage: MS = MatrixSpace(QQ,2,2) 

sage: isinstance(MS,Ring) 

False 

sage: MS in Rings() 

True 

sage: MS.ideal(2) 

Twosided Ideal 

( 

[2 0] 

[0 2] 

) 

of Full MatrixSpace of 2 by 2 dense matrices over Rational Field 

sage: MS.ideal([MS.0,MS.1],side='right') 

Right Ideal 

( 

[1 0] 

[0 0], 

<BLANKLINE> 

[0 1] 

[0 0] 

) 

of Full MatrixSpace of 2 by 2 dense matrices over Rational Field 

 

""" 

if 'coerce' in kwds: 

coerce = kwds['coerce'] 

del kwds['coerce'] 

else: 

coerce = True 

 

from sage.rings.ideal import Ideal_generic 

from types import GeneratorType 

if len(args) == 0: 

gens = [self(0)] 

else: 

gens = args 

while isinstance(gens, (list, tuple, GeneratorType)) and len(gens) == 1: 

first = gens[0] 

if isinstance(first, Ideal_generic): 

R = first.ring() 

m = self.convert_map_from(R) 

if m is not None: 

gens = [m(g) for g in first.gens()] 

coerce = False 

else: 

m = R.convert_map_from(self) 

if m is not None: 

raise NotImplementedError 

else: 

raise TypeError 

break 

elif isinstance(first, (list, tuple, GeneratorType)): 

gens = first 

else: 

try: 

if self.has_coerce_map_from(first): 

gens = first.gens() # we have a ring as argument 

elif isinstance(first, Element): 

gens = [first] 

else: 

raise ArithmeticError("There is no coercion from %s to %s"%(first,self)) 

except TypeError: # first may be a ring element 

pass 

break 

if coerce: 

gens = [self(g) for g in gens] 

from sage.categories.principal_ideal_domains import PrincipalIdealDomains 

if self in PrincipalIdealDomains(): 

# Use GCD algorithm to obtain a principal ideal 

g = gens[0] 

if len(gens) == 1: 

try: 

g = g.gcd(g) # note: we set g = gcd(g, g) to "canonicalize" the generator: make polynomials monic, etc. 

except (AttributeError, NotImplementedError): 

pass 

else: 

for h in gens[1:]: 

g = g.gcd(h) 

gens = [g] 

if 'ideal_class' in kwds: 

C = kwds['ideal_class'] 

del kwds['ideal_class'] 

else: 

C = self._ideal_class_(len(gens)) 

if len(gens) == 1 and isinstance(gens[0], (list, tuple)): 

gens = gens[0] 

return C(self, gens, **kwds) 

 

def _ideal_class_(self,n=0): 

""" 

Return the class that is used to implement ideals of this ring. 

 

NOTE: 

 

We copy the code from :class:`~sage.rings.ring.Ring`. This is 

necessary because not all rings inherit from that class, such 

as matrix algebras. 

 

INPUT: 

 

- ``n`` (optional integer, default 0): The number of generators 

of the ideal to be created. 

 

OUTPUT: 

 

The class that is used to implement ideals of this ring with 

``n`` generators. 

 

NOTE: 

 

Often principal ideals (``n==1``) are implemented via a different 

class. 

 

EXAMPLES:: 

 

sage: MS = MatrixSpace(QQ,2,2) 

sage: MS._ideal_class_() 

<class 'sage.rings.noncommutative_ideals.Ideal_nc'> 

 

We don't know of a commutative ring in Sage that does not inherit 

from the base class of rings. So, we need to cheat in the next 

example:: 

 

sage: super(Ring,QQ)._ideal_class_.__module__ 

'sage.categories.rings' 

sage: super(Ring,QQ)._ideal_class_() 

<class 'sage.rings.ideal.Ideal_generic'> 

sage: super(Ring,QQ)._ideal_class_(1) 

<class 'sage.rings.ideal.Ideal_principal'> 

sage: super(Ring,QQ)._ideal_class_(2) 

<class 'sage.rings.ideal.Ideal_generic'> 

 

""" 

from sage.rings.noncommutative_ideals import Ideal_nc 

try: 

if not self.is_commutative(): 

return Ideal_nc 

except (NotImplementedError,AttributeError): 

return Ideal_nc 

from sage.rings.ideal import Ideal_generic, Ideal_principal 

if n == 1: 

return Ideal_principal 

else: 

return Ideal_generic 

 

## 

# Quotient rings 

# Again, this is defined in sage.rings.ring.pyx 

def quotient(self, I, names=None): 

""" 

Quotient of a ring by a two-sided ideal. 

 

INPUT: 

 

- ``I``: A twosided ideal of this ring. 

- ``names``: a list of strings to be used as names 

for the variables in the quotient ring. 

 

EXAMPLES: 

 

Usually, a ring inherits a method :meth:`sage.rings.ring.Ring.quotient`. 

So, we need a bit of effort to make the following example work with the 

category framework:: 

 

sage: F.<x,y,z> = FreeAlgebra(QQ) 

sage: from sage.rings.noncommutative_ideals import Ideal_nc 

sage: from itertools import product 

sage: class PowerIdeal(Ideal_nc): 

....: def __init__(self, R, n): 

....: self._power = n 

....: Ideal_nc.__init__(self, R, [R.prod(m) for m in product(R.gens(), repeat=n)]) 

....: def reduce(self, x): 

....: R = self.ring() 

....: return add([c*R(m) for m,c in x if len(m) < self._power], R(0)) 

....: 

sage: I = PowerIdeal(F,3) 

sage: Q = Rings().parent_class.quotient(F, I); Q 

Quotient of Free Algebra on 3 generators (x, y, z) over Rational Field by the ideal (x^3, x^2*y, x^2*z, x*y*x, x*y^2, x*y*z, x*z*x, x*z*y, x*z^2, y*x^2, y*x*y, y*x*z, y^2*x, y^3, y^2*z, y*z*x, y*z*y, y*z^2, z*x^2, z*x*y, z*x*z, z*y*x, z*y^2, z*y*z, z^2*x, z^2*y, z^3) 

sage: Q.0 

xbar 

sage: Q.1 

ybar 

sage: Q.2 

zbar 

sage: Q.0*Q.1 

xbar*ybar 

sage: Q.0*Q.1*Q.0 

0 

""" 

from sage.rings.quotient_ring import QuotientRing 

return QuotientRing(self, I, names=names) 

 

def quo(self, I, names=None): 

""" 

Quotient of a ring by a two-sided ideal. 

 

NOTE: 

 

This is a synonym for :meth:`quotient`. 

 

EXAMPLES:: 

 

sage: MS = MatrixSpace(QQ,2) 

sage: I = MS*MS.gens()*MS 

 

``MS`` is not an instance of :class:`~sage.rings.ring.Ring`. 

 

However it is an instance of the parent class of the 

category of rings. The quotient method is inherited from 

there:: 

 

sage: isinstance(MS,sage.rings.ring.Ring) 

False 

sage: isinstance(MS,Rings().parent_class) 

True 

sage: MS.quo(I,names = ['a','b','c','d']) 

Quotient of Full MatrixSpace of 2 by 2 dense matrices over Rational Field by the ideal 

( 

[1 0] 

[0 0], 

<BLANKLINE> 

[0 1] 

[0 0], 

<BLANKLINE> 

[0 0] 

[1 0], 

<BLANKLINE> 

[0 0] 

[0 1] 

) 

 

""" 

return self.quotient(I,names=names) 

 

def quotient_ring(self, I, names=None): 

""" 

Quotient of a ring by a two-sided ideal. 

 

NOTE: 

 

This is a synonyme for :meth:`quotient`. 

 

EXAMPLES:: 

 

sage: MS = MatrixSpace(QQ,2) 

sage: I = MS*MS.gens()*MS 

 

``MS`` is not an instance of :class:`~sage.rings.ring.Ring`, 

but it is an instance of the parent class of the category of 

rings. The quotient method is inherited from there:: 

 

sage: isinstance(MS,sage.rings.ring.Ring) 

False 

sage: isinstance(MS,Rings().parent_class) 

True 

sage: MS.quotient_ring(I,names = ['a','b','c','d']) 

Quotient of Full MatrixSpace of 2 by 2 dense matrices over Rational Field by the ideal 

( 

[1 0] 

[0 0], 

<BLANKLINE> 

[0 1] 

[0 0], 

<BLANKLINE> 

[0 0] 

[1 0], 

<BLANKLINE> 

[0 0] 

[0 1] 

) 

 

""" 

return self.quotient(I,names=names) 

 

def __truediv__(self, I): 

""" 

Since assigning generator names would not work properly, 

the construction of a quotient ring using division syntax 

is not supported. 

 

EXAMPLES:: 

 

sage: MS = MatrixSpace(QQ,2) 

sage: I = MS*MS.gens()*MS 

sage: MS/I 

Traceback (most recent call last): 

... 

TypeError: Use self.quo(I) or self.quotient(I) to construct the quotient ring. 

""" 

raise TypeError("Use self.quo(I) or self.quotient(I) to construct the quotient ring.") 

 

def __getitem__(self, arg): 

""" 

Extend this ring by one or several elements to create a polynomial 

ring, a power series ring, or an algebraic extension. 

 

This is a convenience method intended primarily for interactive 

use. 

 

.. SEEALSO:: 

 

:func:`~sage.rings.polynomial.polynomial_ring_constructor.PolynomialRing`, 

:func:`~sage.rings.power_series_ring.PowerSeriesRing`, 

:meth:`~sage.rings.ring.Ring.extension`, 

:meth:`sage.rings.integer_ring.IntegerRing_class.__getitem__`, 

:meth:`sage.rings.matrix_space.MatrixSpace.__getitem__`, 

:meth:`sage.structure.parent.Parent.__getitem__` 

 

EXAMPLES: 

 

We create several polynomial rings:: 

 

sage: ZZ['x'] 

Univariate Polynomial Ring in x over Integer Ring 

sage: QQ['x'] 

Univariate Polynomial Ring in x over Rational Field 

sage: GF(17)['abc'] 

Univariate Polynomial Ring in abc over Finite Field of size 17 

sage: GF(17)['a,b,c'] 

Multivariate Polynomial Ring in a, b, c over Finite Field of size 17 

sage: GF(17)['a']['b'] 

Univariate Polynomial Ring in b over Univariate Polynomial Ring in a over Finite Field of size 17 

 

We can create skew polynomial rings:: 

 

sage: k.<t> = GF(5^3) 

sage: Frob = k.frobenius_endomorphism() 

sage: k['x',Frob] 

Skew Polynomial Ring in x over Finite Field in t of size 5^3 twisted by t |--> t^5 

 

We can also create power series rings by using double brackets:: 

 

sage: QQ[['t']] 

Power Series Ring in t over Rational Field 

sage: ZZ[['W']] 

Power Series Ring in W over Integer Ring 

 

sage: ZZ[['x,y,z']] 

Multivariate Power Series Ring in x, y, z over Integer Ring 

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

Multivariate Power Series Ring in x, T over Integer Ring 

 

Use :func:`~sage.rings.fraction_field.Frac` or 

:meth:`~sage.rings.ring.CommutativeRing.fraction_field` to obtain 

the fields of rational functions and Laurent series:: 

 

sage: Frac(QQ['t']) 

Fraction Field of Univariate Polynomial Ring in t over Rational Field 

sage: Frac(QQ[['t']]) 

Laurent Series Ring in t over Rational Field 

sage: QQ[['t']].fraction_field() 

Laurent Series Ring in t over Rational Field 

 

Note that the same syntax can be used to create number fields:: 

 

sage: QQ[I] 

Number Field in I with defining polynomial x^2 + 1 

sage: QQ[I].coerce_embedding() 

Generic morphism: 

From: Number Field in I with defining polynomial x^2 + 1 

To: Complex Lazy Field 

Defn: I -> 1*I 

 

:: 

 

sage: QQ[sqrt(2)] 

Number Field in sqrt2 with defining polynomial x^2 - 2 

sage: QQ[sqrt(2)].coerce_embedding() 

Generic morphism: 

From: Number Field in sqrt2 with defining polynomial x^2 - 2 

To: Real Lazy Field 

Defn: sqrt2 -> 1.414213562373095? 

 

:: 

 

sage: QQ[sqrt(2),sqrt(3)] 

Number Field in sqrt2 with defining polynomial x^2 - 2 over its base field 

 

and orders in number fields:: 

 

sage: ZZ[I] 

Order in Number Field in I with defining polynomial x^2 + 1 

sage: ZZ[sqrt(5)] 

Order in Number Field in sqrt5 with defining polynomial x^2 - 5 

sage: ZZ[sqrt(2)+sqrt(3)] 

Order in Number Field in a with defining polynomial x^4 - 10*x^2 + 1 

 

Embeddings are found for simple extensions (when that makes sense):: 

 

sage: QQi.<i> = QuadraticField(-1, 'i') 

sage: QQ[i].coerce_embedding() 

Generic morphism: 

From: Number Field in i with defining polynomial x^2 + 1 

To: Complex Lazy Field 

Defn: i -> 1*I 

 

TESTS: 

 

A few corner cases:: 

 

sage: QQ[()] 

Multivariate Polynomial Ring in no variables over Rational Field 

 

sage: QQ[[]] 

Traceback (most recent call last): 

... 

TypeError: power series rings must have at least one variable 

 

These kind of expressions do not work:: 

 

sage: QQ['a,b','c'] 

Traceback (most recent call last): 

... 

ValueError: variable name 'a,b' is not alphanumeric 

sage: QQ[['a,b','c']] 

Traceback (most recent call last): 

... 

ValueError: variable name 'a,b' is not alphanumeric 

 

sage: QQ[[['x']]] 

Traceback (most recent call last): 

... 

TypeError: expected R[...] or R[[...]], not R[[[...]]] 

 

Extension towers are built as follows and use distinct generator names:: 

 

sage: K = QQ[2^(1/3), 2^(1/2), 3^(1/3)] 

sage: K 

Number Field in a with defining polynomial x^3 - 2 over its base field 

sage: K.base_field() 

Number Field in sqrt2 with defining polynomial x^2 - 2 over its base field 

sage: K.base_field().base_field() 

Number Field in b with defining polynomial x^3 - 3 

 

Embeddings:: 

 

sage: QQ[I](I.pyobject()) 

I 

sage: a = 10^100; expr = (2*a + sqrt(2))/(2*a^2-1) 

sage: QQ[expr].coerce_embedding() is None 

False 

sage: QQ[sqrt(5)].gen() > 0 

True 

sage: expr = sqrt(2) + I*(cos(pi/4, hold=True) - sqrt(2)/2) 

sage: QQ[expr].coerce_embedding() 

Generic morphism: 

From: Number Field in a with defining polynomial x^2 - 2 

To: Real Lazy Field 

Defn: a -> 1.414213562373095? 

""" 

def normalize_arg(arg): 

if isinstance(arg, (tuple, list)): 

# Allowing arbitrary iterables would create confusion, but we 

# may want to support a few more. 

return tuple(arg) 

elif isinstance(arg, str): 

return tuple(arg.split(',')) 

else: 

return (arg,) 

 

# 1. If arg is a list, try to return a power series ring. 

 

if isinstance(arg, list): 

if arg == []: 

raise TypeError("power series rings must have at least one variable") 

elif len(arg) == 1: 

# R[["a,b"]], R[[(a,b)]]... 

if isinstance(arg[0], list): 

raise TypeError("expected R[...] or R[[...]], not R[[[...]]]") 

elts = normalize_arg(arg[0]) 

else: 

elts = normalize_arg(arg) 

from sage.rings.power_series_ring import PowerSeriesRing 

return PowerSeriesRing(self, elts) 

 

if isinstance(arg, tuple): 

from sage.categories.morphism import Morphism 

if len(arg) == 2 and isinstance(arg[1], Morphism): 

from sage.rings.polynomial.skew_polynomial_ring_constructor import SkewPolynomialRing 

return SkewPolynomialRing(self, arg[1], names=arg[0]) 

 

# 2. Otherwise, if all specified elements are algebraic, try to 

# return an algebraic extension 

 

elts = normalize_arg(arg) 

 

try: 

minpolys = [a.minpoly() for a in elts] 

except (AttributeError, NotImplementedError, ValueError, TypeError): 

minpolys = None 

 

if minpolys: 

# how to pass in names? 

names = tuple(_gen_names(elts)) 

if len(elts) == 1: 

from sage.rings.all import CIF, CLF, RIF, RLF 

elt = elts[0] 

try: 

iv = CIF(elt) 

except (TypeError, ValueError): 

emb = None 

else: 

# First try creating an ANRoot manually, because 

# extension(..., embedding=CLF(expr)) (or 

# ...QQbar(expr)) would normalize the expression in 

# QQbar, which currently is VERY slow in many cases. 

# This may fail when minpoly has close roots or elt is 

# a complicated symbolic expression. 

# TODO: Rewrite using #19362 and/or #17886 and/or 

# #15600 once those issues are solved. 

from sage.rings.qqbar import AlgebraicNumber, ANRoot 

try: 

elt = AlgebraicNumber(ANRoot(minpolys[0], iv)) 

except ValueError: 

pass 

# Force a real embedding when possible, to get the 

# right ordered ring structure. 

if (iv.imag().is_zero() or iv.imag().contains_zero() 

and elt.imag().is_zero()): 

emb = RLF(elt) 

else: 

emb = CLF(elt) 

return self.extension(minpolys[0], names[0], embedding=emb) 

try: 

# Doing the extension all at once is best, if possible... 

return self.extension(minpolys, names) 

except (TypeError, ValueError): 

# ...but we can also construct it iteratively 

return reduce(lambda R, ext: R.extension(*ext), zip(minpolys, names), self) 

 

# 2. Otherwise, try to return a polynomial ring 

 

from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing 

return PolynomialRing(self, elts) 

 

class ElementMethods: 

def is_unit(self): 

r""" 

Return whether this element is a unit in the ring. 

 

.. NOTE:: 

 

This is a generic implementation for (non-commutative) rings 

which only works for the one element, its additive inverse, and 

the zero element. Most rings should provide a more specialized 

implementation. 

 

EXAMPLES:: 

 

sage: MS = MatrixSpace(ZZ, 2) 

sage: MS.one().is_unit() 

True 

sage: MS.zero().is_unit() 

False 

sage: MS([1,2,3,4]).is_unit() 

False 

""" 

if self.is_one() or (-self).is_one(): 

return True 

if self.is_zero(): # now 0 != 1 

return False 

raise NotImplementedError 

 

def inverse_of_unit(self): 

r""" 

Return the inverse of this element if it is a unit. 

 

OUTPUT: 

 

An element in the same ring as this element. 

 

EXAMPLES:: 

 

sage: R.<x> = ZZ[] 

sage: S = R.quo(x^2 + x + 1) 

sage: S(1).inverse_of_unit() 

1 

 

This method fails when the element is not a unit:: 

 

sage: 2.inverse_of_unit() 

Traceback (most recent call last): 

... 

ArithmeticError: inverse does not exist 

 

The inverse returned is in the same ring as this element:: 

 

sage: a = -1 

sage: a.parent() 

Integer Ring 

sage: a.inverse_of_unit().parent() 

Integer Ring 

 

Note that this is often not the case when computing inverses in other ways:: 

 

sage: (~a).parent() 

Rational Field 

sage: (1/a).parent() 

Rational Field 

 

""" 

try: 

if not self.is_unit(): 

raise ArithmeticError("element is not a unit") 

except NotImplementedError: 

# if an element does not implement is_unit, we just try to 

# invert it anyway; if the result is in the ring again, it was 

# a unit 

pass 

 

inverse = ~self 

if inverse not in self.parent(): 

raise ArithmeticError("element is not a unit") 

 

# return the inverse (with the correct parent) 

return self.parent()(inverse) 

 

def _divide_if_possible(self, y): 

""" 

Divide ``self`` by ``y`` if possible and raise a 

``ValueError`` otherwise. 

 

EXAMPLES:: 

 

sage: 4._divide_if_possible(2) 

2 

sage: _.parent() 

Integer Ring 

 

:: 

 

sage: 4._divide_if_possible(3) 

Traceback (most recent call last): 

... 

ValueError: 4 is not divisible by 3 

""" 

q, r = self.quo_rem(y) 

if r != 0: 

raise ValueError("%s is not divisible by %s"%(self, y)) 

return q 

 

def _gen_names(elts): 

r""" 

Used to find a name for a generator when rings are created using the 

``__getitem__`` syntax, e.g. ``ZZ['x']``, ``ZZ[sqrt(2)]``. 

 

EXAMPLES:: 

 

sage: from sage.categories.rings import _gen_names 

sage: list(_gen_names([sqrt(5)])) 

['sqrt5'] 

sage: list(_gen_names([sqrt(-17), 2^(1/3)])) 

['a', 'b'] 

sage: list(_gen_names((1..27)))[-1] 

'aa' 

""" 

import re 

from sage.structure.category_object import certify_names 

from sage.combinat.words.words import Words 

it = iter(Words("abcdefghijklmnopqrstuvwxyz", infinite=False)) 

next(it) # skip empty word 

for x in elts: 

name = str(x) 

m = re.match('^sqrt\((\d+)\)$', name) 

if m: 

name = "sqrt%s" % m.groups()[0] 

try: 

certify_names([name]) 

except ValueError: 

name = next(it).string_rep() 

yield name