File: codecstr.gi

package info (click to toggle)
guava 3.6-2
  • links: PTS
  • area: main
  • in suites: lenny, squeeze, wheezy
  • size: 11,788 kB
  • ctags: 2,359
  • sloc: ansic: 20,846; xml: 10,043; sh: 2,855; makefile: 388
file content (1136 lines) | stat: -rw-r--r-- 36,258 bytes parent folder | download | duplicates (2)
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
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
#############################################################################
##
#A  codecstr.gi             GUAVA library                       Reinald Baart
#A                                                         Jasper Cramwinckel
#A                                                            Erik Roijackers
#A                                                                Eric Minkes
#A                                                               David Joyner
##
##  This file contains functions for constructing codes
##
#H  @(#)$Id: codecstr.gi,v 1.6 2003/02/14 02:26:04 gap Exp $
##
Revision.("guava/lib/codecstr_gi") :=
    "@(#)$Id: codecstr.gi,v 1.6 2003/02/14 02:26:04 gap Exp $";

########################################################################
##
#F  AmalgamatedDirectSumCode( <C>, <D>, [, <check> ] )
##
##  Return the amalgamated direct sum code of C en D.
##  
##  This construction is derived from the direct sum construction,
##  but it saves one coordinate over the direct sum.
##  
##  The amalgamated direct sum construction goes as follows:
##
##  Put the generator matrices G and H of C respectively D
##  in standard form as follows:
##     
##     G => [ G' | I ]     and    H => [ I | H' ]
##
##  The generator matrix of the new code then has the following form:
##     
##      [          1 0 ... 0   0 | 0 0 ............ 0 ]
##      [          0 1 ... 0   0 | 0 0 ............ 0 ] 
##      [          .........   . | .................. ] 
##      [   G'     0 0 ... 1   0 | 0 0 ............ 0 ]
##      [                    |---------------|--------]
##      [          0 0 ... 0 | 1 | 0 ... 0 0          ]
##      [--------|-----------|---|                    ]
##      [ 0 0 ............ 0 | 0   1 ... 0 0    H'    ]
##      [ .................. | 0   .........          ]
##      [ 0 0 ............ 0 | 0   0 ... 1 0          ]
##      [ 0 0 ............ 0 | 0   0 ... 0 1          ]
##
##  The codes resulting from [ G' | I ] and [ I | H' ] must
##  be acceptable in the last resp. the first coordinate.
##  Checking whether this is true takes a lot of time, however,
##  and is only performed when the boolean variable check is true.
##

InstallMethod(AmalgamatedDirectSumCode, "method for linear codes, boolean check", 
	true, [IsLinearCode, IsLinearCode, IsBool], 0, 
function(C, D, check) 
	local G, H, Cstandard, Dstandard, NewG, NewH, Nulmat, NewC, i; 

    # check the arguments
    if LeftActingDomain( C ) <> LeftActingDomain( D ) then
        Error( "AmalgamatedDirectSumCode: <C> and <D> must be codes ",
                "over the same field" );
    fi;

	G := ShallowCopy( GeneratorMat( C ) );
	H := ShallowCopy( GeneratorMat( D ) );
	# standard form: G => [ G' | I ]
	PutStandardForm( G, false );
	# standard form: H => [ I | H' ]
	PutStandardForm( H, true );

	# check whether the construction is allowed;
	# this is (at this time) a lot of work
	# maybe it will disappear later
	if check then
		Cstandard := GeneratorMatCode( G, LeftActingDomain( C ) );
		Dstandard := GeneratorMatCode( H, LeftActingDomain( C ) );
		# is the last coordinate of the standardcode C' acceptable ?
		if not IsCoordinateAcceptable( Cstandard, WordLength( Cstandard ) ) then
			Error( "AmalgamatedDirectSumCode: Standard form of <C> is not ",
				   "acceptable in the last coordinate.");
		fi;
		# is the last coordinate of the standardcode D' acceptable ?
		if not IsCoordinateAcceptable( Dstandard, 1 ) then
			Error( "AmalgamatedDirectSumCode: Standard form of <D> is not ",
				   "acceptable in first coordinate.");
		fi;
	fi;

	NewG := G;
	# build upper part of the new generator matrix
	# append n_D - 1 zeroes to all rows of G'
	for i in NewG do
		Append( i, List( [ 1..WordLength( D ) - 1 ], 
				x -> Zero(LeftActingDomain(C) ) ) );
	od;
	# concatenate the last row of G' and the first row of H'
	for i in [ 2 .. WordLength( D ) ] do
		NewG[ Dimension( C ) ][ WordLength( C ) + i - 1 ] :=
		  H[ 1 ][ i ];
	od;
	# throw away the first row of H' (it is already appended)
	NewH := List( [ 2..Length( H ) ], x -> H[ x ] );
	# throw away the first column of H' (it is (1, 0, ..., 0),
	# the one is already present in the new generator matrix)
	NewH := List( [ 1..Length( NewH ) ], x -> List(
				 [ 2 .. WordLength( D ) ], y -> NewH[ x ][ y ] ) );
	# fill the lower left part with zeroes
	Nulmat := List( [ 1 .. Dimension( D ) - 1 ],
					x -> NullVector( WordLength( C ) , LeftActingDomain(C) ) );
	# and put the rest of H' in the lower right corner
	for i in [ 1 .. Length( Nulmat ) ] do
		Append( Nulmat[ i ], NewH[ i ] );
	od;
	# paste the lower and the upper part
	Append( NewG, Nulmat );

	# construct the new code with the new generator matrix
	NewC := GeneratorMatCode( NewG, "amalgamated direct sum code",
								LeftActingDomain( C ) );
	# write history
	NewC!.history := MergeHistories( History( C ), History( D ) );

	# the new covering radius is at most the sum of the
	# two old covering radii, if the old codes are normal
	if HasIsNormalCode( C )
		   and HasIsNormalCode( D )
		   and IsNormalCode( C )
		   and IsNormalCode( D ) then
		if IsBound( C!.boundsCoveringRadius )
			   and IsBound( D!.boundsCoveringRadius ) then
			NewC!.boundsCoveringRadius := [
			  GeneralLowerBoundCoveringRadius( NewC )
			  .. Maximum( C!.boundsCoveringRadius )
				 + Maximum( D!.boundsCoveringRadius ) ];
		fi;
	fi;
	return NewC;
end);

InstallMethod(AmalgamatedDirectSumCode, "two unrestricted codes, boolean check", 
	true, [IsCode, IsCode, IsBool], 0, 
function(C, D, check) 
	local elsC, elsD, i, NewC,
		  newels;

	if LeftActingDomain(C) <> LeftActingDomain(D) then 
		Error("<C> and <D> must be codes over the same field"); 
	elif IsLinearCode(C) and IsLinearCode(D) then 
		# this is much faster, because it only uses 
		# the generator matrices 
		return AmalgamatedDirectSumCode(C, D, check); 
	fi; 

	# check whether the construction is allowed;
	# this is (at this time) a lot of work
	# maybe it will disappear later
	if check then
		# is the last coordinate of C acceptable ?
		if not IsCoordinateAcceptable( C, WordLength( C ) ) then
			Error( "AmalgamatedDirectSumCode: <C> is not ",
				   "acceptable in the last coordinate.");
		fi;
		# is the first coordinate of D acceptable ?
		if not IsCoordinateAcceptable( D, 1 ) then
			Error( "AmalgamatedDirectSumCode: <D> is not ",
				   "acceptable in first coordinate.");
		fi;
	fi;

	# find the elements of the new code
	newels := [];
	for i in AsSSortedList( LeftActingDomain( C ) ) do
		elsC := VectorCodeword( AsSSortedList(
		  CoordinateSubCode( C, WordLength( C ), i ) ) );
		elsD := VectorCodeword( AsSSortedList(
		  CoordinateSubCode( D, 1, i ) ) );
		if Length( elsC ) > 0 and
		   Length( elsD ) > 0 then
			elsD := List( elsD,
			  x -> x{ [ 2 .. WordLength( D ) ] } );
			for i in elsC do
				Append( newels, List( elsD,
				  x -> Concatenation( i, x ) ) );
			od;
		fi;
	od;

	if Length( newels ) = 0 then
		Error( "AmalgamatedDirectSumCode: there are no ",
			   "codewords satisfying the conditions" );
	fi;
	NewC := ElementsCode( newels, "amalgamated direct sum code",
	  LeftActingDomain( C ) );

	# write history
	NewC!.history := MergeHistories( History( C ), History( D ) );

	# the new covering radius is at most the sum of the
	# two old covering radii, if the old codes are normal
	if HasIsNormalCode( C ) 
		   and HasIsNormalCode( D )
		   and IsNormalCode( C )
		   and IsNormalCode( D ) then
		if IsBound( C!.boundsCoveringRadius )
			   and IsBound( D!.boundsCoveringRadius ) then
			NewC!.boundsCoveringRadius := [
			  GeneralLowerBoundCoveringRadius( NewC )
			  .. Maximum( C!.boundsCoveringRadius )
				 + Maximum( C!.boundsCoveringRadius ) ];
		fi;
	fi;
	return NewC;
end);

InstallOtherMethod(AmalgamatedDirectSumCode, "method for two unrestricted codes", 
	true, [IsCode, IsCode], 0, 
function(C, D) 
	return AmalgamatedDirectSumCode(C, D, false); 
end); 


########################################################################
##
#F  BlockwiseDirectSumCode( <C1>, <L1>, <C2>, <L2> )
##
##  Return the blockwise direct sum of C1 and C2 with respect to 
##  the cosets defined by the codewords in L1 and L2.
##
InstallMethod(BlockwiseDirectSumCode, 
	"method for unrestricted codes and lists of codes or of codewords", 
	true, [IsCode, IsList, IsCode, IsList], 0, 
function(C1, L1, C2, L2) 
    
    local k, CosetCodeBlockwiseDirectSumCode,
          SubCodeBlockwiseDirectSumCode, unioncode1, unioncode2, i;

    # check the arguments
    if LeftActingDomain( C1 ) <> LeftActingDomain( C2 ) then
        Error( "BlockwiseDirectSumCode: the fields of <C1> and <C2>",
               "must be the same" );
    fi;
    if Length( L1 ) <> Length( L2 ) then
        Error( "BlockwiseDirectSumCode: <L1> and <L2>",
               "must have equal lengths" );
    fi;
    k := Length( L1 );
    if k = 0 then
        Error( "BlockwiseDirectSumCode: the lists are empty" );
    fi;
    
    CosetCodeBlockwiseDirectSumCode := function( C1, L1, C2, L2 )

        local newels, i, k, subcode1, subcode2, newcode, sum;

        k := Length( L1 );
        # make the elements of the new code
        newels := [];
        for i in [ 1 .. k ] do
            # build subcode 1, using the GUAVA-function CosetCode,
            # which adds the word L1[i] to all codewords of C1
            subcode1 := CosetCode( C1, L1[ i ] );
            subcode2 := CosetCode( C2, L2[ i ] );
            # now build the the direct sum of the two subcodes
            sum := DirectSumCode( subcode1, subcode2 );
            # add the elements of the direct sum-code to
            # the elements we already found
            # in the previous steps
            newels := Union( newels, AsSSortedList( sum ) );
        od;
        # finally build the new code with the computed elements
        newcode := ElementsCode( newels, "blockwise direct sum code",
                           LeftActingDomain( C1 ) );
        # write history
        newcode!.history := MergeHistories( History( C1 ), History( C2 ) );
        return newcode;
    end;

    SubCodeBlockwiseDirectSumCode := function( C1, L1, C2, L2 )

        local unioncode, newels, newcode;

        newels := AsSSortedList( DirectSumCode( L1[ 1 ], L2[ 1 ] ) );
        for i in [ 2 .. Length( L1 ) ] do
            newels := Union( newels, AsSSortedList(
              DirectSumCode( L1[ i ], L2[ i ] ) ) );
        od;
        newcode := ElementsCode( newels, "blockwise direct sum code",
          LeftActingDomain( C1 ) );
        newcode!.history := MergeHistories(
          History( C1 ), History( C2 ) );
        return newcode;
    end;

    if IsCode( L1[ 1 ] ) and IsCode( L2[ 1 ] ) then
        unioncode1 := L1[ 1 ];
        unioncode2 := L2[ 1 ];
        for i in [ 2 .. k ] do

            if not IsCode( L1[ i ] ) or not IsCode( L2[ i ] ) then
                Error( "BlockwiseDirectSumCode: not all elements of ",
                       "the lists are codes" );
            fi;
            unioncode1 := AddedElementsCode( unioncode1,
              AsSSortedList( L1[ i ] ) );
            unioncode2 := AddedElementsCode( unioncode2,
              AsSSortedList( L2[ i ] ) );
            if unioncode1 <> C1 then
                Error( "BlockwiseDirectSumCode: <C1> must be the ",
                       "union of the codes in <L1>" );
            fi;
            if unioncode2 <> C2 then
                Error( "BlockwiseDirectSumCode: <C2> must be the ",
                       "union of the codes in <L2>" );
            fi;
        od;
        return SubCodeBlockwiseDirectSumCode( C1, L1, C2, L2 );
    else
        L1 := Codeword( L1 );
        L2 := Codeword( L2 );
        return CosetCodeBlockwiseDirectSumCode( C1, L1, C2, L2 );
    fi;
end);


########################################################################
##
#F  ExtendedDirectSumCode( <L>, <B>, m )
##
##  The construction as described in the article of Graham and Sloane,
##  section V.
##  ("On the Covering Radius of Codes", R.L. Graham and N.J.A. Sloane,
##    IEEE Information Theory, 1985 pp 385-401)
##

InstallMethod(ExtendedDirectSumCode, "method for linear codes, m", 
	true, [IsLinearCode, IsLinearCode, IsInt], 0, 
function(L, B, m) 
	local m0, GL, GB, NewG, i, j, G, firstzeros, lastzeros, newcode;
    
	# check the arguments
    if WordLength( L ) <> WordLength( B ) then
        Error( "L and B must have the same length" );
    elif m < 1 then
        Error( "m must be a positive integer" );
    fi;

	# if L is a subset of B, then
	# skip the last mth copy of L
	# (it doesn't add anything new to the code )
	if L in B and m > 1 then
		m0 := m - 1;
	else
		m0 := m;
	fi;

	GL := ShallowCopy( GeneratorMat( L ) );
	GB := ShallowCopy( GeneratorMat( B ) );
	# the new generator matrix, fill with zeros first
	NewG := List( [ 1 .. Dimension( L ) * m0 + Dimension( B ) ],
				  x -> NullWord( WordLength( L ) * m ) );
	# first m0 * Dimension(L) rows,
	# form: [ GL 0  ... 0  ]
	#       [ 0  GL ... 0  ]
	#       [ ...........  ]
	#       [ 0  0      GL ] (this row is omitted if L in B)
	for i in [ 1 .. m0 ] do
		# construct rows (i-1)*Dimension(L) till i*Dimension(L)-1
		firstzeros := NullVector( ( i-1 ) * WordLength( L ), 
									LeftActingDomain( L ) );
		lastzeros := NullVector( ( m-i ) * WordLength( L ), 
									LeftActingDomain( L ) );
		for j in [ 1..Dimension( L ) ] do
			NewG[ j + ( i-1 ) * Dimension( L ) ] :=
			  Concatenation( firstzeros, GL[j], lastzeros );
		od;
	od;
	# last row of new generator matrix
	# [ GB GB GB GB ... GB ]
	for i in [ 1..Dimension( B ) ] do
		NewG[ i + m * Dimension( L ) ] :=
		  Concatenation( List( [ 1..m ], x->GB[ i ] ) );
	od;
	newcode := GeneratorMatCode( NewG,
					   Concatenation( String( m ),
							   "-fold extended direct sum code" ),
					   LeftActingDomain( L ) );
	newcode!.history := MergeHistories( History( L ), History( B ) );

	return newcode;
end);

InstallMethod(ExtendedDirectSumCode, "method for unrestricted codes, m", 
	true, [IsCode, IsCode, IsInt], 0, 
function(L, B, m) 
	local SumCode, i, j, el, newcode, lastels;

    if WordLength( L ) <> WordLength( B ) then
		Error( "L and B must have the same length" );
	elif m < 1 then
		Error( "m must be a positive integer" );
	elif IsLinearCode(L) and IsLinearCode( B ) then 
		# this is much faster because it only uses the generator matrices 
		return ExtendedDirectSumCode(L, B, m); 
	fi; 

	SumCode := L;
	for i in [ 2 .. m ] do
		SumCode := DirectSumCode( SumCode, L );
	od;
	lastels := [];
	for i in VectorCodeword( AsSSortedList( B ) ) do
		el := ShallowCopy( i );
		for j in [ 2 .. m ] do
			el := Concatenation( el, i );
		od;
		Append( lastels, el );
	od;
	newcode := AddedElementsCode( SumCode, lastels );
	newcode!.history := MergeHistories( History( L ), History( B ) );
	newcode!.name := Concatenation( String( m ),
	  "-fold extended direct sum code" );

	return newcode;
end);


########################################################################
##
#F  PiecewiseConstantCode( <partition>, <constraints> [, <field> ] )
##

InstallGlobalFunction(PiecewiseConstantCode, 
function ( arg )
    
    local n, partition, constraints, field, i, j, 
          elements, constr, position, newels, addels, 
          ConstantWeightElements, el, sumels;
    
    # check the arguments
    if Length( arg ) < 2 or Length( arg ) > 3 then
        Error( "usage: PiecewiseConstantCode( <partition>, ",
               "<constraints> [, <field> ] )" );
    fi;
    if Length( arg ) = 3 then
        field := arg[ 3 ];
        if not IsField( field ) then
            if not IsInt( field ) then
                Error( "PiecewiseConstantCode: <field> must be a field" );
            else
                field := GF( field );
            fi;
        fi;
    else
        field := GF( 2 );
    fi;
    
    # find out the partition
    partition := arg[ 1 ];
    # allow for an integer as "partition"
    if not IsList( partition ) then
        partition := [ partition ];
    fi;
    for i in partition do
        if not IsInt( i ) then
            Error( "PiecewiseConstantCode: <partition> must be ",
                   "a list of integers" );
        fi;
        if i <= 0 then
            Error( "PiecewiseConstantCode: <partition> must be ",
                   "a list of positive integers" );
        fi;
    od;
    n := Sum( partition );
    
    # check the constraints
    constraints := arg[ 2 ];
    if not IsList( constraints ) then
        constraints := [ constraints ];
    fi;
    for i in [ 1 .. Length( constraints ) ] do
        if not IsList( constraints[ i ] ) then 
            constraints[ i ]:= [ constraints[ i ] ];
        fi;
        if Length( constraints[ i ] ) <> Length( partition ) then
            Error( "PiecewiseConstantCode: the length of constraint ", i,
                   " is not equal to the length of <partition>" );
        fi;
        for j in [ 1 .. Length( constraints[ i ] ) ] do
            if not IsInt( constraints[ i ][ j ] ) then
                Error( "PiecewiseConstantCode: entry ", j,
                       " of constraint ", i,
                       " is not an integer" );
            fi;
            if constraints[ i ][ j ] < 0 
               or constraints[ i ][ j ] > partition[ j ] then
                Error( "PiecewiseConstantCode: entry ", j,
                       " of constraint ", i,
                       " must >= 0 and <= ", partition[ j ] );
            fi;
        od;
    od;
    
    ConstantWeightElements := function( place, weight )
        local els, i, numberofzeros, zerovector;
        if weight = 0 then
            return [ NullVector( n, field ) ];
        fi;
        els := ShallowCopy( VectorCodeword( AsSSortedList( 
                       ConstantWeightSubcode( WholeSpaceCode( 
                               partition[ place ], field ), weight ) ) ) );
        # add zeros to the front
        if place <> 1 then
            numberofzeros := Sum( partition{ [ 1 .. place - 1 ] } );
            zerovector := NullVector( numberofzeros, field );
            for i in [ 1 .. Length( els ) ] do
                els[ i ] := Flat( [ zerovector, els[ i ] ] );
            od;
        fi;
        if place <> Length( partition ) then
            numberofzeros := Sum( partition{ [ place + 1 
                                     .. Length( partition ) ] } );
            zerovector := NullVector( numberofzeros, field );
            for i in [ 1 .. Length( els ) ] do
                els[ i ] := Flat( [ els[ i ], zerovector ] );
            od;
        fi;
        return els;
    end;
    
    # make the new code
    elements := [ ];
    for constr in constraints do
        newels := [ ];
        for i in [ 1 .. Length( partition ) ] do
            addels := ConstantWeightElements( i, constr[ i ] );
            if Length( newels ) > 0 then
                sumels := [ ];
                for el in addels do
                    Append( sumels, List( newels, x -> x + el ) );
                od;
                newels := ShallowCopy( sumels );
            else
                newels := ShallowCopy( addels );
            fi;
        od;
        Append( elements, newels );
    od;
    return ElementsCode( elements, "piecewise constant code", field );
end);


########################################################################
##
#F  GabidulinCode( );
##

##  Use of the Boolean check is an unadvertised feature.  
##  It gives the EnlargedGabidulinCode a back door past the 
##  restriction on m.  Unfortunately, the cleanest way I 
##  could think of to translate it to GAP4 is to have  
##  this as the "official" method and have an Other  
##  method that adds a boolean true to the advertised syntax. 

InstallMethod(GabidulinCode, "method for internal use!", true, 
	[IsInt, IsFFE, IsFFE, IsBool], 0, 
function(m, w1, w2, check) 
    
    local checkmat, nmat, els, i, j, w3,
          fieldels, binels, newels, binnewels, size, one, newcode;
    
    if check <> false and m < 4 then
        Error( "GabidulinCode: <m> must be at least 4" );
    fi;
    
    w3 := w1 + w2;
            
    checkmat := MutableNullMat( 5 * 2^(m-2) - 1, 2 * m - 1, GF( 2 ) );
    
    size := 2^(m-2);
    
    fieldels := SortedGaloisFieldElements( size );
# similar to AsSSortedList( field ) - same in prime field case - but
# returns *all* elements in the form Z(size)^i, rather than 
# simplified to Z(p^d)^j if i divides size-1
    binels := BinaryRepresentation( fieldels, m-2 );
# requires that all elements are in the form Z(size)^i
    one := Z(2)^0;
    
    # make matrix N
    
    for i in [ 1 .. size - 1 ] do
        for j in [ 1 .. m-2 ] do
            checkmat[ i ][ j + 1 ] := binels[ i + 1 ][ j ];
        od;
    od;
    
    # make matrix D
    
    for i in [ 1 .. size ] do
        checkmat[ size - 1 + i ][ 1 ] := one;
        
        for j in [ 1 .. m - 2 ] do
            checkmat[ size - 1 + i ][ j + 1 ] := binels[ i ][ j ];
        od;
        
        newels := ShallowCopy( fieldels );
        for j in [ 2 .. size ] do
            newels[ j ] := w1/fieldels[ j ];
        od;
        binnewels := BinaryRepresentation( newels, m-2 );
        
        for j in [ 1 .. m - 2 ] do
            checkmat[ size - 1 + i ][ j + m - 1 ] := binnewels[ i ][ j ];
        od;
    od;
    
    # make matrix Q
    
    for i in [ 1 .. size ] do
        checkmat[ 2 * size - 1 + i ][ 1 ] := one;
        checkmat[ 2 * size - 1 + i ][ 2 * m - 1 ] := one;
        
        for j in [ 1 .. m - 2 ] do
            checkmat[ 2 * size - 1 + i ][ j + 1 ] := binels[ i ][ j ];
        od;
        
        newels := ShallowCopy( fieldels );
        for j in [ 2 .. size ] do
            newels[ j ] := w2/fieldels[ j ];
        od;
        binnewels := BinaryRepresentation( newels, m-2 );
        
        for j in [ 1 .. m - 2 ] do 
            checkmat[ 2 * size - 1 + i ][ j + m - 1 ] := binnewels[ i ][ j ];
        od;
    od;
    
    # make matrix M
    
    for i in [ 1 .. size ] do
        checkmat[ 3 * size - 1 + i ][ 1 ] := one;
        checkmat[ 3 * size - 1 + i ][ 2 * m - 2 ] := one;
        
        for j in [ 1 .. m - 2 ] do
            checkmat[ 3 * size - 1 + i ][ j + 1 ] := binels[ i ][ j ];
        od;
        
        newels := ShallowCopy( fieldels );
        for j in [ 2 .. size ] do
            newels[ j ] := w3/fieldels[ j ];
        od;
        binnewels := BinaryRepresentation( newels, m-2 );
        
        for j in [ 1 .. m - 2 ] do
            checkmat[ 3 * size - 1 + i ][ j + m - 1 ] := binnewels[ i ][ j ];
        od;
    od;
    
    # make matrix G
    
    for i in [ 1 .. size ] do
        checkmat[ 4 * size - 1 + i ][ 1 ] := one;
        checkmat[ 4 * size - 1 + i ][ 2 * m - 2 ] := one;        
        checkmat[ 4 * size - 1 + i ][ 2 * m - 1 ] := one;    
        
        for j in [ 1 .. m - 2 ] do
            checkmat[ 4 * size - 1 + i ][ m - 1 + j ] := binels[ i ][ j ];
        od;
    od;
    
    checkmat := TransposedMat( checkmat );
    
    newcode := CheckMatCode( checkmat, 
                       Concatenation( "Gabidulin code (m=",String(m),")" ),
                       GF(2) );
    
#    newcode.wordLength := 5 * size - 1;
#    newcode.dimension := 2 * m - 1;
    newcode!.lowerBoundMinimumDistance := 3;
    newcode!.upperBoundMinimumDistance := 3;
    SetMinimumDistance(newcode, 3); 
	newcode!.boundsCoveringRadius := [ 2 ];
    SetCoveringRadius(newcode, 2); 

    return( newcode );    
end);

## This is the advertised syntax, see note above. 
InstallOtherMethod(GabidulinCode, "m, w1, w2", true, 
	[IsInt, IsFFE, IsFFE], 0, 
function(m, w1, w2) 
	return GabidulinCode(m, w1, w2, true); 
end); 


########################################################################
##
#F  EnlargedGabidulinCode( );
##

InstallMethod(EnlargedGabidulinCode, "m, w1, w2, e", true, 
	[IsInt, IsFFE, IsFFE, IsFFE], 0, 
function(m, w1, w2, el) 
    
    local checkmat, nmat, els, i, j, k, w3,
          fieldels, binels, newels, binnewels, 
		  size, one, newcode, bmat, binel;
    
    if m < 4 then
        Error( "EnlargedGabidulinCode: <m> must be at least 4" );
    fi;
    
    w3 := w1 + w2;
            
    checkmat := MutableNullMat( 7 * 2^(m-2) - 2, 2 * m, GF( 2 ) );
    
    size := 2^(m-1);
    
    fieldels := SortedGaloisFieldElements( GF( size ) );
    binels := BinaryRepresentation( fieldels, m-1 );
    
    one := Z(2)^0;
    
    # make matrix Z
    
    bmat := TransposedMat( ShallowCopy( CheckMat( 
                    GabidulinCode( m, w1, w2, false ) ) ) );

    for i in [ 1 .. Length( bmat ) - 1 ] do
        k := i;
        if k > 2^(m-2) - 1 then
            k := k + 1;
        fi;
        for j in [ 1 .. Length( bmat[ 1 ] ) ] do
            checkmat[ i ][ j + 1 ] := bmat[ k ][ j ];
        od;
    od;
    
    # make matrix Y
    
    binel := BinaryRepresentation( el, m );
    
    for i in [ 1 .. size ] do
        checkmat[ Length( bmat ) - 1 + i ][ 1 ] := one;
        for j in [ 1 .. m-1 ] do
            checkmat[ Length( bmat ) - 1 + i ][ j + 1 ] := binels[ i ][ j ];
        od;
        for j in [ 1 .. m ] do
            checkmat[ Length( bmat ) - 1 + i ][ m + j ] := binel[ j ];
        od;
    od;
        
    checkmat := TransposedMat( checkmat );
    newcode := CheckMatCode( checkmat, 
                       Concatenation( "enlarged Gabidulin code (m="
                               ,String(m),")" ),
                       GF(2) );

    newcode!.lowerBoundMinimumDistance := 3;
    newcode!.upperBoundMinimumDistance := 3;
    SetMinimumDistance(newcode, 3); 
	newcode!.boundsCoveringRadius := [ 2 ];
    SetCoveringRadius(newcode, 2); 

    return( newcode );    
end);


########################################################################
##
#F  DavydovCode( );
##

InstallMethod(DavydovCode, "redundancy, v, ei, ej", true, 
	[IsInt, IsInt, IsFFE, IsFFE], 0, 
function(r, v, i, j) 
    
    local checkmat, binel2, fieldels, binels, 
          k, l, field1el, field2el, binel1, newcode;
    
    if r < 4 then
        Error( "DavydovCode: <m> must be at least 5" );
    fi;
    
    # 0 < i < 2^v ??
    # 0 < j < 2^(r-v) ??

    checkmat := MutableNullMat( 2^v + 2^(r-v) - 3, r, GF( 2 ) );
    
    field1el := i;
    binel1 := BinaryRepresentation( field1el, v );
    field2el := j;
    binel2 := BinaryRepresentation( field2el, r - v );

    
    # make matrix K
    
    fieldels := SortedGaloisFieldElements( GF( 2^v ) ); # remove 0
    SubtractSet( fieldels, [ fieldels[ 1 ], i ] );
    binels := BinaryRepresentation( fieldels, v );
    
    
    for k in [ 1 .. 2^v - 2 ] do
        for l in [ 1 .. v ] do
            checkmat[ k ][ l ] := binels[ k ][ l ];
        od;
        for l in [ 1 .. r-v ] do
            checkmat[ k ][ v + l ] := binel2[ l ];
        od;
    od;
    
    # make matrix (column) A
    
    for l in [ 1 .. v ] do
        checkmat[ 2^v - 1 ][ l ] := binel1[ l ];
    od;
    for l in [ 1 .. r-v ] do
        checkmat[ 2^v - 1 ][ v + l ] := binel2[ l ];
    od;
    
    # make matrix S
    
    fieldels := SortedGaloisFieldElements( GF( 2^(r-v) ) );
    SubtractSet( fieldels, [ fieldels[ 1 ], j ] );
    binels := BinaryRepresentation( fieldels, r - v );
    
    for k in [ 1 .. 2^(r-v) - 2 ] do
        for l in [ 1 .. v ] do
            checkmat[ 2^v - 1 + k ][ l ] := binel1[ l ];
        od;
        for l in [ 1 .. r-v ] do
            checkmat[ 2^v - 1 + k ][ v + l ] := binels[ k ][ l ];
        od;
    od;
    
    
    checkmat := TransposedMat( checkmat );
    
    newcode := CheckMatCode( checkmat, 
                       Concatenation( "Davydov code (r=",String(r),
                               ", v=", String(v), ")" ),
                       GF(2) );
    
#    newcode.wordLength := 5 * size - 1;
#    newcode.dimension := newcode.wordLength - (2 * m);
    newcode!.lowerBoundMinimumDistance := 4;
    newcode!.upperBoundMinimumDistance := 4;
    SetMinimumDistance(newcode, 4); 
	newcode!.boundsCoveringRadius := [ 2 ];
    SetCoveringRadius(newcode, 2); 

    return( newcode );    
end);


########################################################################
##
#F  TombakCode( );
##

InstallGlobalFunction(TombakCode, 
function ( arg )
    
    local checkmat, one, m, i, beta, gamma, delta, w1, w2, w3, newcode,
          k, size, fieldels, binels, binel, l, sortlist, sortedlist, 
          newels, binnewels, theta, newsize, betabin, gammbin, gammabin, 
		  deltabin, w1bin, w2bin, w3bin;
    
    m := arg[ 1 ];
    if arg[ Length( arg ) ] <> false and m < 5 then
        Error( "TombakCode: <m> must be at least 5" );
    fi;
    
    beta := arg[ 3 ];
    gamma := arg[ 4 ];
    delta := beta + gamma;
    
    betabin := BinaryRepresentation( beta, m-1 );
    gammabin := BinaryRepresentation( gamma, m-1 );
    deltabin := betabin + gammabin;
    
    w1 := arg[ 5 ];
    w2 := arg[ 6 ];
    w3 := w1 + w2;
    
    w1bin := BinaryRepresentation( w1, m-3 );
    w2bin := BinaryRepresentation( w2, m-3 );
    w3bin := w1bin + w2bin;
    
    i := arg[ 2 ];

    checkmat := MutableNullMat( 15 * 2^(m-3) - 3, 2*m, GF( 2 ) );
    
    one := Z(2)^0;
           
    # make matrix C
    
    size := 2^(m-3);
    
    fieldels := SortedGaloisFieldElements( size );
    binels := BinaryRepresentation( fieldels, m-3 );
    
    binel := betabin;
    
    for k in [ 1 .. size - 1 ] do
        for l in [ 1 .. m - 3 ] do
            checkmat[ k ][ 3 + l ] := binels[ k + 1 ][ l ];
        od;
        for l in [ 1 .. m - 1 ] do
            checkmat[ k ][ m + l ] := binel[ l ];
        od;
        checkmat[ k ][ 2 * m ] := one;
    od;
    
    # make matrix V
    
    binel := gammabin;
    
    for k in [ 1 .. 2^(m-3) ] do
        checkmat[ size - 1 + k ][ 2 ] := one;
        for l in [ 1 .. m - 3 ] do
            checkmat[ size - 1 + k ][ 3 + l ] := binels[ k ][ l ];
        od;
        for l in [ 1 .. m - 1 ] do
            checkmat[ size - 1 + k ][ m + l ] := binel[ l ];
        od;
        checkmat[ size - 1 + k ][ 2 * m ] := one;
    od;
    
    # make matrix X
    
    binel := deltabin;
    
    for k in [ 1 .. size ] do
        checkmat[ 2 * size - 1 + k ][ 2 ] := one;
        checkmat[ 2 * size - 1 + k ][ 3 ] := one;
        for l in [ 1 .. m - 3 ] do
            checkmat[ 2 * size - 1 + k ][ 3 + l ] := binels[ k ][ l ];
        od;
        for l in [ 1 .. m - 1 ] do
            checkmat[ 2 * size - 1 + k ][ m + l ] := binel[ l ];
        od;
    od;
    
    # make sub-matrix Theta
    theta := MutableNullMat( 4*size - 1, 2 * (m-3) + 2, GF( 2 ) );
    
    for k in [ 1 .. size - 1 ] do
        for l in [ 1 .. m-3 ] do
            theta[ k ][ l ] := binels[ k + 1 ][ l ];
        od;
        
        newels := ShallowCopy( fieldels );
        for l in [ 2 .. size ] do 
            newels[ l ] := w1/fieldels[ l ];
        od;
        binnewels := BinaryRepresentation( newels, m-3 );
        
        for l in [ 1 .. m-3 ] do
            theta[ k ][ m-3 + l ] := binnewels[ k + 1 ][ l ];
        od;
    od;
    
    for k in [ 1 .. size ] do
        for l in [ 1 .. m-3 ] do
            theta[ size - 1 + k ][ l ] := binels[ k ][ l ];
        od;
        
        newels := ShallowCopy( fieldels );
        for l in [ 2 .. size ] do
            newels[ l ] := w2/fieldels[ l ];
        od;
        binnewels := BinaryRepresentation( newels, m-3 );
        
        for l in [ 1 .. m-3 ] do
            theta[ size - 1 + k ][ m-3 + l ] := binnewels[ k ][ l ];
        od;
        theta[ size - 1 + k ][ 2*(m-3) + 2 ] := one;
    od;
    
    for k in [ 1 .. size ] do
        for l in [ 1 .. m-3 ] do
            theta[ 2 * size - 1 + k ][ l ] := binels[ k ][ l ];
        od;
        
        newels := ShallowCopy( fieldels );
        for l in [ 2 .. size ] do
            newels[ l ] := w3/fieldels[ l ];
        od;
        binnewels := BinaryRepresentation( newels, m-3 );
        
        for l in [ 1 .. m-3 ] do
            theta[ 2 * size - 1 + k ][ m-3 + l ] := binnewels[ k ][ l ];
        od;
        theta[ 2 * size - 1 + k ][ 2*(m-3) + 1 ] := one;
    od;
    
    for k in [ 1 .. size ] do
        for l in [ 1 .. m-3 ] do
            theta[ 3*size - 1 + k ][ m-3 + l ] := binels[ k ][ l ];
        od;
        
        theta[ 3 * size - 1 + k ][ 2*(m-3) + 1 ] := one;
        theta[ 3 * size - 1 + k ][ 2*(m-3) + 2 ] := one;
    od;
    
    # make matrix Phi
    
    for k in [ 1 .. 4*size - 1 ] do
        checkmat[ 3*size - 1 + k ][ 3 ] := one;
        for l in [ 1 .. 2*m - 4 ] do
            checkmat[ 3*size - 1 + k ][ 3 + l ] := theta[ k ][ l ];
        od;
    od;
    
    # make matrix Lambda
    
    binel := betabin;
    
    for k in [ 1 .. 4*size - 1 ] do
        checkmat[ 7 * size - 2 + k ][ 3 ] := one;
        for l in [ 1 .. m - 3 ] do
            checkmat[ 7 * size - 2 + k ][ 3 + l ] := theta[ k ][ l ];
        od;
        for l in [ 1 .. m - 1 ] do 
			#changed index to m + l from m-1+3+l to keep length correct
            checkmat[ 7 * size - 2 + k ][ m + l ] := 
              theta[ k ][ m-3 + l ] + binel[ l ];
        od;
        checkmat[ 7 * size - 2 + k ][ 2*m ] := one;
    od;
    
    # make matrix Y
    
    newsize := 2^(m-1);
    fieldels := SortedGaloisFieldElements( newsize );
    binels := BinaryRepresentation( fieldels, m-1 );
    
    binel := BinaryRepresentation( i, m );
    
    for k in [ 1 .. newsize ] do
        checkmat[ 11*size - 3 + k ][ 1 ] := one;
        for l in [ 1 .. m-1 ] do
            checkmat[ 11*size - 3 + k ][ 1 + l ] := binels[ k ][ l ];
        od;
        for l in [ 1 .. m ] do
            checkmat[ 11*size - 3 + k ][ m + l ] := binel[ l ];
        od;
    od;
   
    checkmat := TransposedMat( checkmat );
    
    newcode := CheckMatCode( checkmat, 
                       Concatenation( "Tombak code (m=",String(m),")" ),
                       GF(2) );
    
#    newcode.wordLength := 5 * size - 1;
#    newcode.dimension := newcode.wordLength - (2 * m);
    newcode!.lowerBoundMinimumDistance := 4;
    newcode!.upperBoundMinimumDistance := 4;
    SetMinimumDistance(newcode, 4); 
	newcode!.boundsCoveringRadius := [ 2 ];
   	SetCoveringRadius(newcode, 2); 

    return( newcode );    
end);


########################################################################
##
#F  EnlargedTombakCode( );
##

# Must be a GlobalFunctton because GAP doesn't support methods with 7 
# arguments. 
InstallGlobalFunction(EnlargedTombakCode, 
function(m, i, beta, gamma, w1, w2, u) 
    
	local checkmat, fieldels, binels, size, one, 
		  newcode, bmat, binel, k, l; 
    
    if m < 6 then
        Error( "EnlargedTombakCode: <m> must be at least 6" );
    fi;
    
    checkmat := MutableNullMat( 23 * 2^(m-4) - 3, 2 * m - 1, GF( 2 ) );
    
    size := 2^(m-1);
    
    fieldels := SortedGaloisFieldElements( GF( size ) );
    binels := BinaryRepresentation( fieldels, m-1 );
    
    one := Z(2)^0;
    
    # make matrix pi
    
    bmat := TransposedMat( ShallowCopy( CheckMat( 
                    TombakCode( m-1, i, beta, gamma, w1, w2, false ) ) ) );

    for k in [ 1 .. Length( bmat ) ] do
        for l in [ 1 .. Length( bmat[ 1 ] ) ] do
            checkmat[ k ][ l + 1 ] := bmat[ k ][ l ];
        od;
    od;
    
    # make matrix J
    
    binel := BinaryRepresentation( u, m-1 );
    
    for k in [ 1 .. size ] do
        checkmat[ Length( bmat ) + k ][ 1 ] := one;
        for l in [ 1 .. m-1 ] do
            checkmat[ Length( bmat ) + k ][ 1 + l ] := binel[ l ];
        od;
        for l in [ 1 .. m-1 ] do
            checkmat[ Length( bmat ) + k ][ m + l ] := binels[ k ][ l ];
        od;
    od;
        
    checkmat := TransposedMat( checkmat );
    
    newcode := CheckMatCode( checkmat, 
                       Concatenation( "enlarged Tombak code (m="
                               ,String(m),")" ),
                       GF(2) );
    
#    newcode.wordLength := 5 * size - 1;
#    newcode.dimension := newcode.wordLength - (2 * m);
    newcode!.lowerBoundMinimumDistance := 4;
    newcode!.upperBoundMinimumDistance := 4;
    SetMinimumDistance(newcode, 4); 
	newcode!.boundsCoveringRadius := [ 2 ];
    SetCoveringRadius(newcode, 2); 

    return( newcode );    
end);