SOPT
Sparse OPTimisation
wrapper.h
Go to the documentation of this file.
1 #ifndef SOPT_WRAP
2 #define SOPT_WRAP
3 
4 #include <array>
5 #include <type_traits>
6 #include <utility> // for std::move<>
7 
8 #include "sopt/config.h"
9 #include "sopt/exception.h"
10 #include "sopt/maths.h"
11 #include "sopt/types.h"
12 
13 namespace sopt::details {
15 template <typename FUNCTION, typename DERIVED>
16 class AppliedFunction : public Eigen::ReturnByValue<AppliedFunction<FUNCTION, DERIVED>> {
17  public:
18  using PlainObject = typename DERIVED::PlainObject;
19  using Index = typename DERIVED::Index;
20 
21  AppliedFunction(FUNCTION const &func, DERIVED const &x, Index rows)
22  : func(func), x(x), rows_(rows) {}
23  AppliedFunction(FUNCTION const &func, DERIVED const &x) : func(func), x(x), rows_(x.rows()) {}
24  AppliedFunction(AppliedFunction const &c) : func(c.func), x(c.x), rows_(c.rows_) {}
25  AppliedFunction(AppliedFunction &&c) : func(std::move(c.func)), x(c.x), rows_(c.rows_) {}
26 
27  template <typename DESTINATION>
28  void evalTo(DESTINATION &destination) const {
29  func(destination, x);
30  }
31 
32  Index rows() const { return rows_; }
33  Index cols() const { return x.cols(); }
34 
35  private:
36  FUNCTION const func;
37  DERIVED const &x;
38  Index const rows_;
39 };
40 
45 template <typename VECTOR>
46 class WrapFunction {
47  public:
50 
56  WrapFunction(t_Function const &func, std::array<t_int, 3> sizes = {{1, 1, 0}})
57  : func(func), sizes_(sizes) {
58  // cannot devide by zero
59  assert(sizes_[1] != 0);
60  }
61  WrapFunction(WrapFunction const &c) : func(c.func), sizes_(c.sizes_) {}
62  WrapFunction(WrapFunction const &&c) : func(std::move(c.func)), sizes_(std::move(c.sizes_)) {}
63  void operator=(WrapFunction const &c) {
64  func = c.func;
65  sizes_ = c.sizes_;
66  }
67  void operator=(WrapFunction &&c) {
68  func = std::move(c.func);
69  sizes_ = std::move(c.sizes_);
70  }
71 
73  template <typename T0>
75  Eigen::ArrayBase<T0> const &x) const {
77  }
78 
80  template <typename T0>
82  Eigen::ArrayBase<T0> const &x) const {
84  }
85 
87  template <typename T0>
89  Eigen::MatrixBase<T0> const &x) const {
91  }
92 
94  template <typename T0>
96  Eigen::MatrixBase<T0> const &x) const {
98  }
99 
106  std::array<t_int, 3> const &sizes() const { return sizes_; }
107 
109  template <typename T>
110  typename std::enable_if<std::is_integral<T>::value, T>::type rows(T xsize) const {
111  auto const result = (static_cast<t_int>(xsize) * sizes_[0]) / sizes_[1] + sizes_[2];
112  assert(result >= 0);
113  return static_cast<T>(result);
114  }
115 
116  protected:
117  template <typename T>
118  t_uint rows(Eigen::DenseBase<T> const &x) const {
119  return rows(x.size());
120  }
121 
122  private:
124  t_Function func;
126  std::array<t_int, 3> sizes_;
127 };
128 
130 template <typename VECTOR>
132  std::array<t_int, 3> sizes = {{1, 1, 0}}) {
133  return WrapFunction<VECTOR>(func, sizes);
134 }
135 } // namespace sopt::details
136 
137 namespace Eigen::internal {
138 template <typename FUNCTION, typename VECTOR>
139 struct traits<sopt::details::AppliedFunction<FUNCTION, VECTOR>> {
140  using ReturnType = typename VECTOR::PlainObject;
141 };
142 } // namespace Eigen::internal
143 #endif
Expression referencing the result of a function call.
Definition: wrapper.h:16
typename DERIVED::PlainObject PlainObject
Definition: wrapper.h:18
void evalTo(DESTINATION &destination) const
Definition: wrapper.h:28
AppliedFunction(AppliedFunction const &c)
Definition: wrapper.h:24
AppliedFunction(FUNCTION const &func, DERIVED const &x, Index rows)
Definition: wrapper.h:21
AppliedFunction(FUNCTION const &func, DERIVED const &x)
Definition: wrapper.h:23
typename DERIVED::Index Index
Definition: wrapper.h:19
AppliedFunction(AppliedFunction &&c)
Definition: wrapper.h:25
Wraps an std::function to return an expression.
Definition: wrapper.h:46
std::enable_if< std::is_integral< T >::value, T >::type rows(T xsize) const
Output vector size for a input with xsize elements.
Definition: wrapper.h:110
OperatorFunction< VECTOR > t_Function
Type of function wrapped here.
Definition: wrapper.h:49
void operator=(WrapFunction const &c)
Definition: wrapper.h:63
AppliedFunction< t_Function const &, Eigen::MatrixBase< T0 > > operator()(Eigen::MatrixBase< T0 > const &x) const
Function application form.
Definition: wrapper.h:88
WrapFunction(t_Function const &func, std::array< t_int, 3 > sizes={{1, 1, 0}})
Definition: wrapper.h:56
std::array< t_int, 3 > const & sizes() const
Defines relation-ship between input and output sizes.
Definition: wrapper.h:106
WrapFunction(WrapFunction const &&c)
Definition: wrapper.h:62
AppliedFunction< t_Function const &, Eigen::ArrayBase< T0 > > operator*(Eigen::ArrayBase< T0 > const &x) const
Multiplication application form.
Definition: wrapper.h:81
void operator=(WrapFunction &&c)
Definition: wrapper.h:67
AppliedFunction< t_Function const &, Eigen::MatrixBase< T0 > > operator*(Eigen::MatrixBase< T0 > const &x) const
Multiplication application form.
Definition: wrapper.h:95
WrapFunction(WrapFunction const &c)
Definition: wrapper.h:61
AppliedFunction< t_Function const &, Eigen::ArrayBase< T0 > > operator()(Eigen::ArrayBase< T0 > const &x) const
Function application form.
Definition: wrapper.h:74
WrapFunction< VECTOR > wrap(OperatorFunction< VECTOR > const &func, std::array< t_int, 3 > sizes={{1, 1, 0}})
Helper function to wrap functor into expression-able object.
Definition: wrapper.h:131
int t_int
Root of the type hierarchy for signed integers.
Definition: types.h:13
size_t t_uint
Root of the type hierarchy for unsigned integers.
Definition: types.h:15
std::function< void(VECTOR &, VECTOR const &)> OperatorFunction
Typical function out = A*x.
Definition: types.h:43