wuzp
Wrong Unit? Zero Points! Compile-time checks for units, coordinate systems and points vs. vectors.
Loading...
Searching...
No Matches
Indices.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <tuple>
4#include <type_traits>
5
9
10// The indices of a tensor, and how operations combine them. The index placement follows chapter 1 of Carroll, Lecture Notes on
11// General Relativity (https://arxiv.org/abs/gr-qc/9712019), on which his book Spacetime and Geometry is based: an upper index
12// is contravariant, a lower index covariant, a contraction pairs an upper with a lower index, and the order of the indices
13// matters (eqs. 1.59 and 1.60). The traits of this file check the indices of the operands and compute the indices of the
14// result, like the traits of CoordinateSystem.hpp do for coordinate systems; Tensor.hpp combines them with the traits of the
15// quantities.
16
17namespace wuzp {
18
20struct Up {};
21
23struct Down {};
24
36template <UpOrDown... VariancesT_>
37struct Indices {
39 static constexpr int RankV = static_cast<int>(sizeof...(VariancesT_));
40};
41
42namespace internal {
43
45template <typename VarianceT_>
46struct Flip;
47template <>
48struct Flip<Up> {
49 using type = Down;
50};
51template <>
52struct Flip<Down> {
53 using type = Up;
54};
55
57template <typename IndicesT_>
58struct FirstIndex {
59 using type = void;
60};
61template <typename Variance0T_, typename... VariancesT_>
62struct FirstIndex<Indices<Variance0T_, VariancesT_...>> {
63 using type = Variance0T_;
64};
65
67template <typename IndicesT_>
68struct LastIndex {
69 using type = void;
70};
71template <typename Variance0T_, typename... VariancesT_>
72struct LastIndex<Indices<Variance0T_, VariancesT_...>> {
73 using type = std::tuple_element_t<sizeof...(VariancesT_), std::tuple<Variance0T_, VariancesT_...>>;
74};
75
77template <typename IndicesT_>
78struct DropFirstIndex {
79 using type = Indices<>;
80};
81template <typename Variance0T_, typename... VariancesT_>
82struct DropFirstIndex<Indices<Variance0T_, VariancesT_...>> {
83 using type = Indices<VariancesT_...>;
84};
85
87template <typename IndicesT_>
88struct DropLastIndex {
89 using type = Indices<>;
90};
91template <typename Variance0T_, typename Variance1T_>
92struct DropLastIndex<Indices<Variance0T_, Variance1T_>> {
93 using type = Indices<Variance0T_>;
94};
95
97template <typename IndicesLhsT_, typename IndicesRhsT_>
98struct ConcatenateIndices;
99template <typename... VariancesLhsT_, typename... VariancesRhsT_>
100struct ConcatenateIndices<Indices<VariancesLhsT_...>, Indices<VariancesRhsT_...>> {
101 using type = Indices<VariancesLhsT_..., VariancesRhsT_...>;
102};
103
104} // namespace internal
105
106template <typename... VariancesLhsT_, typename... VariancesRhsT_>
107struct Sum<Indices<VariancesLhsT_...>, Indices<VariancesRhsT_...>> {
108 static_assert(std::is_same_v<Indices<VariancesLhsT_...>, Indices<VariancesRhsT_...>>, "Indices do not match.");
109 using type = Indices<VariancesLhsT_...>;
110};
111
112template <typename... VariancesLhsT_, typename... VariancesRhsT_>
113struct Difference<Indices<VariancesLhsT_...>, Indices<VariancesRhsT_...>> {
114 static_assert(std::is_same_v<Indices<VariancesLhsT_...>, Indices<VariancesRhsT_...>>, "Indices do not match.");
115 using type = Indices<VariancesLhsT_...>;
116};
117
126template <typename... VariancesLhsT_, typename... VariancesRhsT_>
127struct Product<Indices<VariancesLhsT_...>, Indices<VariancesRhsT_...>> {
128 static_assert(sizeof...(VariancesLhsT_) == 0 || sizeof...(VariancesRhsT_) == 0,
129 "A tensor can only be multiplied by a scalar, a quantity or a tensor of rank 0. Use innerProduct, outerProduct or "
130 "cwiseProduct for other products.");
131 using type = std::conditional_t<sizeof...(VariancesLhsT_) == 0, Indices<VariancesRhsT_...>, Indices<VariancesLhsT_...>>;
132};
133
135template <typename... VariancesLhsT_, typename... VariancesRhsT_>
136struct Division<Indices<VariancesLhsT_...>, Indices<VariancesRhsT_...>> {
137 static_assert(sizeof...(VariancesRhsT_) == 0,
138 "A tensor can only be divided by a scalar, a quantity or a tensor of rank 0. Use cwiseQuotient for elementwise "
139 "quotients.");
140 using type = Indices<VariancesLhsT_...>;
141};
142
149template <typename... VariancesLhsT_, typename... VariancesRhsT_>
150struct InnerProduct<Indices<VariancesLhsT_...>, Indices<VariancesRhsT_...>> {
151 private:
152 using LhsT = Indices<VariancesLhsT_...>;
153 using RhsT = Indices<VariancesRhsT_...>;
154 static constexpr bool HasIndicesV = sizeof...(VariancesLhsT_) > 0 && sizeof...(VariancesRhsT_) > 0;
155 static constexpr bool OppositeV = !std::is_same_v<typename internal::LastIndex<LhsT>::type, typename internal::FirstIndex<RhsT>::type>;
156
157 public:
158 // Each failure reports one message: the second check assumes that the first one passes.
159 static_assert(HasIndicesV,
160 "An inner product contracts an index of each tensor, but a tensor of rank 0 has none. Multiply by it instead.");
161 static_assert(!HasIndicesV || OppositeV,
162 "An inner product contracts an upper with a lower index, but the last index of the left-hand side and the first index "
163 "of the right-hand side are both upper or both lower.");
164 using type = typename internal::ConcatenateIndices<typename internal::DropLastIndex<LhsT>::type,
165 typename internal::DropFirstIndex<RhsT>::type>::type;
166};
167
170template <typename... VariancesLhsT_, typename... VariancesRhsT_>
171struct OuterProduct<Indices<VariancesLhsT_...>, Indices<VariancesRhsT_...>> {
172 private:
173 static constexpr bool HasIndicesV = sizeof...(VariancesLhsT_) > 0 && sizeof...(VariancesRhsT_) > 0;
174
175 public:
176 static_assert(HasIndicesV, "An outer product with a tensor of rank 0 is a product. Multiply by it instead.");
177 static_assert(!HasIndicesV || sizeof...(VariancesLhsT_) + sizeof...(VariancesRhsT_) <= 2,
178 "The outer product would have rank 3 or more, which is not supported.");
179 using type = Indices<VariancesLhsT_..., VariancesRhsT_...>;
180};
181
184template <typename... VariancesLhsT_, typename... VariancesRhsT_>
185struct CrossProduct<Indices<VariancesLhsT_...>, Indices<VariancesRhsT_...>> {
186 static_assert(std::is_same_v<Indices<VariancesLhsT_...>, Indices<Up>> && std::is_same_v<Indices<VariancesRhsT_...>, Indices<Up>>,
187 "Cross product is only defined for vectors.");
189};
190
196template <>
199};
200template <typename VarianceT_>
204template <typename Variance0T_, typename Variance1T_>
205struct Transpose<Indices<Variance0T_, Variance1T_>> {
207};
208
215template <>
216struct Inverse<Indices<>> {
218};
219template <typename VarianceT_>
220struct Inverse<Indices<VarianceT_>> {
222 "A vector or a covector has no inverse. Use cwiseInverse() for the reciprocals of its components.");
224};
225template <typename Variance0T_, typename Variance1T_>
226struct Inverse<Indices<Variance0T_, Variance1T_>> {
227 using type = Indices<typename internal::Flip<Variance1T_>::type, typename internal::Flip<Variance0T_>::type>;
228};
229
238template <>
241};
242template <typename VarianceT_>
243struct Determinant<Indices<VarianceT_>> {
244 static_assert(FailOnInstantiation<VarianceT_>::value, "A vector or a covector has no determinant.");
246};
247template <typename Variance0T_, typename Variance1T_>
248struct Determinant<Indices<Variance0T_, Variance1T_>> {
249 static_assert(!std::is_same_v<Variance0T_, Variance1T_>,
250 "The determinant of a tensor with two upper or two lower indices depends on the basis. Use getQuantities().determinant() "
251 "for the determinant of its components.");
253};
254
255} // namespace wuzp
Up or Down: whether an index of a tensor is upper (contravariant) or lower (covariant).
Definition concepts.hpp:104
A lower, or covariant, index, e.g. of a covector w_i, whose components transform with the inverse of ...
Definition Indices.hpp:23
False for every type, so that a static_assert on it fails only when the template that names it is ins...
Definition helpers.hpp:21
The indices of a tensor, in their order, e.g. Indices<Up, Down> for a linear map T^i_j,...
Definition Indices.hpp:37
static constexpr int RankV
Definition Indices.hpp:39
typename internal::ConcatenateIndices< typename internal::DropLastIndex< LhsT >::type, typename internal::DropFirstIndex< RhsT >::type >::type type
Definition Indices.hpp:164
Indices< typename internal::Flip< Variance1T_ >::type, typename internal::Flip< Variance0T_ >::type > type
Definition Indices.hpp:227
The inverse: the matrix inverse of a matrix, like Eigen's MatrixBase::inverse(), and the reciprocal o...
Definition traits.hpp:188
std::conditional_t< sizeof...(VariancesLhsT_)==0, Indices< VariancesRhsT_... >, Indices< VariancesLhsT_... > > type
Definition Indices.hpp:131
Indices< Variance1T_, Variance0T_ > type
Definition Indices.hpp:206
Indices< typename internal::Flip< VarianceT_ >::type > type
Definition Indices.hpp:202
An upper, or contravariant, index, e.g. of a vector v^i, whose components transform like the coordina...
Definition Indices.hpp:20