Compute an approximation to the true least-squares-spline to the dataset. If the <nodelist> is not None, nodes will be placed near the x values indicated. If <nodelist> is None,<nodecount> equally-spaced nodes will be placed. Explicit indices for the node placement can be given in nodeindices, which overrides everything else. Definition at line 153 of file spline.py. 00153 : 00154 """Compute an approximation to the true least-squares-spline to the dataset. If the <nodelist> is not None, 00155 nodes will be placed near the x values indicated. If <nodelist> is None,<nodecount> equally-spaced nodes will be placed. 00156 Explicit indices for the node placement can be given in nodeindices, which overrides everything else.""" 00157 00158 assert nodelist or nodecount or nodeindices, "Must have either a list of nodes or a node count" 00159 00160 fitter=fitting_toolkit.polynomial_fit(2) #will fit quadratic sections 00161 00162 if not nodeindices: 00163 if not nodelist: #make equally-spaced nodelist 00164 nodelist=Numeric.array(range(nodecount),Numeric.Float)*((xvals[-1]-xvals[0])/(nodecount-1))+xvals[0] 00165 nodelist[-1]=xvals[-1] #make sure no roundoff error clips the last point! 00166 else: 00167 nodecount=len(nodelist) 00168 00169 nodeindices=Numeric.searchsorted(xvals, nodelist) 00170 boundindices=Numeric.searchsorted(xvals, (nodelist[1:]+nodelist[:-1])*0.5) #find halfway points 00171 else: 00172 boundindices=(nodeindices[1:]+nodeindices[:-1])//2 00173 nodelist=Numeric.take(xvals, nodeindices) 00174 00175 nodecount=len(nodeindices) 00176 00177 ya=Numeric.zeros(nodecount,Numeric.Float) 00178 00179 #fit first chunk un-centered to get slope at start 00180 fitter.fit_data(xvals[:nodeindices[1]], yvals[:nodeindices[1]], xcenter=nodelist[0]) 00181 ya[0]=fitter.funcparams[0] 00182 yp1=fitter.funcparams[1] 00183 00184 for i in range(1,nodecount-1): 00185 chunkstart=boundindices[i-1] 00186 chunkend=boundindices[i] 00187 fitter.fit_data(xvals[chunkstart:chunkend], yvals[chunkstart:chunkend], xcenter=nodelist[i]) 00188 ya[i]=fitter.funcparams[0] 00189 00190 #fit last chunk un-centered to get slope at end 00191 fitter.fit_data(xvals[nodeindices[-2]:], yvals[nodeindices[-2]:], xcenter=nodelist[-1]) 00192 ya[-1]=fitter.funcparams[0] 00193 ypn=fitter.funcparams[1] 00194 00195 y2a=spline(nodelist, ya, yp1=yp1, ypn=ypn) 00196 return nodelist, ya, y2a 00197 00198 if __name__=="__main__":
|