Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
Teuchos_SerialDenseVector.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 
43 #ifndef _TEUCHOS_SERIALDENSEVECTOR_HPP_
44 #define _TEUCHOS_SERIALDENSEVECTOR_HPP_
45 
50 #include "Teuchos_ConfigDefs.hpp"
51 #include "Teuchos_Object.hpp"
53 
57 namespace Teuchos {
58 
59  template<typename OrdinalType, typename ScalarType>
60  class SerialDenseVector : public SerialDenseMatrix<OrdinalType,ScalarType> {
61 
62  public:
64 
65 
67 
70 
72 
79  SerialDenseVector(OrdinalType length, bool zeroOut = true);
80 
82 
87  SerialDenseVector(DataAccess CV, ScalarType* values, OrdinalType length);
88 
91 
93  virtual ~SerialDenseVector ();
95 
97 
98 
100 
107  int size(OrdinalType length_in)
109 
111  int sizeUninitialized(OrdinalType length_in)
113 
115 
121  int resize(OrdinalType length_in)
124 
126 
127 
129 
132  SerialDenseVector<OrdinalType, ScalarType>& operator= (const ScalarType value) { this->putScalar(value); return(*this); }
134 
136 
137 
140  bool operator == (const SerialDenseVector<OrdinalType, ScalarType> &Operand) const;
141 
143 
145  bool operator != (const SerialDenseVector<OrdinalType, ScalarType> &Operand) const;
147 
149 
150 
152 
160 
162 
163 
168  ScalarType& operator () (OrdinalType index);
169 
171 
175  const ScalarType& operator () (OrdinalType index) const;
176 
178 
182  ScalarType& operator [] (OrdinalType index);
183 
185 
189  const ScalarType& operator [] (OrdinalType index) const;
190 
192 
194 
195  ScalarType dot( const SerialDenseVector<OrdinalType,ScalarType> &x) const;
198 
200 
201  OrdinalType length() const {return(this->numRows_);}
204 
206 
207  virtual void print(std::ostream& os) const;
210 };
211 
212  template<typename OrdinalType, typename ScalarType>
214 
215  template<typename OrdinalType, typename ScalarType>
216  SerialDenseVector<OrdinalType, ScalarType>::SerialDenseVector( OrdinalType length_in, bool zeroOut ) : SerialDenseMatrix<OrdinalType,ScalarType>( length_in, 1, zeroOut ) {}
217 
218  template<typename OrdinalType, typename ScalarType>
219  SerialDenseVector<OrdinalType, ScalarType>::SerialDenseVector(DataAccess CV, ScalarType* values_in, OrdinalType length_in) :
220  SerialDenseMatrix<OrdinalType,ScalarType>( CV, values_in, length_in, length_in, 1 ) {}
221 
222  template<typename OrdinalType, typename ScalarType>
224  SerialDenseMatrix<OrdinalType,ScalarType>( Source ) {}
225 
226  template<typename OrdinalType, typename ScalarType>
228 
229  template<typename OrdinalType, typename ScalarType>
231  {
233  return(*this);
234  }
235 
236  template<typename OrdinalType, typename ScalarType>
238  {
239  bool result = 1;
240  if(this->numRows_ != Operand.numRows_)
241  {
242  result = 0;
243  }
244  else
245  {
246  OrdinalType i;
247  for(i = 0; i < this->numRows_; i++) {
248  if((*this)(i) != Operand(i))
249  {
250  return 0;
251  }
252  }
253  }
254  return result;
255  }
256 
257  template<typename OrdinalType, typename ScalarType>
259  {
260  return !((*this)==Operand);
261  }
262 
263  template<typename OrdinalType, typename ScalarType>
265  {
266  TEUCHOS_TEST_FOR_EXCEPTION(this->numRows_!= x.numRows_, std::invalid_argument,
267  "SerialDenseVector<T>::dot : " <<
268  "Number of rows " << this->numRows_ << " not equal to x.numRows_ "<< x.numRows() );
269 
270  // Compute the dot product and return the result.
271  return BLAS<OrdinalType, ScalarType>::DOT(this->numRows_, this->values(), 1, x.values(), 1);
272  }
273 
274  template<typename OrdinalType, typename ScalarType>
276  {
277  os << std::endl;
278  if(this->valuesCopied_)
279  os << "Values_copied : yes" << std::endl;
280  else
281  os << "Values_copied : no" << std::endl;
282  os << "Length : " << this->numRows_ << std::endl;
283  if(this->numRows_ == 0) {
284  os << "(std::vector is empty, no values to display)" << std::endl;
285  } else {
286  for(OrdinalType i = 0; i < this->numRows_; i++) {
287  os << (*this)(i) << " ";
288  }
289  os << std::endl;
290  }
291  }
292 
293  //----------------------------------------------------------------------------------------------------
294  // Accessor methods
295  //----------------------------------------------------------------------------------------------------
296 
297  template<typename OrdinalType, typename ScalarType>
298  inline ScalarType& SerialDenseVector<OrdinalType, ScalarType>::operator () (OrdinalType index)
299  {
300 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
301  this->checkIndex( index );
302 #endif
303  return(this->values_[index]);
304  }
305 
306  template<typename OrdinalType, typename ScalarType>
307  inline const ScalarType& SerialDenseVector<OrdinalType, ScalarType>::operator () (OrdinalType index) const
308  {
309 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
310  this->checkIndex( index );
311 #endif
312  return(this->values_[index]);
313  }
314 
315  template<typename OrdinalType, typename ScalarType>
316  inline const ScalarType& SerialDenseVector<OrdinalType, ScalarType>::operator [] (OrdinalType index) const
317  {
318 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
319  this->checkIndex( index );
320 #endif
321  return(this->values_[index]);
322  }
323 
324  template<typename OrdinalType, typename ScalarType>
325  inline ScalarType& SerialDenseVector<OrdinalType, ScalarType>::operator [] (OrdinalType index)
326  {
327 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
328  this->checkIndex( index );
329 #endif
330  return(this->values_[index]);
331  }
332 
333 } // namespace Teuchos
334 
335 #endif /* _TEUCHOS_SERIALDENSEVECTOR_HPP_ */
ScalarType * values() const
Data array access method.
virtual void print(std::ostream &os) const
Print method. Define the behavior of the std::ostream << operator inherited from the Object class...
ScalarType & operator[](OrdinalType index)
Element access method (non-const).
void checkIndex(OrdinalType rowIndex, OrdinalType colIndex=0) const
Templated serial dense matrix class.
ScalarType dot(const SerialDenseVector< OrdinalType, ScalarType > &x) const
Compute the dot product of this vector and x.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
int resize(OrdinalType length_in)
Resizing method for changing the size of a SerialDenseVector, keeping the entries.
This class creates and provides basic support for dense vectors of templated type as a specialization...
SerialDenseVector()
Default Constructor.
SerialDenseMatrix< OrdinalType, ScalarType > & operator=(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
bool operator==(const SerialDenseVector< OrdinalType, ScalarType > &Operand) const
Equality of two matrices.
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero())
Set all values in the matrix to a constant value.
OrdinalType length() const
Returns the length of this vector.
SerialDenseVector< OrdinalType, ScalarType > & operator=(const ScalarType value)
Set all values in the matrix to a constant value.
The base Teuchos object.
int size(OrdinalType length_in)
Size method for changing the size of a SerialDenseVector, initializing entries to zero...
ScalarType & operator()(OrdinalType index)
Element access method (non-const).
bool operator!=(const SerialDenseVector< OrdinalType, ScalarType > &Operand) const
Inequality of two matrices.
ScalarType DOT(const OrdinalType n, const x_type *x, const OrdinalType incx, const y_type *y, const OrdinalType incy) const
Form the dot product of the vectors x and y.
OrdinalType numRows() const
Returns the row dimension of this matrix.
int sizeUninitialized(OrdinalType length_in)
Same as size() except leaves values uninitialized.
This class creates and provides basic support for dense rectangular matrix of templated type...