sablib
Loading...
Searching...
No Matches
whittaker.h
Go to the documentation of this file.
1
10
11#ifndef __SABLIB_WHITTAKER_H__
12#define __SABLIB_WHITTAKER_H__
13
14#include <stdexcept>
15#include <vector>
16
17#include "../misc/diff.h"
18
19namespace sablib {
20
31const std::vector<double> Whittaker(
32 const std::vector<double> & y, const std::vector<double> & w,
33 const double lambda, const unsigned int s = 2
34);
35
44const std::vector<double> Whittaker(
45 const std::vector<double> & y, const double lambda, const unsigned int s = 2
46);
47
56template <typename Derived>
57const typename Derived::PlainObject
59 const Eigen::MatrixBase<Derived> & y,
60 const Eigen::MatrixBase<Derived> & w,
61 const Eigen::SparseMatrix<typename Derived::PlainObject::Scalar> & lambdaDTD
62)
63{
64 // Although parameters are received as MatrixBase<Derived>, only vector classes are allowed.
65 // Others will be rejected at compile time.
66 static_assert(Derived::IsVectorAtCompileTime, "Error: y and w are not vector.");
67
68 using Scalar = typename Derived::PlainObject::Scalar;
69
70 Eigen::VectorXd z;
71 Eigen::SparseMatrix<typename Derived::PlainObject::Scalar> W;
72 Eigen::SimplicialCholesky< Eigen::SparseMatrix<Scalar> > solver;
73
74 W = w.asDiagonal();
75 solver.compute(W + lambdaDTD);
76
77 if(solver.info() != Eigen::Success) {
78 throw std::runtime_error("Whittaker(): solver calculation fails.");
79 }
80
81 z = solver.solve(W * y);
82
83 return z;
84}
85
86}; // namespace sablib
87
88#endif // __SABLIB_WHITTAKER_H__
MATLAB-like diff() function.
const std::vector< double > Whittaker(const std::vector< double > &y, const std::vector< double > &w, const double lambda, const unsigned int s)
Performs Whittaker smoothing (std::vector<double> version, with weights).
Definition whittaker.cpp:14