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

r""" 

Toric lattices 

 

This module was designed as a part of the framework for toric varieties 

(:mod:`~sage.schemes.toric.variety`, 

:mod:`~sage.schemes.toric.fano_variety`). 

 

All toric lattices are isomorphic to `\ZZ^n` for some `n`, but will prevent 

you from doing "wrong" operations with objects from different lattices. 

 

AUTHORS: 

 

- Andrey Novoseltsev (2010-05-27): initial version. 

- Andrey Novoseltsev (2010-07-30): sublattices and quotients. 

 

EXAMPLES: 

 

The simplest way to create a toric lattice is to specify its dimension only:: 

 

sage: N = ToricLattice(3) 

sage: N 

3-d lattice N 

 

While our lattice ``N`` is called exactly "N" it is a coincidence: all 

lattices are called "N" by default:: 

 

sage: another_name = ToricLattice(3) 

sage: another_name 

3-d lattice N 

 

If fact, the above lattice is exactly the same as before as an object in 

memory:: 

 

sage: N is another_name 

True 

 

There are actually four names associated to a toric lattice and they all must 

be the same for two lattices to coincide:: 

 

sage: N, N.dual(), latex(N), latex(N.dual()) 

(3-d lattice N, 3-d lattice M, N, M) 

 

Notice that the lattice dual to ``N`` is called "M" which is standard in toric 

geometry. This happens only if you allow completely automatic handling of 

names:: 

 

sage: another_N = ToricLattice(3, "N") 

sage: another_N.dual() 

3-d lattice N* 

sage: N is another_N 

False 

 

What can you do with toric lattices? Well, their main purpose is to allow 

creation of elements of toric lattices:: 

 

sage: n = N([1,2,3]) 

sage: n 

N(1, 2, 3) 

sage: M = N.dual() 

sage: m = M(1,2,3) 

sage: m 

M(1, 2, 3) 

 

Dual lattices can act on each other:: 

 

sage: n * m 

14 

sage: m * n 

14 

 

You can also add elements of the same lattice or scale them:: 

 

sage: 2 * n 

N(2, 4, 6) 

sage: n * 2 

N(2, 4, 6) 

sage: n + n 

N(2, 4, 6) 

 

However, you cannot "mix wrong lattices" in your expressions:: 

 

sage: n + m 

Traceback (most recent call last): 

... 

TypeError: unsupported operand parent(s) for +: 

'3-d lattice N' and '3-d lattice M' 

sage: n * n 

Traceback (most recent call last): 

... 

TypeError: elements of the same toric lattice cannot be multiplied! 

sage: n == m 

False 

 

Note that ``n`` and ``m`` are not equal to each other even though they are 

both "just (1,2,3)." Moreover, you cannot easily convert elements between 

toric lattices:: 

 

sage: M(n) 

Traceback (most recent call last): 

... 

TypeError: N(1, 2, 3) cannot be converted to 3-d lattice M! 

 

If you really need to consider elements of one lattice as elements of another, 

you can either use intermediate conversion to "just a vector":: 

 

sage: ZZ3 = ZZ^3 

sage: n_in_M = M(ZZ3(n)) 

sage: n_in_M 

M(1, 2, 3) 

sage: n == n_in_M 

False 

sage: n_in_M == m 

True 

 

Or you can create a homomorphism from one lattice to any other:: 

 

sage: h = N.hom(identity_matrix(3), M) 

sage: h(n) 

M(1, 2, 3) 

 

.. WARNING:: 

 

While integer vectors (elements of `\ZZ^n`) are printed as ``(1,2,3)``, 

in the code ``(1,2,3)`` is a :class:`tuple`, which has nothing to do 

neither with vectors, nor with toric lattices, so the following is 

probably not what you want while working with toric geometry objects:: 

 

sage: (1,2,3) + (1,2,3) 

(1, 2, 3, 1, 2, 3) 

 

Instead, use syntax like :: 

 

sage: N(1,2,3) + N(1,2,3) 

N(2, 4, 6) 

""" 

# Parts of the "tutorial" above are also in toric_lattice_element.pyx. 

 

 

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

# Copyright (C) 2010 Andrey Novoseltsev <novoselt@gmail.com> 

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

# 

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

# 

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

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

from __future__ import print_function 

 

from sage.geometry.toric_lattice_element import (ToricLatticeElement, 

is_ToricLatticeElement) 

from sage.geometry.toric_plotter import ToricPlotter 

from sage.misc.all import latex 

from sage.structure.all import parent 

from sage.structure.richcmp import (richcmp_method, richcmp, rich_to_bool, 

richcmp_not_equal) 

from sage.modules.fg_pid.fgp_element import FGP_Element 

from sage.modules.fg_pid.fgp_module import FGP_Module_class 

from sage.modules.free_module import (FreeModule_ambient_pid, 

FreeModule_generic_pid, 

FreeModule_submodule_pid, 

FreeModule_submodule_with_basis_pid) 

from sage.rings.all import QQ, ZZ 

from sage.structure.factory import UniqueFactory 

 

 

def is_ToricLattice(x): 

r""" 

Check if ``x`` is a toric lattice. 

 

INPUT: 

 

- ``x`` -- anything. 

 

OUTPUT: 

 

- ``True`` if ``x`` is a toric lattice and ``False`` otherwise. 

 

EXAMPLES:: 

 

sage: from sage.geometry.toric_lattice import ( 

....: is_ToricLattice) 

sage: is_ToricLattice(1) 

False 

sage: N = ToricLattice(3) 

sage: N 

3-d lattice N 

sage: is_ToricLattice(N) 

True 

""" 

return isinstance(x, ToricLattice_generic) 

 

 

def is_ToricLatticeQuotient(x): 

r""" 

Check if ``x`` is a toric lattice quotient. 

 

INPUT: 

 

- ``x`` -- anything. 

 

OUTPUT: 

 

- ``True`` if ``x`` is a toric lattice quotient and ``False`` otherwise. 

 

EXAMPLES:: 

 

sage: from sage.geometry.toric_lattice import ( 

....: is_ToricLatticeQuotient) 

sage: is_ToricLatticeQuotient(1) 

False 

sage: N = ToricLattice(3) 

sage: N 

3-d lattice N 

sage: is_ToricLatticeQuotient(N) 

False 

sage: Q = N / N.submodule([(1,2,3), (3,2,1)]) 

sage: Q 

Quotient with torsion of 3-d lattice N 

by Sublattice <N(1, 2, 3), N(0, 4, 8)> 

sage: is_ToricLatticeQuotient(Q) 

True 

""" 

return isinstance(x, ToricLattice_quotient) 

 

 

class ToricLatticeFactory(UniqueFactory): 

r""" 

Create a lattice for toric geometry objects. 

 

INPUT: 

 

- ``rank`` -- nonnegative integer, the only mandatory parameter; 

 

- ``name`` -- string; 

 

- ``dual_name`` -- string; 

 

- ``latex_name`` -- string; 

 

- ``latex_dual_name`` -- string. 

 

OUTPUT: 

 

- lattice. 

 

A toric lattice is uniquely determined by its rank and associated names. 

There are four such "associated names" whose meaning should be clear from 

the names of the corresponding parameters, but the choice of default 

values is a little bit involved. So here is the full description of the 

"naming algorithm": 

 

#. If no names were given at all, then this lattice will be called "N" and 

the dual one "M". These are the standard choices in toric geometry. 

 

#. If ``name`` was given and ``dual_name`` was not, then ``dual_name`` 

will be ``name`` followed by "*". 

 

#. If LaTeX names were not given, they will coincide with the "usual" 

names, but if ``dual_name`` was constructed automatically, the trailing 

star will be typeset as a superscript. 

 

EXAMPLES: 

 

Let's start with no names at all and see how automatic names are given:: 

 

sage: L1 = ToricLattice(3) 

sage: L1 

3-d lattice N 

sage: L1.dual() 

3-d lattice M 

 

If we give the name "N" explicitly, the dual lattice will be called "N*":: 

 

sage: L2 = ToricLattice(3, "N") 

sage: L2 

3-d lattice N 

sage: L2.dual() 

3-d lattice N* 

 

However, we can give an explicit name for it too:: 

 

sage: L3 = ToricLattice(3, "N", "M") 

sage: L3 

3-d lattice N 

sage: L3.dual() 

3-d lattice M 

 

If you want, you may also give explicit LaTeX names:: 

 

sage: L4 = ToricLattice(3, "N", "M", r"\mathbb{N}", r"\mathbb{M}") 

sage: latex(L4) 

\mathbb{N} 

sage: latex(L4.dual()) 

\mathbb{M} 

 

While all four lattices above are called "N", only two of them are equal 

(and are actually the same):: 

 

sage: L1 == L2 

False 

sage: L1 == L3 

True 

sage: L1 is L3 

True 

sage: L1 == L4 

False 

 

The reason for this is that ``L2`` and ``L4`` have different names either 

for dual lattices or for LaTeX typesetting. 

""" 

 

def create_key(self, rank, name=None, dual_name=None, 

latex_name=None, latex_dual_name=None): 

""" 

Create a key that uniquely identifies this toric lattice. 

 

See :class:`ToricLattice <ToricLatticeFactory>` for documentation. 

 

.. WARNING:: 

 

You probably should not use this function directly. 

 

TESTS:: 

 

sage: ToricLattice.create_key(3) 

(3, 'N', 'M', 'N', 'M') 

sage: N = ToricLattice(3) 

sage: loads(dumps(N)) is N 

True 

sage: TestSuite(N).run() 

""" 

rank = int(rank) 

# Should we use standard M and N lattices? 

if name is None: 

if dual_name is not None: 

raise ValueError("you can name the dual lattice only if you " 

"also name the original one!") 

name = "N" 

dual_name = "M" 

if latex_name is None: 

latex_name = name 

# Now name and latex_name are set 

# The default for latex_dual_name depends on whether dual_name was 

# given or constructed, so we determine it before dual_name 

if latex_dual_name is None: 

latex_dual_name = (dual_name if dual_name is not None 

else latex_name + "^*") 

if dual_name is None: 

dual_name = name + "*" 

return (rank, name, dual_name, latex_name, latex_dual_name) 

 

def create_object(self, version, key): 

r""" 

Create the toric lattice described by ``key``. 

 

See :class:`ToricLattice <ToricLatticeFactory>` for documentation. 

 

.. WARNING:: 

 

You probably should not use this function directly. 

 

TESTS:: 

 

sage: key = ToricLattice.create_key(3) 

sage: ToricLattice.create_object(1, key) 

3-d lattice N 

""" 

return ToricLattice_ambient(*key) 

 

 

ToricLattice = ToricLatticeFactory("ToricLattice") 

 

 

# Possible TODO's: 

# - implement a better construction() method, which still will prohibit 

# operations mixing lattices by conversion to ZZ^n 

# - maybe __call__ is not the right place to prohibit conversion between 

# lattices (we need it now so that morphisms behave nicely) 

class ToricLattice_generic(FreeModule_generic_pid): 

r""" 

Abstract base class for toric lattices. 

""" 

 

Element = ToricLatticeElement 

 

# It is not recommended to override __call__ in Parent-derived objects 

# since it may interfere with the coercion model. We do it here to allow 

# N(1,2,3) to be interpreted as N([1,2,3]). We also prohibit N(m) where 

# m is an element of another lattice. Otherwise morphisms will care only 

# about dimension of lattices. 

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

r""" 

Construct a new element of ``self``. 

 

INPUT: 

 

- anything that can be interpreted as coordinates, except for elements 

of other lattices. 

 

OUTPUT: 

 

- :class:`~sage.geometry.toric_lattice_element.ToricLatticeElement`. 

 

TESTS:: 

 

sage: N = ToricLattice(3) 

sage: N.__call__([1,2,3]) 

N(1, 2, 3) 

sage: N([1,2,3]) # indirect test 

N(1, 2, 3) 

 

The point of overriding this function was to allow writing the above 

command as:: 

 

sage: N(1,2,3) 

N(1, 2, 3) 

 

We also test that the special treatment of zero still works:: 

 

sage: N(0) 

N(0, 0, 0) 

 

Quotients of toric lattices can be converted to a new toric 

lattice of the appropriate dimension:: 

 

sage: N3 = ToricLattice(3, 'N3') 

sage: Q = N3 / N3.span([ N3(1,2,3) ]) 

sage: Q.an_element() 

N3[0, 0, 1] 

sage: N2 = ToricLattice(2, 'N2') 

sage: N2( Q.an_element() ) 

N2(1, 0) 

""" 

supercall = super(ToricLattice_generic, self).__call__ 

if args == (0, ): 

# Special treatment for N(0) to return (0,...,0) 

return supercall(*args, **kwds) 

 

if (isinstance(args[0], ToricLattice_quotient_element) 

and args[0].parent().is_torsion_free()): 

# convert a torsion free quotient lattice 

return supercall(list(args[0]), **kwds) 

 

try: 

coordinates = [ZZ(_) for _ in args] 

except TypeError: 

# Prohibit conversion of elements of other lattices 

if (is_ToricLatticeElement(args[0]) 

and args[0].parent().ambient_module() 

is not self.ambient_module()): 

raise TypeError("%s cannot be converted to %s!" 

% (args[0], self)) 

# "Standard call" 

return supercall(*args, **kwds) 

# Coordinates were given without packing them into a list or a tuple 

return supercall(coordinates, **kwds) 

 

def _coerce_map_from_(self, other): 

""" 

Return a coercion map from ``other`` to ``self``, or None. 

 

This prevents the construction of coercion maps between 

lattices with different ambient modules, so :meth:`__call__` 

is invoked instead, which prohibits conversion:: 

 

sage: N = ToricLattice(3) 

sage: M = N.dual() 

sage: M(N(1,2,3)) 

Traceback (most recent call last): 

... 

TypeError: N(1, 2, 3) cannot be converted to 3-d lattice M! 

 

""" 

if (is_ToricLattice(other) and 

other.ambient_module() is not self.ambient_module()): 

return None 

return super(ToricLattice_generic, self)._convert_map_from_(other) 

 

def __contains__(self, point): 

r""" 

Check if ``point`` is an element of ``self``. 

 

INPUT: 

 

- ``point`` -- anything. 

 

OUTPUT: 

 

- ``True`` if ``point`` is an element of ``self``, ``False`` 

otherwise. 

 

TESTS:: 

 

sage: N = ToricLattice(3) 

sage: M = N.dual() 

sage: L = ToricLattice(3, "L") 

sage: 1 in N 

False 

sage: (1,0) in N 

False 

sage: (1,0,0) in N 

True 

sage: N(1,0,0) in N 

True 

sage: M(1,0,0) in N 

False 

sage: L(1,0,0) in N 

False 

sage: (1/2,0,0) in N 

False 

sage: (2/2,0,0) in N 

True 

""" 

try: 

self(point) 

except TypeError: 

return False 

return True 

 

# We need to override this function, otherwise e.g. the sum of elements of 

# different lattices of the same dimension will live in ZZ^n. 

def construction(self): 

r""" 

Return the functorial construction of ``self``. 

 

OUTPUT: 

 

- ``None``, we do not think of toric lattices as constructed from 

simpler objects since we do not want to perform arithmetic involving 

different lattices. 

 

TESTS:: 

 

sage: print(ToricLattice(3).construction()) 

None 

""" 

return None 

 

def direct_sum(self, other): 

r""" 

Return the direct sum with ``other``. 

 

INPUT: 

 

- ``other`` -- a toric lattice or more general module. 

 

OUTPUT: 

 

The direct sum of ``self`` and ``other`` as `\ZZ`-modules. If 

``other`` is a :class:`ToricLattice <ToricLatticeFactory>`, 

another toric lattice will be returned. 

 

EXAMPLES:: 

 

sage: K = ToricLattice(3, 'K') 

sage: L = ToricLattice(3, 'L') 

sage: N = K.direct_sum(L); N 

6-d lattice K+L 

sage: N, N.dual(), latex(N), latex(N.dual()) 

(6-d lattice K+L, 6-d lattice K*+L*, K \oplus L, K^* \oplus L^*) 

 

With default names:: 

 

sage: N = ToricLattice(3).direct_sum(ToricLattice(2)) 

sage: N, N.dual(), latex(N), latex(N.dual()) 

(5-d lattice N+N, 5-d lattice M+M, N \oplus N, M \oplus M) 

 

If ``other`` is not a :class:`ToricLattice 

<ToricLatticeFactory>`, fall back to sum of modules:: 

 

sage: ToricLattice(3).direct_sum(ZZ^2) 

Free module of degree 5 and rank 5 over Integer Ring 

Echelon basis matrix: 

[1 0 0 0 0] 

[0 1 0 0 0] 

[0 0 1 0 0] 

[0 0 0 1 0] 

[0 0 0 0 1] 

""" 

if not isinstance(other, ToricLattice_generic): 

return super(ToricLattice_generic, self).direct_sum(other) 

 

def make_name(N1, N2, use_latex=False): 

if use_latex: 

return latex(N1)+ ' \oplus ' +latex(N2) 

else: 

return N1._name+ '+' +N2._name 

 

rank = self.rank() + other.rank() 

name = make_name(self, other, False) 

dual_name = make_name(self.dual(), other.dual(), False) 

latex_name = make_name(self, other, True) 

latex_dual_name = make_name(self.dual(), other.dual(), True) 

return ToricLattice(rank, name, dual_name, latex_name, latex_dual_name) 

 

def intersection(self, other): 

r""" 

Return the intersection of ``self`` and ``other``. 

 

INPUT: 

 

- ``other`` - a toric (sub)lattice.dual 

 

OUTPUT: 

 

- a toric (sub)lattice. 

 

EXAMPLES:: 

 

sage: N = ToricLattice(3) 

sage: Ns1 = N.submodule([N(2,4,0), N(9,12,0)]) 

sage: Ns2 = N.submodule([N(1,4,9), N(9,2,0)]) 

sage: Ns1.intersection(Ns2) 

Sublattice <N(54, 12, 0)> 

 

Note that if one of the intersecting sublattices is a sublattice of 

another, no new lattices will be constructed:: 

 

sage: N.intersection(N) is N 

True 

sage: Ns1.intersection(N) is Ns1 

True 

sage: N.intersection(Ns1) is Ns1 

True 

""" 

# Lattice-specific input check 

if not is_ToricLattice(other): 

raise TypeError("%s is not a toric lattice!" % other) 

if self.ambient_module() != other.ambient_module(): 

raise ValueError("%s and %s have different ambient lattices!" % 

(self, other)) 

# Construct a generic intersection, but make sure to return a lattice. 

I = super(ToricLattice_generic, self).intersection(other) 

if not is_ToricLattice(I): 

I = self.ambient_module().submodule(I.basis()) 

return I 

 

def quotient(self, sub, check=True, 

positive_point=None, positive_dual_point=None): 

""" 

Return the quotient of ``self`` by the given sublattice ``sub``. 

 

INPUT: 

 

- ``sub`` -- sublattice of self; 

 

- ``check`` -- (default: True) whether or not to check that ``sub`` is 

a valid sublattice. 

 

If the quotient is one-dimensional and torsion free, the 

following two mutually exclusive keyword arguments are also 

allowed. They decide the sign choice for the (single) 

generator of the quotient lattice: 

 

- ``positive_point`` -- a lattice point of ``self`` not in the 

sublattice ``sub`` (that is, not zero in the quotient 

lattice). The quotient generator will be in the same 

direction as ``positive_point``. 

 

- ``positive_dual_point`` -- a dual lattice point. The 

quotient generator will be chosen such that its lift has a 

positive product with ``positive_dual_point``. Note: if 

``positive_dual_point`` is not zero on the sublattice 

``sub``, then the notion of positivity will depend on the 

choice of lift! 

 

EXAMPLES:: 

 

sage: N = ToricLattice(3) 

sage: Ns = N.submodule([N(2,4,0), N(9,12,0)]) 

sage: Q = N/Ns 

sage: Q 

Quotient with torsion of 3-d lattice N 

by Sublattice <N(1, 8, 0), N(0, 12, 0)> 

 

Attempting to quotient one lattice by a sublattice of another 

will result in a ``ValueError``:: 

 

sage: N = ToricLattice(3) 

sage: M = ToricLattice(3, name='M') 

sage: Ms = M.submodule([M(2,4,0), M(9,12,0)]) 

sage: N.quotient(Ms) 

Traceback (most recent call last): 

... 

ValueError: M(1, 8, 0) can not generate a sublattice of 

3-d lattice N 

 

However, if we forget the sublattice structure, then it is 

possible to quotient by vector spaces or modules constructed 

from any sublattice:: 

 

sage: N = ToricLattice(3) 

sage: M = ToricLattice(3, name='M') 

sage: Ms = M.submodule([M(2,4,0), M(9,12,0)]) 

sage: N.quotient(Ms.vector_space()) 

Quotient with torsion of 3-d lattice N by Sublattice 

<N(1, 8, 0), N(0, 12, 0)> 

sage: N.quotient(Ms.sparse_module()) 

Quotient with torsion of 3-d lattice N by Sublattice 

<N(1, 8, 0), N(0, 12, 0)> 

 

See :class:`ToricLattice_quotient` for more examples. 

 

TESTS: 

 

We check that :trac:`19603` is fixed:: 

 

sage: K = Cone([(1,0,0),(0,1,0)]) 

sage: K.lattice() 

3-d lattice N 

sage: K.orthogonal_sublattice() 

Sublattice <M(0, 0, 1)> 

sage: K.lattice().quotient(K.orthogonal_sublattice()) 

Traceback (most recent call last): 

... 

ValueError: M(0, 0, 1) can not generate a sublattice of 

3-d lattice N 

 

We can quotient by the trivial sublattice:: 

 

sage: N = ToricLattice(3) 

sage: N.quotient(N.zero_submodule()) 

3-d lattice, quotient of 3-d lattice N by Sublattice <> 

 

We can quotient a lattice by itself:: 

 

sage: N = ToricLattice(3) 

sage: N.quotient(N) 

0-d lattice, quotient of 3-d lattice N by Sublattice 

<N(1, 0, 0), N(0, 1, 0), N(0, 0, 1)> 

""" 

return ToricLattice_quotient(self, sub, check, 

positive_point, positive_dual_point) 

 

def saturation(self): 

r""" 

Return the saturation of ``self``. 

 

OUTPUT: 

 

- a :class:`toric lattice <ToricLatticeFactory>`. 

 

EXAMPLES:: 

 

sage: N = ToricLattice(3) 

sage: Ns = N.submodule([(1,2,3), (4,5,6)]) 

sage: Ns 

Sublattice <N(1, 2, 3), N(0, 3, 6)> 

sage: Ns_sat = Ns.saturation() 

sage: Ns_sat 

Sublattice <N(1, 0, -1), N(0, 1, 2)> 

sage: Ns_sat is Ns_sat.saturation() 

True 

""" 

S = super(ToricLattice_generic, self).saturation() 

return S if is_ToricLattice(S) else self.ambient_module().submodule(S) 

 

def span(self, gens, base_ring=ZZ, *args, **kwds): 

""" 

Return the span of the given generators. 

 

INPUT: 

 

- ``gens`` -- list of elements of the ambient vector space of 

``self``. 

 

- ``base_ring`` -- (default: `\ZZ`) base ring for the generated module. 

 

OUTPUT: 

 

- submodule spanned by ``gens``. 

 

.. NOTE:: 

 

The output need not be a submodule of ``self``, nor even of the 

ambient space. It must, however, be contained in the ambient 

vector space. 

 

See also :meth:`span_of_basis`, 

:meth:`~sage.modules.free_module.FreeModule_generic_pid.submodule`, 

and 

:meth:`~sage.modules.free_module.FreeModule_generic_pid.submodule_with_basis`, 

 

EXAMPLES:: 

 

sage: N = ToricLattice(3) 

sage: Ns = N.submodule([N.gen(0)]) 

sage: Ns.span([N.gen(1)]) 

Sublattice <N(0, 1, 0)> 

sage: Ns.submodule([N.gen(1)]) 

Traceback (most recent call last): 

... 

ArithmeticError: Argument gens (= [N(0, 1, 0)]) 

does not generate a submodule of self. 

""" 

A = self.ambient_module() 

if base_ring is ZZ and all(g in A for g in gens): 

return ToricLattice_sublattice(A, gens) 

for g in gens: 

if is_ToricLatticeElement(g) and g not in A: 

raise ValueError("%s can not generate a sublattice of %s" 

% (g, A)) 

else: 

return super(ToricLattice_generic, self).span(gens, base_ring, 

*args, **kwds) 

 

def span_of_basis(self, basis, base_ring=ZZ, *args, **kwds): 

r""" 

Return the submodule with the given ``basis``. 

 

INPUT: 

 

- ``basis`` -- list of elements of the ambient vector space of 

``self``. 

 

- ``base_ring`` -- (default: `\ZZ`) base ring for the generated module. 

 

OUTPUT: 

 

- submodule spanned by ``basis``. 

 

.. NOTE:: 

 

The output need not be a submodule of ``self``, nor even of the 

ambient space. It must, however, be contained in the ambient 

vector space. 

 

See also :meth:`span`, 

:meth:`~sage.modules.free_module.FreeModule_generic_pid.submodule`, 

and 

:meth:`~sage.modules.free_module.FreeModule_generic_pid.submodule_with_basis`, 

 

EXAMPLES:: 

 

sage: N = ToricLattice(3) 

sage: Ns = N.span_of_basis([(1,2,3)]) 

sage: Ns.span_of_basis([(2,4,0)]) 

Sublattice <N(2, 4, 0)> 

sage: Ns.span_of_basis([(1/5,2/5,0), (1/7,1/7,0)]) 

Free module of degree 3 and rank 2 over Integer Ring 

User basis matrix: 

[1/5 2/5 0] 

[1/7 1/7 0] 

 

Of course the input basis vectors must be linearly independent:: 

 

sage: Ns.span_of_basis([(1,2,0), (2,4,0)]) 

Traceback (most recent call last): 

... 

ValueError: The given basis vectors must be linearly independent. 

""" 

A = self.ambient_module() 

if base_ring is ZZ and all(g in A for g in basis): 

return ToricLattice_sublattice_with_basis(A, basis) 

for g in basis: 

if is_ToricLatticeElement(g) and g not in A: 

raise ValueError("%s can not generate a sublattice of %s" 

% (g, A)) 

else: 

return super(ToricLattice_generic, self).span_of_basis( 

basis, base_ring, *args, **kwds) 

 

 

@richcmp_method 

class ToricLattice_ambient(ToricLattice_generic, FreeModule_ambient_pid): 

r""" 

Create a toric lattice. 

 

See :class:`ToricLattice <ToricLatticeFactory>` for documentation. 

 

.. WARNING:: 

 

There should be only one toric lattice with the given rank and 

associated names. Using this class directly to create toric lattices 

may lead to unexpected results. Please, use :class:`ToricLattice 

<ToricLatticeFactory>` to create toric lattices. 

 

TESTS:: 

 

sage: N = ToricLattice(3, "N", "M", "N", "M") 

sage: N 

3-d lattice N 

sage: TestSuite(N).run() 

""" 

 

Element = ToricLatticeElement 

 

def __init__(self, rank, name, dual_name, latex_name, latex_dual_name): 

r""" 

See :class:`ToricLattice <ToricLatticeFactory>` for documentation. 

 

TESTS:: 

 

sage: ToricLattice(3, "N", "M", "N", "M") 

3-d lattice N 

""" 

super(ToricLattice_ambient, self).__init__(ZZ, rank) 

self._name = name 

self._dual_name = dual_name 

self._latex_name = latex_name 

self._latex_dual_name = latex_dual_name 

 

def __richcmp__(self, right, op): 

r""" 

Compare ``self`` and ``right``. 

 

INPUT: 

 

- ``right`` -- anything. 

 

OUTPUT: 

 

boolean 

 

There is equality if ``right`` is a toric lattice of the same 

dimension as ``self`` and their associated names are the 

same. 

 

TESTS:: 

 

sage: N3 = ToricLattice(3) 

sage: N4 = ToricLattice(4) 

sage: M3 = N3.dual() 

sage: N3 < N4 

True 

sage: N3 > M3 

True 

sage: N3 == ToricLattice(3) 

True 

""" 

if self is right: 

return rich_to_bool(op, 0) 

if type(self) != type(right): 

return NotImplemented 

 

lx = self.rank() 

rx = right.rank() 

if lx != rx: 

return richcmp_not_equal(lx, rx, op) 

# If lattices are the same as ZZ-modules, compare associated names 

return richcmp([self._name, self._dual_name, 

self._latex_name, self._latex_dual_name], 

[right._name, right._dual_name, 

right._latex_name, right._latex_dual_name], op) 

 

def _latex_(self): 

r""" 

Return a LaTeX representation of ``self``. 

 

OUTPUT: 

 

- string. 

 

TESTS:: 

 

sage: L = ToricLattice(3, "L") 

sage: L.dual()._latex_() 

'L^*' 

""" 

return self._latex_name 

 

def _repr_(self): 

r""" 

Return a string representation of ``self``. 

 

OUTPUT: 

 

- string. 

 

TESTS:: 

 

sage: L = ToricLattice(3, "L") 

sage: L.dual()._repr_() 

'3-d lattice L*' 

""" 

return "%d-d lattice %s" % (self.dimension(), self._name) 

 

def ambient_module(self): 

r""" 

Return the ambient module of ``self``. 

 

OUTPUT: 

 

- :class:`toric lattice <ToricLatticeFactory>`. 

 

.. NOTE:: 

 

For any ambient toric lattice its ambient module is the lattice 

itself. 

 

EXAMPLES:: 

 

sage: N = ToricLattice(3) 

sage: N.ambient_module() 

3-d lattice N 

sage: N.ambient_module() is N 

True 

""" 

return self 

 

def dual(self): 

r""" 

Return the lattice dual to ``self``. 

 

OUTPUT: 

 

- :class:`toric lattice <ToricLatticeFactory>`. 

 

EXAMPLES:: 

 

sage: N = ToricLattice(3) 

sage: N 

3-d lattice N 

sage: M = N.dual() 

sage: M 

3-d lattice M 

sage: M.dual() is N 

True 

 

Elements of dual lattices can act on each other:: 

 

sage: n = N(1,2,3) 

sage: m = M(4,5,6) 

sage: n * m 

32 

sage: m * n 

32 

""" 

if "_dual" not in self.__dict__: 

self._dual = ToricLattice(self.rank(), self._dual_name, 

self._name, self._latex_dual_name, self._latex_name) 

return self._dual 

 

def plot(self, **options): 

r""" 

Plot ``self``. 

 

INPUT: 

 

- any options for toric plots (see :func:`toric_plotter.options 

<sage.geometry.toric_plotter.options>`), none are mandatory. 

 

OUTPUT: 

 

- a plot. 

 

EXAMPLES:: 

 

sage: N = ToricLattice(3) 

sage: N.plot() 

Graphics3d Object 

""" 

if "show_lattice" not in options: 

# Unless user made an explicit decision, we assume that lattice 

# should be visible no matter what is the size of the bounding box. 

options["show_lattice"] = True 

tp = ToricPlotter(options, self.degree()) 

tp.adjust_options() 

return tp.plot_lattice() 

 

 

class ToricLattice_sublattice_with_basis(ToricLattice_generic, 

FreeModule_submodule_with_basis_pid): 

r""" 

Construct the sublattice of ``ambient`` toric lattice with given ``basis``. 

 

INPUT (same as for 

:class:`~sage.modules.free_module.FreeModule_submodule_with_basis_pid`): 

 

- ``ambient`` -- ambient :class:`toric lattice <ToricLatticeFactory>` for 

this sublattice; 

 

- ``basis`` -- list of linearly independent elements of ``ambient``, these 

elements will be used as the default basis of the constructed 

sublattice; 

 

- see the base class for other available options. 

 

OUTPUT: 

 

- sublattice of a toric lattice with a user-specified basis. 

 

See also :class:`ToricLattice_sublattice` if you do not want to specify an 

explicit basis. 

 

EXAMPLES: 

 

The intended way to get objects of this class is to use 

:meth:`submodule_with_basis` method of toric lattices:: 

 

sage: N = ToricLattice(3) 

sage: sublattice = N.submodule_with_basis([(1,1,0), (3,2,1)]) 

sage: sublattice.has_user_basis() 

True 

sage: sublattice.basis() 

[ 

N(1, 1, 0), 

N(3, 2, 1) 

] 

 

Even if you have provided your own basis, you still can access the 

"standard" one:: 

 

sage: sublattice.echelonized_basis() 

[ 

N(1, 0, 1), 

N(0, 1, -1) 

] 

""" 

 

def _repr_(self): 

r""" 

Return a string representation of ``self``. 

 

OUTPUT: 

 

- string. 

 

TESTS:: 

 

sage: L = ToricLattice(3, "L") 

sage: L.submodule_with_basis([(3,2,1),(1,2,3)]) 

Sublattice <L(3, 2, 1), L(1, 2, 3)> 

sage: print(L.submodule([(3,2,1),(1,2,3)])._repr_()) 

Sublattice <L(1, 2, 3), L(0, 4, 8)> 

""" 

s = 'Sublattice ' 

s += '<' 

s += ', '.join(map(str,self.basis())) 

s += '>' 

return s 

 

def _latex_(self): 

r""" 

Return a LaTeX representation of ``self``. 

 

OUTPUT: 

 

- string. 

 

TESTS:: 

 

sage: L = ToricLattice(3, "L") 

sage: L.submodule_with_basis([(3,2,1),(1,2,3)])._latex_() 

'\\left\\langle\\left(3,\\,2,\\,1\\right)_{L}, 

\\left(1,\\,2,\\,3\\right)_{L}\\right\\rangle' 

sage: L.submodule([(3,2,1),(1,2,3)])._latex_() 

'\\left\\langle\\left(1,\\,2,\\,3\\right)_{L}, 

\\left(0,\\,4,\\,8\\right)_{L}\\right\\rangle' 

""" 

s = '\\left\\langle' 

s += ', '.join([ b._latex_() for b in self.basis() ]) 

s += '\\right\\rangle' 

return s 

 

def dual(self): 

r""" 

Return the lattice dual to ``self``. 

 

OUTPUT: 

 

- a :class:`toric lattice quotient <ToricLattice_quotient>`. 

 

EXAMPLES:: 

 

sage: N = ToricLattice(3) 

sage: Ns = N.submodule([(1,1,0), (3,2,1)]) 

sage: Ns.dual() 

2-d lattice, quotient of 3-d lattice M by Sublattice <M(1, -1, -1)> 

""" 

if "_dual" not in self.__dict__: 

if not self is self.saturation(): 

raise ValueError("only dual lattices of saturated sublattices " 

"can be constructed! Got %s." % self) 

self._dual = (self.ambient_module().dual() / 

self.basis_matrix().transpose().integer_kernel()) 

self._dual._dual = self 

return self._dual 

 

def plot(self, **options): 

r""" 

Plot ``self``. 

 

INPUT: 

 

- any options for toric plots (see :func:`toric_plotter.options 

<sage.geometry.toric_plotter.options>`), none are mandatory. 

 

OUTPUT: 

 

- a plot. 

 

EXAMPLES:: 

 

sage: N = ToricLattice(3) 

sage: sublattice = N.submodule_with_basis([(1,1,0), (3,2,1)]) 

sage: sublattice.plot() 

Graphics3d Object 

 

Now we plot both the ambient lattice and its sublattice:: 

 

sage: N.plot() + sublattice.plot(point_color="red") 

Graphics3d Object 

""" 

if "show_lattice" not in options: 

# Unless user made an explicit decision, we assume that lattice 

# should be visible no matter what is the size of the bounding box. 

options["show_lattice"] = True 

if "lattice_filter" in options: 

old = options["lattice_filter"] 

options["lattice_filter"] = lambda pt: pt in self and old(pt) 

else: 

options["lattice_filter"] = lambda pt: pt in self 

tp = ToricPlotter(options, self.degree()) 

tp.adjust_options() 

return tp.plot_lattice() 

 

 

class ToricLattice_sublattice(ToricLattice_sublattice_with_basis, 

FreeModule_submodule_pid): 

r""" 

Construct the sublattice of ``ambient`` toric lattice generated by ``gens``. 

 

INPUT (same as for 

:class:`~sage.modules.free_module.FreeModule_submodule_pid`): 

 

- ``ambient`` -- ambient :class:`toric lattice <ToricLatticeFactory>` for 

this sublattice; 

 

- ``gens`` -- list of elements of ``ambient`` generating the constructed 

sublattice; 

 

- see the base class for other available options. 

 

OUTPUT: 

 

- sublattice of a toric lattice with an automatically chosen basis. 

 

See also :class:`ToricLattice_sublattice_with_basis` if you want to 

specify an explicit basis. 

 

EXAMPLES: 

 

The intended way to get objects of this class is to use 

:meth:`submodule` method of toric lattices:: 

 

sage: N = ToricLattice(3) 

sage: sublattice = N.submodule([(1,1,0), (3,2,1)]) 

sage: sublattice.has_user_basis() 

False 

sage: sublattice.basis() 

[ 

N(1, 0, 1), 

N(0, 1, -1) 

] 

 

For sublattices without user-specified basis, the basis obtained above is 

the same as the "standard" one:: 

 

sage: sublattice.echelonized_basis() 

[ 

N(1, 0, 1), 

N(0, 1, -1) 

] 

""" 

pass 

 

 

 

class ToricLattice_quotient_element(FGP_Element): 

r""" 

Create an element of a toric lattice quotient. 

 

.. WARNING:: 

 

You probably should not construct such elements explicitly. 

 

INPUT: 

 

- same as for :class:`~sage.modules.fg_pid.fgp_element.FGP_Element`. 

 

OUTPUT: 

 

- element of a toric lattice quotient. 

 

TESTS:: 

 

sage: N = ToricLattice(3) 

sage: sublattice = N.submodule([(1,1,0), (3,2,1)]) 

sage: Q = N/sublattice 

sage: e = Q(1,2,3) 

sage: e 

N[1, 2, 3] 

sage: e2 = Q(N(2,3,3)) 

sage: e2 

N[2, 3, 3] 

sage: e == e2 

True 

sage: e.vector() 

(4) 

sage: e2.vector() 

(4) 

""" 

 

def _latex_(self): 

r""" 

Return a LaTeX representation of ``self``. 

 

OUTPUT: 

 

- string. 

 

TESTS:: 

 

sage: N = ToricLattice(3) 

sage: Ns = N.submodule([N(2,4,0), N(9,12,0)]) 

sage: Q = N/Ns 

sage: print(Q.gen(0)._latex_()) 

\left[0,\,1,\,0\right]_{N} 

""" 

return latex(self.lift()).replace("(", "[", 1).replace(")", "]", 1) 

 

def _repr_(self): 

r""" 

Return a string representation of ``self``. 

 

OUTPUT: 

 

- string. 

 

TESTS:: 

 

sage: N = ToricLattice(3) 

sage: Ns = N.submodule([N(2,4,0), N(9,12,0)]) 

sage: Q = N/Ns 

sage: print(Q.gen(0)._repr_()) 

N[0, 1, 0] 

""" 

return str(self.lift()).replace("(", "[", 1).replace(")", "]", 1) 

 

def set_immutable(self): 

r""" 

Make ``self`` immutable. 

 

OUTPUT: 

 

- none. 

 

.. note:: Elements of toric lattice quotients are always immutable, so 

this method does nothing, it is introduced for compatibility 

purposes only. 

 

EXAMPLES:: 

 

sage: N = ToricLattice(3) 

sage: Ns = N.submodule([N(2,4,0), N(9,12,0)]) 

sage: Q = N/Ns 

sage: Q.0.set_immutable() 

""" 

pass 

 

 

class ToricLattice_quotient(FGP_Module_class): 

r""" 

Construct the quotient of a toric lattice ``V`` by its sublattice ``W``. 

 

INPUT: 

 

- ``V`` -- ambient toric lattice; 

 

- ``W`` -- sublattice of ``V``; 

 

- ``check`` -- (default: ``True``) whether to check correctness of input 

or not. 

 

If the quotient is one-dimensional and torsion free, the following 

two mutually exclusive keyword arguments are also allowed. They 

decide the sign choice for the (single) generator of the quotient 

lattice: 

 

- ``positive_point`` -- a lattice point of ``self`` not in the 

sublattice ``sub`` (that is, not zero in the quotient 

lattice). The quotient generator will be in the same direction 

as ``positive_point``. 

 

- ``positive_dual_point`` -- a dual lattice point. The quotient 

generator will be chosen such that its lift has a positive 

product with ``positive_dual_point``. Note: if 

``positive_dual_point`` is not zero on the sublattice ``sub``, 

then the notion of positivity will depend on the choice of lift! 

 

OUTPUT: 

 

- quotient of ``V`` by ``W``. 

 

EXAMPLES: 

 

The intended way to get objects of this class is to use 

:meth:`quotient` method of toric lattices:: 

 

sage: N = ToricLattice(3) 

sage: sublattice = N.submodule([(1,1,0), (3,2,1)]) 

sage: Q = N/sublattice 

sage: Q 

1-d lattice, quotient of 3-d lattice N by Sublattice <N(1, 0, 1), N(0, 1, -1)> 

sage: Q.gens() 

(N[0, 0, 1],) 

 

Here, ``sublattice`` happens to be of codimension one in ``N``. If 

you want to prescribe the sign of the quotient generator, you can 

do either:: 

 

sage: Q = N.quotient(sublattice, positive_point=N(0,0,-1)); Q 

1-d lattice, quotient of 3-d lattice N by Sublattice <N(1, 0, 1), N(0, 1, -1)> 

sage: Q.gens() 

(N[0, 0, -1],) 

 

or:: 

 

sage: M = N.dual() 

sage: Q = N.quotient(sublattice, positive_dual_point=M(0,0,-1)); Q 

1-d lattice, quotient of 3-d lattice N by Sublattice <N(1, 0, 1), N(0, 1, -1)> 

sage: Q.gens() 

(N[0, 0, -1],) 

 

TESTS:: 

 

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

True 

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

True 

""" 

 

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

r""" 

The constructor 

 

See :class:`ToricLattice_quotient` for an explanation of the arguments. 

 

EXAMPLES:: 

 

sage: N = ToricLattice(3) 

sage: from sage.geometry.toric_lattice import ToricLattice_quotient 

sage: ToricLattice_quotient(N, N.span([N(1,2,3)])) 

2-d lattice, quotient of 3-d lattice N by Sublattice <N(1, 2, 3)> 

 

An ``ArithmeticError`` will be raised if ``W`` is not a 

sublattice of ``V``:: 

 

sage: N = ToricLattice(3) 

sage: Ns = N.submodule([N.gen(0)]) 

sage: Ns 

Sublattice <N(1, 0, 0)> 

sage: Ns.span([N.gen(1)]) 

Sublattice <N(0, 1, 0)> 

sage: Ns.quotient(Ns.span([N.gen(1)])) 

Traceback (most recent call last): 

... 

ArithmeticError: W must be a sublattice of V 

""" 

if check: 

try: 

W = V.submodule(W) 

except (TypeError, ArithmeticError): 

raise ArithmeticError("W must be a sublattice of V") 

super(ToricLattice_quotient, self).__init__(V, W, check) 

if (positive_point, positive_dual_point) == (None, None): 

self._flip_sign_of_generator = False 

return 

 

self._flip_sign_of_generator = False 

assert self.is_torsion_free() and self.ngens()==1, \ 

'You may only specify a positive direction in the codimension one case.' 

quotient_generator = self.gen(0) 

lattice = self.V().ambient_module() 

if (positive_point is not None) and (positive_dual_point is None): 

assert positive_point in lattice, 'positive_point must be a lattice point.' 

point_quotient = self(positive_point) 

scalar_product = quotient_generator.vector()[0] * point_quotient.vector()[0] 

if scalar_product==0: 

raise ValueError(str(positive_point)+' is zero in the quotient.') 

elif (positive_point is None) and (positive_dual_point is not None): 

assert positive_dual_point in lattice.dual(), 'positive_dual_point must be a dual lattice point.' 

scalar_product = quotient_generator.lift() * positive_dual_point 

if scalar_product==0: 

raise ValueError(str(positive_dual_point)+' is zero on the lift of the quotient generator.') 

else: 

raise ValueError('You may not specify both positive_point and positive_dual_point.') 

self._flip_sign_of_generator = (scalar_product<0) 

 

def gens(self): 

""" 

Return the generators of the quotient. 

 

OUTPUT: 

 

A tuple of :class:`ToricLattice_quotient_element` generating 

the quotient. 

 

EXAMPLES:: 

 

sage: N = ToricLattice(3) 

sage: Q = N.quotient(N.span([N(1,2,3), N(0,2,1)]), positive_point=N(0,-1,0)) 

sage: Q.gens() 

(N[0, -1, 0],) 

""" 

gens = self.smith_form_gens() 

if self._flip_sign_of_generator: 

assert len(gens)==1 

return (-gens[0],) 

else: 

return gens 

 

# Should be overridden in derived classes. 

Element = ToricLattice_quotient_element 

 

def _element_constructor_(self, *x, **kwds): 

r""" 

Construct an element of ``self``. 

 

INPUT: 

 

- element of a compatible toric object (lattice, sublattice, quotient) 

or something that defines such an element (list, generic vector, 

etc.). 

 

OUTPUT: 

 

- :class:`toric lattice quotient element 

<ToricLattice_quotient_element>`. 

 

EXAMPLES:: 

 

sage: N = ToricLattice(3) 

sage: Ns = N.submodule([N(2,4,0), N(9,12,0)]) 

sage: Q = N/Ns 

sage: x = Q(1,2,3) # indirect doctest 

sage: x 

N[1, 2, 3] 

sage: type(x) 

<class 'sage.geometry.toric_lattice.ToricLattice_quotient_with_category.element_class'> 

sage: x is Q(x) 

True 

sage: x.parent() is Q 

True 

sage: x == Q(N(1,2,3)) 

True 

sage: y = Q(3,6,3) 

sage: y 

N[3, 6, 3] 

sage: x == y 

True 

""" 

if len(x) == 1 and (x[0] not in ZZ or x[0] == 0): 

x = x[0] 

if parent(x) is self: 

return x 

try: 

x = x.lift() 

except AttributeError: 

pass 

try: 

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

except TypeError: 

return self.linear_combination_of_smith_form_gens(x) 

 

def _latex_(self): 

r""" 

Return a LaTeX representation of ``self``. 

 

OUTPUT: 

 

- string. 

 

TESTS:: 

 

sage: N = ToricLattice(3) 

sage: Ns = N.submodule([N(2,4,0), N(9,12,0)]) 

sage: Q = N/Ns 

sage: print(Q._latex_()) 

N / \left\langle\left(1,\,8,\,0\right)_{N}, \left(0,\,12,\,0\right)_{N}\right\rangle 

sage: Ns = N.submodule([N(1,4,0)]) 

sage: Q = N/Ns 

sage: print(Q._latex_()) 

N / \left\langle\left(1,\,4,\,0\right)_{N}\right\rangle 

""" 

return "%s / %s" % (latex(self.V()), latex(self.W())) 

 

def _repr_(self): 

r""" 

Return a string representation of ``self``. 

 

OUTPUT: 

 

- string. 

 

TESTS:: 

 

sage: N = ToricLattice(3) 

sage: Ns = N.submodule([N(2,4,0), N(9,12,0)]) 

sage: Q = N/Ns 

sage: print(Q._repr_()) 

Quotient with torsion of 3-d lattice N 

by Sublattice <N(1, 8, 0), N(0, 12, 0)> 

sage: Ns = N.submodule([N(1,4,0)]) 

sage: Q = N/Ns 

sage: print(Q._repr_()) 

2-d lattice, quotient of 3-d lattice N 

by Sublattice <N(1, 4, 0)> 

""" 

if self.is_torsion_free(): 

return "%d-d lattice, quotient of %s by %s" % (self.rank(), 

self.V(), self.W()) 

else: 

return "Quotient with torsion of %s by %s" % (self.V(), self.W()) 

 

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

r""" 

Construct new quotient modules. 

 

INPUT: 

 

- ``V`` -- ambient toric lattice; 

 

- ``W`` -- sublattice of ``V``; 

 

- ``check`` -- (default: ``True``) whether to check 

correctness of input or not. 

 

TESTS:: 

 

sage: N = ToricLattice(3) 

sage: Ns = N.submodule([N(2,4,0), N(9,12,0)]) 

sage: Q = N/Ns; Q 

Quotient with torsion of 3-d lattice N by Sublattice <N(1, 8, 0), N(0, 12, 0)> 

sage: Q._module_constructor(N,Ns) 

Quotient with torsion of 3-d lattice N by Sublattice <N(1, 8, 0), N(0, 12, 0)> 

""" 

return ToricLattice_quotient(V,W,check) 

 

def base_extend(self, R): 

""" 

Return the base change of ``self`` to the ring ``R``. 

 

INPUT: 

 

- ``R`` -- either `\ZZ` or `\QQ`. 

 

OUTPUT: 

 

- ``self`` if `R=\ZZ`, quotient of the base extension of the ambient 

lattice by the base extension of the sublattice if `R=\QQ`. 

 

EXAMPLES:: 

 

sage: N = ToricLattice(3) 

sage: Ns = N.submodule([N(2,4,0), N(9,12,0)]) 

sage: Q = N/Ns 

sage: Q.base_extend(ZZ) is Q 

True 

sage: Q.base_extend(QQ) 

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

V: Vector space of dimension 3 over Rational Field 

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

Basis matrix: 

[1 0 0] 

[0 1 0] 

""" 

if R is ZZ: 

return self 

if R is QQ: 

return self.V().base_extend(R) / self.W().base_extend(R) 

raise NotImplementedError("quotients of toric lattices can only be " 

"extended to ZZ or QQ, not %s!" % R) 

 

def is_torsion_free(self): 

r""" 

Check if ``self`` is torsion-free. 

 

OUTPUT: 

 

- ``True`` is ``self`` has no torsion and ``False`` otherwise. 

 

EXAMPLES:: 

 

sage: N = ToricLattice(3) 

sage: Ns = N.submodule([N(2,4,0), N(9,12,0)]) 

sage: Q = N/Ns 

sage: Q.is_torsion_free() 

False 

sage: Ns = N.submodule([N(1,4,0)]) 

sage: Q = N/Ns 

sage: Q.is_torsion_free() 

True 

""" 

return sum(self.invariants()) == 0 

 

def dual(self): 

r""" 

Return the lattice dual to ``self``. 

 

OUTPUT: 

 

- a :class:`toric lattice quotient <ToricLattice_quotient>`. 

 

EXAMPLES:: 

 

sage: N = ToricLattice(3) 

sage: Ns = N.submodule([(1, -1, -1)]) 

sage: Q = N / Ns 

sage: Q.dual() 

Sublattice <M(1, 0, 1), M(0, 1, -1)> 

""" 

if "_dual" not in self.__dict__: 

self._dual = self.V().dual().submodule( 

self.W().basis_matrix().transpose().integer_kernel().gens()) 

self._dual._dual = self 

return self._dual 

 

def rank(self): 

r""" 

Return the rank of ``self``. 

 

OUTPUT: 

 

Integer. The dimension of the free part of the quotient. 

 

EXAMPLES:: 

 

sage: N = ToricLattice(3) 

sage: Ns = N.submodule([N(2,4,0), N(9,12,0)]) 

sage: Q = N/Ns 

sage: Q.ngens() 

2 

sage: Q.rank() 

1 

sage: Ns = N.submodule([N(1,4,0)]) 

sage: Q = N/Ns 

sage: Q.ngens() 

2 

sage: Q.rank() 

2 

""" 

return self.V().rank() - self.W().rank() 

 

dimension = rank 

 

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

""" 

Return coordinates of x with respect to the optimized 

representation of self. 

 

INPUT: 

 

- ``x`` -- element of ``self`` or convertable to ``self``. 

 

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

modulo invariants. 

 

OUTPUT: 

 

The coordinates as a vector. 

 

EXAMPLES:: 

 

sage: N = ToricLattice(3) 

sage: Q = N.quotient(N.span([N(1,2,3), N(0,2,1)]), positive_point=N(0,-1,0)) 

sage: q = Q.gen(0); q 

N[0, -1, 0] 

sage: q.vector() # indirect test 

(1) 

sage: Q.coordinate_vector(q) 

(1) 

""" 

coordinates = super(ToricLattice_quotient, self).coordinate_vector(x,reduce) 

if self._flip_sign_of_generator: 

assert len(coordinates)==1, "Sign flipped for a multi-dimensional quotient!" 

return -coordinates 

else: 

return coordinates