Quick Start Guide to sandalwood
Welcome to sandalwood! 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 sandalwood 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 sandalwood 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)
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 sandalwood’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+0j)
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+0.000000000000e+00j 1 (1, 0)
1 1.000000000000e+00+0.000000000000e+00j 2 (0, 2)
2 -1.666666666667e-01+0.000000000000e+00j 3 (3, 0)
3 8.333333333333e-03+0.000000000000e+00j 5 (5, 0)
Symbolic representation of the function: