20#ifndef LIBMESH_DENSE_VECTOR_H
21#define LIBMESH_DENSE_VECTOR_H
24#include "libmesh/libmesh_common.h"
25#include "libmesh/dense_vector_base.h"
26#include "libmesh/compare_types.h"
27#include "libmesh/int_range.h"
28#include "libmesh/tensor_tools.h"
30#ifdef LIBMESH_HAVE_EIGEN
31#include "libmesh/ignore_warnings.h"
33#include "libmesh/restore_warnings.h"
36#ifdef LIBMESH_HAVE_METAPHYSICL
37#include "metaphysicl/raw_type.h"
42#include <initializer_list>
79 template <
typename T2>
85 template <
typename T2>
91 template <
typename T2>
104 virtual unsigned int size() const override final
106 return cast_int<unsigned int>(
_val.size());
109 virtual bool empty() const override final
110 {
return _val.empty(); }
112 virtual void zero() override final;
117 const T & operator() (const
unsigned int i) const;
122 T & operator() (const
unsigned int i);
127 const T & operator[] (const
unsigned int i)
const {
return (*
this)(i); }
134 virtual T
el(
const unsigned int i)
const override final
135 {
return (*
this)(i); }
137 virtual T &
el(
const unsigned int i)
override final
138 {
return (*
this)(i); }
145 template <
typename T2>
162 template <
typename T2>
184 template <
typename T2,
typename T3>
185 typename std::enable_if<
195 template <
typename T2>
204 template <
typename T2>
210 template <
typename T2>
216 template <
typename T2>
224 template <
typename T2>
232 template <
typename T2>
288 typename std::vector<T>::const_iterator
begin()
const {
return _val.begin(); }
289 typename std::vector<T>::iterator
begin() {
return _val.begin(); }
294 typename std::vector<T>::const_iterator
end()
const {
return _val.end(); }
295 typename std::vector<T>::iterator
end() {
return _val.end(); }
333 const std::vector<T2> & other_vals = other_vector.
get_values();
337 const int N = cast_int<int>(other_vals.size());
340 for (
int i=0; i<N; i++)
341 _val.push_back(other_vals[i]);
356template <
typename T2>
359 _val(init_list.begin(), init_list.end())
370 const std::vector<T2> & other_vals = other_vector.
get_values();
374 const int N = cast_int<int>(other_vals.size());
377 for (
int i=0; i<N; i++)
378 _val.push_back(other_vals[i]);
389 _val.swap(other_vector.
_val);
410 const std::vector<T2> & other_vals = other_vector.
get_values();
412 _val.reserve(this->size() + other_vals.size());
413 _val.insert(_val.end(), other_vals.begin(), other_vals.end());
422 std::fill (_val.begin(),
433 libmesh_assert_less (i, _val.size());
444 libmesh_assert_less (i, _val.size());
455 const int N = cast_int<int>(_val.size());
456 for (
int i=0; i<N; i++)
473template<
typename T2,
typename T3>
475typename std::enable_if<
480 libmesh_assert_equal_to (this->size(), vec.
size());
482 const int N = cast_int<int>(_val.size());
483 for (
int i=0; i<N; i++)
484 (*
this)(i) +=
static_cast<T
>(factor)*vec(i);
497 libmesh_assert_equal_to (this->size(), vec.
size());
499#ifdef LIBMESH_HAVE_EIGEN
504 return Eigen::Map<const typename Eigen::Matrix<T2, Eigen::Dynamic, 1>>(vec.
get_values().data(), vec.
size())
505 .dot(Eigen::Map<
const typename Eigen::Matrix<T, Eigen::Dynamic, 1>>(_val.data(), _val.size()));
509 const int N = cast_int<int>(_val.size());
513#pragma clang loop vectorize(enable)
515 for (
int i=0; i<N; i++)
527 libmesh_assert_equal_to (this->size(), vec.
size());
531 const int N = cast_int<int>(_val.size());
532 for (
int i=0; i<N; i++)
533 val += (*
this)(i)*(vec(i));
543 libmesh_assert_equal_to (this->size(), vec.
size());
545 const int N = cast_int<int>(_val.size());
546 for (
int i=0; i<N; i++)
547 if ((*
this)(i) != vec(i))
560 libmesh_assert_equal_to (this->size(), vec.
size());
562 const int N = cast_int<int>(_val.size());
563 for (
int i=0; i<N; i++)
564 if ((*
this)(i) != vec(i))
577 libmesh_assert_equal_to (this->size(), vec.
size());
579 const int N = cast_int<int>(_val.size());
580 for (
int i=0; i<N; i++)
581 (*
this)(i) += vec(i);
593 libmesh_assert_equal_to (this->size(), vec.
size());
595 const int N = cast_int<int>(_val.size());
596 for (
int i=0; i<N; i++)
597 (*
this)(i) -= vec(i);
611 const int N = cast_int<int>(_val.size());
612 for (
int i=1; i!=N; i++)
615 my_min = (my_min < current? my_min : current);
629 const int N = cast_int<int>(_val.size());
630 for (
int i=1; i!=N; i++)
633 my_max = (my_max > current? my_max : current);
647#ifdef LIBMESH_HAVE_EIGEN
648 return Eigen::Map<const typename Eigen::Matrix<T, Eigen::Dynamic, 1>>(_val.data(), _val.size()).
template lpNorm<1>();
651 const int N = cast_int<int>(_val.size());
652 for (
int i=0; i!=N; i++)
653 my_norm += std::abs((*
this)(i));
668#ifdef LIBMESH_HAVE_EIGEN
669 return Eigen::Map<const typename Eigen::Matrix<T, Eigen::Dynamic, 1>>(_val.data(), _val.size()).norm();
672 const int N = cast_int<int>(_val.size());
676#pragma clang loop vectorize(enable)
678 for (
int i=0; i!=N; i++)
681 return sqrt(my_norm);
694#ifdef LIBMESH_HAVE_EIGEN
695 return Eigen::Map<const typename Eigen::Matrix<T, Eigen::Dynamic, 1>>(_val.data(), _val.size()).
template lpNorm<Eigen::Infinity>();
699 const int N = cast_int<int>(_val.size());
700 for (
int i=1; i!=N; i++)
703 my_norm = (my_norm > current? my_norm : current);
705 return sqrt(my_norm);
716 libmesh_assert_less_equal ( sub_n, this->size() );
719 const int N = cast_int<int>(sub_n);
720 for (
int i=0; i<N; i++)
752 bool has_inf =
false;
758 has_inf = has_inf ||
isinf(var(i));
784#ifdef LIBMESH_HAVE_METAPHYSICL
794 const auto s = in.
size();
796 for (
unsigned int i = 0; i < s; ++i)
797 ret(i) = raw_value(in(i));
Defines an abstract dense vector base class for use in Finite Element-type computations.
Defines a dense vector for use in Finite Element-type computations.
DenseVector & operator=(const DenseVector &)=default
void resize(const unsigned int n)
Resize the vector.
std::vector< T >::const_iterator begin() const
DenseVector(const DenseVector< T2 > &other_vector)
Copy-constructor.
std::vector< T >::iterator end()
virtual void zero() override final
Set every element in the vector to 0.
const std::vector< T > & get_values() const
const T & operator[](const unsigned int i) const
std::enable_if< ScalarTraits< T2 >::value, void >::type add(const T2 factor, const DenseVector< T3 > &vec)
Adds factor times vec to this vector.
bool operator==(const DenseVector< T2 > &vec) const
DenseVector< T > & operator-=(const DenseVector< T2 > &vec)
Subtracts vec from this vector.
std::vector< T > & get_values()
DenseVector< T > & operator*=(const T factor)
Multiplies every element in the vector by factor.
std::vector< T >::const_iterator end() const
DenseVector(std::initializer_list< T2 > init_list)
Initializer list constructor.
std::vector< T >::iterator begin()
CompareTypes< T, T2 >::supertype indefinite_dot(const DenseVector< T2 > &vec) const
DenseVector(const unsigned int n, const T &val)
Constructor.
std::vector< T > _val
The actual data values, stored as a 1D array.
const T & operator()(const unsigned int i) const
virtual bool empty() const override final
bool operator!=(const DenseVector< T2 > &vec) const
void append(const DenseVector< T2 > &other_vector)
Append additional entries to (resizing, but unchanging) the vector.
DenseVector(DenseVector &&)=default
The 5 special functions can be defaulted for this class, as it does not manage any memory itself.
DenseVector(const unsigned int n=0)
Constructor.
virtual ~DenseVector()=default
DenseVector< T > & operator+=(const DenseVector< T2 > &vec)
Adds vec to this vector.
DenseVector(const std::vector< T2 > &other_vector)
Copy-constructor, from a std::vector.
virtual T & el(const unsigned int i) override final
DenseVector(const DenseVector &)=default
void swap(DenseVector< T > &other_vector)
STL-like swap method.
void get_principal_subvector(unsigned int sub_n, DenseVector< T > &dest) const
Puts the principal subvector of size sub_n (i.e.
CompareTypes< T, T2 >::supertype dot(const DenseVector< T2 > &vec) const
virtual T el(const unsigned int i) const override final
void scale(const T factor)
Multiplies every element in the vector by factor.
virtual unsigned int size() const override final
The libMesh namespace provides an interface to certain functionality in the library.
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
bool isfinite(std::complex< T > a)
bool isnan(std::complex< T > a)
bool isinf(std::complex< T > a)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real