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

r""" 

Base class for Channels and commonly used channels 

 

Given an input space and an output space, a channel takes element from 

the input space (the message) and transforms it into an element of the output space 

(the transmitted message). 

 

In Sage, Channels simulate error-prone transmission over communication 

channels, and we borrow the nomenclature from communication theory, such as 

"transmission" and "positions" as the elements of transmitted vectors. 

Transmission can be achieved with two methods: 

 

- :meth:`Channel.transmit`. Considering a channel ``Chan`` and a message 

``msg``, transmitting ``msg`` with ``Chan`` can be done this way:: 

 

Chan.transmit(msg) 

 

It can also be written in a more convenient way:: 

 

Chan(msg) 

 

- :meth:`transmit_unsafe`. This does the exact same thing as 

:meth:`transmit` except that it does not check if ``msg`` belongs to the 

input space of ``Chan``:: 

 

Chan.transmit_unsafe(msg) 

 

This is useful in e.g. an inner-loop of a long simulation as a 

lighter-weight alternative to :meth:`Channel.transmit`. 

 

This file contains the following elements: 

 

- :class:`Channel`, the abstract class for Channels 

- :class:`StaticErrorRateChannel`, which creates a specific number of errors in each 

transmitted message 

- :class:`ErrorErasureChannel`, which creates a specific number of errors and a 

specific number of erasures in each transmitted message 

""" 

 

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

# Copyright (C) 2015 David Lucas <david.lucas@inria.fr> 

# 

# 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 sage.structure.sage_object import SageObject 

from sage.rings.integer import Integer 

from sage.rings.finite_rings.finite_field_constructor import GF 

from sage.misc.prandom import randint, random, sample 

from sage.modules.free_module_element import vector 

from sage.misc.abstract_method import abstract_method 

from sage.categories.cartesian_product import cartesian_product 

from sage.modules.free_module import VectorSpace 

from sage.functions.other import binomial 

from copy import copy 

 

def random_error_vector(n, F, error_positions): 

r""" 

Return a vector of length ``n`` over ``F`` filled with random non-zero coefficients 

at the positions given by ``error_positions``. 

 

.. NOTE:: 

 

This is a helper function, which should only be used when implementing new channels. 

 

INPUT: 

 

- ``n`` -- the length of the vector 

 

- ``F`` -- the field over which the vector is defined 

 

- ``error_positions`` -- the non-zero positions of the vector 

 

OUTPUT: 

 

- a vector of ``F`` 

 

AUTHORS: 

 

This function is taken from codinglib (https://bitbucket.org/jsrn/codinglib/) 

and was written by Johan Nielsen. 

 

EXAMPLES:: 

 

sage: from sage.coding.channel_constructions import random_error_vector 

sage: random_error_vector(5, GF(2), [1,3]) 

(0, 1, 0, 1, 0) 

""" 

vect = [F.zero()]*n 

for i in error_positions: 

vect[i] = F._random_nonzero_element() 

return vector(F, vect) 

 

def format_interval(t): 

r""" 

Returns a formatted string representation of ``t``. 

 

This method should be called by any representation function in Channel classes. 

 

.. NOTE:: 

 

This is a helper function, which should only be used when implementing new channels. 

 

INPUT: 

 

- ``t`` -- a list or a tuple 

 

OUTPUT: 

 

- a string 

 

TESTS:: 

 

sage: from sage.coding.channel_constructions import format_interval 

sage: t = (5, 5) 

sage: format_interval(t) 

'5' 

 

sage: t = (2, 10) 

sage: format_interval(t) 

'between 2 and 10' 

 

""" 

return str(t[0]) if t[0] == t[1] else 'between %s and %s' % (t[0], t[1]) 

 

class Channel(SageObject): 

r""" 

Abstract top-class for Channel objects. 

 

All channel objects must inherit from this class. To implement a channel subclass, one should 

do the following: 

 

- inherit from this class, 

 

- call the super constructor, 

 

- override :meth:`transmit_unsafe`. 

 

While not being mandatory, it might be useful to reimplement representation methods (``_repr_`` and 

``_latex_``). 

 

This abstract class provides the following parameters: 

 

- ``input_space`` -- the space of the words to transmit 

 

- ``output_space`` -- the space of the transmitted words 

""" 

 

def __init__(self, input_space, output_space): 

r""" 

Initializes parameters for a Channel object. 

 

This is a private method, which should be called by the constructor 

of every encoder, as it automatically initializes the mandatory 

parameters of a Channel object. 

 

INPUT: 

 

- ``input_space`` -- the space of the words to transmit 

 

- ``output_space`` -- the space of the transmitted words 

 

EXAMPLES: 

 

We first create a new Channel subclass:: 

 

sage: from sage.coding.channel_constructions import Channel 

sage: class ChannelExample(Channel): 

....: def __init__(self, input_space, output_space): 

....: super(ChannelExample, self).__init__(input_space, output_space) 

 

We now create a member of our newly made class:: 

 

sage: input = VectorSpace(GF(7), 6) 

sage: output = VectorSpace(GF(7), 5) 

sage: Chan = ChannelExample(input, output) 

 

We can check its parameters:: 

 

sage: Chan.input_space() 

Vector space of dimension 6 over Finite Field of size 7 

sage: Chan.output_space() 

Vector space of dimension 5 over Finite Field of size 7 

""" 

self._input_space = input_space 

self._output_space = output_space 

 

def transmit(self, message): 

r""" 

Returns ``message``, modified accordingly with the algorithm of the channel it was 

transmitted through. 

 

Checks if ``message`` belongs to the input space, and returns an exception if not. 

Note that ``message`` itself is never modified by the channel. 

 

INPUT: 

 

- ``message`` -- a vector 

 

OUTPUT: 

 

- a vector of the output space of ``self`` 

 

EXAMPLES:: 

 

sage: F = GF(59)^6 

sage: n_err = 2 

sage: Chan = channels.StaticErrorRateChannel(F, n_err) 

sage: msg = F((4, 8, 15, 16, 23, 42)) 

sage: set_random_seed(10) 

sage: Chan.transmit(msg) 

(4, 8, 4, 16, 23, 53) 

 

We can check that the input ``msg`` is not modified:: 

 

sage: msg 

(4, 8, 15, 16, 23, 42) 

 

If we transmit a vector which is not in the input space of ``self``:: 

 

sage: n_err = 2 

sage: Chan = channels.StaticErrorRateChannel(GF(59)^6, n_err) 

sage: msg = (4, 8, 15, 16, 23, 42) 

sage: Chan.transmit(msg) 

Traceback (most recent call last): 

... 

TypeError: Message must be an element of the input space for the given channel 

 

.. NOTE:: 

 

One can also call directly ``Chan(message)``, which does the same as ``Chan.transmit(message)`` 

""" 

if message in self.input_space(): 

return self.transmit_unsafe(message) 

else : 

raise TypeError("Message must be an element of the input space for the given channel") 

 

#Alias for transmit method 

__call__ = transmit 

 

def input_space(self): 

r""" 

Returns the input space of ``self``. 

 

EXAMPLES:: 

 

sage: n_err = 2 

sage: Chan = channels.StaticErrorRateChannel(GF(59)^6, n_err) 

sage: Chan.input_space() 

Vector space of dimension 6 over Finite Field of size 59 

 

""" 

return self._input_space 

 

def output_space(self): 

r""" 

Returns the output space of ``self``. 

 

EXAMPLES:: 

 

sage: n_err = 2 

sage: Chan = channels.StaticErrorRateChannel(GF(59)^6, n_err) 

sage: Chan.output_space() 

Vector space of dimension 6 over Finite Field of size 59 

""" 

return self._output_space 

 

@abstract_method 

def transmit_unsafe(self, message): 

r""" 

Returns ``message``, modified accordingly with the algorithm of the channel it was 

transmitted through. 

 

This method does not check if ``message`` belongs to the input space of``self``. 

 

This is an abstract method which should be reimplemented in all the subclasses of 

Channel. 

""" 

 

 

 

 

 

 

 

 

 

 

class StaticErrorRateChannel(Channel): 

r""" 

Channel which adds a static number of errors to each message it transmits. 

 

The input space and the output space of this channel are the same. 

 

INPUT: 

 

- ``space`` -- the space of both input and output 

 

- ``number_errors`` -- the number of errors added to each transmitted message 

It can be either an integer of a tuple. If a tuple is passed as 

argument, the number of errors will be a random integer between the 

two bounds of the tuple. 

 

EXAMPLES: 

 

We construct a StaticErrorRateChannel which adds 2 errors 

to any transmitted message:: 

 

sage: n_err = 2 

sage: Chan = channels.StaticErrorRateChannel(GF(59)^40, n_err) 

sage: Chan 

Static error rate channel creating 2 errors, of input and output space 

Vector space of dimension 40 over Finite Field of size 59 

 

We can also pass a tuple for the number of errors:: 

 

sage: n_err = (1, 10) 

sage: Chan = channels.StaticErrorRateChannel(GF(59)^40, n_err) 

sage: Chan 

Static error rate channel creating between 1 and 10 errors, 

of input and output space Vector space of dimension 40 over Finite Field of size 59 

""" 

 

def __init__(self, space, number_errors): 

r""" 

TESTS: 

 

If the number of errors exceeds the dimension of the input space, 

it will return an error:: 

 

sage: n_err = 42 

sage: Chan = channels.StaticErrorRateChannel(GF(59)^40, n_err) 

Traceback (most recent call last): 

... 

ValueError: There might be more errors than the dimension of the input space 

""" 

if isinstance(number_errors, (Integer, int)): 

number_errors = (number_errors, number_errors) 

if not isinstance(number_errors, (tuple, list)): 

raise ValueError("number_errors must be a tuple, a list, an Integer or a Python int") 

super(StaticErrorRateChannel, self).__init__(space, space) 

if number_errors[1] > space.dimension(): 

raise ValueError("There might be more errors than the dimension of the input space") 

self._number_errors = number_errors 

 

def _repr_(self): 

r""" 

Returns a string representation of ``self``. 

 

EXAMPLES:: 

 

sage: n_err = 42 

sage: Chan = channels.StaticErrorRateChannel(GF(59)^50, n_err) 

sage: Chan 

Static error rate channel creating 42 errors, of input and output space 

Vector space of dimension 50 over Finite Field of size 59 

""" 

no_err = self.number_errors() 

return "Static error rate channel creating %s errors, of input and output space %s"\ 

% (format_interval(no_err), self.input_space()) 

 

def _latex_(self): 

r""" 

Returns a latex representation of ``self``. 

 

EXAMPLES:: 

 

sage: n_err = 42 

sage: Chan = channels.StaticErrorRateChannel(GF(59)^50, n_err) 

sage: latex(Chan) 

\textnormal{Static error rate channel creating 42 errors, of 

input and output space Vector space of dimension 50 over Finite Field of size 59} 

""" 

no_err = self.number_errors() 

return "\\textnormal{Static error rate channel creating %s errors, of input and output space %s}"\ 

% (format_interval(no_err), self.input_space()) 

 

def transmit_unsafe(self, message): 

r""" 

Returns ``message`` with as many errors as ``self._number_errors`` in it. 

 

If ``self._number_errors`` was passed as a tuple for the number of errors, it will 

pick a random integer between the bounds of the tuple and use it as the number of errors. 

 

This method does not check if ``message`` belongs to the input space of``self``. 

 

INPUT: 

 

- ``message`` -- a vector 

 

OUTPUT: 

 

- a vector of the output space 

 

EXAMPLES:: 

 

sage: F = GF(59)^6 

sage: n_err = 2 

sage: Chan = channels.StaticErrorRateChannel(F, n_err) 

sage: msg = F((4, 8, 15, 16, 23, 42)) 

sage: set_random_seed(10) 

sage: Chan.transmit_unsafe(msg) 

(4, 8, 4, 16, 23, 53) 

 

This checks that :trac:`19863` is fixed:: 

 

sage: V = VectorSpace(GF(2), 1000) 

sage: Chan = channels.StaticErrorRateChannel(V, 367) 

sage: c = V.random_element() 

sage: (c - Chan(c)).hamming_weight() 

367 

""" 

w = copy(message) 

number_errors = randint(*self.number_errors()) 

V = self.input_space() 

R = V.base_ring() 

for i in sample(range(V.dimension()), number_errors): 

err = R.random_element() 

while (w[i] == err): 

err = R.random_element() 

w[i] = err 

return w 

 

def number_errors(self): 

r""" 

Returns the number of errors created by ``self``. 

 

EXAMPLES:: 

 

sage: n_err = 3 

sage: Chan = channels.StaticErrorRateChannel(GF(59)^6, n_err) 

sage: Chan.number_errors() 

(3, 3) 

""" 

return self._number_errors 

 

 

 

 

 

 

 

 

 

 

class ErrorErasureChannel(Channel): 

r""" 

Channel which adds errors and erases several positions in any message it transmits. 

 

The output space of this channel is a Cartesian product 

between its input space and a VectorSpace of the same dimension over GF(2) 

 

INPUT: 

 

- ``space`` -- the input and output space 

 

- ``number_errors`` -- the number of errors created in each transmitted 

message. It can be either an integer of a tuple. If an tuple is passed as 

an argument, the number of errors will be a random integer between the 

two bounds of this tuple. 

 

- ``number_erasures`` -- the number of erasures created in each transmitted 

message. It can be either an integer of a tuple. If an tuple is passed as an 

argument, the number of erasures will be a random integer between the 

two bounds of this tuple. 

 

EXAMPLES: 

 

We construct a ErrorErasureChannel which adds 2 errors 

and 2 erasures to any transmitted message:: 

 

sage: n_err, n_era = 2, 2 

sage: Chan = channels.ErrorErasureChannel(GF(59)^40, n_err, n_era) 

sage: Chan 

Error-and-erasure channel creating 2 errors and 2 erasures 

of input space Vector space of dimension 40 over Finite Field of size 59 

and output space The Cartesian product of (Vector space of dimension 40 

over Finite Field of size 59, Vector space of dimension 40 over Finite Field of size 2) 

 

We can also pass the number of errors and erasures as a couple of integers:: 

 

sage: n_err, n_era = (1, 10), (1, 10) 

sage: Chan = channels.ErrorErasureChannel(GF(59)^40, n_err, n_era) 

sage: Chan 

Error-and-erasure channel creating between 1 and 10 errors and 

between 1 and 10 erasures of input space Vector space of dimension 40 

over Finite Field of size 59 and output space The Cartesian product of 

(Vector space of dimension 40 over Finite Field of size 59, 

Vector space of dimension 40 over Finite Field of size 2) 

""" 

 

def __init__(self, space, number_errors, number_erasures): 

r""" 

 

 

TESTS: 

 

If the sum of number of errors and number of erasures 

exceeds (or may exceed, in the case of tuples) the dimension of the input space, 

it will return an error:: 

 

sage: n_err, n_era = 21, 21 

sage: Chan = channels.ErrorErasureChannel(GF(59)^40, n_err, n_era) 

Traceback (most recent call last): 

... 

ValueError: The total number of errors and erasures can not exceed the dimension of the input space 

""" 

if isinstance(number_errors, (Integer, int)): 

number_errors = (number_errors, number_errors) 

if not isinstance(number_errors, (tuple, list)): 

raise ValueError("number_errors must be a tuple, a list, an Integer or a Python int") 

 

if isinstance(number_erasures, (Integer, int)): 

number_erasures = (number_erasures, number_erasures) 

if not isinstance(number_erasures, (tuple, list)): 

raise ValueError("number_erasures must be a tuple, a list, an Integer or a Python int") 

 

output_space = cartesian_product([space, VectorSpace(GF(2), space.dimension())]) 

super(ErrorErasureChannel, self).__init__(space, output_space) 

if number_errors[1] + number_erasures[1] > space.dimension(): 

raise ValueError("The total number of errors and erasures can not exceed the dimension of the input space") 

self._number_errors = number_errors 

self._number_erasures = number_erasures 

 

def _repr_(self): 

r""" 

Returns a string representation of ``self``. 

 

EXAMPLES:: 

 

sage: n_err, n_era = 21, 21 

sage: Chan = channels.ErrorErasureChannel(GF(59)^50, n_err, n_era) 

sage: Chan 

Error-and-erasure channel creating 21 errors and 21 erasures 

of input space Vector space of dimension 50 over Finite Field of size 59 

and output space The Cartesian product of (Vector space of dimension 50 

over Finite Field of size 59, Vector space of dimension 50 over Finite Field of size 2) 

""" 

no_err = self.number_errors() 

no_era = self.number_erasures() 

return "Error-and-erasure channel creating %s errors and %s erasures of input space %s and output space %s"\ 

% (format_interval(no_err), format_interval(no_era), self.input_space(), self.output_space()) 

 

def _latex_(self): 

r""" 

Returns a latex representation of ``self``. 

 

EXAMPLES:: 

 

sage: n_err, n_era = 21, 21 

sage: Chan = channels.ErrorErasureChannel(GF(59)^50, n_err, n_era) 

sage: latex(Chan) 

\textnormal{Error-and-erasure channel creating 21 errors and 21 erasures 

of input space Vector space of dimension 50 over Finite Field of size 59 

and output space The Cartesian product of (Vector space of dimension 50 

over Finite Field of size 59, Vector space of dimension 50 over Finite Field of size 2)} 

""" 

no_err = self.number_errors() 

no_era = self.number_erasures() 

return "\\textnormal{Error-and-erasure channel creating %s errors and %s erasures of input space %s and output space %s}"\ 

% (format_interval(no_err), format_interval(no_era), self.input_space(), self.output_space()) 

 

def transmit_unsafe(self, message): 

r""" 

Returns ``message`` with as many errors as ``self._number_errors`` in it, and as many erasures 

as ``self._number_erasures`` in it. 

 

If ``self._number_errors`` was passed as an tuple for the number of errors, it will 

pick a random integer between the bounds of the tuple and use it as the number of errors. 

It does the same with ``self._number_erasures``. 

 

All erased positions are set to 0 in the transmitted message. 

It is guaranteed that the erasures and the errors will never overlap: 

the received message will always contains exactly as many errors and erasures 

as expected. 

 

This method does not check if ``message`` belongs to the input space of``self``. 

 

INPUT: 

 

- ``message`` -- a vector 

 

OUTPUT: 

 

- a couple of vectors, namely: 

 

- the transmitted message, which is ``message`` with erroneous and erased positions 

- the erasure vector, which contains ``1`` at the erased positions of the transmitted message 

, 0 elsewhere. 

 

EXAMPLES:: 

 

sage: F = GF(59)^11 

sage: n_err, n_era = 2, 2 

sage: Chan = channels.ErrorErasureChannel(F, n_err, n_era) 

sage: msg = F((3, 14, 15, 9, 26, 53, 58, 9, 7, 9, 3)) 

sage: set_random_seed(10) 

sage: Chan.transmit_unsafe(msg) 

((31, 0, 15, 9, 38, 53, 58, 9, 0, 9, 3), (0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0)) 

""" 

number_errors = randint(*self.number_errors()) 

number_erasures = randint(*self.number_erasures()) 

V = self.input_space() 

n = V.dimension() 

zero = V.base_ring().zero() 

 

errors = sample(range(n), number_errors + number_erasures) 

error_positions = errors[:number_errors] 

erasure_positions = errors[number_errors:] 

 

error_vector = random_error_vector(n, V.base_ring(), error_positions) 

erasure_vector = random_error_vector(n , GF(2), erasure_positions) 

 

message = message + error_vector 

 

for i in erasure_positions: 

message[i] = zero 

return message, erasure_vector 

 

def number_errors(self): 

r""" 

Returns the number of errors created by ``self``. 

 

EXAMPLES:: 

 

sage: n_err, n_era = 3, 0 

sage: Chan = channels.ErrorErasureChannel(GF(59)^6, n_err, n_era) 

sage: Chan.number_errors() 

(3, 3) 

""" 

return self._number_errors 

 

def number_erasures(self): 

r""" 

Returns the number of erasures created by ``self``. 

 

EXAMPLES:: 

 

sage: n_err, n_era = 0, 3 

sage: Chan = channels.ErrorErasureChannel(GF(59)^6, n_err, n_era) 

sage: Chan.number_erasures() 

(3, 3) 

""" 

return self._number_erasures 

 

 

 

 

 

 

 

 

 

 

class QarySymmetricChannel(Channel): 

r""" 

The q-ary symmetric, memoryless communication channel. 

 

Given an alphabet `\Sigma` with `|\Sigma| = q` and an error probability 

`\epsilon`, a q-ary symmetric channel sends an element of `\Sigma` into the 

same element with probability `1 - \epsilon`, and any one of the other `q - 

1` elements with probability `\frac{\epsilon}{q - 1}`. This implementation 

operates over vectors in `\Sigma^n`, and "transmits" each element of the 

vector independently in the above manner. 

 

Though `\Sigma` is usually taken to be a finite field, this implementation 

allows any structure for which Sage can represent `\Sigma^n` and for which 

`\Sigma` has a `random_element()` method. However, beware that if `\Sigma` 

is infinite, errors will not be uniformly distributed (since 

`random_element()` does not draw uniformly at random). 

 

The input space and the output space of this channel are the same: 

`\Sigma^n`. 

 

INPUT: 

 

- ``space`` -- the input and output space of the channel. It has to be 

`GF(q)^n` for some finite field `GF(q)`. 

 

- ``epsilon`` -- the transmission error probability of the individual elements. 

 

EXAMPLES: 

 

We construct a QarySymmetricChannel which corrupts 30% of all transmitted 

symbols:: 

 

sage: epsilon = 0.3 

sage: Chan = channels.QarySymmetricChannel(GF(59)^50, epsilon) 

sage: Chan 

q-ary symmetric channel with error probability 0.300000000000000, 

of input and output space Vector space of dimension 50 over Finite Field of size 59 

""" 

 

def __init__(self, space, epsilon): 

r""" 

TESTS: 

 

If ``space`` is not a vector space, an error is raised:: 

 

sage: epsilon = 0.42 

sage: Chan = channels.QarySymmetricChannel(GF(59), epsilon) 

Traceback (most recent call last): 

... 

ValueError: space has to be of the form Sigma^n, where Sigma has a random_element() method 

 

If ``epsilon`` is not between 0 and 1, an error is raised:: 

 

sage: epsilon = 42 

sage: Chan = channels.QarySymmetricChannel(GF(59)^50, epsilon) 

Traceback (most recent call last): 

... 

ValueError: Error probability must be between 0 and 1 

""" 

if epsilon >= 1 or epsilon <= 0: 

raise ValueError("Error probability must be between 0 and 1") 

 

super(QarySymmetricChannel, self).__init__(space, space) 

self._epsilon = epsilon 

try: 

self.transmit_unsafe(space.random_element()) 

except Exception: 

raise ValueError("space has to be of the form Sigma^n, where Sigma has a random_element() method") 

 

def __repr__(self): 

r""" 

Returns a string representation of ``self``. 

 

EXAMPLES:: 

 

sage: epsilon = 0.3 

sage: Chan = channels.QarySymmetricChannel(GF(59)^50, epsilon) 

sage: Chan 

q-ary symmetric channel with error probability 0.300000000000000, 

of input and output space Vector space of dimension 50 over Finite Field of size 59 

""" 

return "q-ary symmetric channel with error probability %s, of input and output space %s"\ 

% (self.error_probability(), self.input_space()) 

 

def _latex_(self): 

r""" 

Returns a latex representation of ``self``. 

 

EXAMPLES:: 

 

sage: epsilon = 0.3 

sage: Chan = channels.QarySymmetricChannel(GF(59)^50, epsilon) 

sage: latex(Chan) 

\textnormal{q-ary symmetric channel with error probability 0.300000000000000, 

of input and output space Vector space of dimension 50 over Finite Field of size 59} 

""" 

return "\\textnormal{q-ary symmetric channel with error probability %s, of input and output space %s}"\ 

% (self.error_probability(), self.input_space()) 

 

def transmit_unsafe(self, message): 

r""" 

Returns ``message`` where each of the symbols has been changed to another from the alphabet with 

probability :meth:`error_probability`. 

 

This method does not check if ``message`` belongs to the input space of``self``. 

 

INPUT: 

 

- ``message`` -- a vector 

 

EXAMPLES:: 

 

sage: F = GF(59)^11 

sage: epsilon = 0.3 

sage: Chan = channels.QarySymmetricChannel(F, epsilon) 

sage: msg = F((3, 14, 15, 9, 26, 53, 58, 9, 7, 9, 3)) 

sage: set_random_seed(10) 

sage: Chan.transmit_unsafe(msg) 

(3, 14, 15, 53, 12, 53, 58, 9, 55, 9, 3) 

""" 

epsilon = self.error_probability() 

V = self.input_space() 

F = V.base_ring() 

msg = copy(message.list()) 

for i in range(len(msg)): 

if random() <= epsilon: 

a = F.random_element() 

while a == msg[i]: 

a = F.random_element() 

msg[i] = a 

return V(msg) 

 

def error_probability(self): 

r""" 

Returns the error probability of a single symbol transmission of 

``self``. 

 

EXAMPLES:: 

 

sage: epsilon = 0.3 

sage: Chan = channels.QarySymmetricChannel(GF(59)^50, epsilon) 

sage: Chan.error_probability() 

0.300000000000000 

""" 

return self._epsilon 

 

def probability_of_exactly_t_errors(self, t): 

r""" 

Returns the probability ``self`` has to return 

exactly ``t`` errors. 

 

INPUT: 

 

- ``t`` -- an integer 

 

EXAMPLES:: 

 

sage: epsilon = 0.3 

sage: Chan = channels.QarySymmetricChannel(GF(59)^50, epsilon) 

sage: Chan.probability_of_exactly_t_errors(15) 

0.122346861835401 

""" 

n = self.input_space().dimension() 

epsilon = self.error_probability() 

return binomial(n, t) * epsilon**t * (1-epsilon)**(n-t) 

 

def probability_of_at_most_t_errors(self, t): 

r""" 

Returns the probability ``self`` has to return 

at most ``t`` errors. 

 

INPUT: 

 

- ``t`` -- an integer 

 

EXAMPLES:: 

 

sage: epsilon = 0.3 

sage: Chan = channels.QarySymmetricChannel(GF(59)^50, epsilon) 

sage: Chan.probability_of_at_most_t_errors(20) 

0.952236164579467 

""" 

return sum(self.probability_of_exactly_t_errors(i) 

for i in range(t+1))