Cbc  2.9.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CoinDenseVector.hpp
Go to the documentation of this file.
1 /* $Id: CoinDenseVector.hpp 1372 2011-01-03 23:31:00Z lou $ */
2 // Copyright (C) 2003, 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 CoinDenseVector_H
7 #define CoinDenseVector_H
8 
9 #if defined(_MSC_VER)
10 // Turn off compiler warning about long names
11 # pragma warning(disable:4786)
12 #endif
13 
14 #include <cassert>
15 #include <cstdlib>
16 #include <cmath>
17 #include "CoinHelperFunctions.hpp"
18 
19 //#############################################################################
25  template <typename T> void
26  CoinDenseVectorUnitTest(T dummy);
27 
28 //#############################################################################
67 template <typename T> class CoinDenseVector {
68 private:
71  int nElements_;
74  T * elements_;
76 
77 public:
80  inline int getNumElements() const { return nElements_; }
82  inline int size() const { return nElements_; }
84  inline const T * getElements() const { return elements_; }
86  inline T * getElements() { return elements_; }
88 
89  //-------------------------------------------------------------------
90  // Set indices and elements
91  //-------------------------------------------------------------------
94  void clear();
99  T & operator[](int index) const;
100 
105  void setVector(int size, const T * elems);
106 
107 
109  void setConstant(int size, T elems);
110 
111 
115  void setElement(int index, T element);
119  void resize(int newSize, T fill=T());
120 
122  void append(const CoinDenseVector &);
124 
127  inline T oneNorm() const {
129  T norm = 0;
130  for (int i=0; i<nElements_; i++)
131  norm += CoinAbs(elements_[i]);
132  return norm;
133  }
135  inline double twoNorm() const {
136  double norm = 0.;
137  for (int i=0; i<nElements_; i++)
138  norm += elements_[i] * elements_[i];
139  // std namespace removed because it was causing a compile
140  // problem with Microsoft Visual C++
141  return /*std::*/sqrt(norm);
142  }
144  inline T infNorm() const {
145  T norm = 0;
146  for (int i=0; i<nElements_; i++)
147  norm = CoinMax(norm, CoinAbs(elements_[i]));
148  return norm;
149  }
151  inline T sum() const {
152  T sume = 0;
153  for (int i=0; i<nElements_; i++)
154  sume += elements_[i];
155  return sume;
156  }
158  inline void scale(T factor) {
159  for (int i=0; i<nElements_; i++)
160  elements_[i] *= factor;
161  return;
162  }
164 
167  void operator+=(T value);
170  void operator-=(T value);
172  void operator*=(T value);
174  void operator/=(T value);
176 
180  CoinDenseVector();
182  CoinDenseVector(int size, const T * elems);
184  CoinDenseVector(int size, T element=T());
187 
189  ~CoinDenseVector ();
191 
192 private:
195  void gutsOfSetVector(int size, const T * elems);
198  void gutsOfSetConstant(int size, T value);
200 };
201 
202 //#############################################################################
203 
211 template <typename T> inline
214  const CoinDenseVector<T>& op2){
215  assert(op1.size() == op2.size());
216  int size = op1.size();
217  CoinDenseVector<T> op3(size);
218  const T *elements1 = op1.getElements();
219  const T *elements2 = op2.getElements();
220  T *elements3 = op3.getElements();
221  for(int i=0; i<size; i++)
222  elements3[i] = elements1[i] + elements2[i];
223  return op3;
224 }
225 
227 template <typename T> inline
229  const CoinDenseVector<T>& op2){
230  assert(op1.size() == op2.size());
231  int size = op1.size();
232  CoinDenseVector<T> op3(size);
233  const T *elements1 = op1.getElements();
234  const T *elements2 = op2.getElements();
235  T *elements3 = op3.getElements();
236  for(int i=0; i<size; i++)
237  elements3[i] = elements1[i] - elements2[i];
238  return op3;
239 }
240 
241 
243 template <typename T> inline
245  const CoinDenseVector<T>& op2){
246  assert(op1.size() == op2.size());
247  int size = op1.size();
248  CoinDenseVector<T> op3(size);
249  const T *elements1 = op1.getElements();
250  const T *elements2 = op2.getElements();
251  T *elements3 = op3.getElements();
252  for(int i=0; i<size; i++)
253  elements3[i] = elements1[i] * elements2[i];
254  return op3;
255 }
256 
258 template <typename T> inline
260  const CoinDenseVector<T>& op2){
261  assert(op1.size() == op2.size());
262  int size = op1.size();
263  CoinDenseVector<T> op3(size);
264  const T *elements1 = op1.getElements();
265  const T *elements2 = op2.getElements();
266  T *elements3 = op3.getElements();
267  for(int i=0; i<size; i++)
268  elements3[i] = elements1[i] / elements2[i];
269  return op3;
270 }
272 
278 template <typename T> inline
281  int size = op1.size();
282  CoinDenseVector<T> op3(size);
283  const T *elements1 = op1.getElements();
284  T *elements3 = op3.getElements();
285  double dvalue = value;
286  for(int i=0; i<size; i++)
287  elements3[i] = elements1[i] + dvalue;
288  return op3;
289 }
290 
292 template <typename T> inline
294  int size = op1.size();
295  CoinDenseVector<T> op3(size);
296  const T *elements1 = op1.getElements();
297  T *elements3 = op3.getElements();
298  double dvalue = value;
299  for(int i=0; i<size; i++)
300  elements3[i] = elements1[i] - dvalue;
301  return op3;
302 }
303 
305 template <typename T> inline
307  int size = op1.size();
308  CoinDenseVector<T> op3(size);
309  const T *elements1 = op1.getElements();
310  T *elements3 = op3.getElements();
311  double dvalue = value;
312  for(int i=0; i<size; i++)
313  elements3[i] = elements1[i] * dvalue;
314  return op3;
315 }
316 
318 template <typename T> inline
320  int size = op1.size();
321  CoinDenseVector<T> op3(size);
322  const T *elements1 = op1.getElements();
323  T *elements3 = op3.getElements();
324  double dvalue = value;
325  for(int i=0; i<size; i++)
326  elements3[i] = elements1[i] / dvalue;
327  return op3;
328 }
329 
331 template <typename T> inline
333  int size = op1.size();
334  CoinDenseVector<T> op3(size);
335  const T *elements1 = op1.getElements();
336  T *elements3 = op3.getElements();
337  double dvalue = value;
338  for(int i=0; i<size; i++)
339  elements3[i] = elements1[i] + dvalue;
340  return op3;
341 }
342 
344 template <typename T> inline
346  int size = op1.size();
347  CoinDenseVector<T> op3(size);
348  const T *elements1 = op1.getElements();
349  T *elements3 = op3.getElements();
350  double dvalue = value;
351  for(int i=0; i<size; i++)
352  elements3[i] = dvalue - elements1[i];
353  return op3;
354 }
355 
357 template <typename T> inline
359  int size = op1.size();
360  CoinDenseVector<T> op3(size);
361  const T *elements1 = op1.getElements();
362  T *elements3 = op3.getElements();
363  double dvalue = value;
364  for(int i=0; i<size; i++)
365  elements3[i] = elements1[i] * dvalue;
366  return op3;
367 }
368 
370 template <typename T> inline
372  int size = op1.size();
373  CoinDenseVector<T> op3(size);
374  const T *elements1 = op1.getElements();
375  T *elements3 = op3.getElements();
376  double dvalue = value;
377  for(int i=0; i<size; i++)
378  elements3[i] = dvalue / elements1[i];
379  return op3;
380 }
382 
383 #endif
void operator/=(T value)
divide every entry by value
void scale(T factor)
scale vector elements
CoinDenseVector()
Default constructor.
double twoNorm() const
2-norm of vector
T infNorm() const
infinity-norm of vector
void gutsOfSetConstant(int size, T value)
Set all elements to a given value.
CoinDenseVector< T > operator*(const CoinDenseVector< T > &op1, const CoinDenseVector< T > &op2)
Return the element-wise product of two dense vectors.
void setElement(int index, T element)
Set an existing element in the dense vector The first argument is the &quot;index&quot; into the elements() arr...
const T * getElements() const
Get element values.
CoinDenseVector< T > operator+(const CoinDenseVector< T > &op1, const CoinDenseVector< T > &op2)
Return the sum of two dense vectors.
void operator*=(T value)
multiply every entry by value
CoinDenseVector< T > operator-(const CoinDenseVector< T > &op1, const CoinDenseVector< T > &op2)
Return the difference of two dense vectors.
int size() const
void operator+=(T value)
add value to every entry
void operator-=(T value)
subtract value from every entry
void clear()
Reset the vector (i.e. set all elemenets to zero)
void setVector(int size, const T *elems)
Set vector size, and elements.
T * elements_
Vector elements.
T * getElements()
Get element values.
CoinDenseVector & operator=(const CoinDenseVector &)
Assignment operator.
CoinDenseVector< T > operator/(const CoinDenseVector< T > &op1, const CoinDenseVector< T > &op2)
Return the element-wise ratio of two dense vectors.
void gutsOfSetVector(int size, const T *elems)
Copy internal data.
T & operator[](int index) const
Member of array operator.
Dense Vector.
void append(const CoinDenseVector &)
Append a dense vector to this dense vector.
T oneNorm() const
1-norm of vector
T CoinAbs(const T value)
Return the absolute value of the argument.
T sum() const
sum of vector elements
void CoinDenseVectorUnitTest(T dummy)
A function that tests the methods in the CoinDenseVector class.
~CoinDenseVector()
Destructor.
T CoinMax(register const T x1, register const T x2)
Return the larger (according to operator&lt;() of the arguments.
void resize(int newSize, T fill=T())
Resize the dense vector to be the first newSize elements.
int nElements_
Size of element vector.
int getNumElements() const
Get the size.
void setConstant(int size, T elems)
Elements set to have the same scalar value.