mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-21 14:50:44 +01:00
[python/ulab] Updated ulab
This commit is contained in:
66
python/port/mod/ulab/numpy/ndarray/ndarray_iter.c
Normal file
66
python/port/mod/ulab/numpy/ndarray/ndarray_iter.c
Normal file
@@ -0,0 +1,66 @@
|
||||
|
||||
/*
|
||||
* This file is part of the micropython-ulab project,
|
||||
*
|
||||
* https://github.com/v923z/micropython-ulab
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2021 Zoltán Vörös
|
||||
*
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "py/obj.h"
|
||||
#include "py/runtime.h"
|
||||
|
||||
#include "ndarray_iter.h"
|
||||
|
||||
#ifdef NDARRAY_HAS_FLATITER
|
||||
mp_obj_t ndarray_flatiter_make_new(mp_obj_t self_in) {
|
||||
ndarray_flatiter_t *flatiter = m_new_obj(ndarray_flatiter_t);
|
||||
flatiter->base.type = &ndarray_flatiter_type;
|
||||
flatiter->iternext = ndarray_flatiter_next;
|
||||
flatiter->ndarray = MP_OBJ_TO_PTR(self_in);
|
||||
flatiter->cur = 0;
|
||||
return flatiter;
|
||||
}
|
||||
|
||||
mp_obj_t ndarray_flatiter_next(mp_obj_t self_in) {
|
||||
ndarray_flatiter_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
ndarray_obj_t *ndarray = MP_OBJ_TO_PTR(self->ndarray);
|
||||
uint8_t *array = (uint8_t *)ndarray->array;
|
||||
|
||||
if(self->cur < ndarray->len) {
|
||||
uint32_t remainder = self->cur;
|
||||
uint8_t i = ULAB_MAX_DIMS - 1;
|
||||
do {
|
||||
size_t div = (remainder / ndarray->shape[i]);
|
||||
array += remainder * ndarray->strides[i];
|
||||
remainder -= div * ndarray->shape[i];
|
||||
i--;
|
||||
} while(i > ULAB_MAX_DIMS - ndarray->ndim);
|
||||
self->cur++;
|
||||
return ndarray_get_item(ndarray, array);
|
||||
}
|
||||
return MP_OBJ_STOP_ITERATION;
|
||||
}
|
||||
|
||||
mp_obj_t ndarray_new_flatiterator(mp_obj_t flatiter_in, mp_obj_iter_buf_t *iter_buf) {
|
||||
assert(sizeof(ndarray_flatiter_t) <= sizeof(mp_obj_iter_buf_t));
|
||||
ndarray_flatiter_t *iter = (ndarray_flatiter_t *)iter_buf;
|
||||
ndarray_flatiter_t *flatiter = MP_OBJ_TO_PTR(flatiter_in);
|
||||
iter->base.type = &mp_type_polymorph_iter;
|
||||
iter->iternext = ndarray_flatiter_next;
|
||||
iter->ndarray = flatiter->ndarray;
|
||||
iter->cur = 0;
|
||||
return MP_OBJ_FROM_PTR(iter);
|
||||
}
|
||||
|
||||
mp_obj_t ndarray_get_flatiterator(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
|
||||
return ndarray_new_flatiterator(o_in, iter_buf);
|
||||
}
|
||||
#endif /* NDARRAY_HAS_FLATITER */
|
||||
36
python/port/mod/ulab/numpy/ndarray/ndarray_iter.h
Normal file
36
python/port/mod/ulab/numpy/ndarray/ndarray_iter.h
Normal file
@@ -0,0 +1,36 @@
|
||||
|
||||
/*
|
||||
* This file is part of the micropython-ulab project,
|
||||
*
|
||||
* https://github.com/v923z/micropython-ulab
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 Jeff Epler for Adafruit Industries
|
||||
* 2020-2021 Zoltán Vörös
|
||||
*/
|
||||
|
||||
#ifndef _NDARRAY_ITER_
|
||||
#define _NDARRAY_ITER_
|
||||
|
||||
#include "py/runtime.h"
|
||||
#include "py/binary.h"
|
||||
#include "py/obj.h"
|
||||
#include "py/objarray.h"
|
||||
|
||||
#include "../../ulab.h"
|
||||
#include "../../ndarray.h"
|
||||
|
||||
// TODO: take simply mp_obj_ndarray_it_t from ndarray.c
|
||||
typedef struct _mp_obj_ndarray_flatiter_t {
|
||||
mp_obj_base_t base;
|
||||
mp_fun_1_t iternext;
|
||||
mp_obj_t ndarray;
|
||||
size_t cur;
|
||||
} ndarray_flatiter_t;
|
||||
|
||||
mp_obj_t ndarray_get_flatiterator(mp_obj_t , mp_obj_iter_buf_t *);
|
||||
mp_obj_t ndarray_flatiter_make_new(mp_obj_t );
|
||||
mp_obj_t ndarray_flatiter_next(mp_obj_t );
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user