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

""" 

Partition backtrack functions for binary codes 

  

EXAMPLES:: 

  

sage: import sage.groups.perm_gps.partn_ref.refinement_binary 

  

REFERENCE: 

  

- [1] McKay, Brendan D. Practical Graph Isomorphism. Congressus Numerantium, 

Vol. 30 (1981), pp. 45-87. 

  

- [2] Leon, Jeffrey. Permutation Group Algorithms Based on Partitions, I: 

Theory and Algorithms. J. Symbolic Computation, Vol. 12 (1991), pp. 

533-583. 

  

""" 

  

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

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

  

include "sage/data_structures/bitset.pxi" 

from .data_structures cimport * 

from sage.rings.integer cimport Integer 

from sage.structure.element import is_Matrix 

from .double_coset cimport double_coset 

  

  

cdef class LinearBinaryCodeStruct(BinaryCodeStruct): 

  

def __cinit__(self, matrix): 

cdef int i,j 

self.degree = matrix.ncols() 

self.dimension = matrix.nrows() 

if self.dimension >= (sizeof(int) << 3): 

raise NotImplementedError 

# By the time the dimension gets this big, the computation is infeasible anyway... 

self.nwords = 1<<self.dimension 

  

self.basis = <bitset_s *> sig_malloc(self.dimension * sizeof(bitset_s)) 

self.scratch_bitsets = <bitset_s *> sig_malloc((2*self.dimension+2) * sizeof(bitset_s)) 

self.alpha_is_wd = <bitset_s *> sig_malloc(sizeof(bitset_s)) 

self.word_ps = PS_new(self.nwords, 1) 

self.alpha = <int *> sig_malloc((self.nwords+self.degree) * sizeof(int)) 

self.scratch = <int *> sig_malloc((3*self.nwords+3*self.degree+2) * sizeof(int)) 

  

if self.basis is NULL or self.scratch_bitsets is NULL \ 

or self.alpha_is_wd is NULL or self.word_ps is NULL \ 

or self.alpha is NULL or self.scratch is NULL: 

sig_free(self.basis) 

sig_free(self.scratch_bitsets) 

sig_free(self.alpha_is_wd) 

PS_dealloc(self.word_ps) 

sig_free(self.alpha) 

sig_free(self.scratch) 

raise MemoryError 

  

cdef bint memerr = 0 

for i from 0 <= i < self.dimension: 

try: bitset_init(&self.basis[i], self.degree) 

except MemoryError: 

for j from 0 <= j < i: 

bitset_free(&self.basis[j]) 

memerr = 1 

break 

if not memerr: 

for i from 0 <= i < 2*self.dimension+2: 

try: bitset_init(&self.scratch_bitsets[i], self.degree) 

except MemoryError: 

for j from 0 <= j < i: 

bitset_free(&self.scratch_bitsets[j]) 

for j from 0 <= j < self.dimension: 

bitset_free(&self.basis[j]) 

memerr = 1 

break 

if not memerr: 

try: bitset_init(self.alpha_is_wd, self.nwords + self.degree) 

except MemoryError: 

for j from 0 <= j < 2*self.dimension+2: 

bitset_free(&self.scratch_bitsets[j]) 

for j from 0 <= j < self.dimension: 

bitset_free(&self.basis[j]) 

memerr = 1 

if memerr: 

sig_free(self.basis); sig_free(self.scratch_bitsets) 

sig_free(self.alpha_is_wd); PS_dealloc(self.word_ps) 

sig_free(self.alpha); sig_free(self.scratch) 

raise MemoryError 

else: 

bitset_zero(self.alpha_is_wd) 

for j from 0 <= j < self.dimension: 

bitset_zero(&self.basis[j]) 

  

for i,j in matrix.nonzero_positions(): 

bitset_set(&self.basis[i], j) 

  

self.output = NULL 

self.ith_word = &ith_word_linear 

  

def run(self, partition=None): 

""" 

Perform the canonical labeling and automorphism group computation, 

storing results to self. 

  

INPUT: 

partition -- an optional list of lists partition of the columns. 

default is the unit partition. 

  

EXAMPLES: 

sage: from sage.groups.perm_gps.partn_ref.refinement_binary import LinearBinaryCodeStruct 

  

sage: B = LinearBinaryCodeStruct(matrix(GF(2),[[1,0,1],[0,1,1]])) 

sage: B.run() 

sage: B.automorphism_group() 

([[0, 2, 1], [1, 0, 2]], 6, [0, 1]) 

sage: B.canonical_relabeling() 

[0, 1, 2] 

  

sage: B = LinearBinaryCodeStruct(matrix(GF(2),[[1,1,1,1]])) 

sage: B.automorphism_group() 

([[0, 1, 3, 2], [0, 2, 1, 3], [1, 0, 2, 3]], 24, [0, 1, 2]) 

sage: B.canonical_relabeling() 

[0, 1, 2, 3] 

  

sage: B = LinearBinaryCodeStruct(matrix(GF(2),[[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]])) 

sage: B.automorphism_group()[1] == factorial(14) 

True 

  

sage: M = Matrix(GF(2),[\ 

....: [1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0],\ 

....: [0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0],\ 

....: [0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1],\ 

....: [0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1],\ 

....: [0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]]) 

sage: B = LinearBinaryCodeStruct(M) 

sage: B.automorphism_group()[1] 

322560 

  

sage: M = Matrix(GF(2),[\ 

....: [1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0],\ 

....: [0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0],\ 

....: [0,0,0,0,0,1,0,1,0,0,0,1,1,1,1,1,1],\ 

....: [0,0,0,1,1,0,0,0,0,1,1,0,1,1,0,1,1]]) 

sage: B = LinearBinaryCodeStruct(M) 

sage: B.automorphism_group()[1] 

2304 

  

sage: M=Matrix(GF(2),[\ 

....: [1,0,0,1,1,1,1,0,0,1,0,0,0,0,0,0,0],\ 

....: [0,1,0,0,1,1,1,1,0,0,1,0,0,0,0,0,0],\ 

....: [0,0,1,0,0,1,1,1,1,0,0,1,0,0,0,0,0],\ 

....: [0,0,0,1,0,0,1,1,1,1,0,0,1,0,0,0,0],\ 

....: [0,0,0,0,1,0,0,1,1,1,1,0,0,1,0,0,0],\ 

....: [0,0,0,0,0,1,0,0,1,1,1,1,0,0,1,0,0],\ 

....: [0,0,0,0,0,0,1,0,0,1,1,1,1,0,0,1,0],\ 

....: [0,0,0,0,0,0,0,1,0,0,1,1,1,1,0,0,1]]) 

sage: B = LinearBinaryCodeStruct(M) 

sage: B.automorphism_group()[1] 

136 

  

sage: M = Matrix(GF(2),[\ 

....: [1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0], 

....: [0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0], 

....: [0,0,0,0,1,1,0,0,0,0,0,0,1,1,1,1,1,1], 

....: [0,0,1,1,0,0,0,0,0,0,1,1,1,1,0,0,1,1], 

....: [0,0,0,1,0,0,0,1,0,1,0,1,0,1,1,1,0,1], 

....: [0,1,0,0,0,1,0,0,0,1,1,1,0,1,0,1,1,0]]) 

sage: B = LinearBinaryCodeStruct(M) 

sage: B.automorphism_group()[1] 

2160 

  

sage: M=Matrix(GF(2),[\ 

....: [0,1,0,1,1,1,0,0,0,1,0,0,0,1,0,0,0,1,1,1,0,1],\ 

....: [1,0,1,1,1,0,0,0,1,0,0,0,1,0,0,0,1,1,1,0,1,0],\ 

....: [0,1,1,1,0,0,0,1,0,0,1,1,0,0,0,1,1,1,0,1,0,0],\ 

....: [1,1,1,0,0,0,1,0,0,1,0,0,0,0,1,1,1,0,1,0,0,1],\ 

....: [1,1,0,0,0,1,0,0,1,0,1,0,0,1,1,1,0,1,0,0,1,0],\ 

....: [1,0,0,0,1,0,0,1,0,1,1,0,1,1,1,0,1,0,0,1,0,0],\ 

....: [0,0,0,1,0,0,1,0,1,1,1,1,1,1,0,1,0,0,1,0,0,0],\ 

....: [0,0,1,0,0,1,0,1,1,1,0,1,1,0,1,0,0,1,0,0,0,1],\ 

....: [0,1,0,0,1,0,1,1,1,0,0,1,0,1,0,0,1,0,0,0,1,1],\ 

....: [1,0,0,1,0,1,1,1,0,0,0,0,1,0,0,1,0,0,0,1,1,1],\ 

....: [0,0,1,0,1,1,1,0,0,0,1,1,0,0,1,0,0,0,1,1,1,0]]) 

sage: B = LinearBinaryCodeStruct(M) 

sage: B.automorphism_group()[1] 

887040 

  

sage: M = Matrix(GF(2),[\ 

....: [1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], 

....: [0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0], 

....: [0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0], 

....: [0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0], 

....: [0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0], 

....: [0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0], 

....: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1], 

....: [1,0,1,0,1,0,1,0,1,1,0,0,0,0,0,0,1,1,0,0], 

....: [1,1,0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,0,0], 

....: [1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,0,1,0]]) 

sage: B = LinearBinaryCodeStruct(M) 

sage: B.automorphism_group()[1] 

294912 

  

sage: M = Matrix(GF(2), [\ 

....: [1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], 

....: [0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], 

....: [0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0], 

....: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0], 

....: [0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0], 

....: [0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0], 

....: [0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1], 

....: [0,0,0,0,0,0,1,1,0,0,0,0,0,1,0,1,0,0,1,1,1,0,1], 

....: [0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,1,1,1,1,0,0,0,1]]) 

sage: B = LinearBinaryCodeStruct(M) 

sage: B.automorphism_group()[1] 

442368 

  

sage: M = Matrix(GF(2), [\ 

....: [1,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0],\ 

....: [1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,1,1,0,0,0,0,1]]) 

sage: B = LinearBinaryCodeStruct(M) 

sage: B.automorphism_group()[1] 

17868913969917295853568000000 

  

""" 

cdef int i, n = self.degree 

cdef PartitionStack *part 

if partition is None: 

part = PS_new(n, 1) 

else: 

part = PS_from_list(partition) 

if part is NULL: 

raise MemoryError 

  

self.first_time = 1 

  

self.output = get_aut_gp_and_can_lab(<void *>self, part, n, &all_children_are_equivalent, &refine_by_bip_degree, &compare_linear_codes, 1, NULL, NULL, NULL) 

  

PS_dealloc(part) 

  

def automorphism_group(self): 

""" 

Returns a list of generators of the automorphism group, along with its 

order and a base for which the list of generators is a strong generating 

set. 

  

EXAMPLES: (For more examples, see self.run()) 

sage: from sage.groups.perm_gps.partn_ref.refinement_binary import LinearBinaryCodeStruct 

  

sage: B = LinearBinaryCodeStruct(matrix(GF(2),[[1,1,1,1]])) 

sage: B.automorphism_group() 

([[0, 1, 3, 2], [0, 2, 1, 3], [1, 0, 2, 3]], 24, [0, 1, 2]) 

  

""" 

cdef int i, j 

cdef list generators, base 

cdef Integer order 

if self.output is NULL: 

self.run() 

generators = [] 

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

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

order = Integer() 

SC_order(self.output.group, 0, order.value) 

base = [self.output.group.base_orbits[i][0] for i from 0 <= i < self.output.group.base_size] 

return generators, order, base 

  

def canonical_relabeling(self): 

""" 

Returns a canonical relabeling (in list permutation format). 

  

EXAMPLES: (For more examples, see self.run()) 

sage: from sage.groups.perm_gps.partn_ref.refinement_binary import LinearBinaryCodeStruct 

  

sage: B = LinearBinaryCodeStruct(matrix(GF(2), [[1,1,0]])) 

sage: B.automorphism_group() 

([[1, 0, 2]], 2, [0]) 

sage: B.canonical_relabeling() 

[1, 2, 0] 

sage: B = LinearBinaryCodeStruct(matrix(GF(2), [[1,0,1]])) 

sage: B.automorphism_group() 

([[2, 1, 0]], 2, [0]) 

sage: B.canonical_relabeling() 

[1, 0, 2] 

  

""" 

cdef int i 

if self.output is NULL: 

self.run() 

return [self.output.relabeling[i] for i from 0 <= i < self.degree] 

  

def is_isomorphic(self, LinearBinaryCodeStruct other): 

""" 

Calculate whether self is isomorphic to other. 

  

EXAMPLES: 

sage: from sage.groups.perm_gps.partn_ref.refinement_binary import LinearBinaryCodeStruct 

  

sage: B = LinearBinaryCodeStruct(Matrix(GF(2), [[1,1,1,1,0,0],[0,0,1,1,1,1]])) 

sage: C = LinearBinaryCodeStruct(Matrix(GF(2), [[1,1,1,0,0,1],[1,1,0,1,1,0]])) 

sage: B.is_isomorphic(C) 

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

  

""" 

cdef int i, n = self.degree 

cdef int *output 

cdef int *ordering 

cdef PartitionStack *part 

part = PS_new(n, 1) 

ordering = <int *> sig_malloc(self.degree * sizeof(int)) 

output = <int *> sig_malloc(self.degree * 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] = i 

self.first_time = 1 

other.first_time = 1 

  

cdef bint isomorphic = double_coset(<void *> self, <void *> other, part, ordering, n, &all_children_are_equivalent, &refine_by_bip_degree, &compare_linear_codes, 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 

  

def __dealloc__(self): 

cdef int j 

bitset_free(self.alpha_is_wd) 

for j from 0 <= j < 2*self.dimension+2: 

bitset_free(&self.scratch_bitsets[j]) 

for j from 0 <= j < self.dimension: 

bitset_free(&self.basis[j]) 

sig_free(self.basis); sig_free(self.scratch_bitsets) 

sig_free(self.alpha_is_wd); PS_dealloc(self.word_ps) 

sig_free(self.alpha); sig_free(self.scratch) 

if self.output is not NULL: 

deallocate_agcl_output(self.output) 

  

cdef int ith_word_linear(BinaryCodeStruct self, int i, bitset_s *word): 

cdef LinearBinaryCodeStruct LBCS = <LinearBinaryCodeStruct> self 

cdef int j 

bitset_zero(word) 

for j from 0 <= j < LBCS.dimension: 

if (1<<j)&i: 

bitset_xor(word, word, &LBCS.basis[j]) 

return 0 

  

cdef class NonlinearBinaryCodeStruct(BinaryCodeStruct): 

  

def __cinit__(self, arg): 

cdef int i,j 

if is_Matrix(arg): 

self.degree = arg.ncols() 

self.nwords = arg.nrows() 

elif isinstance(arg, tuple): 

assert len(arg) == 2 

self.degree, self.nwords = arg 

else: 

raise NotImplementedError 

  

self.words = <bitset_s *> sig_malloc(self.nwords * sizeof(bitset_s)) 

self.scratch_bitsets = <bitset_s *> sig_malloc((4*self.nwords+1) * sizeof(bitset_s)) 

self.alpha_is_wd = <bitset_s *> sig_malloc(sizeof(bitset_s)) 

self.word_ps = PS_new(self.nwords, 1) 

self.alpha = <int *> sig_malloc((self.nwords+self.degree) * sizeof(int)) 

self.scratch = <int *> sig_malloc((3*self.nwords+3*self.degree+2) * sizeof(int)) 

if self.words is NULL or self.scratch_bitsets is NULL \ 

or self.alpha_is_wd is NULL or self.word_ps is NULL \ 

or self.alpha is NULL or self.scratch is NULL: 

sig_free(self.words) 

sig_free(self.scratch_bitsets) 

sig_free(self.alpha_is_wd) 

PS_dealloc(self.word_ps) 

sig_free(self.alpha) 

sig_free(self.scratch) 

raise MemoryError 

  

cdef bint memerr = 0 

for i from 0 <= i < self.nwords: 

try: bitset_init(&self.words[i], self.degree) 

except MemoryError: 

for j from 0 <= j < i: 

bitset_free(&self.words[j]) 

memerr = 1 

break 

if not memerr: 

for i from 0 <= i < 4*self.nwords: 

try: bitset_init(&self.scratch_bitsets[i], self.degree) 

except MemoryError: 

for j from 0 <= j < i: 

bitset_free(&self.scratch_bitsets[j]) 

for j from 0 <= j < self.nwords: 

bitset_free(&self.words[j]) 

memerr = 1 

break 

if not memerr: 

try: bitset_init(&self.scratch_bitsets[4*self.nwords], self.nwords) 

except MemoryError: 

for j from 0 <= j < 4*self.nwords: 

bitset_free(&self.scratch_bitsets[j]) 

for j from 0 <= j < self.nwords: 

bitset_free(&self.words[j]) 

memerr = 1 

if not memerr: 

try: bitset_init(self.alpha_is_wd, self.nwords + self.degree) 

except MemoryError: 

for j from 0 <= j < 4*self.nwords + 1: 

bitset_free(&self.scratch_bitsets[j]) 

for j from 0 <= j < self.nwords: 

bitset_free(&self.words[j]) 

memerr = 1 

if memerr: 

sig_free(self.words); sig_free(self.scratch_bitsets) 

sig_free(self.alpha_is_wd); PS_dealloc(self.word_ps) 

sig_free(self.alpha); sig_free(self.scratch) 

raise MemoryError 

else: 

bitset_zero(self.alpha_is_wd) 

for j from 0 <= j < self.nwords: 

bitset_zero(&self.words[j]) 

  

if is_Matrix(arg): 

for i,j in arg.nonzero_positions(): 

bitset_set(&self.words[i], j) 

  

self.output = NULL 

self.ith_word = &ith_word_nonlinear 

  

def __dealloc__(self): 

cdef int j 

bitset_free(self.alpha_is_wd) 

for j from 0 <= j < 4*self.nwords + 1: 

bitset_free(&self.scratch_bitsets[j]) 

for j from 0 <= j < self.nwords: 

bitset_free(&self.words[j]) 

sig_free(self.words); sig_free(self.scratch_bitsets) 

sig_free(self.alpha_is_wd); PS_dealloc(self.word_ps) 

sig_free(self.alpha); sig_free(self.scratch) 

if self.output is not NULL: 

deallocate_agcl_output(self.output) 

  

def run(self, partition=None): 

""" 

Perform the canonical labeling and automorphism group computation, 

storing results to self. 

  

INPUT: 

partition -- an optional list of lists partition of the columns. 

default is the unit partition. 

  

EXAMPLES: 

sage: from sage.groups.perm_gps.partn_ref.refinement_binary import NonlinearBinaryCodeStruct 

  

sage: B = NonlinearBinaryCodeStruct(Matrix(GF(2), [[1,0,0,0],[0,0,1,0]])) 

sage: B.run() 

sage: B.automorphism_group() 

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

sage: B.canonical_relabeling() 

[2, 0, 3, 1] 

  

sage: B = NonlinearBinaryCodeStruct(Matrix(GF(2), [[1,1,1,0],[1,1,0,1],[1,0,1,1],[0,1,1,1]])) 

sage: B.run() 

sage: B.automorphism_group() 

([[0, 1, 3, 2], [0, 2, 1, 3], [1, 0, 2, 3]], 24, [0, 1, 2]) 

sage: B.canonical_relabeling() 

[0, 1, 2, 3] 

  

sage: B = NonlinearBinaryCodeStruct(Matrix(GF(2), [[1,1,1,0,0,0],[1,1,0,1,0,0],[1,0,1,1,0,0],[0,1,1,1,0,0],[0,0,0,0,1,0],[0,0,0,0,0,1]])) 

sage: B.run() 

sage: B.automorphism_group() 

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

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

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

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

48, 

[4, 0, 1, 2]) 

sage: B.canonical_relabeling() 

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

  

""" 

cdef int n = self.degree 

cdef PartitionStack *part 

if partition is None: 

part = PS_new(n, 1) 

else: 

part = PS_from_list(partition) 

if part is NULL: 

raise MemoryError 

self.first_time = 1 

  

self.output = get_aut_gp_and_can_lab(<void *> self, part, self.degree, &all_children_are_equivalent, &refine_by_bip_degree, &compare_nonlinear_codes, 1, NULL, NULL, NULL) 

  

PS_dealloc(part) 

  

def automorphism_group(self): 

""" 

Returns a list of generators of the automorphism group, along with its 

order and a base for which the list of generators is a strong generating 

set. 

  

EXAMPLES: (For more examples, see self.run()) 

sage: from sage.groups.perm_gps.partn_ref.refinement_binary import NonlinearBinaryCodeStruct 

  

sage: B = NonlinearBinaryCodeStruct(Matrix(GF(2), [[1,1,1,0,0,0],[1,1,0,1,0,0],[1,0,1,1,0,0],[0,1,1,1,0,0],[0,0,0,0,1,0],[0,0,0,0,0,1]])) 

sage: B.run() 

sage: B.automorphism_group() 

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

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

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

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

48, 

[4, 0, 1, 2]) 

  

""" 

cdef int i, j 

cdef list generators, base 

cdef Integer order 

if self.output is NULL: 

self.run() 

generators = [] 

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

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

order = Integer() 

SC_order(self.output.group, 0, order.value) 

base = [self.output.group.base_orbits[i][0] for i from 0 <= i < self.output.group.base_size] 

return generators, order, base 

  

def canonical_relabeling(self): 

""" 

Returns a canonical relabeling (in list permutation format). 

  

EXAMPLES: (For more examples, see self.run()) 

sage: from sage.groups.perm_gps.partn_ref.refinement_binary import NonlinearBinaryCodeStruct 

  

sage: B = NonlinearBinaryCodeStruct(Matrix(GF(2), [[1,1,1,0,0,0],[1,1,0,1,0,0],[1,0,1,1,0,0],[0,1,1,1,0,0],[0,0,0,0,1,0],[0,0,0,0,0,1]])) 

sage: B.run() 

sage: B.canonical_relabeling() 

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

  

""" 

cdef int i 

if self.output is NULL: 

self.run() 

return [self.output.relabeling[i] for i from 0 <= i < self.degree] 

  

def is_isomorphic(self, NonlinearBinaryCodeStruct other): 

""" 

Calculate whether self is isomorphic to other. 

  

EXAMPLES: 

sage: from sage.groups.perm_gps.partn_ref.refinement_binary import NonlinearBinaryCodeStruct 

  

sage: B = NonlinearBinaryCodeStruct(Matrix(GF(2), [[1,1,1,1,0,0],[0,0,1,1,1,1]])) 

sage: C = NonlinearBinaryCodeStruct(Matrix(GF(2), [[1,1,0,0,1,1],[1,1,1,1,0,0]])) 

sage: B.is_isomorphic(C) 

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

  

""" 

cdef int i, n = self.degree 

cdef int *output 

cdef int *ordering 

cdef PartitionStack *part 

part = PS_new(n, 1) 

ordering = <int *> sig_malloc(n * sizeof(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] = i 

self.first_time = 1 

other.first_time = 1 

  

cdef bint isomorphic = double_coset(<void *> self, <void *> other, part, ordering, n, &all_children_are_equivalent, &refine_by_bip_degree, &compare_nonlinear_codes, 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 

  

cdef int ith_word_nonlinear(BinaryCodeStruct self, int i, bitset_s *word): 

cdef NonlinearBinaryCodeStruct NBCS = <NonlinearBinaryCodeStruct> self 

bitset_copy(word, &NBCS.words[i]) 

return 0 

  

cdef int refine_by_bip_degree(PartitionStack *col_ps, void *S, int *cells_to_refine_by, int ctrb_len): 

""" 

Refines the input partition by checking degrees of vertices to the given 

cells in the associated bipartite graph (vertices split into columns and 

words). 

  

INPUT: 

col_ps -- a partition stack, whose finest partition is the partition to be 

refined. 

S -- a binary code struct object 

cells_to_refine_by -- a list of pointers to cells to check degrees against 

in refining the other cells (updated in place) 

ctrb_len -- how many cells in cells_to_refine_by 

  

OUTPUT: 

  

An integer $I$ invariant under the orbits of $S_n$. That is, if $\gamma$ is a 

permutation of the columns, then 

$$ I(G, PS, cells_to_refine_by) = I( \gamma(G), \gamma(PS), \gamma(cells_to_refine_by) ) .$$ 

  

""" 

cdef BinaryCodeStruct BCS = <BinaryCodeStruct> S 

cdef int current_cell_against = 0 

cdef int current_cell, i, r, j 

cdef int first_largest_subcell 

cdef int invariant = 0 

cdef PartitionStack *word_ps = BCS.word_ps 

cdef int *ctrb = BCS.alpha 

cdef bitset_s *ctrb_is_wd = BCS.alpha_is_wd 

  

word_ps.depth = col_ps.depth 

PS_clear(word_ps) 

bitset_zero(ctrb_is_wd) 

memcpy(ctrb, cells_to_refine_by, ctrb_len * sizeof(int)) 

if BCS.first_time: 

BCS.first_time = 0 

ctrb[ctrb_len] = 0 

bitset_set(ctrb_is_wd, ctrb_len) 

ctrb_len += 1 

cdef int *col_degrees = BCS.scratch # len degree 

cdef int *col_counts = &BCS.scratch[BCS.degree] # len nwords+1 

cdef int *col_output = &BCS.scratch[BCS.degree + BCS.nwords + 1] # len degree 

cdef int *word_degrees = &BCS.scratch[2*BCS.degree + BCS.nwords + 1] # len nwords 

cdef int *word_counts = &BCS.scratch[2*BCS.degree + 2*BCS.nwords + 1] # len degree+1 

cdef int *word_output = &BCS.scratch[3*BCS.degree + 2*BCS.nwords + 2] # len nwords 

cdef bint necessary_to_split_cell 

cdef int against_index 

while not (PS_is_discrete(col_ps) and PS_is_discrete(word_ps)) and current_cell_against < ctrb_len: 

invariant += 1 

current_cell = 0 

if bitset_check(ctrb_is_wd, current_cell_against): 

while current_cell < col_ps.degree: 

invariant += 8 

i = current_cell 

necessary_to_split_cell = 0 

while True: 

col_degrees[i-current_cell] = col_degree(col_ps, BCS, i, ctrb[current_cell_against], word_ps) 

if col_degrees[i-current_cell] != col_degrees[0]: 

necessary_to_split_cell = 1 

i += 1 

if col_ps.levels[i-1] <= col_ps.depth: 

break 

# now, i points to the next cell (before refinement) 

if necessary_to_split_cell: 

invariant += 8 

first_largest_subcell = sort_by_function_codes(col_ps, current_cell, col_degrees, col_counts, col_output, BCS.nwords+1) 

invariant += col_degree(col_ps, BCS, i-1, ctrb[current_cell_against], word_ps) 

invariant += first_largest_subcell 

against_index = current_cell_against 

while against_index < ctrb_len: 

if ctrb[against_index] == current_cell and not bitset_check(ctrb_is_wd, against_index): 

ctrb[against_index] = first_largest_subcell 

break 

against_index += 1 

r = current_cell 

while True: 

if r == current_cell or col_ps.levels[r-1] == col_ps.depth: 

if r != first_largest_subcell: 

ctrb[ctrb_len] = r 

ctrb_len += 1 

r += 1 

if r >= i: 

break 

invariant += (i - current_cell) 

current_cell = i 

else: 

while current_cell < word_ps.degree: 

invariant += 64 

i = current_cell 

necessary_to_split_cell = 0 

while True: 

word_degrees[i-current_cell] = word_degree(word_ps, BCS, i, ctrb[current_cell_against], col_ps) 

if word_degrees[i-current_cell] != word_degrees[0]: 

necessary_to_split_cell = 1 

i += 1 

if word_ps.levels[i-1] <= col_ps.depth: 

break 

# now, i points to the next cell (before refinement) 

if necessary_to_split_cell: 

invariant += 64 

first_largest_subcell = sort_by_function_codes(word_ps, current_cell, word_degrees, word_counts, word_output, BCS.degree+1) 

invariant += word_degree(word_ps, BCS, i-1, ctrb[current_cell_against], col_ps) 

invariant += first_largest_subcell 

against_index = current_cell_against 

while against_index < ctrb_len: 

if ctrb[against_index] == current_cell and bitset_check(ctrb_is_wd, against_index): 

ctrb[against_index] = first_largest_subcell 

break 

against_index += 1 

r = current_cell 

while True: 

if r == current_cell or word_ps.levels[r-1] == col_ps.depth: 

if r != first_largest_subcell: 

ctrb[ctrb_len] = r 

bitset_set(ctrb_is_wd, ctrb_len) 

ctrb_len += 1 

r += 1 

if r >= i: 

break 

invariant += (i - current_cell) 

current_cell = i 

current_cell_against += 1 

return invariant 

  

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

""" 

Compare gamma_1(S1) and gamma_2(S2). 

  

Return return -1 if gamma_1(S1) < gamma_2(S2), 0 if gamma_1(S1) == gamma_2(S2), 

1 if gamma_1(S1) > gamma_2(S2). (Just like the python \code{cmp}) function. 

  

Abstractly, what this function does is relabel the basis of B by gamma_1 and 

gamma_2, run a row reduction on each, and verify that the matrices are the 

same, which holds if and only if the rowspan is the same. In practice, if 

the codes are not equal, the reductions (which are carried out in an 

interleaved way) will terminate as soon as this is discovered, and whichever 

code has a 1 in the entry in which they differ is reported as larger. 

  

INPUT: 

gamma_1, gamma_2 -- list permutations (inverse) 

S1, S2 -- binary code struct objects 

  

""" 

cdef int i, piv_loc_1, piv_loc_2, cur_col, cur_row=0 

cdef bint is_pivot_1, is_pivot_2 

cdef LinearBinaryCodeStruct BCS1 = <LinearBinaryCodeStruct> S1 

cdef LinearBinaryCodeStruct BCS2 = <LinearBinaryCodeStruct> S2 

cdef bitset_s *basis_1 = BCS1.scratch_bitsets # len = dim 

cdef bitset_s *basis_2 = &BCS1.scratch_bitsets[BCS1.dimension] # len = dim 

cdef bitset_s *pivots = &BCS1.scratch_bitsets[2*BCS1.dimension] # len 1 

cdef bitset_s *temp = &BCS1.scratch_bitsets[2*BCS1.dimension+1] # len 1 

for i from 0 <= i < BCS1.dimension: 

bitset_copy(&basis_1[i], &BCS1.basis[i]) 

bitset_copy(&basis_2[i], &BCS2.basis[i]) 

bitset_zero(pivots) 

for cur_col from 0 <= cur_col < BCS1.degree: 

is_pivot_1 = 0 

is_pivot_2 = 0 

for i from cur_row <= i < BCS1.dimension: 

if bitset_check(&basis_1[i], gamma_1[cur_col]): 

is_pivot_1 = 1 

piv_loc_1 = i 

break 

for i from cur_row <= i < BCS1.dimension: 

if bitset_check(&basis_2[i], gamma_2[cur_col]): 

is_pivot_2 = 1 

piv_loc_2 = i 

break 

if is_pivot_1 != is_pivot_2: 

return <int>is_pivot_2 - <int>is_pivot_1 

if is_pivot_1: 

bitset_set(pivots, cur_col) 

if piv_loc_1 != cur_row: 

bitset_copy(temp, &basis_1[piv_loc_1]) 

bitset_copy(&basis_1[piv_loc_1], &basis_1[cur_row]) 

bitset_copy(&basis_1[cur_row], temp) 

if piv_loc_2 != cur_row: 

bitset_copy(temp, &basis_2[piv_loc_2]) 

bitset_copy(&basis_2[piv_loc_2], &basis_2[cur_row]) 

bitset_copy(&basis_2[cur_row], temp) 

for i from 0 <= i < cur_row: 

if bitset_check(&basis_1[i], gamma_1[cur_col]): 

bitset_xor(&basis_1[i], &basis_1[i], &basis_1[cur_row]) 

if bitset_check(&basis_2[i], gamma_2[cur_col]): 

bitset_xor(&basis_2[i], &basis_2[i], &basis_2[cur_row]) 

for i from cur_row < i < BCS1.dimension: 

if bitset_check(&basis_1[i], gamma_1[cur_col]): 

bitset_xor(&basis_1[i], &basis_1[i], &basis_1[cur_row]) 

if bitset_check(&basis_2[i], gamma_2[cur_col]): 

bitset_xor(&basis_2[i], &basis_2[i], &basis_2[cur_row]) 

cur_row += 1 

else: 

for i from 0 <= i < cur_row: 

if bitset_check(&basis_1[i], gamma_1[cur_col]) != bitset_check(&basis_2[i], gamma_2[cur_col]): 

return <int>bitset_check(&basis_2[i], gamma_2[cur_col]) - <int>bitset_check(&basis_1[i], gamma_1[cur_col]) 

return 0 

  

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

""" 

Compare gamma_1(S1) and gamma_2(S2). 

  

Return return -1 if gamma_1(S1) < gamma_2(S2), 0 if gamma_1(S1) == gamma_2(S2), 

1 if gamma_1(S1) > gamma_2(S2). (Just like the python \code{cmp}) function. 

  

INPUT: 

gamma_1, gamma_2 -- list permutations (inverse) 

S1, S2 -- a binary code struct object 

  

""" 

cdef int side=0, i, start, end, n_one_1, n_one_2, cur_col 

cdef int where_0, where_1 

cdef NonlinearBinaryCodeStruct BCS1 = <NonlinearBinaryCodeStruct> S1 

cdef NonlinearBinaryCodeStruct BCS2 = <NonlinearBinaryCodeStruct> S2 

cdef bitset_s *B_1_0 = BCS1.scratch_bitsets # nwords of len degree 

cdef bitset_s *B_1_1 = &BCS1.scratch_bitsets[BCS1.nwords] # nwords of len degree 

cdef bitset_s *B_2_0 = &BCS1.scratch_bitsets[2*BCS1.nwords] # nwords of len degree 

cdef bitset_s *B_2_1 = &BCS1.scratch_bitsets[3*BCS1.nwords] # nwords of len degree 

cdef bitset_s *dividers = &BCS1.scratch_bitsets[4*BCS1.nwords] # 1 of len nwords 

cdef bitset_s *B_1_this 

cdef bitset_s *B_1_other 

cdef bitset_s *B_2_this 

cdef bitset_s *B_2_other 

for i from 0 <= i < BCS1.nwords: 

bitset_copy(&B_1_0[i], &BCS1.words[i]) 

bitset_copy(&B_2_0[i], &BCS2.words[i]) 

bitset_zero(dividers) 

bitset_set(dividers, BCS1.nwords-1) 

  

for cur_col from 0 <= cur_col < BCS1.degree: 

if side == 0: 

B_1_this = B_1_0 

B_1_other = B_1_1 

B_2_this = B_2_0 

B_2_other = B_2_1 

else: 

B_1_this = B_1_1 

B_1_other = B_1_0 

B_2_this = B_2_1 

B_2_other = B_2_0 

side ^= 1 

start = 0 

while start < BCS1.nwords: 

end = start 

while not bitset_check(dividers, end): 

end += 1 

end += 1 

n_one_1 = 0 

n_one_2 = 0 

for i from start <= i < end: 

n_one_1 += bitset_check(&B_1_this[i], gamma_1[cur_col]) 

n_one_2 += bitset_check(&B_2_this[i], gamma_2[cur_col]) 

if n_one_1 != n_one_2: 

if n_one_1 > n_one_2: 

return 1 

else: 

return -1 

where_0 = start 

where_1 = end - n_one_1 

if start < where_1 and where_1 < end: 

bitset_set(dividers, where_1 - 1) 

for i from start <= i < end: 

if bitset_check(&B_1_this[i], gamma_1[cur_col]): 

bitset_copy(&B_1_other[where_1], &B_1_this[i]) 

where_1 += 1 

else: 

bitset_copy(&B_1_other[where_0], &B_1_this[i]) 

where_0 += 1 

where_0 = start 

where_1 = end - n_one_2 

for i from start <= i < end: 

if bitset_check(&B_2_this[i], gamma_2[cur_col]): 

bitset_copy(&B_2_other[where_1], &B_2_this[i]) 

where_1 += 1 

else: 

bitset_copy(&B_2_other[where_0], &B_2_this[i]) 

where_0 += 1 

start = end 

  

return 0 

  

cdef bint all_children_are_equivalent(PartitionStack *col_ps, void *S): 

""" 

Returns True if any refinement of the current partition results in the same 

structure. 

  

WARNING: 

Converse does not hold in general! See Lemma 2.25 of [1] for details, noting 

that the binary code is interpreted as a bipartite graph (see module docs 

for details). 

  

INPUT: 

col_ps -- the partition stack to be checked 

S -- a binary code struct object 

""" 

cdef BinaryCodeStruct BCS = <BinaryCodeStruct> S 

cdef PartitionStack *word_ps = BCS.word_ps 

cdef int i, n = col_ps.degree + BCS.nwords 

cdef bint in_cell = 0 

cdef int nontrivial_cells = 0 

cdef int total_cells = PS_num_cells(col_ps) + PS_num_cells(word_ps) 

if n <= total_cells + 4: 

return 1 

for i from 0 <= i < BCS.nwords: 

if word_ps.levels[i] <= col_ps.depth: 

if in_cell: 

nontrivial_cells += 1 

in_cell = 0 

else: 

in_cell = 1 

in_cell = 0 

for i from 0 <= i < BCS.degree: 

if col_ps.levels[i] <= col_ps.depth: 

if in_cell: 

nontrivial_cells += 1 

in_cell = 0 

else: 

in_cell = 1 

if n == total_cells + nontrivial_cells: 

return 1 

if n == total_cells + nontrivial_cells + 1: 

return 1 

return 0 

  

cdef inline int word_degree(PartitionStack *word_ps, BinaryCodeStruct BCS, int entry, int cell_index, PartitionStack *col_ps): 

""" 

Returns the number of edges from the vertex corresponding to entry to 

vertices in the cell corresponding to cell_index. 

  

INPUT: 

word_ps -- the partition stack to be checked 

col_ps -- corresponding partition stack on columns 

BCS -- a binary code struct object 

entry -- the position of the vertex in question in the entries of word_ps 

cell_index -- the starting position of the cell in question in the entries 

of PS 

""" 

cdef bitset_t cell, word 

cdef int i, h 

bitset_init(cell, BCS.degree) 

bitset_zero(cell) 

bitset_init(word, BCS.degree) 

entry = word_ps.entries[entry] 

bitset_set(cell, col_ps.entries[cell_index]) 

while col_ps.levels[cell_index] > col_ps.depth: 

cell_index += 1 

bitset_set(cell, col_ps.entries[cell_index]) 

BCS.ith_word(BCS, entry, word) 

bitset_and(cell, word, cell) 

h = bitset_hamming_weight(cell) 

bitset_free(cell) 

bitset_free(word) 

return h 

  

cdef inline int col_degree(PartitionStack *col_ps, BinaryCodeStruct BCS, int entry, int cell_index, PartitionStack *word_ps): 

""" 

Returns the number of edges from the vertex corresponding to entry to 

vertices in the cell corresponding to cell_index. 

  

INPUT: 

col_ps -- the partition stack to be checked 

word_ps -- corresponding partition stack on words 

BCS -- a binary code struct object 

entry -- the position of the vertex in question in the entries of word_ps 

cell_index -- the starting position of the cell in question in the entries 

of PS 

""" 

cdef bitset_t word 

bitset_init(word, BCS.degree) 

cdef int degree = 0, word_basis, i, b 

entry = col_ps.entries[entry] 

while True: 

BCS.ith_word(BCS, word_ps.entries[cell_index], word) 

degree += bitset_check(word, entry) 

if not word_ps.levels[cell_index] > col_ps.depth: 

break 

cell_index += 1 

bitset_free(word) 

return degree 

  

cdef inline int sort_by_function_codes(PartitionStack *PS, int start, int *degrees, int *counts, int *output, int count_max): 

""" 

A simple counting sort, given the degrees of vertices to a certain cell. 

  

INPUT: 

PS -- the partition stack to be checked 

start -- beginning index of the cell to be sorted 

degrees -- the values to be sorted by 

count, count_max, output -- scratch space 

  

""" 

cdef int n = PS.degree 

cdef int i, j, max, max_location 

for j from 0 <= j < count_max: 

counts[j] = 0 

i = 0 

while PS.levels[i+start] > PS.depth: 

counts[degrees[i]] += 1 

i += 1 

counts[degrees[i]] += 1 

# i+start is the right endpoint of the cell now 

max = counts[0] 

max_location = 0 

for j from 0 < j < count_max: 

if counts[j] > max: 

max = counts[j] 

max_location = j 

counts[j] += counts[j - 1] 

for j from i >= j >= 0: 

counts[degrees[j]] -= 1 

output[counts[degrees[j]]] = PS.entries[start+j] 

max_location = counts[max_location]+start 

for j from 0 <= j <= i: 

PS.entries[start+j] = output[j] 

j = 1 

while j < count_max and counts[j] <= i: 

if counts[j] > 0: 

PS.levels[start + counts[j] - 1] = PS.depth 

PS_move_min_to_front(PS, start + counts[j-1], start + counts[j] - 1) 

j += 1 

return max_location 

  

  

def random_tests(num=50, n_max=50, k_max=6, nwords_max=200, perms_per_code=10, density_range=(.1,.9)): 

""" 

Tests to make sure that C(gamma(B)) == C(B) for random permutations gamma 

and random codes B, and that is_isomorphic returns an isomorphism. 

  

INPUT: 

num -- run tests for this many codes 

n_max -- test codes with at most this many columns 

k_max -- test codes with at most this for dimension 

perms_per_code -- test each code with this many random permutations 

  

DISCUSSION: 

  

This code generates num random linear codes B on at most n_max columns with 

dimension at most k_max, and a random nonlinear code B2 on at most n_max 

columns with number of words at most nwords_max. The density of entries in 

the basis is chosen randomly between 0 and 1. 

  

For each code B (B2) generated, we uniformly generate perms_per_code random 

permutations and verify that the canonical labels of B and the image of B 

under the generated permutation are equal, and check that the double coset 

function returns an isomorphism. 

  

TESTS:: 

  

sage: import sage.groups.perm_gps.partn_ref.refinement_binary 

sage: sage.groups.perm_gps.partn_ref.refinement_binary.random_tests() # long time (up to 5s on sage.math, 2012) 

All passed: ... random tests on ... codes. 

  

""" 

from sage.misc.misc import walltime 

from sage.misc.prandom import random, randint 

from sage.combinat.permutation import Permutations 

from sage.matrix.constructor import random_matrix, matrix 

from sage.rings.finite_rings.finite_field_constructor import FiniteField as GF 

cdef int h, i, j, n, k, num_tests = 0, num_codes = 0 

cdef LinearBinaryCodeStruct B, C 

cdef NonlinearBinaryCodeStruct B_n, C_n 

for mmm in range(num): 

p = random()*(density_range[1]-density_range[0]) + density_range[0] 

n = randint(2, n_max) 

k = randint(1, min(n-1,k_max) ) 

nwords = randint(1, min(n-1,nwords_max) ) 

S = Permutations(n) 

  

M = random_matrix(GF(2), k, n, sparse=False, density=p).row_space().basis_matrix() 

M_n = random_matrix(GF(2), nwords, n, sparse=False, density=p) 

B = LinearBinaryCodeStruct( M ) 

B_n = NonlinearBinaryCodeStruct( M_n ) 

B.run() 

B_n.run() 

  

for i from 0 <= i < perms_per_code: 

perm = [a-1 for a in list(S.random_element())] 

C = LinearBinaryCodeStruct( matrix(GF(2), B.dimension, B.degree) ) 

C_n = NonlinearBinaryCodeStruct( matrix(GF(2), B_n.nwords, B_n.degree) ) 

for j from 0 <= j < B.dimension: 

for h from 0 <= h < B.degree: 

bitset_set_to(&C.basis[j], perm[h], bitset_check(&B.basis[j], h)) 

for j from 0 <= j < B_n.nwords: 

for h from 0 <= h < B_n.degree: 

bitset_set_to(&C_n.words[j], perm[h], bitset_check(&B_n.words[j], h)) 

# now C is a random permutation of B, and C_n of B_n 

C.run() 

C_n.run() 

B_relab = B.canonical_relabeling() 

C_relab = C.canonical_relabeling() 

B_n_relab = B_n.canonical_relabeling() 

C_n_relab = C_n.canonical_relabeling() 

B_M = matrix(GF(2), B.dimension, B.degree) 

C_M = matrix(GF(2), B.dimension, B.degree) 

B_n_M = matrix(GF(2), B_n.nwords, B_n.degree) 

C_n_M = matrix(GF(2), B_n.nwords, B_n.degree) 

for j from 0 <= j < B.dimension: 

for h from 0 <= h < B.degree: 

B_M[j,B_relab[h]] = bitset_check(&B.basis[j], h) 

C_M[j,C_relab[h]] = bitset_check(&C.basis[j], h) 

for j from 0 <= j < B_n.nwords: 

for h from 0 <= h < B_n.degree: 

B_n_M[j,B_n_relab[h]] = bitset_check(&B_n.words[j], h) 

C_n_M[j,C_n_relab[h]] = bitset_check(&C_n.words[j], h) 

if B_M.row_space() != C_M.row_space(): 

print("can_lab error -- B:") 

for j from 0 <= j < B.dimension: 

print(bitset_string(&B.basis[j])) 

print(perm) 

return 

if sorted(B_n_M.rows()) != sorted(C_n_M.rows()): 

print("can_lab error -- B_n:") 

for j from 0 <= j < B_n.nwords: 

print(bitset_string(&B_n.words[j])) 

print(perm) 

return 

isom = B.is_isomorphic(C) 

if not isom: 

print("isom -- B:") 

for j from 0 <= j < B.dimension: 

print(bitset_string(&B.basis[j])) 

print(perm) 

print(isom) 

return 

isom = B_n.is_isomorphic(C_n) 

if not isom: 

print("isom -- B_n:") 

for j from 0 <= j < B_n.nwords: 

print(bitset_string(&B_n.words[j])) 

print(perm) 

print(isom) 

return 

  

num_tests += 4*perms_per_code 

num_codes += 2 

  

print("All passed: %d random tests on %d codes." % (num_tests, num_codes))