CoinDenseVector.hpp 11.6 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
/* $Id$ */
// Copyright (C) 2003, International Business Machines
// Corporation and others.  All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).

#ifndef CoinDenseVector_H
#define CoinDenseVector_H

#if defined(_MSC_VER)
// Turn off compiler warning about long names
#pragma warning(disable : 4786)
#endif

#include <cassert>
#include <cstdlib>
#include <cmath>
#include "CoinHelperFunctions.hpp"

//#############################################################################
/** A function that tests the methods in the CoinDenseVector 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. */
template < typename T >
void CoinDenseVectorUnitTest(T dummy);

//#############################################################################
/** Dense Vector

Stores a dense (or expanded) vector of floating point values.
Type of vector elements is controlled by templating.
(Some working quantities such as accumulated sums
are explicitly declared of type double). This allows the 
components of the vector integer, single or double precision.

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

    // Create vector and set its value
    CoinDenseVector<double> r(ne,el);

    // access each element
    assert( r.getElements()[0]==10. );
    assert( r.getElements()[1]==40. );
    assert( r.getElements()[2]== 1. );
    assert( r.getElements()[3]==50. );

    // Test for equality
    CoinDenseVector<double> r1;
    r1=r;

    // Add dense vectors.
    // Similarly for subtraction, multiplication,
    // and division.
    CoinDenseVector<double> add = r + r1;
    assert( add[0] == 10.+10. );
    assert( add[1] == 40.+40. );
    assert( add[2] ==  1.+ 1. );
    assert( add[3] == 50.+50. );

    assert( r.sum() == 10.+40.+1.+50. );
@endverbatim
*/
template < typename T >
class CoinDenseVector {
private:
  /**@name Private member data */
  //@{
  /// Size of element vector
  int nElements_;
  ///Vector elements
  T *elements_;
  //@}

public:
  /**@name Get methods. */
  //@{
  /// Get the size
  inline int getNumElements() const { return nElements_; }
  inline int size() const { return nElements_; }
  /// Get element values
  inline const T *getElements() const { return elements_; }
  /// Get element values
  inline T *getElements() { return elements_; }
  //@}

  //-------------------------------------------------------------------
  // Set indices and elements
  //-------------------------------------------------------------------
  /**@name Set methods */
  //@{
  /// Reset the vector (i.e. set all elemenets to zero)
  void clear();
  /** Assignment operator */
  CoinDenseVector &operator=(const CoinDenseVector &);
  /** Member of array operator */
  T &operator[](int index) const;

  /** Set vector size, and elements.
       Size is the length of the elements vector.
       The element vector is copied into this class instance's
       member data. */
  void setVector(int size, const T *elems);

  /** Elements set to have the same scalar value */
  void setConstant(int size, T elems);

  /** Set an existing element in the dense vector
       The first argument is the "index" into the elements() array
   */
  void setElement(int index, T element);
  /** Resize the dense vector to be the first newSize elements.
       If length is decreased, vector is truncated. If increased
       new entries, set to new default element */
  void resize(int newSize, T fill = T());

  /** Append a dense vector to this dense vector */
  void append(const CoinDenseVector &);
  //@}

  /**@name norms, sum and scale */
  //@{
  /// 1-norm of vector
  inline T oneNorm() const
  {
    T norm = 0;
    for (int i = 0; i < nElements_; i++)
      norm += CoinAbs(elements_[i]);
    return norm;
  }
  /// 2-norm of vector
  inline double twoNorm() const
  {
    double norm = 0.;
    for (int i = 0; i < nElements_; i++)
      norm += elements_[i] * elements_[i];
    // std namespace removed because it was causing a compile
    // problem with Microsoft Visual C++
    return /*std::*/ sqrt(norm);
  }
  /// infinity-norm of vector
  inline T infNorm() const
  {
    T norm = 0;
    for (int i = 0; i < nElements_; i++)
      norm = CoinMax(norm, CoinAbs(elements_[i]));
    return norm;
  }
  /// sum of vector elements
  inline T sum() const
  {
    T sume = 0;
    for (int i = 0; i < nElements_; i++)
      sume += elements_[i];
    return sume;
  }
  /// scale vector elements
  inline void scale(T factor)
  {
    for (int i = 0; i < nElements_; i++)
      elements_[i] *= factor;
    return;
  }
  //@}

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

  /**@name Constructors and destructors */
  //@{
  /** Default constructor */
  CoinDenseVector();
  /** Alternate Constructors - set elements to vector of Ts */
  CoinDenseVector(int size, const T *elems);
  /** Alternate Constructors - set elements to same scalar value */
  CoinDenseVector(int size, T element = T());
  /** Copy constructors */
  CoinDenseVector(const CoinDenseVector &);

  /** Destructor */
  ~CoinDenseVector();
  //@}

private:
  /**@name Private methods */
  //@{
  /// Copy internal data
  void gutsOfSetVector(int size, const T *elems);
  /// Set all elements to a given value
  void gutsOfSetConstant(int size, T value);
  //@}
};

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

/**@name Arithmetic operators on dense vectors.

   <strong>NOTE</strong>: Because these methods return an object (they can't
   return a reference, though they could return a pointer...) they are
   <em>very</em> inefficient...
 */
//@{
/// Return the sum of two dense vectors
template < typename T >
inline CoinDenseVector< T > operator+(const CoinDenseVector< T > &op1,
  const CoinDenseVector< T > &op2)
{
  assert(op1.size() == op2.size());
  int size = op1.size();
  CoinDenseVector< T > op3(size);
  const T *elements1 = op1.getElements();
  const T *elements2 = op2.getElements();
  T *elements3 = op3.getElements();
  for (int i = 0; i < size; i++)
    elements3[i] = elements1[i] + elements2[i];
  return op3;
}

/// Return the difference of two dense vectors
template < typename T >
inline CoinDenseVector< T > operator-(const CoinDenseVector< T > &op1,
  const CoinDenseVector< T > &op2)
{
  assert(op1.size() == op2.size());
  int size = op1.size();
  CoinDenseVector< T > op3(size);
  const T *elements1 = op1.getElements();
  const T *elements2 = op2.getElements();
  T *elements3 = op3.getElements();
  for (int i = 0; i < size; i++)
    elements3[i] = elements1[i] - elements2[i];
  return op3;
}

/// Return the element-wise product of two dense vectors
template < typename T >
inline CoinDenseVector< T > operator*(const CoinDenseVector< T > &op1,
  const CoinDenseVector< T > &op2)
{
  assert(op1.size() == op2.size());
  int size = op1.size();
  CoinDenseVector< T > op3(size);
  const T *elements1 = op1.getElements();
  const T *elements2 = op2.getElements();
  T *elements3 = op3.getElements();
  for (int i = 0; i < size; i++)
    elements3[i] = elements1[i] * elements2[i];
  return op3;
}

/// Return the element-wise ratio of two dense vectors
template < typename T >
inline CoinDenseVector< T > operator/(const CoinDenseVector< T > &op1,
  const CoinDenseVector< T > &op2)
{
  assert(op1.size() == op2.size());
  int size = op1.size();
  CoinDenseVector< T > op3(size);
  const T *elements1 = op1.getElements();
  const T *elements2 = op2.getElements();
  T *elements3 = op3.getElements();
  for (int i = 0; i < size; i++)
    elements3[i] = elements1[i] / elements2[i];
  return op3;
}
//@}

/**@name Arithmetic operators on dense vector and a constant. 
   These functions create a dense vector as a result. That dense 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 dense vector and a constant
template < typename T >
inline CoinDenseVector< T > operator+(const CoinDenseVector< T > &op1, T value)
{
  int size = op1.size();
  CoinDenseVector< T > op3(size);
  const T *elements1 = op1.getElements();
  T *elements3 = op3.getElements();
  double dvalue = value;
  for (int i = 0; i < size; i++)
    elements3[i] = elements1[i] + dvalue;
  return op3;
}

/// Return the difference of a dense vector and a constant
template < typename T >
inline CoinDenseVector< T > operator-(const CoinDenseVector< T > &op1, T value)
{
  int size = op1.size();
  CoinDenseVector< T > op3(size);
  const T *elements1 = op1.getElements();
  T *elements3 = op3.getElements();
  double dvalue = value;
  for (int i = 0; i < size; i++)
    elements3[i] = elements1[i] - dvalue;
  return op3;
}

/// Return the element-wise product of a dense vector and a constant
template < typename T >
inline CoinDenseVector< T > operator*(const CoinDenseVector< T > &op1, T value)
{
  int size = op1.size();
  CoinDenseVector< T > op3(size);
  const T *elements1 = op1.getElements();
  T *elements3 = op3.getElements();
  double dvalue = value;
  for (int i = 0; i < size; i++)
    elements3[i] = elements1[i] * dvalue;
  return op3;
}

/// Return the element-wise ratio of a dense vector and a constant
template < typename T >
inline CoinDenseVector< T > operator/(const CoinDenseVector< T > &op1, T value)
{
  int size = op1.size();
  CoinDenseVector< T > op3(size);
  const T *elements1 = op1.getElements();
  T *elements3 = op3.getElements();
  double dvalue = value;
  for (int i = 0; i < size; i++)
    elements3[i] = elements1[i] / dvalue;
  return op3;
}

/// Return the sum of a constant and a dense vector
template < typename T >
inline CoinDenseVector< T > operator+(T value, const CoinDenseVector< T > &op1)
{
  int size = op1.size();
  CoinDenseVector< T > op3(size);
  const T *elements1 = op1.getElements();
  T *elements3 = op3.getElements();
  double dvalue = value;
  for (int i = 0; i < size; i++)
    elements3[i] = elements1[i] + dvalue;
  return op3;
}

/// Return the difference of a constant and a dense vector
template < typename T >
inline CoinDenseVector< T > operator-(T value, const CoinDenseVector< T > &op1)
{
  int size = op1.size();
  CoinDenseVector< T > op3(size);
  const T *elements1 = op1.getElements();
  T *elements3 = op3.getElements();
  double dvalue = value;
  for (int i = 0; i < size; i++)
    elements3[i] = dvalue - elements1[i];
  return op3;
}

/// Return the element-wise product of a constant and a dense vector
template < typename T >
inline CoinDenseVector< T > operator*(T value, const CoinDenseVector< T > &op1)
{
  int size = op1.size();
  CoinDenseVector< T > op3(size);
  const T *elements1 = op1.getElements();
  T *elements3 = op3.getElements();
  double dvalue = value;
  for (int i = 0; i < size; i++)
    elements3[i] = elements1[i] * dvalue;
  return op3;
}

/// Return the element-wise ratio of a a constant and dense vector
template < typename T >
inline CoinDenseVector< T > operator/(T value, const CoinDenseVector< T > &op1)
{
  int size = op1.size();
  CoinDenseVector< T > op3(size);
  const T *elements1 = op1.getElements();
  T *elements3 = op3.getElements();
  double dvalue = value;
  for (int i = 0; i < size; i++)
    elements3[i] = dvalue / elements1[i];
  return op3;
}
//@}

#endif

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