CoinDenseVector.hpp
Go to the documentation of this file.
1 /* $Id: CoinDenseVector.hpp 1215 2009-11-05 11:03:04Z forrest $ */
2 // Copyright (C) 2003, International Business Machines
3 // Corporation and others. All Rights Reserved.
4 #ifndef CoinDenseVector_H
5 #define CoinDenseVector_H
6 
7 #if defined(_MSC_VER)
8 // Turn off compiler warning about long names
9 # pragma warning(disable:4786)
10 #endif
11 
12 #include <cassert>
13 #include <cstdlib>
14 #include <cmath>
15 #include "CoinHelperFunctions.hpp"
16 
17 //#############################################################################
23  template <typename T> void
24  CoinDenseVectorUnitTest(T dummy);
25 
26 //#############################################################################
65 template <typename T> class CoinDenseVector {
66 private:
69  int nElements_;
72  T * elements_;
74 
75 public:
78  inline int getNumElements() const { return nElements_; }
80  inline int size() const { return nElements_; }
82  inline const T * getElements() const { return elements_; }
84  inline T * getElements() { return elements_; }
86 
87  //-------------------------------------------------------------------
88  // Set indices and elements
89  //-------------------------------------------------------------------
92  void clear();
97  T & operator[](int index) const;
98 
103  void setVector(int size, const T * elems);
104 
105 
107  void setConstant(int size, T elems);
108 
109 
113  void setElement(int index, T element);
117  void resize(int newSize, T fill=T());
118 
120  void append(const CoinDenseVector &);
122 
125  inline T oneNorm() const {
127  T norm = 0;
128  for (int i=0; i<nElements_; i++)
129  norm += CoinAbs(elements_[i]);
130  return norm;
131  }
133  inline double twoNorm() const {
134  double norm = 0.;
135  for (int i=0; i<nElements_; i++)
136  norm += elements_[i] * elements_[i];
137  // std namespace removed because it was causing a compile
138  // problem with Microsoft Visual C++
139  return /*std::*/sqrt(norm);
140  }
142  inline T infNorm() const {
143  T norm = 0;
144  for (int i=0; i<nElements_; i++)
145  norm = CoinMax(norm, CoinAbs(elements_[i]));
146  return norm;
147  }
149  inline T sum() const {
150  T sume = 0;
151  for (int i=0; i<nElements_; i++)
152  sume += elements_[i];
153  return sume;
154  }
156  inline void scale(T factor) {
157  for (int i=0; i<nElements_; i++)
158  elements_[i] *= factor;
159  return;
160  }
162 
165  void operator+=(T value);
168  void operator-=(T value);
170  void operator*=(T value);
172  void operator/=(T value);
174 
178  CoinDenseVector();
180  CoinDenseVector(int size, const T * elems);
182  CoinDenseVector(int size, T element=T());
185 
187  ~CoinDenseVector ();
189 
190 private:
193  void gutsOfSetVector(int size, const T * elems);
196  void gutsOfSetConstant(int size, T value);
198 };
199 
200 //#############################################################################
201 
209 template <typename T> inline
212  const CoinDenseVector<T>& op2){
213  assert(op1.size() == op2.size());
214  int size = op1.size();
215  CoinDenseVector<T> op3(size);
216  const T *elements1 = op1.getElements();
217  const T *elements2 = op2.getElements();
218  T *elements3 = op3.getElements();
219  for(int i=0; i<size; i++)
220  elements3[i] = elements1[i] + elements2[i];
221  return op3;
222 }
223 
225 template <typename T> inline
227  const CoinDenseVector<T>& op2){
228  assert(op1.size() == op2.size());
229  int size = op1.size();
230  CoinDenseVector<T> op3(size);
231  const T *elements1 = op1.getElements();
232  const T *elements2 = op2.getElements();
233  T *elements3 = op3.getElements();
234  for(int i=0; i<size; i++)
235  elements3[i] = elements1[i] - elements2[i];
236  return op3;
237 }
238 
239 
241 template <typename T> inline
243  const CoinDenseVector<T>& op2){
244  assert(op1.size() == op2.size());
245  int size = op1.size();
246  CoinDenseVector<T> op3(size);
247  const T *elements1 = op1.getElements();
248  const T *elements2 = op2.getElements();
249  T *elements3 = op3.getElements();
250  for(int i=0; i<size; i++)
251  elements3[i] = elements1[i] * elements2[i];
252  return op3;
253 }
254 
256 template <typename T> inline
258  const CoinDenseVector<T>& op2){
259  assert(op1.size() == op2.size());
260  int size = op1.size();
261  CoinDenseVector<T> op3(size);
262  const T *elements1 = op1.getElements();
263  const T *elements2 = op2.getElements();
264  T *elements3 = op3.getElements();
265  for(int i=0; i<size; i++)
266  elements3[i] = elements1[i] / elements2[i];
267  return op3;
268 }
270 
276 template <typename T> inline
279  int size = op1.size();
280  CoinDenseVector<T> op3(size);
281  const T *elements1 = op1.getElements();
282  T *elements3 = op3.getElements();
283  double dvalue = value;
284  for(int i=0; i<size; i++)
285  elements3[i] = elements1[i] + dvalue;
286  return op3;
287 }
288 
290 template <typename T> inline
292  int size = op1.size();
293  CoinDenseVector<T> op3(size);
294  const T *elements1 = op1.getElements();
295  T *elements3 = op3.getElements();
296  double dvalue = value;
297  for(int i=0; i<size; i++)
298  elements3[i] = elements1[i] - dvalue;
299  return op3;
300 }
301 
303 template <typename T> inline
305  int size = op1.size();
306  CoinDenseVector<T> op3(size);
307  const T *elements1 = op1.getElements();
308  T *elements3 = op3.getElements();
309  double dvalue = value;
310  for(int i=0; i<size; i++)
311  elements3[i] = elements1[i] * dvalue;
312  return op3;
313 }
314 
316 template <typename T> inline
318  int size = op1.size();
319  CoinDenseVector<T> op3(size);
320  const T *elements1 = op1.getElements();
321  T *elements3 = op3.getElements();
322  double dvalue = value;
323  for(int i=0; i<size; i++)
324  elements3[i] = elements1[i] / dvalue;
325  return op3;
326 }
327 
329 template <typename T> inline
331  int size = op1.size();
332  CoinDenseVector<T> op3(size);
333  const T *elements1 = op1.getElements();
334  T *elements3 = op3.getElements();
335  double dvalue = value;
336  for(int i=0; i<size; i++)
337  elements3[i] = elements1[i] + dvalue;
338  return op3;
339 }
340 
342 template <typename T> inline
344  int size = op1.size();
345  CoinDenseVector<T> op3(size);
346  const T *elements1 = op1.getElements();
347  T *elements3 = op3.getElements();
348  double dvalue = value;
349  for(int i=0; i<size; i++)
350  elements3[i] = dvalue - elements1[i];
351  return op3;
352 }
353 
355 template <typename T> inline
357  int size = op1.size();
358  CoinDenseVector<T> op3(size);
359  const T *elements1 = op1.getElements();
360  T *elements3 = op3.getElements();
361  double dvalue = value;
362  for(int i=0; i<size; i++)
363  elements3[i] = elements1[i] * dvalue;
364  return op3;
365 }
366 
368 template <typename T> inline
370  int size = op1.size();
371  CoinDenseVector<T> op3(size);
372  const T *elements1 = op1.getElements();
373  T *elements3 = op3.getElements();
374  double dvalue = value;
375  for(int i=0; i<size; i++)
376  elements3[i] = dvalue / elements1[i];
377  return op3;
378 }
380 
381 #endif
int nElements_
Size of element vector.
CoinDenseVector< T > operator*(const CoinDenseVector< T > &op1, const CoinDenseVector< T > &op2)
Return the element-wise product of two dense vectors.
T CoinMax(register const T x1, register const T x2)
Return the larger (according to operator<() of the arguments.
void scale(T factor)
scale vector elements
void operator*=(T value)
multiply every entry by value
const T * getElements() const
Get element values.
Dense Vector.
int getNumElements() const
Get the size.
void setElement(int index, T element)
Set an existing element in the dense vector The first argument is the "index" into the elements() arr...
T infNorm() const
infinity-norm of vector
T CoinAbs(const T value)
Return the absolute value of the argument.
CoinDenseVector()
Default constructor.
void gutsOfSetConstant(int size, T value)
Set all elements to a given value.
T oneNorm() const
1-norm of vector
void gutsOfSetVector(int size, const T *elems)
Copy internal data.
CoinDenseVector< T > operator-(const CoinDenseVector< T > &op1, const CoinDenseVector< T > &op2)
Return the difference of two dense vectors.
void operator/=(T value)
divide every entry by value
void clear()
Reset the vector (i.e. set all elemenets to zero)
T sum() const
sum of vector elements
T & operator[](int index) const
Member of array operator.
void resize(int newSize, T fill=T())
Resize the dense vector to be the first newSize elements.
void operator+=(T value)
add value to every entry
void append(const CoinDenseVector &)
Append a dense vector to this dense vector.
int size() const
Get the size.
CoinDenseVector & operator=(const CoinDenseVector &)
Assignment operator.
void setVector(int size, const T *elems)
Set vector size, and elements.
~CoinDenseVector()
Destructor.
T * getElements()
Get element values.
CoinDenseVector< T > operator/(const CoinDenseVector< T > &op1, const CoinDenseVector< T > &op2)
Return the element-wise ratio of two dense vectors.
void CoinDenseVectorUnitTest(T dummy)
A function that tests the methods in the CoinDenseVector class.
void operator-=(T value)
subtract value from every entry
CoinDenseVector< T > operator+(const CoinDenseVector< T > &op1, const CoinDenseVector< T > &op2)
Return the sum of two dense vectors.
double twoNorm() const
2-norm of vector
void setConstant(int size, T elems)
Elements set to have the same scalar value.
T * elements_
Vector elements.