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

r""" 

Space of Morphisms of Vector Spaces (Linear Transformations) 

 

AUTHOR: 

 

- Rob Beezer: (2011-06-29) 

 

A :class:`VectorSpaceHomspace` object represents the set of all 

possible homomorphisms from one vector space to another. 

These mappings are usually known as linear transformations. 

 

For more information on the use of linear transformations, 

consult the documentation for vector space morphisms at 

:mod:`sage.modules.vector_space_morphism`. Also, this is 

an extremely thin veneer on free module homspaces 

(:mod:`sage.modules.free_module_homspace`) and free module 

morphisms (:mod:`sage.modules.free_module_morphism`) - 

objects which might also be useful, and places 

where much of the documentation resides. 

 

EXAMPLES: 

 

Creation and basic examination is simple. :: 

 

sage: V = QQ^3 

sage: W = QQ^2 

sage: H = Hom(V, W) 

sage: 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: H.domain() 

Vector space of dimension 3 over Rational Field 

sage: H.codomain() 

Vector space of dimension 2 over Rational Field 

 

Homspaces have a few useful properties. A basis is provided by 

a list of matrix representations, where these matrix representatives 

are relative to the bases of the domain and codomain. :: 

 

sage: K = Hom(GF(3)^2, GF(3)^2) 

sage: B = K.basis() 

sage: for f in B: 

....: print(f) 

....: print("\n") 

Vector space morphism represented by the matrix: 

[1 0] 

[0 0] 

Domain: Vector space of dimension 2 over Finite Field of size 3 

Codomain: Vector space of dimension 2 over Finite Field of size 3 

<BLANKLINE> 

Vector space morphism represented by the matrix: 

[0 1] 

[0 0] 

Domain: Vector space of dimension 2 over Finite Field of size 3 

Codomain: Vector space of dimension 2 over Finite Field of size 3 

<BLANKLINE> 

Vector space morphism represented by the matrix: 

[0 0] 

[1 0] 

Domain: Vector space of dimension 2 over Finite Field of size 3 

Codomain: Vector space of dimension 2 over Finite Field of size 3 

<BLANKLINE> 

Vector space morphism represented by the matrix: 

[0 0] 

[0 1] 

Domain: Vector space of dimension 2 over Finite Field of size 3 

Codomain: Vector space of dimension 2 over Finite Field of size 3 

<BLANKLINE> 

 

The zero and identity mappings are properties of the space. 

The identity mapping will only be available if the domain and codomain 

allow for endomorphisms (equal vector spaces with equal bases). :: 

 

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

sage: g = H.zero() 

sage: g([1, 1/2, -3]) 

(0, 0, 0) 

sage: f = H.identity() 

sage: f([1, 1/2, -3]) 

(1, 1/2, -3) 

 

The homspace may be used with various representations of a 

morphism in the space to create the morphism. We demonstrate 

three ways to create the same linear transformation between 

two two-dimensional subspaces of ``QQ^3``. The ``V.n`` notation 

is a shortcut to the generators of each vector space, better 

known as the basis elements. Note that the matrix representations 

are relative to the bases, which are purposely fixed when the 

subspaces are created ("user bases"). :: 

 

sage: U = QQ^3 

sage: V = U.subspace_with_basis([U.0+U.1, U.1-U.2]) 

sage: W = U.subspace_with_basis([U.0, U.1+U.2]) 

sage: H = Hom(V, W) 

 

First, with a matrix. Note that the matrix representation 

acts by matrix multiplication with the vector on the left. 

The input to the linear transformation, ``(3, 1, 2)``, 

is converted to the coordinate vector ``(3, -2)``, then 

matrix multiplication yields the vector ``(-3, -2)``, 

which represents the vector ``(-3, -2, -2)`` in the codomain. :: 

 

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

sage: f1 = H(m) 

sage: f1 

Vector space morphism represented by the matrix: 

[1 2] 

[3 4] 

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

User basis matrix: 

[ 1 1 0] 

[ 0 1 -1] 

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

User basis matrix: 

[1 0 0] 

[0 1 1] 

sage: f1([3,1,2]) 

(-3, -2, -2) 

 

Second, with a list of images of the domain's basis elements. :: 

 

sage: img = [1*(U.0) + 2*(U.1+U.2), 3*U.0 + 4*(U.1+U.2)] 

sage: f2 = H(img) 

sage: f2 

Vector space morphism represented by the matrix: 

[1 2] 

[3 4] 

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

User basis matrix: 

[ 1 1 0] 

[ 0 1 -1] 

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

User basis matrix: 

[1 0 0] 

[0 1 1] 

sage: f2([3,1,2]) 

(-3, -2, -2) 

 

Third, with a linear function taking the domain to the codomain. :: 

 

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

sage: f3 = H(g) 

sage: f3 

Vector space morphism represented by the matrix: 

[1 2] 

[3 4] 

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

User basis matrix: 

[ 1 1 0] 

[ 0 1 -1] 

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

User basis matrix: 

[1 0 0] 

[0 1 1] 

sage: f3([3,1,2]) 

(-3, -2, -2) 

 

The three linear transformations look the same, and are the same. :: 

 

sage: f1 == f2 

True 

sage: f2 == f3 

True 

 

TESTS:: 

 

sage: V = QQ^2 

sage: W = QQ^3 

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

sage: loads(dumps(H)) 

Set of Morphisms (Linear Transformations) from 

Vector space of dimension 2 over Rational Field to 

Vector space of dimension 3 over Rational Field 

sage: loads(dumps(H)) == H 

True 

""" 

 

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

# Copyright (C) 2011 Rob Beezer <beezer@ups.edu> 

# 

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

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

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

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

from __future__ import print_function 

from __future__ import absolute_import 

 

import inspect 

import sage.matrix.all as matrix 

import sage.modules.free_module_homspace 

 

# This module initially overrides just the minimum functionality necessary 

# from sage.modules.free_module_homspace.FreeModuleHomSpace. 

# If additional methods here override the free module homspace methods, 

# consider adjusting the free module doctests, since many are written with 

# examples that are actually vector spaces and not so many use "pure" modules 

# for the examples. 

 

 

def is_VectorSpaceHomspace(x): 

r""" 

Return ``True`` if ``x`` is a vector space homspace. 

 

INPUT: 

 

``x`` - anything 

 

EXAMPLES: 

 

To be a vector space morphism, the domain and codomain must both be 

vector spaces, in other words, modules over fields. If either 

set is just a module, then the ``Hom()`` constructor will build a 

space of free module morphisms. :: 

 

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

sage: type(H) 

<class 'sage.modules.vector_space_homspace.VectorSpaceHomspace_with_category'> 

sage: sage.modules.vector_space_homspace.is_VectorSpaceHomspace(H) 

True 

 

sage: K = Hom(QQ^3, ZZ^2) 

sage: type(K) 

<class 'sage.modules.free_module_homspace.FreeModuleHomspace_with_category'> 

sage: sage.modules.vector_space_homspace.is_VectorSpaceHomspace(K) 

False 

 

sage: L = Hom(ZZ^3, QQ^2) 

sage: type(L) 

<class 'sage.modules.free_module_homspace.FreeModuleHomspace_with_category'> 

sage: sage.modules.vector_space_homspace.is_VectorSpaceHomspace(L) 

False 

 

sage: sage.modules.vector_space_homspace.is_VectorSpaceHomspace('junk') 

False 

""" 

return isinstance(x, VectorSpaceHomspace) 

 

class VectorSpaceHomspace(sage.modules.free_module_homspace.FreeModuleHomspace): 

 

def __call__(self, A, check=True): 

r""" 

INPUT: 

 

- ``A`` - one of several possible inputs representing 

a morphism from this vector space homspace. 

- a vector space morphism in this homspace 

- a matrix representation relative to the bases of the vector spaces, 

which acts on a vector placed to the left of the matrix 

- a list or tuple containing images of the domain's basis vectors 

- a function from the domain to the codomain 

- ``check`` (default: True) - ``True`` or ``False``, required for 

compatibility with calls from 

:meth:`sage.structure.parent_gens.ParentWithGens.hom`. 

 

EXAMPLES:: 

 

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

sage: H = V.Hom(V) 

sage: H 

Set of Morphisms (Linear Transformations) from 

Vector space of degree 3 and dimension 2 over Rational Field 

User basis matrix: 

[1 1 0] 

[1 0 2] 

to 

Vector space of degree 3 and dimension 2 over Rational Field 

User basis matrix: 

[1 1 0] 

[1 0 2] 

 

Coercing a matrix:: 

 

sage: A = matrix(QQ, [[0, 1], [1, 0]]) 

sage: rho = H(A) # indirect doctest 

sage: rho 

Vector space morphism represented by the matrix: 

[0 1] 

[1 0] 

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

User basis matrix: 

[1 1 0] 

[1 0 2] 

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

User basis matrix: 

[1 1 0] 

[1 0 2] 

 

Coercing a list of images:: 

 

sage: phi = H([V.1, V.0]) 

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

True 

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

True 

sage: phi 

Vector space morphism represented by the matrix: 

[0 1] 

[1 0] 

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

User basis matrix: 

[1 1 0] 

[1 0 2] 

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

User basis matrix: 

[1 1 0] 

[1 0 2] 

 

Coercing a lambda function:: 

 

sage: f = lambda x: vector(QQ, [x[0], (1/2)*x[2], 2*x[1]]) 

sage: zeta = H(f) 

sage: zeta 

Vector space morphism represented by the matrix: 

[0 1] 

[1 0] 

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

User basis matrix: 

[1 1 0] 

[1 0 2] 

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

User basis matrix: 

[1 1 0] 

[1 0 2] 

 

Coercing a vector space morphism into the parent of a second vector 

space morphism will unify their parents:: 

 

sage: U = FreeModule(QQ,3, sparse=True ); V = QQ^4 

sage: W = FreeModule(QQ,3, sparse=False); X = QQ^4 

sage: H = Hom(U, V) 

sage: K = Hom(W, X) 

sage: H is K, H == K 

(False, True) 

 

sage: A = matrix(QQ, 3, 4, [0]*12) 

sage: f = H(A) 

sage: B = matrix(QQ, 3, 4, range(12)) 

sage: g = K(B) 

sage: f.parent() is H and g.parent() is K 

True 

 

sage: h = H(g) 

sage: f.parent() is h.parent() 

True 

 

See other examples in the module-level documentation. 

 

TESTS:: 

 

sage: V = GF(3)^0 

sage: W = GF(3)^1 

sage: H = V.Hom(W) 

sage: H.zero().is_zero() 

True 

 

Previously the above code resulted in a TypeError because the 

dimensions of the matrix were incorrect. 

""" 

from .vector_space_morphism import is_VectorSpaceMorphism, VectorSpaceMorphism 

D = self.domain() 

C = self.codomain() 

from sage.structure.element import is_Matrix 

if is_Matrix(A): 

pass 

elif is_VectorSpaceMorphism(A): 

A = A.matrix() 

elif inspect.isfunction(A): 

try: 

images = [A(g) for g in D.basis()] 

except (ValueError, TypeError, IndexError) as e: 

msg = 'function cannot be applied properly to some basis element because\n' + e.args[0] 

raise ValueError(msg) 

try: 

A = matrix.matrix(D.dimension(), C.dimension(), [C.coordinates(C(a)) for a in images]) 

except (ArithmeticError, TypeError) as e: 

msg = 'some image of the function is not in the codomain, because\n' + e.args[0] 

raise ArithmeticError(msg) 

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

if len(A) != len(D.basis()): 

msg = "number of images should equal the size of the domain's basis (={0}), not {1}" 

raise ValueError(msg.format(len(D.basis()), len(A))) 

try: 

v = [C(a) for a in A] 

A = matrix.matrix(D.dimension(), C.dimension(), [C.coordinates(a) for a in v]) 

except (ArithmeticError, TypeError) as e: 

msg = 'some proposed image is not in the codomain, because\n' + e.args[0] 

raise ArithmeticError(msg) 

else: 

msg = 'vector space homspace can only coerce matrices, vector space morphisms, functions or lists, not {0}' 

raise TypeError(msg.format(A)) 

return VectorSpaceMorphism(self, A) 

 

def _repr_(self): 

r""" 

Text representation of a space of vector space morphisms. 

 

EXAMPLES:: 

 

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

sage: H._repr_().split(' ') 

['Set', 'of', 'Morphisms', '(Linear', 'Transformations)', 

'from', 'Vector', 'space', 'of', 'dimension', '2', 'over', 

'Rational', 'Field', 'to', 'Vector', 'space', 'of', 

'dimension', '3', 'over', 'Rational', 'Field'] 

""" 

msg = 'Set of Morphisms (Linear Transformations) from {0} to {1}' 

return msg.format(self.domain(), self.codomain())