Stimfit 0.12.7
src/stimfit/math/stfmath.h
Go to the documentation of this file.
00001 // Header file for the stimfit namespace
00002 // General-purpose routines
00003 // last revision: 08-08-2006
00004 // C. Schmidt-Hieber, christsc@gmx.de
00005 
00006 // This program is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU General Public License
00008 // as published by the Free Software Foundation; either version 2
00009 // of the License, or (at your option) any later version.
00010 
00011 // This program is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019 
00026 #ifndef _CORE_H
00027 #define _CORE_H
00028 
00029 #ifdef _WINDOWS
00030 #pragma warning( disable : 4251 )  // Disable warning messages
00031 #endif
00032 
00033 #include <vector>
00034 #include <complex>
00035 #include <deque>
00036 #ifdef _OPENMP
00037 #include <omp.h>
00038 #endif
00039 
00040 #include "./../stf.h"
00041 
00042 // header for the fourier transform:
00043 #ifndef TEST_MINIMAL
00044 #include "fftw3.h"
00045 #endif
00046 #include "./spline.h"
00047 
00048 namespace stf {
00049 
00054 
00055 
00059 typedef boost::function<double(double, const Vector_double&)> Func;
00060 
00062 typedef boost::function<Vector_double(double, const Vector_double&)> Jac;
00063 
00065 typedef boost::function<double(double, double, double, double, double)> Scale;
00066 
00068 Vector_double nojac( double x, const Vector_double& p);
00069 
00071 double noscale(double param, double xscale, double xoff, double yscale, double yoff);
00072  
00074 
00079 struct parInfo {
00081     parInfo()
00082     : desc(""),toFit(true), constrained(false), constr_lb(0), constr_ub(0), scale(noscale), unscale(noscale) {}
00083 
00085 
00094   parInfo( const std::string& desc_, bool toFit_, bool constrained_ = false, 
00095              double constr_lb_ = 0, double constr_ub_ = 0, Scale scale_ = noscale, Scale unscale_ = noscale)
00096     : desc(desc_),toFit(toFit_),
00097         constrained(false), constr_lb(constr_lb_), constr_ub(constr_ub_),
00098         scale(scale_), unscale(unscale_)
00099     {}
00100 
00101     std::string desc; 
00102     bool toFit;    
00103     bool constrained; 
00104     double constr_lb; 
00105     double constr_ub; 
00106     Scale scale; 
00107     Scale unscale; 
00108 };
00109 
00111 typedef boost::function<Table(const Vector_double&,const std::vector<stf::parInfo>,double)> Output;
00112  
00114 Table defaultOutput(const Vector_double& pars, 
00115                     const std::vector<parInfo>& parsInfo,
00116                     double chisqr);
00117 
00119 typedef boost::function<void(const Vector_double&, double, double, double, double, double, Vector_double&)> Init;
00120 
00122 
00127 struct StfDll storedFunc {
00128 
00130 
00138     storedFunc( const std::string& name_, const std::vector<parInfo>& pInfo_,
00139             const Func& func_, const Init& init_, const Jac& jac_, bool hasJac_ = true,
00140             const Output& output_ = defaultOutput /*,
00141             bool hasId_ = true*/
00142     ) : name(name_),pInfo(pInfo_),func(func_),init(init_),jac(jac_),hasJac(hasJac_),output(output_) /*, hasId(hasId_)*/
00143     {
00144 /*        if (hasId) {
00145             id = NextId();
00146             std::string new_name;
00147             new_name << id << ": " << name;
00148             name = new_name;
00149         } else
00150             id = 0;
00151 */    }
00152      
00154     ~storedFunc() { }
00155 
00156 //    static int n_funcs;          /*!< Static function counter */
00157 //    int id;                      /*!< Function id; set automatically upon construction, so don't touch. */
00158     std::string name;            
00159     std::vector<parInfo> pInfo;  
00160     Func func;                   
00161     Init init;                   
00162     Jac jac;                     
00163     bool hasJac;                 
00164     Output output;               
00165 //    bool hasId;                  /*!< Determines whether a function should have an id. */
00166 
00167 };
00168 
00170 
00173 template <typename T>
00174 T SQR (T a);
00175 
00177 
00186 Vector_double
00187 filter(
00188         const Vector_double& toFilter,
00189         std::size_t filter_start,
00190         std::size_t filter_end,  
00191         const Vector_double &a,
00192         int SR,
00193         stf::Func func,
00194         bool inverse = false
00195 );
00196 
00198 
00202 std::map<double, int>
00203 histogram(const Vector_double& data, int nbins=-1);
00204 
00206 
00213 StfDll Vector_double
00214 deconvolve(const Vector_double& data, const Vector_double& templ,
00215            int SR, double hipass, double lopass, stfio::ProgressInfo& progDlg);
00216 
00218 
00223 template <class T>
00224 std::vector<T>
00225 cubicSpline(
00226         const std::vector<T>& y,
00227         T oldF,
00228         T newF
00229 );
00230 
00232 
00235 wxString sectionToString(const Section& section);
00236 
00238 /* \param input The valarray to be differentiated.
00239  * \param x_scale The sampling interval.
00240  * \return The result of the differentiation.
00241  */
00242 template <class T>
00243 std::vector<T> diff(const std::vector<T>& input, T x_scale);
00244 
00246 
00252 double integrate_simpson(
00253         const Vector_double& input,
00254         std::size_t a,
00255         std::size_t b,
00256         double x_scale
00257 );
00258 
00260 
00266 double integrate_trapezium(
00267         const Vector_double& input,
00268         std::size_t a,
00269         std::size_t b,
00270         double x_scale
00271 );
00272 
00274 
00286 int
00287 linsolv(
00288         int m,
00289         int n,
00290         int nrhs,
00291         Vector_double& A,
00292         Vector_double& B
00293 );
00294 
00296 
00301 Vector_double
00302 quad(const Vector_double& data, std::size_t begin, std::size_t end);
00303  
00304 
00306 
00310 StfDll Vector_double
00311 detectionCriterion(
00312         const Vector_double& data,
00313         const Vector_double& templ,
00314         stfio::ProgressInfo& progDlg
00315 );
00316 
00317 // TODO: Add negative-going peaks.
00319 
00324 StfDll std::vector<int> peakIndices(const Vector_double& data, double threshold, int minDistance);
00325 
00327 
00331 StfDll Vector_double linCorr(const Vector_double& va1, const Vector_double& va2, stfio::ProgressInfo& progDlg); 
00332 
00334 
00342 double fgaussColqu(double x, const Vector_double& p);
00343 
00345 
00352 double fboltz(double x, const Vector_double& p);
00353 
00355 
00362 double fbessel(double x, int n);
00363 
00365 
00374 double fbessel4(double x, const Vector_double& p);
00375 
00377 
00380 wxString CreatePreview(const wxString& fName);
00381 
00383 
00386 int fac(int arg);
00387 
00389 
00392 int pow2(int arg);
00393 
00395 
00398 wxString noPath(const wxString& fName);
00399 
00402 }
00403 
00404 typedef std::vector< stf::storedFunc >::const_iterator c_stfunc_it; 
00406 inline int stf::pow2(int arg) {return 1<<arg;}
00407 
00409 
00412 template <typename T>
00413 void SWAP(T s1, T s2) {
00414     T aux=s1;
00415     s1=s2;
00416     s2=aux;
00417 }
00418 
00419 template <class T>
00420 std::vector<T>
00421 stf::cubicSpline(const std::vector<T>& y,
00422         T oldF,
00423         T newF)
00424 {
00425     double factor_i=newF/oldF;
00426     int size=(int)y.size();
00427     // size of interpolated data:
00428     int size_i=(int)(size*factor_i);
00429     Vector_double x(size);
00430     Vector_double y_d(size);
00431     for (int n_p=0; n_p < size; ++n_p) {
00432         x[n_p]=n_p;
00433         y_d[n_p]=y[n_p];
00434     }
00435     Vector_double y_i(stf::spline_cubic_set(x,y_d,0,0,0,0));
00436 
00437     std::vector<T> y_if(size_i);
00438     Vector_double x_i(size_i);
00439 
00440     //Cubic spline interpolation:
00441     for (int n_i=0; n_i < size_i; ++n_i) {
00442         x_i[n_i]=(double)n_i * (double)size/(double)size_i;
00443         double yp, ypp;
00444         y_if[n_i]=(T)stf::spline_cubic_val(x,x_i[n_i],y_d,y_i,yp,ypp);
00445     }
00446     return y_if;
00447 }
00448 
00449 template <class T>
00450 std::vector<T> stf::diff(const std::vector<T>& input, T x_scale) {
00451     std::vector<T> diffVA(input.size()-1);
00452     for (unsigned n=0;n<diffVA.size();++n) {
00453         diffVA[n]=(input[n+1]-input[n])/x_scale;
00454     }
00455     return diffVA;
00456 }
00457 
00458 template <typename T>
00459 inline T stf::SQR(T a) {return a*a;}
00460 
00461 #endif