NOX_Playa_Vector.cpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                 Playa: Programmable Linear Algebra
00005 //                 Copyright 2012 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
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 Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
00041 
00042 
00043 // $Id$ 
00044 // $Source$ 
00045 
00046 
00047 //   
00048 
00049 
00050 #include "NOX_Common.H"
00051 #include "NOX_Playa_Vector.hpp"
00052 #include "NOX_Utils.H"
00053 #include "NOX_Random.H" // for Random class
00054 
00055 #ifndef HAVE_TEUCHOS_EXPLICIT_INSTANTIATION
00056 #include "PlayaVectorImpl.hpp"
00057 #include "PlayaLinearOperatorImpl.hpp"
00058 #endif
00059 
00060 using namespace Teuchos;
00061 using std::runtime_error;
00062 using std::cout;
00063 using std::ostream;
00064 
00065 NOX::NOXPlaya::Vector::Vector(const NOX::NOXPlaya::Vector& source, 
00066        NOX::CopyType type)
00067   :
00068   precision(3) // 3 digits of accuracy is default
00069 {
00070  switch (type) 
00071  {
00072   case NOX::DeepCopy:
00073     x = source.x.copy();
00074     break;
00075 
00076   case NOX::ShapeCopy:
00077     x = ((source.x).space()).createMember();
00078     break;
00079 
00080   default:
00081     std::cerr << "NOX:Playa::Vector - invalid CopyType for copy constructor." << std::endl;
00082     throw "NOX Playa Error";
00083   }
00084 }
00085 
00086 NOX::NOXPlaya::Vector::Vector(const Playa::Vector<double>& source, 
00087        NOX::CopyType type)
00088   :
00089   precision(3) // 3 digits of accuracy is default
00090 {
00091   switch (type) 
00092  {
00093     
00094   case NOX::DeepCopy:
00095     x = source.copy();
00096     break;
00097 
00098   case NOX::ShapeCopy:
00099     x = ((source).space()).createMember();
00100     break;
00101 
00102   default:
00103     std::cerr << "NOX:Playa::Vector - invalid CopyType for copy constructor." << std::endl;
00104     throw "NOX Playa Error";
00105   }
00106 }
00107 
00108 
00109 NOX::NOXPlaya::Vector::Vector(const NOX::NOXPlaya::Vector& source, 
00110                          int numdigits,
00111        NOX::CopyType type)
00112   :
00113   precision(numdigits)
00114 {
00115  switch (type) 
00116  {
00117     
00118   case NOX::DeepCopy:
00119     x = source.x.copy();
00120     break;
00121 
00122   case NOX::ShapeCopy:
00123     x = ((source.x).space()).createMember();
00124     break;
00125 
00126   default:
00127     std::cerr << "NOX:Playa::Vector - invalid CopyType for copy constructor." << std::endl;
00128     throw "NOX Playa Error";
00129   }
00130 }
00131 
00132 NOX::NOXPlaya::Vector::Vector(const Playa::Vector<double>& source, 
00133                          int numdigits,
00134        NOX::CopyType type)
00135   :
00136   precision(numdigits)
00137 {
00138   switch (type) 
00139  {
00140     
00141   case NOX::DeepCopy:
00142     x = source.copy();
00143     break;
00144 
00145   case NOX::ShapeCopy:
00146     x = ((source).space()).createMember();
00147     break;
00148 
00149   default:
00150     std::cerr << "NOX:Playa::Vector - invalid CopyType for copy constructor." << std::endl;
00151     throw "NOX Playa Error";
00152   }
00153 }
00154 
00155 
00156 
00157 Playa::Vector<double>& NOX::NOXPlaya::Vector::getPlayaVector()
00158 {
00159   return x;
00160 }
00161  
00162 const Playa::Vector<double>& NOX::NOXPlaya::Vector::getPlayaVector() const
00163 {
00164   return x;
00165 }
00166 
00167 int NOX::NOXPlaya::Vector::getPrecision() const
00168 {
00169   return precision;
00170 }
00171 
00172 NOX::Abstract::Vector& NOX::NOXPlaya::Vector::operator=(
00173              const NOX::Abstract::Vector& source)
00174 {
00175   return operator=(dynamic_cast<const NOX::NOXPlaya::Vector&>(source));
00176 }
00177 
00178 NOX::Abstract::Vector& NOX::NOXPlaya::Vector::operator=(
00179              const NOX::NOXPlaya::Vector& source)
00180 {
00181   // in Playa operator= results in a shallow copy while 
00182   // acceptCopyOf(source.x) provides the deep copy we want
00183   x = source.getPlayaVector().copy();
00184   return *this;
00185 }
00186   
00187 
00188 NOX::Abstract::Vector& NOX::NOXPlaya::Vector::init(double value)
00189 {
00190   x.setToConstant(value);
00191   return *this;
00192 }
00193 
00194 
00195 NOX::Abstract::Vector& NOX::NOXPlaya::Vector::abs(
00196                const NOX::Abstract::Vector& base)
00197 {
00198   return abs(dynamic_cast<const NOX::NOXPlaya::Vector&>(base));
00199 }
00200 
00201 NOX::Abstract::Vector& NOX::NOXPlaya::Vector::abs(
00202                const NOX::NOXPlaya::Vector& base)
00203 {
00204   x.acceptCopyOf(base.x);
00205   x.selfAbs();
00206   return *this;
00207 }
00208 
00209 NOX::Abstract::Vector& NOX::NOXPlaya::Vector::reciprocal(
00210               const NOX::Abstract::Vector& base)
00211 {
00212   return reciprocal(dynamic_cast<const NOX::NOXPlaya::Vector&>(base));
00213 }
00214 
00215 NOX::Abstract::Vector& NOX::NOXPlaya::Vector::reciprocal(
00216               const NOX::NOXPlaya::Vector& base)
00217 {
00218   x.acceptCopyOf(base.x);
00219   x.selfReciprocal();
00220   return *this;
00221 }
00222 
00223 NOX::Abstract::Vector& NOX::NOXPlaya::Vector::scale(double alpha)
00224 {
00225   x.scale(alpha);
00226   return *this;
00227 }
00228 
00229 NOX::Abstract::Vector& NOX::NOXPlaya::Vector::update(
00230                  double alpha, 
00231                  const NOX::Abstract::Vector& a, 
00232                  double gamma)
00233 {
00234   return update( alpha, dynamic_cast<const NOX::NOXPlaya::Vector&>(a), gamma);
00235 }
00236 
00237 NOX::Abstract::Vector& NOX::NOXPlaya::Vector::update(
00238              double alpha, 
00239              const NOX::NOXPlaya::Vector& a, 
00240              double gamma)
00241 {
00242   x.update(alpha,a.x,gamma);
00243   return *this;
00244 }
00245 
00246 NOX::Abstract::Vector& NOX::NOXPlaya::Vector::update(
00247                 double alpha, 
00248                 const NOX::Abstract::Vector& a, 
00249                 double beta, 
00250                 const NOX::Abstract::Vector& b,
00251                 double gamma)
00252 {
00253   return update(alpha, dynamic_cast<const NOX::NOXPlaya::Vector&>(a), 
00254     beta, dynamic_cast<const NOX::NOXPlaya::Vector&>(b), gamma);
00255 }
00256 
00257 NOX::Abstract::Vector& NOX::NOXPlaya::Vector::update(
00258                  double alpha, 
00259                  const NOX::NOXPlaya::Vector& a, 
00260                  double beta, 
00261                  const NOX::NOXPlaya::Vector& b,
00262                  double gamma)
00263 {
00264   x.update(alpha,a.x,beta,b.x,gamma);
00265   return *this;
00266 }
00267 
00268 NOX::Abstract::Vector& NOX::NOXPlaya::Vector::scale(
00269                 const NOX::Abstract::Vector& a)
00270 {  
00271   return scale(dynamic_cast<const NOX::NOXPlaya::Vector&>(a));
00272 }
00273 
00274 NOX::Abstract::Vector& NOX::NOXPlaya::Vector::scale(const NOX::NOXPlaya::Vector& a)
00275 {  
00276   x.selfDotStar(a.x);
00277   return *this;
00278 }
00279 
00280 #ifdef TRILINOS_6
00281 NOX::Abstract::Vector* NOX::NOXPlaya::Vector::clone(NOX::CopyType type) const
00282 {
00283   return new NOX::NOXPlaya::Vector(*this, type);
00284 }
00285 #else
00286 RCP<NOX::Abstract::Vector> NOX::NOXPlaya::Vector::clone(NOX::CopyType type) const
00287 {
00288   return rcp(new NOX::NOXPlaya::Vector(*this, type));
00289 }
00290 #endif
00291 
00292 double NOX::NOXPlaya::Vector::norm(NOX::Abstract::Vector::NormType type) const
00293 {
00294   
00295   if (this->length() == 0)
00296     return 0.0;
00297 
00298   double value;     // final answer
00299 
00300   switch (type) 
00301   {
00302   case MaxNorm:
00303     value = x.normInf();
00304     break;
00305   case OneNorm:
00306     value = x.norm1();
00307     break;
00308   case TwoNorm:
00309   default:
00310     value = x.norm2();
00311    break;
00312   }
00313 
00314   return value;
00315 }
00316 
00317 double NOX::NOXPlaya::Vector::norm(const NOX::Abstract::Vector& weights) const
00318 {
00319   return norm(dynamic_cast<const NOX::NOXPlaya::Vector&>(weights));
00320 }
00321 
00322 double NOX::NOXPlaya::Vector::norm(const NOX::NOXPlaya::Vector& weights) const
00323 {
00324   if (weights.length() != this->length()) 
00325   {
00326     std::cerr << "NOX::NOXPlaya::Vector::norm - size mismatch for weights vector" << std::endl;
00327     throw "NOX::NOXPlaya Error";
00328   }
00329   return x.norm2(weights.getPlayaVector());
00330 }
00331 
00332 double NOX::NOXPlaya::Vector::dot(const NOX::Abstract::Vector& y) const
00333 {
00334   return dot(dynamic_cast<const NOX::NOXPlaya::Vector&>(y));
00335 }
00336 
00337 double NOX::NOXPlaya::Vector::innerProduct(const NOX::Abstract::Vector& y) const
00338 {
00339   return dot(dynamic_cast<const NOX::NOXPlaya::Vector&>(y));
00340 }
00341 
00342 double NOX::NOXPlaya::Vector::dot(const NOX::NOXPlaya::Vector& y) const
00343 {
00344   if (y.length() != this->length()) 
00345   {
00346     std::cerr << "NOX::NOXPlaya::Vector::dot - size mismatch for y vector" << std::endl;
00347     throw "NOX::NOXPlaya Error";
00348   }
00349 
00350   return x.dot(y.x);
00351 }
00352 
00353 NOX::size_type NOX::NOXPlaya::Vector::length() const
00354 {
00355   return (x.space()).dim();
00356 }
00357 
00358 
00359 
00360 ostream& NOX::NOXPlaya::Vector::leftshift(std::ostream& stream) const
00361 {
00362   x.print(stream);
00363   return stream;
00364 }
00365 
00366 namespace std
00367 {
00368 ostream& operator<<(std::ostream& stream, const NOX::NOXPlaya::Vector& v)
00369 {
00370   return v.leftshift(stream);
00371 }
00372 }
00373 
00374 void NOX::NOXPlaya::Vector::print() const
00375 {
00376   cout << *this << std::endl;
00377 }

Site Contact