1 | #ifndef SLININTERP_H_SEEN
|
---|
2 | #define SLININTERP_H_SEEN
|
---|
3 | /*
|
---|
4 | --- SOPHYA software - NTools module ---
|
---|
5 | --- Class SLinInterp1D : Simple linear 1D interpolation class
|
---|
6 | R. Ansari , C. Magneville 2000-2010
|
---|
7 | (C) UPS+LAL IN2P3/CNRS (C) IRFU-SPP/CEA
|
---|
8 | */
|
---|
9 |
|
---|
10 | #include "machdefs.h"
|
---|
11 | #include <iostream>
|
---|
12 | #include <vector>
|
---|
13 | #include <string>
|
---|
14 | #include <exception>
|
---|
15 | #include <stdexcept>
|
---|
16 |
|
---|
17 | #include "genericfunc.h"
|
---|
18 |
|
---|
19 | using namespace std;
|
---|
20 |
|
---|
21 | //-------------------------------------------
|
---|
22 | // --- Class SLinInterp1D :
|
---|
23 | // Simple linear 1D interpolation class
|
---|
24 | //-------------------------------------------
|
---|
25 |
|
---|
26 | namespace SOPHYA {
|
---|
27 |
|
---|
28 | class SLinInterp1D : public GenericFunc {
|
---|
29 | public :
|
---|
30 | //! Default constructor - represent the function y=x
|
---|
31 | SLinInterp1D();
|
---|
32 | // Regularly spaced points in X with Y values defined by yreg
|
---|
33 | SLinInterp1D(double xmin, double xmax, vector<double>& yreg);
|
---|
34 | // Interpolate to a finer regularly spaced grid, from xmin to xmax with npt points if (npt>0)
|
---|
35 | SLinInterp1D(vector<double>& xs, vector<double>& ys, double xmin=1., double xmax=-1., size_t npt=0);
|
---|
36 |
|
---|
37 | virtual ~SLinInterp1D() { }
|
---|
38 |
|
---|
39 | double XMin() const { return xmin_; }
|
---|
40 | double XMax() const { return xmax_; }
|
---|
41 | double DeltaX() { return dx_; }
|
---|
42 | inline double X(long i) const {return xmin_ + i*dx_;} // returns x value for index i
|
---|
43 |
|
---|
44 | // --------------------------------------------------------------
|
---|
45 | //! Return the interpolated Y value as a function of X
|
---|
46 | double YInterp(double x) const ;
|
---|
47 | // To compile without SOPHYA : Comment the following line
|
---|
48 | //! Return the interpolated Y value as a function of X
|
---|
49 | virtual inline double operator()(double x) { return YInterp(x); }
|
---|
50 | // --------------------------------------------------------------
|
---|
51 |
|
---|
52 | // Define the interpolation points through a set of regularly spaced points on X, between xmin and xmax
|
---|
53 | void DefinePoints(double xmin, double xmax, vector<double>& yreg);
|
---|
54 | // Interpolate to a finer regularly spaced grid, from xmin to xmax with npt points
|
---|
55 | void DefinePoints(vector<double>& xs, vector<double>& ys, double xmin=1., double xmax=-1., size_t npt=0);
|
---|
56 |
|
---|
57 | //! Read Y's ( one / line) for regularly spaced X's from file and call DefinePoints(xmin, xmax, yreg)
|
---|
58 | size_t ReadYFromFile(string const& filename, double xmin, double xmax);
|
---|
59 | //! Read pairs of X Y ( one pair / line) from file and call DefinePoints(xs, ys ...)
|
---|
60 | size_t ReadXYFromFile(string const& filename, double xmin=1., double xmax=-1., size_t npt=0);
|
---|
61 |
|
---|
62 | vector<double>& GetVX() { return xs_; }
|
---|
63 | vector<double>& GetVY() { return ys_; }
|
---|
64 |
|
---|
65 | //! Print the object (interpolation points) on "ostream". lev=0,1
|
---|
66 | ostream& Print(ostream& os,int lev=0) const ;
|
---|
67 | //! Print the object (interpolation points) on "cout". lev=0,1
|
---|
68 | inline ostream& Print(int lev=0) const { return Print(cout, lev); }
|
---|
69 |
|
---|
70 | protected:
|
---|
71 | vector<double> yreg_, xs_, ys_; // interpolated y value for regularly spaced x
|
---|
72 | double xmin_, xmax_, dx_; // dx is spacing of finer grid of x's
|
---|
73 | size_t ksmx_; // Maximum index value in xs_, ys_
|
---|
74 | size_t npoints_; // Number of regularly spaced points, xmax not included
|
---|
75 | };
|
---|
76 |
|
---|
77 | /*! operator << overloading - Prints the interpolator object on the stream \b s */
|
---|
78 | inline ostream& operator << (ostream& s, SLinInterp1D const& a)
|
---|
79 | { a.Print(s,0); return s; }
|
---|
80 |
|
---|
81 | } // namespace SOPHYA
|
---|
82 |
|
---|
83 | #endif /* SLININTERP_H__SEEN */
|
---|