CoinPackedVectorBase.hpp
Go to the documentation of this file.
1 /* $Id: CoinPackedVectorBase.hpp 1215 2009-11-05 11:03:04Z forrest $ */
2 // Copyright (C) 2000, International Business Machines
3 // Corporation and others. All Rights Reserved.
4 #ifndef CoinPackedVectorBase_H
5 #define CoinPackedVectorBase_H
6 
7 #if defined(_MSC_VER)
8 // Turn off compiler warning about long names
9 # pragma warning(disable:4786)
10 #endif
11 
12 #include <set>
13 #include <map>
14 #include "CoinError.hpp"
15 #include "CoinFloatEqual.hpp"
16 
17 class CoinPackedVector;
18 
27 
28 public:
31  virtual int getNumElements() const = 0;
34  virtual const int * getIndices() const = 0;
36  virtual const double * getElements() const = 0;
38 
54  void setTestForDuplicateIndex(bool test) const;
63  void setTestForDuplicateIndexWhenTrue(bool test) const;
68  inline void setTestsOff() const
71 
79  double * denseVector(int denseSize) const;
87  double operator[](int i) const;
89 
92  int getMaxIndex() const;
95  int getMinIndex() const;
96 
98  void duplicateIndex(const char* methodName = NULL,
99  const char * className = NULL) const;
100 
103  bool isExistingIndex(int i) const;
104 
107  int findIndex(int i) const;
108 
110 
115  bool operator==(const CoinPackedVectorBase & rhs) const;
117  bool operator!=(const CoinPackedVectorBase & rhs) const;
118 
119 #if 0
120  // LL: This should be implemented eventually. It is useful to have.
124  int lexCompare(const CoinPackedVectorBase& rhs);
125 #endif
126 
133  int compare(const CoinPackedVectorBase& rhs) const;
134 
142  template <class FloatEqual> bool
143  isEquivalent(const CoinPackedVectorBase& rhs, const FloatEqual& eq) const
144  {
145  if (getNumElements() != rhs.getNumElements())
146  return false;
147 
148  duplicateIndex("equivalent", "CoinPackedVector");
149  rhs.duplicateIndex("equivalent", "CoinPackedVector");
150 
151  std::map<int,double> mv;
152  const int * inds = getIndices();
153  const double * elems = getElements();
154  int i;
155  for ( i = getNumElements() - 1; i >= 0; --i) {
156  mv.insert(std::make_pair(inds[i], elems[i]));
157  }
158 
159  std::map<int,double> mvRhs;
160  inds = rhs.getIndices();
161  elems = rhs.getElements();
162  for ( i = getNumElements() - 1; i >= 0; --i) {
163  mvRhs.insert(std::make_pair(inds[i], elems[i]));
164  }
165 
166  std::map<int,double>::const_iterator mvI = mv.begin();
167  std::map<int,double>::const_iterator mvIlast = mv.end();
168  std::map<int,double>::const_iterator mvIrhs = mvRhs.begin();
169  while (mvI != mvIlast) {
170  if (mvI->first != mvIrhs->first || ! eq(mvI->second, mvIrhs->second))
171  return false;
172  ++mvI;
173  ++mvIrhs;
174  }
175  return true;
176  }
177  bool isEquivalent(const CoinPackedVectorBase& rhs) const
178  {
179  return isEquivalent(rhs, CoinRelFltEq());
180  }
182 
183 
186  double dotProduct(const double* dense) const;
188 
190  double oneNorm() const;
191 
193  double normSquare() const;
194 
196  inline double twoNorm() const { return sqrt(normSquare()); }
197 
199  double infNorm() const;
200 
202  double sum() const;
204 
205 protected:
206 
213 
214 public:
216  virtual ~CoinPackedVectorBase();
218 
219 private:
233 
234 protected:
235 
238  void findMaxMinIndices() const;
240 
242  std::set<int> * indexSet(const char* methodName = NULL,
243  const char * className = NULL) const;
244 
246  void clearIndexSet() const;
247  void clearBase() const;
248  void copyMaxMinIndex(const CoinPackedVectorBase & x) const {
249  maxIndex_ = x.maxIndex_;
250  minIndex_ = x.minIndex_;
251  }
253 
254 private:
257  mutable int maxIndex_;
260  mutable int minIndex_;
264  mutable std::set<int> * indexSetPtr_;
270  mutable bool testedDuplicateIndex_;
272 };
273 
274 #endif
bool isEquivalent(const CoinPackedVectorBase &rhs, const FloatEqual &eq) const
equivalent - If shallow packed vector A & B are equivalent, then they are still equivalent no matter ...
CoinPackedVectorBase()
Default constructor.
int getMaxIndex() const
Get value of maximum index.
Equality to a scaled tolerance.
virtual int getNumElements() const =0
Get length of indices and elements vectors.
double dotProduct(const double *dense) const
Create the dot product with a full vector.
std::set< int > * indexSetPtr_
Store the indices in a set.
void clearIndexSet() const
Delete the indexSet.
std::set< int > * indexSet(const char *methodName=NULL, const char *className=NULL) const
Return indexSetPtr_ (create it if necessary).
bool testForDuplicateIndex_
True if the vector should be tested for duplicate indices when they can occur.
int compare(const CoinPackedVectorBase &rhs) const
This method establishes an ordering on packed vectors.
int getMinIndex() const
Get value of minimum index.
void setTestForDuplicateIndex(bool test) const
Set to the argument value whether to test for duplicate indices in the vector whenever they can occur...
bool operator==(const CoinPackedVectorBase &rhs) const
Equal.
void copyMaxMinIndex(const CoinPackedVectorBase &x) const
Find Maximum and Minimum Indices.
bool operator!=(const CoinPackedVectorBase &rhs) const
Not equal.
double sum() const
Sum elements of vector.
Abstract base class for various sparse vectors.
void setTestForDuplicateIndexWhenTrue(bool test) const
Set to the argument value whether to test for duplicate indices in the vector whenever they can occur...
void clearBase() const
Find Maximum and Minimum Indices.
int maxIndex_
Contains max index value or -infinity.
bool isExistingIndex(int i) const
Return true if the i'th element of the full storage vector exists in the packed storage vector...
int minIndex_
Contains minimum index value or infinity.
virtual ~CoinPackedVectorBase()
Destructor.
void setTestsOff() const
Just sets test stuff false without a try etc.
double twoNorm() const
Return the 2-norm of the vector.
double infNorm() const
Return the infinity-norm of the vector.
virtual const double * getElements() const =0
Get element values.
double operator[](int i) const
Access the i'th element of the full storage vector.
bool testForDuplicateIndex() const
Returns true if the vector should be tested for duplicate indices when they can occur.
Sparse Vector.
bool isEquivalent(const CoinPackedVectorBase &rhs) const
Equal.
CoinPackedVectorBase & operator=(const CoinPackedVectorBase &)
This class provides const access to packed vectors, so there's no need to provide an assignment opera...
double oneNorm() const
Return the 1-norm of the vector.
virtual const int * getIndices() const =0
Get indices of elements.
double normSquare() const
Return the square of the 2-norm of the vector.
int findIndex(int i) const
Return the position of the i'th element of the full storage vector.
double * denseVector(int denseSize) const
Get the vector as a dense vector.
bool testedDuplicateIndex_
True if the vector has already been tested for duplicate indices.
void findMaxMinIndices() const
Find Maximum and Minimum Indices.
Function objects for testing equality of real numbers.
void duplicateIndex(const char *methodName=NULL, const char *className=NULL) const
Throw an exception if there are duplicate indices.