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 RCP = Teuchos::RCP<T>;
71  template <typename T> using vector = std::vector<T>;
72 
73 private:
74 
75  const RCP<const vector<Real> > a_; // Diagonal
76  const RCP<const vector<Real> > b_; // Superdiagonal
77  const RCP<const vector<Real> > c_; // Subdiagonal
78 
79  mutable vector<Real> dl_;
80  mutable vector<Real> d_;
81  mutable vector<Real> du_;
82  mutable vector<Real> du2_;
83 
84  mutable vector<int> ipiv_;
85 
86  int N_;
87 
88  Teuchos::LAPACK<int,Real> lapack_;
89 
90  void copy(void) const {
91  for(int i=0;i<N_-1;++i) {
92  dl_[i] = (*c_)[i];
93  d_[i] = (*a_)[i];
94  du_[i] = (*b_)[i];
95  }
96  d_[N_-1] = (*a_)[N_-1];
97  du2_.assign(N_-2,0.0);
98  }
99 
100 public:
101 
103  const RCP<const vector<Real> > &b,
104  const RCP<const vector<Real> > &c ) :
105  StdLinearOperator<Real>(), a_(a), b_(b), c_(c) {
106 
107  N_ = a_->size();
108 
109  dl_.reserve(N_-1);
110  d_.reserve(N_);
111  du_.reserve(N_-1);
112  du2_.reserve(N_-2);
113  ipiv_.reserve(N_);
114  }
115 
117  const RCP<const vector<Real> > &b ) {
118  StrdTridiagonalOperator(a,b,b);
119  }
120 
121 
123 
128 
129 
130  virtual void apply( vector<Real> &Hv, const vector<Real> &v, Real &tol ) const {
131  Hv[0] = (*a_)[0]*v[0] + (*b_)[0]*v[1];
132 
133  for( int i=1; i<N_-1; ++i ) {
134  Hv[i] = (*c_)[i-1]*v[i-1] + (*a_)[i]*v[i] + (*b_)[i]*v[i+1];
135  }
136 
137  Hv[N_-1] = (*a_)[N_-1]*v[N_-1] + (*c_)[N_-2]*v[N_-2];
138 
139  }
140 
141  virtual void applyAdjoint( vector<Real> &Hv, const vector<Real> &v, Real &tol ) const {
142  Hv[0] = (*a_)[0]*v[0] + (*c_)[0]*v[1];
143 
144  for( int i=1; i<N_-1; ++i ) {
145  Hv[i] = (*b_)[i-1]*v[i-1] + (*a_)[i]*v[i] + (*c_)[i]*v[i+1];
146  }
147 
148  Hv[N_-1] = (*a_)[N_-1]*v[N_-1] + (*b_)[N_-2]*v[N_-2];
149  }
150 
151  virtual void applyInverse( vector<Real> &Hv, const vector<Real> &v, Real &tol ) const {
152  Hv = v;
153  copy();
154  int INFO;
155  lapack_.GTTRF(N_,&dl_[0],&d_[0],&du_[0],&du2_[0],&ipiv_[0],&INFO);
156  lapack_.GTTRS('N',N_,1,&dl_[0],&d_[0],&du_[0],&du2_[0],&ipiv_[0],&Hv[0],N_,&INFO);
157 }
158 
159  virtual void applyAdjointInverse( vector<Real> &Hv, const vector<Real> &v, Real &tol ) const {
160  Hv = v;
161  copy();
162  int INFO;
163  lapack_.GTTRF(N_,&dl_[0],&d_[0],&du_[0],&du2_[0],&ipiv_[0],&INFO);
164  lapack_.GTTRS('T',N_,1,&dl_[0],&d_[0],&du_[0],&du2_[0],&ipiv_[0],&Hv[0],N_,&INFO);
165  }
166 
167 }; // class StdTridiagonalOperator
168 
169 } // namespace ROL
170 
171 #endif
Provides the std::vector implementation to apply a linear operator, which is a std::vector representa...
const RCP< const vector< Real > > b_
StdTridiagonalOperator(const RCP< const vector< Real > > &a, const RCP< const vector< Real > > &b)
StdTridiagonalOperator(const RCP< const vector< Real > > &a, const RCP< const vector< Real > > &b, const RCP< const vector< Real > > &c)
Provides the std::vector implementation to apply a linear operator, which encapsulates a tridiagonal ...
virtual void applyAdjoint(vector< Real > &Hv, const vector< Real > &v, Real &tol) const
virtual void applyAdjointInverse(vector< Real > &Hv, const vector< Real > &v, Real &tol) const
Teuchos::LAPACK< int, Real > lapack_
const RCP< const vector< Real > > a_
virtual void applyInverse(vector< Real > &Hv, const vector< Real > &v, Real &tol) const
virtual void apply(vector< Real > &Hv, const vector< Real > &v, Real &tol) const
const RCP< const vector< Real > > c_