CoinUtils  2.10.14
CoinPackedVectorBase.hpp
Go to the documentation of this file.
1 /* $Id: CoinPackedVectorBase.hpp 1416 2011-04-17 09:57:29Z stefan $ */
2 // Copyright (C) 2000, International Business Machines
3 // Corporation and others. All Rights Reserved.
4 // This code is licensed under the terms of the Eclipse Public License (EPL).
5 
6 #ifndef CoinPackedVectorBase_H
7 #define CoinPackedVectorBase_H
8 
9 #include <set>
10 #include <map>
11 #include "CoinPragma.hpp"
12 #include "CoinError.hpp"
13 
14 class CoinPackedVector;
15 
24 
25 public:
28  virtual int getNumElements() const = 0;
31  virtual const int * getIndices() const = 0;
33  virtual const double * getElements() const = 0;
35 
51  void setTestForDuplicateIndex(bool test) const;
60  void setTestForDuplicateIndexWhenTrue(bool test) const;
65  inline void setTestsOff() const
68 
76  double * denseVector(int denseSize) const;
84  double operator[](int i) const;
86 
89  int getMaxIndex() const;
92  int getMinIndex() const;
93 
95  void duplicateIndex(const char* methodName = NULL,
96  const char * className = NULL) const;
97 
100  bool isExistingIndex(int i) const;
101 
104  int findIndex(int i) const;
105 
107 
112  bool operator==(const CoinPackedVectorBase & rhs) const;
114  bool operator!=(const CoinPackedVectorBase & rhs) const;
115 
116 #if 0
117  // LL: This should be implemented eventually. It is useful to have.
121  int lexCompare(const CoinPackedVectorBase& rhs);
122 #endif
123 
130  int compare(const CoinPackedVectorBase& rhs) const;
131 
139  template <class FloatEqual> bool
140  isEquivalent(const CoinPackedVectorBase& rhs, const FloatEqual& eq) const
141  {
142  if (getNumElements() != rhs.getNumElements())
143  return false;
144 
145  duplicateIndex("equivalent", "CoinPackedVector");
146  rhs.duplicateIndex("equivalent", "CoinPackedVector");
147 
148  std::map<int,double> mv;
149  const int * inds = getIndices();
150  const double * elems = getElements();
151  int i;
152  for ( i = getNumElements() - 1; i >= 0; --i) {
153  mv.insert(std::make_pair(inds[i], elems[i]));
154  }
155 
156  std::map<int,double> mvRhs;
157  inds = rhs.getIndices();
158  elems = rhs.getElements();
159  for ( i = getNumElements() - 1; i >= 0; --i) {
160  mvRhs.insert(std::make_pair(inds[i], elems[i]));
161  }
162 
163  std::map<int,double>::const_iterator mvI = mv.begin();
164  std::map<int,double>::const_iterator mvIlast = mv.end();
165  std::map<int,double>::const_iterator mvIrhs = mvRhs.begin();
166  while (mvI != mvIlast) {
167  if (mvI->first != mvIrhs->first || ! eq(mvI->second, mvIrhs->second))
168  return false;
169  ++mvI;
170  ++mvIrhs;
171  }
172  return true;
173  }
174 
175  bool isEquivalent(const CoinPackedVectorBase& rhs) const;
177 
178 
181  double dotProduct(const double* dense) const;
183 
185  double oneNorm() const;
186 
188  double normSquare() const;
189 
191  double twoNorm() const;
192 
194  double infNorm() const;
195 
197  double sum() const;
199 
200 protected:
201 
208 
209 public:
213 
214 private:
228 
229 protected:
230 
233  void findMaxMinIndices() const;
235 
237  std::set<int> * indexSet(const char* methodName = NULL,
238  const char * className = NULL) const;
239 
241  void clearIndexSet() const;
242  void clearBase() const;
243  void copyMaxMinIndex(const CoinPackedVectorBase & x) const {
244  maxIndex_ = x.maxIndex_;
245  minIndex_ = x.minIndex_;
246  }
248 
249 private:
252  mutable int maxIndex_;
255  mutable int minIndex_;
259  mutable std::set<int> * indexSetPtr_;
265  mutable bool testedDuplicateIndex_;
267 };
268 
269 #endif
CoinPackedVectorBase::testForDuplicateIndex
bool testForDuplicateIndex() const
Returns true if the vector should be tested for duplicate indices when they can occur.
Definition: CoinPackedVectorBase.hpp:63
CoinPackedVectorBase::findIndex
int findIndex(int i) const
Return the position of the i'th element of the full storage vector.
CoinPackedVectorBase::indexSetPtr_
std::set< int > * indexSetPtr_
Store the indices in a set.
Definition: CoinPackedVectorBase.hpp:259
CoinPackedVectorBase::setTestForDuplicateIndex
void setTestForDuplicateIndex(bool test) const
Set to the argument value whether to test for duplicate indices in the vector whenever they can occur...
CoinPackedVectorBase::testForDuplicateIndex_
bool testForDuplicateIndex_
True if the vector should be tested for duplicate indices when they can occur.
Definition: CoinPackedVectorBase.hpp:262
CoinPackedVectorBase::compare
int compare(const CoinPackedVectorBase &rhs) const
This method establishes an ordering on packed vectors.
CoinPackedVectorBase::clearIndexSet
void clearIndexSet() const
Delete the indexSet.
CoinPackedVectorBase::minIndex_
int minIndex_
Contains minimum index value or infinity.
Definition: CoinPackedVectorBase.hpp:255
CoinPackedVector
Sparse Vector.
Definition: CoinPackedVector.hpp:123
CoinPackedVectorBase::clearBase
void clearBase() const
CoinPackedVectorBase::normSquare
double normSquare() const
Return the square of the 2-norm of the vector.
CoinPackedVectorBase::getMaxIndex
int getMaxIndex() const
Get value of maximum index.
CoinPackedVectorBase::twoNorm
double twoNorm() const
Return the 2-norm of the vector.
CoinPackedVectorBase::sum
double sum() const
Sum elements of vector.
CoinPackedVectorBase::operator[]
double operator[](int i) const
Access the i'th element of the full storage vector.
CoinPackedVectorBase::duplicateIndex
void duplicateIndex(const char *methodName=NULL, const char *className=NULL) const
Throw an exception if there are duplicate indices.
CoinPragma.hpp
CoinPackedVectorBase::oneNorm
double oneNorm() const
Return the 1-norm of the vector.
CoinPackedVectorBase::operator==
bool operator==(const CoinPackedVectorBase &rhs) const
Equal.
CoinPackedVectorBase::isExistingIndex
bool isExistingIndex(int i) const
Return true if the i'th element of the full storage vector exists in the packed storage vector.
CoinPackedVectorBase::operator=
CoinPackedVectorBase & operator=(const CoinPackedVectorBase &)
This class provides const access to packed vectors, so there's no need to provide an assignment opera...
CoinPackedVectorBase::findMaxMinIndices
void findMaxMinIndices() const
Find Maximum and Minimum Indices.
CoinPackedVectorBase::getMinIndex
int getMinIndex() const
Get value of minimum index.
CoinPackedVectorBase::maxIndex_
int maxIndex_
Contains max index value or -infinity.
Definition: CoinPackedVectorBase.hpp:253
CoinError.hpp
CoinPackedVectorBase::indexSet
std::set< int > * indexSet(const char *methodName=NULL, const char *className=NULL) const
Return indexSetPtr_ (create it if necessary).
CoinPackedVectorBase::getNumElements
virtual int getNumElements() const =0
Get length of indices and elements vectors.
CoinPackedVectorBase::CoinPackedVectorBase
CoinPackedVectorBase()
Default constructor.
CoinPackedVectorBase
Abstract base class for various sparse vectors.
Definition: CoinPackedVectorBase.hpp:23
CoinPackedVectorBase::denseVector
double * denseVector(int denseSize) const
Get the vector as a dense vector.
CoinPackedVectorBase::dotProduct
double dotProduct(const double *dense) const
Create the dot product with a full vector.
CoinPackedVectorBase::operator!=
bool operator!=(const CoinPackedVectorBase &rhs) const
Not equal.
CoinPackedVectorBase::CoinPackedVectorBase
CoinPackedVectorBase(const CoinPackedVectorBase &)
The copy constructor.
CoinPackedVectorBase::copyMaxMinIndex
void copyMaxMinIndex(const CoinPackedVectorBase &x) const
Definition: CoinPackedVectorBase.hpp:243
CoinPackedVectorBase::infNorm
double infNorm() const
Return the infinity-norm of the vector.
CoinPackedVectorBase::isEquivalent
bool isEquivalent(const CoinPackedVectorBase &rhs) const
CoinPackedVectorBase::isEquivalent
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 ...
Definition: CoinPackedVectorBase.hpp:140
CoinPackedVectorBase::testedDuplicateIndex_
bool testedDuplicateIndex_
True if the vector has already been tested for duplicate indices.
Definition: CoinPackedVectorBase.hpp:265
CoinPackedVectorBase::setTestsOff
void setTestsOff() const
Just sets test stuff false without a try etc.
Definition: CoinPackedVectorBase.hpp:65
CoinPackedVectorBase::setTestForDuplicateIndexWhenTrue
void setTestForDuplicateIndexWhenTrue(bool test) const
Set to the argument value whether to test for duplicate indices in the vector whenever they can occur...
CoinPackedVectorBase::getIndices
virtual const int * getIndices() const =0
Get indices of elements.
CoinPackedVectorBase::getElements
virtual const double * getElements() const =0
Get element values.
CoinPackedVectorBase::~CoinPackedVectorBase
virtual ~CoinPackedVectorBase()
Destructor.