DenseLinAlgPack: Concreate C++ Classes for Dense Blas-Compatible Linear Algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
DenseLinAlgLAPack.cpp
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 "DenseLinAlgLAPack.hpp"
43 #include "DenseLinAlgPack_LAPACK_Cpp.hpp"
44 #include "DenseLinAlgPack_DMatrixAsTriSym.hpp"
45 #include "Teuchos_Assert.hpp"
46 
47 namespace {
48 template< class T >
49 inline
50 T my_min( const T& v1, const T& v2 ) { return v1 < v2 ? v1 : v2; }
51 } // end namespace
52 
53 void DenseLinAlgLAPack::potrf( DMatrixSliceTriEle* A )
54 {
55  FortranTypes::f_int info;
56  LAPACK_Cpp::potrf( A->uplo(), A->rows(), A->gms().col_ptr(1), A->gms().max_rows(), &info );
57  if( info != 0 ) {
59  info < 0, std::invalid_argument
60  ,"potrf(...): Error, Invalid argument "
61  << -info << " sent to LAPACK function xPOTRF(...)" );
62  // info > 0
64  true, FactorizationException
65  ,"potrf(...): Error, Minor of order "
66  << info << " is not positive definite, the factorization "
67  "could not be completed" );
68  }
69  // If you get here all went okay and A has been factorized and now
70  // hold the cholesky factor of input A.
71 }
72 
73 void DenseLinAlgLAPack::geqrf( DMatrixSlice* A, DVectorSlice* tau, DVectorSlice* work )
74 {
75  FortranTypes::f_int info;
76  if( tau->dim() != my_min( A->rows(), A->cols() ) ) {
78  true, std::invalid_argument, "geqrf(...): Error, tau is not sized correctly!" );
79  }
80  LAPACK_Cpp::geqrf( A->rows(), A->cols(),A->col_ptr(1), A->max_rows()
81  , tau->raw_ptr(), work->raw_ptr(), work->dim(), &info );
82  if( info != 0 ) {
84  info < 0, std::invalid_argument
85  ,"geqrf(...): Error, Invalid argument "
86  << -info << " sent to LAPACK function xGEQRF(...)" );
87  }
88  // If you get here A and tau contain the QR factors of input A.
89 }
90 
91 void DenseLinAlgLAPack::ormrq(
93  ,const DMatrixSlice& A, const DVectorSlice& tau
94  ,DMatrixSlice* C, DVectorSlice* work
95  )
96 {
97  FortranTypes::f_int info;
98  LAPACK_Cpp::ormqr( side, trans, C->rows(), C->cols()
99  , tau.dim(), A.col_ptr(1), A.max_rows()
100  , tau.raw_ptr(), C->col_ptr(1), C->max_rows()
101  , work->raw_ptr(), work->dim(), &info );
102  if( info != 0 ) {
104  info < 0, std::invalid_argument
105  ,"ormrq(...): Error, Invalid argument "
106  << -info << " sent to LAPACK function xORMRQ(...)" );
107  }
108  // If you get here C contains the desired matrix-matrix multiplication.
109 }
110 
111 void DenseLinAlgLAPack::sytrf(
112  DMatrixSliceTriEle* A, FortranTypes::f_int ipiv[]
113  ,DVectorSlice* work
114  )
115 {
116  FortranTypes::f_int info;
117  LAPACK_Cpp::sytrf( A->uplo(), A->rows(), A->gms().col_ptr(1)
118  , A->gms().max_rows(), ipiv, work->raw_ptr(), work->dim()
119  , &info );
121  info < 0, std::invalid_argument
122  ,"sytrf(...): Error, Invalid argument "
123  << -info << " sent to LAPACK function xSYTRF(...)" );
125  info > 0, FactorizationException
126  ,"sytrf(...): Error, xSYTRF(...) indicates a singular matrix, "
127  << "D("<<info<<","<<info<<") is zero." );
128  // If we get here A and ipiv contain the factorization.
129 }
130 
131 void DenseLinAlgLAPack::sytrs(
132  const DMatrixSliceTriEle& A, FortranTypes::f_int ipiv[]
133  ,DMatrixSlice* B, DVectorSlice* work
134  )
135 {
137  (A.rows() != B->rows()), std::invalid_argument
138  ,"sytrs(...) : Error, The number of rows in A and B must match."
139  );
140  FortranTypes::f_int info;
141  LAPACK_Cpp::sytrs( A.uplo(), A.rows(), B->cols(), A.gms().col_ptr(1)
142  , A.gms().max_rows(), ipiv, B->col_ptr(1), B->max_rows()
143  , &info );
145  info < 0, std::invalid_argument
146  ,"sytrs(...): Error, Invalid argument "
147  << -info << " sent to LAPACK function xSYTRS(...)"
148  );
149 }
150 
151 void DenseLinAlgLAPack::getrf(
152  DMatrixSlice* A, FortranTypes::f_int ipiv[], FortranTypes::f_int* rank
153  )
154 {
155  FortranTypes::f_int info;
156  LAPACK_Cpp::getrf(
157  A->rows(), A->cols(), A->col_ptr(1), A->max_rows()
158  ,ipiv, &info
159  );
160  *rank = my_min( A->rows(), A->cols() );
162  info < 0, std::invalid_argument
163  ,"getrf(...): Error, Invalid argument "
164  << -info << " sent to LAPACK function xGETRF(...)" );
165  if(info > 0)
166  *rank = info - 1;
167 }
168 
169 void DenseLinAlgLAPack::getrs(
170  const DMatrixSlice& LU, const FortranTypes::f_int ipiv[], BLAS_Cpp::Transp transp
171  ,DMatrixSlice* B
172  )
173 {
175  (LU.rows() != LU.cols() || LU.rows() != B->rows() ), std::invalid_argument
176  ,"getrs(...) : Error, A must be square and the number of rows in A and B must match."
177  );
178  FortranTypes::f_int info;
179  LAPACK_Cpp::getrs(
180  transp, LU.rows(), B->cols(), LU.col_ptr(1), LU.max_rows(), ipiv
181  ,B->col_ptr(1), B->max_rows(), &info
182  );
184  info < 0, std::invalid_argument
185  ,"getrs(...): Error, Invalid argument "
186  << -info << " sent to LAPACK function xGETRS(...)" );
187  // If we get here B is the solution to the linear system.
188 }
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Side
Transp