1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008-2017 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA  02110-1301  USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #ifndef UQ_TEUCHOS_VECTOR_H
26 #define UQ_TEUCHOS_VECTOR_H
27 
28 #ifdef QUESO_HAS_TRILINOS
29 #include <Teuchos_SerialDenseVector.hpp>
30 #include <Teuchos_SerialDenseMatrix.hpp>
31 #include <Teuchos_LAPACK.hpp>
32 #endif
33 
34 #include <queso/Defines.h>
35 #include <queso/Vector.h>
36 #include <vector>
37 
38 #ifdef QUESO_HAS_TRILINOS
39 
40 namespace QUESO {
41 
42 /*! \file TeuchosVector.h
43     \brief Vector class using Trilinos Teuchos
44 */
45 
46 /*! \class TeuchosVector
47     \brief Class for vector operations using Teuchos (Trilinos).
48 
49     This class creates and provides basic support for vectors of templated
50     type as a specialization of Vector using Teuchos vectors (from Trilinos), which are defined
51     by an encapsulated Teuchos::SerialDenseVector structure.
52 */
53 
54 
55 class TeuchosVector : public Vector
56 {
57 public:
58 
59   //! @name Constructor/Destructor methods.
60   //@{
61 
62   //! Shaped constructor: creates an empty vector of size given by Map& map.
63   TeuchosVector(const BaseEnvironment& env, const Map& map);
64 
65  //! Shaped constructor: creates an vector of values \c value and of size given by Map& map.
66   TeuchosVector(const BaseEnvironment& env, const Map& map, double value);
67 
68  //! Shaped constructor: creates an vector of size given by Map& map and of values given by an average involving \c d1, \c d2 and the vector size.
69   TeuchosVector(const BaseEnvironment& env, double d1, double d2, const Map& map);
70 
71   //! Shaped constructor: creates an vector of size given by vector \c v and of values given by an average involving \c d1, \c d2 and \c \this vector size.
72   TeuchosVector(const TeuchosVector&         v, double d1, double d2);
73 
74   //! Copy constructor.
75   TeuchosVector(const TeuchosVector&         y);
76 
77   //! Destructor
78   ~TeuchosVector();
79    //@}
80 
81 
82   //! @name Set methods.
83   //@{
84   //! Set all values in the vector to a constant value.
85   TeuchosVector& operator= (double a);
86 
87   //! Copies values from one vector to another.
88   TeuchosVector& operator= (const TeuchosVector& rhs);
89 
90   //! Stores in \c this vector the coordinate-wise multiplication of \c this and a.
91   TeuchosVector& operator*=(double a);
92 
93   //! Stores in \c this vector the coordinate-wise division of \c this by a.
94   TeuchosVector& operator/=(double a);
95 
96   //! Stores in \c this vector the coordinate-wise multiplication of \c this with rhs.
97   TeuchosVector& operator*=(const TeuchosVector& rhs);
98 
99   //! Stores in \c this vector the coordinate-wise division of \c this by rhs.
100   TeuchosVector& operator/=(const TeuchosVector& rhs);
101 
102    //! Stores in \c this vector the coordinate-wise addition of \c this and rhs.
103   TeuchosVector& operator+=(const TeuchosVector& rhs);
104 
105    //! Stores in \c this vector the coordinate-wise subtraction of \c this and \c rhs.
106   TeuchosVector& operator-=(const TeuchosVector& rhs);
107   //@}
108 
109     //! @name Accessor methods.
110   //@{
111   //! Element access method (non-const).
112   double& operator[](unsigned int i);
113 
114   //! Element access method (const).
115   const double& operator[](unsigned int i) const;
116   //@}
117 
118   //! @name Attribute methods.
119   //@{
120   //! Returns the length of this vector
121   unsigned int sizeLocal        () const;
122 
123   //! Returns the global length of this vector.
124   unsigned int sizeGlobal       () const;
125 
126   //TODO
127   double*      values           () ;
128 
129   //! Returns the maximum value in the \c this vector.
130   double       getMaxValue      () const;
131 
132   //! Returns the minimum value in the \c this vector.
133   double       getMinValue      () const;
134 
135   //! This function returns the index of the maximum value in the vector \c this.
136   int          getMaxValueIndex () const;
137 
138   //! This function returns the index of the minimum value in the vector \c this.
139   int          getMinValueIndex () const;
140 
141   //! This function returns maximum value in the vector \c this and its the index.
142   void         getMaxValueAndIndex( double& value, int& index );
143 
144     //! This function returns minimum value in the vector \c this and its the index.
145   void         getMinValueAndIndex( double& value, int& index );
146   //@}
147 
148   //! @name Norm methods
149   //@{
150   //! Returns the norm of the vector, as the square root of 2-norm of this vector.
151   double       norm2Sq          () const;
152 
153   //! Returns the 2-norm (Euclidean norm) of the vector.
154   double       norm2            () const;
155 
156   //! Returns the 1-norm of the vector.
157   double       norm1            () const;
158 
159     //! Returns the infinity-norm (maximum norm) of the vector.
160   double       normInf          () const;
161 
162 
163   //@}
164 
165 
166   //! @name Comparison methods.
167   //@{
168   //! This function returns true if at least one component of \c this is smaller than the respective component of rhs.
169   bool         atLeastOneComponentSmallerThan       (const TeuchosVector& rhs) const;
170 
171   //! This function returns true if at least one component of \c this is bigger than the respective component of rhs.
172   bool         atLeastOneComponentBiggerThan        (const TeuchosVector& rhs) const;
173 
174   //! This function returns true if at least one component of \c this is smaller than or equal to the respective component of rhs.
175   bool         atLeastOneComponentSmallerOrEqualThan(const TeuchosVector& rhs) const;
176 
177   //! This function returns true if at least one component of \c this is bigger than or equal to the respective component of rhs.
178   bool         atLeastOneComponentBiggerOrEqualThan (const TeuchosVector& rhs) const;
179   //@}
180 
181     //! @name Set methods.
182   //@{
183   //! Component-wise sets all values to \c this with value.
184   void	       cwSet(double value);
185 
186   //! Component-wise sets all values of \c this with vector \c vec, starting at position \c initialPos.
187   void         cwSet(unsigned int initialPos, const TeuchosVector& vec);
188 
189   //! Component-wise extracts all values of \c this with vector \c vec, starting at position \c initialPos.
190   void         cwExtract(unsigned int initialPos, TeuchosVector& vec) const;
191 
192   //! This function inverts component-wise the element values of \c this.
193   void         cwInvert();
194 
195   //! Component-wise sets the square-root of \c this.
196   void         cwSqrt();
197 
198   //! This function concatenates vectors \c v1 and \c v2 into \c this vector.
199   void         cwSetConcatenated(const TeuchosVector& v1, const TeuchosVector& v2);
200 
201   //! This function sets component-wise Gaussian random variates, with mean \c mean and standard deviation \c stdDev.
202   void	       cwSetGaussian(double mean, double stdDev);
203 
204   //! This function sets component-wise Gaussian random variates, with vectors for mean and standard deviation.
205   void	       cwSetGaussian(const TeuchosVector& meanVec,       const TeuchosVector& stdDevVec);
206 
207   //! This function sets component-wise a number uniformly distributed in the range of elements of [lowerBoundVec,upperBoundVec].
208   void         cwSetUniform (const TeuchosVector& lowerBoundVec, const TeuchosVector& upperBoundVec);
209 
210   //! This function sets component-wise random variates from the Beta distribution, with vector parameters alpha and beta.
211   void         cwSetBeta        (const TeuchosVector& alpha,   const TeuchosVector& beta     );
212 
213   //! This function sets component-wise random variates from the Inverse Gamma distribution, with parameters given by vectors  \c a and \c b.
214   void         cwSetGamma       (const TeuchosVector& a,       const TeuchosVector& b        );
215 
216   //! This function sets component-wise random variates from the Inverse Gamma distribution, with parameters given by vectors  \c a and \c b.
217   void         cwSetInverseGamma(const TeuchosVector& a,   const TeuchosVector& b     );
218   //@}
219 
220    //! @name Miscellaneous methods.
221   //@{
222 
223    //! This function returns absolute value of all elements in \c this.
224   TeuchosVector abs() const;
225 
226   //! Copies the values of \c this vector (a TeuchosVector) to a std::vector structure.
227   /*! With this method, the std::vector copy of the TeuchosVector may be sorted using std::sort.*/
228   void		copy_to_std_vector(std::vector<double>& vec); // output
229 
230   //! Copies the values of std::vector structure to \c this vector (a TeuchosVector).
231   /*! With this method, after a std::vector is sorted, it may be copied to a TeuchosVector.*/
232   void		copy_from_std_vector(const std::vector<double> vec);
233 
234   //! This function sorts the elements of the vector \c this in ascending numerical order.
235   /*! It copies \c this vector to a std::vector and uses the std::sort functionality to sort it. */
236   void         sort             ();
237 
238   //! Returns the sum of the components of the vector.
239   double       sumOfComponents  () const;
240 
241   //! Broadcasts a message from the process with \c srcRank root to all other processes of the group.
242   void         mpiBcast         (int srcRank, const MpiComm& bcastComm);
243 
244   //! Combines values from all processes and distributes the result back to all processes.
245   void         mpiAllReduce     (RawType_MPI_Op mpiOperation, const MpiComm& opComm, TeuchosVector& resultVec) const;
246 
247   //!  Gathers values from a group of processes and returns all quantiles.
248   void         mpiAllQuantile   (double probability, const MpiComm& opComm, TeuchosVector& resultVec) const;
249 
250   //!  Reproduces MATLAB linear inter/extra-polation.
251   /*! yiVec.matlabLinearInterpExtrap(xVec,yVec,xiVec) interpolates/extrapolates to find yiVec, the values of the
252    * underlying function yVec at the points in the vector xiVec. Thus, yVec and xVec must have the same size;
253    * and yiVec and xiVec must have the same size.*/
254   void         matlabLinearInterpExtrap(const TeuchosVector& xVec, const TeuchosVector& yVec, const TeuchosVector& xiVec);
255 
256   //TODO
257   void         matlabDiff       (unsigned int firstPositionToStoreDiff, double valueForRemainderPosition, TeuchosVector& outputVec) const;
258 
259   //@}
260 
261   //! @name I/O methods.
262   //@{
263   //! Print method.  Defines the behavior of the std::ostream << operator inherited from the Object class.
264   void         print            (std::ostream& os) const;
265   void         subReadContents  (const std::string&            fileName,
266 								 const std::string&            fileType,
267 								 const std::set<unsigned int>& allowedSubEnvIds);
268   void         subWriteContents (const std::string&            varNamePrefix,
269                                  const std::string&            fileName,
270                                  const std::string&            fileType,
271                                  const std::set<unsigned int>& allowedSubEnvIds) const;
272   //@}
273 
274 
275 
276 
277 
278 private:
279   //! Default Constructor
280   /*! Creates an empty vector of no length.*/
281   TeuchosVector();
282 
283   //! Teuchos vector.
284   Teuchos::SerialDenseVector<int,double> m_vec;
285 
286   //! This function copies the elements of the vector \c src into \c this.
287   void         copy             (const TeuchosVector& src);
288 
289 };
290 
291 //TeuchosVector copy		  (int , 						    double []);
292 TeuchosVector operator/    (double a,            		    const TeuchosVector& x  );
293 TeuchosVector operator/    (const TeuchosVector& x,   const TeuchosVector& y  );
294 TeuchosVector operator*    (double a,		    	        const TeuchosVector& x  );
295 TeuchosVector operator*    (const TeuchosVector& x,   const TeuchosVector& y  );
296 double               scalarProduct(const TeuchosVector& x,   const TeuchosVector& y  );
297 TeuchosVector operator+    (const TeuchosVector& x,   const TeuchosVector& y  );
298 TeuchosVector operator-    (const TeuchosVector& x,   const TeuchosVector& y  );
299 bool                 operator==   (const TeuchosVector& lhs, const TeuchosVector& rhs);
300 std::ostream&        operator<<   (std::ostream& os,                const TeuchosVector& obj);
301 
302 }  // End namespace QUESO
303 
304 #endif // ifdef QUESO_HAS_TRILINOS
305 
306 #endif //UQ_TEUCHOS_VECTOR_H
307