42 #include "AbstractLinAlgPack_MatrixComposite.hpp" 43 #include "AbstractLinAlgPack_SpVectorClass.hpp" 44 #include "AbstractLinAlgPack_VectorStdOps.hpp" 45 #include "AbstractLinAlgPack_VectorMutableBlocked.hpp" 46 #include "AbstractLinAlgPack_AssertOp.hpp" 48 #include "Teuchos_Workspace.hpp" 49 #include "Teuchos_Assert.hpp" 50 #include "ProfileHackPack_profile_hack.hpp" 57 AbstractLinAlgPack::value_type
64 AbstractLinAlgPack::value_type
69 return ele != NULL ? ele->
value() : 0.0;
75 Teuchos::RCP<const AbstractLinAlgPack::Vector>
78 ,AbstractLinAlgPack::index_type l
79 ,AbstractLinAlgPack::index_type u
86 Teuchos::RCP<const AbstractLinAlgPack::SpVectorSlice>
89 ,AbstractLinAlgPack::index_type l
90 ,AbstractLinAlgPack::index_type u
101 ,AbstractLinAlgPack::size_type M_rows, AbstractLinAlgPack::size_type M_cols
105 ,
const V& x, AbstractLinAlgPack::value_type b
118 if( vec_list.size() ) {
119 for( vec_list_t::const_iterator itr = vec_list.begin(); itr != vec_list.end(); ++itr ) {
121 op_v_trans = ( M_trans == itr->v_trans_ ?
no_trans :
trans );
122 const AbstractLinAlgPack::index_type
123 r = ( M_trans ==
no_trans ? itr->r_l_ : itr->c_l_ ),
124 c = ( M_trans ==
no_trans ? itr->c_l_ : itr->r_l_ );
125 if( itr->rng_G_.full_range() ) {
151 TEUCHOS_TEST_FOR_EXCEPT(
true);
155 TEUCHOS_TEST_FOR_EXCEPT(
true);
159 if( mat_list.size() ) {
160 for( mat_list_t::const_iterator itr = mat_list.begin(); itr != mat_list.end(); ++itr ) {
161 const AbstractLinAlgPack::index_type
162 rl =
rows(itr->r_l_,itr->c_l_,M_trans),
163 ru =
rows(itr->r_u_,itr->c_u_,M_trans),
164 cl =
cols(itr->r_l_,itr->c_l_,M_trans),
165 cu =
cols(itr->r_u_,itr->c_u_,M_trans);
167 op_P_trans = ( M_trans == itr->P_trans_ ?
no_trans :
trans ),
168 op_A_trans = ( M_trans == itr->A_trans_ ?
no_trans :
trans ),
169 op_Q_trans = ( M_trans == itr->Q_trans_ ?
no_trans :
trans );
170 if( itr->rng_P_.full_range() && itr->rng_Q_.full_range() ) {
184 if( itr->A_ == NULL ) {
185 TEUCHOS_TEST_FOR_EXCEPT( !( itr->P_.get() && !itr->P_->is_identity() ) );
197 TEUCHOS_TEST_FOR_EXCEPT(
true);
210 if( !itr->rng_P_.full_range() && !itr->rng_Q_.full_range() ) {
212 y->
sub_view(rl,ru).get(), a * itr->alpha_
214 itr->A_trans_==
no_trans ? itr->rng_P_ : itr->rng_Q_
215 ,itr->A_trans_==
no_trans ? itr->rng_Q_ : itr->rng_P_ )
221 TEUCHOS_TEST_FOR_EXCEPT(
true);
234 enum EByRowCol { BY_ROW, BY_COL };
235 CompSubEntry(
enum EByRowCol by_row_col)
236 : by_row_col_(by_row_col)
238 bool operator()(
const T& e1,
const T& e2 )
241 ( by_row_col_ == BY_ROW
247 EByRowCol by_row_col_;
256 MatrixComposite::MatrixComposite( size_type
rows, size_type
cols )
258 reinitialize(rows,cols);
261 void MatrixComposite::reinitialize( size_type
rows, size_type
cols )
265 fully_constructed_ =
true;
268 if(matrix_list_.size()) {
269 matrix_list_.erase(matrix_list_.begin(),matrix_list_.end());
271 if(vector_list_.size()) {
272 vector_list_.erase(vector_list_.begin(),vector_list_.end());
274 space_rows_ = Teuchos::null;
275 space_cols_ = Teuchos::null;
278 void MatrixComposite::add_vector(
280 ,size_type col_offset
290 fully_constructed_ =
false;
291 TEUCHOS_TEST_FOR_EXCEPT(
true);
294 void MatrixComposite::add_vector(
296 ,size_type col_offset
304 fully_constructed_ =
false;
305 TEUCHOS_TEST_FOR_EXCEPT(
true);
308 void MatrixComposite::add_vector(
310 ,size_type col_offset
319 TEUCHOS_TEST_FOR_EXCEPT( !( beta != 0.0 ) );
320 TEUCHOS_TEST_FOR_EXCEPT( !( v != NULL ) );
321 fully_constructed_ =
false;
323 TEUCHOS_TEST_FOR_EXCEPT( !( row_offset + v->
dim() <= rows_ ) );
324 TEUCHOS_TEST_FOR_EXCEPT( !( col_offset + 1 <= cols_ ) );
327 TEUCHOS_TEST_FOR_EXCEPT( !( row_offset + 1 <= rows_ ) );
328 TEUCHOS_TEST_FOR_EXCEPT( !( col_offset + v->
dim() <= cols_ ) );
331 vector_list_.push_back(
333 row_offset+1,col_offset+1,beta,
Range1D()
335 ,v,v_release,v_trans ) );
338 void MatrixComposite::remove_vector( vector_list_t::iterator itr )
340 fully_constructed_ =
false;
341 vector_list_.erase(itr);
344 void MatrixComposite::add_matrix(
346 ,size_type col_offset
359 fully_constructed_ =
false;
360 TEUCHOS_TEST_FOR_EXCEPT(
true);
363 void MatrixComposite::add_matrix(
365 ,size_type col_offset
376 fully_constructed_ =
false;
377 TEUCHOS_TEST_FOR_EXCEPT(
true);
380 void MatrixComposite::add_matrix(
382 ,size_type col_offset
393 fully_constructed_ =
false;
394 TEUCHOS_TEST_FOR_EXCEPT(
true);
397 void MatrixComposite::add_matrix(
399 ,size_type col_offset
411 using RangePack::full_range;
413 TEUCHOS_TEST_FOR_EXCEPTION(
414 alpha == 0.0, std::invalid_argument
415 ,
"MatrixComposite::add_matrix(...) : Error!" );
416 TEUCHOS_TEST_FOR_EXCEPTION(
417 A == NULL, std::invalid_argument
418 ,
"MatrixComposite::add_matrix(...) : Error!" );
423 opA_rows =
rows(A_rows,A_cols,A_trans),
424 opA_cols =
cols(A_rows,A_cols,A_trans);
426 rng_P = full_range(rng_P_in,1,opA_rows),
427 rng_Q = full_range(rng_Q_in,1,opA_cols);
429 opPopAopQ_rows = rng_P.
size(),
430 opPopAopQ_cols = rng_Q.size();
432 TEUCHOS_TEST_FOR_EXCEPTION(
433 row_offset + opPopAopQ_rows > rows_, std::invalid_argument
434 ,
"MatrixComposite::add_matrix(...) : Error!" );
435 TEUCHOS_TEST_FOR_EXCEPTION(
436 col_offset + opPopAopQ_cols > cols_, std::invalid_argument
437 ,
"MatrixComposite::add_matrix(...) : Error!" );
439 fully_constructed_ =
false;
441 matrix_list_.push_back(
443 row_offset+1,row_offset+opPopAopQ_rows
444 ,col_offset+1,col_offset+opPopAopQ_cols
460 void MatrixComposite::add_matrix(
462 ,size_type col_offset
473 TEUCHOS_TEST_FOR_EXCEPTION(
474 alpha == 0.0, std::invalid_argument
475 ,
"MatrixComposite::add_matrix(...) : Error!" );
476 TEUCHOS_TEST_FOR_EXCEPTION(
477 A == NULL, std::invalid_argument
478 ,
"MatrixComposite::add_matrix(...) : Error!" );
483 opA_rows =
rows(A_rows,A_cols,A_trans),
484 opA_cols =
cols(A_rows,A_cols,A_trans);
486 TEUCHOS_TEST_FOR_EXCEPTION(
487 row_offset + opA_rows > rows_, std::invalid_argument
488 ,
"MatrixComposite::add_matrix(...) : Error!" );
489 TEUCHOS_TEST_FOR_EXCEPTION(
490 col_offset + opA_cols > cols_, std::invalid_argument
491 ,
"MatrixComposite::add_matrix(...) : Error!" );
493 fully_constructed_ =
false;
495 matrix_list_.push_back(
497 row_offset+1,row_offset+opA_rows
498 ,col_offset+1,col_offset+opA_cols
509 void MatrixComposite::add_matrix(
511 ,size_type col_offset
522 TEUCHOS_TEST_FOR_EXCEPT( !( alpha != 0.0 ) );
523 TEUCHOS_TEST_FOR_EXCEPT( !( P != NULL ) );
525 fully_constructed_ =
false;
530 opP_rows =
rows(P_rows,P_cols,P_trans),
531 opP_cols =
cols(P_rows,P_cols,P_trans);
533 TEUCHOS_TEST_FOR_EXCEPT( !( row_offset + opP_rows <= rows_ ) );
534 TEUCHOS_TEST_FOR_EXCEPT( !( col_offset + opP_cols <= cols_ ) );
536 matrix_list_.push_back(
538 row_offset+1,row_offset+opP_rows,col_offset+1,col_offset+opP_cols,alpha
548 void MatrixComposite::remove_matrix( matrix_list_t::iterator itr )
550 fully_constructed_ =
false;
551 matrix_list_.erase(itr);
554 void MatrixComposite::finish_construction(
559 TEUCHOS_TEST_FOR_EXCEPTION(
560 !space_cols.get(), std::invalid_argument
561 ,
"MatrixComposite::finish_construction(...): Error, space_cols.get() can not be NULL" );
562 TEUCHOS_TEST_FOR_EXCEPTION(
563 !space_rows.get(), std::invalid_argument
564 ,
"MatrixComposite::finish_construction(...): Error, space_rows.get() can not be NULL" );
565 TEUCHOS_TEST_FOR_EXCEPTION(
566 space_cols->dim() != rows_, std::invalid_argument
567 ,
"MatrixComposite::finish_construction(...): Error, space_colss->dim() = " << space_cols->dim()
568 <<
" != rows = " << rows_ <<
" where cols was passed to this->reinitialize(...)" );
569 TEUCHOS_TEST_FOR_EXCEPTION(
570 space_rows->dim() != cols_, std::invalid_argument
571 ,
"MatrixComposite::finish_construction(...): Error, space_rows->dim() = " << space_rows->dim()
572 <<
" != cols = " << cols_ <<
" where cols was passed to this->reinitialize(...)" );
574 space_rows_ = space_rows;
575 space_cols_ = space_cols;
576 fully_constructed_ =
true;
581 int MatrixComposite::num_vectors()
const 583 return vector_list_.size();
586 MatrixComposite::vector_list_t::iterator
587 MatrixComposite::vectors_begin()
589 return vector_list_.begin();
592 MatrixComposite::vector_list_t::iterator
593 MatrixComposite::vectors_end()
595 return vector_list_.end();
598 MatrixComposite::vector_list_t::const_iterator
599 MatrixComposite::vectors_begin()
const 601 return vector_list_.begin();
604 MatrixComposite::vector_list_t::const_iterator
605 MatrixComposite::vectors_end()
const 607 return vector_list_.end();
610 int MatrixComposite::num_matrices()
const 612 return matrix_list_.size();
615 MatrixComposite::matrix_list_t::iterator
616 MatrixComposite::matrices_begin()
618 return matrix_list_.begin();
621 MatrixComposite::matrix_list_t::iterator
622 MatrixComposite::matrices_end()
624 return matrix_list_.end();
627 MatrixComposite::matrix_list_t::const_iterator
628 MatrixComposite::matrices_begin()
const 630 return matrix_list_.begin();
633 MatrixComposite::matrix_list_t::const_iterator
634 MatrixComposite::matrices_end()
const 636 return matrix_list_.end();
643 return fully_constructed_ ? rows_ : 0;
648 return fully_constructed_ ? cols_ : 0;
651 size_type MatrixComposite::nz()
const 653 if(fully_constructed_) {
655 {
for(matrix_list_t::const_iterator itr = matrix_list_.begin(); itr != matrix_list_.end(); ++itr) {
656 if( itr->A_ != NULL )
659 nz += (*itr->P_).nz();
661 {
for(vector_list_t::const_iterator itr = vector_list_.begin(); itr != vector_list_.end(); ++itr) {
672 const VectorSpace& MatrixComposite::space_rows()
const 674 assert_fully_constructed();
678 const VectorSpace& MatrixComposite::space_cols()
const 680 assert_fully_constructed();
687 assert_fully_constructed();
695 return MatrixOp::sub_view(row_rng,col_rng);
700 ,
const Vector& x, value_type b
703 #ifdef PROFILE_HACK_ENABLED 706 assert_fully_constructed();
708 Vp_StMtV_imp(y,a,rows_,cols_,matrix_list_,vector_list_,M_trans,x,b);
716 #ifdef PROFILE_HACK_ENABLED 719 assert_fully_constructed();
721 Vp_StMtV_imp(y,a,rows_,cols_,matrix_list_,vector_list_,M_trans,x,b);
728 ,
const Vector& x, value_type b
731 #ifdef PROFILE_HACK_ENABLED 734 assert_fully_constructed();
745 #ifdef PROFILE_HACK_ENABLED 748 assert_fully_constructed();
754 void MatrixComposite::assert_fully_constructed()
const 756 const bool fully_constructed = fully_constructed_;
757 TEUCHOS_TEST_FOR_EXCEPTION(
758 !fully_constructed, std::logic_error
759 ,
"MatrixComposite::assert_fully_constructed() : Error, not fully constructed!");
element_type * lookup_element(size_type i)
virtual vec_ptr_t sub_view(const Range1D &rng) const
Create an abstract view of a vector object .
virtual vec_mut_ptr_t sub_view(const Range1D &rng)
Create a mutable abstract view of a vector object.
Matrix list entry for a sub-matrix.
std::deque< SubMatrixEntry > matrix_list_t
Warning! This could be changed to some other STL container!
Abstract interface for immutable, finite dimensional, coordinate vectors {abstract}.
void Vt_S(VectorMutable *v_lhs, const value_type &alpha)
v_lhs *= alpha
void Vp_StPtMtV(VectorMutable *v_lhs, value_type alpha, const GenPermMatrixSlice &P_rhs1, BLAS_Cpp::Transp P_rhs1_trans, const MatrixOp &M_rhs2, BLAS_Cpp::Transp M_rhs2_trans, const Vector &v_rhs3, value_type beta=1.0)
v_lhs = alpha * op(P_rhs1) * op(M_rhs2) * v_rhs3 + beta * v_rhs
void Vp_StV(VectorMutable *v_lhs, const value_type &alpha, const Vector &v_rhs)
v_lhs = alpha * v_rhs + v_lhs
size_type rows(size_type rows, size_type cols, BLAS_Cpp::Transp _trans)
Teuchos::RCP< MemMngPack::ReleaseResource > release_resource_ptr_t
std::deque< SubVectorEntry > vector_list_t
Warning! This could be changed to some other STL container!
virtual size_type cols() const
Return the number of columns in the matrix.
Teuchos::RCP< const VectorSpace > space_ptr_t
Abstract interface for objects that represent a space for mutable coordinate vectors.
Vector list entry for a sub-vector.
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)
v_lhs = alpha * op(M_rhs1) * v_rhs2 + beta * v_lhs (BLAS xGEMV)
Base class for all matrices that support basic matrix operations.
value_type dot(const Vector &v_rhs1, const Vector &v_rhs2)
result = v_rhs1' * v_rhs2
virtual index_type dim() const
Return the dimension of this vector.
virtual value_type get_ele(index_type i) const
Fetch an element in the vector.
Abstract interface for mutable coordinate vectors {abstract}.
Sparse storage element type.
void Vp_MtV_assert_compatibility(VectorMutable *v_lhs, const MatrixOp &m_rhs1, BLAS_Cpp::Transp trans_rhs1, const Vector &v_rhs2)
v_lhs += op(m_rhs1) * v_rhs2
virtual size_type rows() const
Return the number of rows in the matrix.
size_type cols(size_type rows, size_type cols, BLAS_Cpp::Transp _trans)
Concrete matrix type to represent general permutation (mapping) matrices.