MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ConstrainedOptPack_initialize_Q_R_Q_X.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 <vector>
43 
46 
48  size_type n_R
49  ,size_type n_X
50  ,const size_type i_x_free[]
51  ,const size_type i_x_fixed[]
52  ,bool test_setup
53  ,size_type Q_R_row_i[]
54  ,size_type Q_R_col_j[]
55  ,GenPermMatrixSlice *Q_R
56  ,size_type Q_X_row_i[]
57  ,size_type Q_X_col_j[]
58  ,GenPermMatrixSlice *Q_X
59  )
60 {
61  namespace GPMSIP = AbstractLinAlgPack::GenPermMatrixSliceIteratorPack;
62  const size_type
63  n = n_R + n_X;
64  //
65  // / i_x_free[lR-1] ,if lR = l_x_XR[i] > 0
66  // Setup l_x_XR[i] = | not set yet ,if l_x_XR[i] == 0
67  // \ i_x_fixed[lX-1] ,if lX = l_x_XR[i] < 0
68  //
69  typedef std::vector<long int> l_x_XR_t; // ToDo: use temporary workspace
70  l_x_XR_t l_x_XR(n);
71  std::fill( l_x_XR.begin(), l_x_XR.end(), 0 );
72  // Setup the fixed portion of l_x_XR[] first.
73  bool i_x_fixed_is_sorted = true;
74  if(test_setup) {
75  size_type last_set = 0;
76  const size_type *i_x_X = i_x_fixed;
77  for( long int lX = 1; lX <= n_X; ++lX, ++i_x_X ) {
78  if( *i_x_X < 1 || *i_x_X > n ) {
79  std::ostringstream omsg;
80  omsg
81  << "initialize_Q_R_Q_X(...) : Error, "
82  << "i_x_fixed[" << (lX-1) << "] = "
83  << (*i_x_X) << " is out of bounds";
84  throw std::invalid_argument( omsg.str() );
85  }
86  const long int l = l_x_XR[*i_x_X-1];
87  if( l != 0 ) {
88  std::ostringstream omsg;
89  omsg
90  << "initialize_Q_R_Q_X(...) : Error, "
91  << "Duplicate entries for i_x_fixed[" << (lX-1) << "] = "
92  << (*i_x_X) << " == i_x_fixed[" << (-l-1) << "]";
93  throw std::invalid_argument( omsg.str() );
94  }
95  l_x_XR[*i_x_X-1] = -lX;
96  if( *i_x_X < last_set )
97  i_x_fixed_is_sorted = false;
98  last_set = *i_x_X;
99  }
100  }
101  else {
102  size_type last_set = 0;
103  const size_type *i_x_X = i_x_fixed;
104  for( size_type lX = 1; lX <= n_X; ++lX, ++i_x_X ) {
105  l_x_XR[*i_x_X-1] = -lX;
106  if( *i_x_X < last_set )
107  i_x_fixed_is_sorted = false;
108  last_set = *i_x_X;
109  }
110  }
111  // Now setup the free portion of l_x_XR[]
112  bool i_x_free_is_sorted = true;
113  if( i_x_free == NULL ) {
114  long int
115  *l_x_XR_itr = &l_x_XR[0];
116  size_type lR = 0;
117  for( size_type i = 1; i <= n; ++i, ++l_x_XR_itr ) {
118  if(*l_x_XR_itr == 0) {
119  ++lR;
120  *l_x_XR_itr = lR;
121  }
122  }
123  TEUCHOS_TEST_FOR_EXCEPT( !( lR == n_R ) );
124  }
125  else {
126  if(test_setup) {
127  size_type last_set = 0;
128  const size_type *i_x_R = i_x_free;
129  for( size_type lR = 1; lR <= n_R; ++lR, ++i_x_R ) {
130  if( *i_x_R < 1 || *i_x_R > n ) {
131  std::ostringstream omsg;
132  omsg
133  << "initialize_Q_R_Q_X(...) : Error, "
134  << "i_x_free[" << (lR-1) << "] = "
135  << (*i_x_R) << " is out of bounds";
136  throw std::invalid_argument( omsg.str() );
137  }
138  const long int l = l_x_XR[*i_x_R-1];
139  if( l != 0 ) {
140  std::ostringstream omsg;
141  omsg
142  << "initialize_Q_R_Q_X(...) : Error, "
143  << "Duplicate entries for i_x_free[" << (lR-1) << "] = "
144  << (*i_x_R) << " == "
145  << (l < 0 ? "i_x_fixed[" : "i_x_free[")
146  << (l < 0 ? -l : l) << "]";
147  throw std::invalid_argument( omsg.str() );
148  }
149  l_x_XR[*i_x_R-1] = lR;
150  if( *i_x_R < last_set )
151  i_x_free_is_sorted = false;
152  last_set = *i_x_R;
153  }
154  }
155  else {
156  size_type last_set = 0;
157  const size_type *i_x_R = i_x_free;
158  for( size_type lR = 1; lR <= n_R; ++lR, ++i_x_R ) {
159  l_x_XR[*i_x_R-1] = lR;
160  if( *i_x_R < last_set )
161  i_x_free_is_sorted = false;
162  last_set = *i_x_R;
163  }
164  }
165  }
166  //
167  // Setup Q_R and Q_X
168  //
169  if( i_x_free_is_sorted && Q_R_row_i == NULL && l_x_XR[n_R-1] == n_R ) {
170  //
171  // Here Q_R = [ I; 0 ] so lets set it
172  //
173  Q_R->initialize(n,n_R,GenPermMatrixSlice::IDENTITY_MATRIX);
174  // Now we just have to set Q_X which may or may not be sorted
175  const long int
176  *l_x_X = &l_x_XR[n_R-1] + 1; // Just in case n_X == 0
177  size_type
178  *row_i_itr = Q_X_row_i,
179  *col_j_itr = Q_X_col_j;
180  for( size_type iX = n_R+1; iX <= n; ++iX, ++l_x_X, ++row_i_itr, ++col_j_itr ) {
181  *row_i_itr = iX; // This is sorted of course
182  *col_j_itr = -(*l_x_X); // This might be sorted
183  TEUCHOS_TEST_FOR_EXCEPT( !( *l_x_X < 0 ) );
184  }
185  Q_X->initialize(
186  n,n_X,n_X,0,0,i_x_fixed_is_sorted?GPMSIP::BY_ROW_AND_COL:GPMSIP::BY_ROW
187  ,Q_X_row_i,Q_X_col_j,test_setup);
188  }
189  else {
190  //
191  // Here i_x_free[] and i_x_fixed[] may be sorted by Q_R != [ I; 0 ]
192  //
193  if( n_X > 0 && Q_R_row_i == NULL )
194  throw std::invalid_argument(
195  "initialize_Q_R_Q_X(...) : Error, "
196  "Q_R_row_i != NULL since Q_R != [ I; 0 ]" );
197  const long int
198  *l_x = &l_x_XR[0];
199  size_type
200  *Q_R_row_i_itr = Q_R_row_i, // Okay if NULL and n_R == 0
201  *Q_R_col_j_itr = Q_R_col_j,
202  *Q_X_row_i_itr = Q_X_row_i, // Okay if NULL and n_X == 0
203  *Q_X_col_j_itr = Q_X_col_j;
204  for( size_type i = 1; i <= n; ++i, ++l_x ) {
205  const long int l = *l_x;
206  TEUCHOS_TEST_FOR_EXCEPT( !( l != 0 ) );
207  if( l > 0 ) {
208  *Q_R_row_i_itr++ = i;
209  *Q_R_col_j_itr++ = l;
210  }
211  else {
212  *Q_X_row_i_itr++ = i;
213  *Q_X_col_j_itr++ = -l;
214  }
215  }
216  TEUCHOS_TEST_FOR_EXCEPT( !( Q_R_row_i_itr - Q_R_row_i == n_R ) );
217  TEUCHOS_TEST_FOR_EXCEPT( !( Q_X_row_i_itr - Q_X_row_i == n_X ) );
218  Q_R->initialize(
219  n,n_R,n_R,0,0,i_x_free_is_sorted?GPMSIP::BY_ROW_AND_COL:GPMSIP::BY_ROW
220  ,Q_R_row_i,Q_R_col_j,test_setup);
221  Q_X->initialize(
222  n,n_X,n_X,0,0,i_x_fixed_is_sorted?GPMSIP::BY_ROW_AND_COL:GPMSIP::BY_ROW
223  ,Q_X_row_i,Q_X_col_j,test_setup);
224  }
225 }
AbstractLinAlgPack::size_type size_type
void initialize_Q_R_Q_X(size_type n_R, size_type n_X, const size_type i_x_free[], const size_type i_x_fixed[], bool test_setup, size_type Q_R_row_i[], size_type Q_R_col_j[], GenPermMatrixSlice *Q_R, size_type Q_X_row_i[], size_type Q_X_col_j[], GenPermMatrixSlice *Q_X)
Initialize GenPermMatrixSlice mapping matrices for Q_R and Q_X.
int n
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)