-
Notifications
You must be signed in to change notification settings - Fork 0
/
row_average.c
72 lines (53 loc) · 1.42 KB
/
row_average.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
/* row_average.c
Average each row of the channel matrix, and print it.
This code is experimental, and error-handling is primitive.
*/
/* Copyright 2013, NICTA. See COPYRIGHT for license details. */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "sparse.h"
int
main(int argc, char *argv[]) {
csc_mat_t *Q;
FILE *in;
int quiet;
csc_errno_t e;
double *row_avg;
int c, r;
if(argc < 2) {
fprintf(stderr, "Usage: %s <channel_matrix> [-q]\n", argv[0]);
return 1;
}
if(argc > 2 && !strcmp(argv[2], "-q")) quiet= 1;
if(!quiet) printf("Loading channel matrix...");
fflush(stdout);
in= fopen(argv[1], "rb");
if(!in) { perror("fopen"); return 1; }
Q= csc_load_binary(in, &e);
if(!Q) { csc_perror(e, "csc_load_binary"); return 1; }
fclose(in);
if(!quiet) printf(" done.\n");
row_avg= calloc(Q->nrow, sizeof(double));
if(!row_avg) {
perror("calloc");
return 1;
}
for(c= 0; c < Q->ncol; c++) {
int64_t i;
for(i= Q->ci[c]; i < Q->ci[c+1]; i++) {
r= Q->rows[i];
double p= Q->entries[i];
assert(0 <= r);
assert(r < Q->nrow);
row_avg[r]+= p * c;
}
}
for(r= 0; r < Q->nrow; r++)
printf("%d %.12e\n", r, row_avg[r]);
free(row_avg);
csc_mat_destroy(Q);
return 0;
}