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

- File List
- File Members

# /home/coin/SVN-release/Bcps-0.91.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 <cstdlib>
    00013 #include <cmath>
    00014 #include "CoinHelperFunctions.hpp"
    00015
    00016 //#############################################################################
    00022     template <typename T> void
    00023     CoinDenseVectorUnitTest(T dummy);
    00024
    00025 //#############################################################################
    00064 template <typename T> class CoinDenseVector {
    00065 private:
    00068
    00069    int nElements_;
    00071    T * elements_;
    00073
    00074 public:
    00077
    00078    inline int getNumElements() const { return nElements_; }
    00079    inline int size() const { return nElements_; }
    00081    inline const T * getElements() const { return elements_; }
    00083    inline T * getElements() { return elements_; }
    00085
    00086    //-------------------------------------------------------------------
    00087    // Set indices and elements
    00088    //-------------------------------------------------------------------
    00091
    00092    void clear();
    00094    CoinDenseVector & operator=(const CoinDenseVector &);
    00096    T & operator[](int index) const;
    00097
    00102    void setVector(int size, const T * elems);
    00103
    00104
    00106    void setConstant(int size, T elems);
    00107
    00108
    00112    void setElement(int index, T element);
    00116    void resize(int newSize, T fill=T());
    00117
    00119    void append(const CoinDenseVector &);
    00121
    00124
    00125    inline T oneNorm() const {
    00126      T norm = 0;
    00127      for (int i=0; i<nElements_; i++)
    00128        norm += CoinAbs(elements_[i]);
    00129      return norm;
    00130    }
    00132    inline double twoNorm() const {
    00133      double norm = 0.;
    00134      for (int i=0; i<nElements_; i++)
    00135        norm += elements_[i] * elements_[i];
    00136      // std namespace removed because it was causing a compile
    00137      // problem with Microsoft Visual C++
    00138      return /*std::*/sqrt(norm);
    00139    }
    00141    inline T infNorm() const {
    00142      T norm = 0;
    00143      for (int i=0; i<nElements_; i++)
    00144        norm = CoinMax(norm, CoinAbs(elements_[i]));
    00145      return norm;
    00146    }
    00148    inline T sum() const {
    00149      T sume = 0;
    00150      for (int i=0; i<nElements_; i++)
    00151        sume += elements_[i];
    00152      return sume;
    00153    }
    00155    inline void scale(T factor) {
    00156      for (int i=0; i<nElements_; i++)
    00157        elements_[i] *= factor;
    00158      return;
    00159    }
    00161
    00164
    00165    void operator+=(T value);
    00167    void operator-=(T value);
    00169    void operator*=(T value);
    00171    void operator/=(T value);
    00173
    00177    CoinDenseVector();
    00179    CoinDenseVector(int size, const T * elems);
    00181    CoinDenseVector(int size, T element=T());
    00183    CoinDenseVector(const CoinDenseVector &);
    00184
    00186    ~CoinDenseVector ();
    00188
    00189 private:
    00192
    00193    void gutsOfSetVector(int size, const T * elems);
    00195    void gutsOfSetConstant(int size, T value);
    00197 };
    00198
    00199 //#############################################################################
    00200
    00208
    00209 template <typename T> inline
    00210 CoinDenseVector<T> operator+(const CoinDenseVector<T>& op1,
    00211                              const CoinDenseVector<T>& op2){
    00212   assert(op1.size() == op2.size());
    00213   int size = op1.size();
    00214   CoinDenseVector<T> op3(size);
    00215   const T *elements1 = op1.getElements();
    00216   const T *elements2 = op2.getElements();
    00217   T *elements3 = op3.getElements();
    00218   for(int i=0; i<size; i++)
    00219     elements3[i] = elements1[i] + elements2[i];
    00220   return op3;
    00221 }
    00222
    00224 template <typename T> inline
    00225 CoinDenseVector<T> operator-(const CoinDenseVector<T>& op1,
    00226                              const CoinDenseVector<T>& op2){
    00227   assert(op1.size() == op2.size());
    00228   int size = op1.size();
    00229   CoinDenseVector<T> op3(size);
    00230   const T *elements1 = op1.getElements();
    00231   const T *elements2 = op2.getElements();
    00232   T *elements3 = op3.getElements();
    00233   for(int i=0; i<size; i++)
    00234     elements3[i] = elements1[i] - elements2[i];
    00235   return op3;
    00236 }
    00237
    00238
    00240 template <typename T> inline
    00241 CoinDenseVector<T> operator*(const CoinDenseVector<T>& op1,
    00242                           const CoinDenseVector<T>& op2){
    00243   assert(op1.size() == op2.size());
    00244   int size = op1.size();
    00245   CoinDenseVector<T> op3(size);
    00246   const T *elements1 = op1.getElements();
    00247   const T *elements2 = op2.getElements();
    00248   T *elements3 = op3.getElements();
    00249   for(int i=0; i<size; i++)
    00250     elements3[i] = elements1[i] * elements2[i];
    00251   return op3;
    00252 }
    00253
    00255 template <typename T> inline
    00256 CoinDenseVector<T> operator/(const CoinDenseVector<T>& op1,
    00257                           const CoinDenseVector<T>& op2){
    00258   assert(op1.size() == op2.size());
    00259   int size = op1.size();
    00260   CoinDenseVector<T> op3(size);
    00261   const T *elements1 = op1.getElements();
    00262   const T *elements2 = op2.getElements();
    00263   T *elements3 = op3.getElements();
    00264   for(int i=0; i<size; i++)
    00265     elements3[i] = elements1[i] / elements2[i];
    00266   return op3;
    00267 }
    00269
    00275
    00276 template <typename T> inline
    00277 CoinDenseVector<T> operator+(const CoinDenseVector<T>& op1, T value){
    00278   int size = op1.size();
    00279   CoinDenseVector<T> op3(size);
    00280   const T *elements1 = op1.getElements();
    00281   T *elements3 = op3.getElements();
    00282   double dvalue = value;
    00283   for(int i=0; i<size; i++)
    00284     elements3[i] = elements1[i] + dvalue;
    00285   return op3;
    00286 }
    00287
    00289 template <typename T> inline
    00290 CoinDenseVector<T> operator-(const CoinDenseVector<T>& op1, T value){
    00291   int size = op1.size();
    00292   CoinDenseVector<T> op3(size);
    00293   const T *elements1 = op1.getElements();
    00294   T *elements3 = op3.getElements();
    00295   double dvalue = value;
    00296   for(int i=0; i<size; i++)
    00297     elements3[i] = elements1[i] - dvalue;
    00298   return op3;
    00299 }
    00300
    00302 template <typename T> inline
    00303 CoinDenseVector<T> operator*(const CoinDenseVector<T>& op1, T value){
    00304   int size = op1.size();
    00305   CoinDenseVector<T> op3(size);
    00306   const T *elements1 = op1.getElements();
    00307   T *elements3 = op3.getElements();
    00308   double dvalue = value;
    00309   for(int i=0; i<size; i++)
    00310     elements3[i] = elements1[i] * dvalue;
    00311   return op3;
    00312 }
    00313
    00315 template <typename T> inline
    00316 CoinDenseVector<T> operator/(const CoinDenseVector<T>& op1, T value){
    00317   int size = op1.size();
    00318   CoinDenseVector<T> op3(size);
    00319   const T *elements1 = op1.getElements();
    00320   T *elements3 = op3.getElements();
    00321   double dvalue = value;
    00322   for(int i=0; i<size; i++)
    00323     elements3[i] = elements1[i] / dvalue;
    00324   return op3;
    00325 }
    00326
    00328 template <typename T> inline
    00329 CoinDenseVector<T> operator+(T value, const CoinDenseVector<T>& op1){
    00330   int size = op1.size();
    00331   CoinDenseVector<T> op3(size);
    00332   const T *elements1 = op1.getElements();
    00333   T *elements3 = op3.getElements();
    00334   double dvalue = value;
    00335   for(int i=0; i<size; i++)
    00336     elements3[i] = elements1[i] + dvalue;
    00337   return op3;
    00338 }
    00339
    00341 template <typename T> inline
    00342 CoinDenseVector<T> operator-(T value, const CoinDenseVector<T>& op1){
    00343   int size = op1.size();
    00344   CoinDenseVector<T> op3(size);
    00345   const T *elements1 = op1.getElements();
    00346   T *elements3 = op3.getElements();
    00347   double dvalue = value;
    00348   for(int i=0; i<size; i++)
    00349     elements3[i] = dvalue - elements1[i];
    00350   return op3;
    00351 }
    00352
    00354 template <typename T> inline
    00355 CoinDenseVector<T> operator*(T value, const CoinDenseVector<T>& op1){
    00356   int size = op1.size();
    00357   CoinDenseVector<T> op3(size);
    00358   const T *elements1 = op1.getElements();
    00359   T *elements3 = op3.getElements();
    00360   double dvalue = value;
    00361   for(int i=0; i<size; i++)
    00362     elements3[i] = elements1[i] * dvalue;
    00363   return op3;
    00364 }
    00365
    00367 template <typename T> inline
    00368 CoinDenseVector<T> operator/(T value, const CoinDenseVector<T>& op1){
    00369   int size = op1.size();
    00370   CoinDenseVector<T> op3(size);
    00371   const T *elements1 = op1.getElements();
    00372   T *elements3 = op3.getElements();
    00373   double dvalue = value;
    00374   for(int i=0; i<size; i++)
    00375     elements3[i] = dvalue / elements1[i];
    00376   return op3;
    00377 }
    00379
    00380 #endif