File: codenorm.gi

package info (click to toggle)
gap-guava 3.20%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,804 kB
  • sloc: ansic: 20,501; xml: 10,382; makefile: 254; sh: 55
file content (293 lines) | stat: -rw-r--r-- 9,093 bytes parent folder | download | duplicates (3)
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
#############################################################################
##
#A  codenorm.gi             GUAVA library                       Reinald Baart
#A                                                         Jasper Cramwinckel
#A                                                            Erik Roijackers
#A                                                                Eric Minkes
##
##  This file contains functions for calculating code norms
##

########################################################################
##
#F  CoordinateSubCode( <code>, <i>, <element> )
##
##  Return the subcode of <code>, that has elements
##  with an <element> in coordinate position <i>.
##  If no elements have an <element> in position <i>, return false.
##

InstallMethod(CoordinateSubCode, "method for unrestricted code, position, FFE",
    true, [IsCode, IsInt, IsFFE], 0,
function ( code, i, element )
    local els;

    if i < 1 or i > WordLength( code ) then
        Error( "CoordinateSubCode: <i> must lie in the range [ 1 .. n ]" );
    fi;
    if not ( element in AsSSortedList( LeftActingDomain( code ) ) ) then
        Error( "CoordinateSubCode: <element> must be an element of ",
                LeftActingDomain( code ) );
    fi;

    els := AsSSortedList( code );
    els := VectorCodeword( els );
    els := Filtered( els, x -> x[ i ] = element );
    if Length( els ) = 0 then
        return false;
    else
        return ElementsCode( els, "subcoordinate code",
                                LeftActingDomain( code )  );
    fi;
end);


########################################################################
##
#F  CoordinateNorm( <code>, <i> )
##
##  Returns the norm of code with respect to coordinate i.
##

InstallMethod(CoordinateNorm, "attribute method for unrestricted code",
    true, [IsCode], 0,
function( code )
    # This is a mutable attribute.  Initial value is all -1, updated as
    # other method of CoordinateNorm is called.
    return List( [ 1 .. WordLength( code ) ], x -> -1 );
end);


InstallOtherMethod(CoordinateNorm, "method for unrestricted code, coordinate",
    true, [IsCode, IsInt], 0,
function ( code, i )

    local max, els, subcode, f, j, w, n, c;

    if i < 1 or i > WordLength( code ) then
        Error( "CoordinateNorm: <i> must lie in the range [ 1 .. n ]" );
    fi;

    if CoordinateNorm( code )[ i ] = -1 then
        max := -1;
        els := AsSSortedList( LeftActingDomain( code ) );
        subcode := [ 1 .. Length( els ) ];
        f := [ 1 .. Length( els ) ];
        for j in [ 1 .. Length( els ) ] do
            subcode[ j ] := CoordinateSubCode( code, j, els[ j ] );
        od;
        for w in Codeword( CosetLeadersMatFFE( CheckMat( code ),
                           LeftActingDomain( code ) ) ) do
            for j in [ 1 .. Length( els ) ] do
                if subcode[ j ] = false then
                    f[ j ] := WordLength( code );
                else
                    f[ j ] := MinimumDistance( subcode[ j ], w );
                fi;
            od;
            n := Sum( f );
            if n > max then
                max := n;
            fi;
        od;
        c := CoordinateNorm( code );
        c[ i ] := max;
    fi;
    return CoordinateNorm(code)[i];
end);


########################################################################
##
#F  CodeNorm( <code> )
##
##  Return the norm of code.
##  The norm of code is the minimum of the coordinate norms
##  of code with respect to i = 1, ..., n.
##

InstallMethod(CodeNorm, "method for unrestricted code", true,
    [IsCode], 0,
function( code )

    return Minimum( List( [ 1 .. WordLength( code ) ],
                           x -> CoordinateNorm( code, x ) ) );
end);


########################################################################
##
#F  IsCoordinateAcceptable( <code>, <i> )
##
##  Test whether coordinate i of <code> is acceptable.
##  (a coordinate is acceptable if the norm of code with respect to
##   that coordinate is less than or equal to one plus two times the
##   covering radius of code).

InstallMethod(IsCoordinateAcceptable, "method for unrestricted code, position",
    true, [IsCode, IsInt], 0,
function ( code, i )

    local cr;

    cr := CoveringRadius( code );
    if IsInt( cr ) then
        if CoordinateNorm( code, i ) <= 2 * cr + 1 then
            return true;
        else
            return false;
        fi;
    else
        Error( "IsCoordinateAcceptable: Sorry, the covering radius is ",
               "not known and not easy to compute." );
    fi;

end);


########################################################################
##
#F  IsNormalCode( <code> )
##
##  Return true if code is a normal code, false otherwise.
##  A code is called normal if its norm is smaller than or
##  equal to two times its covering radius + one.
##

InstallMethod(IsNormalCode, "method for unrestricted code", true,
    [IsCode], 0,
function( code )
    local n, k, d, r, i, isnormal;

    if LeftActingDomain( code ) <> GF(2) then
        Error( "IsNormalCode: <code> must be a binary code" );
    elif IsLinearCode( code ) then
        return IsNormalCode( code );
    else
        n := WordLength( code );
        k := Dimension( code );
        d := MinimumDistance( code );
        r := CoveringRadius( code );
        if not IsInt( r ) then
            r := -1;
        fi;
        if d = 2 * r
           or ( d = 2 * r - 1 and EuclideanRemainder( n, r ) <> 0 )
           or ( r = 1 and n <= 9 )
           or ( r = 1 and Size( code ) <= 95 )
           then
            return true;
        else
            if r >= 0 then
                i := 1;
                isnormal := false;
                while i <= n and not isnormal do
                    isnormal := IsCoordinateAcceptable( code, i );
                    i := i + 1;
                od;
                return isnormal;
            else
                Error( "IsNormalCode: sorry, the covering radius for ",
                       "this code has not yet been computed." );
            fi;
        fi;
    fi;
end);

InstallMethod(IsNormalCode, "method for linear code", true,
    [IsLinearCode], 0,
function( code )
    local n, k, d, r, i, isnormal;
    if LeftActingDomain( code ) <> GF(2) then
        Error("IsNormalCode: <code> must be a binary code");
    fi;

    n := WordLength( code );
    k := Dimension( code );
    d := MinimumDistance( code );
    r := CoveringRadius( code );
    if not IsInt( r ) then
        r := -1;
    fi;
    if d = 2 * r
       or ( d = 2 * r - 1 and EuclideanRemainder( n, r ) <> 0 )
       or ( r = 1 and n <= 9 )
       or ( r = 1 and Size( code ) <= 95 )
       # the following conditions are only valid for linear codes
       or ( n <= 15 )
       or ( k <= 5 )
       or ( n-k <= 7 )
       or ( d <= 4 )
       or ( r >= 0 and r <= 3 )
       or ( IsPerfectCode( code ) )
       then
        return true;
    else
        if r >= 0 then
            # a code is normal if one of the coordinates is acceptable
            i := 1;
            isnormal := false;
            while i <= n and not isnormal do
                isnormal := IsCoordinateAcceptable( code, i );
                i := i + 1;
            od;
            return isnormal;
        else
            Error( "IsNormalCode: sorry, the covering radius for ",
                   "this code has not yet been computed." );
        fi;
    fi;
end);


########################################################################
##
#F  GeneralizedCodeNorm( <code>, <code1>, <code2>, ... , <codek> )
##
##  Compute the k-norm of code with respect to the k subcode
##  code1, code2, ... , codek.
##

InstallGlobalFunction(GeneralizedCodeNorm,
function ( arg )
    local i, mindist, min, max, globalmax, x, union,word;

    if Length( arg ) < 2 then
        Error( "GeneralizedCodeNorm: no subcodes are specified" );
    fi;
    if not IsCode( arg[ 1 ] ) then
        Error( "GeneralizedCodeNorm: <code> must be a code" );
    fi;
    union := arg[ 2 ];
    for i in [ 1 .. Length( arg ) - 1 ] do
        if WordLength( arg[ i + 1 ] ) <> WordLength( arg[ 1 ] ) then
            Error( "GeneralizedCodeNorm: length of code ", i,
                   " is not equal to the length of <code>" );
        fi;
        if not ( arg[ i + 1 ] in arg[ 1 ] ) then
            Error( "GeneralizedCodeNorm: code ", i,
                   " is not a subcode of code." );
        fi;
        if i > 1 then
            union := AddedElementsCode( union,
                       AsSSortedList( arg[ i + 1 ] ) );
        fi;
    od;
    if arg[ 1 ] <> union then
        Error( "GeneralizedCodeNorm: <code> is not the union of the ",
               "subcodes" );
    fi;

    globalmax := -1;
    for word in AsSSortedList( arg[ 1 ] ) do
        mindist := List( [ 2 .. Length( arg ) ],
                          x -> MinimumDistance( arg[ x ], word ) );
        min := Minimum( mindist );
        max := Maximum( mindist );
        if min + max > globalmax then
            globalmax := min + max;
        fi;
    od;
    return globalmax;
end);