dune-common 2.12-git
Loading...
Searching...
No Matches
fvector.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5#ifndef DUNE_COMMON_FVECTOR_HH
6#define DUNE_COMMON_FVECTOR_HH
7
8#include <algorithm>
9#include <array>
10#include <cmath>
11#include <concepts>
12#include <cstdlib>
13#include <cstring>
14#include <type_traits>
15#include <utility>
16#include <initializer_list>
17
22#include <dune/common/math.hh>
29
30namespace Dune {
31
41 template< class K, int SIZE > class FieldVector;
42 template< class K, int SIZE >
44 {
47 typedef K value_type;
48 typedef typename container_type::size_type size_type;
49 };
50
51 template< class K, int SIZE >
52 struct FieldTraits< FieldVector<K,SIZE> >
53 {
56 };
57
66 template<typename C, int SIZE>
68 {
73 constexpr static bool value = true;
74 };
75
76 template<typename T, int SIZE>
78 {
79 constexpr static bool value = true;
80 };
81
82 template<typename T, int SIZE, int SIZE1>
84 {
85 constexpr static bool value = false;
86 };
87
88
102 template< class K, int SIZE >
104 public DenseVector< FieldVector<K,SIZE> >
105 {
107
109 std::array<K,SIZE> _data;
110
111 public:
112
114 static constexpr int dimension = SIZE;
115
117 using size_type = typename Base::size_type;
118
120 using value_type = typename Base::value_type;
121
124
127
128 public:
129
131 constexpr FieldVector ()
132 noexcept(std::is_nothrow_default_constructible_v<K>)
133 : _data{}
134 {}
135
137 explicit(SIZE != 1)
138 constexpr FieldVector (const value_type& value) noexcept
139 : _data{filledArray<SIZE>(value)}
140 {}
141
143 template<Concept::Number S>
144 requires (std::constructible_from<K,S>)
145 explicit(SIZE != 1)
146 constexpr FieldVector (const S& scalar)
147 noexcept(std::is_nothrow_constructible_v<K,S>)
148 : _data{filledArray<SIZE,K>(K(scalar))}
149 {}
150
153 : _data{}
154 {
155 DUNE_ASSERT_BOUNDS(l.size() == size());
156 for (size_type i = 0; i < size(); ++i)
157 _data[i] = std::data(l)[i];
158 }
159
161 template<class V>
163 std::is_assignable_v<K&, decltype(std::declval<const V&>()[0])>)
164 constexpr FieldVector (const DenseVector<V>& x)
165 {
166 DUNE_ASSERT_BOUNDS(x.size() == size());
167 for (size_type i = 0; i < size(); ++i)
168 _data[i] = x[i];
169 }
170
172 template<class OtherK>
173 requires (std::is_assignable_v<K&, const OtherK&>)
174 explicit constexpr FieldVector (const FieldVector<OtherK, SIZE>& x)
175 noexcept(std::is_nothrow_assignable_v<K&, const OtherK&>)
176 {
177 for (size_type i = 0; i < size(); ++i)
178 _data[i] = x[i];
179 }
180
182 constexpr FieldVector (const FieldVector&) = default;
183
184
186 template<class V>
188 std::is_assignable_v<K&, decltype(std::declval<const V&>()[0])>)
190 {
191 DUNE_ASSERT_BOUNDS(x.size() == size());
192 for (size_type i = 0; i < size(); ++i)
193 _data[i] = x[i];
194 return *this;
195 }
196
198 template<Concept::Number S>
199 requires std::constructible_from<K,S>
200 constexpr FieldVector& operator= (const S& scalar)
201 noexcept(std::is_nothrow_constructible_v<K,S>)
202 {
203 _data.fill(K(scalar));
204 return *this;
205 }
206
208 template<class OtherK>
209 requires (std::is_assignable_v<K&, const OtherK&>)
210 constexpr FieldVector& operator= (const FieldVector<OtherK, SIZE>& x)
211 noexcept(std::is_nothrow_assignable_v<K&, const OtherK&>)
212 {
213 for (size_type i = 0; i < size(); ++i)
214 _data[i] = x[i];
215 return *this;
216 }
217
219 constexpr FieldVector& operator= (const FieldVector&) = default;
220
221
224
226 static constexpr size_type size () noexcept { return SIZE; }
227
229
230
233
239 {
241 return _data[i];
242 }
243
249 {
251 return _data[i];
252 }
253
255 constexpr K* data () noexcept
256 {
257 return _data.data();
258 }
259
261 constexpr const K* data () const noexcept
262 {
263 return _data.data();
264 }
265
267 [[deprecated("Please use FieldVector objects of size 1 like vectors, not like scalars!")]]
268 constexpr operator const_reference () const noexcept
269 requires(SIZE == 1)
270 {
271 return _data[0];
272 }
273
275 [[deprecated("Please use FieldVector objects of size 1 like vectors, not like scalars!")]]
276 constexpr operator reference () noexcept
277 requires(SIZE == 1)
278 {
279 return _data[0];
280 }
281
283
284
287
289 template<Concept::Number S>
290 [[deprecated("Please use FieldVector objects of size 1 like vectors, not like scalars!")]]
291 friend constexpr bool operator== (const FieldVector& a, const S& b) noexcept
292 requires(SIZE == 1)
293 {
294 return a._data[0] == b;
295 }
296
298 template<Concept::Number S>
299 [[deprecated("Please use FieldVector objects of size 1 like vectors, not like scalars!")]]
300 friend constexpr bool operator== (const S& a, const FieldVector& b) noexcept
301 requires(SIZE == 1)
302 {
303 return a == b._data[0];
304 }
305
307 template<class T>
309 friend constexpr auto operator<=> (const FieldVector& a, const FieldVector<T,SIZE>& b) noexcept
310 {
311#if __cpp_lib_three_way_comparison
312 return a._data <=> b._data;
313#else
314 return Std::lexicographical_compare_three_way(a.begin(), a.end(), b.begin(), b.end());
315#endif
316 }
317
319 template<Concept::Number S>
320 [[deprecated("Please use FieldVector objects of size 1 like vectors, not like scalars!")]]
321 friend constexpr auto operator<=> (const FieldVector& a, const S& b) noexcept
322 requires(SIZE == 1)
323 {
324 return a._data[0] <=> b;
325 }
326
328 template<Concept::Number S>
329 [[deprecated("Please use FieldVector objects of size 1 like vectors, not like scalars!")]]
330 friend constexpr auto operator<=> (const S& a, const FieldVector& b) noexcept
331 requires(SIZE == 1)
332 {
333 return a <=> b._data[0];
334 }
335
337
338
341
343 template<Concept::Number S>
344 friend constexpr auto operator* (const FieldVector& a, const S& b) noexcept
345 {
346 using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
348 for (size_type i = 0; i < size(); ++i)
349 result[i] = a[i] * b;
350 return result;
351 }
352
354 template<Concept::Number S>
355 friend constexpr auto operator* (const S& a, const FieldVector& b) noexcept
356 {
357 using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
359 for (size_type i = 0; i < size(); ++i)
360 result[i] = a * b[i];
361 return result;
362 }
363
365 template<Concept::Number S>
366 friend constexpr auto operator/ (const FieldVector& a, const S& b) noexcept
367 {
368 using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
370 for (size_type i = 0; i < size(); ++i)
371 result[i] = a[i] / b;
372 return result;
373 }
374
376 template<Concept::Number S>
377 [[deprecated("Please use FieldVector objects of size 1 like vectors, not like scalars!")]]
378 friend constexpr FieldVector operator/ (const S& a, const FieldVector& b) noexcept
379 requires(SIZE == 1)
380 {
381 return FieldVector{a / b[0]};
382 }
383
385 template<Concept::Number S>
386 [[deprecated("Please use FieldVector objects of size 1 like vectors, not like scalars!")]]
387 friend constexpr auto operator+ (const FieldVector& a, const S& b) noexcept
388 requires(SIZE == 1)
389 {
390 using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
392 }
393
395 template<Concept::Number S>
396 [[deprecated("Please use FieldVector objects of size 1 like vectors, not like scalars!")]]
397 friend constexpr auto operator+ (const S& a, const FieldVector& b) noexcept
398 requires(SIZE == 1)
399 {
400 using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
402 }
403
405 template<Concept::Number S>
406 [[deprecated("Please use FieldVector objects of size 1 like vectors, not like scalars!")]]
407 friend constexpr auto operator- (const FieldVector& a, const S& b) noexcept
408 requires(SIZE == 1)
409 {
410 using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
412 }
413
415 template<Concept::Number S>
416 [[deprecated("Please use FieldVector objects of size 1 like vectors, not like scalars!")]]
417 friend constexpr auto operator- (const S& a, const FieldVector& b) noexcept
418 requires(SIZE == 1)
419 {
420 using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
422 }
423
424 using Base::operator+=;
425
427 template<class OtherK>
428 [[deprecated("Please use FieldVector objects of size 1 like vectors, not like scalars!")]]
430 requires ((SIZE!=1) and std::is_convertible_v<OtherK, K>)
431 {
432 (*this) += other[0];
433 return *this;
434 }
435
436 using Base::operator-=;
437
439 template<class OtherK>
440 [[deprecated("Please use FieldVector objects of size 1 like vectors, not like scalars!")]]
442 requires ((SIZE!=1) and std::is_convertible_v<OtherK, K>)
443 {
444 (*this) -= other[0];
445 return *this;
446 }
447
448 using Base::operator*=;
449
451 template<class OtherK>
452 [[deprecated("Please use FieldVector objects of size 1 like vectors, not like scalars!")]]
454 requires (std::is_convertible_v<OtherK, K>)
455 {
456 (*this) *= other[0];
457 return *this;
458 }
459
460 using Base::operator/=;
461
463 template<class OtherK>
464 [[deprecated("Please use FieldVector objects of size 1 like vectors, not like scalars!")]]
466 requires (std::is_convertible_v<OtherK, K>)
467 {
468 (*this) /= other[0];
469 return *this;
470 }
471
473 };
474
486 template<class K, int SIZE>
488 {
490 for (int i = 0; i < SIZE; ++i)
491 in >> w[i];
492 if (in)
493 v = w;
494 return in;
495 }
496
497 /* Overloads for common classification functions */
498 namespace MathOverloads {
499
501 template<class K, int SIZE>
503 {
504 bool out = true;
505 for (int i = 0; i < SIZE; ++i) {
506 out &= Dune::isFinite(b[i]);
507 }
508 return out;
509 }
510
512 template<class K, int SIZE>
514 {
515 bool out = false;
516 for (int i = 0; i < SIZE; ++i) {
517 out |= Dune::isInf(b[i]);
518 }
519 return out;
520 }
521
523 template<class K, int SIZE,
526 {
527 bool out = false;
528 for (int i = 0; i < SIZE; ++i) {
529 out |= Dune::isNaN(b[i]);
530 }
531 return out;
532 }
533
535 template<class K,
539 {
540 return Dune::isUnordered(b[0],c[0]);
541 }
542
543 } // end namespace MathOverloads
544
547} // end namespace Dune
548
549#endif // DUNE_COMMON_FVECTOR_HH
Macro for wrapping boundary checks.
Type traits to determine the type of reals (when working with complex numbers)
This file provides some concepts introduced in the C++20 standard library <compare> and <concepts> no...
Implements the dense vector interface, with an exchangeable storage class.
Utilities for type computations, constraining overloads, ...
Compute type of the result of an arithmetic operation involving two different number types.
Traits for type conversions and type information.
Some useful basic math stuff.
Utility to generate an array with a certain value.
constexpr std::array< T, n > filledArray(const T &t)
Return an array filled with the provided value.
Definition filledarray.hh:33
std::istream & operator>>(std::istream &stream, std::tuple< Ts... > &t)
Read a std::tuple.
Definition streamoperators.hh:43
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition boundschecking.hh:30
STL namespace.
Dune namespace
Definition alignedallocator.hh:13
bool isNaN(const FieldVector< K, SIZE > &b, PriorityTag< 2 >, ADLTag)
Returns whether any entry is NaN.
Definition fvector.hh:525
bool isInf(const FieldVector< K, SIZE > &b, PriorityTag< 2 >, ADLTag)
Returns whether any entry is infinite.
Definition fvector.hh:513
auto isFinite(const FieldVector< K, SIZE > &b, PriorityTag< 2 >, ADLTag)
Returns whether all entries are finite.
Definition fvector.hh:502
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:537
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
vector space out of a tensor product of fields.
Definition fvector.hh:105
constexpr FieldVector(const FieldVector &)=default
Copy constructor with default behavior.
typename Base::size_type size_type
The type used for the index access and size operation.
Definition fvector.hh:117
constexpr FieldVector(const std::initializer_list< K > &l)
Construct from a std::initializer_list of values.
Definition fvector.hh:152
constexpr FieldVector< K, SIZE > & operator-=(const FieldVector< OtherK, 1 > &other)
Subtraction with second operand FieldVector<K,1> used like K.
Definition fvector.hh:441
static constexpr size_type size() noexcept
Obtain the number of elements stored in the vector.
Definition fvector.hh:226
friend constexpr auto operator+(const FieldVector &a, const S &b) noexcept
Binary addition, when using FieldVector<K,1> like K.
Definition fvector.hh:387
constexpr FieldVector< K, SIZE > & operator+=(const FieldVector< OtherK, 1 > &other)
Addition with second operand FieldVector<K,1> used like K.
Definition fvector.hh:429
friend constexpr auto operator-(const FieldVector &a, const S &b) noexcept
Binary subtraction, when using FieldVector<K,1> like K.
Definition fvector.hh:407
friend constexpr auto operator/(const FieldVector &a, const S &b) noexcept
Vector space division by scalar.
Definition fvector.hh:366
typename Base::value_type value_type
The type of the elements stored in the vector.
Definition fvector.hh:120
friend constexpr auto operator*(const FieldVector &a, const S &b) noexcept
Vector space multiplication with scalar.
Definition fvector.hh:344
static constexpr int dimension
The size of this vector.
Definition fvector.hh:114
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:131
constexpr FieldVector< K, SIZE > & operator/=(const FieldVector< OtherK, 1 > &other)
Division with second operand FieldVector<K,1> used like K.
Definition fvector.hh:465
friend constexpr auto operator<=>(const FieldVector &a, const FieldVector< T, SIZE > &b) noexcept
three-way comparison of FieldVectors
Definition fvector.hh:309
constexpr FieldVector< K, SIZE > & operator*=(const FieldVector< OtherK, 1 > &other)
Multiplication with second operand FieldVector<K,1> used like K.
Definition fvector.hh:453
friend constexpr bool operator==(const FieldVector &a, const S &b) noexcept
comparing FieldVectors<1> with scalar for equality
Definition fvector.hh:291
constexpr FieldVector(const DenseVector< V > &x)
Constructor from another dense vector if the elements are assignable to K.
Definition fvector.hh:164
constexpr FieldVector & operator=(const DenseVector< V > &x)
Assignment from another dense vector.
Definition fvector.hh:189
constexpr const K * data() const noexcept
Return pointer to underlying array.
Definition fvector.hh:261
value_type & reference
The type used for references to the vector entries.
Definition fvector.hh:123
const value_type & const_reference
The type used for const references to the vector entries.
Definition fvector.hh:126
constexpr reference operator[](size_type i)
Return a reference to the ith element.
Definition fvector.hh:238
constexpr K * data() noexcept
Return pointer to underlying array.
Definition fvector.hh:255
Interface for a class of dense vectors over a given field.
Definition densevector.hh:244
Traits::value_type value_type
export the type representing the field
Definition densevector.hh:268
constexpr Iterator end()
end iterator
Definition densevector.hh:371
Traits::size_type size_type
The type used for the index access and size operation.
Definition densevector.hh:277
constexpr Iterator begin()
begin iterator
Definition densevector.hh:365
constexpr size_type size() const
size method
Definition densevector.hh:354
Definition ftraits.hh:26
T field_type
export the type representing the field
Definition ftraits.hh:28
T real_type
export the type representing the real type of the field
Definition ftraits.hh:30
std::array< K, SIZE > container_type
Definition fvector.hh:46
container_type::size_type size_type
Definition fvector.hh:48
FieldVector< K, SIZE > derived_type
Definition fvector.hh:45
FieldTraits< K >::real_type real_type
Definition fvector.hh:55
FieldTraits< K >::field_type field_type
Definition fvector.hh:54
TMP to check the size of a DenseVectors statically, if possible.
Definition fvector.hh:68
static constexpr bool value
True if C is not of type FieldVector or its dimension is not equal SIZE.
Definition fvector.hh:73
Tag to make sure the functions in this namespace can be found by ADL.
Definition math.hh:212
Definition matvectraits.hh:31
decltype(std::declval< T1 >()+std::declval< T2 >()) PromotedType
Definition promotiontraits.hh:28
Helper class for tagging priorities.
Definition typeutilities.hh:73
The concept std::three_way_comparable_with specifies that the three way comparison operator <=> on (p...
Definition compare.hh:100
T data(T... args)
T fill(T... args)