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
|
/*!
* \file
* \brief Implementation of a class for algebra on GF(2) (binary) matrices
* \author Erik G. Larsson and Adam Piatyszek
*
* -------------------------------------------------------------------------
*
* IT++ - C++ library of mathematical, signal processing, speech processing,
* and communications classes and functions
*
* Copyright (C) 1995-2008 (see AUTHORS file for a list of contributors)
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* -------------------------------------------------------------------------
*/
#include <itpp/base/gf2mat.h>
#include <itpp/base/specmat.h>
#include <itpp/base/matfunc.h>
#include <itpp/base/converters.h>
#include <iostream>
namespace itpp {
// ====== IMPLEMENTATION OF THE ALIST CLASS ==========
GF2mat_sparse_alist::GF2mat_sparse_alist(const std::string &fname)
: data_ok(false)
{
read(fname);
}
void GF2mat_sparse_alist::read(const std::string &fname)
{
std::ifstream file;
std::string line;
std::stringstream ss;
file.open(fname.c_str());
it_assert(file.is_open(),
"GF2mat_sparse_alist::read(): Could not open file \""
<< fname << "\" for reading");
// parse N and M:
getline(file, line);
ss << line;
ss >> N >> M;
it_assert(!ss.fail(),
"GF2mat_sparse_alist::read(): Wrong alist data (N or M)");
it_assert((N > 0) && (M > 0),
"GF2mat_sparse_alist::read(): Wrong alist data");
ss.seekg(0, std::ios::end);
ss.clear();
// parse max_num_n and max_num_m
getline(file, line);
ss << line;
ss >> max_num_n >> max_num_m;
it_assert(!ss.fail(),
"GF2mat_sparse_alist::read(): Wrong alist data (max_num_{n,m})");
it_assert((max_num_n >= 0) && (max_num_n <= N) &&
(max_num_m >= 0) && (max_num_m <= M),
"GF2mat_sparse_alist::read(): Wrong alist data");
ss.seekg(0, std::ios::end);
ss.clear();
// parse weight of each column n
num_nlist.set_size(N);
num_nlist.clear();
getline(file, line);
ss << line;
for (int i = 0; i < N; i++) {
ss >> num_nlist(i);
it_assert(!ss.fail(),
"GF2mat_sparse_alist::read(): Wrong alist data (num_nlist("
<< i << "))");
it_assert((num_nlist(i) >= 0) && (num_nlist(i) <= M),
"GF2mat_sparse_alist::read(): Wrong alist data (num_nlist("
<< i << "))");
}
ss.seekg(0, std::ios::end);
ss.clear();
// parse weight of each row m
num_mlist.set_size(M);
num_mlist.clear();
getline(file, line);
ss << line;
for (int i = 0; i < M; i++) {
ss >> num_mlist(i);
it_assert(!ss.fail(),
"GF2mat_sparse_alist::read(): Wrong alist data (num_mlist("
<< i << "))");
it_assert((num_mlist(i) >= 0) && (num_mlist(i) <= N),
"GF2mat_sparse_alist::read(): Wrong alist data (num_mlist("
<< i << "))");
}
ss.seekg(0, std::ios::end);
ss.clear();
// parse coordinates in the n direction with non-zero entries
nlist.set_size(N, max_num_n);
nlist.clear();
for (int i = 0; i < N; i++) {
getline(file, line);
ss << line;
for (int j = 0; j < num_nlist(i); j++) {
ss >> nlist(i, j);
it_assert(!ss.fail(),
"GF2mat_sparse_alist::read(): Wrong alist data (nlist("
<< i << "," << j << "))");
it_assert((nlist(i, j) >= 0) && (nlist(i, j) <= M),
"GF2mat_sparse_alist::read(): Wrong alist data (nlist("
<< i << "," << j << "))");
}
ss.seekg(0, std::ios::end);
ss.clear();
}
// parse coordinates in the m direction with non-zero entries
mlist.set_size(M, max_num_m);
mlist.clear();
for (int i = 0; i < M; i++) {
getline(file, line);
ss << line;
for (int j = 0; j < num_mlist(i); j++) {
ss >> mlist(i, j);
it_assert(!ss.fail(),
"GF2mat_sparse_alist::read(): Wrong alist data (mlist("
<< i << "," << j << "))");
it_assert((mlist(i, j) >= 0) && (mlist(i, j) <= N),
"GF2mat_sparse_alist::read(): Wrong alist data (mlist("
<< i << "," << j << "))");
}
ss.seekg(0, std::ios::end);
ss.clear();
}
file.close();
data_ok = true;
}
void GF2mat_sparse_alist::write(const std::string &fname) const
{
it_assert(data_ok,
"GF2mat_sparse_alist::write(): alist data not ready for writing");
std::ofstream file(fname.c_str(), std::ofstream::out);
it_assert(file.is_open(),
"GF2mat_sparse_alist::write(): Could not open file \""
<< fname << "\" for writing");
file << N << " " << M << std::endl;
file << max_num_n << " " << max_num_m << std::endl;
for (int i = 0; i < num_nlist.length() - 1; i++)
file << num_nlist(i) << " ";
file << num_nlist(num_nlist.length() - 1) << std::endl;
for (int i = 0; i < num_mlist.length() - 1; i++)
file << num_mlist(i) << " ";
file << num_mlist(num_mlist.length() - 1) << std::endl;
for (int i = 0; i < N; i++) {
for (int j = 0; j < num_nlist(i) - 1; j++)
file << nlist(i, j) << " ";
file << nlist(i, num_nlist(i) - 1) << std::endl;
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < num_mlist(i) - 1; j++)
file << mlist(i, j) << " ";
file << mlist(i, num_mlist(i) - 1) << std::endl;
}
file.close();
}
GF2mat_sparse GF2mat_sparse_alist::to_sparse(bool transpose) const
{
GF2mat_sparse sbmat(M, N, max_num_m);
for (int i = 0; i < M; i++) {
for (int j = 0; j < num_mlist(i); j++) {
sbmat.set_new(i, mlist(i, j) - 1, bin(1));
}
}
sbmat.compact();
if (transpose) {
return sbmat.transpose();
} else {
return sbmat;
}
}
// ----------------------------------------------------------------------
// WARNING: This method is very slow. Sparse_Mat has to be extended with
// some extra functions to improve the performance of this.
// ----------------------------------------------------------------------
void GF2mat_sparse_alist::from_sparse(const GF2mat_sparse &sbmat,
bool transpose)
{
if (transpose) {
from_sparse(sbmat.transpose(), false);
}
else {
// check matrix dimension
M = sbmat.rows();
N = sbmat.cols();
num_mlist.set_size(M);
num_nlist.set_size(N);
// fill mlist matrix, num_mlist vector and max_num_m
mlist.set_size(0, 0);
int tmp_cols = 0; // initial number of allocated columns
for (int i = 0; i < M; i++) {
ivec temp_row(0);
for (int j = 0; j < N; j++) {
if (sbmat(i, j) == bin(1)) {
temp_row = concat(temp_row, j + 1);
}
}
int trs = temp_row.size();
if (trs > tmp_cols) {
tmp_cols = trs;
mlist.set_size(M, tmp_cols, true);
}
else if (trs < tmp_cols) {
temp_row.set_size(tmp_cols, true);
}
mlist.set_row(i, temp_row);
num_mlist(i) = trs;
}
max_num_m = max(num_mlist);
// fill nlist matrix, num_nlist vector and max_num_n
nlist.set_size(0, 0);
tmp_cols = 0; // initial number of allocated columns
for (int j = 0; j < N; j++) {
ivec temp_row = sbmat.get_col(j).get_nz_indices() + 1;
int trs = temp_row.size();
if (trs > tmp_cols) {
tmp_cols = trs;
nlist.set_size(N, tmp_cols, true);
}
else if (trs < tmp_cols) {
temp_row.set_size(tmp_cols, true);
}
nlist.set_row(j, temp_row);
num_nlist(j) = trs;
}
max_num_n = max(num_nlist);
data_ok = true;
}
}
// ----------------------------------------------------------------------
// Implementation of a dense GF2 matrix class
// ----------------------------------------------------------------------
GF2mat::GF2mat(int i, int j): nrows(i), ncols(j),
nwords((j >> shift_divisor) + 1)
{
data.set_size(nrows, nwords);
data.clear();
}
GF2mat::GF2mat(): nrows(1), ncols(1), nwords(1)
{
data.set_size(nrows, nwords);
data.clear();
}
GF2mat::GF2mat(const bvec &x, bool is_column)
{
if (is_column) { // create column vector
nrows = length(x);
ncols = 1;
nwords = 1;
data.set_size(nrows, nwords);
data.clear();
for (int i = 0; i < nrows; i++) {
set(i, 0, x(i));
}
} else { // create row vector
nrows = 1;
ncols = length(x);
nwords = (ncols >> shift_divisor) + 1;
data.set_size(nrows, nwords);
data.clear();
for (int i = 0; i < ncols; i++) {
set(0, i, x(i));
}
}
}
GF2mat::GF2mat(const bmat &X): nrows(X.rows()), ncols(X.cols())
{
nwords = (ncols >> shift_divisor) + 1;
data.set_size(nrows, nwords);
data.clear();
for (int i = 0; i < nrows; i++) {
for (int j = 0; j < ncols; j++) {
set(i, j, X(i, j));
}
}
}
GF2mat::GF2mat(const GF2mat_sparse &X)
{
nrows=X.rows();
ncols=X.cols();
nwords = (ncols >> shift_divisor) + 1;
data.set_size(nrows,nwords);
for (int i=0; i<nrows; i++) {
for (int j=0; j<nwords; j++) {
data(i,j) = 0;
}
}
for (int j=0; j<ncols; j++) {
for (int i=0; i<X.get_col(j).nnz(); i++) {
bin b = X.get_col(j).get_nz_data(i);
set(X.get_col(j).get_nz_index(i),j,b);
}
}
}
GF2mat::GF2mat(const GF2mat_sparse &X, int m1, int n1, int m2, int n2)
{
it_assert(X.rows()>m2,"GF2mat(): indexes out of range");
it_assert(X.cols()>n2,"GF2mat(): indexes out of range");
it_assert(m1>=0 && n1>=0 && m2>=m1 && n2>=n1,
"GF2mat::GF2mat(): indexes out of range");
nrows=m2-m1+1;
ncols=n2-n1+1;
nwords = (ncols >> shift_divisor) + 1;
data.set_size(nrows,nwords);
for (int i=0; i<nrows; i++) {
for (int j=0; j<nwords; j++) {
data(i,j) = 0;
}
}
for (int i=0; i<nrows; i++) {
for (int j=0; j<ncols; j++) {
bin b = X(i+m1,j+n1);
set(i,j,b);
}
}
}
GF2mat::GF2mat(const GF2mat_sparse &X, const ivec &columns)
{
it_assert(X.cols()>max(columns),
"GF2mat::GF2mat(): index out of range");
it_assert(min(columns)>=0,
"GF2mat::GF2mat(): column index must be positive");
nrows=X.rows();
ncols=length(columns);
nwords = (ncols >> shift_divisor) + 1;
data.set_size(nrows,nwords);
for (int i=0; i<nrows; i++) {
for (int j=0; j<nwords; j++) {
data(i,j) = 0;
}
}
for (int j=0; j<ncols; j++) {
for (int i=0; i<X.get_col(columns(j)).nnz(); i++) {
bin b = X.get_col(columns(j)).get_nz_data(i);
set(X.get_col(columns(j)).get_nz_index(i),j,b);
}
}
}
void GF2mat::set_size(int m, int n, bool copy)
{
nrows = m;
ncols = n;
nwords = (ncols >> shift_divisor) + 1;
data.set_size(nrows, nwords, copy);
if (!copy)
data.clear();
}
GF2mat_sparse GF2mat::sparsify() const
{
GF2mat_sparse Z(nrows,ncols);
for (int i=0; i<nrows; i++) {
for (int j=0; j<ncols; j++) {
if (get(i,j)==1) {
Z.set(i,j,1);
}
}
}
return Z;
}
bvec GF2mat::bvecify() const
{
it_assert(nrows==1 || ncols==1,
"GF2mat::bvecify() matrix must be a vector");
int n = (nrows == 1 ? ncols : nrows);
bvec result(n);
if (nrows == 1) {
for (int i = 0; i < n; i++) {
result(i) = get(0, i);
}
} else {
for (int i = 0; i < n; i++) {
result(i) = get(i, 0);
}
}
return result;
}
void GF2mat::set_row(int i, bvec x)
{
it_assert(length(x)==ncols,
"GF2mat::set_row(): dimension mismatch");
for (int j=0; j<ncols; j++) {
set(i,j,x(j));
}
}
void GF2mat::set_col(int j, bvec x)
{
it_assert(length(x)==nrows,
"GF2mat::set_col(): dimension mismatch");
for (int i=0; i<nrows; i++) {
set(i,j,x(i));
}
}
GF2mat gf2dense_eye(int m)
{
GF2mat Z(m,m);
for (int i=0; i<m; i++) {
Z.set(i,i,1);
}
return Z;
}
GF2mat GF2mat::get_submatrix(int m1, int n1, int m2, int n2) const
{
it_assert(m1>=0 && n1>=0 && m2>=m1 && n2>=n1
&& m2<nrows && n2<ncols,
"GF2mat::get_submatrix() index out of range");
GF2mat result(m2-m1+1,n2-n1+1);
for (int i=m1; i<=m2; i++) {
for (int j=n1; j<=n2; j++) {
result.set(i-m1,j-n1,get(i,j));
}
}
return result;
}
GF2mat GF2mat::concatenate_vertical(const GF2mat &X) const
{
it_assert(X.ncols==ncols,
"GF2mat::concatenate_vertical(): dimension mismatch");
it_assert(X.nwords==nwords,
"GF2mat::concatenate_vertical(): dimension mismatch");
GF2mat result(nrows+X.nrows,ncols);
for (int i=0; i<nrows; i++) {
for (int j=0; j<nwords; j++) {
result.data(i,j) = data(i,j);
}
}
for (int i=0; i<X.nrows; i++) {
for (int j=0; j<nwords; j++) {
result.data(i+nrows,j) = X.data(i,j);
}
}
return result;
}
GF2mat GF2mat::concatenate_horizontal(const GF2mat &X) const
{
it_assert(X.nrows==nrows,
"GF2mat::concatenate_horizontal(): dimension mismatch");
GF2mat result(nrows,X.ncols+ncols);
for (int i=0; i<nrows; i++) {
for (int j=0; j<ncols; j++) {
result.set(i,j,get(i,j));
}
}
for (int i=0; i<nrows; i++) {
for (int j=0; j<X.ncols; j++) {
result.set(i,j+ncols,X.get(i,j));
}
}
return result;
}
bvec GF2mat::get_row(int i) const
{
bvec result(ncols);
for (int j=0; j<ncols; j++) {
result(j) = get(i,j);
}
return result;
}
bvec GF2mat::get_col(int j) const
{
bvec result(nrows);
for (int i=0; i<nrows; i++) {
result(i) = get(i,j);
}
return result;
}
int GF2mat::T_fact(GF2mat &T, GF2mat &U, ivec &perm) const
{
T = gf2dense_eye(nrows);
U = *this;
perm = zeros_i(ncols);
for (int i=0; i<ncols; i++) {
perm(i) = i;
}
if (nrows>250) { // avoid cluttering output ...
it_info_debug("Performing T-factorization of GF(2) matrix... rows: "
<< nrows << " cols: " << ncols << " .... " << std::endl);
}
int pdone=0;
for (int j=0; j<nrows; j++) {
// Now working on diagonal element j,j
// First try find a row with a 1 in column i
int i1,j1;
for (i1=j; i1<nrows; i1++) {
for (j1=j; j1<ncols; j1++) {
if (U.get(i1,j1)==1) { goto found; }
}
}
return j;
found:
U.swap_rows(i1,j);
T.swap_rows(i1,j);
U.swap_cols(j1,j);
int temp = perm(j);
perm(j) = perm(j1);
perm(j1) = temp;
// now subtract row i from remaining rows
for (int i1=j+1; i1<nrows; i1++) {
if (U.get(i1,j)==1) {
int ptemp = floor_i(100.0*(i1+j*nrows)/(nrows*nrows));
if (nrows>250 && ptemp>pdone+10) {
it_info_debug(ptemp << "% done.");
pdone=ptemp;
}
U.add_rows(i1,j);
T.add_rows(i1,j);
}
}
}
return nrows;
}
int GF2mat::T_fact_update_bitflip(GF2mat &T, GF2mat &U,
ivec &perm, int rank,
int r, int c) const
{
// First, update U (before re-triangulization)
int c0=c;
for (c=0; c<ncols; c++) {
if (perm(c)==c0) {
goto foundidx;
}
}
it_error("GF2mat::T_fact_update_bitflip() - internal error");
foundidx:
for (int i=0; i<nrows; i++) {
if (T.get(i,r)==1) {
U.addto_element(i,c,1);
}
}
// first move column c to the end
bvec lastcol = U.get_col(c);
int temp_perm = perm(c);
for (int j=c; j<ncols-1; j++) {
U.set_col(j,U.get_col(j+1));
perm(j) = perm(j+1);
}
U.set_col(ncols-1,lastcol);
perm(ncols-1) = temp_perm;
// then, if the matrix is tall, also move row c to the end
if (nrows>=ncols) {
bvec lastrow_U = U.get_row(c);
bvec lastrow_T = T.get_row(c);
for (int i=c; i<nrows-1; i++) {
U.set_row(i,U.get_row(i+1));
T.set_row(i,T.get_row(i+1));
}
U.set_row(nrows-1,lastrow_U);
T.set_row(nrows-1,lastrow_T);
// Do Gaussian elimination on the last row
for (int j=c; j<ncols; j++) {
if (U.get(nrows-1,j)==1) {
U.add_rows(nrows-1,j);
T.add_rows(nrows-1,j);
}
}
}
// Now, continue T-factorization from the point (rank-1,rank-1)
for (int j=rank-1; j<nrows; j++) {
int i1,j1;
for (i1=j; i1<nrows; i1++) {
for (j1=j; j1<ncols; j1++) {
if (U.get(i1,j1)==1) {
goto found;
}
}
}
return j;
found:
U.swap_rows(i1,j);
T.swap_rows(i1,j);
U.swap_cols(j1,j);
int temp = perm(j);
perm(j) = perm(j1);
perm(j1) = temp;
for (int i1=j+1; i1<nrows; i1++) {
if (U.get(i1,j)==1) {
U.add_rows(i1,j);
T.add_rows(i1,j);
}
}
}
return nrows;
}
bool GF2mat::T_fact_update_addcol(GF2mat &T, GF2mat &U,
ivec &perm, bvec newcol) const
{
int i0 = T.rows();
int j0 = U.cols();
it_assert(j0>0,"GF2mat::T_fact_update_addcol(): dimension mismatch");
it_assert(i0==T.cols(),
"GF2mat::T_fact_update_addcol(): dimension mismatch");
it_assert(i0==U.rows(),
"GF2mat::T_fact_update_addcol(): dimension mismatch");
it_assert(length(perm)==j0,
"GF2mat::T_fact_update_addcol(): dimension mismatch");
it_assert(U.get(j0-1,j0-1)==1,
"GF2mat::T_fact_update_addcol(): dimension mismatch");
// The following test is VERY TIME-CONSUMING
it_assert_debug(U.row_rank()==j0,
"GF2mat::T_fact_update_addcol(): factorization has incorrect rank");
bvec z = T*newcol;
GF2mat Utemp = U.concatenate_horizontal(GF2mat(z,1));
// start working on position (j0,j0)
int i;
for (i=j0; i<i0; i++) {
if (Utemp.get(i,j0)==1) {
goto found;
}
}
return (false); // adding the new column would not improve the rank
found:
perm.set_length(j0+1,true);
perm(j0) = j0;
Utemp.swap_rows(i,j0);
T.swap_rows(i,j0);
for (int i1=j0+1; i1<i0; i1++) {
if (Utemp.get(i1,j0)==1) {
Utemp.add_rows(i1,j0);
T.add_rows(i1,j0);
}
}
U = Utemp;
return (true); // the new column was successfully added
}
GF2mat GF2mat::inverse() const
{
it_assert(nrows==ncols,"GF2mat::inverse(): Matrix must be square");
// first compute the T-factorization
GF2mat T,U;
ivec perm;
int rank = T_fact(T,U,perm);
it_assert(rank==ncols,"GF2mat::inverse(): Matrix is not full rank");
// backward substitution
for (int i=ncols-2; i>=0; i--) {
for (int j=ncols-1; j>i; j--) {
if (U.get(i,j)==1) {
U.add_rows(i,j);
T.add_rows(i,j);
}
}
}
T.permute_rows(perm,1);
return T;
}
int GF2mat::row_rank() const
{
GF2mat T,U;
ivec perm;
int rank = T_fact(T,U,perm);
return rank;
}
bool GF2mat::is_zero() const
{
for (int i=0; i<nrows; i++) {
for (int j=0; j<nwords; j++) {
if (data(i,j)!=0) {
return false;
}
}
}
return true;
}
bool GF2mat::operator==(const GF2mat &X) const
{
if (X.nrows!=nrows) { return false; }
if (X.ncols!=ncols) { return false; }
it_assert(X.nwords==nwords,"GF2mat::operator==() dimension mismatch");
for (int i=0; i<nrows; i++) {
for (int j=0; j<nwords; j++) {
// if (X.get(i,j)!=get(i,j)) {
if (X.data(i,j)!=data(i,j)) {
return false;
}
}
}
return true;
}
void GF2mat::add_rows(int i, int j)
{
it_assert(i>=0 && i<nrows,"GF2mat::add_rows(): out of range");
it_assert(j>=0 && j<nrows,"GF2mat::add_rows(): out of range");
for (int k=0; k<nwords; k++) {
data(i,k) ^= data(j,k);
}
}
void GF2mat::swap_rows(int i, int j)
{
it_assert(i>=0 && i<nrows,"GF2mat::swap_rows(): index out of range");
it_assert(j>=0 && j<nrows,"GF2mat::swap_rows(): index out of range");
for (int k=0; k<nwords; k++) {
int temp = data(i,k);
data(i,k) = data(j,k);
data(j,k) = temp;
}
}
void GF2mat::swap_cols(int i, int j)
{
it_assert(i>=0 && i<ncols,"GF2mat::swap_cols(): index out of range");
it_assert(j>=0 && j<ncols,"GF2mat::swap_cols(): index out of range");
bvec temp = get_col(i);
set_col(i,get_col(j));
set_col(j,temp);
}
void GF2mat::operator=(const GF2mat &X)
{
nrows=X.nrows;
ncols=X.ncols;
nwords=X.nwords;
data = X.data;
}
GF2mat operator*(const GF2mat &X, const GF2mat &Y)
{
it_assert(X.ncols==Y.nrows,"GF2mat::operator*(): dimension mismatch");
it_assert(X.nwords>0,"Gfmat::operator*(): dimension mismatch");
it_assert(Y.nwords>0,"Gfmat::operator*(): dimension mismatch");
/*
// this can be done more efficiently?
GF2mat result(X.nrows,Y.ncols);
for (int i=0; i<X.nrows; i++) {
for (int j=0; j<Y.ncols; j++) {
bin b=0;
for (int k=0; k<X.ncols; k++) {
bin x = X.get(i,k);
bin y = Y.get(k,j);
b ^= (x&y);
}
result.set(i,j,b);
}
}
return result; */
// is this better?
return mult_trans(X,Y.transpose());
}
bvec operator*(const GF2mat &X, const bvec &y)
{
it_assert(length(y)==X.ncols,"GF2mat::operator*(): dimension mismatch");
it_assert(X.nwords>0,"Gfmat::operator*(): dimension mismatch");
/*
// this can be done more efficiently?
bvec result = zeros_b(X.nrows);
for (int i=0; i<X.nrows; i++) {
// do the nwords-1 data columns first
for (int j=0; j<X.nwords-1; j++) {
int ind = j<<shift_divisor;
unsigned char r=X.data(i,j);
while (r) {
result(i) ^= (r & y(ind));
r >>= 1;
ind++;
}
}
// do the last column separately
for (int j=(X.nwords-1)<<shift_divisor; j<X.ncols; j++) {
result(i) ^= (X.get(i,j) & y(j));
}
}
return result; */
// is this better?
return (mult_trans(X,GF2mat(y,0))).bvecify();
}
GF2mat mult_trans(const GF2mat &X, const GF2mat &Y)
{
it_assert(X.ncols==Y.ncols,"GF2mat::mult_trans(): dimension mismatch");
it_assert(X.nwords>0,"GF2mat::mult_trans(): dimension mismatch");
it_assert(Y.nwords>0,"GF2mat::mult_trans(): dimension mismatch");
it_assert(X.nwords==Y.nwords,"GF2mat::mult_trans(): dimension mismatch");
GF2mat result(X.nrows,Y.nrows);
for (int i=0; i<X.nrows; i++) {
for (int j=0; j<Y.nrows; j++) {
bin b=0;
int kindx =i;
int kindy =j;
for (int k=0; k<X.nwords; k++) {
unsigned char r = X.data(kindx) & Y.data(kindy);
/* The following can be speeded up by using a small lookup
table for the number of ones and shift only a few times (or
not at all if table is large) */
while (r) {
b ^= r&1;
r>>=1;
};
kindx += X.nrows;
kindy += Y.nrows;
}
result.set(i,j,b);
}
}
return result;
}
GF2mat GF2mat::transpose() const
{
// CAN BE SPEEDED UP
GF2mat result(ncols,nrows);
for (int i=0; i<nrows; i++) {
for (int j=0; j<ncols; j++) {
result.set(j,i,get(i,j));
}
}
return result;
}
GF2mat operator+(const GF2mat &X, const GF2mat &Y)
{
it_assert(X.nrows==Y.nrows,"GF2mat::operator+(): dimension mismatch");
it_assert(X.ncols==Y.ncols,"GF2mat::operator+(): dimension mismatch");
it_assert(X.nwords==Y.nwords,"GF2mat::operator+(): dimension mismatch");
GF2mat result(X.nrows,X.ncols);
for (int i=0; i<X.nrows; i++) {
for (int j=0; j<X.nwords; j++) {
result.data(i,j) = X.data(i,j) ^ Y.data(i,j);
}
}
return result;
}
void GF2mat::permute_cols(ivec &perm, bool I)
{
it_assert(length(perm)==ncols,
"GF2mat::permute_cols(): dimensions do not match");
GF2mat temp = (*this);
for (int j=0; j<ncols; j++) {
if (I==0) {
set_col(j,temp.get_col(perm(j)));
} else {
set_col(perm(j),temp.get_col(j));
}
}
}
void GF2mat::permute_rows(ivec &perm, bool I)
{
it_assert(length(perm)==nrows,
"GF2mat::permute_rows(): dimensions do not match");
GF2mat temp = (*this);
for (int i=0; i<nrows; i++) {
if (I==0) {
for (int j=0; j<nwords; j++) {
data(i,j) = temp.data(perm(i),j);
}
} else {
for (int j=0; j<nwords; j++) {
data(perm(i),j) = temp.data(i,j);
}
}
}
}
std::ostream &operator<<(std::ostream &os, const GF2mat &X)
{
int i,j;
os << "---- GF(2) matrix of dimension " << X.nrows << "*" << X.ncols
<< " -- Density: " << X.density() << " ----" << std::endl;
for (i=0; i<X.nrows; i++) {
os << " ";
for (j=0; j<X.ncols; j++) {
os << X.get(i,j) << " ";
}
os << std::endl;
}
return os;
}
double GF2mat::density() const
{
int no_of_ones=0;
for (int i=0; i<nrows; i++) {
for (int j=0; j<ncols; j++) {
no_of_ones += (get(i,j)==1 ? 1 : 0);
}
}
return ((double) no_of_ones)/(nrows*ncols);
}
it_file &operator<<(it_file &f, const GF2mat &X)
{
// 3 64-bit unsigned words for: nrows, ncols and nwords + rest for char data
uint64_t bytecount = 3 * sizeof(uint64_t)
+ X.nrows * X.nwords * sizeof(char);
f.write_data_header("GF2mat", bytecount);
f.low_level_write(static_cast<uint64_t>(X.nrows));
f.low_level_write(static_cast<uint64_t>(X.ncols));
f.low_level_write(static_cast<uint64_t>(X.nwords));
for (int i=0; i<X.nrows; i++) {
for (int j=0; j<X.nwords; j++) {
f.low_level_write(static_cast<char>(X.data(i,j)));
}
}
return f;
}
it_ifile &operator>>(it_ifile &f, GF2mat &X)
{
it_file::data_header h;
f.read_data_header(h);
if (h.type == "GF2mat") {
uint64_t tmp;
f.low_level_read(tmp); X.nrows = static_cast<int>(tmp);
f.low_level_read(tmp); X.ncols = static_cast<int>(tmp);
f.low_level_read(tmp); X.nwords = static_cast<int>(tmp);
X.data.set_size(X.nrows,X.nwords);
for (int i=0; i<X.nrows; i++) {
for (int j=0; j<X.nwords; j++) {
char r;
f.low_level_read(r);
X.data(i,j) = static_cast<unsigned char>(r);
}
}
}
else {
it_error("it_ifile &operator>>() - internal error");
}
return f;
}
} // namespace itpp
|