Skip to content

Commit 0b4e580

Browse files
author
AnotherTest
committed
Fix stupidity
1 parent bd99742 commit 0b4e580

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This is a (still incomplete) implementation of Sparse N-Dimensional Arrays based
1717
- [X] Addition
1818
- [X] Subtraction
1919
- [X] Simple Multiplication
20+
- [X] map 1/x
2021
- [ ] Multiplication with nonzero fill values
2122
- [X] reduce one dimension by a given funcion
2223

@@ -25,6 +26,7 @@ This is a (still incomplete) implementation of Sparse N-Dimensional Arrays based
2526
- [ ] CCS compress
2627

2728
### Fancy Operations
29+
- [X] Extraction of dimensions
2830
- [ ] Reshape
2931
- [ ] Flatten
3032
- [ ] Partition

spndarray.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ double reduce_sum(double acc, double x, int count);
125125
double reduce_mean(double acc, double x, int count);
126126

127127
spndarray *spndarray_reduce(spndarray *m, const size_t dim, const reduction_function reduce_fn);
128-
spndarray *spndarray_extract_dimension(spndarray *m, const size_t dim);
128+
spndarray *spndarray_reduce_dimension(spndarray *m, const size_t dim, const size_t idx);
129129
// TODO compress, io, operations, prop, swap
130130

131131
/* spndio.c */

spndop.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ spndarray *spndarray_mul(const spndarray *xm, const spndarray *xn,
4343
if (d != (size_t)-1) // intended underflow don't kill me
4444
f = d;
4545
goto sizecheck;
46-
}
46+
} else goto nosizecheck;
4747
if (m->ndim != n->ndim - 1) {
4848
fprintf(stderr, "mul requires dimensions N and N-1, but got %zd and %zd\n",
4949
m->ndim, n->ndim);
@@ -59,6 +59,7 @@ sizecheck:;
5959
ms, ns);
6060
return NULL;
6161
}
62+
nosizecheck:;
6263
res = spndarray_alloc_nzmax(n->ndim, n->dimsizes, n->nzmax, SPNDARRAY_NTUPLE);
6364
size_t midx[m->ndim], nidx[n->ndim];
6465
// TODO reshape
@@ -101,17 +102,17 @@ spndarray *spndarray_mul_vec(const spndarray *xm, const spndarray *xn,
101102
n = t;
102103
}
103104
if (n->ndim != 1) {
104-
fprintf(stderr, "mul_vec requires a one-dimension vector, but got a %zd onr\n",
105+
fprintf(stderr, "mul_vec requires a one-dimension vector, but got a %zd one\n",
105106
n->ndim);
106107
return NULL;
107108
}
108109
res = spndarray_alloc_nzmax(m->ndim, m->dimsizes, m->nzmax, SPNDARRAY_NTUPLE);
109110
size_t midx[m->ndim];
110111
for (size_t i = 0; i < m->nz; i++) {
111-
size_t ni = &m->data[i] - m->data;
112112
for (size_t j = 0; j < m->ndim; j++)
113-
midx[j] = m->dims[j][ni];
114-
spndarray_set(res, m->data[i] * spndarray_get(n, &m->dims[d][ni]), midx);
113+
midx[j] = m->dims[j][i];
114+
double nv = spndarray_get(n, &m->dims[d][i]);
115+
spndarray_set(res, m->data[i] * nv, midx);
115116
}
116117
return res;
117118
}

spndreduce.c

+20-8
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,30 @@ spndarray *spndarray_reduce(spndarray *m, const size_t dim,
8282
}
8383

8484
/*
85-
* spndarray_extract_dimension()
85+
* spndarray_reduce_dimension()
8686
*
87-
* extract the values of a given dimension
87+
* reduces the values of a given dimension to only the given index in it
8888
*
8989
* Inputs
90-
* dim - the dimension to extract
90+
* dim - the dimension to reduce
91+
* idx - the selected index
9192
*/
92-
spndarray *spndarray_extract_dimension(spndarray *m, size_t dim) {
93-
spndarray *ex = spndarray_alloc_nzmax(1, &m->dimsizes[dim], m->dimsizes[dim] * 0.1, SPNDARRAY_NTUPLE);
94-
for (size_t i = 0; i < m->nz; i++) {
95-
double val = m->data[i];
96-
spndarray_set(ex, val, &i);
93+
spndarray *spndarray_reduce_dimension(spndarray *m, const size_t dim, const size_t idx) {
94+
size_t newdims[m->ndim-1];
95+
for (size_t t = 0; t < m->ndim-1; t++)
96+
newdims[t] = 1;
97+
spndarray *ex = spndarray_alloc_nzmax(m->ndim-1, newdims, m->nz * 0.3, SPNDARRAY_NTUPLE);
98+
for (size_t x = 0; x < m->nz; x++) {
99+
if (m->dims[dim][x] != idx)
100+
continue;
101+
double val = m->data[x];
102+
for (size_t i = 0, j = 0; i < m->ndim; i++, j++) {
103+
if (i != dim) {
104+
newdims[j] = m->dims[i][x];
105+
} else
106+
j--;
107+
}
108+
spndarray_set(ex, val, newdims);
97109
}
98110
return ex;
99111
}

0 commit comments

Comments
 (0)