fix keepdims for std, update docs (#704)
This commit is contained in:
parent
903506ca9a
commit
be15d62632
9 changed files with 366 additions and 258 deletions
|
|
@ -603,8 +603,9 @@ static mp_obj_t numerical_function(size_t n_args, const mp_obj_t *pos_args, mp_m
|
|||
case NUMERICAL_ARGMAX:
|
||||
COMPLEX_DTYPE_NOT_IMPLEMENTED(ndarray->dtype)
|
||||
return numerical_argmin_argmax_ndarray(ndarray, keepdims, axis, optype);
|
||||
case NUMERICAL_SUM:
|
||||
case NUMERICAL_MEAN:
|
||||
case NUMERICAL_STD:
|
||||
case NUMERICAL_SUM:
|
||||
COMPLEX_DTYPE_NOT_IMPLEMENTED(ndarray->dtype)
|
||||
return numerical_sum_mean_std_ndarray(ndarray, axis, keepdims, optype, 0);
|
||||
default:
|
||||
|
|
@ -1398,7 +1399,7 @@ mp_obj_t numerical_std(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_arg
|
|||
mp_obj_t oin = args[0].u_obj;
|
||||
mp_obj_t axis = args[1].u_obj;
|
||||
size_t ddof = args[2].u_int;
|
||||
mp_obj_t keepdims = args[2].u_obj;
|
||||
mp_obj_t keepdims = args[3].u_obj;
|
||||
|
||||
if((axis != mp_const_none) && (mp_obj_get_int(axis) != 0) && (mp_obj_get_int(axis) != 1)) {
|
||||
// this seems to pass with False, and True...
|
||||
|
|
|
|||
|
|
@ -57,35 +57,9 @@
|
|||
(rarray) += (results)->itemsize;\
|
||||
})
|
||||
|
||||
// The mean could be calculated by simply dividing the sum by
|
||||
// the number of elements, but that method is numerically unstable
|
||||
#define RUN_MEAN1(type, array, rarray, ss)\
|
||||
({\
|
||||
mp_float_t M = 0.0;\
|
||||
for(size_t i=0; i < (ss).shape[0]; i++) {\
|
||||
mp_float_t value = (mp_float_t)(*(type *)(array));\
|
||||
M = M + (value - M) / (mp_float_t)(i+1);\
|
||||
(array) += (ss).strides[0];\
|
||||
}\
|
||||
*(rarray)++ = M;\
|
||||
})
|
||||
|
||||
// Instead of the straightforward implementation of the definition,
|
||||
// we take the numerically stable Welford algorithm here
|
||||
// https://www.johndcook.com/blog/2008/09/26/comparing-three-methods-of-computing-standard-deviation/
|
||||
#define RUN_STD1(type, array, rarray, ss, div)\
|
||||
({\
|
||||
mp_float_t M = 0.0, m = 0.0, S = 0.0;\
|
||||
for(size_t i=0; i < (ss).shape[0]; i++) {\
|
||||
mp_float_t value = (mp_float_t)(*(type *)(array));\
|
||||
m = M + (value - M) / (mp_float_t)(i+1);\
|
||||
S = S + (value - M) * (value - m);\
|
||||
M = m;\
|
||||
(array) += (ss).strides[0];\
|
||||
}\
|
||||
*(rarray)++ = MICROPY_FLOAT_C_FUN(sqrt)(S / (div));\
|
||||
})
|
||||
|
||||
#define RUN_MEAN_STD1(type, array, rarray, ss, div, isStd)\
|
||||
({\
|
||||
mp_float_t M = 0.0, m = 0.0, S = 0.0;\
|
||||
|
|
@ -193,14 +167,6 @@
|
|||
RUN_SUM1(type, (array), (results), (rarray), (ss));\
|
||||
} while(0)
|
||||
|
||||
#define RUN_MEAN(type, array, rarray, ss) do {\
|
||||
RUN_MEAN1(type, (array), (rarray), (ss));\
|
||||
} while(0)
|
||||
|
||||
#define RUN_STD(type, array, rarray, ss, div) do {\
|
||||
RUN_STD1(type, (array), (results), (rarray), (ss), (div));\
|
||||
} while(0)
|
||||
|
||||
#define RUN_MEAN_STD(type, array, rarray, ss, div, isStd) do {\
|
||||
RUN_MEAN_STD1(type, (array), (rarray), (ss), (div), (isStd));\
|
||||
} while(0)
|
||||
|
|
@ -234,26 +200,6 @@
|
|||
} while(l < (ss).shape[ULAB_MAX_DIMS - 1]);\
|
||||
} while(0)
|
||||
|
||||
#define RUN_MEAN(type, array, rarray, ss) do {\
|
||||
size_t l = 0;\
|
||||
do {\
|
||||
RUN_MEAN1(type, (array), (rarray), (ss));\
|
||||
(array) -= (ss).strides[0] * (ss).shape[0];\
|
||||
(array) += (ss).strides[ULAB_MAX_DIMS - 1];\
|
||||
l++;\
|
||||
} while(l < (ss).shape[ULAB_MAX_DIMS - 1]);\
|
||||
} while(0)
|
||||
|
||||
#define RUN_STD(type, array, rarray, ss, div) do {\
|
||||
size_t l = 0;\
|
||||
do {\
|
||||
RUN_STD1(type, (array), (rarray), (ss), (div));\
|
||||
(array) -= (ss).strides[0] * (ss).shape[0];\
|
||||
(array) += (ss).strides[ULAB_MAX_DIMS - 1];\
|
||||
l++;\
|
||||
} while(l < (ss).shape[ULAB_MAX_DIMS - 1]);\
|
||||
} while(0)
|
||||
|
||||
#define RUN_MEAN_STD(type, array, rarray, ss, div, isStd) do {\
|
||||
size_t l = 0;\
|
||||
do {\
|
||||
|
|
@ -325,38 +271,6 @@
|
|||
} while(k < (ss).shape[ULAB_MAX_DIMS - 2]);\
|
||||
} while(0)
|
||||
|
||||
#define RUN_MEAN(type, array, rarray, ss) do {\
|
||||
size_t k = 0;\
|
||||
do {\
|
||||
size_t l = 0;\
|
||||
do {\
|
||||
RUN_MEAN1(type, (array), (rarray), (ss));\
|
||||
(array) -= (ss).strides[0] * (ss).shape[0];\
|
||||
(array) += (ss).strides[ULAB_MAX_DIMS - 1];\
|
||||
l++;\
|
||||
} while(l < (ss).shape[ULAB_MAX_DIMS - 1]);\
|
||||
(array) -= (ss).strides[ULAB_MAX_DIMS - 1] * (ss).shape[ULAB_MAX_DIMS - 1];\
|
||||
(array) += (ss).strides[ULAB_MAX_DIMS - 2];\
|
||||
k++;\
|
||||
} while(k < (ss).shape[ULAB_MAX_DIMS - 2]);\
|
||||
} while(0)
|
||||
|
||||
#define RUN_STD(type, array, rarray, ss, div) do {\
|
||||
size_t k = 0;\
|
||||
do {\
|
||||
size_t l = 0;\
|
||||
do {\
|
||||
RUN_STD1(type, (array), (rarray), (ss), (div));\
|
||||
(array) -= (ss).strides[0] * (ss).shape[0];\
|
||||
(array) += (ss).strides[ULAB_MAX_DIMS - 1];\
|
||||
l++;\
|
||||
} while(l < (ss).shape[ULAB_MAX_DIMS - 1]);\
|
||||
(array) -= (ss).strides[ULAB_MAX_DIMS - 1] * (ss).shape[ULAB_MAX_DIMS - 1];\
|
||||
(array) += (ss).strides[ULAB_MAX_DIMS - 2];\
|
||||
k++;\
|
||||
} while(k < (ss).shape[ULAB_MAX_DIMS - 2]);\
|
||||
} while(0)
|
||||
|
||||
#define RUN_MEAN_STD(type, array, rarray, ss, div, isStd) do {\
|
||||
size_t k = 0;\
|
||||
do {\
|
||||
|
|
@ -467,50 +381,6 @@
|
|||
} while(j < (ss).shape[ULAB_MAX_DIMS - 3]);\
|
||||
} while(0)
|
||||
|
||||
#define RUN_MEAN(type, array, rarray, ss) do {\
|
||||
size_t j = 0;\
|
||||
do {\
|
||||
size_t k = 0;\
|
||||
do {\
|
||||
size_t l = 0;\
|
||||
do {\
|
||||
RUN_MEAN1(type, (array), (rarray), (ss));\
|
||||
(array) -= (ss).strides[0] * (ss).shape[0];\
|
||||
(array) += (ss).strides[ULAB_MAX_DIMS - 1];\
|
||||
l++;\
|
||||
} while(l < (ss).shape[ULAB_MAX_DIMS - 1]);\
|
||||
(array) -= (ss).strides[ULAB_MAX_DIMS - 1] * (ss).shape[ULAB_MAX_DIMS - 1];\
|
||||
(array) += (ss).strides[ULAB_MAX_DIMS - 2];\
|
||||
k++;\
|
||||
} while(k < (ss).shape[ULAB_MAX_DIMS - 2]);\
|
||||
(array) -= (ss).strides[ULAB_MAX_DIMS - 2] * (ss).shape[ULAB_MAX_DIMS - 2];\
|
||||
(array) += (ss).strides[ULAB_MAX_DIMS - 3];\
|
||||
j++;\
|
||||
} while(j < (ss).shape[ULAB_MAX_DIMS - 3]);\
|
||||
} while(0)
|
||||
|
||||
#define RUN_STD(type, array, rarray, ss, div) do {\
|
||||
size_t j = 0;\
|
||||
do {\
|
||||
size_t k = 0;\
|
||||
do {\
|
||||
size_t l = 0;\
|
||||
do {\
|
||||
RUN_STD1(type, (array), (rarray), (ss), (div));\
|
||||
(array) -= (ss).strides[0] * (ss).shape[0];\
|
||||
(array) += (ss).strides[ULAB_MAX_DIMS - 1];\
|
||||
l++;\
|
||||
} while(l < (ss).shape[ULAB_MAX_DIMS - 1]);\
|
||||
(array) -= (ss).strides[ULAB_MAX_DIMS - 1] * (ss).shape[ULAB_MAX_DIMS - 1];\
|
||||
(array) += (ss).strides[ULAB_MAX_DIMS - 2];\
|
||||
k++;\
|
||||
} while(k < (ss).shape[ULAB_MAX_DIMS - 2]);\
|
||||
(array) -= (ss).strides[ULAB_MAX_DIMS - 2] * (ss).shape[ULAB_MAX_DIMS - 2];\
|
||||
(array) += (ss).strides[ULAB_MAX_DIMS - 3];\
|
||||
j++;\
|
||||
} while(j < (ss).shape[ULAB_MAX_DIMS - 3]);\
|
||||
} while(0)
|
||||
|
||||
#define RUN_MEAN_STD(type, array, rarray, ss, div, isStd) do {\
|
||||
size_t j = 0;\
|
||||
do {\
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@
|
|||
#include "user/user.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
#define ULAB_VERSION 6.7.1
|
||||
#define ULAB_VERSION 6.7.2
|
||||
#define xstr(s) str(s)
|
||||
#define str(s) #s
|
||||
|
||||
|
|
|
|||
|
|
@ -23,11 +23,12 @@ from sphinx import addnodes
|
|||
# -- Project information -----------------------------------------------------
|
||||
|
||||
project = 'The ulab book'
|
||||
copyright = '2019-2024, Zoltán Vörös and contributors'
|
||||
copyright = '2019-2025, Zoltán Vörös and contributors'
|
||||
author = 'Zoltán Vörös'
|
||||
|
||||
# The full version, including alpha/beta/rc tags
|
||||
release = '6.6.0'
|
||||
release = '6.7.2'
|
||||
|
||||
|
||||
# -- General configuration ---------------------------------------------------
|
||||
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ Welcome to the ulab book!
|
|||
numpy-fft
|
||||
numpy-linalg
|
||||
numpy-random
|
||||
scipy-integrate
|
||||
scipy-linalg
|
||||
scipy-optimize
|
||||
scipy-signal
|
||||
|
|
|
|||
220
docs/manual/source/scipy-integrate.rst
Normal file
220
docs/manual/source/scipy-integrate.rst
Normal file
|
|
@ -0,0 +1,220 @@
|
|||
|
||||
scipy.integrate
|
||||
===============
|
||||
|
||||
This module provides a simplified subset of CPython’s
|
||||
``scipy.integrate`` module. The algorithms were not ported from
|
||||
CPython’s ``scipy.integrate`` for the sake of resource usage, but
|
||||
derived from a paper found in https://www.genivia.com/qthsh.html. There
|
||||
are four numerical integration algorithms:
|
||||
|
||||
1. `scipy.integrate.quad <#quad>`__
|
||||
2. `scipy.integrate.romberg <#romberg>`__
|
||||
3. `scipy.integrate.simpson <#simpson>`__
|
||||
4. `scipy.integrate.tanhsinh <#tanhsinh>`__
|
||||
|
||||
Introduction
|
||||
------------
|
||||
|
||||
Numerical integration works best with float64 math enabled. If you
|
||||
require float64 math, be sure to set ``MICROPY_OBJ_REPR_A`` and
|
||||
``MICROPY_FLOAT_IMPL_DOUBLE``. This being said, the modules work equally
|
||||
well using float32, albeit with reduced precision. The required error
|
||||
tolerance can be specified for each of the function calls using the
|
||||
“eps=” option, defaulting to the compiled in ``etolerance`` value (1e-14
|
||||
for fp64, 1e-8 for fp32).
|
||||
|
||||
The submodule can be enabled by setting
|
||||
``ULAB_SCIPY_HAS_INTEGRATE_MODULE`` in ``code/ulab.h``. As for the
|
||||
individual integration algorithms, you can select which to include by
|
||||
setting one or more of ``ULAB_INTEGRATE_HAS_QUAD``,
|
||||
``ULAB_INTEGRATE_HAS_ROMBERG``, ``ULAB_INTEGRATE_HAS_SIMPSON``, and
|
||||
``ULAB_INTEGRATE_HAS_TANHSINH``.
|
||||
|
||||
Also note that these algorithms do not support complex numbers, although
|
||||
it is certainly possible to implement complex integration in MicroPython
|
||||
on top of this module, e.g. as in
|
||||
https://stackoverflow.com/questions/5965583/use-scipy-integrate-quad-to-integrate-complex-numbers.
|
||||
|
||||
quad
|
||||
----
|
||||
|
||||
``scipy``:
|
||||
https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad.html
|
||||
|
||||
In CPython ``scipy.integrate``, ``quad`` is a wrapper implementing many
|
||||
algorithms based on the Fortran QUADPACK package. Gauss-Kronrod is just
|
||||
one of them, and it is useful for most general-purpose tasks. This
|
||||
particular function implements an Adaptive Gauss-Kronrod (G10,K21)
|
||||
quadrature algorithm. The Gauss–Kronrod quadrature formula is a variant
|
||||
of Gaussian quadrature, in which the evaluation points are chosen so
|
||||
that an accurate approximation can be computed by re-using the
|
||||
information produced by the computation of a less accurate approximation
|
||||
(https://en.wikipedia.org/wiki/Gauss%E2%80%93Kronrod_quadrature_formula).
|
||||
|
||||
The function takes three to five arguments:
|
||||
|
||||
- f, a callable,
|
||||
- a and b, the lower and upper integration limit,
|
||||
- order=, the order of integration (default 5),
|
||||
- eps=, the error tolerance (default etolerance)
|
||||
|
||||
The function returns the result and the error estimate as a tuple of
|
||||
floats.
|
||||
|
||||
.. code::
|
||||
|
||||
# code to be run in micropython
|
||||
|
||||
from ulab import scipy
|
||||
|
||||
f = lambda x: x**2 + 2*x + 1
|
||||
result = scipy.integrate.quad(f, 0, 5, order=5, eps=1e-10)
|
||||
print (f"result = {result}")
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
UsageError: Cell magic `%%micropython` not found.
|
||||
|
||||
|
||||
romberg
|
||||
-------
|
||||
|
||||
``scipy``:
|
||||
https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.romberg.html
|
||||
|
||||
This function implements the Romberg quadrature algorithm. Romberg’s
|
||||
method is a Newton–Cotes formula – it evaluates the integrand at equally
|
||||
spaced points. The integrand must have continuous derivatives, though
|
||||
fairly good results may be obtained if only a few derivatives exist. If
|
||||
it is possible to evaluate the integrand at unequally spaced points,
|
||||
then other methods such as Gaussian quadrature and Clenshaw–Curtis
|
||||
quadrature are generally more accurate
|
||||
(https://en.wikipedia.org/wiki/Romberg%27s_method).
|
||||
|
||||
Please note: This function is deprecated as of SciPy 1.12.0 and will be
|
||||
removed in SciPy 1.15.0. Please use ``scipy.integrate.quad`` instead.
|
||||
|
||||
The function takes three to five arguments:
|
||||
|
||||
- f, a callable,
|
||||
- a and b, the lower and upper integration limit,
|
||||
- steps=, the number of steps taken to calculate (default 100),
|
||||
- eps=, the error tolerance (default etolerance)
|
||||
|
||||
The function returns the result as a float.
|
||||
|
||||
.. code::
|
||||
|
||||
# code to be run in micropython
|
||||
|
||||
from ulab import scipy
|
||||
|
||||
f = lambda x: x**2 + 2*x + 1
|
||||
result = scipy.integrate.romberg(f, 0, 5)
|
||||
print (f"result = {result}")
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
UsageError: Cell magic `%%micropython` not found.
|
||||
|
||||
|
||||
simpson
|
||||
-------
|
||||
|
||||
``scipy``:
|
||||
https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.simpson.html
|
||||
|
||||
This function is different from CPython’s ``simpson`` method in that it
|
||||
does not take an array of function values but determines the optimal
|
||||
spacing of samples itself. Adaptive Simpson’s method, also called
|
||||
adaptive Simpson’s rule, is a method of numerical integration proposed
|
||||
by G.F. Kuncir in 1962. It is probably the first recursive adaptive
|
||||
algorithm for numerical integration to appear in print, although more
|
||||
modern adaptive methods based on Gauss–Kronrod quadrature and
|
||||
Clenshaw–Curtis quadrature are now generally preferred
|
||||
(https://en.wikipedia.org/wiki/Adaptive_Simpson%27s_method).
|
||||
|
||||
The function takes three to five arguments:
|
||||
|
||||
- f, a callable,
|
||||
- a and b, the lower and upper integration limit,
|
||||
- steps=, the number of steps taken to calculate (default 100),
|
||||
- eps=, the error tolerance (default etolerance)
|
||||
|
||||
The function returns the result as a float.
|
||||
|
||||
.. code::
|
||||
|
||||
# code to be run in micropython
|
||||
|
||||
from ulab import scipy
|
||||
|
||||
f = lambda x: x**2 + 2*x + 1
|
||||
result = scipy.integrate.simpson(f, 0, 5)
|
||||
print (f"result = {result}")
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
UsageError: Cell magic `%%micropython` not found.
|
||||
|
||||
|
||||
tanhsinh
|
||||
--------
|
||||
|
||||
``scipy``:
|
||||
https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad.html
|
||||
|
||||
In CPython ``scipy.integrate``, ``tanhsinh`` is written in Python
|
||||
(https://github.com/scipy/scipy/blob/main/scipy/integrate/\_tanhsinh.py).
|
||||
It is used in cases where Newton-Cotes, Gauss-Kronrod, and other
|
||||
formulae do not work due to properties of the integrand or the
|
||||
integration limits. (In SciPy v1.14.1, it is not a public function but
|
||||
it has been marked as public in SciPy v1.15.0rc1).
|
||||
|
||||
This particular function implements an optimized Tanh-Sinh, Sinh-Sinh
|
||||
and Exp-Sinh quadrature algorithm. It is especially applied where
|
||||
singularities or infinite derivatives exist at one or both endpoints.
|
||||
The method uses hyperbolic functions in a change of variables to
|
||||
transform an integral on the interval x ∈ (−1, 1) to an integral on the
|
||||
entire real line t ∈ (−∞, ∞), the two integrals having the same value.
|
||||
After this transformation, the integrand decays with a double
|
||||
exponential rate, and thus, this method is also known as the double
|
||||
exponential (DE) formula
|
||||
(https://en.wikipedia.org/wiki/Tanh-sinh_quadrature).
|
||||
|
||||
As opposed to the three algorithms mentioned before, it also supports
|
||||
integrals with infinite limits like the Gaussian integral
|
||||
(https://en.wikipedia.org/wiki/Gaussian_integral), as shown below.
|
||||
|
||||
The function takes three to five arguments:
|
||||
|
||||
- f, a callable,
|
||||
- a and b, the lower and upper integration limit,
|
||||
- levels=, the number of loops taken to calculate (default 6),
|
||||
- eps=, the error tolerance (default: etolerance)
|
||||
|
||||
The function returns the result and the error estimate as a tuple of
|
||||
floats.
|
||||
|
||||
.. code::
|
||||
|
||||
# code to be run in micropython
|
||||
|
||||
from ulab import scipy, numpy as np
|
||||
from math import *
|
||||
f = lambda x: exp(- x**2)
|
||||
result = scipy.integrate.tanhsinh(f, -np.inf, np.inf)
|
||||
print (f"result = {result}")
|
||||
exact = sqrt(pi) # which is the exact value
|
||||
print (f"exact value = {exact}")
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
UsageError: Cell magic `%%micropython` not found.
|
||||
|
||||
|
||||
.. code::
|
||||
|
||||
# code to be run in CPython
|
||||
|
||||
|
|
@ -1,3 +1,9 @@
|
|||
Sun, 19 Jan 2025
|
||||
|
||||
version 6.7.2
|
||||
|
||||
fix keepdims for std, remove redundant macros from numerical.h, update documentation
|
||||
|
||||
Mon, 30 Dec 2024
|
||||
|
||||
version 6.7.1
|
||||
|
|
|
|||
|
|
@ -57,11 +57,11 @@
|
|||
"# -- Project information -----------------------------------------------------\n",
|
||||
"\n",
|
||||
"project = 'The ulab book'\n",
|
||||
"copyright = '2019-2024, Zoltán Vörös and contributors'\n",
|
||||
"copyright = '2019-2025, Zoltán Vörös and contributors'\n",
|
||||
"author = 'Zoltán Vörös'\n",
|
||||
"\n",
|
||||
"# The full version, including alpha/beta/rc tags\n",
|
||||
"release = '6.6.0'\n",
|
||||
"release = '6.7.2'\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# -- General configuration ---------------------------------------------------\n",
|
||||
|
|
@ -191,6 +191,7 @@
|
|||
" numpy-fft\n",
|
||||
" numpy-linalg\n",
|
||||
" numpy-random\n",
|
||||
" scipy-integrate\n",
|
||||
" scipy-linalg\n",
|
||||
" scipy-optimize\n",
|
||||
" scipy-signal\n",
|
||||
|
|
@ -216,7 +217,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2022-02-09T06:27:21.647179Z",
|
||||
|
|
@ -257,7 +258,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2022-02-09T06:27:42.024028Z",
|
||||
|
|
@ -294,6 +295,10 @@
|
|||
"/home/v923z/anaconda3/lib/python3.11/site-packages/nbconvert/exporters/exporter.py:349: MissingIDFieldWarning: Code cell is missing an id field, this will become a hard error in future nbformat versions. You may want to use `normalize()` on your notebooks before validations (available since nbformat 5.1.4). Previous versions of nbformat are fixing this issue transparently, and will stop doing so in the future.\n",
|
||||
" _, nbc = validator.normalize(nbc)\n",
|
||||
"/home/v923z/anaconda3/lib/python3.11/site-packages/nbconvert/exporters/exporter.py:349: MissingIDFieldWarning: Code cell is missing an id field, this will become a hard error in future nbformat versions. You may want to use `normalize()` on your notebooks before validations (available since nbformat 5.1.4). Previous versions of nbformat are fixing this issue transparently, and will stop doing so in the future.\n",
|
||||
" _, nbc = validator.normalize(nbc)\n",
|
||||
"/home/v923z/anaconda3/lib/python3.11/site-packages/nbconvert/exporters/exporter.py:349: MissingIDFieldWarning: Code cell is missing an id field, this will become a hard error in future nbformat versions. You may want to use `normalize()` on your notebooks before validations (available since nbformat 5.1.4). Previous versions of nbformat are fixing this issue transparently, and will stop doing so in the future.\n",
|
||||
" _, nbc = validator.normalize(nbc)\n",
|
||||
"/home/v923z/anaconda3/lib/python3.11/site-packages/nbconvert/exporters/exporter.py:349: MissingIDFieldWarning: Code cell is missing an id field, this will become a hard error in future nbformat versions. You may want to use `normalize()` on your notebooks before validations (available since nbformat 5.1.4). Previous versions of nbformat are fixing this issue transparently, and will stop doing so in the future.\n",
|
||||
" _, nbc = validator.normalize(nbc)\n"
|
||||
]
|
||||
}
|
||||
|
|
@ -306,6 +311,7 @@
|
|||
" 'numpy-fft',\n",
|
||||
" 'numpy-linalg',\n",
|
||||
" 'numpy-random',\n",
|
||||
" 'scipy-integrate',\n",
|
||||
" 'scipy-linalg',\n",
|
||||
" 'scipy-optimize',\n",
|
||||
" 'scipy-signal',\n",
|
||||
|
|
@ -470,7 +476,7 @@
|
|||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3.8.5 ('base')",
|
||||
"display_name": "base",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
|
|
@ -532,11 +538,6 @@
|
|||
"_Feature"
|
||||
],
|
||||
"window_display": false
|
||||
},
|
||||
"vscode": {
|
||||
"interpreter": {
|
||||
"hash": "9e4ec6f642f986afcc9e252c165e44859a62defc5c697cae6f82c2943465ec10"
|
||||
}
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"%pylab is deprecated, use %matplotlib inline and import the required libraries.\n",
|
||||
"Populating the interactive namespace from numpy and matplotlib\n"
|
||||
]
|
||||
}
|
||||
|
|
@ -31,7 +32,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2022-01-07T19:16:29.118001Z",
|
||||
|
|
@ -49,7 +50,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2022-01-07T19:16:37.453883Z",
|
||||
|
|
@ -77,7 +78,7 @@
|
|||
" if args.unix: # tests the code on the unix port. Note that this works on unix only\n",
|
||||
" with open('/dev/shm/micropython.py', 'w') as fout:\n",
|
||||
" fout.write(cell)\n",
|
||||
" proc = subprocess.Popen([\"../micropython/ports/unix/micropython-2\", \"/dev/shm/micropython.py\"], \n",
|
||||
" proc = subprocess.Popen([\"../micropython/ports/unix/build-2/micropython-2\", \"/dev/shm/micropython.py\"], \n",
|
||||
" stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n",
|
||||
" print(proc.stdout.read().decode(\"utf-8\"))\n",
|
||||
" print(proc.stderr.read().decode(\"utf-8\"))\n",
|
||||
|
|
@ -182,7 +183,7 @@
|
|||
"%%micropython -pyboard 1\n",
|
||||
"\n",
|
||||
"import utime\n",
|
||||
"import ulab as np\n",
|
||||
"from ulab import numpy as np\n",
|
||||
"\n",
|
||||
"def timeit(n=1000):\n",
|
||||
" def wrapper(f, *args, **kwargs):\n",
|
||||
|
|
@ -251,7 +252,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 108,
|
||||
"execution_count": 8,
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2020-10-17T21:26:22.507996Z",
|
||||
|
|
@ -263,11 +264,11 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"array([1.0, 2.0, 3.0], dtype=float)\n",
|
||||
"array([], dtype=float)\n",
|
||||
"array([1.0, 2.0, 3.0], dtype=float64)\n",
|
||||
"array([], dtype=float64)\n",
|
||||
"[] 0\n",
|
||||
"array([1.0, 2.0, 3.0], dtype=float)\n",
|
||||
"array([], dtype=float)\n",
|
||||
"array([1.0, 2.0, 3.0], dtype=float64)\n",
|
||||
"array([], dtype=float64)\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
|
|
@ -276,7 +277,7 @@
|
|||
"source": [
|
||||
"%%micropython -unix 1\n",
|
||||
"\n",
|
||||
"import ulab as np\n",
|
||||
"from ulab import numpy as np\n",
|
||||
"\n",
|
||||
"a = np.array([1, 2, 3])\n",
|
||||
"print(a)\n",
|
||||
|
|
@ -294,13 +295,12 @@
|
|||
"print(a)\n",
|
||||
"\n",
|
||||
"b = np.ones(0) + 1\n",
|
||||
"print(b)\n",
|
||||
"# print('b', b.shape())"
|
||||
"print(b)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 122,
|
||||
"execution_count": 9,
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2020-10-17T21:54:49.123748Z",
|
||||
|
|
@ -312,7 +312,7 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"0, 1, -3array([], dtype=float)\n",
|
||||
"array([], dtype=float64)\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
|
|
@ -321,38 +321,12 @@
|
|||
"source": [
|
||||
"%%micropython -unix 1\n",
|
||||
"\n",
|
||||
"import ulab as np\n",
|
||||
"from ulab import numpy as np\n",
|
||||
"\n",
|
||||
"a = np.array([1, 2, 3])\n",
|
||||
"print(a[0:1:-3])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 127,
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2020-10-17T21:57:01.482277Z",
|
||||
"start_time": "2020-10-17T21:57:01.477362Z"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[0]"
|
||||
]
|
||||
},
|
||||
"execution_count": 127,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"l = list(range(13))\n",
|
||||
"\n",
|
||||
"l[0:10:113]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 81,
|
||||
|
|
@ -382,7 +356,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 375,
|
||||
"execution_count": 10,
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2019-10-18T13:08:28.113525Z",
|
||||
|
|
@ -394,16 +368,16 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"a: array([1.0, 2.0, 0.0, 1.0, 10.0], dtype=float)\n",
|
||||
"a: array([1.0, 2.0, 0.0, 1.0, 10.0], dtype=float64)\n",
|
||||
"min of a: 0.0\n",
|
||||
"argmin of a: 2\n",
|
||||
"\n",
|
||||
"b:\n",
|
||||
" array([[1.0, 2.0, 0.0],\n",
|
||||
"\t [1.0, 10.0, -1.0]], dtype=float)\n",
|
||||
" [1.0, 10.0, -1.0]], dtype=float64)\n",
|
||||
"min of b (flattened): -1.0\n",
|
||||
"min of b (axis=0): array([1.0, 2.0, -1.0], dtype=float)\n",
|
||||
"min of b (axis=1): array([0.0, -1.0], dtype=float)\n",
|
||||
"min of b (axis=0): array([1.0, 2.0, -1.0], dtype=float64)\n",
|
||||
"min of b (axis=1): array([0.0, -1.0], dtype=float64)\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
|
|
@ -412,19 +386,18 @@
|
|||
"source": [
|
||||
"%%micropython -unix 1\n",
|
||||
"\n",
|
||||
"import ulab as np\n",
|
||||
"from ulab import numerical\n",
|
||||
"from ulab import numpy as np\n",
|
||||
"\n",
|
||||
"a = np.array([1, 2, 0, 1, 10])\n",
|
||||
"print('a:', a)\n",
|
||||
"print('min of a:', numerical.min(a))\n",
|
||||
"print('argmin of a:', numerical.argmin(a))\n",
|
||||
"print('min of a:', np.min(a))\n",
|
||||
"print('argmin of a:', np.argmin(a))\n",
|
||||
"\n",
|
||||
"b = np.array([[1, 2, 0], [1, 10, -1]])\n",
|
||||
"print('\\nb:\\n', b)\n",
|
||||
"print('min of b (flattened):', numerical.min(b))\n",
|
||||
"print('min of b (axis=0):', numerical.min(b, axis=0))\n",
|
||||
"print('min of b (axis=1):', numerical.min(b, axis=1))"
|
||||
"print('min of b (flattened):', np.min(b))\n",
|
||||
"print('min of b (axis=0):', np.min(b, axis=0))\n",
|
||||
"print('min of b (axis=1):', np.min(b, axis=1))"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -439,12 +412,14 @@
|
|||
"\n",
|
||||
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.mean.html\n",
|
||||
"\n",
|
||||
"These three functions follow the same pattern: if the axis keyword is not specified, it assumes the default value of `None`, and returns the result of the computation for the flattened array. Otherwise, the calculation is along the given axis."
|
||||
"These three functions follow the same pattern: if the `axis` keyword is not specified, they assume the default value of `None`, and return the result of the computation for the flattened array. Otherwise, the calculation is along the given axis. \n",
|
||||
"\n",
|
||||
"If the `axis` keyword argument is a number (this can also be negative to signify counting from the rightmost axis) the functions contract the arrays, i.e., the results will have one axis fewer than the input array. The only exception to this rule is when the `keepdims` keyword argument is supplied with a value `True`, in which case, the results will have the same number of axis as the input, but the axis specified in `axis` will have a length of 1. This is useful in cases, when the output is to be broadcast with the input in subsequent computations."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 527,
|
||||
"execution_count": 12,
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2019-10-20T06:51:58.845076Z",
|
||||
|
|
@ -458,29 +433,73 @@
|
|||
"text": [
|
||||
"a: \n",
|
||||
" array([[1.0, 2.0, 3.0],\n",
|
||||
"\t [4.0, 5.0, 6.0],\n",
|
||||
"\t [7.0, 8.0, 9.0]], dtype=float)\n",
|
||||
" [4.0, 5.0, 6.0],\n",
|
||||
" [7.0, 8.0, 9.0]], dtype=float64)\n",
|
||||
"sum, flat array: 45.0\n",
|
||||
"mean, horizontal: array([2.0, 5.0, 8.0], dtype=float)\n",
|
||||
"std, vertical: array([2.44949, 2.44949, 2.44949], dtype=float)\n",
|
||||
"mean, horizontal: array([2.0, 5.0, 8.0], dtype=float64)\n",
|
||||
"std, vertical: array([2.449489742783178, 2.449489742783178, 2.449489742783178], dtype=float64)\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%%micropython -pyboard 1\n",
|
||||
"%%micropython -unix 1\n",
|
||||
"\n",
|
||||
"import ulab as np\n",
|
||||
"from ulab import numerical\n",
|
||||
"from ulab import numpy as np\n",
|
||||
"\n",
|
||||
"a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
|
||||
"print('a: \\n', a)\n",
|
||||
"\n",
|
||||
"print('sum, flat array: ', numerical.sum(a))\n",
|
||||
"print('sum, flat array: ', np.sum(a))\n",
|
||||
"\n",
|
||||
"print('mean, horizontal: ', numerical.mean(a, axis=1))\n",
|
||||
"print('mean, horizontal: ', np.mean(a, axis=1))\n",
|
||||
"\n",
|
||||
"print('std, vertical: ', numerical.std(a, axis=0))"
|
||||
"print('std, vertical: ', np.std(a, axis=0))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 52,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"a: \n",
|
||||
" array([[1.0, 2.0, 3.0],\n",
|
||||
" [4.0, 5.0, 6.0],\n",
|
||||
" [7.0, 8.0, 9.0]], dtype=float64)\n",
|
||||
"\n",
|
||||
"std, along 0th axis:\n",
|
||||
" array([2.449489742783178, 2.449489742783178, 2.449489742783178], dtype=float64)\n",
|
||||
"\n",
|
||||
"a: \n",
|
||||
" array([[1.0, 2.0, 3.0],\n",
|
||||
" [4.0, 5.0, 6.0],\n",
|
||||
" [7.0, 8.0, 9.0]], dtype=float64)\n",
|
||||
"\n",
|
||||
"std, along 1st axis, keeping dimensions:\n",
|
||||
" array([[0.8164965809277261],\n",
|
||||
" [0.8164965809277261],\n",
|
||||
" [0.8164965809277261]], dtype=float64)\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%%micropython -unix 1\n",
|
||||
"\n",
|
||||
"from ulab import numpy as np\n",
|
||||
"\n",
|
||||
"a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
|
||||
"print('a: \\n', a)\n",
|
||||
"print('\\nstd, along 0th axis:\\n', np.std(a, axis=0))\n",
|
||||
"\n",
|
||||
"print('\\na: \\n', a)\n",
|
||||
"print('\\nstd, along 1st axis, keeping dimensions:\\n', np.std(a, axis=1, keepdims=True))"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -519,13 +538,12 @@
|
|||
"source": [
|
||||
"%%micropython -unix 1\n",
|
||||
"\n",
|
||||
"import ulab as np\n",
|
||||
"from ulab import numerical\n",
|
||||
"from ulab import numpy as np\n",
|
||||
"\n",
|
||||
"a = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n",
|
||||
"print(\"a:\\t\\t\\t\", a)\n",
|
||||
"\n",
|
||||
"numerical.roll(a, 2)\n",
|
||||
"np.roll(a, 2)\n",
|
||||
"print(\"a rolled to the left:\\t\", a)\n",
|
||||
"\n",
|
||||
"# this should be the original vector\n",
|
||||
|
|
@ -581,19 +599,18 @@
|
|||
"source": [
|
||||
"%%micropython -unix 1\n",
|
||||
"\n",
|
||||
"import ulab as np\n",
|
||||
"from ulab import numerical\n",
|
||||
"from ulab import numpy as np\n",
|
||||
"\n",
|
||||
"a = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n",
|
||||
"print(\"a:\\n\", a)\n",
|
||||
"\n",
|
||||
"numerical.roll(a, 2)\n",
|
||||
"np.roll(a, 2)\n",
|
||||
"print(\"\\na rolled to the left:\\n\", a)\n",
|
||||
"\n",
|
||||
"numerical.roll(a, -1, axis=1)\n",
|
||||
"np.roll(a, -1, axis=1)\n",
|
||||
"print(\"\\na rolled up:\\n\", a)\n",
|
||||
"\n",
|
||||
"numerical.roll(a, 1, axis=None)\n",
|
||||
"np.roll(a, 1, axis=None)\n",
|
||||
"print(\"\\na rolled with None:\\n\", a)"
|
||||
]
|
||||
},
|
||||
|
|
@ -649,9 +666,7 @@
|
|||
"source": [
|
||||
"%%micropython -unix 1\n",
|
||||
"\n",
|
||||
"import ulab as np\n",
|
||||
"from ulab import numerical\n",
|
||||
"from ulab import vector\n",
|
||||
"from ulab import numpy as np\n",
|
||||
"\n",
|
||||
"def dummy_adc():\n",
|
||||
" # dummy adc function, so that the results are reproducible\n",
|
||||
|
|
@ -659,8 +674,8 @@
|
|||
" \n",
|
||||
"n = 10\n",
|
||||
"# These are the normalised weights; the last entry is the most dominant\n",
|
||||
"weight = vector.exp([1, 2, 3, 4, 5])\n",
|
||||
"weight = weight/numerical.sum(weight)\n",
|
||||
"weight = np.exp([1, 2, 3, 4, 5])\n",
|
||||
"weight = weight/np.sum(weight)\n",
|
||||
"\n",
|
||||
"print(weight)\n",
|
||||
"# initial array of samples\n",
|
||||
|
|
@ -669,10 +684,10 @@
|
|||
"for i in range(n):\n",
|
||||
" # a new datum is inserted on the right hand side. This simply overwrites whatever was in the last slot\n",
|
||||
" samples[-1] = dummy_adc()\n",
|
||||
" print(numerical.mean(samples[-5:]*weight))\n",
|
||||
" print(np.mean(samples[-5:]*weight))\n",
|
||||
" print(samples[-5:])\n",
|
||||
" # the data are shifted by one position to the left\n",
|
||||
" numerical.roll(samples, 1)"
|
||||
" numerical.np(samples, 1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -725,17 +740,16 @@
|
|||
"source": [
|
||||
"%%micropython -unix 1\n",
|
||||
"\n",
|
||||
"import ulab as np\n",
|
||||
"from ulab import numerical\n",
|
||||
"from ulab import numpy as np\n",
|
||||
"\n",
|
||||
"a = np.array([1, 2, 3, 4, 5])\n",
|
||||
"print(\"a: \\t\", a)\n",
|
||||
"print(\"a flipped:\\t\", np.flip(a))\n",
|
||||
"\n",
|
||||
"a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.uint8)\n",
|
||||
"print(\"\\na flipped horizontally\\n\", numerical.flip(a, axis=1))\n",
|
||||
"print(\"\\na flipped vertically\\n\", numerical.flip(a, axis=0))\n",
|
||||
"print(\"\\na flipped horizontally+vertically\\n\", numerical.flip(a))"
|
||||
"print(\"\\na flipped horizontally\\n\", np.flip(a, axis=1))\n",
|
||||
"print(\"\\na flipped vertically\\n\", np.flip(a, axis=0))\n",
|
||||
"print(\"\\na flipped horizontally+vertically\\n\", np.flip(a))"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -800,19 +814,18 @@
|
|||
"source": [
|
||||
"%%micropython -unix 1\n",
|
||||
"\n",
|
||||
"import ulab as np\n",
|
||||
"from ulab import numerical\n",
|
||||
"from ulab import numpy as np\n",
|
||||
"\n",
|
||||
"a = np.array(range(9), dtype=np.uint8)\n",
|
||||
"print('a:\\n', a)\n",
|
||||
"\n",
|
||||
"print('\\nfirst derivative:\\n', numerical.diff(a, n=1))\n",
|
||||
"print('\\nsecond derivative:\\n', numerical.diff(a, n=2))\n",
|
||||
"print('\\nfirst derivative:\\n', np.diff(a, n=1))\n",
|
||||
"print('\\nsecond derivative:\\n', np.diff(a, n=2))\n",
|
||||
"\n",
|
||||
"c = np.array([[1, 2, 3, 4], [4, 3, 2, 1], [1, 4, 9, 16], [0, 0, 0, 0]])\n",
|
||||
"print('\\nc:\\n', c)\n",
|
||||
"print('\\nfirst derivative, first axis:\\n', numerical.diff(c, axis=0))\n",
|
||||
"print('\\nfirst derivative, second axis:\\n', numerical.diff(c, axis=1))"
|
||||
"print('\\nfirst derivative, first axis:\\n', np.diff(c, axis=0))\n",
|
||||
"print('\\nfirst derivative, second axis:\\n', np.diff(c, axis=1))"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -858,7 +871,7 @@
|
|||
"source": [
|
||||
"%%micropython -unix 1\n",
|
||||
"\n",
|
||||
"import ulab as np\n",
|
||||
"from ulab import numpy as np\n",
|
||||
"\n",
|
||||
"a = np.array(range(12), dtype=np.int8).reshape((3, 4))\n",
|
||||
"print('a:\\n', a)\n",
|
||||
|
|
@ -925,18 +938,17 @@
|
|||
"source": [
|
||||
"%%micropython -unix 1\n",
|
||||
"\n",
|
||||
"import ulab as np\n",
|
||||
"from ulab import numerical\n",
|
||||
"from ulab import numpy as np\n",
|
||||
"\n",
|
||||
"a = np.array([[1, 12, 3, 0], [5, 3, 4, 1], [9, 11, 1, 8], [7, 10, 0, 1]], dtype=np.float)\n",
|
||||
"print('\\na:\\n', a)\n",
|
||||
"b = numerical.sort(a, axis=0)\n",
|
||||
"b = np.sort(a, axis=0)\n",
|
||||
"print('\\na sorted along vertical axis:\\n', b)\n",
|
||||
"\n",
|
||||
"c = numerical.sort(a, axis=1)\n",
|
||||
"c = np.sort(a, axis=1)\n",
|
||||
"print('\\na sorted along horizontal axis:\\n', c)\n",
|
||||
"\n",
|
||||
"c = numerical.sort(a, axis=None)\n",
|
||||
"c = np.sort(a, axis=None)\n",
|
||||
"print('\\nflattened a sorted:\\n', c)"
|
||||
]
|
||||
},
|
||||
|
|
@ -955,15 +967,13 @@
|
|||
"source": [
|
||||
"%%micropython -pyboard 1\n",
|
||||
"\n",
|
||||
"import ulab as np\n",
|
||||
"from ulab import vector\n",
|
||||
"from ulab import numerical\n",
|
||||
"from ulab import numpy as np\n",
|
||||
"\n",
|
||||
"@timeit\n",
|
||||
"def sort_time(array):\n",
|
||||
" return numerical.sort(array)\n",
|
||||
" return np.sort(array)\n",
|
||||
"\n",
|
||||
"b = vector.sin(np.linspace(0, 6.28, num=1000))\n",
|
||||
"b = np.sin(np.linspace(0, 6.28, num=1000))\n",
|
||||
"print('b: ', b)\n",
|
||||
"sort_time(b)\n",
|
||||
"print('\\nb sorted:\\n', b)"
|
||||
|
|
@ -1025,18 +1035,17 @@
|
|||
"source": [
|
||||
"%%micropython -unix 1\n",
|
||||
"\n",
|
||||
"import ulab as np\n",
|
||||
"from ulab import numerical\n",
|
||||
"from ulab import numpy as np\n",
|
||||
"\n",
|
||||
"a = np.array([[1, 12, 3, 0], [5, 3, 4, 1], [9, 11, 1, 8], [7, 10, 0, 1]], dtype=np.float)\n",
|
||||
"print('\\na:\\n', a)\n",
|
||||
"b = numerical.argsort(a, axis=0)\n",
|
||||
"b = np.argsort(a, axis=0)\n",
|
||||
"print('\\na sorted along vertical axis:\\n', b)\n",
|
||||
"\n",
|
||||
"c = numerical.argsort(a, axis=1)\n",
|
||||
"c = np.argsort(a, axis=1)\n",
|
||||
"print('\\na sorted along horizontal axis:\\n', c)\n",
|
||||
"\n",
|
||||
"c = numerical.argsort(a, axis=None)\n",
|
||||
"c = np.argsort(a, axis=None)\n",
|
||||
"print('\\nflattened a sorted:\\n', c)"
|
||||
]
|
||||
},
|
||||
|
|
@ -1078,12 +1087,11 @@
|
|||
"source": [
|
||||
"%%micropython -unix 1\n",
|
||||
"\n",
|
||||
"import ulab as np\n",
|
||||
"from ulab import numerical\n",
|
||||
"from ulab import numpy as np\n",
|
||||
"\n",
|
||||
"a = np.array([0, 5, 1, 3, 2, 4], dtype=np.uint8)\n",
|
||||
"print('\\na:\\n', a)\n",
|
||||
"b = numerical.argsort(a, axis=1)\n",
|
||||
"b = np.argsort(a, axis=1)\n",
|
||||
"print('\\nsorting indices:\\n', b)\n",
|
||||
"print('\\nthe original array:\\n', a)"
|
||||
]
|
||||
|
|
@ -1091,7 +1099,7 @@
|
|||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"display_name": "base",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
|
|
@ -1105,7 +1113,7 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.5"
|
||||
"version": "3.11.7"
|
||||
},
|
||||
"toc": {
|
||||
"base_numbering": 1,
|
||||
|
|
|
|||
Loading…
Reference in a new issue