This notebook demonstrates how to interpolate a function $R^d \rightarrow R^x$ using multilinear interpolation.

In [1]:
# define the state-space
d = 2
smin = array( [-1.0]*d )
smax = array( [1.0]*d )
orders = array( [5]*d)
In [2]:
# define the function to be approximated (x=2)
fun = lambda x: row_stack([
    pow( sum( pow(abs(x),2), axis=0), 0.5 ),
    pow( sum( pow(abs(x),3), axis=0), 1.0/3.0 )
])
In [3]:
from  multilinear import MultilinearInterpolator
interp_fun = MultilinearInterpolator(smin,smax,orders)
Using compiled linear interpolator

In [4]:
# evaluate the function to be interpolated on grid nodes
values_on_grid = fun( interp_fun.grid )
interp_fun.set_values( values_on_grid )
In [5]:
# create a dense grid
def mlinspace(smin,smax,orders):
    '''Creates a regularly spaced grid'''
    return row_stack( [l.flatten() for l in meshgrid( *[linspace(smin[i], smax[i], orders[i]) for i in range(len(orders)) ], indexing='ij' )] )

# evaluate interpolant
high_orders = [100]*d
finer_grid = mlinspace(smin,smax,high_orders)

true_values = fun( finer_grid )
approximated_values = interp_fun( finer_grid )
In [6]:
figsize(15,15)
subplot(2,2,1)
imshow( true_values[0,:].reshape(high_orders), extent=[-1,1,-1,1] )
title('$\sqrt{x^2+y^2}$ : exact')
subplot(2,2,2)
imshow( approximated_values[0,:].reshape(high_orders), extent=[-1,1,-1,1] )
title('$\sqrt{x^2+y^2}$ : interpolated')
plot(interp_fun.grid[0,:], interp_fun.grid[1,:],'o', color='black')
subplot(2,2,3)
imshow( true_values[1,:].reshape(high_orders), extent=[-1,1,-1,1] )
title('$\sqrt[3]{x^3+y^3}$ : exact')
subplot(2,2,4)
imshow(  approximated_values[1,:].reshape(high_orders), extent=[-1,1,-1,1] )
plot(interp_fun.grid[0,:], interp_fun.grid[1,:],'o', color='black')
title('$\sqrt[3]{x^3+y^3}$ : interpolated')
Out[6]:
<matplotlib.text.Text at 0x2f0a750>