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 <algorithm>
47 #include <cmath>
48 #include <functional>
49 
50 #include <boost/call_traits.hpp>
51 #include <type_traits>
52 
53 namespace Nektar
54 {
55 template <typename DataType> class NekVector
56 {
57 public:
58  /// \brief Creates an empty vector.
60 
61  /// \brief Creates a vector of given size. The elements are not
62  /// initialized.
63  LIB_UTILITIES_EXPORT explicit NekVector(unsigned int size);
64 
65  /// \brief Creates a vector with given size and initial value.
67  unsigned int size,
68  typename boost::call_traits<DataType>::const_reference a);
69 
70  LIB_UTILITIES_EXPORT explicit NekVector(const std::string &vectorValues);
71 
73  typename boost::call_traits<DataType>::const_reference x,
74  typename boost::call_traits<DataType>::const_reference y,
75  typename boost::call_traits<DataType>::const_reference z);
76 
78 
79  LIB_UTILITIES_EXPORT NekVector(unsigned int size,
80  const DataType *const ptr);
82  PointerWrapper h = eCopy);
83  LIB_UTILITIES_EXPORT NekVector(unsigned int size,
85  PointerWrapper h = eCopy);
86 
87  LIB_UTILITIES_EXPORT NekVector(unsigned int size,
88  const Array<OneD, const DataType> &ptr,
89  PointerWrapper h = eCopy);
90 
92 
94  const NekVector<DataType> &rhs);
95 
96  /// \brief Returns the number of dimensions for the point.
97  LIB_UTILITIES_EXPORT unsigned int GetDimension() const;
98 
99  LIB_UTILITIES_EXPORT unsigned int GetRows() const;
100 
101  LIB_UTILITIES_EXPORT DataType *GetRawPtr();
102 
104 
105  LIB_UTILITIES_EXPORT const DataType *GetRawPtr() const;
106 
108 
109  typedef DataType *iterator;
112 
113  typedef const DataType *const_iterator;
116 
117  /// \brief Returns i^{th} element.
118  /// \param i The element to return.
119  /// \pre i < dim
120  /// \return A reference to the i^{th} element.
121  ///
122  /// Retrieves the i^{th} element. Since it returns a reference you may
123  /// assign a new value (i.e., p(2) = 3.2;)
124  ///
125  /// This operator performs range checking.
126  LIB_UTILITIES_EXPORT typename boost::call_traits<DataType>::reference
127  operator()(unsigned int i);
128 
129  LIB_UTILITIES_EXPORT typename boost::call_traits<DataType>::reference
130  operator[](unsigned int i);
131 
132  LIB_UTILITIES_EXPORT typename boost::call_traits<DataType>::reference x();
133 
134  LIB_UTILITIES_EXPORT typename boost::call_traits<DataType>::reference y();
135 
136  LIB_UTILITIES_EXPORT typename boost::call_traits<DataType>::reference z();
137 
139  typename boost::call_traits<DataType>::const_reference val);
140 
142  typename boost::call_traits<DataType>::const_reference val);
143 
145  typename boost::call_traits<DataType>::const_reference val);
146 
148  const NekVector<DataType> &rhs);
149 
151  const NekVector<DataType> &rhs);
152 
154  typename boost::call_traits<DataType>::const_reference rhs);
155 
157  typename boost::call_traits<DataType>::const_reference rhs);
158 
160 
161  LIB_UTILITIES_EXPORT typename boost::call_traits<DataType>::const_reference
162  operator()(unsigned int i) const;
163 
164  LIB_UTILITIES_EXPORT typename boost::call_traits<DataType>::const_reference
165  operator[](unsigned int i) const;
166 
167  LIB_UTILITIES_EXPORT typename boost::call_traits<DataType>::const_reference x()
168  const;
169 
170  LIB_UTILITIES_EXPORT typename boost::call_traits<DataType>::const_reference y()
171  const;
172 
173  LIB_UTILITIES_EXPORT typename boost::call_traits<DataType>::const_reference z()
174  const;
175 
177 
178  LIB_UTILITIES_EXPORT DataType Magnitude() const;
179  LIB_UTILITIES_EXPORT DataType Dot(const NekVector<DataType> &rhs) const;
180 
182  const NekVector<DataType> &rhs) const;
183 
184  LIB_UTILITIES_EXPORT std::string AsString() const;
185 
186  // Norms
187  LIB_UTILITIES_EXPORT DataType L1Norm() const;
188  LIB_UTILITIES_EXPORT DataType L2Norm() const;
189  LIB_UTILITIES_EXPORT DataType InfinityNorm() const;
190 
192 
193 protected:
195  LIB_UTILITIES_EXPORT void SetSize(unsigned int s);
198  LIB_UTILITIES_EXPORT void Resize(unsigned int newSize);
199 
200 private:
201  // Prevents accidental use of wrapped mode around ConstArrays.
203 
204  unsigned int m_size;
207 };
208 
209 template <typename DataType>
211  const NekVector<DataType> &lhs,
212  const NekVector<DataType> &rhs);
213 
214 template <typename DataType>
216  const NekVector<DataType> &lhs,
217  const NekVector<DataType> &rhs);
218 
219 template <typename DataType>
221  const NekVector<DataType> &rhs);
222 
223 template <typename DataType>
225  const NekVector<DataType> &rhs);
226 
227 template <typename LhsDataType, typename RhsDataType>
229  const NekVector<LhsDataType> &lhs, const NekVector<RhsDataType> &rhs);
230 
231 template <typename ResultDataType, typename InputDataType>
233  const NekVector<InputDataType> &lhs,
234  const NekVector<InputDataType> &rhs);
235 
236 template <typename ResultDataType, typename InputDataType>
239  const NekVector<InputDataType> &rhs);
240 
241 template <typename ResultDataType, typename InputDataType>
243  const NekVector<InputDataType> &rhs);
244 
245 template <typename ResultDataType, typename InputDataType>
248 
249 template <typename DataType>
251 Subtract(const NekVector<DataType> &lhs, const NekVector<DataType> &rhs);
252 
253 template <typename ResultDataType, typename InputDataType>
255  const NekVector<InputDataType> &lhs,
256  const NekDouble &rhs);
257 
258 template <typename ResultDataType>
260  const NekDouble &rhs);
261 
262 template <typename DataType>
264  const NekDouble &rhs);
265 
266 template <typename ResultDataType, typename InputDataType>
268  const NekVector<InputDataType> &lhs,
269  const NekVector<InputDataType> &rhs);
270 
271 template <typename ResultDataType, typename InputDataType>
273  const NekVector<InputDataType> &rhs);
274 
275 template <typename DataType, typename InputDataType>
278 
279 template <typename ResultDataType, typename InputDataType>
281  const NekVector<InputDataType> &lhs,
282  const NekDouble &rhs);
283 
284 template <typename ResultDataType>
286  const NekDouble &rhs);
287 
288 template <typename DataType>
290 Multiply(const NekVector<DataType> &lhs, const NekDouble &rhs);
291 
292 template <typename ResultDataType, typename InputDataType>
294  const NekDouble &lhs,
295  const NekVector<InputDataType> &rhs);
296 
297 template <typename ResultDataType, typename InputDataType>
300  const NekVector<InputDataType> &rhs);
301 
302 template <typename DataType>
304 Multiply(const DataType &lhs, const NekVector<DataType> &rhs);
305 
306 template <typename DataType>
308  const NekDouble &rhs)
309 {
310  return Multiply(lhs, rhs);
311 }
312 
313 template <typename DataType>
315  const NekVector<DataType> &rhs)
316 {
317  return Multiply(lhs, rhs);
318 }
319 
320 template <typename DataType>
322  const NekVector<DataType> &rhs)
323 {
324  return Multiply(lhs, rhs);
325 }
326 
327 template <typename DataType>
329  const NekDouble &rhs)
330 {
331  return Divide(lhs, rhs);
332 }
333 
334 template <typename DataType>
336  const NekVector<DataType> &rhs)
337 {
338  return Add(lhs, rhs);
339 }
340 
341 template <typename DataType>
343  const NekVector<DataType> &rhs)
344 {
345  return Subtract(lhs, rhs);
346 }
347 
348 template <typename DataType>
349 LIB_UTILITIES_EXPORT std::ostream &operator<<(std::ostream &os,
350  const NekVector<DataType> &rhs);
351 
352 template <typename DataType>
353 LIB_UTILITIES_EXPORT NekVector<DataType> createVectorFromPoints(
354  const NekPoint<DataType> &source, const NekPoint<DataType> &dest);
355 
356 template <typename DataType>
357 LIB_UTILITIES_EXPORT NekPoint<DataType> findPointAlongVector(
358  const NekVector<DataType> &lhs, const DataType &t);
359 
360 template <typename DataType>
361 LIB_UTILITIES_EXPORT bool operator==(const NekVector<DataType> &lhs,
362  const NekVector<DataType> &rhs);
363 
364 template <typename DataType>
365 LIB_UTILITIES_EXPORT bool operator!=(const NekVector<DataType> &lhs,
366  const NekVector<DataType> &rhs);
367 
368 template <typename DataType>
369 LIB_UTILITIES_EXPORT DataType Magnitude(const NekVector<DataType> &v);
370 
371 template <typename DataType>
372 LIB_UTILITIES_EXPORT DataType Dot(const NekVector<DataType> &lhs,
373  const NekVector<DataType> &rhs);
374 
375 template <typename DataType>
376 LIB_UTILITIES_EXPORT std::vector<DataType> FromString(const std::string &str);
377 
378 /// \todo Do the Norms with Blas where applicable.
379 template <typename DataType>
380 LIB_UTILITIES_EXPORT DataType L1Norm(const NekVector<DataType> &v);
381 
382 template <typename DataType>
383 LIB_UTILITIES_EXPORT DataType L2Norm(const NekVector<DataType> &v);
384 
385 template <typename DataType>
386 LIB_UTILITIES_EXPORT DataType InfinityNorm(const NekVector<DataType> &v);
387 
388 template <typename DataType>
389 LIB_UTILITIES_EXPORT NekVector<DataType> Negate(const NekVector<DataType> &v);
390 
391 template <typename DataType>
392 LIB_UTILITIES_EXPORT void NegateInPlace(NekVector<DataType> &v);
393 
396 
397 template <typename DataType>
398 LIB_UTILITIES_EXPORT void Normalize(NekVector<DataType> &v);
399 
400 template <typename DataType>
401 LIB_UTILITIES_EXPORT NekVector<DataType> Cross(const NekVector<DataType> &lhs,
402  const NekVector<DataType> &rhs);
403 template <typename DataType>
404 LIB_UTILITIES_EXPORT std::string AsString(const NekVector<DataType> &v);
405 
406 } // namespace Nektar
407 
408 #endif // NEKTAR_LIB_UTILITIES_NEK_VECTOR_HPP
#define LIB_UTILITIES_EXPORT
1D Array of constant elements with garbage collection and bounds checking.
Definition: SharedArray.hpp:58
boost::call_traits< DataType >::reference x()
Definition: NekVector.cpp:275
unsigned int m_size
Definition: NekVector.hpp:204
void SetZ(typename boost::call_traits< DataType >::const_reference val)
Definition: NekVector.cpp:312
void SetWrapperType(PointerWrapper p)
Definition: NekVector.cpp:459
boost::call_traits< DataType >::reference operator[](unsigned int i)
Definition: NekVector.cpp:269
DataType L1Norm() const
Definition: NekVector.cpp:426
Array< OneD, DataType > & GetData()
Definition: NekVector.cpp:448
NekVector< DataType > Cross(const NekVector< DataType > &rhs) const
Definition: NekVector.cpp:414
Array< OneD, DataType > & GetPtr()
Definition: NekVector.cpp:217
unsigned int GetDimension() const
Returns the number of dimensions for the point.
Definition: NekVector.cpp:201
NekVector< DataType > & operator+=(const NekVector< DataType > &rhs)
Definition: NekVector.cpp:320
NekVector< DataType > & operator=(const NekVector< DataType > &rhs)
Definition: NekVector.cpp:174
PointerWrapper GetWrapperType() const
Definition: NekVector.cpp:442
void SetY(typename boost::call_traits< DataType >::const_reference val)
Definition: NekVector.cpp:304
Array< OneD, DataType > m_data
Definition: NekVector.hpp:205
void Resize(unsigned int newSize)
Definition: NekVector.cpp:471
PointerWrapper m_wrapperType
Definition: NekVector.hpp:206
DataType Dot(const NekVector< DataType > &rhs) const
Definition: NekVector.cpp:408
unsigned int GetRows() const
Definition: NekVector.cpp:206
std::string AsString() const
Definition: NekVector.cpp:420
void SetSize(unsigned int s)
Definition: NekVector.cpp:453
NekVector< DataType > & operator/=(typename boost::call_traits< DataType >::const_reference rhs)
Definition: NekVector.cpp:344
boost::call_traits< DataType >::reference y()
Definition: NekVector.cpp:282
iterator begin()
Definition: NekVector.cpp:235
DataType * iterator
Definition: NekVector.hpp:109
iterator end()
Definition: NekVector.cpp:241
NekVector< DataType > & operator*=(typename boost::call_traits< DataType >::const_reference rhs)
Definition: NekVector.cpp:336
const DataType * const_iterator
Definition: NekVector.hpp:113
DataType InfinityNorm() const
Definition: NekVector.cpp:436
DataType L2Norm() const
Definition: NekVector.cpp:431
boost::call_traits< DataType >::reference operator()(unsigned int i)
Returns i^{th} element.
Definition: NekVector.cpp:260
DataType Magnitude() const
Definition: NekVector.cpp:402
NekVector< DataType > & operator-=(const NekVector< DataType > &rhs)
Definition: NekVector.cpp:328
NekVector()
Creates an empty vector.
Definition: NekVector.cpp:41
DataType * GetRawPtr()
Definition: NekVector.cpp:211
void SetData(const Array< OneD, DataType > &newData)
Definition: NekVector.cpp:465
boost::call_traits< DataType >::reference z()
Definition: NekVector.cpp:289
NekVector< DataType > operator-() const
Definition: NekVector.cpp:397
void SetX(typename boost::call_traits< DataType >::const_reference val)
Definition: NekVector.cpp:296
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
void Normalize(NekVector< DataType > &v)
Definition: NekVector.cpp:1214
std::string AsString(const NekVector< DataType > &v)
Definition: NekVector.cpp:1266
bool operator==(const Array< OneD, T1 > &lhs, const Array< OneD, T2 > &rhs)
void InvertInPlace(NekDouble &v)
Definition: NekVector.cpp:1229
SNekMat SNekMat void SubtractEqual(NekMatrix< DataType, StandardMatrixTag > &result, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
void MultiplyInvertedLhs(NekVector< ResultDataType > &result, const NekDouble &lhs, const NekVector< InputDataType > &rhs)
Definition: NekVector.cpp:914
NekVector< DataType > operator*(const NekMatrix< LhsDataType, MatrixType > &lhs, const NekVector< DataType > &rhs)
void AddEqualNegatedLhs(NekMatrix< DataType, StandardMatrixTag > &result, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
SNekMat void AddEqual(NekMatrix< DataType, StandardMatrixTag > &result, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
DataType InfinityNorm(const NekVector< DataType > &v)
Definition: NekVector.cpp:1125
void Divide(NekVector< ResultDataType > &result, const NekVector< InputDataType > &lhs, const NekDouble &rhs)
Definition: NekVector.cpp:675
NekVector< DataType > Negate(const NekVector< DataType > &v)
Definition: NekVector.cpp:1143
void Subtract(NekMatrix< DataType, StandardMatrixTag > &result, const NekMatrix< LhsDataType, LhsMatrixType > &lhs, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
std::vector< DataType > FromString(const std::string &str)
Definition: NekVector.cpp:1061
Array< OneD, DataType > operator+(const Array< OneD, DataType > &lhs, typename Array< OneD, DataType >::size_type offset)
void Multiply(NekMatrix< ResultDataType, StandardMatrixTag > &result, const NekMatrix< LhsDataType, LhsMatrixType > &lhs, const ResultDataType &rhs)
void DivideEqual(NekVector< ResultDataType > &result, const NekDouble &rhs)
Definition: NekVector.cpp:711
NekVector< DataType > createVectorFromPoints(const NekPoint< DataType > &source, const NekPoint< DataType > &dest)
Definition: NekVector.cpp:995
void AddNegatedLhs(NekMatrix< DataType, StandardMatrixTag > &result, const NekMatrix< LhsDataType, LhsMatrixType > &lhs, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
void SubtractEqualNegatedLhs(NekMatrix< DataType, StandardMatrixTag > &result, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
void NegateInPlace(NekVector< DataType > &v)
Definition: NekVector.cpp:1160
DataType Magnitude(const NekVector< DataType > &v)
Definition: NekVector.cpp:1174
const NekSingle void MultiplyEqual(NekMatrix< LhsDataType, StandardMatrixTag > &lhs, typename boost::call_traits< LhsDataType >::const_reference rhs)
NekPoint< DataType > operator/(const NekPoint< DataType > &lhs, typename boost::call_traits< DataType >::param_type rhs)
Definition: NekPoint.hpp:452
NekVector< DataType > Cross(const NekVector< DataType > &lhs, const NekVector< DataType > &rhs)
Definition: NekVector.cpp:1246
void SubtractNegatedLhs(NekMatrix< DataType, StandardMatrixTag > &result, const NekMatrix< LhsDataType, LhsMatrixType > &lhs, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
std::ostream & operator<<(std::ostream &os, const NekMatrix< DataType, FormType > &rhs)
Definition: NekMatrix.hpp:49
DataType L2Norm(const NekVector< DataType > &v)
Definition: NekVector.cpp:1107
DataType L1Norm(const NekVector< DataType > &v)
Definition: NekVector.cpp:1089
NekPoint< DataType > findPointAlongVector(const NekVector< DataType > &lhs, const DataType &t)
Definition: NekVector.cpp:1013
bool operator!=(const Array< OneD, T1 > &lhs, const Array< OneD, T2 > &rhs)
NekMatrix< typename NekMatrix< LhsDataType, LhsMatrixType >::NumberType, StandardMatrixTag > operator-(const NekMatrix< LhsDataType, LhsMatrixType > &lhs, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
double NekDouble
PointerWrapper
Specifies if the pointer passed to a NekMatrix or NekVector is copied into an internal representation...
SNekMat SNekMat void Add(NekMatrix< DataType, StandardMatrixTag > &result, const NekMatrix< LhsDataType, LhsMatrixType > &lhs, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
DataType Dot(const NekVector< DataType > &lhs, const NekVector< DataType > &rhs)
Definition: NekVector.cpp:1193