Scippy

SoPlex

Sequential object-oriented simPlex

updatevector.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-2023 Zuse Institute Berlin (ZIB) */
7 /* */
8 /* Licensed under the Apache License, Version 2.0 (the "License"); */
9 /* you may not use this file except in compliance with the License. */
10 /* You may obtain a copy of the License at */
11 /* */
12 /* http://www.apache.org/licenses/LICENSE-2.0 */
13 /* */
14 /* Unless required by applicable law or agreed to in writing, software */
15 /* distributed under the License is distributed on an "AS IS" BASIS, */
16 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
17 /* See the License for the specific language governing permissions and */
18 /* limitations under the License. */
19 /* */
20 /* You should have received a copy of the Apache-2.0 license */
21 /* along with SoPlex; see the file LICENSE. If not email to soplex@zib.de. */
22 /* */
23 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24 
25 /**@file updatevector.h
26  * @brief Dense VectorBase<R> with semi-sparse VectorBase<R> for updates
27  */
28 
29 #ifndef _UPDATEVECTOR_H_
30 #define _UPDATEVECTOR_H_
31 
32 #include <assert.h>
33 
34 
35 #include "soplex/spxdefines.h"
36 #include "soplex/ssvector.h"
37 
38 namespace soplex
39 {
40 
41 /**@brief Dense Vector with semi-sparse Vector for updates
42  @ingroup Algebra
43 
44  In many algorithms vectors are updated in every iteration, by
45  adding a multiple of another VectorBase<R> to it, i.e., given a VectorBase<R> \c
46  x, a scalar \f$\alpha\f$ and another VectorBase<R> \f$\delta\f$, the
47  update to \c x constists of substituting it by \f$x \leftarrow x +
48  \alpha\cdot\delta\f$.
49 
50  While the update itself can easily be expressed with methods of
51  the class VectorBase<R>, it is often desirable to save the last update
52  VectorBase<R> \f$\delta\f$ and value \f$\alpha\f$. This is provided by
53  class UpdateVector<R>.
54 
55  UpdateVectors are derived from VectorBase<R> and provide additional
56  methods for saving and setting the multiplicator \f$\alpha\f$ and
57  the update Vector \f$\delta\f$. Further, it allows for efficient
58  sparse updates, by providing an IdxSet idx() containing the
59  nonzero indices of \f$\delta\f$.
60 */
61 template <class R>
62 class UpdateVector : public VectorBase<R>
63 {
64 private:
65 
66  //------------------------------------
67  /**@name Data */
68  ///@{
69  R theval; ///< update multiplicator
70  SSVectorBase<R> thedelta; ///< update vector
71  ///@}
72 
73 public:
74 
75  //------------------------------------
76  /**@name Constructors / destructors */
77  ///@{
78  /// default constructor.
79  explicit
80  UpdateVector<R>(int p_dim /*=0*/, std::shared_ptr<Tolerances> tols = nullptr)
81  : VectorBase<R> (p_dim)
82  , theval(0)
83  , thedelta(p_dim, tols)
84  {
85  assert(isConsistent());
86  }
87  ///
89  {}
90  /// copy constructor
92  /// assignment from VectorBase<R>
94  {
95  if(this != & rhs)
97 
98  assert(isConsistent());
99 
100  return *this;
101  }
102 
103  /// assignment
105  ///@}
106 
107  //------------------------------------
108  /**@name Access */
109  ///@{
110  /// update multiplicator \f$\alpha\f$, writeable
111  R& value()
112  {
113  return theval;
114  }
115  /// update multiplicator \f$\alpha\f$
116  R value() const
117  {
118  return theval;
119  }
120 
121  /// update VectorBase<R> \f$\delta\f$, writeable
123  {
124  return thedelta;
125  }
126  /// update VectorBase<R> \f$\delta\f$
127  const SSVectorBase<R>& delta() const
128  {
129  return thedelta;
130  }
131 
132  /// nonzero indices of update VectorBase<R> \f$\delta\f$
133  const IdxSet& idx() const
134  {
135  return thedelta.indices();
136  }
137  ///@}
138 
139  //------------------------------------
140  /**@name Modification */
141  ///@{
142  /// Perform the update
143  /** Add \c value() * \c delta() to the UpdateVector<R>. Only the indices
144  * in idx() are affected. For all other indices, delta() is asumed
145  * to be 0.
146  */
147  void update()
148  {
149  this->multAdd(theval, thedelta);
150  }
151 
152  /// clear VectorBase<R> and update vector
153  void clear()
154  {
156  clearUpdate();
157  }
158 
159  /// clear \f$\delta\f$, \f$\alpha\f$
160  void clearUpdate()
161  {
162  thedelta.clear();
163  theval = 0;
164  }
165 
166  /// reset dimension
167  void reDim(int newdim)
168  {
169  VectorBase<R>::reDim(newdim);
170  thedelta.reDim(newdim);
171  }
172 
173  /// set tolerances
174  void setTolerances(std::shared_ptr<Tolerances>& tolerances)
175  {
176  thedelta.setTolerances(tolerances);
177  }
178  ///@}
179 
180  //------------------------------------
181  /**@name Consistency check */
182  ///@{
183  ///
184  bool isConsistent() const;
185  ///@}
186 };
187 
188 
189 } // namespace soplex
190 
191 // General templated functions
192 #include "soplex/updatevector.hpp"
193 
194 #endif // _UPDATEVECTOR_H_
void clearUpdate()
clear ,
Definition: updatevector.h:160
VectorBase< R > & operator=(const VectorBase< S > &vec)
Assignment operator.
Definition: vectorbase.h:157
Dense vector.Class VectorBase provides dense linear algebra vectors. Internally, VectorBase wraps std...
Definition: dsvectorbase.h:37
void clear()
clear VectorBase<R> and update vector
Definition: updatevector.h:153
R value() const
update multiplicator
Definition: updatevector.h:116
void reDim(int newdim)
reset dimension
Definition: updatevector.h:167
Semi sparse vector.This class implements semi-sparse vectors. Such are VectorBases where the indices ...
Definition: dsvectorbase.h:38
SSVectorBase< R > & delta()
update VectorBase<R> , writeable
Definition: updatevector.h:122
R & value()
update multiplicator , writeable
Definition: updatevector.h:111
virtual void setTolerances(std::shared_ptr< Tolerances > newTolerances)
set the _tolerances member variable
Definition: ssvectorbase.h:114
void update()
Perform the update.
Definition: updatevector.h:147
const IdxSet & indices() const
Returns indices.
Definition: ssvectorbase.h:318
Semi sparse vector.
void clear()
Clears vector.
Definition: ssvectorbase.h:616
Dense Vector with semi-sparse Vector for updatesIn many algorithms vectors are updated in every itera...
Definition: updatevector.h:62
void setTolerances(std::shared_ptr< Tolerances > &tolerances)
set tolerances
Definition: updatevector.h:174
void reDim(int newdim)
Resets dimension to newdim.
Definition: ssvectorbase.h:582
Debugging, floating point type and parameter definitions.
VectorBase< R > & multAdd(const S &x, const VectorBase< T > &vec)
Addition of scaled vector.
Definition: vectorbase.h:458
Everything should be within this namespace.
const IdxSet & idx() const
nonzero indices of update VectorBase<R>
Definition: updatevector.h:133
const SSVectorBase< R > & delta() const
update VectorBase<R>
Definition: updatevector.h:127
UpdateVector(int p_dim, std::shared_ptr< Tolerances > tols=nullptr)
default constructor.
Definition: updatevector.h:80
void reDim(int newdim, const bool setZero=true)
Resets VectorBase&#39;s dimension to newdim.
Definition: vectorbase.h:541
void clear()
Set vector to contain all-zeros (keeping the same length)
Definition: vectorbase.h:308
bool isConsistent() const
R theval
update multiplicator
Definition: updatevector.h:69
SSVectorBase< R > thedelta
update vector
Definition: updatevector.h:70
UpdateVector< R > & operator=(const VectorBase< R > &rhs)
assignment from VectorBase<R>
Definition: updatevector.h:93
Set of indices.Class IdxSet provides a set of indices. At construction it must be given an array of i...
Definition: idxset.h:65