-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmat.c
165 lines (141 loc) · 3.67 KB
/
mat.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mat.h"
//take a look at the MSB-LSB format???
binmat_t mat_ini (int rown, int coln)
{
binmat_t A;
A = (binmat_t) malloc (sizeof (struct matrix));
A->coln = coln;
A->rown = rown;
A->rwdcnt = (1 + (coln - 1) / BITS_PER_LONG);
A->alloc_size = rown * A->rwdcnt * sizeof (unsigned long);
A->elem = (unsigned long *)malloc(A->alloc_size);
return A;
}
// assumes s has the proper allocated size
binmat_t mat_ini_from_string(int rown, int coln, const unsigned char * s)
{
binmat_t A;
A = (binmat_t) malloc (sizeof (struct matrix));
A->coln = coln;
A->rown = rown;
A->rwdcnt = (1 + (coln - 1) / BITS_PER_LONG);
A->alloc_size = rown * A->rwdcnt * sizeof (unsigned long);
A->elem = (unsigned long *) s;
fflush(stdout);
return A;
}
void mat_free(binmat_t A)
{
free(A->elem);
free(A);
}
binmat_t mat_copy(binmat_t A)//copying matrix (for the form [G|I].....)
{
binmat_t X;
int i;
X=mat_ini(A->rown,A->coln);//initialize the matrix.
for(i=0;i<((A->rwdcnt)*(A->rown));i++)
X->elem[i]=A->elem[i];
return(X);
}
binmat_t mat_rowxor(binmat_t A,int a, int b)
{
int i;
for(i=0;i<A->rwdcnt;i++)
{
A->elem[a*A->rwdcnt+i]^=A->elem[b*A->rwdcnt+i];
}
return A;
}
//the matrix is reduced from LSB...(from right)
int * mat_rref(binmat_t A)
{
printf("INSIDE - rref\n");
int i,j,failcnt,findrow,max=A->coln - 1;
int *perm;
fflush(stdout);
perm = malloc(A->alloc_size);
fflush(stdout);
for(i=0;i<A->coln;i++)
perm[i]=i;//initialize permutation.
failcnt = 0;
int ctr = 0;
for(i=0;i<A->rown;i++,max--)
{
findrow=0;
for(j=i;j<A->rown;j++)
{
if(mat_coeff(A,j,max))//(A->elem[(j*A->coln)+max])
{
//max--;
printf("checking %d row with %d row\n", i, j);
if (i!=j)//not needed as ith row is 0 and jth row is 1.
{
printf("xoring %d row with %d row\n", i, j);
A=mat_rowxor(A,i,j);//xor to the row.(swap)?
}
findrow=1;
break;
}//largest value found (end if)
// break;
}
if(!findrow)//if no row with a 1 found then swap last column and the column with no 1 down.
{
perm[A->coln - A->rown - 1 - failcnt] = max;
failcnt++;
if (!max)
{
return NULL;
}
i--;
}
else
{
perm[i+A->coln - A->rown] = max;
for(j=i+1;j<A->rown;j++)//fill the column downwards with 0's
{
if(mat_coeff(A,j,(max)))//(A->elem[j*A->coln+max+1])
A=mat_rowxor(A,j,i);//check the arg. order.
}
for(j=i-1;j>=0;j--)//fill the column with 0's upwards too.
{
if(mat_coeff(A,j,(max)))//(A->elem[j*A->coln+max+1])
A=mat_rowxor(A,j,i);
}
}
}//end for(i)
return(perm);
}
void mat_vec_mul(unsigned long *cR, unsigned char *x, binmat_t A)
{
int i,j;
unsigned long *pt;
memset(cR,0,A->rwdcnt*sizeof(long));
pt = A->elem;
for(i=0;i<A->rown;i++)//extract the first column in the form of char array.
{
if((x[i/8]>>(i%8))&1)
for (j = 0; j < A->rwdcnt; ++j)
cR[j] ^= *pt++;
else
pt += A->rwdcnt;
}
}
binmat_t mat_mul(binmat_t A, binmat_t B)
{
binmat_t C;
int i,j,k;
if (A->coln != B->rown)
exit(0);
C = mat_ini(A->rown, B->coln);
memset(C->elem,0,C->alloc_size);
for(i=0;i<A->rown;i++)
for(j=0;j<B->coln;j++)
for (k = 0; k < A->coln; ++k)
if (mat_coeff(A,i,k) && mat_coeff(B,k,j))
mat_change_coeff(C,i,j);
return C;
}