{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "262a5ec8-2553-4237-ab62-319b6ca22089", "metadata": {}, "source": [ "# Example-59: Matrix (element)" ] }, { "cell_type": "code", "execution_count": 1, "id": "a70494a3-5f9f-44c4-8d98-bfbf92ed8163", "metadata": {}, "outputs": [], "source": [ "# Can be used to model 4x4 linear elements\n", "# Transport matrix is constructed given elements of the symmetric matrix\n", "# M = exp(S A)" ] }, { "cell_type": "code", "execution_count": 2, "id": "2003aa98-76f1-4a84-9e2a-9c4ad1fbdb87", "metadata": {}, "outputs": [], "source": [ "import torch\n", "from model.library.drift import Drift\n", "from model.library.matrix import Matrix" ] }, { "cell_type": "code", "execution_count": 3, "id": "035e68e0-235c-4607-81a7-966f60189ef0", "metadata": {}, "outputs": [], "source": [ "# Set state\n", "\n", "state = torch.tensor([0.01, -0.05, -0.01, 0.05], dtype=torch.float64)" ] }, { "cell_type": "code", "execution_count": 4, "id": "eab9b355-261d-4ba9-9701-0060252207d6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([-0.0650, -0.0500, 0.0650, 0.0500], dtype=torch.float64)\n", "\n", "tensor([[1.0000, 1.5000, 0.0000, 0.0000],\n", " [0.0000, 1.0000, 0.0000, 0.0000],\n", " [0.0000, 0.0000, 1.0000, 1.5000],\n", " [0.0000, 0.0000, 0.0000, 1.0000]], dtype=torch.float64)\n", "\n" ] } ], "source": [ "# Drift (forward)\n", "\n", "D = Drift('D', length=1.5)\n", "\n", "print(D(state))\n", "print()\n", "\n", "print(torch.func.jacrev(D)(state))\n", "print()" ] }, { "cell_type": "code", "execution_count": 5, "id": "78150170-966c-49ff-bd57-da4507141185", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([ 0.0850, -0.0500, -0.0850, 0.0500], dtype=torch.float64)\n", "\n", "tensor([[ 1.0000, -1.5000, 0.0000, 0.0000],\n", " [ 0.0000, 1.0000, 0.0000, 0.0000],\n", " [ 0.0000, 0.0000, 1.0000, -1.5000],\n", " [ 0.0000, 0.0000, 0.0000, 1.0000]], dtype=torch.float64)\n", "\n" ] } ], "source": [ "# Drift (inverse)\n", "\n", "D = Drift('D', length=1.5).inverse()\n", "\n", "print(D(state))\n", "print()\n", "\n", "print(torch.func.jacrev(D)(state))\n", "print()" ] }, { "cell_type": "code", "execution_count": 6, "id": "0ab134e4-1b2d-4dc1-ba0f-f678a52463d3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([-0.0650, -0.0500, 0.0650, 0.0500], dtype=torch.float64)\n", "\n", "tensor([[1.0000, 1.5000, 0.0000, 0.0000],\n", " [0.0000, 1.0000, 0.0000, 0.0000],\n", " [0.0000, 0.0000, 1.0000, 1.5000],\n", " [0.0000, 0.0000, 0.0000, 1.0000]], dtype=torch.float64)\n", "\n" ] } ], "source": [ "# Matrix (forward)\n", "\n", "[a11, a12, a13, a14, a22, a23, a24, a33, a34, a44] = [0.0, 0.0, 0.0, 0.0, 1.5, 0.0, 0.0, 0.0, 0.0, 1.5]\n", "\n", "M = Matrix('M', length=1.5, A=[a11, a12, a13, a14, a22, a23, a24, a33, a34, a44])\n", "\n", "print(M(state))\n", "print()\n", "\n", "print(torch.func.jacrev(M)(state))\n", "print()" ] }, { "cell_type": "code", "execution_count": 7, "id": "cb33c9a4-87bd-475e-a910-6009a3548913", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([ 0.0850, -0.0500, -0.0850, 0.0500], dtype=torch.float64)\n", "\n", "tensor([[ 1.0000, -1.5000, 0.0000, 0.0000],\n", " [ 0.0000, 1.0000, 0.0000, 0.0000],\n", " [ 0.0000, 0.0000, 1.0000, -1.5000],\n", " [ 0.0000, 0.0000, 0.0000, 1.0000]], dtype=torch.float64)\n", "\n" ] } ], "source": [ "# Matrix (inverse)\n", "\n", "[a11, a12, a13, a14, a22, a23, a24, a33, a34, a44] = [0.0, 0.0, 0.0, 0.0, 1.5, 0.0, 0.0, 0.0, 0.0, 1.5]\n", "\n", "M = Matrix('M', length=1.5, A=[a11, a12, a13, a14, a22, a23, a24, a33, a34, a44]).inverse()\n", "\n", "print(M(state))\n", "print()\n", "\n", "print(torch.func.jacrev(M)(state))\n", "print()" ] }, { "cell_type": "code", "execution_count": null, "id": "f8c8fd1c-de33-44df-8f83-3a93ffec164e", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "colab": { "collapsed_sections": [ "myt0_gMIOq7b", "5d97819c" ], "name": "03_frequency.ipynb", "provenance": [] }, "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.14.0" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false } }, "nbformat": 4, "nbformat_minor": 5 }