Function composition#

This example shows the composition of multidimensional FDataGrids.

# Author: Pablo Marcos Manchón
# License: MIT

# sphinx_gallery_thumbnail_number = 3

import numpy as np
from mpl_toolkits.mplot3d import axes3d

import skfda

Function composition can be applied to our data once is in functional form using the method compose().

Let \(f: X \rightarrow Y\) and \(g: Y \rightarrow Z\), the composition will produce a third function \(g \circ f: X \rightarrow Z\) which maps \(x \in X\) to \(g(f(x))\) [1].

In Landmark registration it is shown the simplest case, where it is used to apply a transformation of the time scale of unidimensional data to register its features.

The following example shows the basic usage applied to a surface and a curve, although the method will work for data with arbitrary dimensions to.

Firstly we will create a data object containing a surface \(g: \mathbb{R}^2 \rightarrow \mathbb{R}\).

Constructs example surface

X, Y, Z = axes3d.get_test_data(1.2)
data_matrix = [Z.T]
grid_points = [X[0, :], Y[:, 0]]

g = skfda.FDataGrid(data_matrix, grid_points)

# Plots the surface
g.plot()
plot composition
<Figure size 640x480 with 1 Axes>

We will create a parametric curve \(f(t)=(10 \, \cos(t), 10 \, sin(t))\). The result of the composition, \(g \circ f:\mathbb{R} \rightarrow \mathbb{R}\) will be another functional object with the values of \(g\) along the path given by \(f\).

# Creation of circunference in parametric form
t = np.linspace(0, 2 * np.pi, 100)

data_matrix = [10 * np.array([np.cos(t), np.sin(t)]).T]
f = skfda.FDataGrid(data_matrix, t)

# Composition of function
gof = g.compose(f)

gof.plot()
plot composition
<Figure size 640x480 with 1 Axes>

In the following chart it is plotted the curve \((10 \, \cos(t), 10 \, sin(t), g \circ f (t))\) and the surface.

# Plots surface
fig = g.plot(alpha=0.8)

# Plots path along the surface
path = f(t)[0]
fig.axes[0].plot(path[:, 0], path[:, 1], gof(t)[0, ..., 0], color="orange")

fig
plot composition
<Figure size 640x480 with 1 Axes>

[1] Function composition https://en.wikipedia.org/wiki/Function_composition.

Total running time of the script: (0 minutes 0.468 seconds)

Gallery generated by Sphinx-Gallery