-
Notifications
You must be signed in to change notification settings - Fork 0
/
genMatrix2.cpp
100 lines (86 loc) · 1.67 KB
/
genMatrix2.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define NDEBUG
#define BOOST_UBLAS_NDEBUG
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/matrix.hpp>
using namespace boost::numeric::ublas ;
int main(int argc, char **argv)
{
srand(0);
int i,j,q;
int x =400;
int y =100;
int ones=5;
if (argc>1) x = atoi(argv[1]);
if (argc>2) y = atoi(argv[2]);
if (argc>3) ones = atoi(argv[3]);
if (argc==1) {
printf("Usage: %s <width> <height> <ones per row>\n",argv[0]);
exit(-1);
}
matrix<int> G(x,y); //generator
matrix<int> R;
// Constructing H = (At|I(x-y))
matrix<int> H(y+(x-y),x-y);
// I(x-y)
for (i=y;i<y+(x-y);i++)
H(i,i-y)=1;
// At
matrix<int> Gt(y,x-y);
for (j=0;j<(x-y);j++)
for(q=0;q<ones-1;q++) {
int index=rand() % y;
Gt(index,j)=1;
}
for (i=0;i<y;i++)
for (j=0;j<(x-y);j++)
H(i,j)=Gt(i,j);
int Hx = H.size1();
int Hy = H.size2();
int chk;
for(j=0;j<Hy-(x-y);j++) {
chk=0;
for(i=0;i<Hy;i++)
chk+=H(j,i);
if (chk%2)
H(j,rand() % y)=1;
if (chk==0)
H(j,rand() % y)=1;
}
/*
for(i=0;i<Hy;i++) {
chk=0;
for(j=0;j<Hy-(x-y);j++)
chk+=H(j,i);
if (chk%2)
H(rand() % Hx,i)=1;
if (chk==0)
H(rand() % Hx,i)=1;
}
*/
int a,b,c;
for(j=0;j<Hy;j++)
for (i=0;i<Hx;i++)
if (H(i,j))
for (a=i;a<Hx;a++)
if (H(a,j))
for (b=j;b<Hy;b++)
if (H(a,b))
for (c=a;c>0;c--)
if (H(c,b))
H(c,b)=0;
std::cout<<trans(H)<<std::endl;
// Constructing G = (Iy|A)
// Iy
for(i=0;i<y;i++)
G(i,i)=1;
matrix<int> Gp = trans(Gt);
for(i=0;i<y;i++)
for (j=0;j<(x-y);j++)
G(j+y,i)=Gp(j,i);
std::cout<<trans(G)<<std::endl;
// Check, R must be all 0
R=prod(trans(G),H);
}