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

# -*- coding: utf-8 -*- 

from __future__ import print_function, division 

from sympy.core.basic import Basic 

from sympy.core import Symbol, Mod 

from sympy.printing.defaults import DefaultPrinting 

from sympy.utilities import public 

from sympy.utilities.iterables import flatten 

from sympy.combinatorics.free_group import FreeGroupElement, free_group, zero_mul_simp 

 

from itertools import chain, product 

from bisect import bisect_left 

 

 

@public 

def fp_group(fr_grp, relators=[]): 

_fp_group = FpGroup(fr_grp, relators) 

return (_fp_group,) + tuple(_fp_group._generators) 

 

@public 

def xfp_group(fr_grp, relators=[]): 

_fp_group = FpGroup(fr_grp, relators) 

return (_fp_group, _fp_group._generators) 

 

@public 

def vfp_group(fr_grpm, relators): 

_fp_group = FpGroup(symbols, relators) 

pollute([sym.name for sym in _fp_group.symbols], _fp_group.generators) 

return _fp_group 

 

 

def _parse_relators(rels): 

"""Parse the passed relators.""" 

return rels 

 

 

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

# FINITELY PRESENTED GROUPS # 

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

 

 

class FpGroup(DefaultPrinting): 

""" 

The FpGroup would take a FreeGroup and a list/tuple of relators, the 

relators would be specified in such a way that each of them be equal to the 

identity of the provided free group. 

""" 

is_group = True 

is_FpGroup = True 

is_PermutationGroup = False 

 

def __new__(cls, fr_grp, relators): 

relators = _parse_relators(relators) 

# return the corresponding FreeGroup if no relators are specified 

if not relators: 

return fr_grp 

obj = object.__new__(cls) 

obj._free_group = fr_grp 

obj._relators = relators 

obj.generators = obj._generators() 

obj.dtype = type("FpGroupElement", (FpGroupElement,), {"group": obj}) 

return obj 

 

@property 

def free_group(self): 

return self._free_group 

 

def relators(self): 

return tuple(self._relators) 

 

def _generators(self): 

"""Returns the generators of the associated free group.""" 

return self.free_group.generators 

 

def __str__(self): 

if self.free_group.rank > 30: 

str_form = "<fp group with %s generators>" % self.free_group.rank 

else: 

str_form = "<fp group on the generators %s>" % str(self.generators) 

return str_form 

 

__repr__ = __str__ 

 

 

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

# COSET TABLE # 

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

 

class CosetTable(DefaultPrinting): 

# coset_table: Mathematically a coset table 

# represented using a list of lists 

# alpha: Mathematically a coset (precisely, a live coset) 

# represented by an integer between i with 1 <= i <= n 

# α ∈ c 

# x: Mathematically an element of "A" (set of generators and 

# their inverses), represented using "FpGroupElement" 

# fp_grp: Finitely Presented Group with < X|R > as presentation. 

# H: subgroup of fp_grp. 

# NOTE: We start with H as being only a list of words in generators 

# of "fp_grp". Since `.subgroup` method has not been implemented. 

 

""" 

 

References 

========== 

 

[1] Holt, D., Eick, B., O'Brien, E. 

"Handbook of Computational Group Theory" 

 

[2] John J. Cannon; Lucien A. Dimino; George Havas; Jane M. Watson 

Mathematics of Computation, Vol. 27, No. 123. (Jul., 1973), pp. 463-490. 

"Implementation and Analysis of the Todd-Coxeter Algorithm" 

 

""" 

# default limit for the number of cosets allowed in a 

# coset enumeration. 

coset_table_max_limit = 4096000 

# maximun size of deduction stack above or equal to 

# which it is emptied 

max_stack_size = 500 

 

def __init__(self, fp_grp, subgroup): 

self.fp_group = fp_grp 

self.subgroup = subgroup 

# "p" is setup independent of Ω and n 

self.p = [0] 

self.A = list(chain.from_iterable((gen, gen**-1) \ 

for gen in self.fp_group.generators)) 

self.table = [[None]*len(self.A)] 

self.A_dict = {x: self.A.index(x) for x in self.A} 

self.A_dict_inv = {} 

for x, index in self.A_dict.items(): 

if index % 2 == 0: 

self.A_dict_inv[x] = self.A_dict[x] + 1 

else: 

self.A_dict_inv[x] = self.A_dict[x] - 1 

self.deduction_stack = [] 

 

@property 

def omega(self): 

"""Set of live cosets 

""" 

return [coset for coset in range(len(self.p)) if self.p[coset] == coset] 

 

def copy(self): 

self_copy = self.__class__(self.fp_group, self.subgroup) 

self_copy.table = [list(perm_rep) for perm_rep in self.table] 

self_copy.p = list(self.p) 

self_copy.deduction_stack = list(self.deduction_stack) 

return self_copy 

 

def __str__(self): 

return "Coset Table on %s with %s as subgroup generators" \ 

% (self.fp_group, self.subgroup) 

 

__repr__ = __str__ 

 

@property 

def n(self): 

"""The number 'n' represents the length of the sublist containing the 

live cosets. 

""" 

if not self.table: 

return 0 

return max(self.omega) + 1 

 

# Pg 152 [1] 

def is_complete(self): 

""" 

The coset table is called complete if it has no undefined entries 

on the live cosets; that is, α^x is defined for all α ∈ Ω and x ∈ A. 

""" 

return not any([None in self.table[coset] for coset in self.omega]) 

 

# Pg. 153 [1] 

def define(self, alpha, x): 

A = self.A 

if len(self.table) == CosetTable.coset_table_max_limit: 

# abort the further generation of cosets 

return 

self.table.append([None]*len(A)) 

# beta is the new coset generated 

beta = len(self.table) - 1 

self.p.append(beta) 

self.table[alpha][self.A_dict[x]] = beta 

self.table[beta][self.A_dict_inv[x]] = alpha 

 

def define_f(self, alpha, x): 

A = self.A 

if len(self.table) == CosetTable.coset_table_max_limit: 

# abort the further generation of cosets 

return 

self.table.append([None]*len(A)) 

# beta is the new coset generated 

beta = len(self.table) - 1 

self.p.append(beta) 

self.table[alpha][self.A_dict[x]] = beta 

self.table[beta][self.A_dict_inv[x]] = alpha 

# append to deduction stack 

self.deduction_stack.append((alpha, x)) 

 

def scan_f(self, alpha, word): 

# alpha is an integer representing a "coset" 

# since scanning can be in two cases 

# 1. for alpha=0 and w in Y (i.e generating set of H) 

# 2. alpha in omega (set of live cosets), w in R (relators) 

A_dict = self.A_dict 

A_dict_inv = self.A_dict_inv 

f = alpha 

i = 0 

r = len(word) 

b = alpha 

j = r - 1 

# list of union of generators and their inverses 

while i <= j and self.table[f][A_dict[word[i]]] is not None: 

f = self.table[f][A_dict[word[i]]] 

i += 1 

if i > j: 

if f != b: 

self.coincidence_f(f, b) 

return 

while j >= i and self.table[b][A_dict_inv[word[j]]] is not None: 

b = self.table[b][A_dict_inv[word[j]]] 

j -= 1 

if j < i: 

# we have an incorrect completed scan with coincidence f ~ b 

# run the "coincidence" routine 

self.coincidence_f(f, b) 

elif j == i: 

# deduction process 

self.table[f][A_dict[word[i]]] = b 

self.table[b][A_dict_inv[word[i]]] = f 

self.deduction_stack.append((f, word[i])) 

# otherwise scan is incomplete and yields no information 

 

# α, β coincide, i.e. α, β represent the pair of cosets where 

# coincidence occurs 

def coincidence_f(self, alpha, beta): 

""" 

""" 

A_dict = self.A_dict 

A_dict_inv = self.A_dict_inv 

p = self.p 

l = 0 

# behaves as a queue 

q = [] 

self.merge(alpha, beta, q) 

while len(q) > 0: 

gamma = q.pop(0) 

for x in A_dict: 

delta = self.table[gamma][A_dict[x]] 

if delta is not None: 

self.table[delta][A_dict_inv[x]] = None 

self.deduction_stack.append((delta, x**-1)) 

mu = self.rep(gamma) 

nu = self.rep(delta) 

if self.table[mu][A_dict[x]] is not None: 

self.merge(nu, self.table[mu][A_dict[x]], q) 

elif self.table[nu][A_dict_inv[x]] is not None: 

self.merge(mu, self.table[nu][A_dict_inv[x]], q) 

else: 

self.table[mu][A_dict[x]] = nu 

self.table[nu][A_dict_inv[x]] = mu 

 

def scan(self, alpha, word): 

# alpha is an integer representing a "coset" 

# since scanning can be in two cases 

# 1. for alpha=0 and w in Y (i.e generating set of H) 

# 2. alpha in omega (set of live cosets), w in R (relators) 

A_dict = self.A_dict 

A_dict_inv = self.A_dict_inv 

f = alpha 

i = 0 

r = len(word) 

b = alpha 

j = r - 1 

while i <= j and self.table[f][A_dict[word[i]]] is not None: 

f = self.table[f][A_dict[word[i]]] 

i += 1 

if i > j: 

if f != b: 

self.coincidence(f, b) 

return 

while j >= i and self.table[b][A_dict_inv[word[j]]] is not None: 

b = self.table[b][A_dict_inv[word[j]]] 

j -= 1 

if j < i: 

# we have an incorrect completed scan with coincidence f ~ b 

# run the "coincidence" routine 

self.coincidence(f, b) 

elif j == i: 

# deduction process 

self.table[f][A_dict[word[i]]] = b 

self.table[b][A_dict_inv[word[i]]] = f 

# otherwise scan is incomplete and yields no information 

 

# used in the low-index subgroups algorithm 

def scan_check(self, alpha, word): 

""" 

Another version of "scan" routine, it checks whether α scans correctly 

under w, it is a straightforward modification of "scan". "scan_check" 

return false (rather than calling "coincidence") if the scan completes 

incorrectly; otherwise it returns true. 

 

""" 

# alpha is an integer representing a "coset" 

# since scanning can be in two cases 

# 1. for alpha=0 and w in Y (i.e generating set of H) 

# 2. alpha in omega (set of live cosets), w in R (relators) 

A_dict = self.A_dict 

A_dict_inv = self.A_dict_inv 

f = alpha 

i = 0 

r = len(word) 

b = alpha 

j = r - 1 

while i <= j and self.table[f][A_dict[word[i]]] is not None: 

f = self.table[f][A_dict[word[i]]] 

i += 1 

if i > j: 

return f == b 

while j >= i and self.table[b][A_dict_inv[word[j]]] is not None: 

b = self.table[b][A_dict_inv[word[j]]] 

j -= 1 

if j < i: 

# we have an incorrect completed scan with coincidence f ~ b 

# return False, instead of calling coincidence routine 

return False 

elif j == i: 

# deduction process 

self.table[f][A_dict[word[i]]] = b 

self.table[b][A_dict_inv[word[i]]] = f 

return True 

 

def merge(self, k, lamda, q): 

p = self.p 

phi = self.rep(k) 

psi = self.rep(lamda) 

if phi != psi: 

mu = min(phi, psi) 

v = max(phi, psi) 

p[v] = mu 

q.append(v) 

 

def rep(self, k): 

p = self.p 

lamda = k 

rho = p[lamda] 

while rho != lamda: 

lamda = rho 

rho = p[lamda] 

mu = k 

rho = p[mu] 

while rho != lamda: 

p[mu] = lamda 

mu = rho 

rho = p[mu] 

return lamda 

 

# α, β coincide, i.e. α, β represent the pair of cosets where 

# coincidence occurs 

def coincidence(self, alpha, beta): 

""" 

""" 

A_dict = self.A_dict 

A_dict_inv = self.A_dict_inv 

p = self.p 

l = 0 

# behaves as a queue 

q = [] 

self.merge(alpha, beta, q) 

while len(q) > 0: 

gamma = q.pop(0) 

for x in A_dict: 

delta = self.table[gamma][A_dict[x]] 

if delta is not None: 

self.table[delta][A_dict_inv[x]] = None 

mu = self.rep(gamma) 

nu = self.rep(delta) 

if self.table[mu][A_dict[x]] is not None: 

self.merge(nu, self.table[mu][A_dict[x]], q) 

elif self.table[nu][A_dict_inv[x]] is not None: 

self.merge(mu, self.table[nu][A_dict_inv[x]], q) 

else: 

self.table[mu][A_dict[x]] = nu 

self.table[nu][A_dict_inv[x]] = mu 

 

# method used in the HLT strategy 

def scan_and_fill(self, alpha, word): 

A_dict = self.A_dict 

A_dict_inv = self.A_dict_inv 

r = len(word) 

f = alpha 

i = 0 

b = alpha 

j = r - 1 

# loop until it has filled the α row in the table. 

while True: 

# do the forward scanning 

while i <= j and self.table[f][A_dict[word[i]]] is not None: 

f = self.table[f][A_dict[word[i]]] 

i += 1 

if i > j: 

if f != b: 

self.coincidence(f, b) 

return 

# forward scan was incomplete, scan backwards 

while j >= i and self.table[b][A_dict_inv[word[j]]] is not None: 

b = self.table[b][A_dict_inv[word[j]]] 

j -= 1 

if j < i: 

self.coincidence(f, b) 

elif j == i: 

self.table[f][A_dict[word[i]]] = b 

self.table[b][A_dict_inv[word[i]]] = f 

else: 

self.define(f, word[i]) 

 

def scan_and_fill_f(self, alpha, word): 

A_dict = self.A_dict 

A_dict_inv = self.A_dict_inv 

r = len(word) 

f = alpha 

i = 0 

b = alpha 

j = r - 1 

# loop until it has filled the α row in the table. 

while True: 

# do the forward scanning 

while i <= j and self.table[f][A_dict[word[i]]] is not None: 

f = self.table[f][A_dict[word[i]]] 

i += 1 

if i > j: 

if f != b: 

self.coincidence_f(f, b) 

return 

# forward scan was incomplete, scan backwards 

while j >= i and self.table[b][A_dict_inv[word[j]]] is not None: 

b = self.table[b][A_dict_inv[word[j]]] 

j -= 1 

if j < i: 

self.coincidence_f(f, b) 

elif j == i: 

self.table[f][A_dict[word[i]]] = b 

self.table[b][A_dict_inv[word[i]]] = f 

self.deduction_stack.append((f, word[i])) 

else: 

self.define_f(f, word[i]) 

 

def look_ahead(self): 

R = self.fp_group.relators() 

p = self.p 

# complete scan all relators under all cosets(obviously live) 

# without making new definitions 

for beta in self.omega: 

for w in R: 

self.scan(beta, w) 

if p[beta] < beta: 

break 

 

# Pg. 166 

def process_deductions(self, R_c_x, R_c_x_inv): 

p = self.p 

while len(self.deduction_stack) > 0: 

if len(self.deduction_stack) >= CosetTable.max_stack_size: 

self.look_ahead() 

del self.deduction_stack[:] 

else: 

alpha, x = self.deduction_stack.pop() 

if p[alpha] == alpha: 

for w in R_c_x: 

self.scan_f(alpha, w) 

if p[alpha] < alpha: 

break 

beta = self.table[alpha][self.A_dict[x]] 

if beta is not None and p[beta] == beta: 

for w in R_c_x_inv: 

self.scan_f(beta, w) 

if p[beta] < beta: 

break 

 

def process_deductions_check(self, R_c_x, R_c_x_inv): 

""" 

A variation of "process_deductions", this calls "scan_check" wherever 

"process_deductions" calls "scan". 

""" 

p = self.p 

while len(self.deduction_stack) > 0: 

alpha, x = self.deduction_stack.pop() 

for w in R_c_x: 

if not self.scan_check(alpha, w): 

return False 

beta = self.table[alpha][self.A_dict[x]] 

if beta is not None: 

for w in R_c_x_inv: 

if not self.scan_check(beta, w): 

return False 

return True 

 

def switch(self, beta, gamma): 

""" 

Switch the elements β, γ of Ω in C 

""" 

A = self.A 

A_dict = self.A_dict 

A_dict_inv = self.A_dict_inv 

X = self.fp_group.generators 

table = self.table 

for x in A: 

z = table[gamma][A_dict[x]] 

table[gamma][A_dict[x]] = table[beta][A_dict[x]] 

table[beta][A_dict[x]] = z 

for alpha in range(len(self.p)): 

if self.p[alpha] == alpha: 

if table[alpha][A_dict[x]] == beta: 

table[alpha][A_dict[x]] = gamma 

elif table[alpha][A_dict[x]] == gamma: 

table[alpha][A_dict[x]] = beta 

 

def standardize(self): 

"""A coset table is standardized if when running through the cosets 

and within each coset through the generator images (ignoring generator 

inverses), the cosets appear in order of the integers 0, 1, 2, ... n. 

"Standardize" reorders the elements of \Omega such that, if we scan 

the coset table first by elements of \Omega and then by elements of A, 

then the cosets occur in ascending order. `standardize()` is used at 

the end of an enumeration to permute the cosets so that they occur in 

some sort of standard order. 

 

>>> from sympy.combinatorics.free_group import free_group 

>>> from sympy.combinatorics.fp_groups import FpGroup, coset_enumeration_r 

>>> F, x, y = free_group("x, y") 

 

# Example 5.3 from [1] 

>>> f = FpGroup(F, [x**2*y**2, x**3*y**5]) 

>>> C = coset_enumeration_r(f, []) 

>>> C.compress() 

>>> C.table 

[[1, 3, 1, 3], [2, 0, 2, 0], [3, 1, 3, 1], [0, 2, 0, 2]] 

>>> C.standardize() 

>>> C.table 

[[1, 2, 1, 2], [3, 0, 3, 0], [0, 3, 0, 3], [2, 1, 2, 1]] 

 

""" 

A = self.A 

A_dict = self.A_dict 

A_dict_inv = self.A_dict_inv 

gamma = 1 

for alpha, x in product(range(self.n), A): 

beta = self.table[alpha][A_dict[x]] 

if beta >= gamma: 

if beta > gamma: 

self.switch(gamma, beta) 

gamma += 1 

if gamma == self.n: 

return 

 

# Compression of a Coset Table 

# Pg. 167 5.2.3 

def compress(self): 

"""Removes the non-live cosets from the coset table 

""" 

gamma = -1 

A = self.A 

A_dict = self.A_dict 

A_dict_inv = self.A_dict_inv 

chi = tuple([i for i in range(len(self.p)) if self.p[i] != i]) 

for alpha in self.omega: 

gamma += 1 

if gamma != alpha: 

# replace α by γ in coset table 

for x in A: 

beta = self.table[alpha][A_dict[x]] 

self.table[gamma][A_dict[x]] = beta 

self.table[beta][A_dict_inv[x]] == gamma 

# all the cosets in the table are live cosets 

self.p = list(range(gamma + 1)) 

# delete the useless coloumns 

del self.table[len(self.p):] 

# re-define values 

for row in self.table: 

for j in range(len(self.A)): 

row[j] -= bisect_left(chi, row[j]) 

 

def conjugates(self, R): 

R_c = list(chain.from_iterable((rel.cyclic_conjugates(), \ 

(rel**-1).cyclic_conjugates()) for rel in R)) 

R_set = set() 

for conjugate in R_c: 

R_set = R_set.union(conjugate) 

R_c_list = [] 

for x in self.A: 

r = set([word for word in R_set if word[0] == x]) 

R_c_list.append(r) 

R_set.difference_update(r) 

return R_c_list 

 

 

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

# COSET ENUMERATION # 

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

 

# relator-based method 

def coset_enumeration_r(fp_grp, Y): 

""" 

>>> from sympy.combinatorics.free_group import free_group 

>>> from sympy.combinatorics.fp_groups import FpGroup, coset_enumeration_r 

>>> F, x, y = free_group("x, y") 

 

# Example 5.1 from [1] 

>>> f = FpGroup(F, [x**3, y**3, x**-1*y**-1*x*y]) 

>>> C = coset_enumeration_r(f, [x]) 

>>> for i in range(len(C.p)): 

... if C.p[i] == i: 

... print(C.table[i]) 

[0, 0, 1, 2] 

[1, 1, 2, 0] 

[2, 2, 0, 1] 

>>> C.p 

[0, 1, 2, 1, 1] 

 

# Example from exercises Q2 [1] 

>>> f = FpGroup(F, [x**2*y**2, y**-1*x*y*x**-3]) 

>>> C = coset_enumeration_r(f, []) 

>>> C.compress(); C.standardize() 

>>> C.table 

[[1, 2, 3, 4], 

[5, 0, 6, 7], 

[0, 5, 7, 6], 

[7, 6, 5, 0], 

[6, 7, 0, 5], 

[2, 1, 4, 3], 

[3, 4, 2, 1], 

[4, 3, 1, 2]] 

 

# Example 5.2 

>>> f = FpGroup(F, [x**2, y**3, (x*y)**3]) 

>>> Y = [x*y] 

>>> C = coset_enumeration_r(f, Y) 

>>> for i in range(len(C.p)): 

... if C.p[i] == i: 

... print(C.table[i]) 

[1, 1, 2, 1] 

[0, 0, 0, 2] 

[3, 3, 1, 0] 

[2, 2, 3, 3] 

 

# Example 5.3 

>>> f = FpGroup(F, [x**2*y**2, x**3*y**5]) 

>>> Y = [] 

>>> C = coset_enumeration_r(f, Y) 

>>> for i in range(len(C.p)): 

... if C.p[i] == i: 

... print(C.table[i]) 

[1, 3, 1, 3] 

[2, 0, 2, 0] 

[3, 1, 3, 1] 

[0, 2, 0, 2] 

 

# Example 5.4 

>>> F, a, b, c, d, e = free_group("a, b, c, d, e") 

>>> f = FpGroup(F, [a*b*c**-1, b*c*d**-1, c*d*e**-1, d*e*a**-1, e*a*b**-1]) 

>>> Y = [a] 

>>> C = coset_enumeration_r(f, Y) 

>>> for i in range(len(C.p)): 

... if C.p[i] == i: 

... print(C.table[i]) 

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 

 

# example of "compress" method 

>>> C.compress() 

>>> C.table 

[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] 

 

# Exercises Pg. 161, Q2. 

>>> F, x, y = free_group("x, y") 

>>> f = FpGroup(F, [x**2*y**2, y**-1*x*y*x**-3]) 

>>> Y = [] 

>>> C = coset_enumeration_r(f, Y) 

>>> C.compress() 

>>> C.standardize() 

>>> C.table 

[[1, 2, 3, 4], 

[5, 0, 6, 7], 

[0, 5, 7, 6], 

[7, 6, 5, 0], 

[6, 7, 0, 5], 

[2, 1, 4, 3], 

[3, 4, 2, 1], 

[4, 3, 1, 2]] 

 

# John J. Cannon; Lucien A. Dimino; George Havas; Jane M. Watson 

# Mathematics of Computation, Vol. 27, No. 123. (Jul., 1973), pp. 463-490 

# from 1973chwd.pdf 

# Table 1. Ex. 1 

>>> F, r, s, t = free_group("r, s, t") 

>>> E1 = FpGroup(F, [t**-1*r*t*r**-2, r**-1*s*r*s**-2, s**-1*t*s*t**-2]) 

>>> C = coset_enumeration_r(E1, [r]) 

>>> for i in range(len(C.p)): 

... if C.p[i] == i: 

... print(C.table[i]) 

[0, 0, 0, 0, 0, 0] 

 

Ex. 2 

>>> F, a, b = free_group("a, b") 

>>> Cox = FpGroup(F, [a**6, b**6, (a*b)**2, (a**2*b**2)**2, (a**3*b**3)**5]) 

>>> C = coset_enumeration_r(Cox, [a]) 

>>> index = 0 

>>> for i in range(len(C.p)): 

... if C.p[i] == i: 

... index += 1 

>>> index 

500 

 

# Ex. 3 

>>> F, a, b = free_group("a, b") 

>>> B_2_4 = FpGroup(F, [a**4, b**4, (a*b)**4, (a**-1*b)**4, (a**2*b)**4, \ 

(a*b**2)**4, (a**2*b**2)**4, (a**-1*b*a*b)**4, (a*b**-1*a*b)**4]) 

>>> C = coset_enumeration_r(B_2_4, [a]) 

>>> index = 0 

>>> for i in range(len(C.p)): 

... if C.p[i] == i: 

... index += 1 

>>> index 

1024 

 

""" 

# 1. Initialize a coset table C for < X|R > 

C = CosetTable(fp_grp, Y) 

R = fp_grp.relators() 

A_dict = C.A_dict 

A_dict_inv = C.A_dict_inv 

p = C.p 

for w in Y: 

C.scan_and_fill(0, w) 

alpha = 0 

while alpha < C.n: 

if p[alpha] == alpha: 

for w in R: 

C.scan_and_fill(alpha, w) 

if p[alpha] < alpha: 

break 

if p[alpha] >= alpha: 

for x in A_dict: 

if C.table[alpha][A_dict[x]] is None: 

C.define(alpha, x) 

alpha += 1 

return C 

 

 

# Pg. 166 

# coset-table based method 

def coset_enumeration_c(fp_grp, Y): 

""" 

>>> from sympy.combinatorics.free_group import free_group 

>>> from sympy.combinatorics.fp_groups import FpGroup, coset_enumeration_c 

>>> F, x, y = free_group("x, y") 

>>> f = FpGroup(F, [x**3, y**3, x**-1*y**-1*x*y]) 

>>> C = coset_enumeration_c(f, [x]) 

>>> C.table 

[[0, 0, 1, 2], [1, 1, 2, 0], [2, 2, 0, 1]] 

 

""" 

# Initialize a coset table C for < X|R > 

C = CosetTable(fp_grp, Y) 

X = fp_grp.generators 

R = fp_grp.relators() 

A = C.A 

# replace all the elements by cyclic reductions 

R_cyc_red = [rel.identity_cyclic_reduction() for rel in R] 

R_c = list(chain.from_iterable((rel.cyclic_conjugates(), (rel**-1).cyclic_conjugates()) \ 

for rel in R_cyc_red)) 

R_set = set() 

for conjugate in R_c: 

R_set = R_set.union(conjugate) 

# a list of subsets of R_c whose words start with "x". 

R_c_list = [] 

for x in C.A: 

r = set([word for word in R_set if word[0] == x]) 

R_c_list.append(r) 

R_set.difference_update(r) 

for w in Y: 

C.scan_and_fill_f(0, w) 

for x in A: 

C.process_deductions(R_c_list[C.A_dict[x]], R_c_list[C.A_dict_inv[x]]) 

i = 0 

while i < len(C.omega): 

alpha = C.omega[i] 

i += 1 

for x in C.A: 

if C.table[alpha][C.A_dict[x]] is None: 

C.define_f(alpha, x) 

C.process_deductions(R_c_list[C.A_dict[x]], R_c_list[C.A_dict_inv[x]]) 

return C 

 

 

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

# LOW INDEX SUBGROUPS # 

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

 

def low_index_subgroups(G, N, Y=[]): 

""" 

Implements the Low Index Subgroups algorithm, i.e find all subgroups of 

"G" upto a given index "N". This implements the method described in 

[Sim94]. This procedure involves a backtrack search over incomplete Coset 

Tables, rather than over forced coincidences. 

 

G: An FpGroup < X|R > 

N: positive integer, representing the maximun index value for subgroups 

Y: (an optional argument) specifying a list of subgroup generators, such 

that each of the resulting subgroup contains the subgroup generated by Y. 

 

References 

========== 

 

[1] Holt, D., Eick, B., O'Brien, E. 

"Handbook of Computational Group Theory" 

Section 5.4 

 

[2] Marston Conder and Peter Dobcsanyi 

"Applications and Adaptions of the Low Index Subgroups Procedure" 

 

Examples 

======== 

 

>>> from sympy.combinatorics.free_group import free_group 

>>> from sympy.combinatorics.fp_groups import FpGroup, low_index_subgroups 

>>> F, x, y = free_group("x, y") 

>>> f = FpGroup(F, [x**2, y**3, (x*y)**4]) 

>>> L = low_index_subgroups(f, 4) 

>>> for coset_table in L: 

... print(coset_table.table) 

[[0, 0, 0, 0]] 

[[0, 0, 1, 2], [1, 1, 2, 0], [3, 3, 0, 1], [2, 2, 3, 3]] 

[[0, 0, 1, 2], [2, 2, 2, 0], [1, 1, 0, 1]] 

[[1, 1, 0, 0], [0, 0, 1, 1]] 

 

""" 

C = CosetTable(G, []) 

R = G.relators() 

# length chosen for the length of the short relators 

len_short_rel = 5 

# elements of R2 only checked at the last step for complete 

# coset tables 

R2 = set([rel for rel in R if len(rel) > len_short_rel]) 

# elements of R1 are used in inner parts of the process to prune 

# branches of the search tree, 

R1 = set([rel.identity_cyclic_reduction() for rel in set(R) - R2]) 

R1_c_list = C.conjugates(R1) 

S = [] 

descendant_subgroups(S, C, R1_c_list, C.A[0], R2, N, Y) 

return S 

 

 

def descendant_subgroups(S, C, R1_c_list, x, R2, N, Y): 

A_dict = C.A_dict 

A_dict_inv = C.A_dict_inv 

if C.is_complete(): 

# if C is complete then it only needs to test 

# whether the relators in R2 are satisfied 

for w, alpha in product(R2, C.omega): 

if not C.scan_check(alpha, w): 

return 

# relators in R2 are satisfied, append the table to list 

S.append(C) 

else: 

# find the first undefined entry in Coset Table 

for alpha, x in product(range(len(C.table)), C.A): 

if C.table[alpha][A_dict[x]] is None: 

# this is "x" in pseudo-code (using "y" makes it clear) 

undefined_coset, undefined_gen = alpha, x 

break 

# for filling up the undefine entry we try all possible values 

# of β ∈ Ω or β = n where β^(undefined_gen^-1) is undefined 

reach = C.omega + [C.n] 

for beta in reach: 

if beta < N: 

if beta == C.n or C.table[beta][A_dict_inv[undefined_gen]] is None: 

try_descendant(S, C, R1_c_list, R2, N, undefined_coset, \ 

undefined_gen, beta, Y) 

 

 

def try_descendant(S, C, R1_c_list, R2, N, alpha, x, beta, Y): 

""" 

It solves the problem of trying out each individual possibility for α^x. 

""" 

D = C.copy() 

A_dict = D.A_dict 

if beta == D.n and beta < N: 

D.table.append([None]*len(D.A)) 

D.p.append(beta) 

D.table[alpha][D.A_dict[x]] = beta 

D.table[beta][D.A_dict_inv[x]] = alpha 

D.deduction_stack.append((alpha, x)) 

if not D.process_deductions_check(R1_c_list[D.A_dict[x]], \ 

R1_c_list[D.A_dict_inv[x]]): 

return 

for w in Y: 

if not D.scan_check(0, w): 

return 

if first_in_class(D, Y): 

descendant_subgroups(S, D, R1_c_list, x, R2, N, Y) 

 

 

def first_in_class(C, Y=[]): 

""" 

Checks whether the subgroup H=G1 corresponding to the Coset Table could 

possibly be the canonical representative of its conjugacy class. 

 

Parameters 

========== 

 

C: CosetTable 

 

Returns 

======= 

 

bool: True/False 

 

If this returns False, then no descendant of C can have that property, and 

so we can abandon C. If it returns True, then we need to process further 

the node of the search tree corresponding to C, and so we call 

``descendant_subgroups`` recursively on C. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.free_group import free_group 

>>> from sympy.combinatorics.fp_groups import FpGroup, CosetTable, first_in_class 

>>> F, x, y = free_group("x, y") 

>>> f = FpGroup(F, [x**2, y**3, (x*y)**4]) 

>>> C = CosetTable(f, []) 

>>> C.table = [[0, 0, None, None]] 

>>> first_in_class(C) 

True 

>>> C.table = [[1, 1, 1, None], [0, 0, None, 1]]; C.p = [0, 1] 

>>> first_in_class(C) 

True 

>>> C.table = [[1, 1, 2, 1], [0, 0, 0, None], [None, None, None, 0]] 

>>> C.p = [0, 1, 2] 

>>> first_in_class(C) 

False 

>>> C.table = [[1, 1, 1, 2], [0, 0, 2, 0], [2, None, 0, 1]] 

>>> first_in_class(C) 

False 

 

# TODO:: Sims points out in [Sim94] that performance can be improved by 

# remembering some of the information computed by ``first_in_class``. If 

# the ``continue α`` statement is executed at line 14, then the same thing 

# will happen for that value of α in any descendant of the table C, and so 

# the values the values of α for which this occurs could profitably be 

# stored and passed through to the descendants of C. Of course this would 

# make the code more complicated. 

 

# The code below is taken directly from the function on page 208 of [Sim94] 

# ν[α] 

 

""" 

n = C.n 

# lamda is the largest numbered point in Ω_c_α which is currently defined 

lamda = -1 

# for α ∈ Ω_c, ν[α] is the point in Ω_c_α corresponding to α 

nu = [None]*n 

# for α ∈ Ω_c_α, μ[α] is the point in Ω_c corresponding to α 

mu = [None]*n 

# mutually ν and μ are the mutually-inverse equivalence maps between 

# Ω_c_α and Ω_c 

next_alpha = False 

# For each 0≠α ∈ [0 .. nc-1], we start by constructing the equivalent 

# standardized coset table C_α corresponding to H_α 

for alpha in range(1, n): 

# reset ν to "None" after previous value of α 

for beta in range(lamda+1): 

nu[mu[beta]] = None 

# we only want to reject our current table in favour of a preceding 

# table in the ordering in which 1 is replaced by α, if the subgroup 

# G_α corresponding to this preceding table definitely contains the 

# given subgroup 

for w in Y: 

# TODO: this should support input of a list of general words 

# not just the words which are in "A" (i.e gen and gen^-1) 

if C.table[alpha][C.A_dict[w]] != alpha: 

# continue with α 

next_alpha = True 

break 

if next_alpha: 

next_alpha = False 

continue 

# try α as the new point 0 in Ω_C_α 

mu[0] = alpha 

nu[alpha] = 0 

# compare corresponding entries in C and C_α 

lamda = 0 

for beta in range(n): 

for x in C.A: 

gamma = C.table[beta][C.A_dict[x]] 

delta = C.table[mu[beta]][C.A_dict[x]] 

# if either of the entries is undefined, 

# we move with next α 

if gamma is None or delta is None: 

# continue with α 

next_alpha = True 

break 

if nu[delta] is None: 

# delta becomes the next point in Ω_C_α 

lamda += 1 

nu[delta] = lamda 

mu[lamda] = delta 

if nu[delta] < gamma: 

return False 

if nu[delta] > gamma: 

# continue with α 

next_alpha = True 

break 

if next_alpha: 

next_alpha = False 

break 

return True 

 

 

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

# SUBGROUP PRESENTATIONS # 

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

 

# Pg 175 [1] 

def define_schreier_generators(C): 

y = [] 

gamma = 1 

f = C.fp_group 

X = f.generators 

C.P = [[None]*len(C.A) for i in range(C.n)] 

for alpha, x in product(C.omega, C.A): 

beta = C.table[alpha][C.A_dict[x]] 

if beta == gamma: 

C.P[alpha][C.A_dict[x]] = "<identity>" 

C.P[beta][C.A_dict_inv[x]] = "<identity>" 

gamma += 1 

elif x in X and C.P[alpha][C.A_dict[x]] is None: 

y_alpha_x = '%s_%s' % (x, alpha) 

y.append(y_alpha_x) 

C.P[alpha][C.A_dict[x]] = y_alpha_x 

grp_gens = list(free_group(', '.join(y))) 

C._schreier_free_group = grp_gens.pop(0) 

C._schreier_generators = grp_gens 

# replace all elements of P by, free group elements 

for i, j in product(range(len(C.P)), range(len(C.A))): 

# if equals "<identity>", replace by identity element 

if C.P[i][j] == "<identity>": 

C.P[i][j] = C._schreier_free_group.identity 

elif isinstance(C.P[i][j], str): 

r = C._schreier_generators[y.index(C.P[i][j])] 

C.P[i][j] = r 

beta = C.table[i][j] 

C.P[beta][j + 1] = r**-1 

 

 

def reidemeister_relators(C): 

R = C.fp_group.relators() 

rels = [rewrite(C, coset, word) for word in R for coset in range(C.n)] 

identity = C._schreier_free_group.identity 

order_1_gens = set([i for i in rels if len(i) == 1]) 

 

# remove all the order 1 generators from relators 

rels = list(filter(lambda rel: rel not in order_1_gens, rels)) 

 

# replace order 1 generators by identity element in reidemeister relators 

for i in range(len(rels)): 

w = rels[i] 

for gen in order_1_gens: 

w = w.eliminate_word(gen, identity) 

rels[i] = w 

 

C._schreier_generators = [i for i in C._schreier_generators if i not in order_1_gens] 

 

# Tietze transformation 1 i.e TT_1 

# remove cyclic conjugate elements from relators 

i = 0 

while i < len(rels): 

w = rels[i] 

j = i + 1 

while j < len(rels): 

if w.is_cyclic_conjugate(rels[j]): 

del rels[j] 

else: 

j += 1 

i += 1 

 

C._reidemeister_relators = rels 

 

 

def rewrite(C, alpha, w): 

""" 

Parameters 

---------- 

 

C: CosetTable 

α: A live coset 

w: A word in `A*` 

 

Returns 

------- 

 

ρ(τ(α), w) 

 

Examples 

======== 

 

>>> from sympy.combinatorics.fp_groups import FpGroup, CosetTable, define_schreier_generators, rewrite 

>>> from sympy.combinatorics.free_group import free_group 

>>> F, x, y = free_group("x ,y") 

>>> f = FpGroup(F, [x**2, y**3, (x*y)**6]) 

>>> C = CosetTable(f, []) 

>>> C.table = [[1, 1, 2, 3], [0, 0, 4, 5], [4, 4, 3, 0], [5, 5, 0, 2], [2, 2, 5, 1], [3, 3, 1, 4]] 

>>> C.p = [0, 1, 2, 3, 4, 5] 

>>> define_schreier_generators(C) 

>>> rewrite(C, 0, (x*y)**6) 

x_4*y_2*x_3*x_1*x_2*y_4*x_5 

 

""" 

v = C._schreier_free_group.identity 

for i in range(len(w)): 

x_i = w[i] 

v = v*C.P[alpha][C.A_dict[x_i]] 

alpha = C.table[alpha][C.A_dict[x_i]] 

return v 

 

 

# Pg 350, section 2.5.1 from [2] 

def elimination_technique_1(C): 

rels = C._reidemeister_relators 

# the shorter relators are examined first so that generators selected for 

# elimination will have shorter strings as equivalent 

rels.sort(reverse=True) 

gens = C._schreier_generators 

redundant_gens = {} 

contained_gens = [] 

# examine each relator in relator list for any generator occuring exactly 

# once 

next_i = False 

for i in range(len(rels) -1, -1, -1): 

rel = rels[i] 

# don't look for a new generator occuring once in relator which 

# has already found to posses a 

for gen in redundant_gens: 

gen_sym = gen.array_form[0][0] 

if any([gen_sym == r[0] for r in rel.array_form]): 

next_i = True 

break 

if next_i: 

next_i = False 

continue 

for j in range(len(gens) - 1, -1, -1): 

gen = gens[j] 

if rel.generator_count(gen) == 1 and gen not in contained_gens: 

k = rel.exponent_sum(gen) 

gen_index = rel.index(gen**k) 

bk = rel.subword(gen_index + 1, len(rel)) 

fw = rel.subword(0, gen_index) 

chi = (bk*fw).identity_cyclic_reduction() 

redundant_gens[gen] = chi**(-1*k) 

contained_gens.extend(chi.contains_generators()) 

del rels[i]; del gens[j] 

break 

# eliminate the redundant generator from remaing relators 

for i, gen in product(range(len(rels)), redundant_gens): 

rels[i] = (rels[i].eliminate_word(gen, redundant_gens[gen])).identity_cyclic_reduction() 

rels.sort() 

try: 

rels.remove(C._schreier_free_group.identity) 

except ValueError: 

pass 

C._reidemeister_relators = rels 

C._schreier_generators = gens 

 

# Pg 350, section 2.5.2 from [2] 

def elimination_technique_2(C): 

""" 

This technique eliminates one generator at a time. Heuristically this 

seems superior in that we may select for elimination the generator with 

shortest equivalent string at each stage. 

 

>>> from sympy.combinatorics.free_group import free_group 

>>> from sympy.combinatorics.fp_groups import FpGroup, coset_enumeration_r, \ 

reidemeister_relators, define_schreier_generators, elimination_technique_2 

>>> F, x, y = free_group("x, y") 

>>> f = FpGroup(F, [x**3, y**5, (x*y)**2]); H = [x*y, x**-1*y**-1*x*y*x] 

>>> C = coset_enumeration_r(f, H) 

>>> C.compress(); C.standardize() 

>>> define_schreier_generators(C) 

>>> reidemeister_relators(C) 

>>> elimination_technique_2(C) 

([y_1, y_2], [y_2**-3, y_2*y_1*y_2*y_1*y_2*y_1, y_1**2]) 

 

""" 

rels = C._reidemeister_relators 

rels.sort(reverse=True) 

gens = C._schreier_generators 

for i in range(len(gens) - 1, -1, -1): 

rel = rels[i] 

for j in range(len(gens) - 1, -1, -1): 

gen = gens[j] 

if rel.generator_count(gen) == 1: 

k = rel.exponent_sum(gen) 

gen_index = rel.index(gen**k) 

bk = rel.subword(gen_index + 1, len(rel)) 

fw = rel.subword(0, gen_index) 

rep_by = (bk*fw)**(-1*k) 

del rels[i]; del gens[j] 

for l in range(len(rels)): 

rels[l] = rels[l].eliminate_word(gen, rep_by) 

break 

C._reidemeister_relators = rels 

C._schreier_generators = gens 

return C._schreier_generators, C._reidemeister_relators 

 

def simplify_presentation(C): 

""" 

Relies upon `_simplification_technique_1` for its functioning. 

""" 

rels = C._reidemeister_relators 

rels_arr = _simplification_technique_1(rels) 

group = C._schreier_free_group 

 

# don't add "identity" element in relator list 

C._reidemeister_relators = [group.dtype(tuple(r)).identity_cyclic_reduction() for r in rels_arr if r] 

 

def _simplification_technique_1(rels): 

""" 

All relators are checked to see if they are of the form `gen^n`. If any 

such relators are found then all other relators are processed for strings 

in the `gen` known order. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.free_group import free_group 

>>> from sympy.combinatorics.fp_groups import _simplification_technique_1 

>>> F, x, y = free_group("x, y") 

>>> w1 = [x**2*y**4, x**3] 

>>> _simplification_technique_1(w1) 

[[(x, 3)], [(x, -1), (y, 4)]] 

 

>>> w2 = [x**2*y**-4*x**5, x**3, x**2*y**8, y**5] 

>>> _simplification_technique_1(w2) 

[[(x, 3)], [(y, 5)], [(x, -1), (y, -2)], [(x, -1), (y, 1), (x, -1)]] 

 

>>> w3 = [x**6*y**4, x**4] 

>>> _simplification_technique_1(w3) 

[[(x, 4)], [(x, 2), (y, 4)]] 

 

""" 

rels = list(set(rels)) 

rels.sort() 

l_rels = len(rels) 

 

# all syllables with single syllable 

one_syllable_rels = set() 

# since "nw" has a max size = l_rels, only identity element 

# removal can possibly happen 

nw = [None]*l_rels 

for i in range(l_rels): 

w = rels[i].identity_cyclic_reduction() 

if w.number_syllables() == 1: 

 

# replace one syllable relator with the corresponding inverse 

# element, for ex. x**-4 -> x**4 in relator list 

if w.array_form[0][1] < 0: 

rels[i] = w**-1 

one_syllable_rels.add(rels[i]) 

 

# since modifies the array rep., so should be 

# added a list 

nw[i] = list(rels[i].array_form) 

 

# bound the exponent of relators, making use of the single 

# syllable relators 

for i in range(l_rels): 

k = nw[i] 

rels_i = rels[i] 

for gen in one_syllable_rels: 

n = gen.array_form[0][1] 

gen_arr0 = gen.array_form[0][0] 

j = len(k) - 1 

while j >= 0: 

if gen_arr0 == k[j][0] and gen is not rels_i: 

t = Mod(k[j][1], n) 

 

# multiple of one syllable relator 

if t == 0: 

del k[j] 

zero_mul_simp(k, j - 1) 

j = len(k) 

 

# power should be bounded by (-n/2, n/2] 

elif t <= n/2: 

k[j] = k[j][0], Mod(k[j][1], n) 

elif t > n/2: 

k[j] = k[j][0], Mod(k[j][1], n) - n 

j -= 1 

 

return nw 

 

def reidemeister_presentation(fp_grp, H, elm_rounds=2, simp_rounds=2): 

""" 

fp_group: A finitely presented group, an instance of FpGroup 

H: A subgroup whose presentation is to be found, given as a list 

of words in generators of `fp_grp` 

 

Examples 

======== 

 

>>> from sympy.combinatorics.free_group import free_group 

>>> from sympy.combinatorics.fp_groups import FpGroup, reidemeister_presentation 

>>> F, x, y = free_group("x, y") 

 

Example 5.6 Pg. 177 from [1] 

>>> f = FpGroup(F, [x**3, y**5, (x*y)**2]) 

>>> H = [x*y, x**-1*y**-1*x*y*x] 

>>> reidemeister_presentation(f, H) 

((y_1, y_2), (y_1**2, y_2**3, y_2*y_1*y_2*y_1*y_2*y_1)) 

 

Example 5.8 Pg. 183 from [1] 

>>> f = FpGroup(F, [x**3, y**3, (x*y)**3]) 

>>> H = [x*y, x*y**-1] 

>>> reidemeister_presentation(f, H) 

((x_0, y_0), (x_0**3, y_0**3, x_0*y_0*x_0*y_0*x_0*y_0)) 

 

Exercises Q2. Pg 187 from [1] 

>>> f = FpGroup(F, [x**2*y**2, y**-1*x*y*x**-3]) 

>>> H = [x] 

>>> reidemeister_presentation(f, H) 

((x_0,), (x_0**4,)) 

 

Example 5.9 Pg. 183 from [1] 

>>> f = FpGroup(F, [x**3*y**-3, (x*y)**3, (x*y**-1)**2]) 

>>> H = [x] 

>>> reidemeister_presentation(f, H) 

((x_0,), (x_0**6,)) 

 

""" 

C = coset_enumeration_r(fp_grp, H) 

C.compress(); C.standardize() 

define_schreier_generators(C) 

reidemeister_relators(C) 

for i in range(20): 

elimination_technique_1(C) 

simplify_presentation(C) 

C.schreier_generators = tuple(C._schreier_generators) 

C.reidemeister_relators = tuple(C._reidemeister_relators) 

return C.schreier_generators, C.reidemeister_relators 

 

 

FpGroupElement = FreeGroupElement