add ndim property (#725)

This commit is contained in:
Zoltán Vörös 2025-06-06 21:46:21 +02:00 committed by GitHub
parent 844f6e5e1e
commit 66e9eb3ed3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 27 additions and 3 deletions

View file

@ -1398,6 +1398,13 @@ mp_obj_t ndarray_itemsize(mp_obj_t self_in) {
}
#endif
#if NDARRAY_HAS_NDIM
mp_obj_t ndarray_ndim(mp_obj_t self_in) {
ndarray_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(self->ndim);
}
#endif
#if NDARRAY_HAS_SHAPE
mp_obj_t ndarray_shape(mp_obj_t self_in) {
ndarray_obj_t *self = MP_OBJ_TO_PTR(self_in);

View file

@ -232,6 +232,10 @@ mp_obj_t ndarray_dtype(mp_obj_t );
mp_obj_t ndarray_itemsize(mp_obj_t );
#endif
#if NDARRAY_HAS_NDIM
mp_obj_t ndarray_ndim(mp_obj_t );
#endif
#if NDARRAY_HAS_SIZE
mp_obj_t ndarray_size(mp_obj_t );
#endif

View file

@ -6,7 +6,7 @@
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Zoltán Vörös
* Copyright (c) 2021-2025 Zoltán Vörös
*
*/
@ -42,6 +42,11 @@ void ndarray_properties_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
dest[0] = ndarray_itemsize(self_in);
break;
#endif
#if NDARRAY_HAS_NDIM
case MP_QSTR_ndim:
dest[0] = ndarray_ndim(self_in);
break;
#endif
#if NDARRAY_HAS_SHAPE
case MP_QSTR_shape:
dest[0] = ndarray_shape(self_in);

View file

@ -7,7 +7,7 @@
* The MIT License (MIT)
*
* Copyright (c) 2020 Jeff Epler for Adafruit Industries
* 2020-2021 Zoltán Vörös
* 2020-2025 Zoltán Vörös
*/
#ifndef _NDARRAY_PROPERTIES_
@ -36,6 +36,10 @@ MP_DEFINE_CONST_FUN_OBJ_1(ndarray_flatiter_make_new_obj, ndarray_flatiter_make_n
MP_DEFINE_CONST_FUN_OBJ_1(ndarray_itemsize_obj, ndarray_itemsize);
#endif
#if NDARRAY_HAS_NDIM
MP_DEFINE_CONST_FUN_OBJ_1(ndarray_ndim_obj, ndarray_ndim);
#endif
#if NDARRAY_HAS_SHAPE
MP_DEFINE_CONST_FUN_OBJ_1(ndarray_shape_obj, ndarray_shape);
#endif

View file

@ -33,7 +33,7 @@
#include "user/user.h"
#include "utils/utils.h"
#define ULAB_VERSION 6.7.7
#define ULAB_VERSION 6.8.0
#define xstr(s) str(s)
#define str(s) #s

View file

@ -245,6 +245,10 @@
#define NDARRAY_HAS_ITEMSIZE (1)
#endif
#ifndef NDARRAY_HAS_NDIM
#define NDARRAY_HAS_NDIM (1)
#endif
#ifndef NDARRAY_HAS_RESHAPE
#define NDARRAY_HAS_RESHAPE (1)
#endif