VectorAlgebra.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include "DataStructures/TempBuffer.hpp"
7 #include "DataStructures/VectorImpl.hpp"
8 #include "Utilities/Gsl.hpp"
9 
10 // @{
11 /// \ingroup UtilitiesGroup
12 /// \brief Computes the outer product between two vectors.
13 /// \details For vectors \f$A\f$ and \f$B\f$, the resulting outer product is
14 /// \f$\{A_1 B_1,\, A_2 B_1\, \dots\, A_N B_1,\, A_1 B_2\, \dots\, A_N B_M\}\f$.
15 /// This is useful for generating separable volume data from its constituent
16 /// inputs.
17 template <typename LhsVectorType, typename RhsVectorType,
18  typename ResultVectorType =
19  typename blaze::MultTrait<LhsVectorType, RhsVectorType>::Type>
21  const LhsVectorType& lhs,
22  const RhsVectorType& rhs) noexcept {
23  result->destructive_resize(lhs.size() * rhs.size());
24  for (size_t i = 0; i < rhs.size(); ++i) {
25  ResultVectorType view{result->data() + i * lhs.size(), lhs.size()};
26  view = rhs[i] * lhs;
27  }
28 }
29 
30 template <typename LhsVectorType, typename RhsVectorType,
31  typename ResultVectorType =
32  typename blaze::MultTrait<LhsVectorType, RhsVectorType>::Type>
33 ResultVectorType outer_product(const LhsVectorType& lhs,
34  const RhsVectorType& rhs) noexcept {
35  auto result = ResultVectorType{lhs.size() * rhs.size()};
36  outer_product(make_not_null(&result), lhs, rhs);
37  return result;
38 }
39 // @}
40 
41 // @{
42 /// \ingroup UtilitiesGroup
43 /// \brief Repeats a vector `times_to_repeat` times.
44 /// \details This can be useful for generating data that consists of the same
45 /// block of values duplicated a number of times. For instance, this can be used
46 /// to create a vector representing three-dimensional volume data from a
47 /// corresponding two-dimensional vector data, if the two-dimensional data
48 /// corresponds to the two fastest-varying directions of the desired
49 /// three-dimensional representation. The result would then be uniform in the
50 /// slowest-varying direction of the three dimensional grid.
51 template <typename VectorType>
53  const VectorType& to_repeat,
54  const size_t times_to_repeat) noexcept {
55  result->destructive_resize(to_repeat.size() * times_to_repeat);
56  for (size_t i = 0; i < times_to_repeat; ++i) {
57  VectorType view{result->data() + i * to_repeat.size(), to_repeat.size()};
58  view = to_repeat;
59  }
60 }
61 
62 template <typename VectorType>
63 VectorType repeat(const VectorType& to_repeat,
64  const size_t times_to_repeat) noexcept {
65  auto result = VectorType{to_repeat.size() * times_to_repeat};
66  repeat(make_not_null(&result), to_repeat, times_to_repeat);
67  return result;
68 }
69 // @}
void outer_product(const gsl::not_null< ResultVectorType *> result, const LhsVectorType &lhs, const RhsVectorType &rhs) noexcept
Computes the outer product between two vectors.
Definition: VectorAlgebra.hpp:20
void repeat(const gsl::not_null< VectorType *> result, const VectorType &to_repeat, const size_t times_to_repeat) noexcept
Repeats a vector times_to_repeat times.
Definition: VectorAlgebra.hpp:52
Defines functions and classes from the GSL.
gsl::not_null< T * > make_not_null(T *ptr) noexcept
Construct a not_null from a pointer. Often this will be done as an implicit conversion, but it may be necessary to perform the conversion explicitly when type deduction is desired.
Definition: Gsl.hpp:863
Require a pointer to not be a nullptr
Definition: ConservativeFromPrimitive.hpp:12