Nektar++
NekVector.hpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: NekVector.hpp
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: Generic N-Dimensional Vector.
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
35 #ifndef NEKTAR_LIB_UTILITIES_NEK_VECTOR_HPP
36 #define NEKTAR_LIB_UTILITIES_NEK_VECTOR_HPP
37 
40 
42 
45 
46 #include <functional>
47 #include <algorithm>
48 #include <math.h>
49 
50 #include <type_traits>
51 #include <boost/call_traits.hpp>
52 
53 namespace Nektar
54 {
55  template<typename DataType>
56  class NekVector
57  {
58  public:
59  /// \brief Creates an empty vector.
61 
62  /// \brief Creates a vector of given size. The elements are not initialized.
63  LIB_UTILITIES_EXPORT explicit NekVector(unsigned int size);
64 
65  /// \brief Creates a vector with given size and initial value.
66  LIB_UTILITIES_EXPORT NekVector(unsigned int size, typename boost::call_traits<DataType>::const_reference a);
67 
68 
69  LIB_UTILITIES_EXPORT explicit NekVector(const std::string& vectorValues);
70 
71  LIB_UTILITIES_EXPORT NekVector(typename boost::call_traits<DataType>::const_reference x,
72  typename boost::call_traits<DataType>::const_reference y,
73  typename boost::call_traits<DataType>::const_reference z);
74 
76 
77  LIB_UTILITIES_EXPORT NekVector(unsigned int size, const DataType* const ptr);
80 
82 
84 
86 
87 
88  /// \brief Returns the number of dimensions for the point.
89  LIB_UTILITIES_EXPORT unsigned int GetDimension() const;
90 
91  LIB_UTILITIES_EXPORT unsigned int GetRows() const;
92 
93  LIB_UTILITIES_EXPORT DataType* GetRawPtr();
94 
96 
97  LIB_UTILITIES_EXPORT const DataType* GetRawPtr() const;
98 
100 
101  typedef DataType* iterator;
102  LIB_UTILITIES_EXPORT iterator begin();
103  LIB_UTILITIES_EXPORT iterator end();
104 
105  typedef const DataType* const_iterator;
106  LIB_UTILITIES_EXPORT const_iterator begin() const;
107  LIB_UTILITIES_EXPORT const_iterator end() const;
108 
109  /// \brief Returns i^{th} element.
110  /// \param i The element to return.
111  /// \pre i < dim
112  /// \return A reference to the i^{th} element.
113  ///
114  /// Retrieves the i^{th} element. Since it returns a reference you may
115  /// assign a new value (i.e., p(2) = 3.2;)
116  ///
117  /// This operator performs range checking.
118  LIB_UTILITIES_EXPORT typename boost::call_traits<DataType>::reference operator()(unsigned int i);
119 
120  LIB_UTILITIES_EXPORT typename boost::call_traits<DataType>::reference operator[](unsigned int i);
121 
122  LIB_UTILITIES_EXPORT typename boost::call_traits<DataType>::reference x();
123 
124  LIB_UTILITIES_EXPORT typename boost::call_traits<DataType>::reference y();
125 
126  LIB_UTILITIES_EXPORT typename boost::call_traits<DataType>::reference z();
127 
128  LIB_UTILITIES_EXPORT void SetX(typename boost::call_traits<DataType>::const_reference val);
129 
130  LIB_UTILITIES_EXPORT void SetY(typename boost::call_traits<DataType>::const_reference val);
131 
132  LIB_UTILITIES_EXPORT void SetZ(typename boost::call_traits<DataType>::const_reference val);
133 
135 
137 
138  LIB_UTILITIES_EXPORT NekVector<DataType>& operator*=(typename boost::call_traits<DataType>::const_reference rhs);
139 
140  LIB_UTILITIES_EXPORT NekVector<DataType>& operator/=(typename boost::call_traits<DataType>::const_reference rhs);
141 
143 
144  LIB_UTILITIES_EXPORT typename boost::call_traits<DataType>::const_reference operator()(unsigned int i) const;
145 
146  LIB_UTILITIES_EXPORT typename boost::call_traits<DataType>::const_reference operator[](unsigned int i) const;
147 
148  LIB_UTILITIES_EXPORT typename boost::call_traits<DataType>::const_reference x() const;
149 
150  LIB_UTILITIES_EXPORT typename boost::call_traits<DataType>::const_reference y() const;
151 
152  LIB_UTILITIES_EXPORT typename boost::call_traits<DataType>::const_reference z() const;
153 
155 
156  LIB_UTILITIES_EXPORT DataType Magnitude() const;
157  LIB_UTILITIES_EXPORT DataType Dot(const NekVector<DataType>& rhs) const;
158 
160 
161  LIB_UTILITIES_EXPORT std::string AsString() const;
162 
163  // Norms
164  LIB_UTILITIES_EXPORT DataType L1Norm() const;
165  LIB_UTILITIES_EXPORT DataType L2Norm() const;
166  LIB_UTILITIES_EXPORT DataType InfinityNorm() const;
167 
168 
170 
171  protected:
172 
174  LIB_UTILITIES_EXPORT void SetSize(unsigned int s);
177  LIB_UTILITIES_EXPORT void Resize(unsigned int newSize);
178 
179  private:
180  // Prevents accidental use of wrapped mode around ConstArrays.
182 
183  unsigned int m_size;
186  };
187 
188  template<typename DataType>
190  const NekVector<DataType>& lhs,
191  const NekVector<DataType>& rhs);
192 
193  template<typename DataType>
195  const NekVector<DataType>& lhs,
196  const NekVector<DataType>& rhs);
197 
198  template<typename DataType>
200  const NekVector<DataType>& rhs);
201 
202  template<typename DataType>
204  const NekVector<DataType>& rhs);
205 
206  template<typename LhsDataType,
207  typename RhsDataType>
209  const NekVector<RhsDataType>& rhs);
210 
211 
212 
213  template<typename ResultDataType, typename InputDataType>
217 
218  template<typename ResultDataType, typename InputDataType>
222 
223  template<typename ResultDataType, typename InputDataType>
226 
227  template<typename ResultDataType, typename InputDataType>
230 
231  template<typename DataType>
234  const NekVector<DataType>& rhs);
235 
236 
237 
238 
239 
240  template<typename ResultDataType, typename InputDataType>
243  const NekDouble& rhs);
244 
245  template<typename ResultDataType>
247  const NekDouble& rhs);
248 
249  template<typename DataType>
252  const NekDouble& rhs);
253 
254 
255  template<typename ResultDataType, typename InputDataType>
259 
260  template<typename ResultDataType, typename InputDataType>
263 
264  template<typename DataType, typename InputDataType>
268 
269  template<typename ResultDataType, typename InputDataType>
272  const NekDouble& rhs);
273 
274  template<typename ResultDataType>
276  const NekDouble& rhs);
277 
278  template<typename DataType>
281  const NekDouble& rhs);
282 
283  template<typename ResultDataType, typename InputDataType>
285  const NekDouble& lhs,
287 
288  template<typename ResultDataType, typename InputDataType>
290  const NekDouble& lhs,
292 
293  template<typename DataType>
295  LIB_UTILITIES_EXPORT Multiply(const DataType& lhs,
296  const NekVector<DataType>& rhs);
297 
298  template<typename DataType>
300  const NekVector<DataType> &lhs, const NekDouble &rhs)
301  {
302  return Multiply(lhs, rhs);
303  }
304 
305  template<typename DataType>
307  const NekDouble &lhs, const NekVector<DataType>& rhs)
308  {
309  return Multiply(lhs, rhs);
310  }
311 
312  template<typename DataType>
315  {
316  return Multiply(lhs, rhs);
317  }
318 
319  template<typename DataType>
321  const NekVector<DataType> &lhs, const NekDouble &rhs)
322  {
323  return Divide(lhs, rhs);
324  }
325 
326  template<typename DataType>
329  {
330  return Add(lhs, rhs);
331  }
332 
333  template<typename DataType>
336  {
337  return Subtract(lhs, rhs);
338  }
339 
340  template<typename DataType>
341  LIB_UTILITIES_EXPORT std::ostream& operator<<(std::ostream& os, const NekVector<DataType>& rhs);
342 
343  template<typename DataType>
345 
346  template<typename DataType>
348 
349  template<typename DataType>
351 
352  template<typename DataType>
354 
355  template<typename DataType>
357 
358  template<typename DataType>
359  LIB_UTILITIES_EXPORT DataType Dot(const NekVector<DataType>& lhs,
360  const NekVector<DataType>& rhs);
361 
362  template<typename DataType>
363  LIB_UTILITIES_EXPORT std::vector<DataType> FromString(const std::string& str);
364 
365  /// \todo Do the Norms with Blas where applicable.
366  template<typename DataType>
368 
369  template<typename DataType>
371 
372  template<typename DataType>
374 
375  template<typename DataType>
377 
378  template<typename DataType>
380 
383 
384  template<typename DataType>
386 
387  template<typename DataType>
389  const NekVector<DataType>& rhs);
390  template<typename DataType>
391  LIB_UTILITIES_EXPORT std::string AsString(const NekVector<DataType>& v);
392 
393 }
394 
395 #endif // NEKTAR_LIB_UTILITIES_NEK_VECTOR_HPP
396 
NekVector()
Creates an empty vector.
Definition: NekVector.cpp:41
void SetData(const Array< OneD, DataType > &newData)
Definition: NekVector.cpp:413
void SubtractNegatedLhs(NekMatrix< DataType, StandardMatrixTag > &result, const NekMatrix< LhsDataType, LhsMatrixType > &lhs, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
boost::call_traits< DataType >::reference operator[](unsigned int i)
Definition: NekVector.cpp:259
Array< OneD, DataType > m_data
Definition: NekVector.hpp:184
iterator begin()
Definition: NekVector.cpp:239
boost::call_traits< DataType >::reference x()
Definition: NekVector.cpp:265
void SetZ(typename boost::call_traits< DataType >::const_reference val)
Definition: NekVector.cpp:300
unsigned int m_size
Definition: NekVector.hpp:183
void SetX(typename boost::call_traits< DataType >::const_reference val)
Definition: NekVector.cpp:286
NekVector< DataType > & operator/=(typename boost::call_traits< DataType >::const_reference rhs)
Definition: NekVector.cpp:328
DataType L1Norm() const
Definition: NekVector.cpp:392
void MultiplyEqual(NekMatrix< LhsDataType, StandardMatrixTag > &lhs, typename boost::call_traits< LhsDataType >::const_reference rhs)
void SetWrapperType(PointerWrapper p)
Definition: NekVector.cpp:410
NekVector< DataType > createVectorFromPoints(const NekPoint< DataType > &source, const NekPoint< DataType > &dest)
Definition: NekVector.cpp:809
void SetY(typename boost::call_traits< DataType >::const_reference val)
Definition: NekVector.cpp:293
void Resize(unsigned int newSize)
Definition: NekVector.cpp:416
DataType * iterator
Definition: NekVector.hpp:101
void MultiplyInvertedLhs(NekVector< ResultDataType > &result, const NekDouble &lhs, const NekVector< InputDataType > &rhs)
Definition: NekVector.cpp:761
bool operator==(const Array< OneD, NekDouble > &lhs, const Array< OneD, NekDouble > &rhs)
const DataType * const_iterator
Definition: NekVector.hpp:105
DataType Dot(const NekVector< DataType > &rhs) const
Definition: NekVector.cpp:379
StandardMatrixTag & lhs
RhsMatrixType void AddEqual(NekMatrix< DataType, StandardMatrixTag > &result, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
void DivideEqual(NekVector< ResultDataType > &result, const NekDouble &rhs)
Definition: NekVector.cpp:621
void Multiply(NekMatrix< ResultDataType, StandardMatrixTag > &result, const NekMatrix< LhsDataType, LhsMatrixType > &lhs, const ResultDataType &rhs)
NekVector< DataType > & operator-=(const NekVector< DataType > &rhs)
Definition: NekVector.cpp:314
DataType * GetRawPtr()
Definition: NekVector.cpp:221
void AddEqualNegatedLhs(NekMatrix< DataType, StandardMatrixTag > &result, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
NekPoint< DataType > findPointAlongVector(const NekVector< DataType > &lhs, const DataType &t)
Definition: NekVector.cpp:826
void Divide(NekVector< ResultDataType > &result, const NekVector< InputDataType > &lhs, const NekDouble &rhs)
Definition: NekVector.cpp:601
NekVector< DataType > & operator*=(typename boost::call_traits< DataType >::const_reference rhs)
Definition: NekVector.cpp:321
DNekMat void SubtractEqual(NekMatrix< DataType, StandardMatrixTag > &result, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
NekPoint< DataType > operator/(const NekPoint< DataType > &lhs, typename boost::call_traits< DataType >::param_type rhs)
Definition: NekPoint.hpp:446
std::string AsString() const
Definition: NekVector.cpp:388
NekVector< DataType > operator*(const NekMatrix< LhsDataType, MatrixType > &lhs, const NekVector< DataType > &rhs)
NekVector< DataType > & operator+=(const NekVector< DataType > &rhs)
Definition: NekVector.cpp:307
void SubtractEqualNegatedLhs(NekMatrix< DataType, StandardMatrixTag > &result, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
bool operator!=(const Array< OneD, T1 > &lhs, const Array< OneD, T2 > &rhs)
boost::call_traits< DataType >::reference operator()(unsigned int i)
Returns i^{th} element.
Definition: NekVector.cpp:251
#define LIB_UTILITIES_EXPORT
NekVector< DataType > operator-() const
Definition: NekVector.cpp:373
PointerWrapper GetWrapperType() const
Definition: NekVector.cpp:401
std::vector< DataType > FromString(const std::string &str)
Definition: NekVector.cpp:870
double NekDouble
DataType Magnitude() const
Definition: NekVector.cpp:376
PointerWrapper
Specifies if the pointer passed to a NekMatrix or NekVector is copied into an internal representation...
Array< OneD, DataType > & GetData()
Definition: NekVector.cpp:404
void AddNegatedLhs(NekMatrix< DataType, StandardMatrixTag > &result, const NekMatrix< LhsDataType, LhsMatrixType > &lhs, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
boost::call_traits< DataType >::reference z()
Definition: NekVector.cpp:279
DataType InfinityNorm() const
Definition: NekVector.cpp:398
NekVector< DataType > Cross(const NekVector< DataType > &rhs) const
Definition: NekVector.cpp:382
NekVector< DataType > Negate(const NekVector< DataType > &v)
Definition: NekVector.cpp:944
iterator end()
Definition: NekVector.cpp:242
DNekMat void Add(NekMatrix< DataType, StandardMatrixTag > &result, const NekMatrix< LhsDataType, LhsMatrixType > &lhs, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
unsigned int GetDimension() const
Returns the number of dimensions for the point.
Definition: NekVector.cpp:209
PointerWrapper m_wrapperType
Definition: NekVector.hpp:185
boost::call_traits< DataType >::reference y()
Definition: NekVector.cpp:272
StandardMatrixTag boost::call_traits< LhsDataType >::const_reference rhs
unsigned int GetRows() const
Definition: NekVector.cpp:215
DataType L2Norm() const
Definition: NekVector.cpp:395
void InvertInPlace(NekDouble &v)
Definition: NekVector.cpp:1022
NekVector< DataType > & operator=(const NekVector< DataType > &rhs)
Definition: NekVector.cpp:184
RhsMatrixType void Subtract(NekMatrix< DataType, StandardMatrixTag > &result, const NekMatrix< LhsDataType, LhsMatrixType > &lhs, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
1D Array of constant elements with garbage collection and bounds checking.
Definition: SharedArray.hpp:57
void NegateInPlace(NekVector< DataType > &v)
Definition: NekVector.cpp:959
Array< OneD, DataType > operator+(const Array< OneD, DataType > &lhs, size_t offset)
Array< OneD, DataType > & GetPtr()
Definition: NekVector.cpp:227
void SetSize(unsigned int s)
Definition: NekVector.cpp:407