votca 2024.2-dev
Loading...
Searching...
No Matches
spline.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team (http://www.votca.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18// Local VOTCA includes
19#include "votca/tools/spline.h"
20
21namespace votca {
22namespace tools {
23
24using namespace std;
25
26Index Spline::GenerateGrid(double min, double max, double h) {
27 Index vec_size = (Index)((max - min) / h + 1.00000001);
28 r_.resize(vec_size);
29 int i;
30
31 double r_init;
32
33 for (r_init = min, i = 0; i < vec_size - 1; r_init += h) {
34 r_[i++] = r_init;
35 }
36 r_[i] = max;
37 return r_.size();
38}
39
40Eigen::VectorXd Spline::Calculate(const Eigen::VectorXd &x) {
41 Eigen::VectorXd y(x.size());
42 for (Index i = 0; i < x.size(); ++i) {
43 y(i) = Calculate(x(i));
44 }
45 return y;
46}
47
48Eigen::VectorXd Spline::CalculateDerivative(const Eigen::VectorXd &x) {
49 Eigen::VectorXd y(x.size());
50 for (Index i = 0; i < x.size(); ++i) {
51 y(i) = CalculateDerivative(x(i));
52 }
53 return y;
54}
55
56void Spline::Print(std::ostream &out, double interval) {
57 for (double x = r_[0]; x < r_[r_.size() - 1]; x += interval) {
58 out << x << " " << Calculate(x) << "\n";
59 }
60}
61
63 if (r < r_[0]) {
64 return 0;
65 }
66 if (r > r_[r_.size() - 2]) {
67 return r_.size() - 2;
68 }
69 Index i;
70 for (i = 0; i < r_.size(); ++i) {
71 if (r_[i] > r) {
72 break;
73 }
74 }
75 return i - 1;
76}
77
78double Spline::getGridPoint(int i) {
79 if (i >= r_.size()) {
80 return 0;
81 }
82 return r_[i];
83}
84
85} // namespace tools
86} // namespace votca
void Print(std::ostream &out, double interval)
Print spline values (using Calculate()) on output "out" on the entire grid in steps of "interval".
Definition spline.cc:56
virtual double CalculateDerivative(double x)=0
Calculate y value for a given x value on the derivative of the spline created by function Interpolate...
virtual double Calculate(double x)=0
Calculate spline function value for a given x value on the spline created by Interpolate() or Fit()
Eigen::VectorXd r_
Definition spline.h:173
Index getInterval(double r)
Determine the index of the interval containing value r.
Definition spline.cc:62
double getGridPoint(int i)
Get the grid point of certain index.
Definition spline.cc:78
Index GenerateGrid(double min, double max, double h)
Generate the grid for fitting from "min" to "max" in steps of "h".
Definition spline.cc:26
STL namespace.
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26