add documentation for delete, extend test script, handle negative scalars

This commit is contained in:
Zoltán Vörös 2022-01-12 18:05:23 +01:00
parent 6eda5ec53c
commit 923cb823a3
9 changed files with 243 additions and 42 deletions

View file

@ -206,7 +206,15 @@ static mp_obj_t transform_delete(size_t n_args, const mp_obj_t *pos_args, mp_map
size_t *index_array = m_new(size_t, index_len);
if(mp_obj_is_int(indices)) {
*index_array++ = (size_t)mp_obj_get_int(indices);
ssize_t value = (ssize_t)mp_obj_get_int(indices);
if(value < 0) {
value += axis_len;
}
if((value < 0) || (value > (ssize_t)axis_len)) {
mp_raise_ValueError(translate("index is out of bounds"));
} else {
*index_array++ = (size_t)value;
}
} else {
mp_obj_iter_buf_t iter_buf;
mp_obj_t item, iterable = mp_getiter(indices, &iter_buf);

View file

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

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.0.0'
release = '4.1.0'
# -- General configuration ---------------------------------------------------

View file

@ -15,32 +15,33 @@ the firmware was compiled with complex support.
7. `numpy.compress\* <#compress>`__
8. `numpy.conjugate\* <#conjugate>`__
9. `numpy.convolve\* <#convolve>`__
10. `numpy.diff <#diff>`__
11. `numpy.dot <#dot>`__
12. `numpy.equal <#equal>`__
13. `numpy.flip\* <#flip>`__
14. `numpy.imag\* <#imag>`__
15. `numpy.interp <#interp>`__
16. `numpy.isfinite <#isfinite>`__
17. `numpy.isinf <#isinf>`__
18. `numpy.max <#max>`__
19. `numpy.maximum <#maximum>`__
20. `numpy.mean <#mean>`__
21. `numpy.median <#median>`__
22. `numpy.min <#min>`__
23. `numpy.minimum <#minimum>`__
24. `numpy.not_equal <#equal>`__
25. `numpy.polyfit <#polyfit>`__
26. `numpy.polyval <#polyval>`__
27. `numpy.real\* <#real>`__
28. `numpy.roll <#roll>`__
29. `numpy.sort <#sort>`__
30. `numpy.sort_complex\* <#sort_complex>`__
31. `numpy.std <#std>`__
32. `numpy.sum <#sum>`__
33. `numpy.trace <#trace>`__
34. `numpy.trapz <#trapz>`__
35. `numpy.where <#where>`__
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.max <#max>`__
20. `numpy.maximum <#maximum>`__
21. `numpy.mean <#mean>`__
22. `numpy.median <#median>`__
23. `numpy.min <#min>`__
24. `numpy.minimum <#minimum>`__
25. `numpy.not_equal <#equal>`__
26. `numpy.polyfit <#polyfit>`__
27. `numpy.polyval <#polyval>`__
28. `numpy.real\* <#real>`__
29. `numpy.roll <#roll>`__
30. `numpy.sort <#sort>`__
31. `numpy.sort_complex\* <#sort_complex>`__
32. `numpy.std <#std>`__
33. `numpy.sum <#sum>`__
34. `numpy.trace <#trace>`__
35. `numpy.trapz <#trapz>`__
36. `numpy.where <#where>`__
all
---
@ -412,6 +413,67 @@ accept complex arrays.
delete
------
``numpy``:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.delete.html
The function returns a new array with sub-arrays along an axis deleted.
It takes two positional arguments, the array, and the indices, which
will be removed, as well as the ``axis`` keyword argument with a default
value of ``None``. If the ``axis`` is ``None``, the will be flattened
first.
The second positional argument can be a scalar, or any ``micropython``
iterable. Since ``range`` can also be passed in place of the indices,
slicing can be emulated. If the indices are negative, the elements are
counted from the end of the axis.
Note that the function creates a copy of the indices first, because it
is not guaranteed that the indices are ordered. Keep this in mind, when
working with large arrays.
.. code::
# code to be run in micropython
from ulab import numpy as np
a = np.array(range(25), dtype=np.uint8).reshape((5,5))
print('a:\n', a)
print('\naxis = 0\n', np.delete(a, 2, axis=0))
print('\naxis = 1\n', np.delete(a, -2, axis=1))
print('\naxis = None\n', np.delete(a, [0, 1, 2, 22]))
.. parsed-literal::
a:
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]], dtype=uint8)
axis = 0
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]], dtype=uint8)
axis = 1
array([[0, 1, 2, 4],
[5, 6, 7, 9],
[10, 11, 12, 14],
[15, 16, 17, 19],
[20, 21, 22, 24]], dtype=uint8)
axis = None
array([3, 4, 5, ..., 21, 23, 24], dtype=uint8)
diff
----

View file

@ -34,8 +34,8 @@
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2022-01-07T19:45:28.079350Z",
"start_time": "2022-01-07T19:45:28.073911Z"
"end_time": "2022-01-12T16:41:02.299473Z",
"start_time": "2022-01-12T16:41:02.282389Z"
}
},
"outputs": [],
@ -52,8 +52,8 @@
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2022-01-07T19:45:28.654136Z",
"start_time": "2022-01-07T19:45:28.634610Z"
"end_time": "2022-01-12T16:41:02.475299Z",
"start_time": "2022-01-12T16:41:02.401569Z"
}
},
"outputs": [],
@ -241,6 +241,7 @@
"1. [numpy.compress*](#compress)\n",
"1. [numpy.conjugate*](#conjugate)\n",
"1. [numpy.convolve*](#convolve)\n",
"1. [numpy.delete](#delete)\n",
"1. [numpy.diff](#diff)\n",
"1. [numpy.dot](#dot)\n",
"1. [numpy.equal](#equal)\n",
@ -738,6 +739,74 @@
"print(np.convolve(x, y))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## delete\n",
"\n",
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.delete.html\n",
"\n",
"The function returns a new array with sub-arrays along an axis deleted. It takes two positional arguments, the array, and the indices, which will be removed, as well as the `axis` keyword argument with a default value of `None`. If the `axis` is `None`, the will be flattened first. \n",
"\n",
"The second positional argument can be a scalar, or any `micropython` iterable. Since `range` can also be passed in place of the indices, slicing can be emulated. If the indices are negative, the elements are counted from the end of the axis.\n",
"\n",
"Note that the function creates a copy of the indices first, because it is not guaranteed that the indices are ordered. Keep this in mind, when working with large arrays."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2022-01-12T17:03:29.099233Z",
"start_time": "2022-01-12T17:03:29.084117Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a:\n",
" array([[0, 1, 2, 3, 4],\n",
" [5, 6, 7, 8, 9],\n",
" [10, 11, 12, 13, 14],\n",
" [15, 16, 17, 18, 19],\n",
" [20, 21, 22, 23, 24]], dtype=uint8)\n",
"\n",
"axis = 0\n",
" array([[0, 1, 2, 3, 4],\n",
" [5, 6, 7, 8, 9],\n",
" [15, 16, 17, 18, 19],\n",
" [20, 21, 22, 23, 24]], dtype=uint8)\n",
"\n",
"axis = 1\n",
" array([[0, 1, 2, 4],\n",
" [5, 6, 7, 9],\n",
" [10, 11, 12, 14],\n",
" [15, 16, 17, 19],\n",
" [20, 21, 22, 24]], dtype=uint8)\n",
"\n",
"axis = None\n",
" array([3, 4, 5, ..., 21, 23, 24], dtype=uint8)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"from ulab import numpy as np\n",
"\n",
"a = np.array(range(25), dtype=np.uint8).reshape((5,5))\n",
"print('a:\\n', a)\n",
"print('\\naxis = 0\\n', np.delete(a, 2, axis=0))\n",
"print('\\naxis = 1\\n', np.delete(a, -2, axis=1))\n",
"print('\\naxis = None\\n', np.delete(a, [0, 1, 2, 22]))"
]
},
{
"cell_type": "markdown",
"metadata": {},

View file

@ -1,3 +1,9 @@
Wed, 12 Jan 2022
version 4.1.0
add numpy.delete
Sat, 8 Jan 2022
version 4.0.0

View file

@ -17,8 +17,8 @@
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2022-01-07T18:24:12.745063Z",
"start_time": "2022-01-07T18:24:12.733067Z"
"end_time": "2022-01-12T17:00:33.582729Z",
"start_time": "2022-01-12T17:00:33.566591Z"
}
},
"outputs": [
@ -61,7 +61,7 @@
"author = 'Zoltán Vörös'\n",
"\n",
"# The full version, including alpha/beta/rc tags\n",
"release = '4.0.0'\n",
"release = '4.1.0'\n",
"\n",
"\n",
"# -- General configuration ---------------------------------------------------\n",
@ -215,11 +215,11 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2022-01-07T18:24:27.671415Z",
"start_time": "2022-01-07T18:24:24.933205Z"
"end_time": "2022-01-12T17:03:49.038101Z",
"start_time": "2022-01-12T17:03:48.886617Z"
}
},
"outputs": [],
@ -256,11 +256,11 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2022-01-07T19:52:29.910335Z",
"start_time": "2022-01-07T19:52:28.432391Z"
"end_time": "2022-01-12T17:03:52.084601Z",
"start_time": "2022-01-12T17:03:50.354118Z"
}
},
"outputs": [],

View file

@ -18,3 +18,9 @@ for dtype in dtypes:
print(np.delete(a, 2, axis=0))
print(np.delete(a, 2, axis=1))
print(np.delete(a, 2))
for dtype in dtypes:
a = np.array(range(25), dtype=dtype).reshape((5,5))
print(np.delete(a, -3, axis=0))
print(np.delete(a, -3, axis=1))
print(np.delete(a, -3))

View file

@ -93,3 +93,53 @@ array([[0.0, 1.0, 3.0, 4.0],
[15.0, 16.0, 18.0, 19.0],
[20.0, 21.0, 23.0, 24.0]], dtype=float64)
array([0.0, 1.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, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0], dtype=float64)
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]], dtype=uint8)
array([[0, 1, 3, 4],
[5, 6, 8, 9],
[10, 11, 13, 14],
[15, 16, 18, 19],
[20, 21, 23, 24]], dtype=uint8)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24], dtype=uint8)
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]], dtype=int8)
array([[0, 1, 3, 4],
[5, 6, 8, 9],
[10, 11, 13, 14],
[15, 16, 18, 19],
[20, 21, 23, 24]], dtype=int8)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24], dtype=int8)
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]], dtype=uint16)
array([[0, 1, 3, 4],
[5, 6, 8, 9],
[10, 11, 13, 14],
[15, 16, 18, 19],
[20, 21, 23, 24]], dtype=uint16)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24], dtype=uint16)
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]], dtype=int16)
array([[0, 1, 3, 4],
[5, 6, 8, 9],
[10, 11, 13, 14],
[15, 16, 18, 19],
[20, 21, 23, 24]], dtype=int16)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24], dtype=int16)
array([[0.0, 1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0, 9.0],
[15.0, 16.0, 17.0, 18.0, 19.0],
[20.0, 21.0, 22.0, 23.0, 24.0]], dtype=float64)
array([[0.0, 1.0, 3.0, 4.0],
[5.0, 6.0, 8.0, 9.0],
[10.0, 11.0, 13.0, 14.0],
[15.0, 16.0, 18.0, 19.0],
[20.0, 21.0, 23.0, 24.0]], dtype=float64)
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, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 23.0, 24.0], dtype=float64)