ROL
ROL_LinearOperatorSum.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_LINEAROPERATORSUM_H
11 #define ROL_LINEAROPERATORSUM_H
12 
13 #include "ROL_LinearOperator.hpp"
14 
22 namespace ROL {
23 
24 template <class Real>
25 class LinearOperatorSum : public LinearOperator<Real> {
26 
27  typedef Vector<Real> V;
29 
30  typedef typename std::vector<ROL::Ptr<OP> >::size_type size_type;
31 
32 private:
33 
34  ROL::Ptr<std::vector<ROL::Ptr<OP> > > ops_;
35  ROL::Ptr<V> scratch_;
36 
37 public:
38 
39  LinearOperatorSum( ROL::Ptr<OP> &A,
40  ROL::Ptr<OP> &B,
41  ROL::Ptr<V> & scratch ) :
42  scratch_(scratch) {
43  ops_ = ROL::makePtr<std::vector<OP> >>();
44  ops_->push_back(A);
45  ops_->push_back(B);
46  }
47 
48  LinearOperatorSum( ROL::Ptr<OP> &A,
49  ROL::Ptr<OP> &B,
50  ROL::Ptr<OP> &C,
51  ROL::Ptr<V> & scratch ) :
52  scratch_(scratch) {
53  ops_ = ROL::makePtr<std::vector<OP> >>();
54  ops_->push_back(A);
55  ops_->push_back(B);
56  ops_->push_back(C);
57  }
58 
59  // TODO: implementation for arbitrary sum
60 
61  virtual void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
62  for( size_type i=0; i<ops_->size(); ++i ) {
63  (*ops_)[i]->update(x,flag,true);
64  }
65  }
66 
67  virtual void apply( Vector<Real> &Hv, const Vector<Real> &v, Real &tol ) const {
68  (*ops_)[0]->apply(Hv,v,tol);
69  for( size_type i=1; i<ops_->size(); ++i ) {
70  (*ops_)[i]->apply(*scratch_,v,tol);
71  Hv.plus(*scratch_);
72  }
73  }
74 
75  virtual void applyInverse( Vector<Real> &Hv, const Vector<Real> &v, Real &tol ) const {
76  ROL_TEST_FOR_EXCEPTION( true, std::invalid_argument,
77  ">>> ERROR (ROL_LinearOperatorSum, applyInverse): "
78  "Inverse is not defined for general sum of operators.");
79  }
80 
81 }; // class LinearOperatorSum
82 
83 } // namespace ROL
84 
85 #endif // ROL_LINEAROPERATOR_PRODUCT_H
virtual void plus(const Vector &x)=0
Compute , where .
virtual void applyInverse(Vector< Real > &Hv, const Vector< Real > &v, Real &tol) const
Apply inverse of linear operator.
std::vector< ROL::Ptr< OP > >::size_type size_type
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
virtual void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update linear operator.
LinearOperatorSum(ROL::Ptr< OP > &A, ROL::Ptr< OP > &B, ROL::Ptr< V > &scratch)
ROL::Ptr< std::vector< ROL::Ptr< OP > > > ops_
Provides the interface to apply a linear operator.
virtual void apply(Vector< Real > &Hv, const Vector< Real > &v, Real &tol) const
Apply linear operator.
LinearOperator< Real > OP
Provides the interface to sum of linear operators applied to a vector
LinearOperatorSum(ROL::Ptr< OP > &A, ROL::Ptr< OP > &B, ROL::Ptr< OP > &C, ROL::Ptr< V > &scratch)