Reference documentation for deal.II version 8.5.1
vector.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1999 - 2017 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii__vector_h
17 #define dealii__vector_h
18 
19 
20 #include <deal.II/base/config.h>
21 #include <deal.II/base/logstream.h>
22 #include <deal.II/base/exceptions.h>
23 #include <deal.II/base/subscriptor.h>
24 #include <deal.II/base/index_set.h>
25 #include <deal.II/lac/vector_type_traits.h>
26 
27 // boost::serialization::make_array used to be in array.hpp, but was
28 // moved to a different file in BOOST 1.64
29 #include <boost/version.hpp>
30 #if BOOST_VERSION >= 106400
31 # include <boost/serialization/array_wrapper.hpp>
32 #else
33 # include <boost/serialization/array.hpp>
34 #endif
35 #include <boost/serialization/split_member.hpp>
36 
37 #include <cstdio>
38 #include <iostream>
39 #include <cstring>
40 #include <vector>
41 
42 DEAL_II_NAMESPACE_OPEN
43 
44 
45 #ifdef DEAL_II_WITH_PETSC
46 namespace PETScWrappers
47 {
48  class Vector;
49  namespace MPI
50  {
51  class Vector;
52  }
53 }
54 #endif
55 
56 #ifdef DEAL_II_WITH_TRILINOS
57 namespace TrilinosWrappers
58 {
59  namespace MPI
60  {
61  class Vector;
62  }
63  class Vector;
64 }
65 #endif
66 
67 template<typename number> class LAPACKFullMatrix;
68 
69 template <typename> class BlockVector;
70 
71 template <typename> class VectorView;
72 
73 namespace parallel
74 {
75  namespace internal
76  {
77  class TBBPartitioner;
78  }
79 }
80 
81 
82 
99 {
100  enum values
101  {
114  };
115 };
116 
117 
137 template <typename Number>
138 class Vector : public Subscriptor
139 {
140 public:
145  typedef Number value_type;
146  typedef value_type *pointer;
147  typedef const value_type *const_pointer;
148  typedef value_type *iterator;
149  typedef const value_type *const_iterator;
150  typedef value_type &reference;
151  typedef const value_type &const_reference;
152  typedef types::global_dof_index size_type;
153 
164 
177  static const bool supports_distributed_data DEAL_II_DEPRECATED = false;
178 
179 public:
180 
188  Vector ();
189 
197  Vector (const Vector<Number> &v);
198 
199 #ifdef DEAL_II_WITH_CXX11
200 
207  Vector (Vector<Number> &&v);
208 #endif
209 
210 
211 #ifndef DEAL_II_EXPLICIT_CONSTRUCTOR_BUG
212 
224  template <typename OtherNumber>
225  explicit
226  Vector (const Vector<OtherNumber> &v);
227 #endif
228 
229 #ifdef DEAL_II_WITH_PETSC
230 
235  explicit Vector (const PETScWrappers::Vector &v);
236 
247  explicit Vector (const PETScWrappers::MPI::Vector &v);
248 #endif
249 
250 #ifdef DEAL_II_WITH_TRILINOS
251 
262  explicit Vector (const TrilinosWrappers::MPI::Vector &v);
263 
269  explicit Vector (const TrilinosWrappers::Vector &v);
270 #endif
271 
281  explicit Vector (const size_type n);
282 
287  template <typename InputIterator>
288  Vector (const InputIterator first,
289  const InputIterator last);
290 
295  virtual ~Vector ();
296 
310  void compress (::VectorOperation::values operation
311  =::VectorOperation::unknown) const;
312 
328  virtual void reinit (const size_type N,
329  const bool omit_zeroing_entries=false);
330 
338  template <typename Number2>
339  void reinit (const Vector<Number2> &V,
340  const bool omit_zeroing_entries=false);
341 
357  virtual void swap (Vector<Number> &v);
358 
372  Vector<Number> &operator= (const Number s);
373 
380 
381 #ifdef DEAL_II_WITH_CXX11
382 
391 #endif
392 
398  template <typename Number2>
400 
405 
406 #ifdef DEAL_II_WITH_PETSC
407 
414 
427 #endif
428 
429 
430 #ifdef DEAL_II_WITH_TRILINOS
431 
444 
452 #endif
453 
459  template <typename Number2>
460  bool operator== (const Vector<Number2> &v) const;
461 
467  template <typename Number2>
468  bool operator != (const Vector<Number2> &v) const;
469 
471 
472 
477 
491  template <typename Number2>
492  Number operator * (const Vector<Number2> &V) const;
493 
501  real_type norm_sqr () const;
502 
510  Number mean_value () const;
511 
519  real_type l1_norm () const;
520 
529  real_type l2_norm () const;
530 
539  real_type lp_norm (const real_type p) const;
540 
544  real_type linfty_norm () const;
545 
566  Number add_and_dot (const Number a,
567  const Vector<Number> &V,
568  const Vector<Number> &W);
569 
571 
572 
577 
583  iterator begin ();
584 
588  const_iterator begin () const;
589 
593  iterator end ();
594 
599  const_iterator end () const;
600 
604  Number operator() (const size_type i) const;
605 
609  Number &operator() (const size_type i);
610 
616  Number operator[] (const size_type i) const;
617 
623  Number &operator[] (const size_type i);
624 
631  template <typename OtherNumber>
632  void extract_subvector_to (const std::vector<size_type> &indices,
633  std::vector<OtherNumber> &values) const;
634 
639  template <typename ForwardIterator, typename OutputIterator>
640  void extract_subvector_to (ForwardIterator indices_begin,
641  const ForwardIterator indices_end,
642  OutputIterator values_begin) const;
644 
645 
650 
657 
664 
669  template <typename OtherNumber>
670  void add (const std::vector<size_type> &indices,
671  const std::vector<OtherNumber> &values);
672 
677  template <typename OtherNumber>
678  void add (const std::vector<size_type> &indices,
679  const Vector<OtherNumber> &values);
680 
686  template <typename OtherNumber>
687  void add (const size_type n_elements,
688  const size_type *indices,
689  const OtherNumber *values);
690 
697  void add (const Number s);
698 
706  void add (const Vector<Number> &V) DEAL_II_DEPRECATED;
707 
708 
714  void add (const Number a, const Vector<Number> &V,
715  const Number b, const Vector<Number> &W);
716 
722  void add (const Number a, const Vector<Number> &V);
723 
729  void sadd (const Number s,
730  const Vector<Number> &V);
731 
737  void sadd (const Number s,
738  const Number a,
739  const Vector<Number> &V);
740 
748  void sadd (const Number s,
749  const Number a,
750  const Vector<Number> &V,
751  const Number b,
752  const Vector<Number> &W) DEAL_II_DEPRECATED;
753 
762  void sadd (const Number s,
763  const Number a,
764  const Vector<Number> &V,
765  const Number b,
766  const Vector<Number> &W,
767  const Number c,
768  const Vector<Number> &X) DEAL_II_DEPRECATED;
769 
775  Vector<Number> &operator *= (const Number factor);
776 
782  Vector<Number> &operator /= (const Number factor);
783 
791  void scale (const Vector<Number> &scaling_factors);
792 
798  template <typename Number2>
799  void scale (const Vector<Number2> &scaling_factors);
800 
806  void equ (const Number a, const Vector<Number> &u);
807 
811  template <typename Number2>
812  void equ (const Number a, const Vector<Number2> &u);
813 
821  void equ (const Number a, const Vector<Number> &u,
822  const Number b, const Vector<Number> &v) DEAL_II_DEPRECATED;
823 
831  void equ (const Number a, const Vector<Number> &u,
832  const Number b, const Vector<Number> &v,
833  const Number c, const Vector<Number> &w) DEAL_II_DEPRECATED;
834 
847  void ratio (const Vector<Number> &a,
848  const Vector<Number> &b) DEAL_II_DEPRECATED;
849 
861  void update_ghost_values () const;
863 
864 
875  void print (const char *format = 0) const DEAL_II_DEPRECATED;
876 
883  void print (std::ostream &out,
884  const unsigned int precision = 3,
885  const bool scientific = true,
886  const bool across = true) const;
887 
896  void print (LogStream &out,
897  const unsigned int width = 6,
898  const bool across = true) const DEAL_II_DEPRECATED;
899 
905  void block_write (std::ostream &out) const;
906 
918  void block_read (std::istream &in);
919 
924  template <class Archive>
925  void save (Archive &ar, const unsigned int version) const;
926 
931  template <class Archive>
932  void load (Archive &ar, const unsigned int version);
933 
934  BOOST_SERIALIZATION_SPLIT_MEMBER()
935 
944 
950  bool in_local_range (const size_type global_index) const;
951 
968 
972  std::size_t size () const;
973 
979  bool all_zero () const;
980 
990  bool is_non_negative () const;
991 
996  std::size_t memory_consumption () const;
998 
999 protected:
1000 
1005  size_type vec_size;
1006 
1013  size_type max_vec_size;
1014 
1018  Number *val;
1019 
1024  mutable std_cxx11::shared_ptr<parallel::internal::TBBPartitioner> thread_loop_partitioner;
1025 
1029  template <typename Number2> friend class Vector;
1030 
1034  template <typename Number2> friend class LAPACKFullMatrix;
1035 
1039  friend class VectorView<Number>;
1040 
1041 private:
1042 
1047  void allocate();
1048 
1052  void deallocate();
1053 };
1054 
1056 /*----------------------- Inline functions ----------------------------------*/
1057 
1058 
1059 #ifndef DOXYGEN
1060 
1061 
1062 //------------------------ declarations for explicit specializations
1063 
1064 #ifdef DEAL_II_BOOST_BIND_COMPILER_BUG
1065 template <>
1067 Vector<std::complex<float> >::operator= (const std::complex<float> s);
1068 #endif
1069 
1070 
1071 template <>
1073 Vector<int>::lp_norm (const real_type) const;
1074 
1075 
1076 //------------------------ inline functions
1077 
1078 template <typename Number>
1079 inline
1081  :
1082  vec_size(0),
1083  max_vec_size(0),
1084  val(0)
1085 {
1086  reinit(0);
1087 }
1088 
1089 
1090 
1091 template <typename Number>
1092 template <typename InputIterator>
1093 Vector<Number>::Vector (const InputIterator first, const InputIterator last)
1094  :
1095  vec_size (0),
1096  max_vec_size (0),
1097  val (0)
1098 {
1099  // allocate memory. do not initialize it, as we will copy over to it in a
1100  // second
1101  reinit (std::distance (first, last), true);
1102  std::copy (first, last, begin());
1103 }
1104 
1105 
1106 
1107 template <typename Number>
1108 inline
1109 Vector<Number>::Vector (const size_type n)
1110  :
1111  vec_size(0),
1112  max_vec_size(0),
1113  val(0)
1114 {
1115  reinit (n, false);
1116 }
1117 
1118 
1119 
1120 template <typename Number>
1121 inline
1123 {
1124  if (val)
1125  {
1126  deallocate();
1127  val=0;
1128  }
1129 }
1130 
1131 
1132 
1133 template <typename Number>
1134 inline
1135 std::size_t Vector<Number>::size () const
1136 {
1137  return vec_size;
1138 }
1139 
1140 
1141 template <typename Number>
1142 inline
1144 (const size_type) const
1145 {
1146  return true;
1147 }
1148 
1149 
1150 
1151 template <typename Number>
1152 inline
1153 typename Vector<Number>::iterator
1155 {
1156  return &val[0];
1157 }
1158 
1159 
1160 
1161 template <typename Number>
1162 inline
1163 typename Vector<Number>::const_iterator
1164 Vector<Number>::begin () const
1165 {
1166  return &val[0];
1167 }
1168 
1169 
1170 
1171 template <typename Number>
1172 inline
1173 typename Vector<Number>::iterator
1175 {
1176  return &val[vec_size];
1177 }
1178 
1179 
1180 
1181 template <typename Number>
1182 inline
1183 typename Vector<Number>::const_iterator
1184 Vector<Number>::end () const
1185 {
1186  return &val[vec_size];
1187 }
1188 
1189 
1190 
1191 template <typename Number>
1192 inline
1193 Number Vector<Number>::operator() (const size_type i) const
1194 {
1195  Assert (i<vec_size, ExcIndexRange(i,0,vec_size));
1196  return val[i];
1197 }
1198 
1199 
1200 
1201 template <typename Number>
1202 inline
1203 Number &Vector<Number>::operator() (const size_type i)
1204 {
1205  Assert (i<vec_size, ExcIndexRangeType<size_type>(i,0,vec_size));
1206  return val[i];
1207 }
1208 
1209 
1210 
1211 template <typename Number>
1212 inline
1213 Number Vector<Number>::operator[] (const size_type i) const
1214 {
1215  return operator()(i);
1216 }
1217 
1218 
1219 
1220 template <typename Number>
1221 inline
1222 Number &Vector<Number>::operator[] (const size_type i)
1223 {
1224  return operator()(i);
1225 }
1226 
1227 
1228 
1229 template <typename Number>
1230 template <typename OtherNumber>
1231 inline
1232 void Vector<Number>::extract_subvector_to (const std::vector<size_type> &indices,
1233  std::vector<OtherNumber> &values) const
1234 {
1235  for (size_type i = 0; i < indices.size(); ++i)
1236  values[i] = operator()(indices[i]);
1237 }
1238 
1239 
1240 
1241 template <typename Number>
1242 template <typename ForwardIterator, typename OutputIterator>
1243 inline
1244 void Vector<Number>::extract_subvector_to (ForwardIterator indices_begin,
1245  const ForwardIterator indices_end,
1246  OutputIterator values_begin) const
1247 {
1248  while (indices_begin != indices_end)
1249  {
1250  *values_begin = operator()(*indices_begin);
1251  indices_begin++;
1252  values_begin++;
1253  }
1254 }
1255 
1256 
1257 
1258 template <typename Number>
1259 inline
1261 Vector<Number>::operator /= (const Number factor)
1262 {
1263  AssertIsFinite(factor);
1264  Assert (factor != Number(0.), ExcZero() );
1265 
1266  this->operator *= (Number(1.)/factor);
1267  return *this;
1268 }
1269 
1270 
1271 
1272 template <typename Number>
1273 template <typename OtherNumber>
1274 inline
1275 void
1276 Vector<Number>::add (const std::vector<size_type> &indices,
1277  const std::vector<OtherNumber> &values)
1278 {
1279  Assert (indices.size() == values.size(),
1280  ExcDimensionMismatch(indices.size(), values.size()));
1281  add (indices.size(), &indices[0], &values[0]);
1282 }
1283 
1284 
1285 
1286 template <typename Number>
1287 template <typename OtherNumber>
1288 inline
1289 void
1290 Vector<Number>::add (const std::vector<size_type> &indices,
1291  const Vector<OtherNumber> &values)
1292 {
1293  Assert (indices.size() == values.size(),
1294  ExcDimensionMismatch(indices.size(), values.size()));
1295  add (indices.size(), &indices[0], values.val);
1296 }
1297 
1298 
1299 
1300 template <typename Number>
1301 template <typename OtherNumber>
1302 inline
1303 void
1304 Vector<Number>::add (const size_type n_indices,
1305  const size_type *indices,
1306  const OtherNumber *values)
1307 {
1308  for (size_type i=0; i<n_indices; ++i)
1309  {
1310  Assert (indices[i] < vec_size, ExcIndexRange(indices[i],0,vec_size));
1311  Assert (numbers::is_finite(values[i]),
1312  ExcMessage("The given value is not finite but either infinite or Not A Number (NaN)"));
1313 
1314  val[indices[i]] += values[i];
1315  }
1316 }
1317 
1318 
1319 
1320 template <typename Number>
1321 template <typename Number2>
1322 inline
1323 bool
1325 {
1326  return ! (*this == v);
1327 }
1328 
1329 
1330 
1331 template <typename Number>
1332 inline
1333 void
1335 {}
1336 
1337 
1338 
1339 template <typename Number>
1340 inline
1341 void
1343 {}
1344 
1345 
1346 
1347 // Moved from vector.templates.h as an inline function by Luca Heltai
1348 // on 2009/04/12 to prevent strange compiling errors, after making
1349 // swap virtual.
1350 template <typename Number>
1351 inline
1352 void
1354 {
1355  std::swap (vec_size, v.vec_size);
1356  std::swap (max_vec_size, v.max_vec_size);
1357  std::swap (val, v.val);
1358 }
1359 
1360 
1361 
1362 template <typename Number>
1363 template <class Archive>
1364 inline
1365 void
1366 Vector<Number>::save (Archive &ar, const unsigned int) const
1367 {
1368  // forward to serialization function in the base class.
1369  ar &static_cast<const Subscriptor &>(*this);
1370 
1371  ar &vec_size &max_vec_size ;
1372  ar &boost::serialization::make_array(val, max_vec_size);
1373 }
1374 
1375 
1376 
1377 template <typename Number>
1378 template <class Archive>
1379 inline
1380 void
1381 Vector<Number>::load (Archive &ar, const unsigned int)
1382 {
1383  // get rid of previous content
1384  deallocate();
1385 
1386  // the load stuff again from the archive
1387  ar &static_cast<Subscriptor &>(*this);
1388  ar &vec_size &max_vec_size ;
1389 
1390  allocate();
1391  ar &boost::serialization::make_array(val, max_vec_size);
1392 }
1393 
1394 #endif
1395 
1396 
1410 template <typename Number>
1411 inline
1413 {
1414  u.swap (v);
1415 }
1416 
1417 
1421 template <typename number>
1422 inline
1423 std::ostream &
1424 operator << (std::ostream &os, const Vector<number> &v)
1425 {
1426  v.print(os);
1427  return os;
1428 }
1429 
1433 template <typename number>
1434 inline
1435 LogStream &
1436 operator << (LogStream &os, const Vector<number> &v)
1437 {
1438  v.print(os);
1439  return os;
1440 }
1441 
1450 template <typename Number>
1451 struct is_serial_vector< Vector<Number> > : std_cxx11::true_type
1452 {
1453 };
1454 
1455 
1456 DEAL_II_NAMESPACE_CLOSE
1457 
1458 #endif
void block_read(std::istream &in)
void update_ghost_values() const
std::size_t memory_consumption() const
Number value_type
Definition: vector.h:145
real_type l1_norm() const
Number add_and_dot(const Number a, const Vector< Number > &V, const Vector< Number > &W)
Number operator[](const size_type i) const
bool operator!=(const Vector< Number2 > &v) const
void load(Archive &ar, const unsigned int version)
void sadd(const Number s, const Vector< Number > &V)
void extract_subvector_to(const std::vector< size_type > &indices, std::vector< OtherNumber > &values) const
Number operator()(const size_type i) const
real_type linfty_norm() const
bool is_non_negative() const
void equ(const Number a, const Vector< Number > &u)
void print(const char *format=0) const 1
STL namespace.
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
iterator end()
Vector< Number > & operator=(const Number s)
void allocate()
bool is_finite(const double x)
Definition: numbers.h:275
size_type max_vec_size
Definition: vector.h:1013
void add(const std::vector< size_type > &indices, const std::vector< OtherNumber > &values)
static ::ExceptionBase & ExcMessage(std::string arg1)
numbers::NumberTraits< Number >::real_type real_type
Definition: vector.h:163
bool in_local_range(const size_type global_index) const
void scale(const Vector< Number > &scaling_factors)
Vector< Number > & operator+=(const Vector< Number > &V)
unsigned int global_dof_index
Definition: types.h:88
#define Assert(cond, exc)
Definition: exceptions.h:313
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
real_type lp_norm(const real_type p) const
std::size_t size() const
virtual ~Vector()
void ratio(const Vector< Number > &a, const Vector< Number > &b) 1
Vector< Number > & operator-=(const Vector< Number > &V)
Vector< Number > & operator*=(const Number factor)
iterator begin()
Number operator*(const Vector< Number2 > &V) const
void deallocate()
void swap(Vector< Number > &u, Vector< Number > &v)
Definition: vector.h:1412
virtual void swap(Vector< Number > &v)
Vector< Number > & operator/=(const Number factor)
void block_write(std::ostream &out) const
IndexSet locally_owned_elements() const
void save(Archive &ar, const unsigned int version) const
bool operator==(const Vector< Number2 > &v) const
bool all_zero() const
void compress(::VectorOperation::values operation=::VectorOperation::unknown) const
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false)
real_type norm_sqr() const
std_cxx11::shared_ptr< parallel::internal::TBBPartitioner > thread_loop_partitioner
Definition: vector.h:1024
size_type vec_size
Definition: vector.h:1005
Number * val
Definition: vector.h:1018
real_type l2_norm() const
static ::ExceptionBase & ExcZero()
#define AssertIsFinite(number)
Definition: exceptions.h:1197
static const bool supports_distributed_data
Definition: vector.h:177
Number mean_value() const