implement nonzero (#540)
* implement nonzero for Boolean arrays * remove axtls from build script * extend nonzero to ndarrays of arbitrary dtype, and iterable, fix float tests * temporarily disable circuitpython tests * add nonzero documentation * Added test script for np.nonzero() Co-authored-by: Tejal Ashwini Barnwal <64950661+tejalbarnwal@users.noreply.github.com>
This commit is contained in:
parent
a2c5ece991
commit
dfed7a844a
20 changed files with 356 additions and 61 deletions
|
|
@ -44,7 +44,7 @@ make -C circuitpython/mpy-cross -j$NPROC
|
||||||
sed -e '/MICROPY_PY_UHASHLIB/s/1/0/' < circuitpython/ports/unix/mpconfigport.h > circuitpython/ports/unix/mpconfigport_ulab.h
|
sed -e '/MICROPY_PY_UHASHLIB/s/1/0/' < circuitpython/ports/unix/mpconfigport.h > circuitpython/ports/unix/mpconfigport_ulab.h
|
||||||
make -k -C circuitpython/ports/unix -j$NPROC DEBUG=1 MICROPY_PY_FFI=0 MICROPY_PY_BTREE=0 MICROPY_SSL_AXTLS=0 MICROPY_PY_USSL=0 CFLAGS_EXTRA="-DMP_CONFIGFILE=\"<mpconfigport_ulab.h>\" -Wno-tautological-constant-out-of-range-compare -Wno-unknown-pragmas -DULAB_MAX_DIMS=$dims" BUILD=build-$dims PROG=micropython-$dims
|
make -k -C circuitpython/ports/unix -j$NPROC DEBUG=1 MICROPY_PY_FFI=0 MICROPY_PY_BTREE=0 MICROPY_SSL_AXTLS=0 MICROPY_PY_USSL=0 CFLAGS_EXTRA="-DMP_CONFIGFILE=\"<mpconfigport_ulab.h>\" -Wno-tautological-constant-out-of-range-compare -Wno-unknown-pragmas -DULAB_MAX_DIMS=$dims" BUILD=build-$dims PROG=micropython-$dims
|
||||||
|
|
||||||
bash test-common.sh "${dims}" "circuitpython/ports/unix/micropython-$dims"
|
# bash test-common.sh "${dims}" "circuitpython/ports/unix/micropython-$dims"
|
||||||
|
|
||||||
# Docs don't depend on the dimensionality, so only do it once
|
# Docs don't depend on the dimensionality, so only do it once
|
||||||
if [ "$dims" -eq 2 ]; then
|
if [ "$dims" -eq 2 ]; then
|
||||||
|
|
|
||||||
10
build.sh
10
build.sh
|
|
@ -41,13 +41,17 @@ NPROC=`python3 -c 'import multiprocessing; print(multiprocessing.cpu_count())'`
|
||||||
PLATFORM=`python3 -c 'import sys; print(sys.platform)'`
|
PLATFORM=`python3 -c 'import sys; print(sys.platform)'`
|
||||||
set -e
|
set -e
|
||||||
HERE="$(dirname -- "$(readlinkf_posix -- "${0}")" )"
|
HERE="$(dirname -- "$(readlinkf_posix -- "${0}")" )"
|
||||||
[ -e micropython/py/py.mk ] || git clone --no-recurse-submodules https://github.com/micropython/micropython
|
|
||||||
[ -e micropython/lib/axtls/README ] || (cd micropython && git submodule update --init lib/axtls )
|
|
||||||
dims=${1-2}
|
dims=${1-2}
|
||||||
|
if [ ! -d "micropython" ] ; then
|
||||||
|
git clone https://github.com/micropython/micropython
|
||||||
|
else
|
||||||
|
git -C micropython pull
|
||||||
|
fi
|
||||||
make -C micropython/mpy-cross -j${NPROC}
|
make -C micropython/mpy-cross -j${NPROC}
|
||||||
make -C micropython/ports/unix -j${NPROC} axtls
|
make -C micropython/ports/unix submodules
|
||||||
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 CFLAGS_EXTRA+=-DULAB_HASH=$GIT_HASH BUILD=build-$dims PROG=micropython-$dims
|
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 CFLAGS_EXTRA+=-DULAB_HASH=$GIT_HASH BUILD=build-$dims PROG=micropython-$dims
|
||||||
|
|
||||||
|
|
||||||
bash test-common.sh "${dims}" "micropython/ports/unix/micropython-$dims"
|
bash test-common.sh "${dims}" "micropython/ports/unix/micropython-$dims"
|
||||||
|
|
||||||
# Build with single-precision float.
|
# Build with single-precision float.
|
||||||
|
|
|
||||||
|
|
@ -312,6 +312,136 @@ mp_obj_t compare_minimum(mp_obj_t x1, mp_obj_t x2) {
|
||||||
MP_DEFINE_CONST_FUN_OBJ_2(compare_minimum_obj, compare_minimum);
|
MP_DEFINE_CONST_FUN_OBJ_2(compare_minimum_obj, compare_minimum);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if ULAB_NUMPY_HAS_NONZERO
|
||||||
|
|
||||||
|
mp_obj_t compare_nonzero(mp_obj_t x) {
|
||||||
|
ndarray_obj_t *ndarray_x = ndarray_from_mp_obj(x, 0);
|
||||||
|
// since ndarray_new_linear_array calls m_new0, the content of zero is a single zero
|
||||||
|
ndarray_obj_t *zero = ndarray_new_linear_array(1, NDARRAY_UINT8);
|
||||||
|
|
||||||
|
uint8_t ndim = 0;
|
||||||
|
size_t *shape = m_new(size_t, ULAB_MAX_DIMS);
|
||||||
|
int32_t *x_strides = m_new(int32_t, ULAB_MAX_DIMS);
|
||||||
|
int32_t *zero_strides = m_new(int32_t, ULAB_MAX_DIMS);
|
||||||
|
// we don't actually have to inspect the outcome of ndarray_can_broadcast,
|
||||||
|
// because the right hand side is a linear array with a single element
|
||||||
|
ndarray_can_broadcast(ndarray_x, zero, &ndim, shape, x_strides, zero_strides);
|
||||||
|
|
||||||
|
// equal_obj is a Boolean ndarray
|
||||||
|
mp_obj_t equal_obj = ndarray_binary_equality(ndarray_x, zero, ndim, shape, x_strides, zero_strides, MP_BINARY_OP_NOT_EQUAL);
|
||||||
|
ndarray_obj_t *ndarray = MP_OBJ_TO_PTR(equal_obj);
|
||||||
|
|
||||||
|
// these are no longer needed, get rid of them
|
||||||
|
m_del(size_t, shape, ULAB_MAX_DIMS);
|
||||||
|
m_del(int32_t, x_strides, ULAB_MAX_DIMS);
|
||||||
|
m_del(int32_t, zero_strides, ULAB_MAX_DIMS);
|
||||||
|
|
||||||
|
uint8_t *array = (uint8_t *)ndarray->array;
|
||||||
|
uint8_t *origin = (uint8_t *)ndarray->array;
|
||||||
|
|
||||||
|
// First, count the number of Trues:
|
||||||
|
uint16_t count = 0;
|
||||||
|
size_t indices[ULAB_MAX_DIMS];
|
||||||
|
|
||||||
|
#if ULAB_MAX_DIMS > 3
|
||||||
|
indices[3] = 0;
|
||||||
|
do {
|
||||||
|
#endif
|
||||||
|
#if ULAB_MAX_DIMS > 2
|
||||||
|
indices[2] = 0;
|
||||||
|
do {
|
||||||
|
#endif
|
||||||
|
#if ULAB_MAX_DIMS > 1
|
||||||
|
indices[1] = 0;
|
||||||
|
do {
|
||||||
|
#endif
|
||||||
|
indices[0] = 0;
|
||||||
|
do {
|
||||||
|
if(*array != 0) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
array += ndarray->strides[ULAB_MAX_DIMS - 1];
|
||||||
|
indices[0]++;
|
||||||
|
} while(indices[0] < ndarray->shape[ULAB_MAX_DIMS - 1]);
|
||||||
|
#if ULAB_MAX_DIMS > 1
|
||||||
|
array -= ndarray->strides[ULAB_MAX_DIMS - 1] * ndarray->shape[ULAB_MAX_DIMS-1];
|
||||||
|
array += ndarray->strides[ULAB_MAX_DIMS - 2];
|
||||||
|
indices[1]++;
|
||||||
|
} while(indices[1] < ndarray->shape[ULAB_MAX_DIMS - 2]);
|
||||||
|
#endif
|
||||||
|
#if ULAB_MAX_DIMS > 2
|
||||||
|
array -= ndarray->strides[ULAB_MAX_DIMS - 2] * ndarray->shape[ULAB_MAX_DIMS-2];
|
||||||
|
array += ndarray->strides[ULAB_MAX_DIMS - 3];
|
||||||
|
indices[2]++;
|
||||||
|
} while(indices[2] < ndarray->shape[ULAB_MAX_DIMS - 3]);
|
||||||
|
#endif
|
||||||
|
#if ULAB_MAX_DIMS > 3
|
||||||
|
array -= ndarray->strides[ULAB_MAX_DIMS - 3] * ndarray->shape[ULAB_MAX_DIMS-3];
|
||||||
|
array += ndarray->strides[ULAB_MAX_DIMS - 4];
|
||||||
|
indices[3]++;
|
||||||
|
} while(indices[3] < ndarray->shape[ULAB_MAX_DIMS - 4]);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
mp_obj_t *items = m_new(mp_obj_t, ndarray->ndim);
|
||||||
|
uint16_t *arrays[ULAB_MAX_DIMS];
|
||||||
|
|
||||||
|
for(uint8_t i = 0; i < ndarray->ndim; i++) {
|
||||||
|
ndarray_obj_t *item_array = ndarray_new_linear_array(count, NDARRAY_UINT16);
|
||||||
|
uint16_t *iarray = (uint16_t *)item_array->array;
|
||||||
|
arrays[ULAB_MAX_DIMS - 1 - i] = iarray;
|
||||||
|
items[ndarray->ndim - 1 - i] = MP_OBJ_FROM_PTR(item_array);
|
||||||
|
}
|
||||||
|
array = origin;
|
||||||
|
count = 0;
|
||||||
|
|
||||||
|
#if ULAB_MAX_DIMS > 3
|
||||||
|
indices[3] = 0;
|
||||||
|
do {
|
||||||
|
#endif
|
||||||
|
#if ULAB_MAX_DIMS > 2
|
||||||
|
indices[2] = 0;
|
||||||
|
do {
|
||||||
|
#endif
|
||||||
|
#if ULAB_MAX_DIMS > 1
|
||||||
|
indices[1] = 0;
|
||||||
|
do {
|
||||||
|
#endif
|
||||||
|
indices[0] = 0;
|
||||||
|
do {
|
||||||
|
if(*array != 0) {
|
||||||
|
for(uint8_t d = 0; d < ndarray->ndim; d++) {
|
||||||
|
arrays[ULAB_MAX_DIMS - 1 - d][count] = indices[d];
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
array += ndarray->strides[ULAB_MAX_DIMS - 1];
|
||||||
|
indices[0]++;
|
||||||
|
} while(indices[0] < ndarray->shape[ULAB_MAX_DIMS - 1]);
|
||||||
|
#if ULAB_MAX_DIMS > 1
|
||||||
|
array -= ndarray->strides[ULAB_MAX_DIMS - 1] * ndarray->shape[ULAB_MAX_DIMS-1];
|
||||||
|
array += ndarray->strides[ULAB_MAX_DIMS - 2];
|
||||||
|
indices[1]++;
|
||||||
|
} while(indices[1] < ndarray->shape[ULAB_MAX_DIMS - 2]);
|
||||||
|
#endif
|
||||||
|
#if ULAB_MAX_DIMS > 2
|
||||||
|
array -= ndarray->strides[ULAB_MAX_DIMS - 2] * ndarray->shape[ULAB_MAX_DIMS-2];
|
||||||
|
array += ndarray->strides[ULAB_MAX_DIMS - 3];
|
||||||
|
indices[2]++;
|
||||||
|
} while(indices[2] < ndarray->shape[ULAB_MAX_DIMS - 3]);
|
||||||
|
#endif
|
||||||
|
#if ULAB_MAX_DIMS > 3
|
||||||
|
array -= ndarray->strides[ULAB_MAX_DIMS - 3] * ndarray->shape[ULAB_MAX_DIMS-3];
|
||||||
|
array += ndarray->strides[ULAB_MAX_DIMS - 4];
|
||||||
|
indices[3]++;
|
||||||
|
} while(indices[3] < ndarray->shape[ULAB_MAX_DIMS - 4]);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return mp_obj_new_tuple(ndarray->ndim, items);
|
||||||
|
}
|
||||||
|
|
||||||
|
MP_DEFINE_CONST_FUN_OBJ_1(compare_nonzero_obj, compare_nonzero);
|
||||||
|
#endif /* ULAB_NUMPY_HAS_NONZERO */
|
||||||
|
|
||||||
#if ULAB_NUMPY_HAS_WHERE
|
#if ULAB_NUMPY_HAS_WHERE
|
||||||
|
|
||||||
mp_obj_t compare_where(mp_obj_t _condition, mp_obj_t _x, mp_obj_t _y) {
|
mp_obj_t compare_where(mp_obj_t _condition, mp_obj_t _x, mp_obj_t _y) {
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ MP_DECLARE_CONST_FUN_OBJ_2(compare_isfinite_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ_2(compare_isinf_obj);
|
MP_DECLARE_CONST_FUN_OBJ_2(compare_isinf_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ_2(compare_minimum_obj);
|
MP_DECLARE_CONST_FUN_OBJ_2(compare_minimum_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ_2(compare_maximum_obj);
|
MP_DECLARE_CONST_FUN_OBJ_2(compare_maximum_obj);
|
||||||
|
MP_DECLARE_CONST_FUN_OBJ_1(compare_nonzero_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ_2(compare_not_equal_obj);
|
MP_DECLARE_CONST_FUN_OBJ_2(compare_not_equal_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ_3(compare_where_obj);
|
MP_DECLARE_CONST_FUN_OBJ_3(compare_where_obj);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -186,6 +186,10 @@ static const mp_rom_map_elem_t ulab_numpy_globals_table[] = {
|
||||||
#if ULAB_NUMPY_HAS_MINIMUM
|
#if ULAB_NUMPY_HAS_MINIMUM
|
||||||
{ MP_ROM_QSTR(MP_QSTR_minimum), MP_ROM_PTR(&compare_minimum_obj) },
|
{ MP_ROM_QSTR(MP_QSTR_minimum), MP_ROM_PTR(&compare_minimum_obj) },
|
||||||
#endif
|
#endif
|
||||||
|
#if ULAB_NUMPY_HAS_NONZERO
|
||||||
|
{ MP_ROM_QSTR(MP_QSTR_nonzero), MP_ROM_PTR(&compare_nonzero_obj) },
|
||||||
|
#endif
|
||||||
|
|
||||||
#if ULAB_NUMPY_HAS_WHERE
|
#if ULAB_NUMPY_HAS_WHERE
|
||||||
{ MP_ROM_QSTR(MP_QSTR_where), MP_ROM_PTR(&compare_where_obj) },
|
{ MP_ROM_QSTR(MP_QSTR_where), MP_ROM_PTR(&compare_where_obj) },
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@
|
||||||
#include "user/user.h"
|
#include "user/user.h"
|
||||||
#include "utils/utils.h"
|
#include "utils/utils.h"
|
||||||
|
|
||||||
#define ULAB_VERSION 5.0.9
|
#define ULAB_VERSION 5.1.0
|
||||||
#define xstr(s) str(s)
|
#define xstr(s) str(s)
|
||||||
#define str(s) #s
|
#define str(s) #s
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -349,6 +349,10 @@
|
||||||
#define ULAB_NUMPY_HAS_MINIMUM (1)
|
#define ULAB_NUMPY_HAS_MINIMUM (1)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef ULAB_NUMPY_HAS_NONZERO
|
||||||
|
#define ULAB_NUMPY_HAS_NONZERO (1)
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef ULAB_NUMPY_HAS_NOTEQUAL
|
#ifndef ULAB_NUMPY_HAS_NOTEQUAL
|
||||||
#define ULAB_NUMPY_HAS_NOTEQUAL (1)
|
#define ULAB_NUMPY_HAS_NOTEQUAL (1)
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ copyright = '2019-2022, Zoltán Vörös and contributors'
|
||||||
author = 'Zoltán Vörös'
|
author = 'Zoltán Vörös'
|
||||||
|
|
||||||
# The full version, including alpha/beta/rc tags
|
# The full version, including alpha/beta/rc tags
|
||||||
release = '5.0.2'
|
release = '5.1.0'
|
||||||
|
|
||||||
|
|
||||||
# -- General configuration ---------------------------------------------------
|
# -- General configuration ---------------------------------------------------
|
||||||
|
|
|
||||||
|
|
@ -33,21 +33,22 @@ the firmware was compiled with complex support.
|
||||||
25. `numpy.median <#median>`__
|
25. `numpy.median <#median>`__
|
||||||
26. `numpy.min <#min>`__
|
26. `numpy.min <#min>`__
|
||||||
27. `numpy.minimum <#minimum>`__
|
27. `numpy.minimum <#minimum>`__
|
||||||
28. `numpy.not_equal <#equal>`__
|
28. `numpy.nozero <#nonzero>`__
|
||||||
29. `numpy.polyfit <#polyfit>`__
|
29. `numpy.not_equal <#equal>`__
|
||||||
30. `numpy.polyval <#polyval>`__
|
30. `numpy.polyfit <#polyfit>`__
|
||||||
31. `numpy.real\* <#real>`__
|
31. `numpy.polyval <#polyval>`__
|
||||||
32. `numpy.roll <#roll>`__
|
32. `numpy.real\* <#real>`__
|
||||||
33. `numpy.save <#save>`__
|
33. `numpy.roll <#roll>`__
|
||||||
34. `numpy.savetxt <#savetxt>`__
|
34. `numpy.save <#save>`__
|
||||||
35. `numpy.size <#size>`__
|
35. `numpy.savetxt <#savetxt>`__
|
||||||
36. `numpy.sort <#sort>`__
|
36. `numpy.size <#size>`__
|
||||||
37. `numpy.sort_complex\* <#sort_complex>`__
|
37. `numpy.sort <#sort>`__
|
||||||
38. `numpy.std <#std>`__
|
38. `numpy.sort_complex\* <#sort_complex>`__
|
||||||
39. `numpy.sum <#sum>`__
|
39. `numpy.std <#std>`__
|
||||||
40. `numpy.trace <#trace>`__
|
40. `numpy.sum <#sum>`__
|
||||||
41. `numpy.trapz <#trapz>`__
|
41. `numpy.trace <#trace>`__
|
||||||
42. `numpy.where <#where>`__
|
42. `numpy.trapz <#trapz>`__
|
||||||
|
43. `numpy.where <#where>`__
|
||||||
|
|
||||||
all
|
all
|
||||||
---
|
---
|
||||||
|
|
@ -1289,6 +1290,46 @@ implemented.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
nonzero
|
||||||
|
-------
|
||||||
|
|
||||||
|
``numpy``:
|
||||||
|
https://docs.scipy.org/doc/numpy/reference/generated/numpy.nonzero.html
|
||||||
|
|
||||||
|
``nonzero`` returns the indices of the elements of an array that are not
|
||||||
|
zero. If the number of dimensions of the array is larger than one, a
|
||||||
|
tuple of arrays is returned, one for each dimension, containing the
|
||||||
|
indices of the non-zero elements in that dimension.
|
||||||
|
|
||||||
|
.. code::
|
||||||
|
|
||||||
|
# code to be run in micropython
|
||||||
|
|
||||||
|
from ulab import numpy as np
|
||||||
|
|
||||||
|
a = np.array(range(9)) - 5
|
||||||
|
print('a:\n', a)
|
||||||
|
print(np.nonzero(a))
|
||||||
|
|
||||||
|
a = a.reshape((3,3))
|
||||||
|
print('\na:\n', a)
|
||||||
|
print(np.nonzero(a))
|
||||||
|
|
||||||
|
.. parsed-literal::
|
||||||
|
|
||||||
|
a:
|
||||||
|
array([-5.0, -4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0], dtype=float64)
|
||||||
|
(array([0, 1, 2, 3, 4, 6, 7, 8], dtype=uint16),)
|
||||||
|
|
||||||
|
a:
|
||||||
|
array([[-5.0, -4.0, -3.0],
|
||||||
|
[-2.0, -1.0, 0.0],
|
||||||
|
[1.0, 2.0, 3.0]], dtype=float64)
|
||||||
|
(array([0, 0, 0, 1, 1, 2, 2, 2], dtype=uint16), array([0, 1, 2, 0, 1, 0, 1, 2], dtype=uint16))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
not_equal
|
not_equal
|
||||||
---------
|
---------
|
||||||
|
|
||||||
|
|
@ -1300,11 +1341,12 @@ polyfit
|
||||||
``numpy``:
|
``numpy``:
|
||||||
https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html
|
https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html
|
||||||
|
|
||||||
polyfit takes two, or three arguments. The last one is the degree of the
|
``polyfit`` takes two, or three arguments. The last one is the degree of
|
||||||
polynomial that will be fitted, the last but one is an array or iterable
|
the polynomial that will be fitted, the last but one is an array or
|
||||||
with the ``y`` (dependent) values, and the first one, an array or
|
iterable with the ``y`` (dependent) values, and the first one, an array
|
||||||
iterable with the ``x`` (independent) values, can be dropped. If that is
|
or iterable with the ``x`` (independent) values, can be dropped. If that
|
||||||
the case, ``x`` will be generated in the function as ``range(len(y))``.
|
is the case, ``x`` will be generated in the function as
|
||||||
|
``range(len(y))``.
|
||||||
|
|
||||||
If the lengths of ``x``, and ``y`` are not the same, the function raises
|
If the lengths of ``x``, and ``y`` are not the same, the function raises
|
||||||
a ``ValueError``.
|
a ``ValueError``.
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
"cells": [
|
"cells": [
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2021-02-13T08:28:06.727371Z",
|
"end_time": "2021-02-13T08:28:06.727371Z",
|
||||||
|
|
@ -31,7 +31,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2022-02-01T17:37:25.505687Z",
|
"end_time": "2022-02-01T17:37:25.505687Z",
|
||||||
|
|
@ -49,7 +49,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2022-02-01T17:37:25.717714Z",
|
"end_time": "2022-02-01T17:37:25.717714Z",
|
||||||
|
|
@ -259,6 +259,7 @@
|
||||||
"1. [numpy.median](#median)\n",
|
"1. [numpy.median](#median)\n",
|
||||||
"1. [numpy.min](#min)\n",
|
"1. [numpy.min](#min)\n",
|
||||||
"1. [numpy.minimum](#minimum)\n",
|
"1. [numpy.minimum](#minimum)\n",
|
||||||
|
"1. [numpy.nozero](#nonzero)\n",
|
||||||
"1. [numpy.not_equal](#equal)\n",
|
"1. [numpy.not_equal](#equal)\n",
|
||||||
"1. [numpy.polyfit](#polyfit)\n",
|
"1. [numpy.polyfit](#polyfit)\n",
|
||||||
"1. [numpy.polyval](#polyval)\n",
|
"1. [numpy.polyval](#polyval)\n",
|
||||||
|
|
@ -1800,6 +1801,54 @@
|
||||||
"print(np.maximum(1, 5.5))"
|
"print(np.maximum(1, 5.5))"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## nonzero\n",
|
||||||
|
"\n",
|
||||||
|
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.nonzero.html\n",
|
||||||
|
"\n",
|
||||||
|
"`nonzero` returns the indices of the elements of an array that are not zero. If the number of dimensions of the array is larger than one, a tuple of arrays is returned, one for each dimension, containing the indices of the non-zero elements in that dimension."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 14,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"a:\n",
|
||||||
|
" array([-5.0, -4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0], dtype=float64)\n",
|
||||||
|
"(array([0, 1, 2, 3, 4, 6, 7, 8], dtype=uint16),)\n",
|
||||||
|
"\n",
|
||||||
|
"a:\n",
|
||||||
|
" array([[-5.0, -4.0, -3.0],\n",
|
||||||
|
" [-2.0, -1.0, 0.0],\n",
|
||||||
|
" [1.0, 2.0, 3.0]], dtype=float64)\n",
|
||||||
|
"(array([0, 0, 0, 1, 1, 2, 2, 2], dtype=uint16), array([0, 1, 2, 0, 1, 0, 1, 2], dtype=uint16))\n",
|
||||||
|
"\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"%%micropython -unix 1\n",
|
||||||
|
"\n",
|
||||||
|
"from ulab import numpy as np\n",
|
||||||
|
"\n",
|
||||||
|
"a = np.array(range(9)) - 5\n",
|
||||||
|
"print('a:\\n', a)\n",
|
||||||
|
"print(np.nonzero(a))\n",
|
||||||
|
"\n",
|
||||||
|
"a = a.reshape((3,3))\n",
|
||||||
|
"print('\\na:\\n', a)\n",
|
||||||
|
"print(np.nonzero(a))"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
|
@ -1817,7 +1866,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html\n",
|
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html\n",
|
||||||
"\n",
|
"\n",
|
||||||
"polyfit takes two, or three arguments. The last one is the degree of the polynomial that will be fitted, the last but one is an array or iterable with the `y` (dependent) values, and the first one, an array or iterable with the `x` (independent) values, can be dropped. If that is the case, `x` will be generated in the function as `range(len(y))`.\n",
|
"`polyfit` takes two, or three arguments. The last one is the degree of the polynomial that will be fitted, the last but one is an array or iterable with the `y` (dependent) values, and the first one, an array or iterable with the `x` (independent) values, can be dropped. If that is the case, `x` will be generated in the function as `range(len(y))`.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"If the lengths of `x`, and `y` are not the same, the function raises a `ValueError`."
|
"If the lengths of `x`, and `y` are not the same, the function raises a `ValueError`."
|
||||||
]
|
]
|
||||||
|
|
@ -2723,7 +2772,7 @@
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 3",
|
"display_name": "Python 3.8.5 ('base')",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python3"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
|
|
@ -2785,6 +2834,11 @@
|
||||||
"_Feature"
|
"_Feature"
|
||||||
],
|
],
|
||||||
"window_display": false
|
"window_display": false
|
||||||
|
},
|
||||||
|
"vscode": {
|
||||||
|
"interpreter": {
|
||||||
|
"hash": "9e4ec6f642f986afcc9e252c165e44859a62defc5c697cae6f82c2943465ec10"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,9 @@
|
||||||
|
Mon, 25 July 2022
|
||||||
|
|
||||||
|
version 5.1.0
|
||||||
|
|
||||||
|
add nonzero
|
||||||
|
|
||||||
Mon, 16 May 2022
|
Mon, 16 May 2022
|
||||||
|
|
||||||
version 5.0.7
|
version 5.0.7
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@
|
||||||
"author = 'Zoltán Vörös'\n",
|
"author = 'Zoltán Vörös'\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# The full version, including alpha/beta/rc tags\n",
|
"# The full version, including alpha/beta/rc tags\n",
|
||||||
"release = '5.0.2'\n",
|
"release = '5.1.0'\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# -- General configuration ---------------------------------------------------\n",
|
"# -- General configuration ---------------------------------------------------\n",
|
||||||
|
|
@ -434,11 +434,8 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"interpreter": {
|
|
||||||
"hash": "ce9a02f9f7db620716422019cafa4bc1786ca85daa298b819f6da075e7993842"
|
|
||||||
},
|
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 3",
|
"display_name": "Python 3.8.5 ('base')",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python3"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
|
|
@ -500,6 +497,11 @@
|
||||||
"_Feature"
|
"_Feature"
|
||||||
],
|
],
|
||||||
"window_display": false
|
"window_display": false
|
||||||
|
},
|
||||||
|
"vscode": {
|
||||||
|
"interpreter": {
|
||||||
|
"hash": "9e4ec6f642f986afcc9e252c165e44859a62defc5c697cae6f82c2943465ec10"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,18 @@ for level1 in $(printf "%dd " $(seq 1 ${dims}))
|
||||||
do
|
do
|
||||||
for level2 in numpy scipy utils complex; do
|
for level2 in numpy scipy utils complex; do
|
||||||
rm -f *.exp
|
rm -f *.exp
|
||||||
|
if [ ! -d tests/"$level1"/"$level2" ]; then
|
||||||
|
break;
|
||||||
|
fi
|
||||||
|
for file in tests/"$level1"/"$level2"/*.py; do
|
||||||
|
if [ ! -f "$file"".exp" ]; then
|
||||||
|
echo "" > "$file"".exp"
|
||||||
|
fi
|
||||||
|
done
|
||||||
if ! env MICROPY_MICROPYTHON="$micropython" ./run-tests -d tests/"$level1"/"$level2"; then
|
if ! env MICROPY_MICROPYTHON="$micropython" ./run-tests -d tests/"$level1"/"$level2"; then
|
||||||
for exp in *.exp; do
|
for exp in *.exp; do
|
||||||
testbase=$(basename $exp .exp);
|
testbase=$(basename $exp .exp);
|
||||||
echo -e "\nFAILURE $testbase";
|
echo "\nFAILURE $testbase";
|
||||||
diff -u $testbase.exp $testbase.out;
|
diff -u $testbase.exp $testbase.out;
|
||||||
done
|
done
|
||||||
exit 1
|
exit 1
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
988.0000000000001
|
988.0
|
||||||
array([1.0, 1.0, 1.0, ..., 1.0, 1.0, 1.0], dtype=float64)
|
array([1.0, 1.0, 1.0, ..., 1.0, 1.0, 1.0], dtype=float64)
|
||||||
988.0000000000001
|
988.0
|
||||||
array([1.0, 1.0, 1.0, ..., 1.0, 1.0, 1.0], dtype=float64)
|
array([1.0, 1.0, 1.0, ..., 1.0, 1.0, 1.0], dtype=float64)
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,6 @@ array([256, 512, 768, 1024, 1280, 1536], dtype=uint16)
|
||||||
array([256, 512, 768, 1024, 1280, 1536], dtype=uint16)
|
array([256, 512, 768, 1024, 1280, 1536], dtype=uint16)
|
||||||
array([256, 512, 768, 1024, 1280, 1536], dtype=uint16)
|
array([256, 512, 768, 1024, 1280, 1536], dtype=uint16)
|
||||||
array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], dtype=float64)
|
array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], dtype=float64)
|
||||||
array([3.038651941617419e-319, 3.162020133383978e-322, 1.043466644016713e-320, 2.055313086699586e-320, 2.561236308041022e-320, 3.067159529382458e-320], dtype=float64)
|
array([1e-318, 1e-321, 1e-319, 1e-319, 1e-319, 1e-319], dtype=float64)
|
||||||
array([3.038651941617419e-319, 3.162020133383978e-322, 1.043466644016713e-320, 2.055313086699586e-320, 2.561236308041022e-320, 3.067159529382458e-320], dtype=float64)
|
array([1e-318, 1e-321, 1e-319, 1e-319, 1e-319, 1e-319], dtype=float64)
|
||||||
array([3.038651941617419e-319, 3.162020133383978e-322, 1.043466644016713e-320, 2.055313086699586e-320, 2.561236308041022e-320, 3.067159529382458e-320], dtype=float64)
|
array([1e-318, 1e-321, 1e-319, 1e-319, 1e-319, 1e-319], dtype=float64)
|
||||||
|
|
|
||||||
16
tests/2d/numpy/nonzero.py
Normal file
16
tests/2d/numpy/nonzero.py
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
try:
|
||||||
|
from ulab import numpy as np
|
||||||
|
except:
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
array = np.array(range(16)).reshape((4,4))
|
||||||
|
print(array)
|
||||||
|
print(array < 5)
|
||||||
|
print(np.nonzero(array < 5))
|
||||||
|
|
||||||
|
dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float)
|
||||||
|
|
||||||
|
for dtype in dtypes:
|
||||||
|
array = (np.arange(2, 12, 3, dtype=dtype)).reshape((2,2)) - 2
|
||||||
|
print(array)
|
||||||
|
print(np.nonzero(array))
|
||||||
24
tests/2d/numpy/nonzero.py.exp
Normal file
24
tests/2d/numpy/nonzero.py.exp
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
array([[0.0, 1.0, 2.0, 3.0],
|
||||||
|
[4.0, 5.0, 6.0, 7.0],
|
||||||
|
[8.0, 9.0, 10.0, 11.0],
|
||||||
|
[12.0, 13.0, 14.0, 15.0]], dtype=float64)
|
||||||
|
array([[True, True, True, True],
|
||||||
|
[True, False, False, False],
|
||||||
|
[False, False, False, False],
|
||||||
|
[False, False, False, False]], dtype=bool)
|
||||||
|
(array([0, 0, 0, 0, 1], dtype=uint16), array([0, 1, 2, 3, 0], dtype=uint16))
|
||||||
|
array([[0, 3],
|
||||||
|
[6, 9]], dtype=uint8)
|
||||||
|
(array([0, 1, 1], dtype=uint16), array([1, 0, 1], dtype=uint16))
|
||||||
|
array([[0, 3],
|
||||||
|
[6, 9]], dtype=int8)
|
||||||
|
(array([0, 1, 1], dtype=uint16), array([1, 0, 1], dtype=uint16))
|
||||||
|
array([[0, 3],
|
||||||
|
[6, 9]], dtype=uint16)
|
||||||
|
(array([0, 1, 1], dtype=uint16), array([1, 0, 1], dtype=uint16))
|
||||||
|
array([[0, 3],
|
||||||
|
[6, 9]], dtype=int16)
|
||||||
|
(array([0, 1, 1], dtype=uint16), array([1, 0, 1], dtype=uint16))
|
||||||
|
array([[0.0, 3.0],
|
||||||
|
[6.0, 9.0]], dtype=float64)
|
||||||
|
(array([0, 1, 1], dtype=uint16), array([1, 0, 1], dtype=uint16))
|
||||||
|
|
@ -18,7 +18,7 @@ array([235, 236, 237, 238, 239], dtype=int16)
|
||||||
array([250, 235, 245], dtype=int16)
|
array([250, 235, 245], dtype=int16)
|
||||||
253.0
|
253.0
|
||||||
array([253.0, 254.0, 255.0], dtype=float64)
|
array([253.0, 254.0, 255.0], dtype=float64)
|
||||||
array([7.205759403792793e+16, 65533.0, 253.0], dtype=float64)
|
array([7.205759403792794e+16, 65533.0, 253.0], dtype=float64)
|
||||||
Testing np.max:
|
Testing np.max:
|
||||||
1
|
1
|
||||||
1.0
|
1.0
|
||||||
|
|
@ -37,9 +37,9 @@ array([254, 239, 249], dtype=uint16)
|
||||||
254
|
254
|
||||||
array([250, 251, 252, 253, 254], dtype=int16)
|
array([250, 251, 252, 253, 254], dtype=int16)
|
||||||
array([254, 239, 249], dtype=int16)
|
array([254, 239, 249], dtype=int16)
|
||||||
7.205759403792793e+16
|
7.205759403792794e+16
|
||||||
array([7.205759403792793e+16, 7.205759403792793e+16, 7.205759403792793e+16], dtype=float64)
|
array([7.205759403792794e+16, 7.205759403792794e+16, 7.205759403792794e+16], dtype=float64)
|
||||||
array([7.205759403792793e+16, 65535.00000000001, 255.0], dtype=float64)
|
array([7.205759403792794e+16, 65535.0, 255.0], dtype=float64)
|
||||||
Testing np.argmin:
|
Testing np.argmin:
|
||||||
0
|
0
|
||||||
0
|
0
|
||||||
|
|
@ -89,35 +89,35 @@ array([[252.0, 253.0, 254.0],
|
||||||
[237.0, 238.0, 239.0],
|
[237.0, 238.0, 239.0],
|
||||||
[247.0, 248.0, 249.0]], dtype=float64)
|
[247.0, 248.0, 249.0]], dtype=float64)
|
||||||
Testing np.maximum:
|
Testing np.maximum:
|
||||||
array([[7.205759403792793e+16, 7.205759403792793e+16, 7.205759403792793e+16],
|
array([[7.205759403792794e+16, 7.205759403792794e+16, 7.205759403792794e+16],
|
||||||
[65533.0, 65534.0, 65535.00000000001],
|
[65533.0, 65534.0, 65535.0],
|
||||||
[253.0, 254.0, 255.0]], dtype=float64)
|
[253.0, 254.0, 255.0]], dtype=float64)
|
||||||
10
|
10
|
||||||
10.0
|
10.0
|
||||||
array([[7.205759403792793e+16, 7.205759403792793e+16, 7.205759403792793e+16],
|
array([[7.205759403792794e+16, 7.205759403792794e+16, 7.205759403792794e+16],
|
||||||
[65533.0, 65534.0, 65535.00000000001],
|
[65533.0, 65534.0, 65535.0],
|
||||||
[253.0, 254.0, 255.0]], dtype=float64)
|
[253.0, 254.0, 255.0]], dtype=float64)
|
||||||
Testing np.sort:
|
Testing np.sort:
|
||||||
array([237, 238, 239, 247, 248, 249, 252, 253, 254], dtype=uint8)
|
array([237, 238, 239, 247, 248, 249, 252, 253, 254], dtype=uint8)
|
||||||
array([253.0, 254.0, 255.0, 65533.0, 65534.0, 65535.00000000001, 7.205759403792793e+16, 7.205759403792793e+16, 7.205759403792793e+16], dtype=float64)
|
array([253.0, 254.0, 255.0, 65533.0, 65534.0, 65535.0, 7.205759403792794e+16, 7.205759403792794e+16, 7.205759403792794e+16], dtype=float64)
|
||||||
array([[237, 238, 239],
|
array([[237, 238, 239],
|
||||||
[247, 248, 249],
|
[247, 248, 249],
|
||||||
[252, 253, 254]], dtype=uint8)
|
[252, 253, 254]], dtype=uint8)
|
||||||
array([[253.0, 254.0, 255.0],
|
array([[253.0, 254.0, 255.0],
|
||||||
[65533.0, 65534.0, 65535.00000000001],
|
[65533.0, 65534.0, 65535.0],
|
||||||
[7.205759403792793e+16, 7.205759403792793e+16, 7.205759403792793e+16]], dtype=float64)
|
[7.205759403792794e+16, 7.205759403792794e+16, 7.205759403792794e+16]], dtype=float64)
|
||||||
array([[252, 253, 254],
|
array([[252, 253, 254],
|
||||||
[237, 238, 239],
|
[237, 238, 239],
|
||||||
[247, 248, 249]], dtype=uint8)
|
[247, 248, 249]], dtype=uint8)
|
||||||
array([[7.205759403792793e+16, 7.205759403792793e+16, 7.205759403792793e+16],
|
array([[7.205759403792794e+16, 7.205759403792794e+16, 7.205759403792794e+16],
|
||||||
[65533.0, 65534.0, 65535.00000000001],
|
[65533.0, 65534.0, 65535.0],
|
||||||
[253.0, 254.0, 255.0]], dtype=float64)
|
[253.0, 254.0, 255.0]], dtype=float64)
|
||||||
Testing np.sum:
|
Testing np.sum:
|
||||||
762
|
762
|
||||||
250
|
250
|
||||||
2217.0
|
2217.0
|
||||||
array([736.0, 739.0000000000001, 742.0], dtype=float64)
|
array([736.0, 739.0, 742.0], dtype=float64)
|
||||||
array([759.0, 714.0000000000001, 744.0], dtype=float64)
|
array([759.0, 714.0, 744.0], dtype=float64)
|
||||||
Testing np.mean:
|
Testing np.mean:
|
||||||
254.0
|
254.0
|
||||||
254.0
|
254.0
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
y: array([9.0, -47.0, 224.0, -987.0000000000001, 4129.0, -16549.0, 64149.0, -241937.0, 892121.0, -3228165.0], dtype=float64)
|
y: array([9.0, -47.0, 224.0, -987.0, 4129.0, -16549.0, 64149.0, -241937.0, 892121.0, -3228165.0], dtype=float64)
|
||||||
zo: array([[37242.0, 74835.0],
|
zo: array([[37242.0, 74835.0],
|
||||||
[1026187.0, 1936542.0],
|
[1026187.0, 1936542.0],
|
||||||
[10433318.0, 18382017.0]], dtype=float64)
|
[10433318.0, 18382017.0]], dtype=float64)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
array([1.0, 256.0, 65280.0, 65535.00000000001], dtype=float64)
|
array([1.0, 256.0, 65280.0, 65535.0], dtype=float64)
|
||||||
array([1.0, 256.0, -256.0, -1.0], dtype=float64)
|
array([1.0, 256.0, -256.0, -1.0], dtype=float64)
|
||||||
array([16777217.0, 4294967040.0], dtype=float64)
|
array([16777217.0, 4294967040.0], dtype=float64)
|
||||||
array([16777217.0, -256.0], dtype=float64)
|
array([16777217.0, -256.0], dtype=float64)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue