Sandia Home Sandia Home
Main Page | Publications | Downloads | Configuration | Running the Code | Solver Parameters | FAQ | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

APPSPACK_Vector.cpp

Go to the documentation of this file.
00001 // $Id: APPSPACK_Vector.cpp,v 1.35 2006/06/30 01:42:08 tgkolda Exp $ 
00002 // $Source: /space/CVS-Acro/acro/packages/appspack/appspack/src/APPSPACK_Vector.cpp,v $ 
00003 
00004 //@HEADER
00005 // ************************************************************************
00006 // 
00007 //          APPSPACK: Asynchronous Parallel Pattern Search
00008 //                 Copyright (2003) Sandia Corporation
00009 // 
00010 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00011 // license for use of this work by or on behalf of the U.S. Government.
00012 // 
00013 // This library is free software; you can redistribute it and/or modify
00014 // it under the terms of the GNU Lesser General Public License as
00015 // published by the Free Software Foundation; either version 2.1 of the
00016 // License, or (at your option) any later version.
00017 //  
00018 // This library is distributed in the hope that it will be useful, but
00019 // WITHOUT ANY WARRANTY; without even the implied warranty of
00020 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021 // Lesser General Public License for more details.
00022 //                                                                                 
00023 // You should have received a copy of the GNU Lesser General Public
00024 // License along with this library; if not, write to the Free Software
00025 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00026 // USA.                                                                           .
00027 // 
00028 // Questions? Contact Tammy Kolda (tgkolda@sandia.gov) 
00029 // 
00030 // ************************************************************************
00031 //@HEADER
00032 
00038 #include "APPSPACK_Vector.hpp"
00039 #include "APPSPACK_Print.hpp"
00040 #include "APPSPACK_LAPACK_Wrappers.hpp"
00041 #include "APPSPACK_Float.hpp"
00042 
00043 APPSPACK::Vector::Vector()
00044 {
00045 }
00046 
00047 APPSPACK::Vector::Vector(int n) : vec(n)
00048 {
00049 }
00050 
00051 APPSPACK::Vector::Vector(int n, double val) : vec(n, val)
00052 {
00053 }
00054 
00055 APPSPACK::Vector::Vector(int n, double *x) : vec(x,x+n)
00056 {
00057 }
00058 
00059 APPSPACK::Vector::Vector(const APPSPACK::Vector& x) : vec(x.vec)
00060 {
00061 }
00062 
00063 APPSPACK::Vector::Vector(const vector<double>& x) : vec(x)
00064 {
00065 }
00066 
00067 APPSPACK::Vector::~Vector()
00068 {
00069 }
00070 
00071 void APPSPACK::Vector::resize(int n)
00072 {
00073   vec.resize(n);
00074 }
00075 
00076 void APPSPACK::Vector::reserve(int n)
00077 {
00078   vec.reserve(n);
00079 }
00080 
00081 void APPSPACK::Vector::push_back(double a)
00082 {
00083   vec.push_back(a);
00084 }
00085 
00086 void APPSPACK::Vector::assign(int n, double alpha)
00087 {
00088   vec.assign(n,alpha);
00089 }
00090 
00091 void APPSPACK::Vector::append(const Vector& x)
00092 {
00093   if (x.size() > 0)
00094     vec.insert(vec.end(), x.vec.begin(), x.vec.end());
00095 }
00096 
00097 void APPSPACK::Vector::append(int n, double alpha)
00098 {
00099   vec.insert(vec.end(), n, alpha);
00100 }
00101 
00102 void APPSPACK::Vector::erase(int i)
00103 {
00104   vec.erase(vec.begin()+i);
00105 }
00106 
00107 APPSPACK::Vector& APPSPACK::Vector::operator=(const Vector& x)
00108 {
00109   vec = x.vec;
00110   return *this;
00111 }
00112 
00113 int APPSPACK::Vector::size() const
00114 {
00115   return vec.size();
00116 }
00117 
00118 bool APPSPACK::Vector::empty() const
00119 {
00120   return vec.empty();
00121 }
00122 
00123 const vector<double>& APPSPACK::Vector::getStlVector() const
00124 {
00125   return vec;
00126 }
00127 
00128 double APPSPACK::Vector::operator[](int i) const
00129 {
00130   return vec[i];
00131 }
00132 
00133 bool APPSPACK::Vector::operator==(const Vector& a) const
00134 {
00135   return (vec == a.vec);
00136 }
00137 
00138 bool APPSPACK::Vector::operator!=(const Vector& a) const
00139 {
00140   return (vec != a.vec);
00141 }
00142 
00143 void  APPSPACK::Vector::copyToBool(vector<bool>& b) const
00144 {
00145   b.resize(vec.size());
00146   for (int i = 0; i < vec.size(); i ++)
00147     b[i] = (vec[i] == 0) ? false : true;
00148 }
00149 
00150 ostream& APPSPACK::Vector::leftshift(ostream& stream) const
00151 {
00152   for (int i = 0; i < vec.size(); i ++) 
00153   {
00154     stream << APPSPACK::Print::formatDouble(vec[i]) << " ";
00155   }
00156   return stream;
00157 }
00158 
00159 ostream& APPSPACK::Vector::leftshift(ostream& stream, int precision) const
00160 {
00161   for (int i = 0; i < vec.size(); i ++) 
00162   {
00163     stream << APPSPACK::Print::formatDouble(vec[i], precision) << " ";
00164   }
00165   return stream;
00166 }
00167 
00168 ostream& operator<<(ostream& stream, const APPSPACK::Vector& x)
00169 { 
00170   stream << "[";
00171   x.leftshift(stream);
00172   stream << "]";
00173   return stream;
00174 }
00175 
00176 void APPSPACK::Vector::zero()
00177 {
00178   vec.assign(vec.size(), 0);
00179 }
00180 
00181 void APPSPACK::Vector::scale(double alpha)
00182 {
00183   for (int i = 0; i < vec.size(); i++)
00184     vec[i] = alpha * vec[i];
00185 }
00186 
00187 void APPSPACK::Vector::scale(const Vector& s)
00188 {
00189   if (s.vec.size() != vec.size())
00190   {
00191     cerr << "APPSPACK::Vector::scale - Size mismatch" << endl;
00192     throw " APPSPACK Error";
00193   }
00194   
00195   for (int i = 0; i < vec.size(); i++)
00196     vec[i] = vec[i] * s.vec[i];
00197 }
00198 
00199 double APPSPACK::Vector::norm() const
00200 {
00201   double norm=0;
00202   for (int i = 0; i < vec.size(); i++)
00203     norm += vec[i]*vec[i];
00204   return sqrt(norm);
00205 }
00206 
00207 double APPSPACK::Vector::min() const
00208 {
00209   if (vec.empty())
00210   {
00211     cerr << "APPSPACK::Vector::min - empty vector" << endl;
00212     throw "APPSPACK Error";
00213   }
00214 
00215   double minvi = vec[0];
00216   for (int i = 1; i < vec.size(); i++)
00217     minvi = (minvi < vec[i]) ? minvi : vec[i];
00218   return minvi;
00219 }
00220 
00221 double APPSPACK::Vector::max() const
00222 {
00223   if (vec.empty())
00224   {
00225     cerr << "APPSPACK::Vector::max - empty vector" << endl;
00226     throw "APPSPACK Error";
00227   }
00228 
00229   double maxvi = vec[0];
00230   for (int i = 1; i < vec.size(); i++)
00231     maxvi = (maxvi > vec[i]) ? maxvi : vec[i];
00232   return maxvi;
00233 }
00234 
00235 double APPSPACK::Vector::dot(const Vector& x) const
00236 {
00237   if (x.vec.size() != vec.size())
00238   {
00239     cerr << "APPSPACK::Vector::dot - Size mismatch" << endl;
00240     throw " APPSPACK Error";
00241   }
00242   
00243   double z = 0;
00244 
00245 #ifdef HAVE_LAPACK
00246   int inc = 1;
00247   int n   = x.size();
00248   // Copy vectors to avoid const compilation errors.
00249   Vector& xc = const_cast<Vector&>(x);
00250   Vector& vc = const_cast<Vector&>(*this);
00251   z = DDOT_F77(&n, &vc[0], &inc, &xc[0], &inc);
00252 #else  
00253   for (int i = 0; i < x.size(); i ++)
00254     z += vec[i] * x.vec[i];
00255 #endif 
00256 
00257   return z;
00258 }
00259 
00260 APPSPACK::Vector& APPSPACK::Vector::operator+=(const Vector& x)
00261 {
00262   if (x.vec.size() != vec.size())
00263   {
00264     cerr << "APPSPACK::Vector::operator+= - Size mismatch" << endl;
00265     throw " APPSPACK Error";
00266   }
00267   
00268   for (int i = 0; i < vec.size(); i++)
00269     vec[i] += x.vec[i];
00270 
00271   return *this;
00272 }
00273 
00274 APPSPACK::Vector& APPSPACK::Vector::operator-=(const Vector& x)
00275 {
00276   if (x.vec.size() != vec.size())
00277   {
00278     cerr << "APPSPACK::Vector::operator-= - Size mismatch" << endl;
00279     throw " APPSPACK Error";
00280   }
00281   
00282   for (int i = 0; i < vec.size(); i++)
00283     vec[i] -= x.vec[i];
00284 
00285   return *this;
00286 }
00287 
00288 double& APPSPACK::Vector::operator[](int i)
00289 {
00290   return vec[i];
00291 }

 

© Sandia Corporation | Site Contact | Privacy and Security

Generated on Fri Feb 16 10:33:35 2007 for APPSPACK 5.0.1 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2002