rebase from master

This commit is contained in:
Zoltán Vörös 2022-01-15 10:06:45 +01:00
commit b756c36f73
56 changed files with 2801 additions and 58 deletions

2
.gitignore vendored
View file

@ -9,3 +9,5 @@
/code/.atom-build.yml
build/micropython
build/ulab
.idea

View file

@ -45,3 +45,4 @@ make -C micropython/ports/unix -j${NPROC} axtls
make -C micropython/ports/unix -j${NPROC} USER_C_MODULES="${HERE}" DEBUG=1 STRIP=: MICROPY_PY_FFI=0 MICROPY_PY_BTREE=0 CFLAGS_EXTRA=-DULAB_MAX_DIMS=$dims BUILD=build-$dims PROG=micropython-$dims
bash test-common.sh "${dims}" "micropython/ports/unix/micropython-$dims"

View file

@ -1018,7 +1018,7 @@ STATIC uint8_t ndarray_init_helper(size_t n_args, const mp_obj_t *pos_args, mp_m
if(mp_obj_is_type(args[1].u_obj, &ulab_dtype_type)) {
dtype_obj_t *dtype = MP_OBJ_TO_PTR(args[1].u_obj);
_dtype = dtype->dtype;
} else { // this must be an integer defined as a class constant (ulba.uint8 etc.)
} else { // this must be an integer defined as a class constant (ulab.numpy.uint8 etc.)
_dtype = mp_obj_get_int(args[1].u_obj);
}
#else

View file

@ -136,6 +136,7 @@ void ndarray_assign_elements(ndarray_obj_t *, mp_obj_t , uint8_t , size_t *);
size_t *ndarray_contract_shape(ndarray_obj_t *, uint8_t );
int32_t *ndarray_contract_strides(ndarray_obj_t *, uint8_t );
ndarray_obj_t *ndarray_from_iterable(mp_obj_t , uint8_t );
ndarray_obj_t *ndarray_new_dense_ndarray(uint8_t , size_t *, uint8_t );
ndarray_obj_t *ndarray_new_ndarray_from_tuple(mp_obj_tuple_t *, uint8_t );
ndarray_obj_t *ndarray_new_ndarray(uint8_t , size_t *, int32_t *, uint8_t );

View file

@ -17,8 +17,8 @@
#include "py/obj.h"
#include "py/runtime.h"
#include "../ulab.h"
#include "create.h"
#include "../ulab.h"
#include "../ulab_tools.h"
#if ULAB_NUMPY_HAS_ONES | ULAB_NUMPY_HAS_ZEROS | ULAB_NUMPY_HAS_FULL | ULAB_NUMPY_HAS_EMPTY
@ -166,6 +166,60 @@ mp_obj_t create_arange(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_arg
MP_DEFINE_CONST_FUN_OBJ_KW(create_arange_obj, 1, create_arange);
#endif
#if ULAB_NUMPY_HAS_ASARRAY
mp_obj_t create_asarray(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 } },
{ MP_QSTR_dtype, MP_ARG_KW_ONLY | MP_ARG_OBJ, { .u_obj = mp_const_none } },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
uint8_t _dtype;
#if ULAB_HAS_DTYPE_OBJECT
if(mp_obj_is_type(args[1].u_obj, &ulab_dtype_type)) {
dtype_obj_t *dtype = MP_OBJ_TO_PTR(args[1].u_obj);
_dtype = dtype->dtype;
} else { // this must be an integer defined as a class constant (ulab.numpy.uint8 etc.)
if(args[1].u_obj == mp_const_none) {
_dtype = 0;
} else {
_dtype = mp_obj_get_int(args[1].u_obj);
}
}
#else
if(args[1].u_obj == mp_const_none) {
_dtype = 0;
} else {
_dtype = mp_obj_get_int(args[1].u_obj);
}
#endif
if(ulab_tools_mp_obj_is_scalar(args[0].u_obj)) {
return args[0].u_obj;
} else if(mp_obj_is_type(args[0].u_obj, &ulab_ndarray_type)) {
ndarray_obj_t *ndarray = MP_OBJ_TO_PTR(args[0].u_obj);
if((_dtype == ndarray->dtype) || (_dtype == 0)) {
return args[0].u_obj;
} else {
return MP_OBJ_FROM_PTR(ndarray_copy_view_convert_type(ndarray, _dtype));
}
} else if(ndarray_object_is_array_like(args[0].u_obj)) {
if(_dtype == 0) {
_dtype = NDARRAY_FLOAT;
}
return MP_OBJ_FROM_PTR(ndarray_from_iterable(args[0].u_obj, _dtype));
} else {
mp_raise_TypeError(translate("wrong input type"));
}
return mp_const_none; // this should never happen
}
MP_DEFINE_CONST_FUN_OBJ_KW(create_asarray_obj, 1, create_asarray);
#endif
#if ULAB_NUMPY_HAS_CONCATENATE
//| def concatenate(arrays: Tuple[ulab.numpy.ndarray], *, axis: int = 0) -> ulab.numpy.ndarray:
//| """

View file

@ -20,6 +20,11 @@ mp_obj_t create_arange(size_t , const mp_obj_t *, mp_map_t *);
MP_DECLARE_CONST_FUN_OBJ_KW(create_arange_obj);
#endif
#if ULAB_NUMPY_HAS_ASARRAY
mp_obj_t create_arange(size_t , const mp_obj_t *, mp_map_t *);
MP_DECLARE_CONST_FUN_OBJ_KW(create_asarray_obj);
#endif
#if ULAB_NUMPY_HAS_CONCATENATE
mp_obj_t create_concatenate(size_t , const mp_obj_t *, mp_map_t *);
MP_DECLARE_CONST_FUN_OBJ_KW(create_concatenate_obj);

View file

@ -235,6 +235,9 @@ static const mp_rom_map_elem_t ulab_numpy_globals_table[] = {
#if ULAB_NUMPY_HAS_ARGSORT
{ MP_OBJ_NEW_QSTR(MP_QSTR_argsort), (mp_obj_t)&numerical_argsort_obj },
#endif
#if ULAB_NUMPY_HAS_ASARRAY
{ MP_OBJ_NEW_QSTR(MP_QSTR_asarray), (mp_obj_t)&create_asarray_obj },
#endif
#if ULAB_NUMPY_HAS_CROSS
{ MP_OBJ_NEW_QSTR(MP_QSTR_cross), (mp_obj_t)&numerical_cross_obj },
#endif
@ -275,6 +278,9 @@ static const mp_rom_map_elem_t ulab_numpy_globals_table[] = {
#if ULAB_NUMPY_HAS_SAVE
{ MP_OBJ_NEW_QSTR(MP_QSTR_save), (mp_obj_t)&io_save_obj },
#endif
#if ULAB_NUMPY_HAS_SIZE
{ MP_OBJ_NEW_QSTR(MP_QSTR_size), (mp_obj_t)&transform_size_obj },
#endif
#if ULAB_NUMPY_HAS_SORT
{ MP_OBJ_NEW_QSTR(MP_QSTR_sort), (mp_obj_t)&numerical_sort_obj },
#endif

View file

@ -399,5 +399,42 @@ mp_obj_t transform_dot(mp_obj_t _m1, mp_obj_t _m2) {
}
MP_DEFINE_CONST_FUN_OBJ_2(transform_dot_obj, transform_dot);
#endif /* ULAB_NUMPY_HAS_DOT */
#endif /* ULAB_MAX_DIMS > 1 */
#if ULAB_NUMPY_HAS_SIZE
static mp_obj_t transform_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 } },
{ MP_QSTR_axis, MP_ARG_KW_ONLY | MP_ARG_OBJ, { .u_rom_obj = mp_const_none } },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
if(ulab_tools_mp_obj_is_scalar(args[0].u_obj)) {
return mp_obj_new_int(1);
}
if(!ndarray_object_is_array_like(args[0].u_obj)) {
mp_raise_TypeError(translate("first argument must be an ndarray"));
}
if(!mp_obj_is_type(args[0].u_obj, &ulab_ndarray_type)) {
return mp_obj_len_maybe(args[0].u_obj);
}
// at this point, the args[0] is most certainly an ndarray
ndarray_obj_t *ndarray = MP_OBJ_TO_PTR(args[0].u_obj);
mp_obj_t axis = args[1].u_obj;
size_t len;
if(axis != mp_const_none) {
int8_t ax = tools_get_axis(axis, ndarray->ndim);
len = ndarray->shape[ULAB_MAX_DIMS - ndarray->ndim + ax];
} else {
len = ndarray->len;
}
return mp_obj_new_int(len);
}
MP_DEFINE_CONST_FUN_OBJ_KW(transform_size_obj, 1, transform_size);
#endif
#endif

View file

@ -25,5 +25,6 @@
MP_DECLARE_CONST_FUN_OBJ_KW(transform_compress_obj);
MP_DECLARE_CONST_FUN_OBJ_KW(transform_delete_obj);
MP_DECLARE_CONST_FUN_OBJ_2(transform_dot_obj);
MP_DECLARE_CONST_FUN_OBJ_KW(transform_size_obj);
#endif

View file

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

View file

@ -426,6 +426,10 @@
#define ULAB_NUMPY_HAS_ARGSORT (1)
#endif
#ifndef ULAB_NUMPY_HAS_ASARRAY
#define ULAB_NUMPY_HAS_ASARRAY (1)
#endif
#ifndef ULAB_NUMPY_HAS_COMPRESS
#define ULAB_NUMPY_HAS_COMPRESS (1)
#endif
@ -490,6 +494,10 @@
#define ULAB_NUMPY_HAS_SAVE (1)
#endif
#ifndef ULAB_NUMPY_HAS_SIZE
#define ULAB_NUMPY_HAS_SIZE (1)
#endif
#ifndef ULAB_NUMPY_HAS_SORT
#define ULAB_NUMPY_HAS_SORT (1)
#endif

View file

@ -257,4 +257,20 @@ void ulab_rescale_float_strides(int32_t *strides) {
strides[i] /= sz;
}
}
#endif
#endif
bool ulab_tools_mp_obj_is_scalar(mp_obj_t obj) {
#if ULAB_SUPPORTS_COMPLEX
if(mp_obj_is_int(obj) || mp_obj_is_float(obj) || mp_obj_is_type(obj, &mp_type_complex)) {
return true;
} else {
return false;
}
#else
if(mp_obj_is_int(obj) || mp_obj_is_float(obj)) {
return true;
} else {
return false;
}
#endif
}

View file

@ -42,4 +42,5 @@ uint8_t ulab_binary_get_size(uint8_t );
void ulab_rescale_float_strides(int32_t *);
#endif
bool ulab_tools_mp_obj_is_scalar(mp_obj_t );
#endif

View file

@ -27,7 +27,7 @@ copyright = '2019-2022, Zoltán Vörös and contributors'
author = 'Zoltán Vörös'
# The full version, including alpha/beta/rc tags
release = '4.2.0'
release = '4.3.0'
# -- General configuration ---------------------------------------------------

View file

@ -11,39 +11,41 @@ the firmware was compiled with complex support.
3. `numpy.argmax <#argmax>`__
4. `numpy.argmin <#argmin>`__
5. `numpy.argsort <#argsort>`__
6. `numpy.clip <#clip>`__
7. `numpy.compress\* <#compress>`__
8. `numpy.conjugate\* <#conjugate>`__
9. `numpy.convolve\* <#convolve>`__
10. `numpy.delete <#delete>`__
11. `numpy.diff <#diff>`__
12. `numpy.dot <#dot>`__
13. `numpy.equal <#equal>`__
14. `numpy.flip\* <#flip>`__
15. `numpy.imag\* <#imag>`__
16. `numpy.interp <#interp>`__
17. `numpy.isfinite <#isfinite>`__
18. `numpy.isinf <#isinf>`__
19. `numpy.load <#load>`__
20. `numpy.max <#max>`__
21. `numpy.maximum <#maximum>`__
22. `numpy.mean <#mean>`__
23. `numpy.median <#median>`__
24. `numpy.min <#min>`__
25. `numpy.minimum <#minimum>`__
26. `numpy.not_equal <#equal>`__
27. `numpy.polyfit <#polyfit>`__
28. `numpy.polyval <#polyval>`__
29. `numpy.real\* <#real>`__
30. `numpy.roll <#roll>`__
31. `numpy.save <#save>`__
32. `numpy.sort <#sort>`__
33. `numpy.sort_complex\* <#sort_complex>`__
34. `numpy.std <#std>`__
35. `numpy.sum <#sum>`__
36. `numpy.trace <#trace>`__
37. `numpy.trapz <#trapz>`__
38. `numpy.where <#where>`__
6. `numpy.asarray\* <#asarray>`__
7. `numpy.clip <#clip>`__
8. `numpy.compress\* <#compress>`__
9. `numpy.conjugate\* <#conjugate>`__
10. `numpy.convolve\* <#convolve>`__
11. `numpy.delete <#delete>`__
12. `numpy.diff <#diff>`__
13. `numpy.dot <#dot>`__
14. `numpy.equal <#equal>`__
15. `numpy.flip\* <#flip>`__
16. `numpy.imag\* <#imag>`__
17. `numpy.interp <#interp>`__
18. `numpy.isfinite <#isfinite>`__
19. `numpy.isinf <#isinf>`__
20. `numpy.load <#load>`__
21. `numpy.max <#max>`__
22. `numpy.maximum <#maximum>`__
23. `numpy.mean <#mean>`__
24. `numpy.median <#median>`__
25. `numpy.min <#min>`__
26. `numpy.minimum <#minimum>`__
27. `numpy.not_equal <#equal>`__
28. `numpy.polyfit <#polyfit>`__
29. `numpy.polyval <#polyval>`__
30. `numpy.real\* <#real>`__
31. `numpy.roll <#roll>`__
32. `numpy.save <#save>`__
33. `numpy.size <#size>`__
34. `numpy.sort <#sort>`__
35. `numpy.sort_complex\* <#sort_complex>`__
36. `numpy.std <#std>`__
37. `numpy.sum <#sum>`__
38. `numpy.trace <#trace>`__
39. `numpy.trapz <#trapz>`__
40. `numpy.where <#where>`__
all
---
@ -271,6 +273,53 @@ example:
asarray
-------
``numpy``:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.asarray.html
The function takes a single positional argument, and an optional keyword
argument, ``dtype``, with a default value of ``None``.
If the positional argument is an ``ndarray``, and its ``dtypes`` is
identical to the value of the keyword argument, or if the keyword
argument is ``None``, then the positional argument is simply returned.
If the original ``dtype``, and the value of the keyword argument are
different, then a copy is returned, with appropriate ``dtype``
conversion.
If the positional argument is an iterable, then the function is simply
an alias for ``array``.
.. code::
# code to be run in micropython
from ulab import numpy as np
a = np.array(range(9), dtype=np.uint8)
b = np.asarray(a)
c = np.asarray(a, dtype=np.int8)
print('a:{}'.format(a))
print('b:{}'.format(b))
print('a == b: {}'.format(a is b))
print('\nc:{}'.format(c))
print('a == c: {}'.format(a is c))
.. parsed-literal::
a:array([0, 1, 2, 3, 4, 5, 6, 7, 8], dtype=uint8)
b:array([0, 1, 2, 3, 4, 5, 6, 7, 8], dtype=uint8)
a == b: True
c:array([0, 1, 2, 3, 4, 5, 6, 7, 8], dtype=int8)
a == c: False
clip
----
@ -1429,17 +1478,41 @@ format <https://numpy.org/doc/stable/reference/generated/numpy.lib.format.html#m
The function takes two positional arguments, the name of the output
file, and the array.
.. code::
# code to be run in CPython
a = np.array(range(25)).reshape((5, 5))
np.save('a.npy', a)
size
----
The function takes a single positional argument, and an optional keyword
argument, ``axis``, with a default value of ``None``, and returns the
size of an array along that axis. If ``axis`` is ``None``, the total
length of the array (the product of the elements of its shape) is
returned.
.. code::
# code to be run in micropython
from ulab import numpy as np
a = np.array(range(25)).reshape((5, 5))
np.save('a.npy', a)
a = np.ones((2, 3))
print(a)
print('size(a, axis=0): ', np.size(a, axis=0))
print('size(a, axis=1): ', np.size(a, axis=1))
print('size(a, axis=None): ', np.size(a, axis=None))
.. parsed-literal::
array([[1.0, 1.0, 1.0],
[1.0, 1.0, 1.0]], dtype=float64)
size(a, axis=0): 2
size(a, axis=1): 3
size(a, axis=None): 6

View file

@ -34,8 +34,8 @@
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2022-01-12T16:41:02.299473Z",
"start_time": "2022-01-12T16:41:02.282389Z"
"end_time": "2022-01-15T08:50:03.152522Z",
"start_time": "2022-01-15T08:50:03.141317Z"
}
},
"outputs": [],
@ -52,8 +52,8 @@
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2022-01-12T16:41:02.475299Z",
"start_time": "2022-01-12T16:41:02.401569Z"
"end_time": "2022-01-15T08:50:04.183008Z",
"start_time": "2022-01-15T08:50:04.162758Z"
}
},
"outputs": [],
@ -237,6 +237,7 @@
"1. [numpy.argmax](#argmax)\n",
"1. [numpy.argmin](#argmin)\n",
"1. [numpy.argsort](#argsort)\n",
"1. [numpy.asarray*](#asarray)\n",
"1. [numpy.clip](#clip)\n",
"1. [numpy.compress*](#compress)\n",
"1. [numpy.conjugate*](#conjugate)\n",
@ -263,6 +264,7 @@
"1. [numpy.real*](#real)\n",
"1. [numpy.roll](#roll)\n",
"1. [numpy.save](#save)\n",
"1. [numpy.size](#size)\n",
"1. [numpy.sort](#sort)\n",
"1. [numpy.sort_complex*](#sort_complex)\n",
"1. [numpy.std](#std)\n",
@ -545,6 +547,62 @@
"print('\\nthe original array:\\n', a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## asarray\n",
"\n",
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.asarray.html\n",
"\n",
"The function takes a single positional argument, and an optional keyword argument, `dtype`, with a default value of `None`. \n",
"\n",
"If the positional argument is an `ndarray`, and its `dtypes` is identical to the value of the keyword argument, or if the keyword argument is `None`, then the positional argument is simply returned. If the original `dtype`, and the value of the keyword argument are different, then a copy is returned, with appropriate `dtype` conversion. \n",
"\n",
"If the positional argument is an iterable, then the function is simply an alias for `array`."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2022-01-14T20:05:22.017031Z",
"start_time": "2022-01-14T20:05:22.002463Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a:array([0, 1, 2, 3, 4, 5, 6, 7, 8], dtype=uint8)\n",
"b:array([0, 1, 2, 3, 4, 5, 6, 7, 8], dtype=uint8)\n",
"a == b: True\n",
"\n",
"c:array([0, 1, 2, 3, 4, 5, 6, 7, 8], dtype=int8)\n",
"a == c: False\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"from ulab import numpy as np\n",
"\n",
"a = np.array(range(9), dtype=np.uint8)\n",
"b = np.asarray(a)\n",
"c = np.asarray(a, dtype=np.int8)\n",
"print('a:{}'.format(a))\n",
"print('b:{}'.format(b))\n",
"print('a == b: {}'.format(a is b))\n",
"\n",
"print('\\nc:{}'.format(c))\n",
"print('a == c: {}'.format(a is c))"
]
},
{
"cell_type": "markdown",
"metadata": {},
@ -2000,13 +2058,37 @@
"The function takes two positional arguments, the name of the output file, and the array. "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2022-01-15T08:51:08.827144Z",
"start_time": "2022-01-15T08:51:08.813813Z"
}
},
"outputs": [],
"source": [
"a = np.array(range(25)).reshape((5, 5))\n",
"np.save('a.npy', a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## size\n",
"\n",
"The function takes a single positional argument, and an optional keyword argument, `axis`, with a default value of `None`, and returns the size of an array along that axis. If `axis` is `None`, the total length of the array (the product of the elements of its shape) is returned."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2022-01-12T19:10:15.861415Z",
"start_time": "2022-01-12T19:10:15.852451Z"
"end_time": "2022-01-15T08:50:57.254168Z",
"start_time": "2022-01-15T08:50:57.245772Z"
}
},
"outputs": [
@ -2014,6 +2096,11 @@
"name": "stdout",
"output_type": "stream",
"text": [
"array([[1.0, 1.0, 1.0],\n",
" [1.0, 1.0, 1.0]], dtype=float64)\n",
"size(a, axis=0): 2\n",
"size(a, axis=1): 3\n",
"size(a, axis=None): 6\n",
"\n",
"\n"
]
@ -2024,8 +2111,12 @@
"\n",
"from ulab import numpy as np\n",
"\n",
"a = np.array(range(25)).reshape((5, 5))\n",
"np.save('a.npy', a)"
"a = np.ones((2, 3))\n",
"\n",
"print(a)\n",
"print('size(a, axis=0): ', np.size(a, axis=0))\n",
"print('size(a, axis=1): ', np.size(a, axis=1))\n",
"print('size(a, axis=None): ', np.size(a, axis=None))"
]
},
{

View file

@ -1,3 +1,16 @@
Sat, 8 Jan 2022
version 4.3.0
implement numpy.save, numpy.load
Fri, 14 Jan 2022
version 4.2.0
add numpy.size, asarray
Wed, 12 Jan 2022
version 4.2.0

View file

@ -17,8 +17,8 @@
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2022-01-12T17:00:33.582729Z",
"start_time": "2022-01-12T17:00:33.566591Z"
"end_time": "2022-01-15T08:48:23.883953Z",
"start_time": "2022-01-15T08:48:23.877040Z"
}
},
"outputs": [
@ -61,7 +61,7 @@
"author = 'Zoltán Vörös'\n",
"\n",
"# The full version, including alpha/beta/rc tags\n",
"release = '4.2.0'\n",
"release = '4.3.0'\n",
"\n",
"\n",
"# -- General configuration ---------------------------------------------------\n",
@ -215,11 +215,11 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2022-01-12T17:03:49.038101Z",
"start_time": "2022-01-12T17:03:48.886617Z"
"end_time": "2022-01-15T08:48:32.207113Z",
"start_time": "2022-01-15T08:48:32.051714Z"
}
},
"outputs": [],
@ -256,11 +256,11 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2022-01-12T17:03:52.084601Z",
"start_time": "2022-01-12T17:03:50.354118Z"
"end_time": "2022-01-15T08:52:20.686225Z",
"start_time": "2022-01-15T08:52:16.125014Z"
}
},
"outputs": [],

View file

@ -0,0 +1,85 @@
# This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab
#
# The MIT License (MIT)
#
# Copyright (c) 2022 Zoltán Vörös
import sys
use_ulab = False
try:
from ubinascii import a2b_base64 as b64decode
from ubinascii import unhexlify
import ujson as json
from ulab import numpy as np
use_ulab = True
except:
from base64 import b64decode
import json
import numpy as np
from numpy.lib.format import descr_to_dtype
def ulab_descr_to_dtype(descriptor):
descriptor = descriptor[1:]
if descriptor == 'u1':
return np.uint8
elif descriptor == 'i1':
return np.int8
if descriptor == 'u2':
return np.uint16
if descriptor == 'i2':
return np.int16
elif descriptor == 'f8':
if np.float != ord('d'):
raise TypeError('doubles are not supported')
else:
return np.float
elif descriptor == 'f16':
if np.float != ord('f'):
raise TypeError('')
else:
return np.float
else:
raise TypeError('descriptor could not be decoded')
def json_to_ndarray(json_string, b64=True):
"""
Turn a json string into an ndarray
The string must be the representation of a dictionary with the three keys
- dtype: a valid numpy dtype string (one of |u1, |i1, <u2, <i2, <f4, <f8, <c8, <c16, >u2, >i2, >f4, >f8, >c8, >c16)
- array: the hexified, or base64-encoded raw data array
- shape: the shape of the array (a list or tuple of integers)
Usage:
str = '{"dtype": "<f8", "array": "AAAAAAAAAAAAAAAAAADwPwAAAAAAAABAAAAAAAAACEAAAAAAAAAQQAAAAAAAABRAAAAAAAAAGEAAAAAAAAAcQAAAAAAAACBA\n", "shape": [3, 3]}'
json_to_ndarray(str, b64=True)
"""
obj = json.loads(json_string)
print(obj)
if not isinstance(obj, dict):
raise TypeError('input argument must be a dictionary')
if set(obj.keys()) != {'array', 'dtype', 'shape'}:
raise ValueError('input must have the keys "array", "dtype", "shape"')
descriptor = obj['dtype']
if use_ulab:
dtype = ulab_descr_to_dtype(descriptor)
else:
dtype = descr_to_dtype(descriptor)
if not b64:
data = unhexlify(obj['array'])
else:
data = b64decode(obj['array'])
ndarray = np.frombuffer(data, dtype=dtype).reshape(tuple(obj['shape']))
if dtype in (np.uint16, np.int16, np.float):
if sys.byteorder != descriptor[1]:
ndarray.byteswap()
return ndarray

View file

@ -0,0 +1,74 @@
# This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab
#
# The MIT License (MIT)
#
# Copyright (c) 2022 Zoltán Vörös
import sys
use_ulab = False
try:
from ubinascii import b2a_base64 as b64encode
from ubinascii import hexlify
import ujson as json
from ulab import numpy as np
use_ulab = True
except:
from base64 import b64encode
import json
import numpy as np
from numpy.lib.format import dtype_to_descr
def ulab_dtype_to_descr(dtype):
desc = '>'
if sys.byteorder == 'little':
desc = '<'
if dtype == ord('B'):
desc = '|u1'
elif dtype == ord('b'):
desc = '|i1'
elif dtype == ord('H'):
desc = desc + 'u2'
elif dtype == ord('h'):
desc = desc + 'i2'
elif dtype == ord('d'):
desc = desc + 'f8'
elif dtype == ord('f'):
desc = desc + 'f4'
elif dtype == ord('c'):
desc = desc + 'c16'
if np.array([1], dtype=np.float).itemsize == 4:
desc = desc + 'c8'
return desc
def ndarray_to_json(obj, b64=True):
"""
Turn an ndarray into a json string, using either base64 encoding or hexify
Returns a serialised dictionary with three keys:
- dtype: a valid numpy dtype string (one of |u1, |i1, <u2, <i2, <f4, <f8, <c8, <c16, >u2, >i2, >f4, >f8, >c8, >c16)
- array: the hexified, or base64-encoded raw data array
- shape: the shape of the array (a list or tuple of integers)
Usage:
ndarray = np.array([1, 2, 3], dtype=np.uint8)
ndarray_to_json(ndarray, b64=True)
"""
if not isinstance(obj, np.ndarray):
raise TypeError('input argument must be an ndarray')
if use_ulab:
dtype_desciptor = ulab_dtype_to_descr(obj.dtype)
else:
dtype_desciptor = dtype_to_descr(obj.dtype)
if not b64:
data = hexlify(obj.tobytes())
else:
data = b64encode(obj.tobytes())
return json.dumps({'array': data, 'dtype': dtype_desciptor, 'shape': obj.shape})

View file

@ -0,0 +1,5 @@
from . import core
from .core import *
from . import lib
from .lib import *

View file

@ -0,0 +1,5 @@
from .multiarray import *
from .numeric import *
from .fromnumeric import *
from .shape_base import *

View file

@ -0,0 +1,83 @@
# This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab
#
# The MIT License (MIT)
#
# Copyright (c) 2022 Phil Jepsen
from .overrides import set_module
from .multiarray import asarray
from ulab import numpy as np
from ... import numpy
def prod(arr):
result = 1
for x in arr:
result = result * x
return result
def size(a, axis=None):
"""
Return the number of elements along a given axis.
Parameters
----------
a : array_like
Input data.
axis : int, optional
Axis along which the elements are counted. By default, give
the total number of elements.
Returns
-------
element_count : int
Number of elements along the specified axis.
See Also
--------
shape : dimensions of array
ndarray.shape : dimensions of array
ndarray.size : number of elements in array
Examples
--------
>>> a = np.array([[1,2,3],[4,5,6]])
>>> np.size(a)
6
>>> np.size(a,1)
3
>>> np.size(a,0)
2
"""
if axis is None:
try:
return a.size
except AttributeError:
return asarray(a).size
else:
try:
return a.shape[axis]
except AttributeError:
return asarray(a).shape[axis]
def nonzero(a):
if not isinstance(a,(np.ndarray)):
a = asarray(a)
x = a.shape
row = x[0]
if len(x) == 1:
column = 0
else:
column = x[1]
nonzero_row = np.array([],dtype=np.float)
nonzero_col = np.array([],dtype=np.float)
if column == 0:
for i in range(0,row):
if a[i] != 0:
nonzero_row = numpy.append(nonzero_row,i)
return (np.array(nonzero_row, dtype=np.int8),)
for i in range(0,row):
for j in range(0,column):
if a[i,j] != 0:
nonzero_row = numpy.append(nonzero_row,i)
nonzero_col = numpy.append(nonzero_col,j)
return (np.array(nonzero_row, dtype=np.int8), np.array(nonzero_col, dtype=np.int8))

View file

@ -0,0 +1,27 @@
# This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab
#
# The MIT License (MIT)
#
# Copyright (c) 2022 Phil Jepsen
from ulab import numpy as np
def asarray(a, dtype=None):
if isinstance(a,(np.ndarray)):
return a
try:
if dtype is not None:
a = np.array([a], dtype=dtype)
elif isinstance(a, list) or isinstance(a, tuple):
a = np.array(a)
else:
a = np.array([a])
return a
except Exception as e:
if "can't convert complex to float" in e.args or "'complex' object isn't iterable" in e.args:
try:
a = np.array([a], dtype=np.complex).flatten()
return a
except:
pass
raise ValueError('Could not cast %s to array' % (a))

View file

@ -0,0 +1,65 @@
# This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab
#
# The MIT License (MIT)
#
# Copyright (c) 2022 Phil Jepsen
from ulab import numpy as np
from .multiarray import (asarray)
def zeros_like(a, dtype=None, order='K', subok=True, shape=None):
"""
Return an array of zeros with the same shape and type as a given array.
Parameters
----------
a : array_like
The shape and data-type of `a` define these same attributes of
the returned array.
dtype : data-type, optional
Overrides the data type of the result.
.. versionadded:: 1.6.0
order : {'C', 'F', 'A', or 'K'}, optional
Overrides the memory layout of the result. 'C' means C-order,
'F' means F-order, 'A' means 'F' if `a` is Fortran contiguous,
'C' otherwise. 'K' means match the layout of `a` as closely
as possible.
.. versionadded:: 1.6.0
subok : bool, optional.
If True, then the newly created array will use the sub-class
type of `a`, otherwise it will be a base-class array. Defaults
to True.
shape : int or sequence of ints, optional.
Overrides the shape of the result. If order='K' and the number of
dimensions is unchanged, will try to keep order, otherwise,
order='C' is implied.
.. versionadded:: 1.17.0
Returns
-------
out : ndarray
Array of zeros with the same shape and type as `a`.
See Also
--------
empty_like : Return an empty array with shape and type of input.
ones_like : Return an array of ones with shape and type of input.
full_like : Return a new array with shape of input filled with value.
zeros : Return a new array setting values to zero.
Examples
--------
>>> x = np.arange(6)
>>> x = x.reshape((2, 3))
>>> x
array([[0, 1, 2],
[3, 4, 5]])
>>> np.zeros_like(x)
array([[0, 0, 0],
[0, 0, 0]])
>>> y = np.arange(3, dtype=float)
>>> y
array([0., 1., 2.])
>>> np.zeros_like(y)
array([0., 0., 0.])
"""
res = np.full(a.shape, 0, dtype=a.dtype)
return res

View file

@ -0,0 +1,22 @@
# This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab
#
# The MIT License (MIT)
#
# Copyright (c) 2022 Phil Jepsen
import sys
def set_module(module):
"""Decorator for overriding __module__ on a function or class.
Example usage::
@set_module('numpy')
def example():
pass
assert example.__module__ == 'numpy'
"""
def decorator(func):
if module is not None:
sys.modules[func.__globals__['__name__']] = module
return func
return decorator

View file

@ -0,0 +1,22 @@
# This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab
#
# The MIT License (MIT)
#
# Copyright (c) 2022 Phil Jepsen
from ulab import numpy as np
from .multiarray import asarray
def atleast_1d(*arys):
res = []
for ary in arys:
ary = asarray(ary)
if not isinstance(ary,(np.ndarray)):
result = ary.reshape((1,))
else:
result = ary
res.append(result)
if len(res) == 1:
return res[0]
else:
return res

View file

@ -0,0 +1,4 @@
from .function_base import *
from .polynomial import *
from .type_check import *

View file

@ -0,0 +1,20 @@
# This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab
#
# The MIT License (MIT)
#
# Copyright (c) 2022 Phil Jepsen
from ulab import numpy as np
from ..core.multiarray import (asarray)
from ..core.overrides import set_module
@set_module('numpy')
def append(arr, values, axis=None):
arr = asarray(arr)
values = asarray(values)
if axis is None:
if len(arr.shape) != 1:
arr = arr.flatten()
values = values.flatten()
axis = len(arr.shape)-1
return np.concatenate((arr, values), axis=axis)

View file

@ -0,0 +1,42 @@
# This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab
#
# The MIT License (MIT)
#
# Copyright (c) 2022 Phil Jepsen
from ..core import (atleast_1d, asarray)
from ..core.overrides import set_module
from ulab import numpy as np
@set_module('numpy')
def poly(seq_of_zeros):
seq_of_zeros = atleast_1d(seq_of_zeros)
sh = seq_of_zeros.shape
if len(sh) == 2 and sh[0] == sh[1] and sh[0] != 0:
seq_of_zeros = np.linalg.eig(seq_of_zeros)
elif len(sh) == 1:
dt = seq_of_zeros.dtype
# Let object arrays slip through, e.g. for arbitrary precision
if dt != object:
seq_of_zeros = seq_of_zeros #seq_of_zeros.astype(mintypecode(dt.char))
else:
raise ValueError("input must be 1d or non-empty square 2d array.")
if len(seq_of_zeros) == 0:
return 1.0
dt = seq_of_zeros.dtype
a = np.ones((1,), dtype=dt)
for k in range(len(seq_of_zeros)):
a = np.convolve(a, np.array([1, -seq_of_zeros[k]], dtype=dt))
if a.dtype == np.complex:
# if complex roots are all complex conjugates, the roots are real.
roots = asarray(seq_of_zeros, complex)
p = np.sort_complex(roots)
c = np.real(p) - np.imag(p) * 1j
q = np.sort_complex(c)
if np.all(p == q):
a = a.real.copy()
return a

View file

@ -0,0 +1,70 @@
from ulab import numpy as np
from ..core.multiarray import (asarray)
from ..core.overrides import set_module
@set_module('numpy')
# This file is part of the micropython-ulab project, https://github.com/v923z/micropython-ulab
#
# The MIT License (MIT)
#
# Copyright (c) 2022 Phil Jepsen
def _isreal(a):
result = []
for x in a:
if isinstance(x, float):
result.append(True)
elif isinstance(x, complex) and x.imag == 0:
result.append(True)
else:
result.append(False)
return result
def isreal(x):
"""
Returns a bool array, where True if input element is real.
If element has complex type with zero complex part, the return value
for that element is True.
Parameters
----------
x : array_like
Input array.
Returns
-------
out : ndarray, bool
Boolean array of same shape as `x`.
Notes
-----
`isreal` may behave unexpectedly for string or object arrays (see examples)
See Also
--------
iscomplex
isrealobj : Return True if x is not a complex type.
Examples
--------
>>> a = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j], dtype=complex)
>>> np.isreal(a)
array([False, True, True, True, True, False])
The function does not work on string arrays.
>>> a = np.array([2j, "a"], dtype="U")
>>> np.isreal(a) # Warns about non-elementwise comparison
False
Returns True for all elements in input array of ``dtype=object`` even if
any of the elements is complex.
>>> a = np.array([1, "2", 3+4j], dtype=object)
>>> np.isreal(a)
array([ True, True, True])
isreal should not be used with object arrays
>>> a = np.array([1+2j, 2+1j], dtype=object)
>>> np.isreal(a)
array([ True, True])
"""
x = asarray(x)
result = _isreal(x)
return result if len(result) > 1 else result[0]

View file

@ -0,0 +1,3 @@
from . import signal
from .signal import *

View file

@ -0,0 +1,2 @@
from .filter_design import *

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,27 @@
import math
import sys
sys.path.append('.')
from snippets import numpy
from ulab import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print(numpy.size(a))
print(numpy.size(a,1))
print(numpy.size(a,0))
print(numpy.prod([1, 10, 100, 5]))
print(numpy.prod([]))
print(numpy.prod([1.,2.]))
a = np.array([1,2,3])
print(numpy.nonzero(a))
b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(numpy.nonzero(b > 3))
c = np.array([0,1,0,-1])
print(numpy.nonzero(c > 0))
print(numpy.nonzero(c < 0))

View file

@ -0,0 +1,10 @@
6
3
2
5000
1
2.0
(array([0, 1, 2], dtype=int8),)
(array([1, 1, 1, 2, 2, 2], dtype=int8), array([0, 1, 2, 0, 1, 2], dtype=int8))
(array([1], dtype=int8),)
(array([3], dtype=int8),)

View file

@ -0,0 +1,11 @@
import math
import sys
sys.path.append('.')
from snippets import numpy
from ulab import numpy as np
np.set_printoptions(threshold=100)
print (numpy.asarray([1]))
print (numpy.asarray([1.0, 2.0, 3j]))
print (numpy.asarray([4, 3, 1, (2-2j), (2+2j), (2-1j), (2+1j), (2-1j), (2+1j), (1+1j), (1-1j)]))

View file

@ -0,0 +1,3 @@
array([1.0], dtype=float64)
array([1.0+0.0j, 2.0+0.0j, 0.0+3.0j], dtype=complex)
array([4.0+0.0j, 3.0+0.0j, 1.0+0.0j, 2.0-2.0j, 2.0+2.0j, 2.0-1.0j, 2.0+1.0j, 2.0-1.0j, 2.0+1.0j, 1.0+1.0j, 1.0-1.0j], dtype=complex)

View file

@ -0,0 +1,14 @@
import math
import sys
sys.path.append('.')
from ulab import numpy as np
from snippets import numpy
x = np.array([[0, 1, 2],
[3, 4, 5]])
print(numpy.zeros_like(x))
y = np.array([[0, 1j, -2j],[3, 4, 5]], dtype=np.complex)
print(numpy.zeros_like(y))

View file

@ -0,0 +1,4 @@
array([[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0]], dtype=float64)
array([[0.0+0.0j, 0.0+0.0j, 0.0+0.0j],
[0.0+0.0j, 0.0+0.0j, 0.0+0.0j]], dtype=complex)

View file

@ -0,0 +1,15 @@
import math
import sys
sys.path.append('.')
from ulab import numpy as np
from snippets import numpy
print(numpy.atleast_1d(1.0))
x = np.arange(9.0).reshape((3,3))
print(numpy.atleast_1d(x))
print(numpy.atleast_1d(x) is x)
print(numpy.atleast_1d(1, [3, 4]))

View file

@ -0,0 +1,6 @@
array([1.0], dtype=float64)
array([[0.0, 1.0, 2.0],
[3.0, 4.0, 5.0],
[6.0, 7.0, 8.0]], dtype=float64)
True
[array([1.0], dtype=float64), array([3.0, 4.0], dtype=float64)]

View file

@ -0,0 +1,11 @@
import math
import sys
sys.path.append('.')
from snippets import numpy
from ulab import numpy as np
print(numpy.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]]))
print(numpy.append([[1, 2, 3], [4, 5, 6]], [[7, 8, 9]], axis=0))

View file

@ -0,0 +1,4 @@
array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], dtype=float64)
array([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]], dtype=float64)

View file

@ -0,0 +1,16 @@
import math
import sys
sys.path.append('.')
from snippets import numpy
from ulab import numpy as np
print(numpy.poly((0, 0, 0)))
print(numpy.poly((-1./2, 0, 1./2)))
print(numpy.poly((0.847, 0, 0.9883)))
#P = np.array([[0, 1./3], [-1./2, 0]])
#print(numpy.poly(P))

View file

@ -0,0 +1,3 @@
array([1.0, 0.0, 0.0, 0.0], dtype=float64)
array([1.0, 0.0, -0.25, 0.0], dtype=float64)
array([1.0, -1.8353, 0.8370901, 0.0], dtype=float64)

View file

@ -0,0 +1,17 @@
import math
import sys
sys.path.append('.')
from snippets import numpy
from ulab import numpy as np
a = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j], dtype=np.complex)
print(numpy.isreal(a))
a = np.array([1+2j, 2+1j], dtype=np.complex)
print(numpy.isreal(a))
print(numpy.isreal(1))
print(numpy.isreal(1j))

View file

@ -0,0 +1,4 @@
[False, True, True, True, True, False]
[False, False]
True
False

View file

@ -0,0 +1,59 @@
import math
import sys
sys.path.append('.')
from snippets import scipy
from ulab import numpy as np
np.set_printoptions(threshold=100)
a = [4, 3, 1, 2-2j, 2+2j, 2-1j, 2+1j, 2-1j, 2+1j, 1+1j, 1-1j]
#print('_cplxreal: ', scipy.cplxreal(a))
f = np.array([-1.0, -1.0, -1.0, -1.0, 1.0, 1.0, 1.0], dtype=np.float)
t = (0.9984772174419884+0.01125340518638924j)
w = 'real'
#print('nearest_real_complex_idx: ', scipy.nearest_real_complex_idx(f,t,w))
nyquistRate = 48000 * 2
centerFrequency_Hz = 480.0
lowerCutoffFrequency_Hz = centerFrequency_Hz/math.sqrt(2)
upperCutoffFrequenc_Hz = centerFrequency_Hz*math.sqrt(2)
wn = np.array([ lowerCutoffFrequency_Hz, upperCutoffFrequenc_Hz])/nyquistRate
z = []
p = np.array([-0.1564344650402309+0.9876883405951379j, -0.4539904997395468+0.8910065241883679j,
-0.7071067811865476+0.7071067811865475j, -0.8910065241883679+0.4539904997395467j, -0.9876883405951379+0.1564344650402309j,
-0.9876883405951379-0.1564344650402309j, -0.8910065241883679-0.4539904997395467j, -0.7071067811865476-0.7071067811865475j,
-0.4539904997395468-0.8910065241883679j, -0.1564344650402309-0.9876883405951379j], dtype=np.complex)
k = 1
wo = 0.1886352115099219
print(scipy.lp2hp_zpk(z,p,k,wo))
z = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=np.float)
p = np.array([-0.02950904840030544-0.1863127990340476j, -0.08563859394186457-0.1680752041469931j,
-0.1333852372292245-0.1333852372292244j, -0.1680752041469931-0.08563859394186453j, -0.1863127990340476-0.02950904840030543j,
-0.1863127990340476+0.02950904840030543j, -0.1680752041469931+0.08563859394186453j, -0.1333852372292245+0.1333852372292244j,
-0.08563859394186457+0.1680752041469931j, -0.02950904840030544+0.1863127990340476j], dtype=np.complex)
k = 1.0
fs = 2.0
print(scipy.bilinear_zpk(z,p,k,fs))
z = np.array([], dtype=np.float)
p = np.array([-0.3826834323650898+0.9238795325112868j,
-0.9238795325112868+0.3826834323650898j, -0.9238795325112868-0.3826834323650898j,
-0.3826834323650898-0.9238795325112868j], dtype=np.complex)
k = 1
wo = 0.03141673402115484
bw = 0.02221601345771878
print(scipy.lp2bs_zpk(z, p, k, wo=wo, bw=bw))
print(scipy.butter(N=4, Wn=wn, btype='bandpass', analog=False, output='ba'))
print(scipy.butter(N=4, Wn=wn, btype='bandpass', analog=False, output='zpk'))
print(scipy.butter(N=4, Wn=wn, btype='bandpass', analog=False, output='sos'))
print(scipy.butter(N=4, Wn=wn, btype='bandstop', analog=False, output='ba'))
print(scipy.butter(10, 15, 'lp', fs=1000, output='sos'))
print(scipy.butter(10, 15, 'hp', fs=1000, output='sos'))

View file

@ -0,0 +1,21 @@
(array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=float64), array([-0.02950904840030542-0.1863127990340476j, -0.08563859394186453-0.1680752041469931j, -0.1333852372292245-0.1333852372292245j, -0.1680752041469931-0.08563859394186455j, -0.1863127990340476-0.02950904840030542j, -0.1863127990340476+0.02950904840030542j, -0.1680752041469931+0.08563859394186455j, -0.1333852372292245+0.1333852372292245j, -0.08563859394186453+0.1680752041469931j, -0.02950904840030542+0.1863127990340476j], dtype=complex), 1.0)
(array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=float64), array([0.9811181553845975-0.09160115148355542j, 0.9547700993580936-0.08041542979283999j, 0.9334461398558574-0.06239272587315813j, 0.918541250895572-0.03941895649644052j, 0.9108945995807357-0.01346977255018339j, 0.9108945995807357+0.01346977255018339j, 0.918541250895572+0.03941895649644052j, 0.9334461398558574+0.06239272587315813j, 0.9547700993580936+0.08041542979283999j, 0.9811181553845975+0.09160115148355542j], dtype=complex), 0.73979400584056)
(array([0.0+0.03141673402115484j, 0.0+0.03141673402115484j, 0.0+0.03141673402115484j, 0.0+0.03141673402115484j, 0.0-0.03141673402115484j, 0.0-0.03141673402115484j, 0.0-0.03141673402115484j, 0.0-0.03141673402115484j], dtype=complex), array([-0.002920960939069395+0.02254040782120927j, -0.008809831448862637+0.02578034941498454j, -0.008809831448862637-0.02578034941498454j, -0.002920960939069395-0.02254040782120927j, -0.005580739344399452-0.04306532794879095j, -0.01171508867871904-0.03428204969845339j, -0.01171508867871904+0.03428204969845339j, -0.005580739344399452+0.04306532794879095j], dtype=complex), 1.0)
(array([9.375938694653579e-10, 0.0, -3.750375477861432e-09, 0.0, 5.625563216792147e-09, 0.0, -3.750375477861432e-09, 0.0, 9.375938694653579e-10], dtype=float64), array([1.0, -7.969992171296993, 27.79137077985833, -55.37836320535709, 68.97098091968704, -54.97798122059838, 27.39096409669159, -7.798371663621388, 0.9713924646368845], dtype=float64))
(array([1.0+0.0j, 1.0+0.0j, 1.0+0.0j, 1.0+0.0j, -1.0+0.0j, -1.0+0.0j, -1.0+0.0j, -1.0+0.0j], dtype=complex), array([0.9984772174419884-0.01125340518638924j, 0.9955222362276896-0.01283305087503436j, 0.9955222362276896+0.01283305087503436j, 0.9984772174419884+0.01125340518638924j, 0.9969826844852829+0.02147022362342683j, 0.9940139474935357+0.01703981557421542j, 0.9940139474935357-0.01703981557421542j, 0.9969826844852829-0.02147022362342683j], dtype=complex), 9.375938694653579e-10)
array([[9.375938694653579e-10, 1.875187738930716e-09, 9.375938694653579e-10, 1.0, -1.988027894987071, 0.9883540831264847],
[1.0, 2.0, 1.0, 1.0, -1.991044472455379, 0.9912292100185409],
[1.0, -2.0, 1.0, 1.0, -1.993965368970566, 0.9944354436659212],
[1.0, -2.0, 1.0, 1.0, -1.996954434883977, 0.9970833928789848]], dtype=float64)
(array([0.9855924434759455, -7.883766817056333, 27.59075239283292, -55.17858731338059, 68.97201858825612, -55.17858731338059, 27.59075239283291, -7.883766817056331, 0.9855924434759455], dtype=float64), array([1.0, -7.969992171296993, 27.79137077985833, -55.37836320535709, 68.97098091968704, -54.97798122059838, 27.39096409669159, -7.798371663621388, 0.9713924646368845], dtype=float64))
array([[1.609898589131747e-13, 3.219797178263494e-13, 1.609898589131747e-13, 1.0, 0.0, 0.0],
[1.0, 2.0, 1.0, 1.0, -1.821789199161471, 0.8299104063179026],
[1.0, 2.0, 1.0, 1.0, -1.837082501791144, 0.8452718837280704],
[1.0, 2.0, 1.0, 1.0, -1.866892279711715, 0.8752145482536838],
[1.0, 2.0, 1.0, 1.0, -1.909540198716187, 0.918052583977031],
[1.0, -1.0, 0.0, 1.0, -1.962236310769195, 0.9709836057783884]], dtype=float64)
array([[0.73979400584056, -1.47958801168112, 0.73979400584056, 1.0, -1.821789199161471, 0.8299104063179026],
[1.0, -2.0, 1.0, 1.0, -1.837082501791144, 0.8452718837280704],
[1.0, -2.0, 1.0, 1.0, -1.866892279711715, 0.8752145482536838],
[1.0, -2.0, 1.0, 1.0, -1.909540198716187, 0.918052583977031],
[1.0, -2.0, 1.0, 1.0, -1.962236310769195, 0.9709836057783884]], dtype=float64)

0
test-common.sh Normal file → Executable file
View file

18
test-snippets.sh Executable file
View file

@ -0,0 +1,18 @@
#!/bin/sh
set -e
micropython="$1"
for level1 in numpy scipy;
do
for level2 in core lib signal; do
rm -f *.exp
if ! env MICROPY_MICROPYTHON="$micropython" ./run-tests -d snippets/tests/"$level1"/"$level2"; then
for exp in *.exp; do
testbase=$(basename $exp .exp);
echo -e "\nFAILURE $testbase";
diff -u $testbase.exp $testbase.out;
done
exit 1
fi
done
done

15
tests/2d/numpy/asarray.py Normal file
View file

@ -0,0 +1,15 @@
try:
from ulab import numpy as np
except:
import numpy as np
dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float)
for dtype in dtypes:
a = np.ones((2, 2), dtype=dtype)
print()
for _dtype in dtypes:
b = np.asarray(a, dtype=_dtype)
print('a: ', a)
print('b: ', b)
print('a is b: {}\n'.format(a is b))

View file

@ -0,0 +1,155 @@
a: array([[1, 1],
[1, 1]], dtype=uint8)
b: array([[1, 1],
[1, 1]], dtype=uint8)
a is b: True
a: array([[1, 1],
[1, 1]], dtype=uint8)
b: array([[1, 1],
[1, 1]], dtype=int8)
a is b: False
a: array([[1, 1],
[1, 1]], dtype=uint8)
b: array([[1, 1],
[1, 1]], dtype=uint16)
a is b: False
a: array([[1, 1],
[1, 1]], dtype=uint8)
b: array([[1, 1],
[1, 1]], dtype=int16)
a is b: False
a: array([[1, 1],
[1, 1]], dtype=uint8)
b: array([[1.0, 1.0],
[1.0, 1.0]], dtype=float64)
a is b: False
a: array([[1, 1],
[1, 1]], dtype=int8)
b: array([[1, 1],
[1, 1]], dtype=uint8)
a is b: False
a: array([[1, 1],
[1, 1]], dtype=int8)
b: array([[1, 1],
[1, 1]], dtype=int8)
a is b: True
a: array([[1, 1],
[1, 1]], dtype=int8)
b: array([[1, 1],
[1, 1]], dtype=uint16)
a is b: False
a: array([[1, 1],
[1, 1]], dtype=int8)
b: array([[1, 1],
[1, 1]], dtype=int16)
a is b: False
a: array([[1, 1],
[1, 1]], dtype=int8)
b: array([[1.0, 1.0],
[1.0, 1.0]], dtype=float64)
a is b: False
a: array([[1, 1],
[1, 1]], dtype=uint16)
b: array([[1, 1],
[1, 1]], dtype=uint8)
a is b: False
a: array([[1, 1],
[1, 1]], dtype=uint16)
b: array([[1, 1],
[1, 1]], dtype=int8)
a is b: False
a: array([[1, 1],
[1, 1]], dtype=uint16)
b: array([[1, 1],
[1, 1]], dtype=uint16)
a is b: True
a: array([[1, 1],
[1, 1]], dtype=uint16)
b: array([[1, 1],
[1, 1]], dtype=int16)
a is b: False
a: array([[1, 1],
[1, 1]], dtype=uint16)
b: array([[1.0, 1.0],
[1.0, 1.0]], dtype=float64)
a is b: False
a: array([[1, 1],
[1, 1]], dtype=int16)
b: array([[1, 1],
[1, 1]], dtype=uint8)
a is b: False
a: array([[1, 1],
[1, 1]], dtype=int16)
b: array([[1, 1],
[1, 1]], dtype=int8)
a is b: False
a: array([[1, 1],
[1, 1]], dtype=int16)
b: array([[1, 1],
[1, 1]], dtype=uint16)
a is b: False
a: array([[1, 1],
[1, 1]], dtype=int16)
b: array([[1, 1],
[1, 1]], dtype=int16)
a is b: True
a: array([[1, 1],
[1, 1]], dtype=int16)
b: array([[1.0, 1.0],
[1.0, 1.0]], dtype=float64)
a is b: False
a: array([[1.0, 1.0],
[1.0, 1.0]], dtype=float64)
b: array([[1, 1],
[1, 1]], dtype=uint8)
a is b: False
a: array([[1.0, 1.0],
[1.0, 1.0]], dtype=float64)
b: array([[1, 1],
[1, 1]], dtype=int8)
a is b: False
a: array([[1.0, 1.0],
[1.0, 1.0]], dtype=float64)
b: array([[1, 1],
[1, 1]], dtype=uint16)
a is b: False
a: array([[1.0, 1.0],
[1.0, 1.0]], dtype=float64)
b: array([[1, 1],
[1, 1]], dtype=int16)
a is b: False
a: array([[1.0, 1.0],
[1.0, 1.0]], dtype=float64)
b: array([[1.0, 1.0],
[1.0, 1.0]], dtype=float64)
a is b: True

10
tests/2d/numpy/size.py Normal file
View file

@ -0,0 +1,10 @@
try:
from ulab import numpy as np
except:
import numpy as np
a = np.zeros((3, 4))
print(np.size(a, axis=0))
print(np.size(a, axis=1))
print(np.size(a, axis=None))

View file

@ -0,0 +1,3 @@
3
4
12