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   	   ///
88   	   ~UpdateVector()
89   	   {}
90   	   /// copy constructor
91   	   UpdateVector(const UpdateVector<R>&);
92   	   /// assignment from VectorBase<R>
93   	   UpdateVector<R>& operator=(const VectorBase<R>& rhs)
94   	   {
95   	      if(this != & rhs)
96   	         VectorBase<R>::operator=(rhs);
97   	
98   	      assert(isConsistent());
99   	
100  	      return *this;
101  	   }
102  	
103  	   /// assignment
104  	   UpdateVector<R>& operator=(const UpdateVector<R>& rhs);
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
122  	   SSVectorBase<R>& delta()
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  	   {
155  	      VectorBase<R>::clear();
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_
195