Skip to content

Commit

Permalink
honed the explanation on extending ulab
Browse files Browse the repository at this point in the history
  • Loading branch information
v923z committed Nov 4, 2019
1 parent 5f8a5d8 commit d12785a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/manual/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
author = 'Zoltán Vörös'

# The full version, including alpha/beta/rc tags
release = '0.251'
release = '0.252'


# -- General configuration ---------------------------------------------------
Expand Down
36 changes: 29 additions & 7 deletions docs/manual/source/ulab.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2711,7 +2711,7 @@ same way as in ``fft``, and ``ifft``.
As such, ``spectrum`` is really just a short hand for
As such, ``spectrum`` is really just a shorthand for
``np.sqrt(a*a + b*b)``:

.. code::
Expand Down Expand Up @@ -2855,6 +2855,28 @@ and ``typecode`` is one of the values from
NDARRAY_FLOAT = 'f',
};
or

.. code:: c
enum NDARRAY_TYPE {
NDARRAY_UINT8 = 'B',
NDARRAY_INT8 = 'b',
NDARRAY_UINT16 = 'H',
NDARRAY_INT16 = 'h',
NDARRAY_FLOAT = 'd',
};
The ambiguity is caused by the fact that not all platforms implement
``double``, and there one has to take ``float``\ s. But you haven’t
actually got to be concerned by this, because at the very beginning of
``ndarray.h``, this is already taken care of: the preprocessor figures
out, what the ``float`` implementation of the hardware platform is, and
defines the ``NDARRAY_FLOAT`` typecode accordingly. All you have to keep
in mind is that wherever you would use ``float`` or ``double``, you have
to use ``mp_float_t``. That type is defined in ``py/mpconfig.h`` of the
micropython code base.

Therefore, a 4-by-5 matrix of type float can be created as

.. code:: c
Expand Down Expand Up @@ -2894,8 +2916,8 @@ through all entries as

.. code:: c
float *items = (float *)new_ndarray->array->items;
float item;
mp_float_t *items = (mp_float_t *)new_ndarray->array->items;
mp_float_t item;
for(size_t i=0; i < new_ndarray->array->len; i++) {
item = items[i];
Expand All @@ -2906,8 +2928,8 @@ or, since the data are stored in the pointer in a C-like fashion, as

.. code:: c
float *items = (float *)new_ndarray->array->items;
float item;
mp_float_t *items = (mp_float_t *)new_ndarray->array->items;
mp_float_t item;
for(size_t m=0; m < new_ndarray->m; m++) { // loop through the rows
for(size_t n=0; n < new_ndarray->n; n++) { // loop through the columns
Expand Down Expand Up @@ -2941,7 +2963,7 @@ should be equal to ``B``, ``b``, ``H``, ``h``, or ``f``. The size of a
single item is returned by the function
``mp_binary_get_size('@', ndarray->array->typecode, NULL)``. This number
is equal to 1, if the typecode is ``B``, or ``b``, 2, if the typecode is
``H``, or ``h``, and 4, if the typecode is ``f``.
``H``, or ``h``, 4, if the typecode is ``f``, and 8 for ``d``.

Alternatively, the size can be found out by dividing ``ndarray->bytes``
with the product of ``m``, and ``n``, i.e.,
Expand Down Expand Up @@ -3001,7 +3023,7 @@ with 13.
int16_t *items = (int16_t *)ndarray->array->items;
for(size_t i=0; i < ndarray->array->len; i++) items[i] = 13;
} else {
float *items = (float *)ndarray->array->items;
float *items = (mp_float_t *)ndarray->array->items;
for(size_t i=0; i < ndarray->array->len; i++) items[i] = 13;
}
return object_out;
Expand Down
40 changes: 26 additions & 14 deletions docs/ulab-manual.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2019-11-04T21:02:40.581322Z",
"start_time": "2019-11-04T21:02:40.568695Z"
"end_time": "2019-11-04T21:18:30.966523Z",
"start_time": "2019-11-04T21:18:30.959666Z"
}
},
"outputs": [
Expand Down Expand Up @@ -66,7 +66,7 @@
"author = 'Zoltán Vörös'\n",
"\n",
"# The full version, including alpha/beta/rc tags\n",
"release = '0.251'\n",
"release = '0.252'\n",
"\n",
"\n",
"# -- General configuration ---------------------------------------------------\n",
Expand Down Expand Up @@ -120,11 +120,11 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2019-11-04T21:03:19.810768Z",
"start_time": "2019-11-04T21:03:16.524467Z"
"end_time": "2019-11-04T21:18:36.861617Z",
"start_time": "2019-11-04T21:18:33.681724Z"
}
},
"outputs": [],
Expand Down Expand Up @@ -3938,7 +3938,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"As such, `spectrum` is really just a short hand for `np.sqrt(a*a + b*b)`:"
"As such, `spectrum` is really just a shorthand for `np.sqrt(a*a + b*b)`:"
]
},
{
Expand Down Expand Up @@ -4106,6 +4106,18 @@
" NDARRAY_FLOAT = 'f',\n",
"};\n",
"```\n",
"or\n",
"```c\n",
"enum NDARRAY_TYPE {\n",
" NDARRAY_UINT8 = 'B',\n",
" NDARRAY_INT8 = 'b',\n",
" NDARRAY_UINT16 = 'H', \n",
" NDARRAY_INT16 = 'h',\n",
" NDARRAY_FLOAT = 'd',\n",
"};\n",
"```\n",
"The ambiguity is caused by the fact that not all platforms implement `double`, and there one has to take `float`s. But you haven't actually got to be concerned by this, because at the very beginning of `ndarray.h`, this is already taken care of: the preprocessor figures out, what the `float` implementation of the hardware platform is, and defines the `NDARRAY_FLOAT` typecode accordingly. All you have to keep in mind is that wherever you would use `float` or `double`, you have to use `mp_float_t`. That type is defined in `py/mpconfig.h` of the micropython code base.\n",
"\n",
"Therefore, a 4-by-5 matrix of type float can be created as\n",
"\n",
"```c\n",
Expand All @@ -4131,8 +4143,8 @@
"Thus, staying with our example of a 4-by-5 float matrix, we can loop through all entries as\n",
"\n",
"```c\n",
"float *items = (float *)new_ndarray->array->items;\n",
"float item;\n",
"mp_float_t *items = (mp_float_t *)new_ndarray->array->items;\n",
"mp_float_t item;\n",
"\n",
"for(size_t i=0; i < new_ndarray->array->len; i++) {\n",
" item = items[i];\n",
Expand All @@ -4142,8 +4154,8 @@
"or, since the data are stored in the pointer in a C-like fashion, as \n",
"\n",
"```c\n",
"float *items = (float *)new_ndarray->array->items;\n",
"float item;\n",
"mp_float_t *items = (mp_float_t *)new_ndarray->array->items;\n",
"mp_float_t item;\n",
"\n",
"for(size_t m=0; m < new_ndarray->m; m++) { // loop through the rows\n",
" for(size_t n=0; n < new_ndarray->n; n++) { // loop through the columns\n",
Expand All @@ -4168,7 +4180,7 @@
"```c\n",
"ndarray->array->typecode\n",
"```\n",
"should be equal to `B`, `b`, `H`, `h`, or `f`. The size of a single item is returned by the function `mp_binary_get_size('@', ndarray->array->typecode, NULL)`. This number is equal to 1, if the typecode is `B`, or `b`, 2, if the typecode is `H`, or `h`, and 4, if the typecode is `f`. \n",
"should be equal to `B`, `b`, `H`, `h`, or `f`. The size of a single item is returned by the function `mp_binary_get_size('@', ndarray->array->typecode, NULL)`. This number is equal to 1, if the typecode is `B`, or `b`, 2, if the typecode is `H`, or `h`, 4, if the typecode is `f`, and 8 for `d`. \n",
"\n",
"Alternatively, the size can be found out by dividing `ndarray->bytes` with the product of `m`, and `n`, i.e., \n",
"\n",
Expand Down Expand Up @@ -4221,7 +4233,7 @@
" int16_t *items = (int16_t *)ndarray->array->items;\n",
" for(size_t i=0; i < ndarray->array->len; i++) items[i] = 13;\n",
" } else {\n",
" float *items = (float *)ndarray->array->items;\n",
" float *items = (mp_float_t *)ndarray->array->items;\n",
" for(size_t i=0; i < ndarray->array->len; i++) items[i] = 13;\n",
" }\n",
" return object_out;\n",
Expand Down

0 comments on commit d12785a

Please sign in to comment.