casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
MathFunc.h
Go to the documentation of this file.
00001 //# MathFunc.h: Templated letter/envelope classes for single dependent variable functions
00002 //# Copyright (C) 1993,1994,1995,1996,1999,2000,2001,2003
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: MathFunc.h 20229 2008-01-29 15:19:06Z gervandiepen $
00027 
00028 #ifndef SCIMATH_MATHFUNC_H
00029 #define SCIMATH_MATHFUNC_H
00030 
00031 //# MathFunc: A templated letter/envelope set of classes for packaging
00032 //# of specific single dependent variable functions.
00033 
00034 
00035 #include <casa/aips.h>
00036 #include <casa/BasicMath/Math.h>
00037 #include <casa/BasicSL/Constants.h>
00038 #include <casa/Exceptions/Error.h>
00039 #include <casa/Arrays/Vector.h>
00040 #include <casa/BasicSL/String.h>
00041 
00042 namespace casa { //# NAMESPACE CASA - BEGIN
00043 
00044 // <summary>
00045 // Error class for <linkto class=MathFunc>MathFunc</linkto> class
00046 // </summary>
00047 
00048 // <synopsis>
00049 // Error class for <linkto class=MathFunc>MathFunc</linkto> class
00050 // </synopsis>
00051 
00052 class MathFuncError : public AipsError
00053 {
00054 public:
00055     MathFuncError() : AipsError("MathFuncError") {}
00056     MathFuncError(const Char *m)   : AipsError(m) {}
00057     MathFuncError(const String &m) : AipsError(m) {}
00058 
00059     virtual ~MathFuncError() throw() {}
00060 };
00061 
00062 // <summary>
00063 // Fred Schwab function to calculate spheriodal functions
00064 // </summary>
00065 
00066 // <synopsis>
00067 // Fred Schwab function to calculate spheriodal functions.
00068 // </synopsis>
00069 
00070 // <group name="spheriodal functions">
00071 
00072 // Fred Schwab function to calculate spheriodal functions, in C.
00073 extern "C" { 
00074    Int sphfn(Int *, Int *, Int *, float *, float *, Int *);
00075 }
00076 
00077 // C++ wrapper to Fred Schwab function to calculate spheriodal functions.
00078 float sphfn(Int ialf, Int im, float eta);
00079 
00080 // </group>
00081 
00082 
00083 // <summary>
00084 // Enum used to identify function type for
00085 // MathFunc class
00086 // </summary>
00087 
00088 // <synopsis>
00089 // Enum used to identify function type for
00090 // <linkto class=MathFunc>MathFunc</linkto> class
00091 // </synopsis>
00092 
00093 //############################################################################
00094 //# NOTE:  Delete the following listing of the enumerations when enumerations
00095 //#        are handled properly by the documentation extractor.
00096 //############################################################################
00097 
00098 // <note role=caution>
00099 // The following enum documentation is currently
00100 // extracted by hand, and thus
00101 // could be out of date if this documentation was not updated when the
00102 // enum was modified.
00103 // </note>
00104 
00105 // The FUNCTYPE enum is:
00106 // <srcblock>
00107 //
00108 // enum FUNCTYPE { UNARY, GAUSSIAN, KB, MOD_KB, SINC, SPHEROIDAL, EXP_SINC };
00109 //
00110 // </srcblock>
00111 
00112 // <group name="FUNCTYPE enum">
00113 
00114 enum FUNCTYPE { UNARY, GAUSSIAN, KB, MOD_KB, SINC, SPHEROIDAL, EXP_SINC };
00115 
00116 // </group>
00117 
00118 
00119 // <summary>
00120 // Function ID, for use by <linkto class=MathFunc>MathFunc</linkto> class
00121 // </summary>
00122 
00123 // <synopsis>
00124 // Function ID, for use by <linkto class=MathFunc>MathFunc</linkto> class.
00125 // </synopsis>
00126 
00127 struct FuncId { String name; Vector<double> args;};
00128 
00129 
00130 // <summary> A class to generate values of mathematical functions </summary>
00131 //
00132 // <synopsis>
00133 //
00134 // This class is the abstract base class for 1-dimensional math functions.
00135 //
00136 // Actual math functions are then an inherited class from the base
00137 // class. This approach allows one to define actual function values
00138 // for each derived class. Then, one can pass a generic MathFunc
00139 // pointer to other objects, but the other objects will still get
00140 // function values from the actual inherited function.  
00141 //
00142 // By defining each math function as an object, we can place
00143 // parameters which will not change from one call to the function value
00144 // to another in the class definition and they only have to be
00145 // initialized once.
00146 //
00147 // </synopsis>
00148 
00149 //
00150 // MathFunc is the base class for 1-dimensional math functions
00151 //
00152 template<class T>
00153 class MathFunc {
00154 public:
00155 //
00156 // constructors
00157 //
00158    MathFunc(FUNCTYPE);  
00159     // accept up to 4 arguments, the first being the support radius
00160    MathFunc(FUNCTYPE, T cutoff, T arg1 = 1.0e+30, T arg2 = 1.0e+30, 
00161             T arg3 = 1.0e+30);  
00162    MathFunc(String &, Vector<double> &);
00163    MathFunc(const MathFunc<T>&);        // Copy constructor
00164    MathFunc(MathFunc<T> *);               
00165 
00166 //
00167 // Destructor
00168 //
00169    virtual ~MathFunc(); 
00170 
00171 //
00172 // Assignment operator - Note: this function works only for envelops.
00173 // Polymorphism flaws will let you pass a letter as an argument but an
00174 // exception will be thrown at run time.
00175 //
00176    MathFunc<T>& operator=(const MathFunc<T>&);
00177 
00178 //
00179 // return value of support width
00180 //  
00181    virtual T sup_value() const;
00182 
00183 //
00184 // compute and return a value of the math function
00185 //
00186    virtual T value(const T &a) const; 
00187 
00188 //
00189 // create a new math function
00190 //
00191    static MathFunc<T> *newMathFunc(const MathFunc<T>&);
00192 
00193 //
00194 // return a FuncId structure for Table storage/retrieval.
00195 //
00196   virtual FuncId id() const;
00197 
00198 //
00199 // These functions return the static constants used as default
00200 // parameters for the various derived functions
00201 //
00202 
00203 // The default support radius
00204     static T defcutoff() {return defcutoff_p;}
00205 // The default width for Gaussian_Conv
00206     static T defwidth() {return defwidth_p;}
00207 // The default width for KB_Conv and Mod_KB_Conv
00208     static T defKBwidth() {return defKBwidth_p;}
00209 // A default parameter for KB_Conv and Mod_KB_Conv
00210     static T defKBparm() {return defKBparm_p;}
00211 // A default parameter for Mod_KB_Conv
00212     static T defmodKBparm() {return defmodKBparm_p;}
00213 // The default support radius for Sinc_Conv and Sph_Conv
00214     static T defSphcutoff() {return defSphcutoff_p;}
00215 // The default Sinc parameter for Sinc_Conv and Exp_Sinc_Conv
00216     static T defSincparm() {return defSincparm_p;}
00217 // The default parameter for Sph_Conv
00218     static T defSphparm() {return defSphparm_p;}
00219 // The default exponential power for Exp_Sinc_Conv
00220     static T defExpPower() {return defExpPower_p;}
00221 // The default exponential scale length for Exp_Sinc_Conv
00222     static T defExpScale() {return defExpScale_p;}
00223 
00224 protected:
00225 //
00226 // for every derived class, return new of that class with its own parameters
00227 //
00228    virtual MathFunc<T> * clone() const;
00229 
00230 //
00231 // Default constructor (Null)
00232 //
00233    MathFunc();       
00234 
00235 //
00236 // pointer to letter class
00237 //
00238    MathFunc<T> *object;
00239 
00240 private:
00241    static T defcutoff_p;
00242    static T defwidth_p;
00243    static T defKBparm_p;
00244    static T defKBwidth_p;
00245    static T defmodKBparm_p;
00246    static T defSphcutoff_p;
00247    static T defSphparm_p;
00248    static T defSincparm_p;
00249    static T defExpPower_p;
00250    static T defExpScale_p;
00251 };
00252 
00253 //# ========================================================
00254 //# Now we define actual math classes as inherited classes of
00255 //# the base class MathFunc
00256 //# =========================================================
00257 
00258 //
00259 // <summary>Unary</summary>
00260 //
00261 // <synopsis>
00262 // A Unary function (always returns the value 1.0)
00263 // </synopsis>
00264 //
00265 template<class T>
00266 class Unary: public MathFunc<T>
00267 {
00268 public:
00269   //
00270   //default constructor
00271   //
00272   Unary( T cut = MathFunc<T>::defcutoff());
00273 
00274   //
00275   //copy constructor
00276   //
00277   Unary(const Unary<T>&);
00278 
00279   Unary<T>& operator=(const Unary<T>&);
00280 
00281   T sup_value() const{ return sup_width;}
00282 
00283   T value(const T &) const;
00284 
00285 //
00286 // return a FuncId structure for Table storage/retrieval.
00287 //
00288   FuncId id() const;
00289 
00290 private:
00291   MathFunc<T> * clone() const;
00292 
00293   T sup_width;
00294 };
00295 
00296 //
00297 // <category lib=aips sect="Math">
00298 // <summary>Gaussian</summary>
00299 //
00300 // <synopsis>
00301 // A Gaussian
00302 // </synopsis>
00303 //
00304 template<class T>
00305 class GaussianConv: public MathFunc<T>
00306 {
00307 public:
00308   //
00309   //default constructor
00310   //
00311   GaussianConv(   T cut = MathFunc<T>::defcutoff(), 
00312                T wparm = MathFunc<T>::defwidth()); 
00313 
00314   //
00315   //copy constructor
00316   //
00317   GaussianConv(const GaussianConv<T>&);
00318 
00319   GaussianConv<T>& operator=(const GaussianConv<T>&);
00320 
00321   T sup_value() const{ return sup_width;}
00322 
00323   T value(const T &) const;
00324 
00325 //
00326 // return a FuncId structure for Table storage/retrieval.
00327 //
00328   FuncId id() const;
00329 
00330 private:
00331   MathFunc<T> * clone() const;
00332 
00333   T sup_width, fw2; 
00334   const T ln16;
00335 };
00336 
00337 //
00338 // <category lib=aips sect="Math">
00339 // <summary>A Kaiser-Bessel function</summary>
00340 //
00341 // <synopsis>
00342 // A Kaiser-Bessel function
00343 // </synopsis>
00344 //
00345 template<class T>
00346 class KB_Conv: public MathFunc<T>
00347 {
00348 public:
00349 
00350    //default constructor
00351    KB_Conv(T cut = MathFunc<T>::defcutoff(), 
00352            T wparm = MathFunc<T>::defKBwidth(), 
00353            T kbparm = MathFunc<T>::defKBparm());
00354 
00355    // copy constructor
00356    KB_Conv(const KB_Conv<T> &);
00357 
00358    KB_Conv<T>& operator=(const KB_Conv<T>&);
00359 
00360    T sup_value() const { return sup_width;}
00361 
00362    T value(const T &) const;
00363 
00364 //
00365 // return a FuncId structure for Table storage/retrieval.
00366 //
00367   FuncId id() const;
00368 
00369 private:
00370    MathFunc<T> * clone() const;
00371 
00372    T kbparm, fw, sup_width;
00373 };
00374 
00375 //
00376 // <category lib=aips sect="Math">
00377 // <summary>A Kaiser-Bessel function multiplied by a Gaussian</summary>
00378 //
00379 // <synopsis>
00380 // A Kaiser-Bessel function multiplied by a Gaussian
00381 // </synopsis>
00382 //
00383 
00384 template<class T>
00385 class Mod_KB_Conv: public MathFunc<T>
00386 {
00387 public:
00388    //default constructor
00389    Mod_KB_Conv   (T cut    = MathFunc<T>::defcutoff(), 
00390                   T wparm  = MathFunc<T>::defKBwidth(), 
00391                   T kbparm = MathFunc<T>::defKBparm(), 
00392                   T gwparm = MathFunc<T>::defmodKBparm());
00393 
00394    //copy constructor
00395    Mod_KB_Conv(const Mod_KB_Conv<T>&);
00396 
00397    Mod_KB_Conv<T>& operator=(const Mod_KB_Conv<T>&);
00398 
00399    T sup_value() const { return sup_width;}
00400 
00401    T value(const T &) const;
00402 
00403 //
00404 // return a FuncId structure for Table storage/retrieval.
00405 //
00406   FuncId id() const;
00407 
00408 private:
00409    MathFunc<T>* clone() const;
00410 
00411    T kbparm, gw2, sup_width, widthparm;
00412    const T ln16;
00413 };
00414 
00415 // <category lib=aips sect="Math">
00416 // <summary>Sine x / x function</summary>
00417 //
00418 //
00419 // <synopsis>
00420 // Sine x / x function
00421 // </synopsis>
00422 //
00423 template<class T>
00424 class Sinc_Conv: public MathFunc<T>
00425 {
00426 public:
00427    //default constructor
00428    Sinc_Conv(T cut = MathFunc<T>::defSphcutoff(),
00429              T sincparm = MathFunc<T>::defSincparm()); 
00430    //copy constructor
00431    Sinc_Conv(const Sinc_Conv<T>&);
00432 
00433    Sinc_Conv<T>& operator=(const Sinc_Conv<T>&);
00434 
00435    T sup_value() const { return sup_width;}
00436 
00437    T value(const T &) const;
00438 
00439 //
00440 // return a FuncId structure for Table storage/retrieval.
00441 //
00442   FuncId id() const;
00443 
00444 private:
00445    MathFunc<T> * clone() const;
00446 
00447    T Sinc_parm, sup_width;
00448 };
00449 
00450 //
00451 // <category lib=aips sect="Math">
00452 // <summary>Spheroidal function</summary>
00453 //
00454 // <synopsis>
00455 // Spheroidal function - calls Fred Schwab function converted by f2c
00456 // </synopsis>
00457 //
00458 template<class T>
00459 class Sph_Conv: public MathFunc<T>
00460 //
00461 // Spheroidal function - calls Fred Schwab function converted by f2c
00462 //
00463 {
00464 public:
00465    //default constructor
00466    Sph_Conv(T cut = MathFunc<T>::defSphcutoff(),
00467             T Sphparm = MathFunc<T>::defSphparm()); 
00468    //copy constructor
00469    Sph_Conv(const Sph_Conv<T>&);
00470 
00471    Sph_Conv<T>& operator=(const Sph_Conv<T>&);
00472 
00473    T sup_value() const { return sup_width;}
00474 
00475    float value(const float &) const;
00476 
00477 //
00478 // return a FuncId structure for Table storage/retrieval.
00479 //
00480   FuncId id() const;
00481 
00482 private:
00483    MathFunc<T> * clone() const;
00484 
00485    T sup_width, sphparm;
00486 };
00487 
00488 // <category lib=aips sect="Math">
00489 // <summary>Exponential times a Sinc</summary>
00490 //
00491 // <synopsis>
00492 // An Exponential times a Sinc
00493 //
00494 // The <src> value(T &x) </src> is given by
00495 // <src> Exp(-(abs(x) / expscale) ** exppow)  * Sinc( pi * x / sincparm) </src>
00496 //
00497 // where the 3 paramaters correspond to those in the default constructor
00498 // Note that the default case of <src> exppow = 2 </src> is
00499 // a Gaussian times a Sinc.
00500 // Since this is often a useful case, that parameter appears last in the
00501 // constructor.
00502 // </synopsis>
00503 //
00504 template<class T>
00505 class ExpSincConv: public MathFunc<T>
00506 {
00507 public:
00508     //
00509     // default constructor
00510     //
00511     ExpSincConv ( T cut = MathFunc<T>::defcutoff(),
00512                       T sincparm = MathFunc<T>::defSincparm(),
00513                       T exppow = MathFunc<T>::defExpPower(),
00514                       T expscale = MathFunc<T>::defExpScale());
00515     // copy constructor
00516     ExpSincConv (const ExpSincConv<T>&);
00517 
00518     // assignment operator
00519     ExpSincConv<T>& operator=(const ExpSincConv<T>&);
00520 
00521     // get access to the support width
00522     T sup_value() const { return sup_width; }
00523 
00524     // and get the value of the function
00525     T value(const T &) const;
00526 
00527     //
00528     // return FuncID structure for Table storage/retrieval.
00529     //
00530     FuncId id() const;
00531 private:
00532     MathFunc<T> * clone() const;
00533 
00534     T sup_width, scale, exponent, sincpByPi;
00535 };
00536 
00537 
00538 
00539 } //# NAMESPACE CASA - END
00540 
00541 #ifndef CASACORE_NO_AUTO_TEMPLATES
00542 #include <scimath/Mathematics/MathFunc.tcc>
00543 #endif //# CASACORE_NO_AUTO_TEMPLATES
00544 #endif //AIPS_MATHFUNC_H