-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnum_utils.c
More file actions
128 lines (104 loc) · 2.9 KB
/
num_utils.c
File metadata and controls
128 lines (104 loc) · 2.9 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "num_utils.h"
unsigned int array_max(unsigned int *arr, unsigned int length) {
unsigned int i;
unsigned int max = arr[0];
for (i = 1; i < length; ++i) {
if (max < arr[i]) {
max = arr[i];
}
}
return max;
}
unsigned int log_2(unsigned int v) {
// https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
unsigned int r = 0; // r will be lg(v)
while (v >>= 1) // unroll for more speed...
{
r++;
}
return r;
}
unsigned int nbits(unsigned int i) {
if (i == 0) return 0;
return log_2(i) + 1;
}
unsigned int parity(unsigned int v) {
// https://graphics.stanford.edu/~seander/bithacks.html#ParityParallel
v ^= v >> 16;
v ^= v >> 8;
v ^= v >> 4;
v &= 0xf;
return (0x6996 >> v) & 1u;
}
unsigned int hamming_weight(unsigned int v) {
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
v = v - ((v >> 1) & 0x55555555); // reuse input as temporary
v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count
}
double correlation(unsigned int *x, unsigned int *y, unsigned int n) {
double sx = 0.0;
double sy = 0.0;
double sxx = 0.0;
double syy = 0.0;
double sxy = 0.0;
int i;
for(i = 0; i < n; ++i) {
double xi = x[i];
double yi = y[i];
sx += xi;
sy += yi;
sxx += xi * xi;
syy += yi * yi;
sxy += xi * yi;
}
// covariation
double cov = sxy / n - sx * sy / n / n;
if (cov == 0) return 0.0;
// standard error of x
double sigmax = sqrt(sxx / n - sx * sx / n / n);
// standard error of y
double sigmay = sqrt(syy / n - sy * sy / n / n);
// correlation is just a normalized covariation
return cov / sigmax / sigmay;
}
// matrix allocate and print
unsigned int **alloc_uint_matrix(unsigned int m, unsigned int n) {
unsigned int **mat;
unsigned int i;
mat = malloc(m * sizeof(unsigned int*));
for (i = 0; i < m; ++i) {
mat[i] = calloc(n, sizeof(unsigned int));
}
return mat;
}
void print_uint_matrix(unsigned int **mat, unsigned int m, unsigned int n) {
unsigned int i, j;
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
printf("%2d ", mat[i][j]);
}
printf("\n");
}
}
float **alloc_float_matrix(unsigned int m, unsigned int n) {
float **mat;
unsigned int i;
mat = malloc(m * sizeof(float*));
for (i = 0; i < m; ++i) {
mat[i] = calloc(n, sizeof(float));
}
return mat;
}
void print_float_matrix(float **mat, unsigned int m, unsigned int n) {
unsigned int i, j;
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
printf("%5.2f ", mat[i][j]);
}
printf("\n");
}
}