// Copyright (C) 2003, International Business Machines // Corporation and others. All Rights Reserved. #ifndef CoinDenseVector_H #define CoinDenseVector_H #if defined(_MSC_VER) // Turn off compiler warning about long names # pragma warning(disable:4786) #endif #include #include #include #include "CoinHelperFunctions.hpp" //############################################################################# /** A function that tests the methods in the CoinDenseVector class. The only reason for it not to be a member method is that this way it doesn't have to be compiled into the library. And that's a gain, because the library should be compiled with optimization on, but this method should be compiled with debugging. */ template void CoinDenseVectorUnitTest(T dummy); //############################################################################# /** Dense Vector Stores a dense (or expanded) vector of floating point values. Type of vector elements is controlled by templating. (Some working quantities such as accumulated sums are explicitly declared of type double). This allows the components of the vector integer, single or double precision. Here is a sample usage: @verbatim const int ne = 4; double el[ne] = { 10., 40., 1., 50. } // Create vector and set its value CoinDenseVector r(ne,el); // access each element assert( r.getElements()[0]==10. ); assert( r.getElements()[1]==40. ); assert( r.getElements()[2]== 1. ); assert( r.getElements()[3]==50. ); // Test for equality CoinDenseVector r1; r1=r; // Add dense vectors. // Similarly for subtraction, multiplication, // and division. CoinDenseVector add = r + r1; assert( add[0] == 10.+10. ); assert( add[1] == 40.+40. ); assert( add[2] == 1.+ 1. ); assert( add[3] == 50.+50. ); assert( r.sum() == 10.+40.+1.+50. ); @endverbatim */ template class CoinDenseVector { private: /**@name Private member data */ //@{ /// Size of element vector int nElements_; ///Vector elements T * elements_; //@} public: /**@name Get methods. */ //@{ /// Get the size inline int getNumElements() const { return nElements_; } inline int size() const { return nElements_; } /// Get element values inline const T * getElements() const { return elements_; } /// Get element values inline T * getElements() { return elements_; } //@} //------------------------------------------------------------------- // Set indices and elements //------------------------------------------------------------------- /**@name Set methods */ //@{ /// Reset the vector (i.e. set all elemenets to zero) void clear(); /** Assignment operator */ CoinDenseVector & operator=(const CoinDenseVector &); /** Member of array operator */ T & operator[](int index) const; /** Set vector size, and elements. Size is the length of the elements vector. The element vector is copied into this class instance's member data. */ void setVector(int size, const T * elems); /** Elements set to have the same scalar value */ void setConstant(int size, T elems); /** Set an existing element in the dense vector The first argument is the "index" into the elements() array */ void setElement(int index, T element); /** Resize the dense vector to be the first newSize elements. If length is decreased, vector is truncated. If increased new entries, set to new default element */ void resize(int newSize, T fill=T()); /** Append a dense vector to this dense vector */ void append(const CoinDenseVector &); //@} /**@name norms, sum and scale */ //@{ /// 1-norm of vector inline T oneNorm() const { T norm = 0; for (int i=0; ivalue to every entry void operator+=(T value); /// subtract value from every entry void operator-=(T value); /// multiply every entry by value void operator*=(T value); /// divide every entry by value void operator/=(T value); //@} /**@name Constructors and destructors */ //@{ /** Default constructor */ CoinDenseVector(); /** Alternate Constructors - set elements to vector of Ts */ CoinDenseVector(int size, const T * elems); /** Alternate Constructors - set elements to same scalar value */ CoinDenseVector(int size, T element=T()); /** Copy constructors */ CoinDenseVector(const CoinDenseVector &); /** Destructor */ ~CoinDenseVector (); //@} private: /**@name Private methods */ //@{ /// Copy internal data void gutsOfSetVector(int size, const T * elems); /// Set all elements to a given value void gutsOfSetConstant(int size, T value); //@} }; //############################################################################# /**@name Arithmetic operators on dense vectors. NOTE: Because these methods return an object (they can't return a reference, though they could return a pointer...) they are very inefficient... */ //@{ /// Return the sum of two dense vectors template inline CoinDenseVector operator+(const CoinDenseVector& op1, const CoinDenseVector& op2){ assert(op1.size() == op2.size()); int size = op1.size(); CoinDenseVector op3(size); const T *elements1 = op1.getElements(); const T *elements2 = op2.getElements(); T *elements3 = op3.getElements(); for(int i=0; i inline CoinDenseVector operator-(const CoinDenseVector& op1, const CoinDenseVector& op2){ assert(op1.size() == op2.size()); int size = op1.size(); CoinDenseVector op3(size); const T *elements1 = op1.getElements(); const T *elements2 = op2.getElements(); T *elements3 = op3.getElements(); for(int i=0; i inline CoinDenseVector operator*(const CoinDenseVector& op1, const CoinDenseVector& op2){ assert(op1.size() == op2.size()); int size = op1.size(); CoinDenseVector op3(size); const T *elements1 = op1.getElements(); const T *elements2 = op2.getElements(); T *elements3 = op3.getElements(); for(int i=0; i inline CoinDenseVector operator/(const CoinDenseVector& op1, const CoinDenseVector& op2){ assert(op1.size() == op2.size()); int size = op1.size(); CoinDenseVector op3(size); const T *elements1 = op1.getElements(); const T *elements2 = op2.getElements(); T *elements3 = op3.getElements(); for(int i=0; iop1 and the specified operation is done entry-wise with the given value. */ //@{ /// Return the sum of a dense vector and a constant template inline CoinDenseVector operator+(const CoinDenseVector& op1, T value){ int size = op1.size(); CoinDenseVector op3(size); const T *elements1 = op1.getElements(); T *elements3 = op3.getElements(); double dvalue = value; for(int i=0; i inline CoinDenseVector operator-(const CoinDenseVector& op1, T value){ int size = op1.size(); CoinDenseVector op3(size); const T *elements1 = op1.getElements(); T *elements3 = op3.getElements(); double dvalue = value; for(int i=0; i inline CoinDenseVector operator*(const CoinDenseVector& op1, T value){ int size = op1.size(); CoinDenseVector op3(size); const T *elements1 = op1.getElements(); T *elements3 = op3.getElements(); double dvalue = value; for(int i=0; i inline CoinDenseVector operator/(const CoinDenseVector& op1, T value){ int size = op1.size(); CoinDenseVector op3(size); const T *elements1 = op1.getElements(); T *elements3 = op3.getElements(); double dvalue = value; for(int i=0; i inline CoinDenseVector operator+(T value, const CoinDenseVector& op1){ int size = op1.size(); CoinDenseVector op3(size); const T *elements1 = op1.getElements(); T *elements3 = op3.getElements(); double dvalue = value; for(int i=0; i inline CoinDenseVector operator-(T value, const CoinDenseVector& op1){ int size = op1.size(); CoinDenseVector op3(size); const T *elements1 = op1.getElements(); T *elements3 = op3.getElements(); double dvalue = value; for(int i=0; i inline CoinDenseVector operator*(T value, const CoinDenseVector& op1){ int size = op1.size(); CoinDenseVector op3(size); const T *elements1 = op1.getElements(); T *elements3 = op3.getElements(); double dvalue = value; for(int i=0; i inline CoinDenseVector operator/(T value, const CoinDenseVector& op1){ int size = op1.size(); CoinDenseVector op3(size); const T *elements1 = op1.getElements(); T *elements3 = op3.getElements(); double dvalue = value; for(int i=0; i