SquareRoot.hpp
2 // See LICENSE.txt for details.
3
4 #pragma once
5
6 #include <array>
7 #include <cmath>
8 #include <cstddef>
9 #include <type_traits>
10 #include <utility>
11
14 #include "Utilities/Requires.hpp"
15 #include "Utilities/TMPL.hpp"
16
17 namespace TensorExpressions {
18 /// \ingroup TensorExpressionsGroup
19 /// \brief Defines the tensor expression representing the square root of a
20 /// tensor expression that evaluates to a rank 0 tensor
21 ///
22 /// \tparam T the type of the tensor expression of which to take the square
23 /// root
24 template <typename T>
25 struct SquareRoot
26  : public TensorExpression<SquareRoot<T>, typename T::type, tmpl::list<>,
27  tmpl::list<>, tmpl::list<>> {
28  static_assert(
29  std::is_base_of<TensorExpression<T, typename T::type, tmpl::list<>,
30  tmpl::list<>, tmpl::list<>>,
31  T>::value,
32  "Can only take the square root of a tensor expression that evaluates to "
33  "a rank 0 tensor.");
34  using type = typename T::type;
35  using symmetry = tmpl::list<>;
36  using index_list = tmpl::list<>;
37  using args_list = tmpl::list<>;
38  static constexpr auto num_tensor_indices = 0;
39
40  SquareRoot(T t) : t_(std::move(t)) {}
41  ~SquareRoot() override = default;
42
43  /// \brief Returns the square root of the component of the tensor evaluated
44  /// from the RHS tensor expression
45  ///
46  /// \details
47  /// SquareRoot only supports tensor expressions that evaluate to a rank 0
48  /// Tensor. This is why this template is only defined for the case where
49  /// TensorIndices is empty.
50  ///
51  /// \tparam TensorIndices the TensorIndexs of the LHS tensor and RHS tensor
52  /// expression
53  /// \param multi_index the multi-index of the component of which to take the
54  /// square root
55  /// \return the square root of the component of the tensor evaluated from the
56  /// RHS tensor expression
57  template <typename... TensorIndices,
58  Requires<sizeof...(TensorIndices) == 0> = nullptr>
59  SPECTRE_ALWAYS_INLINE decltype(auto) get(
60  const std::array<size_t, 0>& multi_index) const noexcept {
61  return sqrt(t_.template get(multi_index));
62  }
63
64  private:
65  T t_;
66 };
67 } // namespace TensorExpressions
68
69 /// \ingroup TensorExpressionsGroup
70 /// \brief Returns the tensor expression representing the square root of a
71 /// tensor expression that evaluates to a rank 0 tensor
72 ///
73 /// \details
74 /// t must be an expression that, when evaluated, would be a rank 0 tensor.
75 /// For example, if R and S are Tensors, here is a non-exhaustive list of
76 /// some of the acceptable forms that t could take:
77 /// - R()
78 /// - R(ti_A, ti_a)
79 /// - (R(ti_A, ti_B) * S(ti_a, ti_b))
80 ///
81 /// \param t the type of the tensor expression of which to take the square root
82 template <typename T>
84  const TensorExpression<T, typename T::type, tmpl::list<>, tmpl::list<>,
85  tmpl::list<>>& t) {
87 }
TensorExpressions::SquareRoot
Defines the tensor expression representing the square root of a tensor expression that evaluates to a...
Definition: SquareRoot.hpp:25
utility
TensorExpression.hpp
sqrt
auto sqrt(const TensorExpression< T, typename T::type, tmpl::list<>, tmpl::list<>, tmpl::list<>> &t)
Returns the tensor expression representing the square root of a tensor expression that evaluates to a...
Definition: SquareRoot.hpp:83
TensorExpressions::SquareRoot::get
decltype(auto) get(const std::array< size_t, 0 > &multi_index) const noexcept
Returns the square root of the component of the tensor evaluated from the RHS tensor expression.
Definition: SquareRoot.hpp:59
cmath
TensorExpressions
SPECTRE_ALWAYS_INLINE
#define SPECTRE_ALWAYS_INLINE
Definition: ForceInline.hpp:16
cstddef
array
ForceInline.hpp
Requires.hpp
Requires
typename Requires_detail::requires_impl< B >::template_error_type_failed_to_meet_requirements_on_template_parameters Requires
Express requirements on the template parameters of a function or class, replaces std::enable_if_t
Definition: Requires.hpp:67
type_traits
TMPL.hpp
std::is_base_of