interpm_neigh.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2016, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 #ifndef O2SCL_INTERPM_NEIGH_H
24 #define O2SCL_INTERPM_NEIGH_H
25 
26 /** \file interpm_neigh.h
27  \brief File defining \ref o2scl::interpm_neigh
28 */
29 
30 #include <iostream>
31 #include <string>
32 #include <cmath>
33 
34 #include <o2scl/err_hnd.h>
35 
36 #ifndef DOXYGEN_NO_O2NS
37 namespace o2scl {
38 #endif
39 
40  /** \brief Nearest-neighbor interpolation in arbitrary number of
41  dimensions
42 
43  This class performs nearest-neighbor interpolation on a
44  multi-dimensional data set specified as a series of scattered
45  points. The function \ref set_data() takes as input: the number
46  of dimensions, the number of points which specify the data, and
47  a "vector of vectors", e.g. <tt>std::vector<std::vector<double>
48  ></tt> which contains the data for all the points.
49  */
50  template<class vec_t> class interpm_neigh {
51 
52  public:
53 
56 
57  interpm_neigh() {
58  data_set=false;
59  scales.resize(1);
60  scales[0]=1.0;
61  }
62 
63  /// Distance scales for each coordinate
64  ubvector scales;
65 
66  /** \brief Initialize the data for the interpolation
67 
68  The object \c vecs should be a vector (of size <tt>dim+1</tt>)
69  of vectors (all of size <tt>n_points</tt>). It may have any
70  type for which the data can be accessed through
71  <tt>operator[][]</tt>.
72  */
73  template<class vec_vec_t>
74  void set_data(size_t dim, size_t n_points, vec_vec_t &vecs) {
75 
76  if (n_points<1) {
77  O2SCL_ERR2("Must provide at least one point in ",
78  "interpm_neigh::set_data()",exc_efailed);
79  }
80 
81  np=n_points;
82  nd=dim;
83  ptrs.resize(dim+1);
84  for(size_t i=0;i<dim+1;i++) {
85  ptrs[i]=&vecs[i];
86  }
87  data_set=true;
88 
89  return;
90  }
91 
92  /** \brief Perform the interpolation
93  */
94  template<class vec2_t> double operator()(vec2_t &x) const {
95  return eval(x);
96  }
97 
98  /** \brief Perform the interpolation
99  */
100  template<class vec2_t> double eval(vec2_t &x) const {
101 
102  if (data_set==false) {
103  O2SCL_ERR("Data not set in interp_planar::eval_points().",
104  exc_einval);
105  }
106 
107  // Check that some scales have been given
108  size_t nscales=scales.size();
109  if (nscales==0) {
110  O2SCL_ERR("No scales in interpm_neigh::eval().",exc_einval);
111  }
112 
113  // Exhaustively search the data
114  size_t index_min=0;
115  double dist_min=0.0;
116  for(size_t i=0;i<nd;i++) {
117  dist_min+=pow((x[i]-(*(ptrs[i]))[index_min])/scales[i%nscales],2.0);
118  }
119  for(size_t index=1;index<np;index++) {
120  double dist=0.0;
121  for(size_t i=0;i<nd;i++) {
122  dist+=pow((x[i]-(*(ptrs[i]))[index])/scales[i%nscales],2.0);
123  }
124  if (dist<dist_min) {
125  dist_min=dist;
126  index_min=index;
127  }
128  }
129 
130  // Return the function value
131  return (*(ptrs[nd]))[index_min];
132  }
133 
134 #ifndef DOXYGEN_INTERNAL
135 
136  protected:
137 
138  /// The number of points
139  size_t np;
140  /// The number of dimensions
141  size_t nd;
142  /// A vector of pointers holding the data
143  std::vector<vec_t *> ptrs;
144  /// True if the data has been specified
145  bool data_set;
146 
147 #endif
148 
149  };
150 
151 #ifndef DOXYGEN_NO_O2NS
152 }
153 #endif
154 
155 #endif
156 
157 
158 
The main O<span style=&#39;position: relative; top: 0.3em; font-size: 0.8em&#39;>2</span>scl O$_2$scl names...
Definition: anneal.h:42
double eval(vec2_t &x) const
Perform the interpolation.
Nearest-neighbor interpolation in arbitrary number of dimensions.
Definition: interpm_neigh.h:50
size_t np
The number of points.
invalid argument supplied by user
Definition: err_hnd.h:59
bool data_set
True if the data has been specified.
generic failure
Definition: err_hnd.h:61
double operator()(vec2_t &x) const
Perform the interpolation.
Definition: interpm_neigh.h:94
ubvector scales
Distance scales for each coordinate.
Definition: interpm_neigh.h:64
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
Definition: err_hnd.h:281
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
Definition: err_hnd.h:273
size_t nd
The number of dimensions.
void set_data(size_t dim, size_t n_points, vec_vec_t &vecs)
Initialize the data for the interpolation.
Definition: interpm_neigh.h:74
std::vector< vec_t * > ptrs
A vector of pointers holding the data.

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).