generic_matrix.cpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 #include "generic_matrix.hpp"
26 
27 namespace casadi {
28 
29 
30  double index_interp1d(const std::vector<double>& x, double xq, bool equidistant) {
31 
32  if (equidistant) {
33  double delta = x[1]-x[0];
34  double i = (xq-x[0])/delta;
35  double imax = static_cast<double>(x.size()-1);
36  return std::max(std::min(i, imax), 0.0);
37 
38  } else {
39  std::vector<double>::const_iterator it = std::lower_bound(x.begin(), x.end(), xq);
40 
41  // End of x
42  if (it==x.end()) return static_cast<double>(x.size()-1);
43 
44  // Start of x
45  if (it==x.begin()) return 0.0;
46 
47  casadi_int i = std::distance(x.begin(), it);
48 
49  // Exactly on an entry
50  if (*it == xq) return static_cast<double>(i);
51 
52  double b = *it;
53  double a = *(it-1);
54 
55  return static_cast<double>(i)+(xq-b)/(b-a);
56  }
57  }
58 
59 } // namespace casadi
The casadi namespace.
Definition: archiver.cpp:28
double index_interp1d(const std::vector< double > &x, double xq, bool equidistant)