sandalwood Tutorial: Basic Functionality

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

1. Initialization of Global Settings

Before using sandalwood, 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 sandalwood 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.")

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.000000000000e+00j      0  (0, 0, 0)
1  1.000000000000e+00+0.000000000000e+00j      1  (1, 0, 0)
2 -5.000000000000e-01+0.000000000000e+00j      2  (2, 0, 0)
3 -1.666666666667e-01+0.000000000000e+00j      3  (3, 0, 0)
4  4.166666666667e-02+0.000000000000e+00j      4  (4, 0, 0)
5  8.333333333333e-03+0.000000000000e+00j      5  (5, 0, 0)
6 -1.388888888889e-03+0.000000000000e+00j      6  (6, 0, 0)
7 -1.984126984127e-04+0.000000000000e+00j      7  (7, 0, 0)
8  2.480158730159e-05+0.000000000000e+00j      8  (8, 0, 0)


x * sin(x) =
                              Coefficient  Order  Exponents
0  1.000000000000e+00+0.000000000000e+00j      2  (2, 0, 0)
1 -1.666666666667e-01+0.000000000000e+00j      4  (4, 0, 0)
2  8.333333333333e-03+0.000000000000e+00j      6  (6, 0, 0)
3 -1.984126984127e-04+0.000000000000e+00j      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.000000000000e+00j      0  (0, 0, 0)
1 -5.000000000000e-01+0.000000000000e+00j      2  (2, 0, 0)
2  4.166666666667e-02+0.000000000000e+00j      4  (4, 0, 0)
3 -1.388888888889e-03+0.000000000000e+00j      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+0.000000000000e+00j      2  (2, 0, 0)
1 -4.166666666667e-02+0.000000000000e+00j      4  (4, 0, 0)
2  1.388888888889e-03+0.000000000000e+00j      6  (6, 0, 0)
3 -2.480158730159e-05+0.000000000000e+00j      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.000000000000e+00j      0  (0, 0, 0)
1  1.000000000000e+00+0.000000000000e+00j      1  (1, 0, 0)
2  5.000000000000e-01+0.000000000000e+00j      2  (2, 0, 0)
3  1.000000000000e+00+0.000000000000e+00j      2  (0, 2, 0)
4  1.666666666667e-01+0.000000000000e+00j      3  (3, 0, 0)
5  1.000000000000e+00+0.000000000000e+00j      3  (1, 2, 0)
6  1.000000000000e+00+0.000000000000e+00j      3  (0, 0, 3)
7  4.166666666667e-02+0.000000000000e+00j      4  (4, 0, 0)
8  5.000000000000e-01+0.000000000000e+00j      4  (2, 2, 0)
9  1.000000000000e+00+0.000000000000e+00j      4  (1, 0, 3)
10 5.000000000000e-01+0.000000000000e+00j      4  (0, 4, 0)
11 8.333333333333e-03+0.000000000000e+00j      5  (5, 0, 0)
12 1.666666666667e-01+0.000000000000e+00j      5  (3, 2, 0)
13 5.000000000000e-01+0.000000000000e+00j      5  (2, 0, 3)
14 5.000000000000e-01+0.000000000000e+00j      5  (1, 4, 0)
15 1.000000000000e+00+0.000000000000e+00j      5  (0, 2, 3)
16 1.388888888889e-03+0.000000000000e+00j      6  (6, 0, 0)
17 4.166666666667e-02+0.000000000000e+00j      6  (4, 2, 0)
18 1.666666666667e-01+0.000000000000e+00j      6  (3, 0, 3)
19 2.500000000000e-01+0.000000000000e+00j      6  (2, 4, 0)
20 1.000000000000e+00+0.000000000000e+00j      6  (1, 2, 3)
21 1.666666666667e-01+0.000000000000e+00j      6  (0, 6, 0)
22 5.000000000000e-01+0.000000000000e+00j      6  (0, 0, 6)
23 1.984126984127e-04+0.000000000000e+00j      7  (7, 0, 0)
24 8.333333333333e-03+0.000000000000e+00j      7  (5, 2, 0)
25 4.166666666667e-02+0.000000000000e+00j      7  (4, 0, 3)
26 8.333333333333e-02+0.000000000000e+00j      7  (3, 4, 0)
27 5.000000000000e-01+0.000000000000e+00j      7  (2, 2, 3)
28 1.666666666667e-01+0.000000000000e+00j      7  (1, 6, 0)
29 5.000000000000e-01+0.000000000000e+00j      7  (1, 0, 6)
30 5.000000000000e-01+0.000000000000e+00j      7  (0, 4, 3)
31 2.480158730159e-05+0.000000000000e+00j      8  (8, 0, 0)
32 1.388888888889e-03+0.000000000000e+00j      8  (6, 2, 0)
33 8.333333333333e-03+0.000000000000e+00j      8  (5, 0, 3)
34 2.083333333333e-02+0.000000000000e+00j      8  (4, 4, 0)
35 1.666666666667e-01+0.000000000000e+00j      8  (3, 2, 3)
36 8.333333333333e-02+0.000000000000e+00j      8  (2, 6, 0)
37 2.500000000000e-01+0.000000000000e+00j      8  (2, 0, 6)
38 5.000000000000e-01+0.000000000000e+00j      8  (1, 4, 3)
39 4.166666666667e-02+0.000000000000e+00j      8  (0, 8, 0)
40 5.000000000000e-01+0.000000000000e+00j      8  (0, 2, 6)


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