{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# TaylorMap Demo" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Initialize `mtflib` global parameters\n", "\n", "This is a mandatory step before using any `mtflib` functionality. We set the maximum order of the Taylor series and the number of variables." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:03.295424Z", "iopub.status.busy": "2025-09-26T23:21:03.295296Z", "iopub.status.idle": "2025-09-26T23:21:04.455176Z", "shell.execute_reply": "2025-09-26T23:21:04.454622Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Initializing MTF globals with: _MAX_ORDER=4, _MAX_DIMENSION=2\n", "Loading/Precomputing Taylor coefficients up to order 4\n", "Global precomputed coefficients loading/generation complete.\n", "Size of precomputed_coefficients dictionary in memory: 464 bytes, 0.45 KB, 0.00 MB\n", "MTF globals initialized: _MAX_ORDER=4, _MAX_DIMENSION=2, _INITIALIZED=True\n", "Max coefficient count (order=4, nvars=2): 15\n", "Precomputed coefficients loaded and ready for use.\n" ] } ], "source": [ "import numpy as np\n", "\n", "from mtflib import TaylorMap, mtf\n", "\n", "try:\n", " mtf.initialize_mtf(max_order=4, max_dimension=2)\n", "except RuntimeError:\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Create two `TaylorMap` objects\n", "\n", "Let's define two maps from R^2 to R^2." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Map 1: F(x,y) = [sin(x), cos(y)]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:04.483251Z", "iopub.status.busy": "2025-09-26T23:21:04.482976Z", "iopub.status.idle": "2025-09-26T23:21:04.492266Z", "shell.execute_reply": "2025-09-26T23:21:04.491749Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TaylorMap with 2 components (input dim: 2):\n", "--- Component 1 ---\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 1 (1, 0)\n", "1 -1.666666666667e-01 3 (3, 0)\n", "\n", "--- Component 2 ---\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 0 (0, 0)\n", "1 -5.000000000000e-01 2 (0, 2)\n", "2 4.166666666667e-02 4 (0, 4)\n", "\n", "\n" ] } ], "source": [ "x = mtf.var(1)\n", "y = mtf.var(2)\n", "sin_x = mtf.sin(x)\n", "cos_y = mtf.cos(y)\n", "map_F = TaylorMap([sin_x, cos_y])\n", "print(map_F)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Map 2: G(x,y) = [x + y, x - y]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:04.493573Z", "iopub.status.busy": "2025-09-26T23:21:04.493439Z", "iopub.status.idle": "2025-09-26T23:21:04.499090Z", "shell.execute_reply": "2025-09-26T23:21:04.498638Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TaylorMap with 2 components (input dim: 2):\n", "--- Component 1 ---\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 1 (1, 0)\n", "1 1.000000000000e+00 1 (0, 1)\n", "\n", "--- Component 2 ---\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 1 (1, 0)\n", "1 -1.000000000000e+00 1 (0, 1)\n", "\n", "\n" ] } ], "source": [ "map_G = TaylorMap([x + y, x - y])\n", "print(map_G)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Demonstrate Operations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Operation 1: Addition (F + G)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:04.500438Z", "iopub.status.busy": "2025-09-26T23:21:04.500286Z", "iopub.status.idle": "2025-09-26T23:21:04.506087Z", "shell.execute_reply": "2025-09-26T23:21:04.505664Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TaylorMap with 2 components (input dim: 2):\n", "--- Component 1 ---\n", " Coefficient Order Exponents\n", "0 2.000000000000e+00 1 (1, 0)\n", "1 1.000000000000e+00 1 (0, 1)\n", "2 -1.666666666667e-01 3 (3, 0)\n", "\n", "--- Component 2 ---\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 0 (0, 0)\n", "1 1.000000000000e+00 1 (1, 0)\n", "2 -1.000000000000e+00 1 (0, 1)\n", "3 -5.000000000000e-01 2 (0, 2)\n", "4 4.166666666667e-02 4 (0, 4)\n", "\n", "\n" ] } ], "source": [ "map_sum = map_F + map_G\n", "print(map_sum)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Operation 2: Composition F(G(x,y))\n", "\n", "This computes `sin(x+y)` and `cos(x-y)`." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:04.507329Z", "iopub.status.busy": "2025-09-26T23:21:04.507197Z", "iopub.status.idle": "2025-09-26T23:21:04.514534Z", "shell.execute_reply": "2025-09-26T23:21:04.514027Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TaylorMap with 2 components (input dim: 2):\n", "--- Component 1 ---\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 1 (1, 0)\n", "1 1.000000000000e+00 1 (0, 1)\n", "2 -1.666666666667e-01 3 (3, 0)\n", "3 -5.000000000000e-01 3 (2, 1)\n", "4 -5.000000000000e-01 3 (1, 2)\n", "5 -1.666666666667e-01 3 (0, 3)\n", "\n", "--- Component 2 ---\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 0 (0, 0)\n", "1 -5.000000000000e-01 2 (2, 0)\n", "2 1.000000000000e+00 2 (1, 1)\n", "3 -5.000000000000e-01 2 (0, 2)\n", "4 4.166666666667e-02 4 (4, 0)\n", "5 -1.666666666667e-01 4 (3, 1)\n", "6 2.500000000000e-01 4 (2, 2)\n", "7 -1.666666666667e-01 4 (1, 3)\n", "8 4.166666666667e-02 4 (0, 4)\n", "\n", "\n" ] } ], "source": [ "map_composed = map_F.compose(map_G)\n", "print(map_composed)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Operation 3: Trace\n", "\n", "The trace is the sum of the diagonal elements of the Jacobian matrix's linear part. For `F(x,y) = [sin(x), cos(y)]`, the Jacobian is `[[cos(x), 0], [0, -sin(y)]]`. At (0,0), the linear part is `[[1, 0], [0, 0]]`, so the trace is 1." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:04.515871Z", "iopub.status.busy": "2025-09-26T23:21:04.515743Z", "iopub.status.idle": "2025-09-26T23:21:04.518213Z", "shell.execute_reply": "2025-09-26T23:21:04.517763Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Trace of F at (0,0): (1+0j)\n" ] } ], "source": [ "trace_F = map_F.trace()\n", "print(f\"Trace of F at (0,0): {trace_F}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Operation 4: Substitution\n", "\n", "Let's evaluate the composed map `F(G(x,y))` at `x=0.5, y=0.2`. This is equivalent to evaluating `[sin(x+y), cos(x-y)]` at the point, which is `sin(0.7)` and `cos(0.3)`." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:04.519399Z", "iopub.status.busy": "2025-09-26T23:21:04.519281Z", "iopub.status.idle": "2025-09-26T23:21:04.522149Z", "shell.execute_reply": "2025-09-26T23:21:04.521727Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "F(G(0.5, 0.2)) from TaylorMap: [0.64283333 0.9553375 ]\n", "NumPy equivalent for comparison: [np.float64(0.644217687237691), np.float64(0.955336489125606)]\n" ] } ], "source": [ "eval_point = {1: 0.5, 2: 0.2}\n", "result_array = map_composed.substitute(eval_point)\n", "print(f\"F(G(0.5, 0.2)) from TaylorMap: {result_array}\")\n", "\n", "# Compare with numpy to verify\n", "numpy_result = [np.sin(0.7), np.cos(0.3)]\n", "print(f\"NumPy equivalent for comparison: {numpy_result}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Demonstrate Map Inversion" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Operation 5: Inversion of a Map\n", "\n", "Here we create an invertible map `F(x,y) = [x + 0.1*y^2, y - 0.1*x^2]`, invert it, and then compose the result with the original map to verify that we get the identity map." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:04.523391Z", "iopub.status.busy": "2025-09-26T23:21:04.523274Z", "iopub.status.idle": "2025-09-26T23:21:04.541739Z", "shell.execute_reply": "2025-09-26T23:21:04.541263Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- Original Map to Invert ---\n", "TaylorMap with 2 components (input dim: 2):\n", "--- Component 1 ---\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 1 (1, 0)\n", "1 1.000000000000e-01 2 (0, 2)\n", "\n", "--- Component 2 ---\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 1 (0, 1)\n", "1 -1.000000000000e-01 2 (2, 0)\n", "\n", "\n", "\n", "--- Inverted Map ---\n", "TaylorMap with 2 components (input dim: 2):\n", "--- Component 1 ---\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 1 (1, 0)\n", "1 -1.000000000000e-01 2 (0, 2)\n", "2 -2.000000000000e-02 3 (2, 1)\n", "3 -1.000000000000e-03 4 (4, 0)\n", "4 4.000000000000e-03 4 (1, 3)\n", "\n", "--- Component 2 ---\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 1 (0, 1)\n", "1 1.000000000000e-01 2 (2, 0)\n", "2 -2.000000000000e-02 3 (1, 2)\n", "3 -4.000000000000e-03 4 (3, 1)\n", "4 1.000000000000e-03 4 (0, 4)\n", "\n", "\n", "\n", "--- Composition of F_inv o F ---\n", "TaylorMap with 2 components (input dim: 2):\n", "--- Component 1 ---\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 1 (1, 0)\n", "\n", "--- Component 2 ---\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 1 (0, 1)\n", "\n", "\n", "\n", "(Result should be close to the identity map [x, y])\n" ] } ], "source": [ "# Create an invertible map\n", "x_inv = mtf.var(1)\n", "y_inv = mtf.var(2)\n", "f1_inv = x_inv + 0.1 * y_inv**2\n", "f2_inv = y_inv - 0.1 * x_inv**2\n", "map_to_invert = TaylorMap([f1_inv, f2_inv])\n", "print(\"--- Original Map to Invert ---\")\n", "print(map_to_invert)\n", "\n", "# Invert the map\n", "inverted_map = map_to_invert.invert()\n", "print(\"\\n--- Inverted Map ---\")\n", "print(inverted_map)\n", "\n", "# Verify by composing F and F_inv\n", "composition = inverted_map.compose(map_to_invert)\n", "print(\"\\n--- Composition of F_inv o F ---\")\n", "print(composition)\n", "print(\"\\n(Result should be close to the identity map [x, y])\")" ] } ], "metadata": { "kernelspec": { "display_name": "mtf", "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.12.11" } }, "nbformat": 4, "nbformat_minor": 4 }