Ignis
Loading...
Searching...
No Matches
linearInterp.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <cstdlib>
5#include <iostream>
6
13
15
16 public:
17
18 std::vector<double> *X;
19 std::vector<double> *Y;
20
21 int nxy;
22
23 private:
24
25 int ilo;
26 int ihi;
27
29
30 public:
31
35 double interp(double x){
37 return (*Y)[ilo] + (x-(*X)[ilo])*((*Y)[ihi]-(*Y)[ilo])/((*X)[ihi]-(*X)[ilo]);
38 }
39
40 private:
41
44 void set_bounding_indicies(double x){
45 if(x <= (*X)[0])
46 ihi = 1;
47 else if(x >= (*X).back())
48 ihi = nxy-1;
49 else {
50 std::vector<double>::iterator itHi = std::lower_bound((*X).begin(), (*X).end(), x); // lower_bound gives values >= x
51 ihi = itHi - (*X).begin();
52 }
53 ilo = ihi-1;
54 }
55
57
58 public:
59
60 //------------- constructors
61
64
68 linearInterp(std::vector<double> &_X, std::vector<double> &_Y){
69 X = &_X;
70 Y = &_Y;
71 nxy = (*X).size();
72 if((*X).size()!=(*Y).size()){
73 std::cout << std::endl << "Error in interp_linear: X, Y need same size" << std::endl;
74 exit(0);
75 }
76
77 for(int i=1; i<nxy-1; i++)
78 if( ((*X)[i] == (*X)[i-1]) ||
79 ((*X)[i] == (*X)[i+1]) ||
80 ((*X)[i]<(*X)[i-1] && (*X)[i]<(*X)[i+1]) ||
81 ((*X)[i]>(*X)[i-1] && (*X)[i]>(*X)[i+1]) ){
82 std::cout << std::endl << "Error in linearInterp: X should be monotonic" << std::endl;
83 exit(0);
84 }
85 }
86};
87
int ilo
lower of adjacent bounding grid points for desired interpolation point
std::vector< double > * X
abscissas for Y(X)
linearInterp(std::vector< double > &_X, std::vector< double > &_Y)
int ihi
upper of adjacent bounding grid points for desired interpolation point
double interp(double x)
void set_bounding_indicies(double x)
int nxy
number of grid points
linearInterp()
Default (empty) constructor.
std::vector< double > * Y
ordinate values for Y(X)