52 file.open(fname.c_str());
54 "GF2mat_sparse_alist::read(): Could not open file \""
55 << fname <<
"\" for reading");
62 "GF2mat_sparse_alist::read(): Wrong alist data (N or M)");
64 "GF2mat_sparse_alist::read(): Wrong alist data");
65 ss.seekg(0, std::ios::end);
73 "GF2mat_sparse_alist::read(): Wrong alist data (max_num_{n,m})");
76 "GF2mat_sparse_alist::read(): Wrong alist data");
77 ss.seekg(0, std::ios::end);
85 for (
int i = 0;
i <
N;
i++) {
88 "GF2mat_sparse_alist::read(): Wrong alist data (num_nlist("
91 "GF2mat_sparse_alist::read(): Wrong alist data (num_nlist("
94 ss.seekg(0, std::ios::end);
102 for (
int i = 0;
i <
M;
i++) {
105 "GF2mat_sparse_alist::read(): Wrong alist data (num_mlist("
108 "GF2mat_sparse_alist::read(): Wrong alist data (num_mlist("
111 ss.seekg(0, std::ios::end);
117 for (
int i = 0;
i <
N;
i++) {
123 "GF2mat_sparse_alist::read(): Wrong alist data (nlist("
124 <<
i <<
"," <<
j <<
"))");
126 "GF2mat_sparse_alist::read(): Wrong alist data (nlist("
127 <<
i <<
"," <<
j <<
"))");
129 ss.seekg(0, std::ios::end);
136 for (
int i = 0;
i <
M;
i++) {
142 "GF2mat_sparse_alist::read(): Wrong alist data (mlist("
143 <<
i <<
"," <<
j <<
"))");
145 "GF2mat_sparse_alist::read(): Wrong alist data (mlist("
146 <<
i <<
"," <<
j <<
"))");
148 ss.seekg(0, std::ios::end);
159 "GF2mat_sparse_alist::write(): alist data not ready for writing");
161 std::ofstream
file(fname.c_str(), std::ofstream::out);
163 "GF2mat_sparse_alist::write(): Could not open file \""
164 << fname <<
"\" for writing");
166 file <<
N <<
" " <<
M << std::endl;
177 for (
int i = 0;
i <
N;
i++) {
183 for (
int i = 0;
i <
M;
i++) {
197 for (
int i = 0;
i <
M;
i++) {
205 return sbmat.transpose();
234 for (
int i = 0;
i <
M;
i++) {
236 for (
int j = 0;
j <
N;
j++) {
255 nlist.set_size(0, 0);
257 for (
int j = 0;
j <
N;
j++) {
282 nwords((
j >> shift_divisor) + 1)
302 for (
int i = 0;
i < nrows;
i++) {
309 nwords = (ncols >> shift_divisor) + 1;
312 for (
int i = 0;
i < ncols;
i++) {
321 nwords = (ncols >> shift_divisor) + 1;
324 for (
int i = 0;
i < nrows;
i++) {
325 for (
int j = 0;
j < ncols;
j++) {
336 nwords = (ncols >> shift_divisor) + 1;
338 for (
int i = 0;
i < nrows;
i++) {
339 for (
int j = 0;
j < nwords;
j++) {
344 for (
int j = 0;
j < ncols;
j++) {
345 for (
int i = 0;
i <
X.get_col(
j).nnz();
i++) {
346 bin b =
X.get_col(
j).get_nz_data(
i);
347 set(
X.get_col(
j).get_nz_index(
i),
j, b);
354 it_assert(
X.rows() >
m2,
"GF2mat(): indexes out of range");
355 it_assert(
X.cols() > n2,
"GF2mat(): indexes out of range");
357 "GF2mat::GF2mat(): indexes out of range");
361 nwords = (ncols >> shift_divisor) + 1;
364 for (
int i = 0;
i < nrows;
i++) {
365 for (
int j = 0;
j < nwords;
j++) {
370 for (
int i = 0;
i < nrows;
i++) {
371 for (
int j = 0;
j < ncols;
j++) {
382 "GF2mat::GF2mat(): index out of range");
384 "GF2mat::GF2mat(): column index must be positive");
388 nwords = (ncols >> shift_divisor) + 1;
391 for (
int i = 0;
i < nrows;
i++) {
392 for (
int j = 0;
j < nwords;
j++) {
397 for (
int j = 0;
j < ncols;
j++) {
410 nwords = (ncols >> shift_divisor) + 1;
420 for (
int i = 0;
i < nrows;
i++) {
421 for (
int j = 0;
j < ncols;
j++) {
422 if (
get(
i,
j) == 1) {
434 "GF2mat::bvecify() matrix must be a vector");
435 int n = (nrows == 1 ? ncols : nrows);
438 for (
int i = 0;
i < n;
i++) {
443 for (
int i = 0;
i < n;
i++) {
454 "GF2mat::set_row(): dimension mismatch");
455 for (
int j = 0;
j < ncols;
j++) {
463 "GF2mat::set_col(): dimension mismatch");
464 for (
int i = 0;
i < nrows;
i++) {
473 for (
int i = 0;
i < m;
i++) {
482 &&
m2 < nrows && n2 < ncols,
483 "GF2mat::get_submatrix() index out of range");
486 for (
int i =
m1;
i <=
m2;
i++) {
487 for (
int j = n1;
j <= n2;
j++) {
499 "GF2mat::concatenate_vertical(): dimension mismatch");
501 "GF2mat::concatenate_vertical(): dimension mismatch");
504 for (
int i = 0;
i < nrows;
i++) {
505 for (
int j = 0;
j < nwords;
j++) {
510 for (
int i = 0;
i <
X.nrows;
i++) {
511 for (
int j = 0;
j < nwords;
j++) {
522 "GF2mat::concatenate_horizontal(): dimension mismatch");
525 for (
int i = 0;
i < nrows;
i++) {
526 for (
int j = 0;
j < ncols;
j++) {
531 for (
int i = 0;
i < nrows;
i++) {
532 for (
int j = 0;
j <
X.ncols;
j++) {
543 for (
int j = 0;
j < ncols;
j++) {
553 for (
int i = 0;
i < nrows;
i++) {
567 for (
int i = 0;
i < ncols;
i++) {
572 it_info_debug(
"Performing T-factorization of GF(2) matrix... rows: "
573 << nrows <<
" cols: " << ncols <<
" .... " << std::endl);
576 for (
int j = 0;
j < nrows;
j++) {
580 for (
i1 =
j;
i1 < nrows;
i1++) {
581 for (
j1 =
j;
j1 < ncols;
j1++) {
598 for (
int i1 =
j + 1;
i1 < nrows;
i1++) {
599 if (
U.get(
i1,
j) == 1) {
620 for (
c = 0;
c < ncols;
c++) {
625 it_error(
"GF2mat::T_fact_update_bitflip() - internal error");
628 for (
int i = 0;
i < nrows;
i++) {
629 if (T.
get(
i, r) == 1) {
630 U.addto_element(
i,
c, 1);
637 for (
int j =
c;
j < ncols - 1;
j++) {
638 U.set_col(
j,
U.get_col(
j + 1));
645 if (nrows >= ncols) {
648 for (
int i =
c;
i < nrows - 1;
i++) {
649 U.set_row(
i,
U.get_row(
i + 1));
656 for (
int j =
c;
j < ncols;
j++) {
657 if (
U.get(nrows - 1,
j) == 1) {
658 U.add_rows(nrows - 1,
j);
665 for (
int j =
rank - 1;
j < nrows;
j++) {
667 for (
i1 =
j;
i1 < nrows;
i1++) {
668 for (
j1 =
j;
j1 < ncols;
j1++) {
669 if (
U.get(
i1,
j1) == 1) {
686 for (
int i1 =
j + 1;
i1 < nrows;
i1++) {
687 if (
U.get(
i1,
j) == 1) {
702 it_assert(
j0 > 0,
"GF2mat::T_fact_update_addcol(): dimension mismatch");
704 "GF2mat::T_fact_update_addcol(): dimension mismatch");
706 "GF2mat::T_fact_update_addcol(): dimension mismatch");
708 "GF2mat::T_fact_update_addcol(): dimension mismatch");
710 "GF2mat::T_fact_update_addcol(): dimension mismatch");
713 "GF2mat::T_fact_update_addcol(): factorization has incorrect rank");
750 it_assert(nrows == ncols,
"GF2mat::inverse(): Matrix must be square");
756 it_assert(
rank == ncols,
"GF2mat::inverse(): Matrix is not full rank");
759 for (
int i = ncols - 2;
i >= 0;
i--) {
760 for (
int j = ncols - 1;
j >
i;
j--) {
761 if (
U.get(
i,
j) == 1) {
781 for (
int i = 0;
i < nrows;
i++) {
782 for (
int j = 0;
j < nwords;
j++) {
783 if (data(
i,
j) != 0) {
793 if (
X.nrows != nrows) {
return false; }
794 if (
X.ncols != ncols) {
return false; }
795 it_assert(
X.nwords == nwords,
"GF2mat::operator==() dimension mismatch");
797 for (
int i = 0;
i < nrows;
i++) {
798 for (
int j = 0;
j < nwords;
j++) {
811 it_assert(
i >= 0 &&
i < nrows,
"GF2mat::add_rows(): out of range");
812 it_assert(
j >= 0 &&
j < nrows,
"GF2mat::add_rows(): out of range");
813 for (
int k = 0; k < nwords; k++) {
814 data(
i, k) ^= data(
j, k);
820 it_assert(
i >= 0 &&
i < nrows,
"GF2mat::swap_rows(): index out of range");
821 it_assert(
j >= 0 &&
j < nrows,
"GF2mat::swap_rows(): index out of range");
822 for (
int k = 0; k < nwords; k++) {
823 unsigned char temp = data(
i, k);
824 data(
i, k) = data(
j, k);
831 it_assert(
i >= 0 &&
i < ncols,
"GF2mat::swap_cols(): index out of range");
832 it_assert(
j >= 0 &&
j < ncols,
"GF2mat::swap_cols(): index out of range");
849 it_assert(
X.ncols ==
Y.nrows,
"GF2mat::operator*(): dimension mismatch");
850 it_assert(
X.nwords > 0,
"Gfmat::operator*(): dimension mismatch");
851 it_assert(
Y.nwords > 0,
"Gfmat::operator*(): dimension mismatch");
876 it_assert(
X.nwords > 0,
"Gfmat::operator*(): dimension mismatch");
905 it_assert(
X.ncols ==
Y.ncols,
"GF2mat::mult_trans(): dimension mismatch");
906 it_assert(
X.nwords > 0,
"GF2mat::mult_trans(): dimension mismatch");
907 it_assert(
Y.nwords > 0,
"GF2mat::mult_trans(): dimension mismatch");
908 it_assert(
X.nwords ==
Y.nwords,
"GF2mat::mult_trans(): dimension mismatch");
912 for (
int i = 0;
i <
X.nrows;
i++) {
913 for (
int j = 0;
j <
Y.nrows;
j++) {
917 for (
int k = 0; k <
X.nwords; k++) {
940 for (
int i = 0;
i < nrows;
i++) {
941 for (
int j = 0;
j < ncols;
j++) {
950 it_assert(
X.nrows ==
Y.nrows,
"GF2mat::operator+(): dimension mismatch");
951 it_assert(
X.ncols ==
Y.ncols,
"GF2mat::operator+(): dimension mismatch");
952 it_assert(
X.nwords ==
Y.nwords,
"GF2mat::operator+(): dimension mismatch");
955 for (
int i = 0;
i <
X.nrows;
i++) {
956 for (
int j = 0;
j <
X.nwords;
j++) {
967 "GF2mat::permute_cols(): dimensions do not match");
970 for (
int j = 0;
j < ncols;
j++) {
983 "GF2mat::permute_rows(): dimensions do not match");
986 for (
int i = 0;
i < nrows;
i++) {
988 for (
int j = 0;
j < nwords;
j++) {
993 for (
int j = 0;
j < nwords;
j++) {
1004 os <<
"---- GF(2) matrix of dimension " <<
X.nrows <<
"*" <<
X.ncols
1005 <<
" -- Density: " <<
X.density() <<
" ----" << std::endl;
1007 for (
i = 0;
i <
X.nrows;
i++) {
1009 for (
j = 0;
j <
X.ncols;
j++) {
1010 os <<
X.get(
i,
j) <<
" ";
1022 for (
int i = 0;
i < nrows;
i++) {
1023 for (
int j = 0;
j < ncols;
j++) {
1028 return ((
double)
no_of_ones) / (nrows*ncols);
1036 +
X.nrows *
X.nwords *
sizeof(
char);
1042 for (
int i = 0;
i <
X.nrows;
i++) {
1043 for (
int j = 0;
j <
X.nwords;
j++) {
1055 if (
h.type ==
"GF2mat") {
1058 X.nrows =
static_cast<int>(
tmp);
1060 X.ncols =
static_cast<int>(
tmp);
1062 X.nwords =
static_cast<int>(
tmp);
1063 X.
data.set_size(
X.nrows,
X.nwords);
1064 for (
int i = 0;
i <
X.nrows;
i++) {
1065 for (
int j = 0;
j <
X.nwords;
j++) {
1068 X.
data(
i,
j) =
static_cast<unsigned char>(r);
1073 it_error(
"it_ifile &operator>>() - internal error");
void set_length(int n, bool copy=false)
Resizing an Array<T>.
int size() const
Returns the number of data elements in the array object.
T * data
A pointer to the data area.
void set_size(int n, bool copy=false)
Resizing an Array<T>.
int length() const
Returns the number of data elements in the array object.
int max_num_m
Maximum weight of rows.
int max_num_n
Maximum weight of columns.
int M
Size of the matrix: M rows x N columns.
imat mlist
List of integer coordinates in the m direction with non-zero entries.
void read(const std::string &fname)
Read alist data from a file named fname.
void write(const std::string &fname) const
Write alist data to a file named fname.
imat nlist
List of integer coordinates in the n direction with non-zero entries.
int N
Size of the matrix: M rows x N columns.
ivec num_nlist
Weight of each column n.
void from_sparse(const GF2mat_sparse &mat, bool transpose=false)
Import "alist" representation from GF2mat_sparse.
GF2mat_sparse_alist()
Default constructor.
GF2mat_sparse to_sparse(bool transpose=false) const
Convert "alist" representation to GF2mat_sparse.
ivec num_mlist
Weight of each row m.
bool data_ok
Flag indicating that "alist" matrix data are properly set.
Class for dense GF(2) matrices.
bool is_zero() const
Check whether the matrix is identical to zero.
int cols() const
Get number of columns.
double density() const
Compute the matrix density (fraction of elements equal to "1")
int T_fact_update_bitflip(GF2mat &T, GF2mat &U, ivec &P, int rank, int r, int c) const
TXP factorization update, when bit is flipped.
bool operator==(const GF2mat &X) const
Check if equal.
GF2mat concatenate_horizontal(const GF2mat &X) const
Concatenate horizontally (append X on the right side of matrix)
GF2mat transpose() const
Transpose.
bvec get_row(int i) const
Get row.
void permute_rows(ivec &perm, bool I)
Multiply from left with permutation matrix (permute rows).
void operator=(const GF2mat &X)
Assignment operator.
void set_col(int j, bvec x)
Set column j to a binary vector x.
void add_rows(int i, int j)
Add (or equivalently, subtract) rows.
GF2mat concatenate_vertical(const GF2mat &X) const
Concatenate vertically (append X underneath)
int rows() const
Get number of rows.
bin get(int i, int j) const
Getting element.
GF2mat_sparse sparsify() const
Create a sparse GF(2) matrix from a dense GF(2) matrix.
void swap_cols(int i, int j)
Swap columns i and j.
bvec bvecify() const
Create a bvec from a GF(2) matrix (must have one column or one row)
GF2mat get_submatrix(int m1, int n1, int m2, int n2) const
Submatrix from (m1,n1) to (m2,n2)
ITPP_EXPORT GF2mat gf2dense_eye(int m)
GF(2) Identity matrix.
int T_fact(GF2mat &T, GF2mat &U, ivec &P) const
TXP factorization.
void set(int i, int j, bin s)
Set element i,j to s (0 or 1)
GF2mat inverse() const
Inversion.
void set_size(int m, int n, bool copy=false)
Set size of GF(2) matrix. If copy = true, keep data before resizing.
void set_row(int i, bvec x)
Set row i to a binary vector x.
GF2mat()
Default constructor (gives an empty 1 x 1 matrix)
bvec get_col(int j) const
Get column.
bool T_fact_update_addcol(GF2mat &T, GF2mat &U, ivec &P, bvec newcol) const
TXP factorization update, when column is added.
void swap_rows(int i, int j)
Swap rows i and j.
int row_rank() const
Returns the number of linearly independent rows.
void permute_cols(ivec &perm, bool I)
Multiply a matrix from right with a permutation matrix (i.e., permute the columns).
void set_size(int rows, int cols, bool copy=false)
Set size of matrix. If copy = true then keep the data before resizing.
void clear()
Set matrix equal to the all zero matrix.
void set(int r, int c, T v)
Set element (r, c ) equal to v.
Binary arithmetic (boolean) class.
The IT++ file format reading and writing class.
void write_data_header(const std::string &type, uint64_t size)
Write the data header for a variable, specifying the type and size of the data to follow.
void low_level_write(char x)
Write a char value at the current file pointer position.
The IT++ file format reading class.
void low_level_read(char &x)
Read a char value at the current file pointer position.
void read_data_header(it_file_base::data_header &h)
Read data header and return the result in the variable h.
Definitions of converters between different vector and matrix types.
Definition of a class for algebra on GF(2) (binary) matrices.
#define it_error(s)
Abort unconditionally.
#define it_info_debug(s)
Print information message if NDEBUG is not defined.
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
#define it_assert(t, s)
Abort if t is not true.
int rank(const Mat< T > &m, double tol=-1.0)
Calculate the rank of matrix m.
void transpose(const Mat< T > &m, Mat< T > &out)
Transposition of the matrix m returning the transposed matrix in out.
int length(const Vec< T > &v)
Length of vector.
T min(const Vec< T > &in)
Minimum value of vector.
T max(const Vec< T > &v)
Maximum value of vector.
ITPP_EXPORT ivec zeros_i(int size)
A Int vector of zeros.
Mat< bin > bmat
bin matrix
Various functions on vectors and matrices - header file.
GF2mat mult_trans(const GF2mat &X, const GF2mat &Y)
Multiplication X*Y' where X and Y are GF(2) matrices.
std::ostream & operator<<(std::ostream &output, const bin &inbin)
Output stream of bin.
const Array< T > concat(const Array< T > &a, const T &e)
Append element e to the end of the Array a.
GF2mat operator*(const GF2mat &X, const GF2mat &Y)
GF(2) matrix multiplication.
GF2mat gf2dense_eye(int m)
GF(2) Identity matrix.
std::istream & operator>>(std::istream &input, bin &outbin)
Input stream of bin.
GF2mat operator+(const GF2mat &X, const GF2mat &Y)
GF(2) matrix addition.
int floor_i(double x)
The nearest smaller integer.
Definitions of special vectors and matrices.