NOX_Playa_Vector.cpp

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

doxygen