update diag doc, re-organise ulab-ndarray docs
This commit is contained in:
parent
72f7df520a
commit
f2502fa037
4 changed files with 239 additions and 145 deletions
|
|
@ -27,7 +27,8 @@ 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.0'
|
release = '5.0.2'
|
||||||
|
|
||||||
|
|
||||||
# -- General configuration ---------------------------------------------------
|
# -- General configuration ---------------------------------------------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -250,7 +250,7 @@ and will, therefore, be faster. Keep this in mind, whenever the output
|
||||||
type, or performance is important.
|
type, or performance is important.
|
||||||
|
|
||||||
Array initialisation functions
|
Array initialisation functions
|
||||||
------------------------------
|
==============================
|
||||||
|
|
||||||
There are nine functions that can be used for initialising an array.
|
There are nine functions that can be used for initialising an array.
|
||||||
Starred functions accept ``complex`` as the value of the ``dtype``, if
|
Starred functions accept ``complex`` as the value of the ``dtype``, if
|
||||||
|
|
@ -269,7 +269,7 @@ the firmware was compiled with complex support.
|
||||||
11. `numpy.zeros\* <#zeros>`__
|
11. `numpy.zeros\* <#zeros>`__
|
||||||
|
|
||||||
arange
|
arange
|
||||||
~~~~~~
|
------
|
||||||
|
|
||||||
``numpy``:
|
``numpy``:
|
||||||
https://numpy.org/doc/stable/reference/generated/numpy.arange.html
|
https://numpy.org/doc/stable/reference/generated/numpy.arange.html
|
||||||
|
|
@ -300,7 +300,7 @@ keyword argument.
|
||||||
|
|
||||||
|
|
||||||
concatenate
|
concatenate
|
||||||
~~~~~~~~~~~
|
-----------
|
||||||
|
|
||||||
``numpy``:
|
``numpy``:
|
||||||
https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html
|
https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html
|
||||||
|
|
@ -387,14 +387,19 @@ https://numpy.org/doc/stable/reference/generated/numpy.diag.html
|
||||||
|
|
||||||
Extract a diagonal, or construct a diagonal array.
|
Extract a diagonal, or construct a diagonal array.
|
||||||
|
|
||||||
The function takes two arguments, an ``ndarray``, and a shift. If the
|
The function takes a positional argument, an ``ndarray``, or any
|
||||||
first argument is a two-dimensional array, the function returns a
|
``micropython`` iterable, and an optional keyword argument, a shift,
|
||||||
one-dimensional array containing the diagonal entries. The diagonal can
|
with a default value of 0. If the first argument is a two-dimensional
|
||||||
be shifted by an amount given in the second argument.
|
array (or a two-dimensional iterable, e.g., a list of lists), the
|
||||||
|
function returns a one-dimensional array containing the diagonal
|
||||||
|
entries. The diagonal can be shifted by an amount given in the second
|
||||||
|
argument. If the shift is larger than the length of the corresponding
|
||||||
|
axis, an empty array is returned.
|
||||||
|
|
||||||
If the first argument is a one-dimensional array, the function returns a
|
If the first argument is a one-dimensional array, the function returns a
|
||||||
two-dimensional tensor with its diagonal elements given by the first
|
two-dimensional square tensor with its diagonal elements given by the
|
||||||
argument.
|
first argument. Again, the diagonal be shifted by an amount given by the
|
||||||
|
keyword argument.
|
||||||
|
|
||||||
The ``diag`` function can accept a complex array, if the firmware was
|
The ``diag`` function can accept a complex array, if the firmware was
|
||||||
compiled with complex support.
|
compiled with complex support.
|
||||||
|
|
@ -405,15 +410,34 @@ compiled with complex support.
|
||||||
|
|
||||||
from ulab import numpy as np
|
from ulab import numpy as np
|
||||||
|
|
||||||
a = np.array([1, 2, 3, 4])
|
a = np.array([1, 2, 3], dtype=np.uint8)
|
||||||
print(np.diag(a))
|
print(np.diag(a))
|
||||||
|
|
||||||
|
print('\ndiagonal shifted by 2')
|
||||||
|
print(np.diag(a, k=2))
|
||||||
|
|
||||||
|
print('\ndiagonal shifted by -2')
|
||||||
|
print(np.diag(a, k=-2))
|
||||||
|
|
||||||
.. parsed-literal::
|
.. parsed-literal::
|
||||||
|
|
||||||
array([[1.0, 0.0, 0.0, 0.0],
|
array([[1, 0, 0],
|
||||||
[0.0, 2.0, 0.0, 0.0],
|
[0, 2, 0],
|
||||||
[0.0, 0.0, 3.0, 0.0],
|
[0, 0, 3]], dtype=uint8)
|
||||||
[0.0, 0.0, 0.0, 4.0]], dtype=float64)
|
|
||||||
|
diagonal shifted by 2
|
||||||
|
array([[0, 0, 1, 0, 0],
|
||||||
|
[0, 0, 0, 2, 0],
|
||||||
|
[0, 0, 0, 0, 3],
|
||||||
|
[0, 0, 0, 0, 0],
|
||||||
|
[0, 0, 0, 0, 0]], dtype=uint8)
|
||||||
|
|
||||||
|
diagonal shifted by -2
|
||||||
|
array([[0, 0, 0, 0, 0],
|
||||||
|
[0, 0, 0, 0, 0],
|
||||||
|
[1, 0, 0, 0, 0],
|
||||||
|
[0, 2, 0, 0, 0],
|
||||||
|
[0, 0, 3, 0, 0]], dtype=uint8)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -424,19 +448,38 @@ compiled with complex support.
|
||||||
|
|
||||||
from ulab import numpy as np
|
from ulab import numpy as np
|
||||||
|
|
||||||
a = np.array(range(16)).reshape((4, 4))
|
a = np.arange(16).reshape((4, 4))
|
||||||
print('a: ', a)
|
print(a)
|
||||||
print()
|
print('\ndiagonal of a:')
|
||||||
print('diagonal of a: ', np.diag(a))
|
print(np.diag(a))
|
||||||
|
|
||||||
|
print('\ndiagonal of a:')
|
||||||
|
print(np.diag(a))
|
||||||
|
|
||||||
|
print('\ndiagonal of a, shifted by 2')
|
||||||
|
print(np.diag(a, k=2))
|
||||||
|
|
||||||
|
print('\ndiagonal of a, shifted by 5')
|
||||||
|
print(np.diag(a, k=5))
|
||||||
|
|
||||||
.. parsed-literal::
|
.. parsed-literal::
|
||||||
|
|
||||||
a: array([[0.0, 1.0, 2.0, 3.0],
|
array([[0, 1, 2, 3],
|
||||||
[4.0, 5.0, 6.0, 7.0],
|
[4, 5, 6, 7],
|
||||||
[8.0, 9.0, 10.0, 11.0],
|
[8, 9, 10, 11],
|
||||||
[12.0, 13.0, 14.0, 15.0]], dtype=float64)
|
[12, 13, 14, 15]], dtype=int16)
|
||||||
|
|
||||||
diagonal of a: array([0.0, 5.0, 10.0, 15.0], dtype=float64)
|
diagonal of a:
|
||||||
|
array([0, 5, 10, 15], dtype=int16)
|
||||||
|
|
||||||
|
diagonal of a:
|
||||||
|
array([0, 5, 10, 15], dtype=int16)
|
||||||
|
|
||||||
|
diagonal of a, shifted by 2
|
||||||
|
array([2, 7], dtype=int16)
|
||||||
|
|
||||||
|
diagonal of a, shifted by 5
|
||||||
|
array([], dtype=int16)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -454,7 +497,7 @@ The ``empty`` function can accept complex as the value of the dtype, if
|
||||||
the firmware was compiled with complex support.
|
the firmware was compiled with complex support.
|
||||||
|
|
||||||
eye
|
eye
|
||||||
~~~
|
---
|
||||||
|
|
||||||
``numpy``:
|
``numpy``:
|
||||||
https://docs.scipy.org/doc/numpy/reference/generated/numpy.eye.html
|
https://docs.scipy.org/doc/numpy/reference/generated/numpy.eye.html
|
||||||
|
|
@ -475,7 +518,7 @@ The ``eye`` function can accept ``complex`` as the value of the
|
||||||
``dtype``, if the firmware was compiled with complex support.
|
``dtype``, if the firmware was compiled with complex support.
|
||||||
|
|
||||||
With a single argument
|
With a single argument
|
||||||
^^^^^^^^^^^^^^^^^^^^^^
|
~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
.. code::
|
.. code::
|
||||||
|
|
||||||
|
|
@ -497,7 +540,7 @@ With a single argument
|
||||||
|
|
||||||
|
|
||||||
Specifying the dimensions of the matrix
|
Specifying the dimensions of the matrix
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
.. code::
|
.. code::
|
||||||
|
|
||||||
|
|
@ -536,7 +579,7 @@ Specifying the dimensions of the matrix
|
||||||
|
|
||||||
|
|
||||||
frombuffer
|
frombuffer
|
||||||
~~~~~~~~~~
|
----------
|
||||||
|
|
||||||
``numpy``:
|
``numpy``:
|
||||||
https://numpy.org/doc/stable/reference/generated/numpy.frombuffer.html
|
https://numpy.org/doc/stable/reference/generated/numpy.frombuffer.html
|
||||||
|
|
@ -582,7 +625,7 @@ a default of -1, meaning that all data are taken in.
|
||||||
|
|
||||||
|
|
||||||
full
|
full
|
||||||
~~~~
|
----
|
||||||
|
|
||||||
``numpy``:
|
``numpy``:
|
||||||
https://docs.scipy.org/doc/numpy/reference/generated/numpy.full.html
|
https://docs.scipy.org/doc/numpy/reference/generated/numpy.full.html
|
||||||
|
|
@ -622,7 +665,7 @@ value of ``dtype``, if the firmware was compiled with complex support.
|
||||||
|
|
||||||
|
|
||||||
linspace
|
linspace
|
||||||
~~~~~~~~
|
--------
|
||||||
|
|
||||||
``numpy``:
|
``numpy``:
|
||||||
https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html
|
https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html
|
||||||
|
|
@ -671,7 +714,7 @@ complex scalar.
|
||||||
|
|
||||||
|
|
||||||
logspace
|
logspace
|
||||||
~~~~~~~~
|
--------
|
||||||
|
|
||||||
``linspace``\ ’ equivalent for logarithmically spaced data is
|
``linspace``\ ’ equivalent for logarithmically spaced data is
|
||||||
``logspace``. This function produces a sequence of numbers, in which the
|
``logspace``. This function produces a sequence of numbers, in which the
|
||||||
|
|
@ -723,7 +766,7 @@ also accepts the ``base`` argument. The default value is 10.
|
||||||
|
|
||||||
|
|
||||||
ones, zeros
|
ones, zeros
|
||||||
~~~~~~~~~~~
|
-----------
|
||||||
|
|
||||||
``numpy``:
|
``numpy``:
|
||||||
https://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html
|
https://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html
|
||||||
|
|
@ -793,7 +836,7 @@ larger than the maximum dimension of your firmware.
|
||||||
|
|
||||||
|
|
||||||
Customising array printouts
|
Customising array printouts
|
||||||
---------------------------
|
===========================
|
||||||
|
|
||||||
``ndarray``\ s are pretty-printed, i.e., if the number of entries along
|
``ndarray``\ s are pretty-printed, i.e., if the number of entries along
|
||||||
the last axis is larger than 10 (default value), then only the first and
|
the last axis is larger than 10 (default value), then only the first and
|
||||||
|
|
@ -817,7 +860,7 @@ last three entries will be printed. Also note that, as opposed to
|
||||||
|
|
||||||
|
|
||||||
set_printoptions
|
set_printoptions
|
||||||
~~~~~~~~~~~~~~~~
|
----------------
|
||||||
|
|
||||||
The default values can be overwritten by means of the
|
The default values can be overwritten by means of the
|
||||||
``set_printoptions`` function
|
``set_printoptions`` function
|
||||||
|
|
@ -855,7 +898,7 @@ the ellipsis, if the array is longer than ``threshold``.
|
||||||
|
|
||||||
|
|
||||||
get_printoptions
|
get_printoptions
|
||||||
~~~~~~~~~~~~~~~~
|
----------------
|
||||||
|
|
||||||
The set value of the ``threshold`` and ``edgeitems`` can be retrieved by
|
The set value of the ``threshold`` and ``edgeitems`` can be retrieved by
|
||||||
calling the ``get_printoptions`` function with no arguments. The
|
calling the ``get_printoptions`` function with no arguments. The
|
||||||
|
|
@ -878,7 +921,7 @@ function returns a *dictionary* with two keys.
|
||||||
|
|
||||||
|
|
||||||
Methods and properties of ndarrays
|
Methods and properties of ndarrays
|
||||||
----------------------------------
|
==================================
|
||||||
|
|
||||||
Arrays have several *properties* that can queried, and some methods that
|
Arrays have several *properties* that can queried, and some methods that
|
||||||
can be called. With the exception of the flatten and transpose
|
can be called. With the exception of the flatten and transpose
|
||||||
|
|
@ -905,7 +948,7 @@ compiled with complex support.
|
||||||
16. `.sort <#.sort>`__
|
16. `.sort <#.sort>`__
|
||||||
|
|
||||||
.byteswap
|
.byteswap
|
||||||
~~~~~~~~~
|
---------
|
||||||
|
|
||||||
``numpy``
|
``numpy``
|
||||||
https://numpy.org/doc/stable/reference/generated/numpy.char.chararray.byteswap.html
|
https://numpy.org/doc/stable/reference/generated/numpy.char.chararray.byteswap.html
|
||||||
|
|
@ -951,7 +994,7 @@ value of ``inplace``.
|
||||||
|
|
||||||
|
|
||||||
.copy
|
.copy
|
||||||
~~~~~
|
-----
|
||||||
|
|
||||||
The ``.copy`` method creates a new *deep copy* of an array, i.e., the
|
The ``.copy`` method creates a new *deep copy* of an array, i.e., the
|
||||||
entries of the source array are *copied* into the target array.
|
entries of the source array are *copied* into the target array.
|
||||||
|
|
@ -978,7 +1021,7 @@ entries of the source array are *copied* into the target array.
|
||||||
|
|
||||||
|
|
||||||
.dtype
|
.dtype
|
||||||
~~~~~~
|
------
|
||||||
|
|
||||||
``numpy``:
|
``numpy``:
|
||||||
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.dtype.htm
|
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.dtype.htm
|
||||||
|
|
@ -1048,7 +1091,7 @@ the type code for signed 8-bit integers. The object definition adds
|
||||||
around 600 bytes to the firmware.
|
around 600 bytes to the firmware.
|
||||||
|
|
||||||
.flat
|
.flat
|
||||||
~~~~~
|
-----
|
||||||
|
|
||||||
numpy:
|
numpy:
|
||||||
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flat.htm
|
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flat.htm
|
||||||
|
|
@ -1103,7 +1146,7 @@ iterator itself, while flattening produces a new copy.
|
||||||
|
|
||||||
|
|
||||||
.flatten
|
.flatten
|
||||||
~~~~~~~~
|
--------
|
||||||
|
|
||||||
``numpy``:
|
``numpy``:
|
||||||
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flatten.htm
|
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flatten.htm
|
||||||
|
|
@ -1142,7 +1185,7 @@ https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flatten.htm
|
||||||
|
|
||||||
|
|
||||||
.imag
|
.imag
|
||||||
~~~~~
|
-----
|
||||||
|
|
||||||
``numpy``:
|
``numpy``:
|
||||||
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.imag.html
|
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.imag.html
|
||||||
|
|
@ -1179,7 +1222,7 @@ always ``float``, irrespective of the values.
|
||||||
|
|
||||||
|
|
||||||
.itemsize
|
.itemsize
|
||||||
~~~~~~~~~
|
---------
|
||||||
|
|
||||||
``numpy``:
|
``numpy``:
|
||||||
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.itemsize.html
|
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.itemsize.html
|
||||||
|
|
@ -1216,7 +1259,7 @@ the array.
|
||||||
|
|
||||||
|
|
||||||
.real
|
.real
|
||||||
~~~~~
|
-----
|
||||||
|
|
||||||
numpy:
|
numpy:
|
||||||
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.real.html
|
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.real.html
|
||||||
|
|
@ -1250,7 +1293,7 @@ with complex support, and returns a copy with the real part of an array.
|
||||||
|
|
||||||
|
|
||||||
.reshape
|
.reshape
|
||||||
~~~~~~~~
|
--------
|
||||||
|
|
||||||
``numpy``:
|
``numpy``:
|
||||||
https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html
|
https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html
|
||||||
|
|
@ -1291,7 +1334,7 @@ consistent with the old, a ``ValueError`` exception will be raised.
|
||||||
|
|
||||||
Note that `ndarray.reshape()` can also be called by assigning to `ndarray.shape`.
|
Note that `ndarray.reshape()` can also be called by assigning to `ndarray.shape`.
|
||||||
.shape
|
.shape
|
||||||
~~~~~~
|
------
|
||||||
|
|
||||||
``numpy``:
|
``numpy``:
|
||||||
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.shape.html
|
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.shape.html
|
||||||
|
|
@ -1356,7 +1399,7 @@ By assigning a tuple to the ``.shape`` property, the array can be
|
||||||
|
|
||||||
|
|
||||||
.size
|
.size
|
||||||
~~~~~
|
-----
|
||||||
|
|
||||||
``numpy``:
|
``numpy``:
|
||||||
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.size.html
|
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.size.html
|
||||||
|
|
@ -1398,7 +1441,7 @@ The ``.T`` property of the ``ndarray`` is equivalent to
|
||||||
`.transpose <#.transpose>`__.
|
`.transpose <#.transpose>`__.
|
||||||
|
|
||||||
.tobytes
|
.tobytes
|
||||||
~~~~~~~~
|
--------
|
||||||
|
|
||||||
``numpy``:
|
``numpy``:
|
||||||
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.tobytes.html
|
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.tobytes.html
|
||||||
|
|
@ -1444,7 +1487,7 @@ not dense (i.e., it has already been sliced).
|
||||||
|
|
||||||
|
|
||||||
.tolist
|
.tolist
|
||||||
~~~~~~~
|
-------
|
||||||
|
|
||||||
``numpy``:
|
``numpy``:
|
||||||
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.tolist.html
|
https://numpy.org/doc/stable/reference/generated/numpy.ndarray.tolist.html
|
||||||
|
|
@ -1482,7 +1525,7 @@ into a (nested) ``python`` lists.
|
||||||
|
|
||||||
|
|
||||||
.transpose
|
.transpose
|
||||||
~~~~~~~~~~
|
----------
|
||||||
|
|
||||||
``numpy``:
|
``numpy``:
|
||||||
https://docs.scipy.org/doc/numpy/reference/generated/numpy.transpose.html
|
https://docs.scipy.org/doc/numpy/reference/generated/numpy.transpose.html
|
||||||
|
|
@ -1550,7 +1593,7 @@ property:
|
||||||
|
|
||||||
|
|
||||||
.sort
|
.sort
|
||||||
~~~~~
|
-----
|
||||||
|
|
||||||
``numpy``:
|
``numpy``:
|
||||||
https://docs.scipy.org/doc/numpy/reference/generated/numpy.sort.html
|
https://docs.scipy.org/doc/numpy/reference/generated/numpy.sort.html
|
||||||
|
|
@ -1605,13 +1648,13 @@ In-place sorting of an ``ndarray``. For a more detailed exposition, see
|
||||||
|
|
||||||
|
|
||||||
Unary operators
|
Unary operators
|
||||||
---------------
|
===============
|
||||||
|
|
||||||
With the exception of ``len``, which returns a single number, all unary
|
With the exception of ``len``, which returns a single number, all unary
|
||||||
operators manipulate the underlying data element-wise.
|
operators manipulate the underlying data element-wise.
|
||||||
|
|
||||||
len
|
len
|
||||||
~~~
|
---
|
||||||
|
|
||||||
This operator takes a single argument, the array, and returns either the
|
This operator takes a single argument, the array, and returns either the
|
||||||
length of the first axis.
|
length of the first axis.
|
||||||
|
|
@ -1652,7 +1695,7 @@ The number returned by ``len`` is also the length of the iterations,
|
||||||
when the array supplies the elements for an iteration (see later).
|
when the array supplies the elements for an iteration (see later).
|
||||||
|
|
||||||
invert
|
invert
|
||||||
~~~~~~
|
------
|
||||||
|
|
||||||
The function is defined for integer data types (``uint8``, ``int8``,
|
The function is defined for integer data types (``uint8``, ``int8``,
|
||||||
``uint16``, and ``int16``) only, takes a single argument, and returns
|
``uint16``, and ``int16``) only, takes a single argument, and returns
|
||||||
|
|
@ -1688,7 +1731,7 @@ unexpected, as in the example below:
|
||||||
|
|
||||||
|
|
||||||
abs
|
abs
|
||||||
~~~
|
---
|
||||||
|
|
||||||
This function takes a single argument, and returns the
|
This function takes a single argument, and returns the
|
||||||
element-by-element absolute value of the array. When the data type is
|
element-by-element absolute value of the array. When the data type is
|
||||||
|
|
@ -1714,7 +1757,7 @@ returned immediately, and no calculation takes place.
|
||||||
|
|
||||||
|
|
||||||
neg
|
neg
|
||||||
~~~
|
---
|
||||||
|
|
||||||
This operator takes a single argument, and changes the sign of each
|
This operator takes a single argument, and changes the sign of each
|
||||||
element in the array. Unsigned values are wrapped.
|
element in the array. Unsigned values are wrapped.
|
||||||
|
|
@ -1745,7 +1788,7 @@ element in the array. Unsigned values are wrapped.
|
||||||
|
|
||||||
|
|
||||||
pos
|
pos
|
||||||
~~~
|
---
|
||||||
|
|
||||||
This function takes a single argument, and simply returns a copy of the
|
This function takes a single argument, and simply returns a copy of the
|
||||||
array.
|
array.
|
||||||
|
|
@ -1769,7 +1812,7 @@ array.
|
||||||
|
|
||||||
|
|
||||||
Binary operators
|
Binary operators
|
||||||
----------------
|
================
|
||||||
|
|
||||||
``ulab`` implements the ``+``, ``-``, ``*``, ``/``, ``**``, ``<``,
|
``ulab`` implements the ``+``, ``-``, ``*``, ``/``, ``**``, ``<``,
|
||||||
``>``, ``<=``, ``>=``, ``==``, ``!=``, ``+=``, ``-=``, ``*=``, ``/=``,
|
``>``, ``<=``, ``>=``, ``==``, ``!=``, ``+=``, ``-=``, ``*=``, ``/=``,
|
||||||
|
|
@ -1826,7 +1869,7 @@ exception:
|
||||||
on `array comparison <#Comparison-of-arrays>`__ for details.
|
on `array comparison <#Comparison-of-arrays>`__ for details.
|
||||||
|
|
||||||
Upcasting
|
Upcasting
|
||||||
~~~~~~~~~
|
---------
|
||||||
|
|
||||||
Binary operations require special attention, because two arrays with
|
Binary operations require special attention, because two arrays with
|
||||||
different typecodes can be the operands of an operation, in which case
|
different typecodes can be the operands of an operation, in which case
|
||||||
|
|
@ -1849,7 +1892,9 @@ conventions.
|
||||||
``dtype``. Thus, e.g., if the scalar is 123, it will be converted
|
``dtype``. Thus, e.g., if the scalar is 123, it will be converted
|
||||||
into an array of ``dtype`` ``uint8``, while -1000 will be converted
|
into an array of ``dtype`` ``uint8``, while -1000 will be converted
|
||||||
into ``int16``. An ``mp_obj_float``, will always be promoted to
|
into ``int16``. An ``mp_obj_float``, will always be promoted to
|
||||||
``dtype`` ``float``. Other micropython types (e.g., lists, tuples,
|
``dtype`` ``float``. Similarly, if ``ulab`` supports complex arrays,
|
||||||
|
the result of a binary operation involving a ``complex`` array is
|
||||||
|
always complex. Other ``micropython`` types (e.g., lists, tuples,
|
||||||
etc.) raise a ``TypeError`` exception.
|
etc.) raise a ``TypeError`` exception.
|
||||||
|
|
||||||
4.
|
4.
|
||||||
|
|
@ -1905,7 +1950,7 @@ Upcasting can be seen in action in the following snippet:
|
||||||
|
|
||||||
|
|
||||||
Benchmarks
|
Benchmarks
|
||||||
~~~~~~~~~~
|
----------
|
||||||
|
|
||||||
The following snippet compares the performance of binary operations to a
|
The following snippet compares the performance of binary operations to a
|
||||||
possible implementation in python. For the time measurement, we will
|
possible implementation in python. For the time measurement, we will
|
||||||
|
|
@ -1998,7 +2043,7 @@ on graphical
|
||||||
displays <https://forum.micropython.org/viewtopic.php?f=15&t=5815&p=33362&hilit=ufont#p33383>`__.
|
displays <https://forum.micropython.org/viewtopic.php?f=15&t=5815&p=33362&hilit=ufont#p33383>`__.
|
||||||
|
|
||||||
Comparison operators
|
Comparison operators
|
||||||
--------------------
|
====================
|
||||||
|
|
||||||
The smaller than, greater than, smaller or equal, and greater or equal
|
The smaller than, greater than, smaller or equal, and greater or equal
|
||||||
operators return a vector of Booleans indicating the positions
|
operators return a vector of Booleans indicating the positions
|
||||||
|
|
@ -2046,7 +2091,7 @@ following code will not work:
|
||||||
|
|
||||||
|
|
||||||
Iterating over arrays
|
Iterating over arrays
|
||||||
---------------------
|
=====================
|
||||||
|
|
||||||
``ndarray``\ s are iterable, which means that their elements can also be
|
``ndarray``\ s are iterable, which means that their elements can also be
|
||||||
accessed as can the elements of a list, tuple, etc. If the array is
|
accessed as can the elements of a list, tuple, etc. If the array is
|
||||||
|
|
@ -2094,10 +2139,10 @@ reduced-dimensional *view* is created and returned.
|
||||||
|
|
||||||
|
|
||||||
Slicing and indexing
|
Slicing and indexing
|
||||||
--------------------
|
====================
|
||||||
|
|
||||||
Views vs. copies
|
Views vs. copies
|
||||||
~~~~~~~~~~~~~~~~
|
----------------
|
||||||
|
|
||||||
``numpy`` has a very important concept called *views*, which is a
|
``numpy`` has a very important concept called *views*, which is a
|
||||||
powerful extension of ``python``\ ’s own notion of slicing. Slices are
|
powerful extension of ``python``\ ’s own notion of slicing. Slices are
|
||||||
|
|
@ -2210,7 +2255,7 @@ section `Slicing and assigning to
|
||||||
slices <#Slicing-and-assigning-to-slices>`__ should clarify the issue.
|
slices <#Slicing-and-assigning-to-slices>`__ should clarify the issue.
|
||||||
|
|
||||||
Indexing
|
Indexing
|
||||||
~~~~~~~~
|
--------
|
||||||
|
|
||||||
The simplest form of indexing is specifying a single integer between the
|
The simplest form of indexing is specifying a single integer between the
|
||||||
square brackets as in
|
square brackets as in
|
||||||
|
|
@ -2373,7 +2418,7 @@ On the right hand side of the assignment we can even have another array.
|
||||||
|
|
||||||
|
|
||||||
Slicing and assigning to slices
|
Slicing and assigning to slices
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
-------------------------------
|
||||||
|
|
||||||
You can also generate sub-arrays by specifying slices as the index of an
|
You can also generate sub-arrays by specifying slices as the index of an
|
||||||
array. Slices are special python objects of the form
|
array. Slices are special python objects of the form
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,8 @@
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2022-02-01T17:41:38.040350Z",
|
"end_time": "2022-02-09T06:27:15.118699Z",
|
||||||
"start_time": "2022-02-01T17:41:38.023988Z"
|
"start_time": "2022-02-09T06:27:15.100980Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
|
|
@ -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.0'\n",
|
"release = '5.0.2'\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# -- General configuration ---------------------------------------------------\n",
|
"# -- General configuration ---------------------------------------------------\n",
|
||||||
|
|
@ -218,8 +218,8 @@
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2022-02-01T17:41:42.215395Z",
|
"end_time": "2022-02-09T06:27:21.647179Z",
|
||||||
"start_time": "2022-02-01T17:41:40.650763Z"
|
"start_time": "2022-02-09T06:27:20.019520Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
|
|
@ -259,8 +259,8 @@
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2022-01-29T20:52:14.453177Z",
|
"end_time": "2022-02-09T06:27:42.024028Z",
|
||||||
"start_time": "2022-01-29T20:52:04.020858Z"
|
"start_time": "2022-02-09T06:27:36.109093Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
|
|
|
||||||
|
|
@ -34,8 +34,8 @@
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2022-01-07T18:46:22.666663Z",
|
"end_time": "2022-02-09T06:10:18.391925Z",
|
||||||
"start_time": "2022-01-07T18:46:22.663583Z"
|
"start_time": "2022-02-09T06:10:18.388146Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
|
|
@ -52,8 +52,8 @@
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"ExecuteTime": {
|
"ExecuteTime": {
|
||||||
"end_time": "2022-01-07T18:46:29.198681Z",
|
"end_time": "2022-02-09T06:10:19.000982Z",
|
||||||
"start_time": "2022-01-07T18:46:29.177654Z"
|
"start_time": "2022-02-09T06:10:18.979322Z"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
|
|
@ -505,7 +505,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Array initialisation functions\n",
|
"# Array initialisation functions\n",
|
||||||
"\n",
|
"\n",
|
||||||
"There are nine functions that can be used for initialising an array. Starred functions accept `complex` as the value of the `dtype`, if the firmware was compiled with complex support.\n",
|
"There are nine functions that can be used for initialising an array. Starred functions accept `complex` as the value of the `dtype`, if the firmware was compiled with complex support.\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -526,7 +526,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### arange\n",
|
"## arange\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy`: https://numpy.org/doc/stable/reference/generated/numpy.arange.html\n",
|
"`numpy`: https://numpy.org/doc/stable/reference/generated/numpy.arange.html\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -571,7 +571,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### concatenate\n",
|
"## concatenate\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy`: https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html\n",
|
"`numpy`: https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -684,26 +684,44 @@
|
||||||
"\n",
|
"\n",
|
||||||
"Extract a diagonal, or construct a diagonal array.\n",
|
"Extract a diagonal, or construct a diagonal array.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"The function takes two arguments, an `ndarray`, and a shift. If the first argument is a two-dimensional array, the function returns a one-dimensional array containing the diagonal entries. The diagonal can be shifted by an amount given in the second argument.\n",
|
"The function takes a positional argument, an `ndarray`, or any `micropython` iterable, and an optional keyword argument, a shift, with a default value of 0. If the first argument is a two-dimensional array (or a two-dimensional iterable, e.g., a list of lists), the function returns a one-dimensional array containing the diagonal entries. The diagonal can be shifted by an amount given in the second argument. If the shift is larger than the length of the corresponding axis, an empty array is returned.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"If the first argument is a one-dimensional array, the function returns a two-dimensional tensor with its diagonal elements given by the first argument.\n",
|
"If the first argument is a one-dimensional array, the function returns a two-dimensional square tensor with its diagonal elements given by the first argument. Again, the diagonal be shifted by an amount given by the keyword argument.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"The `diag` function can accept a complex array, if the firmware was compiled with complex support."
|
"The `diag` function can accept a complex array, if the firmware was compiled with complex support."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 6,
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2022-02-09T06:24:38.290495Z",
|
||||||
|
"start_time": "2022-02-09T06:24:38.273075Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"array([[1.0, 0.0, 0.0, 0.0],\n",
|
"array([[1, 0, 0],\n",
|
||||||
" [0.0, 2.0, 0.0, 0.0],\n",
|
" [0, 2, 0],\n",
|
||||||
" [0.0, 0.0, 3.0, 0.0],\n",
|
" [0, 0, 3]], dtype=uint8)\n",
|
||||||
" [0.0, 0.0, 0.0, 4.0]], dtype=float64)\n",
|
"\n",
|
||||||
|
"diagonal shifted by 2\n",
|
||||||
|
"array([[0, 0, 1, 0, 0],\n",
|
||||||
|
" [0, 0, 0, 2, 0],\n",
|
||||||
|
" [0, 0, 0, 0, 3],\n",
|
||||||
|
" [0, 0, 0, 0, 0],\n",
|
||||||
|
" [0, 0, 0, 0, 0]], dtype=uint8)\n",
|
||||||
|
"\n",
|
||||||
|
"diagonal shifted by -2\n",
|
||||||
|
"array([[0, 0, 0, 0, 0],\n",
|
||||||
|
" [0, 0, 0, 0, 0],\n",
|
||||||
|
" [1, 0, 0, 0, 0],\n",
|
||||||
|
" [0, 2, 0, 0, 0],\n",
|
||||||
|
" [0, 0, 3, 0, 0]], dtype=uint8)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n"
|
"\n"
|
||||||
]
|
]
|
||||||
|
|
@ -714,25 +732,46 @@
|
||||||
"\n",
|
"\n",
|
||||||
"from ulab import numpy as np\n",
|
"from ulab import numpy as np\n",
|
||||||
"\n",
|
"\n",
|
||||||
"a = np.array([1, 2, 3, 4])\n",
|
"a = np.array([1, 2, 3], dtype=np.uint8)\n",
|
||||||
"print(np.diag(a))"
|
"print(np.diag(a))\n",
|
||||||
|
"\n",
|
||||||
|
"print('\\ndiagonal shifted by 2')\n",
|
||||||
|
"print(np.diag(a, k=2))\n",
|
||||||
|
"\n",
|
||||||
|
"print('\\ndiagonal shifted by -2')\n",
|
||||||
|
"print(np.diag(a, k=-2))"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 11,
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2022-02-09T06:26:39.213828Z",
|
||||||
|
"start_time": "2022-02-09T06:26:39.199294Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"a: array([[0.0, 1.0, 2.0, 3.0],\n",
|
"array([[0, 1, 2, 3],\n",
|
||||||
" [4.0, 5.0, 6.0, 7.0],\n",
|
" [4, 5, 6, 7],\n",
|
||||||
" [8.0, 9.0, 10.0, 11.0],\n",
|
" [8, 9, 10, 11],\n",
|
||||||
" [12.0, 13.0, 14.0, 15.0]], dtype=float64)\n",
|
" [12, 13, 14, 15]], dtype=int16)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"diagonal of a: array([0.0, 5.0, 10.0, 15.0], dtype=float64)\n",
|
"diagonal of a:\n",
|
||||||
|
"array([0, 5, 10, 15], dtype=int16)\n",
|
||||||
|
"\n",
|
||||||
|
"diagonal of a:\n",
|
||||||
|
"array([0, 5, 10, 15], dtype=int16)\n",
|
||||||
|
"\n",
|
||||||
|
"diagonal of a, shifted by 2\n",
|
||||||
|
"array([2, 7], dtype=int16)\n",
|
||||||
|
"\n",
|
||||||
|
"diagonal of a, shifted by 5\n",
|
||||||
|
"array([], dtype=int16)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n"
|
"\n"
|
||||||
]
|
]
|
||||||
|
|
@ -743,10 +782,19 @@
|
||||||
"\n",
|
"\n",
|
||||||
"from ulab import numpy as np\n",
|
"from ulab import numpy as np\n",
|
||||||
"\n",
|
"\n",
|
||||||
"a = np.array(range(16)).reshape((4, 4))\n",
|
"a = np.arange(16).reshape((4, 4))\n",
|
||||||
"print('a: ', a)\n",
|
"print(a)\n",
|
||||||
"print()\n",
|
"print('\\ndiagonal of a:')\n",
|
||||||
"print('diagonal of a: ', np.diag(a))"
|
"print(np.diag(a))\n",
|
||||||
|
"\n",
|
||||||
|
"print('\\ndiagonal of a:')\n",
|
||||||
|
"print(np.diag(a))\n",
|
||||||
|
"\n",
|
||||||
|
"print('\\ndiagonal of a, shifted by 2')\n",
|
||||||
|
"print(np.diag(a, k=2))\n",
|
||||||
|
"\n",
|
||||||
|
"print('\\ndiagonal of a, shifted by 5')\n",
|
||||||
|
"print(np.diag(a, k=5))"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -766,7 +814,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### eye\n",
|
"## eye\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.eye.html\n",
|
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.eye.html\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -784,7 +832,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"#### With a single argument"
|
"### With a single argument"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -823,7 +871,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"#### Specifying the dimensions of the matrix"
|
"### Specifying the dimensions of the matrix"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -892,7 +940,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### frombuffer\n",
|
"## frombuffer\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy`: https://numpy.org/doc/stable/reference/generated/numpy.frombuffer.html\n",
|
"`numpy`: https://numpy.org/doc/stable/reference/generated/numpy.frombuffer.html\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -946,7 +994,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### full\n",
|
"## full\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.full.html\n",
|
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.full.html\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -998,7 +1046,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### linspace\n",
|
"## linspace\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html\n",
|
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -1052,7 +1100,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### logspace\n",
|
"## logspace\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`linspace`' equivalent for logarithmically spaced data is `logspace`. This function produces a sequence of numbers, in which the quotient of consecutive numbers is constant. This is a geometric sequence.\n",
|
"`linspace`' equivalent for logarithmically spaced data is `logspace`. This function produces a sequence of numbers, in which the quotient of consecutive numbers is constant. This is a geometric sequence.\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -1108,7 +1156,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### ones, zeros\n",
|
"## ones, zeros\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html\n",
|
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -1207,7 +1255,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Customising array printouts"
|
"# Customising array printouts"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -1250,7 +1298,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### set_printoptions\n",
|
"## set_printoptions\n",
|
||||||
"\n",
|
"\n",
|
||||||
"The default values can be overwritten by means of the `set_printoptions` function [numpy.set_printoptions](https://numpy.org/doc/1.18/reference/generated/numpy.set_printoptions.html), which accepts two keywords arguments, the `threshold`, and the `edgeitems`. The first of these arguments determines the length of the longest array that will be printed in full, while the second is the number of items that will be printed on the left and right hand side of the ellipsis, if the array is longer than `threshold`."
|
"The default values can be overwritten by means of the `set_printoptions` function [numpy.set_printoptions](https://numpy.org/doc/1.18/reference/generated/numpy.set_printoptions.html), which accepts two keywords arguments, the `threshold`, and the `edgeitems`. The first of these arguments determines the length of the longest array that will be printed in full, while the second is the number of items that will be printed on the left and right hand side of the ellipsis, if the array is longer than `threshold`."
|
||||||
]
|
]
|
||||||
|
|
@ -1298,7 +1346,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### get_printoptions\n",
|
"## get_printoptions\n",
|
||||||
"\n",
|
"\n",
|
||||||
"The set value of the `threshold` and `edgeitems` can be retrieved by calling the `get_printoptions` function with no arguments. The function returns a *dictionary* with two keys."
|
"The set value of the `threshold` and `edgeitems` can be retrieved by calling the `get_printoptions` function with no arguments. The function returns a *dictionary* with two keys."
|
||||||
]
|
]
|
||||||
|
|
@ -1336,7 +1384,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Methods and properties of ndarrays\n",
|
"# Methods and properties of ndarrays\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Arrays have several *properties* that can queried, and some methods that can be called. With the exception of the flatten and transpose operators, properties return an object that describe some feature of the array, while the methods return a new array-like object. The `imag`, and `real` properties are included in the firmware only, when it was compiled with complex support.\n",
|
"Arrays have several *properties* that can queried, and some methods that can be called. With the exception of the flatten and transpose operators, properties return an object that describe some feature of the array, while the methods return a new array-like object. The `imag`, and `real` properties are included in the firmware only, when it was compiled with complex support.\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -1362,7 +1410,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### .byteswap\n",
|
"## .byteswap\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy` https://numpy.org/doc/stable/reference/generated/numpy.char.chararray.byteswap.html\n",
|
"`numpy` https://numpy.org/doc/stable/reference/generated/numpy.char.chararray.byteswap.html\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -1413,7 +1461,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### .copy\n",
|
"## .copy\n",
|
||||||
"\n",
|
"\n",
|
||||||
"The `.copy` method creates a new *deep copy* of an array, i.e., the entries of the source array are *copied* into the target array."
|
"The `.copy` method creates a new *deep copy* of an array, i.e., the entries of the source array are *copied* into the target array."
|
||||||
]
|
]
|
||||||
|
|
@ -1456,7 +1504,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### .dtype\n",
|
"## .dtype\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.dtype.htm\n",
|
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.dtype.htm\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -1556,7 +1604,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### .flat\n",
|
"## .flat\n",
|
||||||
"\n",
|
"\n",
|
||||||
"numpy: https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flat.htm\n",
|
"numpy: https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flat.htm\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -1617,7 +1665,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### .flatten\n",
|
"## .flatten\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flatten.htm\n",
|
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flatten.htm\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -1670,7 +1718,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### .imag\n",
|
"## .imag\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy`: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.imag.html\n",
|
"`numpy`: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.imag.html\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -1719,7 +1767,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### .itemsize\n",
|
"## .itemsize\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy`: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.itemsize.html\n",
|
"`numpy`: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.itemsize.html\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -1771,7 +1819,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### .real\n",
|
"## .real\n",
|
||||||
"\n",
|
"\n",
|
||||||
"numpy: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.real.html\n",
|
"numpy: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.real.html\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -1820,7 +1868,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### .reshape\n",
|
"## .reshape\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html\n",
|
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -1877,7 +1925,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### .shape\n",
|
"## .shape\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy`: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.shape.html\n",
|
"`numpy`: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.shape.html\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -1969,7 +2017,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### .size\n",
|
"## .size\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy`: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.size.html\n",
|
"`numpy`: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.size.html\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -2030,7 +2078,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### .tobytes\n",
|
"## .tobytes\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy`: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.tobytes.html\n",
|
"`numpy`: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.tobytes.html\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -2085,7 +2133,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### .tolist\n",
|
"## .tolist\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy`: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.tolist.html\n",
|
"`numpy`: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.tolist.html\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -2138,7 +2186,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### .transpose\n",
|
"## .transpose\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.transpose.html\n",
|
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.transpose.html\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -2233,7 +2281,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### .sort\n",
|
"## .sort\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.sort.html\n",
|
"`numpy`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.sort.html\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -2303,7 +2351,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Unary operators\n",
|
"# Unary operators\n",
|
||||||
"\n",
|
"\n",
|
||||||
"With the exception of `len`, which returns a single number, all unary operators manipulate the underlying data element-wise. "
|
"With the exception of `len`, which returns a single number, all unary operators manipulate the underlying data element-wise. "
|
||||||
]
|
]
|
||||||
|
|
@ -2312,7 +2360,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### len\n",
|
"## len\n",
|
||||||
"\n",
|
"\n",
|
||||||
"This operator takes a single argument, the array, and returns either the length of the first axis."
|
"This operator takes a single argument, the array, and returns either the length of the first axis."
|
||||||
]
|
]
|
||||||
|
|
@ -2373,7 +2421,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### invert\n",
|
"## invert\n",
|
||||||
"\n",
|
"\n",
|
||||||
"The function is defined for integer data types (`uint8`, `int8`, `uint16`, and `int16`) only, takes a single argument, and returns the element-by-element, bit-wise inverse of the array. If a `float` is supplied, the function raises a `ValueError` exception.\n",
|
"The function is defined for integer data types (`uint8`, `int8`, `uint16`, and `int16`) only, takes a single argument, and returns the element-by-element, bit-wise inverse of the array. If a `float` is supplied, the function raises a `ValueError` exception.\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -2422,7 +2470,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### abs\n",
|
"## abs\n",
|
||||||
"\n",
|
"\n",
|
||||||
"This function takes a single argument, and returns the element-by-element absolute value of the array. When the data type is unsigned (`uint8`, or `uint16`), a copy of the array will be returned immediately, and no calculation takes place."
|
"This function takes a single argument, and returns the element-by-element absolute value of the array. When the data type is unsigned (`uint8`, or `uint16`), a copy of the array will be returned immediately, and no calculation takes place."
|
||||||
]
|
]
|
||||||
|
|
@ -2462,7 +2510,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### neg\n",
|
"## neg\n",
|
||||||
"\n",
|
"\n",
|
||||||
"This operator takes a single argument, and changes the sign of each element in the array. Unsigned values are wrapped. "
|
"This operator takes a single argument, and changes the sign of each element in the array. Unsigned values are wrapped. "
|
||||||
]
|
]
|
||||||
|
|
@ -2509,7 +2557,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### pos\n",
|
"## pos\n",
|
||||||
"\n",
|
"\n",
|
||||||
"This function takes a single argument, and simply returns a copy of the array."
|
"This function takes a single argument, and simply returns a copy of the array."
|
||||||
]
|
]
|
||||||
|
|
@ -2549,7 +2597,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Binary operators\n",
|
"# Binary operators\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`ulab` implements the `+`, `-`, `*`, `/`, `**`, `<`, `>`, `<=`, `>=`, `==`, `!=`, `+=`, `-=`, `*=`, `/=`, `**=` binary operators that work element-wise. Broadcasting is available, meaning that the two operands do not even have to have the same shape. If the lengths along the respective axes are equal, or one of them is 1, or the axis is missing, the element-wise operation can still be carried out. \n",
|
"`ulab` implements the `+`, `-`, `*`, `/`, `**`, `<`, `>`, `<=`, `>=`, `==`, `!=`, `+=`, `-=`, `*=`, `/=`, `**=` binary operators that work element-wise. Broadcasting is available, meaning that the two operands do not even have to have the same shape. If the lengths along the respective axes are equal, or one of them is 1, or the axis is missing, the element-wise operation can still be carried out. \n",
|
||||||
"A thorough explanation of broadcasting can be found under https://numpy.org/doc/stable/user/basics.broadcasting.html. \n",
|
"A thorough explanation of broadcasting can be found under https://numpy.org/doc/stable/user/basics.broadcasting.html. \n",
|
||||||
|
|
@ -2635,7 +2683,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### Upcasting\n",
|
"## Upcasting\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Binary operations require special attention, because two arrays with different typecodes can be the operands of an operation, in which case it is not trivial, what the typecode of the result is. This decision on the result's typecode is called upcasting. Since the number of typecodes in `ulab` is significantly smaller than in `numpy`, we have to define new upcasting rules. Where possible, I followed `numpy`'s conventions. \n",
|
"Binary operations require special attention, because two arrays with different typecodes can be the operands of an operation, in which case it is not trivial, what the typecode of the result is. This decision on the result's typecode is called upcasting. Since the number of typecodes in `ulab` is significantly smaller than in `numpy`, we have to define new upcasting rules. Where possible, I followed `numpy`'s conventions. \n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -2645,7 +2693,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"2. if either of the operands is a float, the result is automatically a float\n",
|
"2. if either of the operands is a float, the result is automatically a float\n",
|
||||||
"\n",
|
"\n",
|
||||||
"3. When one of the operands is a scalar, it will internally be turned into a single-element `ndarray` with the *smallest* possible `dtype`. Thus, e.g., if the scalar is 123, it will be converted into an array of `dtype` `uint8`, while -1000 will be converted into `int16`. An `mp_obj_float`, will always be promoted to `dtype` `float`. Other micropython types (e.g., lists, tuples, etc.) raise a `TypeError` exception. \n",
|
"3. When one of the operands is a scalar, it will internally be turned into a single-element `ndarray` with the *smallest* possible `dtype`. Thus, e.g., if the scalar is 123, it will be converted into an array of `dtype` `uint8`, while -1000 will be converted into `int16`. An `mp_obj_float`, will always be promoted to `dtype` `float`. Similarly, if `ulab` supports complex arrays, the result of a binary operation involving a `complex` array is always complex. Other `micropython` types (e.g., lists, tuples, etc.) raise a `TypeError` exception. \n",
|
||||||
"\n",
|
"\n",
|
||||||
"4. \n",
|
"4. \n",
|
||||||
" \n",
|
" \n",
|
||||||
|
|
@ -2712,7 +2760,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### Benchmarks\n",
|
"## Benchmarks\n",
|
||||||
"\n",
|
"\n",
|
||||||
"The following snippet compares the performance of binary operations to a possible implementation in python. For the time measurement, we will take the following snippet from the micropython manual:"
|
"The following snippet compares the performance of binary operations to a possible implementation in python. For the time measurement, we will take the following snippet from the micropython manual:"
|
||||||
]
|
]
|
||||||
|
|
@ -2830,7 +2878,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Comparison operators\n",
|
"# Comparison operators\n",
|
||||||
"\n",
|
"\n",
|
||||||
"The smaller than, greater than, smaller or equal, and greater or equal operators return a vector of Booleans indicating the positions (`True`), where the condition is satisfied. "
|
"The smaller than, greater than, smaller or equal, and greater or equal operators return a vector of Booleans indicating the positions (`True`), where the condition is satisfied. "
|
||||||
]
|
]
|
||||||
|
|
@ -2903,7 +2951,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Iterating over arrays\n",
|
"# Iterating over arrays\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`ndarray`s are iterable, which means that their elements can also be accessed as can the elements of a list, tuple, etc. If the array is one-dimensional, the iterator returns scalars, otherwise a new reduced-dimensional *view* is created and returned."
|
"`ndarray`s are iterable, which means that their elements can also be accessed as can the elements of a list, tuple, etc. If the array is one-dimensional, the iterator returns scalars, otherwise a new reduced-dimensional *view* is created and returned."
|
||||||
]
|
]
|
||||||
|
|
@ -2965,10 +3013,10 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Slicing and indexing\n",
|
"# Slicing and indexing\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"### Views vs. copies\n",
|
"## Views vs. copies\n",
|
||||||
"\n",
|
"\n",
|
||||||
"`numpy` has a very important concept called *views*, which is a powerful extension of `python`'s own notion of slicing. Slices are special python objects of the form\n",
|
"`numpy` has a very important concept called *views*, which is a powerful extension of `python`'s own notion of slicing. Slices are special python objects of the form\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
@ -3114,7 +3162,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### Indexing\n",
|
"## Indexing\n",
|
||||||
"\n",
|
"\n",
|
||||||
"The simplest form of indexing is specifying a single integer between the square brackets as in "
|
"The simplest form of indexing is specifying a single integer between the square brackets as in "
|
||||||
]
|
]
|
||||||
|
|
@ -3373,7 +3421,7 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"### Slicing and assigning to slices\n",
|
"## Slicing and assigning to slices\n",
|
||||||
"\n",
|
"\n",
|
||||||
"You can also generate sub-arrays by specifying slices as the index of an array. Slices are special python objects of the form "
|
"You can also generate sub-arrays by specifying slices as the index of an array. Slices are special python objects of the form "
|
||||||
]
|
]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue