Skip to content

Commit

Permalink
add random module (#654)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
v923z authored Jan 13, 2024
1 parent 7a93706 commit f2fad82
Show file tree
Hide file tree
Showing 20 changed files with 1,277 additions and 68 deletions.
1 change: 1 addition & 0 deletions code/micropython.mk
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ SRC_USERMOD += $(USERMODULES_DIR)/numpy/linalg/linalg.c
SRC_USERMOD += $(USERMODULES_DIR)/numpy/linalg/linalg_tools.c
SRC_USERMOD += $(USERMODULES_DIR)/numpy/numerical.c
SRC_USERMOD += $(USERMODULES_DIR)/numpy/poly.c
SRC_USERMOD += $(USERMODULES_DIR)/numpy/random/random.c
SRC_USERMOD += $(USERMODULES_DIR)/numpy/stats.c
SRC_USERMOD += $(USERMODULES_DIR)/numpy/transform.c
SRC_USERMOD += $(USERMODULES_DIR)/numpy/vector.c
Expand Down
6 changes: 3 additions & 3 deletions code/ndarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -559,11 +559,11 @@ ndarray_obj_t *ndarray_new_ndarray_from_tuple(mp_obj_tuple_t *_shape, uint8_t dt
// creates a dense array from a tuple
// the function should work in the general n-dimensional case
size_t *shape = m_new(size_t, ULAB_MAX_DIMS);
for(size_t i=0; i < ULAB_MAX_DIMS; i++) {
for(size_t i = 0; i < ULAB_MAX_DIMS; i++) {
if(i >= _shape->len) {
shape[ULAB_MAX_DIMS - i] = 0;
shape[ULAB_MAX_DIMS - 1 - i] = 0;
} else {
shape[ULAB_MAX_DIMS - i] = mp_obj_get_int(_shape->items[i]);
shape[ULAB_MAX_DIMS - 1 - i] = mp_obj_get_int(_shape->items[i]);
}
}
return ndarray_new_dense_ndarray(_shape->len, shape, dtype);
Expand Down
2 changes: 1 addition & 1 deletion code/ndarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
// Constant float objects are a struct in ROM and are referenced via their pointer.

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

Expand Down
4 changes: 4 additions & 0 deletions code/numpy/numpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "io/io.h"
#include "linalg/linalg.h"
#include "numerical.h"
#include "random/random.h"
#include "stats.h"
#include "transform.h"
#include "poly.h"
Expand Down Expand Up @@ -110,6 +111,9 @@ static const mp_rom_map_elem_t ulab_numpy_globals_table[] = {
#if ULAB_NUMPY_HAS_LINALG_MODULE
{ MP_ROM_QSTR(MP_QSTR_linalg), MP_ROM_PTR(&ulab_linalg_module) },
#endif
#if ULAB_NUMPY_HAS_RANDOM_MODULE
{ MP_ROM_QSTR(MP_QSTR_random), MP_ROM_PTR(&ulab_numpy_random_module) },
#endif
#if ULAB_HAS_PRINTOPTIONS
{ MP_ROM_QSTR(MP_QSTR_set_printoptions), MP_ROM_PTR(&ndarray_set_printoptions_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_printoptions), MP_ROM_PTR(&ndarray_get_printoptions_obj) },
Expand Down
Loading

0 comments on commit f2fad82

Please sign in to comment.