|
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 <sstream> 00043 00044 #include "InputStreamHelperPack_EatInputComment.hpp" 00045 #include "DenseLinAlgPack_DMatrixInFunc.hpp" 00046 #include "DenseLinAlgPack_DVectorInFunc.hpp" 00047 #include "DenseLinAlgPack_DMatrixClass.hpp" 00048 00049 namespace { // Local inplementation 00050 std::istream& input_gms(std::istream& is, DenseLinAlgPack::DMatrixSlice* gms, const char func[]); 00051 } 00052 00053 std::istream& DenseLinAlgPack::input(std::istream& is, DMatrix* gm, LinAlgPackIO::fmtflags extra_flags) { 00054 if( !(extra_flags & LinAlgPackIO::ignore_dim_bit) ) { 00055 size_type m, n; 00056 is >> m >> n; 00057 if(is.fail()) 00058 throw LinAlgPackIO::InputException( "DenseLinAlgPack::input() {DMatrix}: " 00059 "Input operation of matrix dimension failed. Check that the constant n " 00060 "is a valid integer." ); 00061 if(is.bad()) 00062 throw std::ios_base::failure( "DenseLinAlgPack::input() {DMatrix}: " 00063 "Input operation failed because the stream became currupted." ); 00064 gm->resize(m,n); 00065 } 00066 DMatrixSlice gms = (*gm)(); 00067 return input_gms(is,&gms,"DenseLinAlgPack::input() {DMatrix}"); 00068 } 00069 00070 std::istream& DenseLinAlgPack::input(std::istream& is, DMatrixSlice* gms, LinAlgPackIO::fmtflags extra_flags) { 00071 if( !(extra_flags & LinAlgPackIO::ignore_dim_bit) ) { 00072 size_type m, n; 00073 is >> m >> n; 00074 if(is.fail()) 00075 throw LinAlgPackIO::InputException( "DenseLinAlgPack::input() {DMatrixSlice}: " 00076 "Input operation of matrix dimension failed. Check that the constant n " 00077 " is a valid integer."); 00078 if(is.bad()) 00079 throw std::ios_base::failure( "DenseLinAlgPack::input() {DMatrixSlice}: " 00080 "Input operation failed because the stream became currupted." ); 00081 DenseLinAlgPack::assert_gms_lhs(*gms,m,n); 00082 } 00083 return input_gms( is, gms, "DenseLinAlgPack::input() {DMatrixSlice}" ); 00084 } 00085 00086 // ////////////////////// 00087 // Local implementation 00088 00089 namespace { 00090 00091 // Read in a specified number of elements into a DMatrixSlice object. 00092 // The dim of gms is not checked. If an element input operation fails or the end of the file 00093 // is reached before all of the elements are read in then a LinAlgPackIO::InputException is thrown. 00094 // If the stream becomes currupted durring the input then a std::ios_base::failure exception 00095 // is thrown. The state of the input steam remains the same on return accept for the char's 00096 // that have been extracted. 00097 std::istream& input_gms(std::istream& is, DenseLinAlgPack::DMatrixSlice* gms, const char func[]) { 00098 using std::ios_base; 00099 using DenseLinAlgPack::size_type; 00100 using DenseLinAlgPack::DVectorSlice; 00101 if(!gms->rows()) return is; // If we are inputting an unsized matrix then there are no elements 00102 // to extract so just return. 00103 ios_base::iostate old_state = is.exceptions(); // save the old state 00104 is.exceptions(ios_base::badbit | ios_base::failbit | ios_base::eofbit); 00105 try { 00106 // Read in the rows 00107 for(size_type i = 1; i <= gms->rows(); ++i) { 00108 InputStreamHelperPack::eat_comment_lines(is,'*'); 00109 DVectorSlice gms_row_i = gms->row(i); 00110 DenseLinAlgPack::input( is, &gms_row_i 00111 , (DenseLinAlgPack::LinAlgPackIO::fmtflags)(DenseLinAlgPack::LinAlgPackIO::ignore_dim_bit) ); 00112 } 00113 } 00114 catch(...) { 00115 is.exceptions(old_state); 00116 throw; 00117 } 00118 is.exceptions(old_state); 00119 return is; 00120 } 00121 00122 } // end namespace
1.7.6.1