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();
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  {
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.size()),
123  m_data(ptr),
124  m_wrapperType(h)
125  {
126  if( h == eCopy )
127  {
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.size(), "Attempting to populate a vector of size " +
142  std::to_string(size) + " but the incoming array only has " +
143  std::to_string(ptr.size()) + " elements.");
144 
146  std::copy(ptr.begin(), ptr.begin()+size, m_data.begin());
147  }
148  }
149 
150  template<typename DataType>
152  m_size(ptr.size()),
153  m_data(ptr, eVECTOR_WRAPPER),
154  m_wrapperType(h)
155  {
156  if( h == eCopy )
157  {
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.size(), "Attempting to populate a vector of size " +
172  std::to_string(size) + " but the incoming array only has " +
173  std::to_string(ptr.size()) + " elements.");
174 
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.size() < newSize )
419  {
420  m_data = Array<OneD, DataType>(newSize);
421  }
422  m_size = newSize;
423  }
424 
427 
428  template<typename DataType>
429  void Add(NekVector<DataType>& result,
430  const NekVector<DataType>& lhs,
431  const NekVector<DataType>& rhs)
432  {
433  DataType* r_buf = result.GetRawPtr();
434  const DataType* lhs_buf = lhs.GetRawPtr();
435  const DataType* rhs_buf = rhs.GetRawPtr();
436  const unsigned int ldim = lhs.GetDimension();
437  for(int i = 0; i < ldim; ++i)
438  {
439  r_buf[i] = lhs_buf[i] + rhs_buf[i];
440  }
441  }
442 
443  template<typename DataType>
445  const NekVector<DataType>& lhs,
446  const NekVector<DataType>& rhs)
447  {
448  DataType* r_buf = result.GetRawPtr();
449  const DataType* lhs_buf = lhs.GetRawPtr();
450  const DataType* rhs_buf = rhs.GetRawPtr();
451  const unsigned int ldim = lhs.GetDimension();
452  for(int i = 0; i < ldim; ++i)
453  {
454  r_buf[i] = -lhs_buf[i] + rhs_buf[i];
455  }
456  }
457 
459  const NekVector<NekDouble>& lhs,
460  const NekVector<NekDouble>& rhs);
462  const NekVector<NekDouble>& lhs,
463  const NekVector<NekDouble>& rhs);
464 
466  const NekVector<NekSingle>& lhs,
467  const NekVector<NekSingle>& rhs);
469  const NekVector<NekSingle>& lhs,
470  const NekVector<NekSingle>& rhs);
471 
472  template<typename DataType>
474  const NekVector<DataType>& rhs)
475  {
476  DataType* r_buf = result.GetRawPtr();
477  const DataType* rhs_buf = rhs.GetRawPtr();
478  const unsigned int rdim = rhs.GetDimension();
479  for(int i = 0; i < rdim; ++i)
480  {
481  r_buf[i] += rhs_buf[i];
482  }
483  }
484 
485  template<typename DataType>
487  const NekVector<DataType>& rhs)
488  {
489  DataType* r_buf = result.GetRawPtr();
490  const DataType* rhs_buf = rhs.GetRawPtr();
491  const unsigned int rdim = rhs.GetDimension();
492  for(int i = 0; i < rdim; ++i)
493  {
494  r_buf[i] = -r_buf[i] + rhs_buf[i];
495  }
496  }
497 
498  template LIB_UTILITIES_EXPORT
500  const NekVector<NekDouble>& rhs);
501  template LIB_UTILITIES_EXPORT
503  const NekVector<NekDouble>& rhs);
504 
505  template LIB_UTILITIES_EXPORT
507  const NekVector<NekSingle>& rhs);
508  template LIB_UTILITIES_EXPORT
510  const NekVector<NekSingle>& rhs);
511 
512  template<typename LhsDataType,
513  typename RhsDataType>
515  const NekVector<RhsDataType>& rhs)
516  {
517  NekVector<LhsDataType> result(lhs.GetDimension());
518  Add(result, lhs, rhs);
519  return result;
520  }
521 
522  template LIB_UTILITIES_EXPORT
524  const NekVector<NekDouble>& rhs);
525 
526  template LIB_UTILITIES_EXPORT
528  const NekVector<NekSingle>& rhs);
529 
530  template<typename ResultDataType, typename InputDataType>
532  const NekVector<InputDataType>& lhs,
533  const NekVector<InputDataType>& rhs)
534  {
535  ResultDataType* r_buf = result.GetRawPtr();
536  typename std::add_const<InputDataType>::type* lhs_buf = lhs.GetRawPtr();
537  typename std::add_const<InputDataType>::type* rhs_buf = rhs.GetRawPtr();
538  const unsigned int ldim = lhs.GetDimension();
539  for(int i = 0; i < ldim; ++i)
540  {
541  r_buf[i] = lhs_buf[i] - rhs_buf[i];
542  }
543  }
544 
545  template<typename ResultDataType, typename InputDataType>
547  const NekVector<InputDataType>& lhs,
548  const NekVector<InputDataType>& rhs)
549  {
550  ResultDataType* r_buf = result.GetRawPtr();
551  typename std::add_const<InputDataType>::type* lhs_buf = lhs.GetRawPtr();
552  typename std::add_const<InputDataType>::type* rhs_buf = rhs.GetRawPtr();
553  const unsigned int ldim = lhs.GetDimension();
554  for(int i = 0; i < ldim; ++i)
555  {
556  r_buf[i] = -lhs_buf[i] - rhs_buf[i];
557  }
558  }
559 
560  template LIB_UTILITIES_EXPORT
562  const NekVector<NekDouble>& lhs,
563  const NekVector<NekDouble>& rhs);
564 
565  template LIB_UTILITIES_EXPORT
567  const NekVector<NekDouble>& lhs,
568  const NekVector<NekDouble>& rhs);
569 
570  template LIB_UTILITIES_EXPORT
572  const NekVector<NekSingle>& lhs,
573  const NekVector<NekSingle>& rhs);
574 
575  template LIB_UTILITIES_EXPORT
577  const NekVector<NekSingle>& lhs,
578  const NekVector<NekSingle>& rhs);
579 
580  template<typename ResultDataType, typename InputDataType>
582  const NekVector<InputDataType>& rhs)
583  {
584  ResultDataType* r_buf = result.GetRawPtr();
585  typename std::add_const<InputDataType>::type* rhs_buf = rhs.GetRawPtr();
586  const unsigned int rdim = rhs.GetDimension();
587  for(int i = 0; i < rdim; ++i)
588  {
589  r_buf[i] -= rhs_buf[i];
590  }
591  }
592 
593  template<typename ResultDataType, typename InputDataType>
595  const NekVector<InputDataType>& rhs)
596  {
597  ResultDataType* r_buf = result.GetRawPtr();
598  typename std::add_const<InputDataType>::type* rhs_buf = rhs.GetRawPtr();
599  const unsigned int rdim = rhs.GetDimension();
600  for(int i = 0; i < rdim; ++i)
601  {
602  r_buf[i] = -r_buf[i] - rhs_buf[i];
603  }
604  }
605 
606  template LIB_UTILITIES_EXPORT
608  const NekVector<NekDouble>& rhs);
609 
610  template LIB_UTILITIES_EXPORT
612  const NekVector<NekDouble>& rhs);
613 
614  template LIB_UTILITIES_EXPORT
616  const NekVector<NekSingle>& rhs);
617 
618  template LIB_UTILITIES_EXPORT
620  const NekVector<NekSingle>& rhs);
621 
622  template<typename DataType>
625  const NekVector<DataType>& rhs)
626  {
627  NekVector<DataType> result(lhs.GetDimension());
628  Subtract(result, lhs, rhs);
629  return result;
630  }
631 
632  template LIB_UTILITIES_EXPORT
633  NekVector<NekDouble>
635  const NekVector<NekDouble>& rhs);
636 
637  template LIB_UTILITIES_EXPORT
640  const NekVector<NekSingle>& rhs);
641 
642  template<typename ResultDataType, typename InputDataType>
644  const NekVector<InputDataType>& lhs,
645  const NekDouble& rhs)
646  {
647  ResultDataType* r_buf = result.GetRawPtr();
648  typename std::add_const<InputDataType>::type* lhs_buf = lhs.GetRawPtr();
649 
650  const unsigned int ldim = lhs.GetDimension();
651  for(int i = 0; i < ldim; ++i)
652  {
653  r_buf[i] = lhs_buf[i] / rhs;
654  }
655  }
656 
657  template<typename ResultDataType, typename InputDataType>
659  const NekVector<InputDataType>& lhs,
660  const NekSingle& rhs)
661  {
662  ResultDataType* r_buf = result.GetRawPtr();
663  typename std::add_const<InputDataType>::type* lhs_buf = lhs.GetRawPtr();
664 
665  const unsigned int ldim = lhs.GetDimension();
666  for(int i = 0; i < ldim; ++i)
667  {
668  r_buf[i] = lhs_buf[i] / rhs;
669  }
670  }
671 
672  template LIB_UTILITIES_EXPORT
674  const NekVector<NekDouble>& lhs,
675  const NekDouble& rhs);
676 
677  template LIB_UTILITIES_EXPORT
679  const NekVector<NekSingle>& lhs,
680  const NekSingle& rhs);
681 
682  template<typename ResultDataType>
684  const NekDouble& rhs)
685  {
686  ResultDataType* r_buf = result.GetRawPtr();
687 
688  const unsigned int resdim = result.GetDimension();
689  for(int i = 0; i < resdim; ++i)
690  {
691  r_buf[i] /= rhs;
692  }
693  }
694 
695  template<typename ResultDataType>
697  const NekSingle& rhs)
698  {
699  ResultDataType* r_buf = result.GetRawPtr();
700 
701  const unsigned int resdim = result.GetDimension();
702  for(int i = 0; i < resdim; ++i)
703  {
704  r_buf[i] /= rhs;
705  }
706  }
707 
708  template LIB_UTILITIES_EXPORT
710  const NekDouble& rhs);
711 
712  template LIB_UTILITIES_EXPORT
714  const NekSingle& rhs);
715 
716  template<typename DataType>
719  const NekDouble& rhs)
720  {
721  NekVector<DataType> result(lhs.GetDimension());
722  Divide(result, lhs, rhs);
723  return result;
724  }
725 
726  template<typename DataType>
727  NekVector<DataType>
729  const NekSingle& rhs)
730  {
731  NekVector<DataType> result(lhs.GetDimension());
732  Divide(result, lhs, rhs);
733  return result;
734  }
735 
736  template LIB_UTILITIES_EXPORT
737  NekVector<NekDouble>
739  const NekDouble& rhs);
740 
741  template LIB_UTILITIES_EXPORT
744  const NekSingle& rhs);
745 
746  template<typename ResultDataType, typename InputDataType>
748  const NekVector<InputDataType>& lhs,
749  const NekVector<InputDataType>& rhs)
750  {
751  ResultDataType* result_buf = result.GetRawPtr();
752  const InputDataType* rhs_buf = rhs.GetRawPtr();
753  const InputDataType* lhs_buf = lhs.GetRawPtr();
754  const unsigned int resdim = result.GetDimension();
755  for(int i = 0; i < resdim; ++i)
756  {
757  result_buf[i] = lhs_buf[i] * rhs_buf[i];
758  }
759  }
760 
761  template LIB_UTILITIES_EXPORT
763 
764  template LIB_UTILITIES_EXPORT
766 
767  template<typename ResultDataType, typename InputDataType>
769  const NekVector<InputDataType>& rhs)
770  {
771  ResultDataType* result_buf = result.GetRawPtr();
772  const InputDataType* rhs_buf = rhs.GetRawPtr();
773  const unsigned int resdim = result.GetDimension();
774  for(int i = 0; i < resdim; ++i)
775  {
776  result_buf[i] *= rhs_buf[i];
777  }
778  }
779 
780  template LIB_UTILITIES_EXPORT
782 
783  template LIB_UTILITIES_EXPORT
785 
786  template<typename DataType, typename InputDataType>
789  const NekVector<InputDataType>& rhs)
790  {
791  NekVector<DataType> result(lhs.GetDimension());
792  Multiply(result, lhs, rhs);
793  return result;
794  }
795 
796  template LIB_UTILITIES_EXPORT
797  NekVector<NekDouble>
799 
800  template LIB_UTILITIES_EXPORT
803 
804 
805  template<typename ResultDataType, typename InputDataType>
807  const NekVector<InputDataType>& lhs,
808  const NekDouble& rhs)
809  {
810  ResultDataType* r_buf = result.GetRawPtr();
811  const InputDataType* lhs_buf = lhs.GetRawPtr();
812 
813  const unsigned int ldim = lhs.GetDimension();
814  for(int i = 0; i < ldim; ++i)
815  {
816  r_buf[i] = lhs_buf[i] * rhs;
817  }
818  }
819 
820  template<typename ResultDataType, typename InputDataType>
822  const NekVector<InputDataType>& lhs,
823  const NekSingle& rhs)
824  {
825  ResultDataType* r_buf = result.GetRawPtr();
826  const InputDataType* lhs_buf = lhs.GetRawPtr();
827 
828  const unsigned int ldim = lhs.GetDimension();
829  for(int i = 0; i < ldim; ++i)
830  {
831  r_buf[i] = lhs_buf[i] * rhs;
832  }
833  }
834 
835  template LIB_UTILITIES_EXPORT
837  const NekVector<NekDouble>& lhs,
838  const NekDouble& rhs);
839 
840  template LIB_UTILITIES_EXPORT
842  const NekVector<NekSingle>& lhs,
843  const NekSingle& rhs);
844 
845  template<typename ResultDataType>
847  const NekDouble& rhs)
848  {
849  ResultDataType* r_buf = result.GetRawPtr();
850  const unsigned int rdim = result.GetDimension();
851  for(unsigned int i = 0; i < rdim; ++i)
852  {
853  r_buf[i] *= rhs;
854  }
855  }
856 
857  template<typename ResultDataType>
859  const NekSingle& rhs)
860  {
861  ResultDataType* r_buf = result.GetRawPtr();
862  const unsigned int rdim = result.GetDimension();
863  for(unsigned int i = 0; i < rdim; ++i)
864  {
865  r_buf[i] *= rhs;
866  }
867  }
868  template LIB_UTILITIES_EXPORT
870  const NekDouble& rhs);
871 
872  template LIB_UTILITIES_EXPORT
874  const NekSingle& rhs);
875 
876  template<typename DataType>
879  const NekDouble& rhs)
880  {
881  NekVector<DataType> result(lhs.GetDimension());
882  Multiply(result, lhs, rhs);
883  return result;
884  }
885 
886  template<typename DataType>
887  NekVector<DataType>
889  const NekSingle& rhs)
890  {
891  NekVector<DataType> result(lhs.GetDimension());
892  Multiply(result, lhs, rhs);
893  return result;
894  }
895 
896  template LIB_UTILITIES_EXPORT
897  NekVector<NekDouble>
899  const NekDouble& rhs);
900 
901  template LIB_UTILITIES_EXPORT
904  const NekSingle& rhs);
905 
906  template<typename ResultDataType, typename InputDataType>
908  const NekDouble& lhs,
909  const NekVector<InputDataType>& rhs)
910  {
911  Multiply(result, rhs, lhs);
912  }
913 
914  template<typename ResultDataType, typename InputDataType>
916  const NekDouble& lhs,
917  const NekVector<InputDataType>& rhs)
918  {
919  ResultDataType* r_buf = result.GetRawPtr();
920  const InputDataType* rhs_buf = rhs.GetRawPtr();
921  NekDouble inverse = 1.0/lhs;
922 
923  const unsigned int rdim = rhs.GetDimension();
924  for(int i = 0; i < rdim; ++i)
925  {
926  r_buf[i] = inverse * rhs_buf[i];
927  }
928  }
929 
930  template<typename ResultDataType, typename InputDataType>
932  const NekSingle& lhs,
933  const NekVector<InputDataType>& rhs)
934  {
935  Multiply(result, rhs, lhs);
936  }
937 
938  template<typename ResultDataType, typename InputDataType>
940  const NekSingle& lhs,
941  const NekVector<InputDataType>& rhs)
942  {
943  ResultDataType* r_buf = result.GetRawPtr();
944  const InputDataType* rhs_buf = rhs.GetRawPtr();
945  NekSingle inverse = 1.0/lhs;
946 
947  const unsigned int rdim = rhs.GetDimension();
948  for(int i = 0; i < rdim; ++i)
949  {
950  r_buf[i] = inverse * rhs_buf[i];
951  }
952  }
953 
954  template LIB_UTILITIES_EXPORT
956  const NekDouble& lhs,
957  const NekVector<NekDouble>& rhs);
958 
959  template LIB_UTILITIES_EXPORT
961  const NekDouble& lhs,
962  const NekVector<NekDouble>& rhs);
963 
964  template LIB_UTILITIES_EXPORT
966  const NekSingle& lhs,
967  const NekVector<NekSingle>& rhs);
968 
969  template LIB_UTILITIES_EXPORT
971  const NekSingle& lhs,
972  const NekVector<NekSingle>& rhs);
973 
974  template<typename DataType>
975  NekVector<DataType> Multiply(const DataType& lhs,
976  const NekVector<DataType>& rhs)
977  {
978  return Multiply(rhs, lhs);
979  }
980 
981  template LIB_UTILITIES_EXPORT
983  const NekVector<NekDouble>& rhs);
984 
985  template LIB_UTILITIES_EXPORT
987  const NekVector<NekSingle>& rhs);
988 
989  template<typename DataType>
990  std::ostream& operator<<(std::ostream& os, const NekVector<DataType>& rhs)
991  {
992  os << rhs.AsString();
993  return os;
994  }
995 
996  template LIB_UTILITIES_EXPORT
997  std::ostream& operator<<(std::ostream& os, const NekVector<NekDouble>& rhs);
998 
999  template LIB_UTILITIES_EXPORT
1000  std::ostream& operator<<(std::ostream& os, const NekVector<NekSingle>& rhs);
1001 
1002  template<typename DataType>
1004  const NekPoint<DataType>& dest)
1005  {
1006  NekVector<DataType> result(3, 0.0);
1007  for(unsigned int i = 0; i < 3; ++i)
1008  {
1009  result[i] = dest[i]-source[i];
1010  }
1011  return result;
1012  }
1013 
1014 
1015  template LIB_UTILITIES_EXPORT
1017  const NekPoint<NekDouble>& dest);
1018 
1019  template LIB_UTILITIES_EXPORT
1021  const NekPoint<NekSingle>& dest);
1022 
1023  template<typename DataType>
1025  const DataType& t)
1026  {
1027  NekPoint<DataType> result;
1028  for(unsigned int i = 0; i < 3; ++i)
1029  {
1030  result[i] = lhs[i]*t;
1031  }
1032 
1033  return result;
1034  }
1035 
1036  template LIB_UTILITIES_EXPORT
1038  const NekDouble& t);
1039 
1040  template LIB_UTILITIES_EXPORT
1042  const NekSingle& t);
1043 
1044  template<typename DataType>
1046  const NekVector<DataType>& rhs)
1047  {
1048  if( lhs.GetDimension() != rhs.GetDimension() )
1049  {
1050  return false;
1051  }
1052 
1053  return std::equal(lhs.begin(), lhs.end(), rhs.begin());
1054  }
1055 
1056  template LIB_UTILITIES_EXPORT
1058  const NekVector<NekDouble>& rhs);
1059 
1060  template LIB_UTILITIES_EXPORT
1062  const NekVector<NekSingle>& rhs);
1063 
1064  template<typename DataType>
1066  const NekVector<DataType>& rhs)
1067  {
1068  return !(lhs == rhs);
1069  }
1070 
1071  template LIB_UTILITIES_EXPORT
1073  const NekVector<NekDouble>& rhs);
1074 
1075  template LIB_UTILITIES_EXPORT
1077  const NekVector<NekSingle>& rhs);
1078 
1079  template<typename DataType>
1080  std::vector<DataType> FromString(const std::string& str)
1081  {
1082  std::vector<DataType> result;
1083 
1084  try
1085  {
1086  typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
1087  boost::char_separator<char> sep("(<,>) ");
1088  tokenizer tokens(str, sep);
1089  for( tokenizer::iterator strIter = tokens.begin(); strIter != tokens.end(); ++strIter)
1090  {
1091  result.push_back(boost::lexical_cast<DataType>(*strIter));
1092  }
1093  }
1094  catch(boost::bad_lexical_cast&)
1095  {
1096  }
1097 
1098  return result;
1099  }
1100 
1101  template LIB_UTILITIES_EXPORT
1102  std::vector<NekDouble> FromString(const std::string& str);
1103 
1104  template LIB_UTILITIES_EXPORT
1105  std::vector<NekSingle> FromString(const std::string& str);
1106 
1107  template<typename DataType>
1108  DataType L1Norm(const NekVector<DataType>& v)
1109  {
1110  typedef NekVector<DataType> VectorType;
1111 
1112  DataType result(0);
1113  for(typename VectorType::const_iterator iter = v.begin(); iter != v.end(); ++iter)
1114  {
1115  result += fabs(*iter);
1116  }
1117 
1118  return result;
1119  }
1120 
1121  template LIB_UTILITIES_EXPORT
1123 
1124  template LIB_UTILITIES_EXPORT
1126 
1127  template<typename DataType>
1128  DataType L2Norm(const NekVector<DataType>& v)
1129  {
1130  typedef NekVector<DataType> VectorType;
1131 
1132  DataType result(0);
1133  for(typename VectorType::const_iterator iter = v.begin(); iter != v.end(); ++iter)
1134  {
1135  DataType v = fabs(*iter);
1136  result += v*v;
1137  }
1138  return sqrt(result);
1139  }
1140 
1141  template LIB_UTILITIES_EXPORT
1143 
1144  template LIB_UTILITIES_EXPORT
1146 
1147  template<typename DataType>
1149  {
1150  DataType result = fabs(v[0]);
1151  const unsigned int vdim = v.GetDimension();
1152  for(unsigned int i = 0; i < vdim; ++i)
1153  {
1154  result = std::max(DataType(fabs(v[i])), result);
1155  }
1156  return result;
1157  }
1158 
1159  template LIB_UTILITIES_EXPORT
1161 
1162  template LIB_UTILITIES_EXPORT
1164 
1165  template<typename DataType>
1167  {
1168  NekVector<DataType> temp(v);
1169  const unsigned int tdim = temp.GetDimension();
1170  for(unsigned int i = 0; i < tdim; ++i)
1171  {
1172  temp(i) = -temp(i);
1173  }
1174  return temp;
1175  }
1176 
1177  template LIB_UTILITIES_EXPORT
1179 
1180  template LIB_UTILITIES_EXPORT
1182 
1183  template<typename DataType>
1185  {
1186  DataType* data = v.GetRawPtr();
1187  const unsigned int vdim = v.GetDimension();
1188  for(unsigned int i = 0; i < vdim; ++i)
1189  {
1190  data[i] = -data[i];
1191  }
1192  }
1193 
1194  template LIB_UTILITIES_EXPORT
1196 
1197  template LIB_UTILITIES_EXPORT
1199 
1200  template<typename DataType>
1201  DataType Magnitude(const NekVector<DataType>& v)
1202  {
1203  DataType result = DataType(0);
1204 
1205  const unsigned int vdim = v.GetDimension();
1206  for(unsigned int i = 0; i < vdim; ++i)
1207  {
1208  result += v[i]*v[i];
1209  }
1210  return sqrt(result);
1211  }
1212 
1213  template LIB_UTILITIES_EXPORT
1215 
1216  template LIB_UTILITIES_EXPORT
1218 
1219  template<typename DataType>
1220  DataType Dot(const NekVector<DataType>& lhs,
1221  const NekVector<DataType>& rhs)
1222  {
1223  ASSERTL1( lhs.GetDimension() == rhs.GetDimension(), "Dot, dimension of the two operands must be identical.");
1224 
1225  DataType result = DataType(0);
1226  const unsigned int ldim = lhs.GetDimension();
1227  for(unsigned int i = 0; i < ldim; ++i)
1228  {
1229  result += lhs[i]*rhs[i];
1230  }
1231 
1232  return result;
1233  }
1234 
1235  template LIB_UTILITIES_EXPORT
1237  const NekVector<NekDouble>& rhs) ;
1238 
1239  template LIB_UTILITIES_EXPORT
1241  const NekVector<NekSingle>& rhs) ;
1242 
1243  template<typename DataType>
1245  {
1246  DataType m = v.Magnitude();
1247  if( m > DataType(0) )
1248  {
1249  v /= m;
1250  }
1251  }
1252 
1253  template LIB_UTILITIES_EXPORT
1255 
1256  void NegateInPlace(NekDouble& v) { v = -v; }
1257  void InvertInPlace(NekDouble& v) { v = 1.0/v; }
1258 
1259  template LIB_UTILITIES_EXPORT
1261 
1262  void NegateInPlace(NekSingle& v) { v = -v; }
1263  void InvertInPlace(NekSingle& v) { v = 1.0/v; }
1264 
1265  template<typename DataType>
1267  const NekVector<DataType>& rhs)
1268  {
1269  ASSERTL1(lhs.GetDimension() == 3 && rhs.GetDimension() == 3, "Cross is only valid for 3D vectors.");
1270 
1271  DataType first = lhs.y()*rhs.z() - lhs.z()*rhs.y();
1272  DataType second = lhs.z()*rhs.x() - lhs.x()*rhs.z();
1273  DataType third = lhs.x()*rhs.y() - lhs.y()*rhs.x();
1274 
1275  NekVector<DataType> result(first, second, third);
1276  return result;
1277  }
1278 
1279  template LIB_UTILITIES_EXPORT
1281 
1282  template LIB_UTILITIES_EXPORT
1284 
1285  template<typename DataType>
1286  std::string AsString(const NekVector<DataType>& v)
1287  {
1288  unsigned int d = v.GetRows();
1289  std::string result = "(";
1290  for(unsigned int i = 0; i < d; ++i)
1291  {
1292  result += boost::lexical_cast<std::string>(v[i]);
1293  if( i < v.GetDimension()-1 )
1294  {
1295  result += ", ";
1296  }
1297  }
1298  result += ")";
1299  return result;
1300  }
1301 
1302  template LIB_UTILITIES_EXPORT
1303  std::string AsString(const NekVector<NekDouble>& v);
1304 
1305  template LIB_UTILITIES_EXPORT
1306  std::string AsString(const NekVector<NekSingle>& v);
1307 }
1308 
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:209
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:250
#define LIB_UTILITIES_EXPORT
Array< OneD, const DataType >::const_iterator begin() const
1D Array of constant elements with garbage collection and bounds checking.
Definition: SharedArray.hpp:59
size_type size() const
Returns the array's size.
boost::call_traits< DataType >::reference x()
Definition: NekVector.cpp:265
unsigned int m_size
Definition: NekVector.hpp:183
void SetZ(typename boost::call_traits< DataType >::const_reference val)
Definition: NekVector.cpp:300
void SetWrapperType(PointerWrapper p)
Definition: NekVector.cpp:410
boost::call_traits< DataType >::reference operator[](unsigned int i)
Definition: NekVector.cpp:259
DataType L1Norm() const
Definition: NekVector.cpp:392
Array< OneD, DataType > & GetData()
Definition: NekVector.cpp:404
NekVector< DataType > Cross(const NekVector< DataType > &rhs) const
Definition: NekVector.cpp:382
Array< OneD, DataType > & GetPtr()
Definition: NekVector.cpp:227
unsigned int GetDimension() const
Returns the number of dimensions for the point.
Definition: NekVector.cpp:209
NekVector< DataType > & operator+=(const NekVector< DataType > &rhs)
Definition: NekVector.cpp:307
NekVector< DataType > & operator=(const NekVector< DataType > &rhs)
Definition: NekVector.cpp:184
PointerWrapper GetWrapperType() const
Definition: NekVector.cpp:401
void SetY(typename boost::call_traits< DataType >::const_reference val)
Definition: NekVector.cpp:293
Array< OneD, DataType > m_data
Definition: NekVector.hpp:184
void Resize(unsigned int newSize)
Definition: NekVector.cpp:416
PointerWrapper m_wrapperType
Definition: NekVector.hpp:185
DataType Dot(const NekVector< DataType > &rhs) const
Definition: NekVector.cpp:379
unsigned int GetRows() const
Definition: NekVector.cpp:215
std::string AsString() const
Definition: NekVector.cpp:388
void SetSize(unsigned int s)
Definition: NekVector.cpp:407
NekVector< DataType > & operator/=(typename boost::call_traits< DataType >::const_reference rhs)
Definition: NekVector.cpp:328
boost::call_traits< DataType >::reference y()
Definition: NekVector.cpp:272
iterator begin()
Definition: NekVector.cpp:239
DataType * iterator
Definition: NekVector.hpp:101
iterator end()
Definition: NekVector.cpp:242
NekVector< DataType > & operator*=(typename boost::call_traits< DataType >::const_reference rhs)
Definition: NekVector.cpp:321
const DataType * const_iterator
Definition: NekVector.hpp:105
DataType InfinityNorm() const
Definition: NekVector.cpp:398
DataType L2Norm() const
Definition: NekVector.cpp:395
boost::call_traits< DataType >::reference operator()(unsigned int i)
Returns i^{th} element.
Definition: NekVector.cpp:251
DataType Magnitude() const
Definition: NekVector.cpp:376
NekVector< DataType > & operator-=(const NekVector< DataType > &rhs)
Definition: NekVector.cpp:314
NekVector()
Creates an empty vector.
Definition: NekVector.cpp:41
DataType * GetRawPtr()
Definition: NekVector.cpp:221
void SetData(const Array< OneD, DataType > &newData)
Definition: NekVector.cpp:413
boost::call_traits< DataType >::reference z()
Definition: NekVector.cpp:279
NekVector< DataType > operator-() const
Definition: NekVector.cpp:373
void SetX(typename boost::call_traits< DataType >::const_reference val)
Definition: NekVector.cpp:286
def copy(self)
Definition: pycml.py:2663
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
void Normalize(NekVector< DataType > &v)
Definition: NekVector.cpp:1244
std::string AsString(const NekVector< DataType > &v)
Definition: NekVector.cpp:1286
bool operator==(const Array< OneD, T1 > &lhs, const Array< OneD, T2 > &rhs)
void InvertInPlace(NekDouble &v)
Definition: NekVector.cpp:1257
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:915
@ eVECTOR_WRAPPER
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:1148
void Divide(NekVector< ResultDataType > &result, const NekVector< InputDataType > &lhs, const NekDouble &rhs)
Definition: NekVector.cpp:643
NekVector< DataType > Negate(const NekVector< DataType > &v)
Definition: NekVector.cpp:1166
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:1080
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:683
NekVector< DataType > createVectorFromPoints(const NekPoint< DataType > &source, const NekPoint< DataType > &dest)
Definition: NekVector.cpp:1003
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:1184
DataType Magnitude(const NekVector< DataType > &v)
Definition: NekVector.cpp:1201
const NekSingle void MultiplyEqual(NekMatrix< LhsDataType, StandardMatrixTag > &lhs, typename boost::call_traits< LhsDataType >::const_reference rhs)
NekVector< DataType > Cross(const NekVector< DataType > &lhs, const NekVector< DataType > &rhs)
Definition: NekVector.cpp:1266
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:1128
DataType L1Norm(const NekVector< DataType > &v)
Definition: NekVector.cpp:1108
NekPoint< DataType > findPointAlongVector(const NekVector< DataType > &lhs, const DataType &t)
Definition: NekVector.cpp:1024
bool operator!=(const Array< OneD, T1 > &lhs, const Array< OneD, T2 > &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:1220
void CopyArray(const Array< OneD, ConstDataType > &source, Array< OneD, DataType > &dest)
scalarT< T > sqrt(scalarT< T > in)
Definition: scalar.hpp:267