libMesh
Loading...
Searching...
No Matches
dense_vector.h
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
19
20#ifndef LIBMESH_DENSE_VECTOR_H
21#define LIBMESH_DENSE_VECTOR_H
22
23// Local Includes
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"
30
31#ifdef LIBMESH_HAVE_EIGEN
32#include "libmesh/ignore_warnings.h"
33#include <Eigen/Core>
34#include "libmesh/restore_warnings.h"
35#endif
36
37#ifdef LIBMESH_HAVE_METAPHYSICL
38#include "metaphysicl/raw_type.h"
39#endif
40
41// C++ includes
42#include <algorithm>
43#include <initializer_list>
44#include <vector>
45
46namespace libMesh
47{
48
59template<typename T>
61{
62public:
63
67 explicit
68 DenseVector(const unsigned int n=0);
69
74 explicit
75 DenseVector(const unsigned int n,
76 const T & val);
77
81 template <typename T2>
82 DenseVector (const DenseVector<T2> & other_vector);
83
87 template <typename T2>
88 DenseVector (const std::vector<T2> & other_vector);
89
93 template <typename T2>
94 DenseVector (std::initializer_list<T2> init_list);
95
100 DenseVector (DenseVector &&) = default;
101 DenseVector (const DenseVector &) = default;
102 DenseVector & operator= (const DenseVector &) = default;
104 virtual ~DenseVector() = default;
105
106 virtual unsigned int size() const override final
107 {
108 return cast_int<unsigned int>(_val.size());
109 }
110
111 virtual bool empty() const override final
112 { return _val.empty(); }
113
114 virtual void zero() override final;
115
119 const T & operator() (const unsigned int i) const;
120
124 T & operator() (const unsigned int i);
125
129 const T & operator[] (const unsigned int i) const { return (*this)(i); }
130
134 T & operator[] (const unsigned int i) { return (*this)(i); }
135
136 virtual T el(const unsigned int i) const override final
137 { return (*this)(i); }
138
139 virtual T & el(const unsigned int i) override final
140 { return (*this)(i); }
141
147 template <typename T2>
149
153 void swap(DenseVector<T> & other_vector);
154
158 void resize (const unsigned int n);
159
164 template <typename T2>
165 void append (const DenseVector<T2> & other_vector);
166
170 void scale (const T factor);
171
177 DenseVector<T> & operator*= (const T factor);
178
186 template <typename T2, typename T3>
187 typename std::enable_if<
188 ScalarTraits<T2>::value, void >::type
189 add (const T2 factor,
190 const DenseVector<T3> & vec);
191
197 template <typename T2>
199
206 template <typename T2>
208
212 template <typename T2>
213 bool operator== (const DenseVector<T2> & vec) const;
214
218 template <typename T2>
219 bool operator!= (const DenseVector<T2> & vec) const;
220
226 template <typename T2>
228
234 template <typename T2>
236
241 Real min () const;
242
247 Real max () const;
248
253 Real l1_norm () const;
254
259 Real l2_norm () const;
260
266
271 void get_principal_subvector (unsigned int sub_n, DenseVector<T> & dest) const;
272
280 std::vector<T> & get_values() { return _val; }
281
285 const std::vector<T> & get_values() const { return _val; }
286
290 typename std::vector<T>::const_iterator begin() const { return _val.begin(); }
291 typename std::vector<T>::iterator begin() { return _val.begin(); }
292
296 typename std::vector<T>::const_iterator end() const { return _val.end(); }
297 typename std::vector<T>::iterator end() { return _val.end(); }
298
302 friend bool isfinite (const DenseVector<T> & var)
303 {
304 using std::isfinite;
305 using libMesh::isfinite; // for T==complex
306 for (const T & v : var._val)
307 if (!isfinite(v))
308 return false;
309 return true;
310 }
311
320 friend bool isinf (const DenseVector<T> & var)
321 {
322 using std::isinf;
323 using libMesh::isinf; // for T==complex
324 using std::isnan;
325 using libMesh::isnan;
326 bool has_inf = false;
327 for (const T & v : var._val)
328 {
329 // NaN anywhere makes us NaN, not inf
330 if (isnan(v))
331 return false;
332 has_inf = has_inf || isinf(v);
333 }
334 return has_inf;
335 }
336
345 friend bool isnan (const DenseVector<T> & var)
346 {
347 using std::isnan;
348 using libMesh::isnan; // for T==complex
349 for (const T & v : var._val)
350 if (isnan(v))
351 return true;
352 return false;
353 }
354
355private:
356
360 std::vector<T> _val;
361};
362
363
364
365// ------------------------------------------------------------
366// DenseVector member functions
367template<typename T>
368inline
369DenseVector<T>::DenseVector(const unsigned int n) :
370 _val (n, T{})
371{
372}
373
374
375template<typename T>
376inline
377DenseVector<T>::DenseVector(const unsigned int n,
378 const T & val) :
379 _val (n, val)
380{
381}
382
383
384
385template<typename T>
386template<typename T2>
387inline
389 DenseVectorBase<T>()
390{
391 const std::vector<T2> & other_vals = other_vector.get_values();
392
393 _val.assign(other_vals.begin(), other_vals.end());
394}
395
396
397
398template<typename T>
399template<typename T2>
400inline
401DenseVector<T>::DenseVector (const std::vector<T2> & other_vector) :
402 _val(other_vector)
403{
404}
405
406
407template<typename T>
408template <typename T2>
409inline
410DenseVector<T>::DenseVector (std::initializer_list<T2> init_list) :
411 _val(init_list.begin(), init_list.end())
412{
413}
414
415
416
417template<typename T>
418template<typename T2>
419inline
421{
422 const std::vector<T2> & other_vals = other_vector.get_values();
423 _val.assign(other_vals.begin(), other_vals.end());
424 return *this;
425}
426
427
428
429template<typename T>
430inline
432{
433 _val.swap(other_vector._val);
434}
435
436
437
438template<typename T>
439inline
440void DenseVector<T>::resize(const unsigned int n)
441{
442 _val.resize(n);
443
444 zero();
445}
446
447
448
449template<typename T>
450template<typename T2>
451inline
452void DenseVector<T>::append (const DenseVector<T2> & other_vector)
453{
454 const std::vector<T2> & other_vals = other_vector.get_values();
455
456 _val.reserve(this->size() + other_vals.size());
457 _val.insert(_val.end(), other_vals.begin(), other_vals.end());
458}
459
460
461
462template<typename T>
463inline
465{
466 std::fill (_val.begin(),
467 _val.end(),
468 T{});
469}
470
471
472
473template<typename T>
474inline
475const T & DenseVector<T>::operator () (const unsigned int i) const
476{
477 libmesh_assert_less (i, _val.size());
478
479 return _val[i];
480}
481
482
483
484template<typename T>
485inline
486T & DenseVector<T>::operator () (const unsigned int i)
487{
488 libmesh_assert_less (i, _val.size());
489
490 return _val[i];
491}
492
493
494
495template<typename T>
496inline
497void DenseVector<T>::scale (const T factor)
498{
499 for (auto & v : _val)
500 v *= factor;
501}
502
503
504
505template<typename T>
506inline
508{
509 this->scale(factor);
510 return *this;
511}
512
513
514
515template<typename T>
516template<typename T2, typename T3>
517inline
518typename std::enable_if<
519 ScalarTraits<T2>::value, void >::type
520DenseVector<T>::add (const T2 factor,
521 const DenseVector<T3> & vec)
522{
523 libmesh_assert_equal_to (this->size(), vec.size());
524
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);
528}
529
530
531
532template<typename T>
533template<typename T2>
534inline
536{
537 if (!_val.size())
538 return 0.;
539
540 libmesh_assert_equal_to (this->size(), vec.size());
541
542#ifdef LIBMESH_HAVE_EIGEN
543 // We reverse the order of the arguments to dot() here since
544 // the convention in Eigen is to take the complex conjugate of the
545 // *first* argument, while ours is to take the complex conjugate of
546 // the second.
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()));
549#else
550 typename CompareTypes<T, T2>::supertype val = 0.;
551
552 const int N = cast_int<int>(_val.size());
553 // The following pragma tells clang's vectorizer that it is safe to
554 // reorder floating point operations for this loop.
555#ifdef __clang__
556#pragma clang loop vectorize(enable)
557#endif
558 for (int i=0; i<N; i++)
559 val += (*this)(i)*libmesh_conj(vec(i));
560
561 return val;
562#endif
563}
564
565template<typename T>
566template<typename T2>
567inline
569{
570 libmesh_assert_equal_to (this->size(), vec.size());
571
572 typename CompareTypes<T, T2>::supertype val = 0.;
573
574 const int N = cast_int<int>(_val.size());
575 for (int i=0; i<N; i++)
576 val += (*this)(i)*(vec(i));
577
578 return val;
579}
580
581template<typename T>
582template<typename T2>
583inline
585{
586 libmesh_assert_equal_to (this->size(), vec.size());
587
588 const int N = cast_int<int>(_val.size());
589 for (int i=0; i<N; i++)
590 if ((*this)(i) != vec(i))
591 return false;
592
593 return true;
594}
595
596
597
598template<typename T>
599template<typename T2>
600inline
602{
603 libmesh_assert_equal_to (this->size(), vec.size());
604
605 const int N = cast_int<int>(_val.size());
606 for (int i=0; i<N; i++)
607 if ((*this)(i) != vec(i))
608 return true;
609
610 return false;
611}
612
613
614
615template<typename T>
616template<typename T2>
617inline
619{
620 libmesh_assert_equal_to (this->size(), vec.size());
621
622 const int N = cast_int<int>(_val.size());
623 for (int i=0; i<N; i++)
624 (*this)(i) += vec(i);
625
626 return *this;
627}
628
629
630
631template<typename T>
632template<typename T2>
633inline
635{
636 libmesh_assert_equal_to (this->size(), vec.size());
637
638 const int N = cast_int<int>(_val.size());
639 for (int i=0; i<N; i++)
640 (*this)(i) -= vec(i);
641
642 return *this;
643}
644
645
646
647template<typename T>
648inline
650{
651 libmesh_assert (this->size());
652 typedef decltype(libmesh_real(T(0))) realfromT;
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);});
657}
658
659
660
661template<typename T>
662inline
664{
665 libmesh_assert (this->size());
666 typedef decltype(libmesh_real(T(0))) realfromT;
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);});
671}
672
673
674
675template<typename T>
676inline
678{
679 if (!_val.size())
680 return 0.;
681
682#ifdef LIBMESH_HAVE_EIGEN
683 return Eigen::Map<const typename Eigen::Matrix<T, Eigen::Dynamic, 1>>(_val.data(), _val.size()).template lpNorm<1>();
684#else
686 (_val.begin(), _val.end(), Real(0), std::plus<>(),
687 [](const T & v){using std::abs; return abs(v);});
688#endif
689}
690
691
692
693template<typename T>
694inline
696{
697 if (!_val.size())
698 return 0.;
699
700#ifdef LIBMESH_HAVE_EIGEN
701 return Eigen::Map<const typename Eigen::Matrix<T, Eigen::Dynamic, 1>>(_val.data(), _val.size()).norm();
702#else
703 using std::sqrt;
704 return sqrt(libmesh_transform_reduce
705 (_val.begin(), _val.end(), Real(0), std::plus<>(),
706 [](const T & v){return TensorTools::norm_sq(v);}));
707#endif
708}
709
710
711
712template<typename T>
713inline
715{
716 if (!_val.size())
717 return 0.;
718
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>();
721#else
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);});
726#endif
727}
728
729
730
731template<typename T>
732inline
734 DenseVector<T> & dest) const
735{
736 libmesh_assert_less_equal ( sub_n, this->size() );
737
738 dest.resize(sub_n);
739 const int N = cast_int<int>(sub_n);
740 for (int i=0; i<N; i++)
741 dest(i) = _val[i];
742}
743
744
745} // namespace libMesh
746
747#ifdef LIBMESH_HAVE_METAPHYSICL
748namespace MetaPhysicL
749{
750template <typename T>
751struct RawType<libMesh::DenseVector<T>>
752{
754
756 {
757 const auto s = in.size();
758 value_type ret(s);
759 for (unsigned int i = 0; i < s; ++i)
760 ret(i) = raw_value(in(i));
761
762 return ret;
763 }
764};
765}
766#endif
767
768#endif // LIBMESH_DENSE_VECTOR_H
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
Real linfty_norm() 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
static const Real b
LIBMESH_DEVICE_INLINE auto norm_sq(const T &a)
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_assert(ctx)
const Number zero
.
Definition libmesh.h:297
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)
static value_type value(const libMesh::DenseVector< T > &in)
libMesh::DenseVector< typename RawType< T >::value_type > value_type