|
DenseLinAlgPack: Concreate C++ Classes for Dense Blas-Compatible Linear Algebra
Version of the Day
|
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 <limits> 00043 #include <algorithm> 00044 00045 #include "DenseLinAlgPack_MatVecCompare.hpp" 00046 #include "DenseLinAlgPack_DMatrixClass.hpp" 00047 #include "DenseLinAlgPack_DMatrixAsTriSym.hpp" 00048 00049 namespace { 00050 // 00051 using DenseLinAlgPack::value_type; 00052 // 00053 using DenseLinAlgPack::sqrt_eps; 00054 // 00055 template< class T > 00056 inline 00057 T my_max( const T& v1, const T& v2 ) { return v1 > v2 ? v1 : v2; } 00058 // 00059 template< class T > 00060 inline 00061 T my_min( const T& v1, const T& v2 ) { return v1 < v2 ? v1 : v2; } 00062 // 00063 inline 00064 bool _comp(value_type val1, value_type val2) 00065 { 00066 const value_type denom = my_max( ::fabs(val1), 1.0 ); // compare relative errors. 00067 return ::fabs(val1 - val2) / denom < sqrt_eps; 00068 } 00069 00070 } // end namespace 00071 00072 bool DenseLinAlgPack::comp(const DVectorSlice& vs1, const DVectorSlice& vs2) { 00073 DVectorSlice::const_iterator 00074 vs1_itr = vs1.begin(), 00075 vs2_itr = vs2.begin(); 00076 for(; vs1_itr != vs1.end() && vs2_itr != vs2.end(); ++vs1_itr, ++vs2_itr) 00077 if( !_comp(*vs1_itr,*vs2_itr) ) return false; 00078 return true; 00079 } 00080 00081 bool DenseLinAlgPack::comp(const DVectorSlice& vs, value_type alpha) { 00082 DVectorSlice::const_iterator vs_itr = vs.begin(); 00083 for(; vs_itr != vs.end(); ++vs_itr) 00084 if( !_comp(*vs_itr,alpha) ) return false; 00085 return true; 00086 } 00087 00088 bool DenseLinAlgPack::comp(const DMatrixSlice& gms1, BLAS_Cpp::Transp trans1 00089 , const DMatrixSlice& gms2, BLAS_Cpp::Transp trans2) 00090 { 00091 for(size_type i = 1; i < my_min(gms1.cols(),gms2.cols()); ++i) 00092 if( !comp( col(gms1,trans1,i) , col( gms2, trans2, i ) ) ) return false; 00093 return true; 00094 } 00095 00096 bool DenseLinAlgPack::comp(const DMatrixSlice& gms, value_type alpha) 00097 { 00098 for(size_type i = 1; i < gms.cols(); ++i) 00099 if( !comp( gms.col(i) , alpha ) ) return false; 00100 return true; 00101 } 00102 00103 bool DenseLinAlgPack::comp(const DMatrixSliceTriEle& tri_gms1, const DMatrixSliceTriEle& tri_gms2) 00104 { 00105 using BLAS_Cpp::bool_to_trans; 00106 BLAS_Cpp::Transp 00107 trans1 = bool_to_trans(tri_gms1.uplo() != BLAS_Cpp::lower), 00108 trans2 = bool_to_trans(tri_gms2.uplo() != BLAS_Cpp::lower); 00109 00110 const size_type n = tri_gms1.rows(); // same as cols() 00111 for(size_type i = 1; i < n; ++i) { 00112 if( !comp( col(tri_gms1.gms(),trans1,i)(i,n), col(tri_gms2.gms(),trans2,i)(i,n) ) ) 00113 return false; 00114 } 00115 return true; 00116 } 00117 00118 bool DenseLinAlgPack::comp(const DMatrixSliceTriEle& tri_gms1, value_type alpha) 00119 { 00120 using BLAS_Cpp::bool_to_trans; 00121 BLAS_Cpp::Transp 00122 trans1 = bool_to_trans(tri_gms1.uplo() != BLAS_Cpp::lower); 00123 00124 const size_type n = tri_gms1.rows(); // same as cols() 00125 for(size_type i = 1; i < n; ++i) { 00126 if( !comp( col(tri_gms1.gms(),trans1,i)(i,n), alpha ) ) 00127 return false; 00128 } 00129 return true; 00130 } 00131 00132 bool DenseLinAlgPack::comp_less(const DVectorSlice& vs, value_type alpha) 00133 { 00134 DVectorSlice::const_iterator vs_itr = vs.begin(); 00135 const value_type denom = my_max( ::fabs(alpha), 1.0 ); 00136 for(; vs_itr != vs.end(); ++vs_itr) 00137 if( *vs_itr > alpha ) return false; 00138 return true; 00139 }
1.7.6.1