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

""" 

Indexed Free Groups 

 

Free groups and free abelian groups implemented using an indexed set of 

generators. 

 

AUTHORS: 

 

- Travis Scrimshaw (2013-10-16): Initial version 

""" 

 

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

# Copyright (C) 2013 Travis Scrimshaw <tscrim at ucdavis.edu> 

# 

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

# 

# The full text of the GPL is available at: 

# 

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

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

from six import integer_types 

 

from copy import copy 

from sage.categories.groups import Groups 

from sage.categories.poor_man_map import PoorManMap 

from sage.groups.group import Group, AbelianGroup 

from sage.monoids.indexed_free_monoid import (IndexedMonoid, 

IndexedMonoidElement, IndexedFreeMonoidElement, 

IndexedFreeAbelianMonoidElement) 

from sage.misc.cachefunc import cached_method 

import sage.data_structures.blas_dict as blas 

from sage.rings.integer import Integer 

from sage.rings.infinity import infinity 

from sage.sets.family import Family 

from six import iteritems 

 

class IndexedGroup(IndexedMonoid): 

""" 

Base class for free (abelian) groups whose generators are indexed 

by a set. 

 

TESTS: 

 

We check finite properties:: 

 

sage: G = Groups().free(index_set=ZZ) 

sage: G.is_finite() 

False 

sage: G = Groups().free(index_set='abc') 

sage: G.is_finite() 

False 

sage: G = Groups().free(index_set=[]) 

sage: G.is_finite() 

True 

 

:: 

 

sage: G = Groups().Commutative().free(index_set=ZZ) 

sage: G.is_finite() 

False 

sage: G = Groups().Commutative().free(index_set='abc') 

sage: G.is_finite() 

False 

sage: G = Groups().Commutative().free(index_set=[]) 

sage: G.is_finite() 

True 

""" 

def order(self): 

r""" 

Return the number of elements of ``self``, which is `\infty` unless 

this is the trivial group. 

 

EXAMPLES:: 

 

sage: G = Groups().free(index_set=ZZ) 

sage: G.order() 

+Infinity 

sage: G = Groups().Commutative().free(index_set='abc') 

sage: G.order() 

+Infinity 

sage: G = Groups().Commutative().free(index_set=[]) 

sage: G.order() 

1 

""" 

return self.cardinality() 

 

def rank(self): 

""" 

Return the rank of ``self``. 

 

This is the number of generators of ``self``. 

 

EXAMPLES:: 

 

sage: G = Groups().free(index_set=ZZ) 

sage: G.rank() 

+Infinity 

sage: G = Groups().free(index_set='abc') 

sage: G.rank() 

3 

sage: G = Groups().free(index_set=[]) 

sage: G.rank() 

0 

 

:: 

 

sage: G = Groups().Commutative().free(index_set=ZZ) 

sage: G.rank() 

+Infinity 

sage: G = Groups().Commutative().free(index_set='abc') 

sage: G.rank() 

3 

sage: G = Groups().Commutative().free(index_set=[]) 

sage: G.rank() 

0 

""" 

return self.group_generators().cardinality() 

 

@cached_method 

def group_generators(self): 

""" 

Return the group generators of ``self``. 

 

EXAMPLES:: 

 

sage: G = Groups.free(index_set=ZZ) 

sage: G.group_generators() 

Lazy family (Generator map from Integer Ring to 

Free group indexed by Integer Ring(i))_{i in Integer Ring} 

sage: G = Groups().free(index_set='abcde') 

sage: sorted(G.group_generators()) 

[F['a'], F['b'], F['c'], F['d'], F['e']] 

""" 

if self._indices.cardinality() == infinity: 

gen = PoorManMap(self.gen, domain=self._indices, codomain=self, name="Generator map") 

return Family(self._indices, gen) 

return Family(self._indices, self.gen) 

 

gens = group_generators 

 

class IndexedFreeGroup(IndexedGroup, Group): 

""" 

An indexed free group. 

 

EXAMPLES:: 

 

sage: G = Groups().free(index_set=ZZ) 

sage: G 

Free group indexed by Integer Ring 

sage: G = Groups().free(index_set='abcde') 

sage: G 

Free group indexed by {'a', 'b', 'c', 'd', 'e'} 

""" 

def __init__(self, indices, prefix, category=None, **kwds): 

""" 

Initialize ``self``. 

 

TESTS:: 

 

sage: G = Groups().free(index_set=ZZ) 

sage: TestSuite(G).run() 

sage: G = Groups().free(index_set='abc') 

sage: TestSuite(G).run() 

""" 

category = Groups().or_subcategory(category) 

IndexedGroup.__init__(self, indices, prefix, category, **kwds) 

 

def _repr_(self): 

""" 

Return a string representation of ``self`` 

 

TESTS:: 

 

sage: Groups().free(index_set=ZZ) # indirect doctest 

Free group indexed by Integer Ring 

""" 

return 'Free group indexed by {}'.format(self._indices) 

 

@cached_method 

def one(self): 

""" 

Return the identity element of ``self``. 

 

EXAMPLES:: 

 

sage: G = Groups().free(ZZ) 

sage: G.one() 

1 

""" 

return self.element_class(self, ()) 

 

def gen(self, x): 

""" 

The generator indexed by ``x`` of ``self``. 

 

EXAMPLES:: 

 

sage: G = Groups().free(index_set=ZZ) 

sage: G.gen(0) 

F[0] 

sage: G.gen(2) 

F[2] 

""" 

if x not in self._indices: 

raise IndexError("{} is not in the index set".format(x)) 

try: 

return self.element_class(self, ((self._indices(x),1),)) 

except TypeError: # Backup (if it is a string) 

return self.element_class(self, ((x,1),)) 

 

class Element(IndexedFreeMonoidElement): 

def __len__(self): 

""" 

Return the length of ``self``. 

 

EXAMPLES:: 

 

sage: G = Groups().free(index_set=ZZ) 

sage: a,b,c,d,e = [G.gen(i) for i in range(5)] 

sage: elt = a*c^-3*b^-2*a 

sage: elt.length() 

7 

sage: len(elt) 

7 

 

sage: G = Groups().free(index_set=ZZ) 

sage: a,b,c,d,e = [G.gen(i) for i in range(5)] 

sage: elt = a*c^-3*b^-2*a 

sage: elt.length() 

7 

sage: len(elt) 

7 

""" 

return sum(abs(exp) for gen,exp in self._monomial) 

 

length = __len__ 

 

def _mul_(self, other): 

""" 

Multiply ``self`` by ``other``. 

 

EXAMPLES:: 

 

sage: G = Groups().free(index_set=ZZ) 

sage: a,b,c,d,e = [G.gen(i) for i in range(5)] 

sage: a*b^2*e*d 

F[0]*F[1]^2*F[4]*F[3] 

sage: (a*b^2*d^2) * (d^-4*b*e) 

F[0]*F[1]^2*F[3]^-2*F[1]*F[4] 

sage: (a*b^-2*d^2) * (d^-2*b^2*a^-1) 

1 

""" 

if not self._monomial: 

return other 

if not other._monomial: 

return self 

 

ret = list(self._monomial) 

rhs = list(other._monomial) 

while len(ret) > 0 and len(rhs) > 0 and ret[-1][0] == rhs[0][0]: 

rhs[0] = (rhs[0][0], rhs[0][1] + ret.pop()[1]) 

if rhs[0][1] == 0: 

rhs.pop(0) 

ret += rhs 

return self.__class__(self.parent(), tuple(ret)) 

 

def __invert__(self): 

""" 

Return the inverse of ``self``. 

 

EXAMPLES:: 

 

sage: G = Groups().free(index_set=ZZ) 

sage: a,b,c,d,e = [G.gen(i) for i in range(5)] 

sage: x = a*b^2*e^-1*d; ~x 

F[3]^-1*F[4]*F[1]^-2*F[0]^-1 

sage: x * ~x 

1 

""" 

return self.__class__(self.parent(), 

tuple((x[0], -x[1]) for x in reversed(self._monomial))) 

 

def to_word_list(self): 

""" 

Return ``self`` as a word represented as a list whose entries 

are the pairs ``(i, s)`` where ``i`` is the index and ``s`` is 

the sign. 

 

EXAMPLES:: 

 

sage: G = Groups().free(index_set=ZZ) 

sage: a,b,c,d,e = [G.gen(i) for i in range(5)] 

sage: x = a*b^2*e*a^-1 

sage: x.to_word_list() 

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

""" 

sign = lambda x: 1 if x > 0 else -1 # It is never 0 

return [ (k, sign(e)) for k,e in self._sorted_items() 

for dummy in range(abs(e))] 

 

class IndexedFreeAbelianGroup(IndexedGroup, AbelianGroup): 

""" 

An indexed free abelian group. 

 

EXAMPLES:: 

 

sage: G = Groups().Commutative().free(index_set=ZZ) 

sage: G 

Free abelian group indexed by Integer Ring 

sage: G = Groups().Commutative().free(index_set='abcde') 

sage: G 

Free abelian group indexed by {'a', 'b', 'c', 'd', 'e'} 

""" 

def __init__(self, indices, prefix, category=None, **kwds): 

""" 

Initialize ``self``. 

 

TESTS:: 

 

sage: G = Groups().Commutative().free(index_set=ZZ) 

sage: TestSuite(G).run() 

sage: G = Groups().Commutative().free(index_set='abc') 

sage: TestSuite(G).run() 

""" 

category = Groups().or_subcategory(category) 

IndexedGroup.__init__(self, indices, prefix, category, **kwds) 

 

def _repr_(self): 

""" 

TESTS:: 

 

sage: Groups.Commutative().free(index_set=ZZ) 

Free abelian group indexed by Integer Ring 

""" 

return 'Free abelian group indexed by {}'.format(self._indices) 

 

def _element_constructor_(self, x=None): 

""" 

Create an element of ``self`` from ``x``. 

 

EXAMPLES:: 

 

sage: G = Groups().Commutative().free(index_set=ZZ) 

sage: G(G.gen(2)) 

F[2] 

sage: G([[1, 3], [-2, 12]]) 

F[-2]^12*F[1]^3 

sage: G({1: 3, -2: 12}) 

F[-2]^12*F[1]^3 

sage: G(-5) 

Traceback (most recent call last): 

... 

TypeError: unable to convert -5, use gen() instead 

 

TESTS:: 

 

sage: G([(1, 3), (1, -5)]) 

F[1]^-2 

 

sage: G([(42, 0)]) 

1 

sage: G([(42, 3), (42, -3)]) 

1 

sage: G({42: 0}) 

1 

""" 

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

d = dict() 

for k, v in x: 

if k in d: 

d[k] += v 

else: 

d[k] = v 

x = d 

if isinstance(x, dict): 

x = {k: v for k, v in iteritems(x) if v != 0} 

return IndexedGroup._element_constructor_(self, x) 

 

@cached_method 

def one(self): 

""" 

Return the identity element of ``self``. 

 

EXAMPLES:: 

 

sage: G = Groups().Commutative().free(index_set=ZZ) 

sage: G.one() 

1 

""" 

return self.element_class(self, {}) 

 

def gen(self, x): 

""" 

The generator indexed by ``x`` of ``self``. 

 

EXAMPLES:: 

 

sage: G = Groups().Commutative().free(index_set=ZZ) 

sage: G.gen(0) 

F[0] 

sage: G.gen(2) 

F[2] 

""" 

if x not in self._indices: 

raise IndexError("{} is not in the index set".format(x)) 

try: 

return self.element_class(self, {self._indices(x):1}) 

except TypeError: # Backup (if it is a string) 

return self.element_class(self, {x:1}) 

 

class Element(IndexedFreeAbelianMonoidElement, IndexedFreeGroup.Element): 

def _mul_(self, other): 

""" 

Multiply ``self`` by ``other``. 

 

EXAMPLES:: 

 

sage: G = Groups().Commutative().free(index_set=ZZ) 

sage: a,b,c,d,e = [G.gen(i) for i in range(5)] 

sage: a*b^2*e^-1*d 

F[0]*F[1]^2*F[3]*F[4]^-1 

sage: (a*b^2*d^2) * (d^-4*b^-2*e) 

F[0]*F[3]^-2*F[4] 

sage: (a*b^-2*d^2) * (d^-2*b^2*a^-1) 

1 

""" 

return self.__class__(self.parent(), 

blas.add(self._monomial, other._monomial)) 

 

def __invert__(self): 

""" 

Return the inverse of ``self``. 

 

EXAMPLES:: 

 

sage: G = Groups().Commutative().free(index_set=ZZ) 

sage: a,b,c,d,e = [G.gen(i) for i in range(5)] 

sage: x = a*b^2*e^-1*d; ~x 

F[0]^-1*F[1]^-2*F[3]^-1*F[4] 

sage: x * ~x 

1 

""" 

return self ** -1 

 

def __floordiv__(self, a): 

""" 

Return the division of ``self`` by ``a``. 

 

EXAMPLES:: 

 

sage: G = Groups().Commutative().free(index_set=ZZ) 

sage: a,b,c,d,e = [G.gen(i) for i in range(5)] 

sage: elt = a*b*c^3*d^2; elt 

F[0]*F[1]*F[2]^3*F[3]^2 

sage: elt // a 

F[1]*F[2]^3*F[3]^2 

sage: elt // c 

F[0]*F[1]*F[2]^2*F[3]^2 

sage: elt // (a*b*d^2) 

F[2]^3 

sage: elt // a^4 

F[0]^-3*F[1]*F[2]^3*F[3]^2 

""" 

return self * ~a 

 

def __pow__(self, n): 

""" 

Raise ``self`` to the power of ``n``. 

 

EXAMPLES:: 

 

sage: G = Groups().Commutative().free(index_set=ZZ) 

sage: a,b,c,d,e = [G.gen(i) for i in range(5)] 

sage: x = a*b^2*e^-1*d; x 

F[0]*F[1]^2*F[3]*F[4]^-1 

sage: x^3 

F[0]^3*F[1]^6*F[3]^3*F[4]^-3 

sage: x^0 

1 

sage: x^-3 

F[0]^-3*F[1]^-6*F[3]^-3*F[4]^3 

""" 

if not isinstance(n, integer_types + (Integer,)): 

raise TypeError("Argument n (= {}) must be an integer".format(n)) 

if n == 1: 

return self 

if n == 0: 

return self.parent().one() 

return self.__class__(self.parent(), {k:v*n for k,v in iteritems(self._monomial)})