-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspndop.c
More file actions
295 lines (276 loc) · 7.77 KB
/
spndop.c
File metadata and controls
295 lines (276 loc) · 7.77 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
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
#include "spndarray.h"
#include <math.h>
#include <stdlib.h>
#include "avl.c"
__attribute__((always_inline)) static inline size_t
array_mul(const size_t len, const size_t *arr, const ssize_t skip) {
size_t prod = 1;
for (size_t i = 0; i < len; i++)
if (i != (size_t)skip)
prod *= arr[i];
return prod;
}
/*
* spndarray_mul()
*
* calculates the dimension-wise multiplication of m and n
* Inputs
* m - first sparse array of size N
* n - second sparse array of size N[-1]
* d - which dimension to multiply over (-1 to skip)
*
* Outputs
* m * n
*
* Notes
* Currently does not support fillvalues other than zero
*/
spndarray *spndarray_mul(const spndarray *xm, const spndarray *xn,
const size_t d) {
spndarray *res;
spndarray *m = (spndarray *)xm;
spndarray *n = (spndarray *)xn;
ssize_t f = -1;
if (n->ndim < m->ndim) {
void *t = m;
m = n;
n = t;
}
if (m->ndim == n->ndim) {
if (d != (size_t)-1) // intended underflow don't kill me
f = d;
goto sizecheck;
} else goto nosizecheck;
if (m->ndim != n->ndim - 1) {
fprintf(stderr, "mul requires dimensions N and N-1, but got %zd and %zd\n",
m->ndim, n->ndim);
return NULL;
}
sizecheck:;
size_t ms = array_mul(m->ndim, m->dimsizes, f);
size_t ns = array_mul(n->ndim, n->dimsizes, d);
if (ms != ns) {
fprintf(stderr,
"mul requires projected data count to be equal, but got %zd and %zd"
" values\n",
ms, ns);
return NULL;
}
nosizecheck:;
res = spndarray_alloc_nzmax(n->ndim, n->dimsizes, n->nzmax, SPNDARRAY_NTUPLE);
size_t midx[m->ndim], nidx[n->ndim];
// TODO reshape
for (size_t i = 0; i < n->nz; i++) {
size_t ni = &n->data[i] - n->data;
for (size_t j = 0; j < n->ndim; j++) {
nidx[j] = n->dims[j][ni];
if (j != d)
midx[j - (j > d)] = nidx[j];
}
spndarray_set(res, spndarray_get(m, midx) * spndarray_get(n, nidx), nidx);
}
return res;
}
/*
* spndarray_mul_vec()
*
* calculates the dimension-wise multiplication of array ND m and 1D array n
* Inputs
* m - first sparse array of size N
* n - second sparse array of size 1
* d - which dimension to multiply over
*
* Outputs
* m * n
*
* Notes
* Currently does not support fillvalues other than zero
*/
spndarray *spndarray_mul_vec(const spndarray *xm, const spndarray *xn,
const size_t d) {
spndarray *res;
spndarray *m = (spndarray *)xm;
spndarray *n = (spndarray *)xn;
if (m->ndim < n->ndim) {
void *t = m;
m = n;
n = t;
}
if (n->ndim != 1) {
fprintf(stderr, "mul_vec requires a one-dimension vector, but got a %zd one\n",
n->ndim);
return NULL;
}
res = spndarray_alloc_nzmax(m->ndim, m->dimsizes, m->nzmax, SPNDARRAY_NTUPLE);
size_t midx[m->ndim];
for (size_t i = 0; i < m->nz; i++) {
for (size_t j = 0; j < m->ndim; j++)
midx[j] = m->dims[j][i];
double nv = spndarray_get(n, &m->dims[d][i]);
spndarray_set(res, m->data[i] * nv, midx);
}
return res;
}
/*
* spndarray_add()
*
* calculates the dimension-wise addition of m and n
* Inputs
* m - first sparse array of size N
* n - second sparse array of size N[-1]
*
* Outputs
* m + n
*/
spndarray *spndarray_add(const spndarray *xm, const spndarray *xn) {
spndarray *res;
spndarray *m = (spndarray *)xm;
spndarray *n = (spndarray *)xn;
if (m->ndim != n->ndim) {
fprintf(stderr,
"add requires dimensions to be equal, but got %zd and %zd\n",
m->ndim, n->ndim);
return NULL;
}
size_t ms = array_mul(m->ndim, m->dimsizes, -1);
size_t ns = array_mul(n->ndim, n->dimsizes, -1);
if (ms != ns) {
fprintf(stderr,
"add requires projected data count to be equal, but got %zd and %zd"
" values\n",
ms, ns);
return NULL;
}
res = spndarray_alloc_nzmax(n->ndim, n->dimsizes, n->nzmax, SPNDARRAY_NTUPLE);
spndarray_set_fillvalue(res, n->fill + m->fill);
size_t midx[m->ndim], nidx[n->ndim];
// TODO reshape
for (size_t i = 0; i < n->nz; i++) {
size_t ni = &n->data[i] - n->data;
for (size_t j = 0; j < n->ndim; j++) {
nidx[j] = n->dims[j][ni];
midx[j] = nidx[j];
}
spndarray_set(res, spndarray_get(m, midx) + spndarray_get(n, nidx), nidx);
}
for (size_t i = 0; i < m->nz; i++) {
size_t mi = &m->data[i] - m->data;
for (size_t j = 0; j < m->ndim; j++) {
midx[j] = m->dims[j][mi];
nidx[j] = midx[j];
}
spndarray_set(res, spndarray_get(m, midx) + spndarray_get(n, nidx), nidx);
}
return res;
}
/*
* spndarray_sub()
*
* calculates the dimension-wise Subtraction of m and n
* Inputs
* m - first sparse array of size N
* n - second sparse array of size N[-1]
*
* Outputs
* m - n
*/
spndarray *spndarray_sub(const spndarray *xm, const spndarray *xn) {
spndarray *res;
spndarray *m = (spndarray *)xm;
spndarray *n = (spndarray *)xn;
if (m->ndim != n->ndim) {
fprintf(stderr,
"sub requires dimensions to be equal, but got %zd and %zd\n",
m->ndim, n->ndim);
return NULL;
}
size_t ms = array_mul(m->ndim, m->dimsizes, -1);
size_t ns = array_mul(n->ndim, n->dimsizes, -1);
if (ms != ns) {
fprintf(stderr,
"sub requires projected data count to be equal, but got %zd and %zd"
" values\n",
ms, ns);
return NULL;
}
res = spndarray_alloc_nzmax(n->ndim, n->dimsizes, n->nzmax, SPNDARRAY_NTUPLE);
spndarray_set_fillvalue(res, n->fill + m->fill);
size_t midx[m->ndim], nidx[n->ndim];
// TODO reshape
for (size_t i = 0; i < n->nz; i++) {
size_t ni = &n->data[i] - n->data;
for (size_t j = 0; j < n->ndim; j++) {
nidx[j] = n->dims[j][ni];
midx[j] = nidx[j];
}
spndarray_set(res, spndarray_get(m, midx) - spndarray_get(n, nidx), nidx);
}
for (size_t i = 0; i < m->nz; i++) {
size_t mi = &m->data[i] - m->data;
for (size_t j = 0; j < m->ndim; j++) {
midx[j] = m->dims[j][mi];
nidx[j] = midx[j];
}
spndarray_set(res, spndarray_get(m, midx) - spndarray_get(n, nidx), nidx);
}
return res;
}
/*
* spndarray_memcpy()
*
* copies an array into another, or allocates a new copy
*
* Inputs
* src - the array to copy from
* dst - the array to copy into, or NULL to allocate a new one
*
* Output
* the destination array
*
* Notes
* if a dst is provided, it must have the same dimensionality as the
* source array
*
* the fillvalues of the source array will _not_ be transferred over,
* they will be simply tranformed to the new fillvalue
* [doing so would cause this array to mostly dense]
*/
spndarray *spndarray_memcpy(const spndarray *src, spndarray *dst) {
if (!dst) {
dst = spndarray_alloc_nzmax(src->ndim, src->dimsizes, src->nzmax, SPNDARRAY_NTUPLE);
} else if (dst->ndim != src->ndim) {
fprintf(stderr,
"memcpy requires dimensionality to be equal between the source and "
"the destination array\n");
return NULL;
}
size_t idx[src->ndim];
spndarray_set_zero(dst);
for (size_t i = 0; i < src->nz; i++) {
for (size_t j = 0; j < src->ndim; j++)
idx[j] = src->dims[j][i];
spndarray_set(dst, src->data[i], idx);
}
return dst;
}
void spndarray_fmap(spndarray *m, double_mapper f) {
for (size_t i = 0; i < m->nz; i++)
m->data[i] = f(m->data[i]);
}
void spndarray_negate(spndarray *m) {
for (size_t i = 0; i < m->nz; i++)
m->data[i] = -m->data[i];
}
/*
* spndarray_mulinverse()
*
* applies 1/x for each value in the array, used for division
*/
void spndarray_mulinverse(spndarray *m) {
if (m->fill == 0.0 && m->nz != array_mul(m->ndim, m->dimsizes, -1)) {
fprintf(stderr, "multiplicativeInverse applied to zero will generate inf\n");
m->fill = 1.0 / 0.0;
}
for (size_t s = 0; s < m->nz; s++)
m->data[s] = 1/m->data[s];
}