MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DenseLinAlgPack_DMatrixInFunc.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
5 // Copyright (2003) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include <sstream>
43 
48 
49 namespace { // Local inplementation
50 std::istream& input_gms(std::istream& is, DenseLinAlgPack::DMatrixSlice* gms, const char func[]);
51 }
52 
53 std::istream& DenseLinAlgPack::input(std::istream& is, DMatrix* gm, LinAlgPackIO::fmtflags extra_flags) {
54  if( !(extra_flags & LinAlgPackIO::ignore_dim_bit) ) {
55  size_type m, n;
56  is >> m >> n;
57  if(is.fail())
58  throw LinAlgPackIO::InputException( "DenseLinAlgPack::input() {DMatrix}: "
59  "Input operation of matrix dimension failed. Check that the constant n "
60  "is a valid integer." );
61  if(is.bad())
62  throw std::ios_base::failure( "DenseLinAlgPack::input() {DMatrix}: "
63  "Input operation failed because the stream became currupted." );
64  gm->resize(m,n);
65  }
66  DMatrixSlice gms = (*gm)();
67  return input_gms(is,&gms,"DenseLinAlgPack::input() {DMatrix}");
68 }
69 
70 std::istream& DenseLinAlgPack::input(std::istream& is, DMatrixSlice* gms, LinAlgPackIO::fmtflags extra_flags) {
71  if( !(extra_flags & LinAlgPackIO::ignore_dim_bit) ) {
72  size_type m, n;
73  is >> m >> n;
74  if(is.fail())
75  throw LinAlgPackIO::InputException( "DenseLinAlgPack::input() {DMatrixSlice}: "
76  "Input operation of matrix dimension failed. Check that the constant n "
77  " is a valid integer.");
78  if(is.bad())
79  throw std::ios_base::failure( "DenseLinAlgPack::input() {DMatrixSlice}: "
80  "Input operation failed because the stream became currupted." );
82  }
83  return input_gms( is, gms, "DenseLinAlgPack::input() {DMatrixSlice}" );
84 }
85 
86 // //////////////////////
87 // Local implementation
88 
89 namespace {
90 
91 // Read in a specified number of elements into a DMatrixSlice object.
92 // The dim of gms is not checked. If an element input operation fails or the end of the file
93 // is reached before all of the elements are read in then a LinAlgPackIO::InputException is thrown.
94 // If the stream becomes currupted durring the input then a std::ios_base::failure exception
95 // is thrown. The state of the input steam remains the same on return accept for the char's
96 // that have been extracted.
97 std::istream& input_gms(std::istream& is, DenseLinAlgPack::DMatrixSlice* gms, const char func[]) {
98  using std::ios_base;
101  if(!gms->rows()) return is; // If we are inputting an unsized matrix then there are no elements
102  // to extract so just return.
103  ios_base::iostate old_state = is.exceptions(); // save the old state
104  is.exceptions(ios_base::badbit | ios_base::failbit | ios_base::eofbit);
105  try {
106  // Read in the rows
107  for(size_type i = 1; i <= gms->rows(); ++i) {
109  DVectorSlice gms_row_i = gms->row(i);
110  DenseLinAlgPack::input( is, &gms_row_i
112  }
113  }
114  catch(...) {
115  is.exceptions(old_state);
116  throw;
117  }
118  is.exceptions(old_state);
119  return is;
120 }
121 
122 } // end namespace
Teuchos::Ordinal size_type
Typedef for the size type of elements that are used by the library.
AbstractLinAlgPack::size_type size_type
VectorSliceTmpl< value_type > DVectorSlice
std::istream & eat_comment_lines(std::istream &is, char comment_identifier)
Discards comment lines from an input stream.
void assert_gms_lhs(const DMatrixSlice &gms_lhs, size_type rows, size_type cols, BLAS_Cpp::Transp trans_rhs=BLAS_Cpp::no_trans)
void resize(size_type rows, size_type cols, value_type val=value_type())
Resize matrix to a (rows x cols) matrix and initializes any added elements by val.
DenseLinAlgPack::VectorSliceTmpl< value_type > DVectorSlice
size_type rows() const
Return the number of rows.
std::istream & input(std::istream &is, DMatrix *gm, LinAlgPackIO::fmtflags extra_flags)
int n
DVectorSlice row(size_type i)
Return DVectorSlice object representing the ith row (1-based; 1,2,..,#this->rows()#, or throw std::out_of_range)