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

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

r""" 

Factorizations 

 

The :class:`Factorization` class provides a structure for holding quite 

general lists of objects with integer multiplicities. These may hold 

the results of an arithmetic or algebraic factorization, where the 

objects may be primes or irreducible polynomials and the 

multiplicities are the (non-zero) exponents in the factorization. For 

other types of examples, see below. 

 

:class:`Factorization` class objects contain a ``list``, so can be 

printed nicely and be manipulated like a list of prime-exponent pairs, 

or easily turned into a plain list. For example, we factor the 

integer `-45`:: 

 

sage: F = factor(-45) 

 

This returns an object of type :class:`Factorization`:: 

 

sage: type(F) 

<class 'sage.structure.factorization_integer.IntegerFactorization'> 

 

It prints in a nice factored form:: 

 

sage: F 

-1 * 3^2 * 5 

 

There is an underlying list representation, which ignores the unit part:: 

 

sage: list(F) 

[(3, 2), (5, 1)] 

 

A :class:`Factorization` is not actually a list:: 

 

sage: isinstance(F, list) 

False 

 

However, we can access the :class:`Factorization` F itself as if it were a list:: 

 

sage: F[0] 

(3, 2) 

sage: F[1] 

(5, 1) 

 

To get at the unit part, use the :meth:`Factorization.unit` function:: 

 

sage: F.unit() 

-1 

 

All factorizations are immutable, up to ordering with ``sort()`` and 

simplifying with ``simplify()``. Thus if you write a function that 

returns a cached version of a factorization, you do not have to return 

a copy. 

 

:: 

 

sage: F = factor(-12); F 

-1 * 2^2 * 3 

sage: F[0] = (5,4) 

Traceback (most recent call last): 

... 

TypeError: 'Factorization' object does not support item assignment 

 

EXAMPLES: 

 

This more complicated example involving polynomials also illustrates 

that the unit part is not discarded from factorizations:: 

 

sage: x = QQ['x'].0 

sage: f = -5*(x-2)*(x-3) 

sage: f 

-5*x^2 + 25*x - 30 

sage: F = f.factor(); F 

(-5) * (x - 3) * (x - 2) 

sage: F.unit() 

-5 

sage: F.value() 

-5*x^2 + 25*x - 30 

 

The underlying list is the list of pairs `(p_i, e_i)`, where each 

`p_i` is a 'prime' and each `e_i` is an integer. The unit part 

is discarded by the list:: 

 

sage: list(F) 

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

sage: len(F) 

2 

sage: F[1] 

(x - 2, 1) 

 

In the ring `\ZZ[x]`, the integer `-5` is not a unit, so the 

factorization has three factors:: 

 

sage: x = ZZ['x'].0 

sage: f = -5*(x-2)*(x-3) 

sage: f 

-5*x^2 + 25*x - 30 

sage: F = f.factor(); F 

(-1) * 5 * (x - 3) * (x - 2) 

sage: F.universe() 

Univariate Polynomial Ring in x over Integer Ring 

sage: F.unit() 

-1 

sage: list(F) 

[(5, 1), (x - 3, 1), (x - 2, 1)] 

sage: F.value() 

-5*x^2 + 25*x - 30 

sage: len(F) 

3 

 

On the other hand, -1 is a unit in `\ZZ`, so it is included in the unit:: 

 

sage: x = ZZ['x'].0 

sage: f = -1*(x-2)*(x-3) 

sage: F = f.factor(); F 

(-1) * (x - 3) * (x - 2) 

sage: F.unit() 

-1 

sage: list(F) 

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

 

Factorizations can involve fairly abstract mathematical objects:: 

 

sage: F = ModularSymbols(11,4).factorization() 

sage: F 

(Modular Symbols subspace of dimension 2 of Modular Symbols space of dimension 6 for Gamma_0(11) of weight 4 with sign 0 over Rational Field) * 

(Modular Symbols subspace of dimension 2 of Modular Symbols space of dimension 6 for Gamma_0(11) of weight 4 with sign 0 over Rational Field) * 

(Modular Symbols subspace of dimension 2 of Modular Symbols space of dimension 6 for Gamma_0(11) of weight 4 with sign 0 over Rational Field) 

sage: type(F) 

<class 'sage.structure.factorization.Factorization'> 

 

 

sage: K.<a> = NumberField(x^2 + 3); K 

Number Field in a with defining polynomial x^2 + 3 

sage: f = K.factor(15); f 

(Fractional ideal (-a))^2 * (Fractional ideal (5)) 

sage: f.universe() 

Monoid of ideals of Number Field in a with defining polynomial x^2 + 3 

sage: f.unit() 

Fractional ideal (1) 

sage: g=K.factor(9); g 

(Fractional ideal (-a))^4 

sage: f.lcm(g) 

(Fractional ideal (-a))^4 * (Fractional ideal (5)) 

sage: f.gcd(g) 

(Fractional ideal (-a))^2 

sage: f.is_integral() 

True 

 

TESTS:: 

 

sage: F = factor(-20); F 

-1 * 2^2 * 5 

sage: G = loads(dumps(F)); G 

-1 * 2^2 * 5 

sage: G == F 

True 

sage: G is F 

False 

 

AUTHORS: 

 

- William Stein (2006-01-22): added unit part as suggested by David Kohel. 

 

- William Stein (2008-01-17): wrote much of the documentation and 

fixed a couple of bugs. 

 

- Nick Alexander (2008-01-19): added support for non-commuting factors. 

 

- John Cremona (2008-08-22): added division, lcm, gcd, is_integral and 

universe functions 

""" 

 

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

# 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 six.moves import range 

from six import iteritems, integer_types 

 

from sage.structure.sage_object import SageObject 

from sage.structure.element import Element 

from sage.structure.sequence import Sequence 

from sage.structure.richcmp import richcmp_method, richcmp, richcmp_not_equal 

from sage.rings.integer import Integer 

from sage.misc.all import prod 

from sage.misc.cachefunc import cached_method 

 

 

 

@richcmp_method 

class Factorization(SageObject): 

""" 

A formal factorization of an object. 

 

EXAMPLES:: 

 

sage: N = 2006 

sage: F = N.factor(); F 

2 * 17 * 59 

sage: F.unit() 

1 

sage: F = factor(-2006); F 

-1 * 2 * 17 * 59 

sage: F.unit() 

-1 

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

True 

sage: F = Factorization([(x,1/3)]) 

Traceback (most recent call last): 

... 

TypeError: no conversion of this rational to integer 

""" 

def __init__(self, x, unit=None, cr=False, sort=True, simplify=True): 

""" 

Create a :class:`Factorization` object. 

 

INPUT: 

 

- ``x`` - a list of pairs (p, e) with e an integer; 

otherwise a TypeError is raised 

 

- ``unit`` - (default: 1) the unit part of the factorization. 

 

- ``cr`` - (default: False) if True, print the factorization with 

carriage returns between factors. 

 

- ``sort`` - (default: True) if True, sort the factors by calling 

the sort function ``self.sort()`` after creating the factorization 

 

- ``simplify`` - (default: True) if True, remove duplicate 

factors from the factorization. See the documentation for 

self.simplify. 

 

OUTPUT: 

 

- a Factorization object 

 

EXAMPLES: 

 

We create a factorization with all the default options:: 

 

sage: Factorization([(2,3), (5, 1)]) 

2^3 * 5 

 

We create a factorization with a specified unit part:: 

 

sage: Factorization([(2,3), (5, 1)], unit=-1) 

-1 * 2^3 * 5 

 

We try to create a factorization but with a string an exponent, which 

results in a TypeError:: 

 

sage: Factorization([(2,3), (5, 'x')]) 

Traceback (most recent call last): 

... 

TypeError: unable to convert 'x' to an integer 

 

We create a factorization that puts newlines after each multiply sign 

when printing. This is mainly useful when the primes are large:: 

 

sage: Factorization([(2,3), (5, 2)], cr=True) 

2^3 * 

5^2 

 

Another factorization with newlines and nontrivial unit part, which 

appears on a line by itself:: 

 

sage: Factorization([(2,3), (5, 2)], cr=True, unit=-2) 

-2 * 

2^3 * 

5^2 

 

A factorization, but where we do not sort the factors:: 

 

sage: Factorization([(5,3), (2, 3)], sort=False) 

5^3 * 2^3 

 

By default, in the commutative case, factorizations are sorted by the 

prime base:: 

 

sage: Factorization([(2, 7), (5,2), (2, 5)]) 

2^12 * 5^2 

sage: R.<a,b> = FreeAlgebra(QQ,2) 

sage: Factorization([(a,1),(b,1),(a,2)]) 

a * b * a^2 

 

Autosorting (the default) swaps around the factors below:: 

 

sage: F = Factorization([(ZZ^3, 2), (ZZ^2, 5)], cr=True); F 

(Ambient free module of rank 2 over the principal ideal domain Integer Ring)^5 * 

(Ambient free module of rank 3 over the principal ideal domain Integer Ring)^2 

""" 

x = [(p, Integer(e)) for (p, e) in x] 

 

try: 

self.__universe = Sequence(t[0] for t in x).universe() 

except TypeError: 

self.__universe = None 

 

self.__x = [(t[0], int(t[1])) for t in x] 

if unit is None: 

if x: 

try: 

unit = self.__universe(1) 

except (AttributeError, TypeError): 

unit = Integer(1) 

else: 

unit = Integer(1) 

self.__unit = unit 

self.__cr = cr 

if sort and self.is_commutative(): 

self.sort() 

if simplify: 

self.simplify() 

 

def __getitem__(self, i): 

""" 

Return `i^{th}` factor of self. 

 

EXAMPLES:: 

 

sage: a = factor(-75); a 

-1 * 3 * 5^2 

sage: a[0] 

(3, 1) 

sage: a[1] 

(5, 2) 

sage: a[-1] 

(5, 2) 

sage: a[5] 

Traceback (most recent call last): 

... 

IndexError: list index out of range 

""" 

return self.__x[i] 

 

def __setitem__(self, i, v): 

""" 

Set the `i^{th}` factor of self. 

 

.. warning:: 

 

NOT ALLOWED -- Factorizations are immutable. 

 

EXAMPLES:: 

 

sage: a = factor(-75); a 

-1 * 3 * 5^2 

sage: a[0] = (2,3) 

Traceback (most recent call last): 

... 

TypeError: 'Factorization' object does not support item assignment 

""" 

raise TypeError("'Factorization' object does not support item assignment") 

 

def __len__(self): 

""" 

Return the number of prime factors of self, not counting 

the unit part. 

 

EXAMPLES:: 

 

sage: len(factor(15)) 

2 

 

Note that the unit part is not included in the count:: 

 

sage: a = factor(-75); a 

-1 * 3 * 5^2 

sage: len(a) 

2 

sage: list(a) 

[(3, 1), (5, 2)] 

sage: len(list(a)) 

2 

""" 

return len(self.__x) 

 

def __richcmp__(self, other, op): 

""" 

Compare ``self`` and ``other``. 

 

This first compares the values. 

 

If values are equal, this compares the units. 

 

If units are equal, this compares the underlying lists of 

``self`` and ``other``. 

 

EXAMPLES: 

 

We compare two contrived formal factorizations:: 

 

sage: a = Factorization([(2, 7), (5,2), (2, 5)]) 

sage: b = Factorization([(2, 7), (5,10), (7, 3)]) 

sage: a 

2^12 * 5^2 

sage: b 

2^7 * 5^10 * 7^3 

sage: a < b 

True 

sage: b < a 

False 

sage: a.value() 

102400 

sage: b.value() 

428750000000 

 

We compare factorizations of some polynomials:: 

 

sage: x = polygen(QQ) 

sage: x^2 - 1 > x^2 - 4 

True 

sage: factor(x^2 - 1) > factor(x^2 - 4) 

True 

""" 

if not isinstance(other, Factorization): 

return NotImplemented 

 

lx = self.value() 

rx = other.value() 

if lx != rx: 

return richcmp_not_equal(lx, rx, op) 

 

lx = self.__unit 

rx = other.__unit 

if lx != rx: 

return richcmp_not_equal(lx, rx, op) 

 

return richcmp(self.__x, other.__x, op) 

 

def __copy__(self): 

r""" 

Return a copy of self. 

 

This is *not* a deepcopy -- only references to the factors are 

returned, not copies of them. Use ``deepcopy(self)`` if you need 

a deep copy of self. 

 

EXAMPLES: 

 

We create a factorization that has mutable primes:: 

 

sage: F = Factorization([([1,2], 5), ([5,6], 10)]); F 

([1, 2])^5 * ([5, 6])^10 

 

We make a copy of it:: 

 

sage: G = copy(F); G 

([1, 2])^5 * ([5, 6])^10 

sage: G is F 

False 

 

Note that if we change one of the mutable "primes" of F, this does 

change G:: 

 

sage: F[1][0][0] = 'hello' 

sage: G 

([1, 2])^5 * (['hello', 6])^10 

""" 

# No need to sort, since the factorization is already sorted 

# in whatever order is desired. 

return Factorization(self.__x, unit=self.__unit, cr=self.__cr, 

sort=False, simplify=False) 

 

def __deepcopy__(self, memo): 

r""" 

Return a deep copy of self. 

 

EXAMPLES: 

 

We make a factorization that has mutable entries:: 

 

sage: F = Factorization([([1,2], 5), ([5,6], 10)]); F 

([1, 2])^5 * ([5, 6])^10 

 

Now we make a copy of it and a deep copy:: 

 

sage: K = copy(F) 

sage: G = deepcopy(F); G 

([1, 2])^5 * ([5, 6])^10 

 

We change one of the mutable entries of F:: 

 

sage: F[0][0][0] = 10 

 

This of course changes F:: 

 

sage: F 

([10, 2])^5 * ([5, 6])^10 

 

It also changes the copy K of F:: 

 

sage: K 

([10, 2])^5 * ([5, 6])^10 

 

It does *not* change the deep copy G:: 

 

sage: G 

([1, 2])^5 * ([5, 6])^10 

""" 

import copy 

return Factorization(copy.deepcopy(list(self), memo), 

cr=self.__cr, sort=False, simplify=False) 

 

def universe(self): 

r""" 

Return the parent structure of my factors. 

 

.. note:: 

 

This used to be called ``base_ring``, but the universe 

of a factorization need not be a ring. 

 

EXAMPLES:: 

 

sage: F = factor(2006) 

sage: F.universe() 

Integer Ring 

 

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

sage: F = Factorization([(z, 2)], 3) 

sage: (F*F^-1).universe() 

Free Algebra on 3 generators (x, y, z) over Rational Field 

 

sage: F = ModularSymbols(11,4).factorization() 

sage: F.universe() 

""" 

try: 

return self.__universe 

except AttributeError: 

return None 

 

def base_change(self, U): 

""" 

Return the factorization self, with its factors (including the 

unit part) coerced into the universe `U`. 

 

EXAMPLES:: 

 

sage: F = factor(2006) 

sage: F.universe() 

Integer Ring 

sage: P.<x> = ZZ[] 

sage: F.base_change(P).universe() 

Univariate Polynomial Ring in x over Integer Ring 

 

This method will return a TypeError if the coercion is not 

possible:: 

 

sage: g = x^2 - 1 

sage: F = factor(g); F 

(x - 1) * (x + 1) 

sage: F.universe() 

Univariate Polynomial Ring in x over Integer Ring 

sage: F.base_change(ZZ) 

Traceback (most recent call last): 

... 

TypeError: Impossible to coerce the factors of (x - 1) * (x + 1) into Integer Ring 

""" 

if len(self) == 0: 

return self 

try: 

return Factorization([(U(f[0]), f[1]) for f in list(self)], unit=U(self.unit())) 

except TypeError: 

raise TypeError("Impossible to coerce the factors of %s into %s"%(self, U)) 

 

def is_commutative(self): 

""" 

Return True if my factors commute. 

 

EXAMPLES:: 

 

sage: F = factor(2006) 

sage: F.is_commutative() 

True 

sage: K = QuadraticField(23, 'a') 

sage: F = K.factor(13) 

sage: F.is_commutative() 

True 

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

sage: F = Factorization([(z, 2)], 3) 

sage: F.is_commutative() 

False 

sage: (F*F^-1).is_commutative() 

False 

""" 

try: 

return self.universe().is_commutative() 

except Exception: 

# This is not the mathematically correct default, but agrees with 

# history -- we've always assumed factored things commute 

return True 

 

def _set_cr(self, cr): 

""" 

Change whether or not the factorization is printed with 

carriage returns after each factor. 

 

EXAMPLES:: 

 

sage: x = polygen(QQ,'x') 

sage: F = factor(x^6 - 1); F 

(x - 1) * (x + 1) * (x^2 - x + 1) * (x^2 + x + 1) 

sage: F._set_cr(True); F 

(x - 1) * 

(x + 1) * 

(x^2 - x + 1) * 

(x^2 + x + 1) 

sage: F._set_cr(False); F 

(x - 1) * (x + 1) * (x^2 - x + 1) * (x^2 + x + 1) 

""" 

self.__cr = bool(cr) 

 

def simplify(self): 

""" 

Combine adjacent products as much as possible. 

 

TESTS:: 

 

sage: R.<x,y> = FreeAlgebra(ZZ, 2) 

sage: F = Factorization([(x,3), (y, 2), (y,2)], simplify=False); F 

x^3 * y^2 * y^2 

sage: F.simplify(); F 

x^3 * y^4 

sage: F * Factorization([(y, -2)], 2) 

(2) * x^3 * y^2 

""" 

repeat = False 

simp = [] 

import itertools 

for obj, agroup in itertools.groupby(list(self), lambda x: x[0]): 

xs = list(agroup) 

if len(xs) > 1: 

repeat = True 

n = sum([x[1] for x in xs]) 

if n != 0: 

simp.append((obj, n)) 

self.__x[0:] = simp 

if repeat: 

self.simplify() 

 

def sort(self, key=None): 

r""" 

Sort the factors in this factorization. 

 

INPUT: 

 

- ``key`` - (default: ``None``) comparison key 

 

OUTPUT: 

 

- changes this factorization to be sorted (inplace) 

 

If ``key`` is ``None``, we determine the comparison key as 

follows: 

 

If the prime in the first factor has a dimension 

method, then we sort based first on *dimension* then on 

the exponent. 

 

If there is no dimension method, we next 

attempt to sort based on a degree method, in which case, we 

sort based first on *degree*, then exponent to break ties 

when two factors have the same degree, and if those match 

break ties based on the actual prime itself. 

 

Otherwise, we sort according to the prime itself. 

 

EXAMPLES: 

 

We create a factored polynomial:: 

 

sage: x = polygen(QQ,'x') 

sage: F = factor(x^3 + 1); F 

(x + 1) * (x^2 - x + 1) 

 

We sort it by decreasing degree:: 

 

sage: F.sort(key=lambda x:(-x[0].degree(), x)) 

sage: F 

(x^2 - x + 1) * (x + 1) 

""" 

if len(self) == 0: 

return 

 

if key is not None: 

self.__x.sort(key=key) 

return 

 

a = self.__x[0][0] 

sort_key = None 

if hasattr(a, 'dimension'): 

try: 

a.dimension() 

 

def sort_key(f): 

return (f[0].dimension(), f[1], f[0]) 

except (AttributeError, NotImplementedError, TypeError): 

pass 

elif hasattr(a, 'degree'): 

try: 

a.degree() 

 

def sort_key(f): 

return (f[0].degree(), f[1], f[0]) 

except (AttributeError, NotImplementedError, TypeError): 

pass 

 

if sort_key is None: 

 

def sort_key(f): 

return f[0] 

 

self.__x.sort(key=sort_key) 

 

def unit(self): 

r""" 

Return the unit part of this factorization. 

 

EXAMPLES: 

 

We create a polynomial over the real double field and factor it:: 

 

sage: x = polygen(RDF, 'x') 

sage: F = factor(-2*x^2 - 1); F 

(-2.0) * (x^2 + 0.5000000000000001) 

 

Note that the unit part of the factorization is `-2.0`:: 

 

sage: F.unit() 

-2.0 

 

sage: F = factor(-2006); F 

-1 * 2 * 17 * 59 

sage: F.unit() 

-1 

""" 

return self.__unit 

 

def _cr(self): 

""" 

Return whether or not factorizations are printed with carriage 

returns between factors. 

 

EXAMPLES: 

 

Our first example involves factoring an integer:: 

 

sage: F = factor(-93930); F 

-1 * 2 * 3 * 5 * 31 * 101 

sage: F._cr() 

False 

sage: F._set_cr(True) 

sage: F._cr() 

True 

 

This of course looks funny:: 

 

sage: F 

-1 * 

2 * 

3 * 

5 * 

31 * 

101 

 

Next we factor a modular symbols space:: 

 

sage: F = ModularSymbols(11).factor(); F 

(Modular Symbols subspace of dimension 1 of ...) * 

(Modular Symbols subspace of dimension 1 of ...) * 

(Modular Symbols subspace of dimension 1 of ...) 

""" 

try: 

return self.__cr 

except AttributeError: 

self.__cr = False 

return False 

 

def _repr_(self): 

""" 

Return the string representation of this factorization. 

 

EXAMPLES:: 

 

sage: f = factor(-100); f 

-1 * 2^2 * 5^2 

sage: f._repr_() 

'-1 * 2^2 * 5^2' 

 

Note that the default printing of a factorization can be overloaded 

using the rename method:: 

 

sage: f.rename('factorization of -100') 

sage: f 

factorization of -100 

 

However _repr_ always prints normally:: 

 

sage: f._repr_() 

'-1 * 2^2 * 5^2' 

 

EXAMPLES:: 

 

sage: x = polygen(QQ) 

sage: Factorization([(x-1,1), (x-2,2)]) 

(x - 1) * (x - 2)^2 

sage: Factorization([(x + 1, -3)]) 

(x + 1)^-3 

""" 

cr = self._cr() 

if len(self) == 0: 

return repr(self.__unit) 

s = '' 

mul = ' * ' 

if cr: 

mul += '\n' 

x = self.__x[0][0] 

try: 

atomic = (isinstance(x, integer_types) or 

self.universe()._repr_option('element_is_atomic')) 

except AttributeError: 

atomic = False 

 

if isinstance(x, Element): 

one = x.parent()(1) 

else: 

one = 1 

 

for i in range(len(self)): 

t = repr(self.__x[i][0]) 

n = self.__x[i][1] 

if not atomic and (n != 1 or len(self) > 1 or self.__unit != one): 

if '+' in t or '-' in t or ' ' in t: 

t = '(%s)'%t 

if n != 1: 

t += '^%s'%n 

s += t 

if i < len(self)-1: 

s += mul 

if self.__unit != one: 

if atomic: 

u = repr(self.__unit) 

else: 

u = '(%s)'%self.__unit 

s = u + mul + s 

return s 

 

def _latex_(self): 

r""" 

Return the LaTeX representation of this factorization. 

 

EXAMPLES:: 

 

sage: f = factor(-100); f 

-1 * 2^2 * 5^2 

sage: latex(f) 

-1 \cdot 2^{2} \cdot 5^{2} 

sage: f._latex_() 

'-1 \\cdot 2^{2} \\cdot 5^{2}' 

sage: x = AA['x'].0; factor(x^2 + x + 1)._latex_() # trac 12178 

'(x^{2} + x + 1.000000000000000?)' 

""" 

if len(self) == 0: 

return self.__unit._latex_() 

try: 

atomic = (isinstance(self.__x[0][0], integer_types) or 

self.universe()._repr_option('element_is_atomic')) 

except AttributeError: 

atomic = False 

s = '' 

for i in range(len(self)): 

t = self.__x[i][0]._latex_() 

if not atomic and ('+' in t or '-' in t or ' ' in t): 

t = '(%s)'%t 

n = self.__x[i][1] 

if n != 1: 

t += '^{%s}'%n 

s += t 

if i < len(self)-1: 

s += ' \\cdot ' 

if self.__unit != 1: 

if atomic: 

u = self.__unit._latex_() 

else: 

u = '\\left(%s\\right)'%self.__unit._latex_() 

s = u + ' \\cdot ' + s 

return s 

 

@cached_method 

def __pari__(self): 

""" 

Return the PARI factorization matrix corresponding to ``self``. 

 

EXAMPLES:: 

 

sage: f = factor(-24) 

sage: pari(f) 

[-1, 1; 2, 3; 3, 1] 

 

sage: R.<x> = QQ[] 

sage: g = factor(x^10 - 1) 

sage: pari(g) 

[x - 1, 1; x + 1, 1; x^4 - x^3 + x^2 - x + 1, 1; x^4 + x^3 + x^2 + x + 1, 1] 

 

""" 

from sage.libs.pari.all import pari 

from itertools import chain 

 

n = len(self) 

if self.__unit == 1: 

init = () 

else: 

init = (self.__unit, 1) 

n += 1 

# concatenate (p, e) tuples 

entries = init + tuple(chain.from_iterable(self)) 

return pari.matrix(n, 2, entries) 

 

def __add__(self, other): 

""" 

Return the (unfactored) sum of self and other. 

 

EXAMPLES:: 

 

sage: factor(-10) + 16 

6 

sage: factor(10) - 16 

-6 

sage: factor(100) + factor(19) 

119 

""" 

if isinstance(other, Factorization): 

other = other.value() 

return self.value() + other 

 

def __sub__(self, other): 

""" 

Return the (unfactored) difference of self and other. 

 

EXAMPLES:: 

 

sage: factor(-10) + 16 

6 

sage: factor(10) - 16 

-6 

""" 

if isinstance(other, Factorization): 

other = other.value() 

return self.value() - other 

 

def __radd__(self, left): 

""" 

Return the (unfactored) sum of self and left. 

 

EXAMPLES:: 

 

sage: 16 + factor(-10) 

6 

""" 

return self.value() + left 

 

 

def __rsub__(self, left): 

""" 

Return the (unfactored) difference of left and self. 

 

EXAMPLES:: 

 

sage: 16 - factor(10) 

6 

""" 

return left - self.value() 

 

def __neg__(self): 

""" 

Return negative of this factorization. 

 

EXAMPLES:: 

 

sage: a = factor(-75); a 

-1 * 3 * 5^2 

sage: -a 

3 * 5^2 

sage: (-a).unit() 

1 

""" 

unit = -self.__unit 

return Factorization(list(self), unit, self.__cr, 

sort=False, simplify=False) 

 

def __rmul__(self, left): 

""" 

Return the product left * self, where left is not a Factorization. 

 

EXAMPLES:: 

 

sage: a = factor(15); a 

3 * 5 

sage: -2 * a 

-2 * 3 * 5 

sage: a * -2 

-2 * 3 * 5 

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

sage: f = Factorization([(x,2),(y,3)]); f 

x^2 * y^3 

sage: x * f 

x^3 * y^3 

sage: f * x 

x^2 * y^3 * x 

 

Note that this does not automatically factor ``left``:: 

 

sage: F = Factorization([(5,3), (2,3)]) 

sage: 46 * F 

2^3 * 5^3 * 46 

""" 

return Factorization([(left, 1)]) * self 

 

def __mul__(self, other): 

r""" 

Return the product of two factorizations, which is obtained by 

combining together like factors. 

 

If the two factorizations have different universes, this 

method will attempt to find a common universe for the 

product. A TypeError is raised if this is impossible. 

 

EXAMPLES:: 

 

sage: factor(-10) * factor(-16) 

2^5 * 5 

sage: factor(-10) * factor(16) 

-1 * 2^5 * 5 

 

sage: R.<x,y> = FreeAlgebra(ZZ, 2) 

sage: F = Factorization([(x,3), (y, 2), (x,1)]); F 

x^3 * y^2 * x 

sage: F*F 

x^3 * y^2 * x^4 * y^2 * x 

sage: -1 * F 

(-1) * x^3 * y^2 * x 

 

sage: P.<x> = ZZ[] 

sage: f = 2*x + 2 

sage: c = f.content(); g = f//c 

sage: Fc = factor(c); Fc.universe() 

Integer Ring 

sage: Fg = factor(g); Fg.universe() 

Univariate Polynomial Ring in x over Integer Ring 

sage: F = Fc * Fg; F.universe() 

Univariate Polynomial Ring in x over Integer Ring 

sage: [type(a[0]) for a in F] 

[<... 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint'>, 

<... 'sage.rings.polynomial.polynomial_integer_dense_flint.Polynomial_integer_dense_flint'>] 

""" 

if not isinstance(other, Factorization): 

return self * Factorization([(other, 1)]) 

 

if len(self) and len(other): 

try: 

# since self is a factorization, all its factors 

# are in the same universe. 

# the same is true for the factorization other. 

# so if we want to put the factorizations together we just 

# need to find a common universe for the first factor of 

# self and the first factor of other 

U = Sequence([self[0][0], other[0][0]]).universe() 

self = self.base_change(U) 

other = other.base_change(U) 

except TypeError: 

raise TypeError("Cannot multiply %s and %s because they cannot be coerced into a common universe"%(self,other)) 

 

if self.is_commutative() and other.is_commutative(): 

d1 = dict(self) 

d2 = dict(other) 

s = {} 

for a in set(d1).union(set(d2)): 

s[a] = d1.get(a,0) + d2.get(a,0) 

return Factorization(list(iteritems(s)), unit=self.unit()*other.unit()) 

else: 

return Factorization(list(self) + list(other), unit=self.unit()*other.unit()) 

 

def __pow__(self, n): 

""" 

Return the `n^{th}` power of a factorization, which is got by 

combining together like factors. 

 

EXAMPLES:: 

 

sage: f = factor(-100); f 

-1 * 2^2 * 5^2 

sage: f^3 

-1 * 2^6 * 5^6 

sage: f^4 

2^8 * 5^8 

 

sage: K.<a> = NumberField(x^3 - 39*x - 91) 

sage: F = K.factor(7); F 

(Fractional ideal (7, a)) * (Fractional ideal (7, a + 2)) * (Fractional ideal (7, a - 2)) 

sage: F^9 

(Fractional ideal (7, a))^9 * (Fractional ideal (7, a + 2))^9 * (Fractional ideal (7, a - 2))^9 

 

sage: R.<x,y> = FreeAlgebra(ZZ, 2) 

sage: F = Factorization([(x,3), (y, 2), (x,1)]); F 

x^3 * y^2 * x 

sage: F**2 

x^3 * y^2 * x^4 * y^2 * x 

""" 

if not isinstance(n, Integer): 

try: 

n = Integer(n) 

except TypeError: 

raise TypeError("Exponent n (= %s) must be an integer." % n) 

if n == 1: 

return self 

if n == 0: 

return Factorization([]) 

if self.is_commutative(): 

return Factorization([(p, n*e) for p, e in self], unit=self.unit()**n, cr=self.__cr, sort=False, simplify=False) 

if n < 0: 

self = ~self 

n = -n 

from sage.arith.power import generic_power 

return generic_power(self, n) 

 

def __invert__(self): 

r""" 

Return the formal inverse of the factors in the factorization. 

 

EXAMPLES:: 

 

sage: F = factor(2006); F 

2 * 17 * 59 

sage: F^-1 

2^-1 * 17^-1 * 59^-1 

 

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

sage: F = Factorization([(x,3), (y, 2), (x,1)], 2); F 

(2) * x^3 * y^2 * x 

sage: F^-1 

(1/2) * x^-1 * y^-2 * x^-3 

""" 

return Factorization([(p,-e) for p,e in reversed(self)], 

cr=self._cr(), unit=self.unit()**(-1)) 

 

def __truediv__(self, other): 

r""" 

Return the quotient of two factorizations, which is obtained by 

multiplying the first by the inverse of the second. 

 

EXAMPLES:: 

 

sage: factor(-10) / factor(-16) 

2^-3 * 5 

sage: factor(-10) / factor(16) 

-1 * 2^-3 * 5 

 

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

sage: F = Factorization([(x,3), (y, 2), (x,1)]); F 

x^3 * y^2 * x 

sage: G = Factorization([(y, 1), (x,1)],1); G 

y * x 

sage: F / G 

x^3 * y 

""" 

if not isinstance(other, Factorization): 

return self / Factorization([(other, 1)]) 

return self * other**-1 

 

__div__ = __truediv__ 

 

def value(self): 

""" 

Return the product of the factors in the factorization, multiplied out. 

 

EXAMPLES:: 

 

sage: F = factor(-2006); F 

-1 * 2 * 17 * 59 

sage: F.value() 

-2006 

 

sage: R.<x,y> = FreeAlgebra(ZZ, 2) 

sage: F = Factorization([(x,3), (y, 2), (x,1)]); F 

x^3 * y^2 * x 

sage: F.value() 

x^3*y^2*x 

""" 

return prod([p**e for p, e in self.__x], self.__unit) 

 

# Two aliases for ``value(self)``. 

expand = value 

prod = value 

 

def gcd(self, other): 

r""" 

Return the gcd of two factorizations. 

 

If the two factorizations have different universes, this 

method will attempt to find a common universe for the 

gcd. A TypeError is raised if this is impossible. 

 

EXAMPLES:: 

 

sage: factor(-30).gcd(factor(-160)) 

2 * 5 

sage: factor(gcd(-30,160)) 

2 * 5 

 

sage: R.<x> = ZZ[] 

sage: (factor(-20).gcd(factor(5*x+10))).universe() 

Univariate Polynomial Ring in x over Integer Ring 

""" 

if not isinstance(other, Factorization): 

raise NotImplementedError("can't take gcd of factorization and non-factorization") 

 

if len(self) and len(other): 

try: 

# first get the two factorizations to have the same 

# universe 

U = Sequence([self[0][0], other[0][0]]).universe() 

self = self.base_change(U) 

other = other.base_change(U) 

except TypeError: 

raise TypeError("Cannot take the gcd of %s and %s because they cannot be coerced into a common universe"%(self,other)) 

 

if self.is_commutative() and other.is_commutative(): 

d1 = dict(self) 

d2 = dict(other) 

s = {} 

for a in set(d1).intersection(set(d2)): 

s[a] = min(d1[a],d2[a]) 

return Factorization(list(iteritems(s))) 

else: 

raise NotImplementedError("gcd is not implemented for non-commutative factorizations") 

 

def lcm(self, other): 

r""" 

Return the lcm of two factorizations. 

 

If the two factorizations have different universes, this 

method will attempt to find a common universe for the 

lcm. A TypeError is raised if this is impossible. 

 

EXAMPLES:: 

 

sage: factor(-10).lcm(factor(-16)) 

2^4 * 5 

sage: factor(lcm(-10,16)) 

2^4 * 5 

 

sage: R.<x> = ZZ[] 

sage: (factor(-20).lcm(factor(5*x+10))).universe() 

Univariate Polynomial Ring in x over Integer Ring 

""" 

if not isinstance(other, Factorization): 

raise NotImplementedError("can't take lcm of factorization and non-factorization") 

 

if len(self) and len(other): 

try: 

# first get the two factorizations to have the same 

# universe 

U = Sequence([self[0][0], other[0][0]]).universe() 

self = self.base_change(U) 

other = other.base_change(U) 

except TypeError: 

raise TypeError("Cannot take the lcm of %s and %s because they cannot be coerced into a common universe"%(self,other)) 

 

if self.is_commutative() and other.is_commutative(): 

d1 = dict(self) 

d2 = dict(other) 

s = {} 

for a in set(d1).union(set(d2)): 

s[a] = max(d1.get(a,0),d2.get(a,0)) 

return Factorization(list(iteritems(s))) 

else: 

raise NotImplementedError("lcm is not implemented for non-commutative factorizations") 

 

def is_integral(self): 

r""" 

Return True iff all exponents of this Factorization are non-negative. 

 

EXAMPLES:: 

 

sage: F = factor(-10); F 

-1 * 2 * 5 

sage: F.is_integral() 

True 

 

sage: F = factor(-10) / factor(16); F 

-1 * 2^-3 * 5 

sage: F.is_integral() 

False 

 

""" 

return all([e >=0 for p,e in self.__x]) 

 

def radical(self): 

""" 

Return the factorization of the radical of the value of self. 

 

First, check that all exponents in the factorization are 

positive, raise ValueError otherwise. If all exponents are 

positive, return self with all exponents set to 1 and with the 

unit set to 1. 

 

EXAMPLES:: 

 

sage: F = factor(-100); F 

-1 * 2^2 * 5^2 

sage: F.radical() 

2 * 5 

sage: factor(1/2).radical() 

Traceback (most recent call last): 

... 

ValueError: All exponents in the factorization must be positive. 

""" 

if not all([e > 0 for p,e in self.__x]): 

raise ValueError("All exponents in the factorization must be positive.") 

return Factorization([(p,1) for p,e in self.__x], unit=self.unit().parent()(1), cr=self.__cr, sort=False, simplify=False) 

 

def radical_value(self): 

""" 

Return the product of the prime factors in self. 

 

First, check that all exponents in the factorization are 

positive, raise ValueError otherwise. If all exponents are 

positive, return the product of the prime factors in self. 

This should be functionally equivalent to 

self.radical().value() 

 

EXAMPLES:: 

 

sage: F = factor(-100); F 

-1 * 2^2 * 5^2 

sage: F.radical_value() 

10 

sage: factor(1/2).radical_value() 

Traceback (most recent call last): 

... 

ValueError: All exponents in the factorization must be positive. 

""" 

if not all([e > 0 for p,e in self.__x]): 

raise ValueError("All exponents in the factorization must be positive.") 

return prod([p for p,e in self.__x])