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

- File List
- File Members

# /home/coin/SVN-release/Alps-1.2.1/CoinUtils/src/CoinDenseVector.hpp

Go to the documentation of this file.

    00001 /* $Id: CoinDenseVector.hpp 1191 2009-07-25 08:38:12Z forrest $ */
    00002 // Copyright (C) 2003, International Business Machines
    00003 // Corporation and others.  All Rights Reserved.
    00004 #ifndef CoinDenseVector_H
    00005 #define CoinDenseVector_H
    00006
    00007 #if defined(_MSC_VER)
    00008 // Turn off compiler warning about long names
    00009 #  pragma warning(disable:4786)
    00010 #endif
    00011
    00012 #include <cassert>
    00013 #include <cstdlib>
    00014 #include <cmath>
    00015 #include "CoinHelperFunctions.hpp"
    00016
    00017 //#############################################################################
    00023     template <typename T> void
    00024     CoinDenseVectorUnitTest(T dummy);
    00025
    00026 //#############################################################################
    00065 template <typename T> class CoinDenseVector {
    00066 private:
    00069
    00070    int nElements_;
    00072    T * elements_;
    00074
    00075 public:
    00078
    00079    inline int getNumElements() const { return nElements_; }
    00080    inline int size() const { return nElements_; }
    00082    inline const T * getElements() const { return elements_; }
    00084    inline T * getElements() { return elements_; }
    00086
    00087    //-------------------------------------------------------------------
    00088    // Set indices and elements
    00089    //-------------------------------------------------------------------
    00092
    00093    void clear();
    00095    CoinDenseVector & operator=(const CoinDenseVector &);
    00097    T & operator[](int index) const;
    00098
    00103    void setVector(int size, const T * elems);
    00104
    00105
    00107    void setConstant(int size, T elems);
    00108
    00109
    00113    void setElement(int index, T element);
    00117    void resize(int newSize, T fill=T());
    00118
    00120    void append(const CoinDenseVector &);
    00122
    00125
    00126    inline T oneNorm() const {
    00127      T norm = 0;
    00128      for (int i=0; i<nElements_; i++)
    00129        norm += CoinAbs(elements_[i]);
    00130      return norm;
    00131    }
    00133    inline double twoNorm() const {
    00134      double norm = 0.;
    00135      for (int i=0; i<nElements_; i++)
    00136        norm += elements_[i] * elements_[i];
    00137      // std namespace removed because it was causing a compile
    00138      // problem with Microsoft Visual C++
    00139      return /*std::*/sqrt(norm);
    00140    }
    00142    inline T infNorm() const {
    00143      T norm = 0;
    00144      for (int i=0; i<nElements_; i++)
    00145        norm = CoinMax(norm, CoinAbs(elements_[i]));
    00146      return norm;
    00147    }
    00149    inline T sum() const {
    00150      T sume = 0;
    00151      for (int i=0; i<nElements_; i++)
    00152        sume += elements_[i];
    00153      return sume;
    00154    }
    00156    inline void scale(T factor) {
    00157      for (int i=0; i<nElements_; i++)
    00158        elements_[i] *= factor;
    00159      return;
    00160    }
    00162
    00165
    00166    void operator+=(T value);
    00168    void operator-=(T value);
    00170    void operator*=(T value);
    00172    void operator/=(T value);
    00174
    00178    CoinDenseVector();
    00180    CoinDenseVector(int size, const T * elems);
    00182    CoinDenseVector(int size, T element=T());
    00184    CoinDenseVector(const CoinDenseVector &);
    00185
    00187    ~CoinDenseVector ();
    00189
    00190 private:
    00193
    00194    void gutsOfSetVector(int size, const T * elems);
    00196    void gutsOfSetConstant(int size, T value);
    00198 };
    00199
    00200 //#############################################################################
    00201
    00209
    00210 template <typename T> inline
    00211 CoinDenseVector<T> operator+(const CoinDenseVector<T>& op1,
    00212                              const CoinDenseVector<T>& op2){
    00213   assert(op1.size() == op2.size());
    00214   int size = op1.size();
    00215   CoinDenseVector<T> op3(size);
    00216   const T *elements1 = op1.getElements();
    00217   const T *elements2 = op2.getElements();
    00218   T *elements3 = op3.getElements();
    00219   for(int i=0; i<size; i++)
    00220     elements3[i] = elements1[i] + elements2[i];
    00221   return op3;
    00222 }
    00223
    00225 template <typename T> inline
    00226 CoinDenseVector<T> operator-(const CoinDenseVector<T>& op1,
    00227                              const CoinDenseVector<T>& op2){
    00228   assert(op1.size() == op2.size());
    00229   int size = op1.size();
    00230   CoinDenseVector<T> op3(size);
    00231   const T *elements1 = op1.getElements();
    00232   const T *elements2 = op2.getElements();
    00233   T *elements3 = op3.getElements();
    00234   for(int i=0; i<size; i++)
    00235     elements3[i] = elements1[i] - elements2[i];
    00236   return op3;
    00237 }
    00238
    00239
    00241 template <typename T> inline
    00242 CoinDenseVector<T> operator*(const CoinDenseVector<T>& op1,
    00243                           const CoinDenseVector<T>& op2){
    00244   assert(op1.size() == op2.size());
    00245   int size = op1.size();
    00246   CoinDenseVector<T> op3(size);
    00247   const T *elements1 = op1.getElements();
    00248   const T *elements2 = op2.getElements();
    00249   T *elements3 = op3.getElements();
    00250   for(int i=0; i<size; i++)
    00251     elements3[i] = elements1[i] * elements2[i];
    00252   return op3;
    00253 }
    00254
    00256 template <typename T> inline
    00257 CoinDenseVector<T> operator/(const CoinDenseVector<T>& op1,
    00258                           const CoinDenseVector<T>& op2){
    00259   assert(op1.size() == op2.size());
    00260   int size = op1.size();
    00261   CoinDenseVector<T> op3(size);
    00262   const T *elements1 = op1.getElements();
    00263   const T *elements2 = op2.getElements();
    00264   T *elements3 = op3.getElements();
    00265   for(int i=0; i<size; i++)
    00266     elements3[i] = elements1[i] / elements2[i];
    00267   return op3;
    00268 }
    00270
    00276
    00277 template <typename T> inline
    00278 CoinDenseVector<T> operator+(const CoinDenseVector<T>& op1, T value){
    00279   int size = op1.size();
    00280   CoinDenseVector<T> op3(size);
    00281   const T *elements1 = op1.getElements();
    00282   T *elements3 = op3.getElements();
    00283   double dvalue = value;
    00284   for(int i=0; i<size; i++)
    00285     elements3[i] = elements1[i] + dvalue;
    00286   return op3;
    00287 }
    00288
    00290 template <typename T> inline
    00291 CoinDenseVector<T> operator-(const CoinDenseVector<T>& op1, T value){
    00292   int size = op1.size();
    00293   CoinDenseVector<T> op3(size);
    00294   const T *elements1 = op1.getElements();
    00295   T *elements3 = op3.getElements();
    00296   double dvalue = value;
    00297   for(int i=0; i<size; i++)
    00298     elements3[i] = elements1[i] - dvalue;
    00299   return op3;
    00300 }
    00301
    00303 template <typename T> inline
    00304 CoinDenseVector<T> operator*(const CoinDenseVector<T>& op1, T value){
    00305   int size = op1.size();
    00306   CoinDenseVector<T> op3(size);
    00307   const T *elements1 = op1.getElements();
    00308   T *elements3 = op3.getElements();
    00309   double dvalue = value;
    00310   for(int i=0; i<size; i++)
    00311     elements3[i] = elements1[i] * dvalue;
    00312   return op3;
    00313 }
    00314
    00316 template <typename T> inline
    00317 CoinDenseVector<T> operator/(const CoinDenseVector<T>& op1, T value){
    00318   int size = op1.size();
    00319   CoinDenseVector<T> op3(size);
    00320   const T *elements1 = op1.getElements();
    00321   T *elements3 = op3.getElements();
    00322   double dvalue = value;
    00323   for(int i=0; i<size; i++)
    00324     elements3[i] = elements1[i] / dvalue;
    00325   return op3;
    00326 }
    00327
    00329 template <typename T> inline
    00330 CoinDenseVector<T> operator+(T value, const CoinDenseVector<T>& op1){
    00331   int size = op1.size();
    00332   CoinDenseVector<T> op3(size);
    00333   const T *elements1 = op1.getElements();
    00334   T *elements3 = op3.getElements();
    00335   double dvalue = value;
    00336   for(int i=0; i<size; i++)
    00337     elements3[i] = elements1[i] + dvalue;
    00338   return op3;
    00339 }
    00340
    00342 template <typename T> inline
    00343 CoinDenseVector<T> operator-(T value, const CoinDenseVector<T>& op1){
    00344   int size = op1.size();
    00345   CoinDenseVector<T> op3(size);
    00346   const T *elements1 = op1.getElements();
    00347   T *elements3 = op3.getElements();
    00348   double dvalue = value;
    00349   for(int i=0; i<size; i++)
    00350     elements3[i] = dvalue - elements1[i];
    00351   return op3;
    00352 }
    00353
    00355 template <typename T> inline
    00356 CoinDenseVector<T> operator*(T value, const CoinDenseVector<T>& op1){
    00357   int size = op1.size();
    00358   CoinDenseVector<T> op3(size);
    00359   const T *elements1 = op1.getElements();
    00360   T *elements3 = op3.getElements();
    00361   double dvalue = value;
    00362   for(int i=0; i<size; i++)
    00363     elements3[i] = elements1[i] * dvalue;
    00364   return op3;
    00365 }
    00366
    00368 template <typename T> inline
    00369 CoinDenseVector<T> operator/(T value, const CoinDenseVector<T>& op1){
    00370   int size = op1.size();
    00371   CoinDenseVector<T> op3(size);
    00372   const T *elements1 = op1.getElements();
    00373   T *elements3 = op3.getElements();
    00374   double dvalue = value;
    00375   for(int i=0; i<size; i++)
    00376     elements3[i] = dvalue / elements1[i];
    00377   return op3;
    00378 }
    00380
    00381 #endif