SquareRoot.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <cstddef>
7 
12 #include "Utilities/TMPL.hpp"
13 
14 namespace TensorExpressions {
15 /// \ingroup TensorExpressionsGroup
16 /// \brief Defines the tensor expression representing the square root of a
17 /// tensor expression that evaluates to a rank 0 tensor
18 ///
19 /// \tparam T the type of the tensor expression of which to take the square
20 /// root
21 template <typename T>
22 struct SquareRoot
23  : public TensorExpression<SquareRoot<T>, typename T::type, tmpl::list<>,
24  tmpl::list<>, tmpl::list<>> {
25  static_assert(
26  std::is_base_of<TensorExpression<T, typename T::type, tmpl::list<>,
27  tmpl::list<>, tmpl::list<>>,
28  T>::value,
29  "Can only take the square root of a tensor expression that evaluates to "
30  "a rank 0 tensor.");
31  using type = typename T::type;
32  using symmetry = tmpl::list<>;
33  using index_list = tmpl::list<>;
34  using args_list = tmpl::list<>;
35  using structure = Tensor_detail::Structure<symmetry>;
36  static constexpr auto num_tensor_indices = 0;
37 
38  SquareRoot(T t) : t_(std::move(t)) {}
39  ~SquareRoot() override = default;
40 
41  /// \brief Returns the square root of the component of the tensor evaluated
42  /// from the RHS tensor expression
43  ///
44  /// \details
45  /// SquareRoot only supports tensor expressions that evaluate to a rank 0
46  /// Tensor. This is why, unlike other derived TensorExpression types, there is
47  /// no second variadic template parameter for the generic indices. In
48  /// addition, this is why this template is only instantiated for the case
49  /// where `Structure` is equal to the Structure of a rank 0 Tensor.
50  ///
51  /// \tparam Structure the Structure of the rank 0 Tensor represented by this
52  /// expression
53  /// \param storage_index the storage index of the component of which to take
54  /// the square root
55  /// \return the square root of the component of the tensor evaluated from the
56  /// RHS tensor expression
57  template <typename Structure>
58  SPECTRE_ALWAYS_INLINE decltype(auto) get(
59  const size_t storage_index) const noexcept {
60  static_assert(std::is_same_v<Structure, structure>,
61  "In retrieving the square root of a tensor expression, the "
62  "provided Structure should be that of a rank 0 Tensor.");
63  ASSERT(storage_index == 0,
64  "In retrieving the square root of a tensor expression, the provided "
65  "storage_index should be 0.");
66  return sqrt(t_.template get<Structure>(storage_index));
67  }
68 
69  private:
70  T t_;
71 };
72 } // namespace TensorExpressions
73 
74 /// \ingroup TensorExpressionsGroup
75 /// \brief Returns the tensor expression representing the square root of a
76 /// tensor expression that evaluates to a rank 0 tensor
77 ///
78 /// \details
79 /// `t` must be an expression that, when evaluated, would be a rank 0 tensor.
80 /// For example, if `R` and `S` are Tensors, here is a non-exhaustive list of
81 /// some of the acceptable forms that `t` could take:
82 /// - `R()`
83 /// - `R(ti_A, ti_a)`
84 /// - `(R(ti_A, ti_B) * S(ti_a, ti_b))`
85 ///
86 /// \param t the type of the tensor expression of which to take the square root
87 template <typename T>
89  const TensorExpression<T, typename T::type, tmpl::list<>, tmpl::list<>,
90  tmpl::list<>>& t) {
92 }
TensorExpressions::SquareRoot
Defines the tensor expression representing the square root of a tensor expression that evaluates to a...
Definition: SquareRoot.hpp:22
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:88
Structure.hpp
TensorExpressions
Definition: AddSubtract.hpp:30
SPECTRE_ALWAYS_INLINE
#define SPECTRE_ALWAYS_INLINE
Definition: ForceInline.hpp:16
cstddef
Assert.hpp
ASSERT
#define ASSERT(a, m)
Assert that an expression should be true.
Definition: Assert.hpp:49
ForceInline.hpp
TensorExpressions::SquareRoot::get
decltype(auto) get(const size_t storage_index) const noexcept
Returns the square root of the component of the tensor evaluated from the RHS tensor expression.
Definition: SquareRoot.hpp:58
TMPL.hpp
std::is_base_of