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>
41 NekVector<DataType>::NekVector() : m_size(0), m_data(), m_wrapperType(eCopy)
42 {
43 }
44 
45 template <typename DataType>
47  : m_size(size), m_data(size), m_wrapperType(eCopy)
48 {
49 }
50 
51 template <typename DataType>
53  unsigned int size, typename boost::call_traits<DataType>::const_reference a)
54  : m_size(size), m_data(size), m_wrapperType(eCopy)
55 {
56  std::fill_n(m_data.get(), m_size, a);
57 }
58 
59 template <typename DataType>
60 NekVector<DataType>::NekVector(const std::string &vectorValues)
61  : m_size(0), m_data(), m_wrapperType(eCopy)
62 {
63  try
64  {
65  std::vector<DataType> values = FromString<DataType>(vectorValues);
66  m_size = values.size();
68  std::copy(values.begin(), values.end(), m_data.begin());
69 
70  ASSERTL0(m_size > 0, "Error converting string values to vector");
71  }
72  catch (std::runtime_error &e)
73  {
74  NEKERROR(ErrorUtil::efatal, e.what());
75  }
76 }
77 
78 template <typename DataType>
80  typename boost::call_traits<DataType>::const_reference x,
81  typename boost::call_traits<DataType>::const_reference y,
82  typename boost::call_traits<DataType>::const_reference z)
83  : m_size(3), m_data(m_size), m_wrapperType(eCopy)
84 {
85  m_data[0] = x;
86  m_data[1] = y;
87  m_data[2] = z;
88 }
89 
90 template <typename DataType>
92  : m_size(rhs.GetDimension()), m_data(rhs.m_data),
93  m_wrapperType(rhs.m_wrapperType)
94 {
95  if (m_wrapperType == eCopy)
96  {
97  m_data = Array<OneD, DataType>(m_size);
98  std::copy(rhs.begin(), rhs.end(), m_data.get());
99  }
100 }
101 
102 template <typename DataType>
103 NekVector<DataType>::NekVector(unsigned int size, const DataType *const ptr)
104  : m_size(size), m_data(size, ptr), m_wrapperType(eCopy)
105 {
106 }
107 
108 template <typename DataType>
111  : m_size(ptr.size()), m_data(ptr), m_wrapperType(h)
112 {
113  if (h == eCopy)
114  {
115  m_data = Array<OneD, DataType>(m_size);
116  CopyArray(ptr, m_data);
117  }
118 }
119 
120 template <typename DataType>
122  PointerWrapper h)
123  : m_size(size), m_data(ptr), m_wrapperType(h)
124 {
125  if (h == eCopy)
126  {
127  ASSERTL0(size <= ptr.size(),
128  "Attempting to populate a vector of size " +
129  std::to_string(size) +
130  " but the incoming array only has " +
131  std::to_string(ptr.size()) + " elements.");
132 
134  std::copy(ptr.begin(), ptr.begin() + size, m_data.begin());
135  }
136 }
137 
138 template <typename DataType>
140  PointerWrapper h)
141  : m_size(ptr.size()), m_data(ptr, eVECTOR_WRAPPER), m_wrapperType(h)
142 {
143  if (h == eCopy)
144  {
145  m_data = Array<OneD, DataType>(m_size);
146  CopyArray(ptr, m_data);
147  }
148 }
149 
150 template <typename DataType>
152  const Array<OneD, const DataType> &ptr,
154  : m_size(size), m_data(ptr, eVECTOR_WRAPPER), m_wrapperType(h)
155 {
156  if (h == eCopy)
157  {
158  ASSERTL0(size <= ptr.size(),
159  "Attempting to populate a vector of size " +
160  std::to_string(size) +
161  " but the incoming array only has " +
162  std::to_string(ptr.size()) + " elements.");
163 
164  m_data = Array<OneD, DataType>(size);
165  std::copy(ptr.begin(), ptr.begin() + size, m_data.begin());
166  }
167 }
168 
169 template <typename DataType> NekVector<DataType>::~NekVector()
170 {
171 }
172 
173 template <typename DataType>
175  const NekVector<DataType> &rhs)
176 {
177  if (m_wrapperType == eCopy)
178  {
179  // If the current vector is a copy, then regardless of the rhs type
180  // we just copy over the values, resizing if needed.
181  if (GetDimension() != rhs.GetDimension())
182  {
183  m_size = rhs.GetDimension();
184  m_data = Array<OneD, DataType>(m_size);
185  }
186  }
187  else if (m_wrapperType == eWrapper)
188  {
189  // If the current vector is wrapped, then just copy over the top,
190  // but the sizes of the two vectors must be the same.
192  GetDimension() == rhs.GetDimension(),
193  "Wrapped NekVectors must have the same dimension in operator=");
194  }
195 
196  std::copy(rhs.begin(), rhs.end(), m_data.get());
197  return *this;
198 }
199 
200 template <typename DataType>
202 {
203  return m_size;
204 }
205 
206 template <typename DataType> unsigned int NekVector<DataType>::GetRows() const
207 {
208  return m_size;
209 }
210 
211 template <typename DataType> DataType *NekVector<DataType>::GetRawPtr()
212 {
213  return this->GetData().get();
214 }
215 
216 template <typename DataType>
218 {
219  return this->GetData();
220 }
221 
222 template <typename DataType>
223 const DataType *NekVector<DataType>::GetRawPtr() const
224 {
225  return m_data.get();
226 }
227 
228 template <typename DataType>
230 {
231  return m_data;
232 }
233 
234 template <typename DataType>
236 {
237  return GetRawPtr();
238 }
239 
240 template <typename DataType>
242 {
243  return GetRawPtr() + this->GetDimension();
244 }
245 
246 template <typename DataType>
248 {
249  return GetRawPtr();
250 }
251 
252 template <typename DataType>
254 {
255  return GetRawPtr() + GetDimension();
256 }
257 
258 template <typename DataType>
259 typename boost::call_traits<DataType>::reference NekVector<
260  DataType>::operator()(unsigned int i)
261 {
262  ASSERTL1(i < this->GetDimension(),
263  "Invalid access to m_data via parenthesis operator");
264  return this->GetData()[i];
265 }
266 
267 template <typename DataType>
268 typename boost::call_traits<DataType>::reference NekVector<
269  DataType>::operator[](unsigned int i)
270 {
271  return this->GetData()[i];
272 }
273 
274 template <typename DataType>
275 typename boost::call_traits<DataType>::reference NekVector<DataType>::x()
276 {
277  ASSERTL1(this->GetDimension() >= 1, "Invalid use of NekVector::x");
278  return (*this)(0);
279 }
280 
281 template <typename DataType>
282 typename boost::call_traits<DataType>::reference NekVector<DataType>::y()
283 {
284  ASSERTL1(this->GetDimension() >= 2, "Invalid use of NekVector::y");
285  return (*this)(1);
286 }
287 
288 template <typename DataType>
289 typename boost::call_traits<DataType>::reference NekVector<DataType>::z()
290 {
291  ASSERTL1(this->GetDimension() >= 3, "Invalid use of NekVector::z");
292  return (*this)(2);
293 }
294 
295 template <typename DataType>
297  typename boost::call_traits<DataType>::const_reference val)
298 {
299  ASSERTL1(this->GetDimension() >= 1, "Invalid use of NekVector::SetX");
300  this->GetData()[0] = val;
301 }
302 
303 template <typename DataType>
305  typename boost::call_traits<DataType>::const_reference val)
306 {
307  ASSERTL1(this->GetDimension() >= 2, "Invalid use of NekVector::SetX");
308  this->GetData()[1] = val;
309 }
310 
311 template <typename DataType>
313  typename boost::call_traits<DataType>::const_reference val)
314 {
315  ASSERTL1(this->GetDimension() >= 3, "Invalid use of NekVector::SetX");
316  this->GetData()[2] = val;
317 }
318 
319 template <typename DataType>
321  const NekVector<DataType> &rhs)
322 {
323  AddEqual(*this, rhs);
324  return *this;
325 }
326 
327 template <typename DataType>
329  const NekVector<DataType> &rhs)
330 {
331  SubtractEqual(*this, rhs);
332  return *this;
333 }
334 
335 template <typename DataType>
337  typename boost::call_traits<DataType>::const_reference rhs)
338 {
339  MultiplyEqual(*this, rhs);
340  return *this;
341 }
342 
343 template <typename DataType>
345  typename boost::call_traits<DataType>::const_reference rhs)
346 {
347  DivideEqual(*this, rhs);
348  return *this;
349 }
350 
351 template <typename DataType> void NekVector<DataType>::Normalize()
352 {
353  return Nektar::Normalize(*this);
354 }
355 
356 template <typename DataType>
357 typename boost::call_traits<DataType>::const_reference NekVector<
358  DataType>::operator()(unsigned int i) const
359 {
360  ASSERTL1(i < GetDimension(),
361  "Invalid access to m_data via parenthesis operator");
362  return m_data[i];
363 }
364 
365 template <typename DataType>
366 typename boost::call_traits<DataType>::const_reference NekVector<
367  DataType>::operator[](unsigned int i) const
368 {
369  return m_data[i];
370 }
371 
372 template <typename DataType>
373 typename boost::call_traits<DataType>::const_reference NekVector<DataType>::x()
374  const
375 {
376  ASSERTL1(GetDimension() >= 1, "Invalid use of NekVector::x");
377  return (*this)(0);
378 }
379 
380 template <typename DataType>
381 typename boost::call_traits<DataType>::const_reference NekVector<DataType>::y()
382  const
383 {
384  ASSERTL1(GetDimension() >= 2, "Invalid use of NekVector::y");
385  return (*this)(1);
386 }
387 
388 template <typename DataType>
389 typename boost::call_traits<DataType>::const_reference NekVector<DataType>::z()
390  const
391 {
392  ASSERTL1(GetDimension() >= 3, "Invalid use of NekVector::z");
393  return (*this)(2);
394 }
395 
396 template <typename DataType>
398 {
399  return Negate(*this);
400 }
401 
402 template <typename DataType> DataType NekVector<DataType>::Magnitude() const
403 {
404  return Nektar::Magnitude(*this);
405 }
406 
407 template <typename DataType>
409 {
410  return Nektar::Dot(*this, rhs);
411 }
412 
413 template <typename DataType>
415  const NekVector<DataType> &rhs) const
416 {
417  return Nektar::Cross(*this, rhs);
418 }
419 
420 template <typename DataType> std::string NekVector<DataType>::AsString() const
421 {
422  return Nektar::AsString(*this);
423 }
424 
425 // Norms
426 template <typename DataType> DataType NekVector<DataType>::L1Norm() const
427 {
428  return Nektar::L1Norm(*this);
429 }
430 
431 template <typename DataType> DataType NekVector<DataType>::L2Norm() const
432 {
433  return Nektar::L2Norm(*this);
434 }
435 
436 template <typename DataType> DataType NekVector<DataType>::InfinityNorm() const
437 {
438  return Nektar::InfinityNorm(*this);
439 }
440 
441 template <typename DataType>
443 {
444  return m_wrapperType;
445 }
446 
447 template <typename DataType>
449 {
450  return m_data;
451 }
452 
453 template <typename DataType> void NekVector<DataType>::SetSize(unsigned int s)
454 {
455  m_size = s;
456 }
457 
458 template <typename DataType>
460 {
461  m_wrapperType = p;
462 }
463 
464 template <typename DataType>
466 {
467  m_data = newData;
468 }
469 
470 template <typename DataType>
471 void NekVector<DataType>::Resize(unsigned int newSize)
472 {
473  if (m_data.size() < newSize)
474  {
475  m_data = Array<OneD, DataType>(newSize);
476  }
477  m_size = newSize;
478 }
479 
482 
483 template <typename DataType>
484 void Add(NekVector<DataType> &result, const NekVector<DataType> &lhs,
485  const NekVector<DataType> &rhs)
486 {
487  DataType *r_buf = result.GetRawPtr();
488  const DataType *lhs_buf = lhs.GetRawPtr();
489  const DataType *rhs_buf = rhs.GetRawPtr();
490  const unsigned int ldim = lhs.GetDimension();
491  for (int i = 0; i < ldim; ++i)
492  {
493  r_buf[i] = lhs_buf[i] + rhs_buf[i];
494  }
495 }
496 
497 template <typename DataType>
499  const NekVector<DataType> &rhs)
500 {
501  DataType *r_buf = result.GetRawPtr();
502  const DataType *lhs_buf = lhs.GetRawPtr();
503  const DataType *rhs_buf = rhs.GetRawPtr();
504  const unsigned int ldim = lhs.GetDimension();
505  for (int i = 0; i < ldim; ++i)
506  {
507  r_buf[i] = -lhs_buf[i] + rhs_buf[i];
508  }
509 }
510 
512  const NekVector<NekDouble> &lhs,
513  const NekVector<NekDouble> &rhs);
515  NekVector<NekDouble> &result, const NekVector<NekDouble> &lhs,
516  const NekVector<NekDouble> &rhs);
517 
519  const NekVector<NekSingle> &lhs,
520  const NekVector<NekSingle> &rhs);
522  NekVector<NekSingle> &result, const NekVector<NekSingle> &lhs,
523  const NekVector<NekSingle> &rhs);
524 
525 template <typename DataType>
527 {
528  DataType *r_buf = result.GetRawPtr();
529  const DataType *rhs_buf = rhs.GetRawPtr();
530  const unsigned int rdim = rhs.GetDimension();
531  for (int i = 0; i < rdim; ++i)
532  {
533  r_buf[i] += rhs_buf[i];
534  }
535 }
536 
537 template <typename DataType>
539  const NekVector<DataType> &rhs)
540 {
541  DataType *r_buf = result.GetRawPtr();
542  const DataType *rhs_buf = rhs.GetRawPtr();
543  const unsigned int rdim = rhs.GetDimension();
544  for (int i = 0; i < rdim; ++i)
545  {
546  r_buf[i] = -r_buf[i] + rhs_buf[i];
547  }
548 }
549 
551  const NekVector<NekDouble> &rhs);
553  NekVector<NekDouble> &result, const NekVector<NekDouble> &rhs);
554 
556  const NekVector<NekSingle> &rhs);
558  NekVector<NekSingle> &result, const NekVector<NekSingle> &rhs);
559 
560 template <typename LhsDataType, typename RhsDataType>
562  const NekVector<RhsDataType> &rhs)
563 {
564  NekVector<LhsDataType> result(lhs.GetDimension());
565  Add(result, lhs, rhs);
566  return result;
567 }
568 
570  const NekVector<NekDouble> &lhs, const NekVector<NekDouble> &rhs);
571 
573  const NekVector<NekSingle> &lhs, const NekVector<NekSingle> &rhs);
574 
575 template <typename ResultDataType, typename InputDataType>
577  const NekVector<InputDataType> &lhs,
578  const NekVector<InputDataType> &rhs)
579 {
580  ResultDataType *r_buf = result.GetRawPtr();
581  typename std::add_const<InputDataType>::type *lhs_buf = lhs.GetRawPtr();
582  typename std::add_const<InputDataType>::type *rhs_buf = rhs.GetRawPtr();
583  const unsigned int ldim = lhs.GetDimension();
584  for (int i = 0; i < ldim; ++i)
585  {
586  r_buf[i] = lhs_buf[i] - rhs_buf[i];
587  }
588 }
589 
590 template <typename ResultDataType, typename InputDataType>
592  const NekVector<InputDataType> &lhs,
593  const NekVector<InputDataType> &rhs)
594 {
595  ResultDataType *r_buf = result.GetRawPtr();
596  typename std::add_const<InputDataType>::type *lhs_buf = lhs.GetRawPtr();
597  typename std::add_const<InputDataType>::type *rhs_buf = rhs.GetRawPtr();
598  const unsigned int ldim = lhs.GetDimension();
599  for (int i = 0; i < ldim; ++i)
600  {
601  r_buf[i] = -lhs_buf[i] - rhs_buf[i];
602  }
603 }
604 
606  const NekVector<NekDouble> &lhs,
607  const NekVector<NekDouble> &rhs);
608 
610  NekVector<NekDouble> &result, const NekVector<NekDouble> &lhs,
611  const NekVector<NekDouble> &rhs);
612 
614  const NekVector<NekSingle> &lhs,
615  const NekVector<NekSingle> &rhs);
616 
618  NekVector<NekSingle> &result, const NekVector<NekSingle> &lhs,
619  const NekVector<NekSingle> &rhs);
620 
621 template <typename ResultDataType, typename InputDataType>
623  const NekVector<InputDataType> &rhs)
624 {
625  ResultDataType *r_buf = result.GetRawPtr();
626  typename std::add_const<InputDataType>::type *rhs_buf = rhs.GetRawPtr();
627  const unsigned int rdim = rhs.GetDimension();
628  for (int i = 0; i < rdim; ++i)
629  {
630  r_buf[i] -= rhs_buf[i];
631  }
632 }
633 
634 template <typename ResultDataType, typename InputDataType>
636  const NekVector<InputDataType> &rhs)
637 {
638  ResultDataType *r_buf = result.GetRawPtr();
639  typename std::add_const<InputDataType>::type *rhs_buf = rhs.GetRawPtr();
640  const unsigned int rdim = rhs.GetDimension();
641  for (int i = 0; i < rdim; ++i)
642  {
643  r_buf[i] = -r_buf[i] - rhs_buf[i];
644  }
645 }
646 
648  NekVector<NekDouble> &result, const NekVector<NekDouble> &rhs);
649 
651  NekVector<NekDouble> &result, const NekVector<NekDouble> &rhs);
652 
654  NekVector<NekSingle> &result, const NekVector<NekSingle> &rhs);
655 
657  NekVector<NekSingle> &result, const NekVector<NekSingle> &rhs);
658 
659 template <typename DataType>
661  const NekVector<DataType> &rhs)
662 {
663  NekVector<DataType> result(lhs.GetDimension());
664  Subtract(result, lhs, rhs);
665  return result;
666 }
667 
669  const NekVector<NekDouble> &lhs, const NekVector<NekDouble> &rhs);
670 
672  const NekVector<NekSingle> &lhs, const NekVector<NekSingle> &rhs);
673 
674 template <typename ResultDataType, typename InputDataType>
676  const NekVector<InputDataType> &lhs, const NekDouble &rhs)
677 {
678  ResultDataType *r_buf = result.GetRawPtr();
679  typename std::add_const<InputDataType>::type *lhs_buf = lhs.GetRawPtr();
680 
681  const unsigned int ldim = lhs.GetDimension();
682  for (int i = 0; i < ldim; ++i)
683  {
684  r_buf[i] = lhs_buf[i] / rhs;
685  }
686 }
687 
688 template <typename ResultDataType, typename InputDataType>
690  const NekVector<InputDataType> &lhs, const NekSingle &rhs)
691 {
692  ResultDataType *r_buf = result.GetRawPtr();
693  typename std::add_const<InputDataType>::type *lhs_buf = lhs.GetRawPtr();
694 
695  const unsigned int ldim = lhs.GetDimension();
696  for (int i = 0; i < ldim; ++i)
697  {
698  r_buf[i] = lhs_buf[i] / rhs;
699  }
700 }
701 
703  const NekVector<NekDouble> &lhs,
704  const NekDouble &rhs);
705 
707  const NekVector<NekSingle> &lhs,
708  const NekSingle &rhs);
709 
710 template <typename ResultDataType>
712 {
713  ResultDataType *r_buf = result.GetRawPtr();
714 
715  const unsigned int resdim = result.GetDimension();
716  for (int i = 0; i < resdim; ++i)
717  {
718  r_buf[i] /= rhs;
719  }
720 }
721 
722 template <typename ResultDataType>
724 {
725  ResultDataType *r_buf = result.GetRawPtr();
726 
727  const unsigned int resdim = result.GetDimension();
728  for (int i = 0; i < resdim; ++i)
729  {
730  r_buf[i] /= rhs;
731  }
732 }
733 
735  const NekDouble &rhs);
736 
738  const NekSingle &rhs);
739 
740 template <typename DataType>
742 {
743  NekVector<DataType> result(lhs.GetDimension());
744  Divide(result, lhs, rhs);
745  return result;
746 }
747 
748 template <typename DataType>
750 {
751  NekVector<DataType> result(lhs.GetDimension());
752  Divide(result, lhs, rhs);
753  return result;
754 }
755 
757  const NekVector<NekDouble> &lhs, const NekDouble &rhs);
758 
760  const NekVector<NekSingle> &lhs, const NekSingle &rhs);
761 
762 template <typename ResultDataType, typename InputDataType>
764  const NekVector<InputDataType> &lhs,
765  const NekVector<InputDataType> &rhs)
766 {
767  ResultDataType *result_buf = result.GetRawPtr();
768  const InputDataType *rhs_buf = rhs.GetRawPtr();
769  const InputDataType *lhs_buf = lhs.GetRawPtr();
770  const unsigned int resdim = result.GetDimension();
771  for (int i = 0; i < resdim; ++i)
772  {
773  result_buf[i] = lhs_buf[i] * rhs_buf[i];
774  }
775 }
776 
778  const NekVector<NekDouble> &lhs,
779  const NekVector<NekDouble> &rhs);
780 
782  const NekVector<NekSingle> &lhs,
783  const NekVector<NekSingle> &rhs);
784 
785 template <typename ResultDataType, typename InputDataType>
787  const NekVector<InputDataType> &rhs)
788 {
789  ResultDataType *result_buf = result.GetRawPtr();
790  const InputDataType *rhs_buf = rhs.GetRawPtr();
791  const unsigned int resdim = result.GetDimension();
792  for (int i = 0; i < resdim; ++i)
793  {
794  result_buf[i] *= rhs_buf[i];
795  }
796 }
797 
799  NekVector<NekDouble> &result, const NekVector<NekDouble> &rhs);
800 
802  NekVector<NekSingle> &result, const NekVector<NekSingle> &rhs);
803 
804 template <typename DataType, typename InputDataType>
806  const NekVector<InputDataType> &rhs)
807 {
808  NekVector<DataType> result(lhs.GetDimension());
809  Multiply(result, lhs, rhs);
810  return result;
811 }
812 
814  const NekVector<NekDouble> &lhs, const NekVector<NekDouble> &rhs);
815 
817  const NekVector<NekSingle> &lhs, const NekVector<NekSingle> &rhs);
818 
819 template <typename ResultDataType, typename InputDataType>
821  const NekVector<InputDataType> &lhs, const NekDouble &rhs)
822 {
823  ResultDataType *r_buf = result.GetRawPtr();
824  const InputDataType *lhs_buf = lhs.GetRawPtr();
825 
826  const unsigned int ldim = lhs.GetDimension();
827  for (int i = 0; i < ldim; ++i)
828  {
829  r_buf[i] = lhs_buf[i] * rhs;
830  }
831 }
832 
833 template <typename ResultDataType, typename InputDataType>
835  const NekVector<InputDataType> &lhs, const NekSingle &rhs)
836 {
837  ResultDataType *r_buf = result.GetRawPtr();
838  const InputDataType *lhs_buf = lhs.GetRawPtr();
839 
840  const unsigned int ldim = lhs.GetDimension();
841  for (int i = 0; i < ldim; ++i)
842  {
843  r_buf[i] = lhs_buf[i] * rhs;
844  }
845 }
846 
848  const NekVector<NekDouble> &lhs,
849  const NekDouble &rhs);
850 
852  const NekVector<NekSingle> &lhs,
853  const NekSingle &rhs);
854 
855 template <typename ResultDataType>
857 {
858  ResultDataType *r_buf = result.GetRawPtr();
859  const unsigned int rdim = result.GetDimension();
860  for (unsigned int i = 0; i < rdim; ++i)
861  {
862  r_buf[i] *= rhs;
863  }
864 }
865 
866 template <typename ResultDataType>
868 {
869  ResultDataType *r_buf = result.GetRawPtr();
870  const unsigned int rdim = result.GetDimension();
871  for (unsigned int i = 0; i < rdim; ++i)
872  {
873  r_buf[i] *= rhs;
874  }
875 }
877  const NekDouble &rhs);
878 
880  const NekSingle &rhs);
881 
882 template <typename DataType>
884  const NekDouble &rhs)
885 {
886  NekVector<DataType> result(lhs.GetDimension());
887  Multiply(result, lhs, rhs);
888  return result;
889 }
890 
891 template <typename DataType>
893  const NekSingle &rhs)
894 {
895  NekVector<DataType> result(lhs.GetDimension());
896  Multiply(result, lhs, rhs);
897  return result;
898 }
899 
901  const NekVector<NekDouble> &lhs, const NekDouble &rhs);
902 
904  const NekVector<NekSingle> &lhs, const NekSingle &rhs);
905 
906 template <typename ResultDataType, typename InputDataType>
908  const NekVector<InputDataType> &rhs)
909 {
910  Multiply(result, rhs, lhs);
911 }
912 
913 template <typename ResultDataType, typename InputDataType>
915  const NekDouble &lhs,
916  const NekVector<InputDataType> &rhs)
917 {
918  ResultDataType *r_buf = result.GetRawPtr();
919  const InputDataType *rhs_buf = rhs.GetRawPtr();
920  NekDouble inverse = 1.0 / lhs;
921 
922  const unsigned int rdim = rhs.GetDimension();
923  for (int i = 0; i < rdim; ++i)
924  {
925  r_buf[i] = inverse * rhs_buf[i];
926  }
927 }
928 
929 template <typename ResultDataType, typename InputDataType>
931  const NekVector<InputDataType> &rhs)
932 {
933  Multiply(result, rhs, lhs);
934 }
935 
936 template <typename ResultDataType, typename InputDataType>
938  const NekSingle &lhs,
939  const NekVector<InputDataType> &rhs)
940 {
941  ResultDataType *r_buf = result.GetRawPtr();
942  const InputDataType *rhs_buf = rhs.GetRawPtr();
943  NekSingle inverse = 1.0 / lhs;
944 
945  const unsigned int rdim = rhs.GetDimension();
946  for (int i = 0; i < rdim; ++i)
947  {
948  r_buf[i] = inverse * rhs_buf[i];
949  }
950 }
951 
953  NekVector<NekDouble> &result, const NekDouble &lhs,
954  const NekVector<NekDouble> &rhs);
955 
957  const NekDouble &lhs,
958  const NekVector<NekDouble> &rhs);
959 
961  NekVector<NekSingle> &result, const NekSingle &lhs,
962  const NekVector<NekSingle> &rhs);
963 
965  const NekSingle &lhs,
966  const NekVector<NekSingle> &rhs);
967 
968 template <typename DataType>
969 NekVector<DataType> Multiply(const DataType &lhs,
970  const NekVector<DataType> &rhs)
971 {
972  return Multiply(rhs, lhs);
973 }
974 
976  const NekDouble &lhs, const NekVector<NekDouble> &rhs);
977 
979  const NekSingle &lhs, const NekVector<NekSingle> &rhs);
980 
981 template <typename DataType>
982 std::ostream &operator<<(std::ostream &os, const NekVector<DataType> &rhs)
983 {
984  os << rhs.AsString();
985  return os;
986 }
987 
988 template LIB_UTILITIES_EXPORT std::ostream &operator<<(
989  std::ostream &os, const NekVector<NekDouble> &rhs);
990 
991 template LIB_UTILITIES_EXPORT std::ostream &operator<<(
992  std::ostream &os, const NekVector<NekSingle> &rhs);
993 
994 template <typename DataType>
996  const NekPoint<DataType> &dest)
997 {
998  NekVector<DataType> result(3, 0.0);
999  for (unsigned int i = 0; i < 3; ++i)
1000  {
1001  result[i] = dest[i] - source[i];
1002  }
1003  return result;
1004 }
1005 
1007  const NekPoint<NekDouble> &source, const NekPoint<NekDouble> &dest);
1008 
1010  const NekPoint<NekSingle> &source, const NekPoint<NekSingle> &dest);
1011 
1012 template <typename DataType>
1014  const DataType &t)
1015 {
1016  NekPoint<DataType> result;
1017  for (unsigned int i = 0; i < 3; ++i)
1018  {
1019  result[i] = lhs[i] * t;
1020  }
1021 
1022  return result;
1023 }
1024 
1026  const NekVector<NekDouble> &lhs, const NekDouble &t);
1027 
1029  const NekVector<NekSingle> &lhs, const NekSingle &t);
1030 
1031 template <typename DataType>
1033 {
1034  if (lhs.GetDimension() != rhs.GetDimension())
1035  {
1036  return false;
1037  }
1038 
1039  return std::equal(lhs.begin(), lhs.end(), rhs.begin());
1040 }
1041 
1043  const NekVector<NekDouble> &rhs);
1044 
1046  const NekVector<NekSingle> &rhs);
1047 
1048 template <typename DataType>
1050 {
1051  return !(lhs == rhs);
1052 }
1053 
1055  const NekVector<NekDouble> &rhs);
1056 
1058  const NekVector<NekSingle> &rhs);
1059 
1060 template <typename DataType>
1061 std::vector<DataType> FromString(const std::string &str)
1062 {
1063  std::vector<DataType> result;
1064 
1065  try
1066  {
1067  typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
1068  boost::char_separator<char> sep("(<,>) ");
1069  tokenizer tokens(str, sep);
1070  for (tokenizer::iterator strIter = tokens.begin();
1071  strIter != tokens.end(); ++strIter)
1072  {
1073  result.push_back(boost::lexical_cast<DataType>(*strIter));
1074  }
1075  }
1076  catch (boost::bad_lexical_cast &)
1077  {
1078  }
1079 
1080  return result;
1081 }
1082 
1083 template LIB_UTILITIES_EXPORT std::vector<NekDouble> FromString(
1084  const std::string &str);
1085 
1086 template LIB_UTILITIES_EXPORT std::vector<NekSingle> FromString(
1087  const std::string &str);
1088 
1089 template <typename DataType> DataType L1Norm(const NekVector<DataType> &v)
1090 {
1091  typedef NekVector<DataType> VectorType;
1092 
1093  DataType result(0);
1094  for (typename VectorType::const_iterator iter = v.begin(); iter != v.end();
1095  ++iter)
1096  {
1097  result += fabs(*iter);
1098  }
1099 
1100  return result;
1101 }
1102 
1104 
1106 
1107 template <typename DataType> DataType L2Norm(const NekVector<DataType> &v)
1108 {
1109  typedef NekVector<DataType> VectorType;
1110 
1111  DataType result(0);
1112  for (typename VectorType::const_iterator iter = v.begin(); iter != v.end();
1113  ++iter)
1114  {
1115  DataType v = fabs(*iter);
1116  result += v * v;
1117  }
1118  return sqrt(result);
1119 }
1120 
1122 
1124 
1125 template <typename DataType> DataType InfinityNorm(const NekVector<DataType> &v)
1126 {
1127  DataType result = fabs(v[0]);
1128  const unsigned int vdim = v.GetDimension();
1129  for (unsigned int i = 0; i < vdim; ++i)
1130  {
1131  result = std::max(DataType(fabs(v[i])), result);
1132  }
1133  return result;
1134 }
1135 
1138 
1141 
1142 template <typename DataType>
1144 {
1145  NekVector<DataType> temp(v);
1146  const unsigned int tdim = temp.GetDimension();
1147  for (unsigned int i = 0; i < tdim; ++i)
1148  {
1149  temp(i) = -temp(i);
1150  }
1151  return temp;
1152 }
1153 
1155  const NekVector<NekDouble> &v);
1156 
1158  const NekVector<NekSingle> &v);
1159 
1160 template <typename DataType> void NegateInPlace(NekVector<DataType> &v)
1161 {
1162  DataType *data = v.GetRawPtr();
1163  const unsigned int vdim = v.GetDimension();
1164  for (unsigned int i = 0; i < vdim; ++i)
1165  {
1166  data[i] = -data[i];
1167  }
1168 }
1169 
1171 
1173 
1174 template <typename DataType> DataType Magnitude(const NekVector<DataType> &v)
1175 {
1176  DataType result = DataType(0);
1177 
1178  const unsigned int vdim = v.GetDimension();
1179  for (unsigned int i = 0; i < vdim; ++i)
1180  {
1181  result += v[i] * v[i];
1182  }
1183  return sqrt(result);
1184 }
1185 
1188 
1191 
1192 template <typename DataType>
1193 DataType Dot(const NekVector<DataType> &lhs, const NekVector<DataType> &rhs)
1194 {
1195  ASSERTL1(lhs.GetDimension() == rhs.GetDimension(),
1196  "Dot, dimension of the two operands must be identical.");
1197 
1198  DataType result = DataType(0);
1199  const unsigned int ldim = lhs.GetDimension();
1200  for (unsigned int i = 0; i < ldim; ++i)
1201  {
1202  result += lhs[i] * rhs[i];
1203  }
1204 
1205  return result;
1206 }
1207 
1209  const NekVector<NekDouble> &rhs);
1210 
1212  const NekVector<NekSingle> &rhs);
1213 
1214 template <typename DataType> void Normalize(NekVector<DataType> &v)
1215 {
1216  DataType m = v.Magnitude();
1217  if (m > DataType(0))
1218  {
1219  v /= m;
1220  }
1221 }
1222 
1224 
1226 {
1227  v = -v;
1228 }
1230 {
1231  v = 1.0 / v;
1232 }
1233 
1235 
1237 {
1238  v = -v;
1239 }
1241 {
1242  v = 1.0 / v;
1243 }
1244 
1245 template <typename DataType>
1247  const NekVector<DataType> &rhs)
1248 {
1249  ASSERTL1(lhs.GetDimension() == 3 && rhs.GetDimension() == 3,
1250  "Cross is only valid for 3D vectors.");
1251 
1252  DataType first = lhs.y() * rhs.z() - lhs.z() * rhs.y();
1253  DataType second = lhs.z() * rhs.x() - lhs.x() * rhs.z();
1254  DataType third = lhs.x() * rhs.y() - lhs.y() * rhs.x();
1255 
1256  NekVector<DataType> result(first, second, third);
1257  return result;
1258 }
1259 
1261  const NekVector<NekDouble> &lhs, const NekVector<NekDouble> &rhs);
1262 
1264  const NekVector<NekSingle> &lhs, const NekVector<NekSingle> &rhs);
1265 
1266 template <typename DataType> std::string AsString(const NekVector<DataType> &v)
1267 {
1268  unsigned int d = v.GetRows();
1269  std::string result = "(";
1270  for (unsigned int i = 0; i < d; ++i)
1271  {
1272  result += boost::lexical_cast<std::string>(v[i]);
1273  if (i < v.GetDimension() - 1)
1274  {
1275  result += ", ";
1276  }
1277  }
1278  result += ")";
1279  return result;
1280 }
1281 
1282 template LIB_UTILITIES_EXPORT std::string AsString(
1283  const NekVector<NekDouble> &v);
1284 
1285 template LIB_UTILITIES_EXPORT std::string AsString(
1286  const NekVector<NekSingle> &v);
1287 } // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
#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:249
#define LIB_UTILITIES_EXPORT
bool operator==(const VertexSharedPtr &v1, const VertexSharedPtr &v2)
Define comparison operator for the vertex struct.
Definition: VtkToFld.cpp:95
Array< OneD, const DataType >::const_iterator begin() const
1D Array of constant elements with garbage collection and bounds checking.
Definition: SharedArray.hpp:58
size_type size() const
Returns the array's size.
boost::call_traits< DataType >::reference x()
Definition: NekVector.cpp:275
unsigned int m_size
Definition: NekVector.hpp:204
unsigned int GetDimension() const
Returns the number of dimensions for the point.
Definition: NekVector.cpp:201
Array< OneD, DataType > m_data
Definition: NekVector.hpp:205
unsigned int GetRows() const
Definition: NekVector.cpp:206
std::string AsString() const
Definition: NekVector.cpp:420
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
const DataType * const_iterator
Definition: NekVector.hpp:113
DataType Magnitude() const
Definition: NekVector.cpp:402
NekVector()
Creates an empty vector.
Definition: NekVector.cpp:41
DataType * GetRawPtr()
Definition: NekVector.cpp:211
boost::call_traits< DataType >::reference z()
Definition: NekVector.cpp:289
def copy(self)
Definition: pycml.py:2663
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
void Normalize(NekVector< DataType > &v)
Definition: NekVector.cpp:1214
template NekSingle L2Norm(const NekVector< NekSingle > &v)
template NekSingle Dot(const NekVector< NekSingle > &lhs, const NekVector< NekSingle > &rhs)
std::string AsString(const NekVector< DataType > &v)
Definition: NekVector.cpp:1266
template void SubtractNegatedLhs(NekVector< NekSingle > &result, const NekVector< NekSingle > &lhs, const NekVector< NekSingle > &rhs)
template std::string AsString(const NekVector< NekSingle > &v)
template NekPoint< NekSingle > findPointAlongVector(const NekVector< NekSingle > &lhs, const NekSingle &t)
template void AddEqualNegatedLhs(NekVector< NekSingle > &result, const NekVector< NekSingle > &rhs)
template NekVector< NekSingle > Cross(const NekVector< NekSingle > &lhs, const NekVector< NekSingle > &rhs)
@ eVECTOR_WRAPPER
template void SubtractEqual(NekVector< NekSingle > &result, const NekVector< NekSingle > &rhs)
DataType InfinityNorm(const NekVector< DataType > &v)
Definition: NekVector.cpp:1125
template NekVector< NekSingle > Multiply(const NekSingle &lhs, const NekVector< NekSingle > &rhs)
void NegateInPlace(NekSingle &v)
Definition: NekVector.cpp:1236
template void AddNegatedLhs(NekVector< NekSingle > &result, const NekVector< NekSingle > &lhs, const NekVector< NekSingle > &rhs)
template void Normalize(NekVector< NekSingle > &v)
template void MultiplyEqual(NekVector< NekSingle > &result, const NekSingle &rhs)
template NekVector< NekSingle > Subtract(const NekVector< NekSingle > &lhs, const NekVector< NekSingle > &rhs)
std::vector< DataType > FromString(const std::string &str)
Definition: NekVector.cpp:1061
template NekVector< NekSingle > Negate(const NekVector< NekSingle > &v)
template NekVector< NekSingle > Add(const NekVector< NekSingle > &lhs, const NekVector< NekSingle > &rhs)
template NekSingle L1Norm(const NekVector< NekSingle > &v)
template NekVector< NekSingle > Divide(const NekVector< NekSingle > &lhs, const NekSingle &rhs)
void InvertInPlace(NekSingle &v)
Definition: NekVector.cpp:1240
template void MultiplyInvertedLhs(NekVector< NekSingle > &result, const NekSingle &lhs, const NekVector< NekSingle > &rhs)
template void SubtractEqualNegatedLhs(NekVector< NekSingle > &result, const NekVector< NekSingle > &rhs)
DataType Magnitude(const NekVector< DataType > &v)
Definition: NekVector.cpp:1174
NekVector< DataType > Cross(const NekVector< DataType > &lhs, const NekVector< DataType > &rhs)
Definition: NekVector.cpp:1246
template void DivideEqual(NekVector< NekSingle > &result, const NekSingle &rhs)
template NekSingle InfinityNorm(const NekVector< NekSingle > &v)
DataType L2Norm(const NekVector< DataType > &v)
Definition: NekVector.cpp:1107
DataType L1Norm(const NekVector< DataType > &v)
Definition: NekVector.cpp:1089
template bool operator!=(const NekVector< NekSingle > &lhs, const NekVector< NekSingle > &rhs)
template void AddEqual(NekVector< NekSingle > &result, const NekVector< NekSingle > &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...
template std::ostream & operator<<(std::ostream &os, const NekVector< NekSingle > &rhs)
DataType Dot(const NekVector< DataType > &lhs, const NekVector< DataType > &rhs)
Definition: NekVector.cpp:1193
void CopyArray(const Array< OneD, ConstDataType > &source, Array< OneD, DataType > &dest)
template NekVector< NekSingle > createVectorFromPoints(const NekPoint< NekSingle > &source, const NekPoint< NekSingle > &dest)
template NekSingle Magnitude(const NekVector< NekSingle > &v)
scalarT< T > sqrt(scalarT< T > in)
Definition: scalar.hpp:294