TSFDenseSerialVector.cpp
Go to the documentation of this file.
00001 #include "TSFDenseSerialVector.hpp"
00002 #include "Teuchos_Utils.hpp"
00003 
00004 
00005 
00006 
00007 using namespace TSFExtended;
00008 using namespace Teuchos;
00009 
00010 
00011 
00012 
00013 void DenseSerialVector::setScalar(const double& a)
00014 {
00015   int n = length();
00016   double* yy = x();
00017   for (int i=0; i<n; i++, yy++)
00018     {
00019       *yy = a;
00020     }
00021 }
00022 
00023 
00024 void DenseSerialVector::negate()
00025 {
00026   int n = length();
00027   double* yy = x();
00028   for (int i=0; i<n; i++, yy++)
00029     {
00030       *yy = -(*yy);
00031     }
00032 }
00033 
00034 void DenseSerialVector::add(const DenseSerialVector& other)
00035 {
00036   int n = length();
00037   checkLength(other, "add");
00038 
00039   blasObject().AXPY(n, 1.0, other.x(), 1, x(), 1);
00040 }
00041 
00042 
00043 
00044 void DenseSerialVector::subtract(const DenseSerialVector& other)
00045 {
00046   int n = length();
00047   checkLength(other, "subtract");
00048 
00049   blasObject().AXPY(n, -1.0, other.x(), 1, x(), 1);
00050 }
00051 
00052 
00053 void DenseSerialVector::daxpy(const DenseSerialVector& other, const double& a)
00054 {
00055   int n = length();
00056   checkLength(other, "daxpy");
00057   blasObject().AXPY(n, a, other.x(), 1, x(), 1);
00058 }
00059 
00060 
00061 void DenseSerialVector::eMult(const DenseSerialVector& other)
00062 {
00063   int n = length();
00064   checkLength(other, "eMult");
00065   double* yy = x();
00066   const double* xx = other.x(); 
00067   for (int i=0; i<n; i++)
00068     {
00069       yy[i] *= xx[i];
00070     }
00071 }
00072 
00073 
00074 /* added by ptb  */
00075 void DenseSerialVector::abs() 
00076 {
00077   int n = length();
00078   double* xx = x();
00079   for (int i = 0; i < n; i++)
00080     {
00081       xx[i] = ::fabs(xx[i]);
00082     }
00083 }
00084 
00085 /* added by ptb  */
00086 double DenseSerialVector::max() const 
00087 {
00088   int n = length();
00089   const double* xx = x();
00090   double ret = xx[0];
00091   for (int i = 1; i < n; i++)
00092     {
00093       if (xx[i] > ret) ret = xx[i];
00094     }
00095   return ret;
00096 }
00097 
00098 /* added by ptb  */
00099 double DenseSerialVector::min() const 
00100 {
00101   int n = length();
00102   const double* xx = x();
00103   double ret = xx[0];
00104   for (int i = 1; i < n; i++)
00105     {
00106       if (xx[i] < ret) ret = xx[i];
00107     }
00108   return ret;
00109 }
00110 
00111 /* added by ptb  */
00112 void DenseSerialVector::dotStar(const DenseSerialVector& y, 
00113                                 const DenseSerialVector& z)
00114 {
00115   int n = length();
00116   double* xx = x();
00117   const double* yy = y.x();
00118   const double* zz = z.x();
00119   for (int i = 0; i < n; i++)
00120   {
00121     xx[i] = yy[i] * zz[i];
00122   }
00123 }
00124 
00125 /* added by ptb  */
00126 void DenseSerialVector::dotSlash(const DenseSerialVector& y, 
00127                                  const DenseSerialVector& z)
00128 {
00129   int n = length();
00130   double* xx = x();
00131   const double* yy = y.x();
00132   const double* zz = z.x();
00133   for (int i = 0; i < n; i++)
00134   {
00135     xx[i] = yy[i] / zz[i];
00136   }
00137 }
00138 
00139 
00140 void DenseSerialVector::scalarMult(const double& a)
00141 {
00142   int n = length();
00143   blasObject().SCAL(n, a, x(), 1);
00144 }
00145 
00146 void DenseSerialVector::scalarPow(const double& a)
00147 {
00148   int n = length();
00149   if (Teuchos::Utils::chop(a-1.0)==0) return;
00150 
00151   double* yy = x();
00152 
00153   if (Teuchos::Utils::chop(a+1.0)==0)
00154     { 
00155       for (int i=0; i<n; i++)
00156         {
00157           yy[i] = 1.0/(yy[i]);
00158         }
00159     }
00160   else if (Teuchos::Utils::chop(a-2.0)==0)
00161     { 
00162       for (int i=0; i<n; i++)
00163         {
00164           yy[i] *= yy[i];
00165         }
00166     }
00167   else if (Teuchos::Utils::chop(a-3.0)==0)
00168     { 
00169       for (int i=0; i<n; i++)
00170         {
00171           double tmp = yy[i]*yy[i];
00172           yy[i] *= tmp;
00173         }
00174     }
00175   else if (Teuchos::Utils::chop(a-4.0)==0)
00176     { 
00177       for (int i=0; i<n; i++)
00178         {
00179           double tmp = yy[i]*yy[i];
00180           yy[i] = tmp*tmp;
00181         }
00182     }
00183   else
00184     {
00185       for (int i=0; i<n; i++)
00186         {
00187           yy[i] = pow(yy[i], a);
00188         }
00189     }
00190 }
00191 
00192 
00193 double DenseSerialVector::dot(const DenseSerialVector& other) const 
00194 {
00195   int n = length();
00196   checkLength(other, "dot product");
00197 
00198   double rtn = 0.0;
00199   const double* xx = x();
00200   const double* yy = other.x();
00201   
00202   rtn = blasObject().DOT(n, xx, 1, yy, 1);
00203   return rtn;
00204 }
00205 
00206 double DenseSerialVector::norm2Squared() const
00207 {
00208   int n = length();
00209   double rtn = 0.0;
00210 
00211   rtn = blasObject().NRM2(n, x(), 1);
00212 
00213   return rtn*rtn;
00214 }
00215 
00216 
00217 double DenseSerialVector::sumElements() const
00218 {
00219   int n = length();
00220   double rtn = 0.0;
00221   const double* xx = x();
00222 
00223 
00224   for (int i=0; i<n; i++)
00225     {
00226       rtn += xx[i];
00227     }
00228   return rtn;
00229 }
00230 
00231 double DenseSerialVector::maxNorm() const
00232 {
00233   int n = length();
00234   double mx = -1.0e30;
00235   const double* xx = x();
00236 
00237   for (int i=0; i<n; i++)
00238     {
00239       double tmp = fabs(xx[i]);
00240       if (tmp > mx) mx = tmp;
00241     }
00242   return mx;
00243 }
00244 
00245 
00246 
00247 string DenseSerialVector::summary() const
00248 {
00249   int n = length();
00250   return "DenseSerialVector[dim=" + Teuchos::toString(n) + "]";
00251 }
00252 
00253 
00254 
00255       
00256 
00257 
00258 
00259 
00260 
00261 
00262 
00263 
00264 

Site Contact