20#ifndef LIBMESH_DENSE_VECTOR_H
21#define LIBMESH_DENSE_VECTOR_H
24#include "libmesh/libmesh_common.h"
25#include "libmesh/compare_types.h"
26#include "libmesh/dense_vector_base.h"
27#include "libmesh/int_range.h"
28#include "libmesh/parallel_algorithms.h"
29#include "libmesh/tensor_tools.h"
31#ifdef LIBMESH_HAVE_EIGEN
32#include "libmesh/ignore_warnings.h"
34#include "libmesh/restore_warnings.h"
37#ifdef LIBMESH_HAVE_METAPHYSICL
38#include "metaphysicl/raw_type.h"
43#include <initializer_list>
81 template <
typename T2>
87 template <
typename T2>
93 template <
typename T2>
106 virtual unsigned int size() const override final
108 return cast_int<unsigned int>(
_val.size());
111 virtual bool empty() const override final
112 {
return _val.empty(); }
114 virtual void zero() override final;
119 const T & operator() (const
unsigned int i) const;
124 T & operator() (const
unsigned int i);
129 const T & operator[] (const
unsigned int i)
const {
return (*
this)(i); }
136 virtual T
el(
const unsigned int i)
const override final
137 {
return (*
this)(i); }
139 virtual T &
el(
const unsigned int i)
override final
140 {
return (*
this)(i); }
147 template <
typename T2>
164 template <
typename T2>
186 template <
typename T2,
typename T3>
187 typename std::enable_if<
197 template <
typename T2>
206 template <
typename T2>
212 template <
typename T2>
218 template <
typename T2>
226 template <
typename T2>
234 template <
typename T2>
290 typename std::vector<T>::const_iterator
begin()
const {
return _val.begin(); }
291 typename std::vector<T>::iterator
begin() {
return _val.begin(); }
296 typename std::vector<T>::const_iterator
end()
const {
return _val.end(); }
297 typename std::vector<T>::iterator
end() {
return _val.end(); }
306 for (
const T & v : var.
_val)
326 bool has_inf =
false;
327 for (
const T & v : var.
_val)
332 has_inf = has_inf ||
isinf(v);
349 for (
const T & v : var.
_val)
391 const std::vector<T2> & other_vals = other_vector.
get_values();
393 _val.assign(other_vals.begin(), other_vals.end());
408template <
typename T2>
411 _val(init_list.begin(), init_list.end())
422 const std::vector<T2> & other_vals = other_vector.
get_values();
423 _val.assign(other_vals.begin(), other_vals.end());
433 _val.swap(other_vector.
_val);
454 const std::vector<T2> & other_vals = other_vector.
get_values();
456 _val.reserve(this->size() + other_vals.size());
457 _val.insert(_val.end(), other_vals.begin(), other_vals.end());
466 std::fill (_val.begin(),
477 libmesh_assert_less (i, _val.size());
488 libmesh_assert_less (i, _val.size());
499 for (
auto & v : _val)
516template<
typename T2,
typename T3>
518typename std::enable_if<
523 libmesh_assert_equal_to (this->size(), vec.
size());
525 const int N = cast_int<int>(_val.size());
526 for (
int i=0; i<N; i++)
527 (*
this)(i) +=
static_cast<T
>(factor)*vec(i);
540 libmesh_assert_equal_to (this->size(), vec.
size());
542#ifdef LIBMESH_HAVE_EIGEN
547 return Eigen::Map<const typename Eigen::Matrix<T2, Eigen::Dynamic, 1>>(vec.
get_values().data(), vec.
size())
548 .dot(Eigen::Map<
const typename Eigen::Matrix<T, Eigen::Dynamic, 1>>(_val.data(), _val.size()));
552 const int N = cast_int<int>(_val.size());
556#pragma clang loop vectorize(enable)
558 for (
int i=0; i<N; i++)
570 libmesh_assert_equal_to (this->size(), vec.
size());
574 const int N = cast_int<int>(_val.size());
575 for (
int i=0; i<N; i++)
576 val += (*
this)(i)*(vec(i));
586 libmesh_assert_equal_to (this->size(), vec.
size());
588 const int N = cast_int<int>(_val.size());
589 for (
int i=0; i<N; i++)
590 if ((*
this)(i) != vec(i))
603 libmesh_assert_equal_to (this->size(), vec.
size());
605 const int N = cast_int<int>(_val.size());
606 for (
int i=0; i<N; i++)
607 if ((*
this)(i) != vec(i))
620 libmesh_assert_equal_to (this->size(), vec.
size());
622 const int N = cast_int<int>(_val.size());
623 for (
int i=0; i<N; i++)
624 (*
this)(i) += vec(i);
636 libmesh_assert_equal_to (this->size(), vec.
size());
638 const int N = cast_int<int>(_val.size());
639 for (
int i=0; i<N; i++)
640 (*
this)(i) -= vec(i);
654 (_val.begin(), _val.end(), std::numeric_limits<realfromT>::max(),
655 [](
const auto & a,
const auto &
b){using std::min; return min(a,b);},
656 [](
const T & v){return libmesh_real(v);});
668 (_val.begin(), _val.end(), std::numeric_limits<realfromT>::lowest(),
669 [](
const auto & a,
const auto &
b){using std::max; return max(a,b);},
670 [](
const T & v){return libmesh_real(v);});
682#ifdef LIBMESH_HAVE_EIGEN
683 return Eigen::Map<const typename Eigen::Matrix<T, Eigen::Dynamic, 1>>(_val.data(), _val.size()).
template lpNorm<1>();
686 (_val.begin(), _val.end(),
Real(0), std::plus<>(),
687 [](
const T & v){
using std::abs;
return abs(v);});
700#ifdef LIBMESH_HAVE_EIGEN
701 return Eigen::Map<const typename Eigen::Matrix<T, Eigen::Dynamic, 1>>(_val.data(), _val.size()).norm();
705 (_val.begin(), _val.end(),
Real(0), std::plus<>(),
719#ifdef LIBMESH_HAVE_EIGEN
720 return Eigen::Map<const typename Eigen::Matrix<T, Eigen::Dynamic, 1>>(_val.data(), _val.size()).
template lpNorm<Eigen::Infinity>();
723 (_val.begin(), _val.end(),
Real(0),
724 [](
const auto & a,
const auto &
b){
using std::max;
return max(a,
b);},
725 [](
const T & v){
using std::abs;
return abs(v);});
736 libmesh_assert_less_equal ( sub_n, this->size() );
739 const int N = cast_int<int>(sub_n);
740 for (
int i=0; i<N; i++)
747#ifdef LIBMESH_HAVE_METAPHYSICL
757 const auto s = in.
size();
759 for (
unsigned int i = 0; i < s; ++i)
760 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.
friend bool isinf(const DenseVector< T > &var)
Returns true iff no entry is NaN and any entry is infinite.
std::vector< T >::iterator begin()
CompareTypes< T, T2 >::supertype indefinite_dot(const DenseVector< T2 > &vec) const
friend bool isnan(const DenseVector< T > &var)
Returns true iff any entry is NaN.
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.
friend bool isfinite(const DenseVector< T > &var)
Returns true iff every entry is finite.
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.
LIBMESH_DEVICE_INLINE bool isfinite(std::complex< T > a)
LIBMESH_DEVICE_INLINE bool isnan(std::complex< T > a)
LIBMESH_DEVICE_INLINE T libmesh_real(T a)
LIBMESH_DEVICE_INLINE bool isinf(std::complex< T > a)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
LIBMESH_DEVICE_INLINE T libmesh_conj(T a)
T libmesh_transform_reduce(InputIt begin, InputIt end, T init, BinaryOp reduce, UnaryOp transform)