Splines for even functions?
Does anyone have a suggestion on how to force a smoothing spline to be an even function f(x) = f(-x)? I am trying to interpolate the following data representing an even function and I need a smooth but even spline. The problem is that, in the process of smoothing, the knots are not chosen evenly so the resulting spline is not even. Increasing the tolerances ultimately forces more knots to be added resulting in an even spline, but it is difficult to automate this process. from scipy import linspace, interpolate, array X = array([-1. , -0.65016502, -0.58856235, -0.26903553, -0.17370892, -0.10011001, 0. , 0.10011001, 0.17370892, 0.26903553, 0.58856235, 0.65016502, 1.]) Y = array([ 1. , 0.62928599, 0.5797223 , 0.39965815, 0.36322694, 0.3508061 , 0.35214793, 0.3508061 , 0.36322694, 0.39965815, 0.5797223 , 0.62928599, 1.]) E = array([ 1.00000000e-12, 1.45164012e-03, 2.04367440e-03, 2.34266209e-03, 1.64542215e-03, 2.21561749e-03, 3.14980262e-03, 2.21561749e-03, 1.64542215e-03, 2.34266209e-03, 2.04367440e-03, 1.45164012e-03, 1.00000000e-12]) f = interpolate.UnivariateSpline(X,Y,1./E,k=3) x = linspace(-1,1,10) max(abs(f(x) - f(-x))) 0.00066571302580914482 Thanks, Michael.
One simple workaround is to explicitly form an even function: g = lambda x: (f(x) + f(-x))/2.0 Since this is linear, it is quite easy to provide derivatives etc. but it would still be nice to have this in the spline itself. Michael. On 20 Mar 2008, at 2:36 AM, Michael McNeil Forbes wrote:
Does anyone have a suggestion on how to force a smoothing spline to be an even function f(x) = f(-x)?
participants (1)
-
Michael McNeil Forbes