Skip to content

Commit 743d864

Browse files
authored
Merge pull request #299 from v923z/legacy-std-fix
fixed the calculation of the standard deviation on iterables
2 parents a9bc6b7 + 483ce14 commit 743d864

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

code/numerical/numerical.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,10 @@ static mp_obj_t numerical_sum_mean_std_iterable(mp_obj_t oin, uint8_t optype, si
6868
size_t count = 0;
6969
mp_obj_iter_buf_t iter_buf;
7070
mp_obj_t item, iterable = mp_getiter(oin, &iter_buf);
71-
if((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
72-
value = mp_obj_get_float(item);
73-
sum += value;
74-
M = m = value;
75-
count++;
76-
}
7771
while((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
7872
value = mp_obj_get_float(item);
7973
sum += value;
80-
m = M + (value - M) / count;
74+
m = M + (value - M) / (count + 1);
8175
s = S + (value - M) * (value - m);
8276
M = m;
8377
S = s;
@@ -88,7 +82,7 @@ static mp_obj_t numerical_sum_mean_std_iterable(mp_obj_t oin, uint8_t optype, si
8882
} else if(optype == NUMERICAL_MEAN) {
8983
return count > 0 ? mp_obj_new_float(m) : mp_obj_new_float(MICROPY_FLOAT_CONST(0.0));
9084
} else { // this should be the case of the standard deviation
91-
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));
85+
return count > ddof ? mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(s / (count - ddof))) : mp_obj_new_float(MICROPY_FLOAT_CONST(0.0));
9286
}
9387
}
9488

code/ulab.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "user/user.h"
3333
#include "vector/vectorise.h"
3434

35-
#define ULAB_VERSION 1.7.1
35+
#define ULAB_VERSION 1.7.2
3636
#define xstr(s) str(s)
3737
#define str(s) #s
3838
#if ULAB_NUMPY_COMPATIBILITY

code/ulab.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@
7474
// the ndarray binary operators
7575
#define NDARRAY_HAS_BINARY_OPS (1)
7676

77-
// Firmware size can be reduced at the expense of speed by using function
78-
// pointers in iterations. For each operator, he function pointer saves around
77+
// Firmware size can be reduced at the expense of speed by using function
78+
// pointers in iterations. For each operator, he function pointer saves around
7979
// 2 kB in the two-dimensional case, and around 4 kB in the four-dimensional case.
8080

8181
#define NDARRAY_BINARY_USES_FUN_POINTER (0)
@@ -209,7 +209,7 @@
209209
#ifndef ULAB_VECTORISE_MODULE
210210
#define ULAB_VECTORISE_MODULE (1)
211211

212-
// Firmware size can be reduced at the expense of speed by using a function
212+
// Firmware size can be reduced at the expense of speed by using a function
213213
// pointer in iterations. Setting ULAB_VECTORISE_USES_FUNCPOINTER to 1 saves
214214
// around 800 bytes in the four-dimensional case, and around 200 in two dimensions.
215215
#define ULAB_VECTORISE_USES_FUN_POINTER (1)

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 1.7.2
4+
5+
fixed code calculating the standard deviation of iterables
6+
17
Fri, 15 Jan 2021
28

39
version 1.7.1

0 commit comments

Comments
 (0)