source: git/ntl/doc/vec_GF2.txt @ 6ce030f

spielwiese
Last change on this file since 6ce030f was e6caf81, checked in by Hans Schönemann <hannes@…>, 20 years ago
*hannes: 5.3.2 git-svn-id: file:///usr/local/Singular/svn/trunk@7472 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 6.3 KB
Line 
1
2/**************************************************************************\
3
4MODULE: vec_GF2
5
6SUMMARY:
7
8a vec_GF2 is a vector of GF2s that behaves much like generic NTL vectors
9(see vector.txt), but there are some differences.
10
11For efficiency, elements of a vec_GF2 are "packed" into a word.
12One may use subscript notation v[i] or v(i) as an r-value
13in an expression, and as an l-value in the following contexts:
14   * on the left-hand side of an assignment operator, 
15   * on the left-hand side of +=, -=, *=, /=,
16   * and as an argument to ++ and --.
17
18One may not use the expression v[i] or v(i) to initialize
19a non-const reference parameter. 
20
21For example, if v, w are vec_GF2's, you can write:
22
23   v[i] = 0;
24   v[i] = v[j] + w[k];
25   v[i] += 1;
26   v[i]++;
27   v[i] += w[i];
28
29It is perhaps helpful to describe how this is implemented,
30without going into all the details.
31The type of a subscript-expression is "subscript_GF2" or
32"const_subscript_GF2", the latter chosen if the vector is read-only.
33Both of these "helper" types have automatic conversions
34operators to GF2.  Moreover, assignment and increment operators
35are defined for "subscript_GF2" (but not for "const_subscript_GF2").
36These operators return references to themselves, so one can
37iterate assignment operators as usual (as usual in NTL,
38the return type of post-increment/decrement is void).
39
40As an alternative, one can use the get and put methods below to access
41vector elements.
42
43There is one subtle but important difference in the semantics
44of vec_GF2 and that of generic NTL vectors.  With a vec_GF2, whenever its
45length is increased (via SetLength), the "new" bits are always 0.
46For example, if v.length() == 20, then
47
48   v.SetLength(10); v.setLength(20);
49
50will effectively clear bits 10..19 of v.
51This is quite different from the semantics of generic NTL vectors, where
52the above sequence would not change the value of v at all.
53One has to be aware of this difference, but it will not matter
54in most ordinary circumstances.
55
56
57\**************************************************************************/
58
59
60
61class vec_GF2 {
62
63public:
64
65   vec_GF2(); // 0 length vector
66   vec_GF2(INIT_SIZE_TYPE, long n); // initialize to length n
67                                      // usage: vec_GF2(INIT_SIZE, n)
68
69   vec_GF2(const vec_GF2& a); // copy constructor
70   vec_GF2& operator=(const vec_GF2& a); // assignment
71   ~vec_GF2(); // destructor
72
73   void SetLength(long n); // set length to n bits
74   void SetMaxLength(long n); // allocate space for n bits
75
76   long length() const; // current length, in bits
77
78   long MaxLength() const; // maximum length, i.e., the maximum
79                           // value passed to either SetLength or SetMaxLength
80                           // since creation or last kill
81
82   long allocated() const; // number of bits for which space is allocated;
83                           // if n <= v.allocated(), then v.SetLength(n)
84                           // will not result in any memory re-allocation.
85
86   // INVARIANT:
87   //    length() <= MaxLength() <= allocated() < 2^(NTL_BITS_PER_LONG-4)
88
89   
90
91   void FixLength(long n); // fix length to n bits
92      // can only be applied after default initialization or kill
93
94   long fixed() const; // test if length has been fixed
95
96   void kill(); // free space and make length 0
97
98   GF2 get(long i) const; // fetch value at index i (indexing from 0)
99
100   void put(long i, GF2 a); // write value a to index i (indexing from 0)
101   void put(long i, long a);
102
103   // Here are the subscripting operators, defined using the
104   // "helper" classes subscript_GF2 and const_subscript_GF2.
105
106   subscript_GF2 operator[](long i);
107
108   subscript_GF2 operator()(long i);
109
110   const_subscript_GF2 operator[](long i) const;
111
112   const_subscript_GF2 operator()(long i) const;
113
114};
115
116
117void swap(vec_GF2& x, vec_GF2& y);
118// swap x and y (fast pointer swap)
119
120void append(vec_GF2& v, GF2 a);
121// append a to v
122
123void append(vec_GF2& v, const vec_GF2& a);
124// append a to v
125
126// equality operators:
127
128long operator==(const vec_GF2& a, const vec_GF2& b);
129long operator!=(const vec_GF2& a, const vec_GF2& b);
130
131
132// I/O operators:
133
134ostream& operator<<(ostream& s, const vec_GF2& a);
135istream& operator>>(istream& s, vec_GF2& a);
136
137// The I/O format is [a_0 a_1 ... a_{n-1}], where each a_i is "0" or "1".
138// On input, the a_i may be arbitrary integers, which are reduced mod 2.
139
140// utility routines:
141
142void clear(vec_GF2& x); // clear all bits--length unchanged
143long IsZero(const vec_GF2& a); // test if all bits are zero
144
145void shift(vec_GF2& x, const vec_GF2& a, long n);
146vec_GF2 shift(const vec_GF2& a, long n);
147// x = a shifted n places, where n may be positive or negative.
148// Generally, x[i] = a[i-n], so positive n shifts to a higher index.
149// The length of x is set to the length of a, and bits
150// are zero-filled or discarded as necessary.
151
152void reverse(vec_GF2& x, const vec_GF2& a); // c = a reversed
153vec_GF2 reverse(const vec_GF2& a);
154
155long weight(const vec_GF2& a); // return number of 1 bits in a
156
157void random(vec_GF2& x, long n);  // x = random vector of length n
158vec_GF2 random_vec_GF2(long n);
159
160
161// arithmetic operations over GF(2):
162
163void add(vec_GF2& x, const vec_GF2& a, const vec_GF2& b);
164void sub(vec_GF2& x, const vec_GF2& a, const vec_GF2& b);
165void negate(vec_GF2& x, const vec_GF2& a);
166
167void mul(vec_GF2& x, const vec_GF2& a, GF2 b);
168void mul(vec_GF2& x, const vec_GF2& a, long b);
169
170void mul(vec_GF2& x, GF2 a, const vec_GF2& b);
171void mul(vec_GF2& x, long a, const vec_GF2& b);
172// x = a * b
173
174void InnerProduct(GF2& x, const vec_GF2& a, const vec_GF2& b);
175// vectors may differ in length
176
177void VectorCopy(vec_GF2& x, const vec_GF2& a, long n);
178vec_GF2 VectorCopy(const vec_GF2& a, long n);
179// x = a copy of a of length exactly n.
180// The input is truncated or padded with zeroes, as necessary.
181
182
183
184// arithmetic operator notation:
185
186vec_GF2 operator+(const vec_GF2& a, const vec_GF2& b);
187vec_GF2 operator-(const vec_GF2& a, const vec_GF2& b);
188vec_GF2 operator-(const vec_GF2& a);
189
190// scalar mul:
191
192vec_GF2 operator*(const vec_GF2& a, GF2 b);
193vec_GF2 operator*(const vec_GF2& a, long b);
194
195vec_GF2 operator*(GF2 a, const vec_GF2& b);
196vec_GF2 operator*(long a, const vec_GF2& b);
197
198// inner product:
199
200inline GF2 operator*(const vec_GF2& a, const vec_GF2& b);
201
202// assignment operator notation:
203
204vec_GF2& operator+=(vec_GF2& x, const vec_GF2& a);
205vec_GF2& operator-=(vec_GF2& x, const vec_GF2& a);
206
207vec_GF2& operator*=(vec_GF2& x, GF2 a);
208vec_GF2& operator*=(vec_GF2& x, long a);
209
Note: See TracBrowser for help on using the repository browser.