Repository navigation
Expand file tree
/
Copy pathfit.hpp
More file actions
68 lines (56 loc) · 2.02 KB
/
Copy pathfit.hpp
File metadata and controls
68 lines (56 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/// Copyright 2016 Thomas E. Vaughan
///
/// The present software is redistributable under the terms of the GNU LESSER
/// GENERAL PUBLIC LICENSE, which must be distributed in the file, 'LICENSE',
/// along with the software.
///
/// \file fit.hpp
/// \brief Declaration of linreg::fit.
#ifndef LINREG_FIT_HPP
#define LINREG_FIT_HPP
#include <memory> // for shared_ptr<>
#include <Eigen/SVD> // for MatrixX2d, VectorXd
#include "basis.hpp" // for abstract_basis
namespace linreg
{
/// Method of finding coefficients of linear regression.
enum fit_solution {
/// Invert the square matrix obtained by differentiating the sum of
/// squared deviations with respect to each coefficient, by setting each
/// resultant equation to zero, and expressing the result as a matrix
/// equation.
FIT_SIMPLE,
/// Find the best fitting coeffiecients by way of the singular-value
/// decomposition of the rectangular matrix that is a factor of the
/// square matrix described above.
FIT_SVD
};
/// Best coefficients for fitting a set of basis functions to a collection
/// of data points; an instance of class fit also serves as the
/// corresponding function object.
class fit
{
typedef std::shared_ptr<abstract_basis const> basis_ptr; ///< Short hand.
basis_ptr basis_; ///< Shared pointer to basis.
Eigen::VectorXd coefs_; ///< Best-fit coefficients.
public:
/// Construct from basis, data, and (optionally) the method of fit.
fit(basis_ptr b, Eigen::MatrixX2d const &d, fit_solution s = FIT_SVD);
/// \return Shared pointer to basis.
basis_ptr basis() const
{
return basis_;
}
/// \return Reference to best-fit coefficients.
Eigen::VectorXd const &coefs() const
{
return coefs_;
}
/// \return Value of best-fit function at specified argument.
double operator()(double x) const
{
return coefs_.dot((*basis_)(x));
}
};
}
#endif // ndef LINREG_FIT_HPP