TSFEpetraMatrixMatrixProduct.cpp
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 #include "TSFEpetraMatrix.hpp"
00030 #include "TSFEpetraMatrixMatrixProduct.hpp"
00031 #include "TSFEpetraVector.hpp"
00032 #include "SundanceExceptions.hpp"
00033 #include "EpetraExt_MatrixMatrix.h"
00034 
00035 #ifndef HAVE_TEUCHOS_EXPLICIT_INSTANTIATION
00036 #include "TSFVectorImpl.hpp"
00037 #include "TSFLinearOperatorImpl.hpp"
00038 #endif
00039 
00040 
00041 
00042 namespace TSFExtended
00043 {
00044 using namespace Teuchos;
00045 using Sundance::RuntimeError;
00046 
00047 
00048 LinearOperator<double> epetraLeftScale(
00049   const Vector<double>& d,
00050   const LinearOperator<double>& A)
00051 {
00052   /* Extract the underlying Epetra matrix. Type checking is done
00053    * ny rcp_dynamic_cast, so we need no error check here. */
00054   RCP<const Epetra_CrsMatrix> A_crs = EpetraMatrix::getConcretePtr(A);
00055   
00056   /* Make a deep copy of A */
00057   RCP<Epetra_CrsMatrix> mtxCopy = rcp(new Epetra_CrsMatrix(*A_crs));
00058 
00059   /* Extract the underlying Epetra vector. Type checking is done
00060    * internally, so we need no error check here. */
00061   const Epetra_Vector& epv = EpetraVector::getConcrete(d);
00062   
00063   /* Scale the copy */
00064   mtxCopy->LeftScale(epv);
00065 
00066   /* Prepare an operator object for the scaled matrix */
00067   RCP<const EpetraVectorSpace> domain 
00068     = rcp_dynamic_cast<const EpetraVectorSpace>(A.domain().ptr());
00069 
00070   RCP<const EpetraVectorSpace> range 
00071     = rcp_dynamic_cast<const EpetraVectorSpace>(A.range().ptr());
00072 
00073   RCP<LinearOpBase<double> > rtn 
00074     = rcp(new EpetraMatrix(mtxCopy, domain, range));
00075   return rtn;
00076   
00077 }
00078 
00079 LinearOperator<double> epetraRightScale(
00080   const LinearOperator<double>& A,
00081   const Vector<double>& d)
00082 {
00083   /* Extract the underlying Epetra matrix. Type checking is done
00084    * ny rcp_dynamic_cast, so we need no error check here. */
00085   RCP<const Epetra_CrsMatrix> A_crs = EpetraMatrix::getConcretePtr(A);
00086   
00087   /* Make a deep copy of A */
00088   RCP<Epetra_CrsMatrix> mtxCopy = rcp(new Epetra_CrsMatrix(*A_crs));
00089 
00090   /* Extract the underlying Epetra vector. Type checking is done
00091    * internally, so we need no error check here. */
00092   const Epetra_Vector& epv = EpetraVector::getConcrete(d);
00093   
00094   /* Scale the copy */
00095   mtxCopy->RightScale(epv);
00096 
00097   /* Prepare an operator object for the scaled matrix */
00098   RCP<const EpetraVectorSpace> domain 
00099     = rcp_dynamic_cast<const EpetraVectorSpace>(A.domain().ptr());
00100 
00101   RCP<const EpetraVectorSpace> range 
00102     = rcp_dynamic_cast<const EpetraVectorSpace>(A.range().ptr());
00103 
00104   RCP<LinearOpBase<double> > rtn 
00105     = rcp(new EpetraMatrix(mtxCopy, domain, range));
00106   return rtn;
00107   
00108 }
00109 
00110 
00111 LinearOperator<double> epetraMatrixMatrixProduct(
00112   const LinearOperator<double>& A,
00113   const LinearOperator<double>& B)
00114 {
00115   /* Extract the underlying Epetra matrix for A. Type checking is done
00116    * ny rcp_dynamic_cast, so we need no error check here. */
00117   RCP<const Epetra_CrsMatrix> A_crs = EpetraMatrix::getConcretePtr(A);
00118 
00119   /* Extract the underlying Epetra matrix for A. Type checking is done
00120    * ny rcp_dynamic_cast, so we need no error check here. */
00121   RCP<const Epetra_CrsMatrix> B_crs = EpetraMatrix::getConcretePtr(B);
00122   
00123   bool transA = false;
00124   bool transB = false;
00125   
00126 
00127   /* Get the row map from A. We will need this to build the target matrix C */
00128   const Epetra_Map* rowmap 
00129     = transA ? &(A_crs->DomainMap()) : &(A_crs->RowMap());
00130 
00131   /* make the target matrix */
00132   RCP<Epetra_CrsMatrix> C = rcp(new Epetra_CrsMatrix(Copy, *rowmap, 1));
00133 
00134   /* Carry out the multiplication */
00135   int ierr 
00136     = EpetraExt::MatrixMatrix::Multiply(*A_crs, transA, *B_crs, transB, *C);
00137   TEST_FOR_EXCEPTION(ierr != 0, RuntimeError,
00138     "EpetraExt Matrix-matrix multiply failed with error code ierr=" << ierr);
00139 
00140   /* Prepare an operator object for the scaled matrix */
00141   RCP<const EpetraVectorSpace> range 
00142     = rcp_dynamic_cast<const EpetraVectorSpace>(A.range().ptr());
00143 
00144   RCP<const EpetraVectorSpace> domain 
00145     = rcp_dynamic_cast<const EpetraVectorSpace>(B.domain().ptr());
00146 
00147   RCP<LinearOpBase<double> > rtn 
00148     = rcp(new EpetraMatrix(C, domain, range));
00149   return rtn;
00150   
00151 }
00152 
00153 }

Site Contact