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#include "sablib_export.h"
17
18#include "../misc/diff.h"
19
20namespace sablib {
21
32SABLIB_EXPORT const std::vector<double> Whittaker(
33 const std::vector<double> & y, const std::vector<double> & w,
34 const double lambda, const unsigned int s = 2
35);
36
45SABLIB_EXPORT const std::vector<double> Whittaker(
46 const std::vector<double> & y, const double lambda, const unsigned int s = 2
47);
48
57template <typename Derived>
58const typename Derived::PlainObject
60 const Eigen::MatrixBase<Derived> & y,
61 const Eigen::MatrixBase<Derived> & w,
62 const Eigen::SparseMatrix<typename Derived::PlainObject::Scalar> & lambdaDTD
63)
64{
65 // Although parameters are received as MatrixBase<Derived>, only vector classes are allowed.
66 // Others will be rejected at compile time.
67 static_assert(Derived::IsVectorAtCompileTime, "Error: y and w are not vector.");
68
69 using Scalar = typename Derived::PlainObject::Scalar;
70
71 Eigen::VectorXd z;
72 Eigen::SparseMatrix<typename Derived::PlainObject::Scalar> W;
73 Eigen::SimplicialCholesky< Eigen::SparseMatrix<Scalar> > solver;
74
75 W = w.asDiagonal();
76 solver.compute(W + lambdaDTD);
77
78 if(solver.info() != Eigen::Success) {
79 throw std::runtime_error("Whittaker(): solver calculation fails.");
80 }
81
82 z = solver.solve(W * y);
83
84 return z;
85}
86
87}; // namespace sablib
88
89#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