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

""" 

Elements of Affine Groups 

 

The class in this module is used to represent the elements of 

:func:`~sage.groups.affine_gps.affine_group.AffineGroup` and its 

subgroups. 

 

EXAMPLES:: 

 

sage: F = AffineGroup(3, QQ) 

sage: F([1,2,3,4,5,6,7,8,0], [10,11,12]) 

[1 2 3] [10] 

x |-> [4 5 6] x + [11] 

[7 8 0] [12] 

 

sage: G = AffineGroup(2, ZZ) 

sage: g = G([[1,1],[0,1]], [1,0]) 

sage: h = G([[1,2],[0,1]], [0,1]) 

sage: g*h 

[1 3] [2] 

x |-> [0 1] x + [1] 

sage: h*g 

[1 3] [1] 

x |-> [0 1] x + [1] 

sage: g*h != h*g 

True 

 

AUTHORS: 

 

- Volker Braun 

""" 

 

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

# Copyright (C) 2013 Volker Braun <vbraun.name@gmail.com> 

# 

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

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

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

# (at your option) any later version. 

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

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

 

from sage.structure.element import is_Matrix 

from sage.misc.cachefunc import cached_method 

from sage.structure.element import MultiplicativeGroupElement 

from sage.structure.richcmp import richcmp, richcmp_not_equal 

 

 

class AffineGroupElement(MultiplicativeGroupElement): 

""" 

An affine group element. 

 

INPUT: 

 

- ``A`` -- an invertible matrix, or something defining a 

matrix if ``convert==True``. 

 

- ``b``-- a vector, or something defining a vector if 

``convert==True`` (default: ``0``, defining the zero 

vector). 

 

- ``parent`` -- the parent affine group. 

 

- ``convert`` - bool (default: ``True``). Whether to convert 

``A`` into the correct matrix space and ``b`` into the 

correct vector space. 

 

- ``check`` - bool (default: ``True``). Whether to do some 

checks or just accept the input as valid. 

 

As a special case, ``A`` can be a matrix obtained from 

:meth:`matrix`, that is, one row and one column larger. In 

that case, the group element defining that matrix is 

reconstructed. 

 

OUTPUT: 

 

The affine group element `x \mapsto Ax + b` 

 

EXAMPLES:: 

 

sage: G = AffineGroup(2, GF(3)) 

sage: g = G.random_element() 

sage: type(g) 

<class 'sage.groups.affine_gps.affine_group.AffineGroup_with_category.element_class'> 

sage: G(g.matrix()) == g 

True 

sage: G(2) 

[2 0] [0] 

x |-> [0 2] x + [0] 

 

Conversion from a matrix and a matrix group element:: 

 

sage: M = Matrix(4, 4, [0, 0, -1, 1, 0, -1, 0, 1, -1, 0, 0, 1, 0, 0, 0, 1]) 

sage: A = AffineGroup(3, ZZ) 

sage: A(M) 

[ 0 0 -1] [1] 

x |-> [ 0 -1 0] x + [1] 

[-1 0 0] [1] 

sage: G = MatrixGroup([M]) 

sage: A(G.0) 

[ 0 0 -1] [1] 

x |-> [ 0 -1 0] x + [1] 

[-1 0 0] [1] 

""" 

def __init__(self, parent, A, b=0, convert=True, check=True): 

r""" 

Create element of an affine group. 

 

TESTS:: 

 

sage: G = AffineGroup(4, GF(5)) 

sage: g = G.random_element() 

sage: TestSuite(g).run() 

""" 

try: 

A = A.matrix() 

except AttributeError: 

pass 

if is_Matrix(A) and A.nrows() == A.ncols() == parent.degree()+1: 

g = A 

d = parent.degree() 

A = g.submatrix(0, 0, d, d) 

b = [ g[i,d] for i in range(d) ] 

convert = True 

if convert: 

A = parent.matrix_space()(A) 

b = parent.vector_space()(b) 

if check: 

# Note: the coercion framework expects that we raise TypeError for invalid input 

if not is_Matrix(A): 

raise TypeError('A must be a matrix') 

if not (A.parent() is parent.matrix_space()): 

raise TypeError('A must be an element of '+str(parent.matrix_space())) 

if not (b.parent() is parent.vector_space()): 

raise TypeError('b must be an element of '+str(parent.vector_space())) 

parent._element_constructor_check(A, b) 

super(AffineGroupElement, self).__init__(parent) 

self._A = A 

self._b = b 

 

def A(self): 

""" 

Return the general linear part of an affine group element. 

 

OUTPUT: 

 

The matrix `A` of the affine group element `Ax + b`. 

 

EXAMPLES:: 

 

sage: G = AffineGroup(3, QQ) 

sage: g = G([1,2,3,4,5,6,7,8,0], [10,11,12]) 

sage: g.A() 

[1 2 3] 

[4 5 6] 

[7 8 0] 

""" 

return self._A 

 

def b(self): 

""" 

Return the translation part of an affine group element. 

 

OUTPUT: 

 

The vector `b` of the affine group element `Ax + b`. 

 

EXAMPLES:: 

 

sage: G = AffineGroup(3, QQ) 

sage: g = G([1,2,3,4,5,6,7,8,0], [10,11,12]) 

sage: g.b() 

(10, 11, 12) 

""" 

return self._b 

 

@cached_method 

def matrix(self): 

""" 

Return the standard matrix representation of ``self``. 

 

.. SEEALSO:: 

 

- :meth:`AffineGroup.linear_space()` 

 

EXAMPLES:: 

 

sage: G = AffineGroup(3, GF(7)) 

sage: g = G([1,2,3,4,5,6,7,8,0], [10,11,12]) 

sage: g 

[1 2 3] [3] 

x |-> [4 5 6] x + [4] 

[0 1 0] [5] 

sage: g.matrix() 

[1 2 3|3] 

[4 5 6|4] 

[0 1 0|5] 

[-----+-] 

[0 0 0|1] 

sage: parent(g.matrix()) 

Full MatrixSpace of 4 by 4 dense matrices over Finite Field of size 7 

sage: g.matrix() == matrix(g) 

True 

 

Composition of affine group elements equals multiplication of 

the matrices:: 

 

sage: g1 = G.random_element() 

sage: g2 = G.random_element() 

sage: g1.matrix() * g2.matrix() == (g1*g2).matrix() 

True 

""" 

A = self._A 

b = self._b 

parent = self.parent() 

d = parent.degree() 

from sage.matrix.constructor import matrix, zero_matrix, block_matrix 

zero = zero_matrix(parent.base_ring(), 1, d) 

one = matrix(parent.base_ring(), [[1]]) 

m = block_matrix(2,2, [A, b.column(), zero, one]) 

m.set_immutable() 

return m 

 

_matrix_ = matrix 

 

def _repr_(self): 

""" 

Return a string representation of ``self``. 

 

EXAMPLES:: 

 

sage: G = AffineGroup(2, QQ) 

sage: g = G([[1, 1], [0, 1]], [3,4]) 

sage: g 

[1 1] [3] 

x |-> [0 1] x + [4] 

""" 

A = str(self._A) 

b = str(self._b.column()) 

deg = self.parent().degree() 

indices = range(deg) 

s = [] 

for Ai, bi, i in zip(A.splitlines(), b.splitlines(), indices): 

if i == deg//2: 

s.append('x |-> '+Ai+' x + '+bi) 

else: 

s.append(' '+Ai+' '+bi) 

return '\n'.join(s) 

 

def _latex_(self): 

r""" 

Return a LaTeX representation of ``self``. 

 

EXAMPLES:: 

 

sage: G = AffineGroup(2, QQ) 

sage: g = G([[1, 1], [0, 1]], [3,4]) 

sage: latex(g) 

\vec{x}\mapsto \left(\begin{array}{rr} 

1 & 1 \\ 

0 & 1 

\end{array}\right)\vec{x} + \left(\begin{array}{r} 

3 \\ 

4 

\end{array}\right) 

sage: g._latex_() 

'\\vec{x}\\mapsto \\left(\\begin{array}{rr}\n1 & 1 \\\\\n0 & 

1\n\\end{array}\\right)\\vec{x} + \\left(\\begin{array}{r}\n3 

\\\\\n4\n\\end{array}\\right)' 

""" 

return r'\vec{x}\mapsto '+self.A()._latex_()+r'\vec{x} + '+self.b().column()._latex_() 

 

def _mul_(self, other): 

""" 

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

 

INPUT: 

 

- ``other`` -- another element of the same affine group. 

 

OUTPUT: 

 

The product of the affine group elements ``self`` and 

``other`` defined by the composition of the two affine 

transformations. 

 

EXAMPLES:: 

 

sage: G = AffineGroup(2, GF(3)) 

sage: g = G([1,1, 0,1], [0,1]) 

sage: h = G([1,1, 0,1], [1,2]) 

sage: g*h 

[1 2] [0] 

x |-> [0 1] x + [0] 

sage: g.matrix() * h.matrix() == (g*h).matrix() 

True 

""" 

parent = self.parent() 

A = self._A * other._A 

b = self._b + self._A * other._b 

return parent.element_class(parent, A, b, check=False) 

 

def __call__(self, v): 

""" 

Apply the affine transformation to ``v``. 

 

INPUT: 

 

- ``v`` -- a multivariate polynomial, a vector, or anything 

that can be converted into a vector. 

 

OUTPUT: 

 

The image of ``v`` under the affine group element. 

 

EXAMPLES:: 

 

sage: G = AffineGroup(2, QQ) 

sage: g = G([0,1,-1,0],[2,3]); g 

[ 0 1] [2] 

x |-> [-1 0] x + [3] 

sage: v = vector([4,5]) 

sage: g(v) 

(7, -1) 

 

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

sage: g(x), g(y) 

(y + 2, -x + 3) 

sage: p = x^2 + 2*x*y + y + 1 

sage: g(p) 

-2*x*y + y^2 - 5*x + 10*y + 20 

 

The action on polynomials is such that it intertwines with 

evaluation. That is:: 

 

sage: p(*g(v)) == g(p)(*v) 

True 

 

Test that the univariate polynomial ring is covered:: 

 

sage: H = AffineGroup(1, QQ) 

sage: h = H([2],[3]); h 

x |-> [2] x + [3] 

sage: R.<z> = QQ[] 

sage: h(z+1) 

3*z + 2 

""" 

from sage.rings.polynomial.polynomial_element import is_Polynomial 

from sage.rings.polynomial.multi_polynomial import is_MPolynomial 

parent = self.parent() 

if is_Polynomial(v) and parent.degree() == 1: 

ring = v.parent() 

return ring([self._A[0,0], self._b[0]]) 

if is_MPolynomial(v) and parent.degree() == v.parent().ngens(): 

ring = v.parent() 

from sage.modules.all import vector 

image_coords = self._A * vector(ring, ring.gens()) + self._b 

return v(*image_coords) 

v = parent.vector_space()(v) 

return self._A*v + self._b 

 

def _act_on_(self, x, self_on_left): 

""" 

Define the multiplicative action of the affine group elements. 

 

EXAMPLES:: 

 

sage: G = AffineGroup(2, GF(3)) 

sage: g = G([1,2,3,4], [5,6]) 

sage: g 

[1 2] [2] 

x |-> [0 1] x + [0] 

sage: v = vector(GF(3), [1,-1]); v 

(1, 2) 

sage: g*v 

(1, 2) 

sage: g*v == g.A() * v + g.b() 

True 

""" 

if self_on_left: 

return self(x) 

 

def inverse(self): 

""" 

Return the inverse group element. 

 

OUTPUT: 

 

Another affine group element. 

 

EXAMPLES:: 

 

sage: G = AffineGroup(2, GF(3)) 

sage: g = G([1,2,3,4], [5,6]) 

sage: g 

[1 2] [2] 

x |-> [0 1] x + [0] 

sage: ~g 

[1 1] [1] 

x |-> [0 1] x + [0] 

sage: g * g.inverse() 

[1 0] [0] 

x |-> [0 1] x + [0] 

sage: g * g.inverse() == g.inverse() * g == G(1) 

True 

""" 

parent = self.parent() 

A = parent.matrix_space()(self._A.inverse()) 

b = -A*self.b() 

return parent.element_class(parent, A, b, check=False) 

 

__invert__ = inverse 

 

def _richcmp_(self, other, op): 

""" 

Compare ``self`` with ``other``. 

 

OUTPUT: 

 

boolean 

 

EXAMPLES:: 

 

sage: F = AffineGroup(3, QQ) 

sage: g = F([1,2,3,4,5,6,7,8,0], [10,11,12]) 

sage: h = F([1,2,3,4,5,6,7,8,0], [10,11,0]) 

sage: g == h 

False 

sage: g == g 

True 

""" 

lx = self._A 

rx = other._A 

if lx != rx: 

return richcmp_not_equal(lx, rx, op) 

 

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

 

def list(self): 

""" 

Return list representation of ``self``. 

 

EXAMPLES:: 

 

sage: F = AffineGroup(3, QQ) 

sage: g = F([1,2,3,4,5,6,7,8,0], [10,11,12]) 

sage: g 

[1 2 3] [10] 

x |-> [4 5 6] x + [11] 

[7 8 0] [12] 

sage: g.matrix() 

[ 1 2 3|10] 

[ 4 5 6|11] 

[ 7 8 0|12] 

[--------+--] 

[ 0 0 0| 1] 

sage: g.list() 

[[1, 2, 3, 10], [4, 5, 6, 11], [7, 8, 0, 12], [0, 0, 0, 1]] 

""" 

return [r.list() for r in self.matrix().rows()]