Thyra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExampleTridiagSerialLinearOp.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
4 //
5 // Copyright 2004 NTESS and the Thyra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef THYRA_EXAMPLE_TRIDIAG_SERIAL_LINEAR_OP_HPP
11 #define THYRA_EXAMPLE_TRIDIAG_SERIAL_LINEAR_OP_HPP
12 
13 #include "Thyra_LinearOpDefaultBase.hpp"
14 #include "Thyra_DefaultSpmdVectorSpace.hpp"
15 #include "Thyra_DetachedVectorView.hpp"
16 #include "Teuchos_Assert.hpp"
17 
18 
46 template<class Scalar>
48 {
49 public:
50 
53 
56  const Thyra::Ordinal dim,
60  )
61  { this->initialize(dim, lower, diag, upper); }
62 
84  void initialize(
85  const Thyra::Ordinal dim,
89  )
90  {
91  TEUCHOS_TEST_FOR_EXCEPT( dim < 2 );
92  space_ = Thyra::defaultSpmdVectorSpace<Scalar>(dim);
93  lower_ = lower;
94  diag_ = diag;
95  upper_ = upper;
96  }
97 
98 protected:
99 
100  // Overridden from LinearOpBase
101 
104  { return space_; }
105 
108  { return space_; }
109 
111  bool opSupportedImpl(Thyra::EOpTransp M_trans) const
112  { return true; } // This class supports everything!
113 
115  void applyImpl(
116  const Thyra::EOpTransp M_trans,
117  const Thyra::MultiVectorBase<Scalar> &X_in,
119  const Scalar alpha,
120  const Scalar beta
121  ) const;
122 
123 private:
124 
126  Teuchos::Array<Scalar> lower_; // size = dim - 1
127  Teuchos::Array<Scalar> diag_; // size = dim
128  Teuchos::Array<Scalar> upper_; // size = dim - 1
129 
130 }; // end class ExampleTridiagSerialLinearOp
131 
132 
133 template<class Scalar>
135  const Thyra::EOpTransp M_trans,
136  const Thyra::MultiVectorBase<Scalar> &X_in,
138  const Scalar alpha,
139  const Scalar beta
140  ) const
141 {
142 
144  typedef Thyra::Ordinal Ordinal;
145 
146  const Ordinal dim = space_->dim();
147 
148  // Loop over the input columns
149 
150  const Ordinal m = X_in.domain()->dim();
151 
152  for (Ordinal col_j = 0; col_j < m; ++col_j) {
153 
154  // Get access the the elements of column j
155  Thyra::ConstDetachedVectorView<Scalar> x_vec(X_in.col(col_j));
156  Thyra::DetachedVectorView<Scalar> y_vec(Y_inout->col(col_j));
157  const Teuchos::ArrayRCP<const Scalar> x = x_vec.sv().values();
158  const Teuchos::ArrayRCP<Scalar> y = y_vec.sv().values();
159 
160  // Perform y = beta*y (being careful to set y=0 if beta=0 in case y is
161  // uninitialized on input!)
162  if( beta == ST::zero() ) {
163  for( Ordinal k = 0; k < dim; ++k ) y[k] = ST::zero();
164  }
165  else if( beta != ST::one() ) {
166  for( Ordinal k = 0; k < dim; ++k ) y[k] *= beta;
167  }
168 
169  // Perform y = alpha*op(M)*x
170  Ordinal k = 0;
171  if( M_trans == Thyra::NOTRANS ) {
172  y[k] += alpha * ( diag_[k]*x[k] + upper_[k]*x[k+1] ); // First row
173  for( k = 1; k < dim - 1; ++k ) // Middle rows
174  y[k] += alpha * ( lower_[k-1]*x[k-1] + diag_[k]*x[k] + upper_[k]*x[k+1] );
175  y[k] += alpha * ( lower_[k-1]*x[k-1] + diag_[k]*x[k] ); // Last row
176  }
177  else if( M_trans == Thyra::CONJ ) {
178  y[k] += alpha * ( ST::conjugate(diag_[k])*x[k] + ST::conjugate(upper_[k])*x[k+1] );
179  for( k = 1; k < dim - 1; ++k )
180  y[k] += alpha * ( ST::conjugate(lower_[k-1])*x[k-1]
181  + ST::conjugate(diag_[k])*x[k] + ST::conjugate(upper_[k])*x[k+1] );
182  y[k] += alpha * ( ST::conjugate(lower_[k-1])*x[k-1] + ST::conjugate(diag_[k])*x[k] );
183  }
184  else if( M_trans == Thyra::TRANS ) {
185  y[k] += alpha * ( diag_[k]*x[k] + lower_[k]*x[k+1] );
186  for( k = 1; k < dim - 1; ++k )
187  y[k] += alpha * ( upper_[k-1]*x[k-1] + diag_[k]*x[k] + lower_[k]*x[k+1] );
188  y[k] += alpha * ( upper_[k-1]*x[k-1] + diag_[k]*x[k] );
189  }
190  else if( M_trans == Thyra::CONJTRANS ) {
191  y[k] += alpha * ( ST::conjugate(diag_[k])*x[k] + ST::conjugate(lower_[k])*x[k+1] );
192  for( k = 1; k < dim - 1; ++k )
193  y[k] += alpha * ( ST::conjugate(upper_[k-1])*x[k-1]
194  + ST::conjugate(diag_[k])*x[k] + ST::conjugate(lower_[k])*x[k+1] );
195  y[k] += alpha * ( ST::conjugate(upper_[k-1])*x[k-1] + ST::conjugate(diag_[k])*x[k] );
196  }
197  else {
198  TEUCHOS_TEST_FOR_EXCEPT(true); // Throw exception if we get here!
199  }
200  }
201 
202 }
203 
204 
205 #endif // THYRA_EXAMPLE_TRIDIAG_SERIAL_LINEAR_OP_HPP
ExampleTridiagSerialLinearOp(const Thyra::Ordinal dim, const Teuchos::ArrayView< const Scalar > &lower, const Teuchos::ArrayView< const Scalar > &diag, const Teuchos::ArrayView< const Scalar > &upper)
initialize().
EOpTransp
Enumeration for determining how a linear operator is applied. `*.
void initialize(const Thyra::Ordinal dim, const Teuchos::ArrayView< const Scalar > &lower, const Teuchos::ArrayView< const Scalar > &diag, const Teuchos::ArrayView< const Scalar > &upper)
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > domain() const
void applyImpl(const Thyra::EOpTransp M_trans, const Thyra::MultiVectorBase< Scalar > &X_in, const Teuchos::Ptr< Thyra::MultiVectorBase< Scalar > > &Y_inout, const Scalar alpha, const Scalar beta) const
Use the non-transposed operator.
Node subclass that provides a good default implementation for the describe() function.
Create an explicit non-mutable (const) view of a VectorBase object.
Use the transposed operator with complex-conjugate clements (same as TRANS for real scalar types)...
Use the non-transposed operator with complex-conjugate elements (same as NOTRANS for real scalar type...
Use the transposed operator.
Teuchos::Ordinal Ordinal
Type for the dimension of a vector space. `*.
Interface for a collection of column vectors called a multi-vector.
Create an explicit mutable (non-const) view of a VectorBase object.
RCP< const VectorBase< Scalar > > col(Ordinal j) const
Calls colImpl().
bool opSupportedImpl(Thyra::EOpTransp M_trans) const
ExampleTridiagSerialLinearOp()
Construct to uninitialized.
virtual RCP< const VectorSpaceBase< Scalar > > domain() const =0
Return a smart pointer for the domain space for this operator.
Teuchos::RCP< const Thyra::VectorSpaceBase< Scalar > > range() const
Simple example subclass for serial tridiagonal matrices.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)