-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprsmtrx.c
323 lines (253 loc) · 8.48 KB
/
sprsmtrx.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
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
#include "sprsmtrx.h"
int int_entry(struct int_sparse_matrix* mymat, int row_no, int col_no) {
int probe;
probe = 1;
while (probe <= mymat->nos_active_cols[row_no-1] &&
mymat->index_of_active_cols[row_no-1][probe-1] != col_no) {
probe++;
}
if (probe > mymat->nos_active_cols[row_no-1]) {
return 0;
}
else {
return mymat->entries[row_no-1][probe-1];
}
}
void set_int_entry(struct int_sparse_matrix* mymat, int row_no, int col_no, int val) {
int probe;
probe = 1;
while (probe <= mymat->nos_active_cols[row_no-1] &&
mymat->index_of_active_cols[row_no-1][probe-1] != col_no) {
probe++;
}
if (probe <= mymat->nos_active_cols[row_no-1]) {
mymat->entries[row_no-1][probe-1] = val;
}
}
void increment_int_entry(struct int_sparse_matrix* mymat, int row_no, int col_no, int incr) {
int probe;
probe = 1;
while (probe <= mymat->nos_active_cols[row_no-1] &&
mymat->index_of_active_cols[row_no-1][probe-1] != col_no) {
probe++;
}
if (probe > mymat->nos_active_cols[row_no-1] && incr != 0) {
fprintf(stderr, "We are incrementing a zero in a int sparse matrix.\n");
exit(0);
}
mymat->entries[row_no-1][probe-1] += incr;
}
double dbl_entry(struct dbl_sparse_matrix* mymat, int row_no, int col_no) {
int probe;
probe = 1;
while (probe <= mymat->nos_active_cols[row_no-1] &&
mymat->index_of_active_cols[row_no-1][probe-1] != col_no) {
probe++;
}
if (probe > mymat->nos_active_cols[row_no-1]) {
return 0.0;
}
else {
return mymat->entries[row_no-1][probe-1];
}
}
void set_dbl_entry(struct dbl_sparse_matrix* mymat, int row_no, int col_no, double val) {
int probe;
probe = 1;
while (probe <= mymat->nos_active_cols[row_no-1] &&
mymat->index_of_active_cols[row_no-1][probe-1] != col_no) {
probe++;
}
if (probe <= mymat->nos_active_cols[row_no-1]) {
mymat->entries[row_no-1][probe-1] = val;
}
}
void increment_dbl_entry(struct dbl_sparse_matrix* mymat, int row_no, int col_no, double incr) {
int probe;
probe = 1;
while (probe <= mymat->nos_active_cols[row_no-1] &&
mymat->index_of_active_cols[row_no-1][probe-1] != col_no) {
probe++;
}
if (probe <= mymat->nos_active_cols[row_no-1]) {
mymat->entries[row_no-1][probe-1] += incr;
}
}
struct int_sparse_matrix copy_of_int_sp_matrix(struct int_sparse_matrix* given) {
int i, k, no_rows;
struct int_sparse_matrix answer;
answer.no_rows = given->no_rows;
answer.no_cols = given->no_cols;
no_rows = answer.no_rows;
answer.nos_active_cols = malloc(no_rows * sizeof(int));
for (i = 1; i <= no_rows; i++) {
answer.nos_active_cols[i-1] = given->nos_active_cols[i-1];
}
answer.index_of_active_cols = malloc(no_rows * sizeof(int*));
for (i = 1; i <= no_rows; i++) {
answer.index_of_active_cols[i-1] = malloc(answer.nos_active_cols[i-1] * sizeof(int));
for (k = 1; k <= answer.nos_active_cols[i-1]; k++) {
answer.index_of_active_cols[i-1][k-1] = given->index_of_active_cols[i-1][k-1];
}
}
answer.entries = malloc(no_rows * sizeof(int*));
for (i = 1; i <= no_rows; i++) {
answer.entries[i-1] = malloc(answer.nos_active_cols[i-1] * sizeof(int));
for (k = 1; k <= answer.nos_active_cols[i-1]; k++) {
answer.entries[i-1][k-1] = 0;
}
}
return answer;
}
struct dbl_sparse_matrix copy_of_dbl_sp_mat(struct dbl_sparse_matrix* given) {
int i, j, no_rows;
struct dbl_sparse_matrix copy;
copy.no_rows = given->no_rows;
copy.no_cols = given->no_cols;
no_rows = copy.no_rows;
copy.nos_active_cols = malloc(no_rows * sizeof(int));
for (i = 1; i <= no_rows; i++) {
copy.nos_active_cols[i-1] = given->nos_active_cols[i-1];
}
copy.index_of_active_cols = malloc(no_rows * sizeof(int*));
for (i = 1; i <= no_rows; i++) {
copy.index_of_active_cols[i-1] = malloc(copy.nos_active_cols[i-1] * sizeof(int));
for (j = 1; j <= copy.nos_active_cols[i-1]; j++) {
copy.index_of_active_cols[i-1][j-1] = given->index_of_active_cols[i-1][j-1];
}
}
copy.entries = malloc(no_rows * sizeof(double*));
for (i = 1; i <= no_rows; i++) {
copy.entries[i-1] = malloc(copy.nos_active_cols[i-1] * sizeof(double));
for (j = 1; j <= copy.nos_active_cols[i-1]; j++) {
copy.entries[i-1][j-1] = given->entries[i-1][j-1];
}
}
return copy;
}
int* new_nos_active_cols(struct dbl_sparse_matrix* mydsp,
struct subset* J_subset, struct subset* P_subset) {
int i, k, new_row_no;
int* new_nos;
new_nos = malloc(J_subset->subset_size * sizeof(int));
new_row_no = 0;
for (i = 1; i <= mydsp->no_rows; i++) {
if (J_subset->indicator[i-1] == 1) {
new_row_no++;
new_nos[new_row_no-1] = 0;
for (k = 1; k <= mydsp->nos_active_cols[i-1]; k++) {
if (P_subset->indicator[mydsp->index_of_active_cols[i-1][k-1]-1] == 1) {
new_nos[new_row_no-1]++;
}
}
}
}
return new_nos;
}
int** new_index_of_active_cols(struct dbl_sparse_matrix* mydsp,
struct subset* J_subset, struct subset* P_subset) {
int i, j, k, new_elt_no, new_no_cols, new_row_no, new_col_no;
int* elt_no_key;
int** new_index;
elt_no_key = malloc(P_subset->large_set_size * sizeof(int));
new_elt_no = 0;
for (j = 1; j <= P_subset->large_set_size; j++) {
if (P_subset->indicator[j-1] == 1) {
new_elt_no++;
elt_no_key[j-1] = new_elt_no;
}
}
new_index = malloc(J_subset->subset_size * sizeof(int*));
new_row_no = 0;
for (i = 1; i <= mydsp->no_rows; i++) {
if (J_subset->indicator[i-1] == 1) {
new_row_no++;
new_no_cols = 0;
new_index[new_row_no-1] = 0;
for (k = 1; k <= mydsp->nos_active_cols[i-1]; k++) {
if (P_subset->indicator[mydsp->index_of_active_cols[i-1][k-1]-1] == 1) {
new_no_cols++;
}
}
new_index[new_row_no-1] = malloc(new_no_cols * sizeof(int));
new_col_no = 0;
for (k = 1; k <= mydsp->nos_active_cols[i-1]; k++) {
if (P_subset->indicator[mydsp->index_of_active_cols[i-1][k-1]-1] == 1) {
new_col_no++;
new_index[new_row_no-1][new_col_no-1] = elt_no_key[mydsp->index_of_active_cols[i-1][k-1]-1];
}
}
}
}
free(elt_no_key);
return new_index;
}
struct dbl_sparse_matrix zero_dbl_sp_mat_for_subsets(struct dbl_sparse_matrix* mydsp,
struct subset* J_subset, struct subset* P_subset) {
int i, j;
struct dbl_sparse_matrix answer;
answer.no_rows = J_subset->subset_size;
answer.no_cols = P_subset->subset_size;
answer.nos_active_cols = new_nos_active_cols(mydsp, J_subset, P_subset);
answer.index_of_active_cols = new_index_of_active_cols(mydsp, J_subset, P_subset);
answer.entries = malloc(answer.no_rows * sizeof(double*));
for (i = 1; i <= answer.no_rows; i++) {
answer.entries[i-1] = malloc(answer.nos_active_cols[i-1] * sizeof(double));
for (j = 1; j <= answer.nos_active_cols[i-1]; j++) {
answer.entries[i-1][j-1] = 0.0;
}
}
return answer;
}
struct int_sparse_matrix zero_int_sp_mat_from_dbl_sp_mat(struct dbl_sparse_matrix* given) {
int i, j, no_rows;
struct int_sparse_matrix answer;
answer.no_rows = given->no_rows;
answer.no_cols = given->no_cols;
no_rows = answer.no_rows;
answer.nos_active_cols = malloc(no_rows * sizeof(int));
for (i = 1; i <= no_rows; i++) {
answer.nos_active_cols[i-1] = given->nos_active_cols[i-1];
}
answer.index_of_active_cols = malloc(no_rows * sizeof(int*));
for (i = 1; i <= no_rows; i++) {
answer.index_of_active_cols[i-1] = malloc(answer.nos_active_cols[i-1] * sizeof(int));
for (j = 1; j <= answer.nos_active_cols[i-1]; j++) {
answer.index_of_active_cols[i-1][j-1] = given->index_of_active_cols[i-1][j-1];
}
}
answer.entries = malloc(no_rows * sizeof(int*));
for (i = 1; i <= no_rows; i++) {
answer.entries[i-1] = malloc(answer.nos_active_cols[i-1] * sizeof(int));
for (j = 1; j <= answer.nos_active_cols[i-1]; j++) {
answer.entries[i-1][j-1] = 0;
}
}
return answer;
}
void destroy_int_sp_mat(struct int_sparse_matrix* mymat) {
int i, nst;
nst = mymat->no_rows;
free(mymat->nos_active_cols);
for (i = 1; i <= nst; i++) {
free(mymat->index_of_active_cols[i-1]);
}
free(mymat->index_of_active_cols);
for (i = 1; i <= nst; i++) {
free(mymat->entries[i-1]);
}
free(mymat->entries);
}
void destroy_dbl_sp_mat(struct dbl_sparse_matrix* mymat) {
int i, nst;
nst = mymat->no_rows;
free(mymat->nos_active_cols);
for (i = 1; i <= nst; i++) {
free(mymat->index_of_active_cols[i-1]);
}
free(mymat->index_of_active_cols);
for (i = 1; i <= nst; i++) {
free(mymat->entries[i-1]);
}
free(mymat->entries);
}