mtflib Tutorial: Basic Functionality

This notebook covers the fundamental concepts of mtflib, including initialization, variable creation, and basic operations on Taylor functions like arithmetic, differentiation, and integration.

1. Initialization of Global Settings

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.

Important: These settings should be initialized only once per session. If you need to change them, a kernel restart is required.

[1]:
import numpy as np

from mtflib import mtf

if not mtf.get_mtf_initialized_status():
    mtf.initialize_mtf(max_order=8, max_dimension=3)
else:
    print("MTF globals are already initialized.")
Initializing MTF globals with: _MAX_ORDER=8, _MAX_DIMENSION=3
Loading/Precomputing Taylor coefficients up to order 8
Precomputed sin coefficients (order 5) insufficient, recomputing up to order 8.
Recomputed and saved sin coefficients up to order 8.
Precomputed cos coefficients (order 5) insufficient, recomputing up to order 8.
Recomputed and saved cos coefficients up to order 8.
Precomputed exp coefficients (order 5) insufficient, recomputing up to order 8.
Recomputed and saved exp coefficients up to order 8.
Precomputed gaussian coefficients (order 5) insufficient, recomputing up to order 8.
Recomputed and saved gaussian coefficients up to order 8.
Precomputed sqrt coefficients (order 5) insufficient, recomputing up to order 8.
Recomputed and saved sqrt coefficients up to order 8.
Precomputed isqrt coefficients (order 5) insufficient, recomputing up to order 8.
Recomputed and saved isqrt coefficients up to order 8.
Precomputed log coefficients (order 5) insufficient, recomputing up to order 8.
Recomputed and saved log coefficients up to order 8.
Precomputed arctan coefficients (order 5) insufficient, recomputing up to order 8.
Recomputed and saved arctan coefficients up to order 8.
Precomputed sinh coefficients (order 5) insufficient, recomputing up to order 8.
Recomputed and saved sinh coefficients up to order 8.
Precomputed cosh coefficients (order 5) insufficient, recomputing up to order 8.
Recomputed and saved cosh coefficients up to order 8.
Precomputed arctanh coefficients (order 5) insufficient, recomputing up to order 8.
Recomputed and saved arctanh coefficients up to order 8.
Precomputed arcsin coefficients (order 5) insufficient, recomputing up to order 8.
Recomputed and saved arcsin coefficients up to order 8.
Precomputed arccos coefficients (order 5) insufficient, recomputing up to order 8.
Recomputed and saved arccos coefficients up to order 8.
Precomputed inverse coefficients (order 5) insufficient, recomputing up to order 8.
Recomputed and saved inverse coefficients up to order 8.
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=8, _MAX_DIMENSION=3, _INITIALIZED=True
Max coefficient count (order=8, nvars=3): 165
Precomputed coefficients loaded and ready for use.
/tmp/ipykernel_833/2691079699.py:3: 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

We define symbolic variables using mtf.var(var_id), where var_id is an integer from 1 to max_dimension.

[2]:
x = mtf.var(1)
y = mtf.var(2)
z = mtf.var(3)

print("Variable x:")
print(x)
Variable x:
         Coefficient  Order  Exponents
0 1.000000000000e+00      1  (1, 0, 0)

3. Basic Operations with Taylor Functions

Arithmetic Operations

Standard arithmetic operations like addition, subtraction, multiplication, and exponentiation are supported.

[3]:
sin_x = mtf.sin(x)
cos_x = mtf.cos(x)

# Addition
sum_tf = sin_x + cos_x
print(f"sin(x) + cos(x) =\n{sum_tf}\n")

# Multiplication
product_tf = x * sin_x
print(f"x * sin(x) =\n{product_tf}\n")

# Exponentiation
squared_x = x**2
print(f"x^2 =\n{squared_x}\n")
sin(x) + cos(x) =
          Coefficient  Order  Exponents
0  1.000000000000e+00      0  (0, 0, 0)
1  1.000000000000e+00      1  (1, 0, 0)
2 -5.000000000000e-01      2  (2, 0, 0)
3 -1.666666666667e-01      3  (3, 0, 0)
4  4.166666666667e-02      4  (4, 0, 0)
5  8.333333333333e-03      5  (5, 0, 0)
6 -1.388888888889e-03      6  (6, 0, 0)
7 -1.984126984127e-04      7  (7, 0, 0)
8  2.480158730159e-05      8  (8, 0, 0)


x * sin(x) =
          Coefficient  Order  Exponents
0  1.000000000000e+00      2  (2, 0, 0)
1 -1.666666666667e-01      4  (4, 0, 0)
2  8.333333333333e-03      6  (6, 0, 0)
3 -1.984126984127e-04      8  (8, 0, 0)


x^2 =
         Coefficient  Order  Exponents
0 1.000000000000e+00      2  (2, 0, 0)


Differentiation

You can compute the derivative of a Taylor function with respect to any variable.

[4]:
# First derivative of sin(x) with respect to x (variable 1)
derivative_sin_x = sin_x.derivative(1)
print(f"Derivative of sin(x) w.r.t. x:\n{derivative_sin_x}")
Derivative of sin(x) w.r.t. x:
          Coefficient  Order  Exponents
0  1.000000000000e+00      0  (0, 0, 0)
1 -5.000000000000e-01      2  (2, 0, 0)
2  4.166666666667e-02      4  (4, 0, 0)
3 -1.388888888889e-03      6  (6, 0, 0)

Integration

Both indefinite and definite integrals can be computed.

[5]:
# Indefinite integral of sin(x) w.r.t. x
indef_integral_sin_x = sin_x.integrate(1)
print(f"Indefinite integral of sin(x) w.r.t. x:\n{indef_integral_sin_x}\n")

# Definite integral of sin(x) w.r.t. x from 0 to pi/2
def_integral_sin_x = sin_x.integrate(1, lower_limit=0, upper_limit=np.pi / 2)
print(f"Definite integral of sin(x) from 0 to pi/2:\n{def_integral_sin_x}")
Indefinite integral of sin(x) w.r.t. x:
          Coefficient  Order  Exponents
0  5.000000000000e-01      2  (2, 0, 0)
1 -4.166666666667e-02      4  (4, 0, 0)
2  1.388888888889e-03      6  (6, 0, 0)
3 -2.480158730159e-05      8  (8, 0, 0)


Definite integral of sin(x) from 0 to pi/2:
                             Coefficient  Order  Exponents
0 9.999752627236e-01+0.000000000000e+00j      0  (0, 0, 0)

4. Working with Multivariate Functions

The same operations can be applied to functions of multiple variables.

[6]:
# A function of three variables
exp_xyz = mtf.exp(x + y**2 + z**3)
print(f"f(x,y,z) = exp(x + y^2 + z^3):\n{exp_xyz}\n")

# Differentiate with respect to y (variable 2)
derivative_exp_xyz_y = exp_xyz.derivative(2)
print(f"Derivative w.r.t. y:\n{derivative_exp_xyz_y}")
f(x,y,z) = exp(x + y^2 + z^3):
          Coefficient  Order  Exponents
0  1.000000000000e+00      0  (0, 0, 0)
1  1.000000000000e+00      1  (1, 0, 0)
2  5.000000000000e-01      2  (2, 0, 0)
3  1.000000000000e+00      2  (0, 2, 0)
4  1.666666666667e-01      3  (3, 0, 0)
5  1.000000000000e+00      3  (1, 2, 0)
6  1.000000000000e+00      3  (0, 0, 3)
7  4.166666666667e-02      4  (4, 0, 0)
8  5.000000000000e-01      4  (2, 2, 0)
9  1.000000000000e+00      4  (1, 0, 3)
10 5.000000000000e-01      4  (0, 4, 0)
11 8.333333333333e-03      5  (5, 0, 0)
12 1.666666666667e-01      5  (3, 2, 0)
13 5.000000000000e-01      5  (2, 0, 3)
14 5.000000000000e-01      5  (1, 4, 0)
15 1.000000000000e+00      5  (0, 2, 3)
16 1.388888888889e-03      6  (6, 0, 0)
17 4.166666666667e-02      6  (4, 2, 0)
18 1.666666666667e-01      6  (3, 0, 3)
19 2.500000000000e-01      6  (2, 4, 0)
20 1.000000000000e+00      6  (1, 2, 3)
21 1.666666666667e-01      6  (0, 6, 0)
22 5.000000000000e-01      6  (0, 0, 6)
23 1.984126984127e-04      7  (7, 0, 0)
24 8.333333333333e-03      7  (5, 2, 0)
25 4.166666666667e-02      7  (4, 0, 3)
26 8.333333333333e-02      7  (3, 4, 0)
27 5.000000000000e-01      7  (2, 2, 3)
28 1.666666666667e-01      7  (1, 6, 0)
29 5.000000000000e-01      7  (1, 0, 6)
30 5.000000000000e-01      7  (0, 4, 3)
31 2.480158730159e-05      8  (8, 0, 0)
32 1.388888888889e-03      8  (6, 2, 0)
33 8.333333333333e-03      8  (5, 0, 3)
34 2.083333333333e-02      8  (4, 4, 0)
35 1.666666666667e-01      8  (3, 2, 3)
36 8.333333333333e-02      8  (2, 6, 0)
37 2.500000000000e-01      8  (2, 0, 6)
38 5.000000000000e-01      8  (1, 4, 3)
39 4.166666666667e-02      8  (0, 8, 0)
40 5.000000000000e-01      8  (0, 2, 6)


Derivative w.r.t. y:
          Coefficient  Order  Exponents
0  2.000000000000e+00      1  (0, 1, 0)
1  2.000000000000e+00      2  (1, 1, 0)
2  1.000000000000e+00      3  (2, 1, 0)
3  2.000000000000e+00      3  (0, 3, 0)
4  3.333333333333e-01      4  (3, 1, 0)
5  2.000000000000e+00      4  (1, 3, 0)
6  2.000000000000e+00      4  (0, 1, 3)
7  8.333333333333e-02      5  (4, 1, 0)
8  1.000000000000e+00      5  (2, 3, 0)
9  2.000000000000e+00      5  (1, 1, 3)
10 1.000000000000e+00      5  (0, 5, 0)
11 1.666666666667e-02      6  (5, 1, 0)
12 3.333333333333e-01      6  (3, 3, 0)
13 1.000000000000e+00      6  (2, 1, 3)
14 1.000000000000e+00      6  (1, 5, 0)
15 2.000000000000e+00      6  (0, 3, 3)
16 2.777777777778e-03      7  (6, 1, 0)
17 8.333333333333e-02      7  (4, 3, 0)
18 3.333333333333e-01      7  (3, 1, 3)
19 5.000000000000e-01      7  (2, 5, 0)
20 2.000000000000e+00      7  (1, 3, 3)
21 3.333333333333e-01      7  (0, 7, 0)
22 1.000000000000e+00      7  (0, 1, 6)