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
DenseLinAlgPack_PermVecMat.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 "DenseLinAlgPack_PermVecMat.hpp"
43 #include "DenseLinAlgPack_DMatrixClass.hpp"
44 #include "DenseLinAlgPack_IVector.hpp"
45 
46 #ifdef TEUCHOS_DEBUG // Debug only!
47 bool DenseLinAlgPack::PermVecMat_print = false;
48 #include <iostream>
49 #include "DenseLinAlgPack_PermOut.hpp"
50 #include "DenseLinAlgPack_DVectorOut.hpp"
51 #endif
52 
53 // Local assert function
54 namespace {
55 inline void i_assert_perm_size(size_t size1, size_t size2)
56 {
57 #ifdef LINALGPACK_CHECK_RHS_SIZES
58  if(size1 != size2)
59  throw std::length_error("The size of the permutation vector is not correct");
60 #endif
61 }
62 
63 } // end namespace
64 
65 void DenseLinAlgPack::identity_perm(IVector* perm) {
66  if(!perm->size())
67  throw std::length_error("DenseLinAlgPack::identity_perm(): perm must be sized");
68  IVector::iterator itr_perm = perm->begin();
69  for(size_type i = 1; i <= perm->size(); ++i)
70  *itr_perm++ = i;
71 }
72 
73 void DenseLinAlgPack::inv_perm(const IVector& perm, IVector* inv_perm) {
74  inv_perm->resize(perm.size());
75  for(size_type i = 1; i <= perm.size(); ++i)
76  (*inv_perm)(perm(i)) = i;
77 }
78 
79 void DenseLinAlgPack::perm_ele(const IVector& perm, DVectorSlice* vs)
80 {
81  i_assert_perm_size(vs->dim(),perm.size());
82  DVector tmp_v(vs->dim());
83  DVector::iterator v_itr = tmp_v.begin(),
84  v_itr_end = tmp_v.end();
85  IVector::const_iterator perm_itr = perm.begin();
86  // Copy the elements in the permed order into the temp vector
87  for(; v_itr != v_itr_end; ++v_itr, ++perm_itr)
88  *v_itr = (*vs)(*perm_itr);
89 
90  // Copy the permed vector back
91  (*vs) = tmp_v();
92 }
93 
94 void DenseLinAlgPack::perm_ele(const DVectorSlice& x, const IVector& perm, DVectorSlice* y)
95 {
96  i_assert_perm_size(x.dim(),perm.size());
97  i_assert_perm_size(y->dim(),perm.size());
98 
99  IVector::const_iterator
100  perm_itr = perm.begin();
101  DVectorSlice::iterator
102  y_itr = y->begin(),
103  y_end = y->end();
104  while(y_itr != y_end)
105  *y_itr++ = x(*perm_itr++);
106 }
107 
108 void DenseLinAlgPack::inv_perm_ele(const DVectorSlice& y, const IVector& perm, DVectorSlice* x)
109 {
110  i_assert_perm_size(y.dim(),perm.size());
111  i_assert_perm_size(x->dim(),perm.size());
112 #ifdef TEUCHOS_DEBUG
113  if( PermVecMat_print ) {
114  std::cerr
115  << "enter inv_perm_ele(y,perm,x):\n"
116  << "before:\n"
117  << "y =\n" << y
118  << "perm =\n" << perm
119  << "x =\n" << *x;
120  }
121 #endif
122  DVectorSlice::const_iterator
123  y_itr = y.begin(),
124  y_end = y.end();
125  IVector::const_iterator
126  perm_itr = perm.begin();
127  while(y_itr != y_end)
128  (*x)(*perm_itr++) = *y_itr++;
129 #ifdef TEUCHOS_DEBUG
130  if( PermVecMat_print ) {
131  std::cerr
132  << "inv_perm_ele(y,perm,x):\n"
133  << "after:\n"
134  << "x =\n" << *x
135  << "exit inv_perm_ele(...) ...\n";
136  }
137 #endif
138 }
139 
140 void DenseLinAlgPack::perm_rows(const IVector& row_perm, DMatrixSlice* gms)
141 {
142  i_assert_perm_size(gms->rows(),row_perm.size());
143  DMatrix tmp_gm(gms->rows(),gms->cols());
144  DMatrixSlice::size_type rows = gms->rows(), i;
145  // Copy the rows in the correct order into the temp matrix.
146  for(i = 1; i <= rows; ++i)
147  tmp_gm.row(i) = gms->row(row_perm(i));
148  // Copy the permed matrix back
149  (*gms) = tmp_gm();
150 }
151 
152 void DenseLinAlgPack::perm_cols(const IVector& col_perm, DMatrixSlice* gms)
153 {
154  i_assert_perm_size(gms->cols(),col_perm.size());
155  DMatrix tmp_gm(gms->rows(),gms->cols());
156  DMatrixSlice::size_type cols = gms->cols(), i;
157  // Copy the columns in the correct order into the temp matrix.
158  for(i = 1; i <= cols; ++i)
159  tmp_gm.col(i) = gms->col(col_perm(i));
160  // Copy the permed matrix back
161  (*gms) = tmp_gm();
162 }
163 
164 void DenseLinAlgPack::perm_rows_cols(const IVector& row_perm, const IVector& col_perm
165  , DMatrixSlice* gms)
166 {
167  i_assert_perm_size(gms->rows(),row_perm.size());
168  i_assert_perm_size(gms->cols(),col_perm.size());
169  DMatrix tmp_gm(gms->rows(),gms->cols());
170  DMatrixSlice::size_type rows = gms->rows(), cols = gms->cols(), i;
171  // Copy the rows in the correct order into the temp matrix.
172  for(i = 1; i <= rows; ++i)
173  tmp_gm.row(i) = gms->row(row_perm(i));
174  // Copy the columns in the correct order back into matrix.
175  for(i = 1; i <= cols; ++i)
176  gms->col(i) = tmp_gm.col(col_perm(i));
177 }
size_type rows(size_type rows, size_type cols, BLAS_Cpp::Transp _trans)
size_t size_type
size_type cols(size_type rows, size_type cols, BLAS_Cpp::Transp _trans)