Nektar++
NekVector.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: NekVector.cpp
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:
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
36 
37 namespace Nektar
38 {
39 
40  template<typename DataType>
42  m_size(0),
43  m_data(),
44  m_wrapperType(eCopy)
45  {
46  }
47 
48  template<typename DataType>
49  NekVector<DataType>::NekVector(unsigned int size) :
50  m_size(size),
51  m_data(size),
52  m_wrapperType(eCopy)
53  {
54  }
55 
56  template<typename DataType>
57  NekVector<DataType>::NekVector(unsigned int size, typename boost::call_traits<DataType>::const_reference a) :
58  m_size(size),
59  m_data(size),
60  m_wrapperType(eCopy)
61  {
62  std::fill_n(m_data.get(), m_size, a);
63  }
64 
65  template<typename DataType>
66  NekVector<DataType>::NekVector(const std::string& vectorValues) :
67  m_size(0),
68  m_data(),
69  m_wrapperType(eCopy)
70  {
71  try
72  {
73  std::vector<DataType> values = FromString<DataType>(vectorValues);
74  m_size = values.size();
75  m_data = Array<OneD, DataType>(m_size);
76  std::copy(values.begin(), values.end(), m_data.begin());
77 
78  ASSERTL0(m_size > 0, "Error converting string values to vector");
79  }
80  catch(std::runtime_error& e)
81  {
82  NEKERROR(ErrorUtil::efatal, e.what());
83  }
84  }
85 
86  template<typename DataType>
87  NekVector<DataType>::NekVector(typename boost::call_traits<DataType>::const_reference x,
88  typename boost::call_traits<DataType>::const_reference y,
89  typename boost::call_traits<DataType>::const_reference z) :
90  m_size(3),
91  m_data(m_size),
92  m_wrapperType(eCopy)
93  {
94  m_data[0] = x;
95  m_data[1] = y;
96  m_data[2] = z;
97  }
98 
99  template<typename DataType>
101  m_size(rhs.GetDimension()),
102  m_data(rhs.m_data),
103  m_wrapperType(rhs.m_wrapperType)
104  {
105  if( m_wrapperType == eCopy )
106  {
107  m_data = Array<OneD, DataType>(m_size);
108  std::copy(rhs.begin(), rhs.end(), m_data.get());
109  }
110  }
111 
112  template<typename DataType>
113  NekVector<DataType>::NekVector(unsigned int size, const DataType* const ptr) :
114  m_size(size),
115  m_data(size, ptr),
116  m_wrapperType(eCopy)
117  {
118  }
119 
120  template<typename DataType>
122  m_size(ptr.num_elements()),
123  m_data(ptr),
124  m_wrapperType(h)
125  {
126  if( h == eCopy )
127  {
128  m_data = Array<OneD, DataType>(m_size);
129  CopyArray(ptr, m_data);
130  }
131  }
132 
133  template<typename DataType>
135  m_size(size),
136  m_data(ptr),
137  m_wrapperType(h)
138  {
139  if( h == eCopy )
140  {
141  ASSERTL0(size <= ptr.num_elements(), "Attempting to populate a vector of size " +
142  std::to_string(size) + " but the incoming array only has " +
143  std::to_string(ptr.num_elements()) + " elements.");
144 
145  m_data = Array<OneD, DataType>(size);
146  std::copy(ptr.begin(), ptr.begin()+size, m_data.begin());
147  }
148  }
149 
150  template<typename DataType>
152  m_size(ptr.num_elements()),
153  m_data(ptr, eVECTOR_WRAPPER),
154  m_wrapperType(h)
155  {
156  if( h == eCopy )
157  {
158  m_data = Array<OneD, DataType>(m_size);
159  CopyArray(ptr, m_data);
160  }
161  }
162 
163  template<typename DataType>
165  m_size(size),
166  m_data(ptr, eVECTOR_WRAPPER),
167  m_wrapperType(h)
168  {
169  if( h == eCopy )
170  {
171  ASSERTL0(size <= ptr.num_elements(), "Attempting to populate a vector of size " +
172  std::to_string(size) + " but the incoming array only has " +
173  std::to_string(ptr.num_elements()) + " elements.");
174 
175  m_data = Array<OneD, DataType>(size);
176  std::copy(ptr.begin(), ptr.begin()+size, m_data.begin());
177  }
178  }
179 
180  template<typename DataType>
182 
183  template<typename DataType>
185  {
186  if( m_wrapperType == eCopy )
187  {
188  // If the current vector is a copy, then regardless of the rhs type
189  // we just copy over the values, resizing if needed.
190  if( GetDimension() != rhs.GetDimension() )
191  {
192  m_size = rhs.GetDimension();
193  m_data = Array<OneD, DataType>(m_size);
194  }
195  }
196  else if( m_wrapperType == eWrapper )
197  {
198  // If the current vector is wrapped, then just copy over the top,
199  // but the sizes of the two vectors must be the same.
200  ASSERTL0(GetDimension() == rhs.GetDimension(), "Wrapped NekVectors must have the same dimension in operator=");
201  }
202 
203  std::copy(rhs.begin(), rhs.end(), m_data.get());
204  return *this;
205  }
206 
207 
208  template<typename DataType>
210  {
211  return m_size;
212  }
213 
214  template<typename DataType>
215  unsigned int NekVector<DataType>::GetRows() const
216  {
217  return m_size;
218  }
219 
220  template<typename DataType>
222  {
223  return this->GetData().get();
224  }
225 
226  template<typename DataType>
227  Array<OneD, DataType>& NekVector<DataType>::GetPtr() { return this->GetData(); }
228 
229  template<typename DataType>
230  const DataType* NekVector<DataType>::GetRawPtr() const
231  {
232  return m_data.get();
233  }
234 
235  template<typename DataType>
237 
238  template<typename DataType>
240 
241  template<typename DataType>
242  typename NekVector<DataType>::iterator NekVector<DataType>::end() { return GetRawPtr() + this->GetDimension(); }
243 
244  template<typename DataType>
245  typename NekVector<DataType>::const_iterator NekVector<DataType>::begin() const { return GetRawPtr(); }
246 
247  template<typename DataType>
248  typename NekVector<DataType>::const_iterator NekVector<DataType>::end() const { return GetRawPtr() + GetDimension(); }
249 
250  template<typename DataType>
251  typename boost::call_traits<DataType>::reference NekVector<DataType>::operator()(unsigned int i)
252  {
253  ASSERTL1(i < this->GetDimension(),
254  "Invalid access to m_data via parenthesis operator");
255  return this->GetData()[i];
256  }
257 
258  template<typename DataType>
259  typename boost::call_traits<DataType>::reference NekVector<DataType>::operator[](unsigned int i)
260  {
261  return this->GetData()[i];
262  }
263 
264  template<typename DataType>
265  typename boost::call_traits<DataType>::reference NekVector<DataType>::x()
266  {
267  ASSERTL1(this->GetDimension() >= 1, "Invalid use of NekVector::x");
268  return (*this)(0);
269  }
270 
271  template<typename DataType>
272  typename boost::call_traits<DataType>::reference NekVector<DataType>::y()
273  {
274  ASSERTL1(this->GetDimension() >= 2, "Invalid use of NekVector::y");
275  return (*this)(1);
276  }
277 
278  template<typename DataType>
279  typename boost::call_traits<DataType>::reference NekVector<DataType>::z()
280  {
281  ASSERTL1(this->GetDimension() >= 3, "Invalid use of NekVector::z");
282  return (*this)(2);
283  }
284 
285  template<typename DataType>
286  void NekVector<DataType>::SetX(typename boost::call_traits<DataType>::const_reference val)
287  {
288  ASSERTL1(this->GetDimension() >= 1, "Invalid use of NekVector::SetX");
289  this->GetData()[0] = val;
290  }
291 
292  template<typename DataType>
293  void NekVector<DataType>::SetY(typename boost::call_traits<DataType>::const_reference val)
294  {
295  ASSERTL1(this->GetDimension() >= 2, "Invalid use of NekVector::SetX");
296  this->GetData()[1] = val;
297  }
298 
299  template<typename DataType>
300  void NekVector<DataType>::SetZ(typename boost::call_traits<DataType>::const_reference val)
301  {
302  ASSERTL1(this->GetDimension() >= 3, "Invalid use of NekVector::SetX");
303  this->GetData()[2] = val;
304  }
305 
306  template<typename DataType>
308  {
309  AddEqual(*this, rhs);
310  return *this;
311  }
312 
313  template<typename DataType>
315  {
316  SubtractEqual(*this, rhs);
317  return *this;
318  }
319 
320  template<typename DataType>
321  NekVector<DataType>& NekVector<DataType>::operator*=(typename boost::call_traits<DataType>::const_reference rhs)
322  {
323  MultiplyEqual(*this, rhs);
324  return *this;
325  }
326 
327  template<typename DataType>
328  NekVector<DataType>& NekVector<DataType>::operator/=(typename boost::call_traits<DataType>::const_reference rhs)
329  {
330  DivideEqual(*this, rhs);
331  return *this;
332  }
333 
334  template<typename DataType>
336 
337  template<typename DataType>
338  typename boost::call_traits<DataType>::const_reference NekVector<DataType>::operator()(unsigned int i) const
339  {
340  ASSERTL1(i < GetDimension(),
341  "Invalid access to m_data via parenthesis operator");
342  return m_data[i];
343  }
344 
345  template<typename DataType>
346  typename boost::call_traits<DataType>::const_reference NekVector<DataType>::operator[](unsigned int i) const
347  {
348  return m_data[i];
349  }
350 
351  template<typename DataType>
352  typename boost::call_traits<DataType>::const_reference NekVector<DataType>::x() const
353  {
354  ASSERTL1( GetDimension() >= 1, "Invalid use of NekVector::x");
355  return (*this)(0);
356  }
357 
358  template<typename DataType>
359  typename boost::call_traits<DataType>::const_reference NekVector<DataType>::y() const
360  {
361  ASSERTL1( GetDimension() >= 2, "Invalid use of NekVector::y");
362  return (*this)(1);
363  }
364 
365  template<typename DataType>
366  typename boost::call_traits<DataType>::const_reference NekVector<DataType>::z() const
367  {
368  ASSERTL1( GetDimension() >= 3, "Invalid use of NekVector::z");
369  return (*this)(2);
370  }
371 
372  template<typename DataType>
374 
375  template<typename DataType>
376  DataType NekVector<DataType>::Magnitude() const { return Nektar::Magnitude(*this); }
377 
378  template<typename DataType>
379  DataType NekVector<DataType>::Dot(const NekVector<DataType>& rhs) const { return Nektar::Dot(*this, rhs); }
380 
381  template<typename DataType>
383  {
384  return Nektar::Cross(*this, rhs);
385  }
386 
387  template<typename DataType>
388  std::string NekVector<DataType>::AsString() const { return Nektar::AsString(*this); }
389 
390  // Norms
391  template<typename DataType>
392  DataType NekVector<DataType>::L1Norm() const { return Nektar::L1Norm(*this); }
393 
394  template<typename DataType>
395  DataType NekVector<DataType>::L2Norm() const { return Nektar::L2Norm(*this); }
396 
397  template<typename DataType>
398  DataType NekVector<DataType>::InfinityNorm() const { return Nektar::InfinityNorm(*this); }
399 
400  template<typename DataType>
401  PointerWrapper NekVector<DataType>::GetWrapperType() const { return m_wrapperType; }
402 
403  template<typename DataType>
405 
406  template<typename DataType>
407  void NekVector<DataType>::SetSize(unsigned int s) { m_size = s; }
408 
409  template<typename DataType>
411 
412  template<typename DataType>
413  void NekVector<DataType>::SetData(const Array<OneD, DataType>& newData) { m_data = newData; }
414 
415  template<typename DataType>
416  void NekVector<DataType>::Resize(unsigned int newSize)
417  {
418  if(m_data.num_elements() < newSize )
419  {
420  m_data = Array<OneD, DataType>(newSize);
421  }
422  m_size = newSize;
423  }
424 
426 
427  template<typename DataType>
428  void Add(NekVector<DataType>& result,
429  const NekVector<DataType>& lhs,
430  const NekVector<DataType>& rhs)
431  {
432  DataType* r_buf = result.GetRawPtr();
433  const DataType* lhs_buf = lhs.GetRawPtr();
434  const DataType* rhs_buf = rhs.GetRawPtr();
435  const unsigned int ldim = lhs.GetDimension();
436  for(int i = 0; i < ldim; ++i)
437  {
438  r_buf[i] = lhs_buf[i] + rhs_buf[i];
439  }
440  }
441 
442  template<typename DataType>
444  const NekVector<DataType>& lhs,
445  const NekVector<DataType>& rhs)
446  {
447  DataType* r_buf = result.GetRawPtr();
448  const DataType* lhs_buf = lhs.GetRawPtr();
449  const DataType* rhs_buf = rhs.GetRawPtr();
450  const unsigned int ldim = lhs.GetDimension();
451  for(int i = 0; i < ldim; ++i)
452  {
453  r_buf[i] = -lhs_buf[i] + rhs_buf[i];
454  }
455  }
456 
457  template LIB_UTILITIES_EXPORT void Add(NekVector<NekDouble>& result,
458  const NekVector<NekDouble>& lhs,
459  const NekVector<NekDouble>& rhs);
460  template LIB_UTILITIES_EXPORT void AddNegatedLhs(NekVector<NekDouble>& result,
461  const NekVector<NekDouble>& lhs,
462  const NekVector<NekDouble>& rhs);
463 
464  template<typename DataType>
466  const NekVector<DataType>& rhs)
467  {
468  DataType* r_buf = result.GetRawPtr();
469  const DataType* rhs_buf = rhs.GetRawPtr();
470  const unsigned int rdim = rhs.GetDimension();
471  for(int i = 0; i < rdim; ++i)
472  {
473  r_buf[i] += rhs_buf[i];
474  }
475  }
476 
477  template<typename DataType>
479  const NekVector<DataType>& rhs)
480  {
481  DataType* r_buf = result.GetRawPtr();
482  const DataType* rhs_buf = rhs.GetRawPtr();
483  const unsigned int rdim = rhs.GetDimension();
484  for(int i = 0; i < rdim; ++i)
485  {
486  r_buf[i] = -r_buf[i] + rhs_buf[i];
487  }
488  }
489 
490  template LIB_UTILITIES_EXPORT
491  void AddEqual(NekVector<NekDouble>& result,
492  const NekVector<NekDouble>& rhs);
493  template LIB_UTILITIES_EXPORT
494  void AddEqualNegatedLhs(NekVector<NekDouble>& result,
495  const NekVector<NekDouble>& rhs);
496 
497  template<typename LhsDataType,
498  typename RhsDataType>
500  const NekVector<RhsDataType>& rhs)
501  {
502  NekVector<LhsDataType> result(lhs.GetDimension());
503  Add(result, lhs, rhs);
504  return result;
505  }
506 
507  template LIB_UTILITIES_EXPORT
508  NekVector<NekDouble> Add(const NekVector<NekDouble>& lhs,
509  const NekVector<NekDouble>& rhs);
510 
511  template<typename ResultDataType, typename InputDataType>
513  const NekVector<InputDataType>& lhs,
514  const NekVector<InputDataType>& rhs)
515  {
516  ResultDataType* r_buf = result.GetRawPtr();
517  typename std::add_const<InputDataType>::type* lhs_buf = lhs.GetRawPtr();
518  typename std::add_const<InputDataType>::type* rhs_buf = rhs.GetRawPtr();
519  const unsigned int ldim = lhs.GetDimension();
520  for(int i = 0; i < ldim; ++i)
521  {
522  r_buf[i] = lhs_buf[i] - rhs_buf[i];
523  }
524  }
525 
526  template<typename ResultDataType, typename InputDataType>
528  const NekVector<InputDataType>& lhs,
529  const NekVector<InputDataType>& rhs)
530  {
531  ResultDataType* r_buf = result.GetRawPtr();
532  typename std::add_const<InputDataType>::type* lhs_buf = lhs.GetRawPtr();
533  typename std::add_const<InputDataType>::type* rhs_buf = rhs.GetRawPtr();
534  const unsigned int ldim = lhs.GetDimension();
535  for(int i = 0; i < ldim; ++i)
536  {
537  r_buf[i] = -lhs_buf[i] - rhs_buf[i];
538  }
539  }
540 
541  template LIB_UTILITIES_EXPORT
542  void Subtract(NekVector<NekDouble>& result,
543  const NekVector<NekDouble>& lhs,
544  const NekVector<NekDouble>& rhs);
545 
546  template LIB_UTILITIES_EXPORT
547  void SubtractNegatedLhs(NekVector<NekDouble>& result,
548  const NekVector<NekDouble>& lhs,
549  const NekVector<NekDouble>& rhs);
550 
551  template<typename ResultDataType, typename InputDataType>
553  const NekVector<InputDataType>& rhs)
554  {
555  ResultDataType* r_buf = result.GetRawPtr();
556  typename std::add_const<InputDataType>::type* rhs_buf = rhs.GetRawPtr();
557  const unsigned int rdim = rhs.GetDimension();
558  for(int i = 0; i < rdim; ++i)
559  {
560  r_buf[i] -= rhs_buf[i];
561  }
562  }
563 
564  template<typename ResultDataType, typename InputDataType>
566  const NekVector<InputDataType>& rhs)
567  {
568  ResultDataType* r_buf = result.GetRawPtr();
569  typename std::add_const<InputDataType>::type* rhs_buf = rhs.GetRawPtr();
570  const unsigned int rdim = rhs.GetDimension();
571  for(int i = 0; i < rdim; ++i)
572  {
573  r_buf[i] = -r_buf[i] - rhs_buf[i];
574  }
575  }
576 
577  template LIB_UTILITIES_EXPORT
578  void SubtractEqual(NekVector<NekDouble>& result,
579  const NekVector<NekDouble>& rhs);
580 
581  template LIB_UTILITIES_EXPORT
582  void SubtractEqualNegatedLhs(NekVector<NekDouble>& result,
583  const NekVector<NekDouble>& rhs);
584 
585  template<typename DataType>
588  const NekVector<DataType>& rhs)
589  {
590  NekVector<DataType> result(lhs.GetDimension());
591  Subtract(result, lhs, rhs);
592  return result;
593  }
594 
595  template LIB_UTILITIES_EXPORT
596  NekVector<NekDouble>
597  Subtract(const NekVector<NekDouble>& lhs,
598  const NekVector<NekDouble>& rhs);
599 
600  template<typename ResultDataType, typename InputDataType>
602  const NekVector<InputDataType>& lhs,
603  const NekDouble& rhs)
604  {
605  ResultDataType* r_buf = result.GetRawPtr();
606  typename std::add_const<InputDataType>::type* lhs_buf = lhs.GetRawPtr();
607 
608  const unsigned int ldim = lhs.GetDimension();
609  for(int i = 0; i < ldim; ++i)
610  {
611  r_buf[i] = lhs_buf[i] / rhs;
612  }
613  }
614 
615  template LIB_UTILITIES_EXPORT
616  void Divide(NekVector<NekDouble>& result,
617  const NekVector<NekDouble>& lhs,
618  const NekDouble& rhs);
619 
620  template<typename ResultDataType>
622  const NekDouble& rhs)
623  {
624  ResultDataType* r_buf = result.GetRawPtr();
625 
626  const unsigned int resdim = result.GetDimension();
627  for(int i = 0; i < resdim; ++i)
628  {
629  r_buf[i] /= rhs;
630  }
631  }
632 
633  template LIB_UTILITIES_EXPORT
634  void DivideEqual(NekVector<NekDouble>& result,
635  const NekDouble& rhs);
636 
637  template<typename DataType>
640  const NekDouble& rhs)
641  {
642  NekVector<DataType> result(lhs.GetDimension());
643  Divide(result, lhs, rhs);
644  return result;
645  }
646 
647  template LIB_UTILITIES_EXPORT
648  NekVector<NekDouble>
649  Divide(const NekVector<NekDouble>& lhs,
650  const NekDouble& rhs);
651 
652 
653  template<typename ResultDataType, typename InputDataType>
655  const NekVector<InputDataType>& lhs,
656  const NekVector<InputDataType>& rhs)
657  {
658  ResultDataType* result_buf = result.GetRawPtr();
659  const InputDataType* rhs_buf = rhs.GetRawPtr();
660  const InputDataType* lhs_buf = lhs.GetRawPtr();
661  const unsigned int resdim = result.GetDimension();
662  for(int i = 0; i < resdim; ++i)
663  {
664  result_buf[i] = lhs_buf[i] * rhs_buf[i];
665  }
666  }
667 
668  template LIB_UTILITIES_EXPORT
669  void Multiply(NekVector<NekDouble>& result, const NekVector<NekDouble>& lhs, const NekVector<NekDouble>& rhs);
670 
671  template<typename ResultDataType, typename InputDataType>
673  const NekVector<InputDataType>& rhs)
674  {
675  ResultDataType* result_buf = result.GetRawPtr();
676  const InputDataType* rhs_buf = rhs.GetRawPtr();
677  const unsigned int resdim = result.GetDimension();
678  for(int i = 0; i < resdim; ++i)
679  {
680  result_buf[i] *= rhs_buf[i];
681  }
682  }
683 
684  template LIB_UTILITIES_EXPORT
685  void MultiplyEqual(NekVector<NekDouble>& result, const NekVector<NekDouble>& rhs);
686 
687  template<typename DataType, typename InputDataType>
690  const NekVector<InputDataType>& rhs)
691  {
692  NekVector<DataType> result(lhs.GetDimension());
693  Multiply(result, lhs, rhs);
694  return result;
695  }
696 
697  template LIB_UTILITIES_EXPORT
698  NekVector<NekDouble>
699  Multiply(const NekVector<NekDouble>& lhs, const NekVector<NekDouble>& rhs);
700 
701 
702  template<typename ResultDataType, typename InputDataType>
704  const NekVector<InputDataType>& lhs,
705  const NekDouble& rhs)
706  {
707  ResultDataType* r_buf = result.GetRawPtr();
708  const InputDataType* lhs_buf = lhs.GetRawPtr();
709 
710  const unsigned int ldim = lhs.GetDimension();
711  for(int i = 0; i < ldim; ++i)
712  {
713  r_buf[i] = lhs_buf[i] * rhs;
714  }
715  }
716 
717  template LIB_UTILITIES_EXPORT
718  void Multiply(NekVector<NekDouble>& result,
719  const NekVector<NekDouble>& lhs,
720  const NekDouble& rhs);
721 
722  template<typename ResultDataType>
724  const NekDouble& rhs)
725  {
726  ResultDataType* r_buf = result.GetRawPtr();
727  const unsigned int rdim = result.GetDimension();
728  for(unsigned int i = 0; i < rdim; ++i)
729  {
730  r_buf[i] *= rhs;
731  }
732  }
733  template LIB_UTILITIES_EXPORT
734  void MultiplyEqual(NekVector<NekDouble>& result,
735  const NekDouble& rhs);
736 
737  template<typename DataType>
740  const NekDouble& rhs)
741  {
742  NekVector<DataType> result(lhs.GetDimension());
743  Multiply(result, lhs, rhs);
744  return result;
745  }
746 
747  template LIB_UTILITIES_EXPORT
748  NekVector<NekDouble>
749  Multiply(const NekVector<NekDouble>& lhs,
750  const NekDouble& rhs);
751 
752  template<typename ResultDataType, typename InputDataType>
754  const NekDouble& lhs,
755  const NekVector<InputDataType>& rhs)
756  {
757  Multiply(result, rhs, lhs);
758  }
759 
760  template<typename ResultDataType, typename InputDataType>
762  const NekDouble& lhs,
763  const NekVector<InputDataType>& rhs)
764  {
765  ResultDataType* r_buf = result.GetRawPtr();
766  const InputDataType* rhs_buf = rhs.GetRawPtr();
767  NekDouble inverse = 1.0/lhs;
768 
769  const unsigned int rdim = rhs.GetDimension();
770  for(int i = 0; i < rdim; ++i)
771  {
772  r_buf[i] = inverse * rhs_buf[i];
773  }
774  }
775 
776  template LIB_UTILITIES_EXPORT
777  void MultiplyInvertedLhs(NekVector<NekDouble>& result,
778  const NekDouble& lhs,
779  const NekVector<NekDouble>& rhs);
780 
781  template LIB_UTILITIES_EXPORT
782  void Multiply(NekVector<NekDouble>& result,
783  const NekDouble& lhs,
784  const NekVector<NekDouble>& rhs);
785 
786  template<typename DataType>
787  NekVector<DataType> Multiply(const DataType& lhs,
788  const NekVector<DataType>& rhs)
789  {
790  return Multiply(rhs, lhs);
791  }
792 
793  template LIB_UTILITIES_EXPORT
794  NekVector<NekDouble> Multiply(const NekDouble& lhs,
795  const NekVector<NekDouble>& rhs);
796 
797 
798  template<typename DataType>
799  std::ostream& operator<<(std::ostream& os, const NekVector<DataType>& rhs)
800  {
801  os << rhs.AsString();
802  return os;
803  }
804 
805  template LIB_UTILITIES_EXPORT
806  std::ostream& operator<<(std::ostream& os, const NekVector<NekDouble>& rhs);
807 
808  template<typename DataType>
810  const NekPoint<DataType>& dest)
811  {
812  NekVector<DataType> result(3, 0.0);
813  for(unsigned int i = 0; i < 3; ++i)
814  {
815  result[i] = dest[i]-source[i];
816  }
817  return result;
818  }
819 
820 
821  template LIB_UTILITIES_EXPORT
822  NekVector<NekDouble> createVectorFromPoints(const NekPoint<NekDouble>& source,
823  const NekPoint<NekDouble>& dest);
824 
825  template<typename DataType>
827  const DataType& t)
828  {
829  NekPoint<DataType> result;
830  for(unsigned int i = 0; i < 3; ++i)
831  {
832  result[i] = lhs[i]*t;
833  }
834 
835  return result;
836  }
837 
838  template LIB_UTILITIES_EXPORT
839  NekPoint<NekDouble> findPointAlongVector(const NekVector<NekDouble>& lhs,
840  const NekDouble& t);
841 
842  template<typename DataType>
844  const NekVector<DataType>& rhs)
845  {
846  if( lhs.GetDimension() != rhs.GetDimension() )
847  {
848  return false;
849  }
850 
851  return std::equal(lhs.begin(), lhs.end(), rhs.begin());
852  }
853 
854  template LIB_UTILITIES_EXPORT
855  bool operator==(const NekVector<NekDouble>& lhs,
856  const NekVector<NekDouble>& rhs);
857 
858  template<typename DataType>
860  const NekVector<DataType>& rhs)
861  {
862  return !(lhs == rhs);
863  }
864 
865  template LIB_UTILITIES_EXPORT
866  bool operator!=(const NekVector<NekDouble>& lhs,
867  const NekVector<NekDouble>& rhs);
868 
869  template<typename DataType>
870  std::vector<DataType> FromString(const std::string& str)
871  {
872  std::vector<DataType> result;
873 
874  try
875  {
876  typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
877  boost::char_separator<char> sep("(<,>) ");
878  tokenizer tokens(str, sep);
879  for( tokenizer::iterator strIter = tokens.begin(); strIter != tokens.end(); ++strIter)
880  {
881  result.push_back(boost::lexical_cast<DataType>(*strIter));
882  }
883  }
884  catch(boost::bad_lexical_cast&)
885  {
886  }
887 
888  return result;
889  }
890 
891  template LIB_UTILITIES_EXPORT
892  std::vector<NekDouble> FromString(const std::string& str);
893 
894  template<typename DataType>
895  DataType L1Norm(const NekVector<DataType>& v)
896  {
897  typedef NekVector<DataType> VectorType;
898 
899  DataType result(0);
900  for(typename VectorType::const_iterator iter = v.begin(); iter != v.end(); ++iter)
901  {
902  result += fabs(*iter);
903  }
904 
905  return result;
906  }
907 
908  template LIB_UTILITIES_EXPORT
909  NekDouble L1Norm(const NekVector<NekDouble>& v);
910 
911  template<typename DataType>
912  DataType L2Norm(const NekVector<DataType>& v)
913  {
914  typedef NekVector<DataType> VectorType;
915 
916  DataType result(0);
917  for(typename VectorType::const_iterator iter = v.begin(); iter != v.end(); ++iter)
918  {
919  DataType v = fabs(*iter);
920  result += v*v;
921  }
922  return sqrt(result);
923  }
924 
925  template LIB_UTILITIES_EXPORT
926  NekDouble L2Norm(const NekVector<NekDouble>& v);
927 
928  template<typename DataType>
930  {
931  DataType result = fabs(v[0]);
932  const unsigned int vdim = v.GetDimension();
933  for(unsigned int i = 0; i < vdim; ++i)
934  {
935  result = std::max(fabs(v[i]), result);
936  }
937  return result;
938  }
939 
940  template LIB_UTILITIES_EXPORT
941  NekDouble InfinityNorm(const NekVector<NekDouble>& v);
942 
943  template<typename DataType>
945  {
946  NekVector<DataType> temp(v);
947  const unsigned int tdim = temp.GetDimension();
948  for(unsigned int i = 0; i < tdim; ++i)
949  {
950  temp(i) = -temp(i);
951  }
952  return temp;
953  }
954 
955  template LIB_UTILITIES_EXPORT
956  NekVector<NekDouble> Negate(const NekVector<NekDouble>& v);
957 
958  template<typename DataType>
960  {
961  DataType* data = v.GetRawPtr();
962  const unsigned int vdim = v.GetDimension();
963  for(unsigned int i = 0; i < vdim; ++i)
964  {
965  data[i] = -data[i];
966  }
967  }
968 
969  template LIB_UTILITIES_EXPORT
970  void NegateInPlace(NekVector<NekDouble>& v);
971 
972  template<typename DataType>
973  DataType Magnitude(const NekVector<DataType>& v)
974  {
975  DataType result = DataType(0);
976 
977  const unsigned int vdim = v.GetDimension();
978  for(unsigned int i = 0; i < vdim; ++i)
979  {
980  result += v[i]*v[i];
981  }
982  return sqrt(result);
983  }
984 
985  template LIB_UTILITIES_EXPORT
986  NekDouble Magnitude(const NekVector<NekDouble>& v) ;
987 
988  template<typename DataType>
989  DataType Dot(const NekVector<DataType>& lhs,
990  const NekVector<DataType>& rhs)
991  {
992  ASSERTL1( lhs.GetDimension() == rhs.GetDimension(), "Dot, dimension of the two operands must be identical.");
993 
994  DataType result = DataType(0);
995  const unsigned int ldim = lhs.GetDimension();
996  for(unsigned int i = 0; i < ldim; ++i)
997  {
998  result += lhs[i]*rhs[i];
999  }
1000 
1001  return result;
1002  }
1003 
1004  template LIB_UTILITIES_EXPORT
1005  NekDouble Dot(const NekVector<NekDouble>& lhs,
1006  const NekVector<NekDouble>& rhs) ;
1007 
1008  template<typename DataType>
1010  {
1011  DataType m = v.Magnitude();
1012  if( m > DataType(0) )
1013  {
1014  v /= m;
1015  }
1016  }
1017 
1018  template LIB_UTILITIES_EXPORT
1019  void Normalize(NekVector<NekDouble>& v);
1020 
1021  void NegateInPlace(NekDouble& v) { v = -v; }
1022  void InvertInPlace(NekDouble& v) { v = 1.0/v; }
1023 
1024  template<typename DataType>
1026  const NekVector<DataType>& rhs)
1027  {
1028  ASSERTL1(lhs.GetDimension() == 3 && rhs.GetDimension() == 3, "Cross is only valid for 3D vectors.");
1029 
1030  DataType first = lhs.y()*rhs.z() - lhs.z()*rhs.y();
1031  DataType second = lhs.z()*rhs.x() - lhs.x()*rhs.z();
1032  DataType third = lhs.x()*rhs.y() - lhs.y()*rhs.x();
1033 
1034  NekVector<DataType> result(first, second, third);
1035  return result;
1036  }
1037 
1038  template LIB_UTILITIES_EXPORT
1039  NekVector<NekDouble> Cross(const NekVector<NekDouble>& lhs, const NekVector<NekDouble>& rhs);
1040 
1041  template<typename DataType>
1042  std::string AsString(const NekVector<DataType>& v)
1043  {
1044  unsigned int d = v.GetRows();
1045  std::string result = "(";
1046  for(unsigned int i = 0; i < d; ++i)
1047  {
1048  result += boost::lexical_cast<std::string>(v[i]);
1049  if( i < v.GetDimension()-1 )
1050  {
1051  result += ", ";
1052  }
1053  }
1054  result += ")";
1055  return result;
1056  }
1057 
1058  template LIB_UTILITIES_EXPORT
1059  std::string AsString(const NekVector<NekDouble>& v);
1060 }
1061 
NekVector()
Creates an empty vector.
Definition: NekVector.cpp:41
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
iterator begin()
Definition: NekVector.cpp:239
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
Definition: ErrorUtil.hpp:209
template NekVector< NekDouble > Subtract(const NekVector< NekDouble > &lhs, const NekVector< NekDouble > &rhs)
template void SubtractEqual(NekVector< NekDouble > &result, const NekVector< NekDouble > &rhs)
boost::call_traits< DataType >::reference x()
Definition: NekVector.cpp:265
template void Normalize(NekVector< NekDouble > &v)
template std::string AsString(const NekVector< NekDouble > &v)
template void SubtractNegatedLhs(NekVector< NekDouble > &result, const NekVector< NekDouble > &lhs, const NekVector< NekDouble > &rhs)
template NekDouble InfinityNorm(const NekVector< NekDouble > &v)
template NekVector< NekDouble > Add(const NekVector< NekDouble > &lhs, const NekVector< NekDouble > &rhs)
DataType L1Norm(const NekVector< DataType > &v)
Definition: NekVector.cpp:895
NekVector< DataType > Cross(const NekVector< DataType > &lhs, const NekVector< DataType > &rhs)
Definition: NekVector.cpp:1025
void Normalize(NekVector< DataType > &v)
Definition: NekVector.cpp:1009
template void SubtractEqualNegatedLhs(NekVector< NekDouble > &result, const NekVector< NekDouble > &rhs)
DataType * iterator
Definition: NekVector.hpp:101
template bool operator!=(const NekVector< NekDouble > &lhs, const NekVector< NekDouble > &rhs)
template NekVector< NekDouble > Multiply(const NekDouble &lhs, const NekVector< NekDouble > &rhs)
template NekPoint< NekDouble > findPointAlongVector(const NekVector< NekDouble > &lhs, const NekDouble &t)
def copy(self)
Definition: pycml.py:2663
DataType Magnitude(const NekVector< DataType > &v)
Definition: NekVector.cpp:973
template NekVector< NekDouble > createVectorFromPoints(const NekPoint< NekDouble > &source, const NekPoint< NekDouble > &dest)
const DataType * const_iterator
Definition: NekVector.hpp:105
StandardMatrixTag & lhs
DataType L2Norm(const NekVector< DataType > &v)
Definition: NekVector.cpp:912
std::string AsString(const NekVector< DataType > &v)
Definition: NekVector.cpp:1042
DataType * GetRawPtr()
Definition: NekVector.cpp:221
template NekDouble Magnitude(const NekVector< NekDouble > &v)
DataType InfinityNorm(const NekVector< DataType > &v)
Definition: NekVector.cpp:929
DataType Dot(const NekVector< DataType > &lhs, const NekVector< DataType > &rhs)
Definition: NekVector.cpp:989
std::string AsString() const
Definition: NekVector.cpp:388
template void DivideEqual(NekVector< NekDouble > &result, const NekDouble &rhs)
template void MultiplyInvertedLhs(NekVector< NekDouble > &result, const NekDouble &lhs, const NekVector< NekDouble > &rhs)
template NekVector< NekDouble > Negate(const NekVector< NekDouble > &v)
#define LIB_UTILITIES_EXPORT
template void AddEqual(NekVector< NekDouble > &result, const NekVector< NekDouble > &rhs)
std::vector< DataType > FromString(const std::string &str)
Definition: NekVector.cpp:870
double NekDouble
void CopyArray(const Array< OneD, ConstDataType > &source, Array< OneD, DataType > &dest)
DataType Magnitude() const
Definition: NekVector.cpp:376
PointerWrapper
Specifies if the pointer passed to a NekMatrix or NekVector is copied into an internal representation...
NekMatrix< typename NekMatrix< LhsDataType, LhsMatrixType >::NumberType, StandardMatrixTag > operator-(const NekMatrix< LhsDataType, LhsMatrixType > &lhs, const NekMatrix< RhsDataType, RhsMatrixType > &rhs)
template NekDouble L1Norm(const NekVector< NekDouble > &v)
void NegateInPlace(NekDouble &v)
Definition: NekVector.cpp:1021
boost::call_traits< DataType >::reference z()
Definition: NekVector.cpp:279
iterator end()
Definition: NekVector.cpp:242
template NekVector< NekDouble > Divide(const NekVector< NekDouble > &lhs, const NekDouble &rhs)
template NekDouble L2Norm(const NekVector< NekDouble > &v)
unsigned int GetDimension() const
Returns the number of dimensions for the point.
Definition: NekVector.cpp:209
boost::call_traits< DataType >::reference y()
Definition: NekVector.cpp:272
template void AddEqualNegatedLhs(NekVector< NekDouble > &result, const NekVector< NekDouble > &rhs)
StandardMatrixTag boost::call_traits< LhsDataType >::const_reference rhs
Array< OneD, const DataType >::const_iterator begin() const
unsigned int GetRows() const
Definition: NekVector.cpp:215
size_t num_elements() const
Returns the array&#39;s size.
void InvertInPlace(NekDouble &v)
Definition: NekVector.cpp:1022
template NekVector< NekDouble > Cross(const NekVector< NekDouble > &lhs, const NekVector< NekDouble > &rhs)
1D Array of constant elements with garbage collection and bounds checking.
Definition: SharedArray.hpp:57
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode...
Definition: ErrorUtil.hpp:250
bool operator==(const VertexSharedPtr &v1, const VertexSharedPtr &v2)
Define comparison operator for the vertex struct.
Definition: VtkToFld.cpp:55
template void MultiplyEqual(NekVector< NekDouble > &result, const NekDouble &rhs)
template void AddNegatedLhs(NekVector< NekDouble > &result, const NekVector< NekDouble > &lhs, const NekVector< NekDouble > &rhs)
template NekDouble Dot(const NekVector< NekDouble > &lhs, const NekVector< NekDouble > &rhs)