51 #include "ConstrainedOptPack_MatrixKKTFullSpaceRelaxed.hpp"
52 #include "AbstractLinAlgPack/src/DirectSparseFortranCompatibleSolver.h"
53 #include "AbstractLinAlgPack/src/MatrixConvertToSparseFortranCompatible.hpp"
54 #include "AbstractLinAlgPack/test/TestMatrixConvertToSparseFortranCompatible.hpp"
55 #include "DenseLinAlgPack_DVectorClass.hpp"
56 #include "DenseLinAlgPack_AssertOp.hpp"
58 namespace ConstrainedOptPack {
61 const direct_solver_ptr_t& direct_solver )
63 direct_solver_(direct_solver)
66 , G_(NULL), convG_(0), G_nz_(0)
67 , A_(NULL), convA_(0), A_nz_(0)
71 const MatrixOp& G,
const MatrixOp& A
72 , std::ostream* out, EPrintMoreOrLess print_what, ERunTests test_what )
76 print_what_ = print_what;
77 test_what_ = test_what;
81 validate_and_set_matrices(G,A);
83 use_relaxation_ =
false;
97 const MatrixOp& G,
const MatrixOp& A
98 ,
const DVectorSlice& c, value_type M
99 , std::ostream* out, EPrintMoreOrLess print_what, ERunTests test_what )
109 initialized_ =
false;
114 direct_solver().release_memory();
124 assert_initialized();
125 return n_ + m_ + (use_relaxation_ ? 1 : 0 );
130 assert_initialized();
131 return n_ + m_ + (use_relaxation_ ? 1 : 0 );
138 assert_initialized();
146 assert_initialized();
154 ,
const DVectorSlice& vs_rhs2, value_type beta)
const
158 assert_initialized();
163 if( use_relaxation_ ) {
178 y1 = (*vs_lhs)(1,n_),
179 y2 = (*vs_lhs)(n_+1,n_+m_);
182 x2 = vs_rhs2(n_+1,n_+m_);
199 ,
const DVectorSlice& vs_rhs2)
const
201 assert_initialized();
211 assert_matrices_set();
215 if( use_relaxation_ ) {
224 typedef MatrixConvertToSparseFortranCompatible MCTSFC_t;
225 const FortranTypes::f_int
226 A_nz = convA_->num_nonzeros(MCTSFC_t::EXTRACT_FULL_MATRIX);
227 nz = convG_->num_nonzeros( extract_region )
228 + ( extract_region == MCTSFC_t::EXTRACT_FULL_MATRIX ? 2 * A_nz : A_nz );
234 EExtractRegion extract_region
235 ,
const FortranTypes::f_int len_Aval
236 , FortranTypes::f_dbl_prec Aval[]
237 ,
const FortranTypes::f_int len_Aij
238 , FortranTypes::f_int Arow[]
239 , FortranTypes::f_int Acol[]
240 ,
const FortranTypes::f_int row_offset
241 ,
const FortranTypes::f_int col_offset
244 assert_matrices_set();
246 if( len_Aval == 0 && len_Aij == 0 ) {
249 <<
"\n*** MatrixKKTFullSpaceRelaxed::coor_extract_nonzeros(...) : "
250 <<
"Warning, nothing to compute: len_Aval==0 && len_Aij==0\n";
255 if( test_what_ == RUN_TESTS ) {
257 typedef MatrixConvertToSparseFortranCompatible
259 namespace slaptp = AbstractLinAlgPack::TestingPack;
260 using slaptp::TestMatrixConvertToSparseFortranCompatible;
268 slaptp::ETestSparseFortranFormat
269 sparse_format = slaptp::TEST_COOR_FORMAT;
273 <<
"\n*** Testing conversion interface for submatrix G ...\n";
275 bool result = TestMatrixConvertToSparseFortranCompatible(
276 extract_region,sparse_format,*convG_,*G_
277 ,warning_tol,error_tol,
true,out_
278 ,print_what_==PRINT_MORE );
281 char omsg[] =
"MatrixKKTFullSpaceRelaxed : Error, the conversion "
282 "interface for G did not check out\n";
285 << std::endl << omsg;
286 throw std::logic_error(omsg);
291 <<
"\n*** Conversion interface for G checked out!\n";
297 MCTSFC_t::EExtractRegion
298 extract_region = MCTSFC_t::EXTRACT_FULL_MATRIX;
299 slaptp::ETestSparseFortranFormat
300 sparse_format = slaptp::TEST_COOR_FORMAT;
304 <<
"\n*** Testing conversion interface for submatrix A ...\n";
306 bool result = TestMatrixConvertToSparseFortranCompatible(
307 extract_region,sparse_format,*convA_,*A_
308 ,warning_tol,error_tol,
true,out_
309 ,print_what_==PRINT_MORE );
312 char omsg[] =
"MatrixKKTFullSpaceRelaxed : Error, the conversion "
313 "interface for A did not check out\n";
316 << std::endl << omsg;
317 throw std::logic_error(omsg);
322 <<
"\n*** Conversion interface for A checked out!\n";
329 if( use_relaxation_ ) {
339 typedef MatrixConvertToSparseFortranCompatible MCTSFC_t;
341 switch(extract_region) {
342 case MCTSFC_t::EXTRACT_FULL_MATRIX:
345 case MCTSFC_t::EXTRACT_UPPER_TRIANGULAR:
348 case MCTSFC_t::EXTRACT_LOWER_TRIANGULAR:
357 const FortranTypes::f_int
358 G_lo_nz = convG_->num_nonzeros( MCTSFC_t::EXTRACT_LOWER_TRIANGULAR ),
359 A_nz = convA_->num_nonzeros( MCTSFC_t::EXTRACT_FULL_MATRIX);
361 && (len_Aij == 0 || len_Aij == G_lo_nz + A_nz) );
363 convG_->coor_extract_nonzeros(
364 MCTSFC_t::EXTRACT_LOWER_TRIANGULAR
365 , (len_Aval > 0 ? G_lo_nz : 0), Aval
366 , (len_Aij > 0 ? G_lo_nz : 0), Arow, Acol, 0, 0 );
370 convA_->coor_extract_nonzeros(
371 MCTSFC_t::EXTRACT_FULL_MATRIX
372 , (len_Aval > 0 ? A_nz : 0), Aval+G_lo_nz
373 , (len_Aij > 0 ? A_nz: 0), Acol+G_lo_nz, Arow+G_lo_nz, 0, n_ );
385 void MatrixKKTFullSpaceRelaxed::assert_initialized()
const
388 throw NotInitializedException(
"MatrixKKTFullSpaceRelaxed::assert_initialized() : "
389 "Error, called a member function without initialize(...) or "
390 "initialize_relaxed(...) being called first probably" );
394 void MatrixKKTFullSpaceRelaxed::assert_matrices_set()
const
396 if( !G_ || !convG_ || !A_ || !convA_ ) {
397 throw NotInitializedException(
"MatrixKKTFullSpaceRelaxed::assert_matrices_set() : "
398 "Error, the matrices G and A have not been set yet" );
402 void MatrixKKTFullSpaceRelaxed::validate_and_set_matrices(
403 const MatrixOp& G,
const MatrixOp& A )
409 if( G.cols() != n ) {
410 std::ostringstream omsg;
412 <<
"MatrixKKTFullSpaceRelaxed::validate_and_set_matrices(...) : Error, "
413 <<
"The matrix G with rows = " << n
414 <<
" is not square with cols = " << G.cols();
415 throw std::length_error(omsg.str());
417 if( A.rows() != n ) {
418 std::ostringstream omsg;
420 <<
"MatrixKKTFullSpaceRelaxed::validate_and_set_matrices(...) : Error, "
421 <<
"The number of rows in the matrix A with rows = " << A.rows()
423 <<
" does not match the size of G with rows = cols = " << n;
424 throw std::length_error(omsg.str());
426 if( !(m < n) || n == 0 || m == 0 ) {
427 std::ostringstream omsg;
429 <<
"MatrixKKTFullSpaceRelaxed::validate_and_set_matrices(...) : Error, "
430 <<
"the size of the matrices G nxn and A nxm is not valid with "
431 <<
"n = " << n <<
" and m = " << m;
432 throw std::length_error(omsg.str());
435 const MatrixConvertToSparseFortranCompatible
436 *convG =
dynamic_cast<const MatrixConvertToSparseFortranCompatible*
>(&G);
438 std::ostringstream omsg;
440 <<
"MatrixKKTFullSpaceRelaxed::validate_and_set_matrices(...) : Error, "
441 <<
"The matrix G with concrete type " <<
typeName(G)
442 <<
" does not support the MatrixConvertToSparseFortranCompatible "
444 throw InvalidMatrixType(omsg.str());
446 const MatrixConvertToSparseFortranCompatible
447 *convA =
dynamic_cast<const MatrixConvertToSparseFortranCompatible*
>(&A);
449 std::ostringstream omsg;
451 <<
"MatrixKKTFullSpaceRelaxed::validate_and_set_matrices(...) : Error, "
452 <<
"The matrix A with concrete type " <<
typeName(A)
453 <<
" does not support the MatrixConvertToSparseFortranCompatible "
455 throw InvalidMatrixType(omsg.str());
void set_uninitialized()
Set the matrix to uninitialized.
void coor_extract_nonzeros(EExtractRegion extract_region, const FortranTypes::f_int len_Aval, FortranTypes::f_dbl_prec Aval[], const FortranTypes::f_int len_Aij, FortranTypes::f_int Arow[], FortranTypes::f_int Acol[], const FortranTypes::f_int row_offset, const FortranTypes::f_int col_offset) const
FortranTypes::f_int num_nonzeros(EExtractRegion extract_region) const
void Vp_StMtV(DVectorSlice *vs_lhs, value_type alpha, BLAS_Cpp::Transp trans_rhs1, const DVectorSlice &vs_rhs2, value_type beta) const
(2) vs_lhs = alpha * op(M_rhs1) * vs_rhs2 + beta * vs_lhs (BLAS xGEMV)
void release_memory()
Clear all allocated storage.
MatrixOp & operator=(const MatrixOp &m)
MatrixKKTFullSpaceRelaxed(const direct_solver_ptr_t &direct_solver=0)
void Vp_StMtV(VectorMutable *v_lhs, value_type alpha, const MatrixOp &M_rhs1, BLAS_Cpp::Transp trans_rhs1, const Vector &v_rhs2, value_type beta=1.0)
void V_InvMtV(DVectorSlice *v_lhs, BLAS_Cpp::Transp trans_rhs1, const DVectorSlice &vs_rhs2) const
(1) v_lhs = inv(op(M_rhs1)) * vs_rhs2
void initialize(const MatrixOp &G, const MatrixOp &A, std::ostream *out=0, EPrintMoreOrLess print_what=PRINT_LESS, ERunTests test_what=NO_TESTS)
Initialize the nonrelaxed matrix.
void initialize_relaxed(const MatrixOp &G, const MatrixOp &A, const DVectorSlice &c, value_type bigM=1e+10, std::ostream *out=0, EPrintMoreOrLess print_what=PRINT_LESS, ERunTests test_what=NO_TESTS)
Initialize the relaxed matrix.
std::ostream & output(std::ostream &out) const
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
std::string typeName(const T &t)