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 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include <iomanip>
00030 #include <ostream>
00031 #include <vector>
00032 #include <typeinfo>
00033 
00034 #include <math.h>
00035 
00036 #include "DenseLinAlgPack_TestDenseLinAlgPack.hpp"
00037 #include "DenseLinAlgPack_DVectorClass.hpp"
00038 #include "DenseLinAlgPack_DVectorOp.hpp"
00039 #include "DenseLinAlgPack_DVectorOut.hpp"
00040 #include "DenseLinAlgPack_MatVecCompare.hpp"
00041 
00042 namespace {
00043 
00044 // 2/10/00: See TestVectorClass.cpp
00045 inline
00046 bool update_success( bool result, bool *success ) {
00047   return TestingHelperPack::update_success( result, success );
00048 }
00049 
00050 } // end namespace
00051 
00052 bool DenseLinAlgPack::TestingPack::TestVectorOp(std::ostream* out)
00053 {
00054 
00055   using DenseLinAlgPack::comp;
00056   using DenseLinAlgPack::sqrt_eps;
00057 
00058   bool success = true;
00059   bool result, result1, result2;
00060 
00061   value_type
00062     rval;
00063 
00064   if(out)
00065     *out  << "\n***********************************"
00066         << "\n*** Testing VectorOp Operations ***"
00067         << "\n***********************************\n"
00068         << std::boolalpha;
00069 
00070   try {
00071 
00072   if(out) *out << "\nLet alpha1 = 2.0, alpha2 = 0.5, v1val = 1.0, v2val = 2.0;\n";
00073   const value_type
00074     alpha1 = 2.0,
00075     alpha2 = 0.5,
00076     v1val = 1.0,
00077     v2val = 2.0;
00078 
00079   const int n = 6;
00080 
00081   if(out) *out << "\nVector v(n), v1(v1val,n), v2(v2val,n);\n";
00082   DVector v(n), v1(v1val,n), v2(v2val,n);
00083 
00084   // /////////////////////////////////
00085   // Test Algebraic Functions
00086 
00087   if(out) *out << "\n***\n*** Testing DVectorSlice modifying functions \n***\n";
00088 
00089   if(out) *out << "\nv = 1.0; Vp_S(&v()," << alpha1 << ");\n";
00090   v = 1.0;
00091   Vp_S(&v(),alpha1);
00092   result = update_success( comp( v, 1.0 + alpha1 ), &success );
00093   if(out)
00094     *out  << "v =\n" << v
00095         << "v == 1.0 + " << alpha1 << " : " << result << std::endl;
00096 
00097   if(out) *out << "\nv = 1.0; Vt_S(&v()," << alpha1 << ");\n";
00098   v = 1.0;
00099   Vt_S(&v(),alpha1);
00100   result = update_success( comp( v, alpha1 ), &success );
00101   if(out)
00102     *out  << "v =\n" << v
00103         << "v == " << alpha1 << " : " << result << std::endl;
00104 
00105   if(out) *out << "\nv = 1.0; Vp_StV(&v()," << alpha1 << ",v1());\n";
00106   v = 1.0;
00107   Vp_StV(&v(),alpha1,v1());
00108   result = update_success( comp( v, 1.0 + alpha1 ), &success );
00109   if(out)
00110     *out  << "v =\n" << v
00111         << "v == "<<v1val<<" + "<<alpha1<<" : " << result << std::endl;
00112 
00113   // DVectorSlice as lhs
00114 
00115   if(out) *out << "\n***\n*** Testing DVectorSlice as lhs algebric operations\n***\n";
00116 
00117   if(out) *out << "\nv = -10.0; V_VpV(&v(),v1(),v2());\n";
00118   v = -10;
00119   V_VpV(&v(),v1(),v2());
00120   result = update_success( comp( v, v1val+v2val ), &success );
00121   if(out)
00122     *out  << "v =\n" << v
00123         << "v == "<<v1val<<" + "<<v2val<<" : " << result << std::endl;
00124 
00125   if(out) *out << "\nv = -10.0; V_VmV(&v(),v1(),v2());\n";
00126   v = -10;
00127   V_VmV(&v(),v1(),v2());
00128   result = update_success( comp( v, v1val-v2val ), &success );
00129   if(out)
00130     *out  << "v =\n" << v
00131         << "v == "<<v1val<<" - "<<v2val<<" : " << result << std::endl;
00132 
00133   if(out) *out << "\nv = -10.0; V_mV(&v(),v1());\n";
00134   v = -10;
00135   V_mV(&v(),v1());
00136   result = update_success( comp( v, -v1val ), &success );
00137   if(out)
00138     *out  << "v =\n" << v
00139         << "v == "<<-v1val<<" : " << result << std::endl;
00140 
00141   if(out) *out << "\nv = -10.0; V_StV(&v(),"<<alpha2<<",v2());\n";
00142   v = -10;
00143   V_StV(&v(),alpha2,v2());
00144   result = update_success( comp( v, alpha2*v2val ), &success );
00145   if(out)
00146     *out  << "v =\n" << v
00147         << "v == "<<alpha2<<" * "<<v2val<<" : " << result << std::endl;
00148 
00149   // DVector as lhs
00150 
00151   if(out) *out << "\n***\n*** Testing DVector as lhs algebric operations\n***\n";
00152 
00153   if(out) *out << "\nv.free(); V_VpV(&v,v1(),v2());\n";
00154   v.free();
00155   V_VpV(&v,v1(),v2());
00156   result = update_success( comp( v, v1val+v2val ), &success );
00157   if(out)
00158     *out  << "v =\n" << v
00159         << "v == "<<v1val<<" + "<<v2val<<" : " << result << std::endl;
00160 
00161   if(out) *out << "\nv.free(); V_VmV(&v,v1(),v2());\n";
00162   v.free();
00163   V_VmV(&v,v1(),v2());
00164   result = update_success( comp( v, v1val-v2val ), &success );
00165   if(out)
00166     *out  << "v =\n" << v
00167         << "v == "<<v1val<<" - "<<v2val<<" : " << result << std::endl;
00168 
00169   if(out) *out << "\nv.free(); V_mV(&v,v1());\n";
00170   v.free();
00171   V_mV(&v,v1());
00172   result = update_success( comp( v, -v1val ), &success );
00173   if(out)
00174     *out  << "v =\n" << v
00175         << "v == "<<-v1val<<" : " << result << std::endl;
00176 
00177   if(out) *out << "\nv.free(); V_StV(&v,"<<alpha2<<",v2());\n";
00178   v.free();
00179   V_StV(&v,alpha2,v2());
00180   result = update_success( comp( v, alpha2*v2val ), &success );
00181   if(out)
00182     *out  << "v =\n" << v
00183         << "v == "<<alpha2<<" * "<<v2val<<" : " << result << std::endl;
00184 
00185   // ////////////////////////////////
00186   // Test Elementary math functions
00187   if(out) *out << "\n***\n*** Testing Elementary math functions\n***\n";
00188   
00189   // ToDo: implement at some point
00190   if(out) *out << "\nWarning! Not Tested!\n";
00191 
00192   // DVectorSlice as lhs
00193 
00194   // DVector as lhs
00195 
00196   // /////////////////////////////////
00197   // Test Scalar Returning Functions
00198 
00199   if(out) *out << "\n***\n*** Testing Scalar Returning Functions\n***\n";
00200 
00201   if(out) *out << "\n(dot(v1(),v2()) -> ";
00202   rval = dot(v1(),v2());
00203   result = update_success( ::fabs( rval - v1val*v2val*n ) < sqrt_eps, &success );
00204   if(out) *out << rval <<") == " << v1val*v2val*n << " : " << result << std::endl;
00205 
00206   if(out) *out << "\n(norm_1(v2()) -> ";
00207   rval = norm_1(v2());
00208   result = update_success( ::fabs( rval - v2val*n ) < sqrt_eps, &success );
00209   if(out) *out << rval <<") == " << v2val*n << " : " << result << std::endl;
00210 
00211   if(out) *out << "\n(norm_2(v2()) -> ";
00212   rval = norm_2(v2());
00213   result = update_success( ::fabs( rval - v2val*::sqrt((value_type)n) ) < sqrt_eps, &success );
00214   if(out) *out << rval <<") == " << v2val*::sqrt((value_type)n) << " : " << result << std::endl;
00215 
00216   if(out) *out << "\n(norm_inf(v2()) -> ";
00217   rval = norm_inf(v2());
00218   result = update_success( ::fabs( rval - v2val ) < sqrt_eps, &success );
00219   if(out) *out << rval <<") == " << v2val << " : " << result << std::endl;
00220 
00221   if(out) *out << "\nv1(n/2) = 2*v1val;\n(max(v1()) -> ";
00222   v1(n/2) = 2*v1val;
00223   rval = max(v1());
00224   result = update_success( ::fabs( rval - 2*v1val ) < sqrt_eps, &success );
00225   if(out) *out << rval <<") == " << 2*v1val << " : " << result << std::endl;
00226 
00227   if(out) *out << "\nv1(n/2+1) = -2*v1val;\n(min(v1()) -> ";
00228   v1(n/2+1) = -2*v1val;
00229   rval = min(v1());
00230   result = update_success( ::fabs( rval - (-2*v1val) ) < sqrt_eps, &success );
00231   if(out) *out << rval <<") == " << (-2*v1val) << " : " << result << std::endl;
00232 
00233   } // end try
00234   catch( const std::exception& excpt ) {
00235     success = false;
00236     if(out)
00237       (*out)  << "\nError, a standard exception was thrown: "
00238           << typeName(excpt) << ": "
00239           << excpt.what() << std::endl; 
00240   }
00241   catch(...) {
00242     success = false;
00243     if(out)
00244       (*out)  << "\nError, an unknown exception was thrown\n";
00245   }
00246 
00247   if(out) {
00248     if(success)
00249       (*out)
00250         << "\n*** Congradulations, VectorOp operations seem to check out. ***\n";
00251     else
00252       (*out)
00253         << "\n*** Oops, all of the tests for VectorOp operations "
00254           "where not successful. ***\n";
00255   }
00256 
00257 
00258   return success;
00259 }
00260 

Generated on Tue Oct 20 12:51:46 2009 for MOOCHO (Single Doxygen Collection) by doxygen 1.4.7