Compare commits
15 commits
github-act
...
2dim
| Author | SHA1 | Date | |
|---|---|---|---|
| 70af1c8a77 | |||
| a2962f0fe5 | |||
| cf057c4df9 | |||
| de6b7772e4 | |||
| 43551c9a7a | |||
| 23adf6e4a7 | |||
| f6d123beb8 | |||
| 27996c9003 | |||
|
|
bec24fe4a4 | ||
|
|
e71f667114 | ||
|
|
017c1c2c46 | ||
|
|
6fe015f134 | ||
|
|
3727c38182 | ||
|
|
d8bfe46bd8 | ||
|
|
2e3a0b4483 |
25 changed files with 1352 additions and 5837 deletions
4
.github/workflows/build.yml
vendored
4
.github/workflows/build.yml
vendored
|
|
@ -40,9 +40,6 @@ jobs:
|
|||
- name: Build mpy-cross
|
||||
run: make -C micropython/mpy-cross -j2
|
||||
|
||||
- name: Enable ulab in micropython unix port
|
||||
run: echo '#define MODULE_ULAB_ENABLED (1)' >> micropython/ports/unix/mpconfigport.h
|
||||
|
||||
- name: Build micropython unix port
|
||||
run: |
|
||||
make -C micropython/ports/unix -j2 deplibs
|
||||
|
|
@ -57,6 +54,5 @@ jobs:
|
|||
echo -e "\nFAILURE $testbase";
|
||||
diff -u $testbase.exp $testbase.out;
|
||||
done
|
||||
working-directory: tests
|
||||
if: failure()
|
||||
|
||||
|
|
|
|||
33
code/extras.c
Normal file
33
code/extras.c
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
|
||||
/*
|
||||
* This file is part of the micropython-ulab project,
|
||||
*
|
||||
* https://github.com/v923z/micropython-ulab
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 Zoltán Vörös
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "py/obj.h"
|
||||
#include "py/runtime.h"
|
||||
#include "py/misc.h"
|
||||
#include "extras.h"
|
||||
|
||||
#if ULAB_EXTRAS_MODULE
|
||||
|
||||
STATIC const mp_rom_map_elem_t ulab_filter_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_extras) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(mp_module_ulab_extras_globals, ulab_extras_globals_table);
|
||||
|
||||
mp_obj_module_t ulab_filter_module = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&mp_module_ulab_extras_globals,
|
||||
};
|
||||
|
||||
#endif
|
||||
23
code/extras.h
Normal file
23
code/extras.h
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
|
||||
/*
|
||||
* This file is part of the micropython-ulab project,
|
||||
*
|
||||
* https://github.com/v923z/micropython-ulab
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 Zoltán Vörös
|
||||
*/
|
||||
|
||||
#ifndef _EXTRA_
|
||||
#define _EXTRA_
|
||||
|
||||
#include "ulab.h"
|
||||
#include "ndarray.h"
|
||||
|
||||
#if ULAB_EXTRAS_MODULE
|
||||
|
||||
mp_obj_module_t ulab_extras_module;
|
||||
|
||||
#endif
|
||||
#endif
|
||||
38
code/fft.c
38
code/fft.c
|
|
@ -14,13 +14,14 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "py/runtime.h"
|
||||
#include "py/builtin.h"
|
||||
#include "py/binary.h"
|
||||
#include "py/obj.h"
|
||||
#include "py/objarray.h"
|
||||
#include "ndarray.h"
|
||||
#include "fft.h"
|
||||
|
||||
#if ULAB_FFT_FFT || ULAB_FFT_IFFT || ULAB_FFT_SPECTRUM
|
||||
#if ULAB_FFT_MODULE
|
||||
|
||||
enum FFT_TYPE {
|
||||
FFT_FFT,
|
||||
|
|
@ -102,8 +103,9 @@ mp_obj_t fft_fft_ifft_spectrum(size_t n_args, mp_obj_t arg_re, mp_obj_t arg_im,
|
|||
memcpy((mp_float_t *)out_re->array->items, (mp_float_t *)re->array->items, re->bytes);
|
||||
} else {
|
||||
for(size_t i=0; i < len; i++) {
|
||||
data_re[i] = ndarray_get_float_value(re->array->items, re->array->typecode, i);
|
||||
*data_re++ = ndarray_get_float_value(re->array->items, re->array->typecode, i);
|
||||
}
|
||||
data_re -= len;
|
||||
}
|
||||
ndarray_obj_t *out_im = create_new_ndarray(1, len, NDARRAY_FLOAT);
|
||||
mp_float_t *data_im = (mp_float_t *)out_im->array->items;
|
||||
|
|
@ -117,23 +119,27 @@ mp_obj_t fft_fft_ifft_spectrum(size_t n_args, mp_obj_t arg_re, mp_obj_t arg_im,
|
|||
memcpy((mp_float_t *)out_im->array->items, (mp_float_t *)im->array->items, im->bytes);
|
||||
} else {
|
||||
for(size_t i=0; i < len; i++) {
|
||||
data_im[i] = ndarray_get_float_value(im->array->items, im->array->typecode, i);
|
||||
*data_im++ = ndarray_get_float_value(im->array->items, im->array->typecode, i);
|
||||
}
|
||||
data_im -= len;
|
||||
}
|
||||
}
|
||||
|
||||
if((type == FFT_FFT) || (type == FFT_SPECTRUM)) {
|
||||
fft_kernel(data_re, data_im, len, 1);
|
||||
if(type == FFT_SPECTRUM) {
|
||||
for(size_t i=0; i < len; i++) {
|
||||
data_re[i] = MICROPY_FLOAT_C_FUN(sqrt)(data_re[i]*data_re[i] + data_im[i]*data_im[i]);
|
||||
*data_re = MICROPY_FLOAT_C_FUN(sqrt)(*data_re * *data_re + *data_im * *data_im);
|
||||
data_re++;
|
||||
data_im++;
|
||||
}
|
||||
}
|
||||
} else { // inverse transform
|
||||
fft_kernel(data_re, data_im, len, -1);
|
||||
// TODO: numpy accepts the norm keyword argument
|
||||
for(size_t i=0; i < len; i++) {
|
||||
data_re[i] /= len;
|
||||
data_im[i] /= len;
|
||||
*data_re++ /= len;
|
||||
*data_im++ /= len;
|
||||
}
|
||||
}
|
||||
if(type == FFT_SPECTRUM) {
|
||||
|
|
@ -146,7 +152,6 @@ mp_obj_t fft_fft_ifft_spectrum(size_t n_args, mp_obj_t arg_re, mp_obj_t arg_im,
|
|||
}
|
||||
}
|
||||
|
||||
#if ULAB_FFT_FFT
|
||||
mp_obj_t fft_fft(size_t n_args, const mp_obj_t *args) {
|
||||
if(n_args == 2) {
|
||||
return fft_fft_ifft_spectrum(n_args, args[0], args[1], FFT_FFT);
|
||||
|
|
@ -156,9 +161,7 @@ mp_obj_t fft_fft(size_t n_args, const mp_obj_t *args) {
|
|||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fft_fft_obj, 1, 2, fft_fft);
|
||||
#endif
|
||||
|
||||
#if ULAB_FFT_IFFT
|
||||
mp_obj_t fft_ifft(size_t n_args, const mp_obj_t *args) {
|
||||
if(n_args == 2) {
|
||||
return fft_fft_ifft_spectrum(n_args, args[0], args[1], FFT_IFFT);
|
||||
|
|
@ -168,9 +171,7 @@ mp_obj_t fft_ifft(size_t n_args, const mp_obj_t *args) {
|
|||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fft_ifft_obj, 1, 2, fft_ifft);
|
||||
#endif
|
||||
|
||||
#if ULAB_FFT_SPECTRUM
|
||||
mp_obj_t fft_spectrum(size_t n_args, const mp_obj_t *args) {
|
||||
if(n_args == 2) {
|
||||
return fft_fft_ifft_spectrum(n_args, args[0], args[1], FFT_SPECTRUM);
|
||||
|
|
@ -180,6 +181,21 @@ mp_obj_t fft_spectrum(size_t n_args, const mp_obj_t *args) {
|
|||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fft_spectrum_obj, 1, 2, fft_spectrum);
|
||||
|
||||
#if !CIRCUITPY
|
||||
STATIC const mp_rom_map_elem_t ulab_fft_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_fft) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_fft), (mp_obj_t)&fft_fft_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_ifft), (mp_obj_t)&fft_ifft_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_spectrum), (mp_obj_t)&fft_spectrum_obj },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(mp_module_ulab_fft_globals, ulab_fft_globals_table);
|
||||
|
||||
mp_obj_module_t ulab_fft_module = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&mp_module_ulab_fft_globals,
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
|
|||
16
code/fft.h
16
code/fft.h
|
|
@ -19,19 +19,13 @@
|
|||
|
||||
#define SWAP(t, a, b) { t tmp = a; a = b; b = tmp; }
|
||||
|
||||
#if ULAB_FFT_FFT
|
||||
mp_obj_t fft_fft(size_t , const mp_obj_t *);
|
||||
#if ULAB_FFT_MODULE
|
||||
|
||||
extern mp_obj_module_t ulab_fft_module;
|
||||
|
||||
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(fft_fft_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_FFT_IFFT
|
||||
mp_obj_t fft_ifft(size_t , const mp_obj_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(fft_ifft_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_FFT_SPECTRUM
|
||||
mp_obj_t fft_spectrum(size_t , const mp_obj_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(fft_spectrum_obj);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
#include "py/misc.h"
|
||||
#include "filter.h"
|
||||
|
||||
#if ULAB_FILTER_CONVOLVE
|
||||
#if ULAB_FILTER_MODULE
|
||||
mp_obj_t filter_convolve(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_a, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = mp_const_none } },
|
||||
|
|
@ -83,4 +83,19 @@ mp_obj_t filter_convolve(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_a
|
|||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(filter_convolve_obj, 2, filter_convolve);
|
||||
|
||||
#if !CIRCUITPY
|
||||
STATIC const mp_rom_map_elem_t ulab_filter_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_filter) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_convolve), (mp_obj_t)&filter_convolve_obj },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(mp_module_ulab_filter_globals, ulab_filter_globals_table);
|
||||
|
||||
mp_obj_module_t ulab_filter_module = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&mp_module_ulab_filter_globals,
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -15,9 +15,11 @@
|
|||
#include "ulab.h"
|
||||
#include "ndarray.h"
|
||||
|
||||
#if ULAB_FILTER_CONVOLVE
|
||||
mp_obj_t filter_convolve(size_t , const mp_obj_t *, mp_map_t *);
|
||||
#if ULAB_FILTER_MODULE
|
||||
|
||||
extern mp_obj_module_t ulab_filter_module;
|
||||
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(filter_convolve_obj);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
|||
103
code/linalg.c
103
code/linalg.c
|
|
@ -17,69 +17,8 @@
|
|||
#include "py/misc.h"
|
||||
#include "linalg.h"
|
||||
|
||||
#if ULAB_LINALG_TRANSPOSE
|
||||
mp_obj_t linalg_transpose(mp_obj_t self_in) {
|
||||
ndarray_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
// the size of a single item in the array
|
||||
uint8_t _sizeof = mp_binary_get_size('@', self->array->typecode, NULL);
|
||||
|
||||
// NOTE:
|
||||
// if the matrices are square, we can simply swap items, but
|
||||
// generic matrices can't be transposed in place, so we have to
|
||||
// declare a temporary variable
|
||||
|
||||
// NOTE:
|
||||
// In the old matrix, the coordinate (m, n) is m*self->n + n
|
||||
// We have to assign this to the coordinate (n, m) in the new
|
||||
// matrix, i.e., to n*self->m + m (since the new matrix has self->m columns)
|
||||
|
||||
// one-dimensional arrays can be transposed by simply swapping the dimensions
|
||||
if((self->m != 1) && (self->n != 1)) {
|
||||
uint8_t *c = (uint8_t *)self->array->items;
|
||||
// self->bytes is the size of the bytearray, irrespective of the typecode
|
||||
uint8_t *tmp = m_new(uint8_t, self->bytes);
|
||||
for(size_t m=0; m < self->m; m++) {
|
||||
for(size_t n=0; n < self->n; n++) {
|
||||
memcpy(tmp+_sizeof*(n*self->m + m), c+_sizeof*(m*self->n + n), _sizeof);
|
||||
}
|
||||
}
|
||||
memcpy(self->array->items, tmp, self->bytes);
|
||||
m_del(uint8_t, tmp, self->bytes);
|
||||
}
|
||||
SWAP(size_t, self->m, self->n);
|
||||
return mp_const_none;
|
||||
}
|
||||
#if ULAB_LINALG_MODULE
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(linalg_transpose_obj, linalg_transpose);
|
||||
#endif
|
||||
|
||||
#if ULAB_LINALG_RESHAPE
|
||||
mp_obj_t linalg_reshape(mp_obj_t self_in, mp_obj_t shape) {
|
||||
ndarray_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
if(!MP_OBJ_IS_TYPE(shape, &mp_type_tuple) || (MP_OBJ_SMALL_INT_VALUE(mp_obj_len_maybe(shape)) != 2)) {
|
||||
mp_raise_ValueError(translate("shape must be a 2-tuple"));
|
||||
}
|
||||
|
||||
mp_obj_iter_buf_t iter_buf;
|
||||
mp_obj_t item, iterable = mp_getiter(shape, &iter_buf);
|
||||
uint16_t m, n;
|
||||
item = mp_iternext(iterable);
|
||||
m = mp_obj_get_int(item);
|
||||
item = mp_iternext(iterable);
|
||||
n = mp_obj_get_int(item);
|
||||
if(m*n != self->m*self->n) {
|
||||
// TODO: the proper error message would be "cannot reshape array of size %d into shape (%d, %d)"
|
||||
mp_raise_ValueError(translate("cannot reshape array (incompatible input/output shape)"));
|
||||
}
|
||||
self->m = m;
|
||||
self->n = n;
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(linalg_reshape_obj, linalg_reshape);
|
||||
#endif
|
||||
|
||||
#if ULAB_LINALG_SIZE
|
||||
mp_obj_t linalg_size(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = mp_const_none } },
|
||||
|
|
@ -119,9 +58,7 @@ mp_obj_t linalg_size(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args)
|
|||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(linalg_size_obj, 1, linalg_size);
|
||||
#endif
|
||||
|
||||
#if ULAB_LINALG_INV || ULAB_POLY_POLYFIT
|
||||
bool linalg_invert_matrix(mp_float_t *data, size_t N) {
|
||||
// returns true, of the inversion was successful,
|
||||
// false, if the matrix is singular
|
||||
|
|
@ -163,9 +100,7 @@ bool linalg_invert_matrix(mp_float_t *data, size_t N) {
|
|||
m_del(mp_float_t, unit, N*N);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if ULAB_LINALG_INV
|
||||
mp_obj_t linalg_inv(mp_obj_t o_in) {
|
||||
// since inv is not a class method, we have to inspect the input argument first
|
||||
if(!MP_OBJ_IS_TYPE(o_in, &ulab_ndarray_type)) {
|
||||
|
|
@ -200,9 +135,7 @@ mp_obj_t linalg_inv(mp_obj_t o_in) {
|
|||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(linalg_inv_obj, linalg_inv);
|
||||
#endif
|
||||
|
||||
#if ULAB_LINALG_DOT
|
||||
mp_obj_t linalg_dot(mp_obj_t _m1, mp_obj_t _m2) {
|
||||
// TODO: should the results be upcast?
|
||||
if(!MP_OBJ_IS_TYPE(_m1, &ulab_ndarray_type) || !MP_OBJ_IS_TYPE(_m2, &ulab_ndarray_type)) {
|
||||
|
|
@ -233,9 +166,7 @@ mp_obj_t linalg_dot(mp_obj_t _m1, mp_obj_t _m2) {
|
|||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(linalg_dot_obj, linalg_dot);
|
||||
#endif
|
||||
|
||||
#if ULAB_LINALG_ZEROS || ULAB_LINALG_ONES
|
||||
mp_obj_t linalg_zeros_ones(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, uint8_t kind) {
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} } ,
|
||||
|
|
@ -269,25 +200,19 @@ mp_obj_t linalg_zeros_ones(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw
|
|||
}
|
||||
return MP_OBJ_FROM_PTR(ndarray);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if ULAB_LINALG_ZEROS
|
||||
mp_obj_t linalg_zeros(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
return linalg_zeros_ones(n_args, pos_args, kw_args, 0);
|
||||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(linalg_zeros_obj, 0, linalg_zeros);
|
||||
#endif
|
||||
|
||||
#if ULAB_LINALG_ONES
|
||||
mp_obj_t linalg_ones(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
return linalg_zeros_ones(n_args, pos_args, kw_args, 1);
|
||||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(linalg_ones_obj, 0, linalg_ones);
|
||||
#endif
|
||||
|
||||
#if ULAB_LINALG_EYE
|
||||
mp_obj_t linalg_eye(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
|
||||
|
|
@ -330,9 +255,7 @@ mp_obj_t linalg_eye(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args)
|
|||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(linalg_eye_obj, 0, linalg_eye);
|
||||
#endif
|
||||
|
||||
#if ULAB_LINALG_DET
|
||||
mp_obj_t linalg_det(mp_obj_t oin) {
|
||||
if(!MP_OBJ_IS_TYPE(oin, &ulab_ndarray_type)) {
|
||||
mp_raise_TypeError(translate("function defined for ndarrays only"));
|
||||
|
|
@ -371,9 +294,7 @@ mp_obj_t linalg_det(mp_obj_t oin) {
|
|||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(linalg_det_obj, linalg_det);
|
||||
#endif
|
||||
|
||||
#if ULAB_LINALG_EIG
|
||||
mp_obj_t linalg_eig(mp_obj_t oin) {
|
||||
if(!MP_OBJ_IS_TYPE(oin, &ulab_ndarray_type)) {
|
||||
mp_raise_TypeError(translate("function defined for ndarrays only"));
|
||||
|
|
@ -502,4 +423,26 @@ mp_obj_t linalg_eig(mp_obj_t oin) {
|
|||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(linalg_eig_obj, linalg_eig);
|
||||
|
||||
#if !CIRCUITPY
|
||||
STATIC const mp_rom_map_elem_t ulab_linalg_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_linalg) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_size), (mp_obj_t)&linalg_size_obj },
|
||||
{ MP_ROM_QSTR(MP_QSTR_inv), (mp_obj_t)&linalg_inv_obj },
|
||||
{ MP_ROM_QSTR(MP_QSTR_dot), (mp_obj_t)&linalg_dot_obj },
|
||||
{ MP_ROM_QSTR(MP_QSTR_zeros), (mp_obj_t)&linalg_zeros_obj },
|
||||
{ MP_ROM_QSTR(MP_QSTR_ones), (mp_obj_t)&linalg_ones_obj },
|
||||
{ MP_ROM_QSTR(MP_QSTR_eye), (mp_obj_t)&linalg_eye_obj },
|
||||
{ MP_ROM_QSTR(MP_QSTR_det), (mp_obj_t)&linalg_det_obj },
|
||||
{ MP_ROM_QSTR(MP_QSTR_eig), (mp_obj_t)&linalg_eig_obj },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(mp_module_ulab_linalg_globals, ulab_linalg_globals_table);
|
||||
|
||||
mp_obj_module_t ulab_linalg_module = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&mp_module_ulab_linalg_globals,
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -15,8 +15,6 @@
|
|||
#include "ulab.h"
|
||||
#include "ndarray.h"
|
||||
|
||||
#define SWAP(t, a, b) { t tmp = a; a = b; b = tmp; }
|
||||
|
||||
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
|
||||
#define epsilon 1.2e-7
|
||||
#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
|
||||
|
|
@ -25,59 +23,13 @@
|
|||
|
||||
#define JACOBI_MAX 20
|
||||
|
||||
// TODO: transpose, reshape and size should probably be part of ndarray.c
|
||||
#if ULAB_LINALG_TRANSPOSE
|
||||
mp_obj_t linalg_transpose(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(linalg_transpose_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_LINALG_RESHAPE
|
||||
mp_obj_t linalg_reshape(mp_obj_t , mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_2(linalg_reshape_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_LINALG_SIZE
|
||||
mp_obj_t linalg_size(size_t , const mp_obj_t *, mp_map_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(linalg_size_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_LINALG_INV || ULAB_POLY_POLYFIT
|
||||
#if ULAB_LINALG_MODULE || ULAB_POLY_MODULE
|
||||
bool linalg_invert_matrix(mp_float_t *, size_t );
|
||||
#endif
|
||||
|
||||
#if ULAB_LINALG_INV
|
||||
mp_obj_t linalg_inv(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(linalg_inv_obj);
|
||||
#endif
|
||||
#if ULAB_LINALG_MODULE
|
||||
|
||||
#if ULAB_LINALG_DOT
|
||||
mp_obj_t linalg_dot(mp_obj_t , mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_2(linalg_dot_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_LINALG_ZEROS
|
||||
mp_obj_t linalg_zeros(size_t , const mp_obj_t *, mp_map_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(linalg_zeros_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_LINALG_ONES
|
||||
mp_obj_t linalg_ones(size_t , const mp_obj_t *, mp_map_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(linalg_ones_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_LINALG_EYE
|
||||
mp_obj_t linalg_eye(size_t , const mp_obj_t *, mp_map_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(linalg_eye_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_LINALG_DET
|
||||
mp_obj_t linalg_det(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(linalg_det_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_LINALG_EIG
|
||||
mp_obj_t linalg_eig(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(linalg_eig_obj);
|
||||
#endif
|
||||
extern mp_obj_module_t ulab_linalg_module;
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -7,10 +7,13 @@ SRC_USERMOD += $(USERMODULES_DIR)/linalg.c
|
|||
SRC_USERMOD += $(USERMODULES_DIR)/vectorise.c
|
||||
SRC_USERMOD += $(USERMODULES_DIR)/poly.c
|
||||
SRC_USERMOD += $(USERMODULES_DIR)/fft.c
|
||||
SRC_USERMOD += $(USERMODULES_DIR)/filter.c
|
||||
SRC_USERMOD += $(USERMODULES_DIR)/numerical.c
|
||||
SRC_USERMOD += $(USERMODULES_DIR)/filter.c
|
||||
SRC_USERMOD += $(USERMODULES_DIR)/extras.c
|
||||
SRC_USERMOD += $(USERMODULES_DIR)/ulab.c
|
||||
|
||||
# We can add our module folder to include paths if needed
|
||||
# This is not actually needed in this example.
|
||||
CFLAGS_USERMOD += -I$(USERMODULES_DIR)
|
||||
|
||||
CFLAGS_EXTRA = -DMODULE_ULAB_ENABLED=1
|
||||
|
|
|
|||
|
|
@ -304,7 +304,7 @@ mp_obj_t insert_slice_list(ndarray_obj_t *ndarray, size_t m, size_t n,
|
|||
mp_obj_t row_list, mp_obj_t column_list,
|
||||
ndarray_obj_t *values) {
|
||||
if((m != values->m) && (n != values->n)) {
|
||||
if((values->array->len != 1)) { // not a single item
|
||||
if(values->array->len != 1) { // not a single item
|
||||
mp_raise_ValueError(translate("could not broadast input array from shape"));
|
||||
}
|
||||
}
|
||||
|
|
@ -595,7 +595,7 @@ mp_obj_t ndarray_iternext(mp_obj_t self_in) {
|
|||
ndarray_obj_t *ndarray = MP_OBJ_TO_PTR(self->ndarray);
|
||||
// TODO: in numpy, ndarrays are iterated with respect to the first axis.
|
||||
size_t iter_end = 0;
|
||||
if((ndarray->m == 1)) {
|
||||
if(ndarray->m == 1) {
|
||||
iter_end = ndarray->array->len;
|
||||
} else {
|
||||
iter_end = ndarray->m;
|
||||
|
|
@ -890,12 +890,12 @@ mp_obj_t ndarray_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
|
|||
return ndarray_copy(self_in);
|
||||
}
|
||||
ndarray = MP_OBJ_TO_PTR(ndarray_copy(self_in));
|
||||
if((self->array->typecode == NDARRAY_INT8)) {
|
||||
if(self->array->typecode == NDARRAY_INT8) {
|
||||
int8_t *array = (int8_t *)ndarray->array->items;
|
||||
for(size_t i=0; i < self->array->len; i++) {
|
||||
if(array[i] < 0) array[i] = -array[i];
|
||||
}
|
||||
} else if((self->array->typecode == NDARRAY_INT16)) {
|
||||
} else if(self->array->typecode == NDARRAY_INT16) {
|
||||
int16_t *array = (int16_t *)ndarray->array->items;
|
||||
for(size_t i=0; i < self->array->len; i++) {
|
||||
if(array[i] < 0) array[i] = -array[i];
|
||||
|
|
@ -912,18 +912,66 @@ mp_obj_t ndarray_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
|
|||
}
|
||||
}
|
||||
|
||||
mp_obj_t ndarray_transpose(mp_obj_t self_in) {
|
||||
ndarray_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
// the size of a single item in the array
|
||||
uint8_t _sizeof = mp_binary_get_size('@', self->array->typecode, NULL);
|
||||
|
||||
// NOTE:
|
||||
// if the matrices are square, we can simply swap items, but
|
||||
// generic matrices can't be transposed in place, so we have to
|
||||
// declare a temporary variable
|
||||
|
||||
// NOTE:
|
||||
// In the old matrix, the coordinate (m, n) is m*self->n + n
|
||||
// We have to assign this to the coordinate (n, m) in the new
|
||||
// matrix, i.e., to n*self->m + m (since the new matrix has self->m columns)
|
||||
|
||||
// one-dimensional arrays can be transposed by simply swapping the dimensions
|
||||
if((self->m != 1) && (self->n != 1)) {
|
||||
uint8_t *c = (uint8_t *)self->array->items;
|
||||
// self->bytes is the size of the bytearray, irrespective of the typecode
|
||||
uint8_t *tmp = m_new(uint8_t, self->bytes);
|
||||
for(size_t m=0; m < self->m; m++) {
|
||||
for(size_t n=0; n < self->n; n++) {
|
||||
memcpy(tmp+_sizeof*(n*self->m + m), c+_sizeof*(m*self->n + n), _sizeof);
|
||||
}
|
||||
}
|
||||
memcpy(self->array->items, tmp, self->bytes);
|
||||
m_del(uint8_t, tmp, self->bytes);
|
||||
}
|
||||
SWAP(size_t, self->m, self->n);
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(ndarray_transpose_obj, ndarray_transpose);
|
||||
|
||||
mp_obj_t ndarray_reshape(mp_obj_t self_in, mp_obj_t shape) {
|
||||
ndarray_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
if(!MP_OBJ_IS_TYPE(shape, &mp_type_tuple) || (MP_OBJ_SMALL_INT_VALUE(mp_obj_len_maybe(shape)) != 2)) {
|
||||
mp_raise_ValueError(translate("shape must be a 2-tuple"));
|
||||
}
|
||||
|
||||
mp_obj_iter_buf_t iter_buf;
|
||||
mp_obj_t item, iterable = mp_getiter(shape, &iter_buf);
|
||||
uint16_t m, n;
|
||||
item = mp_iternext(iterable);
|
||||
m = mp_obj_get_int(item);
|
||||
item = mp_iternext(iterable);
|
||||
n = mp_obj_get_int(item);
|
||||
if(m*n != self->m*self->n) {
|
||||
// TODO: the proper error message would be "cannot reshape array of size %d into shape (%d, %d)"
|
||||
mp_raise_ValueError(translate("cannot reshape array (incompatible input/output shape)"));
|
||||
}
|
||||
self->m = m;
|
||||
self->n = n;
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(ndarray_reshape_obj, ndarray_reshape);
|
||||
|
||||
mp_int_t ndarray_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
|
||||
ndarray_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
// buffer_p.get_buffer() returns zero for success, while mp_get_buffer returns true for success
|
||||
return !mp_get_buffer(self->array, bufinfo, flags);
|
||||
}
|
||||
|
||||
void ndarray_attributes(mp_obj_t self_in, qstr attribute, mp_obj_t *destination) {
|
||||
if(attribute == MP_QSTR_size) {
|
||||
destination[0] = ndarray_size(self_in);
|
||||
} else if(attribute == MP_QSTR_itemsize) {
|
||||
destination[0] = ndarray_itemsize(self_in);
|
||||
} else if(attribute == MP_QSTR_shape) {
|
||||
destination[0] = ndarray_shape(self_in);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,6 +29,8 @@
|
|||
#define translate(x) x
|
||||
#endif
|
||||
|
||||
#define SWAP(t, a, b) { t tmp = a; a = b; b = tmp; }
|
||||
|
||||
extern const mp_obj_type_t ulab_ndarray_type;
|
||||
|
||||
enum NDARRAY_TYPE {
|
||||
|
|
@ -70,10 +72,18 @@ mp_obj_t ndarray_unary_op(mp_unary_op_t , mp_obj_t );
|
|||
|
||||
mp_obj_t ndarray_shape(mp_obj_t );
|
||||
mp_obj_t ndarray_size(mp_obj_t );
|
||||
mp_obj_t ndarray_itemsize(mp_obj_t );
|
||||
mp_obj_t ndarray_flatten(size_t , const mp_obj_t *, mp_map_t *);
|
||||
mp_int_t ndarray_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
|
||||
|
||||
void ndarray_attributes(mp_obj_t , qstr , mp_obj_t *);
|
||||
mp_obj_t ndarray_reshape(mp_obj_t , mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_2(ndarray_reshape_obj);
|
||||
|
||||
mp_obj_t ndarray_transpose(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(ndarray_transpose_obj);
|
||||
|
||||
mp_int_t ndarray_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
|
||||
//void ndarray_attributes(mp_obj_t , qstr , mp_obj_t *);
|
||||
|
||||
|
||||
#define CREATE_SINGLE_ITEM(outarray, type, typecode, value) do {\
|
||||
ndarray_obj_t *tmp = create_new_ndarray(1, 1, (typecode));\
|
||||
|
|
|
|||
62
code/ndarray_properties.h
Normal file
62
code/ndarray_properties.h
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
|
||||
/*
|
||||
* 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 Zoltán Vörös
|
||||
*/
|
||||
|
||||
#ifndef _NDARRAY_PROPERTIES_
|
||||
#define _NDARRAY_PROPERTIES_
|
||||
|
||||
#include "py/runtime.h"
|
||||
#include "py/binary.h"
|
||||
#include "py/obj.h"
|
||||
#include "py/objarray.h"
|
||||
|
||||
#include "ndarray.h"
|
||||
|
||||
typedef struct _mp_obj_property_t {
|
||||
mp_obj_base_t base;
|
||||
mp_obj_t proxy[3]; // getter, setter, deleter
|
||||
} mp_obj_property_t;
|
||||
|
||||
/* v923z: it is not at all clear to me, why this must be declared; it should already be in obj.h */
|
||||
typedef struct _mp_obj_none_t {
|
||||
mp_obj_base_t base;
|
||||
} mp_obj_none_t;
|
||||
|
||||
const mp_obj_type_t mp_type_NoneType;
|
||||
const mp_obj_none_t mp_const_none_obj = {{&mp_type_NoneType}};
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(ndarray_get_shape_obj, ndarray_shape);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(ndarray_get_size_obj, ndarray_size);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(ndarray_get_itemsize_obj, ndarray_itemsize);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(ndarray_flatten_obj, 1, ndarray_flatten);
|
||||
|
||||
STATIC const mp_obj_property_t ndarray_shape_obj = {
|
||||
.base.type = &mp_type_property,
|
||||
.proxy = {(mp_obj_t)&ndarray_get_shape_obj,
|
||||
(mp_obj_t)&mp_const_none_obj,
|
||||
(mp_obj_t)&mp_const_none_obj},
|
||||
};
|
||||
|
||||
STATIC const mp_obj_property_t ndarray_size_obj = {
|
||||
.base.type = &mp_type_property,
|
||||
.proxy = {(mp_obj_t)&ndarray_get_size_obj,
|
||||
(mp_obj_t)&mp_const_none_obj,
|
||||
(mp_obj_t)&mp_const_none_obj},
|
||||
};
|
||||
|
||||
STATIC const mp_obj_property_t ndarray_itemsize_obj = {
|
||||
.base.type = &mp_type_property,
|
||||
.proxy = {(mp_obj_t)&ndarray_get_itemsize_obj,
|
||||
(mp_obj_t)&mp_const_none_obj,
|
||||
(mp_obj_t)&mp_const_none_obj},
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -19,6 +19,8 @@
|
|||
#include "py/misc.h"
|
||||
#include "numerical.h"
|
||||
|
||||
#if ULAB_NUMERICAL_MODULE
|
||||
|
||||
enum NUMERICAL_FUNCTION_TYPE {
|
||||
NUMERICAL_MIN,
|
||||
NUMERICAL_MAX,
|
||||
|
|
@ -29,7 +31,6 @@ enum NUMERICAL_FUNCTION_TYPE {
|
|||
NUMERICAL_STD,
|
||||
};
|
||||
|
||||
#if ULAB_NUMERICAL_LINSPACE
|
||||
mp_obj_t numerical_linspace(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = mp_const_none } },
|
||||
|
|
@ -80,7 +81,6 @@ mp_obj_t numerical_linspace(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
|
|||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(numerical_linspace_obj, 2, numerical_linspace);
|
||||
#endif
|
||||
|
||||
void axis_sorter(ndarray_obj_t *ndarray, mp_obj_t axis, size_t *m, size_t *n, size_t *N,
|
||||
size_t *increment, size_t *len, size_t *start_inc) {
|
||||
|
|
@ -255,7 +255,6 @@ mp_obj_t numerical_argmin_argmax_ndarray(ndarray_obj_t *ndarray, mp_obj_t axis,
|
|||
return MP_OBJ_FROM_PTR(results);
|
||||
}
|
||||
|
||||
|
||||
STATIC mp_obj_t numerical_function(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, uint8_t optype) {
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = mp_const_none} } ,
|
||||
|
|
@ -372,7 +371,6 @@ mp_obj_t numerical_std(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_arg
|
|||
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(numerical_std_obj, 1, numerical_std);
|
||||
|
||||
#if ULAB_NUMERICAL_ROLL
|
||||
mp_obj_t numerical_roll(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = mp_const_none } },
|
||||
|
|
@ -454,9 +452,7 @@ mp_obj_t numerical_roll(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar
|
|||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(numerical_roll_obj, 2, numerical_roll);
|
||||
#endif
|
||||
|
||||
#if ULAB_NUMERICAL_FLIP
|
||||
mp_obj_t numerical_flip(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = mp_const_none } },
|
||||
|
|
@ -505,9 +501,7 @@ mp_obj_t numerical_flip(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar
|
|||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(numerical_flip_obj, 1, numerical_flip);
|
||||
#endif
|
||||
|
||||
#if ULAB_NUMERICAL_DIFF
|
||||
mp_obj_t numerical_diff(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = mp_const_none } },
|
||||
|
|
@ -577,9 +571,7 @@ mp_obj_t numerical_diff(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar
|
|||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(numerical_diff_obj, 1, numerical_diff);
|
||||
#endif
|
||||
|
||||
#if ULAB_NUMERICAL_SORT
|
||||
mp_obj_t numerical_sort_helper(mp_obj_t oin, mp_obj_t axis, uint8_t inplace) {
|
||||
if(!MP_OBJ_IS_TYPE(oin, &ulab_ndarray_type)) {
|
||||
mp_raise_TypeError(translate("sort argument must be an ndarray"));
|
||||
|
|
@ -665,9 +657,7 @@ mp_obj_t numerical_sort_inplace(size_t n_args, const mp_obj_t *pos_args, mp_map_
|
|||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(numerical_sort_inplace_obj, 1, numerical_sort_inplace);
|
||||
#endif
|
||||
|
||||
#if ULAB_NUMERICAL_ARGSORT
|
||||
mp_obj_t numerical_argsort(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = mp_const_none } },
|
||||
|
|
@ -739,4 +729,30 @@ mp_obj_t numerical_argsort(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw
|
|||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(numerical_argsort_obj, 1, numerical_argsort);
|
||||
|
||||
#if !CIRCUITPY
|
||||
STATIC const mp_rom_map_elem_t ulab_numerical_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_linspace), (mp_obj_t)&numerical_linspace_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_sum), (mp_obj_t)&numerical_sum_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_mean), (mp_obj_t)&numerical_mean_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_std), (mp_obj_t)&numerical_std_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_min), (mp_obj_t)&numerical_min_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_max), (mp_obj_t)&numerical_max_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_argmin), (mp_obj_t)&numerical_argmin_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_argmax), (mp_obj_t)&numerical_argmax_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_roll), (mp_obj_t)&numerical_roll_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_flip), (mp_obj_t)&numerical_flip_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_diff), (mp_obj_t)&numerical_diff_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_sort), (mp_obj_t)&numerical_sort_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_argsort), (mp_obj_t)&numerical_argsort_obj },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(mp_module_ulab_numerical_globals, ulab_numerical_globals_table);
|
||||
|
||||
mp_obj_module_t ulab_numerical_module = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&mp_module_ulab_numerical_globals,
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -15,78 +15,15 @@
|
|||
#include "ulab.h"
|
||||
#include "ndarray.h"
|
||||
|
||||
#if ULAB_NUMERICAL_LINSPACE
|
||||
mp_obj_t numerical_linspace(size_t , const mp_obj_t *, mp_map_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_linspace_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_NUMERICAL_SUM
|
||||
mp_obj_t numerical_sum(size_t , const mp_obj_t *, mp_map_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_sum_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_NUMERICAL_MEAN
|
||||
mp_obj_t numerical_mean(size_t , const mp_obj_t *, mp_map_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_mean_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_NUMERICAL_STD
|
||||
mp_obj_t numerical_std(size_t , const mp_obj_t *, mp_map_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_std_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_NUMERICAL_MIN
|
||||
mp_obj_t numerical_min(size_t , const mp_obj_t *, mp_map_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_min_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_NUMERICAL_MAX
|
||||
mp_obj_t numerical_max(size_t , const mp_obj_t *, mp_map_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_max_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_NUMERICAL_ARGMIN
|
||||
mp_obj_t numerical_argmin(size_t , const mp_obj_t *, mp_map_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_argmin_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_NUMERICAL_ARGMAX
|
||||
mp_obj_t numerical_argmax(size_t , const mp_obj_t *, mp_map_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_argmax_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_NUMERICAL_ROLL
|
||||
mp_obj_t numerical_roll(size_t , const mp_obj_t *, mp_map_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_roll_obj);
|
||||
#endif
|
||||
#if ULAB_NUMERICAL_MODULE
|
||||
|
||||
extern mp_obj_module_t ulab_numerical_module;
|
||||
|
||||
// TODO: implement minimum/maximum, and cumsum
|
||||
//mp_obj_t numerical_minimum(mp_obj_t , mp_obj_t );
|
||||
//mp_obj_t numerical_maximum(mp_obj_t , mp_obj_t );
|
||||
//mp_obj_t numerical_cumsum(size_t , const mp_obj_t *, mp_map_t *);
|
||||
|
||||
#if ULAB_NUMERICAL_FLIP
|
||||
mp_obj_t numerical_flip(size_t , const mp_obj_t *, mp_map_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_flip_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_NUMERICAL_DIFF
|
||||
mp_obj_t numerical_diff(size_t , const mp_obj_t *, mp_map_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_diff_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_NUMERICAL_SORT
|
||||
mp_obj_t numerical_sort(size_t , const mp_obj_t *, mp_map_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_sort_obj);
|
||||
|
||||
mp_obj_t numerical_sort_inplace(size_t , const mp_obj_t *, mp_map_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_sort_inplace_obj);
|
||||
|
||||
mp_obj_t numerical_argsort(size_t , const mp_obj_t *, mp_map_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_argsort_obj);
|
||||
#endif
|
||||
|
||||
#define RUN_ARGMIN(in, out, typein, typeout, len, start, increment, op, pos) do {\
|
||||
typein *array = (typein *)(in)->array->items;\
|
||||
typeout *outarray = (typeout *)(out)->array->items;\
|
||||
|
|
@ -211,4 +148,20 @@ MP_DECLARE_CONST_FUN_OBJ_KW(numerical_argsort_obj);
|
|||
}\
|
||||
} while(0)
|
||||
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_linspace_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_min_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_max_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_argmin_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_argmax_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_sum_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_mean_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_std_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_roll_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_flip_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_diff_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_sort_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_sort_inplace_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(numerical_argsort_obj);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
|||
26
code/poly.c
26
code/poly.c
|
|
@ -16,7 +16,7 @@
|
|||
#include "linalg.h"
|
||||
#include "poly.h"
|
||||
|
||||
#if ULAB_POLY_POLYVAL || ULAB_POLY_POLYFIT
|
||||
#if ULAB_POLY_MODULE
|
||||
bool object_is_nditerable(mp_obj_t o_in) {
|
||||
if(MP_OBJ_IS_TYPE(o_in, &ulab_ndarray_type) ||
|
||||
MP_OBJ_IS_TYPE(o_in, &mp_type_tuple) ||
|
||||
|
|
@ -35,9 +35,7 @@ size_t get_nditerable_len(mp_obj_t o_in) {
|
|||
return (size_t)mp_obj_get_int(mp_obj_len_maybe(o_in));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if ULAB_POLY_POLYVAL
|
||||
mp_obj_t poly_polyval(mp_obj_t o_p, mp_obj_t o_x) {
|
||||
// TODO: return immediately, if o_p is not an iterable
|
||||
// TODO: there is a bug here: matrices won't work,
|
||||
|
|
@ -86,9 +84,7 @@ mp_obj_t poly_polyval(mp_obj_t o_p, mp_obj_t o_x) {
|
|||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(poly_polyval_obj, poly_polyval);
|
||||
#endif
|
||||
|
||||
#if ULAB_POLY_POLYFIT
|
||||
mp_obj_t poly_polyfit(size_t n_args, const mp_obj_t *args) {
|
||||
if((n_args != 2) && (n_args != 3)) {
|
||||
mp_raise_ValueError(translate("number of arguments must be 2, or 3"));
|
||||
|
|
@ -96,8 +92,8 @@ mp_obj_t poly_polyfit(size_t n_args, const mp_obj_t *args) {
|
|||
if(!object_is_nditerable(args[0])) {
|
||||
mp_raise_ValueError(translate("input data must be an iterable"));
|
||||
}
|
||||
uint16_t lenx, leny;
|
||||
uint8_t deg;
|
||||
uint16_t lenx = 0, leny = 0;
|
||||
uint8_t deg = 0;
|
||||
mp_float_t *x, *XT, *y, *prod;
|
||||
|
||||
if(n_args == 2) { // only the y values are supplied
|
||||
|
|
@ -200,4 +196,20 @@ mp_obj_t poly_polyfit(size_t n_args, const mp_obj_t *args) {
|
|||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poly_polyfit_obj, 2, 3, poly_polyfit);
|
||||
|
||||
#if !CIRCUITPY
|
||||
STATIC const mp_rom_map_elem_t ulab_poly_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_poly) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_polyval), (mp_obj_t)&poly_polyval_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_polyfit), (mp_obj_t)&poly_polyfit_obj },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(mp_module_ulab_poly_globals, ulab_poly_globals_table);
|
||||
|
||||
mp_obj_module_t ulab_poly_module = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&mp_module_ulab_poly_globals,
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
|
|||
12
code/poly.h
12
code/poly.h
|
|
@ -14,14 +14,12 @@
|
|||
|
||||
#include "ulab.h"
|
||||
|
||||
#if ULAB_POLY_POLYVAL
|
||||
mp_obj_t poly_polyval(mp_obj_t , mp_obj_t );
|
||||
#if ULAB_POLY_MODULE
|
||||
|
||||
extern mp_obj_module_t ulab_poly_module;
|
||||
|
||||
MP_DECLARE_CONST_FUN_OBJ_2(poly_polyval_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_POLY_POLYFIT
|
||||
mp_obj_t poly_polyfit(size_t , const mp_obj_t *);
|
||||
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(poly_polyfit_obj);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
|||
182
code/ulab.c
182
code/ulab.c
|
|
@ -20,27 +20,25 @@
|
|||
|
||||
#include "ulab.h"
|
||||
#include "ndarray.h"
|
||||
#include "ndarray_properties.h"
|
||||
#include "linalg.h"
|
||||
#include "vectorise.h"
|
||||
#include "poly.h"
|
||||
#include "fft.h"
|
||||
#include "filter.h"
|
||||
#include "numerical.h"
|
||||
#include "extras.h"
|
||||
|
||||
STATIC MP_DEFINE_STR_OBJ(ulab_version_obj, "0.32.0");
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(ndarray_shape_obj, ndarray_shape);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(ndarray_flatten_obj, 1, ndarray_flatten);
|
||||
STATIC MP_DEFINE_STR_OBJ(ulab_version_obj, "0.34.0");
|
||||
|
||||
STATIC const mp_rom_map_elem_t ulab_ndarray_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_flatten), MP_ROM_PTR(&ndarray_flatten_obj) },
|
||||
#if ULAB_LINALG_TRANSPOSE
|
||||
{ MP_ROM_QSTR(MP_QSTR_transpose), MP_ROM_PTR(&linalg_transpose_obj) },
|
||||
#endif
|
||||
#if ULAB_LINALG_RESHAPE
|
||||
{ MP_ROM_QSTR(MP_QSTR_reshape), MP_ROM_PTR(&linalg_reshape_obj) },
|
||||
#endif
|
||||
{ MP_ROM_QSTR(MP_QSTR_sort), MP_ROM_PTR(&numerical_sort_inplace_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_reshape), MP_ROM_PTR(&ndarray_reshape_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_transpose), MP_ROM_PTR(&ndarray_transpose_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_shape), MP_ROM_PTR(&ndarray_shape_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_size), MP_ROM_PTR(&ndarray_size_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_itemsize), MP_ROM_PTR(&ndarray_itemsize_obj) },
|
||||
// { MP_ROM_QSTR(MP_QSTR_sort), MP_ROM_PTR(&numerical_sort_inplace_obj) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(ulab_ndarray_locals_dict, ulab_ndarray_locals_dict_table);
|
||||
|
|
@ -56,162 +54,33 @@ const mp_obj_type_t ulab_ndarray_type = {
|
|||
.binary_op = ndarray_binary_op,
|
||||
.buffer_p = { .get_buffer = ndarray_get_buffer, },
|
||||
.locals_dict = (mp_obj_dict_t*)&ulab_ndarray_locals_dict,
|
||||
.attr = ndarray_attributes,
|
||||
};
|
||||
|
||||
#if !CIRCUITPY
|
||||
STATIC const mp_map_elem_t ulab_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_ulab) },
|
||||
{ MP_ROM_QSTR(MP_QSTR___version__), MP_ROM_PTR(&ulab_version_obj) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_array), (mp_obj_t)&ulab_ndarray_type },
|
||||
#if ULAB_LINALG_SIZE
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_size), (mp_obj_t)&linalg_size_obj },
|
||||
#if ULAB_LINALG_MODULE
|
||||
{ MP_ROM_QSTR(MP_QSTR_linalg), MP_ROM_PTR(&ulab_linalg_module) },
|
||||
#endif
|
||||
#if ULAB_LINALG_INV
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_inv), (mp_obj_t)&linalg_inv_obj },
|
||||
#if ULAB_VECTORISE_MODULE
|
||||
{ MP_ROM_QSTR(MP_QSTR_vector), MP_ROM_PTR(&ulab_vectorise_module) },
|
||||
#endif
|
||||
#if ULAB_LINALG_DOT
|
||||
{ MP_ROM_QSTR(MP_QSTR_dot), (mp_obj_t)&linalg_dot_obj },
|
||||
#if ULAB_NUMERICAL_MODULE
|
||||
{ MP_ROM_QSTR(MP_QSTR_numerical), MP_ROM_PTR(&ulab_numerical_module) },
|
||||
#endif
|
||||
#if ULAB_LINALG_ZEROS
|
||||
{ MP_ROM_QSTR(MP_QSTR_zeros), (mp_obj_t)&linalg_zeros_obj },
|
||||
#if ULAB_POLY_MODULE
|
||||
{ MP_ROM_QSTR(MP_QSTR_poly), MP_ROM_PTR(&ulab_poly_module) },
|
||||
#endif
|
||||
#if ULAB_LINALG_ONES
|
||||
{ MP_ROM_QSTR(MP_QSTR_ones), (mp_obj_t)&linalg_ones_obj },
|
||||
#if ULAB_FFT_MODULE
|
||||
{ MP_ROM_QSTR(MP_QSTR_fft), MP_ROM_PTR(&ulab_fft_module) },
|
||||
#endif
|
||||
#if ULAB_LINALG_EYE
|
||||
{ MP_ROM_QSTR(MP_QSTR_eye), (mp_obj_t)&linalg_eye_obj },
|
||||
#if ULAB_FILTER_MODULE
|
||||
{ MP_ROM_QSTR(MP_QSTR_filter), MP_ROM_PTR(&ulab_filter_module) },
|
||||
#endif
|
||||
#if ULAB_LINALG_DET
|
||||
{ MP_ROM_QSTR(MP_QSTR_det), (mp_obj_t)&linalg_det_obj },
|
||||
#endif
|
||||
#if ULAB_LINALG_EIG
|
||||
{ MP_ROM_QSTR(MP_QSTR_eig), (mp_obj_t)&linalg_eig_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_ACOS
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_acos), (mp_obj_t)&vectorise_acos_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_ACOSH
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_acosh), (mp_obj_t)&vectorise_acosh_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_ASIN
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_asin), (mp_obj_t)&vectorise_asin_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_ASINH
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_asinh), (mp_obj_t)&vectorise_asinh_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_ATAN
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_atan), (mp_obj_t)&vectorise_atan_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_ATANH
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_atanh), (mp_obj_t)&vectorise_atanh_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_CEIL
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_ceil), (mp_obj_t)&vectorise_ceil_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_COS
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_cos), (mp_obj_t)&vectorise_cos_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_ERF
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_erf), (mp_obj_t)&vectorise_erf_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_ERFC
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_erfc), (mp_obj_t)&vectorise_erfc_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_EXP
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_exp), (mp_obj_t)&vectorise_exp_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_EXPM1
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_expm1), (mp_obj_t)&vectorise_expm1_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_FLOOR
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_floor), (mp_obj_t)&vectorise_floor_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_GAMMA
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_gamma), (mp_obj_t)&vectorise_gamma_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_LGAMMA
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_lgamma), (mp_obj_t)&vectorise_lgamma_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_LOG
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_log), (mp_obj_t)&vectorise_log_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_LOG10
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_log10), (mp_obj_t)&vectorise_log10_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_LOG2
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_log2), (mp_obj_t)&vectorise_log2_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_SIN
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_sin), (mp_obj_t)&vectorise_sin_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_SINH
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_sinh), (mp_obj_t)&vectorise_sinh_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_SQRT
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_sqrt), (mp_obj_t)&vectorise_sqrt_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_TAN
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_tan), (mp_obj_t)&vectorise_tan_obj },
|
||||
#endif
|
||||
#if ULAB_VECTORISE_TANH
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_tanh), (mp_obj_t)&vectorise_tanh_obj },
|
||||
#endif
|
||||
#if ULAB_NUMERICAL_LINSPACE
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_linspace), (mp_obj_t)&numerical_linspace_obj },
|
||||
#endif
|
||||
#if ULAB_NUMERICAL_SUM
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_sum), (mp_obj_t)&numerical_sum_obj },
|
||||
#endif
|
||||
#if ULAB_NUMERICAL_MEAN
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_mean), (mp_obj_t)&numerical_mean_obj },
|
||||
#endif
|
||||
#if ULAB_NUMERICAL_STD
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_std), (mp_obj_t)&numerical_std_obj },
|
||||
#endif
|
||||
#if ULAB_NUMERICAL_MIN
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_min), (mp_obj_t)&numerical_min_obj },
|
||||
#endif
|
||||
#if ULAB_NUMERICAL_MAX
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_max), (mp_obj_t)&numerical_max_obj },
|
||||
#endif
|
||||
#if ULAB_NUMERICAL_ARGMIN
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_argmin), (mp_obj_t)&numerical_argmin_obj },
|
||||
#endif
|
||||
#if ULAB_NUMERICAL_ARGMAX
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_argmax), (mp_obj_t)&numerical_argmax_obj },
|
||||
#endif
|
||||
#if ULAB_NUMERICAL_ROLL
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_roll), (mp_obj_t)&numerical_roll_obj },
|
||||
#endif
|
||||
#if ULAB_NUMERICAL_FLIP
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_flip), (mp_obj_t)&numerical_flip_obj },
|
||||
#endif
|
||||
#if ULAB_NUMERICAL_DIFF
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_diff), (mp_obj_t)&numerical_diff_obj },
|
||||
#endif
|
||||
#if ULAB_NUMERICAL_SORT
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_sort), (mp_obj_t)&numerical_sort_obj },
|
||||
#endif
|
||||
#if ULAB_NUMERICAL_ARGSORT
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_argsort), (mp_obj_t)&numerical_argsort_obj },
|
||||
#endif
|
||||
#if ULAB_POLY_POLYVAL
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_polyval), (mp_obj_t)&poly_polyval_obj },
|
||||
#endif
|
||||
#if ULAB_POLY_POLYFIT
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_polyfit), (mp_obj_t)&poly_polyfit_obj },
|
||||
#endif
|
||||
#if ULAB_FFT_FFT
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_fft), (mp_obj_t)&fft_fft_obj },
|
||||
#endif
|
||||
#if ULAB_FFT_IFFT
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_ifft), (mp_obj_t)&fft_ifft_obj },
|
||||
#endif
|
||||
#if ULAB_FFT_SPECTRUM
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_spectrum), (mp_obj_t)&fft_spectrum_obj },
|
||||
#endif
|
||||
#if ULAB_FILTER_CONVOLVE
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_convolve), (mp_obj_t)&filter_convolve_obj },
|
||||
#if ULAB_EXTRAS_MODULE
|
||||
{ MP_ROM_QSTR(MP_QSTR_extras), MP_ROM_PTR(&ulab_extras_module) },
|
||||
#endif
|
||||
// class constants
|
||||
{ MP_ROM_QSTR(MP_QSTR_uint8), MP_ROM_INT(NDARRAY_UINT8) },
|
||||
|
|
@ -226,9 +95,10 @@ STATIC MP_DEFINE_CONST_DICT (
|
|||
ulab_globals_table
|
||||
);
|
||||
|
||||
const mp_obj_module_t ulab_user_cmodule = {
|
||||
mp_obj_module_t ulab_user_cmodule = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&mp_module_ulab_globals,
|
||||
};
|
||||
|
||||
MP_REGISTER_MODULE(MP_QSTR_ulab, ulab_user_cmodule, MODULE_ULAB_ENABLED);
|
||||
#endif
|
||||
|
|
|
|||
65
code/ulab.h
65
code/ulab.h
|
|
@ -13,67 +13,24 @@
|
|||
#define __ULAB__
|
||||
|
||||
// vectorise (all functions) takes approx. 3 kB of flash space
|
||||
#define ULAB_VECTORISE_ACOS (1)
|
||||
#define ULAB_VECTORISE_ACOSH (1)
|
||||
#define ULAB_VECTORISE_ASIN (1)
|
||||
#define ULAB_VECTORISE_ASINH (1)
|
||||
#define ULAB_VECTORISE_ATAN (1)
|
||||
#define ULAB_VECTORISE_ATANH (1)
|
||||
#define ULAB_VECTORISE_CEIL (1)
|
||||
#define ULAB_VECTORISE_COS (1)
|
||||
#define ULAB_VECTORISE_ERF (1)
|
||||
#define ULAB_VECTORISE_ERFC (1)
|
||||
#define ULAB_VECTORISE_EXP (1)
|
||||
#define ULAB_VECTORISE_EXPM1 (1)
|
||||
#define ULAB_VECTORISE_FLOOR (1)
|
||||
#define ULAB_VECTORISE_GAMMA (1)
|
||||
#define ULAB_VECTORISE_LGAMMA (1)
|
||||
#define ULAB_VECTORISE_LOG (1)
|
||||
#define ULAB_VECTORISE_LOG10 (1)
|
||||
#define ULAB_VECTORISE_LOG2 (1)
|
||||
#define ULAB_VECTORISE_SIN (1)
|
||||
#define ULAB_VECTORISE_SINH (1)
|
||||
#define ULAB_VECTORISE_SQRT (1)
|
||||
#define ULAB_VECTORISE_TAN (1)
|
||||
#define ULAB_VECTORISE_TANH (1)
|
||||
#define ULAB_VECTORISE_MODULE (1)
|
||||
|
||||
// linalg adds around 6 kB
|
||||
#define ULAB_LINALG_TRANSPOSE (1)
|
||||
#define ULAB_LINALG_RESHAPE (1)
|
||||
#define ULAB_LINALG_SIZE (1)
|
||||
#define ULAB_LINALG_INV (1)
|
||||
#define ULAB_LINALG_DOT (1)
|
||||
#define ULAB_LINALG_ZEROS (1)
|
||||
#define ULAB_LINALG_ONES (1)
|
||||
#define ULAB_LINALG_EYE (1)
|
||||
#define ULAB_LINALG_DET (1)
|
||||
#define ULAB_LINALG_EIG (1)
|
||||
#define ULAB_LINALG_MODULE (1)
|
||||
|
||||
// poly is approx. 2.5 kB
|
||||
#define ULAB_POLY_POLYVAL (1)
|
||||
#define ULAB_POLY_POLYFIT (1)
|
||||
#define ULAB_POLY_MODULE (1)
|
||||
|
||||
//
|
||||
#define ULAB_NUMERICAL_LINSPACE (1)
|
||||
#define ULAB_NUMERICAL_SUM (1)
|
||||
#define ULAB_NUMERICAL_MEAN (1)
|
||||
#define ULAB_NUMERICAL_STD (1)
|
||||
#define ULAB_NUMERICAL_MIN (1)
|
||||
#define ULAB_NUMERICAL_MAX (1)
|
||||
#define ULAB_NUMERICAL_ARGMIN (1)
|
||||
#define ULAB_NUMERICAL_ARGMAX (1)
|
||||
#define ULAB_NUMERICAL_ROLL (1)
|
||||
#define ULAB_NUMERICAL_FLIP (1)
|
||||
#define ULAB_NUMERICAL_DIFF (1)
|
||||
#define ULAB_NUMERICAL_SORT (1)
|
||||
#define ULAB_NUMERICAL_ARGSORT (1)
|
||||
// numerical is about 12 kB
|
||||
#define ULAB_NUMERICAL_MODULE (1)
|
||||
|
||||
// FFT costs about 2 kB of flash space
|
||||
#define ULAB_FFT_FFT (1)
|
||||
#define ULAB_FFT_IFFT (1)
|
||||
#define ULAB_FFT_SPECTRUM (1)
|
||||
#define ULAB_FFT_MODULE (1)
|
||||
|
||||
// the filter module takes about 0.8 kB of flash space
|
||||
#define ULAB_FILTER_CONVOLVE (1)
|
||||
// the filter module takes about 1 kB of flash space
|
||||
#define ULAB_FILTER_MODULE (1)
|
||||
|
||||
// user-defined modules
|
||||
#define ULAB_EXTRAS_MODULE (0)
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@
|
|||
#define MP_PI MICROPY_FLOAT_CONST(3.14159265358979323846)
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_MODULE
|
||||
mp_obj_t vectorise_generic_vector(mp_obj_t o_in, mp_float_t (*f)(mp_float_t)) {
|
||||
// Return a single value, if o_in is not iterable
|
||||
if(mp_obj_is_float(o_in) || MP_OBJ_IS_INT(o_in)) {
|
||||
|
|
@ -62,117 +63,112 @@ mp_obj_t vectorise_generic_vector(mp_obj_t o_in, mp_float_t (*f)(mp_float_t)) {
|
|||
}
|
||||
|
||||
|
||||
#if ULAB_VECTORISE_ACOS
|
||||
MATH_FUN_1(acos, acos);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_acos_obj, vectorise_acos);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_ACOSH
|
||||
MATH_FUN_1(acosh, acosh);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_acosh_obj, vectorise_acosh);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_ASIN
|
||||
MATH_FUN_1(asin, asin);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_asin_obj, vectorise_asin);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_ASINH
|
||||
MATH_FUN_1(asinh, asinh);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_asinh_obj, vectorise_asinh);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_ATAN
|
||||
MATH_FUN_1(atan, atan);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_atan_obj, vectorise_atan);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_ATANH
|
||||
MATH_FUN_1(atanh, atanh);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_atanh_obj, vectorise_atanh);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_CEIL
|
||||
MATH_FUN_1(ceil, ceil);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_ceil_obj, vectorise_ceil);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_COS
|
||||
MATH_FUN_1(cos, cos);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_cos_obj, vectorise_cos);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_ERF
|
||||
MATH_FUN_1(cosh, cosh);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_cosh_obj, vectorise_cosh);
|
||||
|
||||
MATH_FUN_1(erf, erf);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_erf_obj, vectorise_erf);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_ERFC
|
||||
MATH_FUN_1(erfc, erfc);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_erfc_obj, vectorise_erfc);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_EXP
|
||||
MATH_FUN_1(exp, exp);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_exp_obj, vectorise_exp);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_EXPM1
|
||||
MATH_FUN_1(expm1, expm1);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_expm1_obj, vectorise_expm1);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_FLOOR
|
||||
MATH_FUN_1(floor, floor);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_floor_obj, vectorise_floor);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_GAMMA
|
||||
MATH_FUN_1(gamma, tgamma);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_gamma_obj, vectorise_gamma);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_LGAMMA
|
||||
MATH_FUN_1(lgamma, lgamma);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_lgamma_obj, vectorise_lgamma);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_LOG
|
||||
MATH_FUN_1(log, log);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_log_obj, vectorise_log);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_LOG10
|
||||
MATH_FUN_1(log10, log10);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_log10_obj, vectorise_log10);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_LOG2
|
||||
MATH_FUN_1(log2, log2);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_log2_obj, vectorise_log2);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_SIN
|
||||
MATH_FUN_1(sin, sin);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_sin_obj, vectorise_sin);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_SINH
|
||||
MATH_FUN_1(sinh, sinh);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_sinh_obj, vectorise_sinh);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_SQRT
|
||||
MATH_FUN_1(sqrt, sqrt);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_sqrt_obj, vectorise_sqrt);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_TAN
|
||||
MATH_FUN_1(tan, tan);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_tan_obj, vectorise_tan);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_TANH
|
||||
MATH_FUN_1(tanh, tanh);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(vectorise_tanh_obj, vectorise_tanh);
|
||||
|
||||
#if !CIRCUITPY
|
||||
STATIC const mp_rom_map_elem_t ulab_vectorise_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_vector) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_acos), (mp_obj_t)&vectorise_acos_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_acosh), (mp_obj_t)&vectorise_acosh_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_asin), (mp_obj_t)&vectorise_asin_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_asinh), (mp_obj_t)&vectorise_asinh_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_atan), (mp_obj_t)&vectorise_atan_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_atanh), (mp_obj_t)&vectorise_atanh_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_ceil), (mp_obj_t)&vectorise_ceil_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_cos), (mp_obj_t)&vectorise_cos_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_erf), (mp_obj_t)&vectorise_erf_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_erfc), (mp_obj_t)&vectorise_erfc_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_exp), (mp_obj_t)&vectorise_exp_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_expm1), (mp_obj_t)&vectorise_expm1_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_floor), (mp_obj_t)&vectorise_floor_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_gamma), (mp_obj_t)&vectorise_gamma_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_lgamma), (mp_obj_t)&vectorise_lgamma_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_log), (mp_obj_t)&vectorise_log_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_log10), (mp_obj_t)&vectorise_log10_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_log2), (mp_obj_t)&vectorise_log2_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_sin), (mp_obj_t)&vectorise_sin_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_sinh), (mp_obj_t)&vectorise_sinh_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_sqrt), (mp_obj_t)&vectorise_sqrt_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_tan), (mp_obj_t)&vectorise_tan_obj },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_tanh), (mp_obj_t)&vectorise_tanh_obj },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(mp_module_ulab_vectorise_globals, ulab_vectorise_globals_table);
|
||||
|
||||
mp_obj_module_t ulab_vectorise_module = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&mp_module_ulab_vectorise_globals,
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
|
|||
119
code/vectorise.h
119
code/vectorise.h
|
|
@ -15,121 +15,9 @@
|
|||
#include "ulab.h"
|
||||
#include "ndarray.h"
|
||||
|
||||
#if ULAB_VECTORISE_MODULE
|
||||
|
||||
#if ULAB_VECTORISE_ACOS
|
||||
mp_obj_t vectorise_acos(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_acos_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_ACOSH
|
||||
mp_obj_t vectorise_acosh(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_acosh_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_ASIN
|
||||
mp_obj_t vectorise_asin(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_asin_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_ASINH
|
||||
mp_obj_t vectorise_asinh(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_asinh_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_ATANH
|
||||
mp_obj_t vectorise_atan(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_atan_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_ATANH
|
||||
mp_obj_t vectorise_atanh(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_atanh_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_CEIL
|
||||
mp_obj_t vectorise_ceil(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_ceil_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_COS
|
||||
mp_obj_t vectorise_cos(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_cos_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_ERF
|
||||
mp_obj_t vectorise_erf(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_erf_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_ERFC
|
||||
mp_obj_t vectorise_erfc(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_erfc_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_EXP
|
||||
mp_obj_t vectorise_exp(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_exp_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_EXPM1
|
||||
mp_obj_t vectorise_expm1(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_expm1_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_FLOOR
|
||||
mp_obj_t vectorise_floor(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_floor_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_GAMMA
|
||||
mp_obj_t vectorise_gamma(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_gamma_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_LGAMMA
|
||||
mp_obj_t vectorise_lgamma(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_lgamma_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_LOG
|
||||
mp_obj_t vectorise_log(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_log_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_LOG10
|
||||
mp_obj_t vectorise_log10(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_log10_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_LOG2
|
||||
mp_obj_t vectorise_log2(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_log2_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_SIN
|
||||
mp_obj_t vectorise_sin(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_sin_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_SINH
|
||||
mp_obj_t vectorise_sinh(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_sinh_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_SQRT
|
||||
mp_obj_t vectorise_sqrt(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_sqrt_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_TAN
|
||||
mp_obj_t vectorise_tan(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_tan_obj);
|
||||
#endif
|
||||
|
||||
#if ULAB_VECTORISE_TANH
|
||||
mp_obj_t vectorise_tanh(mp_obj_t );
|
||||
MP_DECLARE_CONST_FUN_OBJ_1(vectorise_tanh_obj);
|
||||
#endif
|
||||
mp_obj_module_t ulab_vectorise_module;
|
||||
|
||||
#define ITERATE_VECTOR(type, source, out) do {\
|
||||
type *input = (type *)(source)->array->items;\
|
||||
|
|
@ -141,6 +29,7 @@ MP_DECLARE_CONST_FUN_OBJ_1(vectorise_tanh_obj);
|
|||
#define MATH_FUN_1(py_name, c_name) \
|
||||
mp_obj_t vectorise_ ## py_name(mp_obj_t x_obj) { \
|
||||
return vectorise_generic_vector(x_obj, MICROPY_FLOAT_C_FUN(c_name)); \
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,4 +1,34 @@
|
|||
|
||||
Tue, 18 Feb 2020
|
||||
|
||||
version 0.34.0
|
||||
|
||||
split ulab into multiple modules
|
||||
|
||||
Sun, 16 Feb 2020
|
||||
|
||||
version 0.33.2
|
||||
|
||||
moved properties into ndarray_properties.h, implemented pointer arithmetic in fft.c to save some time
|
||||
|
||||
Fri, 14 Feb 2020
|
||||
|
||||
version 0.33.1
|
||||
|
||||
added the __name__attribute to all sub-modules
|
||||
|
||||
Thu, 13 Feb 2020
|
||||
|
||||
version 0.33.0
|
||||
|
||||
sub-modules are now proper sub-modules of ulab
|
||||
|
||||
Mon, 17 Feb 2020
|
||||
|
||||
version 0.32.1
|
||||
|
||||
temporary fix for issue #40
|
||||
|
||||
Tue, 11 Feb 2020
|
||||
|
||||
version 0.32.0
|
||||
|
|
|
|||
|
|
@ -293,11 +293,11 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 17,
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2020-02-10T18:34:14.611085Z",
|
||||
"start_time": "2020-02-10T18:34:14.607299Z"
|
||||
"end_time": "2020-02-16T14:53:49.098172Z",
|
||||
"start_time": "2020-02-16T14:53:49.093201Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
|
|
@ -311,11 +311,11 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 18,
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2020-02-10T18:34:15.995782Z",
|
||||
"start_time": "2020-02-10T18:34:15.975187Z"
|
||||
"end_time": "2020-02-16T14:53:53.396267Z",
|
||||
"start_time": "2020-02-16T14:53:53.375754Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
|
|
@ -384,13 +384,20 @@
|
|||
"ip.register_magics(PyboardMagic)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## pyboard"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 520,
|
||||
"execution_count": 111,
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2019-10-20T06:48:01.610725Z",
|
||||
"start_time": "2019-10-20T06:48:00.856261Z"
|
||||
"end_time": "2020-02-16T18:36:59.172039Z",
|
||||
"start_time": "2020-02-16T18:36:59.144651Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
|
|
@ -402,11 +409,11 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 501,
|
||||
"execution_count": 110,
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2019-10-19T13:36:42.010602Z",
|
||||
"start_time": "2019-10-19T13:36:42.003900Z"
|
||||
"end_time": "2020-02-16T17:34:35.250747Z",
|
||||
"start_time": "2020-02-16T17:34:35.241871Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
|
|
@ -575,21 +582,34 @@
|
|||
"source": [
|
||||
"## Customising `ulab`\n",
|
||||
"\n",
|
||||
"`ulab` implements a great number of functions, and it is quite possible that you do not need all of them in a particular application. If you want to save some flash space, you can easily exclude arbitrary functions from the firmware. The [https://github.com/v923z/micropython-ulab/blob/master/code/ulab.h](ulab.h) header file contains a pre-processor flag for all functions in `ulab`. The default setting is 1 for each of them, but if you change that to 0, the corresponding function will not be part of the compiled firmware. \n",
|
||||
"`ulab` implements a great number of functions, which are organised in sub-modules. E.g., functions related to Fourier transforms are located in the `ulab.fft` sub-module, so you would import `fft` as\n",
|
||||
"\n",
|
||||
"```python\n",
|
||||
"import ulab\n",
|
||||
"from ulab import fft\n",
|
||||
"```\n",
|
||||
"by which point you can get the FFT of your data by calling `fft.fft(...)`. \n",
|
||||
"\n",
|
||||
"The idea of such grouping of functions and methods is to provide a means for granularity: It is quite possible that you do not need all functions in a particular application. If you want to save some flash space, you can easily exclude arbitrary sub-modules from the firmware. The [https://github.com/v923z/micropython-ulab/blob/master/code/ulab.h](ulab.h) header file contains a pre-processor flag for each sub-module. The default setting is 1 for each of them, but if you change that to 0, the corresponding sub-module will not be part of the compiled firmware. \n",
|
||||
"\n",
|
||||
"The first couple of lines of the file look like this\n",
|
||||
"\n",
|
||||
"```c\n",
|
||||
"// vectorise (all functions) takes approx. 3 kB of flash space\n",
|
||||
"#define ULAB_VECTORISE_ACOS (1)\n",
|
||||
"#define ULAB_VECTORISE_ACOSH (1)\n",
|
||||
"#define ULAB_VECTORISE_ASIN (1)\n",
|
||||
"#define ULAB_VECTORISE_ASINH (1)\n",
|
||||
"#define ULAB_VECTORISE_ATAN (1)\n",
|
||||
"#define ULAB_VECTORISE_ATANH (1)\n",
|
||||
"#define ULAB_VECTORISE_MODULE (1)\n",
|
||||
"\n",
|
||||
"// linalg adds around 6 kB\n",
|
||||
"#define ULAB_LINALG_MODULE (1)\n",
|
||||
"\n",
|
||||
"// poly is approx. 2.5 kB\n",
|
||||
"#define ULAB_POLY_MODULE (1)\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"In order to simplify navigation in the file, each flag begins with `ULAB_`, continues with the sub-module, where the function itself is implemented, and ends with the function's name. Each section displays a hint as to how much space you can save by un-setting the flag."
|
||||
"In order to simplify navigation in the header, each flag begins with `ULAB_`, and continues with the name of the sub-module. This name is also the `.c` file, where the sub-module is implemented. So, e.g., the linear algebra routines can be found in `linalg.c`, and the corresponding compiler flag is `ULAB_LINALG_MODULE`. Each section displays a hint as to how much space you can save by un-setting the flag.\n",
|
||||
"\n",
|
||||
"At first, having to import everything in this way might appear to be overly complicated, but there is a very good reason behind all this: you can find out at the time of importing, whether a function is part of your `ulab` firmware, or not. The alternative, namely, that you do not have to import anything beyond `ulab`, could be catastrophic: you would learn only at run time that a particular function is not in the firmware, and that is most probably too late.\n",
|
||||
"\n",
|
||||
"The standard sub-modules, `vector`, `linalg`, `numerical`, `poly`, and `fft` are all `numpy`-compatible. User-defined functions that accept `ndarray`s as their argument should be implemented in the `extra` sub-module, or its sub-modules."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -692,13 +712,13 @@
|
|||
"source": [
|
||||
"# ndarray, the basic container\n",
|
||||
"\n",
|
||||
"The `ndarray` is the underlying container of numerical data. It is derived from micropython's own `array` object, but has a great number of extra features starting with how it can be initialised, how operations can be done on it, and which functions can accept it as an argument.\n",
|
||||
"The `ndarray` is the underlying container of numerical data. It is derived from micropython's own `array` object, but has a great number of extra features starting with how it can be initialised, which operations can be done on it, and which functions can accept it as an argument. One important property of an `ndarray` is that it is also a proper `micropython` iterable.\n",
|
||||
"\n",
|
||||
"Since the `ndarray` is a binary container, it is also compact, meaning that it takes only a couple of bytes of extra RAM in addition to what is required for storing the numbers themselves. `ndarray`s are also type-aware, i.e., one can save RAM by specifying a data type, and using the smallest reasonable one. Five such types are defined, namely `uint8`, `int8`, which occupy a single byte of memory per datum, `uint16`, and `int16`, which occupy two bytes per datum, and `float`, which occupies four or eight bytes per datum. The precision/size of the `float` type depends on the definition of `mp_float_t`. Some platforms, e.g., the PYBD, implement `double`s, but some, e.g., the pyboard.v.11, don't. You can find out, what type of float your particular platform implements by looking at the output of the [.rawsize](#.rawsize) class method.\n",
|
||||
"Since the `ndarray` is a binary container, it is also compact, meaning that it takes only a couple of bytes of extra RAM in addition to what is required for storing the numbers themselves. `ndarray`s are also type-aware, i.e., one can save RAM by specifying a data type, and using the smallest reasonable one. Five such types are defined, namely `uint8`, `int8`, which occupy a single byte of memory per datum, `uint16`, and `int16`, which occupy two bytes per datum, and `float`, which occupies four or eight bytes per datum. The precision/size of the `float` type depends on the definition of `mp_float_t`. Some platforms, e.g., the PYBD, implement `double`s, but some, e.g., the pyboard.v.11, don't. You can find out, what type of float your particular platform implements by looking at the output of the [.itemsize](#.itemsize) class property.\n",
|
||||
"\n",
|
||||
"On the following pages, we will see how one can work with `ndarray`s. Those familiar with `numpy` should find that the nomenclature and naming conventions of `numpy` are adhered to as closely as possible. I will point out the few differences, where necessary.\n",
|
||||
"\n",
|
||||
"For the sake of comparison, in addition to `ulab` code snippets, sometimes the equivalent `numpy` code is also presented. You can find out, where the snippet is supposed to run by looking at its first line, the header.\n",
|
||||
"For the sake of comparison, in addition to the `ulab` code snippets, sometimes the equivalent `numpy` code is also presented. You can find out, where the snippet is supposed to run by looking at its first line, the header.\n",
|
||||
"\n",
|
||||
"Hint: you can easily port existing `numpy` code, if you `import ulab as np`."
|
||||
]
|
||||
|
|
@ -3961,11 +3981,11 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 458,
|
||||
"execution_count": 114,
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2019-10-19T13:07:43.168629Z",
|
||||
"start_time": "2019-10-19T13:07:43.130341Z"
|
||||
"end_time": "2020-02-16T18:38:07.294862Z",
|
||||
"start_time": "2020-02-16T18:38:07.233842Z"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
|
|
@ -3973,13 +3993,13 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"real part:\t array([5119.996, -5.004663, -5.004798, ..., -5.005482, -5.005643, -5.006577], dtype=float)\n",
|
||||
"\n",
|
||||
"imaginary part:\t array([0.0, 1631.333, 815.659, ..., -543.764, -815.6588, -1631.333], dtype=float)\n",
|
||||
"\n",
|
||||
"real part:\t array([5119.996, -5.004663, -5.004798, ..., -5.005482, -5.005643, -5.006577], dtype=float)\n",
|
||||
"\n",
|
||||
"imaginary part:\t array([0.0, 1631.333, 815.659, ..., -543.764, -815.6588, -1631.333], dtype=float)\n",
|
||||
"real part:\t array([5119.996, -5.004663, -5.004798, ..., -5.005482, -5.005643, -5.006577], dtype=float)\r\n",
|
||||
"\r\n",
|
||||
"imaginary part:\t array([0.0, 1631.333, 815.659, ..., -543.764, -815.6588, -1631.333], dtype=float)\r\n",
|
||||
"\r\n",
|
||||
"real part:\t array([5119.996, -5.004663, -5.004798, ..., -5.005482, -5.005643, -5.006577], dtype=float)\r\n",
|
||||
"\r\n",
|
||||
"imaginary part:\t array([0.0, 1631.333, 815.659, ..., -543.764, -815.6588, -1631.333], dtype=float)\r\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
|
|
@ -3988,16 +4008,20 @@
|
|||
"%%micropython -pyboard 1\n",
|
||||
"\n",
|
||||
"import ulab as np\n",
|
||||
"from ulab import numerical\n",
|
||||
"from ulab import vector\n",
|
||||
"from ulab import fft\n",
|
||||
"from ulab import linalg\n",
|
||||
"\n",
|
||||
"x = np.linspace(0, 10, num=1024)\n",
|
||||
"y = np.sin(x)\n",
|
||||
"z = np.zeros(len(x))\n",
|
||||
"x = numerical.linspace(0, 10, num=1024)\n",
|
||||
"y = vector.sin(x)\n",
|
||||
"z = linalg.zeros(len(x))\n",
|
||||
"\n",
|
||||
"a, b = np.fft(x)\n",
|
||||
"a, b = fft.fft(x)\n",
|
||||
"print('real part:\\t', a)\n",
|
||||
"print('\\nimaginary part:\\t', b)\n",
|
||||
"\n",
|
||||
"c, d = np.fft(x, z)\n",
|
||||
"c, d = fft.fft(x, z)\n",
|
||||
"print('\\nreal part:\\t', c)\n",
|
||||
"print('\\nimaginary part:\\t', d)"
|
||||
]
|
||||
|
|
@ -4336,7 +4360,7 @@
|
|||
" NDARRAY_FLOAT = 'd',\n",
|
||||
"};\n",
|
||||
"```\n",
|
||||
"The ambiguity is caused by the fact that not all platforms implement `double`, and there one has to take `float`s. But you haven't actually got to be concerned by this, because at the very beginning of `ndarray.h`, this is already taken care of: the preprocessor figures out, what the `float` implementation of the hardware platform is, and defines the `NDARRAY_FLOAT` typecode accordingly. All you have to keep in mind is that wherever you would use `float` or `double`, you have to use `mp_float_t`. That type is defined in `py/mpconfig.h` of the micropython code base.\n",
|
||||
"The ambiguity is caused by the fact that not all platforms implement `double`, and there one has to take `float`s. But you haven't actually got to be concerned by this, because at the very beginning of `ndarray.h`, this is already taken care of: the pre-processor figures out, what the `float` implementation of the hardware platform is, and defines the `NDARRAY_FLOAT` typecode accordingly. All you have to keep in mind is that wherever you would use `float` or `double`, you have to use `mp_float_t`. That type is defined in `py/mpconfig.h` of the micropython code base.\n",
|
||||
"\n",
|
||||
"Therefore, a 4-by-5 matrix of type float can be created as\n",
|
||||
"\n",
|
||||
|
|
|
|||
5981
docs/ulab.ipynb
5981
docs/ulab.ipynb
File diff suppressed because it is too large
Load diff
|
|
@ -1,6 +1,2 @@
|
|||
try:
|
||||
import ulab
|
||||
except ImportError:
|
||||
raise SystemExit
|
||||
|
||||
print(ulab.eye(3))
|
||||
from ulab import linalg
|
||||
print(linalg.eye(3))
|
||||
|
|
|
|||
Loading…
Reference in a new issue