{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "4404ab81-3b3e-459c-b722-ac376fc826d2", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 15, "id": "e0ab07d7-0f0e-44e1-a37e-6885d8bb34c4", "metadata": {}, "outputs": [], "source": [ "def createXYGrid(bbox, meshsize):\n", " x1, y1, x2, y2 = bbox\n", " xgrid, ygrid =np.meshgrid(np.linspace(x1, x2 ,meshsize), np.linspace(y1,y2,meshsize))\n", " \n", " zeroes = np.zeros(xgrid.shape)\n", " \n", " # create the individual position vectors\n", " vecs = np.dstack((xgrid, ygrid, zeroes))\n", " return xgrid, ygrid,vecs\n", "\n", "def createXZGrid(bbox, meshsize):\n", " x1, z1, x2, z2 = bbox\n", " xgrid, zgrid =np.meshgrid(np.linspace(x1, x2 ,meshsize), np.linspace(z1,z2,meshsize))\n", " \n", " zeroes = np.zeros(xgrid.shape)\n", " \n", " # create the individual position vectors\n", " vecs = np.dstack((xgrid, zeroes, zgrid))\n", " return xgrid, zgrid, vecs" ] }, { "cell_type": "code", "execution_count": 3, "id": "1d089355-e77d-435f-961a-b471e2126d10", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-3, 6, -3])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.cross([1,2,3],[4,5,6])" ] }, { "cell_type": "code", "execution_count": 4, "id": "813faaf7-3df2-4a79-9cb6-815dad5ea1ed", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-3, 6, -3],\n", " [-6, 12, -6]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.cross([1,2,],[[4,5,6],[7,8,9]])" ] }, { "cell_type": "code", "execution_count": 8, "id": "e7b65392-0447-4c72-94de-3db7756482d0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[1, 4],\n", " [2, 5],\n", " [3, 6]]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.dstack(([1,2,3],[4,5,6]))\n", "x" ] }, { "cell_type": "code", "execution_count": 10, "id": "edec09a0-a99b-4c6b-9c8c-dce6061fca80", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 3, 2)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.shape" ] }, { "cell_type": "code", "execution_count": 12, "id": "1d5ffa0a-53af-49b2-ac7a-b1f2b54831a6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 4],\n", " [2, 5],\n", " [3, 6]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.stack(([1,2,3],[4,5,6]), axis=1); x" ] }, { "cell_type": "code", "execution_count": 13, "id": "9a579aca-c318-4490-90e8-73b6494c857c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 2)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.shape" ] }, { "cell_type": "code", "execution_count": 14, "id": "de47f268-ab14-42a9-897a-18d6ba1eb47d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 4]\n", "[2 5]\n", "[3 6]\n" ] } ], "source": [ "for zz in x: print (zz)" ] }, { "cell_type": "code", "execution_count": 19, "id": "1e37913c-4d29-4f8b-ac09-76936abba983", "metadata": {}, "outputs": [], "source": [ "meshsize = 100\n", "bbox = (-.03,-.03, .03, .03) # bounding box of visual area in meters\n", "\n", "# create the individual position vectors\n", "xgrid, ygrid, vecs = createXYGrid(bbox, meshsize)" ] }, { "cell_type": "code", "execution_count": 21, "id": "31a7be65-d588-483f-bf66-2263d7122f1f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(100, 100, 3)" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vecs.shape" ] }, { "cell_type": "code", "execution_count": 22, "id": "3f8a36c3-0250-4231-8bdd-1da0ad8e1202", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-0.03, -0.03, 0. ])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vecs[0][0]" ] }, { "cell_type": "code", "execution_count": 24, "id": "1fce0842-6218-48c0-a722-bf7661812049", "metadata": {}, "outputs": [], "source": [ "radius = .03\n", "angnum = 720 # The number of evenly spaced angle values around a circumference of length 2*pi*R\n", "\n", "\n", "angles = np.linspace(0, 2*np.pi, angnum, endpoint=False)\n", "\n", "# create an array of the vector l\n", "coss = np.cos(angles)\n", "sins = np.sin(angles)\n", "zz = np.zeros_like(angles)\n", "l = np.stack((coss, sins, zz), axis=-1)*radius" ] }, { "cell_type": "code", "execution_count": 25, "id": "3455ea5d-04c5-48c6-93c0-a6e16a52147f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.03 , 0. , 0. ],\n", " [ 0.02999886, 0.0002618 , 0. ],\n", " [ 0.02999543, 0.00052357, 0. ],\n", " ...,\n", " [ 0.02998972, -0.00078531, 0. ],\n", " [ 0.02999543, -0.00052357, 0. ],\n", " [ 0.02999886, -0.0002618 , 0. ]])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 26, "id": "f2336b6f-4764-4755-8b5c-08a07a5f36ad", "metadata": {}, "outputs": [], "source": [ "dl = np.diff(l, n=1, axis=0)\n", "dl = np.append(dl, [dl[0]], axis=0)" ] }, { "cell_type": "code", "execution_count": 27, "id": "83baf809-301c-4f24-9e01-cacb9c039ca7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((720, 3), (720, 3))" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(l.shape, dl.shape)" ] }, { "cell_type": "code", "execution_count": 28, "id": "59f1c892-189b-491c-b596-2ad3fb8bd10b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(720, 2, 3)" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.stack((l, dl), axis=1)\n", "x.shape" ] }, { "cell_type": "code", "execution_count": 30, "id": "f60780ad-3731-4537-a1a5-fa08d896ab8c", "metadata": {}, "outputs": [], "source": [ "a,b = x[0]" ] }, { "cell_type": "code", "execution_count": 31, "id": "3674b37b-3e73-4643-8b62-5ad563aba87f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.03, 0. , 0. ])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 33, "id": "ba871b06-904c-4d77-a709-ae75083191c4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(720, 3)" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[:,0].shape" ] }, { "cell_type": "code", "execution_count": 34, "id": "1f37fccc-29d3-46bb-8de6-9c69169ee15d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(100, 100, 3)" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vecs.shape" ] }, { "cell_type": "code", "execution_count": 48, "id": "957eea1a-2a69-41f7-8318-0bdef2fb1730", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(100, 100, 720, 3)" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rp = vecs[:,:,np.newaxis] - x[:,0]\n", "rp.shape" ] }, { "cell_type": "code", "execution_count": 49, "id": "9ff820bc-20ba-44b3-bb09-e75bb696d9a7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-0.06, -0.03, 0. ])" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rp[0,0,0]" ] }, { "cell_type": "code", "execution_count": 51, "id": "1556eea6-d6e3-45d2-8d49-5bf16bdc1909", "metadata": {}, "outputs": [], "source": [ "cp = np.cross(dl,rp)" ] }, { "cell_type": "code", "execution_count": 52, "id": "aed0cdd3-c9ec-4c7f-bb10-37a0e19bd832", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(100, 100, 720, 3)" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cp.shape" ] }, { "cell_type": "code", "execution_count": 53, "id": "33f13442-72ea-4b73-adef-59cd65d93248", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(100, 100, 3)" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cp[:,:,0,:].shape" ] }, { "cell_type": "code", "execution_count": 46, "id": "81ee59eb-42d6-4bbe-8bc7-e88401cb6078", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([-0.06, -0.03, 0. ]),\n", " array([[ 0.03 , 0. , 0. ],\n", " [ 0.02999886, 0.0002618 , 0. ],\n", " [ 0.02999543, 0.00052357, 0. ],\n", " ...,\n", " [ 0.02998972, -0.00078531, 0. ],\n", " [ 0.02999543, -0.00052357, 0. ],\n", " [ 0.02999886, -0.0002618 , 0. ]]))" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aa[0,0,0], x[:,0]" ] }, { "cell_type": "code", "execution_count": 47, "id": "ce7423bc-5347-4eb4-8e07-a2d960f29a0e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-0.03, -0.03, 0. ])" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vecs[0,0]" ] }, { "cell_type": "code", "execution_count": 54, "id": "694a89cc-eb23-445d-b62a-3d7bf334cec6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1],\n", " [2, 2],\n", " [3, 3],\n", " [4, 4]])" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.array([[1,1],[2,2],[3,3],[4,4]]); a" ] }, { "cell_type": "code", "execution_count": 55, "id": "02c4e7ca-544f-48be-b6fa-bad56ca98466", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4, 2)" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.shape" ] }, { "cell_type": "code", "execution_count": 56, "id": "c860c660-68dd-472b-b4f2-3e4593a5d3d8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 3, 3],\n", " [ 2, 2],\n", " [ 1, 1],\n", " [ 0, 0],\n", " [-1, -1],\n", " [-2, -2]])" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = np.array([[3,3],[2,2],[1,1],[0,0],[-1, -1], [-2, -2]]);b" ] }, { "cell_type": "code", "execution_count": 57, "id": "01d8d48a-a2ae-4327-b1c9-483e3b908e32", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(6, 2)" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.shape" ] }, { "cell_type": "code", "execution_count": 58, "id": "9cc873bb-7ae9-4b80-ab51-b78035ca8dfd", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "operands could not be broadcast together with shapes (4,2) (6,2) ", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[58], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\n", "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (4,2) (6,2) " ] } ], "source": [ "a - b" ] }, { "cell_type": "code", "execution_count": 64, "id": "d0cf3914-992b-45dc-b1ee-efd5b2c05deb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4, 1, 2)" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a1 = a[:, np.newaxis]\n", "a1.shape" ] }, { "cell_type": "code", "execution_count": 62, "id": "c33433da-b4ab-4d4b-8093-f7e868b009aa", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(6, 2)" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.shape" ] }, { "cell_type": "code", "execution_count": 67, "id": "876fb3fa-b1a1-4252-a278-0213d24d5217", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4, 6, 2)" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = a1 - b\n", "c.shape" ] }, { "cell_type": "code", "execution_count": 70, "id": "273e3008-659f-4103-9d28-aaf96fd0b7ef", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.ndim\n" ] }, { "cell_type": "code", "execution_count": 71, "id": "bad40882-ad09-4ea9-a902-e5db2bd7b826", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(c)" ] }, { "cell_type": "code", "execution_count": null, "id": "87b48a14-32bd-4015-b0f3-1989654653c8", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }