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

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

1952

1953

1954

1955

1956

1957

1958

1959

1960

1961

1962

1963

1964

1965

1966

1967

1968

1969

1970

1971

1972

1973

1974

1975

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

1986

r""" 

Base class for maps 

  

AUTHORS: 

  

- Robert Bradshaw: initial implementation 

  

- Sebastien Besnier (2014-05-5): :class:`FormalCompositeMap` contains 

a list of Map instead of only two Map. See :trac:`16291`. 

""" 

  

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

# Copyright (C) 2008 Robert Bradshaw <robertwb@math.washington.edu> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

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

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

  

from __future__ import absolute_import, print_function 

  

from . import homset 

import weakref 

from sage.ext.stdsage cimport HAS_DICTIONARY 

from sage.arith.power cimport generic_power 

from sage.structure.parent cimport Set_PythonType 

from sage.misc.constant_function import ConstantFunction 

from sage.misc.superseded import deprecated_function_alias 

from sage.structure.element cimport parent 

from cpython.object cimport PyObject_RichCompare 

  

  

def unpickle_map(_class, parent, _dict, _slots): 

""" 

Auxiliary function for unpickling a map. 

  

TESTS:: 

  

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

sage: f = R.hom([x+y, x-y], R) 

sage: f == loads(dumps(f)) # indirect doctest 

True 

""" 

# should we use slots? 

# from element.pyx 

cdef Map mor = _class.__new__(_class) 

mor._set_parent(parent) 

mor._update_slots(_slots) 

if HAS_DICTIONARY(mor): 

mor.__dict__ = _dict 

return mor 

  

def is_Map(x): 

""" 

Auxiliary function: Is the argument a map? 

  

EXAMPLES:: 

  

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

sage: f = R.hom([x+y, x-y], R) 

sage: from sage.categories.map import is_Map 

sage: is_Map(f) 

True 

""" 

return isinstance(x, Map) 

  

cdef class Map(Element): 

""" 

Basic class for all maps. 

  

.. NOTE:: 

  

The call method is of course not implemented in this base class. This must 

be done in the sub classes, by overloading ``_call_`` and possibly also 

``_call_with_args``. 

  

EXAMPLES: 

  

Usually, instances of this class will not be constructed directly, but 

for example like this:: 

  

sage: from sage.categories.morphism import SetMorphism 

sage: X.<x> = ZZ[] 

sage: Y = ZZ 

sage: phi = SetMorphism(Hom(X, Y, Rings()), lambda p: p[0]) 

sage: phi(x^2+2*x-1) 

-1 

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

sage: f = R.hom([x+y, x-y], R) 

sage: f(x^2+2*x-1) 

x^2 + 2*x*y + y^2 + 2*x + 2*y - 1 

""" 

  

def __init__(self, parent, codomain=None): 

""" 

INPUT: 

  

There can be one or two arguments of this init method. If it is one argument, 

it must be a hom space. If it is two arguments, it must be two parent structures 

that will be domain and codomain of the map-to-be-created. 

  

TESTS:: 

  

sage: from sage.categories.map import Map 

  

Using a hom space:: 

  

sage: Map(Hom(QQ, ZZ, Rings())) 

Generic map: 

From: Rational Field 

To: Integer Ring 

  

Using domain and codomain:: 

  

sage: Map(QQ['x'], SymmetricGroup(6)) 

Generic map: 

From: Univariate Polynomial Ring in x over Rational Field 

To: Symmetric group of order 6! as a permutation group 

""" 

if codomain is not None: 

if isinstance(parent, type): 

parent = Set_PythonType(parent) 

parent = homset.Hom(parent, codomain) 

elif not isinstance(parent, homset.Homset): 

raise TypeError("parent (=%s) must be a Homspace" % parent) 

Element.__init__(self, parent) 

D = parent.domain() 

C = parent.codomain() 

self._category_for = parent.homset_category() 

self._codomain = C 

self.domain = ConstantFunction(D) 

self.codomain = ConstantFunction(C) 

self._is_coercion = False 

if D.is_exact() and C.is_exact(): 

self._coerce_cost = 10 # default value. 

else: 

self._coerce_cost = 10000 # inexact morphisms are bad. 

  

def __copy__(self): 

""" 

Return copy, with strong references to domain and codomain. 

  

.. NOTE:: 

  

To implement copying on sub-classes, do not override this method, but 

implement cdef methods ``_extra_slots()`` returning a dictionary and 

``_update_slots()`` using this dictionary to fill the cdef or cpdef 

slots of the subclass. 

  

EXAMPLES:: 

  

sage: phi = QQ['x']._internal_coerce_map_from(ZZ) 

sage: phi.domain 

<weakref at ...; to 'sage.rings.integer_ring.IntegerRing_class' at ...> 

sage: type(phi) 

<type 'sage.categories.map.FormalCompositeMap'> 

sage: psi = copy(phi) # indirect doctest 

sage: psi 

Composite map: 

From: Integer Ring 

To: Univariate Polynomial Ring in x over Rational Field 

Defn: Natural morphism: 

From: Integer Ring 

To: Rational Field 

then 

Polynomial base injection morphism: 

From: Rational Field 

To: Univariate Polynomial Ring in x over Rational Field 

sage: psi.domain 

The constant function (...) -> Integer Ring 

sage: psi(3) 

3 

""" 

cdef Map out = Element.__copy__(self) 

# Element.__copy__ updates the __dict__, but not the slots. 

# Let's do this now, but with strong references. 

out._parent = self.parent() # self._parent might be None 

out._update_slots(self._extra_slots()) 

return out 

  

def parent(self): 

r""" 

Return the homset containing this map. 

  

.. NOTE:: 

  

The method :meth:`_make_weak_references`, that is used for the maps 

found by the coercion system, needs to remove the usual strong 

reference from the coercion map to the homset containing it. As long 

as the user keeps strong references to domain and codomain of the map, 

we will be able to reconstruct the homset. However, a strong reference 

to the coercion map does not prevent the domain from garbage collection! 

  

EXAMPLES:: 

  

sage: Q = QuadraticField(-5) 

sage: phi = CDF._internal_convert_map_from(Q) 

sage: print(phi.parent()) 

Set of field embeddings from Number Field in a with defining polynomial x^2 + 5 to Complex Double Field 

  

We now demonstrate that the reference to the coercion map `\phi` does 

not prevent `Q` from being garbage collected:: 

  

sage: import gc 

sage: del Q 

sage: _ = gc.collect() 

sage: phi.parent() 

Traceback (most recent call last): 

... 

ValueError: This map is in an invalid state, the domain has been garbage collected 

  

You can still obtain copies of the maps used by the coercion system with 

strong references:: 

  

sage: Q = QuadraticField(-5) 

sage: phi = CDF.convert_map_from(Q) 

sage: print(phi.parent()) 

Set of field embeddings from Number Field in a with defining polynomial x^2 + 5 to Complex Double Field 

sage: import gc 

sage: del Q 

sage: _ = gc.collect() 

sage: phi.parent() 

Set of field embeddings from Number Field in a with defining polynomial x^2 + 5 to Complex Double Field 

""" 

if self._parent is None: 

D = self.domain() 

C = self._codomain 

if C is None or D is None: 

raise ValueError("This map is in an invalid state, the domain has been garbage collected") 

return homset.Hom(D, C, self._category_for) 

return self._parent 

  

def _make_weak_references(self): 

""" 

Only store weak references to domain and codomain of this map. 

  

.. NOTE:: 

  

This method is internally used on maps that are used for coercions 

or conversions between parents. Without using this method, some objects 

would stay alive indefinitely as soon as they are involved in a coercion 

or conversion. 

  

.. SEEALSO:: 

  

:meth:`_make_strong_references` 

  

EXAMPLES:: 

  

sage: Q = QuadraticField(-5) 

sage: phi = CDF._internal_convert_map_from(Q) 

  

By :trac:`14711`, maps used in the coercion and conversion system 

use *weak* references to domain and codomain, in contrast to other 

maps:: 

  

sage: phi.domain 

<weakref at ...; to 'NumberField_quadratic_with_category' at ...> 

sage: phi._make_strong_references() 

sage: print(phi.domain) 

The constant function (...) -> Number Field in a with defining polynomial x^2 + 5 

  

Now, as there is a strong reference, `Q` cannot be garbage collected:: 

  

sage: import gc 

sage: _ = gc.collect() 

sage: C = Q.__class__.__base__ 

sage: numberQuadFields = len([x for x in gc.get_objects() if isinstance(x, C)]) 

sage: del Q, x 

sage: _ = gc.collect() 

sage: numberQuadFields == len([x for x in gc.get_objects() if isinstance(x, C)]) 

True 

  

However, if we now make the references weak again, the number field can 

be garbage collected, which of course makes the map and its parents 

invalid. This is why :meth:`_make_weak_references` should only be used 

if one really knows what one is doing:: 

  

sage: phi._make_weak_references() 

sage: del x 

sage: _ = gc.collect() 

sage: numberQuadFields == len([x for x in gc.get_objects() if isinstance(x, C)]) + 1 

True 

sage: phi 

Defunct map 

""" 

if not isinstance(self.domain, ConstantFunction): 

return 

self.domain = weakref.ref(self.domain()) 

# Save the category before clearing the parent. 

self._category_for = self._parent.homset_category() 

self._parent = None 

  

def _make_strong_references(self): 

""" 

Store strong references to domain and codomain of this map. 

  

.. NOTE:: 

  

By default, maps keep strong references to domain and codomain, 

preventing them thus from garbage collection. However, in Sage's 

coercion system, these strong references are replaced by weak 

references, since otherwise some objects would stay alive indefinitely 

as soon as they are involved in a coercion or conversion. 

  

.. SEEALSO:: 

  

:meth:`_make_weak_references` 

  

EXAMPLES:: 

  

sage: Q = QuadraticField(-5) 

sage: phi = CDF._internal_convert_map_from(Q) 

  

By :trac:`14711`, maps used in the coercion and conversion system 

use *weak* references to domain and codomain, in contrast to other 

maps:: 

  

sage: phi.domain 

<weakref at ...; to 'NumberField_quadratic_with_category' at ...> 

sage: phi._make_strong_references() 

sage: print(phi.domain) 

The constant function (...) -> Number Field in a with defining polynomial x^2 + 5 

  

Now, as there is a strong reference, `Q` cannot be garbage collected:: 

  

sage: import gc 

sage: _ = gc.collect() 

sage: C = Q.__class__.__base__ 

sage: numberQuadFields = len([x for x in gc.get_objects() if isinstance(x, C)]) 

sage: del Q, x 

sage: _ = gc.collect() 

sage: numberQuadFields == len([x for x in gc.get_objects() if isinstance(x, C)]) 

True 

  

However, if we now make the references weak again, the number field can 

be garbage collected, which of course makes the map and its parents 

invalid. This is why :meth:`_make_weak_references` should only be used 

if one really knows what one is doing:: 

  

sage: phi._make_weak_references() 

sage: del x 

sage: _ = gc.collect() 

sage: numberQuadFields == len([x for x in gc.get_objects() if isinstance(x, C)]) + 1 

True 

sage: phi 

Defunct map 

sage: phi._make_strong_references() 

Traceback (most recent call last): 

... 

RuntimeError: The domain of this map became garbage collected 

sage: phi.parent() 

Traceback (most recent call last): 

... 

ValueError: This map is in an invalid state, the domain has been garbage collected 

""" 

if isinstance(self.domain, ConstantFunction): 

return 

D = self.domain() 

C = self._codomain 

if D is None or C is None: 

raise RuntimeError("The domain of this map became garbage collected") 

self.domain = ConstantFunction(D) 

self._parent = homset.Hom(D, C, self._category_for) 

  

cdef _update_slots(self, dict slots): 

""" 

Set various attributes of this map to implement unpickling. 

  

INPUT: 

  

- ``slots`` -- A dictionary of slots to be updated. 

The dictionary must have the keys ``'_domain'`` and 

``'_codomain'``, and may have the keys ``'_repr_type_str'`` 

and ``'_is_coercion'``. 

  

TESTS: 

  

Since it is a ``cdef``d method, it is tested using a dummy python method. 

:: 

  

sage: from sage.categories.map import Map 

sage: f = Map(Hom(QQ, ZZ, Rings())) 

sage: f._update_slots_test({"_domain": RR, "_codomain": QQ}) # indirect doctest 

sage: f.domain() 

Real Field with 53 bits of precision 

sage: f.codomain() 

Rational Field 

sage: f._repr_type_str 

sage: f._update_slots_test({"_repr_type_str": "bla", "_domain": RR, "_codomain": QQ}) 

sage: f._repr_type_str 

'bla' 

""" 

# todo: the following can break during unpickling of complex 

# objects with circular references! In that case, _slots might 

# contain incomplete objects. 

self.domain = ConstantFunction(slots['_domain']) 

self._codomain = slots['_codomain'] 

self.codomain = ConstantFunction(self._codomain) 

  

# Several pickles exist without the following, so these are 

# optional 

self._repr_type_str = slots.get('_repr_type_str') 

self._is_coercion = slots.get('_is_coercion') 

  

def _update_slots_test(self, _slots): 

""" 

A Python method to test the cdef _update_slots method. 

  

TESTS:: 

  

sage: from sage.categories.map import Map 

sage: f = Map(Hom(QQ, ZZ, Rings())) 

sage: f._update_slots_test({"_domain": RR, "_codomain": QQ}) 

sage: f.domain() 

Real Field with 53 bits of precision 

sage: f.codomain() 

Rational Field 

sage: f._repr_type_str 

sage: f._update_slots_test({"_repr_type_str": "bla", "_domain": RR, "_codomain": QQ}) 

sage: f._repr_type_str 

'bla' 

""" 

self._update_slots(_slots) 

  

cdef dict _extra_slots(self): 

""" 

Return a dict with attributes to pickle and copy this map. 

""" 

return dict( 

_domain=self.domain(), 

_codomain=self._codomain, 

_is_coercion=self._is_coercion, 

_repr_type_str=self._repr_type_str) 

  

def _extra_slots_test(self): 

""" 

A Python method to test the cdef _extra_slots method. 

  

TESTS:: 

  

sage: from sage.categories.map import Map 

sage: f = Map(Hom(QQ, ZZ, Rings())) 

sage: f._extra_slots_test() 

{'_codomain': Integer Ring, 

'_domain': Rational Field, 

'_is_coercion': False, 

'_repr_type_str': None} 

""" 

return self._extra_slots() 

  

def __reduce__(self): 

""" 

TESTS:: 

  

sage: from sage.categories.map import Map 

sage: f = Map(Hom(QQ, ZZ, Rings())); f 

Generic map: 

From: Rational Field 

To: Integer Ring 

sage: loads(dumps(f)) # indirect doctest 

Generic map: 

From: Rational Field 

To: Integer Ring 

""" 

if HAS_DICTIONARY(self): 

_dict = self.__dict__ 

else: 

_dict = {} 

return unpickle_map, (type(self), self.parent(), _dict, self._extra_slots()) 

  

def _repr_type(self): 

""" 

Return a string describing the specific type of this map, to be used when printing ``self``. 

  

.. NOTE:: 

  

By default, the string ``"Generic"`` is returned. Subclasses may overload this method. 

  

EXAMPLES:: 

  

sage: from sage.categories.map import Map 

sage: f = Map(Hom(QQ, ZZ, Rings())) 

sage: print(f._repr_type()) 

Generic 

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

sage: phi = R.hom([x+y, x-y], R) 

sage: print(phi._repr_type()) 

Ring 

""" 

if self._repr_type_str is None: 

return "Generic" 

else: 

return self._repr_type_str 

  

def _repr_defn(self): 

""" 

Return a string describing the definition of ``self``, to be used when printing ``self``. 

  

.. NOTE:: 

  

By default, the empty string is returned. Subclasses may overload this method. 

  

EXAMPLES:: 

  

sage: from sage.categories.map import Map 

sage: f = Map(Hom(QQ, ZZ, Rings())) 

sage: f._repr_defn() == '' 

True 

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

sage: f = R.hom([x+y, x-y], R) 

sage: print(f._repr_defn()) 

x |--> x + y 

y |--> x - y 

""" 

return "" 

  

def _repr_(self): 

""" 

.. NOTE:: 

  

The string representation is based on the strings returned by 

:meth:`_repr_defn` and :meth:`_repr_type`, as well as the domain 

and the codomain. 

  

A map that has been subject to :meth:`_make_weak_references` has 

probably been used internally in the coercion system. Hence, it 

may become defunct by garbage collection of the domain. In this 

case, a warning is printed accordingly. 

  

EXAMPLES:: 

  

sage: from sage.categories.map import Map 

sage: Map(Hom(QQ, ZZ, Rings())) # indirect doctest 

Generic map: 

From: Rational Field 

To: Integer Ring 

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

sage: R.hom([x+y, x-y], R) 

Ring endomorphism of Multivariate Polynomial Ring in x, y over Rational Field 

Defn: x |--> x + y 

y |--> x - y 

  

TESTS:: 

  

sage: Q = QuadraticField(-5) 

sage: phi = CDF._internal_coerce_map_from(Q); phi 

(map internal to coercion system -- copy before use) 

Composite map: 

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

To: Complex Double Field 

sage: del Q 

sage: import gc 

sage: _ = gc.collect() 

sage: phi 

Defunct map 

""" 

D = self.domain() 

if D is None: 

return "Defunct map" 

s = "%s map:"%self._repr_type() 

s += "\n From: %s"%D 

s += "\n To: %s"%self._codomain 

if isinstance(self.domain, ConstantFunction): 

d = self._repr_defn() 

if d != '': 

s += "\n Defn: %s"%('\n '.join(d.split('\n'))) 

else: 

d = "(map internal to coercion system -- copy before use)" 

s = d + "\n" + s 

return s 

  

def _default_repr_(self): 

D = self.domain() 

if D is None: 

return "Defunct map" 

s = "%s map:"%self._repr_type() 

s += "\n From: %s"%D 

s += "\n To: %s"%self._codomain 

d = self._repr_defn() 

if d != '': 

s += "\n Defn: %s"%('\n '.join(d.split('\n'))) 

return s 

  

def domains(self): 

""" 

Iterate over the domains of the factors of a (composite) map. 

  

This default implementation simply yields the domain of this map. 

  

.. SEEALSO:: :meth:`FormalCompositeMap.domains` 

  

EXAMPLES:: 

  

sage: list(QQ.coerce_map_from(ZZ).domains()) 

[Integer Ring] 

""" 

yield self.domain() 

  

def category_for(self): 

""" 

Returns the category self is a morphism for. 

  

.. NOTE:: 

  

This is different from the category of maps to which this 

map belongs *as an object*. 

  

EXAMPLES:: 

  

sage: from sage.categories.morphism import SetMorphism 

sage: X.<x> = ZZ[] 

sage: Y = ZZ 

sage: phi = SetMorphism(Hom(X, Y, Rings()), lambda p: p[0]) 

sage: phi.category_for() 

Category of rings 

sage: phi.category() 

Category of homsets of unital magmas and additive unital additive magmas 

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

sage: f = R.hom([x+y, x-y], R) 

sage: f.category_for() 

Join of Category of unique factorization domains 

and Category of commutative algebras 

over (number fields and quotient fields and metric spaces) 

and Category of infinite sets 

sage: f.category() 

Category of endsets of unital magmas 

and right modules over (number fields and quotient fields and metric spaces) 

and left modules over (number fields and quotient fields and metric spaces) 

  

  

FIXME: find a better name for this method 

""" 

if self._category_for is None: 

# This can happen if the map is the result of unpickling. 

# We have initialised self._parent, but could not set 

# self._category_for at that moment, because it could 

# happen that the parent was not fully constructed and 

# did not know its category yet. 

self._category_for = self._parent.homset_category() 

return self._category_for 

  

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

""" 

Apply this map to ``x``. 

  

IMPLEMENTATION: 

  

- To implement the call method in a subclass of Map, implement 

:meth:`_call_` and possibly also :meth:`_call_with_args` and 

:meth:`pushforward`. 

- If the parent of ``x`` cannot be coerced into the domain of 

``self``, then the method ``pushforward`` is called with ``x`` 

and the other given arguments, provided it is implemented. 

In that way, ``self`` could be applied to objects like ideals 

or sub-modules. 

- If there is no coercion and if ``pushforward`` is not implemented 

or fails, ``_call_`` is called after conversion into the domain 

(which may be possible even when there is no coercion); if there 

are additional arguments (or keyword arguments), 

:meth:`_call_with_args` is called instead. Note that the 

positional arguments after ``x`` are passed as a tuple to 

:meth:`_call_with_args` and the named arguments are passed 

as a dictionary. 

  

INPUT: 

  

- ``x`` -- an element coercible to the domain of ``self``; also objects 

like ideals are supported in some cases 

  

OUTPUT: 

  

an element (or ideal, etc.) 

  

EXAMPLES:: 

  

sage: R.<x,y> = QQ[]; phi = R.hom([y, x]) 

sage: phi(y) # indirect doctest 

x 

  

We take the image of an ideal:: 

  

sage: I = ideal(x, y); I 

Ideal (x, y) of Multivariate Polynomial Ring in x, y over Rational Field 

sage: phi(I) 

Ideal (y, x) of Multivariate Polynomial Ring in x, y over Rational Field 

  

TESTS: 

  

We test that the map can be applied to something that converts 

(but not coerces) into the domain and can *not* be dealt with 

by :meth:`pushforward` (see :trac:`10496`):: 

  

sage: D = {(0, 2): -1, (0, 0): -1, (1, 1): 7, (2, 0): 1/3} 

sage: phi(D) 

-x^2 + 7*x*y + 1/3*y^2 - 1 

  

We test what happens if the argument can't be converted into 

the domain:: 

  

sage: from sage.categories.map import Map 

sage: f = Map(Hom(ZZ, QQ, Rings())) 

sage: f(1/2) 

Traceback (most recent call last): 

... 

TypeError: 1/2 fails to convert into the map's domain Integer Ring, but a `pushforward` method is not properly implemented 

  

We test that the default call method really works as described 

above (that was fixed in :trac:`10496`):: 

  

sage: class FOO(Map): 

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

....: print("_call_ {}".format(parent(x))) 

....: return self.codomain()(x) 

....: def _call_with_args(self, x, args=(), kwds={}): 

....: print("_call_with_args {}".format(parent(x))) 

....: return self.codomain()(x)^kwds.get('exponent', 1) 

....: def pushforward(self, x, exponent=1): 

....: print("pushforward {}".format(parent(x))) 

....: return self.codomain()(1/x)^exponent 

sage: f = FOO(ZZ, QQ) 

sage: f(1/1) #indirect doctest 

pushforward Rational Field 

1 

  

``_call_`` and ``_call_with_args_`` are used *after* coercion:: 

  

sage: f(int(1)) 

_call_ Integer Ring 

1 

sage: f(int(2), exponent=2) 

_call_with_args Integer Ring 

4 

  

``pushforward`` is called without conversion:: 

  

sage: f(1/2) 

pushforward Rational Field 

2 

sage: f(1/2, exponent=2) 

pushforward Rational Field 

4 

  

If the argument does not coerce into the domain, and if 

``pushforward`` fails, ``_call_`` is tried after conversion:: 

  

sage: g = FOO(QQ, ZZ) 

sage: g(SR(3)) 

pushforward Symbolic Ring 

_call_ Rational Field 

3 

sage: g(SR(3), exponent=2) 

pushforward Symbolic Ring 

_call_with_args Rational Field 

9 

  

If conversion fails as well, an error is raised:: 

  

sage: h = FOO(ZZ, ZZ) 

sage: h(2/3) 

Traceback (most recent call last): 

... 

TypeError: 2/3 fails to convert into the map's domain Integer Ring, but a `pushforward` method is not properly implemented 

""" 

P = parent(x) 

cdef Parent D = self.domain() 

if P is D: # we certainly want to call _call_/with_args 

if not args and not kwds: 

return self._call_(x) 

return self._call_with_args(x, args, kwds) 

# Is there coercion? 

converter = D._internal_coerce_map_from(P) 

if converter is None: 

try: 

return self.pushforward(x, *args, **kwds) 

except (AttributeError, TypeError, NotImplementedError): 

pass 

try: 

x = D(x) 

except (TypeError, NotImplementedError): 

raise TypeError("%s fails to convert into the map's domain %s, but a `pushforward` method is not properly implemented" % (x, D)) 

else: 

x = converter(x) 

if not args and not kwds: 

return self._call_(x) 

return self._call_with_args(x, args, kwds) 

  

cpdef Element _call_(self, x): 

""" 

Call method with a single argument, not implemented in the base class. 

  

TESTS:: 

  

sage: from sage.categories.map import Map 

sage: f = Map(Hom(QQ, ZZ, Rings())) 

sage: f(1/2) # indirect doctest 

Traceback (most recent call last): 

... 

NotImplementedError: <type 'sage.categories.map.Map'> 

""" 

raise NotImplementedError(type(self)) 

  

cpdef Element _call_with_args(self, x, args=(), kwds={}): 

""" 

Call method with multiple arguments, not implemented in the base class. 

  

TESTS:: 

  

sage: from sage.categories.map import Map 

sage: f = Map(Hom(QQ, ZZ, Rings())) 

sage: f(1/2, 2, foo='bar') # indirect doctest 

Traceback (most recent call last): 

... 

NotImplementedError: _call_with_args not overridden to accept arguments for <type 'sage.categories.map.Map'> 

""" 

if len(args) == 0 and len(kwds) == 0: 

return self(x) 

else: 

raise NotImplementedError("_call_with_args not overridden to accept arguments for %s" % type(self)) 

  

def __mul__(self, right): 

r""" 

The multiplication * operator is operator composition 

  

IMPLEMENTATION: 

  

If you want to change the behaviour of composition for 

derived classes, please overload :meth:`_composition_` 

(but not :meth:`_composition`!) of the left factor. 

  

INPUT: 

  

- ``self`` -- Map 

- ``right`` -- Map 

  

OUTPUT: 

  

The map `x \mapsto self(right(x))`. 

  

EXAMPLES:: 

  

sage: from sage.categories.morphism import SetMorphism 

sage: X.<x> = ZZ[] 

sage: Y = ZZ 

sage: Z = QQ 

sage: phi_xy = SetMorphism(Hom(X, Y, Rings()), lambda p: p[0]) 

sage: phi_yz = SetMorphism(Hom(Y, Z, CommutativeAdditiveMonoids()), lambda y: QQ(y)/2) 

sage: phi_yz * phi_xy 

Composite map: 

From: Univariate Polynomial Ring in x over Integer Ring 

To: Rational Field 

Defn: Generic morphism: 

From: Univariate Polynomial Ring in x over Integer Ring 

To: Integer Ring 

then 

Generic morphism: 

From: Integer Ring 

To: Rational Field 

  

If ``right`` is a ring homomorphism given by the images of 

generators, then it is attempted to form the composition 

accordingly. Only if this fails, or if the result does not 

belong to the given homset, a formal composite map is 

returned (as above). 

:: 

  

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

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

sage: f = R.hom([x+y, x-y], R) 

sage: f = R.hom([a+b, a-b]) 

sage: g = S.hom([x+y, x-y]) 

sage: f*g 

Ring endomorphism of Multivariate Polynomial Ring in a, b over Rational Field 

Defn: a |--> 2*a 

b |--> 2*b 

sage: h = SetMorphism(Hom(S, QQ, Rings()), lambda p: p.lc()) 

sage: h*f 

Composite map: 

From: Multivariate Polynomial Ring in x, y over Rational Field 

To: Rational Field 

Defn: Ring morphism: 

From: Multivariate Polynomial Ring in x, y over Rational Field 

To: Multivariate Polynomial Ring in a, b over Rational Field 

Defn: x |--> a + b 

y |--> a - b 

then 

Generic morphism: 

From: Multivariate Polynomial Ring in a, b over Rational Field 

To: Rational Field 

""" 

if not isinstance(right, Map): 

raise TypeError("right (=%s) must be a map to multiply it by %s" % (right, self)) 

if right.codomain() != self.domain(): 

raise TypeError("self (=%s) domain must equal right (=%s) codomain" % (self, right)) 

return self._composition(right) 

  

def _composition(self, right): 

""" 

Composition of maps, which generically returns a :class:`CompositeMap`. 

  

INPUT: 

  

- ``self`` -- a Map in some ``Hom(Y, Z, category_left)`` 

- ``right`` -- a Map in some ``Hom(X, Y, category_right)`` 

  

OUTPUT: 

  

Returns the composition of ``self`` and ``right`` as a 

morphism in ``Hom(X, Z, category)`` where ``category`` is the 

meet of ``category_left`` and ``category_right``. 

  

EXAMPLES:: 

  

sage: from sage.categories.morphism import SetMorphism 

sage: X.<x> = ZZ[] 

sage: Y = ZZ 

sage: Z = QQ 

sage: phi_xy = SetMorphism(Hom(X, Y, Rings()), lambda p: p[0]) 

sage: phi_yz = SetMorphism(Hom(Y, Z, CommutativeAdditiveMonoids()), lambda y: QQ(y)/2) 

sage: phi_yz._composition(phi_xy) 

Composite map: 

From: Univariate Polynomial Ring in x over Integer Ring 

To: Rational Field 

Defn: Generic morphism: 

From: Univariate Polynomial Ring in x over Integer Ring 

To: Integer Ring 

then 

Generic morphism: 

From: Integer Ring 

To: Rational Field 

sage: phi_yz.category_for() 

Category of commutative additive monoids 

""" 

category = self.category_for()._meet_(right.category_for()) 

H = homset.Hom(right.domain(), self._codomain, category) 

return self._composition_(right, H) 

  

def _composition_(self, right, homset): 

""" 

INPUT: 

  

- ``self``, ``right`` -- maps 

- homset -- a homset 

  

ASSUMPTION: 

  

The codomain of ``right`` is contained in the domain of ``self``. 

This assumption is not verified. 

  

OUTPUT: 

  

Returns a formal composite map, the composition of ``right`` 

followed by ``self``, as a morphism in ``homset``. 

  

Classes deriving from :class:`Map` are encouraged to override 

this whenever meaningful. This is the case, e.g., for ring 

homomorphisms. 

  

EXAMPLES:: 

  

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

sage: Ry.<y> = ZZ['y'] 

sage: Rz.<z> = ZZ['z'] 

sage: phi_xy = Rx.hom([y+1]); phi_xy 

Ring morphism: 

From: Univariate Polynomial Ring in x over Integer Ring 

To: Univariate Polynomial Ring in y over Integer Ring 

Defn: x |--> y + 1 

sage: phi_yz = Ry.hom([z+1]); phi_yz 

Ring morphism: 

From: Univariate Polynomial Ring in y over Integer Ring 

To: Univariate Polynomial Ring in z over Integer Ring 

Defn: y |--> z + 1 

sage: phi_xz = phi_yz._composition_(phi_xy, Hom(Rx, Rz, Monoids())) 

sage: phi_xz 

Composite map: 

From: Univariate Polynomial Ring in x over Integer Ring 

To: Univariate Polynomial Ring in z over Integer Ring 

Defn: Ring morphism: 

From: Univariate Polynomial Ring in x over Integer Ring 

To: Univariate Polynomial Ring in y over Integer Ring 

Defn: x |--> y + 1 

then 

Ring morphism: 

From: Univariate Polynomial Ring in y over Integer Ring 

To: Univariate Polynomial Ring in z over Integer Ring 

Defn: y |--> z + 1 

sage: phi_xz.category_for() 

Category of monoids 

  

TESTS: 

  

This illustrates that it is not tested whether the maps can actually 

be composed, i.e., whether codomain and domain match. 

:: 

  

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

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

sage: f_R = R.hom([x+y, x-y], R) 

sage: f_S = S.hom([a+b, a-b], S) 

sage: foo_bar = f_R._composition_(f_S, Hom(S, R, Monoids())) 

sage: foo_bar(a) 

2*x 

  

However, it is tested when attempting to compose the maps in 

the usual multiplicative notation:: 

  

sage: f_R*f_S 

Traceback (most recent call last): 

... 

TypeError: self (=Ring endomorphism of Multivariate Polynomial Ring in x, y over Rational Field 

Defn: x |--> x + y 

y |--> x - y) domain must equal right (=Ring endomorphism of Multivariate Polynomial Ring in a, b over Rational Field 

Defn: a |--> a + b 

b |--> a - b) codomain 

""" 

return FormalCompositeMap(homset, right, self) 

  

def pre_compose(self, right): 

""" 

INPUT: 

  

- ``self`` -- a Map in some ``Hom(Y, Z, category_left)`` 

- ``left`` -- a Map in some ``Hom(X, Y, category_right)`` 

  

Returns the composition of ``right`` followed by ``self`` as a 

morphism in ``Hom(X, Z, category)`` where ``category`` is the 

meet of ``category_left`` and ``category_right``. 

  

EXAMPLES:: 

  

sage: from sage.categories.morphism import SetMorphism 

sage: X.<x> = ZZ[] 

sage: Y = ZZ 

sage: Z = QQ 

sage: phi_xy = SetMorphism(Hom(X, Y, Rings()), lambda p: p[0]) 

sage: phi_yz = SetMorphism(Hom(Y, Z, Monoids()), lambda y: QQ(y**2)) 

sage: phi_xz = phi_yz.pre_compose(phi_xy); phi_xz 

Composite map: 

From: Univariate Polynomial Ring in x over Integer Ring 

To: Rational Field 

Defn: Generic morphism: 

From: Univariate Polynomial Ring in x over Integer Ring 

To: Integer Ring 

then 

Generic morphism: 

From: Integer Ring 

To: Rational Field 

sage: phi_xz.category_for() 

Category of monoids 

""" 

D = self.domain() 

if D is not right.codomain(): 

right = right.extend_codomain(D) 

return self._composition(right) 

  

def post_compose(self, left): 

""" 

INPUT: 

  

- ``self`` -- a Map in some ``Hom(X, Y, category_right)`` 

- ``left`` -- a Map in some ``Hom(Y, Z, category_left)`` 

  

Returns the composition of ``self`` followed by ``right`` as a 

morphism in ``Hom(X, Z, category)`` where ``category`` is the 

meet of ``category_left`` and ``category_right``. 

  

Caveat: see the current restrictions on :meth:`Category.meet` 

  

EXAMPLES:: 

  

sage: from sage.categories.morphism import SetMorphism 

sage: X.<x> = ZZ[] 

sage: Y = ZZ 

sage: Z = QQ 

sage: phi_xy = SetMorphism(Hom(X, Y, Rings()), lambda p: p[0]) 

sage: phi_yz = SetMorphism(Hom(Y, Z, Monoids()), lambda y: QQ(y**2)) 

sage: phi_xz = phi_xy.post_compose(phi_yz); phi_xz 

Composite map: 

From: Univariate Polynomial Ring in x over Integer Ring 

To: Rational Field 

Defn: Generic morphism: 

From: Univariate Polynomial Ring in x over Integer Ring 

To: Integer Ring 

then 

Generic morphism: 

From: Integer Ring 

To: Rational Field 

sage: phi_xz.category_for() 

Category of monoids 

""" 

return left._composition(self) 

  

def extend_domain(self, new_domain): 

r""" 

INPUT: 

  

- ``self`` -- a member of Hom(Y, Z) 

- ``new_codomain`` -- an object X such that there is a canonical coercion 

`\phi` in Hom(X, Y) 

  

OUTPUT: 

  

An element of Hom(X, Z) obtained by composing self with `\phi`. If 

no canonical `\phi` exists, a TypeError is raised. 

  

EXAMPLES:: 

  

sage: mor = CDF.coerce_map_from(RDF) 

sage: mor.extend_domain(QQ) 

Composite map: 

From: Rational Field 

To: Complex Double Field 

Defn: Native morphism: 

From: Rational Field 

To: Real Double Field 

then 

Native morphism: 

From: Real Double Field 

To: Complex Double Field 

sage: mor.extend_domain(ZZ['x']) 

Traceback (most recent call last): 

... 

TypeError: No coercion from Univariate Polynomial Ring in x over Integer Ring to Real Double Field 

""" 

D = self.domain() 

if D is None: 

raise ValueError("This map became defunct by garbage collection") 

cdef Map connecting = D._internal_coerce_map_from(new_domain) 

if connecting is None: 

raise TypeError("No coercion from %s to %s" % (new_domain, D)) 

elif connecting.codomain() is not D: 

raise RuntimeError("BUG: coerce_map_from should always return a map to self (%s)" % D) 

else: 

return self.pre_compose(connecting.__copy__()) 

  

def extend_codomain(self, new_codomain): 

r""" 

INPUT: 

  

- ``self`` -- a member of Hom(X, Y) 

- ``new_codomain`` -- an object Z such that there is a canonical coercion 

`\phi` in Hom(Y, Z) 

  

OUTPUT: 

  

An element of Hom(X, Z) obtained by composing self with `\phi`. If 

no canonical `\phi` exists, a TypeError is raised. 

  

EXAMPLES:: 

  

sage: mor = QQ.coerce_map_from(ZZ) 

sage: mor.extend_codomain(RDF) 

Composite map: 

From: Integer Ring 

To: Real Double Field 

Defn: Natural morphism: 

From: Integer Ring 

To: Rational Field 

then 

Native morphism: 

From: Rational Field 

To: Real Double Field 

sage: mor.extend_codomain(GF(7)) 

Traceback (most recent call last): 

... 

TypeError: No coercion from Rational Field to Finite Field of size 7 

""" 

cdef Map connecting = new_codomain._internal_coerce_map_from(self._codomain) 

if connecting is None: 

raise TypeError("No coercion from %s to %s" % (self._codomain, new_codomain)) 

elif connecting.domain() is not self._codomain: 

raise RuntimeError("BUG: coerce_map_from should always return a map from its input (%s)" % new_codomain) 

else: 

return self.post_compose(connecting.__copy__()) 

  

def is_surjective(self): 

""" 

Tells whether the map is surjective (not implemented in the base class). 

  

TESTS:: 

  

sage: from sage.categories.map import Map 

sage: f = Map(Hom(QQ, ZZ, Rings())) 

sage: f.is_surjective() 

Traceback (most recent call last): 

... 

NotImplementedError: <type 'sage.categories.map.Map'> 

""" 

raise NotImplementedError(type(self)) 

  

cpdef _pow_int(self, n): 

""" 

TESTS:: 

  

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

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

Ring endomorphism of Univariate Polynomial Ring in x over Integer Ring 

Defn: x |--> x + 1 

  

sage: phi^0 

Identity endomorphism of Univariate Polynomial Ring in x over Integer Ring 

  

sage: phi^2 == phi*phi 

True 

  

sage: S.<y> = QQ[] 

sage: psi = R.hom([y^2]) 

sage: psi^1 

Ring morphism: 

From: Univariate Polynomial Ring in x over Integer Ring 

To: Univariate Polynomial Ring in y over Rational Field 

Defn: x |--> y^2 

sage: psi^2 

Traceback (most recent call last): 

... 

TypeError: self must be an endomorphism. 

  

sage: K.<a> = NumberField(x^4 - 5*x + 5) 

sage: C5.<z> = CyclotomicField(5) 

sage: tau = K.hom([z - z^2]); tau 

Ring morphism: 

From: Number Field in a with defining polynomial x^4 - 5*x + 5 

To: Cyclotomic Field of order 5 and degree 4 

Defn: a |--> -z^2 + z 

sage: tau^-1 

Ring morphism: 

From: Cyclotomic Field of order 5 and degree 4 

To: Number Field in a with defining polynomial x^4 - 5*x + 5 

Defn: z |--> 3/11*a^3 + 4/11*a^2 + 9/11*a - 14/11 

""" 

if self.domain() is not self._codomain and n != 1 and n != -1: 

raise TypeError("self must be an endomorphism.") 

if n == 0: 

from sage.categories.morphism import IdentityMorphism 

return IdentityMorphism(self._parent) 

return generic_power(self, n) 

  

def section(self): 

""" 

Return a section of self. 

  

NOTE: 

  

By default, it returns ``None``. You may override it in subclasses. 

  

TESTS:: 

  

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

sage: f = R.hom([x+y, x-y], R) 

sage: print(f.section()) 

None 

  

sage: f = QQ.coerce_map_from(ZZ); f 

Natural morphism: 

From: Integer Ring 

To: Rational Field 

sage: ff = f.section(); ff 

Generic map: 

From: Rational Field 

To: Integer Ring 

sage: ff(4/2) 

2 

sage: parent(ff(4/2)) is ZZ 

True 

sage: ff(1/2) 

Traceback (most recent call last): 

... 

TypeError: no conversion of this rational to integer 

""" 

return None 

  

def __hash__(self): 

""" 

Return the hash of this map. 

  

TESTS:: 

  

sage: f = sage.rings.morphism.RingMap(ZZ.Hom(ZZ)) 

sage: type(f) 

<type 'sage.rings.morphism.RingMap'> 

sage: hash(f) == hash(f) 

True 

sage: {f: 1}[f] 

1 

""" 

D = self.domain() 

if D is None: 

raise ValueError("This map became defunct by garbage collection") 

return hash((self.domain(), self._codomain)) 

  

cdef class Section(Map): 

""" 

A formal section of a map. 

  

NOTE: 

  

Call methods are not implemented for the base class ``Section``. 

  

EXAMPLES:: 

  

sage: from sage.categories.map import Section 

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

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

sage: f = R.hom([a+b, a-b]) 

sage: sf = Section(f); sf 

Section map: 

From: Multivariate Polynomial Ring in a, b over Rational Field 

To: Multivariate Polynomial Ring in x, y over Integer Ring 

sage: sf(a) 

Traceback (most recent call last): 

... 

NotImplementedError: <type 'sage.categories.map.Section'> 

""" 

  

def __init__(self, map): 

""" 

INPUT: 

  

A map. 

  

TESTS:: 

  

sage: from sage.categories.map import Section 

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

sage: f = R.hom([x+y, x-y], R) 

sage: sf = Section(f); sf 

Section map: 

From: Multivariate Polynomial Ring in x, y over Rational Field 

To: Multivariate Polynomial Ring in x, y over Rational Field 

""" 

from sage.categories.homset import Hom 

from sage.categories.all import SetsWithPartialMaps 

Map.__init__(self, Hom(map.codomain(), map.domain(), SetsWithPartialMaps())) 

self._inverse = map # TODO: Use this attribute somewhere! 

  

cdef dict _extra_slots(self): 

""" 

Helper for pickling and copying. 

  

TESTS:: 

  

sage: from sage.categories.map import Section 

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

sage: f = R.hom([x+y, x-y], R) 

sage: sf = Section(f) 

sage: copy(sf) # indirect doctest 

Section map: 

From: Multivariate Polynomial Ring in x, y over Rational Field 

To: Multivariate Polynomial Ring in x, y over Rational Field 

""" 

slots = Map._extra_slots(self) 

slots['_inverse'] = self._inverse 

return slots 

  

cdef _update_slots(self, dict _slots): 

""" 

Helper for pickling and copying. 

  

TESTS:: 

  

sage: from sage.categories.map import Section 

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

sage: f = R.hom([x+y, x-y], R) 

sage: sf = Section(f) 

sage: copy(sf) # indirect doctest 

Section map: 

From: Multivariate Polynomial Ring in x, y over Rational Field 

To: Multivariate Polynomial Ring in x, y over Rational Field 

""" 

Map._update_slots(self, _slots) 

self._inverse = _slots['_inverse'] 

  

def _repr_type(self): 

""" 

Return a string describing the type of this map (which is "Section"). 

  

TESTS:: 

  

sage: from sage.categories.map import Section 

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

sage: f = R.hom([x+y, x-y], R) 

sage: sf = Section(f) 

sage: sf # indirect doctest 

Section map: 

From: Multivariate Polynomial Ring in x, y over Rational Field 

To: Multivariate Polynomial Ring in x, y over Rational Field 

""" 

return "Section" 

  

def inverse(self): 

""" 

Return inverse of ``self``. 

  

TESTS:: 

  

sage: from sage.categories.map import Section 

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

sage: f = R.hom([x+y, x-y], R) 

sage: sf = Section(f) 

sage: sf.inverse() 

Ring endomorphism of Multivariate Polynomial Ring in x, y over Rational Field 

Defn: x |--> x + y 

y |--> x - y 

""" 

return self._inverse 

  

cdef class FormalCompositeMap(Map): 

""" 

Formal composite maps. 

  

A formal composite map is formed by two maps, so that the codomain of the 

first map is contained in the domain of the second map. 

  

.. NOTE:: 

  

When calling a composite with additional arguments, these arguments are 

*only* passed to the second underlying map. 

  

EXAMPLES:: 

  

sage: R.<x> = QQ[] 

sage: S.<a> = QQ[] 

sage: from sage.categories.morphism import SetMorphism 

sage: f = SetMorphism(Hom(R, S, Rings()), lambda p: p[0]*a^p.degree()) 

sage: g = S.hom([2*x]) 

sage: f*g 

Composite map: 

From: Univariate Polynomial Ring in a over Rational Field 

To: Univariate Polynomial Ring in a over Rational Field 

Defn: Ring morphism: 

From: Univariate Polynomial Ring in a over Rational Field 

To: Univariate Polynomial Ring in x over Rational Field 

Defn: a |--> 2*x 

then 

Generic morphism: 

From: Univariate Polynomial Ring in x over Rational Field 

To: Univariate Polynomial Ring in a over Rational Field 

sage: g*f 

Composite map: 

From: Univariate Polynomial Ring in x over Rational Field 

To: Univariate Polynomial Ring in x over Rational Field 

Defn: Generic morphism: 

From: Univariate Polynomial Ring in x over Rational Field 

To: Univariate Polynomial Ring in a over Rational Field 

then 

Ring morphism: 

From: Univariate Polynomial Ring in a over Rational Field 

To: Univariate Polynomial Ring in x over Rational Field 

Defn: a |--> 2*x 

sage: (f*g)(2*a^2+5) 

5*a^2 

sage: (g*f)(2*x^2+5) 

20*x^2 

""" 

  

def __init__(self, parent, first, second=None): 

""" 

INPUT: 

  

- ``parent``: a homset 

- ``first``: a map or a list of maps 

- ``second``: a map or None 

  

.. NOTE:: 

  

The intended use is of course that the codomain of the 

first map is contained in the domain of the second map, 

so that the two maps can be composed, and that the 

composition belongs to ``parent``. However, none of 

these conditions is verified in the init method. 

  

The user is advised to compose two maps ``f`` and ``g`` 

in multiplicative notation, ``g*f``, since this will in 

some cases return a more efficient map object than a 

formal composite map. 

  

TESTS:: 

  

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

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

sage: f = R.hom([a+b, a-b]) 

sage: g = S.hom([x+y, x-y]) 

sage: H = Hom(R, R, Rings()) 

sage: from sage.categories.map import FormalCompositeMap 

sage: m = FormalCompositeMap(H, f, g); m 

Composite map: 

From: Multivariate Polynomial Ring in x, y over Rational Field 

To: Multivariate Polynomial Ring in x, y over Rational Field 

Defn: Ring morphism: 

From: Multivariate Polynomial Ring in x, y over Rational Field 

To: Multivariate Polynomial Ring in a, b over Rational Field 

Defn: x |--> a + b 

y |--> a - b 

then 

Ring morphism: 

From: Multivariate Polynomial Ring in a, b over Rational Field 

To: Multivariate Polynomial Ring in x, y over Rational Field 

Defn: a |--> x + y 

b |--> x - y 

sage: m(x), m(y) 

(2*x, 2*y) 

""" 

Map.__init__(self, parent) 

  

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

self.__list = first 

self._coerce_cost = sum((<Map>f)._coerce_cost for f in first) 

return 

  

self.__list = [] 

if isinstance(first, FormalCompositeMap): 

self.__list += (<FormalCompositeMap>first).__list 

else: 

self.__list += [first] 

  

if isinstance(second, FormalCompositeMap): 

self.__list += (<FormalCompositeMap>second).__list 

else: 

self.__list += [second] 

self._coerce_cost = (<Map>first)._coerce_cost + (<Map>second)._coerce_cost 

  

def __copy__(self): 

""" 

Since :meth:`_extra_slots` would return the uncopied constituents 

of this composite map, we cannot rely on the default copying method 

of maps. 

  

TESTS:: 

  

sage: copy(QQ['q,t'].coerce_map_from(int)) # indirect doctest 

Composite map: 

From: Set of Python objects of class 'int' 

To: Multivariate Polynomial Ring in q, t over Rational Field 

Defn: Native morphism: 

From: Set of Python objects of class 'int' 

To: Rational Field 

then 

Polynomial base injection morphism: 

From: Rational Field 

To: Multivariate Polynomial Ring in q, t over Rational Field 

""" 

return FormalCompositeMap(self.parent(), [f.__copy__() for f in self.__list]) 

  

cdef _update_slots(self, dict _slots): 

""" 

Used in pickling and copying. 

  

TESTS:: 

  

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

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

sage: f = R.hom([a+b, a-b]) 

sage: g = S.hom([x+y, x-y]) 

sage: from sage.categories.map import FormalCompositeMap 

sage: H = Hom(R, R, Rings()) 

sage: m = FormalCompositeMap(H, f, g) 

sage: m == loads(dumps(m)) # indirect doctest 

True 

""" 

self.__list = _slots['__list'] 

Map._update_slots(self, _slots) 

  

cdef dict _extra_slots(self): 

""" 

Used in pickling and copying. 

  

TESTS:: 

  

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

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

sage: f = R.hom([a+b, a-b]) 

sage: g = S.hom([x+y, x-y]) 

sage: from sage.categories.map import FormalCompositeMap 

sage: H = Hom(R, R, Rings()) 

sage: m = FormalCompositeMap(H, f, g) 

sage: m == loads(dumps(m)) # indirect doctest 

True 

""" 

slots = Map._extra_slots(self) 

slots['__list'] = self.__list 

return slots 

  

def __richcmp__(self, other, int op): 

""" 

TESTS:: 

  

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

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

sage: f = R.hom([a+b, a-b]) 

sage: g = S.hom([x+y, x-y]) 

sage: from sage.categories.map import FormalCompositeMap 

sage: H = Hom(R, R, Rings()) 

sage: m = FormalCompositeMap(H, f, g) 

sage: m == loads(dumps(m)) 

True 

  

sage: m == None 

False 

sage: m == 2 

False 

""" 

if type(self) is not type(other): 

return NotImplemented 

left = (<FormalCompositeMap>self).__list 

right = (<FormalCompositeMap>other).__list 

return PyObject_RichCompare(left, right, op) 

  

def __hash__(self): 

""" 

Return the hash of this map. 

  

TESTS:: 

  

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

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

sage: f = R.hom([a+b, a-b]) 

sage: g = S.hom([x+y, x-y]) 

sage: from sage.categories.map import FormalCompositeMap 

sage: H = Hom(R, R, Rings()) 

sage: m = FormalCompositeMap(H, f, g) 

sage: hash(m) == hash(m) 

True 

sage: {m: 1}[m] 

1 

sage: n = FormalCompositeMap(Hom(S, S, Rings()), g, f) 

sage: hash(m) == hash(n) 

False 

sage: len({m: 1, n: 2}.keys()) 

2 

""" 

return hash(tuple(self.__list)) 

  

def __getitem__(self, i): 

r""" 

Return the `i`-th map of the formal composition. 

  

If ``self`` represents `f_n \circ f_{n-1} \circ \cdots \circ 

f_1 \circ f_0`, then ``self[i]`` gives `f_i`. Support 

negative indices as ``list.__getitem__``. Raise an error if 

the index does not match, in the same way as 

``list.__getitem__``. 

  

EXAMPLES:: 

  

sage: from sage.categories.map import Map 

sage: f = Map(ZZ, QQ) 

sage: g = Map(QQ, ZZ) 

sage: (f*g)[0] 

Generic map: 

From: Rational Field 

To: Integer Ring 

sage: (f*g)[1] 

Generic map: 

From: Integer Ring 

To: Rational Field 

sage: (f*g)[-1] 

Generic map: 

From: Integer Ring 

To: Rational Field 

sage: (f*g)[-2] 

Generic map: 

From: Rational Field 

To: Integer Ring 

sage: (f*g)[-3] 

Traceback (most recent call last): 

... 

IndexError: list index out of range 

sage: (f*g)[2] 

Traceback (most recent call last): 

... 

IndexError: list index out of range 

  

""" 

return self.__list[i] 

  

cpdef Element _call_(self, x): 

""" 

Call with a single argument 

  

TESTS:: 

  

sage: R.<x> = QQ[] 

sage: S.<a> = QQ[] 

sage: from sage.categories.morphism import SetMorphism 

sage: f = SetMorphism(Hom(R, S, Rings()), lambda p: p[0]*a^p.degree()) 

sage: g = S.hom([2*x]) 

sage: (g*f)((x+1)^2), (f*g)((a+1)^2) # indirect doctest 

(4*x^2, a^2) 

""" 

for f in self.__list: 

x = f._call_(x) 

return x 

  

cpdef Element _call_with_args(self, x, args=(), kwds={}): 

""" 

Additional arguments are only passed to the last applied map. 

  

TESTS:: 

  

sage: from sage.categories.morphism import SetMorphism 

sage: R.<x> = QQ[] 

sage: def foo(x, *args, **kwds): 

....: print('foo called with {} {}'.format(args, kwds)) 

....: return x 

sage: def bar(x, *args, **kwds): 

....: print('bar called with {} {}'.format(args, kwds)) 

....: return x 

sage: f = SetMorphism(Hom(R, R, Rings()), foo) 

sage: b = SetMorphism(Hom(R, R, Rings()), bar) 

sage: c = b*f 

sage: c(2, 'hello world', test=1) # indirect doctest 

foo called with () {} 

bar called with ('hello world',) {'test': 1} 

2 

sage: c = f*b 

sage: c(2, 'hello world', test=1) 

bar called with () {} 

foo called with ('hello world',) {'test': 1} 

2 

""" 

for f in self.__list[:-1]: 

x = f._call_(x) 

return self.__list[-1]._call_with_args(x, args, kwds) 

  

def _repr_type(self): 

""" 

Return a string describing the type of ``self``, namely "Composite" 

  

TESTS:: 

  

sage: R.<x> = QQ[] 

sage: S.<a> = QQ[] 

sage: from sage.categories.morphism import SetMorphism 

sage: f = SetMorphism(Hom(R, S, Rings()), lambda p: p[0]*a^p.degree()) 

sage: g = S.hom([2*x]) 

sage: f*g # indirect doctest 

Composite map: 

From: Univariate Polynomial Ring in a over Rational Field 

To: Univariate Polynomial Ring in a over Rational Field 

Defn: Ring morphism: 

From: Univariate Polynomial Ring in a over Rational Field 

To: Univariate Polynomial Ring in x over Rational Field 

Defn: a |--> 2*x 

then 

Generic morphism: 

From: Univariate Polynomial Ring in x over Rational Field 

To: Univariate Polynomial Ring in a over Rational Field 

""" 

return "Composite" 

  

def _repr_defn(self): 

""" 

Return a string describing the definition of ``self`` 

  

The return value is obtained from the string representations 

of the two constituents. 

  

TESTS:: 

  

sage: R.<x> = QQ[] 

sage: S.<a> = QQ[] 

sage: from sage.categories.morphism import SetMorphism 

sage: f = SetMorphism(Hom(R, S, Rings()), lambda p: p[0]*a^p.degree()) 

sage: g = S.hom([2*x]) 

sage: f*g # indirect doctest 

Composite map: 

From: Univariate Polynomial Ring in a over Rational Field 

To: Univariate Polynomial Ring in a over Rational Field 

Defn: Ring morphism: 

From: Univariate Polynomial Ring in a over Rational Field 

To: Univariate Polynomial Ring in x over Rational Field 

Defn: a |--> 2*x 

then 

Generic morphism: 

From: Univariate Polynomial Ring in x over Rational Field 

To: Univariate Polynomial Ring in a over Rational Field 

""" 

s = " %s"%(self.__list[0]) 

for f in self.__list[1:]: 

s += "\nthen\n %s" % f 

return s 

  

def first(self): 

r""" 

Return the first map in the formal composition. 

  

If ``self`` represents `f_n \circ f_{n-1} \circ \cdots \circ 

f_1 \circ f_0`, then ``self.first()`` returns `f_0`. We have 

``self == self.then() * self.first()``. 

  

EXAMPLES:: 

  

sage: R.<x> = QQ[] 

sage: S.<a> = QQ[] 

sage: from sage.categories.morphism import SetMorphism 

sage: f = SetMorphism(Hom(R, S, Rings()), lambda p: p[0]*a^p.degree()) 

sage: g = S.hom([2*x]) 

sage: fg = f * g 

sage: fg.first() == g 

True 

sage: fg == fg.then() * fg.first() 

True 

""" 

return self.__list[0] 

  

def then(self): 

r""" 

Return the tail of the list of maps. 

  

If ``self`` represents `f_n \circ f_{n-1} \circ \cdots \circ 

f_1 \circ f_0`, then ``self.first()`` returns `f_n \circ 

f_{n-1} \circ \cdots \circ f_1`. We have ``self == 

self.then() * self.first()``. 

  

EXAMPLES:: 

  

sage: R.<x> = QQ[] 

sage: S.<a> = QQ[] 

sage: from sage.categories.morphism import SetMorphism 

sage: f = SetMorphism(Hom(R, S, Rings()), lambda p: p[0]*a^p.degree()) 

sage: g = S.hom([2*x]) 

sage: (f*g).then() == f 

True 

""" 

if len(self.__list) == 2: return self.__list[1] 

return FormalCompositeMap(self.__list[1:]) 

  

second = deprecated_function_alias(16291, then) 

  

def is_injective(self): 

""" 

Tell whether ``self`` is injective. 

  

It raises ``NotImplementedError`` if it can't be determined. 

  

EXAMPLES:: 

  

sage: V1 = QQ^2 

sage: V2 = QQ^3 

sage: phi1 = (QQ^1).hom(Matrix([[1, 1]]), V1) 

sage: phi2 = V1.hom(Matrix([[1, 2, 3], [4, 5, 6]]), V2) 

  

If both constituents are injective, the composition is injective:: 

  

sage: from sage.categories.map import FormalCompositeMap 

sage: c1 = FormalCompositeMap(Hom(QQ^1, V2, phi1.category_for()), phi1, phi2) 

sage: c1.is_injective() 

True 

  

If it cannot be determined whether the composition is injective, 

an error is raised:: 

  

sage: psi1 = V2.hom(Matrix([[1, 2], [3, 4], [5, 6]]), V1) 

sage: c2 = FormalCompositeMap(Hom(V1, V1, phi2.category_for()), phi2, psi1) 

sage: c2.is_injective() 

Traceback (most recent call last): 

... 

NotImplementedError: Not enough information to deduce injectivity. 

  

If the first map is surjective and the second map is not injective, 

then the composition is not injective:: 

  

sage: psi2 = V1.hom([[1], [1]], QQ^1) 

sage: c3 = FormalCompositeMap(Hom(V2, QQ^1, phi2.category_for()), psi2, psi1) 

sage: c3.is_injective() 

False 

  

TESTS: 

  

Check that :trac:`23205` has been resolved:: 

  

sage: f = QQ.hom(QQbar)*ZZ.hom(QQ) 

sage: f.is_injective() 

True 

  

""" 

try: 

# we try the category first 

# as of 2017-06, the MRO of this class does not get patched to 

# include the category's MorphismMethods (because it is a Cython 

# class); therefore, we can not simply call "super" but need to 

# invoke the category method explicitly 

return self.getattr_from_category('is_injective')() 

except (AttributeError, NotImplementedError): 

pass 

  

injectives = [] 

for f in self.__list: 

if f.is_injective(): 

injectives.append(f) 

else: 

break 

else: 

return True 

  

if all([f.is_surjective() for f in injectives]): 

return False 

  

raise NotImplementedError("Not enough information to deduce injectivity.") 

  

def is_surjective(self): 

""" 

Tell whether ``self`` is surjective. 

  

It raises ``NotImplementedError`` if it can't be determined. 

  

EXAMPLES:: 

  

sage: from sage.categories.map import FormalCompositeMap 

sage: V3 = QQ^3 

sage: V2 = QQ^2 

sage: V1 = QQ^1 

  

If both maps are surjective, the composition is surjective:: 

  

sage: phi32 = V3.hom(Matrix([[1, 2], [3, 4], [5, 6]]), V2) 

sage: phi21 = V2.hom(Matrix([[1], [1]]), V1) 

sage: c_phi = FormalCompositeMap(Hom(V3, V1, phi32.category_for()), phi32, phi21) 

sage: c_phi.is_surjective() 

True 

  

If the second map is not surjective, the composition is not 

surjective:: 

  

sage: FormalCompositeMap(Hom(V3, V1, phi32.category_for()), phi32, V2.hom(Matrix([[0], [0]]), V1)).is_surjective() 

False 

  

If the second map is an isomorphism and the first map is not 

surjective, then the composition is not surjective:: 

  

sage: FormalCompositeMap(Hom(V2, V1, phi32.category_for()), V2.hom(Matrix([[0], [0]]), V1), V1.hom(Matrix([[1]]), V1)).is_surjective() 

False 

  

Otherwise, surjectivity of the composition cannot be determined:: 

  

sage: FormalCompositeMap(Hom(V2, V1, phi32.category_for()), 

....: V2.hom(Matrix([[1, 1], [1, 1]]), V2), 

....: V2.hom(Matrix([[1], [1]]), V1)).is_surjective() 

Traceback (most recent call last): 

... 

NotImplementedError: Not enough information to deduce surjectivity. 

""" 

try: 

# we try the category first 

# as of 2017-06, the MRO of this class does not get patched to 

# include the category's MorphismMethods (because it is a Cython 

# class); therefore, we can not simply call "super" but need to 

# invoke the category method explicitly 

return self.getattr_from_category('is_surjective')() 

except (AttributeError, NotImplementedError): 

pass 

  

surjectives = [] 

for f in self.__list[::-1]: 

if f.is_surjective(): 

surjectives.append(f) 

else: 

break 

else: 

return True 

  

if all([f.is_injective() for f in surjectives]): 

return False 

  

raise NotImplementedError("Not enough information to deduce surjectivity.") 

  

def domains(self): 

""" 

Iterate over the domains of the factors of this map. 

  

(This is useful in particular to check for loops in coercion maps.) 

  

.. SEEALSO:: :meth:`Map.domains` 

  

EXAMPLES:: 

  

sage: f = QQ.coerce_map_from(ZZ) 

sage: g = MatrixSpace(QQ, 2, 2).coerce_map_from(QQ) 

sage: list((g*f).domains()) 

[Integer Ring, Rational Field] 

""" 

for f in self.__list: 

yield f.domain()