MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AbstractLinAlgPack_MatrixVectorTemplateOpDef.hpp
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 // Definitions of template functions declared in AbstractLinAlgPack_MatrixVectorTemplateOp.hpp.
43 
44 #ifndef MATRIX_VECTOR_TEMPLATE_OP_DEF_H
45 #define MATRIX_VECTOR_TEMPLATE_OP_DEF_H
46 
49 
50 // ///////////////////////////////////
51 // Matrix assignment
52 
53 namespace {
54 // Typedef for vector returning functions (row or col but not diagonal)
57 // Implement sparse matrix, dense matrix assignment. Sizes are not checked.
58 template<class T_Matrix>
59 void imp_assign(AbstractLinAlgPack::DMatrixSlice& gms_lhs, const T_Matrix& gm_rhs
60  , BLAS_Cpp::Transp trans_rhs)
61 {
62  // If trans, then copy col into row, otherwise copy col into col.
63  Pvec_func vec_func;
64  if(trans_rhs == BLAS_Cpp::no_trans) vec_func = &AbstractLinAlgPack::DMatrixSlice::col;
66  for(int k = 1; k <= gm_rhs.cols(); ++k)
67  AbstractLinAlgPack::assign((gms_lhs.*vec_func)(k), gm_rhs.col(k));
68 }
69 } // end namespace
70 
71 // Definitions of template functions for matrix-matrix assignment
72 
74 template<class T_Matrix>
75 void AbstractLinAlgPack::assign(DMatrix& gm_lhs, const T_Matrix& gm_rhs
76  , BLAS_Cpp::Transp trans_rhs)
77 {
78  DenseLinAlgPack::resize_gm_lhs(gm_lhs,gm_rhs.rows(),gm_rhs.cols(),trans_rhs);
79  DMatrixSlice gms_lhs = gm_lhs;
80  imp_assign(gms_lhs,gm_rhs,trans_rhs);
81 }
82 
84 template<class T_Matrix>
85 void AbstractLinAlgPack::assign(DMatrixSlice& gms_lhs, const T_Matrix& gm_rhs
86  , BLAS_Cpp::Transp trans_rhs)
87 {
88  DenseLinAlgPack::assert_gms_lhs(gms_lhs,gm_rhs.rows(),gm_rhs.cols(),trans_rhs);
89  imp_assign(gms_lhs,gm_rhs,trans_rhs);
90 }
91 
92 // //////////////////////////////////
93 // Matrix-DVector multiplication
94 
95 namespace {
96 // Throw an exeption of the rhs arguments do not match
97 template<class T_Matrix>
98 void imp_assert_V_MtV_rhs_sizes(const T_Matrix& gm_rhs1, BLAS_Cpp::Transp trans_rhs1
99  , const AbstractLinAlgPack::DVectorSlice& vs_rhs2)
100 {
101  typename T_Matrix::size_type
102  cols = (trans_rhs1 == BLAS_Cpp::no_trans) ? gm_rhs1.cols() : gm_rhs1.rows();
103 
104  if(cols != vs_rhs2.size())
105  throw std::length_error("V_MtV: The sizes of the rhs expression do not match");
106 }
107 
108 // Implementation of matrix-vector multiply (no transpose). Sizes are not checked.
109 template<class T_Matrix>
110 void imp_V_MtV_no_trans(AbstractLinAlgPack::DVectorSlice& vs_lhs, const T_Matrix& gm_rhs1
111  , const AbstractLinAlgPack::DVectorSlice& vs_rhs2)
112 {
113  typedef typename T_Matrix::size_type size_type;
114  size_type rows = gm_rhs1.rows();
116  for(size_type i = 1; i <= rows; ++i)
117  *itr_v_lhs++ = AbstractLinAlgPack::dot(vs_rhs2,gm_rhs1.row(i));
118 }
119 // Implementation of matrix-vector multiply (transpose). Sizes are not checked.
120 template<class T_Matrix>
121 void imp_V_MtV_trans(AbstractLinAlgPack::DVectorSlice& vs_lhs, const T_Matrix& gm_rhs1
122  , const AbstractLinAlgPack::DVectorSlice& vs_rhs2)
123 {
124  typedef typename T_Matrix::size_type size_type;
125  size_type cols = gm_rhs1.cols();
127  for(size_type j = 1; j <= cols; ++j)
128  *itr_v_lhs++ = AbstractLinAlgPack::dot(vs_rhs2,gm_rhs1.col(j));
129 }
130 
131 } // end namespace
132 
133 // Definitions of template functions for matrix-vector multiplication
134 
135 template<class T_Matrix>
136 void AbstractLinAlgPack::V_MtV(DVector& v_lhs, const T_Matrix& gm_rhs1, BLAS_Cpp::Transp trans_rhs1
137  , const DVectorSlice& vs_rhs2)
138 {
139  imp_assert_V_MtV_rhs_sizes(gm_rhs1,trans_rhs1,vs_rhs2);
140  v_lhs.resize( (trans_rhs1==BLAS_Cpp::no_trans) ? gm_rhs1.rows() : gm_rhs1.cols() );
141  DVectorSlice vs_lhs = v_lhs;
142  if(trans_rhs1 == BLAS_Cpp::no_trans)
143  imp_V_MtV_no_trans(vs_lhs,gm_rhs1,vs_rhs2);
144  else
145  imp_V_MtV_trans(vs_lhs,gm_rhs1,vs_rhs2);
146 }
147 
148 template<class T_Matrix>
149 void AbstractLinAlgPack::V_MtV(DVectorSlice& v_lhs, const T_Matrix& gm_rhs1, BLAS_Cpp::Transp trans_rhs1
150  , const DVectorSlice& vs_rhs2)
151 {
152  imp_assert_V_MtV_rhs_sizes(gm_rhs1,trans_rhs1,vs_rhs2);
153  DenseLinAlgPack::assert_resize_vs_lhs(v_lhs, (trans_rhs1==BLAS_Cpp::no_trans) ? gm_rhs1.rows() : gm_rhs1.cols());
154  if(trans_rhs1 == BLAS_Cpp::no_trans)
155  imp_V_MtV_no_trans(v_lhs,gm_rhs1,vs_rhs2);
156  else
157  imp_V_MtV_trans(v_lhs,gm_rhs1,vs_rhs2);
158 }
159 
160 #endif // MATRIX_VECTOR_TEMPLATE_OP_DEF_H
AbstractLinAlgPack::size_type size_type
size_type rows(size_type rows, size_type cols, BLAS_Cpp::Transp _trans)
Return rows of a possible transposed matrix.
void resize(size_type n, value_type val=value_type())
C++ Standard Library compatable iterator class for accesing nonunit stride arrays of data...
Not transposed.
void assert_gms_lhs(const DMatrixSlice &gms_lhs, size_type rows, size_type cols, BLAS_Cpp::Transp trans_rhs=BLAS_Cpp::no_trans)
size_type rows() const
Return the number of rows.
value_type dot(const Vector &v_rhs1, const Vector &v_rhs2)
result = v_rhs1' * v_rhs2
void assign(DMatrix &gm_lhs, const T_Matrix &gm_rhs, BLAS_Cpp::Transp trans_rhs)
gm_lhs = T_M (templated matrix type T_M)
DenseLinAlgPack::VectorSliceTmpl< value_type > DVectorSlice
size_type rows() const
Return the number of rows.
void resize_gm_lhs(DMatrix *gm_rhs, size_type rows, size_type cols, BLAS_Cpp::Transp trans_rhs)
Utility to resize a DMatrix to the size of a rhs matrix.
DVectorSlice col(size_type j)
Return DVectorSlice object representing the jth column (1-based; 1,2,..,#this->cols()#, or throw std::out_of_range)
Transp
TRANS.
size_type cols(size_type rows, size_type cols, BLAS_Cpp::Transp _trans)
Return columns of a possible transposed matrix.
void V_MtV(DVector &v_lhs, const T_Matrix &gm_rhs1, BLAS_Cpp::Transp trans_rhs1, const DVectorSlice &vs_rhs2)
v_lhs = T_M * vs_lhs (templated matrix type T_M)
DVectorSlice row(size_type i)
Return DVectorSlice object representing the ith row (1-based; 1,2,..,#this->rows()#, or throw std::out_of_range)