MOOCHO (Single Doxygen Collection) Version of the Day
DenseLinAlgPack_PermVecMat.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00005 //                  Copyright (2003) 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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00038 // 
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include "DenseLinAlgPack_PermVecMat.hpp"
00043 #include "DenseLinAlgPack_DMatrixClass.hpp"
00044 #include "DenseLinAlgPack_IVector.hpp"
00045 
00046 #ifdef TEUCHOS_DEBUG   // Debug only!
00047 bool DenseLinAlgPack::PermVecMat_print = false;
00048 #include <iostream>
00049 #include "DenseLinAlgPack_PermOut.hpp"
00050 #include "DenseLinAlgPack_DVectorOut.hpp"
00051 #endif
00052 
00053 // Local assert function
00054 namespace {
00055 inline void i_assert_perm_size(size_t size1, size_t size2)
00056 {
00057 #ifdef LINALGPACK_CHECK_RHS_SIZES
00058   if(size1 != size2)
00059     throw std::length_error("The size of the permutation vector is not correct");
00060 #endif
00061 }
00062 
00063 } // end namespace
00064 
00065 void DenseLinAlgPack::identity_perm(IVector* perm) {
00066   if(!perm->size())
00067     throw std::length_error("DenseLinAlgPack::identity_perm(): perm must be sized");
00068   IVector::iterator itr_perm    = perm->begin();
00069   for(size_type i = 1; i <= perm->size(); ++i)
00070     *itr_perm++ = i;
00071 }
00072 
00073 void DenseLinAlgPack::inv_perm(const IVector& perm, IVector* inv_perm) {
00074   inv_perm->resize(perm.size());
00075   for(size_type i = 1; i <= perm.size(); ++i)
00076     (*inv_perm)(perm(i)) = i;
00077 }
00078 
00079 void DenseLinAlgPack::perm_ele(const IVector& perm, DVectorSlice* vs)
00080 {
00081   i_assert_perm_size(vs->dim(),perm.size());
00082   DVector tmp_v(vs->dim());
00083   DVector::iterator   v_itr   = tmp_v.begin(),
00084               v_itr_end = tmp_v.end();
00085   IVector::const_iterator perm_itr = perm.begin();
00086   // Copy the elements in the permed order into the temp vector
00087   for(; v_itr != v_itr_end; ++v_itr, ++perm_itr)
00088     *v_itr = (*vs)(*perm_itr);
00089     
00090   // Copy the permed vector back
00091   (*vs) = tmp_v();
00092 }
00093 
00094 void DenseLinAlgPack::perm_ele(const DVectorSlice& x, const IVector& perm, DVectorSlice* y)
00095 {
00096   i_assert_perm_size(x.dim(),perm.size());
00097   i_assert_perm_size(y->dim(),perm.size());
00098 
00099   IVector::const_iterator
00100     perm_itr  = perm.begin();
00101   DVectorSlice::iterator
00102     y_itr   = y->begin(),
00103     y_end   = y->end();
00104   while(y_itr != y_end)
00105     *y_itr++ = x(*perm_itr++);
00106 }
00107 
00108 void DenseLinAlgPack::inv_perm_ele(const DVectorSlice& y, const IVector& perm, DVectorSlice* x)
00109 {
00110   i_assert_perm_size(y.dim(),perm.size());
00111   i_assert_perm_size(x->dim(),perm.size());
00112 #ifdef TEUCHOS_DEBUG
00113   if( PermVecMat_print ) {
00114     std::cerr
00115       << "enter inv_perm_ele(y,perm,x):\n"
00116       << "before:\n"
00117       << "y =\n" << y
00118       << "perm =\n" << perm
00119       << "x =\n" << *x;
00120   }
00121 #endif  
00122   DVectorSlice::const_iterator
00123     y_itr   = y.begin(),
00124     y_end   = y.end();
00125   IVector::const_iterator
00126     perm_itr  = perm.begin();
00127   while(y_itr != y_end)
00128     (*x)(*perm_itr++) = *y_itr++;
00129 #ifdef TEUCHOS_DEBUG
00130   if( PermVecMat_print ) {
00131     std::cerr
00132       << "inv_perm_ele(y,perm,x):\n"
00133       << "after:\n"
00134       << "x =\n" << *x
00135       << "exit inv_perm_ele(...) ...\n";
00136   }
00137 #endif  
00138 }
00139 
00140 void DenseLinAlgPack::perm_rows(const IVector& row_perm, DMatrixSlice* gms)
00141 {
00142   i_assert_perm_size(gms->rows(),row_perm.size());
00143   DMatrix tmp_gm(gms->rows(),gms->cols());
00144   DMatrixSlice::size_type rows = gms->rows(), i;
00145   // Copy the rows in the correct order into the temp matrix.
00146   for(i = 1; i <= rows; ++i)
00147     tmp_gm.row(i) = gms->row(row_perm(i));
00148   // Copy the permed matrix back
00149   (*gms) = tmp_gm();
00150 }
00151 
00152 void DenseLinAlgPack::perm_cols(const IVector& col_perm, DMatrixSlice* gms)
00153 {
00154   i_assert_perm_size(gms->cols(),col_perm.size());
00155   DMatrix tmp_gm(gms->rows(),gms->cols());
00156   DMatrixSlice::size_type cols = gms->cols(), i;
00157   // Copy the columns in the correct order into the temp matrix.
00158   for(i = 1; i <= cols; ++i)
00159     tmp_gm.col(i) = gms->col(col_perm(i));
00160   // Copy the permed matrix back
00161   (*gms) = tmp_gm();
00162 }
00163 
00164 void DenseLinAlgPack::perm_rows_cols(const IVector& row_perm, const IVector& col_perm
00165   , DMatrixSlice* gms)
00166 {
00167   i_assert_perm_size(gms->rows(),row_perm.size());
00168   i_assert_perm_size(gms->cols(),col_perm.size());
00169   DMatrix tmp_gm(gms->rows(),gms->cols());
00170   DMatrixSlice::size_type rows = gms->rows(), cols = gms->cols(), i;
00171   // Copy the rows in the correct order into the temp matrix.
00172   for(i = 1; i <= rows; ++i)
00173     tmp_gm.row(i) = gms->row(row_perm(i));
00174   // Copy the columns in the correct order back into matrix.
00175   for(i = 1; i <= cols; ++i)
00176     gms->col(i) = tmp_gm.col(col_perm(i));
00177 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines