diff options
Diffstat (limited to 'doc/source')
-rw-r--r-- | doc/source/reference/c-api/iterator.rst | 60 | ||||
-rw-r--r-- | doc/source/user/absolute_beginners.rst | 2 | ||||
-rw-r--r-- | doc/source/user/c-info.python-as-glue.rst | 4 | ||||
-rw-r--r-- | doc/source/user/plot_approx.py | 19 | ||||
-rw-r--r-- | doc/source/user/plot_face.py | 5 | ||||
-rw-r--r-- | doc/source/user/plot_final.py | 19 | ||||
-rw-r--r-- | doc/source/user/plot_gray.py | 8 | ||||
-rw-r--r-- | doc/source/user/plot_gray_svd.py | 16 | ||||
-rw-r--r-- | doc/source/user/plot_reconstructed.py | 17 |
9 files changed, 63 insertions, 87 deletions
diff --git a/doc/source/reference/c-api/iterator.rst b/doc/source/reference/c-api/iterator.rst index b4adaef9b..07187e7f1 100644 --- a/doc/source/reference/c-api/iterator.rst +++ b/doc/source/reference/c-api/iterator.rst @@ -203,6 +203,66 @@ is used to control the memory layout of the allocated result, typically } +Multi Index Tracking Example +---------------------------- + +This example shows you how to work with the :c:data:`NPY_ITER_MULTI_INDEX` flag. For simplicity, we assume the argument is a two-dimensional array. + +.. code-block:: c + + int PrintMultiIndex(PyArrayObject *arr) { + NpyIter *iter; + NpyIter_IterNextFunc *iternext; + npy_intp multi_index[2]; + + iter = NpyIter_New( + arr, NPY_ITER_READONLY | NPY_ITER_MULTI_INDEX | NPY_ITER_REFS_OK, + NPY_KEEPORDER, NPY_NO_CASTING, NULL); + if (iter == NULL) { + return -1; + } + if (NpyIter_GetNDim(iter) != 2) { + NpyIter_Deallocate(iter); + PyErr_SetString(PyExc_ValueError, "Array must be 2-D"); + return -1; + } + if (NpyIter_GetIterSize(iter) != 0) { + iternext = NpyIter_GetIterNext(iter, NULL); + if (iternext == NULL) { + NpyIter_Deallocate(iter); + return -1; + } + NpyIter_GetMultiIndexFunc *get_multi_index = + NpyIter_GetGetMultiIndex(iter, NULL); + if (get_multi_index == NULL) { + NpyIter_Deallocate(iter); + return -1; + } + + do { + get_multi_index(iter, multi_index); + printf("multi_index is [%" NPY_INTP_FMT ", %" NPY_INTP_FMT "]\n", + multi_index[0], multi_index[1]); + } while (iternext(iter)); + } + if (!NpyIter_Deallocate(iter)) { + return -1; + } + return 0; + } + +When called with a 2x3 array, the above example prints: + +.. code-block:: sh + + multi_index is [0, 0] + multi_index is [0, 1] + multi_index is [0, 2] + multi_index is [1, 0] + multi_index is [1, 1] + multi_index is [1, 2] + + Iterator Data Types --------------------- diff --git a/doc/source/user/absolute_beginners.rst b/doc/source/user/absolute_beginners.rst index a4a82afb6..dae2c4f06 100644 --- a/doc/source/user/absolute_beginners.rst +++ b/doc/source/user/absolute_beginners.rst @@ -1504,7 +1504,7 @@ You can use ``np.load()`` to reconstruct your array. :: >>> b = np.load('filename.npy') -If you want to check your array, you can run::: +If you want to check your array, you can run:: >>> print(b) [1 2 3 4 5 6] diff --git a/doc/source/user/c-info.python-as-glue.rst b/doc/source/user/c-info.python-as-glue.rst index 4db789856..363099fe3 100644 --- a/doc/source/user/c-info.python-as-glue.rst +++ b/doc/source/user/c-info.python-as-glue.rst @@ -53,7 +53,7 @@ needs is to call out from Python code to a fast, machine-code routine relatively easy to do is a big reason why Python is such an excellent high-level language for scientific and engineering programming. -Their are two basic approaches to calling compiled code: writing an +There are two basic approaches to calling compiled code: writing an extension module that is then imported to Python using the import command, or calling a shared-library subroutine directly from Python using the `ctypes <https://docs.python.org/3/library/ctypes.html>`_ @@ -97,7 +97,7 @@ control over how the C-library gets used and called which can lead to a lean and tight interface with minimal over-head. The disadvantage is that you have to write, debug, and maintain C-code, although most of it can be adapted using the time-honored technique of -"cutting-pasting-and-modifying" from other extension modules. Because, +"cutting-pasting-and-modifying" from other extension modules. Because the procedure of calling out to additional C-code is fairly regimented, code-generation procedures have been developed to make this process easier. One of these code-generation techniques is diff --git a/doc/source/user/plot_approx.py b/doc/source/user/plot_approx.py deleted file mode 100644 index a2d6981d9..000000000 --- a/doc/source/user/plot_approx.py +++ /dev/null @@ -1,19 +0,0 @@ -from scipy import misc -import matplotlib.pyplot as plt -import numpy as np -from numpy import linalg - -img = misc.face() -img_array = img / 255 -img_gray = img_array @ [0.2126, 0.7152, 0.0722] - -U, s, Vt = linalg.svd(img_gray) - -Sigma = np.zeros((768, 1024)) -for i in range(768): - Sigma[i, i] = s[i] - -k = 10 - -approx = U @ Sigma[:, :k] @ Vt[:k, :] -plt.imshow(approx, cmap="gray") diff --git a/doc/source/user/plot_face.py b/doc/source/user/plot_face.py deleted file mode 100644 index c0891e770..000000000 --- a/doc/source/user/plot_face.py +++ /dev/null @@ -1,5 +0,0 @@ -from scipy import misc -import matplotlib.pyplot as plt - -img = misc.face() -plt.imshow(img) diff --git a/doc/source/user/plot_final.py b/doc/source/user/plot_final.py deleted file mode 100644 index 10cb097dd..000000000 --- a/doc/source/user/plot_final.py +++ /dev/null @@ -1,19 +0,0 @@ -from scipy import misc -import matplotlib.pyplot as plt -import numpy as np -from numpy import linalg - -img = misc.face() -img_array = img / 255 -img_array_transposed = np.transpose(img_array, (2, 0, 1)) - -U, s, Vt = linalg.svd(img_array_transposed) - -Sigma = np.zeros((3, 768, 1024)) -for j in range(3): - np.fill_diagonal(Sigma[j, :, :], s[j, :]) - -k = 10 - -approx_img = U @ Sigma[..., :k] @ Vt[..., :k, :] -plt.imshow(np.transpose(approx_img, (1, 2, 0))) diff --git a/doc/source/user/plot_gray.py b/doc/source/user/plot_gray.py deleted file mode 100644 index 6cb46bbe4..000000000 --- a/doc/source/user/plot_gray.py +++ /dev/null @@ -1,8 +0,0 @@ -from scipy import misc -import matplotlib.pyplot as plt -import numpy as np - -img = misc.face() -img_array = img / 255 -img_gray = img_array @ [0.2126, 0.7152, 0.0722] -plt.imshow(img_gray, cmap="gray") diff --git a/doc/source/user/plot_gray_svd.py b/doc/source/user/plot_gray_svd.py deleted file mode 100644 index 95439939d..000000000 --- a/doc/source/user/plot_gray_svd.py +++ /dev/null @@ -1,16 +0,0 @@ -from scipy import misc -import matplotlib.pyplot as plt -import numpy as np -from numpy import linalg - -img = misc.face() -img_array = img / 255 -img_gray = img_array @ [0.2126, 0.7152, 0.0722] - -U, s, Vt = linalg.svd(img_gray) - -Sigma = np.zeros((768, 1024)) -for i in range(768): - Sigma[i, i] = s[i] - -plt.plot(s) diff --git a/doc/source/user/plot_reconstructed.py b/doc/source/user/plot_reconstructed.py deleted file mode 100644 index 37cf3c626..000000000 --- a/doc/source/user/plot_reconstructed.py +++ /dev/null @@ -1,17 +0,0 @@ -from scipy import misc -import matplotlib.pyplot as plt -import numpy as np -from numpy import linalg - -img = misc.face() -img_array = img / 255 -img_array_transposed = np.transpose(img_array, (2, 0, 1)) - -U, s, Vt = linalg.svd(img_array_transposed) - -Sigma = np.zeros((3, 768, 1024)) -for j in range(3): - np.fill_diagonal(Sigma[j, :, :], s[j, :]) - -reconstructed = U @ Sigma @ Vt -plt.imshow(np.transpose(reconstructed, (1, 2, 0))) |