Skip to content

Commit ce763eb

Browse files
authored
Merge pull request #337 from v923z/dense-fix
fix ndarray_is_dense, eye, ones, full, and zeros for Boolean type
2 parents 34c7a45 + 54ff3f3 commit ce763eb

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

code/ndarray.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -588,14 +588,14 @@ void ndarray_assign_elements(ndarray_obj_t *ndarray, mp_obj_t iterable, uint8_t
588588
bool ndarray_is_dense(ndarray_obj_t *ndarray) {
589589
// returns true, if the array is dense, false otherwise
590590
// the array should be dense, if the very first stride can be calculated from shape
591-
// TODO: this function could probably be removed
592591
int32_t stride = ndarray->itemsize;
593-
for(uint8_t i=ULAB_MAX_DIMS; i > ULAB_MAX_DIMS-ndarray->ndim; i--) {
592+
for(uint8_t i = ULAB_MAX_DIMS - 1; i > ULAB_MAX_DIMS-ndarray->ndim; i--) {
594593
stride *= ndarray->shape[i];
595594
}
596-
return stride == ndarray->strides[ULAB_MAX_DIMS-ndarray->ndim-1] ? true : false;
595+
return stride == ndarray->strides[ULAB_MAX_DIMS-ndarray->ndim] ? true : false;
597596
}
598597

598+
599599
ndarray_obj_t *ndarray_new_ndarray(uint8_t ndim, size_t *shape, int32_t *strides, uint8_t dtype) {
600600
// Creates the base ndarray with shape, and initialises the values to straight 0s
601601
ndarray_obj_t *ndarray = m_new_obj(ndarray_obj_t);
@@ -1843,7 +1843,7 @@ mp_obj_t ndarray_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
18431843
#if NDARRAY_HAS_UNARY_OP_ABS
18441844
case MP_UNARY_OP_ABS:
18451845
ndarray = ndarray_copy_view(self);
1846-
// if Booleam, NDARRAY_UINT8, or NDARRAY_UINT16, there is nothing to do
1846+
// if Boolean, NDARRAY_UINT8, or NDARRAY_UINT16, there is nothing to do
18471847
if(self->dtype == NDARRAY_INT8) {
18481848
int8_t *array = (int8_t *)ndarray->array;
18491849
for(size_t i=0; i < self->len; i++, array++) {

code/ulab.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
#include "user/user.h"
3535

36-
#define ULAB_VERSION 2.4.1
36+
#define ULAB_VERSION 2.4.2
3737
#define xstr(s) str(s)
3838
#define str(s) #s
3939
#define ULAB_VERSION_STRING xstr(ULAB_VERSION) xstr(-) xstr(ULAB_MAX_DIMS) xstr(D)

code/ulab_create.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ static mp_obj_t create_zeros_ones_full(mp_obj_t oshape, uint8_t dtype, mp_obj_t
4646
ndarray = ndarray_new_dense_ndarray(len, shape, dtype);
4747
}
4848
if(value != mp_const_none) {
49+
if(dtype == NDARRAY_BOOL) {
50+
dtype = NDARRAY_UINT8;
51+
if(mp_obj_is_true(value)) {
52+
value = mp_obj_new_int(1);
53+
} else {
54+
value = mp_obj_new_int(0);
55+
}
56+
}
4957
for(size_t i=0; i < ndarray->len; i++) {
5058
mp_binary_set_val_array(dtype, ndarray->array, i, value);
5159
}
@@ -373,6 +381,9 @@ mp_obj_t create_eye(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args)
373381
m = mp_obj_get_int(args[1].u_rom_obj);
374382
}
375383
ndarray_obj_t *ndarray = ndarray_new_dense_ndarray(2, ndarray_shape_vector(0, 0, n, m), dtype);
384+
if(dtype == NDARRAY_BOOL) {
385+
dtype = NDARRAY_UINT8;
386+
}
376387
mp_obj_t one = mp_obj_new_int(1);
377388
size_t i = 0;
378389
if((args[2].u_int >= 0)) {

docs/ulab-change-log.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Sun, 21 Feb 2021
2+
3+
version 2.4.2
4+
5+
fix ndarray_is_dense, eye, ones, full, and zeros for Boolean type
6+
17
Sat, 13 Feb 2021
28

39
version 2.4.1

0 commit comments

Comments
 (0)