Reconstructing polynomials from scipy.interpolate.RectBivariateSpline

Viewed 15

I have fitted a 2-D cubic spline using scipy.interpolate.RectBivariateSpline. I would like to access/reconstruct the underlying polynomials within each rectangular cell. How can I do this? My code so far is written below.

I have been able to get the knot points and the coefficients with get_knots() and get_coeffs() so it should be possible to build the polynomials, but I do not know the form of the polynomials that the coefficients correspond to. I tried looking at the SciPy source code but I could not locate the underlying dfitpack.regrid_smth function.

A code demonstrating the fitting:

import numpy as np                                                              
from scipy.interpolate import RectBivariateSpline

# Evaluate a demonstration function Z(x, y) = sin(sin(x * y)) on a mesh     
# of points.                                                                
x0 = -1.0                                                                   
x1 =  1.0                                                                   
n_x = 11                                                                    
x = np.linspace(x0, x1, num = n_x)                                          
                                                                            
y0 = -2.0                                                                   
y1 =  2.0                                                                   
n_y = 21                                                                    
y = np.linspace(y0, y1, num = n_y)                                          
                                                                            
X, Y = np.meshgrid(x, y, indexing = 'ij')                                   
                                                                            
Z = np.sin(np.sin(X * Y))                                                   
                                                                            
# Fit the sampled function using SciPy's RectBivariateSpline.               
order_spline = 3                                                            
smoothing = 0.0                                                             
spline_fit_func = RectBivariateSpline(x, y, Z,                              
        kx = order_spline, ky = order_spline, s = smoothing)

And to plot it:

import matplotlib.pyplot as plt

# Make axes.                                                                
fig, ax_arr = plt.subplots(1, 2, sharex = True, sharey = True, figsize = (12.0, 8.0))
                                                                            
# Plot the input function.                                                  
ax = ax_arr[0]                                                              
ax.set_aspect(1.0)                                                          
                                                                            
d_x = x[1] - x[0]                                                           
x_edges = np.zeros(n_x + 1)                                                 
x_edges[:-1] = x - (d_x / 2.0)                                              
x_edges[-1] = x[-1] + (d_x / 2.0)                                           
                                                                            
d_y = y[1] - y[0]                                                           
y_edges = np.zeros(n_y + 1)                                                 
y_edges[:-1] = y - (d_y / 2.0)                                              
y_edges[-1] = y[-1] + (d_y / 2.0)                                           
                                                                            
ax.pcolormesh(x_edges, y_edges, Z.T)                                        
                                                                            
ax.set_title('Input function')                                              
                                                                            
# Plot the fitted function.                                                 
ax = ax_arr[1]                                                              
ax.set_aspect(1.0)                                                          
                                                                            
n_x_span = n_x * 10                                                         
x_span_edges = np.linspace(x0, x1, num = n_x_span)                          
x_span_centres = (x_span_edges[1:] + x_span_edges[:-1]) / 2.0               
#                                                                           
n_y_span = n_y * 10                                                         
y_span_edges = np.linspace(y0, y1, num = n_y_span)                          
y_span_centres = (y_span_edges[1:] + y_span_edges[:-1]) / 2.0               
                                                                            
Z_fit = spline_fit_func(x_span_centres, y_span_centres)                     
ax.pcolormesh(x_span_edges, y_span_edges, Z_fit.T)                          
                                                                            
x_knot, y_knot = spline_fit_func.get_knots()                                
X_knot, Y_knot = np.meshgrid(x_knot, y_knot)                                
                                                                            
# Plot the knots.                                                           
ax.scatter(X_knot, Y_knot, s = 1, c = 'r')                                  
                                                                            
ax.set_title('Fitted function and knots')                                   
                                                                            
plt.show()
0 Answers
Related