sablib
Loading...
Searching...
No Matches
expand.h
Go to the documentation of this file.
1
6
7#ifndef __SABLIB_EXPAND_H__
8#define __SABLIB_EXPAND_H__
9
10#include <stdexcept>
11#include <Eigen/Eigen>
12
13namespace sablib {
14
22template <typename Derived>
23const typename Derived::PlainObject ExpandBoundaries(const Eigen::MatrixBase<Derived> & y, const unsigned int n)
24{
25 // Although parameters are received as MatrixBase<Derived>, only vector classes are allowed.
26 // Others will be rejected at compile time.
27 static_assert(Derived::IsVectorAtCompileTime, "Error: y is not vector.");
28
29 typename Derived::PlainObject result(y.size() + 2 * n);
30
31 if(y.cols() == 1) {
32 result.block(0, 0, n, 1).fill(y(0));
33 result.block(result.size() - n, 0, n, 1).fill(y(y.size() - 1));
34 result.block(n, 0, y.size(), 1) = y;
35 }
36 else if(y.rows() == 1) {
37 result.block(0, 0, 1, n).fill(y(0));
38 result.block(0, result.size() - n, 1, n).fill(y(y.size() - 1));
39 result.block(0, n, 1, y.size()) = y;
40 }
41
42 return result;
43}
44
53template <typename Derived>
54const typename Derived::PlainObject TrimBoundaries(const Eigen::MatrixBase<Derived> & y, const unsigned int n)
55{
56 // Although parameters are received as MatrixBase<Derived>, only vector classes are allowed.
57 // Others will be rejected at compile time.
58 static_assert(Derived::IsVectorAtCompileTime, "Error: y is not vector.");
59
60 if(y.size() < 2 * n) {
61 throw std::invalid_argument("TrimBoubdaries(): n is too large.");
62 }
63
64 if(y.cols() == 1) {
65 return y.block(n, 0, y.size() - 2 * n, 1);
66 }
67 else if(y.rows() == 1) {
68 return y.block(0, n, 1, y.size() - 2 * n);
69 }
70}
71
72}; // namespace sablib
73
74#endif // __SABLIB_EXPAND_H__
const Derived::PlainObject ExpandBoundaries(const Eigen::MatrixBase< Derived > &y, const unsigned int n)
Expands the boundaries of a vector by padding with the first and last elements.
Definition expand.h:23
const Derived::PlainObject TrimBoundaries(const Eigen::MatrixBase< Derived > &y, const unsigned int n)
Trims the specified number of elements from both ends of a vector.
Definition expand.h:54