{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Multiply vectors and matrices\n", "This example notebook is translated from [ImageJ macro](https://github.com/clij/clij2-docs/tree/master/src/main/macro/anisotropic_coordinates.ijm).\n", "\n", "When dealing with three dimensional point coordinates, it is mandatory to take the voxel size into account, e.g. for measuring distances between points. This example shows how to multiply a list of coordinates by a given voxel size.\n", "\n", "## Initialize GPU" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pyclesperanto_prototype as cle\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load some vector and X/Y/Z-pointlist in the GPU" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Array shape:(3, 1)\n", "GPU array shape:(3, 1)\n" ] } ], "source": [ "voxel_size = np.asarray([[0.2], [0.2], [0.5]])\n", "print(\"Array shape:\" + str(voxel_size.shape))\n", "\n", "gpu_voxel_size = cle.push(voxel_size)\n", "print(\"GPU array shape:\" + str(voxel_size.shape))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Array shape:(3, 4)\n", "GPU array shape:(3, 4)\n" ] } ], "source": [ "pointlist = np.asarray(\n", " [\n", " [1, 3, 5, 11], # X\n", " [0, 6, 1, 2 ], # Y\n", " [9, 11, 6, 2 ] # Z\n", " ]\n", ")\n", "print(\"Array shape:\" + str(pointlist.shape))\n", "\n", "gpu_pointlist = cle.push(pointlist)\n", "print(\"GPU array shape:\" + str(gpu_pointlist.shape))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.2]\n", " [0.2]\n", " [0.5]]\n" ] } ], "source": [ "print(gpu_voxel_size)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1. 3. 5. 11.]\n", " [ 0. 6. 1. 2.]\n", " [ 9. 11. 6. 2.]]\n" ] } ], "source": [ "print(gpu_pointlist)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Element-wise matrix multiplication" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.2 0.6 1. 2.2]\n", " [0. 1.2 0.2 0.4]\n", " [4.5 5.5 3. 1. ]]\n" ] } ], "source": [ "gpu_updated_pointlist = cle.multiply_images(gpu_pointlist, gpu_voxel_size)\n", "\n", "print(gpu_updated_pointlist)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note: If the resulting image does not exist in first place, clesperanto has to guess its size. Thus, it takes the input size of the first image to operate. So, if you permute the input parameters, the resulting matrix size will be ‘wrong’:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.2]\n", " [0. ]\n", " [4.5]]\n" ] } ], "source": [ "gpu_updated_pointlist = cle.multiply_images(gpu_voxel_size, gpu_pointlist)\n", "\n", "print(gpu_updated_pointlist)" ] }, { "cell_type": "code", "execution_count": null, "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.9.13" } }, "nbformat": 4, "nbformat_minor": 4 }