MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DenseLinAlgPack_TestVectorOp.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 <iomanip>
43 #include <ostream>
44 #include <vector>
45 #include <typeinfo>
46 
47 #include <math.h>
48 
54 
55 namespace {
56 
57 // 2/10/00: See TestVectorClass.cpp
58 inline
59 bool update_success( bool result, bool *success ) {
60  return TestingHelperPack::update_success( result, success );
61 }
62 
63 } // end namespace
64 
66 {
67 
70 
71  bool success = true;
72  bool result, result1, result2;
73 
75  rval;
76 
77  if(out)
78  *out << "\n***********************************"
79  << "\n*** Testing VectorOp Operations ***"
80  << "\n***********************************\n"
81  << std::boolalpha;
82 
83  try {
84 
85  if(out) *out << "\nLet alpha1 = 2.0, alpha2 = 0.5, v1val = 1.0, v2val = 2.0;\n";
86  const value_type
87  alpha1 = 2.0,
88  alpha2 = 0.5,
89  v1val = 1.0,
90  v2val = 2.0;
91 
92  const int n = 6;
93 
94  if(out) *out << "\nVector v(n), v1(v1val,n), v2(v2val,n);\n";
95  DVector v(n), v1(v1val,n), v2(v2val,n);
96 
97  // /////////////////////////////////
98  // Test Algebraic Functions
99 
100  if(out) *out << "\n***\n*** Testing DVectorSlice modifying functions \n***\n";
101 
102  if(out) *out << "\nv = 1.0; Vp_S(&v()," << alpha1 << ");\n";
103  v = 1.0;
104  Vp_S(&v(),alpha1);
105  result = update_success( comp( v, 1.0 + alpha1 ), &success );
106  if(out)
107  *out << "v =\n" << v
108  << "v == 1.0 + " << alpha1 << " : " << result << std::endl;
109 
110  if(out) *out << "\nv = 1.0; Vt_S(&v()," << alpha1 << ");\n";
111  v = 1.0;
112  Vt_S(&v(),alpha1);
113  result = update_success( comp( v, alpha1 ), &success );
114  if(out)
115  *out << "v =\n" << v
116  << "v == " << alpha1 << " : " << result << std::endl;
117 
118  if(out) *out << "\nv = 1.0; Vp_StV(&v()," << alpha1 << ",v1());\n";
119  v = 1.0;
120  Vp_StV(&v(),alpha1,v1());
121  result = update_success( comp( v, 1.0 + alpha1 ), &success );
122  if(out)
123  *out << "v =\n" << v
124  << "v == "<<v1val<<" + "<<alpha1<<" : " << result << std::endl;
125 
126  // DVectorSlice as lhs
127 
128  if(out) *out << "\n***\n*** Testing DVectorSlice as lhs algebric operations\n***\n";
129 
130  if(out) *out << "\nv = -10.0; V_VpV(&v(),v1(),v2());\n";
131  v = -10;
132  V_VpV(&v(),v1(),v2());
133  result = update_success( comp( v, v1val+v2val ), &success );
134  if(out)
135  *out << "v =\n" << v
136  << "v == "<<v1val<<" + "<<v2val<<" : " << result << std::endl;
137 
138  if(out) *out << "\nv = -10.0; V_VmV(&v(),v1(),v2());\n";
139  v = -10;
140  V_VmV(&v(),v1(),v2());
141  result = update_success( comp( v, v1val-v2val ), &success );
142  if(out)
143  *out << "v =\n" << v
144  << "v == "<<v1val<<" - "<<v2val<<" : " << result << std::endl;
145 
146  if(out) *out << "\nv = -10.0; V_mV(&v(),v1());\n";
147  v = -10;
148  V_mV(&v(),v1());
149  result = update_success( comp( v, -v1val ), &success );
150  if(out)
151  *out << "v =\n" << v
152  << "v == "<<-v1val<<" : " << result << std::endl;
153 
154  if(out) *out << "\nv = -10.0; V_StV(&v(),"<<alpha2<<",v2());\n";
155  v = -10;
156  V_StV(&v(),alpha2,v2());
157  result = update_success( comp( v, alpha2*v2val ), &success );
158  if(out)
159  *out << "v =\n" << v
160  << "v == "<<alpha2<<" * "<<v2val<<" : " << result << std::endl;
161 
162  // DVector as lhs
163 
164  if(out) *out << "\n***\n*** Testing DVector as lhs algebric operations\n***\n";
165 
166  if(out) *out << "\nv.free(); V_VpV(&v,v1(),v2());\n";
167  v.free();
168  V_VpV(&v,v1(),v2());
169  result = update_success( comp( v, v1val+v2val ), &success );
170  if(out)
171  *out << "v =\n" << v
172  << "v == "<<v1val<<" + "<<v2val<<" : " << result << std::endl;
173 
174  if(out) *out << "\nv.free(); V_VmV(&v,v1(),v2());\n";
175  v.free();
176  V_VmV(&v,v1(),v2());
177  result = update_success( comp( v, v1val-v2val ), &success );
178  if(out)
179  *out << "v =\n" << v
180  << "v == "<<v1val<<" - "<<v2val<<" : " << result << std::endl;
181 
182  if(out) *out << "\nv.free(); V_mV(&v,v1());\n";
183  v.free();
184  V_mV(&v,v1());
185  result = update_success( comp( v, -v1val ), &success );
186  if(out)
187  *out << "v =\n" << v
188  << "v == "<<-v1val<<" : " << result << std::endl;
189 
190  if(out) *out << "\nv.free(); V_StV(&v,"<<alpha2<<",v2());\n";
191  v.free();
192  V_StV(&v,alpha2,v2());
193  result = update_success( comp( v, alpha2*v2val ), &success );
194  if(out)
195  *out << "v =\n" << v
196  << "v == "<<alpha2<<" * "<<v2val<<" : " << result << std::endl;
197 
198  // ////////////////////////////////
199  // Test Elementary math functions
200  if(out) *out << "\n***\n*** Testing Elementary math functions\n***\n";
201 
202  // ToDo: implement at some point
203  if(out) *out << "\nWarning! Not Tested!\n";
204 
205  // DVectorSlice as lhs
206 
207  // DVector as lhs
208 
209  // /////////////////////////////////
210  // Test Scalar Returning Functions
211 
212  if(out) *out << "\n***\n*** Testing Scalar Returning Functions\n***\n";
213 
214  if(out) *out << "\n(dot(v1(),v2()) -> ";
215  rval = dot(v1(),v2());
216  result = update_success( ::fabs( rval - v1val*v2val*n ) < sqrt_eps, &success );
217  if(out) *out << rval <<") == " << v1val*v2val*n << " : " << result << std::endl;
218 
219  if(out) *out << "\n(norm_1(v2()) -> ";
220  rval = norm_1(v2());
221  result = update_success( ::fabs( rval - v2val*n ) < sqrt_eps, &success );
222  if(out) *out << rval <<") == " << v2val*n << " : " << result << std::endl;
223 
224  if(out) *out << "\n(norm_2(v2()) -> ";
225  rval = norm_2(v2());
226  result = update_success( ::fabs( rval - v2val*::sqrt((value_type)n) ) < sqrt_eps, &success );
227  if(out) *out << rval <<") == " << v2val*::sqrt((value_type)n) << " : " << result << std::endl;
228 
229  if(out) *out << "\n(norm_inf(v2()) -> ";
230  rval = norm_inf(v2());
231  result = update_success( ::fabs( rval - v2val ) < sqrt_eps, &success );
232  if(out) *out << rval <<") == " << v2val << " : " << result << std::endl;
233 
234  if(out) *out << "\nv1(n/2) = 2*v1val;\n(max(v1()) -> ";
235  v1(n/2) = 2*v1val;
236  rval = max(v1());
237  result = update_success( ::fabs( rval - 2*v1val ) < sqrt_eps, &success );
238  if(out) *out << rval <<") == " << 2*v1val << " : " << result << std::endl;
239 
240  if(out) *out << "\nv1(n/2+1) = -2*v1val;\n(min(v1()) -> ";
241  v1(n/2+1) = -2*v1val;
242  rval = min(v1());
243  result = update_success( ::fabs( rval - (-2*v1val) ) < sqrt_eps, &success );
244  if(out) *out << rval <<") == " << (-2*v1val) << " : " << result << std::endl;
245 
246  } // end try
247  catch( const std::exception& excpt ) {
248  success = false;
249  if(out)
250  (*out) << "\nError, a standard exception was thrown: "
251  << typeName(excpt) << ": "
252  << excpt.what() << std::endl;
253  }
254  catch(...) {
255  success = false;
256  if(out)
257  (*out) << "\nError, an unknown exception was thrown\n";
258  }
259 
260  if(out) {
261  if(success)
262  (*out)
263  << "\n*** Congradulations, VectorOp operations seem to check out. ***\n";
264  else
265  (*out)
266  << "\n*** Oops, all of the tests for VectorOp operations "
267  "where not successful. ***\n";
268  }
269 
270 
271  return success;
272 }
273 
std::string typeName(const T &t)
void Vt_S(VectorMutable *v_lhs, const value_type &alpha)
v_lhs *= alpha
void Vp_StV(VectorMutable *v_lhs, const value_type &alpha, const Vector &v_rhs)
v_lhs = alpha * v_rhs + v_lhs
void V_StV(VectorMutable *v_lhs, value_type alpha, const V &V_rhs)
v_lhs = alpha * V_rhs.
void V_mV(VectorMutable *v_lhs, const V &V_rhs)
v_lhs = - V_rhs.
void V_VpV(VectorMutable *v_lhs, const V1 &V1_rhs1, const V2 &V2_rhs2)
std::ostream * out
value_type norm_inf(const SparseVectorSlice< T_Ele > &sv_rhs)
result = ||sv_rhs||inf (BLAS IxAMAX)
value_type dot(const Vector &v_rhs1, const Vector &v_rhs2)
result = v_rhs1' * v_rhs2
void sqrt(DVectorSlice *vs_lhs, const DVectorSlice &vs_rhs)
vs_lhs = sqrt(vs_rhs)
value_type norm_2(const SparseVectorSlice< T_Ele > &sv_rhs)
result = ||sv_rhs||2 (BLAS xNRM2)
void Vp_S(VectorMutable *v_lhs, const value_type &alpha)
v_lhs += alpha
FortranTypes::f_dbl_prec value_type
Typedef for the value type of elements that is used for the library.
bool update_success(bool result_check, bool *success)
Helper function for updating a flag for if an operation returned false.
value_type norm_1(const SparseVectorSlice< T_Ele > &sv_rhs)
result = ||sv_rhs||1 (BLAS xASUM)
bool comp(const DVectorSlice &vs1, const DVectorSlice &vs2)
int n
void V_VmV(VectorMutable *v_lhs, const V1 &V1_rhs1, const V2 &V2_rhs2)
v_lhs = V_rhs1 - V_rhs2.