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

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

1715

1716

1717

1718

1719

1720

1721

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

r""" 

Finitely generated modules over a PID 

 

You can use Sage to compute with finitely generated modules (FGM's) 

over a principal ideal domain R presented as a quotient V/W, where V 

and W are free. 

 

NOTE: Currently this is only enabled over R=ZZ, since it has not been 

tested and debugged over more general PIDs. All algorithms make sense 

whenever there is a Hermite form implementation. In theory the 

obstruction to extending the implementation is only that one has to 

decide how elements print. If you're annoyed that by this, fix things 

and post a patch! 

 

We represent M=V/W as a pair (V,W) with W contained in V, and we 

internally represent elements of M non-canonically as elements x of 

V. We also fix independent generators g[i] for M in V, and when we 

print out elements of V we print their coordinates with respect to the 

g[i]; over `\ZZ` this is canonical, since each coefficient is reduce 

modulo the additive order of g[i]. To obtain the vector in V 

corresponding to x in M, use x.lift(). 

 

Morphisms between finitely generated R modules are well supported. 

You create a homomorphism by simply giving the images of generators of 

M0 in M1. Given a morphism phi:M0-->M1, you can compute the image of 

phi, the kernel of phi, and using y=phi.lift(x) you can lift an 

elements x in M1 to an element y in M0, if such a y exists. 

 

TECHNICAL NOTE: For efficiency, we introduce a notion of optimized 

representation for quotient modules. The optimized representation of 

M=V/W is the quotient V'/W' where V' has as basis lifts of the 

generators g[i] for M. We internally store a morphism from M0=V0/W0 

to M1=V1/W1 by giving a morphism from the optimized representation V0' 

of M0 to V1 that sends W0 into W1. 

 

 

 

The following TUTORIAL illustrates several of the above points. 

 

First we create free modules V0 and W0 and the quotient module M0. 

Notice that everything works fine even though V0 and W0 are not 

contained inside `\ZZ^n`, which is extremely convenient. :: 

 

sage: V0 = span([[1/2,0,0],[3/2,2,1],[0,0,1]],ZZ); W0 = V0.span([V0.0+2*V0.1, 9*V0.0+2*V0.1, 4*V0.2]) 

sage: M0 = V0/W0; M0 

Finitely generated module V/W over Integer Ring with invariants (4, 16) 

 

The invariants are computed using the Smith normal form algorithm, and 

determine the structure of this finitely generated module. 

 

You can get the V and W used in constructing the quotient module using 

V() and W() methods:: 

 

sage: M0.V() 

Free module of degree 3 and rank 3 over Integer Ring 

Echelon basis matrix: 

[1/2 0 0] 

[ 0 2 0] 

[ 0 0 1] 

sage: M0.W() 

Free module of degree 3 and rank 3 over Integer Ring 

Echelon basis matrix: 

[1/2 4 0] 

[ 0 32 0] 

[ 0 0 4] 

 

We note that the optimized representation of M0, mentioned above in 

the technical note has a V that need not be equal to V0, in general. :: 

 

sage: M0.optimized()[0].V() 

Free module of degree 3 and rank 2 over Integer Ring 

User basis matrix: 

[0 0 1] 

[0 2 0] 

 

Create elements of M0 either by coercing in elements of V0, getting generators, 

or coercing in a list or tuple or coercing in 0. Finally, one can express an 

element as a linear combination of the smith form generators :: 

 

sage: M0(V0.0) 

(0, 14) 

sage: M0(V0.0 + W0.0) # no difference modulo W0 

(0, 14) 

sage: M0.linear_combination_of_smith_form_gens([3,20]) 

(3, 4) 

sage: 3*M0.0 + 20*M0.1 

(3, 4) 

 

We make an element of M0 by taking a difference of two generators, and 

lift it. We also illustrate making an element from a list, which 

coerces to V0, then take the equivalence class modulo W0. :: 

 

sage: x = M0.0 - M0.1; x 

(1, 15) 

sage: x.lift() 

(0, -2, 1) 

sage: M0(vector([1/2,0,0])) 

(0, 14) 

sage: x.additive_order() 

16 

 

Similarly, we construct V1 and W1, and the quotient M1, in a completely different 

2-dimensional ambient space. :: 

 

sage: V1 = span([[1/2,0],[3/2,2]],ZZ); W1 = V1.span([2*V1.0, 3*V1.1]) 

sage: M1 = V1/W1; M1 

Finitely generated module V/W over Integer Ring with invariants (6) 

 

We create the homomorphism from M0 to M1 that sends both generators of 

M0 to 3 times the generator of M1. This is well defined since 3 times 

the generator has order 2. :: 

 

sage: f = M0.hom([3*M1.0, 3*M1.0]); f 

Morphism from module over Integer Ring with invariants (4, 16) to module with invariants (6,) that sends the generators to [(3), (3)] 

 

We evaluate the homomorphism on our element x of the domain, and on the 

first generator of the domain. We also evaluate at an element of V0, 

which is coerced into M0. :: 

 

sage: f(x) 

(0) 

sage: f(M0.0) 

(3) 

sage: f(V0.1) 

(3) 

 

Here we illustrate lifting an element of the image of f, i.e., finding 

an element of M0 that maps to a given element of M1:: 

 

sage: y = f.lift(3*M1.0); y 

(0, 13) 

sage: f(y) 

(3) 

 

We compute the kernel of f, i.e., the submodule of elements of M0 that 

map to 0. Note that the kernel is not explicitly represented as a 

submodule, but as another quotient V/W where V is contained in V0. 

You can explicitly coerce elements of the kernel into M0 though. :: 

 

sage: K = f.kernel(); K 

Finitely generated module V/W over Integer Ring with invariants (2, 16) 

 

sage: M0(K.0) 

(2, 0) 

sage: M0(K.1) 

(3, 1) 

sage: f(M0(K.0)) 

(0) 

sage: f(M0(K.1)) 

(0) 

 

We compute the image of f. :: 

 

sage: f.image() 

Finitely generated module V/W over Integer Ring with invariants (2) 

 

Notice how the elements of the image are written as (0) and (1), 

despite the image being naturally a submodule of M1, which has 

elements (0), (1), (2), (3), (4), (5). However, below we coerce the 

element (1) of the image into the codomain, and get (3):: 

 

sage: list(f.image()) 

[(0), (1)] 

sage: list(M1) 

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

sage: x = f.image().0; x 

(1) 

sage: M1(x) 

(3) 

 

 

TESTS:: 

 

sage: from sage.modules.fg_pid.fgp_module import FGP_Module 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ) 

sage: W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = FGP_Module(V, W); Q 

Finitely generated module V/W over Integer Ring with invariants (4, 12) 

sage: Q.linear_combination_of_smith_form_gens([1,3]) 

(1, 3) 

sage: Q(V([1,3,4])) 

(0, 11) 

sage: Q(W([1,16,0])) 

(0, 0) 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],QQ) 

sage: W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1]) 

sage: Q = FGP_Module(V, W); Q 

Finitely generated module V/W over Rational Field with invariants (0) 

sage: q = Q.an_element(); q 

(1) 

sage: q*(1/2) 

(1/2) 

sage: (1/2)*q 

(1/2) 

 

AUTHOR: 

 

- William Stein, 2009 

""" 

 

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

# Copyright (C) 2009 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 print_function 

from __future__ import absolute_import 

 

from sage.modules.module import Module 

from sage.modules.free_module import is_FreeModule 

from sage.structure.all import parent 

from sage.structure.sequence import Sequence 

from .fgp_element import DEBUG, FGP_Element 

from .fgp_morphism import FGP_Morphism, FGP_Homset 

from sage.rings.all import Integer, ZZ 

from sage.arith.all import lcm 

from sage.misc.cachefunc import cached_method 

from sage.misc.superseded import deprecation 

 

import sage.misc.weak_dict 

from functools import reduce 

_fgp_module = sage.misc.weak_dict.WeakValueDictionary() 

 

 

 

 

def FGP_Module(V, W, check=True): 

""" 

INPUT: 

 

- ``V`` -- a free R-module 

 

- ``W`` -- a free R-submodule of ``V`` 

 

- ``check`` -- bool (default: ``True``); if ``True``, more checks 

on correctness are performed; in particular, we check the data 

types of ``V`` and ``W``, and that ``W`` is a submodule of ``V`` 

with the same base ring. 

 

OUTPUT: 

 

- the quotient ``V/W`` as a finitely generated R-module 

 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: import sage.modules.fg_pid.fgp_module 

sage: Q = sage.modules.fg_pid.fgp_module.FGP_Module(V, W) 

sage: type(Q) 

<class 'sage.modules.fg_pid.fgp_module.FGP_Module_class_with_category'> 

sage: Q is sage.modules.fg_pid.fgp_module.FGP_Module(V, W, check=False) 

True 

""" 

key = (V,V.basis_matrix(),W,W.basis_matrix()) 

try: 

return _fgp_module[key] 

except KeyError: pass 

M = FGP_Module_class(V, W, check=check) 

_fgp_module[key] = M 

return M 

 

 

def is_FGP_Module(x): 

""" 

Return true of x is an FGP module, i.e., a finitely generated 

module over a PID represented as a quotient of finitely generated 

free modules over a PID. 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]); Q = V/W 

sage: sage.modules.fg_pid.fgp_module.is_FGP_Module(V) 

False 

sage: sage.modules.fg_pid.fgp_module.is_FGP_Module(Q) 

True 

""" 

return isinstance(x, FGP_Module_class) 

 

 

class FGP_Module_class(Module): 

""" 

A finitely generated module over a PID presented as a quotient V/W. 

 

INPUT: 

 

- ``V`` -- an R-module 

 

- ``W`` -- an R-submodule of V 

 

- ``check`` -- bool (default: True) 

 

EXAMPLES:: 

 

sage: A = (ZZ^1)/span([[100]], ZZ); A 

Finitely generated module V/W over Integer Ring with invariants (100) 

sage: A.V() 

Ambient free module of rank 1 over the principal ideal domain Integer Ring 

sage: A.W() 

Free module of degree 1 and rank 1 over Integer Ring 

Echelon basis matrix: 

[100] 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W; Q 

Finitely generated module V/W over Integer Ring with invariants (4, 12) 

sage: type(Q) 

<class 'sage.modules.fg_pid.fgp_module.FGP_Module_class_with_category'> 

 

TESTS:: 

 

Make sure that the problems in 

http://trac.sagemath.org/sage_trac/ticket/7516 are fixed:: 

 

sage: V = FreeModule(QQ, 2) 

sage: W = V.submodule([V([1,1])]) 

sage: Z = W.submodule([]) 

sage: WmodZ = W / Z 

sage: loads(dumps(WmodZ))==WmodZ 

True 

""" 

 

# The class to be used for creating elements of this 

# module. Should be overridden in derived classes. 

Element = FGP_Element 

 

def __init__(self, V, W, check=True): 

""" 

INPUT: 

 

- ``V`` -- an R-module 

 

- ``W`` -- an R-submodule of V 

 

- ``check`` -- bool (default: True); if True, more checks on 

correctness are performed; in particular, we check 

the data types of V and W, and that W is a 

submodule of V with the same base ring. 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W; Q 

Finitely generated module V/W over Integer Ring with invariants (4, 12) 

sage: type(Q) 

<class 'sage.modules.fg_pid.fgp_module.FGP_Module_class_with_category'> 

""" 

if check: 

if not is_FreeModule(V): 

raise TypeError("V must be a FreeModule") 

if not is_FreeModule(W): 

raise TypeError("W must be a FreeModule") 

if not W.is_submodule(V): 

raise ValueError("W must be a submodule of V") 

if V.base_ring() != W.base_ring(): 

raise ValueError("W and V must have the same base ring") 

self._W = W 

self._V = V 

Module.__init__(self, base=V.base_ring()) 

 

# Note: There once was a 

# def _subquotient_class(): 

# method that returned a functionoid to construct new modules, so 

# you would call module._subquotient_class()(V,W,check). This has 

# been replaced with module._module_constructor(V,W,check). 

 

def _module_constructor(self, V, W, check=True): 

r""" 

Construct a quotient module ``V/W``. 

 

This should be overridden in derived classes. 

 

INPUT: 

 

- ``V`` -- an R-module. 

 

- ``W`` -- an R-submodule of ``V``. 

 

- ``check`` -- bool (default: True). 

 

OUTPUT: 

 

The quotient ``V/W``. 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W; Q 

Finitely generated module V/W over Integer Ring with invariants (4, 12) 

sage: Q._module_constructor(V,W) 

Finitely generated module V/W over Integer Ring with invariants (4, 12) 

""" 

return FGP_Module(V,W,check) 

 

def _coerce_map_from_(self, S): 

""" 

Return whether ``S`` canonically coerces to ``self``a. 

 

INPUT: 

 

- ``S`` -- anything. 

 

OUTPUT: 

 

Boolean. 

 

EXAMPLES:: 

 

sage: V = span([[5, -1/2]],ZZ); W = span([[20,-2]],ZZ); Q = V/W; phi=Q.hom([2*Q.0]) 

sage: Q._coerce_map_from_(ZZ) 

False 

sage: Q._coerce_map_from_(phi.kernel()) 

True 

sage: Q._coerce_map_from_(Q) 

True 

sage: Q._coerce_map_from_(phi.image()) 

True 

sage: Q._coerce_map_from_(V/V.zero_submodule()) 

True 

sage: Q._coerce_map_from_(V/V) 

False 

sage: Q._coerce_map_from_(ZZ^2) 

False 

 

Of course, `V` canonically coerces to `Q`, as does twice `V`:: 

 

sage: Q._coerce_map_from_(V) 

True 

sage: Q._coerce_map_from_(V.scale(2)) 

True 

""" 

if is_FGP_Module(S): 

return S.has_canonical_map_to(self) 

return self._V.has_coerce_map_from(S) 

 

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

r""" 

Return the image of this module under scalar multiplication by ``other``. 

 

INPUT: 

 

- ``other`` -- an element of the base ring 

- ``switch_sides`` -- (default: ``False``) left or right multiplication 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ) 

sage: W = span([2*V.0,4*V.1,3*V.2]) 

sage: Q = V/W 

sage: Q 

Finitely generated module V/W over Integer Ring with invariants (2, 12) 

sage: 2*Q 

Finitely generated module V/W over Integer Ring with invariants (6) 

sage: Q*3 

Finitely generated module V/W over Integer Ring with invariants (2, 4) 

""" 

if other in self.base_ring(): 

return self._module_constructor(other*self.V() + self.W(), self.W()) 

raise ValueError("Scalar multiplication of a module is only " + 

"defined for an element of the base ring.") 

 

def _repr_(self): 

""" 

Return string representation of this module. 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: (V/W)._repr_() 

'Finitely generated module V/W over Integer Ring with invariants (4, 12)' 

""" 

I = str(self.invariants()).replace(',)',')') 

return "Finitely generated module V/W over %s with invariants %s"%(self.base_ring(), I) 

 

def __truediv__(self, other): 

""" 

Return the quotient self/other, where other must be a 

submodule of self. 

 

EXAMPLES:: 

 

sage: V = span([[5, -1/2]],ZZ); W = span([[20,-2]],ZZ); Q = V/W; phi=Q.hom([2*Q.0]) 

sage: Q 

Finitely generated module V/W over Integer Ring with invariants (4) 

sage: Q/phi.kernel() 

Finitely generated module V/W over Integer Ring with invariants (2) 

sage: Q/Q 

Finitely generated module V/W over Integer Ring with invariants () 

""" 

if not is_FGP_Module(other): 

if is_FreeModule(other): 

other = other / other.zero_submodule() 

else: 

raise TypeError("other must be an FGP module") 

if not other.is_submodule(self): 

raise ValueError("other must be a submodule of self") 

return self._module_constructor(self._V, other._V+self._W) 

 

def __eq__(self, other): 

""" 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W 

sage: Q == Q 

True 

sage: loads(dumps(Q)) == Q 

True 

sage: Q == V 

False 

sage: Q == V/V.zero_submodule() 

False 

""" 

if not is_FGP_Module(other): 

return False 

return self._V == other._V and self._W == other._W 

 

def __ne__(self, other): 

""" 

True iff self is not equal to other. 

 

This may not be needed for modules created using the function 

:func:`FGP_Module`, since those have uniqueness built into 

them, but if the modules are created directly using the 

__init__ method for this class, then this may fail; in 

particular, for modules M and N with ``M == N`` returning 

True, it may be the case that ``M != N`` may also return True. 

In particular, for derived classes whose __init__ methods just 

call the __init__ method for this class need this. See 

http://trac.sagemath.org/sage_trac/ticket/9940 for 

illustrations. 

 

EXAMPLES: 

 

Make sure that the problems in 

http://trac.sagemath.org/sage_trac/ticket/9940 are fixed:: 

 

sage: G = AdditiveAbelianGroup([0,0]) 

sage: H = AdditiveAbelianGroup([0,0]) 

sage: G == H 

True 

sage: G != H # indirect doctest 

False 

 

sage: N1 = ToricLattice(3) 

sage: sublattice1 = N1.submodule([(1,1,0), (3,2,1)]) 

sage: Q1 = N1/sublattice1 

sage: N2 = ToricLattice(3) 

sage: sublattice2 = N2.submodule([(1,1,0), (3,2,1)]) 

sage: Q2 = N2/sublattice2 

sage: Q1 == Q2 

True 

sage: Q1 != Q2 

False 

""" 

return not self == other 

 

# __le__ is a synonym for `is_submodule`: see below 

 

def __lt__(self, other): 

""" 

True iff self is a proper submodule of other. 

 

EXAMPLES:: 

 

sage: V = ZZ^2; W = V.span([[1,2]]); W2 = W.scale(2) 

sage: A = V/W; B = W/W2 

sage: B < A 

False 

sage: A = V/W2; B = W/W2 

sage: B < A 

True 

sage: A < A 

False 

""" 

return self <= other and not self == other 

 

def __gt__(self, other): 

""" 

True iff other is a proper submodule of self. 

 

EXAMPLES:: 

 

sage: V = ZZ^2; W = V.span([[1,2]]); W2 = W.scale(2) 

sage: A = V/W; B = W/W2 

sage: A > B 

False 

sage: A = V/W2; B = W/W2 

sage: A > B 

True 

sage: A > A 

False 

""" 

return self >= other and not self == other 

 

def __ge__(self, other): 

""" 

True iff other is a submodule of self. 

 

EXAMPLES:: 

 

sage: V = ZZ^2; W = V.span([[1,2]]); W2 = W.scale(2) 

sage: A = V/W; B = W/W2 

sage: A >= B 

False 

sage: A = V/W2; B = W/W2 

sage: A >= B 

True 

sage: A >= A 

True 

""" 

return other.is_submodule(self) 

 

 

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

""" 

INPUT: 

 

- ``x`` -- a vector, an fgp module element, or a list or tuple: 

 

- list or tuple: take the corresponding linear combination of 

the generators of self. 

 

- vector: coerce vector into V and define the 

corresponding element of V/W 

 

- fgp module element: lift to element of ambient vector 

space and try to put into V. If x is in self already, 

just return x. 

 

- `check` -- bool (default: True) 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W 

sage: x = Q(V.0-V.1); x # indirect doctest 

(0, 3) 

sage: type(x) 

<class 'sage.modules.fg_pid.fgp_module.FGP_Module_class_with_category.element_class'> 

sage: x is Q(x) 

True 

sage: x.parent() is Q 

True 

""" 

if isinstance(x, FGP_Element): 

x = x.lift() 

elif isinstance(x,(list,tuple)): 

deprecation(16261, "The default behaviour changed! If you" 

" *really* want a linear combination of smith" 

" generators, use .linear_combination_of_smith_form_gens.") 

return self.element_class(self, self._V(x)) 

 

def linear_combination_of_smith_form_gens(self, x): 

r""" 

Compute a linear combination of the optimised generators of this module 

as returned by :meth:`.smith_form_gens`. 

 

EXAMPLES:: 

 

sage: X = ZZ**2 / span([[3,0],[0,2]], ZZ) 

sage: X.linear_combination_of_smith_form_gens([1]) 

(1) 

 

""" 

try: 

x = self.optimized()[0].V().linear_combination_of_basis(x) 

except ValueError as msg: 

raise TypeError(msg) 

return self.element_class(self, self._V(x)) 

 

def __contains__(self, x): 

""" 

Return true if x is contained in self. 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W; Q 

Finitely generated module V/W over Integer Ring with invariants (4, 12) 

sage: Q.0 in Q 

True 

sage: 0 in Q 

True 

sage: vector([1,2,3/7]) in Q 

False 

sage: vector([1,2,3]) in Q 

True 

sage: Q.0 - Q.1 in Q 

True 

""" 

if parent(x) is self: 

return True 

try: 

self(x) 

return True 

except TypeError: 

return False 

 

def submodule(self, x): 

""" 

Return the submodule defined by x. 

 

INPUT: 

 

- ``x`` -- list, tuple, or FGP module 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W; Q 

Finitely generated module V/W over Integer Ring with invariants (4, 12) 

sage: Q.gens() 

((1, 0), (0, 1)) 

 

We create submodules generated by a list or tuple of elements:: 

 

sage: Q.submodule([Q.0]) 

Finitely generated module V/W over Integer Ring with invariants (4) 

sage: Q.submodule([Q.1]) 

Finitely generated module V/W over Integer Ring with invariants (12) 

sage: Q.submodule((Q.0, Q.0 + 3*Q.1)) 

Finitely generated module V/W over Integer Ring with invariants (4, 4) 

 

A submodule defined by a submodule:: 

 

sage: A = Q.submodule((Q.0, Q.0 + 3*Q.1)); A 

Finitely generated module V/W over Integer Ring with invariants (4, 4) 

sage: Q.submodule(A) 

Finitely generated module V/W over Integer Ring with invariants (4, 4) 

 

Inclusion is checked:: 

 

sage: A.submodule(Q) 

Traceback (most recent call last): 

... 

ValueError: x.V() must be contained in self's V. 

""" 

if is_FGP_Module(x): 

if not x._W.is_submodule(self._W): 

raise ValueError("x.W() must be contained in self's W.") 

 

V = x._V 

if not V.is_submodule(self._V): 

raise ValueError("x.V() must be contained in self's V.") 

 

return x 

 

if not isinstance(x, (list, tuple)): 

raise TypeError("x must be a list, tuple, or FGP module") 

 

x = Sequence(x) 

if is_FGP_Module(x.universe()): 

# TODO: possibly inefficient in some cases 

x = [self(v).lift() for v in x] 

V = self._V.submodule(x) + self._W 

return self._module_constructor(V, self._W) 

 

def has_canonical_map_to(self, A): 

""" 

Return True if self has a canonical map to A, relative to the 

given presentation of A. This means that A is a finitely 

generated quotient module, self.V() is a submodule of A.V() 

and self.W() is a submodule of A.W(), i.e., that there is a 

natural map induced by inclusion of the V's. Note that we do 

*not* require that this natural map be injective; for this use 

:meth:`is_submodule`. 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W; Q 

Finitely generated module V/W over Integer Ring with invariants (4, 12) 

sage: A = Q.submodule((Q.0, Q.0 + 3*Q.1)); A 

Finitely generated module V/W over Integer Ring with invariants (4, 4) 

sage: A.has_canonical_map_to(Q) 

True 

sage: Q.has_canonical_map_to(A) 

False 

 

""" 

if not is_FGP_Module(A): 

return False 

if self.cardinality() == 0 and self.base_ring() == A.base_ring(): 

return True 

return self.V().is_submodule(A.V()) and self.W().is_submodule(A.W()) 

 

def is_submodule(self, A): 

""" 

Return True if self is a submodule of A. More precisely, this returns True if 

if ``self.V()`` is a submodule of ``A.V()``, with ``self.W()`` equal to ``A.W()``. 

 

Compare :meth:`.has_canonical_map_to`. 

 

EXAMPLES:: 

 

sage: V = ZZ^2; W = V.span([[1,2]]); W2 = W.scale(2) 

sage: A = V/W; B = W/W2 

sage: B.is_submodule(A) 

False 

sage: A = V/W2; B = W/W2 

sage: B.is_submodule(A) 

True 

 

This example illustrates that this command works in a subtle cases.:: 

 

sage: A = ZZ^1 

sage: Q3 = A / A.span([[3]]) 

sage: Q6 = A / A.span([[6]]) 

sage: Q6.is_submodule(Q3) 

False 

sage: Q6.has_canonical_map_to(Q3) 

True 

sage: Q = A.span([[2]]) / A.span([[6]]) 

sage: Q.is_submodule(Q6) 

True 

""" 

if not self.has_canonical_map_to(A): 

return False 

 

return self.V().is_submodule(A.V()) and self.W() == A.W() 

 

__le__ = is_submodule 

 

def V(self): 

""" 

If this module was constructed as a quotient V/W, returns V. 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W 

sage: Q.V() 

Free module of degree 3 and rank 3 over Integer Ring 

Echelon basis matrix: 

[1/2 0 0] 

[ 0 1 0] 

[ 0 0 1] 

 

""" 

return self._V 

 

def cover(self): 

""" 

If this module was constructed as V/W, returns the cover module V. This is the same as self.V(). 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W 

sage: Q.V() 

Free module of degree 3 and rank 3 over Integer Ring 

Echelon basis matrix: 

[1/2 0 0] 

[ 0 1 0] 

[ 0 0 1] 

""" 

return self.V() 

 

def W(self): 

""" 

If this module was constructed as a quotient V/W, returns W. 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W 

sage: Q.W() 

Free module of degree 3 and rank 3 over Integer Ring 

Echelon basis matrix: 

[1/2 8 0] 

[ 0 12 0] 

[ 0 0 4] 

""" 

return self._W 

 

def relations(self): 

""" 

If this module was constructed as V/W, returns the relations module V. This is the same as self.W(). 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W 

sage: Q.relations() 

Free module of degree 3 and rank 3 over Integer Ring 

Echelon basis matrix: 

[1/2 8 0] 

[ 0 12 0] 

[ 0 0 4] 

""" 

return self.W() 

 

@cached_method 

def _relative_matrix(self): 

""" 

V has a fixed choice of basis, and W has a fixed choice of 

basis, and both V and W are free R-modules. Since W is 

contained in V, we can write each basis element of W as an 

R-linear combination of the basis for V. This function 

returns that matrix over R, where each row corresponds to a 

basis element of W. 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W 

sage: Q._relative_matrix() 

[ 1 8 0] 

[ 0 12 0] 

[ 0 0 4] 

""" 

V = self._V 

W = self._W 

A = V.coordinate_module(W).basis_matrix().change_ring(self.base_ring()) 

return A 

 

@cached_method 

def _smith_form(self): 

""" 

Return matrices S, U, and V such that S = U*R*V, and S is in 

Smith normal form, and R is the relative matrix that defines 

self (see :meth:`._relative_matrix`). 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W 

sage: Q._smith_form() 

( 

[ 1 0 0] [1 0 0] [ 1 0 -8] 

[ 0 4 0] [0 0 1] [ 0 0 1] 

[ 0 0 12], [0 1 0], [ 0 1 0] 

) 

""" 

return self._relative_matrix().smith_form() 

 

def base_ring(self): 

""" 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W 

sage: Q.base_ring() 

Integer Ring 

""" 

return self._V.base_ring() 

 

@cached_method 

def invariants(self, include_ones=False): 

""" 

Return the diagonal entries of the smith form of the relative 

matrix that defines self (see :meth:`._relative_matrix`) 

padded with zeros, excluding 1's by default. Thus if v is the 

list of integers returned, then self is abstractly isomorphic to 

the product of cyclic groups `Z/nZ` where `n` is in `v`. 

 

INPUT: 

 

- ``include_ones`` -- bool (default: False); if True, also 

include 1's in the output list. 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W 

sage: Q.invariants() 

(4, 12) 

 

An example with 1 and 0 rows:: 

 

sage: V = ZZ^3; W = V.span([[1,2,0],[0,1,0], [0,2,0]]); Q = V/W; Q 

Finitely generated module V/W over Integer Ring with invariants (0) 

sage: Q.invariants() 

(0,) 

sage: Q.invariants(include_ones=True) 

(1, 1, 0) 

 

""" 

D,_,_ = self._smith_form() 

 

v = [D[i,i] for i in range(D.nrows())] + [Integer(0)]*(D.ncols()-D.nrows()) 

w = tuple([x for x in v if x != 1]) 

v = tuple(v) 

self.invariants.set_cache(v, True) 

self.invariants.set_cache(w, False) 

return self.invariants(include_ones) 

 

def gens(self): 

""" 

Returns tuple of elements `g_0,...,g_n` of self such that the module generated by 

the gi is isomorphic to the direct sum of R/ei*R, where ei are the 

invariants of self and R is the base ring. 

 

Note that these are not generally uniquely determined, and depending on 

how Smith normal form is implemented for the base ring, they may not 

even be deterministic. 

 

This can safely be overridden in all derived classes. 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W 

sage: Q.gens() 

((1, 0), (0, 1)) 

sage: Q.0 

(1, 0) 

""" 

return self.smith_form_gens() 

 

@cached_method 

def smith_form_gens(self): 

""" 

Return a set of generators for self which are in Smith normal form. 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W 

sage: Q.smith_form_gens() 

((1, 0), (0, 1)) 

sage: [x.lift() for x in Q.smith_form_gens()] 

[(0, 0, 1), (0, 1, 0)] 

""" 

# Get the rightmost transformation in the Smith form 

_, _, X = self._smith_form() 

# Invert it to get a matrix whose rows (in terms of the basis for V) 

# are the gi (including 1 invariants). 

Y = X**(-1) 

# Get the basis matrix for V 

B = self._V.basis_matrix() 

# Multiply to express the gi in terms of the ambient vector space. 

Z = Y*B 

# Make gens out of the rows of Z that correspond to non-1 invariants. 

v = self.invariants(include_ones=True) 

non1 = [i for i in range(Z.nrows()) if v[i] != 1] 

Z = Z.matrix_from_rows(non1) 

self._gens = tuple([self(z, check=DEBUG) for z in Z.rows()]) 

return self._gens 

 

def coordinate_vector(self, x, reduce=False): 

""" 

Return coordinates of x with respect to the optimized 

representation of self. 

 

INPUT: 

 

- ``x`` -- element of self 

 

- ``reduce`` -- (default: False); if True, reduce 

coefficients modulo invariants; this is 

ignored if the base ring isn't ZZ. 

 

OUTPUT: 

 

The coordinates as a vector. That is, the same type as 

``self.V()``, but in general with fewer entries. 

 

EXAMPLES:: 

 

sage: V = span([[1/4,0,0],[3/4,4,2],[0,0,2]],ZZ); W = V.span([4*V.0+12*V.1]) 

sage: Q = V/W; Q 

Finitely generated module V/W over Integer Ring with invariants (4, 0, 0) 

sage: Q.coordinate_vector(-Q.0) 

(-1, 0, 0) 

sage: Q.coordinate_vector(-Q.0, reduce=True) 

(3, 0, 0) 

 

If x isn't in self, it is coerced in:: 

 

sage: Q.coordinate_vector(V.0) 

(1, 0, -3) 

sage: Q.coordinate_vector(Q(V.0)) 

(1, 0, -3) 

 

 

TESTS:: 

 

sage: V = span([[1/2,0,0],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W; Q 

Finitely generated module V/W over Integer Ring with invariants (4, 12) 

sage: Q.coordinate_vector(Q.0 - Q.1) 

(1, -1) 

 

sage: O, X = Q.optimized() 

sage: O.V() 

Free module of degree 3 and rank 2 over Integer Ring 

User basis matrix: 

[0 0 1] 

[0 2 0] 

sage: phi = Q.hom([Q.0, 4*Q.1]) 

sage: x = Q(V.0); x 

(0, 4) 

sage: Q.coordinate_vector(x, reduce=True) 

(0, 4) 

sage: Q.coordinate_vector(-x, reduce=False) 

(0, -4) 

sage: x == 4*Q.1 

True 

sage: x = Q(V.1); x 

(0, 1) 

sage: Q.coordinate_vector(x) 

(0, 1) 

sage: x == Q.1 

True 

sage: x = Q(V.2); x 

(1, 0) 

sage: Q.coordinate_vector(x) 

(1, 0) 

sage: x == Q.0 

True 

""" 

try: T = self.__T 

except AttributeError: 

self.optimized() # computes T as side effect -- see the "optimized" method. 

T = self.__T 

 

x = self(x) 

 

c = self._V.coordinate_vector(x.lift()) 

b = (c*T).change_ring(self.base_ring()) 

if reduce and self.base_ring() == ZZ: 

 

I = self.invariants() 

return b.parent()([b[i] if I[i] == 0 else b[i] % I[i] for i in range(len(I))]) 

 

else: 

# Don't know (or not requested) canonical way to reduce 

# each entry yet, or how to compute invariants. 

return b 

 

def gen(self, i): 

""" 

Return the i-th generator of self. 

 

INPUT: 

 

- ``i`` -- integer 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W; Q 

Finitely generated module V/W over Integer Ring with invariants (4, 12) 

sage: Q.gen(0) 

(1, 0) 

sage: Q.gen(1) 

(0, 1) 

sage: Q.gen(2) 

Traceback (most recent call last): 

... 

ValueError: Generator 2 not defined 

sage: Q.gen(-1) 

Traceback (most recent call last): 

... 

ValueError: Generator -1 not defined 

""" 

v = self.gens() 

if i < 0 or i >= len(v): 

raise ValueError("Generator %s not defined"%i) 

return v[i] 

 

def smith_form_gen(self, i): 

""" 

Return the i-th generator of self. A private name (so we can freely 

override gen() in derived classes). 

 

INPUT: 

 

- ``i`` -- integer 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W; Q 

Finitely generated module V/W over Integer Ring with invariants (4, 12) 

sage: Q.smith_form_gen(0) 

(1, 0) 

sage: Q.smith_form_gen(1) 

(0, 1) 

""" 

v = self.smith_form_gens() 

if i < 0 or i >= len(v): 

raise ValueError("Smith form generator %s not defined"%i) 

return v[i] 

 

def optimized(self): 

""" 

Return a module isomorphic to this one, but with V replaced by 

a submodule of V such that the generators of self all lift 

trivially to generators of V. Replace W by the intersection 

of V and W. This has the advantage that V has small dimension 

and any homomorphism from self trivially extends to a 

homomorphism from V. 

 

OUTPUT: 

 

- ``Q`` -- an optimized quotient V0/W0 with V0 a submodule of V 

such that phi: V0/W0 --> V/W is an isomorphism 

 

- ``Z`` -- matrix such that if x is in self.V() and 

c gives the coordinates of x in terms of the 

basis for self.V(), then c*Z is in V0 

and c*Z maps to x via phi above. 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W 

sage: O, X = Q.optimized(); O 

Finitely generated module V/W over Integer Ring with invariants (4, 12) 

sage: O.V() 

Free module of degree 3 and rank 2 over Integer Ring 

User basis matrix: 

[0 0 1] 

[0 1 0] 

sage: O.W() 

Free module of degree 3 and rank 2 over Integer Ring 

Echelon basis matrix: 

[ 0 12 0] 

[ 0 0 4] 

sage: X 

[0 4 0] 

[0 1 0] 

[0 0 1] 

sage: OV = O.V() 

sage: Q(OV([0,-8,0])) == V.0 

True 

sage: Q(OV([0,1,0])) == V.1 

True 

sage: Q(OV([0,0,1])) == V.2 

True 

""" 

try: 

if self.__optimized is True: 

return self, None 

return self.__optimized 

except AttributeError: pass 

V = self._V.span_of_basis([x.lift() for x in self.smith_form_gens()]) 

M = self._module_constructor(V, self._W.intersection(V)) 

# Compute matrix T of linear transformation from self._V to V. 

# This matrix T gives each basis element of self._V in terms 

# of our new optimized V, modulo the W's. 

A = V.basis_matrix().stack(self._W.basis_matrix()) 

B, d = A._clear_denom() 

H, U = B.hermite_form(transformation=True) 

Y = H.solve_left(d*self._V.basis_matrix()) 

T = Y * U.matrix_from_columns(range(V.rank())) 

self.__T = T 

 

# Finally we multiply by V.basis_matrix() so X gives vectors 

# in the ambient space instead of coefficients of linear 

# combinations of the basis for V. 

X = T * V.basis_matrix() 

 

self.__optimized = M, X 

return M, X 

 

 

def hom(self, im_gens, codomain=None, check=True): 

""" 

Homomorphism defined by giving the images of ``self.gens()`` in some 

fixed fg R-module. 

 

.. NOTE:: 

 

We do not assume that the generators given by ``self.gens()`` are 

the same as the Smith form generators, since this may not be true 

for a general derived class. 

 

INPUT: 

 

- ``im_gens`` -- a list of the images of ``self.gens()`` in some 

R-module 

 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W 

sage: phi = Q.hom([3*Q.1, Q.0]) 

sage: phi 

Morphism from module over Integer Ring with invariants (4, 12) to module with invariants (4, 12) that sends the generators to [(0, 3), (1, 0)] 

sage: phi(Q.0) 

(0, 3) 

sage: phi(Q.1) 

(1, 0) 

sage: Q.0 == phi(Q.1) 

True 

 

This example illustrates creating a morphism to a free module. 

The free module is turned into an FGP module (i.e., quotient 

V/W with W=0), and the morphism is constructed:: 

 

sage: V = span([[1/2,0,0],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1]) 

sage: Q = V/W; Q 

Finitely generated module V/W over Integer Ring with invariants (2, 0, 0) 

sage: phi = Q.hom([0,V.0,V.1]); phi 

Morphism from module over Integer Ring with invariants (2, 0, 0) to module with invariants (0, 0, 0) that sends the generators to [(0, 0, 0), (1, 0, 0), (0, 1, 0)] 

sage: phi.domain() 

Finitely generated module V/W over Integer Ring with invariants (2, 0, 0) 

sage: phi.codomain() 

Finitely generated module V/W over Integer Ring with invariants (0, 0, 0) 

sage: phi(Q.0) 

(0, 0, 0) 

sage: phi(Q.1) 

(1, 0, 0) 

sage: phi(Q.2) == V.1 

True 

 

Constructing two zero maps from the zero module:: 

 

sage: A = (ZZ^2)/(ZZ^2); A 

Finitely generated module V/W over Integer Ring with invariants () 

sage: A.hom([]) 

Morphism from module over Integer Ring with invariants () to module with invariants () that sends the generators to [] 

sage: A.hom([]).codomain() is A 

True 

sage: B = (ZZ^3)/(ZZ^3) 

sage: A.hom([],codomain=B) 

Morphism from module over Integer Ring with invariants () to module with invariants () that sends the generators to [] 

sage: phi = A.hom([],codomain=B); phi 

Morphism from module over Integer Ring with invariants () to module with invariants () that sends the generators to [] 

sage: phi(A(0)) 

() 

sage: phi(A(0)) == B(0) 

True 

 

 

A degenerate case:: 

 

sage: A = (ZZ^2)/(ZZ^2) 

sage: phi = A.hom([]); phi 

Morphism from module over Integer Ring with invariants () to module with invariants () that sends the generators to [] 

sage: phi(A(0)) 

() 

 

The code checks that the morphism is valid. In the example 

below we try to send a generator of order 2 to an element of 

order 14:: 

 

sage: V = span([[1/14,3/14],[0,1/2]],ZZ); W = ZZ^2 

sage: Q = V/W; Q 

Finitely generated module V/W over Integer Ring with invariants (2, 14) 

sage: Q.linear_combination_of_smith_form_gens([1,11]).additive_order() 

14 

sage: f = Q.hom([Q.linear_combination_of_smith_form_gens([1,11]), Q.linear_combination_of_smith_form_gens([1,3])]); f 

Traceback (most recent call last): 

... 

ValueError: phi must send optimized submodule of M.W() into N.W() 

 

 

""" 

if len(im_gens) == 0: 

# 0 map 

N = self if codomain is None else codomain 

else: 

if codomain is None: 

im_gens = Sequence(im_gens) 

N = im_gens.universe() 

else: 

N = codomain 

im_gens = Sequence(im_gens, universe=N) 

 

if is_FreeModule(N): 

# If im_smith_gens aren't in an R-module, but are in a Free-module, 

# then we quotient out by the 0 submodule and get an R-module. 

N = FGP_Module(N, N.zero_submodule(), check=DEBUG) 

im_gens = Sequence(im_gens, universe=N) 

 

if len(im_gens) == 0: 

VO = self.optimized()[0].V() 

H = VO.Hom(N.V()) 

return FGP_Morphism(self.Hom(N), H(0), check=DEBUG) 

 

if self.gens() == self.smith_form_gens(): 

return self._hom_from_smith(im_gens, check) 

else: 

return self._hom_general(im_gens, check) 

 

 

def _hom_general(self, im_gens, check=True): 

""" 

Homomorphism defined by giving the images of ``self.gens()`` in some 

fixed fg R-module. We do not assume that the generators given by 

``self.gens()`` are the same as the Smith form generators, since this 

may not be true for a general derived class. 

 

INPUT: 

 

- ``im_gens`` - a Sequence object giving the images of ``self.gens()``, 

whose universe is some fixed fg R-module 

 

EXAMPLES:: 

 

sage: class SillyModule(sage.modules.fg_pid.fgp_module.FGP_Module_class): 

....: def gens(self): 

....: return tuple(flatten([[x,x] for x in self.smith_form_gens()])) 

sage: A = SillyModule(ZZ**1, span([[3]], ZZ)) 

sage: A.gen(0) 

(1) 

sage: A.gen(1) 

(1) 

sage: B = ZZ**1 / span([[3]], ZZ) 

sage: A.hom([B.0, 2*B.0], B) 

Traceback (most recent call last): 

... 

ValueError: Images do not determine a valid homomorphism 

sage: A.hom([B.0, B.0], B) # indirect doctest 

Morphism from module over Integer Ring with invariants (3,) to module with invariants (3,) that sends the generators to [(1), (1)] 

 

""" 

m = self.ngens() 

A = ZZ**m 

q = A.hom([x.lift() for x in self.gens()], self.V()) 

B = q.inverse_image(self.W()) 

N = im_gens.universe() 

r = A.hom([x.lift() for x in im_gens], N.V()) 

if check: 

if not r(B).is_submodule(N.W()): 

raise ValueError("Images do not determine a valid homomorphism") 

smith_images = Sequence([N(r(q.lift(x.lift()))) for x in self.smith_form_gens()]) 

return self._hom_from_smith(smith_images, check=DEBUG) 

 

def _hom_from_smith(self, im_smith_gens, check=True): 

""" 

Homomorphism defined by giving the images of the Smith-form generators 

of self in some fixed fg R-module. 

 

INPUT: 

 

- ``im_gens`` -- a Sequence object giving the images of the Smith-form 

generators of self, whose universe is some fixed fg R-module 

 

EXAMPLES:: 

 

sage: class SillyModule(sage.modules.fg_pid.fgp_module.FGP_Module_class): 

....: def gens(self): 

....: return tuple(flatten([[x,x] for x in self.smith_form_gens()])) 

sage: A = SillyModule(ZZ**1, span([[3]], ZZ)) 

sage: A.gen(0) 

(1) 

sage: A.gen(1) 

(1) 

sage: B = ZZ**1 / span([[3]], ZZ) 

sage: A._hom_from_smith(Sequence([B.0])) 

Morphism from module over Integer Ring with invariants (3,) to module with invariants (3,) that sends the generators to [(1), (1)] 

""" 

if len(im_smith_gens) != len(self.smith_form_gens()): 

raise ValueError("im_gens must have length the same as self.smith_form_gens()") 

 

# replace self by representation in which smith-gens g_i are a basis for V. 

M, _ = self.optimized() 

# Define morphism from M to N 

f = M.V().hom([x.lift() for x in im_smith_gens]) 

N = im_smith_gens.universe() 

homspace = self.Hom(N) 

phi = FGP_Morphism(homspace, f, check=DEBUG) 

return phi 

 

def _Hom_(self, N, category=None): 

""" 

EXAMPLES:: 

 

sage: V = span([[1/2,0,0],[3/2,2,1],[0,0,1]],ZZ); W = V.span([V.0+2*V.1, 9*V.0+2*V.1, 4*V.2]) 

sage: Q = V/W 

sage: Q.Hom(Q) # indirect doctest 

Set of Morphisms from Finitely generated module V/W over Integer Ring with invariants (4, 16) to Finitely generated module V/W over Integer Ring with invariants (4, 16) in Category of modules over Integer Ring 

sage: M = V/V.zero_submodule() 

sage: H = M.Hom(Q); H 

Set of Morphisms from Finitely generated module V/W over Integer Ring with invariants (0, 0, 0) to Finitely generated module V/W over Integer Ring with invariants (4, 16) in Category of modules over Integer Ring 

sage: Hom(M,Q) is H 

True 

sage: type(Hom(M,Q)) 

<class 'sage.modules.fg_pid.fgp_morphism.FGP_Homset_class_with_category'> 

sage: H.category() 

Category of homsets of modules over Integer Ring 

sage: H.homset_category() 

Category of modules over Integer Ring 

 

The category is correctly adjusted when constructing Hom sets 

with more general codomains (see :trac:`16402`):: 

 

sage: V = ZZ^2 

sage: W = V.quotient(V.span([[1, 1]])) 

sage: H = W.Hom(QQ); H 

Set of Morphisms from Finitely generated module V/W over Integer Ring with invariants (0) to Rational Field in Category of commutative additive groups 

sage: type(H) 

<class 'sage.categories.homset.Homset_with_category'> 

 

""" 

if isinstance(N, FGP_Module_class): 

return FGP_Homset(self, N) 

return super(FGP_Module_class, self)._Hom_(N, category=category) 

 

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

""" 

Create a random element of self=V/W, by creating a random element of V and 

reducing it modulo W. 

 

All arguments are passed onto the random_element method of V. 

 

EXAMPLES:: 

 

sage: V = span([[1/2,1,1],[3/2,2,1],[0,0,1]],ZZ); W = V.span([2*V.0+4*V.1, 9*V.0+12*V.1, 4*V.2]) 

sage: Q = V/W 

sage: Q.random_element() 

(1, 10) 

""" 

return self(self._V.random_element(*args, **kwds)) 

 

def cardinality(self): 

""" 

Return the cardinality of this module as a set. 

 

EXAMPLES:: 

 

sage: V = ZZ^2; W = V.span([[1,2],[3,4]]); A = V/W; A 

Finitely generated module V/W over Integer Ring with invariants (2) 

sage: A.cardinality() 

2 

sage: V = ZZ^2; W = V.span([[1,2]]); A = V/W; A 

Finitely generated module V/W over Integer Ring with invariants (0) 

sage: A.cardinality() 

+Infinity 

sage: V = QQ^2; W = V.span([[1,2]]); A = V/W; A 

Vector space quotient V/W of dimension 1 over Rational Field where 

V: Vector space of dimension 2 over Rational Field 

W: Vector space of degree 2 and dimension 1 over Rational Field 

Basis matrix: 

[1 2] 

sage: A.cardinality() 

+Infinity 

""" 

try: 

return self.__cardinality 

except AttributeError: 

pass 

from sage.rings.all import infinity 

from sage.misc.all import prod 

v = self.invariants() 

self.__cardinality = infinity if 0 in v else prod(v) 

return self.__cardinality 

 

def list(self): 

""" 

Return a list of the elements of ``self``. 

 

EXAMPLES:: 

 

sage: V = ZZ^2; W = V.span([[1,2],[3,4]]) 

sage: list(V/W) 

[(0), (1)] 

""" 

return [e for e in self] 

 

def __iter__(self): 

""" 

Return iterator over all elements of ``self``. 

 

EXAMPLES:: 

 

sage: V = span([[1/2,0,0],[3/2,2,1],[0,0,1]],ZZ); W = V.span([V.0+2*V.1, 4*V.0+2*V.1, 4*V.2]) 

sage: Q = V/W; Q 

Finitely generated module V/W over Integer Ring with invariants (2, 12) 

sage: z = list(V/W) 

sage: z 

[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (0, 10), (0, 11), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11)] 

sage: len(z) 

24 

 

We test that the trivial module is handled correctly (:trac:`6561`):: 

 

sage: A = (ZZ**1)/(ZZ**1); list(A) == [A(0)] 

True 

""" 

if self.base_ring() != ZZ: 

raise NotImplementedError("only implemented over ZZ") 

v = self.invariants() 

if 0 in v: 

raise NotImplementedError("currently self must be finite to iterate over") 

B = self.optimized()[0].V().basis_matrix() 

V = self.base_ring()**B.nrows() 

from sage.misc.mrange import cartesian_product_iterator 

for a in cartesian_product_iterator([range(k) for k in v]): 

b = V(a) * B 

yield self(b) 

 

def construction(self): 

""" 

The construction functor and ambient module for ``self``. 

 

EXAMPLES:: 

 

sage: W = ZZ^2 

sage: A1 = W.submodule([[1,0]]) 

sage: B1 = W.submodule([[2,0]]) 

sage: T1 = A1 / B1 

sage: T1.construction() 

(QuotientModuleFunctor, 

Free module of degree 2 and rank 1 over Integer Ring 

Echelon basis matrix: 

[1 0]) 

 

TESTS:: 

 

sage: W = ZZ^2 

sage: A1 = W.submodule([[1,0]]) 

sage: A2 = W.submodule([[0,1]]) 

sage: B1 = W.submodule([[2,0]]) 

sage: B2 = W.submodule([[0,2]]) 

sage: T1 = A1 / B1 

sage: T2 = A2 / B2 

sage: t1 = T1.an_element() 

sage: t2 = T2.an_element() 

sage: t1 + t2 

(1, 1) 

""" 

from sage.modules.module_functors import QuotientModuleFunctor 

return (QuotientModuleFunctor(self._W), self._V) 

 

def is_finite(self): 

""" 

Return True if self is finite and False otherwise. 

 

EXAMPLES:: 

 

sage: V = span([[1/2,0,0],[3/2,2,1],[0,0,1]],ZZ); W = V.span([V.0+2*V.1, 9*V.0+2*V.1, 4*V.2]) 

sage: Q = V/W; Q 

Finitely generated module V/W over Integer Ring with invariants (4, 16) 

sage: Q.is_finite() 

True 

sage: Q = V/V.zero_submodule(); Q 

Finitely generated module V/W over Integer Ring with invariants (0, 0, 0) 

sage: Q.is_finite() 

False 

""" 

return 0 not in self.invariants() 

 

def annihilator(self): 

""" 

Return the ideal of the base ring that annihilates self. This 

is precisely the ideal generated by the LCM of the invariants 

of self if self is finite, and is 0 otherwise. 

 

EXAMPLES:: 

 

sage: V = span([[1/2,0,0],[3/2,2,1],[0,0,1]],ZZ); W = V.span([V.0+2*V.1, 9*V.0+2*V.1, 4*V.2]) 

sage: Q = V/W; Q.annihilator() 

Principal ideal (16) of Integer Ring 

sage: Q.annihilator().gen() 

16 

 

sage: Q = V/V.span([V.0]); Q 

Finitely generated module V/W over Integer Ring with invariants (0, 0) 

sage: Q.annihilator() 

Principal ideal (0) of Integer Ring 

 

We check that :trac:`22720` is resolved:: 

 

sage: H=AdditiveAbelianGroup([]) 

sage: H.annihilator() 

Principal ideal (1) of Integer Ring 

""" 

if not self.is_finite(): 

g = 0 

elif self.cardinality() == 1: 

g = 1 

else: 

g = reduce(lcm, self.invariants()) 

return self.base_ring().ideal(g) 

 

def ngens(self): 

r""" 

Return the number of generators of self. 

 

(Note for developers: This is just the length of :meth:`.gens`, rather 

than of the minimal set of generators as returned by 

:meth:`.smith_form_gens`; these are the same in the 

:class:`~sage.modules.fg_pid.fgp_module.FGP_Module_class`, but not 

necessarily in derived classes.) 

 

EXAMPLES:: 

 

sage: A = (ZZ**2) / span([[4,0],[0,3]], ZZ) 

sage: A.ngens() 

1 

 

This works (but please don't do it in production code!) :: 

 

sage: A.gens = lambda: [1,2,"Barcelona!"] 

sage: A.ngens() 

3 

""" 

return len(self.gens()) 

 

def __hash__(self): 

r""" 

Calculate a hash for self. 

 

EXAMPLES:: 

 

sage: A = (ZZ**2) / span([[4,0],[0,3]], ZZ) 

sage: hash(A) 

1328975982 # 32-bit 

-7071641102956720018 # 64-bit 

""" 

return hash( (self.V(), self.W()) ) 

 

############################################################## 

# Useful for testing 

############################################################## 

 

def random_fgp_module(n, R=ZZ, finite=False): 

""" 

Return a random FGP module inside a rank n free module over R. 

 

INPUT: 

 

- ``n`` -- nonnegative integer 

 

- ``R`` -- base ring (default: ZZ) 

 

- ``finite`` -- bool (default: True); if True, make the random module finite. 

 

EXAMPLES:: 

 

sage: import sage.modules.fg_pid.fgp_module as fgp 

sage: fgp.random_fgp_module(4) 

Finitely generated module V/W over Integer Ring with invariants (4) 

""" 

K = R.fraction_field() 

V = K**n 

i = ZZ.random_element(max(n,1)) 

A = V.span([V.random_element() for _ in range(i)], R) 

if not finite: 

i = ZZ.random_element(i+1) 

while True: 

B = A.span([A.random_element() for _ in range(i)], R) 

#Q = A/B 

Q = FGP_Module_class(A,B,check=DEBUG) 

if not finite or Q.is_finite(): 

return Q 

 

def random_fgp_morphism_0(*args, **kwds): 

""" 

Construct a random fgp module using random_fgp_module, 

then construct a random morphism that sends each generator 

to a random multiple of itself. Inputs are the same 

as to random_fgp_module. 

 

EXAMPLES:: 

 

sage: import sage.modules.fg_pid.fgp_module as fgp 

sage: fgp.random_fgp_morphism_0(4) 

Morphism from module over Integer Ring with invariants (4,) to module with invariants (4,) that sends the generators to [(0)] 

 

""" 

A = random_fgp_module(*args, **kwds) 

return A.hom([ZZ.random_element()*g for g in A.smith_form_gens()]) 

 

def test_morphism_0(*args, **kwds): 

""" 

EXAMPLES:: 

 

sage: import sage.modules.fg_pid.fgp_module as fgp 

sage: s = 0 # we set a seed so results clearly and easily reproducible across runs. 

sage: set_random_seed(s); v = [fgp.test_morphism_0(1) for _ in range(30)] 

sage: set_random_seed(s); v = [fgp.test_morphism_0(2) for _ in range(30)] 

sage: set_random_seed(s); v = [fgp.test_morphism_0(3) for _ in range(10)] 

sage: set_random_seed(s); v = [fgp.test_morphism_0(i) for i in range(1,20)] 

sage: set_random_seed(s); v = [fgp.test_morphism_0(4) for _ in range(50)] # long time 

""" 

phi = random_fgp_morphism_0(*args, **kwds) 

K = phi.kernel() 

I = phi.image() 

from sage.misc.all import prod 

if prod(K.invariants()): 

assert prod(phi.domain().invariants()) % prod(K.invariants()) == 0 

assert I.is_submodule(phi.codomain()) 

if len(I.smith_form_gens()) > 0: 

x = phi.lift(I.smith_form_gen(0)) 

assert phi(x) == I.smith_form_gen(0)