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

r""" 

Morphisms defined by a matrix 

 

A matrix morphism is a morphism that is defined by multiplication 

by a matrix. Elements of domain must either have a method 

``vector()`` that returns a vector that the defining 

matrix can hit from the left, or be coercible into vector space of 

appropriate dimension. 

 

EXAMPLES:: 

 

sage: from sage.modules.matrix_morphism import MatrixMorphism, is_MatrixMorphism 

sage: V = QQ^3 

sage: T = End(V) 

sage: M = MatrixSpace(QQ,3) 

sage: I = M.identity_matrix() 

sage: m = MatrixMorphism(T, I); m 

Morphism defined by the matrix 

[1 0 0] 

[0 1 0] 

[0 0 1] 

sage: is_MatrixMorphism(m) 

True 

sage: m.charpoly('x') 

x^3 - 3*x^2 + 3*x - 1 

sage: m.base_ring() 

Rational Field 

sage: m.det() 

1 

sage: m.fcp('x') 

(x - 1)^3 

sage: m.matrix() 

[1 0 0] 

[0 1 0] 

[0 0 1] 

sage: m.rank() 

3 

sage: m.trace() 

3 

 

AUTHOR: 

 

- William Stein: initial versions 

 

- David Joyner (2005-12-17): added examples 

 

- William Stein (2005-01-07): added __reduce__ 

 

- Craig Citro (2008-03-18): refactored MatrixMorphism class 

 

- Rob Beezer (2011-07-15): additional methods, bug fixes, documentation 

""" 

 

import sage.categories.morphism 

import sage.categories.homset 

import sage.matrix.all as matrix 

from sage.structure.all import Sequence, parent 

from sage.structure.richcmp import richcmp, op_NE, op_EQ 

 

 

def is_MatrixMorphism(x): 

""" 

Return True if x is a Matrix morphism of free modules. 

 

EXAMPLES:: 

 

sage: V = ZZ^2; phi = V.hom([3*V.0, 2*V.1]) 

sage: sage.modules.matrix_morphism.is_MatrixMorphism(phi) 

True 

sage: sage.modules.matrix_morphism.is_MatrixMorphism(3) 

False 

""" 

return isinstance(x, MatrixMorphism_abstract) 

 

class MatrixMorphism_abstract(sage.categories.morphism.Morphism): 

def __init__(self, parent): 

""" 

INPUT: 

 

 

- ``parent`` - a homspace 

 

- ``A`` - matrix 

 

 

EXAMPLES:: 

 

sage: from sage.modules.matrix_morphism import MatrixMorphism 

sage: T = End(ZZ^3) 

sage: M = MatrixSpace(ZZ,3) 

sage: I = M.identity_matrix() 

sage: A = MatrixMorphism(T, I) 

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

True 

""" 

if not sage.categories.homset.is_Homset(parent): 

raise TypeError("parent must be a Hom space") 

sage.categories.morphism.Morphism.__init__(self, parent) 

 

def _richcmp_(self, other, op): 

""" 

Rich comparison of morphisms. 

 

EXAMPLES:: 

 

sage: V = ZZ^2 

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

sage: psi = V.hom([5*V.0, 5*V.1]) 

sage: id = V.hom([V.0, V.1]) 

sage: phi == phi 

True 

sage: phi == psi 

False 

sage: psi == End(V)(5) 

True 

sage: psi == 5 * id 

True 

sage: psi == 5 # no coercion 

False 

sage: id == End(V).identity() 

True 

""" 

if not isinstance(other, MatrixMorphism) or op not in (op_EQ, op_NE): 

# Generic comparison 

return sage.categories.morphism.Morphism._richcmp_(self, other, op) 

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

 

def _call_(self, x): 

""" 

Evaluate this matrix morphism at an element of the domain. 

 

.. NOTE:: 

 

Coercion is done in the generic :meth:`__call__` method, 

which calls this method. 

 

EXAMPLES:: 

 

sage: V = QQ^3; W = QQ^2 

sage: H = Hom(V, W); H 

Set of Morphisms (Linear Transformations) from 

Vector space of dimension 3 over Rational Field to 

Vector space of dimension 2 over Rational Field 

sage: phi = H(matrix(QQ, 3, 2, range(6))); phi 

Vector space morphism represented by the matrix: 

[0 1] 

[2 3] 

[4 5] 

Domain: Vector space of dimension 3 over Rational Field 

Codomain: Vector space of dimension 2 over Rational Field 

sage: phi(V.0) 

(0, 1) 

sage: phi(V([1, 2, 3])) 

(16, 22) 

 

Last, we have a situation where coercion occurs:: 

 

sage: U = V.span([[3,2,1]]) 

sage: U.0 

(1, 2/3, 1/3) 

sage: phi(2*U.0) 

(16/3, 28/3) 

 

TESTS:: 

 

sage: V = QQ^3; W = span([[1,2,3],[-1,2,5/3]], QQ) 

sage: phi = V.hom(matrix(QQ,3,[1..9])) 

 

We compute the image of some elements:: 

 

sage: phi(V.0) #indirect doctest 

(1, 2, 3) 

sage: phi(V.1) 

(4, 5, 6) 

sage: phi(V.0 - 1/4*V.1) 

(0, 3/4, 3/2) 

 

We restrict ``phi`` to ``W`` and compute the image of an element:: 

 

sage: psi = phi.restrict_domain(W) 

sage: psi(W.0) == phi(W.0) 

True 

sage: psi(W.1) == phi(W.1) 

True 

""" 

try: 

if parent(x) is not self.domain(): 

x = self.domain()(x) 

except TypeError: 

raise TypeError("%s must be coercible into %s"%(x,self.domain())) 

if self.domain().is_ambient(): 

x = x.element() 

else: 

x = self.domain().coordinate_vector(x) 

C = self.codomain() 

v = x.change_ring(C.base_ring()) * self.matrix() 

if not C.is_ambient(): 

v = C.linear_combination_of_basis(v) 

# The call method of parents uses (coercion) morphisms. 

# Hence, in order to avoid recursion, we call the element 

# constructor directly; after all, we already know the 

# coordinates. 

return C._element_constructor_(v) 

 

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

""" 

Like :meth:`_call_`, but takes optional and keyword arguments. 

 

EXAMPLES:: 

 

sage: V = RR^2 

sage: f = V.hom(V.gens()) 

sage: f._matrix *= I # f is now invalid 

sage: f((1, 0)) 

Traceback (most recent call last): 

... 

TypeError: Unable to coerce entries (=[1.00000000000000*I, 0.000000000000000]) to coefficients in Real Field with 53 bits of precision 

sage: f((1, 0), coerce=False) 

(1.00000000000000*I, 0.000000000000000) 

 

""" 

if self.domain().is_ambient(): 

x = x.element() 

else: 

x = self.domain().coordinate_vector(x) 

C = self.codomain() 

v = x.change_ring(C.base_ring()) * self.matrix() 

if not C.is_ambient(): 

v = C.linear_combination_of_basis(v) 

# The call method of parents uses (coercion) morphisms. 

# Hence, in order to avoid recursion, we call the element 

# constructor directly; after all, we already know the 

# coordinates. 

return C._element_constructor_(v, *args, **kwds) 

 

def __invert__(self): 

""" 

Invert this matrix morphism. 

 

EXAMPLES:: 

 

sage: V = QQ^2; phi = V.hom([3*V.0, 2*V.1]) 

sage: phi^(-1) 

Vector space morphism represented by the matrix: 

[1/3 0] 

[ 0 1/2] 

Domain: Vector space of dimension 2 over Rational Field 

Codomain: Vector space of dimension 2 over Rational Field 

 

Check that a certain non-invertible morphism isn't invertible:: 

 

sage: V = ZZ^2; phi = V.hom([3*V.0, 2*V.1]) 

sage: phi^(-1) 

Traceback (most recent call last): 

... 

ZeroDivisionError: matrix morphism not invertible 

""" 

try: 

B = ~(self.matrix()) 

except ZeroDivisionError: 

raise ZeroDivisionError("matrix morphism not invertible") 

try: 

return self.parent().reversed()(B) 

except TypeError: 

raise ZeroDivisionError("matrix morphism not invertible") 

 

def inverse(self): 

r""" 

Returns the inverse of this matrix morphism, if the inverse exists. 

 

Raises a ``ZeroDivisionError`` if the inverse does not exist. 

 

EXAMPLES: 

 

An invertible morphism created as a restriction of 

a non-invertible morphism, and which has an unequal 

domain and codomain. :: 

 

sage: V = QQ^4 

sage: W = QQ^3 

sage: m = matrix(QQ, [[2, 0, 3], [-6, 1, 4], [1, 2, -4], [1, 0, 1]]) 

sage: phi = V.hom(m, W) 

sage: rho = phi.restrict_domain(V.span([V.0, V.3])) 

sage: zeta = rho.restrict_codomain(W.span([W.0, W.2])) 

sage: x = vector(QQ, [2, 0, 0, -7]) 

sage: y = zeta(x); y 

(-3, 0, -1) 

sage: inv = zeta.inverse(); inv 

Vector space morphism represented by the matrix: 

[-1 3] 

[ 1 -2] 

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

Basis matrix: 

[1 0 0] 

[0 0 1] 

Codomain: Vector space of degree 4 and dimension 2 over Rational Field 

Basis matrix: 

[1 0 0 0] 

[0 0 0 1] 

sage: inv(y) == x 

True 

 

An example of an invertible morphism between modules, 

(rather than between vector spaces). :: 

 

sage: M = ZZ^4 

sage: p = matrix(ZZ, [[ 0, -1, 1, -2], 

....: [ 1, -3, 2, -3], 

....: [ 0, 4, -3, 4], 

....: [-2, 8, -4, 3]]) 

sage: phi = M.hom(p, M) 

sage: x = vector(ZZ, [1, -3, 5, -2]) 

sage: y = phi(x); y 

(1, 12, -12, 21) 

sage: rho = phi.inverse(); rho 

Free module morphism defined by the matrix 

[ -5 3 -1 1] 

[ -9 4 -3 2] 

[-20 8 -7 4] 

[ -6 2 -2 1] 

Domain: Ambient free module of rank 4 over the principal ideal domain ... 

Codomain: Ambient free module of rank 4 over the principal ideal domain ... 

sage: rho(y) == x 

True 

 

A non-invertible morphism, despite having an appropriate 

domain and codomain. :: 

 

sage: V = QQ^2 

sage: m = matrix(QQ, [[1, 2], [20, 40]]) 

sage: phi = V.hom(m, V) 

sage: phi.is_bijective() 

False 

sage: phi.inverse() 

Traceback (most recent call last): 

... 

ZeroDivisionError: matrix morphism not invertible 

 

The matrix representation of this morphism is invertible 

over the rationals, but not over the integers, thus the 

morphism is not invertible as a map between modules. 

It is easy to notice from the definition that every 

vector of the image will have a second entry that 

is an even integer. :: 

 

sage: V = ZZ^2 

sage: q = matrix(ZZ, [[1, 2], [3, 4]]) 

sage: phi = V.hom(q, V) 

sage: phi.matrix().change_ring(QQ).inverse() 

[ -2 1] 

[ 3/2 -1/2] 

sage: phi.is_bijective() 

False 

sage: phi.image() 

Free module of degree 2 and rank 2 over Integer Ring 

Echelon basis matrix: 

[1 0] 

[0 2] 

sage: phi.lift(vector(ZZ, [1, 1])) 

Traceback (most recent call last): 

... 

ValueError: element is not in the image 

sage: phi.inverse() 

Traceback (most recent call last): 

... 

ZeroDivisionError: matrix morphism not invertible 

 

The unary invert operator (~, tilde, "wiggle") is synonymous 

with the ``inverse()`` method (and a lot easier to type). :: 

 

sage: V = QQ^2 

sage: r = matrix(QQ, [[4, 3], [-2, 5]]) 

sage: phi = V.hom(r, V) 

sage: rho = phi.inverse() 

sage: zeta = ~phi 

sage: rho.is_equal_function(zeta) 

True 

 

TESTS:: 

 

sage: V = QQ^2 

sage: W = QQ^3 

sage: U = W.span([W.0, W.1]) 

sage: m = matrix(QQ, [[2, 1], [3, 4]]) 

sage: phi = V.hom(m, U) 

sage: inv = phi.inverse() 

sage: (inv*phi).is_identity() 

True 

sage: (phi*inv).is_identity() 

True 

""" 

return ~self 

 

def __rmul__(self, left): 

""" 

EXAMPLES:: 

 

sage: V = ZZ^2; phi = V.hom([V.0+V.1, 2*V.1]) 

sage: 2*phi 

Free module morphism defined by the matrix 

[2 2] 

[0 4]... 

sage: phi*2 

Free module morphism defined by the matrix 

[2 2] 

[0 4]... 

""" 

R = self.base_ring() 

return self.parent()(R(left) * self.matrix()) 

 

def __mul__(self, right): 

""" 

Composition of morphisms, denoted by \*. 

 

EXAMPLES:: 

 

sage: V = ZZ^2; phi = V.hom([V.0+V.1, 2*V.1]) 

sage: phi*phi 

Free module morphism defined by the matrix 

[1 3] 

[0 4] 

Domain: Ambient free module of rank 2 over the principal ideal domain ... 

Codomain: Ambient free module of rank 2 over the principal ideal domain ... 

 

sage: V = QQ^3 

sage: E = V.endomorphism_ring() 

sage: phi = E(Matrix(QQ,3,range(9))) ; phi 

Vector space morphism represented by the matrix: 

[0 1 2] 

[3 4 5] 

[6 7 8] 

Domain: Vector space of dimension 3 over Rational Field 

Codomain: Vector space of dimension 3 over Rational Field 

sage: phi*phi 

Vector space morphism represented by the matrix: 

[ 15 18 21] 

[ 42 54 66] 

[ 69 90 111] 

Domain: Vector space of dimension 3 over Rational Field 

Codomain: Vector space of dimension 3 over Rational Field 

sage: phi.matrix()**2 

[ 15 18 21] 

[ 42 54 66] 

[ 69 90 111] 

 

:: 

 

sage: W = QQ**4 

sage: E_VW = V.Hom(W) 

sage: psi = E_VW(Matrix(QQ,3,4,range(12))) ; psi 

Vector space morphism represented by the matrix: 

[ 0 1 2 3] 

[ 4 5 6 7] 

[ 8 9 10 11] 

Domain: Vector space of dimension 3 over Rational Field 

Codomain: Vector space of dimension 4 over Rational Field 

sage: psi*phi 

Vector space morphism represented by the matrix: 

[ 20 23 26 29] 

[ 56 68 80 92] 

[ 92 113 134 155] 

Domain: Vector space of dimension 3 over Rational Field 

Codomain: Vector space of dimension 4 over Rational Field 

sage: phi*psi 

Traceback (most recent call last): 

... 

TypeError: Incompatible composition of morphisms: domain of left morphism must be codomain of right. 

sage: phi.matrix()*psi.matrix() 

[ 20 23 26 29] 

[ 56 68 80 92] 

[ 92 113 134 155] 

 

Composite maps can be formed with matrix morphisms:: 

 

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

sage: V, VtoK, KtoV = K.vector_space() 

sage: f = V.hom([V.0 - V.1, V.0 + V.1])*KtoV; f 

Composite map: 

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

To: Vector space of dimension 2 over Rational Field 

Defn: Isomorphism map: 

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

To: Vector space of dimension 2 over Rational Field 

then 

Vector space morphism represented by the matrix: 

[ 1 -1] 

[ 1 1] 

Domain: Vector space of dimension 2 over Rational Field 

Codomain: Vector space of dimension 2 over Rational Field 

sage: f(a) 

(1, 1) 

""" 

if not isinstance(right, MatrixMorphism): 

if isinstance(right, (sage.categories.morphism.Morphism, sage.categories.map.Map)): 

return sage.categories.map.Map.__mul__(self, right) 

R = self.base_ring() 

return self.parent()(self.matrix() * R(right)) 

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

raise TypeError("Incompatible composition of morphisms: domain of left morphism must be codomain of right.") 

M = right.matrix() * self.matrix() 

return right.domain().Hom(self.codomain())(M) 

 

def __add__(self, right): 

""" 

Sum of morphisms, denoted by +. 

 

EXAMPLES:: 

 

sage: phi = (ZZ**2).endomorphism_ring()(Matrix(ZZ,2,[2..5])) ; phi 

Free module morphism defined by the matrix 

[2 3] 

[4 5] 

Domain: Ambient free module of rank 2 over the principal ideal domain ... 

Codomain: Ambient free module of rank 2 over the principal ideal domain ... 

sage: phi + 3 

Free module morphism defined by the matrix 

[5 3] 

[4 8] 

Domain: Ambient free module of rank 2 over the principal ideal domain ... 

Codomain: Ambient free module of rank 2 over the principal ideal domain ... 

sage: phi + phi 

Free module morphism defined by the matrix 

[ 4 6] 

[ 8 10] 

Domain: Ambient free module of rank 2 over the principal ideal domain ... 

Codomain: Ambient free module of rank 2 over the principal ideal domain ... 

sage: psi = (ZZ**3).endomorphism_ring()(Matrix(ZZ,3,[22..30])) ; psi 

Free module morphism defined by the matrix 

[22 23 24] 

[25 26 27] 

[28 29 30] 

Domain: Ambient free module of rank 3 over the principal ideal domain ... 

Codomain: Ambient free module of rank 3 over the principal ideal domain ... 

sage: phi + psi 

Traceback (most recent call last): 

... 

ValueError: a matrix from 

Full MatrixSpace of 3 by 3 dense matrices over Integer Ring 

cannot be converted to a matrix in 

Full MatrixSpace of 2 by 2 dense matrices over Integer Ring! 

""" 

# TODO: move over to any coercion model! 

if not isinstance(right, MatrixMorphism): 

R = self.base_ring() 

return self.parent()(self.matrix() + R(right)) 

if not right.parent() == self.parent(): 

right = self.parent()(right) 

M = self.matrix() + right.matrix() 

return self.domain().Hom(right.codomain())(M) 

 

def __neg__(self): 

""" 

EXAMPLES:: 

 

sage: V = ZZ^2; phi = V.hom([V.0+V.1, 2*V.1]) 

sage: -phi 

Free module morphism defined by the matrix 

[-1 -1] 

[ 0 -2]... 

""" 

return self.parent()(-self.matrix()) 

 

def __sub__(self, other): 

""" 

EXAMPLES:: 

 

sage: V = ZZ^2; phi = V.hom([V.0+V.1, 2*V.1]) 

sage: phi - phi 

Free module morphism defined by the matrix 

[0 0] 

[0 0]... 

""" 

# TODO: move over to any coercion model! 

if not isinstance(other, MatrixMorphism): 

R = self.base_ring() 

return self.parent()(self.matrix() - R(other)) 

if not other.parent() == self.parent(): 

other = self.parent()(other) 

return self.parent()(self.matrix() - other.matrix()) 

 

def base_ring(self): 

""" 

Return the base ring of self, that is, the ring over which self is 

given by a matrix. 

 

EXAMPLES:: 

 

sage: sage.modules.matrix_morphism.MatrixMorphism((ZZ**2).endomorphism_ring(), Matrix(ZZ,2,[3..6])).base_ring() 

Integer Ring 

""" 

return self.domain().base_ring() 

 

def characteristic_polynomial(self, var='x'): 

r""" 

Return the characteristic polynomial of this endomorphism. 

 

``characteristic_polynomial`` and ``char_poly`` are the same method. 

 

INPUT: 

- var -- variable 

 

EXAMPLES:: 

 

sage: V = ZZ^2; phi = V.hom([V.0+V.1, 2*V.1]) 

sage: phi.characteristic_polynomial() 

x^2 - 3*x + 2 

sage: phi.charpoly() 

x^2 - 3*x + 2 

sage: phi.matrix().charpoly() 

x^2 - 3*x + 2 

sage: phi.charpoly('T') 

T^2 - 3*T + 2 

""" 

if not self.is_endomorphism(): 

raise ArithmeticError("charpoly only defined for endomorphisms " +\ 

"(i.e., domain = range)") 

return self.matrix().charpoly(var) 

 

charpoly = characteristic_polynomial 

 

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

""" 

Return decomposition of this endomorphism, i.e., sequence of 

subspaces obtained by finding invariant subspaces of self. 

 

See the documentation for self.matrix().decomposition for more 

details. All inputs to this function are passed onto the 

matrix one. 

 

EXAMPLES:: 

 

sage: V = ZZ^2; phi = V.hom([V.0+V.1, 2*V.1]) 

sage: phi.decomposition() 

[ 

Free module of degree 2 and rank 1 over Integer Ring 

Echelon basis matrix: 

[0 1], 

Free module of degree 2 and rank 1 over Integer Ring 

Echelon basis matrix: 

[ 1 -1] 

] 

""" 

if not self.is_endomorphism(): 

raise ArithmeticError("Matrix morphism must be an endomorphism.") 

D = self.domain() 

E = self.matrix().decomposition(*args,**kwds) 

if D.is_ambient(): 

return Sequence([D.submodule(V, check=False) for V, _ in E], 

cr=True, check=False) 

else: 

B = D.basis_matrix() 

R = D.base_ring() 

return Sequence([D.submodule((V.basis_matrix() * B).row_module(R), 

check=False) for V, _ in E], 

cr=True, check=False) 

 

def trace(self): 

r""" 

Return the trace of this endomorphism. 

 

EXAMPLES:: 

 

sage: V = ZZ^2; phi = V.hom([V.0+V.1, 2*V.1]) 

sage: phi.trace() 

3 

""" 

return self._matrix.trace() 

 

def det(self): 

""" 

Return the determinant of this endomorphism. 

 

EXAMPLES:: 

 

sage: V = ZZ^2; phi = V.hom([V.0+V.1, 2*V.1]) 

sage: phi.det() 

2 

""" 

if not self.is_endomorphism(): 

raise ArithmeticError("Matrix morphism must be an endomorphism.") 

return self.matrix().determinant() 

 

def fcp(self, var='x'): 

""" 

Return the factorization of the characteristic polynomial. 

 

EXAMPLES:: 

 

sage: V = ZZ^2; phi = V.hom([V.0+V.1, 2*V.1]) 

sage: phi.fcp() 

(x - 2) * (x - 1) 

sage: phi.fcp('T') 

(T - 2) * (T - 1) 

""" 

return self.charpoly(var).factor() 

 

def kernel(self): 

""" 

Compute the kernel of this morphism. 

 

EXAMPLES:: 

 

sage: V = VectorSpace(QQ,3) 

sage: id = V.Hom(V)(identity_matrix(QQ,3)) 

sage: null = V.Hom(V)(0*identity_matrix(QQ,3)) 

sage: id.kernel() 

Vector space of degree 3 and dimension 0 over Rational Field 

Basis matrix: 

[] 

sage: phi = V.Hom(V)(matrix(QQ,3,range(9))) 

sage: phi.kernel() 

Vector space of degree 3 and dimension 1 over Rational Field 

Basis matrix: 

[ 1 -2 1] 

sage: hom(CC^2, CC^2, matrix(CC, [[1,0], [0,1]])).kernel() 

Vector space of degree 2 and dimension 0 over Complex Field with 53 bits of precision 

Basis matrix: 

[] 

""" 

V = self.matrix().kernel() 

D = self.domain() 

if not D.is_ambient(): 

# Transform V to ambient space 

# This is a matrix multiply: we take the linear combinations of the basis for 

# D given by the elements of the basis for V. 

B = V.basis_matrix() * D.basis_matrix() 

V = B.row_module(D.base_ring()) 

return self.domain().submodule(V, check=False) 

 

def image(self): 

""" 

Compute the image of this morphism. 

 

EXAMPLES:: 

 

sage: V = VectorSpace(QQ,3) 

sage: phi = V.Hom(V)(matrix(QQ, 3, range(9))) 

sage: phi.image() 

Vector space of degree 3 and dimension 2 over Rational Field 

Basis matrix: 

[ 1 0 -1] 

[ 0 1 2] 

sage: hom(GF(7)^3, GF(7)^2, zero_matrix(GF(7), 3, 2)).image() 

Vector space of degree 2 and dimension 0 over Finite Field of size 7 

Basis matrix: 

[] 

 

 

Compute the image of the identity map on a ZZ-submodule:: 

 

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

sage: phi = V.Hom(V)(identity_matrix(ZZ,2)) 

sage: phi(V.0) == V.0 

True 

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

True 

sage: phi.image() 

Free module of degree 2 and rank 2 over Integer Ring 

Echelon basis matrix: 

[1 0] 

[0 2] 

sage: phi.image() == V 

True 

""" 

V = self.matrix().image() 

D = self.codomain() 

if not D.is_ambient(): 

# Transform V to ambient space 

# This is a matrix multiply: we take the linear combinations of the basis for 

# D given by the elements of the basis for V. 

B = V.basis_matrix() * D.basis_matrix() 

V = B.row_module(self.domain().base_ring()) 

return self.codomain().submodule(V, check=False) 

 

def matrix(self): 

""" 

EXAMPLES:: 

 

sage: V = ZZ^2; phi = V.hom(V.basis()) 

sage: phi.matrix() 

[1 0] 

[0 1] 

sage: sage.modules.matrix_morphism.MatrixMorphism_abstract.matrix(phi) 

Traceback (most recent call last): 

... 

NotImplementedError: this method must be overridden in the extension class 

""" 

raise NotImplementedError("this method must be overridden in the extension class") 

 

def _matrix_(self): 

""" 

EXAMPLES: 

 

Check that this works with the :func:`matrix` function 

(:trac:`16844`):: 

 

sage: H = Hom(ZZ^2, ZZ^3) 

sage: x = H.an_element() 

sage: matrix(x) 

[0 0 0] 

[0 0 0] 

 

TESTS: 

 

``matrix(x)`` is immutable:: 

 

sage: H = Hom(QQ^3, QQ^2) 

sage: phi = H(matrix(QQ, 3, 2, list(reversed(range(6))))); phi 

Vector space morphism represented by the matrix: 

[5 4] 

[3 2] 

[1 0] 

Domain: Vector space of dimension 3 over Rational Field 

Codomain: Vector space of dimension 2 over Rational Field 

sage: A = phi.matrix() 

sage: A[1, 1] = 19 

Traceback (most recent call last): 

... 

ValueError: matrix is immutable; please change a copy instead (i.e., use copy(M) to change a copy of M). 

""" 

return self.matrix() 

 

def rank(self): 

r""" 

Returns the rank of the matrix representing this morphism. 

 

EXAMPLES:: 

 

sage: V = ZZ^2; phi = V.hom(V.basis()) 

sage: phi.rank() 

2 

sage: V = ZZ^2; phi = V.hom([V.0, V.0]) 

sage: phi.rank() 

1 

""" 

return self.matrix().rank() 

 

def nullity(self): 

r""" 

Returns the nullity of the matrix representing this morphism, which is the 

dimension of its kernel. 

 

EXAMPLES:: 

 

sage: V = ZZ^2; phi = V.hom(V.basis()) 

sage: phi.nullity() 

0 

sage: V = ZZ^2; phi = V.hom([V.0, V.0]) 

sage: phi.nullity() 

1 

""" 

return self._matrix.left_nullity() 

 

def is_bijective(self): 

r""" 

Tell whether ``self`` is bijective. 

 

EXAMPLES: 

 

Two morphisms that are obviously not bijective, simply on 

considerations of the dimensions. However, each fullfills 

half of the requirements to be a bijection. :: 

 

sage: V1 = QQ^2 

sage: V2 = QQ^3 

sage: m = matrix(QQ, [[1, 2, 3], [4, 5, 6]]) 

sage: phi = V1.hom(m, V2) 

sage: phi.is_injective() 

True 

sage: phi.is_bijective() 

False 

sage: rho = V2.hom(m.transpose(), V1) 

sage: rho.is_surjective() 

True 

sage: rho.is_bijective() 

False 

 

We construct a simple bijection between two one-dimensional 

vector spaces. :: 

 

sage: V1 = QQ^3 

sage: V2 = QQ^2 

sage: phi = V1.hom(matrix(QQ, [[1, 2], [3, 4], [5, 6]]), V2) 

sage: x = vector(QQ, [1, -1, 4]) 

sage: y = phi(x); y 

(18, 22) 

sage: rho = phi.restrict_domain(V1.span([x])) 

sage: zeta = rho.restrict_codomain(V2.span([y])) 

sage: zeta.is_bijective() 

True 

 

AUTHOR: 

 

- Rob Beezer (2011-06-28) 

""" 

return self.is_injective() and self.is_surjective() 

 

def is_identity(self): 

r""" 

Determines if this morphism is an identity function or not. 

 

EXAMPLES: 

 

A homomorphism that cannot possibly be the identity 

due to an unequal domain and codomain. :: 

 

sage: V = QQ^3 

sage: W = QQ^2 

sage: m = matrix(QQ, [[1, 2], [3, 4], [5, 6]]) 

sage: phi = V.hom(m, W) 

sage: phi.is_identity() 

False 

 

A bijection, but not the identity. :: 

 

sage: V = QQ^3 

sage: n = matrix(QQ, [[3, 1, -8], [5, -4, 6], [1, 1, -5]]) 

sage: phi = V.hom(n, V) 

sage: phi.is_bijective() 

True 

sage: phi.is_identity() 

False 

 

A restriction that is the identity. :: 

 

sage: V = QQ^3 

sage: p = matrix(QQ, [[1, 0, 0], [5, 8, 3], [0, 0, 1]]) 

sage: phi = V.hom(p, V) 

sage: rho = phi.restrict(V.span([V.0, V.2])) 

sage: rho.is_identity() 

True 

 

An identity linear transformation that is defined with a 

domain and codomain with wildly different bases, so that the 

matrix representation is not simply the identity matrix. :: 

 

sage: A = matrix(QQ, [[1, 1, 0], [2, 3, -4], [2, 4, -7]]) 

sage: B = matrix(QQ, [[2, 7, -2], [-1, -3, 1], [-1, -6, 2]]) 

sage: U = (QQ^3).subspace_with_basis(A.rows()) 

sage: V = (QQ^3).subspace_with_basis(B.rows()) 

sage: H = Hom(U, V) 

sage: id = lambda x: x 

sage: phi = H(id) 

sage: phi([203, -179, 34]) 

(203, -179, 34) 

sage: phi.matrix() 

[ 1 0 1] 

[ -9 -18 -2] 

[-17 -31 -5] 

sage: phi.is_identity() 

True 

 

TESTS:: 

 

sage: V = QQ^10 

sage: H = Hom(V, V) 

sage: id = H.identity() 

sage: id.is_identity() 

True 

 

AUTHOR: 

 

- Rob Beezer (2011-06-28) 

""" 

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

return False 

# testing for the identity matrix will only work for 

# endomorphisms which have the same basis for domain and codomain 

# so we test equality on a basis, which is sufficient 

return all( self(u) == u for u in self.domain().basis() ) 

 

def is_zero(self): 

r""" 

Determines if this morphism is a zero function or not. 

 

EXAMPLES: 

 

A zero morphism created from a function. :: 

 

sage: V = ZZ^5 

sage: W = ZZ^3 

sage: z = lambda x: zero_vector(ZZ, 3) 

sage: phi = V.hom(z, W) 

sage: phi.is_zero() 

True 

 

An image list that just barely makes a non-zero morphism. :: 

 

sage: V = ZZ^4 

sage: W = ZZ^6 

sage: z = zero_vector(ZZ, 6) 

sage: images = [z, z, W.5, z] 

sage: phi = V.hom(images, W) 

sage: phi.is_zero() 

False 

 

TESTS:: 

 

sage: V = QQ^10 

sage: W = QQ^3 

sage: H = Hom(V, W) 

sage: rho = H.zero() 

sage: rho.is_zero() 

True 

 

AUTHOR: 

 

- Rob Beezer (2011-07-15) 

""" 

# any nonzero entry in any matrix representation 

# disqualifies the morphism as having totally zero outputs 

return self._matrix.is_zero() 

 

def is_equal_function(self, other): 

r""" 

Determines if two morphisms are equal functions. 

 

INPUT: 

 

- ``other`` - a morphism to compare with ``self`` 

 

OUTPUT: 

 

Returns ``True`` precisely when the two morphisms have 

equal domains and codomains (as sets) and produce identical 

output when given the same input. Otherwise returns ``False``. 

 

This is useful when ``self`` and ``other`` may have different 

representations. 

 

Sage's default comparison of matrix morphisms requires the 

domains to have the same bases and the codomains to have the 

same bases, and then compares the matrix representations. 

This notion of equality is more permissive (it will 

return ``True`` "more often"), but is more correct 

mathematically. 

 

EXAMPLES: 

 

Three morphisms defined by combinations of different 

bases for the domain and codomain and different functions. 

Two are equal, the third is different from both of the others. :: 

 

sage: B = matrix(QQ, [[-3, 5, -4, 2], 

....: [-1, 2, -1, 4], 

....: [ 4, -6, 5, -1], 

....: [-5, 7, -6, 1]]) 

sage: U = (QQ^4).subspace_with_basis(B.rows()) 

sage: C = matrix(QQ, [[-1, -6, -4], 

....: [ 3, -5, 6], 

....: [ 1, 2, 3]]) 

sage: V = (QQ^3).subspace_with_basis(C.rows()) 

sage: H = Hom(U, V) 

 

sage: D = matrix(QQ, [[-7, -2, -5, 2], 

....: [-5, 1, -4, -8], 

....: [ 1, -1, 1, 4], 

....: [-4, -1, -3, 1]]) 

sage: X = (QQ^4).subspace_with_basis(D.rows()) 

sage: E = matrix(QQ, [[ 4, -1, 4], 

....: [ 5, -4, -5], 

....: [-1, 0, -2]]) 

sage: Y = (QQ^3).subspace_with_basis(E.rows()) 

sage: K = Hom(X, Y) 

 

sage: f = lambda x: vector(QQ, [x[0]+x[1], 2*x[1]-4*x[2], 5*x[3]]) 

sage: g = lambda x: vector(QQ, [x[0]-x[2], 2*x[1]-4*x[2], 5*x[3]]) 

 

sage: rho = H(f) 

sage: phi = K(f) 

sage: zeta = H(g) 

 

sage: rho.is_equal_function(phi) 

True 

sage: phi.is_equal_function(rho) 

True 

sage: zeta.is_equal_function(rho) 

False 

sage: phi.is_equal_function(zeta) 

False 

 

TESTS:: 

 

sage: H = Hom(ZZ^2, ZZ^2) 

sage: phi = H(matrix(ZZ, 2, range(4))) 

sage: phi.is_equal_function('junk') 

Traceback (most recent call last): 

... 

TypeError: can only compare to a matrix morphism, not junk 

 

AUTHOR: 

 

- Rob Beezer (2011-07-15) 

""" 

if not is_MatrixMorphism(other): 

msg = 'can only compare to a matrix morphism, not {0}' 

raise TypeError(msg.format(other)) 

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

return False 

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

return False 

# check agreement on any basis of the domain 

return all( self(u) == other(u) for u in self.domain().basis() ) 

 

def restrict_domain(self, sub): 

""" 

Restrict this matrix morphism to a subspace sub of the domain. The 

subspace sub should have a basis() method and elements of the basis 

should be coercible into domain. 

 

The resulting morphism has the same codomain as before, but a new 

domain. 

 

EXAMPLES:: 

 

sage: V = ZZ^2; phi = V.hom([3*V.0, 2*V.1]) 

sage: phi.restrict_domain(V.span([V.0])) 

Free module morphism defined by the matrix 

[3 0] 

Domain: Free module of degree 2 and rank 1 over Integer Ring 

Echelon ... 

Codomain: Ambient free module of rank 2 over the principal ideal domain ... 

sage: phi.restrict_domain(V.span([V.1])) 

Free module morphism defined by the matrix 

[0 2]... 

""" 

D = self.domain() 

if hasattr(D, 'coordinate_module'): 

# We only have to do this in case the module supports 

# alternative basis. Some modules do, some modules don't. 

V = D.coordinate_module(sub) 

else: 

V = sub.free_module() 

A = self.matrix().restrict_domain(V) 

H = sub.Hom(self.codomain()) 

return H(A) 

 

def restrict_codomain(self, sub): 

""" 

Restrict this matrix morphism to a subspace sub of the codomain. 

 

The resulting morphism has the same domain as before, but a new 

codomain. 

 

EXAMPLES:: 

 

sage: V = ZZ^2; phi = V.hom([4*(V.0+V.1),0]) 

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

sage: phi 

Free module morphism defined by the matrix 

[4 4] 

[0 0] 

Domain: Ambient free module of rank 2 over the principal ideal domain ... 

Codomain: Ambient free module of rank 2 over the principal ideal domain ... 

sage: psi = phi.restrict_codomain(W); psi 

Free module morphism defined by the matrix 

[2] 

[0] 

Domain: Ambient free module of rank 2 over the principal ideal domain ... 

Codomain: Free module of degree 2 and rank 1 over Integer Ring 

Echelon ... 

 

An example in which the codomain equals the full ambient space, but 

with a different basis:: 

 

sage: V = QQ^2 

sage: W = V.span_of_basis([[1,2],[3,4]]) 

sage: phi = V.hom(matrix(QQ,2,[1,0,2,0]),W) 

sage: phi.matrix() 

[1 0] 

[2 0] 

sage: phi(V.0) 

(1, 2) 

sage: phi(V.1) 

(2, 4) 

sage: X = V.span([[1,2]]); X 

Vector space of degree 2 and dimension 1 over Rational Field 

Basis matrix: 

[1 2] 

sage: phi(V.0) in X 

True 

sage: phi(V.1) in X 

True 

sage: psi = phi.restrict_codomain(X); psi 

Vector space morphism represented by the matrix: 

[1] 

[2] 

Domain: Vector space of dimension 2 over Rational Field 

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

Basis matrix: 

[1 2] 

sage: psi(V.0) 

(1, 2) 

sage: psi(V.1) 

(2, 4) 

sage: psi(V.0).parent() is X 

True 

""" 

H = self.domain().Hom(sub) 

C = self.codomain() 

if hasattr(C, 'coordinate_module'): 

# We only have to do this in case the module supports 

# alternative basis. Some modules do, some modules don't. 

V = C.coordinate_module(sub) 

else: 

V = sub.free_module() 

return H(self.matrix().restrict_codomain(V)) 

 

 

def restrict(self, sub): 

""" 

Restrict this matrix morphism to a subspace sub of the domain. 

 

The codomain and domain of the resulting matrix are both sub. 

 

EXAMPLES:: 

 

sage: V = ZZ^2; phi = V.hom([3*V.0, 2*V.1]) 

sage: phi.restrict(V.span([V.0])) 

Free module morphism defined by the matrix 

[3] 

Domain: Free module of degree 2 and rank 1 over Integer Ring 

Echelon ... 

Codomain: Free module of degree 2 and rank 1 over Integer Ring 

Echelon ... 

 

sage: V = (QQ^2).span_of_basis([[1,2],[3,4]]) 

sage: phi = V.hom([V.0+V.1, 2*V.1]) 

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

True 

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

sage: phi(W) 

Vector space of degree 2 and dimension 1 over Rational Field 

Basis matrix: 

[ 1 4/3] 

sage: psi = phi.restrict(W); psi 

Vector space morphism represented by the matrix: 

[2] 

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

Basis matrix: 

[ 1 4/3] 

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

Basis matrix: 

[ 1 4/3] 

sage: psi.domain() == W 

True 

sage: psi(W.0) == 2*W.0 

True 

""" 

if not self.is_endomorphism(): 

raise ArithmeticError("matrix morphism must be an endomorphism") 

D = self.domain() 

C = self.codomain() 

if D is not C and (D.basis() != C.basis()): 

# Tricky case when two bases for same space 

return self.restrict_domain(sub).restrict_codomain(sub) 

if hasattr(D, 'coordinate_module'): 

# We only have to do this in case the module supports 

# alternative basis. Some modules do, some modules don't. 

V = D.coordinate_module(sub) 

else: 

V = sub.free_module() 

A = self.matrix().restrict(V) 

H = sage.categories.homset.End(sub, self.domain().category()) 

return H(A) 

 

 

class MatrixMorphism(MatrixMorphism_abstract): 

""" 

A morphism defined by a matrix. 

 

INPUT: 

 

- ``parent`` -- a homspace 

 

- ``A`` -- matrix or a :class:`MatrixMorphism_abstract` instance 

 

- ``copy_matrix`` -- (default: ``True``) make an immutable copy of 

the matrix ``A`` if it is mutable; if ``False``, then this makes 

``A`` immutable 

""" 

def __init__(self, parent, A, copy_matrix=True): 

""" 

Initialize ``self``. 

 

EXAMPLES:: 

 

sage: from sage.modules.matrix_morphism import MatrixMorphism 

sage: T = End(ZZ^3) 

sage: M = MatrixSpace(ZZ,3) 

sage: I = M.identity_matrix() 

sage: A = MatrixMorphism(T, I) 

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

True 

""" 

if parent is None: 

raise ValueError("no parent given when creating this matrix morphism") 

if isinstance(A, MatrixMorphism_abstract): 

A = A.matrix() 

R = A.base_ring() 

if A.nrows() != parent.domain().rank(): 

raise ArithmeticError("number of rows of matrix (={}) must equal rank of domain (={})".format(A.nrows(), parent.domain().rank())) 

if A.ncols() != parent.codomain().rank(): 

raise ArithmeticError("number of columns of matrix (={}) must equal rank of codomain (={})".format(A.ncols(), parent.codomain().rank())) 

if A.is_mutable(): 

if copy_matrix: 

from copy import copy 

A = copy(A) 

A.set_immutable() 

self._matrix = A 

MatrixMorphism_abstract.__init__(self, parent) 

 

def matrix(self, side='left'): 

r""" 

Return a matrix that defines this morphism. 

 

INPUT: 

 

- ``side`` -- (default: ``'left'``) the side of the matrix 

where a vector is placed to effect the morphism (function) 

 

OUTPUT: 

 

A matrix which represents the morphism, relative to bases 

for the domain and codomain. If the modules are provided 

with user bases, then the representation is relative to 

these bases. 

 

Internally, Sage represents a matrix morphism with the 

matrix multiplying a row vector placed to the left of the 

matrix. If the option ``side='right'`` is used, then a 

matrix is returned that acts on a vector to the right of 

the matrix. These two matrices are just transposes of 

each other and the difference is just a preference for 

the style of representation. 

 

EXAMPLES:: 

 

sage: V = ZZ^2; W = ZZ^3 

sage: m = column_matrix([3*V.0 - 5*V.1, 4*V.0 + 2*V.1, V.0 + V.1]) 

sage: phi = V.hom(m, W) 

sage: phi.matrix() 

[ 3 4 1] 

[-5 2 1] 

 

sage: phi.matrix(side='right') 

[ 3 -5] 

[ 4 2] 

[ 1 1] 

 

TESTS:: 

 

sage: V = ZZ^2 

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

sage: phi.matrix(side='junk') 

Traceback (most recent call last): 

... 

ValueError: side must be 'left' or 'right', not junk 

""" 

if not side in ['left', 'right']: 

raise ValueError("side must be 'left' or 'right', not {0}".format(side)) 

if side == 'left': 

return self._matrix 

else: 

return self._matrix.transpose() 

 

def is_injective(self): 

""" 

Tell whether ``self`` is injective. 

 

EXAMPLES:: 

 

sage: V1 = QQ^2 

sage: V2 = QQ^3 

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

sage: phi.is_injective() 

True 

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

sage: psi.is_injective() 

False 

 

AUTHOR: 

 

-- Simon King (2010-05) 

""" 

return self._matrix.kernel().dimension() == 0 

 

def is_surjective(self): 

r""" 

Tell whether ``self`` is surjective. 

 

EXAMPLES:: 

 

sage: V1 = QQ^2 

sage: V2 = QQ^3 

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

sage: phi.is_surjective() 

False 

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

sage: psi.is_surjective() 

True 

 

An example over a PID that is not `\ZZ`. :: 

 

sage: R = PolynomialRing(QQ, 'x') 

sage: A = R^2 

sage: B = R^2 

sage: H = A.hom([B([x^2-1, 1]), B([x^2, 1])]) 

sage: H.image() 

Free module of degree 2 and rank 2 over Univariate Polynomial Ring in x over Rational Field 

Echelon basis matrix: 

[ 1 0] 

[ 0 -1] 

sage: H.is_surjective() 

True 

 

This tests if :trac:`11552` is fixed. :: 

 

sage: V = ZZ^2 

sage: m = matrix(ZZ, [[1,2],[0,2]]) 

sage: phi = V.hom(m, V) 

sage: phi.lift(vector(ZZ, [0, 1])) 

Traceback (most recent call last): 

... 

ValueError: element is not in the image 

sage: phi.is_surjective() 

False 

 

AUTHORS: 

 

- Simon King (2010-05) 

- Rob Beezer (2011-06-28) 

""" 

# Testing equality of free modules over PIDs is unreliable 

# see Trac #11579 for explanation and status 

# We test if image equals codomain with two inclusions 

# reverse inclusion of below is trivially true 

return self.codomain().is_submodule(self.image()) 

 

def _repr_(self): 

r""" 

Return string representation of this matrix morphism. 

 

This will typically be overloaded in a derived class. 

 

EXAMPLES:: 

 

sage: V = ZZ^2; phi = V.hom([3*V.0, 2*V.1]) 

sage: sage.modules.matrix_morphism.MatrixMorphism._repr_(phi) 

'Morphism defined by the matrix\n[3 0]\n[0 2]' 

 

sage: phi._repr_() 

'Free module morphism defined by the matrix\n[3 0]\n[0 2]\nDomain: Ambient free module of rank 2 over the principal ideal domain Integer Ring\nCodomain: Ambient free module of rank 2 over the principal ideal domain Integer Ring' 

""" 

return "Morphism defined by the matrix\n{0}".format(self.matrix())