Skip to content

Commit f2fad82

Browse files
authored
add random module (#654)
* add random module skeleton * add Generator object * add placeholder for random.random method * add rudimentary random.random implementation * generator object accept seed(s) argument * add out keyword * add support for out keyword argument * update change log * add links to header files * fix file link * fix error messages * add uniform to random module * add normal distribution * fix argument options in normal and uniform * update documentation
1 parent 7a93706 commit f2fad82

20 files changed

+1277
-68
lines changed

code/micropython.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ SRC_USERMOD += $(USERMODULES_DIR)/numpy/linalg/linalg.c
2525
SRC_USERMOD += $(USERMODULES_DIR)/numpy/linalg/linalg_tools.c
2626
SRC_USERMOD += $(USERMODULES_DIR)/numpy/numerical.c
2727
SRC_USERMOD += $(USERMODULES_DIR)/numpy/poly.c
28+
SRC_USERMOD += $(USERMODULES_DIR)/numpy/random/random.c
2829
SRC_USERMOD += $(USERMODULES_DIR)/numpy/stats.c
2930
SRC_USERMOD += $(USERMODULES_DIR)/numpy/transform.c
3031
SRC_USERMOD += $(USERMODULES_DIR)/numpy/vector.c

code/ndarray.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -559,11 +559,11 @@ ndarray_obj_t *ndarray_new_ndarray_from_tuple(mp_obj_tuple_t *_shape, uint8_t dt
559559
// creates a dense array from a tuple
560560
// the function should work in the general n-dimensional case
561561
size_t *shape = m_new(size_t, ULAB_MAX_DIMS);
562-
for(size_t i=0; i < ULAB_MAX_DIMS; i++) {
562+
for(size_t i = 0; i < ULAB_MAX_DIMS; i++) {
563563
if(i >= _shape->len) {
564-
shape[ULAB_MAX_DIMS - i] = 0;
564+
shape[ULAB_MAX_DIMS - 1 - i] = 0;
565565
} else {
566-
shape[ULAB_MAX_DIMS - i] = mp_obj_get_int(_shape->items[i]);
566+
shape[ULAB_MAX_DIMS - 1 - i] = mp_obj_get_int(_shape->items[i]);
567567
}
568568
}
569569
return ndarray_new_dense_ndarray(_shape->len, shape, dtype);

code/ndarray.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
// Constant float objects are a struct in ROM and are referenced via their pointer.
4141

4242
// Use ULAB_DEFINE_FLOAT_CONST to define a constant float object.
43-
// id is the name of the constant, num is it's floating point value.
43+
// id is the name of the constant, num is its floating point value.
4444
// hex32 is computed as: hex(int.from_bytes(array.array('f', [num]), 'little'))
4545
// hex64 is computed as: hex(int.from_bytes(array.array('d', [num]), 'little'))
4646

code/numpy/numpy.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "io/io.h"
2828
#include "linalg/linalg.h"
2929
#include "numerical.h"
30+
#include "random/random.h"
3031
#include "stats.h"
3132
#include "transform.h"
3233
#include "poly.h"
@@ -110,6 +111,9 @@ static const mp_rom_map_elem_t ulab_numpy_globals_table[] = {
110111
#if ULAB_NUMPY_HAS_LINALG_MODULE
111112
{ MP_ROM_QSTR(MP_QSTR_linalg), MP_ROM_PTR(&ulab_linalg_module) },
112113
#endif
114+
#if ULAB_NUMPY_HAS_RANDOM_MODULE
115+
{ MP_ROM_QSTR(MP_QSTR_random), MP_ROM_PTR(&ulab_numpy_random_module) },
116+
#endif
113117
#if ULAB_HAS_PRINTOPTIONS
114118
{ MP_ROM_QSTR(MP_QSTR_set_printoptions), MP_ROM_PTR(&ndarray_set_printoptions_obj) },
115119
{ MP_ROM_QSTR(MP_QSTR_get_printoptions), MP_ROM_PTR(&ndarray_get_printoptions_obj) },

0 commit comments

Comments
 (0)