Allium  0.1.0
linear_operator.hpp
1 // Copyright 2020 Hannah Rittich
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ALLIUM_LA_LINEAR_OPERATOR_HPP
16 #define ALLIUM_LA_LINEAR_OPERATOR_HPP
17 
18 #include <memory>
19 
20 namespace allium {
24 
28  template <typename V>
30  public:
31  using Vector = V;
32  using Number = typename Vector::Number;
33  using Real = typename Vector::Real;
34 
35  virtual ~LinearOperator() {}
36 
37  virtual void apply(Vector& result,
38  const Vector& arg) = 0;
39  };
40 
44  template <typename V, typename F>
45  class FunctorLinearOperator final : public LinearOperator<V> {
46  public:
47  using typename LinearOperator<V>::Vector;
48 
49  FunctorLinearOperator(const F& fun) : m_fun(fun) {};
50  FunctorLinearOperator(F&& fun) : m_fun(std::move(fun)) {};
51 
52  void apply(Vector& result, const Vector& arg) override {
53  m_fun(result, arg);
54  }
55  private:
56  F m_fun;
57  };
58 
64  template <typename V, typename F>
67  return FunctorLinearOperator<V, std::remove_reference_t<F>>(std::forward<F>(f));
68  }
69 
71 }
72 
73 #endif
Wraps a functor into a linear operator.
Definition: linear_operator.hpp:45
Linear operator for the use in iterative linear solvers.
Definition: linear_operator.hpp:29
FunctorLinearOperator< V, std::remove_reference_t< F > > make_linear_operator(F &&f)
Converts any functor into a LinearOperator.
Definition: linear_operator.hpp:66