{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# `mtflib` Tutorial: Advanced Functionality\n", "\n", "This notebook explores advanced topics in `mtflib`, including substitution, composition, accessing coefficients, and the use of other elementary functions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Initialization\n", "\n", "As always, we begin by initializing the global settings for `mtflib`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:20:59.935488Z", "iopub.status.busy": "2025-09-26T23:20:59.935371Z", "iopub.status.idle": "2025-09-26T23:21:01.283036Z", "shell.execute_reply": "2025-09-26T23:21:01.282506Z" } }, "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", "import sympy as sp\n", "from IPython.display import display\n", "\n", "from mtflib import ComplexMultivariateTaylorFunction, 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.\")\n", "\n", "# Define some variables for our examples\n", "x = mtf.var(1)\n", "y = mtf.var(2)\n", "z = mtf.var(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Substitution and Composition\n", "\n", "A powerful feature of `mtflib` is the ability to substitute variables with constants or even other Taylor functions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Substitution with a Constant\n", "\n", "Replace a variable in a Taylor function with a constant value." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:01.305683Z", "iopub.status.busy": "2025-09-26T23:21:01.305420Z", "iopub.status.idle": "2025-09-26T23:21:01.313220Z", "shell.execute_reply": "2025-09-26T23:21:01.312769Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original function f(x, y, z):\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 1 (1, 0, 0)\n", "1 1.000000000000e+00 1 (0, 1, 0)\n", "2 1.000000000000e+00 4 (0, 0, 4)\n", "\n", "\n", "After substituting x=0.1, we get g(y, z):\n", " Coefficient Order Exponents\n", "0 1.000000000000e-01+0.000000000000e+00j 0 (0, 0, 0)\n", "1 1.000000000000e+00+0.000000000000e+00j 1 (0, 1, 0)\n", "2 1.000000000000e+00+0.000000000000e+00j 4 (0, 0, 4)\n", "\n" ] } ], "source": [ "f_xyz = x + y + z**4\n", "print(f\"Original function f(x, y, z):\\n{f_xyz}\\n\")\n", "\n", "# Substitute x with 0.1\n", "g_yz = f_xyz.substitute_variable(1, 0.1)\n", "print(f\"After substituting x=0.1, we get g(y, z):\\n{g_yz}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Composition with other Taylor Functions\n", "\n", "Substitute variables with other Taylor functions to compose them." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:01.314488Z", "iopub.status.busy": "2025-09-26T23:21:01.314358Z", "iopub.status.idle": "2025-09-26T23:21:01.321540Z", "shell.execute_reply": "2025-09-26T23:21:01.321094Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Composed function f(g(y), h(y,z)):\n", " Coefficient Order Exponents\n", "0 2.000000000000e+00 0 (0, 0, 0)\n", "1 2.000000000000e+00 1 (0, 1, 0)\n", "2 1.000000000000e+00 2 (0, 2, 0)\n", "3 3.000000000000e+00 2 (0, 1, 1)\n", "4 3.000000000000e+00 4 (0, 2, 2)\n", "5 1.000000000000e+00 6 (0, 3, 3)\n", "\n" ] } ], "source": [ "# Outer function: f(x, y) = x^2 + y^3\n", "f_xy = x**2 + y**3\n", "\n", "# Substituting functions:\n", "# g(y) = 1 + y\n", "# h(y, z) = 1 + y*z\n", "g_y = 1 + y\n", "h_yz = 1 + y * z\n", "\n", "# Perform composition: f(g(y), h(y,z))\n", "composed_f = f_xy.compose({1: g_y, 2: h_yz})\n", "print(f\"Composed function f(g(y), h(y,z)):\\n{composed_f}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Accessing Coefficients\n", "\n", "You can directly access the coefficients and exponents of a Taylor function." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:01.322960Z", "iopub.status.busy": "2025-09-26T23:21:01.322839Z", "iopub.status.idle": "2025-09-26T23:21:01.327747Z", "shell.execute_reply": "2025-09-26T23:21:01.327319Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Coefficients of sin(x):\n", " Exponent (np.int32(1), np.int32(0), np.int32(0)): 1.0\n", " Exponent (np.int32(3), np.int32(0), np.int32(0)): -0.16666666666666666\n", " Exponent (np.int32(5), np.int32(0), np.int32(0)): 0.008333333333333333\n", " Exponent (np.int32(7), np.int32(0), np.int32(0)): -0.0001984126984126984\n" ] } ], "source": [ "sin_x = mtf.sin(x)\n", "coefficients = {tuple(exp): coeff for exp, coeff in zip(sin_x.exponents, sin_x.coeffs)}\n", "\n", "print(\"Coefficients of sin(x):\")\n", "for exp, coeff in coefficients.items():\n", " print(f\" Exponent {exp}: {coeff}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Other Elementary Functions\n", "\n", "`mtflib` supports a variety of other elementary functions." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:01.329083Z", "iopub.status.busy": "2025-09-26T23:21:01.328966Z", "iopub.status.idle": "2025-09-26T23:21:01.384047Z", "shell.execute_reply": "2025-09-26T23:21:01.383602Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- Example: mtf.exp(x+2*y) ---\n", "The following is a SymPy object, which will render beautifully in a notebook.\n" ] }, { "data": { "text/latex": [ "$\\displaystyle - 0.000198413 x^{7} - 0.00277778 x^{6} y - 0.0166667 x^{5} y^{2} + 0.00833333 x^{5} - 0.0555556 x^{4} y^{3} + 0.0833333 x^{4} y - 0.111111 x^{3} y^{4} + 0.333333 x^{3} y^{2} - 0.166667 x^{3} - 0.133333 x^{2} y^{5} + 0.666667 x^{2} y^{3} - 1.0 x^{2} y - 0.0888889 x y^{6} + 0.666667 x y^{4} - 2.0 x y^{2} + 1.0 x - 0.0253968 y^{7} + 0.266667 y^{5} - 1.33333 y^{3} + 2.0 y$" ], "text/plain": [ "-0.000198413*x**7 - 0.00277778*x**6*y - 0.0166667*x**5*y**2 + 0.00833333*x**5 - 0.0555556*x**4*y**3 + 0.0833333*x**4*y - 0.111111*x**3*y**4 + 0.333333*x**3*y**2 - 0.166667*x**3 - 0.133333*x**2*y**5 + 0.666667*x**2*y**3 - 1.0*x**2*y - 0.0888889*x*y**6 + 0.666667*x*y**4 - 2.0*x*y**2 + 1.0*x - 0.0253968*y**7 + 0.266667*y**5 - 1.33333*y**3 + 2.0*y" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "--- Example: mtf.exp(i*x) ---\n", "The following is a SymPy object for a complex function.\n" ] }, { "data": { "text/latex": [ "$\\displaystyle 2.48016 \\cdot 10^{-5} x^{8} - 0.000198413 i x^{7} - 0.00138889 x^{6} + 0.00833333 i x^{5} + 0.0416667 x^{4} - 0.166667 i x^{3} - 0.5 x^{2} + 1.0 i x + 1.0$" ], "text/plain": [ "2.48016e-5*x**8 - 0.000198413*I*x**7 - 0.00138889*x**6 + 0.00833333*I*x**5 + 0.0416667*x**4 - 0.166667*I*x**3 - 0.5*x**2 + 1.0*I*x + 1.0" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "--- Example: Custom coefficient formatting ---\n", "The following is a SymPy object with custom rational formatting.\n" ] }, { "data": { "text/latex": [ "$\\displaystyle - \\frac{x^{6} y}{360} - \\frac{x^{5} y^{2}}{60} + \\frac{x^{5}}{120} - \\frac{x^{4} y^{3}}{18} + \\frac{x^{4} y}{12} - \\frac{x^{3} y^{4}}{9} + \\frac{x^{3} y^{2}}{3} - \\frac{x^{3}}{6} - \\frac{2 x^{2} y^{5}}{15} + \\frac{2 x^{2} y^{3}}{3} - x^{2} y - \\frac{4 x y^{6}}{45} + \\frac{2 x y^{4}}{3} - 2 x y^{2} + x - \\frac{8 y^{7}}{315} + \\frac{4 y^{5}}{15} - \\frac{4 y^{3}}{3} + 2 y$" ], "text/plain": [ "-x**6*y/360 - x**5*y**2/60 + x**5/120 - x**4*y**3/18 + x**4*y/12 - x**3*y**4/9 + x**3*y**2/3 - x**3/6 - 2*x**2*y**5/15 + 2*x**2*y**3/3 - x**2*y - 4*x*y**6/45 + 2*x*y**4/3 - 2*x*y**2 + x - 8*y**7/315 + 4*y**5/15 - 4*y**3/3 + 2*y" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "print(\"--- Example: mtf.exp(x+2*y) ---\")\n", "x = mtf.var(1)\n", "y = mtf.var(2)\n", "mtf_sin = mtf.sin(x + 2 * y)\n", "sympy_sin_expr = mtf_sin.symprint()\n", "print(\"The following is a SymPy object, which will render beautifully in a notebook.\")\n", "display(sympy_sin_expr)\n", "\n", "# --- Example with ComplexMultivariateTaylorFunction ---\n", "print(\"--- Example: mtf.exp(i*x) ---\")\n", "i = 1j\n", "\n", "x_complex = ComplexMultivariateTaylorFunction.from_variable(var_index=1, dimension=1)\n", "mtf_complex_exp = mtf.exp(i * x_complex)\n", "sympy_complex_expr = mtf_complex_exp.symprint(symbols=[\"x\"])\n", "print(\"The following is a SymPy object for a complex function.\")\n", "display(sympy_complex_expr)\n", "\n", "# --- Example with custom coefficient formatting ---\n", "print(\"--- Example: Custom coefficient formatting ---\")\n", "\n", "\n", "def custom_formatter(c, p):\n", " # A simple rational formatter\n", " if np.iscomplexobj(c):\n", " return sp.Rational(c.real).limit_denominator(10**p) + sp.I * sp.Rational(\n", " c.imag\n", " ).limit_denominator(10**p)\n", " else:\n", " return sp.Rational(c).limit_denominator(10**p)\n", "\n", "\n", "# Re-initialize for the 2D mtf_sin\n", "x = mtf.var(1)\n", "y = mtf.var(2)\n", "mtf_sin = mtf.sin(x + 2 * y)\n", "\n", "sympy_custom_format_expr = mtf_sin.symprint(\n", " precision=3, coeff_formatter=custom_formatter\n", ")\n", "print(\"The following is a SymPy object with custom rational formatting.\")\n", "display(sympy_custom_format_expr)" ] } ], "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 }