Loading...
Searching...
No Matches
fvector.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_COMMON_FVECTOR_HH
#define DUNE_COMMON_FVECTOR_HH
#include <algorithm>
#include <array>
#include <cmath>
#include <concepts>
#include <cstdlib>
#include <cstring>
#include <type_traits>
#include <utility>
#include <initializer_list>
#include <dune/common/boundschecking.hh>
#include <dune/common/densevector.hh>
#include <dune/common/filledarray.hh>
#include <dune/common/ftraits.hh>
#include <dune/common/math.hh>
#include <dune/common/promotiontraits.hh>
#include <dune/common/typetraits.hh>
#include <dune/common/typeutilities.hh>
#include <dune/common/concepts/number.hh>
#include <dune/common/std/algorithm.hh>
#include <dune/common/std/compare.hh>
namespace Dune {
template< class K, int SIZE > class FieldVector;
template< class K, int SIZE >
struct DenseMatVecTraits< FieldVector<K,SIZE> >
{
typedef FieldVector<K,SIZE> derived_type;
typedef std::array<K,SIZE> container_type;
typedef K value_type;
typedef typename container_type::size_type size_type;
};
template< class K, int SIZE >
struct FieldTraits< FieldVector<K,SIZE> >
{
typedef typename FieldTraits<K>::field_type field_type;
typedef typename FieldTraits<K>::real_type real_type;
};
template<typename C, int SIZE>
struct IsFieldVectorSizeCorrect
{
constexpr static bool value = true;
};
template<typename T, int SIZE>
struct IsFieldVectorSizeCorrect<FieldVector<T,SIZE>,SIZE>
{
constexpr static bool value = true;
};
template<typename T, int SIZE, int SIZE1>
struct IsFieldVectorSizeCorrect<FieldVector<T,SIZE1>,SIZE>
{
constexpr static bool value = false;
};
template< class K, int SIZE >
class FieldVector :
public DenseVector< FieldVector<K,SIZE> >
{
using Base = DenseVector< FieldVector<K,SIZE> >;
std::array<K,SIZE> _data;
public:
static constexpr int dimension = SIZE;
using size_type = typename Base::size_type;
using value_type = typename Base::value_type;
using reference = value_type&;
using const_reference = const value_type&;
public:
constexpr FieldVector ()
noexcept(std::is_nothrow_default_constructible_v<K>)
: _data{}
{}
explicit(SIZE != 1)
constexpr FieldVector (const value_type& value) noexcept
: _data{filledArray<SIZE>(value)}
{}
template<Concept::Number S>
requires (std::constructible_from<K,S>)
explicit(SIZE != 1)
constexpr FieldVector (const S& scalar)
noexcept(std::is_nothrow_constructible_v<K,S>)
: _data{filledArray<SIZE,K>(K(scalar))}
{}
constexpr FieldVector (const std::initializer_list<K>& l)
: _data{}
{
assert(l.size() == size());
for (size_type i = 0; i < size(); ++i)
_data[i] = std::data(l)[i];
}
template<class V>
requires (IsFieldVectorSizeCorrect<V,SIZE>::value &&
std::is_assignable_v<K&, decltype(std::declval<const V&>()[0])>)
constexpr FieldVector (const DenseVector<V>& x)
{
assert(x.size() == size());
for (size_type i = 0; i < size(); ++i)
_data[i] = x[i];
}
template<class OtherK>
requires (std::is_assignable_v<K&, const OtherK&>)
explicit constexpr FieldVector (const FieldVector<OtherK, SIZE>& x)
noexcept(std::is_nothrow_assignable_v<K&, const OtherK&>)
{
for (size_type i = 0; i < size(); ++i)
_data[i] = x[i];
}
constexpr FieldVector (const FieldVector&) = default;
template<class V>
requires (IsFieldVectorSizeCorrect<V,SIZE>::value &&
std::is_assignable_v<K&, decltype(std::declval<const V&>()[0])>)
constexpr FieldVector& operator= (const DenseVector<V>& x)
{
assert(x.size() == size());
for (size_type i = 0; i < size(); ++i)
_data[i] = x[i];
return *this;
}
template<Concept::Number S>
requires std::constructible_from<K,S>
constexpr FieldVector& operator= (const S& scalar)
noexcept(std::is_nothrow_constructible_v<K,S>)
{
_data.fill(K(scalar));
return *this;
}
template<class OtherK>
requires (std::is_assignable_v<K&, const OtherK&>)
constexpr FieldVector& operator= (const FieldVector<OtherK, SIZE>& x)
noexcept(std::is_nothrow_assignable_v<K&, const OtherK&>)
{
for (size_type i = 0; i < size(); ++i)
_data[i] = x[i];
return *this;
}
constexpr FieldVector& operator= (const FieldVector&) = default;
static constexpr size_type size () noexcept { return SIZE; }
constexpr reference operator[] (size_type i)
{
DUNE_ASSERT_BOUNDS(i < size());
return _data[i];
}
constexpr const_reference operator[] (size_type i) const
{
DUNE_ASSERT_BOUNDS(i < size());
return _data[i];
}
constexpr K* data () noexcept
{
return _data.data();
}
constexpr const K* data () const noexcept
{
return _data.data();
}
constexpr operator const_reference () const noexcept
requires(SIZE == 1)
{
return _data[0];
}
constexpr operator reference () noexcept
requires(SIZE == 1)
{
return _data[0];
}
template<Concept::Number S>
friend constexpr bool operator== (const FieldVector& a, const S& b) noexcept
requires(SIZE == 1)
{
return a._data[0] == b;
}
template<Concept::Number S>
friend constexpr bool operator== (const S& a, const FieldVector& b) noexcept
requires(SIZE == 1)
{
return a == b._data[0];
}
template<class T>
requires (Std::three_way_comparable_with<K,T>)
friend constexpr auto operator<=> (const FieldVector& a, const FieldVector<T,SIZE>& b) noexcept
{
#if __cpp_lib_three_way_comparison
return a._data <=> b._data;
#else
return Std::lexicographical_compare_three_way(a.begin(), a.end(), b.begin(), b.end());
#endif
}
template<Concept::Number S>
friend constexpr auto operator<=> (const FieldVector& a, const S& b) noexcept
requires(SIZE == 1)
{
return a._data[0] <=> b;
}
template<Concept::Number S>
friend constexpr auto operator<=> (const S& a, const FieldVector& b) noexcept
requires(SIZE == 1)
{
return a <=> b._data[0];
}
template<Concept::Number S>
friend constexpr auto operator* (const FieldVector& a, const S& b) noexcept
{
using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
FieldVector<ResultValueType,dimension> result;
for (size_type i = 0; i < size(); ++i)
result[i] = a[i] * b;
return result;
}
template<Concept::Number S>
friend constexpr auto operator* (const S& a, const FieldVector& b) noexcept
{
using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
FieldVector<ResultValueType,dimension> result;
for (size_type i = 0; i < size(); ++i)
result[i] = a * b[i];
return result;
}
template<Concept::Number S>
friend constexpr auto operator/ (const FieldVector& a, const S& b) noexcept
{
using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
FieldVector<ResultValueType,dimension> result;
for (size_type i = 0; i < size(); ++i)
result[i] = a[i] / b;
return result;
}
template<Concept::Number S>
friend constexpr FieldVector operator/ (const S& a, const FieldVector& b) noexcept
requires(SIZE == 1)
{
return FieldVector{a / b[0]};
}
template<Concept::Number S>
friend constexpr auto operator+ (const FieldVector& a, const S& b) noexcept
requires(SIZE == 1)
{
using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
return FieldVector<ResultValueType,dimension>{a[0] + b};
}
template<Concept::Number S>
friend constexpr auto operator+ (const S& a, const FieldVector& b) noexcept
requires(SIZE == 1)
{
using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
return FieldVector<ResultValueType,dimension>{a + b[0]};
}
template<Concept::Number S>
friend constexpr auto operator- (const FieldVector& a, const S& b) noexcept
requires(SIZE == 1)
{
using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
return FieldVector<ResultValueType,dimension>{a[0] - b};
}
template<Concept::Number S>
friend constexpr auto operator- (const S& a, const FieldVector& b) noexcept
requires(SIZE == 1)
{
using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
return FieldVector<ResultValueType,dimension>{a - b[0]};
}
};
template<class K, int SIZE>
std::istream& operator>> (std::istream& in, FieldVector<K, SIZE>& v)
{
FieldVector<K, SIZE> w;
for (int i = 0; i < SIZE; ++i)
in >> w[i];
if (in)
v = w;
return in;
}
/* Overloads for common classification functions */
namespace MathOverloads {
template<class K, int SIZE>
auto isFinite (const FieldVector<K,SIZE>& b, PriorityTag<2>, ADLTag)
{
bool out = true;
for (int i = 0; i < SIZE; ++i) {
out &= Dune::isFinite(b[i]);
}
return out;
}
template<class K, int SIZE>
bool isInf (const FieldVector<K,SIZE>& b, PriorityTag<2>, ADLTag)
{
bool out = false;
for (int i = 0; i < SIZE; ++i) {
out |= Dune::isInf(b[i]);
}
return out;
}
template<class K, int SIZE,
std::enable_if_t<HasNaN<K>::value, int> = 0>
bool isNaN (const FieldVector<K,SIZE>& b, PriorityTag<2>, ADLTag)
{
bool out = false;
for (int i = 0; i < SIZE; ++i) {
out |= Dune::isNaN(b[i]);
}
return out;
}
template<class K,
std::enable_if_t<HasNaN<K>::value, int> = 0>
bool isUnordered (const FieldVector<K,1>& b, const FieldVector<K,1>& c,
PriorityTag<2>, ADLTag)
{
return Dune::isUnordered(b[0],c[0]);
}
} // end namespace MathOverloads
} // end namespace Dune
#endif // DUNE_COMMON_FVECTOR_HH
typetraits.hh
Traits for type conversions and type information.
typeutilities.hh
Utilities for type computations, constraining overloads, ...
ftraits.hh
Type traits to determine the type of reals (when working with complex numbers).
algorithm.hh
compare.hh
This file provides some concepts introduced in the C++20 standard library <compare> and <concepts> no...
number.hh
boundschecking.hh
Macro for wrapping boundary checks.
densevector.hh
Implements the dense vector interface, with an exchangeable storage class.
filledarray.hh
Utility to generate an array with a certain value.
promotiontraits.hh
Compute type of the result of an arithmetic operation involving two different number types.
math.hh
Some useful basic math stuff.
Dune::filledArray
constexpr std::array< T, n > filledArray(const T &t)
Return an array filled with the provided value.
Definition filledarray.hh:33
Dune::operator>>
std::istream & operator>>(std::istream &stream, std::tuple< Ts... > &t)
Read a std::tuple.
Definition streamoperators.hh:43
DUNE_ASSERT_BOUNDS
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition boundschecking.hh:30
Dune::operator*
constexpr decltype(auto) operator*() const
Dereferencing operator.
Definition iteratorfacades.hh:1119
std
STL namespace.
Dune
Dune namespace
Definition alignedallocator.hh:13
Dune::MathOverloads
namespace for customization of math functions with Dune-Semantics
Definition fvector.hh:431
Dune::MathOverloads::isNaN
bool isNaN(const FieldVector< K, SIZE > &b, PriorityTag< 2 >, ADLTag)
Returns whether any entry is NaN.
Definition fvector.hh:458
Dune::MathOverloads::isInf
bool isInf(const FieldVector< K, SIZE > &b, PriorityTag< 2 >, ADLTag)
Returns whether any entry is infinite.
Definition fvector.hh:446
Dune::MathOverloads::isFinite
auto isFinite(const FieldVector< K, SIZE > &b, PriorityTag< 2 >, ADLTag)
Returns whether all entries are finite.
Definition fvector.hh:435
Dune::MathOverloads::isUnordered
bool isUnordered(const FieldVector< K, 1 > &b, const FieldVector< K, 1 > &c, PriorityTag< 2 >, ADLTag)
Returns true if either b or c is NaN.
Definition fvector.hh:470
Dune::Std::lexicographical_compare_three_way
constexpr auto lexicographical_compare_three_way(I1 f1, I1 l1, I2 f2, I2 l2, Cmp comp={}) -> decltype(comp(*f1, *f2))
Lexicographically compares two ranges [first1, last1) and [first2, last2) using three-way comparison ...
Definition algorithm.hh:37
Dune::FieldVector
vector space out of a tensor product of fields.
Definition fvector.hh:97
Dune::FieldVector::FieldVector
constexpr FieldVector(const FieldVector &)=default
Copy constructor with default behavior.
Dune::FieldVector< T, n >::size_type
typename Base::size_type size_type
Definition fvector.hh:109
Dune::FieldVector::FieldVector
constexpr FieldVector(const std::initializer_list< K > &l)
Construct from a std::initializer_list of values.
Definition fvector.hh:144
Dune::FieldVector< T, n >::size
static constexpr size_type size() noexcept
Definition fvector.hh:218
Dune::FieldVector::operator+
friend constexpr auto operator+(const FieldVector &a, const S &b) noexcept
Binary addition, when using FieldVector<K,1> like K.
Definition fvector.hh:371
Dune::FieldVector::operator/
friend constexpr auto operator/(const FieldVector &a, const S &b) noexcept
Vector space division by scalar.
Definition fvector.hh:352
Dune::FieldVector< T, n >::value_type
typename Base::value_type value_type
Definition fvector.hh:112
Dune::FieldVector< T, n >::dimension
static constexpr int dimension
Definition fvector.hh:106
Dune::FieldVector::FieldVector
constexpr FieldVector() noexcept(std::is_nothrow_default_constructible_v< K >)
Default constructor, making value-initialized vector with all components set to zero.
Definition fvector.hh:123
Dune::FieldVector< T, n >::operator<=>
friend constexpr auto operator<=>(const FieldVector &a, const FieldVector< T, SIZE > &b) noexcept
Definition fvector.hh:297
Dune::FieldVector::operator==
friend constexpr bool operator==(const FieldVector &a, const S &b) noexcept
comparing FieldVectors<1> with scalar for equality
Definition fvector.hh:280
Dune::FieldVector::FieldVector
constexpr FieldVector(const DenseVector< V > &x)
Constructor from another dense vector if the elements are assignable to K.
Definition fvector.hh:156
Dune::FieldVector::operator=
constexpr FieldVector & operator=(const DenseVector< V > &x)
Assignment from another dense vector.
Definition fvector.hh:181
Dune::FieldVector::data
constexpr const K * data() const noexcept
Return pointer to underlying array.
Definition fvector.hh:253
Dune::FieldVector< T, n >::reference
value_type & reference
Definition fvector.hh:115
Dune::FieldVector< T, n >::const_reference
const value_type & const_reference
Definition fvector.hh:118
Dune::FieldVector::operator[]
constexpr reference operator[](size_type i)
Return a reference to the ith element.
Definition fvector.hh:230
Dune::FieldVector::data
constexpr K * data() noexcept
Return pointer to underlying array.
Definition fvector.hh:247
Dune::DenseVector::value_type
Traits::value_type value_type
export the type representing the field
Definition densevector.hh:265
Dune::DenseVector< FieldVector< K, SIZE > >::operator-
constexpr derived_type operator-() const
Definition densevector.hh:469
Dune::DenseVector::end
constexpr Iterator end()
end iterator
Definition densevector.hh:368
Dune::DenseVector::size_type
Traits::size_type size_type
The type used for the index access and size operation.
Definition densevector.hh:274
Dune::DenseVector< FieldVector< K, SIZE > >::DenseVector
constexpr DenseVector()=default
Dune::DenseVector::begin
constexpr Iterator begin()
begin iterator
Definition densevector.hh:362
Dune::DenseVector::size
constexpr size_type size() const
size method
Definition densevector.hh:351
Dune::FieldTraits
Definition ftraits.hh:26
Dune::FieldTraits::field_type
T field_type
export the type representing the field
Definition ftraits.hh:28
Dune::FieldTraits::real_type
T real_type
export the type representing the real type of the field
Definition ftraits.hh:30
Dune::DenseMatVecTraits< FieldVector< K, SIZE > >::value_type
K value_type
Definition fvector.hh:47
Dune::DenseMatVecTraits< FieldVector< K, SIZE > >::container_type
std::array< K, SIZE > container_type
Definition fvector.hh:46
Dune::DenseMatVecTraits< FieldVector< K, SIZE > >::size_type
container_type::size_type size_type
Definition fvector.hh:48
Dune::DenseMatVecTraits< FieldVector< K, SIZE > >::derived_type
FieldVector< K, SIZE > derived_type
Definition fvector.hh:45
Dune::FieldTraits< FieldVector< K, SIZE > >::real_type
FieldTraits< K >::real_type real_type
Definition fvector.hh:55
Dune::FieldTraits< FieldVector< K, SIZE > >::field_type
FieldTraits< K >::field_type field_type
Definition fvector.hh:54
Dune::IsFieldVectorSizeCorrect
TMP to check the size of a DenseVectors statically, if possible.
Definition fvector.hh:68
Dune::IsFieldVectorSizeCorrect::value
static constexpr bool value
True if C is not of type FieldVector or its dimension is not equal SIZE.
Definition fvector.hh:73
Dune::IsFieldVectorSizeCorrect< FieldVector< T, SIZE >, SIZE >::value
static constexpr bool value
Definition fvector.hh:79
Dune::IsFieldVectorSizeCorrect< FieldVector< T, SIZE1 >, SIZE >::value
static constexpr bool value
Definition fvector.hh:85
Dune::MathOverloads::ADLTag
Tag to make sure the functions in this namespace can be found by ADL.
Definition math.hh:212
Dune::DenseMatVecTraits
Definition matvectraits.hh:31
Dune::PromotionTraits::PromotedType
decltype(std::declval< T1 >()+std::declval< T2 >()) PromotedType
Definition promotiontraits.hh:28
Dune::PriorityTag
Helper class for tagging priorities.
Definition typeutilities.hh:73
Dune::Std::three_way_comparable_with
The concept std::three_way_comparable_with specifies that the three way comparison operator <=> on (p...
Definition compare.hh:100