CoinPackedVector.hpp 20.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
/* $Id$ */
// Copyright (C) 2000, International Business Machines
// Corporation and others.  All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).

#ifndef CoinPackedVector_H
#define CoinPackedVector_H

#include <map>

#include "CoinPragma.hpp"
#include "CoinPackedVectorBase.hpp"
#include "CoinSort.hpp"

#ifdef COIN_FAST_CODE
#ifndef COIN_NOTEST_DUPLICATE
#define COIN_NOTEST_DUPLICATE
#endif
#endif

#ifndef COIN_NOTEST_DUPLICATE
#define COIN_DEFAULT_VALUE_FOR_DUPLICATE true
#else
#define COIN_DEFAULT_VALUE_FOR_DUPLICATE false
#endif
/** Sparse Vector

Stores vector of indices and associated element values.
Supports sorting of vector while maintaining the original indices.

Here is a sample usage:
@verbatim
    const int ne = 4;
    int inx[ne] =   {  1,   4,  0,   2 }
    double el[ne] = { 10., 40., 1., 50. }

    // Create vector and set its value
    CoinPackedVector r(ne,inx,el);

    // access each index and element
    assert( r.indices ()[0]== 1  );
    assert( r.elements()[0]==10. );
    assert( r.indices ()[1]== 4  );
    assert( r.elements()[1]==40. );
    assert( r.indices ()[2]== 0  );
    assert( r.elements()[2]== 1. );
    assert( r.indices ()[3]== 2  );
    assert( r.elements()[3]==50. );

    // access original position of index
    assert( r.originalPosition()[0]==0 );
    assert( r.originalPosition()[1]==1 );
    assert( r.originalPosition()[2]==2 );
    assert( r.originalPosition()[3]==3 );

    // access as a full storage vector
    assert( r[ 0]==1. );
    assert( r[ 1]==10.);
    assert( r[ 2]==50.);
    assert( r[ 3]==0. );
    assert( r[ 4]==40.);

    // sort Elements in increasing order
    r.sortIncrElement();

    // access each index and element
    assert( r.indices ()[0]== 0  );
    assert( r.elements()[0]== 1. );
    assert( r.indices ()[1]== 1  );
    assert( r.elements()[1]==10. );
    assert( r.indices ()[2]== 4  );
    assert( r.elements()[2]==40. );
    assert( r.indices ()[3]== 2  );
    assert( r.elements()[3]==50. );    

    // access original position of index    
    assert( r.originalPosition()[0]==2 );
    assert( r.originalPosition()[1]==0 );
    assert( r.originalPosition()[2]==1 );
    assert( r.originalPosition()[3]==3 );

    // access as a full storage vector
    assert( r[ 0]==1. );
    assert( r[ 1]==10.);
    assert( r[ 2]==50.);
    assert( r[ 3]==0. );
    assert( r[ 4]==40.);

    // Restore orignal sort order
    r.sortOriginalOrder();
    
    assert( r.indices ()[0]== 1  );
    assert( r.elements()[0]==10. );
    assert( r.indices ()[1]== 4  );
    assert( r.elements()[1]==40. );
    assert( r.indices ()[2]== 0  );
    assert( r.elements()[2]== 1. );
    assert( r.indices ()[3]== 2  );
    assert( r.elements()[3]==50. );

    // Tests for equality and equivalence
    CoinPackedVector r1;
    r1=r;
    assert( r==r1 );
    assert( r.equivalent(r1) );
    r.sortIncrElement();
    assert( r!=r1 );
    assert( r.equivalent(r1) );

    // Add packed vectors.
    // Similarly for subtraction, multiplication,
    // and division.
    CoinPackedVector add = r + r1;
    assert( add[0] ==  1.+ 1. );
    assert( add[1] == 10.+10. );
    assert( add[2] == 50.+50. );
    assert( add[3] ==  0.+ 0. );
    assert( add[4] == 40.+40. );

    assert( r.sum() == 10.+40.+1.+50. );
@endverbatim
*/
class CoinPackedVector : public CoinPackedVectorBase {
  friend void CoinPackedVectorUnitTest();

public:
  /**@name Get methods. */
  //@{
  /// Get the size
  virtual int getNumElements() const { return nElements_; }
  /// Get indices of elements
  virtual const int *getIndices() const { return indices_; }
  /// Get element values
  virtual const double *getElements() const { return elements_; }
  /// Get indices of elements
  int *getIndices() { return indices_; }
  /// Get the size
  inline int getVectorNumElements() const { return nElements_; }
  /// Get indices of elements
  inline const int *getVectorIndices() const { return indices_; }
  /// Get element values
  inline const double *getVectorElements() const { return elements_; }
  /// Get element values
  double *getElements() { return elements_; }
  /** Get pointer to int * vector of original postions.
       If the packed vector has not been sorted then this
       function returns the vector: 0, 1, 2, ..., size()-1. */
  const int *getOriginalPosition() const { return origIndices_; }
  //@}

  //-------------------------------------------------------------------
  // Set indices and elements
  //-------------------------------------------------------------------
  /**@name Set methods */
  //@{
  /// Reset the vector (as if were just created an empty vector)
  void clear();
  /** Assignment operator. <br>
       <strong>NOTE</strong>: This operator keeps the current
       <code>testForDuplicateIndex</code> setting, and affter copying the data
       it acts accordingly. */
  CoinPackedVector &operator=(const CoinPackedVector &);
  /** Assignment operator from a CoinPackedVectorBase. <br>
       <strong>NOTE</strong>: This operator keeps the current
       <code>testForDuplicateIndex</code> setting, and affter copying the data
       it acts accordingly. */
  CoinPackedVector &operator=(const CoinPackedVectorBase &rhs);

  /** Assign the ownership of the arguments to this vector.
       Size is the length of both the indices and elements vectors.
       The indices and elements vectors are copied into this class instance's
       member data. The last argument indicates whether this vector will have
       to be tested for duplicate indices.
   */
  void assignVector(int size, int *&inds, double *&elems,
    bool testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE);

  /** Set vector size, indices, and elements.
       Size is the length of both the indices and elements vectors.
       The indices and elements vectors are copied into this class instance's
       member data. The last argument specifies whether this vector will have
       to be checked for duplicate indices whenever that can happen. */
  void setVector(int size, const int *inds, const double *elems,
    bool testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE);

  /** Elements set to have the same scalar value */
  void setConstant(int size, const int *inds, double elems,
    bool testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE);

  /** Indices are not specified and are taken to be 0,1,...,size-1 */
  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,
    but only where non zero*/
  void setFullNonZero(int size, const double *elems,
    bool testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE);

  /** Set an existing element in the packed vector
       The first argument is the "index" into the elements() array
   */
  void setElement(int index, double element);

  /// Insert an element into the vector
  void insert(int index, double element);
  /// Append a CoinPackedVector to the end
  void append(const CoinPackedVectorBase &caboose);

  /// Swap values in positions i and j of indices and elements
  void swap(int i, int j);

  /** Resize the packed vector to be the first newSize elements.
       Problem with truncate: what happens with origIndices_ ??? */
  void truncate(int newSize);
  //@}

  /**@name Arithmetic operators. */
  //@{
  /// add <code>value</code> to every entry
  void operator+=(double value);
  /// subtract <code>value</code> from every entry
  void operator-=(double value);
  /// multiply every entry by <code>value</code>
  void operator*=(double value);
  /// divide every entry by <code>value</code>
  void operator/=(double value);
  //@}

  /**@name Sorting */
  //@{
  /** Sort the packed storage vector.
       Typcical usages:
       <pre> 
       packedVector.sort(CoinIncrIndexOrdered());   //increasing indices
       packedVector.sort(CoinIncrElementOrdered()); // increasing elements
       </pre>
   */
  template < class CoinCompare3 >
  void sort(const CoinCompare3 &tc)
  {
    CoinSort_3(indices_, indices_ + nElements_, origIndices_, elements_,
      tc);
  }

  void sortIncrIndex()
  {
    CoinSort_3(indices_, indices_ + nElements_, origIndices_, elements_,
      CoinFirstLess_3< int, int, double >());
  }

  void sortDecrIndex()
  {
    CoinSort_3(indices_, indices_ + nElements_, origIndices_, elements_,
      CoinFirstGreater_3< int, int, double >());
  }

  void sortIncrElement()
  {
    CoinSort_3(elements_, elements_ + nElements_, origIndices_, indices_,
      CoinFirstLess_3< double, int, int >());
  }

  void sortDecrElement()
  {
    CoinSort_3(elements_, elements_ + nElements_, origIndices_, indices_,
      CoinFirstGreater_3< double, int, int >());
  }

  /** Sort in original order.
       If the vector has been sorted, then this method restores
       to its orignal sort order.
   */
  void sortOriginalOrder();
  //@}

  /**@name Memory usage */
  //@{
  /** Reserve space.
       If one knows the eventual size of the packed vector,
       then it may be more efficient to reserve the space.
   */
  void reserve(int n);
  /** capacity returns the size which could be accomodated without
       having to reallocate storage.
   */
  int capacity() const { return capacity_; }
  //@}
  /**@name Constructors and destructors */
  //@{
  /** Default constructor */
  CoinPackedVector(bool testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE);
  /** \brief Alternate Constructors - set elements to vector of doubles
   
     This constructor copies the vectors provided as parameters.
   */
  CoinPackedVector(int size, const int *inds, const double *elems,
    bool testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE);
  /** \brief Alternate Constructors - set elements to vector of doubles

     This constructor takes ownership of the vectors passed as parameters.
     \p inds and \p elems will be NULL on return.
   */
  CoinPackedVector(int capacity, int size, int *&inds, double *&elems,
    bool testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE);
  /** Alternate Constructors - set elements to same scalar value */
  CoinPackedVector(int size, const int *inds, double element,
    bool testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE);
  /** Alternate Constructors - construct full storage with indices 0 through
       size-1. */
  CoinPackedVector(int size, const double *elements,
    bool testForDuplicateIndex = COIN_DEFAULT_VALUE_FOR_DUPLICATE);
  /** Copy constructor. */
  CoinPackedVector(const CoinPackedVector &);
  /** Copy constructor <em>from a PackedVectorBase</em>. */
  CoinPackedVector(const CoinPackedVectorBase &rhs);
  /** Destructor */
  virtual ~CoinPackedVector();
  //@}

private:
  /**@name Private methods */
  //@{
  /// Copy internal date
  void gutsOfSetVector(int size,
    const int *inds, const double *elems,
    bool testForDuplicateIndex,
    const char *method);
  ///
  void gutsOfSetConstant(int size,
    const int *inds, double value,
    bool testForDuplicateIndex,
    const char *method);
  //@}

private:
  /**@name Private member data */
  //@{
  /// Vector indices
  int *indices_;
  ///Vector elements
  double *elements_;
  /// Size of indices and elements vectors
  int nElements_;
  /// original unsorted indices
  int *origIndices_;
  /// Amount of memory allocated for indices_, origIndices_, and elements_.
  int capacity_;
  //@}
};

//#############################################################################

/**@name Arithmetic operators on packed vectors.

   <strong>NOTE</strong>: These methods operate on those positions where at
   least one of the arguments has a value listed. At those positions the
   appropriate operation is executed, Otherwise the result of the operation is
   considered 0.<br>
   <strong>NOTE 2</strong>: There are two kind of operators here. One is used
   like "c = binaryOp(a, b)", the other is used like "binaryOp(c, a, b)", but
   they are really the same. The first is much more natural to use, but it
   involves the creation of a temporary object (the function *must* return an
   object), while the second form puts the result directly into the argument
   "c". Therefore, depending on the circumstances, the second form can be
   significantly faster.
 */
//@{
template < class BinaryFunction >
void binaryOp(CoinPackedVector &retVal,
  const CoinPackedVectorBase &op1, double value,
  BinaryFunction bf)
{
  retVal.clear();
  const int s = op1.getNumElements();
  if (s > 0) {
    retVal.reserve(s);
    const int *inds = op1.getIndices();
    const double *elems = op1.getElements();
    for (int i = 0; i < s; ++i) {
      retVal.insert(inds[i], bf(value, elems[i]));
    }
  }
}

template < class BinaryFunction >
inline void
binaryOp(CoinPackedVector &retVal,
  double value, const CoinPackedVectorBase &op2,
  BinaryFunction bf)
{
  binaryOp(retVal, op2, value, bf);
}

template < class BinaryFunction >
void binaryOp(CoinPackedVector &retVal,
  const CoinPackedVectorBase &op1, const CoinPackedVectorBase &op2,
  BinaryFunction bf)
{
  retVal.clear();
  const int s1 = op1.getNumElements();
  const int s2 = op2.getNumElements();
  /*
  Replaced || with &&, in response to complaint from Sven deVries, who
  rightly points out || is not appropriate for additive operations. &&
  should be ok as long as binaryOp is understood not to create something
  from nothing.		-- lh, 04.06.11
*/
  if (s1 == 0 && s2 == 0)
    return;

  retVal.reserve(s1 + s2);

  const int *inds1 = op1.getIndices();
  const double *elems1 = op1.getElements();
  const int *inds2 = op2.getIndices();
  const double *elems2 = op2.getElements();

  int i;
  // loop once for each element in op1
  for (i = 0; i < s1; ++i) {
    const int index = inds1[i];
    const int pos2 = op2.findIndex(index);
    const double val = bf(elems1[i], pos2 == -1 ? 0.0 : elems2[pos2]);
    // if (val != 0.0) // *THINK* : should we put in only nonzeros?
    retVal.insert(index, val);
  }
  // loop once for each element in operand2
  for (i = 0; i < s2; ++i) {
    const int index = inds2[i];
    // if index exists in op1, then element was processed in prior loop
    if (op1.isExistingIndex(index))
      continue;
    // Index does not exist in op1, so the element value must be zero
    const double val = bf(0.0, elems2[i]);
    // if (val != 0.0) // *THINK* : should we put in only nonzeros?
    retVal.insert(index, val);
  }
}

//-----------------------------------------------------------------------------

template < class BinaryFunction >
CoinPackedVector
binaryOp(const CoinPackedVectorBase &op1, double value,
  BinaryFunction bf)
{
  CoinPackedVector retVal;
  retVal.setTestForDuplicateIndex(true);
  binaryOp(retVal, op1, value, bf);
  return retVal;
}

template < class BinaryFunction >
CoinPackedVector
binaryOp(double value, const CoinPackedVectorBase &op2,
  BinaryFunction bf)
{
  CoinPackedVector retVal;
  retVal.setTestForDuplicateIndex(true);
  binaryOp(retVal, op2, value, bf);
  return retVal;
}

template < class BinaryFunction >
CoinPackedVector
binaryOp(const CoinPackedVectorBase &op1, const CoinPackedVectorBase &op2,
  BinaryFunction bf)
{
  CoinPackedVector retVal;
  retVal.setTestForDuplicateIndex(true);
  binaryOp(retVal, op1, op2, bf);
  return retVal;
}

//-----------------------------------------------------------------------------
/// Return the sum of two packed vectors
inline CoinPackedVector operator+(const CoinPackedVectorBase &op1,
  const CoinPackedVectorBase &op2)
{
  CoinPackedVector retVal;
  retVal.setTestForDuplicateIndex(true);
  binaryOp(retVal, op1, op2, std::plus< double >());
  return retVal;
}

/// Return the difference of two packed vectors
inline CoinPackedVector operator-(const CoinPackedVectorBase &op1,
  const CoinPackedVectorBase &op2)
{
  CoinPackedVector retVal;
  retVal.setTestForDuplicateIndex(true);
  binaryOp(retVal, op1, op2, std::minus< double >());
  return retVal;
}

/// Return the element-wise product of two packed vectors
inline CoinPackedVector operator*(const CoinPackedVectorBase &op1,
  const CoinPackedVectorBase &op2)
{
  CoinPackedVector retVal;
  retVal.setTestForDuplicateIndex(true);
  binaryOp(retVal, op1, op2, std::multiplies< double >());
  return retVal;
}

/// Return the element-wise ratio of two packed vectors
inline CoinPackedVector operator/(const CoinPackedVectorBase &op1,
  const CoinPackedVectorBase &op2)
{
  CoinPackedVector retVal;
  retVal.setTestForDuplicateIndex(true);
  binaryOp(retVal, op1, op2, std::divides< double >());
  return retVal;
}
//@}

/// Returns the dot product of two CoinPackedVector objects whose elements are
/// doubles.  Use this version if the vectors are *not* guaranteed to be sorted.
inline double sparseDotProduct(const CoinPackedVectorBase &op1,
  const CoinPackedVectorBase &op2)
{
  int len, i;
  double acc = 0.0;
  CoinPackedVector retVal;

  CoinPackedVector retval = op1 * op2;
  len = retval.getNumElements();
  double *CParray = retval.getElements();

  for (i = 0; i < len; i++) {
    acc += CParray[i];
  }
  return acc;
}

/// Returns the dot product of two sorted CoinPackedVector objects.
///  The vectors should be sorted in ascending order of indices.
inline double sortedSparseDotProduct(const CoinPackedVectorBase &op1,
  const CoinPackedVectorBase &op2)
{
  int i, j, len1, len2;
  double acc = 0.0;

  const double *v1val = op1.getElements();
  const double *v2val = op2.getElements();
  const int *v1ind = op1.getIndices();
  const int *v2ind = op2.getIndices();

  len1 = op1.getNumElements();
  len2 = op2.getNumElements();

  i = 0;
  j = 0;

  while (i < len1 && j < len2) {
    if (v1ind[i] == v2ind[j]) {
      acc += v1val[i] * v2val[j];
      i++;
      j++;
    } else if (v2ind[j] < v1ind[i]) {
      j++;
    } else {
      i++;
    } // end if-else-elseif
  } // end while
  return acc;
}

//-----------------------------------------------------------------------------

/**@name Arithmetic operators on packed vector and a constant. <br>
   These functions create a packed vector as a result. That packed vector will
   have the same indices as <code>op1</code> and the specified operation is
   done entry-wise with the given value. */
//@{
/// Return the sum of a packed vector and a constant
inline CoinPackedVector
operator+(const CoinPackedVectorBase &op1, double value)
{
  CoinPackedVector retVal(op1);
  retVal += value;
  return retVal;
}

/// Return the difference of a packed vector and a constant
inline CoinPackedVector
operator-(const CoinPackedVectorBase &op1, double value)
{
  CoinPackedVector retVal(op1);
  retVal -= value;
  return retVal;
}

/// Return the element-wise product of a packed vector and a constant
inline CoinPackedVector
operator*(const CoinPackedVectorBase &op1, double value)
{
  CoinPackedVector retVal(op1);
  retVal *= value;
  return retVal;
}

/// Return the element-wise ratio of a packed vector and a constant
inline CoinPackedVector
operator/(const CoinPackedVectorBase &op1, double value)
{
  CoinPackedVector retVal(op1);
  retVal /= value;
  return retVal;
}

//-----------------------------------------------------------------------------

/// Return the sum of a constant and a packed vector
inline CoinPackedVector
operator+(double value, const CoinPackedVectorBase &op1)
{
  CoinPackedVector retVal(op1);
  retVal += value;
  return retVal;
}

/// Return the difference of a constant and a packed vector
inline CoinPackedVector
operator-(double value, const CoinPackedVectorBase &op1)
{
  CoinPackedVector retVal(op1);
  const int size = retVal.getNumElements();
  double *elems = retVal.getElements();
  for (int i = 0; i < size; ++i) {
    elems[i] = value - elems[i];
  }
  return retVal;
}

/// Return the element-wise product of a constant and a packed vector
inline CoinPackedVector
operator*(double value, const CoinPackedVectorBase &op1)
{
  CoinPackedVector retVal(op1);
  retVal *= value;
  return retVal;
}

/// Return the element-wise ratio of a a constant and packed vector
inline CoinPackedVector
operator/(double value, const CoinPackedVectorBase &op1)
{
  CoinPackedVector retVal(op1);
  const int size = retVal.getNumElements();
  double *elems = retVal.getElements();
  for (int i = 0; i < size; ++i) {
    elems[i] = value / elems[i];
  }
  return retVal;
}
//@}

//#############################################################################
/** A function that tests the methods in the CoinPackedVector 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. */
void CoinPackedVectorUnitTest();

#endif

/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
*/