Skip to content

Commit ba0e2a5

Browse files
committed
fixed the computation of the standard deviation on iterables
1 parent bd4b9ee commit ba0e2a5

File tree

3 files changed

+9
-12
lines changed

3 files changed

+9
-12
lines changed

code/numpy/numerical/numerical.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,10 @@ static mp_obj_t numerical_sum_mean_std_iterable(mp_obj_t oin, uint8_t optype, si
6767
size_t count = 0;
6868
mp_obj_iter_buf_t iter_buf;
6969
mp_obj_t item, iterable = mp_getiter(oin, &iter_buf);
70-
if((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
71-
value = mp_obj_get_float(item);
72-
sum += value;
73-
M = m = value;
74-
count++;
75-
}
7670
while((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
7771
value = mp_obj_get_float(item);
7872
sum += value;
79-
m = M + (value - M) / count;
73+
m = M + (value - M) / (count + 1);
8074
s = S + (value - M) * (value - m);
8175
M = m;
8276
S = s;
@@ -87,7 +81,7 @@ static mp_obj_t numerical_sum_mean_std_iterable(mp_obj_t oin, uint8_t optype, si
8781
} else if(optype == NUMERICAL_MEAN) {
8882
return count > 0 ? mp_obj_new_float(m) : mp_obj_new_float(MICROPY_FLOAT_CONST(0.0));
8983
} else { // this should be the case of the standard deviation
90-
return count > ddof ? mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(count * s / (count - ddof))) : mp_obj_new_float(MICROPY_FLOAT_CONST(0.0));
84+
return count > ddof ? mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(s / (count - ddof))) : mp_obj_new_float(MICROPY_FLOAT_CONST(0.0));
9185
}
9286
}
9387

code/ulab.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
#include "ndarray.h"
2525
#include "ndarray_properties.h"
2626

27-
#if CIRCUITPY
28-
#include "circuitpy/vector/vector.h"
29-
#else
3027
#include "numpy/numpy.h"
3128
#include "scipy/scipy.h"
3229
#include "numpy/fft/fft.h"
@@ -37,7 +34,7 @@
3734

3835
#include "user/user.h"
3936

40-
#define ULAB_VERSION 2.1.3
37+
#define ULAB_VERSION 2.1.5
4138
#define xstr(s) str(s)
4239
#define str(s) #s
4340
#define ULAB_VERSION_STRING xstr(ULAB_VERSION) xstr(-) xstr(ULAB_MAX_DIMS) xstr(D)

docs/ulab-change-log.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Fri, 29 Jan 2021
2+
3+
version 2.1.5
4+
5+
fixed error, when calculating standard deviation of iterables
6+
17
Thu, 21 Jan 2021
28

39
version 2.1.3

0 commit comments

Comments
 (0)