19#ifndef SRC_UTILS_INCLUDE_UTILS_VECTOR_HPP
20#define SRC_UTILS_INCLUDE_UTILS_VECTOR_HPP
29#include <boost/qvm/deduce_vec.hpp>
30#include <boost/qvm/vec_traits.hpp>
39#include <initializer_list>
48template <
typename T, std::
size_t N>
class Vector :
public Array<T, N> {
54 using Array<T, N>::operator[];
74 constexpr void copy_init(T
const *first, T
const *last) {
76 while (first != last) {
82 template <
class Range>
83 explicit constexpr Vector(Range
const &rng)
86 copy_init(std::begin(v), std::end(v));
91 throw std::length_error(
92 "Construction of Vector from Container of wrong length.");
95 copy_init(v.begin(), v.end());
98 template <
typename InputIterator>
99 Vector(InputIterator first, InputIterator last) :
Base() {
100 if (std::distance(first, last) == N) {
101 std::copy_n(first, N,
begin());
103 throw std::length_error(
104 "Construction of Vector from Container of wrong length.");
112 for (std::size_t i = 0u; i != N; ++i) {
123 return std::span<T, N>(
const_cast<T *
>(
begin()),
size());
126 constexpr operator std::span<T, N>()
const {
return as_span(); }
132 [](
auto const &e) { return static_cast<U>(e); });
137 T
norm2()
const {
return (*
this) * (*this); }
148 auto const l =
norm();
150 for (std::size_t i = 0u; i < N; ++i)
151 this->
operator[](i) /= l;
176template <std::
size_t N,
typename T,
typename U,
typename Op>
180 using R =
decltype(op(declval<T>(), declval<U>()));
183 std::transform(std::begin(a), std::end(a), std::begin(b), std::begin(ret),
189template <std::
size_t N,
typename T,
typename Op>
191 std::transform(std::begin(a), std::end(a), std::begin(b), std::begin(a), op);
195template <std::
size_t N,
typename T,
typename Op>
197 for (
unsigned int i = 0; i < N; i++) {
199 if (!
static_cast<bool>(op(a[i], b[i]))) {
208template <std::
size_t N,
typename T>
210 return detail::all_of(a, b, std::less<T>());
213template <std::
size_t N,
typename T>
215 return detail::all_of(a, b, std::greater<T>());
218template <std::
size_t N,
typename T>
220 return detail::all_of(a, b, std::less_equal<T>());
223template <std::
size_t N,
typename T>
225 return detail::all_of(a, b, std::greater_equal<T>());
228template <std::
size_t N,
typename T>
230 return detail::all_of(a, b, std::equal_to<T>());
233template <std::
size_t N,
typename T>
238template <std::
size_t N,
typename T,
typename U>
240 return detail::binary_op(a, b, std::plus<>());
243template <std::
size_t N,
typename T>
245 return detail::binary_op_assign(a, b, std::plus<T>());
248template <std::
size_t N,
typename T,
typename U>
250 return detail::binary_op(a, b, std::minus<>());
253template <std::
size_t N,
typename T>
257 std::transform(std::begin(a), std::end(a), std::begin(ret), std::negate<T>());
262template <std::
size_t N,
typename T>
264 return detail::binary_op_assign(a, b, std::minus<T>());
268template <std::size_t N,
typename T,
class U,
269 std::enable_if_t<std::is_arithmetic_v<U>,
bool> =
true>
271 using R =
decltype(a * std::declval<T>());
274 std::transform(std::begin(b), std::end(b), std::begin(ret),
275 [a](T
const &val) {
return a * val; });
280template <std::size_t N,
typename T,
class U,
281 std::enable_if_t<std::is_arithmetic_v<U>,
bool> =
true>
283 using R =
decltype(std::declval<T>() * a);
286 std::transform(std::begin(b), std::end(b), std::begin(ret),
287 [a](T
const &val) {
return a * val; });
292template <std::
size_t N,
typename T>
294 std::transform(std::begin(b), std::end(b), std::begin(b),
295 [a](T
const &val) {
return a * val; });
300template <std::
size_t N,
typename T>
304 std::transform(std::begin(a), std::end(a), ret.
begin(),
305 [b](T
const &val) { return val / b; });
309template <std::
size_t N,
typename T>
313 std::transform(std::begin(b), std::end(b), ret.
begin(),
314 [a](T
const &val) { return a / val; });
318template <std::
size_t N,
typename T>
320 std::transform(std::begin(a), std::end(a), std::begin(a),
321 [b](T
const &val) {
return val / b; });
326template <
class T>
struct is_vector : std::false_type {};
327template <
class T, std::
size_t N>
328struct is_vector<Vector<T, N>> : std::true_type {};
332template <std::size_t N,
typename T,
class U,
333 class = std::enable_if_t<not(detail::is_vector<T>::value or
334 detail::is_vector<U>::value)>>
337 using R =
decltype(declval<T>() * declval<U>());
339 return std::inner_product(std::begin(a), std::end(a), std::begin(b), R{});
343 std::size_t N,
typename T,
class U,
344 class = std::enable_if_t<std::is_integral_v<T> and std::is_integral_v<U>>>
347 using R =
decltype(declval<T>() % declval<U>());
350 std::transform(std::begin(a), std::end(a), std::begin(b), std::begin(ret),
351 [](T
const &ai, U
const &bi) {
return ai % bi; });
361 std::transform(std::begin(a), std::end(a), ret.
begin(),
362 [](T
const &v) { return sqrt(v); });
369 return {a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2],
370 a[0] * b[1] - a[1] * b[0]};
375 return std::accumulate(v.
cbegin(), v.
cend(), T{1}, std::multiplies<T>());
378template <
class T,
class U, std::
size_t N>
381 using R =
decltype(declval<T>() * declval<U>());
385 [](
auto const &ai,
auto const &bi) { return ai * bi; });
392template <
class T,
class U, std::size_t N,
393 class = std::enable_if_t<not(detail::is_vector<T>::value)>>
396 using R =
decltype(declval<T>() * declval<U>());
403template <
class T,
class U, std::size_t N,
404 class = std::enable_if_t<not(detail::is_vector<U>::value)>>
407 using R =
decltype(declval<T>() * declval<U>());
414template <
typename T,
typename U,
415 class = std::enable_if_t<not(detail::is_vector<T>::value or
416 detail::is_vector<U>::value)>>
421template <
class T,
class U, std::
size_t N>
424 using R =
decltype(declval<T>() * declval<U>());
428 [](
auto const &ai,
auto const &bi) { return ai / bi; });
435template <
class T,
class U, std::size_t N,
436 class = std::enable_if_t<not(detail::is_vector<U>::value)>>
439 using R =
decltype(declval<T>() * declval<U>());
446template <
class T,
class U, std::size_t N,
447 class = std::enable_if_t<not(detail::is_vector<T>::value)>>
450 using R =
decltype(declval<T>() * declval<U>());
453 std::transform(std::begin(b), std::end(b), ret.
begin(),
454 [a](T
const &bi) { return a / bi; });
458template <
typename T,
typename U,
459 class = std::enable_if_t<not(detail::is_vector<T>::value or
460 detail::is_vector<U>::value)>>
467 return {T{1}, T{0}, T{0}};
469 return {T{0}, T{1}, T{0}};
471 return {T{0}, T{0}, T{1}};
472 throw std::domain_error(
"coordinate out of range");
487template <std::
size_t I,
class T, std::
size_t N>
492template <
class T, std::
size_t N>
495template <std::
size_t I,
class T, std::
size_t N>
505 static constexpr std::size_t dim = N;
508 template <std::
size_t I>
513 template <std::
size_t I>
514 static constexpr inline scalar_type
529template <
typename T>
struct deduce_vec<
Utils::Vector<T, 3>, 3> {
Array implementation with CUDA support.
#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.
Vector & operator=(Vector const &)=default
constexpr Vector(Range const &rng)
Vector normalized() const
std::vector< T > as_vector() const
Vector(InputIterator first, InputIterator last)
constexpr Vector(std::initializer_list< T > v)
constexpr Vector(T const (&v)[N])
constexpr std::span< T, N > as_span() const
Vector(Vector const &)=default
static DEVICE_QUALIFIER constexpr Vector< T, N > broadcast(typename Base::value_type const &value)
Create a vector that has all entries set to the same value.
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, N > & operator-=(Vector< T, N > &a, Vector< T, N > const &b)
Vector< T, N > & operator/=(Vector< T, N > &a, T const &b)
Vector< T, N > & operator+=(Vector< T, N > &a, Vector< T, N > const &b)
constexpr bool operator<(Vector< T, N > const &a, Vector< T, N > const &b)
constexpr bool operator>=(Vector< T, N > const &a, Vector< T, N > const &b)
constexpr bool operator>(Vector< T, N > const &a, Vector< T, N > const &b)
Vector< T, 3 > vector_product(Vector< T, 3 > const &a, Vector< T, 3 > const &b)
auto get(Array< T, N > const &a) -> std::enable_if_t<(I< N), const T & >
Vector< T, N > operator/(Vector< T, N > const &a, T const &b)
Quaternion< T > operator*(const U &b, const Quaternion< T > &a)
Product quaternion and arithmetic type.
constexpr bool operator==(Vector< T, N > const &a, Vector< T, N > const &b)
constexpr bool operator!=(Vector< T, N > const &a, Vector< T, N > const &b)
Vector< T, N > sqrt(Vector< T, N > const &a)
constexpr bool operator<=(Vector< T, N > const &a, Vector< T, N > const &b)
auto operator%(Vector< T, N > const &a, Vector< U, N > const &b)
auto hadamard_division(Vector< T, N > const &a, Vector< U, N > const &b)
auto hadamard_product(Vector< T, N > const &a, Vector< U, N > const &b)
Vector< T, N > & operator*=(Vector< T, N > &b, T const &a)
auto operator-(Vector< T, N > const &a, Vector< U, N > const &b)
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 turns a Vector<1, T> into T.
typename Utils::Vector< T, 3 > type
static constexpr scalar_type read_element(::Utils::Vector< T, N > const &v)
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)