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

- File List
- File Members

# /home/coin/svn-release/Alps-1.4.8/CoinUtils/src/CoinDenseVector.hpp

Go to the documentation of this file.

    00001 /* $Id: CoinDenseVector.hpp 1372 2011-01-03 23:31:00Z lou $ */
    00002 // Copyright (C) 2003, International Business Machines
    00003 // Corporation and others.  All Rights Reserved.
    00004 // This code is licensed under the terms of the Eclipse Public License (EPL).
    00005
    00006 #ifndef CoinDenseVector_H
    00007 #define CoinDenseVector_H
    00008
    00009 #if defined(_MSC_VER)
    00010 // Turn off compiler warning about long names
    00011 #  pragma warning(disable:4786)
    00012 #endif
    00013
    00014 #include <cassert>
    00015 #include <cstdlib>
    00016 #include <cmath>
    00017 #include "CoinHelperFunctions.hpp"
    00018
    00019 //#############################################################################
    00025     template <typename T> void
    00026     CoinDenseVectorUnitTest(T dummy);
    00027
    00028 //#############################################################################
    00067 template <typename T> class CoinDenseVector {
    00068 private:
    00071
    00072    int nElements_;
    00074    T * elements_;
    00076
    00077 public:
    00080
    00081    inline int getNumElements() const { return nElements_; }
    00082    inline int size() const { return nElements_; }
    00084    inline const T * getElements() const { return elements_; }
    00086    inline T * getElements() { return elements_; }
    00088
    00089    //-------------------------------------------------------------------
    00090    // Set indices and elements
    00091    //-------------------------------------------------------------------
    00094
    00095    void clear();
    00097    CoinDenseVector & operator=(const CoinDenseVector &);
    00099    T & operator[](int index) const;
    00100
    00105    void setVector(int size, const T * elems);
    00106
    00107
    00109    void setConstant(int size, T elems);
    00110
    00111
    00115    void setElement(int index, T element);
    00119    void resize(int newSize, T fill=T());
    00120
    00122    void append(const CoinDenseVector &);
    00124
    00127
    00128    inline T oneNorm() const {
    00129      T norm = 0;
    00130      for (int i=0; i<nElements_; i++)
    00131        norm += CoinAbs(elements_[i]);
    00132      return norm;
    00133    }
    00135    inline double twoNorm() const {
    00136      double norm = 0.;
    00137      for (int i=0; i<nElements_; i++)
    00138        norm += elements_[i] * elements_[i];
    00139      // std namespace removed because it was causing a compile
    00140      // problem with Microsoft Visual C++
    00141      return /*std::*/sqrt(norm);
    00142    }
    00144    inline T infNorm() const {
    00145      T norm = 0;
    00146      for (int i=0; i<nElements_; i++)
    00147        norm = CoinMax(norm, CoinAbs(elements_[i]));
    00148      return norm;
    00149    }
    00151    inline T sum() const {
    00152      T sume = 0;
    00153      for (int i=0; i<nElements_; i++)
    00154        sume += elements_[i];
    00155      return sume;
    00156    }
    00158    inline void scale(T factor) {
    00159      for (int i=0; i<nElements_; i++)
    00160        elements_[i] *= factor;
    00161      return;
    00162    }
    00164
    00167
    00168    void operator+=(T value);
    00170    void operator-=(T value);
    00172    void operator*=(T value);
    00174    void operator/=(T value);
    00176
    00180    CoinDenseVector();
    00182    CoinDenseVector(int size, const T * elems);
    00184    CoinDenseVector(int size, T element=T());
    00186    CoinDenseVector(const CoinDenseVector &);
    00187
    00189    ~CoinDenseVector ();
    00191
    00192 private:
    00195
    00196    void gutsOfSetVector(int size, const T * elems);
    00198    void gutsOfSetConstant(int size, T value);
    00200 };
    00201
    00202 //#############################################################################
    00203
    00211
    00212 template <typename T> inline
    00213 CoinDenseVector<T> operator+(const CoinDenseVector<T>& op1,
    00214                              const CoinDenseVector<T>& op2){
    00215   assert(op1.size() == op2.size());
    00216   int size = op1.size();
    00217   CoinDenseVector<T> op3(size);
    00218   const T *elements1 = op1.getElements();
    00219   const T *elements2 = op2.getElements();
    00220   T *elements3 = op3.getElements();
    00221   for(int i=0; i<size; i++)
    00222     elements3[i] = elements1[i] + elements2[i];
    00223   return op3;
    00224 }
    00225
    00227 template <typename T> inline
    00228 CoinDenseVector<T> operator-(const CoinDenseVector<T>& op1,
    00229                              const CoinDenseVector<T>& op2){
    00230   assert(op1.size() == op2.size());
    00231   int size = op1.size();
    00232   CoinDenseVector<T> op3(size);
    00233   const T *elements1 = op1.getElements();
    00234   const T *elements2 = op2.getElements();
    00235   T *elements3 = op3.getElements();
    00236   for(int i=0; i<size; i++)
    00237     elements3[i] = elements1[i] - elements2[i];
    00238   return op3;
    00239 }
    00240
    00241
    00243 template <typename T> inline
    00244 CoinDenseVector<T> operator*(const CoinDenseVector<T>& op1,
    00245                           const CoinDenseVector<T>& op2){
    00246   assert(op1.size() == op2.size());
    00247   int size = op1.size();
    00248   CoinDenseVector<T> op3(size);
    00249   const T *elements1 = op1.getElements();
    00250   const T *elements2 = op2.getElements();
    00251   T *elements3 = op3.getElements();
    00252   for(int i=0; i<size; i++)
    00253     elements3[i] = elements1[i] * elements2[i];
    00254   return op3;
    00255 }
    00256
    00258 template <typename T> inline
    00259 CoinDenseVector<T> operator/(const CoinDenseVector<T>& op1,
    00260                           const CoinDenseVector<T>& op2){
    00261   assert(op1.size() == op2.size());
    00262   int size = op1.size();
    00263   CoinDenseVector<T> op3(size);
    00264   const T *elements1 = op1.getElements();
    00265   const T *elements2 = op2.getElements();
    00266   T *elements3 = op3.getElements();
    00267   for(int i=0; i<size; i++)
    00268     elements3[i] = elements1[i] / elements2[i];
    00269   return op3;
    00270 }
    00272
    00278
    00279 template <typename T> inline
    00280 CoinDenseVector<T> operator+(const CoinDenseVector<T>& op1, T value){
    00281   int size = op1.size();
    00282   CoinDenseVector<T> op3(size);
    00283   const T *elements1 = op1.getElements();
    00284   T *elements3 = op3.getElements();
    00285   double dvalue = value;
    00286   for(int i=0; i<size; i++)
    00287     elements3[i] = elements1[i] + dvalue;
    00288   return op3;
    00289 }
    00290
    00292 template <typename T> inline
    00293 CoinDenseVector<T> operator-(const CoinDenseVector<T>& op1, T value){
    00294   int size = op1.size();
    00295   CoinDenseVector<T> op3(size);
    00296   const T *elements1 = op1.getElements();
    00297   T *elements3 = op3.getElements();
    00298   double dvalue = value;
    00299   for(int i=0; i<size; i++)
    00300     elements3[i] = elements1[i] - dvalue;
    00301   return op3;
    00302 }
    00303
    00305 template <typename T> inline
    00306 CoinDenseVector<T> operator*(const CoinDenseVector<T>& op1, T value){
    00307   int size = op1.size();
    00308   CoinDenseVector<T> op3(size);
    00309   const T *elements1 = op1.getElements();
    00310   T *elements3 = op3.getElements();
    00311   double dvalue = value;
    00312   for(int i=0; i<size; i++)
    00313     elements3[i] = elements1[i] * dvalue;
    00314   return op3;
    00315 }
    00316
    00318 template <typename T> inline
    00319 CoinDenseVector<T> operator/(const CoinDenseVector<T>& op1, T value){
    00320   int size = op1.size();
    00321   CoinDenseVector<T> op3(size);
    00322   const T *elements1 = op1.getElements();
    00323   T *elements3 = op3.getElements();
    00324   double dvalue = value;
    00325   for(int i=0; i<size; i++)
    00326     elements3[i] = elements1[i] / dvalue;
    00327   return op3;
    00328 }
    00329
    00331 template <typename T> inline
    00332 CoinDenseVector<T> operator+(T value, const CoinDenseVector<T>& op1){
    00333   int size = op1.size();
    00334   CoinDenseVector<T> op3(size);
    00335   const T *elements1 = op1.getElements();
    00336   T *elements3 = op3.getElements();
    00337   double dvalue = value;
    00338   for(int i=0; i<size; i++)
    00339     elements3[i] = elements1[i] + dvalue;
    00340   return op3;
    00341 }
    00342
    00344 template <typename T> inline
    00345 CoinDenseVector<T> operator-(T value, const CoinDenseVector<T>& op1){
    00346   int size = op1.size();
    00347   CoinDenseVector<T> op3(size);
    00348   const T *elements1 = op1.getElements();
    00349   T *elements3 = op3.getElements();
    00350   double dvalue = value;
    00351   for(int i=0; i<size; i++)
    00352     elements3[i] = dvalue - elements1[i];
    00353   return op3;
    00354 }
    00355
    00357 template <typename T> inline
    00358 CoinDenseVector<T> operator*(T value, const CoinDenseVector<T>& op1){
    00359   int size = op1.size();
    00360   CoinDenseVector<T> op3(size);
    00361   const T *elements1 = op1.getElements();
    00362   T *elements3 = op3.getElements();
    00363   double dvalue = value;
    00364   for(int i=0; i<size; i++)
    00365     elements3[i] = elements1[i] * dvalue;
    00366   return op3;
    00367 }
    00368
    00370 template <typename T> inline
    00371 CoinDenseVector<T> operator/(T value, const CoinDenseVector<T>& op1){
    00372   int size = op1.size();
    00373   CoinDenseVector<T> op3(size);
    00374   const T *elements1 = op1.getElements();
    00375   T *elements3 = op3.getElements();
    00376   double dvalue = value;
    00377   for(int i=0; i<size; i++)
    00378     elements3[i] = dvalue / elements1[i];
    00379   return op3;
    00380 }
    00382
    00383 #endif