CoinUtils  2.11.9
CoinPackedVectorBase.hpp
Go to the documentation of this file.
1 /* $Id$ */
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
66  {
67  testForDuplicateIndex_ = false;
68  testedDuplicateIndex_ = false;
69  }
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 >
143  bool
144  isEquivalent(const CoinPackedVectorBase &rhs, const FloatEqual &eq) const
145  {
146  if (getNumElements() != rhs.getNumElements())
147  return false;
148 
149  duplicateIndex("equivalent", "CoinPackedVector");
150  rhs.duplicateIndex("equivalent", "CoinPackedVector");
151 
152  std::map< int, double > mv;
153  const int *inds = getIndices();
154  const double *elems = getElements();
155  int i;
156  for (i = getNumElements() - 1; i >= 0; --i) {
157  mv.insert(std::make_pair(inds[i], elems[i]));
158  }
159 
160  std::map< int, double > mvRhs;
161  inds = rhs.getIndices();
162  elems = rhs.getElements();
163  for (i = getNumElements() - 1; i >= 0; --i) {
164  mvRhs.insert(std::make_pair(inds[i], elems[i]));
165  }
166 
167  std::map< int, double >::const_iterator mvI = mv.begin();
168  std::map< int, double >::const_iterator mvIlast = mv.end();
169  std::map< int, double >::const_iterator mvIrhs = mvRhs.begin();
170  while (mvI != mvIlast) {
171  if (mvI->first != mvIrhs->first || !eq(mvI->second, mvIrhs->second))
172  return false;
173  ++mvI;
174  ++mvIrhs;
175  }
176  return true;
177  }
178 
179  bool isEquivalent(const CoinPackedVectorBase &rhs) const;
181 
184  double dotProduct(const double *dense) const;
186 
188  double oneNorm() const;
189 
191  double normSquare() const;
192 
194  double twoNorm() const;
195 
197  double infNorm() const;
198 
200  double sum() const;
202 
203 protected:
210 
211 public:
213  virtual ~CoinPackedVectorBase();
215 
216 private:
230 
231 protected:
234  void findMaxMinIndices() const;
236 
238  std::set< int > *indexSet(const char *methodName = NULL,
239  const char *className = NULL) const;
240 
242  void clearIndexSet() const;
243  void clearBase() const;
245  {
246  maxIndex_ = x.maxIndex_;
247  minIndex_ = x.minIndex_;
248  }
250 
251 private:
254  mutable int maxIndex_;
257  mutable int minIndex_;
261  mutable std::set< int > *indexSetPtr_;
264  mutable bool testForDuplicateIndex_;
267  mutable bool testedDuplicateIndex_;
269 };
270 
271 #endif
272 
273 /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
274 */
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::setTestForDuplicateIndex
void setTestForDuplicateIndex(bool test) const
Set to the argument value whether to test for duplicate indices in the vector whenever they can occur...
CoinError.hpp
CoinPackedVectorBase::testForDuplicateIndex_
bool testForDuplicateIndex_
True if the vector should be tested for duplicate indices when they can occur.
Definition: CoinPackedVectorBase.hpp:264
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:257
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.
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::indexSet
std::set< int > * indexSet(const char *methodName=NULL, const char *className=NULL) const
Return indexSetPtr_ (create it if necessary).
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:255
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::indexSetPtr_
std::set< int > * indexSetPtr_
Store the indices in a set.
Definition: CoinPackedVectorBase.hpp:261
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::copyMaxMinIndex
void copyMaxMinIndex(const CoinPackedVectorBase &x) const
Definition: CoinPackedVectorBase.hpp:244
CoinPackedVectorBase::infNorm
double infNorm() const
Return the infinity-norm of the vector.
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:144
CoinPackedVectorBase::testedDuplicateIndex_
bool testedDuplicateIndex_
True if the vector has already been tested for duplicate indices.
Definition: CoinPackedVectorBase.hpp:267
CoinPackedVectorBase::setTestsOff
void setTestsOff() const
Just sets test stuff false without a try etc.
Definition: CoinPackedVectorBase.hpp:65
CoinPragma.hpp
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.