MOOCHO (Single Doxygen Collection) Version of the Day
DenseLinAlgPack_TestVectorOp.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00005 //                  Copyright (2003) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00038 // 
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include <iomanip>
00043 #include <ostream>
00044 #include <vector>
00045 #include <typeinfo>
00046 
00047 #include <math.h>
00048 
00049 #include "DenseLinAlgPack_TestDenseLinAlgPack.hpp"
00050 #include "DenseLinAlgPack_DVectorClass.hpp"
00051 #include "DenseLinAlgPack_DVectorOp.hpp"
00052 #include "DenseLinAlgPack_DVectorOut.hpp"
00053 #include "DenseLinAlgPack_MatVecCompare.hpp"
00054 
00055 namespace {
00056 
00057 // 2/10/00: See TestVectorClass.cpp
00058 inline
00059 bool update_success( bool result, bool *success ) {
00060   return TestingHelperPack::update_success( result, success );
00061 }
00062 
00063 } // end namespace
00064 
00065 bool DenseLinAlgPack::TestingPack::TestVectorOp(std::ostream* out)
00066 {
00067 
00068   using DenseLinAlgPack::comp;
00069   using DenseLinAlgPack::sqrt_eps;
00070 
00071   bool success = true;
00072   bool result, result1, result2;
00073 
00074   value_type
00075     rval;
00076 
00077   if(out)
00078     *out  << "\n***********************************"
00079         << "\n*** Testing VectorOp Operations ***"
00080         << "\n***********************************\n"
00081         << std::boolalpha;
00082 
00083   try {
00084 
00085   if(out) *out << "\nLet alpha1 = 2.0, alpha2 = 0.5, v1val = 1.0, v2val = 2.0;\n";
00086   const value_type
00087     alpha1 = 2.0,
00088     alpha2 = 0.5,
00089     v1val = 1.0,
00090     v2val = 2.0;
00091 
00092   const int n = 6;
00093 
00094   if(out) *out << "\nVector v(n), v1(v1val,n), v2(v2val,n);\n";
00095   DVector v(n), v1(v1val,n), v2(v2val,n);
00096 
00097   // /////////////////////////////////
00098   // Test Algebraic Functions
00099 
00100   if(out) *out << "\n***\n*** Testing DVectorSlice modifying functions \n***\n";
00101 
00102   if(out) *out << "\nv = 1.0; Vp_S(&v()," << alpha1 << ");\n";
00103   v = 1.0;
00104   Vp_S(&v(),alpha1);
00105   result = update_success( comp( v, 1.0 + alpha1 ), &success );
00106   if(out)
00107     *out  << "v =\n" << v
00108         << "v == 1.0 + " << alpha1 << " : " << result << std::endl;
00109 
00110   if(out) *out << "\nv = 1.0; Vt_S(&v()," << alpha1 << ");\n";
00111   v = 1.0;
00112   Vt_S(&v(),alpha1);
00113   result = update_success( comp( v, alpha1 ), &success );
00114   if(out)
00115     *out  << "v =\n" << v
00116         << "v == " << alpha1 << " : " << result << std::endl;
00117 
00118   if(out) *out << "\nv = 1.0; Vp_StV(&v()," << alpha1 << ",v1());\n";
00119   v = 1.0;
00120   Vp_StV(&v(),alpha1,v1());
00121   result = update_success( comp( v, 1.0 + alpha1 ), &success );
00122   if(out)
00123     *out  << "v =\n" << v
00124         << "v == "<<v1val<<" + "<<alpha1<<" : " << result << std::endl;
00125 
00126   // DVectorSlice as lhs
00127 
00128   if(out) *out << "\n***\n*** Testing DVectorSlice as lhs algebric operations\n***\n";
00129 
00130   if(out) *out << "\nv = -10.0; V_VpV(&v(),v1(),v2());\n";
00131   v = -10;
00132   V_VpV(&v(),v1(),v2());
00133   result = update_success( comp( v, v1val+v2val ), &success );
00134   if(out)
00135     *out  << "v =\n" << v
00136         << "v == "<<v1val<<" + "<<v2val<<" : " << result << std::endl;
00137 
00138   if(out) *out << "\nv = -10.0; V_VmV(&v(),v1(),v2());\n";
00139   v = -10;
00140   V_VmV(&v(),v1(),v2());
00141   result = update_success( comp( v, v1val-v2val ), &success );
00142   if(out)
00143     *out  << "v =\n" << v
00144         << "v == "<<v1val<<" - "<<v2val<<" : " << result << std::endl;
00145 
00146   if(out) *out << "\nv = -10.0; V_mV(&v(),v1());\n";
00147   v = -10;
00148   V_mV(&v(),v1());
00149   result = update_success( comp( v, -v1val ), &success );
00150   if(out)
00151     *out  << "v =\n" << v
00152         << "v == "<<-v1val<<" : " << result << std::endl;
00153 
00154   if(out) *out << "\nv = -10.0; V_StV(&v(),"<<alpha2<<",v2());\n";
00155   v = -10;
00156   V_StV(&v(),alpha2,v2());
00157   result = update_success( comp( v, alpha2*v2val ), &success );
00158   if(out)
00159     *out  << "v =\n" << v
00160         << "v == "<<alpha2<<" * "<<v2val<<" : " << result << std::endl;
00161 
00162   // DVector as lhs
00163 
00164   if(out) *out << "\n***\n*** Testing DVector as lhs algebric operations\n***\n";
00165 
00166   if(out) *out << "\nv.free(); V_VpV(&v,v1(),v2());\n";
00167   v.free();
00168   V_VpV(&v,v1(),v2());
00169   result = update_success( comp( v, v1val+v2val ), &success );
00170   if(out)
00171     *out  << "v =\n" << v
00172         << "v == "<<v1val<<" + "<<v2val<<" : " << result << std::endl;
00173 
00174   if(out) *out << "\nv.free(); V_VmV(&v,v1(),v2());\n";
00175   v.free();
00176   V_VmV(&v,v1(),v2());
00177   result = update_success( comp( v, v1val-v2val ), &success );
00178   if(out)
00179     *out  << "v =\n" << v
00180         << "v == "<<v1val<<" - "<<v2val<<" : " << result << std::endl;
00181 
00182   if(out) *out << "\nv.free(); V_mV(&v,v1());\n";
00183   v.free();
00184   V_mV(&v,v1());
00185   result = update_success( comp( v, -v1val ), &success );
00186   if(out)
00187     *out  << "v =\n" << v
00188         << "v == "<<-v1val<<" : " << result << std::endl;
00189 
00190   if(out) *out << "\nv.free(); V_StV(&v,"<<alpha2<<",v2());\n";
00191   v.free();
00192   V_StV(&v,alpha2,v2());
00193   result = update_success( comp( v, alpha2*v2val ), &success );
00194   if(out)
00195     *out  << "v =\n" << v
00196         << "v == "<<alpha2<<" * "<<v2val<<" : " << result << std::endl;
00197 
00198   // ////////////////////////////////
00199   // Test Elementary math functions
00200   if(out) *out << "\n***\n*** Testing Elementary math functions\n***\n";
00201   
00202   // ToDo: implement at some point
00203   if(out) *out << "\nWarning! Not Tested!\n";
00204 
00205   // DVectorSlice as lhs
00206 
00207   // DVector as lhs
00208 
00209   // /////////////////////////////////
00210   // Test Scalar Returning Functions
00211 
00212   if(out) *out << "\n***\n*** Testing Scalar Returning Functions\n***\n";
00213 
00214   if(out) *out << "\n(dot(v1(),v2()) -> ";
00215   rval = dot(v1(),v2());
00216   result = update_success( ::fabs( rval - v1val*v2val*n ) < sqrt_eps, &success );
00217   if(out) *out << rval <<") == " << v1val*v2val*n << " : " << result << std::endl;
00218 
00219   if(out) *out << "\n(norm_1(v2()) -> ";
00220   rval = norm_1(v2());
00221   result = update_success( ::fabs( rval - v2val*n ) < sqrt_eps, &success );
00222   if(out) *out << rval <<") == " << v2val*n << " : " << result << std::endl;
00223 
00224   if(out) *out << "\n(norm_2(v2()) -> ";
00225   rval = norm_2(v2());
00226   result = update_success( ::fabs( rval - v2val*::sqrt((value_type)n) ) < sqrt_eps, &success );
00227   if(out) *out << rval <<") == " << v2val*::sqrt((value_type)n) << " : " << result << std::endl;
00228 
00229   if(out) *out << "\n(norm_inf(v2()) -> ";
00230   rval = norm_inf(v2());
00231   result = update_success( ::fabs( rval - v2val ) < sqrt_eps, &success );
00232   if(out) *out << rval <<") == " << v2val << " : " << result << std::endl;
00233 
00234   if(out) *out << "\nv1(n/2) = 2*v1val;\n(max(v1()) -> ";
00235   v1(n/2) = 2*v1val;
00236   rval = max(v1());
00237   result = update_success( ::fabs( rval - 2*v1val ) < sqrt_eps, &success );
00238   if(out) *out << rval <<") == " << 2*v1val << " : " << result << std::endl;
00239 
00240   if(out) *out << "\nv1(n/2+1) = -2*v1val;\n(min(v1()) -> ";
00241   v1(n/2+1) = -2*v1val;
00242   rval = min(v1());
00243   result = update_success( ::fabs( rval - (-2*v1val) ) < sqrt_eps, &success );
00244   if(out) *out << rval <<") == " << (-2*v1val) << " : " << result << std::endl;
00245 
00246   } // end try
00247   catch( const std::exception& excpt ) {
00248     success = false;
00249     if(out)
00250       (*out)  << "\nError, a standard exception was thrown: "
00251           << typeName(excpt) << ": "
00252           << excpt.what() << std::endl; 
00253   }
00254   catch(...) {
00255     success = false;
00256     if(out)
00257       (*out)  << "\nError, an unknown exception was thrown\n";
00258   }
00259 
00260   if(out) {
00261     if(success)
00262       (*out)
00263         << "\n*** Congradulations, VectorOp operations seem to check out. ***\n";
00264     else
00265       (*out)
00266         << "\n*** Oops, all of the tests for VectorOp operations "
00267           "where not successful. ***\n";
00268   }
00269 
00270 
00271   return success;
00272 }
00273 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines