ROL
ROL_StdTridiagonalOperator.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #ifndef ROL_STDTRIDIAGONALOPERATOR_H
45 #define ROL_STDTRIDIAGONALOPERATOR_H
46 
48 
65 namespace ROL {
66 
67 template <class Real>
69 
70  template <typename T> using vector = std::vector<T>;
71 
72 private:
73 
74  const ROL::Ptr<const vector<Real> > a_; // Diagonal
75  const ROL::Ptr<const vector<Real> > b_; // Superdiagonal
76  const ROL::Ptr<const vector<Real> > c_; // Subdiagonal
77 
78  mutable vector<Real> dl_;
79  mutable vector<Real> d_;
80  mutable vector<Real> du_;
81  mutable vector<Real> du2_;
82 
83  mutable vector<int> ipiv_;
84 
85  int N_;
86 
87  ROL::LAPACK<int,Real> lapack_;
88 
89  void copy(void) const {
90  for(int i=0;i<N_-1;++i) {
91  dl_[i] = (*c_)[i];
92  d_[i] = (*a_)[i];
93  du_[i] = (*b_)[i];
94  }
95  d_[N_-1] = (*a_)[N_-1];
96  du2_.assign(N_-2,0.0);
97  }
98 
99 public:
100 
101  StdTridiagonalOperator( const ROL::Ptr<const vector<Real> > &a,
102  const ROL::Ptr<const vector<Real> > &b,
103  const ROL::Ptr<const vector<Real> > &c ) :
104  StdLinearOperator<Real>(), a_(a), b_(b), c_(c) {
105 
106  N_ = a_->size();
107 
108  dl_.resize(N_-1);
109  d_.resize(N_);
110  du_.resize(N_-1);
111  du2_.resize(N_-2);
112  ipiv_.resize(N_);
113  }
114 
115  StdTridiagonalOperator( const ROL::Ptr<const vector<Real> > &a,
116  const ROL::Ptr<const vector<Real> > &b ) {
117  StrdTridiagonalOperator(a,b,b);
118  }
119 
120 
122 
127 
128 
129  virtual void apply( vector<Real> &Hv, const vector<Real> &v, Real &tol ) const {
130  Hv[0] = (*a_)[0]*v[0] + (*b_)[0]*v[1];
131 
132  for( int i=1; i<N_-1; ++i ) {
133  Hv[i] = (*c_)[i-1]*v[i-1] + (*a_)[i]*v[i] + (*b_)[i]*v[i+1];
134  }
135 
136  Hv[N_-1] = (*a_)[N_-1]*v[N_-1] + (*c_)[N_-2]*v[N_-2];
137 
138  }
139 
140  virtual void applyAdjoint( vector<Real> &Hv, const vector<Real> &v, Real &tol ) const {
141  Hv[0] = (*a_)[0]*v[0] + (*c_)[0]*v[1];
142 
143  for( int i=1; i<N_-1; ++i ) {
144  Hv[i] = (*b_)[i-1]*v[i-1] + (*a_)[i]*v[i] + (*c_)[i]*v[i+1];
145  }
146 
147  Hv[N_-1] = (*a_)[N_-1]*v[N_-1] + (*b_)[N_-2]*v[N_-2];
148  }
149 
150  virtual void applyInverse( vector<Real> &Hv, const vector<Real> &v, Real &tol ) const {
151  Hv = v;
152  copy();
153  int INFO;
154  lapack_.GTTRF(N_,&dl_[0],&d_[0],&du_[0],&du2_[0],&ipiv_[0],&INFO);
155  lapack_.GTTRS('N',N_,1,&dl_[0],&d_[0],&du_[0],&du2_[0],&ipiv_[0],&Hv[0],N_,&INFO);
156 }
157 
158  virtual void applyAdjointInverse( vector<Real> &Hv, const vector<Real> &v, Real &tol ) const {
159  Hv = v;
160  copy();
161  int INFO;
162  lapack_.GTTRF(N_,&dl_[0],&d_[0],&du_[0],&du2_[0],&ipiv_[0],&INFO);
163  lapack_.GTTRS('T',N_,1,&dl_[0],&d_[0],&du_[0],&du2_[0],&ipiv_[0],&Hv[0],N_,&INFO);
164  }
165 
166 }; // class StdTridiagonalOperator
167 
168 } // namespace ROL
169 
170 #endif
Provides the std::vector implementation to apply a linear operator, which is a std::vector representa...
StdTridiagonalOperator(const ROL::Ptr< const vector< Real > > &a, const ROL::Ptr< const vector< Real > > &b, const ROL::Ptr< const vector< Real > > &c)
virtual void applyAdjoint(vector< Real > &Hv, const vector< Real > &v, Real &tol) const
StdTridiagonalOperator(const ROL::Ptr< const vector< Real > > &a, const ROL::Ptr< const vector< Real > > &b)
virtual void applyInverse(vector< Real > &Hv, const vector< Real > &v, Real &tol) const
Provides the std::vector implementation to apply a linear operator, which encapsulates a tridiagonal ...
const ROL::Ptr< const vector< Real > > c_
virtual void apply(vector< Real > &Hv, const vector< Real > &v, Real &tol) const
virtual void applyAdjointInverse(vector< Real > &Hv, const vector< Real > &v, Real &tol) const
const ROL::Ptr< const vector< Real > > b_
const ROL::Ptr< const vector< Real > > a_