From ecb0a7775beaed66d19109fc1ea0573c7e74a622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20V=C3=B6r=C3=B6s?= Date: Tue, 3 Nov 2020 21:28:32 +0100 Subject: [PATCH] fixed pointer bug in eig --- code/linalg/linalg.c | 5 +-- code/ulab.c | 2 +- docs/manual/source/conf.py | 4 +-- docs/manual/source/ulab-linalg.rst | 25 +++++++------- docs/ulab-change-log.md | 6 ++++ docs/ulab-convert.ipynb | 22 ++++++------- docs/ulab-linalg.ipynb | 53 +++++++++++++++--------------- 7 files changed, 63 insertions(+), 54 deletions(-) diff --git a/code/linalg/linalg.c b/code/linalg/linalg.c index 3c4cda2..4bb1423 100644 --- a/code/linalg/linalg.c +++ b/code/linalg/linalg.c @@ -313,8 +313,8 @@ MP_DEFINE_CONST_FUN_OBJ_2(linalg_dot_obj, linalg_dot); static mp_obj_t linalg_eig(mp_obj_t oin) { ndarray_obj_t *in = linalg_object_is_square(oin); uint8_t *iarray = (uint8_t *)in->array; - mp_float_t *array = m_new(mp_float_t, in->len); - size_t S = in->shape[ULAB_MAX_DIMS - 2]; + size_t S = in->shape[ULAB_MAX_DIMS - 1]; + mp_float_t *array = m_new(mp_float_t, S*S); for(size_t i=0; i < S; i++) { // rows for(size_t j=0; j < S; j++) { // columns *array++ = ndarray_get_float_value(iarray, in->dtype); @@ -323,6 +323,7 @@ static mp_obj_t linalg_eig(mp_obj_t oin) { iarray -= in->strides[ULAB_MAX_DIMS - 1] * S; iarray += in->strides[ULAB_MAX_DIMS - 2]; } + array -= S * S; // make sure the matrix is symmetric for(size_t m=0; m < S; m++) { for(size_t n=m+1; n < S; n++) { diff --git a/code/ulab.c b/code/ulab.c index 71947c2..9958715 100644 --- a/code/ulab.c +++ b/code/ulab.c @@ -32,7 +32,7 @@ #include "user/user.h" #include "vector/vectorise.h" -#define ULAB_VERSION 1.2.0 +#define ULAB_VERSION 1.2.1 #define xstr(s) str(s) #define str(s) #s #if ULAB_NUMPY_COMPATIBILITY diff --git a/docs/manual/source/conf.py b/docs/manual/source/conf.py index b818bfe..55d6932 100644 --- a/docs/manual/source/conf.py +++ b/docs/manual/source/conf.py @@ -27,7 +27,7 @@ copyright = '2019-2020, Zoltán Vörös and contributors' author = 'Zoltán Vörös' # The full version, including alpha/beta/rc tags -release = '1.2.0' +release = '1.2.1' # -- General configuration --------------------------------------------------- @@ -58,7 +58,7 @@ latex_maketitle = r''' \Huge\textbf{The $\mu$lab book} \vskip 0.5em \LARGE -\textbf{Release 1.2.0} +\textbf{Release 1.2.1} \vskip 5em \huge\textbf{Zoltán Vörös} \end{flushright} diff --git a/docs/manual/source/ulab-linalg.rst b/docs/manual/source/ulab-linalg.rst index 4daa239..a5cbced 100644 --- a/docs/manual/source/ulab-linalg.rst +++ b/docs/manual/source/ulab-linalg.rst @@ -287,19 +287,20 @@ stabilisation routines for robots. a = np.array([[1, 2, 1, 4], [2, 5, 3, 5], [1, 3, 6, 1], [4, 5, 1, 7]], dtype=np.uint8) x, y = linalg.eig(a) - print('eigenvectors of a:\n', x) - print('\neigenvalues of a:\n', y) + print('eigenvectors of a:\n', y) + print('\neigenvalues of a:\n', x) .. parsed-literal:: eigenvectors of a: - array([-1.165288, 0.8029362, 5.585626, 13.77673], dtype=float) + array([[0.8151560042509081, -0.4499411232970823, -0.1644660242574522, 0.3256141906686505], + [0.2211334179893007, 0.7846992598235538, 0.08372081379922657, 0.5730077734355189], + [-0.1340114162071679, -0.3100776411558949, 0.8742786816656, 0.3486109343758527], + [-0.5183258053659028, -0.292663481927148, -0.4489749870391468, 0.6664142156731531]], dtype=float) eigenvalues of a: - array([[0.8151754, -0.4499267, -0.1643907, 0.3256237], - [0.2211193, 0.7847154, 0.08373602, 0.5729892], - [-0.1340859, -0.3100657, 0.8742685, 0.3486182], - [-0.5182822, -0.2926556, -0.4490192, 0.6664218]], dtype=float) + array([-1.165288365404889, 0.8029365530314914, 5.585625756072663, 13.77672605630074], dtype=float) + @@ -311,19 +312,19 @@ The same matrix diagonalised with ``numpy`` yields: a = array([[1, 2, 1, 4], [2, 5, 3, 5], [1, 3, 6, 1], [4, 5, 1, 7]], dtype=np.uint8) x, y = eig(a) - print('eigenvectors of a:\n', x) - print('\neigenvalues of a:\n', y) + print('eigenvectors of a:\n', y) + print('\neigenvalues of a:\n', x) .. parsed-literal:: eigenvectors of a: - [13.77672606 -1.16528837 0.80293655 5.58562576] - - eigenvalues of a: [[ 0.32561419 0.815156 0.44994112 -0.16446602] [ 0.57300777 0.22113342 -0.78469926 0.08372081] [ 0.34861093 -0.13401142 0.31007764 0.87427868] [ 0.66641421 -0.51832581 0.29266348 -0.44897499]] + + eigenvalues of a: + [13.77672606 -1.16528837 0.80293655 5.58562576] When comparing results, we should keep two things in mind: diff --git a/docs/ulab-change-log.md b/docs/ulab-change-log.md index 2443cbb..1bbb6e4 100644 --- a/docs/ulab-change-log.md +++ b/docs/ulab-change-log.md @@ -1,5 +1,11 @@ Tue, 3 Nov 2020 +version 1.2.1 + + fixed pointer issue in eig, and corrected the docs + +Tue, 3 Nov 2020 + version 1.2.0 added median function diff --git a/docs/ulab-convert.ipynb b/docs/ulab-convert.ipynb index a1cccbe..f50e5b6 100644 --- a/docs/ulab-convert.ipynb +++ b/docs/ulab-convert.ipynb @@ -14,11 +14,11 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 1, "metadata": { "ExecuteTime": { - "end_time": "2020-11-03T19:56:04.701289Z", - "start_time": "2020-11-03T19:56:04.691215Z" + "end_time": "2020-11-03T20:26:50.212009Z", + "start_time": "2020-11-03T20:26:50.202152Z" } }, "outputs": [ @@ -61,7 +61,7 @@ "author = 'Zoltán Vörös'\n", "\n", "# The full version, including alpha/beta/rc tags\n", - "release = '1.2.0'\n", + "release = '1.2.1'\n", "\n", "\n", "# -- General configuration ---------------------------------------------------\n", @@ -92,7 +92,7 @@ "\\Huge\\textbf{The $\\mu$lab book}\n", "\\vskip 0.5em\n", "\\LARGE\n", - "\\textbf{Release 1.2.0}\n", + "\\textbf{Release 1.2.1}\n", "\\vskip 5em\n", "\\huge\\textbf{Zoltán Vörös}\n", "\\end{flushright}\n", @@ -255,11 +255,11 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 2, "metadata": { "ExecuteTime": { - "end_time": "2020-11-03T19:56:16.427813Z", - "start_time": "2020-11-03T19:56:16.398143Z" + "end_time": "2020-11-03T20:26:54.904105Z", + "start_time": "2020-11-03T20:26:53.219731Z" } }, "outputs": [], @@ -293,11 +293,11 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 3, "metadata": { "ExecuteTime": { - "end_time": "2020-11-03T19:56:24.888593Z", - "start_time": "2020-11-03T19:56:17.294538Z" + "end_time": "2020-11-03T20:27:00.291103Z", + "start_time": "2020-11-03T20:26:54.934703Z" } }, "outputs": [], diff --git a/docs/ulab-linalg.ipynb b/docs/ulab-linalg.ipynb index 5a8c542..e05c579 100644 --- a/docs/ulab-linalg.ipynb +++ b/docs/ulab-linalg.ipynb @@ -5,8 +5,8 @@ "execution_count": 1, "metadata": { "ExecuteTime": { - "end_time": "2020-10-17T17:02:14.307511Z", - "start_time": "2020-10-17T17:02:12.758371Z" + "end_time": "2020-11-03T20:12:21.089435Z", + "start_time": "2020-11-03T20:12:20.903465Z" } }, "outputs": [ @@ -34,8 +34,8 @@ "execution_count": 2, "metadata": { "ExecuteTime": { - "end_time": "2020-10-17T17:02:15.792631Z", - "start_time": "2020-10-17T17:02:15.786010Z" + "end_time": "2020-11-03T20:12:22.308564Z", + "start_time": "2020-11-03T20:12:22.298510Z" } }, "outputs": [], @@ -52,8 +52,8 @@ "execution_count": 3, "metadata": { "ExecuteTime": { - "end_time": "2020-10-17T17:02:16.470429Z", - "start_time": "2020-10-17T17:02:16.409450Z" + "end_time": "2020-11-03T20:12:23.830659Z", + "start_time": "2020-11-03T20:12:23.769500Z" } }, "outputs": [], @@ -598,11 +598,11 @@ }, { "cell_type": "code", - "execution_count": 496, + "execution_count": 18, "metadata": { "ExecuteTime": { - "end_time": "2019-10-19T13:27:30.977472Z", - "start_time": "2019-10-19T13:27:30.943326Z" + "end_time": "2020-11-03T20:25:26.952290Z", + "start_time": "2020-11-03T20:25:26.930184Z" } }, "outputs": [ @@ -611,27 +611,28 @@ "output_type": "stream", "text": [ "eigenvectors of a:\n", - " array([-1.165288, 0.8029362, 5.585626, 13.77673], dtype=float)\n", + " array([[0.8151560042509081, -0.4499411232970823, -0.1644660242574522, 0.3256141906686505],\n", + " [0.2211334179893007, 0.7846992598235538, 0.08372081379922657, 0.5730077734355189],\n", + " [-0.1340114162071679, -0.3100776411558949, 0.8742786816656, 0.3486109343758527],\n", + " [-0.5183258053659028, -0.292663481927148, -0.4489749870391468, 0.6664142156731531]], dtype=float)\n", "\n", "eigenvalues of a:\n", - " array([[0.8151754, -0.4499267, -0.1643907, 0.3256237],\n", - "\t [0.2211193, 0.7847154, 0.08373602, 0.5729892],\n", - "\t [-0.1340859, -0.3100657, 0.8742685, 0.3486182],\n", - "\t [-0.5182822, -0.2926556, -0.4490192, 0.6664218]], dtype=float)\n", + " array([-1.165288365404889, 0.8029365530314914, 5.585625756072663, 13.77672605630074], dtype=float)\n", + "\n", "\n" ] } ], "source": [ - "%%micropython -pyboard 1\n", + "%%micropython -unix 1\n", "\n", "import ulab as np\n", "from ulab import linalg\n", "\n", "a = np.array([[1, 2, 1, 4], [2, 5, 3, 5], [1, 3, 6, 1], [4, 5, 1, 7]], dtype=np.uint8)\n", "x, y = linalg.eig(a)\n", - "print('eigenvectors of a:\\n', x)\n", - "print('\\neigenvalues of a:\\n', y)" + "print('eigenvectors of a:\\n', y)\n", + "print('\\neigenvalues of a:\\n', x)" ] }, { @@ -643,11 +644,11 @@ }, { "cell_type": "code", - "execution_count": 389, + "execution_count": 6, "metadata": { "ExecuteTime": { - "end_time": "2019-10-19T09:47:46.835349Z", - "start_time": "2019-10-19T09:47:46.785592Z" + "end_time": "2020-11-03T20:13:27.236159Z", + "start_time": "2020-11-03T20:13:27.069967Z" } }, "outputs": [ @@ -656,21 +657,21 @@ "output_type": "stream", "text": [ "eigenvectors of a:\n", - " [13.77672606 -1.16528837 0.80293655 5.58562576]\n", - "\n", - "eigenvalues of a:\n", " [[ 0.32561419 0.815156 0.44994112 -0.16446602]\n", " [ 0.57300777 0.22113342 -0.78469926 0.08372081]\n", " [ 0.34861093 -0.13401142 0.31007764 0.87427868]\n", - " [ 0.66641421 -0.51832581 0.29266348 -0.44897499]]\n" + " [ 0.66641421 -0.51832581 0.29266348 -0.44897499]]\n", + "\n", + "eigenvalues of a:\n", + " [13.77672606 -1.16528837 0.80293655 5.58562576]\n" ] } ], "source": [ "a = array([[1, 2, 1, 4], [2, 5, 3, 5], [1, 3, 6, 1], [4, 5, 1, 7]], dtype=np.uint8)\n", "x, y = eig(a)\n", - "print('eigenvectors of a:\\n', x)\n", - "print('\\neigenvalues of a:\\n', y)" + "print('eigenvectors of a:\\n', y)\n", + "print('\\neigenvalues of a:\\n', x)" ] }, {