CoinUtils 2.10.15

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

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CoinPackedVector.hpp
Go to the documentation of this file.
1 /* $Id: CoinPackedVector.hpp 1509 2011-12-05 13:50:48Z forrest $ */
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 CoinPackedVector_H
7 #define CoinPackedVector_H
8
9 #include <map>
10
11 #include "CoinPragma.hpp"
12 #include "CoinPackedVectorBase.hpp"
13 #include "CoinSort.hpp"
14
15 #ifdef COIN_FAST_CODE
16 #ifndef COIN_NOTEST_DUPLICATE
17 #define COIN_NOTEST_DUPLICATE
18 #endif
19 #endif
20
21 #ifndef COIN_NOTEST_DUPLICATE
22 #define COIN_DEFAULT_VALUE_FOR_DUPLICATE true
23 #else
24 #define COIN_DEFAULT_VALUE_FOR_DUPLICATE false
25 #endif
26
123 class CoinPackedVector : public CoinPackedVectorBase {
124 friend void CoinPackedVectorUnitTest();
125
126 public:
129 virtual int getNumElements() const { return nElements_; }
132 virtual const int * getIndices() const { return indices_; }
134 virtual const double * getElements() const { return elements_; }
136 int * getIndices() { return indices_; }
138 inline int getVectorNumElements() const { return nElements_; }
140 inline const int * getVectorIndices() const { return indices_; }
142 inline const double * getVectorElements() const { return elements_; }
144 double * getElements() { return elements_; }
148 const int * getOriginalPosition() const { return origIndices_; }
150
151 //-------------------------------------------------------------------
152 // Set indices and elements
153 //-------------------------------------------------------------------
156 void clear();
162 CoinPackedVector & operator=(const CoinPackedVector &);
167 CoinPackedVector & operator=(const CoinPackedVectorBase & rhs);
168
175 void assignVector(int size, int*& inds, double*& elems,
176 bool testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE);
177
183 void setVector(int size, const int * inds, const double * elems,
184 bool testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE);
185
187 void setConstant(int size, const int * inds, double elems,
188 bool testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE);
189
191 void setFull(int size, const double * elems,
192 bool testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE);
193
196 void setFullNonZero(int size, const double * elems,
197 bool testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE);
198
202 void setElement(int index, double element);
203
205 void insert(int index, double element);
207 void append(const CoinPackedVectorBase & caboose);
208
210 void swap(int i, int j);
211
214 void truncate(int newSize);
216
219 void operator+=(double value);
222 void operator-=(double value);
224 void operator*=(double value);
226 void operator/=(double value);
228
238 template <class CoinCompare3>
239 void sort(const CoinCompare3 & tc)
240 { CoinSort_3(indices_, indices_ + nElements_, origIndices_, elements_,
241 tc); }
242
243 void sortIncrIndex()
244 { CoinSort_3(indices_, indices_ + nElements_, origIndices_, elements_,
245 CoinFirstLess_3<int, int, double>()); }
246
247 void sortDecrIndex()
248 { CoinSort_3(indices_, indices_ + nElements_, origIndices_, elements_,
249 CoinFirstGreater_3<int, int, double>()); }
250
251 void sortIncrElement()
252 { CoinSort_3(elements_, elements_ + nElements_, origIndices_, indices_,
253 CoinFirstLess_3<double, int, int>()); }
254
255 void sortDecrElement()
256 { CoinSort_3(elements_, elements_ + nElements_, origIndices_, indices_,
257 CoinFirstGreater_3<double, int, int>()); }
258
259
264 void sortOriginalOrder();
266
273 void reserve(int n);
277 int capacity() const { return capacity_; }
279
282 CoinPackedVector(bool testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE);
287 CoinPackedVector(int size, const int * inds, const double * elems,
288 bool testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE);
294 CoinPackedVector(int capacity, int size, int *&inds, double *&elems,
295 bool testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE);
297 CoinPackedVector(int size, const int * inds, double element,
298 bool testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE);
301 CoinPackedVector(int size, const double * elements,
302 bool testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE);
304 CoinPackedVector(const CoinPackedVector &);
306 CoinPackedVector(const CoinPackedVectorBase & rhs);
308 virtual ~CoinPackedVector ();
310
311 private:
314 void gutsOfSetVector(int size,
316 const int * inds, const double * elems,
317 bool testForDuplicateIndex,
318 const char * method);
320 void gutsOfSetConstant(int size,
321 const int * inds, double value,
322 bool testForDuplicateIndex,
323 const char * method);
325
326 private:
329 int * indices_;
332 double * elements_;
334 int nElements_;
336 int * origIndices_;
338 int capacity_;
340 };
341
342 //#############################################################################
343
359 template <class BinaryFunction> void
360 binaryOp(CoinPackedVector& retVal,
361 const CoinPackedVectorBase& op1, double value,
362 BinaryFunction bf)
363 {
364 retVal.clear();
365 const int s = op1.getNumElements();
366 if (s > 0) {
367 retVal.reserve(s);
368 const int * inds = op1.getIndices();
369 const double * elems = op1.getElements();
370 for (int i=0; i<s; ++i ) {
371 retVal.insert(inds[i], bf(value, elems[i]));
372 }
373 }
374 }
375
376 template <class BinaryFunction> inline void
377 binaryOp(CoinPackedVector& retVal,
378 double value, const CoinPackedVectorBase& op2,
379 BinaryFunction bf)
380 {
381 binaryOp(retVal, op2, value, bf);
382 }
383
384 template <class BinaryFunction> void
385 binaryOp(CoinPackedVector& retVal,
386 const CoinPackedVectorBase& op1, const CoinPackedVectorBase& op2,
387 BinaryFunction bf)
388 {
389 retVal.clear();
390 const int s1 = op1.getNumElements();
391 const int s2 = op2.getNumElements();
392 /*
393 Replaced || with &&, in response to complaint from Sven deVries, who
394 rightly points out || is not appropriate for additive operations. &&
395 should be ok as long as binaryOp is understood not to create something
396 from nothing. -- lh, 04.06.11
397 */
398 if (s1 == 0 && s2 == 0)
399 return;
400
401 retVal.reserve(s1+s2);
402
403 const int * inds1 = op1.getIndices();
404 const double * elems1 = op1.getElements();
405 const int * inds2 = op2.getIndices();
406 const double * elems2 = op2.getElements();
407
408 int i;
409 // loop once for each element in op1
410 for ( i=0; i<s1; ++i ) {
411 const int index = inds1[i];
412 const int pos2 = op2.findIndex(index);
413 const double val = bf(elems1[i], pos2 == -1 ? 0.0 : elems2[pos2]);
414 // if (val != 0.0) // *THINK* : should we put in only nonzeros?
415 retVal.insert(index, val);
416 }
417 // loop once for each element in operand2
418 for ( i=0; i<s2; ++i ) {
419 const int index = inds2[i];
420 // if index exists in op1, then element was processed in prior loop
421 if ( op1.isExistingIndex(index) )
422 continue;
423 // Index does not exist in op1, so the element value must be zero
424 const double val = bf(0.0, elems2[i]);
425 // if (val != 0.0) // *THINK* : should we put in only nonzeros?
426 retVal.insert(index, val);
427 }
428 }
429
430 //-----------------------------------------------------------------------------
431
432 template <class BinaryFunction> CoinPackedVector
433 binaryOp(const CoinPackedVectorBase& op1, double value,
434 BinaryFunction bf)
435 {
436 CoinPackedVector retVal;
437 retVal.setTestForDuplicateIndex(true);
438 binaryOp(retVal, op1, value, bf);
439 return retVal;
440 }
441
442 template <class BinaryFunction> CoinPackedVector
443 binaryOp(double value, const CoinPackedVectorBase& op2,
444 BinaryFunction bf)
445 {
446 CoinPackedVector retVal;
447 retVal.setTestForDuplicateIndex(true);
448 binaryOp(retVal, op2, value, bf);
449 return retVal;
450 }
451
452 template <class BinaryFunction> CoinPackedVector
453 binaryOp(const CoinPackedVectorBase& op1, const CoinPackedVectorBase& op2,
454 BinaryFunction bf)
455 {
456 CoinPackedVector retVal;
457 retVal.setTestForDuplicateIndex(true);
458 binaryOp(retVal, op1, op2, bf);
459 return retVal;
460 }
461
462 //-----------------------------------------------------------------------------
464 inline CoinPackedVector operator+(const CoinPackedVectorBase& op1,
465 const CoinPackedVectorBase& op2)
466 {
467 CoinPackedVector retVal;
468 retVal.setTestForDuplicateIndex(true);
469 binaryOp(retVal, op1, op2, std::plus<double>());
470 return retVal;
471 }
472
474 inline CoinPackedVector operator-(const CoinPackedVectorBase& op1,
475 const CoinPackedVectorBase& op2)
476 {
477 CoinPackedVector retVal;
478 retVal.setTestForDuplicateIndex(true);
479 binaryOp(retVal, op1, op2, std::minus<double>());
480 return retVal;
481 }
482
484 inline CoinPackedVector operator*(const CoinPackedVectorBase& op1,
485 const CoinPackedVectorBase& op2)
486 {
487 CoinPackedVector retVal;
488 retVal.setTestForDuplicateIndex(true);
489 binaryOp(retVal, op1, op2, std::multiplies<double>());
490 return retVal;
491 }
492
494 inline CoinPackedVector operator/(const CoinPackedVectorBase& op1,
495 const CoinPackedVectorBase& op2)
496 {
497 CoinPackedVector retVal;
498 retVal.setTestForDuplicateIndex(true);
499 binaryOp(retVal, op1, op2, std::divides<double>());
500 return retVal;
501 }
503
506 inline double sparseDotProduct(const CoinPackedVectorBase& op1,
507 const CoinPackedVectorBase& op2){
508 int len, i;
509 double acc = 0.0;
510 CoinPackedVector retVal;
511
512 CoinPackedVector retval = op1*op2;
513 len = retval.getNumElements();
514 double * CParray = retval.getElements();
515
516 for(i = 0; i < len; i++){
517 acc += CParray[i];
518 }
519 return acc;
520 }
521
522
525 inline double sortedSparseDotProduct(const CoinPackedVectorBase& op1,
526 const CoinPackedVectorBase& op2){
527 int i, j, len1, len2;
528 double acc = 0.0;
529
530 const double* v1val = op1.getElements();
531 const double* v2val = op2.getElements();
532 const int* v1ind = op1.getIndices();
533 const int* v2ind = op2.getIndices();
534
535 len1 = op1.getNumElements();
536 len2 = op2.getNumElements();
537
538 i = 0;
539 j = 0;
540
541 while(i < len1 && j < len2){
542 if(v1ind[i] == v2ind[j]){
543 acc += v1val[i] * v2val[j];
544 i++;
545 j++;
546 }
547 else if(v2ind[j] < v1ind[i]){
548 j++;
549 }
550 else{
551 i++;
552 } // end if-else-elseif
553 } // end while
554 return acc;
555 }
556
557
558 //-----------------------------------------------------------------------------
559
565 inline CoinPackedVector
567 operator+(const CoinPackedVectorBase& op1, double value)
568 {
569 CoinPackedVector retVal(op1);
570 retVal += value;
571 return retVal;
572 }
573
575 inline CoinPackedVector
576 operator-(const CoinPackedVectorBase& op1, double value)
577 {
578 CoinPackedVector retVal(op1);
579 retVal -= value;
580 return retVal;
581 }
582
584 inline CoinPackedVector
585 operator*(const CoinPackedVectorBase& op1, double value)
586 {
587 CoinPackedVector retVal(op1);
588 retVal *= value;
589 return retVal;
590 }
591
593 inline CoinPackedVector
594 operator/(const CoinPackedVectorBase& op1, double value)
595 {
596 CoinPackedVector retVal(op1);
597 retVal /= value;
598 return retVal;
599 }
600
601 //-----------------------------------------------------------------------------
602
604 inline CoinPackedVector
605 operator+(double value, const CoinPackedVectorBase& op1)
606 {
607 CoinPackedVector retVal(op1);
608 retVal += value;
609 return retVal;
610 }
611
613 inline CoinPackedVector
614 operator-(double value, const CoinPackedVectorBase& op1)
615 {
616 CoinPackedVector retVal(op1);
617 const int size = retVal.getNumElements();
618 double* elems = retVal.getElements();
619 for (int i = 0; i < size; ++i) {
620 elems[i] = value - elems[i];
621 }
622 return retVal;
623 }
624
626 inline CoinPackedVector
627 operator*(double value, const CoinPackedVectorBase& op1)
628 {
629 CoinPackedVector retVal(op1);
630 retVal *= value;
631 return retVal;
632 }
633
635 inline CoinPackedVector
636 operator/(double value, const CoinPackedVectorBase& op1)
637 {
638 CoinPackedVector retVal(op1);
639 const int size = retVal.getNumElements();
640 double* elems = retVal.getElements();
641 for (int i = 0; i < size; ++i) {
642 elems[i] = value / elems[i];
643 }
644 return retVal;
645 }
647
648 //#############################################################################
654 void
655 CoinPackedVectorUnitTest();
656
657 #endif
CoinPackedVector::clear
void clear()
Reset the vector (as if were just created an empty vector)
CoinPackedVector::sortDecrIndex
void sortDecrIndex()
Definition: CoinPackedVector.hpp:247
CoinPackedVector::operator=
CoinPackedVector & operator=(const CoinPackedVector &)
Assignment operator.
CoinPackedVector::truncate
void truncate(int newSize)
Resize the packed vector to be the first newSize elements.
CoinPackedVector::getElements
double * getElements()
Get element values.
Definition: CoinPackedVector.hpp:144
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...
CoinPackedVector::gutsOfSetVector
void gutsOfSetVector(int size, const int *inds, const double *elems, bool testForDuplicateIndex, const char *method)
Copy internal date.
CoinPackedVector::CoinPackedVector
CoinPackedVector(bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
Default constructor.
CoinPackedVector::nElements_
int nElements_
Size of indices and elements vectors.
Definition: CoinPackedVector.hpp:334
operator*
CoinDenseVector< T > operator*(const CoinDenseVector< T > &op1, const CoinDenseVector< T > &op2)
Return the element-wise product of two dense vectors.
Definition: CoinDenseVector.hpp:244
binaryOp
void binaryOp(CoinPackedVector &retVal, const CoinPackedVectorBase &op1, double value, BinaryFunction bf)
Definition: CoinPackedVector.hpp:360
CoinPackedVector::setElement
void setElement(int index, double element)
Set an existing element in the packed vector The first argument is the &quot;index&quot; into the elements() ar...
CoinPackedVectorBase::isExistingIndex
bool isExistingIndex(int i) const
Return true if the i&#39;th element of the full storage vector exists in the packed storage vector...
CoinPackedVector::getNumElements
virtual int getNumElements() const
Get the size.
Definition: CoinPackedVector.hpp:130
CoinPackedVector::gutsOfSetConstant
void gutsOfSetConstant(int size, const int *inds, double value, bool testForDuplicateIndex, const char *method)
CoinPackedVectorUnitTest
void CoinPackedVectorUnitTest()
A function that tests the methods in the CoinPackedVector class.
CoinPackedVector::getVectorIndices
const int * getVectorIndices() const
Get indices of elements.
Definition: CoinPackedVector.hpp:140
CoinPackedVector::getElements
virtual const double * getElements() const
Get element values.
Definition: CoinPackedVector.hpp:134
CoinPackedVector::operator+=
void operator+=(double value)
add value to every entry
operator+
CoinDenseVector< T > operator+(const CoinDenseVector< T > &op1, const CoinDenseVector< T > &op2)
Return the sum of two dense vectors.
Definition: CoinDenseVector.hpp:213
operator-
CoinDenseVector< T > operator-(const CoinDenseVector< T > &op1, const CoinDenseVector< T > &op2)
Return the difference of two dense vectors.
Definition: CoinDenseVector.hpp:228
CoinPackedVector::getOriginalPosition
const int * getOriginalPosition() const
Get pointer to int * vector of original postions.
Definition: CoinPackedVector.hpp:148
CoinPackedVector::operator-=
void operator-=(double value)
subtract value from every entry
CoinFirstGreater_3
Function operator.
Definition: CoinSort.hpp:489
CoinPackedVector::operator/=
void operator/=(double value)
divide every entry by value
CoinPackedVectorBase
Abstract base class for various sparse vectors.
Definition: CoinPackedVectorBase.hpp:23
CoinPackedVectorBase::getElements
virtual const double * getElements() const =0
Get element values.
CoinPackedVectorBase.hpp
CoinPackedVector::operator*=
void operator*=(double value)
multiply every entry by value
CoinPragma.hpp
CoinPackedVector::sortIncrIndex
void sortIncrIndex()
Definition: CoinPackedVector.hpp:243
CoinPackedVector::indices_
int * indices_
Vector indices.
Definition: CoinPackedVector.hpp:330
CoinPackedVectorBase::testForDuplicateIndex
bool testForDuplicateIndex() const
Returns true if the vector should be tested for duplicate indices when they can occur.
Definition: CoinPackedVectorBase.hpp:63
operator/
CoinDenseVector< T > operator/(const CoinDenseVector< T > &op1, const CoinDenseVector< T > &op2)
Return the element-wise ratio of two dense vectors.
Definition: CoinDenseVector.hpp:259
CoinPackedVector::~CoinPackedVector
virtual ~CoinPackedVector()
Destructor.
CoinPackedVector::sortIncrElement
void sortIncrElement()
Definition: CoinPackedVector.hpp:251
CoinPackedVector::getVectorNumElements
int getVectorNumElements() const
Get the size.
Definition: CoinPackedVector.hpp:138
COIN_DEFAULT_VALUE_FOR_DUPLICATE
#define COIN_DEFAULT_VALUE_FOR_DUPLICATE
Definition: CoinPackedVector.hpp:22
CoinPackedVector::assignVector
void assignVector(int size, int *&inds, double *&elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
Assign the ownership of the arguments to this vector.
CoinSort.hpp
CoinPackedVector::setVector
void setVector(int size, const int *inds, const double *elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
Set vector size, indices, and elements.
CoinPackedVector::sort
void sort(const CoinCompare3 &tc)
Sort the packed storage vector.
Definition: CoinPackedVector.hpp:239
CoinPackedVectorBase::getNumElements
virtual int getNumElements() const =0
Get length of indices and elements vectors.
CoinPackedVector::insert
void insert(int index, double element)
Insert an element into the vector.
CoinPackedVector::getVectorElements
const double * getVectorElements() const
Get element values.
Definition: CoinPackedVector.hpp:142
CoinPackedVector::capacity
int capacity() const
capacity returns the size which could be accomodated without having to reallocate storage...
Definition: CoinPackedVector.hpp:277
CoinPackedVector::CoinPackedVectorUnitTest
friend void CoinPackedVectorUnitTest()
A function that tests the methods in the CoinPackedVector class.
CoinPackedVector::origIndices_
int * origIndices_
original unsorted indices
Definition: CoinPackedVector.hpp:336
CoinPackedVector::reserve
void reserve(int n)
Reserve space.
sortedSparseDotProduct
double sortedSparseDotProduct(const CoinPackedVectorBase &op1, const CoinPackedVectorBase &op2)
Returns the dot product of two sorted CoinPackedVector objects.
Definition: CoinPackedVector.hpp:525
CoinFirstLess_3
Function operator.
Definition: CoinSort.hpp:478
CoinPackedVector::setFull
void setFull(int size, const double *elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
Indices are not specified and are taken to be 0,1,...,size-1.
CoinPackedVector::sortOriginalOrder
void sortOriginalOrder()
Sort in original order.
CoinSort_3
void CoinSort_3(S *sfirst, S *slast, T *tfirst, U *ufirst, const CoinCompare3 &tc)
Sort a triple of containers.
Definition: CoinSort.hpp:636
CoinPackedVector::getIndices
int * getIndices()
Get indices of elements.
Definition: CoinPackedVector.hpp:136
sparseDotProduct
double sparseDotProduct(const CoinPackedVectorBase &op1, const CoinPackedVectorBase &op2)
Returns the dot product of two CoinPackedVector objects whose elements are doubles.
Definition: CoinPackedVector.hpp:506
CoinPackedVector::setConstant
void setConstant(int size, const int *inds, double elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
Elements set to have the same scalar value.
CoinPackedVectorBase::getIndices
virtual const int * getIndices() const =0
Get indices of elements.
CoinPackedVectorBase::findIndex
int findIndex(int i) const
Return the position of the i&#39;th element of the full storage vector.
CoinPackedVector::getIndices
virtual const int * getIndices() const
Get indices of elements.
Definition: CoinPackedVector.hpp:132
CoinPackedVector::append
void append(const CoinPackedVectorBase &caboose)
Append a CoinPackedVector to the end.
CoinPackedVector
Sparse Vector.
Definition: CoinPackedVector.hpp:123
CoinPackedVector::capacity_
int capacity_
Amount of memory allocated for indices_, origIndices_, and elements_.
Definition: CoinPackedVector.hpp:338
CoinPackedVector::setFullNonZero
void setFullNonZero(int size, const double *elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
Indices are not specified and are taken to be 0,1,...,size-1, but only where non zero.
CoinPackedVector::elements_
double * elements_
Vector elements.
Definition: CoinPackedVector.hpp:332
CoinPackedVector::swap
void swap(int i, int j)
Swap values in positions i and j of indices and elements.
CoinPackedVector::sortDecrElement
void sortDecrElement()
Definition: CoinPackedVector.hpp:255