MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AbstractLinAlgPack_PermutationSerial.cpp
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 #include <assert.h>
43 
49 #include "Teuchos_Assert.hpp"
50 
51 namespace AbstractLinAlgPack {
52 
53 // Constructors / initializers
54 
55 PermutationSerial::PermutationSerial( size_type dim )
56  :space_(dim)
57 {}
58 
59 PermutationSerial::PermutationSerial(
60  const i_vector_ptr_t &perm
61  ,const i_vector_ptr_t &inv_perm
62  ,bool allocate_missing_perm
63  ,bool check_inv_perm
64  )
65 {
66  this->initialize(perm,inv_perm,allocate_missing_perm,check_inv_perm);
67 }
68 
69 void PermutationSerial::initialize_identity( size_type dim )
70 {
71  namespace rcp = MemMngPack;
72  space_.initialize(dim);
73  perm_ = Teuchos::null;
74  inv_perm_ = Teuchos::null;
75 }
76 
77 void PermutationSerial::initialize(
78  const i_vector_ptr_t &perm
79  ,const i_vector_ptr_t &inv_perm
80  ,bool allocate_missing_perm
81  ,bool check_inv_perm
82  )
83 {
85  perm.get() == NULL && inv_perm.get() == NULL, std::invalid_argument
86  ,"PermutationSerial::initialize(...) : Error!" );
87  if( perm.get() != NULL && inv_perm.get() != NULL ) {
89  perm->size() != inv_perm->size(), std::invalid_argument
90  ,"PermutationSerial::initialize(...) : Error!" );
91  if(check_inv_perm) {
92  // ToDo: Permform this check!
93  }
94  }
95  space_.initialize( perm.get() ? perm->size() : inv_perm->size() );
96  perm_ = perm;
97  inv_perm_ = inv_perm;
98  if( allocate_missing_perm && perm_.get() == NULL ) {
100  _perm = Teuchos::rcp(new IVector(inv_perm_->size()));
101  DenseLinAlgPack::inv_perm( *inv_perm_, _perm.get() );
102  perm_ = _perm;
103  }
104  if( allocate_missing_perm && inv_perm_.get() == NULL ) {
106  _inv_perm = Teuchos::rcp(new IVector(perm_->size()));
107  DenseLinAlgPack::inv_perm( *perm_, _inv_perm.get() );
108  inv_perm_ = _inv_perm;
109  }
110 }
111 
112 // Overridden from Permutation
113 
114 const VectorSpace& PermutationSerial::space() const
115 {
116  return space_;
117 }
118 
119 size_type PermutationSerial::dim() const
120 {
121  return space_.dim();
122 }
123 
124 bool PermutationSerial::is_identity() const
125 {
126  return perm_.get() == NULL && inv_perm_.get() == NULL;
127 }
128 
129 std::ostream& PermutationSerial::output(std::ostream& out) const
130 {
131  const size_type dim = this->dim();
132  out << "Serial " << dim << " x " << dim << " permtutation matrix:\n";
133  out << "perm =";
134  if( perm_.get() )
135  out << "\n" << *perm_;
136  else
137  out << " NULL\n";
138  out << "inv_perm =";
139  if( inv_perm_.get() )
140  out << "\n" << *inv_perm_;
141  else
142  out << " NULL\n";
143  return out;
144 }
145 
146 void PermutationSerial::permute(
147  BLAS_Cpp::Transp P_trans
148  ,const Vector &x
149  ,VectorMutable *y
150  ) const
151 {
152 #ifdef TEUCHOS_DEBUG
154  y == NULL, std::invalid_argument
155  ,"PermutationSerial::permute(P_trans,x,y) : Error!" );
156 #endif
157 #ifdef ABSTRACTLINALGPACK_ASSERT_COMPATIBILITY
158  bool is_compatible;
159  is_compatible = space_.is_compatible(x.space());
161  !is_compatible, std::invalid_argument
162  ,"PermutationSerial::permute(P_trans,x,y) : Error, "
163  "this->space().is_compatible(x.space()) returned false!" );
164  is_compatible = space_.is_compatible(y->space());
166  !is_compatible, std::invalid_argument
167  ,"PermutationSerial::permute(P_trans,x,y) : Error, "
168  "this->space().is_compatible(y->space()) returned false!" );
169 #endif
170  VectorDenseMutableEncap y_d(*y);
171  VectorDenseEncap x_d(x);
172  const IVector *p = NULL;
173  bool call_inv_perm = false;
174  if( ( p = perm_.get() ) != NULL ) {
175  if( P_trans == BLAS_Cpp::no_trans )
176  call_inv_perm = false;
177  else
178  call_inv_perm = true;
179  }
180  else if( ( p = inv_perm_.get() ) != NULL ) {
181  if( P_trans == BLAS_Cpp::no_trans )
182  call_inv_perm = true;
183  else
184  call_inv_perm = false;
185  }
186  if( p ) {
187  if( call_inv_perm )
188  DenseLinAlgPack::inv_perm_ele( x_d(), *p, &y_d() );
189  else
190  DenseLinAlgPack::perm_ele( x_d(), *p, &y_d() );
191  }
192  else {
193  // Just the identity permutation, nothing to do!
194  }
195 }
196 
197 void PermutationSerial::permute(
198  BLAS_Cpp::Transp P_trans
199  ,VectorMutable *y
200  ) const
201 {
202 #ifdef TEUCHOS_DEBUG
204  y == NULL, std::invalid_argument
205  ,"PermutationSerial::permute(P_trans,y) : Error!" );
206 #endif
207  VectorSpace::vec_mut_ptr_t
208  t = y->clone();
209  this->permute(P_trans,*t,y);
210 }
211 
212 } // end namespace AbstractLinAlgPack
AbstractLinAlgPack::size_type size_type
void inv_perm(const IVector &perm, IVector *inv_perm)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
T * get() const
std::ostream & output(std::ostream &o, const COOMatrix &coom)
Output stream function for COOMatrix.
Not transposed.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
std::ostream * out
void inv_perm_ele(const DVectorSlice &y, const IVector &perm, DVectorSlice *x)
Perform x = P'*y.
Transp
TRANS.
void perm_ele(const IVector &perm, DVectorSlice *vs)
Permute a DVectorSlice in place.