Skip to content

Commit

Permalink
Merge pull request #299 from v923z/legacy-std-fix
Browse files Browse the repository at this point in the history
fixed the calculation of the standard deviation on iterables
  • Loading branch information
v923z authored Jan 29, 2021
2 parents a9bc6b7 + 483ce14 commit 743d864
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
10 changes: 2 additions & 8 deletions code/numerical/numerical.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,10 @@ static mp_obj_t numerical_sum_mean_std_iterable(mp_obj_t oin, uint8_t optype, si
size_t count = 0;
mp_obj_iter_buf_t iter_buf;
mp_obj_t item, iterable = mp_getiter(oin, &iter_buf);
if((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
value = mp_obj_get_float(item);
sum += value;
M = m = value;
count++;
}
while((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
value = mp_obj_get_float(item);
sum += value;
m = M + (value - M) / count;
m = M + (value - M) / (count + 1);
s = S + (value - M) * (value - m);
M = m;
S = s;
Expand All @@ -88,7 +82,7 @@ static mp_obj_t numerical_sum_mean_std_iterable(mp_obj_t oin, uint8_t optype, si
} else if(optype == NUMERICAL_MEAN) {
return count > 0 ? mp_obj_new_float(m) : mp_obj_new_float(MICROPY_FLOAT_CONST(0.0));
} else { // this should be the case of the standard deviation
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));
return count > ddof ? mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(s / (count - ddof))) : mp_obj_new_float(MICROPY_FLOAT_CONST(0.0));
}
}

Expand Down
2 changes: 1 addition & 1 deletion code/ulab.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "user/user.h"
#include "vector/vectorise.h"

#define ULAB_VERSION 1.7.1
#define ULAB_VERSION 1.7.2
#define xstr(s) str(s)
#define str(s) #s
#if ULAB_NUMPY_COMPATIBILITY
Expand Down
6 changes: 3 additions & 3 deletions code/ulab.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
// the ndarray binary operators
#define NDARRAY_HAS_BINARY_OPS (1)

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

#define NDARRAY_BINARY_USES_FUN_POINTER (0)
Expand Down Expand Up @@ -209,7 +209,7 @@
#ifndef ULAB_VECTORISE_MODULE
#define ULAB_VECTORISE_MODULE (1)

// Firmware size can be reduced at the expense of speed by using a function
// Firmware size can be reduced at the expense of speed by using a function
// pointer in iterations. Setting ULAB_VECTORISE_USES_FUNCPOINTER to 1 saves
// around 800 bytes in the four-dimensional case, and around 200 in two dimensions.
#define ULAB_VECTORISE_USES_FUN_POINTER (1)
Expand Down
6 changes: 6 additions & 0 deletions docs/ulab-change-log.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Fri, 29 Jan 2021

version 1.7.2

fixed code calculating the standard deviation of iterables

Fri, 15 Jan 2021

version 1.7.1
Expand Down

0 comments on commit 743d864

Please sign in to comment.