Nektar++
Loading...
Searching...
No Matches
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 <type_traits>
51
52namespace Nektar
53{
54template <typename DataType> class NekVector
55{
56public:
57 /// \brief Creates an empty vector.
59
60 /// \brief Creates a vector of given size. The elements are not
61 /// initialized.
62 LIB_UTILITIES_EXPORT explicit NekVector(unsigned int size);
63
64 /// \brief Creates a vector with given size and initial value.
65 LIB_UTILITIES_EXPORT NekVector(unsigned int size, const DataType &a);
66
67 LIB_UTILITIES_EXPORT explicit NekVector(const std::string &vectorValues);
68
69 LIB_UTILITIES_EXPORT NekVector(const DataType &x, const DataType &y,
70 const DataType &z);
71
73
74 LIB_UTILITIES_EXPORT NekVector(unsigned int size,
75 const DataType *const ptr);
78 LIB_UTILITIES_EXPORT NekVector(unsigned int size,
81
82 LIB_UTILITIES_EXPORT NekVector(unsigned int size,
85
87
89 const NekVector<DataType> &rhs);
90
91 /// \brief Returns the number of dimensions for the point.
92 LIB_UTILITIES_EXPORT unsigned int GetDimension() const;
93
94 LIB_UTILITIES_EXPORT unsigned int GetRows() const;
95
97
99
100 LIB_UTILITIES_EXPORT const DataType *GetRawPtr() const;
101
103
104 typedef DataType *iterator;
107
108 typedef const DataType *const_iterator;
111
112 /// \brief Returns i^{th} element.
113 /// \param i The element to return.
114 /// \pre i < dim
115 /// \return A reference to the i^{th} element.
116 ///
117 /// Retrieves the i^{th} element. Since it returns a reference you may
118 /// assign a new value (i.e., p(2) = 3.2;)
119 ///
120 /// This operator performs range checking.
121 LIB_UTILITIES_EXPORT DataType &operator()(unsigned int i);
122
123 LIB_UTILITIES_EXPORT DataType &operator[](unsigned int i);
124
125 LIB_UTILITIES_EXPORT DataType &x();
126
127 LIB_UTILITIES_EXPORT DataType &y();
128
129 LIB_UTILITIES_EXPORT DataType &z();
130
131 LIB_UTILITIES_EXPORT void SetX(const DataType &val);
132
133 LIB_UTILITIES_EXPORT void SetY(const DataType &val);
134
135 LIB_UTILITIES_EXPORT void SetZ(const DataType &val);
136
138 const NekVector<DataType> &rhs);
139
141 const NekVector<DataType> &rhs);
142
144
146
148
149 LIB_UTILITIES_EXPORT const DataType &operator()(unsigned int i) const;
150
151 LIB_UTILITIES_EXPORT const DataType &operator[](unsigned int i) const;
152
153 LIB_UTILITIES_EXPORT const DataType &x() const;
154
155 LIB_UTILITIES_EXPORT const DataType &y() const;
156
157 LIB_UTILITIES_EXPORT const DataType &z() const;
158
160
161 LIB_UTILITIES_EXPORT DataType Magnitude() const;
162 LIB_UTILITIES_EXPORT DataType Dot(const NekVector<DataType> &rhs) const;
163
165 const NekVector<DataType> &rhs) const;
166
167 LIB_UTILITIES_EXPORT std::string AsString() const;
168
169 // Norms
170 LIB_UTILITIES_EXPORT DataType L1Norm() const;
171 LIB_UTILITIES_EXPORT DataType L2Norm() const;
172 LIB_UTILITIES_EXPORT DataType InfinityNorm() const;
173
175
176protected:
178 LIB_UTILITIES_EXPORT void SetSize(unsigned int s);
181 LIB_UTILITIES_EXPORT void Resize(unsigned int newSize);
182
183private:
184 // Prevents accidental use of wrapped mode around ConstArrays.
186
187 unsigned int m_size;
190};
191
192template <typename DataType>
194 const NekVector<DataType> &lhs,
195 const NekVector<DataType> &rhs);
196
197template <typename DataType>
199 const NekVector<DataType> &lhs,
200 const NekVector<DataType> &rhs);
201
202template <typename DataType>
204 const NekVector<DataType> &rhs);
205
206template <typename DataType>
208 const NekVector<DataType> &rhs);
209
210template <typename LhsDataType, typename RhsDataType>
212 const NekVector<LhsDataType> &lhs, const NekVector<RhsDataType> &rhs);
213
214template <typename ResultDataType, typename InputDataType>
216 const NekVector<InputDataType> &lhs,
217 const NekVector<InputDataType> &rhs);
218
219template <typename ResultDataType, typename InputDataType>
222 const NekVector<InputDataType> &rhs);
223
224template <typename ResultDataType, typename InputDataType>
226 const NekVector<InputDataType> &rhs);
227
228template <typename ResultDataType, typename InputDataType>
231
232template <typename DataType>
234Subtract(const NekVector<DataType> &lhs, const NekVector<DataType> &rhs);
235
236template <typename ResultDataType, typename InputDataType>
238 const NekVector<InputDataType> &lhs,
239 const NekDouble &rhs);
240
241template <typename ResultDataType>
243 const NekDouble &rhs);
244
245template <typename DataType>
247 const NekDouble &rhs);
248
249template <typename ResultDataType, typename InputDataType>
251 const NekVector<InputDataType> &lhs,
252 const NekVector<InputDataType> &rhs);
253
254template <typename ResultDataType, typename InputDataType>
256 const NekVector<InputDataType> &rhs);
257
258template <typename DataType, typename InputDataType>
261
262template <typename ResultDataType, typename InputDataType>
264 const NekVector<InputDataType> &lhs,
265 const NekDouble &rhs);
266
267template <typename ResultDataType>
269 const NekDouble &rhs);
270
271template <typename DataType>
273Multiply(const NekVector<DataType> &lhs, const NekDouble &rhs);
274
275template <typename ResultDataType, typename InputDataType>
277 const NekDouble &lhs,
278 const NekVector<InputDataType> &rhs);
279
280template <typename ResultDataType, typename InputDataType>
283 const NekVector<InputDataType> &rhs);
284
285template <typename DataType>
287Multiply(const DataType &lhs, const NekVector<DataType> &rhs);
288
289template <typename DataType>
291 const NekDouble &rhs)
292{
293 return Multiply(lhs, rhs);
294}
295
296template <typename DataType>
298 const NekVector<DataType> &rhs)
299{
300 return Multiply(lhs, rhs);
301}
302
303template <typename DataType>
305 const NekVector<DataType> &rhs)
306{
307 return Multiply(lhs, rhs);
308}
309
310template <typename DataType>
312 const NekDouble &rhs)
313{
314 return Divide(lhs, rhs);
315}
316
317template <typename DataType>
319 const NekVector<DataType> &rhs)
320{
321 return Add(lhs, rhs);
322}
323
324template <typename DataType>
326 const NekVector<DataType> &rhs)
327{
328 return Subtract(lhs, rhs);
329}
330
331template <typename DataType>
332LIB_UTILITIES_EXPORT std::ostream &operator<<(std::ostream &os,
333 const NekVector<DataType> &rhs);
334
335template <typename DataType>
337 const NekPoint<DataType> &source, const NekPoint<DataType> &dest);
338
339template <typename DataType>
341 const NekVector<DataType> &lhs, const DataType &t);
342
343template <typename DataType>
344LIB_UTILITIES_EXPORT bool operator==(const NekVector<DataType> &lhs,
345 const NekVector<DataType> &rhs);
346
347template <typename DataType>
348LIB_UTILITIES_EXPORT bool operator!=(const NekVector<DataType> &lhs,
349 const NekVector<DataType> &rhs);
350
351template <typename DataType>
352LIB_UTILITIES_EXPORT DataType Magnitude(const NekVector<DataType> &v);
353
354template <typename DataType>
355LIB_UTILITIES_EXPORT DataType Dot(const NekVector<DataType> &lhs,
356 const NekVector<DataType> &rhs);
357
358template <typename DataType>
359LIB_UTILITIES_EXPORT std::vector<DataType> FromString(const std::string &str);
360
361/// \todo Do the Norms with Blas where applicable.
362template <typename DataType>
363LIB_UTILITIES_EXPORT DataType L1Norm(const NekVector<DataType> &v);
364
365template <typename DataType>
366LIB_UTILITIES_EXPORT DataType L2Norm(const NekVector<DataType> &v);
367
368template <typename DataType>
369LIB_UTILITIES_EXPORT DataType InfinityNorm(const NekVector<DataType> &v);
370
371template <typename DataType>
372LIB_UTILITIES_EXPORT NekVector<DataType> Negate(const NekVector<DataType> &v);
373
374template <typename DataType>
375LIB_UTILITIES_EXPORT void NegateInPlace(NekVector<DataType> &v);
376
379
380template <typename DataType>
381LIB_UTILITIES_EXPORT void Normalize(NekVector<DataType> &v);
382
383template <typename DataType>
384LIB_UTILITIES_EXPORT NekVector<DataType> Cross(const NekVector<DataType> &lhs,
385 const NekVector<DataType> &rhs);
386template <typename DataType>
387LIB_UTILITIES_EXPORT std::string AsString(const NekVector<DataType> &v);
388
389} // namespace Nektar
390
391#endif // NEKTAR_LIB_UTILITIES_NEK_VECTOR_HPP
#define LIB_UTILITIES_EXPORT
unsigned int m_size
void SetWrapperType(PointerWrapper p)
DataType L1Norm() const
Array< OneD, DataType > & GetData()
NekVector< DataType > Cross(const NekVector< DataType > &rhs) const
Array< OneD, DataType > & GetPtr()
unsigned int GetDimension() const
Returns the number of dimensions for the point.
NekVector< DataType > & operator/=(const DataType &rhs)
NekVector< DataType > & operator+=(const NekVector< DataType > &rhs)
NekVector< DataType > & operator=(const NekVector< DataType > &rhs)
PointerWrapper GetWrapperType() const
DataType & x()
void SetZ(const DataType &val)
DataType & y()
void SetY(const DataType &val)
Array< OneD, DataType > m_data
void Resize(unsigned int newSize)
DataType & z()
void SetX(const DataType &val)
PointerWrapper m_wrapperType
DataType Dot(const NekVector< DataType > &rhs) const
unsigned int GetRows() const
std::string AsString() const
void SetSize(unsigned int s)
DataType * iterator
const DataType * const_iterator
NekVector< DataType > & operator*=(const DataType &rhs)
DataType & operator()(unsigned int i)
Returns i^{th} element.
DataType & operator[](unsigned int i)
DataType InfinityNorm() const
DataType L2Norm() const
DataType Magnitude() const
NekVector< DataType > & operator-=(const NekVector< DataType > &rhs)
NekVector()
Creates an empty vector.
Definition NekVector.cpp:42
DataType * GetRawPtr()
void SetData(const Array< OneD, DataType > &newData)
NekVector< DataType > operator-() const
std::ostream & operator<<(std::ostream &os, const BasisKey &rhs)
void Normalize(NekVector< DataType > &v)
std::string AsString(const NekVector< DataType > &v)
bool operator==(const Array< OneD, T1 > &lhs, const Array< OneD, T2 > &rhs)
void InvertInPlace(NekDouble &v)
SNekMat SNekMat void SubtractEqual(NekMatrix< DataType, StandardMatrixTag > &result, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
NekMatrix< typename NekMatrix< LhsDataType, LhsMatrixType >::NumberType, StandardMatrixTag > operator-(const NekMatrix< LhsDataType, LhsMatrixType > &lhs, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
void MultiplyInvertedLhs(NekVector< ResultDataType > &result, const NekDouble &lhs, const NekVector< InputDataType > &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)
void Divide(NekVector< ResultDataType > &result, const NekVector< InputDataType > &lhs, const NekDouble &rhs)
NekVector< DataType > Negate(const NekVector< DataType > &v)
void Subtract(NekMatrix< DataType, StandardMatrixTag > &result, const NekMatrix< LhsDataType, LhsMatrixType > &lhs, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
NekPoint< DataType > operator/(const NekPoint< DataType > &lhs, const typename NekPoint< DataType >::DataType rhs)
Definition NekPoint.hpp:441
std::vector< DataType > FromString(const std::string &str)
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)
NekVector< DataType > operator*(const NekMatrix< LhsDataType, MatrixType > &lhs, const NekVector< DataType > &rhs)
void DivideEqual(NekVector< ResultDataType > &result, const NekDouble &rhs)
NekVector< DataType > createVectorFromPoints(const NekPoint< DataType > &source, const NekPoint< DataType > &dest)
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)
DataType Magnitude(const NekVector< DataType > &v)
NekVector< DataType > Cross(const NekVector< DataType > &lhs, const NekVector< DataType > &rhs)
void SubtractNegatedLhs(NekMatrix< DataType, StandardMatrixTag > &result, const NekMatrix< LhsDataType, LhsMatrixType > &lhs, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
DataType L2Norm(const NekVector< DataType > &v)
DataType L1Norm(const NekVector< DataType > &v)
NekPoint< DataType > findPointAlongVector(const NekVector< DataType > &lhs, const DataType &t)
const NekSingle void MultiplyEqual(NekMatrix< LhsDataType, StandardMatrixTag > &lhs, const typename NekMatrix< LhsDataType, StandardMatrixTag >::NumberType &rhs)
bool operator!=(const Array< OneD, T1 > &lhs, const Array< OneD, T2 > &rhs)
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)