File: codeword.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 (836 lines) | stat: -rw-r--r-- 21,502 bytes parent folder | download
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
#############################################################################
##
#A  codeword.gi             GUAVA library                       Reinald Baart
#A                                                        &Jasper Cramwinckel
#A                                                           &Erik Roijackers
##
##  This file contains functions for working with codewords
##  Codeword is a record with the following field:
##  !.treatAsPoly
##
##  Codeword can have the following attributes: 
##	VectorCodeword
##	PolyCodeword 
##	Weight 
##	WordLength 
##	Support 
## 	
#H  @(#)$Id: codeword.gi,v 1.9 2003/02/12 03:49:19 gap Exp $
##
Revision.("guava/lib/codeword_gi") :=
    "@(#)$Id: codeword.gi,v 1.9 2003/02/12 03:49:19 gap Exp $";

DeclareRepresentation("IsCodewordRep", 
			    IsAttributeStoringRep and IsComponentObjectRep, 
			    ["treatAsPoly"]);

BindGlobal("CodewordFamily",
        NewFamily("CodewordFamily", IsCodeword,IsCodeword and IsCodewordRep)); 

BindGlobal("CodewordType",NewType(CodewordFamily, IsCodeword ));


# Function to objectify codeword using input 
# vector, length, and field or ffe 
MakeCodeword := function(vec, n, F) 
	local v;
	if Length(vec) > n then 
		vec := vec{[1..n]};
    elif Length(vec) < n then 
		vec := Concatenation(vec, [1..n-Length(vec)]*Zero(F)); 
    fi;
	vec := vec * One(F);
	v := Objectify(CodewordType, rec());
	SetVectorCodeword(v, vec);
	SetWordLength(v, n);
	return v;
end;


## Function to select an appropriate field based on contents of list. 
SelectField := function(list) 
	local f;
	f := Maximum(list) + 1;
	while not IsPrimePowerInt(f) do 
		f := f + 1;
	od;
	return GF(f);
end;

#############################################################################
##
#F  Codeword( <list> [, <F>] or . . . . . . . . . . . .  creates new codeword
#F  Codeword( <P> [, <n>] [, <F>] ) . . . . . . . . . . . . . . . . . . . . .
##  Codeword( <P>, Code)  . . . . . . . . . . . . . . . . . . . . . . . . . . 
##

InstallMethod(Codeword, "list,n,FFE", true, [IsList, IsInt, IsFFE], 1, 
function(list, n, ffe)
	local c;
	if Length(list) > 0 and not (IsRat(list[1]) or IsFFE(list[1])) then 
		return List(list, i->Codeword(i, n, ffe)); 
	fi; 
	c := MakeCodeword(list, n, ffe);  
	TreatAsVector(c);
	return c;
end);

InstallOtherMethod(Codeword, "list,n,Field", true, [IsList, IsInt, IsField], 1, 
function(list, n, F)
  	local i;
	if Length(list) > 0 and not (IsRat(list[1]) or IsFFE(list[1])) then 
  		return List(list, i->Codeword(i, n, F)); 
	fi;
	if Length(list) > 0 and IsRat(list[1]) and not IsPrime(Size(F)) then 
		list := List(list, i->AsSSortedList(F)[Int(i)mod Size(F) + 1]);
	fi;
	return Codeword(list,n,One(F));
end);

InstallOtherMethod(Codeword, "list,FFE", true, [IsList, IsFFE], 1, 
function(list, ffe)
	if Length(list) > 0 and not (IsRat(list[1]) or IsFFE(list[1])) then 
		return List(list, i->Codeword(i, ffe)); 
	fi;
	return Codeword(list, Length(list), ffe);
end);

InstallOtherMethod(Codeword, "list,Field", true, [IsList, IsField], 1, 
function(list, F)
	if Length(list) > 0 and not (IsRat(list[1]) or IsFFE(list[1])) then 
		return List(list, i->Codeword(i, F)); 
	fi;
		return Codeword(list, Length(list), F);
end);

InstallOtherMethod(Codeword, "list,n", true, [IsList, IsInt], 1, 
function(list, n)
local o;
  if Length(list)=0 then
	o:=Z(2);
  elif IsRat(list[1]) then 
 	o:=SelectField(list);
  elif IsFFE(list[1]) then 
	o:=Maximum(list);
  else 
    return List(list, i->Codeword(i, n)); 
  fi;
  return Codeword(list,n,o);
end);

InstallOtherMethod(Codeword,"list",true,[IsList],1,
function(list)
local o;
  if Length(list)=0 then  
	o:=Z(2);
  elif IsRat(list[1]) then 
  	o:= SelectField(list);
  elif  IsFFE(list[1]) then  
	o:= Maximum(list);
  else  
  	return List(list, i->Codeword(i)); 
  fi;
  return Codeword(list,Length(list),o);
end);

InstallOtherMethod(Codeword, "list,Code", true, [IsList, IsCode], 0, 
function(l, C) 
	return Codeword(l, WordLength(C), LeftActingDomain(C));
end);


## Methods with a string provided ##

## helper function to convert string to list/vector.  Digits go to approp.
## digit.  Other characters go to 0.
StringToVec := function(s)
	local val, i, S; 
	S := [];
	for i in s do 
		val := Position("0123456789", i);
		if val = fail then 
			val := 0;
		else 
			val := val-1;
		fi;
		Add(S, val);
	od;
	return S;
end;


InstallOtherMethod(Codeword,"string,n,FFE",true,[IsString,IsInt,IsFFE],0,
function(s,n,ffe)
	s:=StringToVec(s);
	return Codeword(s,n,ffe);
end);

InstallOtherMethod(Codeword,"string,n,Field",true,[IsString,IsInt,IsField],0,
function(s,n,F)
	s := StringToVec(s);
	return Codeword(s, n, F);
end);

InstallOtherMethod(Codeword, "string,FFE", true, [IsString, IsFFE], 0, 
function(s, ffe)
	return Codeword(s, Length(s), ffe);
end);

InstallOtherMethod(Codeword, "string,Field", true, [IsString, IsField], 0, 
function(s, F) 
	return Codeword(s, Length(s), F);
end);

InstallOtherMethod(Codeword, "string,n", true, [IsString, IsInt], 0, 
function(s, n)
	local F;
	s := StringToVec(s);
	F := SelectField(s); 
	return Codeword(s, n, F);
end);

InstallOtherMethod(Codeword, "string", true, [IsString], 0, 
function(s) 
	return Codeword(s, Length(s)); 
end);

InstallOtherMethod(Codeword, "string,Code", true, [IsString, IsCode], 0, 
function(s, C)
	return Codeword(s, WordLength(C), LeftActingDomain(C));
end);


## Methods with a poly provided ##
InstallOtherMethod(Codeword,"poly,n,FFE", 
  	function(pf, inf, ff)
  		return IsIdenticalObj(CoefficientsFamily(pf), ff);
	end, 
	[IsUnivariatePolynomial,IsInt,IsFFE],0,
function(p,n,ffe)
  	local c;
	p := CoefficientsOfLaurentPolynomial(p);
  	p := ShiftedCoeffs(p[1],p[2]);
  	c := Codeword(p,n,ffe);
    TreatAsPoly(c);
	return c;
end);

InstallOtherMethod(Codeword, "poly,n,Field",  
	function(pf,inf,ff)
		return IsIdenticalObj(CoefficientsFamily(pf), ElementsFamily(ff));
	end,
	[IsUnivariatePolynomial, IsInt, IsField], 0, 
function(p, n, F)
	return Codeword(p, n, One(F));
end);

InstallOtherMethod(Codeword, "poly,FFE",  
	function(pf, ff)
		return IsIdenticalObj(CoefficientsFamily(pf), ff);
	end,
	[IsUnivariatePolynomial, IsFFE], 0, 
function(p, ffe)
	local c;
	p := CoefficientsOfLaurentPolynomial; 
	p := ShiftedCoeffs(p[1],p[2]);
	c := Codeword(p, Length(p), ffe);
	TreatAsPoly(c); 
	return c;
end);

InstallOtherMethod(Codeword, "poly,Field",  
	function(pf,ff)
		return IsIdenticalObj(CoefficientsFamily(pf), ElementsFamily(ff));
	end,
	[IsUnivariatePolynomial, IsField], 0, 
function(p, F)
	local c;
	p := CoefficientsOfLaurentPolynomial(p);
	p := ShiftedCoeffs(p[1],p[2]); 
	c := Codeword(p, Length(p), One(F));
	TreatAsPoly(c);
	return c;
end);

InstallOtherMethod(Codeword, "poly,n", true, 
	[IsUnivariatePolynomial, IsInt], 0, 
function(p, n)
	local c, F;
	F := CoefficientsRing(DefaultRing(p)); 
	p := CoefficientsOfLaurentPolynomial(p);
	p := ShiftedCoeffs(p[1],p[2]); 
	c := Codeword(p, n, F); 
	TreatAsPoly(c);
	return c;
end);

InstallOtherMethod(Codeword, "poly", true, 
	[IsUnivariatePolynomial], 0, 
function(p)
	local c, F; 
	F := CoefficientsRing(DefaultRing(p));
	p := CoefficientsOfLaurentPolynomial(p); 
	p := ShiftedCoeffs(p[1],p[2]); 
	c := Codeword(p, Length(p), F);
	TreatAsPoly(c);
	return c;
end);

InstallOtherMethod(Codeword, "poly,Code", true, 
	[IsUnivariatePolynomial, IsCode], 0, 
function(p, C)
	return Codeword(p, WordLength(C), LeftActingDomain(C));
end);


## Methods with a codeword provided ##
InstallOtherMethod(Codeword,"codeword,n",true,[IsCodeword,IsInt],0,
function(w,n)
  return Codeword(VectorCodeword(w),n, Field(VectorCodeword(w)));
end);

InstallOtherMethod(Codeword, "codeword", true, [IsCodeword], 0,
function(w)
	return Codeword(VectorCodeword(w), WordLength(w), Field(VectorCodeword(w)));
end);

InstallOtherMethod(Codeword, "codeword,n,Field", true, 
	[IsCodeword, IsInt, IsField], 0, 
function(w, n, F)
	return Codeword(VectorCodeword(w),n,F);
end);

InstallOtherMethod(Codeword, "codeword,n,FFE", true, 
	[IsCodeword, IsInt, IsFFE], 0, 
function(w, n, ffe) 
	return Codeword(VectorCodeword(w), n, ffe);
end);

InstallOtherMethod(Codeword, "codeword,Field", true, [IsCodeword, IsField], 0,
function(w, F)
	return Codeword(VectorCodeword(w),WordLength(w), F);
end);

InstallOtherMethod(Codeword, "codeword,FFE", true, [IsCodeword, IsFFE], 0, 
function(w, ffe)
	return Codeword(VectorCodeword(w), WordLength(w), ffe);
end);

InstallOtherMethod(Codeword, "codeword,Code", true, [IsCodeword, IsCode], 0, 
function(w, C)
	return Codeword(VectorCodeword(w), WordLength(C), LeftActingDomain(C));
end);


#############################################################################
##
#F  Field( <c> )
##
#InstallOtherMethod(FieldByGenerators,"codewords",true,[IsCodewordCollection],0,
#function(l)
#  return FieldByGenerators(VectorCodeword(l[1]));
#end);

#############################################################################
##
#M  Print( <v> )  . . . . . . . . . . . . . . . . . . . . . prints a codeword
##
PrintViewCodeword:=function(w)
local v, q, isclear, i, l, power;
  if w!.treatAsPoly then 
    v := VectorCodeword(w);
    if Length(v) > 0 then 
      q := Size(Field(v));
    else 
      Print("[ ]");
      return;
    fi;
    isclear := true;
    for power in Reversed([0..WordLength(w)-1]) do 
      if v[power+1] <> 0*Z(q) then 
	if not isclear then 
		Print(" + "); 
	fi;
	isclear := false; 
	if power = 0 or v[power+1] <> Z(q)^0 then 
		if IsPrime(q) then 
			Print(String(Int(v[power+1])));
		else
			i := LogFFE(v[power+1], Z(q));
			if i = 0 then 
				Print("1");
			elif i = 1 then 
				Print("a");
			else
				Print("(a^",String(i),")");
			fi;
		fi;
	fi;
	if power > 0 then 
		Print("x");
		if power > 1 then 
			Print("^", String(power));
		fi;
	fi;
      fi;
    od;
    if isclear then 
      Print("0");
    fi; 
  else
    Print("[ ");
    v := VectorCodeword(w);
    if Length(v) > 0 then 
	    q := Size(Field(v));
    else 
	    Print("]");
	    return;
    fi;
    if not IsPrime(q) then 
	    for i in v do 
		    if i = 0 * Z(q) then 
			    Print("0 ");
		    else
			    l := LogFFE(i, Z(q));
			    if l = 0 then 
				    Print("1 ");
			    elif l = 1 then 
				    Print("a ");
			    else
				    Print("a^", String(l), " ");
			    fi;
		    fi;
	    od;
    else
	    for i in IntVecFFE(v) do 
		    Print(i, " ");
	    od;
    fi;
    Print("]");
  fi;
end;

InstallMethod(PrintObj, "codeword", true, [IsCodeword], 0, 
  PrintViewCodeword);

InstallMethod(ViewObj, "codeword", true, [IsCodeword], 0, 
  PrintViewCodeword);

#############################################################################
##
## list methods for codewords (to permit codeword+list...)
InstallOtherMethod(Length,"codeword",true,[IsCodeword],0,
  w->Length(VectorCodeword(w)));

InstallOtherMethod(\[\],"codeword",true,[IsCodeword,IsPosInt],0,
function(w,i)
  return VectorCodeword(w)[i];
end);

#############################################################################
##
#F  \+( <l>, <r> )  . . . . . . . . . . . . . . . . . . . .  sum of codewords
##

InstallOtherMethod(\+, "codeword+codeword", true, [IsCodeword, IsCodeword], 0, 
function(a, b) 
	return Codeword(VectorCodeword(a) + VectorCodeword(b));
end);

InstallOtherMethod(\+, "codeword+list", true, [IsCodeword, IsList], 0, 
function(w, l)
	return Codeword(VectorCodeword(w) + l); 
end);

InstallOtherMethod(\+, "list+codeword", true, [IsList,IsCodeword], 0, 
function(l, w)
	return Codeword(l+VectorCodeword(w)); 
end);

InstallOtherMethod(\+, "poly+codeword", true, 
	[IsUnivariatePolynomial, IsCodeword], 0, 
function(p, w)
	return Codeword(p) + w;
end);

InstallOtherMethod(\+, "codeword+poly", true, 
	[IsCodeword, IsUnivariatePolynomial], 0, 
function(w, p)
	return w + Codeword(p);
end);

InstallOtherMethod(\+, "string+codeword", true, [IsString, IsCodeword], 0, 
function(s, w)
	return Codeword(s) + w;
end);

InstallOtherMethod(\+, "codeword+string", true, [IsCodeword, IsString], 0, 
function(w, s)
	return w + Codeword(s);
end);

InstallOtherMethod(\+, "Rat+codeword", true, [IsRat, IsCodeword], 0, 
function(r, w)
	return Codeword(r + VectorCodeword(w));
end);

InstallOtherMethod(\+, "codeword+Rat", true, [IsCodeword, IsRat], 0, 
function(w, r)
	return Codeword(VectorCodeword(w) + r);
end);

InstallOtherMethod(\+, "FFE+codeword", true, [IsFFE, IsCodeword], 0, 
function(ffe, w)
	return Codeword(ffe + VectorCodeword(w));
end);

InstallOtherMethod(\+, "codeword+FFE", true, [IsCodeword, IsFFE], 0, 
function(w, ffe)
	return Codeword(VectorCodeword(w) + ffe);
end);


#############################################################################
##
#F  \*( <l>, <r> )  . . . . . . . . . . . .  product of codewords
##

InstallOtherMethod(\*, "codeword*codeword", true, [IsCodeword, IsCodeword], 0, 
function(a, b)
	return VectorCodeword(a) * VectorCodeword(b);
end);

InstallOtherMethod(\*, "matrix*codeword", true, [IsMatrix, IsCodeword], 0, 
function(m, w)
	return Codeword(m * VectorCodeword(w));
end);

InstallOtherMethod(\*, "codeword*matrix", true, [IsCodeword, IsMatrix], 0, 
function(w, m)
	return Codeword(VectorCodeword(w) * m);
end);

InstallOtherMethod(\*, "FFE*codeword", true, [IsFFE, IsCodeword], 0, 
function(ffe, w)
	return Codeword(ffe * VectorCodeword(w));
end);

InstallOtherMethod(\*, "codeword*FFE", true, [IsCodeword, IsFFE], 0, 
function(w, ffe)
	return Codeword(VectorCodeword(w) * ffe);
end);

InstallOtherMethod(\*, "Rat*codeword", true, [IsRat, IsCodeword], 0, 
function(r, w)
	return Codeword(r * VectorCodeword(w));
end);

InstallOtherMethod(\*, "codeword*Rat", true, [IsCodeword, IsRat], 0, 
function(w, r)
	return Codeword(VectorCodeword(w) * r);
end);


#############################################################################
##
#F  \-( <l>, <r> )  . . . . . . . . . . . . . . . . . difference of codewords
##

InstallOtherMethod(\-, "codeword-codeword", true, [IsCodeword, IsCodeword], 0, 
function(a, b)
	return Codeword(VectorCodeword(a) - VectorCodeword(b)); 
end);

InstallOtherMethod(\-, "vector-codeword", true, [IsVector, IsCodeword], 0, 
function(v, w)
	return Codeword(v - VectorCodeword(w));
end);

InstallOtherMethod(\-, "codeword-vector", true, [IsCodeword, IsVector], 0, 
function(w,v)
	return Codeword(VectorCodeword(w) - v);
end);

InstallOtherMethod(\-, "poly-codeword", true, 
	[IsUnivariatePolynomial, IsCodeword], 0, 
function(p, w)
	return Codeword(p) - w;
end);

InstallOtherMethod(\-, "codeword-poly", true, 
	[IsCodeword, IsUnivariatePolynomial], 0, 
function(w, p)
	return w - Codeword(p);
end);

InstallOtherMethod(\-, "string-codeword", true, [IsString, IsCodeword], 0, 
function(s, w)
	return Codeword(s) - w;
end);

InstallOtherMethod(\-, "codeword-string", true, [IsCodeword, IsString], 0, 
function(w, s)
	return w - Codeword(s);
end);

InstallOtherMethod(\-, "Rat-codeword", true, [IsRat, IsCodeword], 0, 
function(r, w)
	return Codeword(r - VectorCodeword(w));
end);

InstallOtherMethod(\-, "codeword-Rat", true, [IsCodeword, IsRat], 0, 
function(w, r)
	return Codeword(VectorCodeword(w) - r);
end);

InstallOtherMethod(\-, "FFE-codeword", true, [IsFFE, IsCodeword], 0, 
function(ffe, w)
	return Codeword(ffe - VectorCodeword(w));
end);

InstallOtherMethod(\-, "codeword-FFE", true, [IsCodeword, IsFFE], 0, 
function(w, ffe)
	return Codeword(VectorCodeword(w) - ffe);
end);


#############################################################################
##
#F  \=( <l>, <r> )  . . . . . . . . . . . . . . . . . . equality of codewords
##

InstallMethod(\=, "codeword=codeword", true, [IsCodeword, IsCodeword], 0, 
function(a, b)
	return VectorCodeword(a) = VectorCodeword(b);
end);

InstallMethod(\=, "vector=codeword", true, [IsVector, IsCodeword], 0, 
function(v, w)
	return v = VectorCodeword(w);
end);

InstallMethod(\=, "codeword=vector", true, [IsCodeword, IsVector], 0, 
function(w, v)
	return VectorCodeword(w) = v;
end);

InstallMethod(\=, "poly=codeword", true, 
	[IsUnivariatePolynomial, IsCodeword], 0, 
function(p, w)
	return VectorCodeword(Codeword(p)) = VectorCodeword(w);
end);

InstallMethod(\=, "codeword=poly", true, 
	[IsCodeword, IsUnivariatePolynomial], 0, 
function(w, p)
	return VectorCodeword(w) = VectorCodeword(Codeword(p));
end);

InstallMethod(\=, "string=codeword", true, 
	[IsString, IsCodeword], 0, 
function (s, w)
	return VectorCodeword(Codeword(s)) = VectorCodeword(w);
end);

InstallMethod(\=, "codeword=string", true, 
	[IsCodeword, IsString], 0, 
function(w, s)
	return VectorCodeword(w) = VectorCodeword(Codeword(s));
end);


#############################################################################
##
#F  \<( <l>, <r> )  . . . . . . . . . . . . . . . . less than for codewords
##

InstallMethod(\<, "codeword<codeword", IsIdenticalObj, 
	[IsCodeword, IsCodeword], 0, 
function(a,b)
	return VectorCodeword(a) < VectorCodeword(b);
end);


#############################################################################
##
#F  Support( <v> )  . . . . . . . set of coordinates in which <v> is not zero
##

InstallMethod(Support, "codeword", true, [IsCodeword], 0, 
function (c)
	local i, S, zero;
	S := [];
	zero := Zero(VectorCodeword(c)[1]);   
	for i in [1..WordLength(c)] do 
		if VectorCodeword(c)[i] <> zero then 
			Add(S, i);
		fi;
	od;
	return S;
end);


#############################################################################
##
#F  TreatAsPoly( <v> )  . . . . . . . . . . . .  treat codeword as polynomial
##
##  The codeword <v> will be treated as a polynomial
##

InstallMethod(TreatAsPoly, "codeword", true, [IsCodeword], 0, 
function(c)
	c!.treatAsPoly := true;
end);

InstallOtherMethod(TreatAsPoly, "list of codewords", true, [IsList], 0, 
function(list)
local i;
  if IsCodeword(list) then
    TryNextMethod(); # codeword is list
  fi;
  for i in list do 
    TreatAsPoly(i);
  od;
end);


#############################################################################
##
#F  TreatAsVector( <v> )  . . . . . . . . . . . .  treat codeword as a vector
##
##  The codeword <v> will be treated as a vector
##

InstallMethod(TreatAsVector, "codeword", true, [IsCodeword], 0, 
function(c)
	c!.treatAsPoly := false;
end);

InstallOtherMethod(TreatAsVector, "list of codewords", true, [IsList], 0, 
function(list) 
local i;
  if IsCodeword(list) then
    TryNextMethod(); # codeword is list
  fi;
  for i in list do 
    TreatAsVector(i);
  od;
end);


#############################################################################
##
#F  PolyCodeword( <arg> ) . . . . . . . . . . converts input to polynomial(s)
##
## Input may be codeword, polynomial, vector or a list of those
##  -currently only codeword or list of 

InstallMethod(PolyCodeword, "poly from codeword", true, [IsCodeword], 0, 
function(w)
	local fam, cf, F;
	cf := VectorCodeword(w);
	if Length(cf) > 0 then 
		F := Field(cf);
	else 
		F := GF(2);
	fi;
	fam := ElementsFamily(FamilyObj(F)); 
	return LaurentPolynomialByCoefficients(fam, cf, 0);
end);

InstallOtherMethod(PolyCodeword, "polys from codeword list", true, [IsList], 0, 
function(l)
	return List(l, i->PolyCodeword(Codeword(i)));	
end);


#############################################################################
##
#F  VectorCodeword( <arg> ) . . . . . . . . . . . .  converts input to vector
##
## Input may be codeword, polynomial, vector or a list of those
##  - currently only codeword or list of!

InstallMethod(VectorCodeword, "vector from codeword", true, [IsCodeword], 0, 
function(w)
	local p;
	if HasPolyCodeword(w) then 
		p := PolyCodeword(w);
	else
		Error("VectorCodeword and PolyCodeword both unknown");
	fi;
	p := CoefficientsOfLaurentPolynomial(p);
	p := ShiftedCoeffs(p[1], p[2]);
	return p; 
end);

InstallOtherMethod(VectorCodeword, "vectors from codeword list", true, 
	[IsList], 0, 
function(l)
	return List(l, i->VectorCodeword(Codeword(i)));
end);


#############################################################################
##
#F  Weight( <v> ) . . . . . . . . . . . calculates the weight of codeword <v>
##

InstallMethod(Weight, "codeword", true, [IsCodeword], 0, 
function(w)
    local vec; 
    vec := VectorCodeword(w);
    return DistanceVecFFE( 0*vec, vec );  
end );


#############################################################################
##
#F  DistanceCodeword( <a>, <b> )  . the distance between codeword <a> and <b>
##

InstallMethod(DistanceCodeword, "two codewords", true, 
	[IsCodeword, IsCodeword], 0, 
function(w1, w2)
	return DistanceVecFFE(VectorCodeword(w1), VectorCodeword(w2));
end);


#############################################################################
##
#F  NullWord( <C> ) or NullWord( <n>, <F> ) . . . . . . . . . . all zero word
##

InstallMethod(NullWord, "n-FFE", true, [IsInt, IsFFE], 0, 
function(n,ffe)
	return Codeword(List([1..n], i->0), n, ffe);
end);

InstallOtherMethod(NullWord, "n-Field", true, [IsInt, IsField], 0, 
function(n, F)
	return Codeword(List([1..n], i->0), n, One(F));
end);

InstallOtherMethod(NullWord, "n", true, [IsInt], 0, 
function(n)
	return Codeword(List([1..n], i->0), n, One(GF(2)));
end);

InstallOtherMethod(NullWord, "Code", true, [IsCode], 0, 
function(C)
	local n;
	n := WordLength(C); 
	return Codeword(List([1..n], i->0),n,One(LeftActingDomain(C)));
end);


#############################################################################
##
## Zero(<w>) .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  zero codeword 
##
InstallOtherMethod(Zero, "method for codewords", true, [IsCodeword], 0, 
function(w) 
	return Zero(VectorCodeword(w)[1]) * w; 
end);