{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# `mtflib` Tutorial: Basic Functionality\n", "\n", "This notebook covers the fundamental concepts of `mtflib`, including initialization, variable creation, and basic operations on Taylor functions like arithmetic, differentiation, and integration." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Initialization of Global Settings\n", "\n", "Before using `mtflib`, we must initialize its global settings. This step is crucial as it defines the maximum order and dimension for the Taylor series expansions. \n", "\n", "**Important**: These settings should be initialized only once per session. If you need to change them, a kernel restart is required." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:15.278028Z", "iopub.status.busy": "2025-09-26T23:21:15.277871Z", "iopub.status.idle": "2025-09-26T23:21:16.518446Z", "shell.execute_reply": "2025-09-26T23:21:16.517979Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Initializing MTF globals with: _MAX_ORDER=8, _MAX_DIMENSION=3\n", "Loading/Precomputing Taylor coefficients up to order 8\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=8, _MAX_DIMENSION=3, _INITIALIZED=True\n", "Max coefficient count (order=8, nvars=3): 165\n", "Precomputed coefficients loaded and ready for use.\n" ] } ], "source": [ "import numpy as np\n", "\n", "from mtflib import mtf\n", "\n", "if not mtf.get_mtf_initialized_status():\n", " mtf.initialize_mtf(max_order=8, max_dimension=3)\n", "else:\n", " print(\"MTF globals are already initialized.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Defining Symbolic Variables\n", "\n", "We define symbolic variables using `mtf.var(var_id)`, where `var_id` is an integer from 1 to `max_dimension`." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:16.542615Z", "iopub.status.busy": "2025-09-26T23:21:16.542355Z", "iopub.status.idle": "2025-09-26T23:21:16.548746Z", "shell.execute_reply": "2025-09-26T23:21:16.548139Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Variable x:\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 1 (1, 0, 0)\n", "\n" ] } ], "source": [ "x = mtf.var(1)\n", "y = mtf.var(2)\n", "z = mtf.var(3)\n", "\n", "print(\"Variable x:\")\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Basic Operations with Taylor Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Arithmetic Operations\n", "\n", "Standard arithmetic operations like addition, subtraction, multiplication, and exponentiation are supported." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:16.550388Z", "iopub.status.busy": "2025-09-26T23:21:16.550237Z", "iopub.status.idle": "2025-09-26T23:21:16.561812Z", "shell.execute_reply": "2025-09-26T23:21:16.561276Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sin(x) + cos(x) =\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 0 (0, 0, 0)\n", "1 1.000000000000e+00 1 (1, 0, 0)\n", "2 -5.000000000000e-01 2 (2, 0, 0)\n", "3 -1.666666666667e-01 3 (3, 0, 0)\n", "4 4.166666666667e-02 4 (4, 0, 0)\n", "5 8.333333333333e-03 5 (5, 0, 0)\n", "6 -1.388888888889e-03 6 (6, 0, 0)\n", "7 -1.984126984127e-04 7 (7, 0, 0)\n", "8 2.480158730159e-05 8 (8, 0, 0)\n", "\n", "\n", "x * sin(x) =\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 2 (2, 0, 0)\n", "1 -1.666666666667e-01 4 (4, 0, 0)\n", "2 8.333333333333e-03 6 (6, 0, 0)\n", "3 -1.984126984127e-04 8 (8, 0, 0)\n", "\n", "\n", "x^2 =\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 2 (2, 0, 0)\n", "\n", "\n" ] } ], "source": [ "sin_x = mtf.sin(x)\n", "cos_x = mtf.cos(x)\n", "\n", "# Addition\n", "sum_tf = sin_x + cos_x\n", "print(f\"sin(x) + cos(x) =\\n{sum_tf}\\n\")\n", "\n", "# Multiplication\n", "product_tf = x * sin_x\n", "print(f\"x * sin(x) =\\n{product_tf}\\n\")\n", "\n", "# Exponentiation\n", "squared_x = x**2\n", "print(f\"x^2 =\\n{squared_x}\\n\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Differentiation\n", "\n", "You can compute the derivative of a Taylor function with respect to any variable." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:16.563372Z", "iopub.status.busy": "2025-09-26T23:21:16.563241Z", "iopub.status.idle": "2025-09-26T23:21:16.567512Z", "shell.execute_reply": "2025-09-26T23:21:16.567030Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Derivative of sin(x) w.r.t. x:\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 0 (0, 0, 0)\n", "1 -5.000000000000e-01 2 (2, 0, 0)\n", "2 4.166666666667e-02 4 (4, 0, 0)\n", "3 -1.388888888889e-03 6 (6, 0, 0)\n", "\n" ] } ], "source": [ "# First derivative of sin(x) with respect to x (variable 1)\n", "derivative_sin_x = sin_x.derivative(1)\n", "print(f\"Derivative of sin(x) w.r.t. x:\\n{derivative_sin_x}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Integration\n", "\n", "Both indefinite and definite integrals can be computed." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:16.569137Z", "iopub.status.busy": "2025-09-26T23:21:16.568993Z", "iopub.status.idle": "2025-09-26T23:21:16.575766Z", "shell.execute_reply": "2025-09-26T23:21:16.575254Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Indefinite integral of sin(x) w.r.t. x:\n", " Coefficient Order Exponents\n", "0 5.000000000000e-01 2 (2, 0, 0)\n", "1 -4.166666666667e-02 4 (4, 0, 0)\n", "2 1.388888888889e-03 6 (6, 0, 0)\n", "3 -2.480158730159e-05 8 (8, 0, 0)\n", "\n", "\n", "Definite integral of sin(x) from 0 to pi/2:\n", " Coefficient Order Exponents\n", "0 9.999752627236e-01+0.000000000000e+00j 0 (0, 0, 0)\n", "\n" ] } ], "source": [ "# Indefinite integral of sin(x) w.r.t. x\n", "indef_integral_sin_x = sin_x.integrate(1)\n", "print(f\"Indefinite integral of sin(x) w.r.t. x:\\n{indef_integral_sin_x}\\n\")\n", "\n", "# Definite integral of sin(x) w.r.t. x from 0 to pi/2\n", "def_integral_sin_x = sin_x.integrate(1, lower_limit=0, upper_limit=np.pi / 2)\n", "print(f\"Definite integral of sin(x) from 0 to pi/2:\\n{def_integral_sin_x}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Working with Multivariate Functions\n", "\n", "The same operations can be applied to functions of multiple variables." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:16.577106Z", "iopub.status.busy": "2025-09-26T23:21:16.576961Z", "iopub.status.idle": "2025-09-26T23:21:16.594206Z", "shell.execute_reply": "2025-09-26T23:21:16.593694Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "f(x,y,z) = exp(x + y^2 + z^3):\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 0 (0, 0, 0)\n", "1 1.000000000000e+00 1 (1, 0, 0)\n", "2 5.000000000000e-01 2 (2, 0, 0)\n", "3 1.000000000000e+00 2 (0, 2, 0)\n", "4 1.666666666667e-01 3 (3, 0, 0)\n", "5 1.000000000000e+00 3 (1, 2, 0)\n", "6 1.000000000000e+00 3 (0, 0, 3)\n", "7 4.166666666667e-02 4 (4, 0, 0)\n", "8 5.000000000000e-01 4 (2, 2, 0)\n", "9 1.000000000000e+00 4 (1, 0, 3)\n", "10 5.000000000000e-01 4 (0, 4, 0)\n", "11 8.333333333333e-03 5 (5, 0, 0)\n", "12 1.666666666667e-01 5 (3, 2, 0)\n", "13 5.000000000000e-01 5 (2, 0, 3)\n", "14 5.000000000000e-01 5 (1, 4, 0)\n", "15 1.000000000000e+00 5 (0, 2, 3)\n", "16 1.388888888889e-03 6 (6, 0, 0)\n", "17 4.166666666667e-02 6 (4, 2, 0)\n", "18 1.666666666667e-01 6 (3, 0, 3)\n", "19 2.500000000000e-01 6 (2, 4, 0)\n", "20 1.000000000000e+00 6 (1, 2, 3)\n", "21 1.666666666667e-01 6 (0, 6, 0)\n", "22 5.000000000000e-01 6 (0, 0, 6)\n", "23 1.984126984127e-04 7 (7, 0, 0)\n", "24 8.333333333333e-03 7 (5, 2, 0)\n", "25 4.166666666667e-02 7 (4, 0, 3)\n", "26 8.333333333333e-02 7 (3, 4, 0)\n", "27 5.000000000000e-01 7 (2, 2, 3)\n", "28 1.666666666667e-01 7 (1, 6, 0)\n", "29 5.000000000000e-01 7 (1, 0, 6)\n", "30 5.000000000000e-01 7 (0, 4, 3)\n", "31 2.480158730159e-05 8 (8, 0, 0)\n", "32 1.388888888889e-03 8 (6, 2, 0)\n", "33 8.333333333333e-03 8 (5, 0, 3)\n", "34 2.083333333333e-02 8 (4, 4, 0)\n", "35 1.666666666667e-01 8 (3, 2, 3)\n", "36 8.333333333333e-02 8 (2, 6, 0)\n", "37 2.500000000000e-01 8 (2, 0, 6)\n", "38 5.000000000000e-01 8 (1, 4, 3)\n", "39 4.166666666667e-02 8 (0, 8, 0)\n", "40 5.000000000000e-01 8 (0, 2, 6)\n", "\n", "\n", "Derivative w.r.t. y:\n", " Coefficient Order Exponents\n", "0 2.000000000000e+00 1 (0, 1, 0)\n", "1 2.000000000000e+00 2 (1, 1, 0)\n", "2 1.000000000000e+00 3 (2, 1, 0)\n", "3 2.000000000000e+00 3 (0, 3, 0)\n", "4 3.333333333333e-01 4 (3, 1, 0)\n", "5 2.000000000000e+00 4 (1, 3, 0)\n", "6 2.000000000000e+00 4 (0, 1, 3)\n", "7 8.333333333333e-02 5 (4, 1, 0)\n", "8 1.000000000000e+00 5 (2, 3, 0)\n", "9 2.000000000000e+00 5 (1, 1, 3)\n", "10 1.000000000000e+00 5 (0, 5, 0)\n", "11 1.666666666667e-02 6 (5, 1, 0)\n", "12 3.333333333333e-01 6 (3, 3, 0)\n", "13 1.000000000000e+00 6 (2, 1, 3)\n", "14 1.000000000000e+00 6 (1, 5, 0)\n", "15 2.000000000000e+00 6 (0, 3, 3)\n", "16 2.777777777778e-03 7 (6, 1, 0)\n", "17 8.333333333333e-02 7 (4, 3, 0)\n", "18 3.333333333333e-01 7 (3, 1, 3)\n", "19 5.000000000000e-01 7 (2, 5, 0)\n", "20 2.000000000000e+00 7 (1, 3, 3)\n", "21 3.333333333333e-01 7 (0, 7, 0)\n", "22 1.000000000000e+00 7 (0, 1, 6)\n", "\n" ] } ], "source": [ "# A function of three variables\n", "exp_xyz = mtf.exp(x + y**2 + z**3)\n", "print(f\"f(x,y,z) = exp(x + y^2 + z^3):\\n{exp_xyz}\\n\")\n", "\n", "# Differentiate with respect to y (variable 2)\n", "derivative_exp_xyz_y = exp_xyz.derivative(2)\n", "print(f\"Derivative w.r.t. y:\\n{derivative_exp_xyz_y}\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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 }