Alexandria
2.16
Please provide a description of the project.
MathUtils
src
lib
interpolation
linear.cpp
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2012-2020 Euclid Science Ground Segment
3
*
4
* This library is free software; you can redistribute it and/or modify it under
5
* the terms of the GNU Lesser General Public License as published by the Free
6
* Software Foundation; either version 3.0 of the License, or (at your option)
7
* any later version.
8
*
9
* This library is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12
* details.
13
*
14
* You should have received a copy of the GNU Lesser General Public License
15
* along with this library; if not, write to the Free Software Foundation, Inc.,
16
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
*/
18
25
#include <limits>
26
#include "
ElementsKernel/Exception.h
"
27
#include "
MathUtils/interpolation/interpolation.h
"
28
#include "
MathUtils/function/Polynomial.h
"
29
#include "
MathUtils/function/Piecewise.h
"
30
31
namespace
Euclid
{
32
namespace
MathUtils {
33
34
std::unique_ptr<Function>
linearInterpolation
(
const
std::vector<double>
& x,
const
std::vector<double>
& y,
35
bool
extrapolate) {
36
std::vector<std::shared_ptr<Function>
> functions {};
37
for
(
size_t
i=0; i<x.
size
()-1; i++) {
38
double
coef1 = (y[i+1]-y[i]) / (x[i+1]-x[i]);
39
double
coef0 = y[i] - coef1*x[i];
40
functions.
push_back
(
std::shared_ptr<Function>
(
new
Polynomial
{{coef0,coef1}}));
41
}
42
43
if
(extrapolate) {
44
std::vector<double>
x_copy(x);
45
x_copy.
front
() =
std::numeric_limits<double>::lowest
();
46
x_copy.
back
() =
std::numeric_limits<double>::max
();
47
return
std::unique_ptr<Function>
(
new
Piecewise
{x_copy,
std::move
(functions)});
48
}
49
50
return
std::unique_ptr<Function>
(
new
Piecewise
{x,
std::move
(functions)});
51
}
52
53
}
// End of MathUtils
54
}
// end of namespace Euclid
std::shared_ptr
STL class.
std::move
T move(T... args)
interpolation.h
std::vector< double >
std::vector::size
T size(T... args)
Euclid::MathUtils::Piecewise
Represents a piecewise function.
Definition:
Piecewise.h:48
std::vector::back
T back(T... args)
std::vector::front
T front(T... args)
Piecewise.h
std::numeric_limits::lowest
T lowest(T... args)
std::vector::push_back
T push_back(T... args)
Euclid::MathUtils::Polynomial
Represents a polynomial function.
Definition:
Polynomial.h:43
Exception.h
Euclid::MathUtils::linearInterpolation
std::unique_ptr< Function > linearInterpolation(const std::vector< double > &x, const std::vector< double > &y, bool extrapolate)
Performs linear interpolation for the given set of data points.
Definition:
linear.cpp:34
Polynomial.h
std::numeric_limits::max
T max(T... args)
std::unique_ptr
STL class.
Euclid
Definition:
InstOrRefHolder.h:29
Generated by
1.8.18