circuitpython-ulab/tests/tests.ipynb
2019-12-24 08:17:28 +01:00

1243 lines
41 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Test suite for micropython-ulab"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-03T21:26:04.387843Z",
"start_time": "2019-12-03T21:26:04.194851Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Populating the interactive namespace from numpy and matplotlib\n"
]
}
],
"source": [
"%pylab inline"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-03T21:11:58.682375Z",
"start_time": "2019-12-03T21:11:58.676781Z"
}
},
"outputs": [],
"source": [
"from IPython.core.magic import Magics, magics_class, line_cell_magic\n",
"from IPython.core.magic import cell_magic, register_cell_magic, register_line_magic\n",
"from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring\n",
"import subprocess\n",
"import os"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-03T21:12:01.505258Z",
"start_time": "2019-12-03T21:12:01.471310Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'/home/v923z/sandbox/micropython/v1.11/ulab/tests'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pwd"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-04T05:59:43.347528Z",
"start_time": "2019-12-04T05:59:43.329673Z"
}
},
"outputs": [],
"source": [
"@magics_class\n",
"class PyboardMagic(Magics):\n",
" @cell_magic\n",
" @magic_arguments()\n",
" @argument('-skip')\n",
" @argument('-unix')\n",
" @argument('-file')\n",
" @argument('-data')\n",
" @argument('-time')\n",
" @argument('-memory')\n",
" def micropython(self, line='', cell=None):\n",
" args = parse_argstring(self.micropython, line)\n",
" if args.skip: # doesn't care about the cell's content\n",
" print('skipped execution')\n",
" return None # do not parse the rest\n",
" 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\", \"/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",
" return None\n",
" if args.file: # can be used to copy the cell content onto the pyboard's flash\n",
" spaces = \" \"\n",
" try:\n",
" with open(args.file, 'w') as fout:\n",
" fout.write(cell.replace('\\t', spaces))\n",
" print('written cell to {}'.format(args.file))\n",
" except:\n",
" print('Failed to write to disc!')\n",
" return None # do not parse the rest\n",
" if args.data: # can be used to load data from the pyboard directly into kernel space\n",
" message = pyb.exec(cell)\n",
" if len(message) == 0:\n",
" print('pyboard >>>')\n",
" else:\n",
" print(message.decode('utf-8'))\n",
" # register new variable in user namespace\n",
" self.shell.user_ns[args.data] = string_to_matrix(message.decode(\"utf-8\"))\n",
" \n",
" if args.time: # measures the time of executions\n",
" pyb.exec('import utime')\n",
" message = pyb.exec('t = utime.ticks_us()\\n' + cell + '\\ndelta = utime.ticks_diff(utime.ticks_us(), t)' + \n",
" \"\\nprint('execution time: {:d} us'.format(delta))\")\n",
" print(message.decode('utf-8'))\n",
" \n",
" if args.memory: # prints out memory information \n",
" message = pyb.exec('from micropython import mem_info\\nprint(mem_info())\\n')\n",
" print(\"memory before execution:\\n========================\\n\", message.decode('utf-8'))\n",
" message = pyb.exec(cell)\n",
" print(\">>> \", message.decode('utf-8'))\n",
" message = pyb.exec('print(mem_info())')\n",
" print(\"memory after execution:\\n========================\\n\", message.decode('utf-8'))\n",
"\n",
" else:\n",
" message = pyb.exec(cell)\n",
" print(message.decode('utf-8'))\n",
"\n",
"ip = get_ipython()\n",
"ip.register_magics(PyboardMagic)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-04T05:57:03.581970Z",
"start_time": "2019-12-04T05:57:03.572718Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'/home/v923z/sandbox/micropython/v1.11/ulab/tests'"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pwd"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-04T05:59:45.288713Z",
"start_time": "2019-12-04T05:59:45.276563Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"array([[[0, 1, 2, 3, 4],\n",
"[5, 6, 7, 8, 9],\n",
"[10, 11, 12, 13, 14]],\n",
"\n",
"[[15, 16, 17, 18, 19],\n",
"[20, 21, 22, 23, 24],\n",
"[25, 26, 27, 28, 29]]], dtype=uint8)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"\n",
"a = np.array(range(30), dtype=np.uint8)\n",
"b = a.reshape((2, 3, 5))\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-02T20:46:22.829967Z",
"start_time": "2019-12-02T20:46:22.808076Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 0, 1, 2, 3, 4],\n",
" [ 5, 6, 7, 8, 9],\n",
" [10, 11, 12, 13, 14]],\n",
"\n",
" [[15, 16, 17, 18, 19],\n",
" [20, 21, 22, 23, 24],\n",
" [25, 26, 27, 28, 29]]], dtype=uint8)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.array(range(30), dtype=np.uint8)\n",
"b = a.reshape((2, 3, 5))\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 136,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-06T06:03:15.228973Z",
"start_time": "2019-12-06T06:03:15.208700Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"array([[[0, 1, 2, 3, 4],\n",
"[5, 6, 7, 8, 9],\n",
"[10, 11, 12, 13, 14]],\n",
"\n",
"[[15, 16, 17, 18, 19],\n",
"[20, 21, 22, 23, 24],\n",
"[25, 26, 27, 28, 29]]], dtype=uint16)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"\n",
"a = np.array(range(30), dtype=np.uint8)\n",
"b = a.reshape((2, 3, 5))\n",
"c = np.array(b, dtype=np.uint16)\n",
"print(c)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unary operators"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### len"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-05T06:07:38.622907Z",
"start_time": "2019-12-05T06:07:38.612074Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"length of a: 30\n",
"length of b: 3\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"\n",
"a = np.array(range(30), dtype=np.uint8)\n",
"b = a.reshape((2, 3, 5))\n",
"print('length of a: ', len(a))\n",
"print('length of b: ', len(b))"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-05T06:16:59.368578Z",
"start_time": "2019-12-05T06:16:59.363921Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"length of a: 30\n",
"length of b: 2\n"
]
}
],
"source": [
"a = np.array(range(30), dtype=np.uint8)\n",
"b = a.reshape((2, 3, 5))\n",
"print('length of a: ', len(a))\n",
"print('length of b: ', len(b))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### invert"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"\n",
"a = np.array(range(30), dtype=np.uint8)\n",
"b = a.reshape((2, 3, 5))\n",
"print('length of a: ', len(a))\n",
"print('length of b: ', len(b))"
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-05T17:01:11.671529Z",
"start_time": "2019-12-05T17:01:11.665062Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"inverse of a: [255 254 253 252 251 250 249 248 247 246 245 244 243 242 241 240 239 238\n",
" 237 236 235 234 233 232 231]\n",
"inverse of b: [[ 0 1 2 3 4]\n",
" [ 5 6 7 8 9]\n",
" [10 11 12 13 14]\n",
" [15 16 17 18 19]\n",
" [20 21 22 23 24]]\n"
]
}
],
"source": [
"a = np.array(range(25), dtype=np.uint8)\n",
"b = a.reshape((5, 5))\n",
"c = ~b[1:4:2, 0:4:2]\n",
"print('inverse of a: ', ~a)\n",
"print('inverse of b: ', b)"
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-05T19:45:02.620042Z",
"start_time": "2019-12-05T19:45:02.606065Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"array([False, False, True, False, True], dtype=bool)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"\n",
"a = np.array([0.0, 0, 5.0, 0, 1], dtype=np.bool)\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": 134,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-05T21:24:50.125860Z",
"start_time": "2019-12-05T21:24:50.116170Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(array([[ 10, 21, 102, 203],\n",
" [ 14, 25, 106, 207],\n",
" [ 18, 29, 110, 211],\n",
" [ 22, 33, 114, 215]]), (4,))"
]
},
"execution_count": 134,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = array(range(16))\n",
"a = a.reshape((4, 4))\n",
"b = array([10, 20, 100, 200])\n",
"b = transpose(b)\n",
"a + b, b.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Binary operators"
]
},
{
"cell_type": "code",
"execution_count": 139,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-06T06:17:20.791273Z",
"start_time": "2019-12-06T06:17:20.780470Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"array([2000], dtype=uint16)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"\n",
"a = np.array(range(25), dtype=np.uint8)\n",
"b = a.reshape((5, 5))\n",
"c = b + 2000\n",
"print(c)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Slicing"
]
},
{
"cell_type": "code",
"execution_count": 217,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-10T18:32:40.190775Z",
"start_time": "2019-12-10T18:32:40.164630Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"array([[6, 8],\n",
"[16, 18]], dtype=uint8)\n",
"array([[0, 1, 2, 3, 4],\n",
"[5, 100, 7, 200, 9],\n",
"[10, 11, 12, 13, 14],\n",
"[15, 100, 17, 200, 19],\n",
"[20, 21, 22, 23, 24]], dtype=uint8)\n",
"array([[0, 1, 2, 3, 4],\n",
"[5, 100, 7, 200, 9],\n",
"[10, 11, 12, 13, 14],\n",
"[15, 50, 17, 60, 19],\n",
"[20, 21, 22, 23, 24]], dtype=uint8)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"\n",
"a = np.array(range(25), dtype=np.uint8)\n",
"b = a.reshape((5, 5))\n",
"c = b[1:4:2, 1:4:2]\n",
"print(c)\n",
"c = np.array([100, 200], dtype=np.uint8)\n",
"b[1:4:2, 1:4:2] = c\n",
"print(b)\n",
"\n",
"c = np.array([100, 200, 50, 60], dtype=np.uint8).reshape((2,2))\n",
"b[1:4:2, 1:4:2] = c\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 230,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-11T16:16:30.918348Z",
"start_time": "2019-12-11T16:16:30.911253Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 10, 20, 30, 40],\n",
" [ 20, 40, 60, 80],\n",
" [ 30, 60, 90, 120],\n",
" [ 40, 80, 120, 160]])"
]
},
"execution_count": 230,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = array([1, 2, 3, 4]).reshape((1, 4))\n",
"b = array([10, 20, 30, 40]).reshape((4, 1))\n",
"\n",
"b@a"
]
},
{
"cell_type": "code",
"execution_count": 224,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-10T19:08:59.256112Z",
"start_time": "2019-12-10T19:08:59.248772Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ True, True],\n",
" [False, False]])"
]
},
"execution_count": 224,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.array([1, 2, 3, 4], dtype=float).reshape((2, 2))\n",
"b = np.array([1, 2], dtype=uint8)\n",
"a == b"
]
},
{
"cell_type": "code",
"execution_count": 218,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-10T19:02:32.909863Z",
"start_time": "2019-12-10T19:02:32.896619Z"
}
},
"outputs": [
{
"ename": "ValueError",
"evalue": "operands could not be broadcast together with shapes (5,5) (4,) ",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-218-0b9a6ba35443>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mb\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (5,5) (4,) "
]
}
],
"source": [
"a = np.array(range(25), dtype=np.uint8)\n",
"b = a.reshape((5, 5))\n",
"c = np.array([1, 2, 3, 4])\n",
"b + c"
]
},
{
"cell_type": "code",
"execution_count": 165,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-08T20:47:47.550115Z",
"start_time": "2019-12-08T20:47:47.540941Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 6 8]\n",
" [16 18]]\n",
"[[ 0 1 2 3 4]\n",
" [ 5 100 7 200 9]\n",
" [ 10 11 12 13 14]\n",
" [ 15 100 17 200 19]\n",
" [ 20 21 22 23 24]]\n"
]
}
],
"source": [
"a = np.array(range(25), dtype=np.uint8)\n",
"b = a.reshape((5, 5))\n",
"c = b[1:4:2, 1:4:2]\n",
"print(c)\n",
"c = np.array(range(30))\n",
"d = c.reshape((2, 3, 5))\n",
"b[1:4:2, 1:4:2] = np.array([100, 200.1])\n",
"print(b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## flatten"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-04T18:19:10.798300Z",
"start_time": "2019-12-04T18:19:10.768262Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"array([[[0, 1, 2, 3, 4],\n",
"[5, 6, 7, 8, 9],\n",
"[10, 11, 12, 13, 14]],\n",
"\n",
"[[15, 16, 17, 18, 19],\n",
"[20, 21, 22, 23, 24],\n",
"[25, 26, 27, 28, 29]]], dtype=uint8)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"\n",
"a = np.array(range(30), dtype=np.uint8)\n",
"b = a.reshape((2, 3, 5))\n",
"c = b.flatten()\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-04T18:04:02.712390Z",
"start_time": "2019-12-04T18:04:02.622111Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23\n",
" 24 25 26 27 28 29]\n"
]
}
],
"source": [
"a = np.array(range(30), dtype=np.uint8)\n",
"b = a.reshape((2, 3, 5))\n",
"c = b.flatten()\n",
"print(c)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Vectorised calculations"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-03T21:48:25.814105Z",
"start_time": "2019-12-03T21:48:25.803443Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"array([1.0, 2.718281828459045, 7.38905609893065, 20.08553692318767, 54.59815003314424, 148.4131591025766, 403.4287934927351, 1096.633158428459, 2980.957987041728, 8103.083927575385], dtype=float) \n",
"\n",
"array([[[0.0, 0.8414709848078965, 0.9092974268256818, 0.1411200080598672, -0.7568024953079282],\n",
"[-0.9589242746631385, -0.2794154981989259, 0.6569865987187891, 0.9893582466233818, 0.4121184852417566],\n",
"[-0.5440211108893697, -0.9999902065507035, -0.5365729180004349, 0.4201670368266409, 0.9906073556948703]],\n",
"\n",
"[[0.6502878401571168, -0.2879033166650653, -0.9613974918795568, -0.7509872467716761, 0.1498772096629524],\n",
"[0.9129452507276277, 0.836655638536056, -0.008851309290403876, -0.8462204041751706, -0.9055783620066238],\n",
"[-0.132351750097773, 0.7625584504796027, 0.956375928404503, 0.2709057883078691, -0.6636338842129676]]], dtype=float) \n",
"\n",
"(1, 2)\n",
"array([1.0, 2.718281828459045, 7.38905609893065, 20.08553692318767, 54.59815003314424, 148.4131591025766, 403.4287934927351, 1096.633158428459, 2980.957987041728, 8103.083927575385], dtype=float)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"\n",
"a = np.array(range(10))\n",
"print(np.exp(a), '\\n')\n",
"\n",
"a = np.array(range(30))\n",
"b = a.reshape((2, 3, 5))\n",
"print(np.sin(b), '\\n')\n",
"\n",
"a = np.array(range(25))\n",
"b = a.reshape((5, 5))\n",
"c = b[2:5:2,1:5:2]\n",
"print(c.shape())\n",
"\n",
"a = range(10)\n",
"print(np.exp(a))"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-03T21:40:06.196177Z",
"start_time": "2019-12-03T21:40:06.181972Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1.00000000e+00 2.71828183e+00 7.38905610e+00 2.00855369e+01\n",
" 5.45981500e+01 1.48413159e+02 4.03428793e+02 1.09663316e+03\n",
" 2.98095799e+03 8.10308393e+03] \n",
"\n",
"(2, 3, 5)\n",
"[[[ 0. 0.84147098 0.90929743 0.14112001 -0.7568025 ]\n",
" [-0.95892427 -0.2794155 0.6569866 0.98935825 0.41211849]\n",
" [-0.54402111 -0.99999021 -0.53657292 0.42016704 0.99060736]]\n",
"\n",
" [[ 0.65028784 -0.28790332 -0.96139749 -0.75098725 0.14987721]\n",
" [ 0.91294525 0.83665564 -0.00885131 -0.8462204 -0.90557836]\n",
" [-0.13235175 0.76255845 0.95637593 0.27090579 -0.66363388]]] \n",
"\n",
"[[-0.99999021 0.42016704]\n",
" [ 0.83665564 -0.8462204 ]]\n"
]
}
],
"source": [
"a = np.array(range(10))\n",
"print(np.exp(a), '\\n')\n",
"\n",
"a = np.array(range(30))\n",
"b = a.reshape((2, 3, 5))\n",
"print(b.shape)\n",
"print(np.sin(b), '\\n')\n",
"\n",
"a = np.array(range(25))\n",
"b = a.reshape((5, 5))\n",
"print(np.sin(b[2:5:2,1:5:2]))"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-03T21:26:11.856646Z",
"start_time": "2019-12-03T21:26:11.850674Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"-0.9589242746631385"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sin(5.)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Polynomial"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-03T19:48:13.780600Z",
"start_time": "2019-12-03T19:48:13.761738Z"
},
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"array([3.0, 6.0, 11.0, 18.0, 27.0, 38.0, 51.00000000000001, 66.00000000000001, 83.00000000000001, 102.0], dtype=float)\n",
"array([[3.0, 6.0],\n",
"[11.0, 18.0],\n",
"[27.0, 38.0],\n",
"[51.00000000000001, 66.00000000000001],\n",
"[83.00000000000001, 102.0]], dtype=float)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"\n",
"a = np.array(range(10))\n",
"p = np.array([1, 2, 3])\n",
"\n",
"print(np.polyval(p, a))\n",
"\n",
"b = a.reshape((5, 2))\n",
"print(np.polyval(p, b))"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-03T17:11:36.898591Z",
"start_time": "2019-12-03T17:11:36.893661Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 3 6 11 18 27 38 51 66 83 102]\n",
"[[ 3 6]\n",
" [ 11 18]\n",
" [ 27 38]\n",
" [ 51 66]\n",
" [ 83 102]]\n"
]
}
],
"source": [
"a = np.array(range(10))\n",
"p = np.array([1, 2, 3])\n",
"\n",
"print(np.polyval(p, a))\n",
"\n",
"b = a.reshape((5, 2))\n",
"print(np.polyval(p, b))"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-03T20:01:28.491356Z",
"start_time": "2019-12-03T20:01:28.464044Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"independent values:\t array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0], dtype=float)\n",
"dependent values:\t array([9.0, 4.0, 1.0, 0.0, 1.0, 4.0, 9.0], dtype=float)\n",
"fitted values:\t\t array([1.0, -6.0, 9.000000000000004], dtype=float)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"\n",
"x = np.array([0, 1, 2, 3, 4, 5, 6])\n",
"y = np.array([9, 4, 1, 0, 1, 4, 9])\n",
"print('independent values:\\t', x)\n",
"print('dependent values:\\t', y)\n",
"print('fitted values:\\t\\t', np.polyfit(x, y, 2))"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-03T20:01:22.905241Z",
"start_time": "2019-12-03T20:01:22.899389Z"
},
"scrolled": true
},
"source": [
"x = np.array([0, 1, 2, 3, 4, 5, 6])\n",
"y = np.array([9, 4, 1, 0, 1, 4, 9])\n",
"print('independent values:\\t', x)\n",
"print('dependent values:\\t', y)\n",
"print('fitted values:\\t\\t', np.polyfit(x, y, 2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Numerical"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## linspace"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-04T06:12:19.977007Z",
"start_time": "2019-12-04T06:12:19.965884Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"default sequence:\t array([0.0, 0.2040816326530612, 0.4081632653061225, 0.6122448979591837, 0.8163265306122449, 1.020408163265306, 1.224489795918367, 1.428571428571429, 1.63265306122449, 1.836734693877551, 2.040816326530612, 2.244897959183674, 2.448979591836735, 2.653061224489796, 2.857142857142857, 3.061224489795918, 3.265306122448979, 3.46938775510204, 3.673469387755101, 3.877551020408162, 4.081632653061223, 4.285714285714284, 4.489795918367345, 4.693877551020406, 4.897959183673467, 5.102040816326528, 5.306122448979589, 5.51020408163265, 5.714285714285711, 5.918367346938772, 6.122448979591833, 6.326530612244894, 6.530612244897955, 6.734693877551016, 6.938775510204077, 7.142857142857138, 7.346938775510199, 7.55102040816326, 7.755102040816321, 7.959183673469382, 8.163265306122444, 8.367346938775505, 8.571428571428566, 8.775510204081627, 8.979591836734688, 9.183673469387749, 9.38775510204081, 9.591836734693871, 9.795918367346932, 9.999999999999993], dtype=float)\n",
"num=5:\t\t\t array([0.0, 2.5, 5.0, 7.5, 10.0], dtype=float)\n",
"num=5:\t\t\t array([0.0, 2.0, 4.0, 6.0, 8.0], dtype=float)\n",
"num=7:\t\t\t array([0, 0, 1, 2, 2, 3, 4], dtype=uint8)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"\n",
"# generate a sequence with defaults\n",
"print('default sequence:\\t', np.linspace(0, 10))\n",
"\n",
"# num=5\n",
"print('num=5:\\t\\t\\t', np.linspace(0, 10, num=5))\n",
"\n",
"# num=5, endpoint=False\n",
"print('num=5:\\t\\t\\t', np.linspace(0, 10, num=5, endpoint=False))\n",
"\n",
"# num=5, endpoint=False, dtype=uint8\n",
"print('num=7:\\t\\t\\t', np.linspace(0, 5, num=7, endpoint=False, dtype=np.uint8))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# FFT"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-04T06:03:59.561966Z",
"start_time": "2019-12-04T06:03:59.549858Z"
},
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"real part:\t array([8128.0, -64.00000000000047, -64.00000000000003, -64.00000000000043, -64.0, -63.99999999999991, -63.99999999999994, -63.99999999999991, -64.00000000000006, -64.00000000000015, -64.00000000000006, -64.00000000000012, -64.00000000000007, -64.00000000000009, -64.00000000000015, -64.00000000000043, -64.0, -64.00000000000005, -63.99999999999995, -64.00000000000002, -63.99999999999997, -63.99999999999993, -63.99999999999995, -63.99999999999991, -63.99999999999998, -63.99999999999999, -63.99999999999999, -63.99999999999999, -63.99999999999999, -63.99999999999996, -63.99999999999996, -64.00000000000002, -63.99999999999999, -63.99999999999995, -64.00000000000002, -64.00000000000003, -64.00000000000002, -64.0, -64.0, -63.99999999999998, -64.00000000000003, -64.00000000000003, -64.00000000000003, -64.0, -64.00000000000003, -64.00000000000003, -64.00000000000006, -64.00000000000012, -64.0, -64.0, -63.99999999999999, -63.99999999999999, -63.99999999999999, -63.99999999999999, -63.99999999999997, -63.99999999999995, -63.99999999999999, -63.99999999999999, -63.99999999999999, -63.99999999999996, -63.99999999999999, -63.99999999999997, -63.99999999999996, -63.99999999999989, -64.0, -63.99999999999999, -63.99999999999999, -63.99999999999998, -63.99999999999999, -64.0, -63.99999999999999, -63.99999999999998, -64.0, -64.0, -64.0, -63.99999999999998, -64.00000000000002, -63.99999999999999, -64.0, -63.99999999999999, -64.0, -63.99999999999999, -64.0, -63.99999999999998, -64.0, -64.00000000000002, -63.99999999999999, -63.99999999999999, -64.00000000000002, -64.0, -64.00000000000002, -63.99999999999998, -64.00000000000002, -64.00000000000003, -64.00000000000002, -63.99999999999994, -64.0, -64.00000000000005, -63.99999999999996, -63.99999999999992, -63.99999999999999, -64.0, -63.99999999999996, -63.99999999999996, -63.99999999999999, -63.99999999999994, -63.99999999999997, -63.99999999999991, -63.99999999999995, -63.99999999999995, -63.99999999999992, -63.99999999999971, -64.0, -63.99999999999992, -64.00000000000002, -63.99999999999991, -64.0, -64.00000000000006, -64.00000000000003, -64.00000000000006, -63.99999999999999, -63.99999999999991, -63.99999999999995, -63.99999999999977, -63.99999999999997, -64.00000000000009, -64.00000000000019, -63.99999999999989], dtype=float)\n",
"\n",
"imaginary part:\t array([0.0, 2607.070967813331, 1302.74992799918, 867.6268315105549, 649.8029048069671, 518.8982914353221, 431.4529539465592, 368.8410883276037, 321.7497274960543, 285.0049423625223, 255.5023221612854, 231.2662836036754, 210.9797253720525, 193.7307650763375, 178.8680174393905, 165.9137611352366, 154.5096679918781, 144.3816865234661, 135.316630883113, 127.1461627099753, 119.7355783545209, 112.9757996860283, 106.7775491573444, 101.0670443494533, 95.7827688105913, 90.87301782362191, 86.29401046315012, 82.00842112233945, 77.98422563763051, 74.19378606933986, 70.61311844694247, 67.22130215935096, 64.0, 60.93306538885926, 58.00621881722539, 55.20677966443128, 52.52344261303427, 49.94609021785241, 47.46563496141023, 45.0738854953628, 42.76343282683512, 40.52755303536438, 38.36012375564311, 36.25555217474197, 34.20871270085064, 32.21489278715481, 30.26974565704444, 28.36924888901518, 26.50966799187809, 24.68752424135174, 22.89956616412955, 21.14274415003736, 19.41418775086992, 17.71118528898653, 16.03116545224356, 14.37168059629627, 12.7303915122981, 11.10505344882276, 9.493503202454235, 7.893647112751268, 6.303449814858539, 4.720923617436767, 3.144118385245861, 1.571111814971346, 0.0, -1.571111814971346, -3.144118385245861, -4.720923617436767, -6.303449814858539, -7.893647112751296, -9.493503202454235, -11.10505344882276, -12.73039151229813, -14.3716805962963, -16.03116545224356, -17.71118528898656, -19.41418775086994, -21.1427441500374, -22.89956616412958, -24.68752424135193, -26.50966799187809, -28.36924888901534, -30.26974565704446, -32.21489278715484, -34.20871270085064, -36.25555217474198, -38.36012375564309, -40.52755303536436, -42.76343282683511, -45.07388549536283, -47.46563496141026, -49.94609021785239, -52.52344261303425, -55.20677966443133, -58.00621881722539, -60.93306538885924, -64.0, -67.22130215935099, -70.61311844694247, -74.19378606933987, -77.98422563763052, -82.00842112233952, -86.29401046315008, -90.87301782362188, -95.78276881059134, -101.0670443494534, -106.7775491573445, -112.9757996860283, -119.735578354521, -127.1461627099754, -135.3166308831131, -144.3816865234664, -154.5096679918781, -165.9137611352367, -178.8680174393905, -193.7307650763375, -210.9797253720525, -231.2662836036754, -255.5023221612853, -285.0049423625221, -321.7497274960542, -368.8410883276038, -431.4529539465592, -518.8982914353219, -649.8029048069671, -867.6268315105551, -1302.74992799918, -2607.07096781333], dtype=float)\n",
"\n",
"real part:\t array([8128.0, -64.00000000000047, -64.00000000000003, -64.00000000000043, -64.0, -63.99999999999991, -63.99999999999994, -63.99999999999991, -64.00000000000006, -64.00000000000015, -64.00000000000006, -64.00000000000012, -64.00000000000007, -64.00000000000009, -64.00000000000015, -64.00000000000043, -64.0, -64.00000000000005, -63.99999999999995, -64.00000000000002, -63.99999999999997, -63.99999999999993, -63.99999999999995, -63.99999999999991, -63.99999999999998, -63.99999999999999, -63.99999999999999, -63.99999999999999, -63.99999999999999, -63.99999999999996, -63.99999999999996, -64.00000000000002, -63.99999999999999, -63.99999999999995, -64.00000000000002, -64.00000000000003, -64.00000000000002, -64.0, -64.0, -63.99999999999998, -64.00000000000003, -64.00000000000003, -64.00000000000003, -64.0, -64.00000000000003, -64.00000000000003, -64.00000000000006, -64.00000000000012, -64.0, -64.0, -63.99999999999999, -63.99999999999999, -63.99999999999999, -63.99999999999999, -63.99999999999997, -63.99999999999995, -63.99999999999999, -63.99999999999999, -63.99999999999999, -63.99999999999996, -63.99999999999999, -63.99999999999997, -63.99999999999996, -63.99999999999989, -64.0, -63.99999999999999, -63.99999999999999, -63.99999999999998, -63.99999999999999, -64.0, -63.99999999999999, -63.99999999999998, -64.0, -64.0, -64.0, -63.99999999999998, -64.00000000000002, -63.99999999999999, -64.0, -63.99999999999999, -64.0, -63.99999999999999, -64.0, -63.99999999999998, -64.0, -64.00000000000002, -63.99999999999999, -63.99999999999999, -64.00000000000002, -64.0, -64.00000000000002, -63.99999999999998, -64.00000000000002, -64.00000000000003, -64.00000000000002, -63.99999999999994, -64.0, -64.00000000000005, -63.99999999999996, -63.99999999999992, -63.99999999999999, -64.0, -63.99999999999996, -63.99999999999996, -63.99999999999999, -63.99999999999994, -63.99999999999997, -63.99999999999991, -63.99999999999995, -63.99999999999995, -63.99999999999992, -63.99999999999971, -64.0, -63.99999999999992, -64.00000000000002, -63.99999999999991, -64.0, -64.00000000000006, -64.00000000000003, -64.00000000000006, -63.99999999999999, -63.99999999999991, -63.99999999999995, -63.99999999999977, -63.99999999999997, -64.00000000000009, -64.00000000000019, -63.99999999999989], dtype=float)\n",
"\n",
"imaginary part:\t array([0.0, 2607.070967813331, 1302.74992799918, 867.6268315105549, 649.8029048069671, 518.8982914353221, 431.4529539465592, 368.8410883276037, 321.7497274960543, 285.0049423625223, 255.5023221612854, 231.2662836036754, 210.9797253720525, 193.7307650763375, 178.8680174393905, 165.9137611352366, 154.5096679918781, 144.3816865234661, 135.316630883113, 127.1461627099753, 119.7355783545209, 112.9757996860283, 106.7775491573444, 101.0670443494533, 95.7827688105913, 90.87301782362191, 86.29401046315012, 82.00842112233945, 77.98422563763051, 74.19378606933986, 70.61311844694247, 67.22130215935096, 64.0, 60.93306538885926, 58.00621881722539, 55.20677966443128, 52.52344261303427, 49.94609021785241, 47.46563496141023, 45.0738854953628, 42.76343282683512, 40.52755303536438, 38.36012375564311, 36.25555217474197, 34.20871270085064, 32.21489278715481, 30.26974565704444, 28.36924888901518, 26.50966799187809, 24.68752424135174, 22.89956616412955, 21.14274415003736, 19.41418775086992, 17.71118528898653, 16.03116545224356, 14.37168059629627, 12.7303915122981, 11.10505344882276, 9.493503202454235, 7.893647112751268, 6.303449814858539, 4.720923617436767, 3.144118385245861, 1.571111814971346, 0.0, -1.571111814971346, -3.144118385245861, -4.720923617436767, -6.303449814858539, -7.893647112751296, -9.493503202454235, -11.10505344882276, -12.73039151229813, -14.3716805962963, -16.03116545224356, -17.71118528898656, -19.41418775086994, -21.1427441500374, -22.89956616412958, -24.68752424135193, -26.50966799187809, -28.36924888901534, -30.26974565704446, -32.21489278715484, -34.20871270085064, -36.25555217474198, -38.36012375564309, -40.52755303536436, -42.76343282683511, -45.07388549536283, -47.46563496141026, -49.94609021785239, -52.52344261303425, -55.20677966443133, -58.00621881722539, -60.93306538885924, -64.0, -67.22130215935099, -70.61311844694247, -74.19378606933987, -77.98422563763052, -82.00842112233952, -86.29401046315008, -90.87301782362188, -95.78276881059134, -101.0670443494534, -106.7775491573445, -112.9757996860283, -119.735578354521, -127.1461627099754, -135.3166308831131, -144.3816865234664, -154.5096679918781, -165.9137611352367, -178.8680174393905, -193.7307650763375, -210.9797253720525, -231.2662836036754, -255.5023221612853, -285.0049423625221, -321.7497274960542, -368.8410883276038, -431.4529539465592, -518.8982914353219, -649.8029048069671, -867.6268315105551, -1302.74992799918, -2607.07096781333], dtype=float)\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"\n",
"# x = np.linspace(0, 10, num=1024)\n",
"x = np.array(range(128))\n",
"y = np.sin(x)\n",
"z = np.zeros(len(x))\n",
"\n",
"a, b = np.fft(x)\n",
"print('real part:\\t', a)\n",
"print('\\nimaginary part:\\t', b)\n",
"\n",
"c, d = np.fft(x, z)\n",
"print('\\nreal part:\\t', c)\n",
"print('\\nimaginary part:\\t', d)"
]
},
{
"cell_type": "code",
"execution_count": 234,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-12T16:30:19.694403Z",
"start_time": "2019-12-12T16:30:19.682184Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n",
"\n",
"\n"
]
}
],
"source": [
"%%micropython -unix 1\n",
"\n",
"import ulab as np\n",
"\n",
"a = np.array([1, 2, 3, 4])\n",
"print(a*a)"
]
},
{
"cell_type": "code",
"execution_count": 237,
"metadata": {
"ExecuteTime": {
"end_time": "2019-12-12T16:32:54.356989Z",
"start_time": "2019-12-12T16:32:54.351814Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"array([10000, 28928], dtype=uint16)"
]
},
"execution_count": 237,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = array([100, 400], dtype=uint16)\n",
"a*a"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}