Scippy

SoPlex

Sequential object-oriented simPlex

vectorbase.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the class library */
4 /* SoPlex --- the Sequential object-oriented simPlex. */
5 /* */
6 /* Copyright (C) 1996 Roland Wunderling */
7 /* 1996-2017 Konrad-Zuse-Zentrum */
8 /* fuer Informationstechnik Berlin */
9 /* */
10 /* SoPlex is distributed under the terms of the ZIB Academic Licence. */
11 /* */
12 /* You should have received a copy of the ZIB Academic License */
13 /* along with SoPlex; see the file COPYING. If not email to soplex@zib.de. */
14 /* */
15 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
16 
17 /**@file vectorbase.h
18  * @brief Dense vector.
19  */
20 #ifndef _VECTORBASE_H_
21 #define _VECTORBASE_H_
22 
23 #include <assert.h>
24 #include <string.h>
25 #include <math.h>
26 #include <iostream>
27 
28 namespace soplex
29 {
30 template < class R > class SVectorBase;
31 template < class R > class SSVectorBase;
32 
33 /**@brief Dense vector.
34  * @ingroup Algebra
35  *
36  * Class VectorBase provides dense linear algebra vectors. It does not provide memory management for the %array of
37  * values. Instead, the constructor requires a pointer to a memory block large enough to fit the desired dimension of
38  * Real or Rational values.
39  *
40  * After construction, the values of a VectorBase can be accessed with the subscript operator[](). Safety is provided by
41  * qchecking of array bound when accessing elements with the subscript operator[]() (only when compiled without \c
42  * -DNDEBUG).
43  *
44  * A VectorBase is distinguished from a simple array of %Reals or %Rationals by providing a set of mathematical
45  * operations. Since VectorBase does not provide any memory management features, no operations are available that would
46  * require allocation of temporary memory space.
47  *
48  * The following mathematical operations are provided by class VectorBase (VectorBase \p a, \p b; R \p x):
49  *
50  * <TABLE>
51  * <TR><TD>Operation</TD><TD>Description </TD><TD></TD>&nbsp;</TR>
52  * <TR><TD>\c -= </TD><TD>subtraction </TD><TD>\c a \c -= \c b </TD></TR>
53  * <TR><TD>\c += </TD><TD>addition </TD><TD>\c a \c += \c b </TD></TR>
54  * <TR><TD>\c * </TD><TD>scalar product</TD>
55  * <TD>\c x = \c a \c * \c b </TD></TR>
56  * <TR><TD>\c *= </TD><TD>scaling </TD><TD>\c a \c *= \c x </TD></TR>
57  * <TR><TD>maxAbs() </TD><TD>infinity norm </TD>
58  * <TD>\c a.maxAbs() == \f$\|a\|_{\infty}\f$ </TD></TR>
59  * <TR><TD>minAbs() </TD><TD> </TD>
60  * <TD>\c a.minAbs() == \f$\min |a_i|\f$ </TD></TR>
61  *
62  * <TR><TD>length() </TD><TD>euclidian norm</TD>
63  * <TD>\c a.length() == \f$\sqrt{a^2}\f$ </TD></TR>
64  * <TR><TD>length2()</TD><TD>square norm </TD>
65  * <TD>\c a.length2() == \f$a^2\f$ </TD></TR>
66  * <TR><TD>multAdd(\c x,\c b)</TD><TD>add scaled vector</TD>
67  * <TD> \c a += \c x * \c b </TD></TR>
68  * </TABLE>
69  *
70  * When using any of these operations, the vectors involved must be of the same dimension. Also an SVectorBase \c b is
71  * allowed if it does not contain nonzeros with index greater than the dimension of \c a.q
72  */
73 template < class R >
74 class VectorBase
75 {
76 protected:
77 
78  // ------------------------------------------------------------------------------------------------------------------
79  /**@name Data */
80  //@{
81 
82  /// Dimension of vector.
83  int dimen;
84 
85  /// Values of vector.
86  /** The memory block pointed to by val must at least have size dimen * sizeof(R). */
87  R* val;
88 
89  //@}
90 
91 public:
92 
93  // ------------------------------------------------------------------------------------------------------------------
94  /**@name Construction and assignment */
95  //@{
96 
97  /// Constructor.
98  /** There is no default constructor since the storage for a VectorBase must be provided externally. Storage must be
99  * passed as a memory block val at construction. It must be large enough to fit at least dimen values.
100  */
101  VectorBase<R>(int p_dimen, R* p_val)
102  : dimen(p_dimen)
103  , val(p_val)
104  {
105  assert(dimen >= 0);
106  assert(isConsistent());
107  }
108 
109  /// Assignment operator.
110  template < class S >
112  {
113  if( (VectorBase<S>*)this != &vec )
114  {
115  assert(dim() == vec.dim());
116 
117  for( int i = 0; i < dimen; i++ )
118  val[i] = vec[i];
119 
120  assert(isConsistent());
121  }
122 
123  return *this;
124  }
125 
126  /// Assignment operator.
128  {
129  if( this != &vec )
130  {
131  assert(dim() == vec.dim());
132 
133  for( int i = 0; i < dimen; i++ )
134  val[i] = vec[i];
135 
136  assert(isConsistent());
137  }
138 
139  return *this;
140  }
141 
142  /// scale and assign
143  VectorBase<Real>& scaleAssign(int scaleExp, const VectorBase<Real>& vec)
144  {
145  if( this != &vec )
146  {
147  assert(dim() == vec.dim());
148 
149  for( int i = 0; i < dimen; i++ )
150  val[i] = spxLdexp(vec[i], scaleExp);
151 
152  assert(isConsistent());
153  }
154 
155  return *this;
156  }
157 
158  /// scale and assign
159  VectorBase<Real>& scaleAssign(const int* scaleExp, const VectorBase<Real>& vec, bool negateExp = false)
160  {
161  if( this != &vec )
162  {
163  assert(dim() == vec.dim());
164 
165  if( negateExp)
166  {
167  for( int i = 0; i < dimen; i++ )
168  val[i] = spxLdexp(vec[i], -scaleExp[i]);
169  }
170  else
171  {
172  for( int i = 0; i < dimen; i++ )
173  val[i] = spxLdexp(vec[i], scaleExp[i]);
174  }
175 
176  assert(isConsistent());
177  }
178 
179  return *this;
180  }
181 
182 
183  /// Assignment operator.
184  /** Assigning an SVectorBase to a VectorBase using operator=() will set all values to 0 except the nonzeros of \p vec.
185  * This is diffent in method assign().
186  */
187  template < class S >
189 
190  /// Assignment operator.
191  /** Assigning an SSVectorBase to a VectorBase using operator=() will set all values to 0 except the nonzeros of \p
192  * vec. This is diffent in method assign().
193  */
194  /**@todo do we need this also in non-template version, because SSVectorBase can be automatically cast to VectorBase? */
195  template < class S >
197 
198  /// Assign values of \p vec.
199  /** Assigns all nonzeros of \p vec to the vector. All other values remain unchanged. */
200  template < class S >
201  VectorBase<R>& assign(const SVectorBase<S>& vec);
202 
203  /// Assign values of \p vec.
204  /** Assigns all nonzeros of \p vec to the vector. All other values remain unchanged. */
205  template < class S >
206  VectorBase<R>& assign(const SSVectorBase<S>& vec);
207 
208  //@}
209 
210  // ------------------------------------------------------------------------------------------------------------------
211  /**@name Access */
212  //@{
213 
214  /// Dimension of vector.
215  int dim() const
216  {
217  return dimen;
218  }
219 
220  /// Return \p n 'th value by reference.
221  R& operator[](int n)
222  {
223  assert(n >= 0 && n < dimen);
224  return val[n];
225  }
226 
227  /// Return \p n 'th value.
228  const R& operator[](int n) const
229  {
230  assert(n >= 0 && n < dimen);
231  return val[n];
232  }
233 
234  /// Equality operator.
235  friend bool operator==(const VectorBase<R>& vec1, const VectorBase<R>& vec2)
236  {
237  if( &vec1 == &vec2 )
238  return true;
239  else if( vec1.dim() != vec2.dim() )
240  return false;
241  else
242  {
243  for( int i = 0; i < vec1.dim(); i++ )
244  {
245  if( vec1[i] != vec2[i] )
246  return false;
247  }
248  }
249 
250  return true;
251  }
252 
253  //@}
254 
255  // ------------------------------------------------------------------------------------------------------------------
256  /**@name Arithmetic operations */
257  //@{
258 
259  /// Set vector to 0.
260  void clear()
261  {
262  if( dimen > 0 )
263  {
264  for( int i = 0; i < dimen; i++ )
265  val[i] = 0;
266  }
267  }
268 
269  /// Addition.
270  template < class S >
272  {
273  assert(dim() == vec.dim());
274  assert(dim() == dimen);
275 
276  for( int i = 0; i < dimen; i++ )
277  val[i] += vec[i];
278 
279  return *this;
280  }
281 
282  /// Addition.
283  template < class S >
285 
286  /// Addition.
287  template < class S >
289 
290  /// Subtraction.
291  template < class S >
293  {
294  assert(dim() == vec.dim());
295  assert(dim() == dimen);
296 
297  for( int i = 0; i < dimen; i++ )
298  val[i] -= vec[i];
299 
300  return *this;
301  }
302 
303  /// Subtraction.
304  template < class S >
306 
307  /// Subtraction.
308  template < class S >
310 
311  /// Scaling.
312  template < class S >
314  {
315  assert(dim() == dimen);
316 
317  for( int i = 0; i < dimen; i++ )
318  val[i] *= x;
319 
320  return *this;
321  }
322 
323  /// Division.
324  template < class S >
326  {
327  assert(x != 0);
328 
329  for( int i = 0; i < dim(); i++ )
330  val[i] /= x;
331 
332  return *this;
333  }
334 
335  /// Inner product.
336  R operator*(const VectorBase<R>& vec) const
337  {
338  assert(vec.dim() == dimen);
339 
340  R x = 0;
341 
342  for( int i = 0; i < dimen; i++ )
343  x += val[i] * vec.val[i];
344 
345  return x;
346  }
347 
348  /// Inner product.
349  R operator*(const SVectorBase<R>& vec) const;
350 
351  /// Inner product.
352  R operator*(const SSVectorBase<R>& vec) const;
353 
354  /// Maximum absolute value, i.e., infinity norm.
355  R maxAbs() const
356  {
357  assert(dim() > 0);
358  assert(dim() == dimen);
359 
360  R maxi = 0;
361 
362  for( int i = 0; i < dimen; i++ )
363  {
364  R x = spxAbs(val[i]);
365 
366  if( x > maxi )
367  maxi = x;
368  }
369 
370  assert(maxi >= 0);
371 
372  return maxi;
373  }
374 
375  /// Minimum absolute value.
376  R minAbs() const
377  {
378  assert(dim() > 0);
379  assert(dim() == dimen);
380 
381  R mini = spxAbs(val[0]);
382 
383  for( int i = 1; i < dimen; i++ )
384  {
385  R x = spxAbs(val[i]);
386 
387  if( x < mini )
388  mini = x;
389  }
390 
391  assert(mini >= 0);
392 
393  return mini;
394  }
395 
396  /// Floating point approximation of euclidian norm (without any approximation guarantee).
397  Real length() const
398  {
399  return spxSqrt((Real)length2());
400  }
401 
402  /// Squared norm.
403  R length2() const
404  {
405  return (*this) * (*this);
406  }
407 
408  /// Addition of scaled vector.
409  template < class S, class T >
410  VectorBase<R>& multAdd(const S& x, const VectorBase<T>& vec)
411  {
412  assert(vec.dim() == dimen);
413 
414  for( int i = 0; i < dimen; i++ )
415  val[i] += x * vec.val[i];
416 
417  return *this;
418  }
419 
420  /// Addition of scaled vector.
421  template < class S, class T >
422  VectorBase<R>& multAdd(const S& x, const SVectorBase<T>& vec);
423 
424  /// Subtraction of scaled vector.
425  template < class S, class T >
426  VectorBase<R>& multSub(const S& x, const SVectorBase<T>& vec);
427 
428  /// Addition of scaled vector.
429  template < class S, class T >
430  VectorBase<R>& multAdd(const S& x, const SSVectorBase<T>& vec);
431 
432  //@}
433 
434  // ------------------------------------------------------------------------------------------------------------------
435  /**@name Utilities */
436  //@{
437 
438  /// Conversion to C-style pointer.
439  /** This function serves for using a VectorBase in an C-style function. It returns a pointer to the first value of
440  * the array.
441  *
442  * @todo check whether this non-const c-style access should indeed be public
443  */
444  R* get_ptr()
445  {
446  return val;
447  }
448 
449  /// Conversion to C-style pointer.
450  /** This function serves for using a VectorBase in an C-style function. It returns a pointer to the first value of
451  * the array.
452  */
453  const R* get_const_ptr() const
454  {
455  return val;
456  }
457 
458  /// Consistency check.
459  bool isConsistent() const
460  {
461 #ifdef ENABLE_CONSISTENCY_CHECKS
462  if( dim() > 0 && val == 0 )
463  return MSGinconsistent("VectorBase");
464 #endif
465 
466  return true;
467  }
468 
469  //@}
470 
471 private:
472 
473  // ------------------------------------------------------------------------------------------------------------------
474  /**@name Blocked */
475  //@{
476 
477  /// Blocked default constructor.
479  {
480  }
481 
482  //@}
483 };
484 
485 
486 
487 /// Assignment operator (specialization for Real).
488 template <>
489 inline
491 {
492  if( this != &vec )
493  {
494  assert(dim() == vec.dim());
495 
496  memcpy(val, vec.val, (unsigned int)dimen*sizeof(Real));
497 
498  assert(isConsistent());
499  }
500  return *this;
501 }
502 
503 
504 
505 /// Assignment operator (specialization for Real).
506 template <>
507 template <>
508 inline
510 {
511  if( (VectorBase<Rational>*)this != &vec )
512  {
513  assert(dim() == vec.dim());
514 
515  for( int i = 0; i < dimen; i++ )
516  val[i] = Real(vec[i]);
517 
518  assert(isConsistent());
519  }
520 
521  return *this;
522 }
523 
524 
525 
526 /// Set vector to 0 (specialization for Real).
527 template<>
528 inline
530 {
531  if( dimen > 0 )
532  memset(val, 0, (unsigned int)dimen * sizeof(Real));
533 }
534 
535 
536 
537 #ifndef SOPLEX_LEGACY
538 /// Inner product.
539 template<>
540 inline
542 {
543  assert(vec.dim() == dimen);
544 
545  if( dimen <= 0 || vec.dim() <= 0 )
546  return 0;
547 
548  Rational x = val[0];
549  x *= vec.val[0];
550 
551  for( int i = 1; i < dimen && i < vec.dim(); i++ )
552  x.addProduct(val[i], vec.val[i]);
553 
554  return x;
555 }
556 #endif
557 
558 } // namespace soplex
559 #endif // _VECTORBASE_H_
Rational spxAbs(const Rational &r)
Absolute.
Definition: rational.cpp:3909
Rational & addProduct(const Rational &r, const Rational &s)
add product of two rationals
Definition: rational.cpp:3356
VectorBase< Real > & scaleAssign(int scaleExp, const VectorBase< Real > &vec)
scale and assign
Definition: vectorbase.h:143
R minAbs() const
Minimum absolute value.
Definition: vectorbase.h:376
VectorBase< R > & operator=(const VectorBase< S > &vec)
Assignment operator.
Definition: vectorbase.h:111
R length2() const
Squared norm.
Definition: vectorbase.h:403
Dense vector.Class VectorBase provides dense linear algebra vectors. It does not provide memory manag...
Definition: dsvectorbase.h:28
Real length() const
Floating point approximation of euclidian norm (without any approximation guarantee).
Definition: vectorbase.h:397
Wrapper for GMP type mpq_class.We wrap mpq_class so that we can replace it by a double type if GMP is...
Definition: rational.h:45
VectorBase< R > & operator+=(const VectorBase< S > &vec)
Addition.
Definition: vectorbase.h:271
VectorBase< Real > & scaleAssign(const int *scaleExp, const VectorBase< Real > &vec, bool negateExp=false)
scale and assign
Definition: vectorbase.h:159
Semi sparse vector.This class implements semi-sparse vectors. Such are DVectorBases where the indices...
Definition: dsvectorbase.h:29
const R * get_const_ptr() const
Conversion to C-style pointer.
Definition: vectorbase.h:453
R * get_ptr()
Conversion to C-style pointer.
Definition: vectorbase.h:444
Real spxLdexp(Real x, int exp)
returns x * 2^exp
Definition: spxdefines.h:347
double Real
Definition: spxdefines.h:215
Real spxSqrt(Real a)
returns square root
Definition: spxdefines.h:329
VectorBase< R > & operator*=(const S &x)
Scaling.
Definition: vectorbase.h:313
R * val
Values of vector.
Definition: vectorbase.h:87
friend bool operator==(const VectorBase< R > &vec1, const VectorBase< R > &vec2)
Equality operator.
Definition: vectorbase.h:235
bool isConsistent() const
Consistency check.
Definition: vectorbase.h:459
VectorBase()
Blocked default constructor.
Definition: vectorbase.h:478
R & operator[](int n)
Return n &#39;th value by reference.
Definition: vectorbase.h:221
VectorBase< R > & multAdd(const S &x, const VectorBase< T > &vec)
Addition of scaled vector.
Definition: vectorbase.h:410
int dim() const
Dimension of vector.
Definition: vectorbase.h:215
Everything should be within this namespace.
VectorBase< R > & operator-=(const VectorBase< S > &vec)
Subtraction.
Definition: vectorbase.h:292
R operator*(const VectorBase< R > &vec) const
Inner product.
Definition: vectorbase.h:336
void clear()
Set vector to 0.
Definition: vectorbase.h:260
const R & operator[](int n) const
Return n &#39;th value.
Definition: vectorbase.h:228
int dimen
Dimension of vector.
Definition: vectorbase.h:83
VectorBase< R > & operator/=(const S &x)
Division.
Definition: vectorbase.h:325
Sparse vectors.Class SVectorBase provides packed sparse vectors. Such are a sparse vectors...
Definition: dvectorbase.h:31
#define MSGinconsistent(name)
Definition: spxdefines.h:123
VectorBase< R > & assign(const SVectorBase< S > &vec)
Assign values of vec.
Definition: basevectors.h:80
R maxAbs() const
Maximum absolute value, i.e., infinity norm.
Definition: vectorbase.h:355
VectorBase< R > & multSub(const S &x, const SVectorBase< T > &vec)
Subtraction of scaled vector.
Definition: basevectors.h:293
VectorBase< R > & operator=(const VectorBase< R > &vec)
Assignment operator.
Definition: vectorbase.h:127