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

""" 

Set systems 

  

Many matroid methods return a collection of subsets. In this module a class 

:class:`SetSystem <sage.matroids.set_system.SetSystem>` is defined to do 

just this. The class is intended for internal use, so all you can do as a user 

is iterate over its members. 

  

The class is equipped with partition refinement methods to help with matroid 

isomorphism testing. 

  

AUTHORS: 

  

- Rudi Pendavingh, Stefan van Zwam (2013-04-01): initial version 

  

Methods 

======= 

""" 

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

# Copyright (C) 2013 Rudi Pendavingh <rudi.pendavingh@gmail.com> 

# Copyright (C) 2013 Stefan van Zwam <stefanvanzwam@gmail.com> 

# 

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

# 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 __future__ import print_function 

  

from cysignals.memory cimport check_allocarray, check_reallocarray, sig_free 

include 'sage/data_structures/bitset.pxi' 

  

# SetSystem 

  

cdef class SetSystem: 

""" 

A ``SetSystem`` is an enumerator of a collection of subsets of a given 

fixed and finite ground set. It offers the possibility to enumerate its 

contents. One is most likely to encounter these as output from some 

Matroid methods:: 

  

sage: M = matroids.named_matroids.Fano() 

sage: M.circuits() 

Iterator over a system of subsets 

  

To access the sets in this structure, simply iterate over them. The 

simplest way must be:: 

  

sage: from sage.matroids.set_system import SetSystem 

sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) 

sage: T = list(S) 

  

Or immediately use it to iterate:: 

  

sage: from sage.matroids.set_system import SetSystem 

sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) 

sage: [min(X) for X in S] 

[1, 3, 1] 

  

Note that this class is intended for runtime, so no loads/dumps mechanism 

was implemented. 

  

.. WARNING:: 

  

The only guaranteed behavior of this class is that it is iterable. It 

is expected that M.circuits(), M.bases(), and so on will in the near 

future return actual iterators. All other methods (which are already 

hidden by default) are only for internal use by the Sage matroid code. 

""" 

def __cinit__(self, groundset, subsets=None, capacity=1): 

""" 

Init internal data structures. 

  

EXAMPLES:: 

  

sage: from sage.matroids.set_system import SetSystem 

sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) 

sage: S 

Iterator over a system of subsets 

""" 

cdef long i 

if not isinstance(groundset, tuple): 

self._groundset = tuple(groundset) 

else: 

self._groundset = groundset 

self._idx = {} 

for i in xrange(len(groundset)): 

self._idx[groundset[i]] = i 

  

self._groundset_size = len(groundset) 

self._bitset_size = max(self._groundset_size, 1) 

self._capacity = capacity 

self._subsets = <bitset_t*>check_allocarray(self._capacity, sizeof(bitset_t)) 

bitset_init(self._temp, self._bitset_size) 

self._len = 0 

  

def __init__(self, groundset, subsets=None, capacity=1): 

""" 

Create a SetSystem. 

  

INPUT: 

  

- ``groundset`` -- a list or tuple of finitely many elements. 

- ``subsets`` -- (default: ``None``) an enumerator for a set of 

subsets of ``groundset``. 

- ``capacity`` -- (default: ``1``) Initial maximal capacity of the set 

system. 

  

EXAMPLES:: 

  

sage: from sage.matroids.set_system import SetSystem 

sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) 

sage: S 

Iterator over a system of subsets 

sage: sorted(S[1]) 

[3, 4] 

sage: for s in S: print(sorted(s)) 

[1, 2] 

[3, 4] 

[1, 2, 4] 

  

""" 

if subsets is not None: 

for e in subsets: 

self.append(e) 

  

def __dealloc__(self): 

cdef long i 

for i in xrange(self._len): 

bitset_free(self._subsets[i]) 

sig_free(self._subsets) 

bitset_free(self._temp) 

  

def __len__(self): 

""" 

Return the number of subsets in this SetSystem. 

  

EXAMPLES:: 

  

sage: from sage.matroids.set_system import SetSystem 

sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) 

sage: S 

Iterator over a system of subsets 

sage: len(S) 

3 

""" 

return self._len 

  

def __iter__(self): 

""" 

Return an iterator for the subsets in this SetSystem. 

  

EXAMPLES:: 

  

sage: from sage.matroids.set_system import SetSystem 

sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) 

sage: for s in S: print(sorted(s)) 

[1, 2] 

[3, 4] 

[1, 2, 4] 

""" 

return SetSystemIterator(self) 

  

def __getitem__(self, k): 

""" 

Return the `k`-th subset in this SetSystem. 

  

INPUT: 

  

- ``k`` -- an integer. The index of the subset in the system. 

  

OUTPUT: 

  

The subset at index `k`. 

  

EXAMPLES:: 

  

sage: from sage.matroids.set_system import SetSystem 

sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) 

sage: sorted(S[0]) 

[1, 2] 

sage: sorted(S[1]) 

[3, 4] 

sage: sorted(S[2]) 

[1, 2, 4] 

""" 

if k < len(self): 

return self.subset(k) 

else: 

raise ValueError("out of range") 

  

def __repr__(self): 

""" 

Return a string representation of ``self``. 

  

EXAMPLES:: 

  

sage: from sage.matroids.set_system import SetSystem 

sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) 

sage: repr(S) # indirect doctest 

'Iterator over a system of subsets' 

  

""" 

return "Iterator over a system of subsets" 

  

cdef copy(self): 

cdef SetSystem S 

S = SetSystem(self._groundset, capacity=len(self)) 

for i in xrange(len(self)): 

S._append(self._subsets[i]) 

return S 

  

cdef _relabel(self, l): 

""" 

Relabel each element `e` of the ground set as `l(e)`, where `l` is a 

given injective map. 

  

INPUT: 

  

- ``l`` -- a python object such that `l[e]` is the new label of e. 

  

OUTPUT: 

  

``None``. 

  

""" 

cdef long i 

E = [] 

for i in range(self._groundset_size): 

if self._groundset[i] in l: 

E.append(l[self._E[i]]) 

else: 

E.append(self._E[i]) 

self._groundset = E 

self._idx = {} 

for i in xrange(self._groundset_size): 

self._idx[self._groundset[i]] = i 

  

cpdef _complements(self): 

""" 

Return a SetSystem containing the complements of each element in the 

groundset. 

  

EXAMPLES:: 

  

sage: from sage.matroids.set_system import SetSystem 

sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) 

sage: T = S._complements() 

sage: for t in T: print(sorted(t)) 

[3, 4] 

[1, 2] 

[3] 

  

""" 

cdef SetSystem S 

if self._groundset_size == 0: 

return self 

S = SetSystem(self._groundset, capacity=len(self)) 

for i in xrange(len(self)): 

bitset_complement(self._temp, self._subsets[i]) 

S._append(self._temp) 

return S 

  

cdef inline resize(self, k=None): 

""" 

Change the capacity of the SetSystem. 

""" 

if k is None: 

k = self._len 

for i in xrange(k, self._len): 

bitset_free(self._subsets[i]) 

self._len = min(self._len, k) 

k2 = max(k, 1) 

self._subsets = <bitset_t*>check_reallocarray(self._subsets, k2, sizeof(bitset_t)) 

self._capacity = k2 

  

cdef inline _append(self, bitset_t X): 

""" 

Append subset in internal, bitset format 

""" 

if self._capacity == self._len: 

self.resize(self._capacity * 2) 

bitset_init(self._subsets[self._len], self._bitset_size) 

bitset_copy(self._subsets[self._len], X) 

self._len += 1 

  

cdef inline append(self, X): 

""" 

Append subset. 

""" 

if self._capacity == self._len: 

self.resize(self._capacity * 2) 

bitset_init(self._subsets[self._len], self._bitset_size) 

bitset_clear(self._subsets[self._len]) 

for x in X: 

bitset_add(self._subsets[self._len], self._idx[x]) 

self._len += 1 

  

cdef inline _subset(self, long k): 

""" 

Return the k-th subset, in index format. 

""" 

return bitset_list(self._subsets[k]) 

  

cdef subset(self, k): 

""" 

Return the k-th subset. 

""" 

cdef long i 

F = set() 

i = bitset_first(self._subsets[k]) 

while i >= 0: 

F.add(self._groundset[i]) 

i = bitset_next(self._subsets[k], i + 1) 

return frozenset(F) 

  

cpdef _get_groundset(self): 

""" 

Return the ground set of this SetSystem. 

  

EXAMPLES:: 

  

sage: from sage.matroids.set_system import SetSystem 

sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) 

sage: sorted(S._get_groundset()) 

[1, 2, 3, 4] 

""" 

return frozenset(self._groundset) 

  

cpdef is_connected(self): 

""" 

Test if the :class:`SetSystem` is connected. 

  

A :class:`SetSystem` is connected if there is no nonempty proper subset 

``X`` of the ground set so the each subset is either contained in ``X`` 

or disjoint from ``X``. 

  

EXAMPLES:: 

  

sage: from sage.matroids.set_system import SetSystem 

sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) 

sage: S.is_connected() 

True 

sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4]]) 

sage: S.is_connected() 

False 

sage: S = SetSystem([1], []) 

sage: S.is_connected() 

True 

  

""" 

if self._groundset_size <= 1: 

return True 

cdef long i 

bitset_clear(self._temp) 

cdef bitset_t active 

bitset_init(active, self._len) 

bitset_complement(active, active) 

  

# We compute the union of all sets containing 0, and deactivate them. 

for i in xrange(self._len): 

if bitset_in(self._subsets[i], 0): 

bitset_union(self._temp, self._subsets[i], self._temp) 

bitset_discard(active, i) 

  

cdef bint closed = False 

while not closed: 

closed = True 

  

# We update _temp with all active sets that intersects it. If there 

# is no such set, then _temp is closed (i.e. a connected component). 

i = bitset_first(active) 

while i>=0: 

if not bitset_are_disjoint(self._temp, self._subsets[i]): 

bitset_union(self._temp, self._subsets[i], self._temp) 

bitset_discard(active, i) 

closed = False 

i = bitset_next(active, i+1) 

bitset_free(active) 

bitset_complement(self._temp, self._temp) 

return bitset_isempty(self._temp) 

  

# isomorphism 

  

cdef list _incidence_count(self, E): 

""" 

For the sub-collection indexed by ``E``, count how often each element 

occurs. 

""" 

cdef long i, e 

cdef list cnt 

cnt = [0 for v in xrange(self._groundset_size)] 

for e in E: 

i = bitset_first(self._subsets[e]) 

while i >= 0: 

cnt[i] += 1 

i = bitset_next(self._subsets[e], i + 1) 

return cnt 

  

cdef SetSystem _groundset_partition(self, SetSystem P, list cnt): 

""" 

Helper method for partition methods below. 

""" 

cdef dict C 

cdef long i, j, v, t0, t 

cdef bint split 

  

C = {} 

for i in xrange(len(P)): 

v = bitset_first(P._subsets[i]) 

if v < 0: 

continue 

t0 = cnt[v] 

v = bitset_next(P._subsets[i], v + 1) 

split = False 

while v >= 0: 

t = cnt[v] 

if t != t0: 

split = True 

if t < t0: 

t0 = t 

v = bitset_next(P._subsets[i], v + 1) 

if split: 

C.clear() 

v = bitset_first(P._subsets[i]) 

while v >= 0: 

t = cnt[v] 

if t != t0: 

if t in C: 

C[t].add(v) 

else: 

C[t] = set([v]) 

v = bitset_next(P._subsets[i], v + 1) 

for t in sorted(C): 

bitset_clear(self._temp) 

for v in C[t]: 

bitset_add(self._temp, v) 

bitset_discard(P._subsets[i], v) 

P._append(self._temp) 

  

cdef long subset_characteristic(self, SetSystem P, long e): 

""" 

Helper method for partition methods below. 

""" 

cdef long c 

c = 0 

for p in xrange(len(P)): 

c <<= bitset_len(P._subsets[p]) 

bitset_intersection(self._temp, P._subsets[p], self._subsets[e]) 

c += bitset_len(self._temp) 

return c 

  

cdef subsets_partition(self, SetSystem P=None, E=None): 

""" 

Helper method for partition methods below. 

""" 

S = {} 

if P is None: 

P = self.groundset_partition() 

if E is None: 

E = xrange(self._len) 

if len(E) == 0: 

return [E] 

  

ED = [(self.subset_characteristic(P, e), e) for e in E] 

ED.sort() 

  

EP = [] 

ep = [] 

d = ED[0][0] 

eh = [d] 

for ed in ED: 

if ed[0] != d: 

EP.append(ep) 

ep = [ed[1]] 

d = ed[0] 

else: 

ep.append(ed[1]) 

eh.append(ed[0]) 

EP.append(ep) 

return EP, hash(tuple(eh)) 

  

cdef _distinguish(self, v): 

""" 

Helper method for partition methods below. 

""" 

cdef SetSystem S 

S = SetSystem(self._groundset, capacity=len(self) + 1) 

bitset_clear(self._temp) 

bitset_add(self._temp, v) 

for i in xrange(len(self)): 

bitset_difference(S._temp, self._subsets[i], self._temp) 

S._append(S._temp) 

S._append(self._temp) 

return S 

  

# partition functions 

cdef initial_partition(self, SetSystem P=None, E=None): 

""" 

Helper method for partition methods below. 

""" 

if E is None: 

E = xrange(self._len) 

if P is None: 

if self._groundset: 

P = SetSystem(self._groundset, [self._groundset], capacity=self._groundset_size) 

else: 

P = SetSystem([], []) 

cnt = self._incidence_count(E) 

self._groundset_partition(P, cnt) 

return P 

  

cpdef _equitable_partition(self, SetSystem P=None, EP=None): 

""" 

Return an equitable ordered partition of the ground set of the 

hypergraph whose edges are the subsets in this SetSystem. 

  

Given any ordered partition `P = (p_1, ..., p_k)` of the ground set of 

a hypergraph, any edge `e` of the hypergraph has a characteristic 

intersection number sequence `i(e)=(|p_1\cap e|, ... , |p_k\cap e|))`. 

There is an ordered partition `EP` of the edges that groups the edges 

according to this intersection number sequence. Given this an ordered 

partition of the edges, we may similarly refine `P` to a new ordered 

partition `P'`, by considering the incidence numbers of ground set 

elements with each partition element of `EP`. 

  

The ordered partition `P` is equitable when `P' = P`. 

  

INPUT: 

  

- ``P``, an equitable ordered partition of the ground set, stored as 

a SetSystem. 

- ``EP``, the corresponding equitable partition of the edges, stored 

as a list of lists of indices of subsets of this SetSystem. 

  

OUTPUT: 

  

- ``P``, an equitable ordered partition of the ground set, stored as a 

SetSystem. 

- ``EP``, the corresponding equitable partition of the edges, stored 

as a list of lists of indices of subsets of this SetSystem. 

- ``h``, an integer invariant of the SetSystem. 

  

EXAMPLES:: 

  

sage: from sage.matroids.set_system import SetSystem 

sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) 

sage: for p in S._equitable_partition()[0]: print(sorted(p)) 

[3] 

[4] 

[1, 2] 

sage: T = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 3, 4]]) 

sage: for p in T._equitable_partition()[0]: print(sorted(p)) 

[2] 

[1] 

[3, 4] 

  

.. NOTE:: 

  

We do not maintain any well-defined order when refining a 

partition. We do maintain that the resulting order of the 

partition elements is an invariant of the isomorphism class of the 

hypergraph. 

""" 

cdef long h, l 

cdef list EP2, H 

  

if P is None: 

P = self.initial_partition() 

else: 

P = P.copy() 

if EP is None: 

EP = self.subsets_partition(P)[0] 

  

h = len(EP) 

pl = 0 

while len(P) > pl: 

H = [h] 

pl = len(P) 

EP2 = [] 

for ep in EP: 

SP, h = self.subsets_partition(P, ep) 

H.append(h) 

for p in SP: 

cnt = self._incidence_count(p) 

self._groundset_partition(P, cnt) 

if len(p) > 1: 

EP2.append(p) 

EP = EP2 

h = hash(tuple(H)) 

  

return P, EP, h 

  

cpdef _heuristic_partition(self, SetSystem P=None, EP=None): 

""" 

Return an heuristic ordered partition into singletons of the ground 

set of the hypergraph whose edges are the subsets in this SetSystem. 

  

This partition obtained as follows: make an equitable 

partition ``P``, and while ``P`` has a partition element ``p`` with 

more than one element, select an arbitrary ``e`` from the first such 

``p`` and split ``p`` into ``p-e``. Then replace ``P`` with 

the equitable refinement of this partition. 

  

INPUT: 

  

- ``P`` -- (default: ``None``) an ordered partition of the ground set. 

- ``EP`` -- (default: ``None``) the corresponding partition of the 

edges, stored as a list of lists of indices of subsets of this 

SetSystem. 

  

OUTPUT: 

  

- ``P`` -- an ordered partition of the ground set into singletons, 

stored as a SetSystem. 

- ``EP`` -- the corresponding partition of the edges, stored as a list 

of lists of indices of subsets of this SetSystem. 

- ``h`` -- an integer invariant of the SetSystem. 

  

EXAMPLES:: 

  

sage: from sage.matroids.set_system import SetSystem 

sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) 

sage: for p in S._heuristic_partition()[0]: print(sorted(p)) 

[3] 

[4] 

[2] 

[1] 

sage: T = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 3, 4]]) 

sage: for p in T._heuristic_partition()[0]: print(sorted(p)) 

[2] 

[1] 

[4] 

[3] 

""" 

P, EP, h = self._equitable_partition(P, EP) 

for i in xrange(len(P)): 

if bitset_len(P._subsets[i]) > 1: 

return self._heuristic_partition(P._distinguish(bitset_first(P._subsets[i])), EP) 

return P, EP, h 

  

cpdef _isomorphism(self, SetSystem other, SetSystem SP=None, SetSystem OP=None): 

""" 

Return a groundset isomorphism between this SetSystem and an other. 

  

INPUT: 

  

- ``other`` -- a SetSystem 

- ``SP`` (optional) -- a SetSystem storing an ordered partition of the 

ground set of ``self`` 

- ``OP`` (optional) -- a SetSystem storing an ordered partition of the 

ground set of ``other`` 

  

OUTPUT: 

  

``morphism`` -- a dictionary containing an isomorphism respecting the 

given ordered partitions, or ``None`` if no such isomorphism exists. 

  

EXAMPLES:: 

  

sage: from sage.matroids.set_system import SetSystem 

sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) 

sage: T = SetSystem(['a', 'b', 'c', 'd'], [['a', 'b'], ['c', 'd'], 

....: ['a', 'c', 'd']]) 

sage: S._isomorphism(T) 

{1: 'c', 2: 'd', 3: 'b', 4: 'a'} 

sage: S = SetSystem([], []) 

sage: S._isomorphism(S) 

{} 

""" 

cdef long l, p 

if SP is None or OP is None: 

SP, SEP, sh = self._equitable_partition() 

OP, OEP, oh = other._equitable_partition() 

if sh != oh: 

return None 

if len(SP) != len(OP): 

return None 

p = SP._groundset_size + 1 

for i in xrange(len(SP)): 

l = bitset_len(SP._subsets[i]) 

if l != bitset_len(OP._subsets[i]): 

return None 

if l != 1 and l < p: 

p = l 

for i in xrange(len(SP)): 

if bitset_len(SP._subsets[i]) == p: 

SP2, SEP, sh = self._equitable_partition(SP._distinguish(bitset_first(SP._subsets[i]))) 

v = bitset_first(OP._subsets[i]) 

while v >= 0: 

OP2, OEP, oh = other._equitable_partition(OP._distinguish(v)) 

if sh == oh: 

m = self._isomorphism(other, SP2, OP2) 

if m is not None: 

return m 

v = bitset_next(OP._subsets[i], v + 1) 

return None 

if sorted([self.subset_characteristic(SP, i) for i in xrange(len(self))]) != sorted([other.subset_characteristic(OP, i) for i in xrange(len(other))]): 

return None 

return dict([(self._groundset[bitset_first(SP._subsets[i])], other._groundset[bitset_first(OP._subsets[i])]) for i in xrange(len(SP))]) 

  

cpdef _equivalence(self, is_equiv, SetSystem other, SetSystem SP=None, SetSystem OP=None): 

""" 

Return a groundset isomorphism that is an equivalence between this 

SetSystem and an other. 

  

INPUT: 

  

- ``is_equiv`` -- a function that determines if a given groundset 

isomorphism is a valid equivalence 

- ``other`` -- a SetSystem 

- ``SP`` (optional) -- a SetSystem storing an ordered partition of the 

groundset of ``self`` 

- ``OP`` (optional) -- a SetSystem storing an ordered partition of the 

groundset of ``other`` 

  

OUTPUT: 

  

``morphism``, a dictionary containing an isomorphism respecting the 

given ordered partitions, so that ``is_equiv(self, other, morphism)`` 

is ``True``; or ``None`` if no such equivalence exists. 

  

EXAMPLES:: 

  

sage: from sage.matroids.set_system import SetSystem 

sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) 

sage: T = SetSystem(['a', 'b', 'c', 'd'], [['a', 'b'], ['c', 'd'], 

....: ['a', 'c', 'd']]) 

sage: S._equivalence(lambda self, other, morph:True, T) 

{1: 'c', 2: 'd', 3: 'b', 4: 'a'} 

  

Check that :trac:`15189` is fixed:: 

  

sage: M = Matroid(ring=GF(5), reduced_matrix=[[1,0,3],[0,1,1],[1,1,0]]) 

sage: N = Matroid(ring=GF(5), reduced_matrix=[[1,0,1],[0,1,1],[1,1,0]]) 

sage: M.is_field_isomorphic(N) 

False 

sage: any(M.is_field_isomorphism(N, p) for p in Permutations(range(6))) 

False 

""" 

if SP is None or OP is None: 

SP, SEP, sh = self._equitable_partition() 

OP, OEP, oh = other._equitable_partition() 

if sh != oh: 

return None 

if len(SP) != len(OP): 

return None 

for i in xrange(len(SP)): 

if bitset_len(SP._subsets[i]) != bitset_len(OP._subsets[i]): 

return None 

for i in xrange(len(SP)): 

if bitset_len(SP._subsets[i]) > 1: 

SP2, SEP, sh = self._equitable_partition(SP._distinguish(bitset_first(SP._subsets[i]))) 

v = bitset_first(OP._subsets[i]) 

while v >= 0: 

OP2, OEP, oh = other._equitable_partition(OP._distinguish(v)) 

if sh == oh: 

m = self._equivalence(is_equiv, other, SP2, OP2) 

if m is not None: 

return m 

v = bitset_next(OP._subsets[i], v + 1) 

return None 

morph = dict([(self._groundset[bitset_first(SP._subsets[i])], other._groundset[bitset_first(OP._subsets[i])]) for i in xrange(len(SP))]) 

if is_equiv(self, other, morph): 

return morph 

  

  

cdef class SetSystemIterator: 

def __init__(self, H): 

""" 

Create an iterator for a SetSystem. 

  

Called internally when iterating over the contents of a SetSystem. 

  

EXAMPLES:: 

  

sage: from sage.matroids.set_system import SetSystem 

sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) 

sage: type(S.__iter__()) 

<... 'sage.matroids.set_system.SetSystemIterator'> 

""" 

self._H = H 

self._pointer = -1 

self._len = len(H) 

  

def __next__(self): 

""" 

Return the next subset of a SetSystem. 

  

EXAMPLES:: 

  

sage: from sage.matroids.set_system import SetSystem 

sage: S = SetSystem([1, 2, 3, 4], [[1, 2], [3, 4], [1, 2, 4]]) 

sage: I = S.__iter__() 

sage: sorted(I.__next__()) 

[1, 2] 

sage: sorted(I.__next__()) 

[3, 4] 

sage: sorted(I.__next__()) 

[1, 2, 4] 

""" 

self._pointer += 1 

if self._pointer == self._len: 

self._pointer = -1 

raise StopIteration 

else: 

return self._H.subset(self._pointer)