Quick Start Guide to mtflib
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.
1. Initialization
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.
Note: If you need to change these values later, you’ll have to restart your Python session (or Jupyter kernel).
[1]:
from mtflib import mtf
# Initialize global settings for Taylor series
# max_order: highest order of the series
# max_dimension: number of variables (e.g., 2 for x, y)
mtf.initialize_mtf(max_order=5, max_dimension=2)
Initializing MTF globals with: _MAX_ORDER=5, _MAX_DIMENSION=2
Loading/Precomputing Taylor coefficients up to order 5
Precomputing sin Taylor coefficients up to order 5 and saving to /home/docs/checkouts/readthedocs.org/user_builds/mtflibrary/envs/latest/lib/python3.13/site-packages/mtflib/precomputed_coefficients_data/sin_coefficients.json
Computed and saved sin coefficients up to order 5.
Precomputing cos Taylor coefficients up to order 5 and saving to /home/docs/checkouts/readthedocs.org/user_builds/mtflibrary/envs/latest/lib/python3.13/site-packages/mtflib/precomputed_coefficients_data/cos_coefficients.json
Computed and saved cos coefficients up to order 5.
Precomputing exp Taylor coefficients up to order 5 and saving to /home/docs/checkouts/readthedocs.org/user_builds/mtflibrary/envs/latest/lib/python3.13/site-packages/mtflib/precomputed_coefficients_data/exp_coefficients.json
Computed and saved exp coefficients up to order 5.
Precomputing gaussian Taylor coefficients up to order 5 and saving to /home/docs/checkouts/readthedocs.org/user_builds/mtflibrary/envs/latest/lib/python3.13/site-packages/mtflib/precomputed_coefficients_data/gaussian_coefficients.json
Computed and saved gaussian coefficients up to order 5.
Precomputing sqrt Taylor coefficients up to order 5 and saving to /home/docs/checkouts/readthedocs.org/user_builds/mtflibrary/envs/latest/lib/python3.13/site-packages/mtflib/precomputed_coefficients_data/sqrt_coefficients.json
Computed and saved sqrt coefficients up to order 5.
Precomputing isqrt Taylor coefficients up to order 5 and saving to /home/docs/checkouts/readthedocs.org/user_builds/mtflibrary/envs/latest/lib/python3.13/site-packages/mtflib/precomputed_coefficients_data/isqrt_coefficients.json
Computed and saved isqrt coefficients up to order 5.
Precomputing log Taylor coefficients up to order 5 and saving to /home/docs/checkouts/readthedocs.org/user_builds/mtflibrary/envs/latest/lib/python3.13/site-packages/mtflib/precomputed_coefficients_data/log_coefficients.json
Computed and saved log coefficients up to order 5.
Precomputing arctan Taylor coefficients up to order 5 and saving to /home/docs/checkouts/readthedocs.org/user_builds/mtflibrary/envs/latest/lib/python3.13/site-packages/mtflib/precomputed_coefficients_data/arctan_coefficients.json
Computed and saved arctan coefficients up to order 5.
Precomputing sinh Taylor coefficients up to order 5 and saving to /home/docs/checkouts/readthedocs.org/user_builds/mtflibrary/envs/latest/lib/python3.13/site-packages/mtflib/precomputed_coefficients_data/sinh_coefficients.json
Computed and saved sinh coefficients up to order 5.
Precomputing cosh Taylor coefficients up to order 5 and saving to /home/docs/checkouts/readthedocs.org/user_builds/mtflibrary/envs/latest/lib/python3.13/site-packages/mtflib/precomputed_coefficients_data/cosh_coefficients.json
Computed and saved cosh coefficients up to order 5.
Precomputing arctanh Taylor coefficients up to order 5 and saving to /home/docs/checkouts/readthedocs.org/user_builds/mtflibrary/envs/latest/lib/python3.13/site-packages/mtflib/precomputed_coefficients_data/arctanh_coefficients.json
Computed and saved arctanh coefficients up to order 5.
Precomputing arcsin Taylor coefficients up to order 5 and saving to /home/docs/checkouts/readthedocs.org/user_builds/mtflibrary/envs/latest/lib/python3.13/site-packages/mtflib/precomputed_coefficients_data/arcsin_coefficients.json
Computed and saved arcsin coefficients up to order 5.
Precomputing arccos Taylor coefficients up to order 5 and saving to /home/docs/checkouts/readthedocs.org/user_builds/mtflibrary/envs/latest/lib/python3.13/site-packages/mtflib/precomputed_coefficients_data/arccos_coefficients.json
Computed and saved arccos coefficients up to order 5.
Precomputing inverse Taylor coefficients up to order 5 and saving to /home/docs/checkouts/readthedocs.org/user_builds/mtflibrary/envs/latest/lib/python3.13/site-packages/mtflib/precomputed_coefficients_data/inverse_coefficients.json
Computed and saved inverse coefficients up to order 5.
Global precomputed coefficients loading/generation complete.
Size of precomputed_coefficients dictionary in memory: 464 bytes, 0.45 KB, 0.00 MB
MTF globals initialized: _MAX_ORDER=5, _MAX_DIMENSION=2, _INITIALIZED=True
Max coefficient count (order=5, nvars=2): 21
Precomputed coefficients loaded and ready for use.
/tmp/ipykernel_746/646042316.py:1: DeprecationWarning: mtflib is deprecated and has been superseded by sandalwood. Please migrate to the sandalwood package. For details, visit: https://github.com/shashi-manikonda/sandalwood
from mtflib import mtf
2. Defining Symbolic Variables
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.
[2]:
# var(1) corresponds to 'x', var(2) to 'y'
x = mtf.var(1)
y = mtf.var(2)
3. Creating a Taylor Function
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.
[3]:
# Let's create a Taylor series for the function f(x, y) = sin(x) + y^2
f = mtf.sin(x) + y**2
4. Evaluating the Function
We can evaluate our Taylor function at any point. The eval() method takes a NumPy array representing the point (x, y).
[4]:
import numpy as np
# Evaluate f at the point (x=0.5, y=2.0)
eval_point = np.array([0.5, 2.0])
result = f.eval(eval_point)
print("f(x, y) = sin(x) + y^2")
print(f"Result of f(0.5, 2.0): {result[0]}")
# For comparison, let's calculate the exact value
exact_value = np.sin(0.5) + 4.0
print(f"Exact value: {exact_value}")
f(x, y) = sin(x) + y^2
Result of f(0.5, 2.0): 4.479427083333333
Exact value: 4.479425538604203
5. Viewing the Taylor Series
You can also inspect the Taylor series coefficients directly, or get a nice symbolic representation.
[5]:
from IPython.display import display
# Print the Taylor series coefficients in a table
print("Taylor Series Representation:")
print(f)
# Display a symbolic representation (requires SymPy)
print("Symbolic representation of the function:")
display(f.symprint())
Taylor Series Representation:
Coefficient Order Exponents
0 1.000000000000e+00 1 (1, 0)
1 1.000000000000e+00 2 (0, 2)
2 -1.666666666667e-01 3 (3, 0)
3 8.333333333333e-03 5 (5, 0)
Symbolic representation of the function: