29#include <boost/qvm/deduce_vec.hpp>
30#include <boost/qvm/vec_traits.hpp>
42#include <initializer_list>
54#if defined(__GNUC__) and not defined(__clang__)
55#pragma GCC diagnostic push
56#pragma GCC diagnostic ignored "-Wanalyzer-out-of-bounds"
61template <
typename T, std::
size_t N>
class Vector :
public Array<T, N> {
67 using Base::operator[];
80 template <
class U>
struct is_vector : std::false_type {};
81 template <
class U, std::
size_t Np>
88 void swap(
Vector &rhs) { std::ranges::swap_ranges(*
this, rhs); }
91 constexpr void copy_init(T
const *values)
noexcept {
92 for (std::size_t i{0}; i != N; ++i) {
93 (*this)[i] = values[i];
102 template <
class Range>
104 not std::is_same_v<std::remove_cvref_t<Range>, T[N]>)
106 :
Vector(std::begin(rng), std::end(rng)) {}
108#if __cpp_lib_containers_ranges
109 template <std::ranges::input_range Range>
110 Vector(std::from_range_t, Range &&rng)
114 explicit constexpr Vector(T
const (&v)[N]) noexcept : Base() {
115 if constexpr (N != 0) {
116 copy_init(std::cbegin(v));
122 constexpr Vector(std::initializer_list<T> v) : Base() {
124 throw std::length_error(
125 "Construction of Vector from Container of wrong length.");
127 if constexpr (N != 0) {
128 copy_init(v.begin());
132 template <
typename InputIterator>
133 Vector(InputIterator first, InputIterator last) : Base() {
134 if (std::distance(first, last) == N) {
135 std::copy_n(first, N,
begin());
137 throw std::length_error(
138 "Construction of Vector from Container of wrong length.");
146 for (std::size_t i = 0u; i != N; ++i) {
156 constexpr std::span<const T, N>
as_span() const noexcept {
157 return std::span<const T, N>(
begin(), N);
160 constexpr operator std::span<const T, N>() const noexcept {
167 std::ranges::transform(*
this, ret.
begin(),
168 [](T
const &e) { return static_cast<U>(e); });
173 constexpr T
norm2()
const {
return (*
this) * (*this); }
183 auto const l =
norm();
198 auto const l =
norm();
199 return (l != T(0)) ? (*this) / l : *
this;
219template <std::
size_t N,
typename T,
typename U,
typename Op>
224 using R =
decltype(std::declval<T>() + std::declval<U>());
228 std::transform(std::begin(a), std::end(a), std::begin(b), std::begin(ret),
235#define ESPRESSO_VECTOR_COMPARISON(op) \
236 template <std::size_t N, typename T> \
238 operator op(Vector<T, N> const &a, Vector<T, N> const &b) noexcept( \
239 noexcept(std::declval<T const &>() op std::declval<T const &>())) { \
240 for (std::size_t i = 0u; i < N; ++i) { \
241 if (not(a[i] op b[i])) { \
253#undef ESPRESSO_VECTOR_COMPARISON
255template <std::
size_t N,
typename T,
typename U>
257 return detail::binary_op(a, b, std::plus<>());
260template <std::
size_t N,
typename T>
263 std::ranges::transform(a, b, std::begin(a), std::plus<T>());
267template <std::
size_t N,
typename T,
typename U>
269 return detail::binary_op(a, b, std::minus<>());
272template <std::
size_t N,
typename T>
275 std::ranges::transform(a, std::begin(ret), std::negate<T>());
279template <std::
size_t N,
typename T>
282 std::ranges::transform(a, b, std::begin(a), std::minus<T>());
287template <std::
size_t N,
typename T,
class U>
288 requires(std::is_arithmetic_v<U>)
290 using R =
decltype(a * std::declval<T>());
292 std::ranges::transform(b, std::begin(ret), [a](T
const &v) {
return a * v; });
296template <std::
size_t N,
typename T,
class U>
297 requires(std::is_arithmetic_v<U>)
299 using R =
decltype(std::declval<T>() * b);
301 std::ranges::transform(a, std::begin(ret), [b](T
const &v) {
return b * v; });
305template <std::
size_t N,
typename T>
307 std::ranges::transform(b, std::begin(b), [a](T
const &v) {
return a * v; });
312template <std::
size_t N,
typename T,
class U>
314 using R =
decltype(std::declval<T>() / b);
316 std::ranges::transform(a, std::begin(ret), [b](T
const &v) {
return v / b; });
320template <std::
size_t N,
typename T,
class U>
322 using R =
decltype(a / std::declval<T>());
324 std::ranges::transform(b, std::begin(ret), [a](T
const &v) {
return a / v; });
328template <std::
size_t N,
typename T>
330 std::ranges::transform(a, std::begin(a), [b](T
const &v) {
return v / b; });
335template <
class T>
using is_vector = Vector<int, 1>::is_vector<T>;
339template <std::
size_t N,
typename T,
class U>
340 requires(not(detail::is_vector<T>::value or detail::is_vector<U>::value))
342 using R =
decltype(std::declval<T>() * std::declval<U>());
346 for (std::size_t i = 0u; i < N; ++i) {
352template <std::
size_t N,
typename T,
class U>
353 requires(std::is_integral_v<T> and std::is_integral_v<U>)
355 using R =
decltype(std::declval<T>() % std::declval<U>());
357 std::ranges::transform(a, b, std::begin(ret), std::modulus<>());
364 using R =
decltype(
sqrt(std::declval<T>()));
366 std::ranges::transform(a, ret.
begin(), [](T
const &v) { return sqrt(v); });
373 T v[3] = {a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2],
374 a[0] * b[1] - a[1] * b[0]};
380 return std::accumulate(v.
cbegin(), v.
cend(), T{1}, std::multiplies<T>());
383template <
class T,
class U, std::
size_t N>
385 using R =
decltype(std::declval<T>() * std::declval<U>());
386 auto constexpr proj = std::identity{};
388 std::ranges::transform(a, b, ret.
begin(), std::multiplies<>(), proj, proj);
394template <
typename T,
typename U>
395 requires(not(detail::is_vector<T>::value and detail::is_vector<U>::value))
400template <
class T,
class U, std::
size_t N>
402 using R =
decltype(std::declval<T>() / std::declval<U>());
403 auto constexpr proj = std::identity{};
405 std::ranges::transform(a, b, std::begin(ret), std::divides<>(), proj, proj);
411template <
typename T,
typename U>
412 requires(not(detail::is_vector<T>::value and detail::is_vector<U>::value))
419 return {T{1}, T{0}, T{0}};
421 return {T{0}, T{1}, T{0}};
423 return {T{0}, T{0}, T{1}};
424 throw std::domain_error(
"coordinate out of range");
439template <std::
size_t I,
class T, std::
size_t N>
444template <std::
size_t I,
class T, std::
size_t N>
456template <std::
size_t I,
class T, std::
size_t N>
457struct std::tuple_element<I,
Utils::Vector<T, N>> {
458 static_assert(I < N,
"Utils::Vector index must be in range");
462template <
class T, std::
size_t N>
463struct std::tuple_size<
Utils::Vector<T, N>>
464 : std::integral_constant<std::size_t, N> {};
472 static constexpr std::size_t
dim = N;
475 template <std::
size_t I>
480 template <std::
size_t I>
481 static constexpr inline scalar_type
496template <
typename T>
struct deduce_vec<
Utils::Vector<T, 3>, 3> {
507#if defined(__GNUC__) and not defined(__clang__)
508#pragma GCC diagnostic pop
Array implementation with CUDA support.
#define ESPRESSO_VECTOR_COMPARISON(op)
#define UTILS_ARRAY_BOOST_MPI_T(Container, N)
Mark array types as MPI data types.
#define UTILS_ARRAY_BOOST_BIT_S(Container, N)
Mark array types as MPI bitwise serializable.
#define UTILS_ARRAY_BOOST_CLASS(Container, N, ImplementationLevel)
Redefinition of BOOST_CLASS_IMPLEMENTATION for array types.
#define UTILS_ARRAY_BOOST_TRACK(Container, N, TrackingLevel)
Redefinition of BOOST_CLASS_TRACKING for array types.
Compiler-attribute macros shared across ESPResSo headers.
#define ESPRESSO_ATTR_ALWAYS_INLINE
Vector() noexcept=default
constexpr Vector(T const (&v)[N]) noexcept
DEVICE_QUALIFIER constexpr iterator begin() noexcept
Vector normalized() const
DEVICE_QUALIFIER constexpr const_iterator cbegin() const noexcept
std::vector< T > as_vector() const
Vector(InputIterator first, InputIterator last)
constexpr Vector(std::initializer_list< T > v)
DEVICE_QUALIFIER constexpr const_iterator cend() const noexcept
constexpr std::span< const T, N > as_span() const noexcept
DEVICE_QUALIFIER constexpr iterator end() noexcept
static DEVICE_QUALIFIER constexpr Vector< T, N > broadcast(typename Base::value_type const &value) noexcept
Create a vector that has all entries set to the same value.
constexpr Vector(Range &&rng)
constexpr T norm2() const
constexpr Vector(Array< T, N > const &array) noexcept
auto operator+(Vector< T, N > const &a, Vector< U, N > const &b)
Vector< T, 3 > unit_vector(unsigned int i)
T product(Vector< T, N > const &v)
Vector< T, 3 > vector_product(Vector< T, 3 > const &a, Vector< T, 3 > const &b)
Vector< double, N > VectorXd
T & get(Array< T, N > &a) noexcept
Vector< int, N > VectorXi
auto operator/(Vector< T, N > const &a, U const &b)
ESPRESSO_ATTR_ALWAYS_INLINE auto & operator+=(Vector< T, N > &a, Vector< T, N > const &b)
ESPRESSO_ATTR_ALWAYS_INLINE Vector< T, N > & operator-=(Vector< T, N > &a, Vector< T, N > const &b)
auto hadamard_division(Vector< T, N > const &a, Vector< U, N > const &b)
auto & operator*=(Vector< T, N > &b, T const &a)
auto hadamard_product(Vector< T, N > const &a, Vector< U, N > const &b)
auto operator-(Vector< T, N > const &a, Vector< U, N > const &b)
auto sqrt(Vector< T, N > const &a)
auto & operator/=(Vector< T, N > &a, T const &b)
Vector< float, N > VectorXf
DEVICE_QUALIFIER constexpr reference at(size_type i)
DEVICE_QUALIFIER constexpr bool empty() const noexcept
DEVICE_QUALIFIER constexpr reference back()
DEVICE_QUALIFIER constexpr pointer data() noexcept
DEVICE_QUALIFIER constexpr size_type max_size() const noexcept
DEVICE_QUALIFIER constexpr iterator begin() noexcept
DEVICE_QUALIFIER constexpr const_iterator cbegin() const noexcept
DEVICE_QUALIFIER constexpr const_iterator cend() const noexcept
DEVICE_QUALIFIER constexpr size_type size() const noexcept
DEVICE_QUALIFIER constexpr reference front()
DEVICE_QUALIFIER constexpr iterator end() noexcept
DEVICE_QUALIFIER void fill(const value_type &value)
Meta function to turn a Vector<T, 1> into T.
Utils::Vector< T, 3 > type
static constexpr scalar_type read_element(::Utils::Vector< T, N > const &v)
static constexpr std::size_t dim
static scalar_type & write_element_idx(std::size_t i, ::Utils::Vector< T, N > &v)
static constexpr scalar_type & write_element(::Utils::Vector< T, N > &v)
static scalar_type read_element_idx(std::size_t i, ::Utils::Vector< T, N > const &v)