LifeV
VectorSmall.hpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5  Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6  Copyright (C) 2010 EPFL, Politecnico di Milano, Emory UNiversity
7 
8  This file is part of the LifeV library
9 
10  LifeV is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 3 of the License, or
13  (at your option) any later version.
14 
15  LifeV is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public
21  License along with this library; if not, see <http://www.gnu.org/licenses/>
22 
23 
24 *******************************************************************************
25 */
26 //@HEADER
27 
28 /*!
29  * @file
30  @brief This file contains a simple \f$ R^3 \f$ point
31 
32  @date 06/2011
33  @author A. Cervone <ant.cervone@gmail.com>
34  */
35 
36 #ifndef _VECTORSMALL_H_
37 #define _VECTORSMALL_H_ 1
38 
39 #include <lifev/core/LifeV.hpp>
40 #include <lifev/core/array/RNM.hpp>
41 //#include <lifev/core/array/MatrixSmall.hpp>
42 
43 #include <vector>
44 #include <cmath>
45 
46 // LifeV namespace.
47 namespace LifeV
48 {
49 //! class VectorSmall This class implements a simple \f$ R^n \f$ vector
50 
51 /*!
52  @author A. Cervone <ant.cervone@gmail.com>
53 
54  This class implements a simple \f$ R^n \f$ vector.
55  <br>
56  It allows all kind of geometric operations on the node,
57  such as summation, multiplication by scalar, scalar product,
58  cross product, norm, etc.
59  <br>
60  The implementation is oriented to best perform with small (less than 30)
61  values of \f$ n \f$.
62  <br>
63  The interface of the class is designed to stay compatible with the Eigen
64  library Matrix class.
65 
66 */
67 template<UInt Dim1, UInt Dim2>
68 class MatrixSmall;
69 
70 template <UInt Dim>
72 {
73 
74 public:
75 
76  //! @name Constructors and destructors
77  //@{
78 
79  //! Empty constructor (all components are set to zero)
81  {
82  for ( UInt i = 0; i < Dim; i++ )
83  {
84  M_coords[ i ] = 0.;
85  }
86  }
87 
88  //! Non Empty constructor
89  VectorSmall(const Real value)
90  {
91  for ( UInt i = 0; i < Dim; i++ )
92  {
93  M_coords[ i ] = value;
94  }
95  }
96 
97  //! Assignment operator
98  VectorSmall<Dim>& operator= ( VectorSmall<Dim> const& vector )
99  {
100  for ( UInt i = 0; i < Dim; i++ )
101  {
102  M_coords[ i ] = vector.M_coords[ i ];
103  }
104  return *this;
105  }
106 
107  //! Copy constructor
108  VectorSmall ( VectorSmall<Dim> const& vector )
109  {
110  *this = vector;
111  }
112 
113  //@}
114 
115  //! @name Static initializations
116  //@{
117 
118  //! Constant initialization
119  static VectorSmall<Dim> Constant ( Real const& value )
120  {
121  VectorSmall<Dim> v;
122  for ( UInt i = 0; i < Dim; i++ )
123  {
124  v[ i ] = value;
125  }
126  return v;
127  }
128 
129  //! Zero initialization
130  static VectorSmall<Dim> Zero ()
131  {
132  return VectorSmall<Dim>::Constant ( 0. );
133  }
134 
135  //@}
136 
137  //! @name Overloaded operators
138  //@{
139 
140  //! Operator +=
141  VectorSmall<Dim>& operator+= ( VectorSmall<Dim> const& vector )
142  {
143  for ( UInt i = 0; i < Dim; i++ )
144  {
145  M_coords[ i ] += vector.M_coords[ i ];
146  }
147  return *this;
148  }
149 
150  //! Operator +
151  VectorSmall<Dim> operator+ ( VectorSmall<Dim> const& vector ) const
152  {
153  VectorSmall<Dim> tmp ( *this );
154  return tmp += vector;
155  }
156 
157  //! Operator -=
158  VectorSmall<Dim>& operator-= ( VectorSmall<Dim> const& vector )
159  {
160  for ( UInt i = 0; i < Dim; i++ )
161  {
162  M_coords[ i ] -= vector.M_coords[ i ];
163  }
164  return *this;
165  }
166 
167  //! Operator -
168  VectorSmall<Dim> operator- ( VectorSmall<Dim> const& vector ) const
169  {
170  VectorSmall tmp ( *this );
171  return tmp -= vector;
172  }
173 
174  //! Operator *= (multiplication by scalar)
175  VectorSmall<Dim>& operator*= ( Real const& factor )
176  {
177  for ( UInt i = 0; i < Dim; i++ )
178  {
179  M_coords[ i ] *= factor;
180  }
181  return *this;
182  }
183 
184  //! Operator /= (division by scalar)
185  VectorSmall<Dim>& operator/= ( Real const& factor )
186  {
187  ASSERT ( factor != 0. , "Division by zero!" );
188  *this *= 1. / factor;
189  return *this;
190  }
191 
192  //! Operator / (division by scalar)
193  VectorSmall<Dim> operator/ ( Real const& factor ) const
194  {
195  VectorSmall<Dim> tmp ( *this );
196  return tmp /= factor;
197  }
198 
199  //! Operator []
200  Real const& operator[] ( UInt const& i ) const
201  {
202  ASSERT ( i < Dim, "trying to access an index that exceeds the dimension of the array" );
203  return M_coords [ i ];
204  }
205 
206  //! Operator []
207  Real& operator[] ( UInt const& i )
208  {
209  ASSERT ( i < Dim, "trying to set an index that exceeds the dimension of the array" );
210  return M_coords [ i ];
211  }
212 
213  //! Operator ()
214  Real const& operator() ( UInt const& i ) const
215  {
216  ASSERT ( i < Dim, "trying to access an index that exceeds the dimension of the array" );
217  return M_coords [ i ];
218  }
219 
220  //! Operator ()
221  Real& operator() ( UInt const& i )
222  {
223  ASSERT ( i < Dim, "trying to set an index that exceeds the dimension of the array" );
224  return M_coords [ i ];
225  }
226 
227  //@}
228 
229  //! @name Geometric Methods
230  //@{
231 
232  //! Scalar product
233  /*!
234  @param vector second operand
235  @return scalar product value
236  */
237  Real dot ( VectorSmall<Dim> const& vector ) const
238  {
239  Real scalarProduct = 0.;
240  for ( UInt i = 0; i < Dim; i++ )
241  {
242  scalarProduct += M_coords[ i ] * vector.M_coords[ i ];
243  }
244  return scalarProduct;
245  }
246 
247  MatrixSmall< Dim , Dim > outerProduct ( VectorSmall<Dim> const& vector ) const
248  {
249  MatrixSmall<Dim, Dim> result;
250 
251  for ( UInt i = 0; i < Dim; i++ )
252  for ( UInt j = 0; j < Dim; j++ )
253  {
254  result[i][j] = M_coords[ i ] * vector.M_coords[ j ];
255  }
256  return result;
257  }
258 
259  //! \f$ L^2 \f$ norm
260  /*!
261  @return norm value
262  */
263  Real norm () const
264  {
265  return std::sqrt ( this->dot ( *this ) );
266  }
267 
268  //! Normalize vector
269  void normalize ()
270  {
271  *this /= norm ();
272  }
273 
274  //! Create the versor associated to this VectorSmall
275  /*!
276  @return the versor associated to this VectorSmall
277  */
279  {
280  return VectorSmall<Dim> ( ( *this ) / norm () );
281  }
282 
283  //@}
284 
285  //! @name Tools
286  //@{
287 
288  //! function to get the size of the VectorSmall ( for compatibility with Eigen)
289  /*!
290  @return the fixed size of the VectorSmall
291  */
292  static UInt size()
293  {
294  return Dim;
295  }
296 
297  //@}
298 
299 private:
300 
301  //! @name Data
302  //@{
303 
304  //! Data storage
305  Real M_coords[ Dim ];
306 
307  //@}
308 };
309 
310 //! @name External overloaded operators
311 //@{
312 
313 //! Operator * (multiplication by scalar on the right)
314 template <UInt Dim>
315 inline VectorSmall<Dim> operator* ( VectorSmall<Dim> const& vector, Real const& factor )
316 {
317  VectorSmall<Dim> tmp ( vector );
318  return tmp *= factor;
319 }
320 
321 //! Operator * (multiplication by scalar on the left)
322 template <UInt Dim>
323 inline VectorSmall<Dim> operator* ( Real const& factor, VectorSmall<Dim> const& vector )
324 {
325  VectorSmall<Dim> tmp ( vector );
326  return tmp *= factor;
327 }
328 
329 //! Operator <<
330 template <UInt Dim>
331 inline std::ostream& operator<< ( std::ostream& out , VectorSmall<Dim> const& point )
332 {
333  out << "( ";
334  for ( UInt i = 0; i < Dim; i++ )
335  {
336  out << point[ i ] << " ";
337  }
338  out << ")";
339  return out;
340 }
341 
342 //@}
343 
344 //! @name Conversion free-functions
345 //@{
346 
347 //! Conversion of an array (std::vector, KN, ecc.) to a VectorSmall
348 /*!
349 @param coords vector of point coordinates with operator[] available
350 @return the VectorSmall that corresponds to the input
351 */
352 template <UInt Dim, typename Vector>
353 inline VectorSmall<Dim> castToVectorSmall ( Vector const& coords )
354 {
355  ASSERT ( coords.size() == Dim , "the input vector has the wrong dimension" );
356  VectorSmall<Dim> tmp;
357  for ( UInt i = 0; i < Dim; i++ )
358  {
359  tmp[ i ] = coords[ i ];
360  }
361  return tmp;
362 }
363 
364 //@}
365 
366 
367 //! class VectorSmall<3> Partial specialization for the 3D case
368 template <>
369 class VectorSmall<3>
370 {
371 
372 public:
373 
374  //! @name Constructors and destructors
375  //@{
376  //! Empty constructor (all components are set to zero)
378  {
379  M_coords[ 0 ] = M_coords[ 1 ] = M_coords[ 2 ] = 0.;
380  }
381 
382  //! Non Empty constructor
383  VectorSmall(const Real value)
384  {
385  M_coords[ 0 ] = M_coords[ 1 ] = M_coords[ 2 ] = value;
386  }
387 
388  //! Full constructor with all components explicitly initialized
389  /*!
390  @param x x-component of the point
391  @param y y-component of the point
392  @param z z-component of the point
393  */
394  VectorSmall ( Real const& x, Real const& y, Real const& z )
395  {
396  M_coords[ 0 ] = x;
397  M_coords[ 1 ] = y;
398  M_coords[ 2 ] = z;
399  }
400 
401 
402  VectorSmall ( Real* rawVector )
403  {
404  M_coords[ 0 ] = rawVector[0];
405  M_coords[ 1 ] = rawVector[1];
406  M_coords[ 2 ] = rawVector[2];
407  }
408 
409  //! Assignment operator
410  VectorSmall<3>& operator= ( VectorSmall<3> const& vector )
411  {
412  M_coords[ 0 ] = vector.M_coords[ 0 ];
413  M_coords[ 1 ] = vector.M_coords[ 1 ];
414  M_coords[ 2 ] = vector.M_coords[ 2 ];
415  return *this;
416  }
417 
418  //! Copy constructor
419  VectorSmall ( VectorSmall<3> const& vector )
420  {
421  *this = vector;
422  }
423 
424  //@}
425 
426  //! @name Overloaded operators
427  //@{
428 
429  //! Operator +=
430  VectorSmall<3>& operator+= ( VectorSmall<3> const& vector )
431  {
432  M_coords[ 0 ] += vector.M_coords[ 0 ];
433  M_coords[ 1 ] += vector.M_coords[ 1 ];
434  M_coords[ 2 ] += vector.M_coords[ 2 ];
435  return *this;
436  }
437 
438  //! Operator +
439  VectorSmall<3> operator+ ( VectorSmall<3> const& vector ) const
440  {
441  VectorSmall<3> tmp ( *this );
442  return tmp += vector;
443  }
444 
445  //! Operator -=
446  VectorSmall<3>& operator-= ( VectorSmall<3> const& vector )
447  {
448  M_coords[ 0 ] -= vector.M_coords[ 0 ];
449  M_coords[ 1 ] -= vector.M_coords[ 1 ];
450  M_coords[ 2 ] -= vector.M_coords[ 2 ];
451  return *this;
452  }
453 
454  //! Operator -
455  VectorSmall<3> operator- ( VectorSmall<3> const& vector ) const
456  {
457  VectorSmall<3> tmp ( *this );
458  return tmp -= vector;
459  }
460 
461  //! Operator *= (multiplication by scalar)
462  VectorSmall<3>& operator*= ( Real const& factor )
463  {
464  M_coords[ 0 ] *= factor;
465  M_coords[ 1 ] *= factor;
466  M_coords[ 2 ] *= factor;
467  return *this;
468  }
469 
470  //! Operator * (multiplication by scalar on the right)
471  VectorSmall<3> operator* ( Real const& factor ) const
472  {
473  VectorSmall<3> tmp ( *this );
474  return tmp *= factor;
475  }
476 
477  //! Operator /= (division by scalar)
478  VectorSmall<3>& operator/= ( Real const& factor )
479  {
480  ASSERT ( factor != 0. , "Division by zero!" );
481  *this *= 1. / factor;
482  return *this;
483  }
484 
485  //! Operator / (division by scalar)
486  VectorSmall<3> operator/ ( Real const& factor ) const
487  {
488  VectorSmall<3> tmp ( *this );
489  return tmp /= factor;
490  }
491 
492  //! Operator []
493  Real const& operator[] ( UInt const& i ) const
494  {
495  ASSERT ( i < 3 , "trying to access an index different from 0,1,2" );
496  return M_coords [ i ];
497  }
498 
499  //! Operator []
500  Real& operator[] ( UInt const& i )
501  {
502  ASSERT ( i < 3 , "trying to set an index different from 0,1,2" );
503  return M_coords [ i ];
504  }
505 
506  //! Operator ()
507  Real const& operator() ( UInt const& i ) const
508  {
509  ASSERT ( i < 3 , "trying to access an index different from 0,1,2" );
510  return M_coords [ i ];
511  }
512 
513  //! Operator ()
514  Real& operator() ( UInt const& i )
515  {
516  ASSERT ( i < 3 , "trying to set an index different from 0,1,2" );
517  return M_coords [ i ];
518  }
519 
520  //@}
521 
522  //! @name Geometric Methods
523  //@{
524 
525  //! Scalar product
526  /*!
527  @param vector second operand
528  @return scalar product value
529  */
530  Real dot ( VectorSmall<3> const& vector ) const
531  {
532  return ( M_coords[ 0 ] * vector.M_coords[ 0 ]
533  + M_coords[ 1 ] * vector.M_coords[ 1 ]
534  + M_coords[ 2 ] * vector.M_coords[ 2 ] );
535  }
536 
537  //! Cross product
538  /*!
539  @param vector second operand
540  */
541  VectorSmall<3> cross ( VectorSmall<3> const& vector ) const
542  {
543  return VectorSmall ( M_coords[ 1 ] * vector.M_coords[ 2 ]
544  - M_coords[ 2 ] * vector.M_coords[ 1 ],
545  M_coords[ 2 ] * vector.M_coords[ 0 ]
546  - M_coords[ 0 ] * vector.M_coords[ 2 ],
547  M_coords[ 0 ] * vector.M_coords[ 1 ]
548  - M_coords[ 1 ] * vector.M_coords[ 0 ] );
549  }
550 
551  //! Outer product
552  /*!
553  @param vector second operand
554  */
555  MatrixSmall<3, 3> outerProduct ( VectorSmall<3> const& vector ) const;
556 
557  //! Extraction of a component
558  /*!
559  @param index of the component to be extracted
560  @return extracted component
561  */
562  Real extract ( UInt const& i ) const
563  {
564  return ( M_coords[ i ] );
565  }
566 
567 
568 
569  //! \f$ L^2 \f$ norm
570  /*!
571  @return norm value
572  */
573  Real norm () const
574  {
575  return std::sqrt ( this->dot ( *this ) );
576  }
577 
578  //! Normalize vector
579  void normalize ()
580  {
581  *this /= norm ();
582  }
583 
584  //! Create the versor associated to this VectorSmall
585  /*!
586  @return the versor associated to this VectorSmall
587  */
589  {
590  return VectorSmall<3> ( ( *this ) / norm () );
591  }
592 
593  //@}
594 
595  //! @name Tools
596  //@{
597 
598  //! function to get the size of the VectorSmall ( for compatibility with Eigen)
599  /*!
600  @return the fixed size of the VectorSmall
601  */
602  UInt size() const
603  {
604  return 3;
605  }
606 
607  //@}
608 
609 private:
610 
611  //! @name Data
612  //@{
613 
614  //! Data storage
616 
617  //@}
618 };
619 
620 //! @name External overloaded operators
621 //@{
622 
623 //! Operator * (multiplication by scalar on the left)
624 inline VectorSmall<3> operator* ( Real const& factor, VectorSmall<3> const& vector )
625 {
626  VectorSmall<3> tmp ( vector );
627  tmp *= factor;
628  return tmp;
629 }
630 
631 //@}
632 
633 //! @name Conversion free-functions
634 //@{
635 
636 //! Conversion of an array (std::vector, KNM, ecc.) to a VectorSmall
637 /*!
638 @param coords vector of point coordinates with operator[] available
639 @return the VectorSmall that corresponds to the input
640 */
641 template <typename Vector>
642 inline VectorSmall<3> castToVector3D ( Vector const& coords )
643 {
644  ASSERT ( coords.size() == 3 , "the input vector has the wrong dimension" );
645  return VectorSmall<3> ( coords[ 0 ], coords[ 1 ], coords[ 2 ] );
646 }
647 
648 //@}
649 
651 
652 } // namespace LifeV
653 
654 
655 #endif //_VECTORSMALL_H_
656 
657 // -*- mode: c++ -*-
VectorSmall< 3 > cross(VectorSmall< 3 > const &vector) const
Cross product.
static VectorSmall< Dim > Zero()
Zero initialization.
VectorSmall(const Real value)
Non Empty constructor.
Definition: VectorSmall.hpp:89
VectorSmall< 3 > operator-(VectorSmall< 3 > const &vector) const
Operator -.
VectorSmall< 3 > castToVector3D(Vector const &coords)
Conversion of an array (std::vector, KNM, ecc.) to a VectorSmall.
Real const & operator[](UInt const &i) const
Operator [].
VectorSmall< Dim > operator+(VectorSmall< Dim > const &vector) const
Operator +.
Real & operator()(UInt const &i)
Operator ()
VectorSmall< Dim > operator-(VectorSmall< Dim > const &vector) const
Operator -.
Real dot(VectorSmall< 3 > const &vector) const
Scalar product.
VectorSmall< Dim > normalized()
Create the versor associated to this VectorSmall.
void normalize()
Normalize vector.
Real const & operator[](UInt const &i) const
Operator [].
VectorSmall< Dim > operator/(Real const &factor) const
Operator / (division by scalar)
VectorSmall< 3 > operator+(VectorSmall< 3 > const &vector) const
Operator +.
MatrixSmall< 3, 3 > outerProduct(VectorSmall< 3 > const &vector) const
Outer product.
Definition: VectorSmall.cpp:12
VectorSmall(VectorSmall< Dim > const &vector)
Copy constructor.
VectorSmall()
Empty constructor (all components are set to zero)
Definition: VectorSmall.hpp:80
VectorSmall< 3 > & operator=(VectorSmall< 3 > const &vector)
Assignment operator.
void updateInverseJacobian(const UInt &iQuadPt)
UInt size() const
function to get the size of the VectorSmall ( for compatibility with Eigen)
void normalize()
Normalize vector.
VectorSmall< Dim > & operator*=(Real const &factor)
Operator *= (multiplication by scalar)
VectorSmall< Dim > operator*(VectorSmall< Dim > const &vector, Real const &factor)
Operator * (multiplication by scalar on the right)
VectorSmall< 3 > & operator-=(VectorSmall< 3 > const &vector)
Operator -=.
VectorSmall< 3 > operator*(Real const &factor) const
Operator * (multiplication by scalar on the right)
VectorSmall< 3 > & operator*=(Real const &factor)
Operator *= (multiplication by scalar)
VectorSmall(VectorSmall< 3 > const &vector)
Copy constructor.
Real & operator[](UInt const &i)
Operator [].
#define ASSERT(X, A)
Definition: LifeAssert.hpp:90
Real dot(VectorSmall< Dim > const &vector) const
Scalar product.
static UInt size()
function to get the size of the VectorSmall ( for compatibility with Eigen)
VectorSmall< Dim > & operator=(VectorSmall< Dim > const &vector)
Assignment operator.
Definition: VectorSmall.hpp:98
Real M_coords[3]
Data storage.
Real norm() const
norm
Real const & operator()(UInt const &i) const
Operator ()
VectorSmall< 3 > & operator+=(VectorSmall< 3 > const &vector)
Operator +=.
Real M_coords[Dim]
Data storage.
Real norm() const
norm
VectorSmall< 3 > & operator/=(Real const &factor)
Operator /= (division by scalar)
double Real
Generic real data.
Definition: LifeV.hpp:175
MatrixSmall< Dim, Dim > outerProduct(VectorSmall< Dim > const &vector) const
VectorSmall< 3 > normalized()
Create the versor associated to this VectorSmall.
VectorSmall< Dim > & operator+=(VectorSmall< Dim > const &vector)
Operator +=.
VectorSmall< 3 > operator/(Real const &factor) const
Operator / (division by scalar)
VectorSmall()
Empty constructor (all components are set to zero)
VectorSmall< 3 > operator*(Real const &factor, VectorSmall< 3 > const &vector)
Operator * (multiplication by scalar on the left)
Real & operator[](UInt const &i)
Operator [].
VectorSmall< 3 > Vector3D
VectorSmall(Real *rawVector)
VectorSmall< Dim > & operator-=(VectorSmall< Dim > const &vector)
Operator -=.
Real extract(UInt const &i) const
Extraction of a component.
static VectorSmall< Dim > Constant(Real const &value)
Constant initialization.
VectorSmall< Dim > castToVectorSmall(Vector const &coords)
Conversion of an array (std::vector, KN, ecc.) to a VectorSmall.
VectorSmall< Dim > & operator/=(Real const &factor)
Operator /= (division by scalar)
VectorSmall(Real const &x, Real const &y, Real const &z)
Full constructor with all components explicitly initialized.
uint32_type UInt
generic unsigned integer (used mainly for addressing)
Definition: LifeV.hpp:191
VectorSmall< Dim > operator*(Real const &factor, VectorSmall< Dim > const &vector)
Operator * (multiplication by scalar on the left)
Real & operator()(UInt const &i)
Operator ()
Real const & operator()(UInt const &i) const
Operator ()
VectorSmall(const Real value)
Non Empty constructor.