Skip to content

Commit c3d71de

Browse files
committed
fix typo and shape in radnom module
1 parent 20f7259 commit c3d71de

File tree

5 files changed

+27
-26
lines changed

5 files changed

+27
-26
lines changed

code/ndarray.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,9 @@ ndarray_obj_t *ndarray_new_dense_ndarray(uint8_t ndim, size_t *shape, uint8_t dt
563563
ndarray_obj_t *ndarray_new_ndarray_from_tuple(mp_obj_tuple_t *_shape, uint8_t dtype) {
564564
// creates a dense array from a tuple
565565
// the function should work in the general n-dimensional case
566+
if(_shape->len > ULAB_MAX_DIMS) {
567+
mp_raise_ValueError(MP_ERROR_TEXT("maximum number of dimensions is " MP_STRINGIFY(ULAB_MAX_DIMS)));
568+
}
566569
size_t *shape = m_new0(size_t, ULAB_MAX_DIMS);
567570
for(size_t i = 0; i < _shape->len; i++) {
568571
shape[ULAB_MAX_DIMS - 1 - i] = mp_obj_get_int(_shape->items[_shape->len - 1 - i]);

code/numpy/random/random.c

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const mp_obj_type_t random_generator_type = {
5757
void random_generator_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
5858
(void)kind;
5959
random_generator_obj_t *self = MP_OBJ_TO_PTR(self_in);
60-
mp_printf(MP_PYTHON_PRINTER, "Gnerator() at 0x%p", self);
60+
mp_printf(MP_PYTHON_PRINTER, "Generator() at 0x%p", self);
6161
}
6262

6363
mp_obj_t random_generator_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
@@ -149,12 +149,9 @@ static mp_obj_t random_normal(size_t n_args, const mp_obj_t *pos_args, mp_map_t
149149
ndarray = ndarray_new_linear_array((size_t)mp_obj_get_int(size), NDARRAY_FLOAT);
150150
} else if(mp_obj_is_type(size, &mp_type_tuple)) {
151151
mp_obj_tuple_t *_shape = MP_OBJ_TO_PTR(size);
152-
if(_shape->len > ULAB_MAX_DIMS) {
153-
mp_raise_ValueError(MP_ERROR_TEXT("maximum number of dimensions is " MP_STRINGIFY(ULAB_MAX_DIMS)));
154-
}
155152
ndarray = ndarray_new_ndarray_from_tuple(_shape, NDARRAY_FLOAT);
156153
} else { // input type not supported
157-
mp_raise_TypeError(MP_ERROR_TEXT("shape must be None, and integer or a tuple of integers"));
154+
mp_raise_TypeError(MP_ERROR_TEXT("shape must be None, an integer or a tuple of integers"));
158155
}
159156
} else {
160157
// return single value
@@ -221,27 +218,16 @@ static mp_obj_t random_random(size_t n_args, const mp_obj_t *pos_args, mp_map_t
221218
mp_obj_t out = args[2].u_obj;
222219

223220
ndarray_obj_t *ndarray = NULL;
224-
size_t *shape = m_new(size_t, ULAB_MAX_DIMS);
225-
uint8_t ndim = 1;
221+
size_t *shape = m_new0(size_t, ULAB_MAX_DIMS);
226222

227223
if(size != mp_const_none) {
228224
if(mp_obj_is_int(size)) {
229225
shape[ULAB_MAX_DIMS - 1] = (size_t)mp_obj_get_int(size);
230226
} else if(mp_obj_is_type(size, &mp_type_tuple)) {
231227
mp_obj_tuple_t *_shape = MP_OBJ_TO_PTR(size);
232-
if(_shape->len > ULAB_MAX_DIMS) {
233-
mp_raise_ValueError(MP_ERROR_TEXT("maximum number of dimensions is " MP_STRINGIFY(ULAB_MAX_DIMS)));
234-
}
235-
ndim = _shape->len;
236-
for(size_t i = 0; i < ULAB_MAX_DIMS; i++) {
237-
if(i >= ndim) {
238-
shape[ULAB_MAX_DIMS - 1 - i] = 0;
239-
} else {
240-
shape[ULAB_MAX_DIMS - 1 - i] = mp_obj_get_int(_shape->items[i]);
241-
}
242-
}
228+
ndarray = ndarray_new_ndarray_from_tuple(_shape, NDARRAY_FLOAT);
243229
} else { // input type not supported
244-
mp_raise_TypeError(MP_ERROR_TEXT("shape must be None, and integer or a tuple of integers"));
230+
mp_raise_TypeError(MP_ERROR_TEXT("shape must be None, an integer or a tuple of integers"));
245231
}
246232
}
247233

@@ -267,7 +253,8 @@ static mp_obj_t random_random(size_t n_args, const mp_obj_t *pos_args, mp_map_t
267253
}
268254
} else { // out == None
269255
if(size != mp_const_none) {
270-
ndarray = ndarray_new_dense_ndarray(ndim, shape, NDARRAY_FLOAT);
256+
mp_obj_tuple_t *_shape = MP_OBJ_TO_PTR(size);
257+
ndarray = ndarray_new_ndarray_from_tuple(_shape, NDARRAY_FLOAT);
271258
} else {
272259
// return single value
273260
mp_float_t value;
@@ -336,13 +323,9 @@ static mp_obj_t random_uniform(size_t n_args, const mp_obj_t *pos_args, mp_map_t
336323
return mp_obj_new_float(value);
337324
} else if(mp_obj_is_type(size, &mp_type_tuple)) {
338325
mp_obj_tuple_t *_shape = MP_OBJ_TO_PTR(size);
339-
// TODO: this could be reduced, if the inspection was in the ndarray_new_ndarray_from_tuple function
340-
if(_shape->len > ULAB_MAX_DIMS) {
341-
mp_raise_ValueError(MP_ERROR_TEXT("maximum number of dimensions is " MP_STRINGIFY(ULAB_MAX_DIMS)));
342-
}
343326
ndarray = ndarray_new_ndarray_from_tuple(_shape, NDARRAY_FLOAT);
344327
} else { // input type not supported
345-
mp_raise_TypeError(MP_ERROR_TEXT("shape must be None, and integer or a tuple of integers"));
328+
mp_raise_TypeError(MP_ERROR_TEXT("shape must be None, an integer or a tuple of integers"));
346329
}
347330

348331
mp_float_t *array = (mp_float_t *)ndarray->array;

code/ulab.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include "user/user.h"
3434
#include "utils/utils.h"
3535

36-
#define ULAB_VERSION 6.7.3
36+
#define ULAB_VERSION 6.7.4
3737
#define xstr(s) str(s)
3838
#define str(s) #s
3939

docs/ulab-change-log.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Thu, 29 May 2025
2+
3+
version 6.7.5
4+
5+
fix typo and shape in radnom module
6+
17
Sun, 26 Jan 2025
28

39
version 6.7.3

tests/2d/numpy/random.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
try:
2+
from ulab import numpy as np
3+
except ImportError:
4+
import numpy as np
5+
6+
rng = np.random.Generator(1234)
7+
8+
random_array = rng.random((1, 2))
9+
print("random() shape:", random_array.shape)

0 commit comments

Comments
 (0)