[0404a4]: src / cyclpoly.cc Maximize Restore History

Download this file

cyclpoly.cc    277 lines (252 with data), 7.9 kB

  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
//Copyright (C) 2003 David Bateman
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, see
// <http://www.gnu.org/licenses/>.
//
// In addition to the terms of the GPL, you are permitted to link
// this program with any Open Source program, as defined by the
// Open Source Initiative (www.opensource.org)
#include <iostream>
#include <iomanip>
#include <sstream>
#include <octave/oct.h>
#include <octave/pager.h>
enum cyclic_poly_type
{
CYCLIC_POLY_MIN=0,
CYCLIC_POLY_MAX,
CYCLIC_POLY_ALL,
CYCLIC_POLY_L
};
// A simplified version of the filter function for specific lengths of
// a and b in the Galois field GF(2)
Array<int>
filter_gf2 (const Array<int>& b, const Array<int>& a,
const Array<int>& x, const int& n)
{
int x_len = x.length ();
Array<int> si (dim_vector (n, 1), 0);
Array<int> y (dim_vector (x_len, 1), 0);
for (int i = 0; i < x_len; i++)
{
y(i) = si(0);
if (b(0) && x(i))
y(i) ^= 1;
for (int j = 0; j < n - 1; j++)
{
si(j) = si(j+1);
if (a(j+1) && y(i))
si(j) ^= 1;
if (b(j+1) && x(i))
si(j) ^= 1;
}
si(n-1) = 0;
if (a(n) && y(i))
si(n-1) ^= 1;
if (b(n) && x(i))
si(n-1) ^= 1;
}
return y;
}
// Cyclic polynomial is irreducible. I.E. it divides into x^n-1
// without remainder There must surely be an easier way of doing this
// as the polynomials are over GF(2).
static bool
do_is_cyclic_polynomial (const unsigned long long& a1, const int& n,
const int& m)
{
Array<int> a (dim_vector (n+1, 1), 0);
Array<int> y (dim_vector (n+1, 1), 0);
Array<int> x (dim_vector (n-m+2, 1), 0);
y(0) = 1;
y(n) = 1;
x(0) = 1;
for (int i=0; i < m+1; i++)
a(i) = (a1 & (1UL << i) ? 1 : 0);
Array<int> b = filter_gf2 (y, a, x, n);
b.resize(dim_vector (n+1, 1), 0);
Array<int> p (dim_vector (m+1, 1), 0);
p(0) = 1;
Array<int> q = filter_gf2 (a, p, b, m);
for (int i=0; i < n+1; i++)
if (y(i) ^ q(i))
return false;
return true;
}
DEFUN_DLD (cyclpoly, args, nargout,
"-*- texinfo -*-\n\
@deftypefn {Loadable Function} {@var{y} =} cyclpoly (@var{n}, @var{k})\n\
@deftypefnx {Loadable Function} {@var{y} =} cyclpoly (@var{n}, @var{k}, @var{opt})\n\
@deftypefnx {Loadable Function} {@var{y} =} cyclpoly (@var{n}, @var{k}, @var{opt}, @var{rep})\n\
This function returns the cyclic generator polynomials of the code\n\
[@var{n},@var{k}]. By default the the polynomial with the smallest\n\
weight is returned. However this behavior can be overridden with the\n\
@var{opt} flag. Valid values of @var{opt} are:\n\
\n\
@table @asis\n\
@item @qcode{\"all\"}\n\
Returns all of the polynomials of the code [@var{n},@var{k}]\n\
@item @qcode{\"min\"}\n\
Returns the polynomial of minimum weight of the code [@var{n},@var{k}]\n\
@item @qcode{\"max\"}\n\
Returns the polynomial of the maximum weight of the code [@var{n},@var{k}]\n\
@item @var{l}\n\
Returns the polynomials having exactly the weight @var{l}\n\
@end table\n\
\n\
The polynomials are returns as row-vectors in the variable @var{y}. Each\n\
row of @var{y} represents a polynomial with the least-significant term\n\
first. The polynomials can be returned with an integer representation\n\
if @var{rep} is @qcode{\"integer\"}. The default behaviour is given if @var{rep}\n\
is @qcode{\"polynomial\"}.\n\
@seealso{gf, isprimitive}\n\
@end deftypefn")
{
octave_value retval;
int nargin = args.length ();
bool polyrep = true;
enum cyclic_poly_type type = CYCLIC_POLY_MIN;
RowVector cyclic_polys;
int l=0;
if ((nargin < 2) || (nargin > 4))
{
error ("cyclpoly: incorrect number of arguments");
return retval;
}
int n = args(0).int_value ();
int k = args(1).int_value ();;
if (n < 1)
{
error ("cyclpoly: n must be 1 or greater");
return retval;
}
if (n <= k)
{
error ("cyclpoly: k must be less than n");
return retval;
}
for (int i = 2; i < nargin; i++)
{
if (args(i).is_scalar_type ())
{
l = args(i).int_value ();
type = CYCLIC_POLY_L;
}
else if (args(i).is_string ())
{
std::string s_arg = args(i).string_value ();
if (s_arg == "integer")
polyrep = false;
else if (s_arg == "polynomial")
polyrep = true;
else if (s_arg == "min")
type = CYCLIC_POLY_MIN;
else if (s_arg == "max")
type = CYCLIC_POLY_MAX;
else if (s_arg == "all")
type = CYCLIC_POLY_ALL;
else
{
error ("cyclpoly: invalid argument");
return retval;
}
}
else
{
error ("cyclpoly: incorrect argument type");
return retval;
}
}
int m = n - k;
// Matlab code seems to think that 1+x+x^3 is of larger weight than
// 1+x^2+x^3. So for matlab compatiability the list of polynomials
// should be reversed by replacing "i+=2" with "i-=2" and visa-versa.
// Thats not going to happen!!!
switch (type)
{
case CYCLIC_POLY_MIN:
cyclic_polys.resize (1);
for (unsigned long long i = (1UL<<m)+1; i < (1UL<<(1+m)); i+=2)
if (do_is_cyclic_polynomial (i, n, m))
{
cyclic_polys(0) = (double)i;
break;
}
break;
case CYCLIC_POLY_MAX:
cyclic_polys.resize (1);
for (unsigned long long i = (1UL<<(m+1))-1; i > (1UL<<m); i-=2)
if (do_is_cyclic_polynomial (i, n, m))
{
cyclic_polys(0) = (double)i;
break;
}
break;
case CYCLIC_POLY_ALL:
for (unsigned long long i = (1UL<<m)+1; i < (1UL<<(1+m)); i+=2)
if (do_is_cyclic_polynomial (i, n, m))
{
cyclic_polys.resize (cyclic_polys.length ()+1);
cyclic_polys(cyclic_polys.length ()-1) = (double)i;
}
break;
case CYCLIC_POLY_L:
for (unsigned long long i = ((unsigned long long)1<<m)+1;
i < ((unsigned long long)1<<(1+m)); i+=2)
{
int li = 0;
for (int j=0; j < m+1; j++)
if (i & ((unsigned long long)1 << j))
li++;
if (li == l)
{
if (do_is_cyclic_polynomial (i, n, m))
{
cyclic_polys.resize (cyclic_polys.length ()+1);
cyclic_polys(cyclic_polys.length ()-1) = (double)i;
}
}
}
break;
default:
error ("cyclpoly: impossible");
break;
}
if (cyclic_polys.length () == 0)
{
octave_stdout <<
"cyclpoly: no generator polynomial statifies constraints" << std::endl;
retval = octave_value (Matrix (0, 0));
}
else
{
if (polyrep)
{
Matrix polys (cyclic_polys.length (), m+1, 0);
for (int i = 0 ; i < cyclic_polys.length (); i++)
for (int j = 0; j < m+1; j++)
if ((unsigned long long)cyclic_polys(i) & (1<<j))
polys(i, j) = 1;
retval = octave_value (polys);
}
else
retval = octave_value (cyclic_polys);
}
return retval;
}
/*
;;; Local Variables: ***
;;; mode: C++ ***
;;; End: ***
*/