20 #ifndef __numeric_vector_h__
21 #define __numeric_vector_h__
24 #include <boost/shared_ptr.hpp>
25 #include <boost/numeric/ublas/vector.hpp>
27 #include <feel/feelcore/traits.hpp>
29 #include <feel/feelalg/datamap.hpp>
33 namespace ublas = boost::numeric::ublas;
36 template <
typename T>
class Vector;
37 template <
typename T>
class MatrixSparse;
38 template <
typename T>
class MatrixShell;
54 typedef typename type_traits<T>::real_type real_type;
56 typedef Vector<T> self_type;
57 typedef boost::shared_ptr<Vector<T> > self_ptrtype;
58 typedef boost::shared_ptr<Vector<T> > clone_ptrtype;
60 typedef DataMap datamap_type;
61 typedef boost::shared_ptr<datamap_type> datamap_ptrtype;
67 Vector ( datamap_ptrtype
const& n );
72 Vector (
const size_type n, WorldComm
const& _worldComm = Environment::worldComm() );
80 WorldComm
const& _worldComm = Environment::worldComm() );
90 datamap_type
const& map()
const
95 datamap_ptrtype
const& mapPtr()
const
100 void setMap( datamap_ptrtype
const& d )
126 virtual void close () = 0;
131 virtual void clear ();
139 virtual void zero () = 0;
171 virtual clone_ptrtype
clone ()
const = 0;
188 const bool =
false );
194 const bool =
false );
198 void init ( datamap_ptrtype
const& dm )
227 virtual Vector<T> &
operator= (
const Vector<T> &V );
232 Vector<T> &
operator= (
const std::vector<T> &v );
237 virtual value_type
sum()
const = 0;
244 virtual real_type
min ()
const = 0;
256 virtual real_type
max ()
const = 0;
267 virtual real_type
l1Norm ()
const = 0;
274 virtual real_type
l2Norm ()
const = 0;
292 return M_map->nDof();
301 return M_map->nLocalDofWithGhost();
311 return M_map->minMyGID();
321 return M_map->maxMyGID()+1;
324 virtual bool localIndexIsGhost(
size_type localDof)
const
326 return M_map->dofGlobalProcessIsGhost(localDof);
334 return M_map->comm();
357 virtual void set (
const size_type i,
const value_type& value ) = 0;
362 virtual void add (
const size_type i,
const value_type& value ) = 0;
367 virtual void addVector (
int* i,
int n, value_type* v ) = 0;
374 virtual void add (
const value_type& s ) = 0;
404 virtual void addVector (
const std::vector<T>& v,
405 const std::vector<size_type>& dof_indices ) = 0;
414 const std::vector<size_type>& dof_indices ) = 0;
454 virtual void addVector (
const DenseVector<T>& V,
455 const std::vector<size_type>& dof_indices ) = 0;
458 virtual value_type dot(
Vector<T> const& v ) = 0;
459 virtual value_type dot( boost::shared_ptr<
Vector<T> >
const& v ) {
return dot( *v ); }
465 virtual void insert (
const std::vector<T>& v,
466 const std::vector<size_type>& dof_indices ) = 0;
474 virtual void insert (
const Vector<T>& V,
475 const std::vector<size_type>& dof_indices ) = 0;
483 virtual void insert (
const ublas::vector<T>& V,
484 const std::vector<size_type>& dof_indices ) = 0;
490 virtual void scale (
const T factor ) = 0;
500 virtual int compare (
const Vector<T> &other_vector,
501 const real_type threshold = 1e-10 )
const;
508 virtual void print( std::ostream& os=std::cout )
const;
513 friend std::ostream& operator << ( std::ostream& os, const Vector<T>& v )
525 virtual void printMatlab(
const std::string name=
"NULL",
bool renumber =
false )
const
527 std::cerr <<
"ERROR: Not Implemented in base class yet!" << std::endl;
528 std::cerr <<
"ERROR writing MATLAB file " << name << std::endl;
529 FEELPP_ASSERT( 0 ).error(
"invalid call" );
539 const std::vector<size_type>& )
const
541 std::cerr <<
"ERROR: Not Implemented in base class yet!" << std::endl;
542 FEELPP_ASSERT( 0 ).error(
"invalid call" );
566 typedef boost::shared_ptr<vector_type> vector_ptrtype;
577 template <
typename T>
578 typename type_traits<T>::real_type
581 FEELPP_ASSERT( v1.localSize() == v2.localSize() &&
582 v1.size() == v2.size() )
583 ( v1.localSize() )( v2.localSize() )
584 ( v1.size() )( v2.size() ).error(
"incompatible vector sizes" );
586 typedef typename type_traits<T>::real_type real_type;
592 real_type global_res = 0;
594 if ( v1.comm().size() == 1 )
597 res += v1( start + i )* v2( start + i );
605 if ( !v1.localIndexIsGhost( start + i ) )
606 res += v1( start + i )* v2( start + i );
608 #if defined( FEELPP_HAS_MPI )
609 mpi::all_reduce( v1.comm(), res, global_res, std::plus<real_type>() );
622 template <
typename T>
623 typename type_traits<T>::real_type
625 boost::shared_ptr<
Vector<T> >
const& v2 )
630 template <
typename T>
631 typename type_traits<T>::real_type
632 dot( boost::shared_ptr<Vector<T> >
const& v1,
633 boost::shared_ptr<Vector<T> >
const& v2 )
637 template <
typename T>
638 typename type_traits<T>::real_type
639 dot( Vector<T>
const& v1,
640 Vector<T>
const& v2 )
650 #endif // #ifdef __numeric_vector_h__