TSFNonmemberOpHelpersImpl.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 /* ***********************************************************************
00003 // 
00004 //           TSFExtended: Trilinos Solver Framework Extended
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // **********************************************************************/
00027  /* @HEADER@ */
00028 
00029 #ifndef TSFNONMEMBEROPHELPERS_IMPL_HPP
00030 #define TSFNONMEMBEROPHELPERS_IMPL_HPP
00031 
00032 
00033 #include "TSFMultiVectorOperator.hpp"
00034 #include "TSFCommonOperatorsDecl.hpp"
00035 #include "Thyra_DefaultZeroLinearOp.hpp"
00036 #include "Thyra_DefaultDiagonalLinearOp.hpp"
00037 #include "Thyra_DefaultAddedLinearOp.hpp"
00038 #include "Thyra_DefaultMultipliedLinearOp.hpp"
00039 #include "Thyra_DefaultScaledAdjointLinearOp.hpp"
00040 #include "Thyra_DefaultIdentityLinearOp.hpp"
00041 #include "SundanceOut.hpp"
00042 
00043 
00044 #ifndef HAVE_TEUCHOS_EXPLICIT_INSTANTIATION
00045 #include "TSFCommonOperatorsImpl.hpp"
00046 #include "TSFLinearOperatorImpl.hpp"
00047 #endif
00048 
00049 namespace TSFExtended
00050 {
00051 
00052 template <class Scalar> inline
00053 LinearOperator<Scalar> zeroOperator(
00054   const VectorSpace<Scalar>& domain,
00055   const VectorSpace<Scalar>& range)
00056 {
00057   RCP<LinearOpBase<Scalar> > op 
00058     = rcp(new SimpleZeroOp<Scalar>(domain, range));
00059 
00060   return op;
00061 }
00062 
00063 
00064 template <class Scalar> inline
00065 LinearOperator<Scalar> identityOperator(
00066   const VectorSpace<Scalar>& space)
00067 {
00068   RCP<LinearOpBase<Scalar> > op 
00069     = rcp(new SimpleIdentityOp<Scalar>(space));
00070 
00071   return op;
00072 }
00073 
00074 
00075 template <class Scalar> inline
00076 LinearOperator<Scalar> diagonalOperator(
00077   const Vector<Scalar>& vec)
00078 {
00079   RCP<LinearOpBase<Scalar> > op 
00080     = rcp(new DefaultDiagonalLinearOp<Scalar>(vec.ptr()));
00081 
00082   return op;
00083 }
00084 
00085 
00086 
00087 template <class Scalar> inline
00088 LinearOperator<Scalar> composedOperator(
00089   const Array<LinearOperator<Scalar> >& ops)
00090 {
00091   /* We will strip out any identity operators, and if we find a zero
00092   * operator the whole works becomes a zero operator */ 
00093   Array<LinearOperator<Scalar> > strippedOps;
00094 
00095   for (int i=0; i<ops.size(); i++)
00096   {
00097     LinearOperator<Scalar> op_i = ops[i];
00098 
00099     /* if a factor is zero, the whole operator is
00100      * a zero operator */
00101     const SimpleZeroOp<Scalar>* zPtr 
00102       = dynamic_cast<const SimpleZeroOp<Scalar>*>(op_i.ptr().get());
00103 
00104     if (zPtr != 0) 
00105     {
00106       VectorSpace<Scalar> r = ops[0].range();
00107       VectorSpace<Scalar> d = ops[ops.size()-1].domain();
00108       return zeroOperator(d, r);
00109     }
00110 
00111     /* if a factor is the identity, skip it */
00112     const SimpleIdentityOp<Scalar>* IPtr 
00113       = dynamic_cast<const SimpleIdentityOp<Scalar>*>(op_i.ptr().get());  
00114     if (IPtr != 0) 
00115     {
00116       continue;
00117     }
00118 
00119     strippedOps.append(op_i);
00120   }
00121   
00122   TEST_FOR_EXCEPT(strippedOps.size() < 1);
00123   if (strippedOps.size()==1) return strippedOps[0];
00124   
00125   RCP<LinearOpBase<Scalar> > op 
00126     = rcp(new SimpleComposedOp<Scalar>(strippedOps));
00127   return op;
00128 }
00129 
00130 
00131 
00132 
00133 
00134 template <class Scalar> inline
00135 LinearOperator<Scalar> addedOperator(
00136   const Array<LinearOperator<Scalar> >& ops)
00137 {
00138   /* We will strip out any zero operators */
00139   Array<LinearOperator<Scalar> > strippedOps;
00140 
00141   for (int i=0; i<ops.size(); i++)
00142   {
00143     LinearOperator<Scalar> op_i = ops[i];
00144 
00145     /* Ignore any zero operators */
00146     const SimpleZeroOp<Scalar>* zPtr 
00147       = dynamic_cast<const SimpleZeroOp<Scalar>*>(op_i.ptr().get());
00148 
00149     if (zPtr != 0) continue;
00150 
00151     strippedOps.append(op_i);
00152   }
00153   
00154   TEST_FOR_EXCEPT(strippedOps.size() < 1);
00155   if (strippedOps.size()==1) return strippedOps[0];
00156   
00157   RCP<LinearOpBase<Scalar> > op 
00158     = rcp(new SimpleAddedOp<Scalar>(strippedOps));
00159   
00160   return op;
00161 }
00162 
00163 
00164 
00165 template <class Scalar> inline
00166 LinearOperator<Scalar> scaledOperator(
00167   const Scalar& scale,
00168   const LinearOperator<Scalar>& op)
00169 {
00170   RCP<LinearOpBase<Scalar> > A 
00171     = rcp(new DefaultScaledAdjointLinearOp<Scalar>(scale, Thyra::NOTRANS, op.ptr()));
00172 
00173   return A;
00174 }
00175 
00176 
00177 
00178 template <class Scalar> inline
00179 LinearOperator<Scalar> scaledTransposedOperator(
00180   const Scalar& scale,
00181   const LinearOperator<Scalar>& op)
00182 {
00183   RCP<LinearOpBase<Scalar> > A 
00184     = rcp(new DefaultScaledAdjointLinearOp<Scalar>(scale, Thyra::TRANS, op.ptr()));
00185 
00186   return A;
00187 }
00188 
00189 
00190 
00191 template <class Scalar> inline
00192 LinearOperator<Scalar> transposedOperator(
00193   const LinearOperator<Scalar>& op)
00194 {
00195 
00196   /* If the operator is a transpose, return the untransposed op */
00197   const SimpleTransposedOp<Scalar>* tPtr
00198     = dynamic_cast<const SimpleTransposedOp<Scalar>*>(op.ptr().get());
00199   if (tPtr)
00200   {
00201     return tPtr->op();
00202   }
00203 
00204   /* If the operator is zero, return a transposed zero */
00205   const SimpleZeroOp<Scalar>* zPtr 
00206     = dynamic_cast<const SimpleZeroOp<Scalar>*>(op.ptr().get());
00207 
00208   if (zPtr != 0) 
00209   {
00210     VectorSpace<Scalar> r = op.range();
00211     VectorSpace<Scalar> d = op.domain();
00212     return zeroOperator(r, d);
00213   }
00214 
00215 
00216   /* Return a transposed operator */
00217   RCP<LinearOpBase<Scalar> > A
00218     = rcp(new SimpleTransposedOp<Scalar>(op));
00219       
00220   return A;
00221 }
00222 
00223   
00224 template <class Scalar> inline
00225 LinearOperator<Scalar> multiVectorOperator(
00226   const Teuchos::Array<Vector<Scalar> >& cols,
00227   const VectorSpace<Scalar>& domain)
00228 {
00229   RCP<LinearOpBase<Scalar> > A
00230     = rcp(new MultiVectorOperator<Scalar>(cols, domain));
00231 
00232   return A;
00233 }
00234 
00235  
00236   
00237 }
00238 
00239 
00240 #endif

Site Contact