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

""" 

Python interface to partition backtrack functions 

  

EXAMPLES:: 

  

sage: import sage.groups.perm_gps.partn_ref.refinement_python 

  

This module provides Python frontends to the Cython-based partition backtrack 

functions. This allows one to write the three input functions 

(all_children_are_equivalent, refine_and_return_invariant, and compare_structures) 

in pure Python, and still use the Cython algorithms. Experimentation with 

specific partition backtrack implementations no longer requires compilation, as 

the input functions can be dynamically changed at runtime. 

  

NOTE: 

  

This is not intended for production quality implementations of partition 

refinement, but instead for experimentation, learning, and use of the Python 

debugger. 

  

""" 

  

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

# Copyright (C) 2006 - 2011 Robert L. Miller <rlmillster@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 cysignals.memory cimport sig_malloc, sig_free 

  

from .data_structures cimport * 

from .automorphism_group_canonical_label cimport ( 

get_aut_gp_and_can_lab, aut_gp_and_can_lab, 

allocate_agcl_output, deallocate_agcl_output) 

from .double_coset cimport double_coset 

from sage.rings.integer cimport Integer 

  

  

cdef class PythonPartitionStack: 

""" 

Instances of this class wrap a (Cython) PartitionStack. 

""" 

  

def __init__(self, int n): 

""" 

Initialize a PartitionStack. 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import PythonPartitionStack 

sage: P = PythonPartitionStack(7) # implicit doctest 

  

""" 

self.c_ps = PS_new(n, 1) 

  

def __dealloc__(self): 

""" 

Deallocate the PartitionStack. 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import PythonPartitionStack 

sage: P = PythonPartitionStack(7) 

sage: del(P) # implicit doctest 

  

""" 

PS_dealloc(self.c_ps) 

  

def __repr__(self): 

""" 

Returns a string representing the stack. 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import PythonPartitionStack 

sage: P = PythonPartitionStack(7) 

sage: P # implicit doctest 

PythonPartitionStack of degree 7 and depth 0. 

  

""" 

return "PythonPartitionStack of degree %d and depth %d."%(self.c_ps.degree, self.c_ps.depth) 

  

def display(self): 

""" 

Prints a representation of the stack. 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import PythonPartitionStack 

sage: P = PythonPartitionStack(7) 

sage: P.depth(1) 

1 

sage: P.set_level(2,1) 

sage: P.display() 

(0 1 2 3 4 5 6) 

(0 1 2|3 4 5 6) 

  

""" 

PS_print(self.c_ps) 

  

def is_discrete(self): 

""" 

Returns whether the deepest partition consists only of singleton cells. 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import PythonPartitionStack 

sage: P = PythonPartitionStack(7) 

sage: P.is_discrete() 

False 

sage: [P.set_level(i,0) for i in range(7)] 

[None, None, None, None, None, None, None] 

sage: P.is_discrete() 

True 

  

""" 

return PS_is_discrete(self.c_ps) 

  

def num_cells(self): 

""" 

Returns the number of cells in the deepest partition. 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import PythonPartitionStack 

sage: P = PythonPartitionStack(7) 

sage: P.num_cells() 

1 

  

""" 

return PS_num_cells(self.c_ps) 

  

def move_min_to_front(self, int start, int end): 

""" 

Makes sure that the first element of the segment of entries i with 

start <= i <= end is minimal. 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import PythonPartitionStack 

sage: P = PythonPartitionStack(7) 

sage: P.set_entry(1,0) 

sage: P.set_entry(0,1) 

sage: P.display() 

(1 0 2 3 4 5 6) 

sage: P.move_min_to_front(0,1) 

sage: P.display() 

(0 1 2 3 4 5 6) 

  

""" 

PS_move_min_to_front(self.c_ps, start, end) 

  

def __copy__(self): 

""" 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import PythonPartitionStack 

sage: P = PythonPartitionStack(7) 

sage: Q = copy(P) 

sage: P.display() 

(0 1 2 3 4 5 6) 

sage: Q.display() 

(0 1 2 3 4 5 6) 

  

""" 

cdef PythonPartitionStack cpy 

cpy = PythonPartitionStack(self.c_ps.degree) 

PS_copy_from_to(self.c_ps, cpy.c_ps) 

return cpy 

  

def clear(self): 

""" 

Sets the current partition to the first shallower one, i.e. forgets about 

boundaries between cells that are new to the current level. 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import PythonPartitionStack 

sage: P = PythonPartitionStack(7) 

sage: P.depth(1) 

1 

sage: P.set_level(2,1) 

sage: P.display() 

(0 1 2 3 4 5 6) 

(0 1 2|3 4 5 6) 

sage: P.clear() 

sage: P.display() 

(0 1 2 3 4 5 6) 

(0 1 2 3 4 5 6) 

  

""" 

PS_clear(self.c_ps) 

  

def entries(self): 

""" 

Returns the entries array as a Python list of ints. 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import PythonPartitionStack 

sage: P = PythonPartitionStack(7) 

sage: P.entries() 

[0, 1, 2, 3, 4, 5, 6] 

sage: P.levels() 

[7, 7, 7, 7, 7, 7, -1] 

  

""" 

cdef int i 

return [self.c_ps.entries[i] for i from 0 <= i < self.c_ps.degree] 

  

def set_entry(self, int i, int entry): 

""" 

Sets the ith entry of the entries array to entry. 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import PythonPartitionStack 

sage: P = PythonPartitionStack(7) 

sage: P.set_entry(1,0) 

sage: P.set_entry(0,1) 

sage: P.display() 

(1 0 2 3 4 5 6) 

  

""" 

self.c_ps.entries[i] = entry 

  

def get_entry(self, int i): 

""" 

Gets the ith entry of the entries array. 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import PythonPartitionStack 

sage: P = PythonPartitionStack(7) 

sage: P.get_entry(0) 

0 

  

""" 

return self.c_ps.entries[i] 

  

def levels(self): 

""" 

Return the levels array as a Python list of ints. 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import PythonPartitionStack 

sage: P = PythonPartitionStack(7) 

sage: P.entries() 

[0, 1, 2, 3, 4, 5, 6] 

sage: P.levels() 

[7, 7, 7, 7, 7, 7, -1] 

  

""" 

return [self.c_ps.levels[i] for i from 0 <= i < self.c_ps.degree] 

  

def set_level(self, int i, int level): 

""" 

Sets the ith entry of the levels array to entry. 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import PythonPartitionStack 

sage: P = PythonPartitionStack(7) 

sage: P.depth(1) 

1 

sage: P.set_level(2,1) 

sage: P.display() 

(0 1 2 3 4 5 6) 

(0 1 2|3 4 5 6) 

  

""" 

self.c_ps.levels[i] = level 

  

def get_level(self, int i): 

""" 

Gets the ith entry of the levels array. 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import PythonPartitionStack 

sage: P = PythonPartitionStack(7) 

sage: P.get_level(0) 

7 

  

""" 

return self.c_ps.levels[i] 

  

def depth(self, new=None): 

""" 

Returns the depth of the deepest partition in the stack, setting it to 

new if new is not None. 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import PythonPartitionStack 

sage: P = PythonPartitionStack(7) 

sage: P.depth() 

0 

  

""" 

if new is not None: 

self.c_ps.depth = new 

return self.c_ps.depth 

  

def degree(self, new=None): 

""" 

Returns the degree of the partition stack, setting it to 

new if new is not None. 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import PythonPartitionStack 

sage: P = PythonPartitionStack(7) 

sage: P.degree() 

7 

  

""" 

if new is not None: 

self.c_ps.degree = new 

return self.c_ps.degree 

  

def partition(self, int k): 

""" 

Return the partition at level k, as a Python list of lists. 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import PythonPartitionStack 

sage: P = PythonPartitionStack(7) 

sage: P.depth(1) 

1 

sage: P.set_level(2,1) 

sage: P.partition(0) 

[[0, 1, 2, 3, 4, 5, 6]] 

sage: P.partition(1) 

[[0, 1, 2], [3, 4, 5, 6]] 

  

""" 

cdef int i 

cdef list partition = [], cell = [] 

for i from 0 <= i < self.c_ps.degree: 

cell.append(self.c_ps.entries[i]) 

if self.c_ps.levels[i] <= k: 

partition.append(cell) 

if i < self.c_ps.degree: 

cell = [] 

return partition 

  

class PythonObjectWrapper: 

""" 

Instances of this class wrap a Python object and the refinement functions. 

""" 

def __init__(self, obj, acae_fn, rari_fn, cs_fn, int degree): 

""" 

Initialize a PythonObjectWrapper. 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import PythonObjectWrapper 

sage: def acae(a,b): 

....: return 0 

sage: def rari(a,b,c): 

....: return 0 

sage: def cs(a,b,c,d,e): 

....: return 0 

sage: from sage.groups.perm_gps.partn_ref.refinement_python import PythonObjectWrapper 

sage: P = PythonObjectWrapper(None, acae, rari, cs, 7) # implicit doctest 

sage: P.obj 

sage: P.degree 

7 

sage: P.acae_fn 

<function acae at ...> 

sage: P.rari_fn 

<function rari at ...> 

sage: P.cs_fn 

<function cs at ...> 

  

""" 

self.degree = degree 

self.obj = obj 

self.acae_fn = acae_fn 

self.rari_fn = rari_fn 

self.cs_fn = cs_fn 

  

cdef bint all_children_are_equivalent_python(PartitionStack *PS, void *S): 

""" 

Python conversion of all_children_are_equivalent function. 

""" 

cdef PythonPartitionStack Py_PS = PythonPartitionStack(PS.degree) 

cdef object S_obj = <object> S 

PS_copy_from_to(PS, Py_PS.c_ps) 

return S_obj.acae_fn(Py_PS, S_obj.obj) 

  

cdef int refine_and_return_invariant_python(PartitionStack *PS, void *S, int *cells_to_refine_by, int ctrb_len): 

""" 

Python conversion of refine_and_return_invariant function. 

""" 

cdef PythonPartitionStack Py_PS = PythonPartitionStack(PS.degree) 

cdef object S_obj = <object> S 

PS_copy_from_to(PS, Py_PS.c_ps) 

cdef int i 

cdef list ctrb_py = [cells_to_refine_by[i] for i from 0 <= i < ctrb_len] 

return S_obj.rari_fn(Py_PS, S_obj.obj, ctrb_py) 

  

cdef int compare_structures_python(int *gamma_1, int *gamma_2, void *S1, void *S2, int degree): 

""" 

Python conversion of compare_structures function. 

""" 

cdef int i 

cdef object S1_obj = <object> S1, S2_obj = <object> S2 

cdef list gamma_1_py = [gamma_1[i] for i from 0 <= i < degree] 

cdef list gamma_2_py = [gamma_2[i] for i from 0 <= i < degree] 

return S1_obj.cs_fn(gamma_1_py, gamma_2_py, S1_obj.obj, S2_obj.obj, degree) 

  

def aut_gp_and_can_lab_python(S, partition, n, 

all_children_are_equivalent, 

refine_and_return_invariant, 

compare_structures, 

canonical_label, base, order): 

""" 

Calls the automorphism group and canonical label function. 

  

INPUT: 

  

S -- the object to examine 

partition -- an ordered partition, as a list of lists 

n -- the degree of the automorphism group to be computed 

  

:: 

  

all_children_are_equivalent -- Python function of "signature": 

bool all_children_are_equivalent(PythonPartitionStack, object) 

refine_and_return_invariant -- Python function of "signature": 

int refine_and_return_invariant(PythonPartitionStack, object, list) 

compare_structures -- Python function of "signature": 

int compare_structures(list, list, object, object) 

(see automorphism_group_canonical_label.pyx for more documentation) 

  

:: 

  

canonical_label -- boolean; whether to search for a canonical label 

base -- boolean; whether to return a base for the automorphism group 

order -- boolean; whether to return the order of the automorphism group 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import aut_gp_and_can_lab_python 

sage: def acae(a,b): 

....: return 0 

sage: def rari(a,b,c): 

....: return 0 

sage: def cs(a,b,c,d,e): 

....: return 0 

sage: aut_gp_and_can_lab_python(None, [[0,1,2,3],[4,5]], 6, acae, rari, cs, True, True, True) 

([[0, 1, 3, 2, 4, 5], 

[0, 2, 1, 3, 4, 5], 

[1, 0, 2, 3, 4, 5], 

[0, 1, 2, 3, 5, 4]], 

[0, 1, 2, 3, 4, 5], 

[4, 0, 1, 2], 

48) 

sage: factorial(4)*factorial(2) 

48 

  

""" 

obj_wrapper = PythonObjectWrapper(S, all_children_are_equivalent, refine_and_return_invariant, compare_structures, n) 

cdef aut_gp_and_can_lab *output 

cdef PythonPartitionStack Py_PS = PythonPartitionStack(n) 

cdef int i, j 

cdef Integer I 

  

cdef PartitionStack *part = PS_from_list(partition) 

if part is NULL: 

raise MemoryError 

  

output = get_aut_gp_and_can_lab(<void *> obj_wrapper, part, n, 

&all_children_are_equivalent_python, 

&refine_and_return_invariant_python, 

&compare_structures_python, 

canonical_label, NULL, NULL, NULL) 

  

list_of_gens = [] 

for i from 0 <= i < output.num_gens: 

list_of_gens.append([output.generators[j+i*n] for j from 0 <= j < n]) 

return_tuple = [list_of_gens] 

if canonical_label: 

return_tuple.append([output.relabeling[i] for i from 0 <= i < n]) 

if base: 

return_tuple.append([output.group.base_orbits[i][0] for i from 0 <= i < output.group.base_size]) 

if order: 

I = Integer() 

SC_order(output.group, 0, I.value) 

return_tuple.append(I) 

PS_dealloc(part) 

deallocate_agcl_output(output) 

if len(return_tuple) == 1: 

return return_tuple[0] 

else: 

return tuple(return_tuple) 

  

  

def double_coset_python(S1, S2, partition1, ordering2, n, 

all_children_are_equivalent, 

refine_and_return_invariant, 

compare_structures): 

""" 

Calls the double coset function. 

  

INPUT: 

  

S1, S2 -- the objects to examine 

partition1 -- an ordered partition, as a list of lists 

ordering2 -- represents a partition of the points of S2, 

as a relabeling of partition1 

n -- the degree 

  

:: 

  

all_children_are_equivalent -- Python function of "signature": 

bool all_children_are_equivalent(PythonPartitionStack, object) 

refine_and_return_invariant -- Python function of "signature": 

int refine_and_return_invariant(PythonPartitionStack, object, list) 

compare_structures -- Python function of "signature": 

int compare_structures(list, list, object, object) 

(see double_coset.pyx for more documentation) 

  

EXAMPLES:: 

  

sage: from sage.groups.perm_gps.partn_ref.refinement_python import double_coset_python 

sage: def acae(a,b): 

....: return 0 

sage: def rari(a,b,c): 

....: return 0 

sage: def cs(a,b,c,d,e): 

....: return 0 

sage: double_coset_python(None, None, [[0,1,2,3],[4,5]], [2,3,1,5,0,4], 6, acae, rari, cs) 

[1, 2, 3, 5, 0, 4] 

  

sage: def compare_lists(p1,p2,l1,l2,deg): 

....: for i in range(len(l1)): 

....: a1 = l1[p1[i]] 

....: a2 = l2[p2[i]] 

....: if a1 < a2: return -1 

....: if a1 > a2: return 1 

....: return 0 

  

sage: double_coset_python([0,0,1], [1,0,0], [[0,1,2]], [0,1,2], 3, acae, rari, compare_lists) 

[1, 2, 0] 

  

""" 

obj_wrapper1 = PythonObjectWrapper(S1, all_children_are_equivalent, refine_and_return_invariant, compare_structures, n) 

obj_wrapper2 = PythonObjectWrapper(S2, all_children_are_equivalent, refine_and_return_invariant, compare_structures, n) 

  

cdef PartitionStack *part = PS_from_list(partition1) 

cdef int *ordering = <int *> sig_malloc(n * sizeof(int)) 

cdef int *output = <int *> sig_malloc(n * sizeof(int)) 

if part is NULL or ordering is NULL or output is NULL: 

PS_dealloc(part) 

sig_free(ordering) 

sig_free(output) 

raise MemoryError 

for i from 0 <= i < n: 

ordering[i] = ordering2[i] 

  

cdef bint isomorphic = double_coset(<void *> obj_wrapper1, <void *> obj_wrapper2, 

part, ordering, n, 

&all_children_are_equivalent_python, 

&refine_and_return_invariant_python, 

&compare_structures_python, NULL, NULL, output) 

  

PS_dealloc(part) 

sig_free(ordering) 

if isomorphic: 

output_py = [output[i] for i from 0 <= i < n] 

else: 

output_py = False 

sig_free(output) 

return output_py