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

Site Contact