Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/coding/code_bounds.py
8815 views
1
r"""
2
Bounds for Parameters of Codes
3
4
This module provided some upper and lower bounds for the parameters
5
of codes.
6
7
AUTHORS:
8
9
- David Joyner (2006-07): initial implementation.
10
11
- William Stein (2006-07): minor editing of docs and code (fixed bug
12
in elias_bound_asymp)
13
14
- David Joyner (2006-07): fixed dimension_upper_bound to return an
15
integer, added example to elias_bound_asymp.
16
17
- " (2009-05): removed all calls to Guava but left it as an option.
18
19
- Dima Pasechnik (2012-10): added LP bounds.
20
21
Let `F` be a finite field (we denote the finite field with
22
`q` elements by `\GF{q}`).
23
A subset `C` of `V=F^n` is called a code of
24
length `n`. A subspace of `V` (with the standard
25
basis) is called a linear code of length `n`. If its
26
dimension is denoted `k` then we typically store a basis of
27
`C` as a `k\times n` matrix (the rows are the basis
28
vectors). If `F=\GF{2}` then `C` is
29
called a binary code. If `F` has `q` elements
30
then `C` is called a `q`-ary code. The elements
31
of a code `C` are called codewords. The information rate
32
of `C` is
33
34
35
.. math::
36
37
R={\frac{\log_q\vert C\vert}{n}},
38
39
40
where `\vert C\vert` denotes the number of elements of
41
`C`. If `{\bf v}=(v_1,v_2,...,v_n)`,
42
`{\bf w}=(w_1,w_2,...,w_n)` are vectors in
43
`V=F^n` then we define
44
45
46
.. math::
47
48
d({\bf v},{\bf w}) =\vert\{i\ \vert\ 1\leq i\leq n,\ v_i\not= w_i\}\vert
49
50
51
to be the Hamming distance between `{\bf v}` and
52
`{\bf w}`. The function
53
`d:V\times V\rightarrow \Bold{N}` is called the Hamming
54
metric. The weight of a vector (in the Hamming metric) is
55
`d({\bf v},{\bf 0})`. The minimum distance of a linear
56
code is the smallest non-zero weight of a codeword in `C`.
57
The relatively minimum distance is denoted
58
59
60
.. math::
61
62
\delta = d/n.
63
64
A linear code with length
65
`n`, dimension `k`, and minimum distance
66
`d` is called an `[n,k,d]_q`-code and
67
`n,k,d` are called its parameters. A (not necessarily
68
linear) code `C` with length `n`, size
69
`M=|C|`, and minimum distance `d` is called an
70
`(n,M,d)_q`-code (using parentheses instead of square
71
brackets). Of course, `k=\log_q(M)` for linear codes.
72
73
What is the "best" code of a given length? Let `F` be a
74
finite field with `q` elements. Let `A_q(n,d)`
75
denote the largest `M` such that there exists a
76
`(n,M,d)` code in `F^n`. Let `B_q(n,d)`
77
(also denoted `A^{lin}_q(n,d)`) denote the largest
78
`k` such that there exists a `[n,k,d]` code in
79
`F^n`. (Of course, `A_q(n,d)\geq B_q(n,d)`.)
80
Determining `A_q(n,d)` and `B_q(n,d)` is one of
81
the main problems in the theory of error-correcting codes.
82
83
These quantities related to solving a generalization of the
84
childhood game of "20 questions".
85
86
GAME: Player 1 secretly chooses a number from `1` to
87
`M` (`M` is large but fixed). Player 2 asks a
88
series of "yes/no questions" in an attempt to determine that
89
number. Player 1 may lie at most `e` times
90
(`e\geq 0` is fixed). What is the minimum number of "yes/no
91
questions" Player 2 must ask to (always) be able to correctly
92
determine the number Player 1 chose?
93
94
If feedback is not allowed (the only situation considered here),
95
call this minimum number `g(M,e)`.
96
97
Lemma: For fixed `e` and `M`, `g(M,e)` is
98
the smallest `n` such that `A_2(n,2e+1)\geq M`.
99
100
Thus, solving the solving a generalization of the game of "20
101
questions" is equivalent to determining `A_2(n,d)`! Using
102
Sage, you can determine the best known estimates for this number in
103
2 ways:
104
105
1. Indirectly, using best_known_linear_code_www(n, k, F),
106
which connects to the website http://www.codetables.de by Markus Grassl;
107
108
2. codesize_upper_bound(n,d,q), dimension_upper_bound(n,d,q),
109
and best_known_linear_code(n, k, F).
110
111
The output of :func:`best_known_linear_code`,
112
:func:`best_known_linear_code_www`, or :func:`dimension_upper_bound` would
113
give only special solutions to the GAME because the bounds are applicable
114
to only linear codes. The output of :func:`codesize_upper_bound` would give
115
the best possible solution, that may belong to a linear or nonlinear code.
116
117
This module implements:
118
119
- codesize_upper_bound(n,d,q), for the best known (as of May,
120
2006) upper bound A(n,d) for the size of a code of length n,
121
minimum distance d over a field of size q.
122
123
- dimension_upper_bound(n,d,q), an upper bound
124
`B(n,d)=B_q(n,d)` for the dimension of a linear code of
125
length n, minimum distance d over a field of size q.
126
127
- gilbert_lower_bound(n,q,d), a lower bound for number of
128
elements in the largest code of min distance d in
129
`\GF{q}^n`.
130
131
- gv_info_rate(n,delta,q), `log_q(GLB)/n`, where GLB is
132
the Gilbert lower bound and delta = d/n.
133
134
- gv_bound_asymp(delta,q), asymptotic analog of Gilbert lower
135
bound.
136
137
- plotkin_upper_bound(n,q,d)
138
139
- plotkin_bound_asymp(delta,q), asymptotic analog of Plotkin
140
bound.
141
142
- griesmer_upper_bound(n,q,d)
143
144
- elias_upper_bound(n,q,d)
145
146
- elias_bound_asymp(delta,q), asymptotic analog of Elias bound.
147
148
- hamming_upper_bound(n,q,d)
149
150
- hamming_bound_asymp(delta,q), asymptotic analog of Hamming
151
bound.
152
153
- singleton_upper_bound(n,q,d)
154
155
- singleton_bound_asymp(delta,q), asymptotic analog of Singleton
156
bound.
157
158
- mrrw1_bound_asymp(delta,q), "first" asymptotic
159
McEliese-Rumsey-Rodemich-Welsh bound for the information rate.
160
161
- Delsarte (a.k.a. Linear Programming (LP)) upper bounds.
162
163
PROBLEM: In this module we shall typically either (a) seek bounds
164
on k, given n, d, q, (b) seek bounds on R, delta, q (assuming n is
165
"infinity").
166
167
TODO:
168
169
- Johnson bounds for binary codes.
170
171
- mrrw2_bound_asymp(delta,q), "second" asymptotic
172
McEliese-Rumsey-Rodemich-Welsh bound for the information rate.
173
174
REFERENCES:
175
176
- C. Huffman, V. Pless, Fundamentals of error-correcting codes,
177
Cambridge Univ. Press, 2003.
178
"""
179
180
#*****************************************************************************
181
# Copyright (C) 2006 David Joyner <[email protected]>
182
# 2006 William Stein <[email protected]>
183
#
184
# Distributed under the terms of the GNU General Public License (GPL)
185
#
186
# http://www.gnu.org/licenses/
187
#*****************************************************************************
188
189
from sage.interfaces.all import gap
190
from sage.rings.all import QQ, RR, ZZ, RDF
191
from sage.rings.arith import factorial
192
from sage.functions.all import log, sqrt
193
from sage.misc.decorators import rename_keyword
194
from delsarte_bounds import delsarte_bound_hamming_space, \
195
delsarte_bound_additive_hamming_space
196
197
@rename_keyword(deprecation=6094, method="algorithm")
198
def codesize_upper_bound(n,d,q,algorithm=None):
199
r"""
200
This computes the minimum value of the upper bound using the
201
methods of Singleton, Hamming, Plotkin, and Elias.
202
203
If algorithm="gap" then this returns the best known upper
204
bound `A(n,d)=A_q(n,d)` for the size of a code of length n,
205
minimum distance d over a field of size q. The function first
206
checks for trivial cases (like d=1 or n=d), and if the value
207
is in the built-in table. Then it calculates the minimum value
208
of the upper bound using the algorithms of Singleton, Hamming,
209
Johnson, Plotkin and Elias. If the code is binary,
210
`A(n, 2\ell-1) = A(n+1,2\ell)`, so the function
211
takes the minimum of the values obtained from all algorithms for the
212
parameters `(n, 2\ell-1)` and `(n+1, 2\ell)`. This
213
wraps GUAVA's (i.e. GAP's package Guava) UpperBound( n, d, q ).
214
215
If algorithm="LP" then this returns the Delsarte (a.k.a. Linear
216
Programming) upper bound.
217
218
EXAMPLES::
219
220
sage: codesize_upper_bound(10,3,2)
221
93
222
sage: codesize_upper_bound(24,8,2,algorithm="LP")
223
4096
224
sage: codesize_upper_bound(10,3,2,algorithm="gap") # optional - gap_packages (Guava package)
225
85
226
sage: codesize_upper_bound(11,3,4,algorithm=None)
227
123361
228
sage: codesize_upper_bound(11,3,4,algorithm="gap") # optional - gap_packages (Guava package)
229
123361
230
sage: codesize_upper_bound(11,3,4,algorithm="LP")
231
109226
232
233
"""
234
if algorithm=="gap":
235
return int(gap.eval("UpperBound(%s,%s,%s)"%( n, d, q )))
236
if algorithm=="LP":
237
return int(delsarte_bound_hamming_space(n,d,q))
238
else:
239
eub = elias_upper_bound(n,q,d)
240
gub = griesmer_upper_bound(n,q,d)
241
hub = hamming_upper_bound(n,q,d)
242
pub = plotkin_upper_bound(n,q,d)
243
sub = singleton_upper_bound(n,q,d)
244
return min([eub,gub,hub,pub,sub])
245
246
@rename_keyword(deprecation=6094, method="algorithm")
247
def dimension_upper_bound(n,d,q,algorithm=None):
248
r"""
249
Returns an upper bound `B(n,d) = B_q(n,d)` for the
250
dimension of a linear code of length n, minimum distance d over a
251
field of size q.
252
Parameter "algorithm" has the same meaning as in :func:`codesize_upper_bound`
253
254
EXAMPLES::
255
256
sage: dimension_upper_bound(10,3,2)
257
6
258
sage: dimension_upper_bound(30,15,4)
259
13
260
sage: dimension_upper_bound(30,15,4,algorithm="LP")
261
12
262
263
"""
264
q = ZZ(q)
265
if algorithm=="LP":
266
return delsarte_bound_additive_hamming_space(n,d,q)
267
268
else: # algorithm==None or algorithm="gap":
269
return int(log(codesize_upper_bound(n,d,q,algorithm=algorithm),q))
270
271
272
def volume_hamming(n,q,r):
273
r"""
274
Returns number of elements in a Hamming ball of radius r in `\GF{q}^n`.
275
Agrees with Guava's SphereContent(n,r,GF(q)).
276
277
EXAMPLES::
278
279
sage: volume_hamming(10,2,3)
280
176
281
"""
282
ans=sum([factorial(n)/(factorial(i)*factorial(n-i))*(q-1)**i for i in range(r+1)])
283
return ans
284
285
def gilbert_lower_bound(n,q,d):
286
r"""
287
Returns lower bound for number of elements in the largest code of
288
minimum distance d in `\GF{q}^n`.
289
290
EXAMPLES::
291
292
sage: gilbert_lower_bound(10,2,3)
293
128/7
294
"""
295
ans=q**n/volume_hamming(n,q,d-1)
296
return ans
297
298
@rename_keyword(deprecation=6094, method="algorithm")
299
def plotkin_upper_bound(n,q,d, algorithm=None):
300
r"""
301
Returns Plotkin upper bound for number of elements in the largest
302
code of minimum distance d in `\GF{q}^n`.
303
304
The algorithm="gap" option wraps Guava's UpperBoundPlotkin.
305
306
EXAMPLES::
307
308
sage: plotkin_upper_bound(10,2,3)
309
192
310
sage: plotkin_upper_bound(10,2,3,algorithm="gap") # optional - gap_packages (Guava package)
311
192
312
"""
313
if algorithm=="gap":
314
ans=gap.eval("UpperBoundPlotkin(%s,%s,%s)"%(n,d,q))
315
#print "calling Guava ..."
316
return QQ(ans)
317
else:
318
t = 1 - 1/q
319
if (q==2) and (n == 2*d) and (d%2 == 0):
320
return 4*d
321
elif (q==2) and (n == 2*d + 1) and (d%2 == 1):
322
return 4*d + 4
323
elif d > t*n:
324
return int(d/( d - t*n))
325
elif d < t*n + 1:
326
fact = (d-1) / t
327
if RR(fact)==RR(int(fact)):
328
fact = int(fact) + 1
329
return int(d/( d - t * fact)) * q**(n - fact)
330
331
@rename_keyword(deprecation=6094, method="algorithm")
332
def griesmer_upper_bound(n,q,d,algorithm=None):
333
r"""
334
Returns the Griesmer upper bound for number of elements in the
335
largest code of minimum distance d in `\GF{q}^n`.
336
Wraps GAP's UpperBoundGriesmer.
337
338
EXAMPLES::
339
340
sage: griesmer_upper_bound(10,2,3)
341
128
342
sage: griesmer_upper_bound(10,2,3,algorithm="gap") # optional - gap_packages (Guava package)
343
128
344
"""
345
if algorithm=="gap":
346
#print "calling Guava ..."
347
ans=gap.eval("UpperBoundGriesmer(%s,%s,%s)"%(n,d,q))
348
return QQ(ans)
349
else:
350
den = 1
351
s = 0
352
k = 0
353
add = 0
354
while s <= n:
355
if not(add == 1):
356
if d%den==0:
357
add = int(d/den)
358
else:
359
add = int(d/den)+1
360
s = s + add
361
den = den * q
362
k = k + 1
363
return q**(k-1)
364
365
366
@rename_keyword(deprecation=6094, method="algorithm")
367
def elias_upper_bound(n,q,d,algorithm=None):
368
r"""
369
Returns the Elias upper bound for number of elements in the largest
370
code of minimum distance d in `\GF{q}^n`. Wraps
371
GAP's UpperBoundElias.
372
373
EXAMPLES::
374
375
sage: elias_upper_bound(10,2,3)
376
232
377
sage: elias_upper_bound(10,2,3,algorithm="gap") # optional - gap_packages (Guava package)
378
232
379
380
"""
381
r = 1-1/q
382
if algorithm=="gap":
383
#print "calling Guava ..."
384
ans=gap.eval("UpperBoundElias(%s,%s,%s)"%(n,d,q))
385
return QQ(ans)
386
else:
387
def ff(n,d,w,q):
388
return r*n*d*q**n/((w**2-2*r*n*w+r*n*d)*volume_hamming(n,q,w));
389
def get_list(n,d,q):
390
I = []
391
for i in range(1,int(r*n)+1):
392
if i**2-2*r*n*i+r*n*d>0:
393
I.append(i)
394
return I
395
I = get_list(n,d,q)
396
bnd = min([ff(n,d,w,q) for w in I])
397
return int(bnd)
398
399
def hamming_upper_bound(n,q,d):
400
r"""
401
Returns the Hamming upper bound for number of elements in the
402
largest code of minimum distance d in `\GF{q}^n`.
403
Wraps GAP's UpperBoundHamming.
404
405
The Hamming bound (also known as the sphere packing bound) returns
406
an upper bound on the size of a code of length n, minimum distance
407
d, over a field of size q. The Hamming bound is obtained by
408
dividing the contents of the entire space
409
`\GF{q}^n` by the contents of a ball with radius
410
floor((d-1)/2). As all these balls are disjoint, they can never
411
contain more than the whole vector space.
412
413
414
.. math::
415
416
M \leq {q^n \over V(n,e)},
417
418
419
420
where M is the maximum number of codewords and `V(n,e)` is
421
equal to the contents of a ball of radius e. This bound is useful
422
for small values of d. Codes for which equality holds are called
423
perfect.
424
425
EXAMPLES::
426
427
sage: hamming_upper_bound(10,2,3)
428
93
429
"""
430
return int((q**n)/(volume_hamming(n, q, int((d-1)/2))))
431
432
def singleton_upper_bound(n,q,d):
433
r"""
434
Returns the Singleton upper bound for number of elements in the
435
largest code of minimum distance d in `\GF{q}^n`.
436
Wraps GAP's UpperBoundSingleton.
437
438
This bound is based on the shortening of codes. By shortening an
439
`(n, M, d)` code d-1 times, an `(n-d+1,M,1)` code
440
results, with `M \leq q^n-d+1`. Thus
441
442
443
.. math::
444
445
M \leq q^{n-d+1}.
446
447
448
449
Codes that meet this bound are called maximum distance separable
450
(MDS).
451
452
EXAMPLES::
453
454
sage: singleton_upper_bound(10,2,3)
455
256
456
"""
457
return q**(n - d + 1)
458
459
def gv_info_rate(n,delta,q):
460
"""
461
GV lower bound for information rate of a q-ary code of length n
462
minimum distance delta\*n
463
464
EXAMPLES::
465
466
sage: RDF(gv_info_rate(100,1/4,3))
467
0.367049926083
468
"""
469
q = ZZ(q)
470
ans=log(gilbert_lower_bound(n,q,int(n*delta)),q)/n
471
return ans
472
473
def entropy(x,q):
474
"""
475
Computes the entropy at `x` on the `q`-ary symmetric channel.
476
477
INPUT:
478
479
- ``x`` - real number in the interval `[0, 1]`.
480
481
- ``q`` - integer greater than 1. This is the base of the logarithm.
482
483
EXAMPLES::
484
485
sage: entropy(0, 2)
486
0
487
sage: entropy(1/5,4)
488
1/5*log(3)/log(4) - 4/5*log(4/5)/log(4) - 1/5*log(1/5)/log(4)
489
sage: entropy(1, 3)
490
log(2)/log(3)
491
492
Check that values not within the limits are properly handled::
493
494
sage: entropy(1.1, 2)
495
Traceback (most recent call last):
496
...
497
ValueError: The entropy function is defined only for x in the interval [0, 1]
498
sage: entropy(1, 1)
499
Traceback (most recent call last):
500
...
501
ValueError: The value q must be an integer greater than 1
502
"""
503
if x < 0 or x > 1:
504
raise ValueError("The entropy function is defined only for x in the"
505
" interval [0, 1]")
506
q = ZZ(q) # This will error out if q is not an integer
507
if q < 2: # Here we check that q is actually at least 2
508
raise ValueError("The value q must be an integer greater than 1")
509
if x == 0:
510
return 0
511
if x == 1:
512
return log(q-1,q)
513
H = x*log(q-1,q)-x*log(x,q)-(1-x)*log(1-x,q)
514
return H
515
516
def gv_bound_asymp(delta,q):
517
"""
518
Computes the asymptotic GV bound for the information rate, R.
519
520
EXAMPLES::
521
522
sage: RDF(gv_bound_asymp(1/4,2))
523
0.188721875541
524
sage: f = lambda x: gv_bound_asymp(x,2)
525
sage: plot(f,0,1)
526
"""
527
return (1-entropy(delta,q))
528
529
530
def hamming_bound_asymp(delta,q):
531
"""
532
Computes the asymptotic Hamming bound for the information rate.
533
534
EXAMPLES::
535
536
sage: RDF(hamming_bound_asymp(1/4,2))
537
0.4564355568
538
sage: f = lambda x: hamming_bound_asymp(x,2)
539
sage: plot(f,0,1)
540
"""
541
return (1-entropy(delta/2,q))
542
543
def singleton_bound_asymp(delta,q):
544
"""
545
Computes the asymptotic Singleton bound for the information rate.
546
547
EXAMPLES::
548
549
sage: singleton_bound_asymp(1/4,2)
550
3/4
551
sage: f = lambda x: singleton_bound_asymp(x,2)
552
sage: plot(f,0,1)
553
"""
554
return (1-delta)
555
556
def plotkin_bound_asymp(delta,q):
557
"""
558
Computes the asymptotic Plotkin bound for the information rate,
559
provided `0 < \delta < 1-1/q`.
560
561
EXAMPLES::
562
563
sage: plotkin_bound_asymp(1/4,2)
564
1/2
565
"""
566
r = 1-1/q
567
return (1-delta/r)
568
569
def elias_bound_asymp(delta,q):
570
"""
571
Computes the asymptotic Elias bound for the information rate,
572
provided `0 < \delta < 1-1/q`.
573
574
EXAMPLES::
575
576
sage: elias_bound_asymp(1/4,2)
577
0.39912396330...
578
"""
579
r = 1-1/q
580
return RDF((1-entropy(r-sqrt(r*(r-delta)), q)))
581
582
def mrrw1_bound_asymp(delta,q):
583
"""
584
Computes the first asymptotic McEliese-Rumsey-Rodemich-Welsh bound
585
for the information rate, provided `0 < \delta < 1-1/q`.
586
587
EXAMPLES::
588
589
sage: mrrw1_bound_asymp(1/4,2)
590
0.354578902665
591
"""
592
return RDF(entropy((q-1-delta*(q-2)-2*sqrt((q-1)*delta*(1-delta)))/q,q))
593
594