SquareRoot.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <array>
7 #include <cstddef>
8 
11 #include "Utilities/Requires.hpp"
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  static constexpr auto num_tensor_indices = 0;
36 
37  SquareRoot(T t) : t_(std::move(t)) {}
38  ~SquareRoot() override = default;
39 
40  /// \brief Returns the square root of the component of the tensor evaluated
41  /// from the RHS tensor expression
42  ///
43  /// \details
44  /// SquareRoot only supports tensor expressions that evaluate to a rank 0
45  /// Tensor. This is why this template is only defined for the case where
46  /// `TensorIndices` is empty.
47  ///
48  /// \tparam TensorIndices the TensorIndexs of the LHS tensor and RHS tensor
49  /// expression
50  /// \param multi_index the multi-index of the component of which to take the
51  /// square root
52  /// \return the square root of the component of the tensor evaluated from the
53  /// RHS tensor expression
54  template <typename... TensorIndices,
55  Requires<sizeof...(TensorIndices) == 0> = nullptr>
56  SPECTRE_ALWAYS_INLINE decltype(auto) get(
57  const std::array<size_t, 0>& multi_index) const noexcept {
58  return sqrt(t_.template get(multi_index));
59  }
60 
61  private:
62  T t_;
63 };
64 } // namespace TensorExpressions
65 
66 /// \ingroup TensorExpressionsGroup
67 /// \brief Returns the tensor expression representing the square root of a
68 /// tensor expression that evaluates to a rank 0 tensor
69 ///
70 /// \details
71 /// `t` must be an expression that, when evaluated, would be a rank 0 tensor.
72 /// For example, if `R` and `S` are Tensors, here is a non-exhaustive list of
73 /// some of the acceptable forms that `t` could take:
74 /// - `R()`
75 /// - `R(ti_A, ti_a)`
76 /// - `(R(ti_A, ti_B) * S(ti_a, ti_b))`
77 ///
78 /// \param t the type of the tensor expression of which to take the square root
79 template <typename T>
81  const TensorExpression<T, typename T::type, tmpl::list<>, tmpl::list<>,
82  tmpl::list<>>& t) {
84 }
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:80
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:56
TensorExpressions
Definition: AddSubtract.hpp:30
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
TMPL.hpp
std::is_base_of