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

r""" 

Parity-check code 

 

A simple way of detecting up to one error is to use the device of adding a 

parity check to ensure that the sum of the digits in a transmitted word is 

even. 

 

A parity-check code of dimension `k` over `F_q` is the set: 

`\{(m_1, m_2, \dots, m_k, - \Sigma_{i=1}^k m_i) \mid (m_1, m_2, \dots, m_k) \in F_q^k\}` 

 

REFERENCE: 

 

.. [W88] Codes and Cryptography, Dominic Welsh, Oxford Sciences 

Publications, 1988 

""" 

 

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

# Copyright (C) 2016 Tania Richmond <tania.richmond@univ-tln.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 sage.coding.linear_code import AbstractLinearCode,\ 

LinearCodeGeneratorMatrixEncoder 

from sage.coding.encoder import Encoder 

from sage.rings.integer import Integer 

from sage.rings.finite_rings.finite_field_constructor import GF 

from sage.matrix.constructor import identity_matrix 

from sage.modules.free_module_element import vector 

from sage.modules.free_module import VectorSpace 

 

 

class ParityCheckCode(AbstractLinearCode): 

r""" 

Representation of a parity-check code. 

 

INPUT: 

 

- ``base_field`` -- the base field over which ``self`` is defined. 

 

- ``dimension`` -- the dimension of ``self``. 

 

EXAMPLES:: 

 

sage: C = codes.ParityCheckCode(GF(5), 7) 

sage: C 

[8, 7] parity-check code over GF(5) 

""" 

 

_registered_encoders = {} 

_registered_decoders = {} 

 

def __init__(self, base_field=GF(2), dimension=7): 

r""" 

Initialize mandatory parameters for a parity-check code object. 

 

INPUT: 

 

- ``base_field`` -- the base field over which ``self`` is defined 

or GF(2) if no base_field. 

 

- ``dimension`` -- the dimension of ``self`` or 7 if no dimension. 

 

EXAMPLES:: 

 

sage: C = codes.ParityCheckCode(GF(5), 7) 

sage: C 

[8, 7] parity-check code over GF(5) 

""" 

 

if not base_field.is_finite(): 

raise ValueError("base_field has to be a finite field") 

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

raise ValueError("dimension must be an integer") 

 

self._dimension = dimension 

super(ParityCheckCode, self).__init__( 

base_field, dimension + 1, "ParityCheckCodeGeneratorMatrixEncoder", 

"Syndrome") 

 

def __eq__(self, other): 

r""" 

Test equality of parity-check code objects. 

 

EXAMPLES:: 

 

sage: C1 = codes.ParityCheckCode(GF(5), 7) 

sage: C2 = codes.ParityCheckCode(GF(5), 7) 

sage: C1 == C2 

True 

""" 

return (isinstance(other, ParityCheckCode) 

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

and self.dimension() == other.dimension()) 

 

def _repr_(self): 

r""" 

Return a string representation of ``self``. 

 

EXAMPLES:: 

 

sage: C = codes.ParityCheckCode(GF(5), 7) 

sage: C 

[8, 7] parity-check code over GF(5) 

""" 

return ("[%s, %s] parity-check code over GF(%s)" 

% (self.length(), self.dimension(), 

self.base_field().cardinality())) 

 

def _latex_(self): 

r""" 

Return a latex representation of ``self``. 

 

EXAMPLES:: 

 

sage: C = codes.ParityCheckCode(GF(5), 7) 

sage: latex(C) 

[8, 7] \textnormal{parity-check code over } \Bold{F}_{5} 

""" 

return "[%s, %s] \\textnormal{parity-check code over } %s"\ 

% (self.length(), self.dimension(), self.base_field()._latex_()) 

 

def minimum_distance(self): 

r""" 

Return the minimum distance of ``self``. 

 

It is always 2 as ``self`` is a parity-check code. 

 

EXAMPLES:: 

 

sage: C = codes.ParityCheckCode(GF(5), 7) 

sage: C.minimum_distance() 

2 

""" 

return 2 

 

 

####################### encoders ############################### 

 

class ParityCheckCodeGeneratorMatrixEncoder(LinearCodeGeneratorMatrixEncoder): 

r""" 

Encoder for parity-check codes which uses a generator matrix to obtain 

codewords. 

 

INPUT: 

 

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

 

EXAMPLES:: 

 

sage: C = codes.ParityCheckCode(GF(5), 7) 

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

sage: E 

Generator matrix-based encoder for [8, 7] parity-check code over GF(5) 

 

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

 

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

sage: E 

Generator matrix-based encoder for [8, 7] parity-check code over GF(5) 

""" 

 

def __init__(self, code): 

r""" 

TESTS: 

 

If ``code`` is not a parity-check code, an error is raised:: 

 

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

sage: codes.encoders.ParityCheckCodeGeneratorMatrixEncoder(C) 

Traceback (most recent call last): 

... 

ValueError: code has to be a parity-check code 

""" 

if not isinstance(code, ParityCheckCode): 

raise ValueError("code has to be a parity-check code") 

super(ParityCheckCodeGeneratorMatrixEncoder, self).__init__(code) 

 

def generator_matrix(self): 

r""" 

Return a generator matrix of ``self``. 

 

EXAMPLES:: 

 

sage: C = codes.ParityCheckCode(GF(5),7) 

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

sage: E.generator_matrix() 

[1 0 0 0 0 0 0 4] 

[0 1 0 0 0 0 0 4] 

[0 0 1 0 0 0 0 4] 

[0 0 0 1 0 0 0 4] 

[0 0 0 0 1 0 0 4] 

[0 0 0 0 0 1 0 4] 

[0 0 0 0 0 0 1 4] 

""" 

k = self.code().dimension() 

field = self.code().base_field() 

G = identity_matrix(field, k) 

G = G.augment(vector(field, [-field.one()] * k)) 

G.set_immutable() 

return G 

 

 

class ParityCheckCodeStraightforwardEncoder(Encoder): 

r""" 

Encoder for parity-check codes which computes the sum of message symbols 

and appends its opposite to the message to obtain codewords. 

 

INPUT: 

 

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

 

EXAMPLES:: 

 

sage: C = codes.ParityCheckCode(GF(5), 7) 

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

sage: E 

Parity-check encoder for the [8, 7] parity-check code over GF(5) 

 

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

 

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

sage: E 

Parity-check encoder for the [8, 7] parity-check code over GF(5) 

""" 

 

def __init__(self, code): 

r""" 

TESTS: 

 

If ``code`` is not a parity-check code, an error is raised:: 

 

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

sage: codes.encoders.ParityCheckCodeStraightforwardEncoder(C) 

Traceback (most recent call last): 

... 

ValueError: code has to be a parity-check code 

""" 

if not isinstance(code, ParityCheckCode): 

raise ValueError("code has to be a parity-check code") 

super(ParityCheckCodeStraightforwardEncoder, self).__init__(code) 

 

def _repr_(self): 

r""" 

Return a string representation of ``self``. 

 

EXAMPLES:: 

 

sage: C = codes.ParityCheckCode(GF(5), 7) 

sage: codes.encoders.ParityCheckCodeStraightforwardEncoder(C) 

Parity-check encoder for the [8, 7] parity-check code over GF(5) 

""" 

return "Parity-check encoder for the %s" % self.code() 

 

def _latex_(self): 

r""" 

Return a latex representation of ``self``. 

 

EXAMPLES:: 

 

sage: C = codes.ParityCheckCode(GF(5), 7) 

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

sage: latex(E) 

\textnormal{Parity-check encoder for the } [8, 7] \textnormal{parity-check code over } \Bold{F}_{5} 

""" 

return ("\\textnormal{Parity-check encoder for the } %s" % 

self.code()._latex_()) 

 

def __eq__(self, other): 

r""" 

Test equality of parity-check code objects. 

 

EXAMPLES:: 

 

sage: C1 = codes.ParityCheckCode(GF(5), 7) 

sage: C2 = codes.ParityCheckCode(GF(5), 7) 

sage: C1 == C2 

True 

""" 

return (isinstance(other, ParityCheckCodeStraightforwardEncoder) 

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

 

def encode(self, message): 

r""" 

Transform the vector ``message`` into a codeword of :meth:`code`. 

 

INPUT: 

 

- ``message`` -- A ``self.code().dimension()``-vector from the message 

space of ``self``. 

 

OUTPUT: 

 

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

 

EXAMPLES:: 

 

sage: C = codes.ParityCheckCode(GF(5),7) 

sage: message = vector(C.base_field(),[1,0,4,2,0,3,2]) 

sage: C.encode(message) 

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

""" 

parity=self.code().base_field().zero() 

for i in message.list(): 

parity += i 

return vector(self.code().base_field(), message.list() + [-parity]) 

 

def unencode_nocheck(self, word): 

r""" 

Return the message corresponding to the vector ``word``. 

 

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

belongs to the code. 

 

INPUT: 

 

- ``word`` -- A ``self.code().length()``-vector from the ambiant space 

of ``self``. 

 

OUTPUT: 

 

- A vector corresponding to the ``self.code().dimension()``-first 

symbols in ``word``. 

 

EXAMPLES:: 

 

sage: C = codes.ParityCheckCode(GF(5), 7) 

sage: word = vector(C.base_field(), [1, 0, 4, 2, 0, 3, 2, 3]) 

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

sage: E.unencode_nocheck(word) 

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

""" 

return word[:-1] 

 

def message_space(self): 

r""" 

Return the message space of ``self``. 

 

EXAMPLES:: 

 

sage: C = codes.ParityCheckCode(GF(5),7) 

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

sage: E.message_space() 

Vector space of dimension 7 over Finite Field of size 5 

""" 

return VectorSpace(self.code().base_field(), self.code().dimension()) 

 

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

 

ParityCheckCode._registered_encoders["ParityCheckCodeGeneratorMatrixEncoder"] = ParityCheckCodeGeneratorMatrixEncoder 

ParityCheckCode._registered_encoders["ParityCheckCodeStraightforwardEncoder"] = ParityCheckCodeStraightforwardEncoder