SoPlex Doxygen Documentation
vector.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-2012 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SoPlex is distributed under the terms of the ZIB Academic Licence. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SoPlex; see the file COPYING. If not email to soplex@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file vector.h
17  * @brief Dense vector for linear algebra.
18  */
19 #ifndef _VECTOR_H_
20 #define _VECTOR_H_
21 
22 #include <assert.h>
23 #include <string.h>
24 #include <math.h>
25 #include <iostream>
26 
27 #include "spxdefines.h"
28 
29 namespace soplex
30 {
31 class SVector;
32 class SSVector;
33 class Vector_exact;
34 
35 /**@brief Dense vector
36  @ingroup Algebra
37 
38  Class Vector provides dense linear algebra vectors. It does not
39  provide memory management for the %array of values. Instead, the
40  constructor requires a pointer to a memory block large enough to
41  fit the desired dimension of Real values.
42 
43  After construction, the values of a Vector can be accessed with
44  the subscript operator[]() . Safety is provided by
45  - checking of array bound when accessing elements with the
46  subscript operator[]() (only when compiled without \c -DNDEBUG).
47 
48  A Vector is distinguished from a simple array of #Real%s by
49  providing a set of mathematical operations. Since Vector does
50  not provide any memory management features, no operations are
51  available that would require allocation of temporary memory
52  space.
53 
54  The following mathematical operations are provided by class Vector
55  (Vector \p a, \p b; Real \p x):
56 
57  <TABLE>
58  <TR><TD>Operation</TD><TD>Description </TD><TD></TD>&nbsp;</TR>
59  <TR><TD>\c -= </TD><TD>subtraction </TD><TD>\c a \c -= \c b </TD></TR>
60  <TR><TD>\c += </TD><TD>addition </TD><TD>\c a \c += \c b </TD></TR>
61  <TR><TD>\c * </TD><TD>scalar product</TD>
62  <TD>\c x = \c a \c * \c b </TD></TR>
63  <TR><TD>\c *= </TD><TD>scaling </TD><TD>\c a \c *= \c x </TD></TR>
64  <TR><TD>maxAbs() </TD><TD>infinity norm </TD>
65  <TD>\c a.maxAbs() == \f$\|a\|_{\infty}\f$ </TD></TR>
66  <TR><TD>minAbs() </TD><TD> </TD>
67  <TD>\c a.minAbs() == \f$\min |a_i|\f$ </TD></TR>
68 
69  <TR><TD>length() </TD><TD>euclidian norm</TD>
70  <TD>\c a.length() == \f$\sqrt{a^2}\f$ </TD></TR>
71  <TR><TD>length2()</TD><TD>square norm </TD>
72  <TD>\c a.length2() == \f$a^2\f$ </TD></TR>
73  <TR><TD>multAdd(\c x,\c b)</TD><TD>add scaled vector</TD>
74  <TD> \c a += \c x * \c b </TD></TR>
75  </TABLE>
76 
77  When using any of these operations, the vectors involved must be of
78  the same dimension. Also an SVector \c b is allowed if it
79  does not contain nonzeros with index greater than the dimension of
80  \c a.
81 */
82 class Vector
83 {
84 protected:
85 
86  //------------------------------------
87  /**@name Data */
88  //@{
89  /// dimension of vector
90  int dimen;
91  /// values of a vector
92  /** The memory block pointed to by val must at least have size
93  * dimen * sizeof(Real).
94  */
96  //@}
97 
98 public:
99 
100  //------------------------------------
101  /**@name Construction and assignment */
102  //@{
103  /// construction
104  /** There is no default constructor since the storage for a
105  * Vector must be provided externally.
106  * Storage must be passed as a memory block val at construction. It
107  * must be large enough to fit at least dimen Real values.
108  */
109  Vector(int p_dimen, Real *p_val)
110  : dimen(p_dimen)
111  , val(p_val)
112  {
113  assert(dimen >= 0);
114  assert(isConsistent());
115  }
116  /// Assignment operator.
117  Vector& operator=(const Vector& vec);
118 
119  /// Assignment operator.
120  /** Assigning a SVector to a Vector using operator=()
121  * will set all values to 0 except the nonzeros of \p vec.
122  * This is diffent in method assign().
123  */
124  Vector& operator=(const SVector& vec);
125 
126  /// Assignment operator.
127  /** Assigning a SSVector to a Vector using operator=()
128  * will set all values to 0 except the nonzeros of \p vec.
129  * This is diffent in method assign().
130  */
131  Vector& operator=(const SSVector& vec);
132 
133  /// Assignment operator.
134  Vector& operator=(const Vector_exact& vec);
135 
136  /// Assign values of \p sv.
137  /** Assigns all nonzeros of \p sv to the vector.
138  * All other values remain unchanged.
139  */
140  Vector& assign(const SVector& sv);
141 
142  /// Assign values of \p sv.
143  /** Assigns all nonzeros of \p sv to the vector.
144  * All other values remain unchanged.
145  */
146  Vector& assign(const SSVector& sv);
147  //@}
148 
149  //------------------------------------
150  /**@name Access */
151  //@{
152  /// dimension of vector
153  inline int dim() const
154  {
155  return dimen;
156  }
157  /// return \p n 'th value by reference
158  Real& operator[](int n)
159  {
160  assert(n >= 0 && n < dimen);
161  return val[n];
162  }
163 
164  /// return \p n 'th value
165  Real operator[](int n) const
166  {
167  assert(n >= 0 && n < dimen);
168  return val[n];
169  }
170  //@}
171 
172  //------------------------------------
173  /**@name Algebraic methods */
174  //@{
175  /// vector addition
176  Vector& operator+=(const Vector& vec);
177  /// vector addition
178  Vector& operator+=(const SVector& vec);
179  /// vector addition
180  Vector& operator+=(const SSVector& vec);
181 
182  /// vector difference
183  Vector& operator-=(const Vector& vec);
184  /// vector difference
185  Vector& operator-=(const SVector& vec);
186  /// vector difference
187  Vector& operator-=(const SSVector& vec);
188 
189  /// scaling
190  Vector& operator*=(Real x);
191 
192  /// inner product.
193  Real operator*(const SSVector& v) const;
194  /// inner product.
195  Real operator*(const SVector& v) const;
196  /// inner product.
197  Real operator*(const Vector& v) const
198  {
199  assert(v.dim() == dimen);
200  Real x = 0;
201  for(int i = 0; i < dimen; i++)
202  x += val[i] * v.val[i];
203  return x;
204  }
205 
206  /// absolute biggest element (infinity norm).
207  Real maxAbs() const;
208  /// absolute smallest element.
209  Real minAbs() const;
210  /// euclidian norm.
211  Real length() const;
212  /// squared norm.
213  Real length2() const;
214 
215  /// addition of scaled vector
216  Vector& multAdd(Real x, const SVector& vec);
217  /// addition of scaled vector
218  Vector& multAdd(Real x, const SSVector& svec);
219  /// addition of scaled vector
220  Vector& multAdd(Real x, const Vector& vec)
221  {
222  assert(vec.dim() == dimen);
223 
224  for(int i = 0; i < dimen; i++)
225  val[i] += x * vec.val[i];
226 
227  return *this;
228  }
229  //@}
230 
231  //------------------------------------
232  /**@name Utilities */
233  //@{
234  /// Conversion to C-style pointer.
235  /** This function serves for using a Vector in an C-style
236  * function. It returns a pointer to the first value of the array.
237  *
238  * @todo check whether this non-const c-style acces should indeed be public
239  */
241  {
242  return val;
243  }
244  /// Conversion to C-style pointer.
245  /** This function serves for using a Vector in an C-style
246  * function. It returns a pointer to the first value of the array.
247  */
248  const Real* get_const_ptr() const
249  {
250  return val;
251  }
252  /// output operator.
253  friend std::ostream& operator<<(std::ostream& s, const Vector& vec);
254 
255  /// consistency check.
256  bool isConsistent() const;
257 
258  /// set vector to 0.
259  void clear()
260  {
261  if (dimen > 0)
262  memset(val, 0, dimen * sizeof(Real));
263  }
264  //@}
265 
266 private:
267 
268  //------------------------------------
269  /**@name Blocked */
270  //@{
271  /// we have no default constructor.
272  Vector();
273  //@}
274 };
275 } // namespace soplex
276 #endif // _VECTOR_H_
277 
278 //-----------------------------------------------------------------------------
279 //Emacs Local Variables:
280 //Emacs mode:c++
281 //Emacs c-basic-offset:3
282 //Emacs tab-width:8
283 //Emacs indent-tabs-mode:nil
284 //Emacs End:
285 //-----------------------------------------------------------------------------