SOPT
Sparse OPTimisation
chained_operators.h
Go to the documentation of this file.
1 #ifndef SOPT_CHAINED_OPERATORS_H
2 #define SOPT_CHAINED_OPERATORS_H
3 
4 #include "sopt/config.h"
5 #include <memory>
6 #include <type_traits>
7 #include <vector>
8 #include "sopt/types.h"
9 
10 namespace sopt {
11 template <typename T0, typename... T>
13  if (sizeof...(args) == 0) return arg0;
14 
15  std::vector<OperatorFunction<T0>> const funcs = {arg0, args...};
16  const std::shared_ptr<T0> buffer = std::make_shared<T0>();
17  OperatorFunction<T0> result = [funcs, buffer](T0 &output, T0 const &input) -> void {
18  auto first = funcs.rbegin();
19  auto const last = funcs.rend();
20  if (funcs.size() == 1)
21  (*first)(output, input);
22  else if (funcs.size() % 2 == 1)
23  (*first)(output, input);
24  else {
25  (*first)(*buffer, input);
26  first++;
27  (*first)(output, *buffer);
28  }
29  for (++first; first != last; first++) {
30  (*first)(*buffer, output);
31  first++;
32  (*first)(output, *buffer);
33  }
34  };
35  return result;
36 }
37 } // namespace sopt
38 #endif
OperatorFunction< T0 > chained_operators(OperatorFunction< T0 > const &arg0, T const &... args)
std::function< void(VECTOR &, VECTOR const &)> OperatorFunction
Typical function out = A*x.
Definition: types.h:43