{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "24a1b786",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "d325c8b8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.spatial.transform import Rotation as R\nimport numpy as np",
            "outputs": []
        },
        {
            "id": "d11a016b",
            "cell_type": "markdown",
            "source": "Single rotation applied on a single vector:\n",
            "metadata": {}
        },
        {
            "id": "7a9bb454",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "vector = np.array([1, 0, 0])\nr = R.from_rotvec([0, 0, np.pi/2])\nr.as_matrix()",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 2.22044605e-16, -1.00000000e+00,  0.00000000e+00],\n       [ 1.00000000e+00,  2.22044605e-16,  0.00000000e+00],\n       [ 0.00000000e+00,  0.00000000e+00,  1.00000000e+00]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "4a68f8c7",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "r.apply(vector)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([2.22044605e-16, 1.00000000e+00, 0.00000000e+00])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f6ac92f9",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "r.apply(vector).shape",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(3,)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "776c7db6",
            "cell_type": "markdown",
            "source": "Single rotation applied on multiple vectors:\n",
            "metadata": {}
        },
        {
            "id": "7179b12f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "vectors = np.array([\n[1, 0, 0],\n[1, 2, 3]])\nr = R.from_rotvec([0, 0, np.pi/4])\nr.as_matrix()",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 0.70710678, -0.70710678,  0.        ],\n       [ 0.70710678,  0.70710678,  0.        ],\n       [ 0.        ,  0.        ,  1.        ]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "7bc47bb7",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "r.apply(vectors)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 0.70710678,  0.70710678,  0.        ],\n       [-0.70710678,  2.12132034,  3.        ]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "b1744dde",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "r.apply(vectors).shape",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(2, 3)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "3bebf691",
            "cell_type": "markdown",
            "source": "Multiple rotations on a single vector:\n",
            "metadata": {}
        },
        {
            "id": "6e716958",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "r = R.from_rotvec([[0, 0, np.pi/4], [np.pi/2, 0, 0]])\nvector = np.array([1,2,3])\nr.as_matrix()",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[[ 7.07106781e-01, -7.07106781e-01,  0.00000000e+00],\n        [ 7.07106781e-01,  7.07106781e-01,  0.00000000e+00],\n        [ 0.00000000e+00,  0.00000000e+00,  1.00000000e+00]],\n       [[ 1.00000000e+00,  0.00000000e+00,  0.00000000e+00],\n        [ 0.00000000e+00,  2.22044605e-16, -1.00000000e+00],\n        [ 0.00000000e+00,  1.00000000e+00,  2.22044605e-16]]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "0643efc5",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "r.apply(vector)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[-0.70710678,  2.12132034,  3.        ],\n       [ 1.        , -3.        ,  2.        ]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "0bcb0e5a",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "r.apply(vector).shape",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(2, 3)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "130b13c0",
            "cell_type": "markdown",
            "source": "Multiple rotations on multiple vectors. Each rotation is applied on the\ncorresponding vector:\n",
            "metadata": {}
        },
        {
            "id": "71e11bc6",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "r = R.from_euler('zxy', [\n[0, 0, 90],\n[45, 30, 60]], degrees=True)\nvectors = [\n[1, 2, 3],\n[1, 0, -1]]\nr.apply(vectors)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 3.        ,  2.        , -1.        ],\n       [-0.09026039,  1.11237244, -0.86860844]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "85765bf9",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "r.apply(vectors).shape",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(2, 3)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f68b8e10",
            "cell_type": "markdown",
            "source": "It is also possible to apply the inverse rotation:\n",
            "metadata": {}
        },
        {
            "id": "b0c7dabd",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "r = R.from_euler('zxy', [\n[0, 0, 90],\n[45, 30, 60]], degrees=True)\nvectors = [\n[1, 2, 3],\n[1, 0, -1]]\nr.apply(vectors, inverse=True)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[-3.        ,  2.        ,  1.        ],\n       [ 1.09533535, -0.8365163 ,  0.3169873 ]])"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}