BTSpline1D.hh

Go to the documentation of this file.
00001 // @(#) $Id: BTSpline1D.hh,v 1.1 2005/08/20 21:45:11 tjrob Exp $  $Name:  $
00002 //
00003 // ********************************************************************
00004 // * DISCLAIMER                                                       *
00005 // *                                                                  *
00006 // * Neither the authors of this software system, nor their employing *
00007 // * institutes,nor the agencies providing financial support for this *
00008 // * work  make  any representation or  warranty, express or implied, *
00009 // * regarding  this  software system or assume any liability for its *
00010 // * use.                                                             *
00011 // *                                                                  *
00012 // * This  code  implementation is the  intellectual property  of     *
00013 // * FERMILAB.                                                        *
00014 // * By copying,  distributing  or modifying the Program (or any work *
00015 // * based  on  the Program)  you indicate  your  acceptance of  this *
00016 // * statement, and all its terms.                                    *
00017 // ********************************************************************
00018 //
00019 //
00020 // BTSpline1D.hh
00021 //
00022 // Created: Mark Fishler (5/00)
00023 //    
00024 
00025 #ifndef BTSPLINE1D_H
00026 #define BTSPLINE1D_H
00027 
00028 // The BTSpline1D class represents a table-driven cubic spline in 1 dimension.
00029 // The user supplies the table or the function to the constructor; thereafter, 
00030 // the spline is invoked as if it were a simple function of the input value x, 
00031 // as in 
00032 //
00033 //     double answer = mySpline ( x );
00034 //
00035 // The spline requires (or pre-computes) values of gradient at each point in
00036 // the table.  For cases where the gradient is known analytically, these may be
00037 // supplied directly.  Normally, the spline would be fed just the function or
00038 // values at the grid points; the gradients are pre-computed from the condition
00039 // that the function has a continuous second derivative.  
00040 
00041 // A related class is BTSpline, for an N-dimensional cubic spline taking
00042 // an array instead of a value for x.  The general BTSpline class is correct
00043 // for 1 dimension, but involves a bit of extra overhead, and the inconvenience
00044 // of requiring a pointer to the value x (rather than the value itself) because
00045 // it will treat x[] as an array of dimension 1.  
00046 
00047 // See BTSpline.h.
00048 
00049 #ifdef BTSpline_DESIGN
00050 
00051 The BTSpline1D class is an object representing the transformation between some
00052 value x and a value phi, based on tables of values and gradients (derivatives).
00053 The values and gradients are computed (or supplied) at a set of points 
00054 (we call these node points) upon construction. These node points may be 
00055 irregularly spaced. 
00056 
00057 From then on, the user can invoke the () operator supplying the value of x;
00058 the operator returns the value of the function phi, based on an interpolating 
00059 function which exactly matches the values and slopes at every node point.
00060 
00061 A given instance of a BTSpline is constructed from:
00062 
00063  (1) An integer extent of the array defining the node points.
00064  (2) An array of N numbers representing the coordinates of each node points.
00065  (3) The values and optionally the gradients of phi at those node points.  
00066 
00067 For convenience, the value and gradient information can be supplied in
00068 any of several ways:
00069 
00070 * A pointer to a function returning a double which takes a double x.  If this 
00071   is supplied, then the constructor will call the function however many times 
00072   to fill in the tables of values at the node points.  It will then compute
00073   the gradients needed for continuity in the second derivative, using "natural"
00074   boundary conditions (second derivative zero at both ends).
00075 
00076 * A single table containing the value for each point.  Again, derivatives are 
00077   comuted using "natural" boundary conditions.
00078 
00079 * Either a pointer to a function or a table, as above, followed by two 
00080   additional arguments which represent the values  of slope at the beginnng 
00081   and end boundaries; 
00082 
00083 * A table of values and a separate table of one gradients (derivative) per 
00084   point.  
00085 
00086 * A pointer to a function returning a void, which takes a double x and
00087   two pointers to value and gradient.  If this is supplied, then the 
00088   constructor will call the function however many times to fill in the
00089   tables of values and gradients at the node points.
00090 
00091 The tables are captured in the class instance data, and may be discarded after
00092 construction.
00093 
00094 The only user-relevant method other than the constructor is 
00095         double operator() ( double x ) const;
00096 that is, supplying a x for the desired sample point and receiving a double 
00097 value
00098 of phi.  The value returned is a cubic function in each interval which will
00099 match the values and the gradients at every node.
00100 
00101 The class does NOT verify that x lies within the domain defined by the node
00102 points; if it is outside, then the value obtained is still a valid
00103 approximation, but one which will get worse as the distance from the nearest 
00104 node grows large.
00105 
00106 #endif
00107 
00108 class BTSpline1D {
00109 
00110 // The usage of the spline, and any functions provided, are based on doubles.  
00111 // However, for space purposes, the class might be reconfigured to store (and 
00112 // accept) tables in the float format.  
00113 
00114 typedef double Data_t;          // In the future we MAY want to template off
00115                                 // the type of table data; using Data_t for
00116                                 // this purpose will make this easy.
00117 
00118 public:
00119 
00120   // Invoke the spline to get a value:
00121 
00122   double operator() ( double x ) const;
00123 
00124   // (Deep) copy constructor
00125 
00126   BTSpline1D ( const BTSpline1D & s );
00127   BTSpline1D (  );
00128   
00129   
00130 
00131   // Constructors A -- the class accepts value info in two ways, and optionally
00132   //                   accepts boundary slope info (if that is not provided, 
00133   //                   then the second derivative is set to 0 at boundaries).
00134 
00135 #ifdef FUTURE
00136 
00137   BTSpline1D (  
00138           const std::vector<Data_t> nodes, // Locations in x of nodes 
00139           const std::vector<Data_t> values // Values being approximated
00140   );
00141 
00142   BTSpline1D (  
00143           const std::vector<Data_t> nodes, // Locations in x of nodes 
00144           const std::vector<Data_t> values,// Values being approximated
00145           Data_t slope0, Data_t slopeN
00146   );
00147 
00148 #endif // FUTURE
00149 
00150   BTSpline1D (  
00151           int extent,                      // Number of node points
00152           const Data_t* nodes,             // Locations in x of nodes 
00153           const Data_t* values             // Values being approximated
00154   );
00155 
00156   BTSpline1D (  
00157           int extent,                      // Number of node points
00158           const Data_t* nodes,             // Locations in x of nodes 
00159           double values_function (double x)// Function being approximated
00160   );
00161 
00162   BTSpline1D (  
00163           int extent,                      // Number of node points
00164           const Data_t* nodes,             // Locations in x of nodes 
00165           const Data_t* values,            // Values being approximated
00166           Data_t slope0, Data_t slopeN
00167   );
00168 
00169   BTSpline1D (  
00170           int extent,                      // Number of node points
00171           const Data_t* nodes,             // Locations in x of nodes 
00172           double values_function (double x), // Function being approximated
00173           Data_t slope0, Data_t slopeN
00174   );
00175 
00176   // Constructors B -- the class accepts value and gradient info in three ways:
00177 
00178 #ifdef FUTURE
00179 
00180   BTSpline1D (  
00181           const std::vector<Data_t> nodes,    // Locations in x of nodes 
00182           const std::vector<Data_t> values,   // Values at each node point
00183           const std::vector<Data_t> gradients // derivatives at each node point
00184   );
00185 
00186   // And some appropriate form for passing a values/grads function.
00187 
00188 #endif // FUTURE
00189 
00190   BTSpline1D (  
00191           int extent,                   // Number of node points
00192           const Data_t* nodes,          // Locations in x of nodes 
00193           const Data_t* values,         // values at each node point
00194           const Data_t* gradients       // derivatives at each node point
00195                                         // nodes, values, gradients are arrays
00196                                         // of extent Data_ts.
00197            );
00198 
00199   BTSpline1D (
00200           int extent,                   // Number of node points
00201           const Data_t* nodes,          // Locations in x of nodes 
00202           void values_grads_function (
00203                 double x, 
00204                 double* val, 
00205                 double* grads)
00206                                         // Function filling value and 
00207                                         // gradient for input x
00208            );
00209 
00210   ~BTSpline1D();
00211 
00212 protected:
00213 
00214   // member data:
00215 
00216   int     extent_;
00217   Data_t*  nodePoints_; 
00218   Data_t*  distances_;          // distances separating nodes
00219   Data_t*  values_;
00220   Data_t*  grads_;
00221   Data_t*  secondDerivs_;
00222 
00223   bool    naturalBoundaryConditions;
00224   Data_t  slope0_;
00225   Data_t  slopeN_;
00226     
00227   // internal methods:
00228 
00229   void captureGrid( int extent, const Data_t* nodes );
00230   void captureValues( int extent, const Data_t* values );
00231   void fillValues( int extent, double values_function(double x));
00232   void computeSecondDerivs();
00233 
00234 }; // BTSpline
00235 
00236 #endif // BTSPLINE1D_H
00237 
g4beamline