Clp 1.17.6

- 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
CoinDenseVector.hpp
Go to the documentation of this file.
1 /* $Id: CoinDenseVector.hpp 2083 2019-01-06 19:38:09Z unxusr $ */
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 >
26 void CoinDenseVectorUnitTest(T dummy);
27
28 //#############################################################################
67 template < typename T >
68 class CoinDenseVector {
69 private:
72 int nElements_;
75 T *elements_;
77
78 public:
81 inline int getNumElements() const { return nElements_; }
83 inline int size() const { return nElements_; }
85 inline const T *getElements() const { return elements_; }
87 inline T *getElements() { return elements_; }
89
90 //-------------------------------------------------------------------
91 // Set indices and elements
92 //-------------------------------------------------------------------
95 void clear();
98 CoinDenseVector &operator=(const CoinDenseVector &);
100 T &operator[](int index) const;
101
106 void setVector(int size, const T *elems);
107
109 void setConstant(int size, T elems);
110
114 void setElement(int index, T element);
118 void resize(int newSize, T fill = T());
119
121 void append(const CoinDenseVector &);
123
126 inline T oneNorm() const
128 {
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 {
137 double norm = 0.;
138 for (int i = 0; i < nElements_; i++)
139 norm += elements_[i] * elements_[i];
140 // std namespace removed because it was causing a compile
141 // problem with Microsoft Visual C++
142 return /*std::*/ sqrt(norm);
143 }
145 inline T infNorm() const
146 {
147 T norm = 0;
148 for (int i = 0; i < nElements_; i++)
149 norm = CoinMax(norm, CoinAbs(elements_[i]));
150 return norm;
151 }
153 inline T sum() const
154 {
155 T sume = 0;
156 for (int i = 0; i < nElements_; i++)
157 sume += elements_[i];
158 return sume;
159 }
161 inline void scale(T factor)
162 {
163 for (int i = 0; i < nElements_; i++)
164 elements_[i] *= factor;
165 return;
166 }
168
171 void operator+=(T value);
174 void operator-=(T value);
176 void operator*=(T value);
178 void operator/=(T value);
180
184 CoinDenseVector();
186 CoinDenseVector(int size, const T *elems);
188 CoinDenseVector(int size, T element = T());
190 CoinDenseVector(const CoinDenseVector &);
191
193 ~CoinDenseVector();
195
196 private:
199 void gutsOfSetVector(int size, const T *elems);
202 void gutsOfSetConstant(int size, T value);
204 };
205
206 //#############################################################################
207
215 template < typename T >
217 inline CoinDenseVector< T > operator+(const CoinDenseVector< T > &op1,
218 const CoinDenseVector< T > &op2)
219 {
220 assert(op1.size() == op2.size());
221 int size = op1.size();
222 CoinDenseVector< T > op3(size);
223 const T *elements1 = op1.getElements();
224 const T *elements2 = op2.getElements();
225 T *elements3 = op3.getElements();
226 for (int i = 0; i < size; i++)
227 elements3[i] = elements1[i] + elements2[i];
228 return op3;
229 }
230
232 template < typename T >
233 inline CoinDenseVector< T > operator-(const CoinDenseVector< T > &op1,
234 const CoinDenseVector< T > &op2)
235 {
236 assert(op1.size() == op2.size());
237 int size = op1.size();
238 CoinDenseVector< T > op3(size);
239 const T *elements1 = op1.getElements();
240 const T *elements2 = op2.getElements();
241 T *elements3 = op3.getElements();
242 for (int i = 0; i < size; i++)
243 elements3[i] = elements1[i] - elements2[i];
244 return op3;
245 }
246
248 template < typename T >
249 inline CoinDenseVector< T > operator*(const CoinDenseVector< T > &op1,
250 const CoinDenseVector< T > &op2)
251 {
252 assert(op1.size() == op2.size());
253 int size = op1.size();
254 CoinDenseVector< T > op3(size);
255 const T *elements1 = op1.getElements();
256 const T *elements2 = op2.getElements();
257 T *elements3 = op3.getElements();
258 for (int i = 0; i < size; i++)
259 elements3[i] = elements1[i] * elements2[i];
260 return op3;
261 }
262
264 template < typename T >
265 inline CoinDenseVector< T > operator/(const CoinDenseVector< T > &op1,
266 const CoinDenseVector< T > &op2)
267 {
268 assert(op1.size() == op2.size());
269 int size = op1.size();
270 CoinDenseVector< T > op3(size);
271 const T *elements1 = op1.getElements();
272 const T *elements2 = op2.getElements();
273 T *elements3 = op3.getElements();
274 for (int i = 0; i < size; i++)
275 elements3[i] = elements1[i] / elements2[i];
276 return op3;
277 }
279
285 template < typename T >
287 inline CoinDenseVector< T > operator+(const CoinDenseVector< T > &op1, T value)
288 {
289 int size = op1.size();
290 CoinDenseVector< T > op3(size);
291 const T *elements1 = op1.getElements();
292 T *elements3 = op3.getElements();
293 double dvalue = value;
294 for (int i = 0; i < size; i++)
295 elements3[i] = elements1[i] + dvalue;
296 return op3;
297 }
298
300 template < typename T >
301 inline CoinDenseVector< T > operator-(const CoinDenseVector< T > &op1, T value)
302 {
303 int size = op1.size();
304 CoinDenseVector< T > op3(size);
305 const T *elements1 = op1.getElements();
306 T *elements3 = op3.getElements();
307 double dvalue = value;
308 for (int i = 0; i < size; i++)
309 elements3[i] = elements1[i] - dvalue;
310 return op3;
311 }
312
314 template < typename T >
315 inline CoinDenseVector< T > operator*(const CoinDenseVector< T > &op1, T value)
316 {
317 int size = op1.size();
318 CoinDenseVector< T > op3(size);
319 const T *elements1 = op1.getElements();
320 T *elements3 = op3.getElements();
321 double dvalue = value;
322 for (int i = 0; i < size; i++)
323 elements3[i] = elements1[i] * dvalue;
324 return op3;
325 }
326
328 template < typename T >
329 inline CoinDenseVector< T > operator/(const CoinDenseVector< T > &op1, T value)
330 {
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 >
343 inline CoinDenseVector< T > operator+(T value, const CoinDenseVector< T > &op1)
344 {
345 int size = op1.size();
346 CoinDenseVector< T > op3(size);
347 const T *elements1 = op1.getElements();
348 T *elements3 = op3.getElements();
349 double dvalue = value;
350 for (int i = 0; i < size; i++)
351 elements3[i] = elements1[i] + dvalue;
352 return op3;
353 }
354
356 template < typename T >
357 inline CoinDenseVector< T > operator-(T value, const CoinDenseVector< T > &op1)
358 {
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] = dvalue - elements1[i];
366 return op3;
367 }
368
370 template < typename T >
371 inline CoinDenseVector< T > operator*(T value, const CoinDenseVector< T > &op1)
372 {
373 int size = op1.size();
374 CoinDenseVector< T > op3(size);
375 const T *elements1 = op1.getElements();
376 T *elements3 = op3.getElements();
377 double dvalue = value;
378 for (int i = 0; i < size; i++)
379 elements3[i] = elements1[i] * dvalue;
380 return op3;
381 }
382
384 template < typename T >
385 inline CoinDenseVector< T > operator/(T value, const CoinDenseVector< T > &op1)
386 {
387 int size = op1.size();
388 CoinDenseVector< T > op3(size);
389 const T *elements1 = op1.getElements();
390 T *elements3 = op3.getElements();
391 double dvalue = value;
392 for (int i = 0; i < size; i++)
393 elements3[i] = dvalue / elements1[i];
394 return op3;
395 }
397
398 #endif
399
400 /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
401 */
CoinHelperFunctions.hpp
CoinDenseVector::operator/=
void operator/=(T value)
divide every entry by value
CoinDenseVector::scale
void scale(T factor)
scale vector elements
Definition: CoinDenseVector.hpp:161
CoinDenseVector::CoinDenseVector
CoinDenseVector()
Default constructor.
CoinDenseVector::twoNorm
double twoNorm() const
2-norm of vector
Definition: CoinDenseVector.hpp:135
CoinDenseVector::infNorm
T infNorm() const
infinity-norm of vector
Definition: CoinDenseVector.hpp:145
CoinDenseVector::gutsOfSetConstant
void gutsOfSetConstant(int size, T value)
Set all elements to a given value.
CoinDenseVector::setElement
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...
CoinDenseVector::getElements
const T * getElements() const
Get element values.
Definition: CoinDenseVector.hpp:85
CoinDenseVector::operator*=
void operator*=(T value)
multiply every entry by value
CoinDenseVector::size
int size() const
Definition: CoinDenseVector.hpp:83
CoinDenseVector::operator+=
void operator+=(T value)
add value to every entry
CoinDenseVector::operator-=
void operator-=(T value)
subtract value from every entry
operator+
CoinDenseVector< T > operator+(const CoinDenseVector< T > &op1, const CoinDenseVector< T > &op2)
Return the sum of two dense vectors.
Definition: CoinDenseVector.hpp:217
CoinDenseVector::clear
void clear()
Reset the vector (i.e. set all elemenets to zero)
CoinDenseVector::setVector
void setVector(int size, const T *elems)
Set vector size, and elements.
CoinDenseVector::elements_
T * elements_
Vector elements.
Definition: CoinDenseVector.hpp:75
CoinDenseVector::getElements
T * getElements()
Get element values.
Definition: CoinDenseVector.hpp:87
CoinDenseVector::operator=
CoinDenseVector & operator=(const CoinDenseVector &)
Assignment operator.
operator-
CoinDenseVector< T > operator-(const CoinDenseVector< T > &op1, const CoinDenseVector< T > &op2)
Return the difference of two dense vectors.
Definition: CoinDenseVector.hpp:233
CoinDenseVector::gutsOfSetVector
void gutsOfSetVector(int size, const T *elems)
Copy internal data.
CoinMax
T CoinMax(const T x1, const T x2)
Return the larger (according to operator&lt;() of the arguments.
Definition: CoinHelperFunctions.hpp:639
CoinDenseVector::operator[]
T & operator[](int index) const
Member of array operator.
CoinDenseVector
Dense Vector.
Definition: CoinDenseVector.hpp:68
CoinDenseVector::append
void append(const CoinDenseVector &)
Append a dense vector to this dense vector.
operator/
CoinDenseVector< T > operator/(const CoinDenseVector< T > &op1, const CoinDenseVector< T > &op2)
Return the element-wise ratio of two dense vectors.
Definition: CoinDenseVector.hpp:265
CoinDenseVector::oneNorm
T oneNorm() const
1-norm of vector
Definition: CoinDenseVector.hpp:127
CoinAbs
T CoinAbs(const T value)
Return the absolute value of the argument.
Definition: CoinHelperFunctions.hpp:663
CoinDenseVector::sum
T sum() const
sum of vector elements
Definition: CoinDenseVector.hpp:153
CoinDenseVectorUnitTest
void CoinDenseVectorUnitTest(T dummy)
A function that tests the methods in the CoinDenseVector class.
CoinDenseVector::~CoinDenseVector
~CoinDenseVector()
Destructor.
operator*
CoinDenseVector< T > operator*(const CoinDenseVector< T > &op1, const CoinDenseVector< T > &op2)
Return the element-wise product of two dense vectors.
Definition: CoinDenseVector.hpp:249
CoinDenseVector::resize
void resize(int newSize, T fill=T())
Resize the dense vector to be the first newSize elements.
CoinDenseVector::nElements_
int nElements_
Size of element vector.
Definition: CoinDenseVector.hpp:73
CoinDenseVector::getNumElements
int getNumElements() const
Get the size.
Definition: CoinDenseVector.hpp:82
CoinDenseVector::setConstant
void setConstant(int size, T elems)
Elements set to have the same scalar value.