{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Quick Start Guide to `mtflib`\n", "\n", "Welcome to `mtflib`! This notebook provides a quick introduction to the basic functionality of the library. We'll walk through the essential steps to get you started with creating and evaluating multivariate Taylor functions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Initialization\n", "\n", "Before we can do anything, we need to initialize the `mtflib` global settings. This is a one-time setup that defines the maximum order of the Taylor series and the number of variables (dimensions) you'll be working with in your session. \n", "\n", "**Note:** If you need to change these values later, you'll have to restart your Python session (or Jupyter kernel)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:11.632785Z", "iopub.status.busy": "2025-09-26T23:21:11.632630Z", "iopub.status.idle": "2025-09-26T23:21:12.778042Z", "shell.execute_reply": "2025-09-26T23:21:12.777257Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Initializing MTF globals with: _MAX_ORDER=5, _MAX_DIMENSION=2\n", "Loading/Precomputing Taylor coefficients up to order 5\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=5, _MAX_DIMENSION=2, _INITIALIZED=True\n", "Max coefficient count (order=5, nvars=2): 21\n", "Precomputed coefficients loaded and ready for use.\n" ] } ], "source": [ "from mtflib import mtf\n", "\n", "# Initialize global settings for Taylor series\n", "# max_order: highest order of the series\n", "# max_dimension: number of variables (e.g., 2 for x, y)\n", "mtf.initialize_mtf(max_order=5, max_dimension=2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Defining Symbolic Variables\n", "\n", "Next, we create symbolic variables. These are the building blocks for our Taylor functions. We use `mtf.var(id)` where `id` is an integer from 1 to `max_dimension`." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:12.808304Z", "iopub.status.busy": "2025-09-26T23:21:12.808017Z", "iopub.status.idle": "2025-09-26T23:21:12.811093Z", "shell.execute_reply": "2025-09-26T23:21:12.810625Z" } }, "outputs": [], "source": [ "# var(1) corresponds to 'x', var(2) to 'y'\n", "x = mtf.var(1)\n", "y = mtf.var(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Creating a Taylor Function\n", "\n", "Now we can create a Taylor function by combining our symbolic variables with `mtflib`'s elementary functions (like `mtf.sin`, `mtf.cos`, `mtf.exp`, etc.) and standard arithmetic operations." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:12.812284Z", "iopub.status.busy": "2025-09-26T23:21:12.812153Z", "iopub.status.idle": "2025-09-26T23:21:12.816376Z", "shell.execute_reply": "2025-09-26T23:21:12.815930Z" } }, "outputs": [], "source": [ "# Let's create a Taylor series for the function f(x, y) = sin(x) + y^2\n", "f = mtf.sin(x) + y**2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Evaluating the Function\n", "\n", "We can evaluate our Taylor function at any point. The `eval()` method takes a NumPy array representing the point `(x, y)`." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:12.817631Z", "iopub.status.busy": "2025-09-26T23:21:12.817489Z", "iopub.status.idle": "2025-09-26T23:21:12.820331Z", "shell.execute_reply": "2025-09-26T23:21:12.819903Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "f(x, y) = sin(x) + y^2\n", "Result of f(0.5, 2.0): 4.479427083333333\n", "Exact value: 4.479425538604203\n" ] } ], "source": [ "import numpy as np\n", "\n", "# Evaluate f at the point (x=0.5, y=2.0)\n", "eval_point = np.array([0.5, 2.0])\n", "result = f.eval(eval_point)\n", "\n", "print(\"f(x, y) = sin(x) + y^2\")\n", "print(f\"Result of f(0.5, 2.0): {result[0]}\")\n", "\n", "# For comparison, let's calculate the exact value\n", "exact_value = np.sin(0.5) + 4.0\n", "print(f\"Exact value: {exact_value}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5. Viewing the Taylor Series\n", "\n", "You can also inspect the Taylor series coefficients directly, or get a nice symbolic representation." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2025-09-26T23:21:12.821802Z", "iopub.status.busy": "2025-09-26T23:21:12.821678Z", "iopub.status.idle": "2025-09-26T23:21:13.110424Z", "shell.execute_reply": "2025-09-26T23:21:13.109789Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Taylor Series Representation:\n", " Coefficient Order Exponents\n", "0 1.000000000000e+00 1 (1, 0)\n", "1 1.000000000000e+00 2 (0, 2)\n", "2 -1.666666666667e-01 3 (3, 0)\n", "3 8.333333333333e-03 5 (5, 0)\n", "\n", "Symbolic representation of the function:\n" ] }, { "data": { "text/latex": [ "$\\displaystyle 0.00833333 x^{5} - 0.166667 x^{3} + 1.0 x + 1.0 y^{2}$" ], "text/plain": [ "0.00833333*x**5 - 0.166667*x**3 + 1.0*x + 1.0*y**2" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from IPython.display import display\n", "\n", "# Print the Taylor series coefficients in a table\n", "print(\"Taylor Series Representation:\")\n", "print(f)\n", "\n", "# Display a symbolic representation (requires SymPy)\n", "print(\"Symbolic representation of the function:\")\n", "display(f.symprint())" ] } ], "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 }