[ec7c13]: src / ov-galois.cc Maximize Restore History

Download this file

ov-galois.cc    801 lines (673 with data), 18.1 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
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
//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 <octave/config.h>
#include <octave/unwind-prot.h>
#include <octave/pr-output.h>
#include "galois.h"
#include "ov-galois.h"
#include <octave/byte-swap.h>
#include <octave/ls-oct-ascii.h>
DEFINE_OCTAVE_ALLOCATOR (octave_galois);
DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_galois, "galois", "galois");
octave_value
octave_galois::resize (const dim_vector& dv, bool) const
{
if (dv.length () > 2)
{
error ("Can not resize galois structure to NDArray");
return octave_value ();
}
galois retval (gval);
retval.resize (dv);
return new octave_galois (retval);
}
octave_value_list
octave_galois::dotref (const octave_value_list& idx)
{
octave_value_list retval;
assert (idx.length () == 1);
std::string nm = idx(0).string_value ();
if (nm == __GALOIS_PRIMPOLY_STR)
retval(0) = octave_value ((double)gval.primpoly ());
else if (nm == __GALOIS_ORDER_STR)
retval(0) = octave_value ((double)gval.m ());
else if (nm == __GALOIS_DATA_STR)
{
int r = gval.rows ();
int c = gval.columns ();
Matrix data (r, c);
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
data(i, j) = (double)gval(i, j);
retval(0) = octave_value (data);
}
#ifdef GALOIS_DISP_PRIVATES
else if (nm == __GALOIS_LENGTH_STR)
retval(0) = octave_value ((double)gval.n ());
else if (nm == __GALOIS_ALPHA_TO_STR)
{
int n = gval.n ();
Matrix data (n+1, 1);
for (int i = 0; i < n+1; i++)
data(i, 0) = (double)gval.alpha_to (i);
retval(0) = octave_value (data);
}
else if (nm == __GALOIS_INDEX_OF_STR)
{
int n = gval.n ();
Matrix data (n+1, 1);
for (int i = 0; i < n+1; i++)
data(i, 0) = (double)gval.index_of (i);
retval(0) = octave_value (data);
}
#endif
else
error ("galois structure has no member `%s'", nm.c_str ());
return retval;
}
octave_value
octave_galois::do_index_op (const octave_value_list& idx,
bool resize_ok)
{
octave_value retval;
int len = idx.length ();
switch (len)
{
case 2:
{
idx_vector i = idx (0).index_vector ();
idx_vector j = idx (1).index_vector ();
retval = new octave_galois (gval.index (i, j, resize_ok));
}
break;
case 1:
{
idx_vector i = idx (0).index_vector ();
retval = new octave_galois (gval.index (i, resize_ok));
}
break;
default:
{
std::string n = type_name ();
error ("invalid number of indices (%d) for %s value",
len, n.c_str ());
}
break;
}
return retval;
}
octave_value
octave_galois::subsref (const std::string &type,
const std::list<octave_value_list>& idx)
{
octave_value retval;
int skip = 1;
switch (type[0])
{
case '(':
retval = do_index_op (idx.front (), true);
break;
case '.':
{
octave_value_list t = dotref (idx.front ());
retval = (t.length () == 1) ? t(0) : octave_value (t);
}
break;
case '{':
error ("%s cannot be indexed with %c", type_name ().c_str (), type[0]);
break;
default:
panic_impossible ();
}
if (! error_state)
retval = retval.next_subsref (type, idx, skip);
return retval;
}
bool
octave_galois::is_true (void) const
{
bool retval = false;
if (rows () > 0 && columns () > 0)
{
boolMatrix m = (gval.all () . all ());
retval = (m.rows () == 1 && m.columns () == 1 && m(0, 0));
}
return retval;
}
bool
octave_galois::print_as_scalar (void) const
{
int nr = rows ();
int nc = columns ();
return ((nr == 1 && nc == 1) || (nr == 0 || nc == 0));
}
void
octave_galois::print (std::ostream& os, bool) const
{
print_raw (os);
}
void
octave_galois::print_raw (std::ostream& os, bool) const
{
bool first = true;
int m = gval.m ();
int primpoly = gval.primpoly ();
Matrix data (gval.rows (), gval.cols ());
indent (os);
if (m == 1)
os << "GF(2) array.";
else
{
os << "GF(2^" << m << ") array. Primitive Polynomial = ";
for (int i = m; i >= 0; i--)
{
if (primpoly & (1<<i))
{
if (i > 0)
{
if (first)
{
first = false;
os << "D";
}
else
os << "+D";
if (i != 1)
os << "^" << i;
}
else
{
if (first)
{
first = false;
os << "1";
}
else
os << "+1";
}
}
}
os << " (decimal " << primpoly << ")";
}
newline (os);
newline (os);
indent (os);
os << "Array elements = ";
newline (os);
newline (os);
for (int i = 0; i < gval.rows (); i++)
for (int j = 0; j < gval.columns (); j++)
data(i, j) = (double)gval(i, j);
octave_print_internal (os, data, false, current_print_indent_level ());
newline (os);
}
bool
octave_galois::print_name_tag (std::ostream& os, const std::string& name) const
{
bool retval = false;
indent (os);
// Vstruct_levels_to_print was made static in Octave 3.4, which means
// the name_tag might be printed on a seperate line when it shouldn't be.
#if 0
if (Vstruct_levels_to_print < 0)
#else
if (false)
#endif
os << name << " = ";
else
{
os << name << " =";
newline (os);
retval = true;
}
return retval;
}
void
octave_galois::print_info (std::ostream& os, const std::string& prefix) const
{
gval.print_info (os, prefix);
}
double
octave_galois::double_value (bool) const
{
double retval = octave_NaN;
if (rows () > 0 && columns () > 0)
{
gripe_implicit_conversion ("Octave:array-as-scalar",
"real matrix", "real scalar");
retval = (double) gval (0, 0);
}
else
gripe_invalid_conversion ("galois", "real scalar");
return retval;
}
Complex
octave_galois::complex_value (bool) const
{
double tmp = octave_NaN;
Complex retval (tmp, tmp);
if (rows () > 0 && columns () > 0)
{
gripe_implicit_conversion ("Octave:array-as-scalar",
"real matrix", "real scalar");
retval = (double) gval (0, 0);
}
else
gripe_invalid_conversion ("galois", "complex scalar");
return retval;
}
Matrix
octave_galois::matrix_value (bool) const
{
Matrix retval;
retval.resize (rows (), columns ());
for (int i = 0; i < rows (); i++)
for (int j = 0; j < columns (); j++)
retval(i, j) = gval(i, j);
return retval;
}
NDArray
octave_galois::array_value (bool) const
{
int nr = rows ();
int nc = columns ();
dim_vector dv (nr, nc);
NDArray retval (dv);
for (int i = 0; i < nr; i++)
for (int j = 0; j < nc; j++)
retval(i + j*nr) = gval(i, j);
return retval;
}
void
octave_galois::assign (const octave_value_list& idx,
const galois& rhs)
{
int len = idx.length ();
if (gval.have_field () && rhs.have_field ())
{
if ((gval.m () != rhs.m ()) || (gval.primpoly () != rhs.primpoly ()))
{
(*current_liboctave_error_handler) ("can not assign data between two different Galois Fields");
return;
}
}
switch (len)
{
case 2:
{
idx_vector i = idx (0).index_vector ();
if (! error_state)
{
idx_vector j = idx (1).index_vector ();
if (! error_state)
gval.assign (i, j, rhs);
}
}
break;
case 1:
{
idx_vector i = idx (0).index_vector ();
if (! error_state)
gval.assign (i, rhs);
}
break;
default:
error ("invalid number of indices (%d) for galois assignment",
len);
break;
}
}
bool
octave_galois::save_ascii (std::ostream& os)
{
dim_vector d = dims ();
Matrix tmp = matrix_value ();
// Note use N-D way of writing matrix for eventual conversion
// of octave_galois to handle N-D arrays
os << "# m: " << m () << "\n";
os << "# prim: " << primpoly () << "\n";
os << "# ndims: " << d.length () << "\n";
for (int i = 0; i < d.length (); i++)
os << " " << d (i);
os << "\n" << tmp;
return true;
}
bool
octave_galois::load_ascii (std::istream& is)
{
int mord, prim, mdims;
bool success = true;
if (extract_keyword (is, "m", mord) && extract_keyword (is, "prim", prim) &&
extract_keyword (is, "ndims", mdims))
{
dim_vector dv;
dv.resize (mdims);
for (int i = 0; i < mdims; i++)
is >> dv(i);
if (dv.length () != 2)
{
error ("load: N-D galois matrix not supported");
success = false;
}
else
{
Matrix tmp (dv(0), dv(1));
is >> tmp;
if (!is)
{
error ("load: failed to load matrix constant");
success = false;
}
gval = galois (tmp, mord, prim);
}
}
else
{
error ("load: failed to extract galois field order, primitive and/or imension");
success = false;
}
return success;;
}
bool
octave_galois::save_binary (std::ostream& os, bool& save_as_floats)
{
char tmp = m ();
os.write (X_CAST (char *, &tmp), 1);
int32_t itmp = primpoly ();
os.write (X_CAST (char *, &itmp), 4);
dim_vector d = dims ();
// Don't handle N-D arrays yet
if (d.length () != 2)
return false;
// Use negative value for ndims to be consistent with other formats
itmp = - d.length ();
os.write (X_CAST (char *, &itmp), 4);
for (int i = 0; i < d.length (); i++)
{
itmp = d(i);
os.write (X_CAST (char *, &itmp), 4);
}
Matrix m = matrix_value ();
save_type st;
if (tmp < 8)
st = LS_U_CHAR;
else if (tmp < 16)
st = LS_U_SHORT;
else
st = LS_U_INT;
const double *mtmp = m.data ();
write_doubles (os, mtmp, st, d.numel ());
return true;
}
bool
octave_galois::load_binary (std::istream& is, bool swap,
oct_mach_info::float_format fmt)
{
char mord;
int32_t prim, mdims;
if (! is.read (X_CAST (char *, &mord), 1))
return false;
if (! is.read (X_CAST (char *, &prim), 4))
return false;
if (swap)
swap_bytes <4> (X_CAST (char *, &prim));
if (! is.read (X_CAST (char *, &mdims), 4))
return false;
if (swap)
swap_bytes <4> (X_CAST (char *, &mdims));
// Don't treat N-D arrays yet
if (mdims == -2)
{
mdims = - mdims;
int32_t di;
dim_vector dv;
dv.resize (mdims);
for (int i = 0; i < mdims; i++)
{
if (! is.read (X_CAST (char *, &di), 4))
return false;
if (swap)
swap_bytes <4> (X_CAST (char *, &di));
dv(i) = di;
}
char tmp;
if (! is.read (X_CAST (char *, &tmp), 1))
return false;
Matrix m (dv(0), dv(1));
double *re = m.fortran_vec ();
read_doubles (is, re, X_CAST (save_type, tmp), dv.numel (), swap, fmt);
if (error_state || ! is)
return false;
gval = galois (m, mord, prim);
}
return true;
}
#if defined (HAVE_HDF5)
bool
octave_galois::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats)
{
Matrix mval = matrix_value ();
hid_t group_hid = -1;
#if HAVE_HDF5_18
group_hid = H5Gcreate (loc_id, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
#else
group_hid = H5Gcreate (loc_id, name, 0);
#endif
if (group_hid < 0 ) return false;
dim_vector d = dims ();
OCTAVE_LOCAL_BUFFER (hsize_t, hdims, d.length () > 2 ? d.length () : 3);
hid_t space_hid = -1, data_hid = -1;
bool retval = true;
char tmp;
int32_t itmp;
space_hid = H5Screate_simple (0, hdims, (hsize_t*) 0);
if (space_hid < 0)
{
H5Gclose (group_hid);
return false;
}
#if HAVE_HDF5_18
data_hid = H5Dcreate (group_hid, "m", H5T_NATIVE_UCHAR, space_hid,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
#else
data_hid = H5Dcreate (group_hid, "m", H5T_NATIVE_UCHAR, space_hid,
H5P_DEFAULT);
#endif
if (data_hid < 0)
{
H5Sclose (space_hid);
H5Gclose (group_hid);
return false;
}
tmp = m ();
retval = H5Dwrite (data_hid, H5T_NATIVE_UCHAR, H5S_ALL, H5S_ALL, H5P_DEFAULT,
(void*) &tmp) >= 0;
H5Dclose (data_hid);
if (!retval)
{
H5Sclose (space_hid);
H5Gclose (group_hid);
return false;
}
#if HAVE_HDF5_18
data_hid = H5Dcreate (group_hid, "prim", H5T_NATIVE_UINT, space_hid,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
#else
data_hid = H5Dcreate (group_hid, "prim", H5T_NATIVE_UINT, space_hid,
H5P_DEFAULT);
#endif
if (data_hid < 0)
{
H5Sclose (space_hid);
H5Gclose (group_hid);
return false;
}
itmp = primpoly ();
retval = H5Dwrite (data_hid, H5T_NATIVE_UINT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
(void*) &itmp) >= 0;
H5Dclose (data_hid);
if (!retval)
{
H5Sclose (space_hid);
H5Gclose (group_hid);
return false;
}
H5Sclose (space_hid);
// Octave uses column-major, while HDF5 uses row-major ordering
for (int i = 0, j = d.length () - 1; i < d.length (); i++, j--)
hdims[i] = d (j);
space_hid = H5Screate_simple (d.length (), hdims, (hsize_t*) 0);
if (space_hid < 0) return false;
double *mtmp = mval.fortran_vec ();
OCTAVE_LOCAL_BUFFER (uint32_t, vtmp, d.numel ());
hid_t save_type_hid = H5T_NATIVE_UINT;
if (tmp <= 8)
{
save_type_hid = H5T_NATIVE_UCHAR;
char *wtmp = (char *)vtmp;
for (int i = 0; i < d.numel (); i++)
wtmp[i] = (char) mtmp[i];
}
else if (tmp <= 16)
{
save_type_hid = H5T_NATIVE_USHORT;
uint16_t *wtmp = (uint16_t *)vtmp;
for (int i = 0; i < d.numel (); i++)
wtmp[i] = (uint16_t) mtmp[i];
}
else
{
for (int i = 0; i < d.numel (); i++)
vtmp[i] = (uint32_t) mtmp[i];
}
#if HAVE_HDF5_18
data_hid = H5Dcreate (group_hid, "val", save_type_hid, space_hid,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
#else
data_hid = H5Dcreate (group_hid, "val", save_type_hid, space_hid,
H5P_DEFAULT);
#endif
if (data_hid < 0)
{
H5Sclose (space_hid);
H5Gclose (group_hid);
return false;
}
retval = H5Dwrite (data_hid, save_type_hid, H5S_ALL, H5S_ALL,
H5P_DEFAULT, (void*) vtmp) >= 0;
H5Dclose (data_hid);
H5Sclose (space_hid);
H5Gclose (group_hid);
return retval;
}
bool
octave_galois::load_hdf5 (hid_t loc_id, const char *name,
bool have_h5giterate_bug)
{
bool retval = false;
char mord;
unsigned int prim;
hid_t group_hid, data_hid, space_id;
hsize_t rank;
#if HAVE_HDF5_18
group_hid = H5Gopen (loc_id, name, H5P_DEFAULT);
#else
group_hid = H5Gopen (loc_id, name);
#endif
if (group_hid < 0 ) return false;
#if HAVE_HDF5_18
data_hid = H5Dopen (group_hid, "m", H5P_DEFAULT);
#else
data_hid = H5Dopen (group_hid, "m");
#endif
space_id = H5Dget_space (data_hid);
rank = H5Sget_simple_extent_ndims (space_id);
if (rank != 0)
{
H5Dclose (data_hid);
H5Gclose (group_hid);
return false;
}
if (H5Dread (data_hid, H5T_NATIVE_UCHAR, H5S_ALL, H5S_ALL,
H5P_DEFAULT, (void *) &mord) < 0)
{
H5Dclose (data_hid);
H5Gclose (group_hid);
return false;
}
H5Dclose (data_hid);
#if HAVE_HDF5_18
data_hid = H5Dopen (group_hid, "prim", H5P_DEFAULT);
#else
data_hid = H5Dopen (group_hid, "prim");
#endif
space_id = H5Dget_space (data_hid);
rank = H5Sget_simple_extent_ndims (space_id);
if (rank != 0)
{
H5Dclose (data_hid);
H5Gclose (group_hid);
return false;
}
if (H5Dread (data_hid, H5T_NATIVE_UINT, H5S_ALL, H5S_ALL,
H5P_DEFAULT, (void *) &prim) < 0)
{
H5Dclose (data_hid);
H5Gclose (group_hid);
return false;
}
H5Dclose (data_hid);
#if HAVE_HDF5_18
data_hid = H5Dopen (group_hid, "val", H5P_DEFAULT);
#else
data_hid = H5Dopen (group_hid, "val");
#endif
space_id = H5Dget_space (data_hid);
rank = H5Sget_simple_extent_ndims (space_id);
// Only handle matrices for now
if (rank != 2)
{
H5Sclose (space_id);
H5Dclose (data_hid);
H5Gclose (group_hid);
return false;
}
OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank);
OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank);
H5Sget_simple_extent_dims (space_id, hdims, maxdims);
MArray<int> m (dim_vector (hdims[1], hdims[0]));
int *re = m.fortran_vec ();
if (H5Dread (data_hid, H5T_NATIVE_UINT, H5S_ALL, H5S_ALL,
H5P_DEFAULT, (void *) re) >= 0)
{
retval = true;
gval = galois (m, mord, prim);
}
H5Sclose (space_id);
H5Dclose (data_hid);
return retval;
}
#endif
/*
;;; Local Variables: ***
;;; mode: C++ ***
;;; End: ***
*/