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

r""" 

Reed-Muller code 

 

Given integers `m, r` and a finite field `F`, 

the corresponding Reed-Muller Code is the set: 

 

.. MATH:: 

 

\{ (f(\alpha_i)\mid \alpha_i \in F^m) \mid f \in F[x_1,x_2,\ldots,x_m], \deg f \leq r \} 

 

This file contains the following elements: 

 

- :class:`QAryReedMullerCode`, the class for Reed-Muller codes over non-binary field of size q and `r<q` 

- :class:`BinaryReedMullerCode`, the class for Reed-Muller codes over binary field and `r<=m` 

- :class:`ReedMullerVectorEncoder`, an encoder with a vectorial message space (for both the two code classes) 

- :class:`ReedMullerPolynomialEncoder`, an encoder with a polynomial message space (for both the code classes) 

""" 

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

# Copyright (C) 2016 Parthasarathi Panda <parthasarathipanda314@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 six.moves import range 

 

from operator import mul 

from sage.matrix.constructor import matrix 

from sage.functions.other import binomial 

from sage.calculus.var import var 

from sage.misc.functional import symbolic_sum 

from sage.coding.linear_code import AbstractLinearCode, LinearCodeSyndromeDecoder 

from sage.coding.encoder import Encoder 

from sage.combinat.subset import Subsets 

from sage.combinat.tuple import Tuples 

from sage.categories.finite_fields import FiniteFields 

from sage.rings.finite_rings.finite_field_constructor import GF 

from sage.rings.integer import Integer 

from sage.modules.free_module_element import vector 

from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing 

from sage.interfaces.gap import gfq_gap_to_sage 

from sage.interfaces.all import gap 

from sage.misc.cachefunc import cached_method 

from functools import reduce 

 

 

def _binomial_sum(n, k): 

r""" 

Returns the sum of all binomials `\binom{n}{i}`, 

with `i` ranging from `0` to `k` and including `k`. 

 

INPUT: 

 

- ``n, k`` - integers 

 

EXAMPLES:: 

 

sage: from sage.coding.reed_muller_code import _binomial_sum 

sage: _binomial_sum(4, 2) 

11 

""" 

s = 1 

nCi = 1 

for i in range(k): 

nCi = ((n - i) * nCi) // (i + 1) 

s = nCi + s 

return s 

 

 

def _multivariate_polynomial_interpolation(evaluation, order, polynomial_ring): 

r""" 

Returns `f \in \GF{q}[X_1,...,X_m]` such that `f(\mathbf a) = v[i(\mathbf a)]` 

for all `\mathbf a \in \GF{q^m}`, where `v \in \GF{q}^{q^m}` is a given 

vector of evaluations, and `i(a)` is a specific ordering of `\GF{q^m}` (see below for details) 

 

The ordering `i(a)` is the one used by Sage when listing the elements 

of a Finite Field with a call to the method ``list``. 

 

In case the polynomial `f` does not exist, this method returns an arbitrary polynomial. 

 

INPUT: 

 

- ``evaluation`` -- A vector or a list of evaluation of the polynomial at all the points. 

 

- ``num_of_var`` -- The number of variables used in the polynomial to interpolate 

 

- ``order`` -- The degree of the polynomial to interpolate 

 

- ``polynomial_ring`` -- The Polynomial Ring the polynomial in question is from 

 

EXAMPLES:: 

 

sage: from sage.coding.reed_muller_code import _multivariate_polynomial_interpolation 

sage: F = GF(3) 

sage: R.<x,y> = F[] 

sage: v = vector(F, [1, 2, 0, 0, 2, 1, 1, 1, 1]) 

sage: _multivariate_polynomial_interpolation(v, 2, R) 

x*y + y^2 + x + y + 1 

 

If there does not exist 

""" 

def _interpolate(evaluation, num_of_var, order): 

if num_of_var == 0 or order == 0: 

return evaluation[0] 

base_field = polynomial_ring.base_ring() 

q = base_field.cardinality() 

n_by_q = q**(num_of_var - 1) 

d = min(order + 1, q) 

multipoint_evaluation_list = [] 

uni_poly_ring = PolynomialRing(base_field, 'x') 

base_field_zero = base_field.zero() 

for k in range(n_by_q): 

iterator = iter(base_field) 

points = [] 

for i in range(d): 

xcoordinate = next(iterator) 

points.append((xcoordinate, evaluation[k + i * n_by_q])) 

polyVector = uni_poly_ring.lagrange_polynomial( 

points).coefficients(sparse=False) 

if len(polyVector) < d: 

# adding zeros to represent a (d-1) degree polynomial 

polyVector += [base_field_zero] * (d - len(polyVector)) 

multipoint_evaluation_list.append(polyVector) 

poly = polynomial_ring.zero() 

z = 1 

x = polynomial_ring.gen(num_of_var - 1) 

for k in range(d): # computing the polynomial 

poly = poly + z * _interpolate([multipoint_evaluation_list[i][k] 

for i in range(n_by_q)], num_of_var - 1, order - k) 

z *= x 

return poly 

return _interpolate(evaluation, polynomial_ring.ngens(), order) 

 

 

def ReedMullerCode(base_field, order, num_of_var): 

r""" 

Returns a Reed-Muller code. 

 

A Reed-Muller Code of order `r` and number of variables `m` over a finite field `F` is the set: 

 

.. MATH:: 

 

\{ (f(\alpha_i)\mid \alpha_i \in F^m) \mid f \in F[x_1,x_2,\ldots,x_m], \deg f \leq r \} 

 

INPUT: 

 

- ``base_field`` -- The finite field `F` over which the code is built. 

 

- ``order`` -- The order of the Reed-Muller Code, which is the maximum 

degree of the polynomial to be used in the code. 

 

- ``num_of_var`` -- The number of variables used in polynomial. 

 

.. WARNING:: 

 

For now, this implementation only supports Reed-Muller codes whose order is less than q. 

Binary Reed-Muller codes must have their order less than or 

equal to their number of variables. 

 

EXAMPLES: 

 

We build a Reed-Muller code:: 

 

sage: F = GF(3) 

sage: C = codes.ReedMullerCode(F, 2, 2) 

sage: C 

Reed-Muller Code of order 2 and 2 variables over Finite Field of size 3 

 

We ask for its parameters:: 

 

sage: C.length() 

9 

sage: C.dimension() 

6 

sage: C.minimum_distance() 

3 

 

If one provides a finite field of size 2, a Binary Reed-Muller code is built:: 

 

sage: F = GF(2) 

sage: C = codes.ReedMullerCode(F, 2, 2) 

sage: C 

Binary Reed-Muller Code of order 2 and number of variables 2 

""" 

if not(base_field in FiniteFields): 

raise ValueError("The parameter `base_field` must be a finite field") 

q = base_field.cardinality() 

if q == 2: 

return BinaryReedMullerCode(order, num_of_var) 

else: 

return QAryReedMullerCode(base_field, order, num_of_var) 

 

 

class QAryReedMullerCode(AbstractLinearCode): 

r""" 

Representation of a q-ary Reed-Muller code. 

 

For details on the definition of Reed-Muller codes, refer to 

:meth:`ReedMullerCode`. 

 

.. NOTE:: 

 

It is better to use the aforementioned method rather than calling 

this class directly, as :meth:`ReedMullerCode` creates either 

a binary or a q-ary Reed-Muller code according to the arguments it receives. 

 

INPUT: 

 

- ``base_field`` -- A finite field, which is the base field of the code. 

 

- ``order`` -- The order of the Reed-Muller Code, i.e., the maximum degree of the polynomial to be used in the code. 

 

- ``num_of_var`` -- The number of variables used in polynomial. 

 

.. WARNING:: 

 

For now, this implementation only supports Reed-Muller codes whose order is less than q. 

 

EXAMPLES:: 

 

sage: from sage.coding.reed_muller_code import QAryReedMullerCode 

sage: F = GF(3) 

sage: C = QAryReedMullerCode(F, 2, 2) 

sage: C 

Reed-Muller Code of order 2 and 2 variables over Finite Field of size 3 

""" 

 

_registered_encoders = {} 

_registered_decoders = {} 

 

def __init__(self, base_field, order, num_of_var): 

r""" 

TESTS: 

 

Note that the order given cannot be greater than (q-1). An error is raised if that happens:: 

 

sage: from sage.coding.reed_muller_code import QAryReedMullerCode 

sage: C = QAryReedMullerCode(GF(3), 4, 4) 

Traceback (most recent call last): 

... 

ValueError: The order must be less than 3 

 

The order and the number of variable must be integers:: 

 

sage: C = QAryReedMullerCode(GF(3),1.1,4) 

Traceback (most recent call last): 

... 

ValueError: The order of the code must be an integer 

 

The base_field parameter must be a finite field:: 

 

sage: C = QAryReedMullerCode(QQ,1,4) 

Traceback (most recent call last): 

... 

ValueError: the input `base_field` must be a FiniteField 

""" 

# input sanitization 

if not(base_field in FiniteFields): 

raise ValueError("the input `base_field` must be a FiniteField") 

if not(isinstance(order, (Integer, int))): 

raise ValueError("The order of the code must be an integer") 

if not(isinstance(num_of_var, (Integer, int))): 

raise ValueError("The number of variables must be an integer") 

q = base_field.cardinality() 

if (order >= q): 

raise ValueError("The order must be less than %s" % q) 

 

super(QAryReedMullerCode,self).__init__(base_field, q**num_of_var, "EvaluationVector", "Syndrome") 

self._order = order 

self._num_of_var = num_of_var 

self._dimension = binomial(num_of_var + order, order) 

 

def order(self): 

r""" 

Returns the order of ``self``. 

 

Order is the maximum degree of the polynomial used in the Reed-Muller code. 

 

EXAMPLES:: 

 

sage: from sage.coding.reed_muller_code import QAryReedMullerCode 

sage: F = GF(59) 

sage: C = QAryReedMullerCode(F, 2, 4) 

sage: C.order() 

2 

""" 

return self._order 

 

def number_of_variables(self): 

r""" 

Returns the number of variables of the polynomial ring used in ``self``. 

 

EXAMPLES:: 

 

sage: from sage.coding.reed_muller_code import QAryReedMullerCode 

sage: F = GF(59) 

sage: C = QAryReedMullerCode(F, 2, 4) 

sage: C.number_of_variables() 

4 

""" 

return self._num_of_var 

 

def minimum_distance(self): 

r""" 

Returns the minimum distance between two words in ``self``. 

 

The minimum distance of a q-ary Reed-Muller code with order `d` and number of variables `m` is `(q-d)q^{m-1}` 

 

EXAMPLES:: 

 

sage: from sage.coding.reed_muller_code import QAryReedMullerCode 

sage: F = GF(5) 

sage: C = QAryReedMullerCode(F, 2, 4) 

sage: C.minimum_distance() 

375 

""" 

d = self.order() 

q = self.base_field().cardinality() 

n = self.length() 

return ((q - d) * n) // q 

 

def _repr_(self): 

r""" 

Returns a string representation of ``self``. 

 

EXAMPLES:: 

 

sage: from sage.coding.reed_muller_code import QAryReedMullerCode 

sage: F = GF(59) 

sage: C = QAryReedMullerCode(F, 2, 4) 

sage: C 

Reed-Muller Code of order 2 and 4 variables over Finite Field of size 59 

""" 

return "Reed-Muller Code of order %s and %s variables over %s" % ( 

self.order(), self.number_of_variables(), self.base_field()) 

 

def _latex_(self): 

r""" 

Returns a latex representation of ``self``. 

 

EXAMPLES:: 

 

sage: from sage.coding.reed_muller_code import QAryReedMullerCode 

sage: F = GF(59) 

sage: C = QAryReedMullerCode(F, 2, 4) 

sage: latex(C) 

\textnormal{Reed-Muller Code of order} 2 \textnormal{and }4 \textnormal{variables over} \Bold{F}_{59} 

""" 

return "\\textnormal{Reed-Muller Code of order} %s \\textnormal{and }%s \\textnormal{variables over} %s"\ 

% (self.order(), self.number_of_variables(), self.base_field()._latex_()) 

 

def __eq__(self, other): 

r""" 

Tests equality between Reed-Muller Code objects. 

 

EXAMPLES:: 

 

sage: from sage.coding.reed_muller_code import QAryReedMullerCode 

sage: F = GF(59) 

sage: C1 = QAryReedMullerCode(F, 2, 4) 

sage: C2 = QAryReedMullerCode(GF(59), 2, 4) 

sage: C1.__eq__(C2) 

True 

""" 

# I am not comparing the base field directly because of possible change 

# in variables 

return isinstance(other, QAryReedMullerCode) \ 

and self.base_field() == other.base_field() \ 

and self.order() == other.order() \ 

and self.number_of_variables() == other.number_of_variables() 

 

 

class BinaryReedMullerCode(AbstractLinearCode): 

r""" 

Representation of a binary Reed-Muller code. 

 

For details on the definition of Reed-Muller codes, refer to 

:meth:`ReedMullerCode`. 

 

.. NOTE:: 

 

It is better to use the aforementioned method rather than calling 

this class directly, as :meth:`ReedMullerCode` creates either 

a binary or a q-ary Reed-Muller code according to the arguments it receives. 

 

 

INPUT: 

 

- ``order`` -- The order of the Reed-Muller Code, i.e., the maximum degree of the polynomial to be used in the code. 

 

- ``num_of_var`` -- The number of variables used in the polynomial. 

 

EXAMPLES: 

 

A binary Reed-Muller code can be constructed by simply giving the order of the code and the number of variables:: 

 

sage: C = codes.BinaryReedMullerCode(2, 4) 

sage: C 

Binary Reed-Muller Code of order 2 and number of variables 4 

""" 

 

_registered_encoders = {} 

_registered_decoders = {} 

 

def __init__(self, order, num_of_var): 

r""" 

TESTS: 

 

If the order given is greater than the number of variables an error is raised:: 

 

sage: C = codes.BinaryReedMullerCode(5, 4) 

Traceback (most recent call last): 

... 

ValueError: The order must be less than or equal to 4 

 

The order and the number of variable must be integers:: 

 

sage: C = codes.BinaryReedMullerCode(1.1,4) 

Traceback (most recent call last): 

... 

ValueError: The order of the code must be an integer 

""" 

# input sanitization 

if not(isinstance(order, (Integer, int))): 

raise ValueError("The order of the code must be an integer") 

if not(isinstance(num_of_var, (Integer, int))): 

raise ValueError("The number of variables must be an integer") 

if (num_of_var < order): 

raise ValueError( 

"The order must be less than or equal to %s" % 

num_of_var) 

 

super(BinaryReedMullerCode, self).__init__(GF(2), 2**num_of_var, 

"EvaluationVector", "Syndrome") 

self._order = order 

self._num_of_var = num_of_var 

self._dimension = _binomial_sum(num_of_var, order) 

 

def order(self): 

r""" 

Returns the order of ``self``. Order is the maximum degree of the polynomial used in the Reed-Muller code. 

 

EXAMPLES:: 

 

sage: C = codes.BinaryReedMullerCode(2, 4) 

sage: C.order() 

2 

""" 

return self._order 

 

def number_of_variables(self): 

r""" 

Returns the number of variables of the polynomial ring used in ``self``. 

 

EXAMPLES:: 

 

sage: C = codes.BinaryReedMullerCode(2, 4) 

sage: C.number_of_variables() 

4 

""" 

return self._num_of_var 

 

def minimum_distance(self): 

r""" 

Returns the minimum distance of ``self``. 

The minimum distance of a binary Reed-Muller code of order $d$ and number of variables $m$ is $q^{m-d}$ 

 

EXAMPLES:: 

 

sage: C = codes.BinaryReedMullerCode(2, 4) 

sage: C.minimum_distance() 

4 

""" 

return 2**(self.number_of_variables() - self.order()) 

 

def _repr_(self): 

r""" 

Returns a string representation of ``self``. 

 

EXAMPLES:: 

 

sage: C = codes.BinaryReedMullerCode(2, 4) 

sage: C 

Binary Reed-Muller Code of order 2 and number of variables 4 

""" 

return "Binary Reed-Muller Code of order %s and number of variables %s" % ( 

self.order(), self.number_of_variables()) 

 

def _latex_(self): 

r""" 

Returns a latex representation of ``self``. 

 

EXAMPLES:: 

 

sage: C = codes.BinaryReedMullerCode(2, 4) 

sage: latex(C) 

\textnormal{Binary Reed-Muller Code of order} 2 \textnormal{and number of variables} 4 

""" 

return "\\textnormal{Binary Reed-Muller Code of order} %s \\textnormal{and number of variables} %s" % ( 

self.order(), self.number_of_variables()) 

 

def __eq__(self, other): 

r""" 

Tests equality between Reed-Muller Code objects. 

 

EXAMPLES:: 

 

sage: C1 = codes.BinaryReedMullerCode(2, 4) 

sage: C2 = codes.BinaryReedMullerCode(2, 4) 

sage: C1.__eq__(C2) 

True 

""" 

return isinstance(other, BinaryReedMullerCode) \ 

and self.order() == other.order() \ 

and self.number_of_variables() == other.number_of_variables() 

 

 

class ReedMullerVectorEncoder(Encoder): 

r""" 

Encoder for Reed-Muller codes which encodes vectors into codewords. 

 

Consider a Reed-Muller code of order `r`, number of variables `m`, length `n`, 

dimension `k` over some finite field `F`. 

Let those variables be `(x_1, x_2, \dots, x_m)`. 

We order the monomials by lowest power on lowest index variables. If we have three monomials 

`x_1 \times x_2`, `x_1 \times x_2^2` and `x_1^2 \times x_2`, the ordering is: 

`x_1 \times x_2 < x_1 \times x_2^2 < x_1^2 \times x_2` 

 

Let now `(v_1,v_2,\ldots,v_k)` be a vector of `F`, which corresponds to the polynomial 

`f = \Sigma^{k}_{i=1} v_i \times x_i`. 

 

Let `(\beta_1, \beta_2, \ldots, \beta_q)` be the elements of `F` ordered as they are 

returned by Sage when calling ``F.list()``. 

 

The aforementioned polynomial `f` is encoded as: 

 

`(f(\alpha_{11},\alpha_{12},\ldots,\alpha_{1m}),f(\alpha_{21},\alpha_{22},\ldots, 

\alpha_{2m}),\ldots,f(\alpha_{q^m1},\alpha_{q^m2},\ldots,\alpha_{q^mm}`, with 

`\alpha_{ij}=\beta_{i \ mod \ q^j} \forall (i,j)` 

 

INPUT: 

 

- ``code`` -- The associated code of this encoder. 

 

EXAMPLES:: 

 

sage: C1=codes.ReedMullerCode(GF(2), 2, 4) 

sage: E1=codes.encoders.ReedMullerVectorEncoder(C1) 

sage: E1 

Evaluation vector-style encoder for Binary Reed-Muller Code of order 2 and number of variables 4 

sage: C2=codes.ReedMullerCode(GF(3), 2, 2) 

sage: E2=codes.encoders.ReedMullerVectorEncoder(C2) 

sage: E2 

Evaluation vector-style encoder for Reed-Muller Code of order 2 and 2 variables over Finite Field of size 3 

 

Actually, we can construct the encoder from ``C`` directly:: 

 

sage: C=codes.ReedMullerCode(GF(2), 2, 4) 

sage: E = C.encoder("EvaluationVector") 

sage: E 

Evaluation vector-style encoder for Binary Reed-Muller Code of order 2 and number of variables 4 

""" 

 

def __init__(self, code): 

r""" 

TESTS: 

 

If ``code`` is not a Reed-Muller code, an error is raised:: 

 

sage: C = codes.random_linear_code(GF(11), 10, 4) 

sage: codes.encoders.ReedMullerVectorEncoder(C) 

Traceback (most recent call last): 

... 

ValueError: the code has to be a Reed-Muller code 

""" 

if not ( 

isinstance( 

code, 

QAryReedMullerCode) or isinstance( 

code, 

BinaryReedMullerCode)): 

raise ValueError("the code has to be a Reed-Muller code") 

super(ReedMullerVectorEncoder, self).__init__(code) 

 

def _repr_(self): 

r""" 

Returns a string representation of ``self``. 

 

EXAMPLES:: 

 

sage: F = GF(11) 

sage: C = codes.ReedMullerCode(F, 2, 4) 

sage: E=codes.encoders.ReedMullerVectorEncoder(C) 

sage: E 

Evaluation vector-style encoder for Reed-Muller Code of order 2 and 4 variables over Finite Field of size 11 

""" 

return "Evaluation vector-style encoder for %s" % self.code() 

 

def _latex_(self): 

r""" 

Returns a string representation of ``self``. 

 

EXAMPLES:: 

 

sage: F = GF(11) 

sage: C = codes.ReedMullerCode(F, 2, 4) 

sage: E=codes.encoders.ReedMullerVectorEncoder(C) 

sage: latex(E) 

\textnormal{Evaluation vector-style encoder for }\textnormal{Reed-Muller Code of order} 2 \textnormal{and }4 \textnormal{variables over} \Bold{F}_{11} 

""" 

return "\\textnormal{Evaluation vector-style encoder for }%s" % self.code()._latex_() 

 

def __eq__(self, other): 

r""" 

Tests equality between ReedMullerVectorEncoder objects. 

 

EXAMPLES:: 

 

sage: F = GF(11) 

sage: C = codes.ReedMullerCode(F, 2, 4) 

sage: D1 = codes.encoders.ReedMullerVectorEncoder(C) 

sage: D2 = codes.encoders.ReedMullerVectorEncoder(C) 

sage: D1.__eq__(D2) 

True 

sage: D1 is D2 

False 

""" 

return (isinstance(other, ReedMullerVectorEncoder) 

) and self.code() == other.code() 

 

@cached_method 

def generator_matrix(self): 

r""" 

Returns a generator matrix of ``self`` 

 

EXAMPLES:: 

 

sage: F = GF(3) 

sage: C = codes.ReedMullerCode(F, 2, 2) 

sage: E = codes.encoders.ReedMullerVectorEncoder(C) 

sage: E.generator_matrix() 

[1 1 1 1 1 1 1 1 1] 

[0 1 2 0 1 2 0 1 2] 

[0 0 0 1 1 1 2 2 2] 

[0 1 1 0 1 1 0 1 1] 

[0 0 0 0 1 2 0 2 1] 

[0 0 0 1 1 1 1 1 1] 

""" 

C = self.code() 

base_field = C.base_field() 

order = C.order() 

num_of_var = C.number_of_variables() 

q = base_field.cardinality() 

dimension = C.dimension() 

points = base_field**num_of_var 

matrix_list = [] 

max_individual_degree = min(order, (q - 1)) 

for degree in range(order + 1): 

exponents = Subsets(list(range(num_of_var)) * max_individual_degree, 

degree, submultiset=True) 

matrix_list += [[reduce(mul, [x[i] for i in exponent], 1) 

for x in points] for exponent in exponents] 

M = matrix(base_field, matrix_list) 

M.set_immutable() 

return M 

 

def points(self): 

r""" 

Returns the points of $F^m$, where $F$ is base field and $m$ is the number of variables, in order of which polynomials are evaluated on. 

 

EXAMPLES:: 

 

sage: F = GF(3) 

sage: Fx.<x0,x1> = F[] 

sage: C = codes.ReedMullerCode(F, 2, 2) 

sage: E = C.encoder("EvaluationVector") 

sage: E.points() 

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

""" 

code = self.code() 

return ((code.base_field())**code.number_of_variables()).list() 

 

 

class ReedMullerPolynomialEncoder(Encoder): 

r""" 

Encoder for Reed-Muller codes which encodes appropriate multivariate polynomials into codewords. 

 

Consider a Reed-Muller code of order `r`, number of variables `m`, length `n`, 

dimension `k` over some finite field `F`. 

Let those variables be `(x_1, x_2, \dots, x_m)`. 

We order the monomials by lowest power on lowest index variables. If we have three monomials 

`x_1 \times x_2`, `x_1 \times x_2^2` and `x_1^2 \times x_2`, the ordering is: 

`x_1 \times x_2 < x_1 \times x_2^2 < x_1^2 \times x_2` 

 

Let now `f` be a polynomial of the multivariate polynomial ring `F[x_1, \dots, x_m]`. 

 

Let `(\beta_1, \beta_2, \ldots, \beta_q)` be the elements of `F` ordered as they are 

returned by Sage when calling ``F.list()``. 

 

The aforementioned polynomial `f` is encoded as: 

 

`(f(\alpha_{11},\alpha_{12},\ldots,\alpha_{1m}),f(\alpha_{21},\alpha_{22},\ldots, 

\alpha_{2m}),\ldots,f(\alpha_{q^m1},\alpha_{q^m2},\ldots,\alpha_{q^mm}`, with 

`\alpha_{ij}=\beta_{i \ mod \ q^j} \forall (i,j)` 

 

 

INPUT: 

 

- ``code`` -- The associated code of this encoder. 

 

-``polynomial_ring`` -- (default:``None``) The polynomial ring from which the message is chosen. 

If this is set to ``None``, a polynomial ring in `x` will be built 

from the code parameters. 

 

EXAMPLES:: 

 

sage: C1=codes.ReedMullerCode(GF(2), 2, 4) 

sage: E1=codes.encoders.ReedMullerPolynomialEncoder(C1) 

sage: E1 

Evaluation polynomial-style encoder for Binary Reed-Muller Code of order 2 and number of variables 4 

sage: C2=codes.ReedMullerCode(GF(3), 2, 2) 

sage: E2=codes.encoders.ReedMullerPolynomialEncoder(C2) 

sage: E2 

Evaluation polynomial-style encoder for Reed-Muller Code of order 2 and 2 variables over Finite Field of size 3 

 

We can also pass a predefined polynomial ring:: 

 

sage: R=PolynomialRing(GF(3), 2, 'y') 

sage: C=codes.ReedMullerCode(GF(3), 2, 2) 

sage: E=codes.encoders.ReedMullerPolynomialEncoder(C, R) 

sage: E 

Evaluation polynomial-style encoder for Reed-Muller Code of order 2 and 2 variables over Finite Field of size 3 

 

Actually, we can construct the encoder from ``C`` directly:: 

 

sage: E = C1.encoder("EvaluationPolynomial") 

sage: E 

Evaluation polynomial-style encoder for Binary Reed-Muller Code of order 2 and number of variables 4 

""" 

 

def __init__(self, code, polynomial_ring=None): 

r""" 

TESTS: 

 

If ``code`` is not a Reed-Muller code, an error is raised:: 

 

sage: C = codes.random_linear_code(GF(11), 10, 4) 

sage: codes.encoders.ReedMullerPolynomialEncoder(C) 

Traceback (most recent call last): 

... 

ValueError: the code has to be a Reed-Muller code 

 

If the polynomial ring passed is not according to the requirement (over a different field or different number of variables) then an error is raised:: 

 

sage: F=GF(59) 

sage: R.<x,y,z,w>=F[] 

sage: C=codes.ReedMullerCode(F, 2, 3) 

sage: E=codes.encoders.ReedMullerPolynomialEncoder(C, R) 

Traceback (most recent call last): 

... 

ValueError: The Polynomial ring should be on Finite Field of size 59 and should have 3 variables 

""" 

if not ( 

isinstance(code, QAryReedMullerCode) 

or isinstance(code, BinaryReedMullerCode)): 

raise ValueError("the code has to be a Reed-Muller code") 

super(ReedMullerPolynomialEncoder, self).__init__(code) 

if polynomial_ring is None: 

self._polynomial_ring = PolynomialRing(code.base_field(), 

code.number_of_variables(), 'x') 

else: 

if (polynomial_ring.base_ring() == code.base_field()) and ( 

len(polynomial_ring.variable_names()) == code.number_of_variables()): 

self._polynomial_ring = polynomial_ring 

else: 

raise ValueError( 

"The Polynomial ring should be on %s and should have %s variables" % 

(code.base_field(), code.number_of_variables())) 

 

def _repr_(self): 

r""" 

Returns a string representation of ``self``. 

 

EXAMPLES:: 

 

sage: F = GF(59) 

sage: C = codes.ReedMullerCode(F, 2, 4) 

sage: E=codes.encoders.ReedMullerPolynomialEncoder(C) 

sage: E 

Evaluation polynomial-style encoder for Reed-Muller Code of order 2 and 4 variables over Finite Field of size 59 

""" 

return "Evaluation polynomial-style encoder for %s" % self.code() 

 

def _latex_(self): 

r""" 

Returns a string representation of ``self``. 

 

EXAMPLES:: 

 

sage: F = GF(59) 

sage: C = codes.ReedMullerCode(F, 2, 4) 

sage: E=codes.encoders.ReedMullerPolynomialEncoder(C) 

sage: latex(E) 

\textnormal{Evaluation polynomial-style encoder for }\textnormal{Reed-Muller Code of order} 2 \textnormal{and }4 \textnormal{variables over} \Bold{F}_{59} 

""" 

return "\\textnormal{Evaluation polynomial-style encoder for }%s" % self.code()._latex_() 

 

def __eq__(self, other): 

r""" 

Tests equality between ReedMullerVectorEncoder objects. 

 

EXAMPLES:: 

 

sage: F = GF(11) 

sage: C = codes.ReedMullerCode(F, 2, 4) 

sage: D1 = codes.encoders.ReedMullerPolynomialEncoder(C) 

sage: D2 = codes.encoders.ReedMullerPolynomialEncoder(C) 

sage: D1.__eq__(D2) 

True 

sage: D1 is D2 

False 

""" 

return isinstance(other, ReedMullerPolynomialEncoder) \ 

and self.code() == other.code() 

 

def encode(self, p): 

r""" 

Transforms the polynomial ``p`` into a codeword of :meth:`code`. 

 

INPUT: 

 

- ``p`` -- A polynomial from the message space of ``self`` of degree 

less than ``self.code().order()``. 

 

OUTPUT: 

 

- A codeword in associated code of ``self`` 

 

EXAMPLES:: 

 

sage: F = GF(3) 

sage: Fx.<x0,x1> = F[] 

sage: C = codes.ReedMullerCode(F, 2, 2) 

sage: E = C.encoder("EvaluationPolynomial") 

sage: p = x0*x1 + x1^2 + x0 + x1 + 1 

sage: c = E.encode(p); c 

(1, 2, 0, 0, 2, 1, 1, 1, 1) 

sage: c in C 

True 

 

If a polynomial with good monomial degree but wrong monomial 

degree is given,an error is raised:: 

 

sage: p = x0^2*x1 

sage: E.encode(p) 

Traceback (most recent call last): 

... 

ValueError: The polynomial to encode must have degree at most 2 

 

If ``p`` is not an element of the proper polynomial ring, an error is raised:: 

 

sage: Qy.<y1,y2> = QQ[] 

sage: p = y1^2 + 1 

sage: E.encode(p) 

Traceback (most recent call last): 

... 

ValueError: The value to encode must be in Multivariate Polynomial Ring in x0, x1 over Finite Field of size 3 

""" 

M = self.message_space() 

if p not in M: 

raise ValueError("The value to encode must be in %s" % M) 

C = self.code() 

if p.degree() > C.order(): 

raise ValueError("The polynomial to encode must have degree at most %s" 

% C.order()) 

base_fieldTuple = Tuples(C.base_field().list(), C.number_of_variables()) 

return vector(C.base_ring(), [p(x) for x in base_fieldTuple]) 

 

def unencode_nocheck(self, c): 

r""" 

Returns the message corresponding to the codeword ``c``. 

 

Use this method with caution: it does not check if ``c`` 

belongs to the code, and if this is not the case, the output is 

unspecified. Instead, use :meth:`unencode`. 

 

INPUT: 

 

- ``c`` -- A codeword of :meth:`code`. 

 

OUTPUT: 

 

- An polynomial of degree less than ``self.code().order()``. 

 

EXAMPLES:: 

 

sage: F = GF(3) 

sage: C = codes.ReedMullerCode(F, 2, 2) 

sage: E = C.encoder("EvaluationPolynomial") 

sage: c = vector(F, (1, 2, 0, 0, 2, 1, 1, 1, 1)) 

sage: c in C 

True 

sage: p = E.unencode_nocheck(c); p 

x0*x1 + x1^2 + x0 + x1 + 1 

sage: E.encode(p) == c 

True 

 

Note that no error is thrown if ``c`` is not a codeword, and that the 

result is undefined:: 

 

sage: c = vector(F, (1, 2, 0, 0, 2, 1, 0, 1, 1)) 

sage: c in C 

False 

sage: p = E.unencode_nocheck(c); p 

-x0*x1 - x1^2 + x0 + 1 

sage: E.encode(p) == c 

False 

 

""" 

return _multivariate_polynomial_interpolation( 

c, 

self.code().order(), 

self.polynomial_ring()) 

 

def message_space(self): 

r""" 

Returns the message space of ``self`` 

 

EXAMPLES:: 

 

sage: F = GF(11) 

sage: C = codes.ReedMullerCode(F, 2, 4) 

sage: E = C.encoder("EvaluationPolynomial") 

sage: E.message_space() 

Multivariate Polynomial Ring in x0, x1, x2, x3 over Finite Field of size 11 

""" 

return self._polynomial_ring 

 

def polynomial_ring(self): 

r""" 

Returns the polynomial ring associated with ``self`` 

 

EXAMPLES:: 

 

sage: F = GF(11) 

sage: C = codes.ReedMullerCode(F, 2, 4) 

sage: E = C.encoder("EvaluationPolynomial") 

sage: E.polynomial_ring() 

Multivariate Polynomial Ring in x0, x1, x2, x3 over Finite Field of size 11 

""" 

return self._polynomial_ring 

 

def points(self): 

r""" 

Returns the evaluation points in the appropriate order as used by ``self`` when 

encoding a message. 

 

EXAMPLES:: 

 

sage: F = GF(3) 

sage: Fx.<x0,x1> = F[] 

sage: C = codes.ReedMullerCode(F, 2, 2) 

sage: E = C.encoder("EvaluationPolynomial") 

sage: E.points() 

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

""" 

code = self.code() 

return ((code.base_field())**code.number_of_variables()).list() 

 

 

####################### registration ############################### 

 

QAryReedMullerCode._registered_encoders["EvaluationVector"] = ReedMullerVectorEncoder 

QAryReedMullerCode._registered_encoders["EvaluationPolynomial"] = ReedMullerPolynomialEncoder 

 

QAryReedMullerCode._registered_decoders["Syndrome"] = LinearCodeSyndromeDecoder 

 

BinaryReedMullerCode._registered_encoders["EvaluationVector"] = ReedMullerVectorEncoder 

BinaryReedMullerCode._registered_encoders["EvaluationPolynomial"] = ReedMullerPolynomialEncoder 

 

BinaryReedMullerCode._registered_decoders["Syndrome"] = LinearCodeSyndromeDecoder