- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# /home/coin/SVN-release/Clp-1.5.0/CoinUtils/src/CoinDenseVector.hpp

Go to the documentation of this file.

    00001 // Copyright (C) 2003, International Business Machines
    00002 // Corporation and others.  All Rights Reserved.
    00003 #ifndef CoinDenseVector_H
    00004 #define CoinDenseVector_H
    00005
    00006 #if defined(_MSC_VER)
    00007 // Turn off compiler warning about long names
    00008 #  pragma warning(disable:4786)
    00009 #endif
    00010
    00011 #include <cassert>
    00012 #include <cmath>
    00013 #include "CoinHelperFunctions.hpp"
    00014
    00015 //#############################################################################
    00021     template <typename T> void
    00022     CoinDenseVectorUnitTest(T dummy);
    00023
    00024 //#############################################################################
    00063 template <typename T> class CoinDenseVector {
    00064 private:
    00067
    00068    int nElements_;
    00070    T * elements_;
    00072
    00073 public:
    00076
    00077    inline int getNumElements() const { return nElements_; }
    00078    inline int size() const { return nElements_; }
    00080    inline const T * getElements() const { return elements_; }
    00082    inline T * getElements() { return elements_; }
    00084
    00085    //-------------------------------------------------------------------
    00086    // Set indices and elements
    00087    //-------------------------------------------------------------------
    00090
    00091    void clear();
    00093    CoinDenseVector & operator=(const CoinDenseVector &);
    00095    T & operator[](int index) const;
    00096
    00101    void setVector(int size, const T * elems);
    00102
    00103
    00105    void setConstant(int size, T elems);
    00106
    00107
    00111    void setElement(int index, T element);
    00115    void resize(int newSize, T fill=T());
    00116
    00118    void append(const CoinDenseVector &);
    00120
    00123
    00124    inline T oneNorm() const {
    00125      T norm = 0;
    00126      for (int i=0; i<nElements_; i++)
    00127        norm += CoinAbs(elements_[i]);
    00128      return norm;
    00129    }
    00131    inline double twoNorm() const {
    00132      double norm = 0.;
    00133      for (int i=0; i<nElements_; i++)
    00134        norm += elements_[i] * elements_[i];
    00135      // std namespace removed because it was causing a compile
    00136      // problem with Microsoft Visual C++
    00137      return /*std::*/sqrt(norm);
    00138    }
    00140    inline T infNorm() const {
    00141      T norm = 0;
    00142      for (int i=0; i<nElements_; i++)
    00143        norm = CoinMax(norm, CoinAbs(elements_[i]));
    00144      return norm;
    00145    }
    00147    inline T sum() const {
    00148      T sume = 0;
    00149      for (int i=0; i<nElements_; i++)
    00150        sume += elements_[i];
    00151      return sume;
    00152    }
    00154    inline void scale(T factor) {
    00155      for (int i=0; i<nElements_; i++)
    00156        elements_[i] *= factor;
    00157      return;
    00158    }
    00160
    00163
    00164    void operator+=(T value);
    00166    void operator-=(T value);
    00168    void operator*=(T value);
    00170    void operator/=(T value);
    00172
    00176    CoinDenseVector();
    00178    CoinDenseVector(int size, const T * elems);
    00180    CoinDenseVector(int size, T element=T());
    00182    CoinDenseVector(const CoinDenseVector &);
    00183
    00185    ~CoinDenseVector ();
    00187
    00188 private:
    00191
    00192    void gutsOfSetVector(int size, const T * elems);
    00194    void gutsOfSetConstant(int size, T value);
    00196 };
    00197
    00198 //#############################################################################
    00199
    00207
    00208 template <typename T> inline
    00209 CoinDenseVector<T> operator+(const CoinDenseVector<T>& op1,
    00210                              const CoinDenseVector<T>& op2){
    00211   assert(op1.size() == op2.size());
    00212   int size = op1.size();
    00213   CoinDenseVector<T> op3(size);
    00214   const T *elements1 = op1.getElements();
    00215   const T *elements2 = op2.getElements();
    00216   T *elements3 = op3.getElements();
    00217   for(int i=0; i<size; i++)
    00218     elements3[i] = elements1[i] + elements2[i];
    00219   return op3;
    00220 }
    00221
    00223 template <typename T> inline
    00224 CoinDenseVector<T> operator-(const CoinDenseVector<T>& op1,
    00225                              const CoinDenseVector<T>& op2){
    00226   assert(op1.size() == op2.size());
    00227   int size = op1.size();
    00228   CoinDenseVector<T> op3(size);
    00229   const T *elements1 = op1.getElements();
    00230   const T *elements2 = op2.getElements();
    00231   T *elements3 = op3.getElements();
    00232   for(int i=0; i<size; i++)
    00233     elements3[i] = elements1[i] - elements2[i];
    00234   return op3;
    00235 }
    00236
    00237
    00239 template <typename T> inline
    00240 CoinDenseVector<T> operator*(const CoinDenseVector<T>& op1,
    00241                           const CoinDenseVector<T>& op2){
    00242   assert(op1.size() == op2.size());
    00243   int size = op1.size();
    00244   CoinDenseVector<T> op3(size);
    00245   const T *elements1 = op1.getElements();
    00246   const T *elements2 = op2.getElements();
    00247   T *elements3 = op3.getElements();
    00248   for(int i=0; i<size; i++)
    00249     elements3[i] = elements1[i] * elements2[i];
    00250   return op3;
    00251 }
    00252
    00254 template <typename T> inline
    00255 CoinDenseVector<T> operator/(const CoinDenseVector<T>& op1,
    00256                           const CoinDenseVector<T>& op2){
    00257   assert(op1.size() == op2.size());
    00258   int size = op1.size();
    00259   CoinDenseVector<T> op3(size);
    00260   const T *elements1 = op1.getElements();
    00261   const T *elements2 = op2.getElements();
    00262   T *elements3 = op3.getElements();
    00263   for(int i=0; i<size; i++)
    00264     elements3[i] = elements1[i] / elements2[i];
    00265   return op3;
    00266 }
    00268
    00274
    00275 template <typename T> inline
    00276 CoinDenseVector<T> operator+(const CoinDenseVector<T>& op1, T value){
    00277   int size = op1.size();
    00278   CoinDenseVector<T> op3(size);
    00279   const T *elements1 = op1.getElements();
    00280   T *elements3 = op3.getElements();
    00281   double dvalue = value;
    00282   for(int i=0; i<size; i++)
    00283     elements3[i] = elements1[i] + dvalue;
    00284   return op3;
    00285 }
    00286
    00288 template <typename T> inline
    00289 CoinDenseVector<T> operator-(const CoinDenseVector<T>& op1, T value){
    00290   int size = op1.size();
    00291   CoinDenseVector<T> op3(size);
    00292   const T *elements1 = op1.getElements();
    00293   T *elements3 = op3.getElements();
    00294   double dvalue = value;
    00295   for(int i=0; i<size; i++)
    00296     elements3[i] = elements1[i] - dvalue;
    00297   return op3;
    00298 }
    00299
    00301 template <typename T> inline
    00302 CoinDenseVector<T> operator*(const CoinDenseVector<T>& op1, T value){
    00303   int size = op1.size();
    00304   CoinDenseVector<T> op3(size);
    00305   const T *elements1 = op1.getElements();
    00306   T *elements3 = op3.getElements();
    00307   double dvalue = value;
    00308   for(int i=0; i<size; i++)
    00309     elements3[i] = elements1[i] * dvalue;
    00310   return op3;
    00311 }
    00312
    00314 template <typename T> inline
    00315 CoinDenseVector<T> operator/(const CoinDenseVector<T>& op1, T value){
    00316   int size = op1.size();
    00317   CoinDenseVector<T> op3(size);
    00318   const T *elements1 = op1.getElements();
    00319   T *elements3 = op3.getElements();
    00320   double dvalue = value;
    00321   for(int i=0; i<size; i++)
    00322     elements3[i] = elements1[i] / dvalue;
    00323   return op3;
    00324 }
    00325
    00327 template <typename T> inline
    00328 CoinDenseVector<T> operator+(T value, const CoinDenseVector<T>& op1){
    00329   int size = op1.size();
    00330   CoinDenseVector<T> op3(size);
    00331   const T *elements1 = op1.getElements();
    00332   T *elements3 = op3.getElements();
    00333   double dvalue = value;
    00334   for(int i=0; i<size; i++)
    00335     elements3[i] = elements1[i] + dvalue;
    00336   return op3;
    00337 }
    00338
    00340 template <typename T> inline
    00341 CoinDenseVector<T> operator-(T value, const CoinDenseVector<T>& op1){
    00342   int size = op1.size();
    00343   CoinDenseVector<T> op3(size);
    00344   const T *elements1 = op1.getElements();
    00345   T *elements3 = op3.getElements();
    00346   double dvalue = value;
    00347   for(int i=0; i<size; i++)
    00348     elements3[i] = dvalue - elements1[i];
    00349   return op3;
    00350 }
    00351
    00353 template <typename T> inline
    00354 CoinDenseVector<T> operator*(T value, const CoinDenseVector<T>& op1){
    00355   int size = op1.size();
    00356   CoinDenseVector<T> op3(size);
    00357   const T *elements1 = op1.getElements();
    00358   T *elements3 = op3.getElements();
    00359   double dvalue = value;
    00360   for(int i=0; i<size; i++)
    00361     elements3[i] = elements1[i] * dvalue;
    00362   return op3;
    00363 }
    00364
    00366 template <typename T> inline
    00367 CoinDenseVector<T> operator/(T value, const CoinDenseVector<T>& op1){
    00368   int size = op1.size();
    00369   CoinDenseVector<T> op3(size);
    00370   const T *elements1 = op1.getElements();
    00371   T *elements3 = op3.getElements();
    00372   double dvalue = value;
    00373   for(int i=0; i<size; i++)
    00374     elements3[i] = dvalue / elements1[i];
    00375   return op3;
    00376 }
    00378
    00379 #endif