SimiLie
Loading...
Searching...
No Matches
character.hpp
1// SPDX-FileCopyrightText: 2024 Baptiste Legouix
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
6#include <ddc/ddc.hpp>
7
8#include <similie/misc/specialization.hpp>
9
10#include "relabelization.hpp"
11#include "tensor_impl.hpp"
12
13namespace sil {
14
15namespace tensor {
16
18{
19};
20
22{
23};
24
25namespace detail {
26
27template <class NaturalIndices>
28struct TensorNaturalIndexFromTypeSeqDim;
29
30template <class... CDim>
31struct TensorNaturalIndexFromTypeSeqDim<ddc::detail::TypeSeq<CDim...>>
32{
33 using type = TensorNaturalIndex<CDim...>;
34};
35
36} // namespace detail
37
38// struct representing an index mu or nu in a tensor Tmunu.
39template <TensorNatIndex NaturalIndex>
41 : detail::TensorNaturalIndexFromTypeSeqDim<typename NaturalIndex::type_seq_dimensions>::type
42{
44};
45
46template <TensorNatIndex NaturalIndex>
48 : detail::TensorNaturalIndexFromTypeSeqDim<typename NaturalIndex::type_seq_dimensions>::type
49{
51};
52
53// helpes to lower, upper or uncharacterize indices
54namespace detail {
55
56template <class Index>
57struct Lower;
58
59template <TensorNatIndex NaturalIndex>
60struct Lower<Covariant<NaturalIndex>>
61{
62 using type = Covariant<NaturalIndex>;
63};
64
65template <TensorNatIndex NaturalIndex>
66struct Lower<Contravariant<NaturalIndex>>
67{
68 using type = Covariant<NaturalIndex>;
69};
70
71template <template <class...> class T, TensorIndex... Index>
72struct Lower<T<Index...>>
73{
74 using type = T<typename Lower<Index>::type...>;
75};
76
77} // namespace detail
78
79template <class T>
80using lower_t = detail::Lower<T>::type;
81
82namespace detail {
83
84template <class Index>
85struct Upper;
86
87template <TensorNatIndex NaturalIndex>
88struct Upper<Covariant<NaturalIndex>>
89{
90 using type = Contravariant<NaturalIndex>;
91};
92
93template <TensorNatIndex NaturalIndex>
94struct Upper<Contravariant<NaturalIndex>>
95{
96 using type = Contravariant<NaturalIndex>;
97};
98
99template <template <class...> class T, TensorIndex... Index>
100struct Upper<T<Index...>>
101{
102 using type = T<typename Upper<Index>::type...>;
103};
104
105} // namespace detail
106
107template <class T>
108using upper_t = detail::Upper<T>::type;
109
110namespace detail {
111
112template <class Index>
113struct SwapCharacter;
114
115template <TensorNatIndex NaturalIndex>
116struct SwapCharacter<Covariant<NaturalIndex>>
117{
118 using type = Contravariant<NaturalIndex>;
119};
120
121template <TensorNatIndex NaturalIndex>
122struct SwapCharacter<Contravariant<NaturalIndex>>
123{
124 using type = Covariant<NaturalIndex>;
125};
126
127template <template <class...> class T, TensorIndex... Index>
128struct SwapCharacter<T<Index...>>
129{
130 using type = T<typename SwapCharacter<Index>::type...>;
131};
132
133} // namespace detail
134
135template <class T>
136using swap_character_t = detail::SwapCharacter<T>::type;
137
138namespace detail {
139
140template <class Index>
141struct Uncharacterize;
142
143template <class NaturalIndex>
144struct Uncharacterize<Covariant<NaturalIndex>>
145{
146 using type = NaturalIndex;
147};
148
149template <class NaturalIndex>
150struct Uncharacterize<Contravariant<NaturalIndex>>
151{
152 using type = NaturalIndex;
153};
154
155template <template <class...> class T, TensorIndex... Index>
156struct Uncharacterize<T<Index...>>
157{
158 using type = T<typename Uncharacterize<Index>::type...>;
159};
160
161template <class Index>
162struct Uncharacterize
163{
164 using type = detail::RelabelizeIndices<
165 Index,
166 ddc::to_type_seq_t<typename Index::subindices_domain_t>,
167 typename Uncharacterize<
168 ddc::to_type_seq_t<typename Index::subindices_domain_t>>::type>::type;
169};
170
171} // namespace detail
172
173template <class Index>
174using uncharacterize_t = detail::Uncharacterize<Index>::type;
175
176
177// uncharacterize a tensor
178template <misc::Specialization<Tensor> TensorType>
180 TensorType,
181 ddc::to_type_seq_t<typename TensorType::accessor_t::natural_domain_t>,
183
184template <misc::Specialization<Tensor> TensorType>
186{
188 ddc::to_type_seq_t<typename TensorType::accessor_t::natural_domain_t>,
190 ddc::to_type_seq_t<typename TensorType::accessor_t::natural_domain_t>>>(tensor);
191}
192
193// check if index is covariant
194namespace detail {
195
196template <TensorNatIndex Index>
197struct IsCovariant;
198
199template <TensorNatIndex Index>
200struct IsCovariant<Covariant<Index>>
201{
202 static constexpr bool value = true;
203};
204
205template <TensorNatIndex Index>
206struct IsCovariant<Contravariant<Index>>
207{
208 static constexpr bool value = false;
209};
210
211} // namespace detail
212
213template <class T>
214bool constexpr is_covariant_v = detail::IsCovariant<T>::value;
215
216namespace detail {
217
218template <class Seq>
219struct AreCovariant;
220
221template <TensorNatIndex... Index>
222struct AreCovariant<ddc::detail::TypeSeq<Index...>>
223{
224 static constexpr bool value = (is_covariant_v<Index> && ...);
225};
226
227} // namespace detail
228
229template <misc::Specialization<ddc::detail::TypeSeq> Seq>
230bool constexpr are_covariant_v = detail::AreCovariant<Seq>::value;
231
232// check if index is contravariant
233namespace detail {
234
235template <TensorNatIndex Index>
236struct IsContravariant;
237
238template <TensorNatIndex Index>
239struct IsContravariant<Covariant<Index>>
240{
241 static constexpr bool value = false;
242};
243
244template <TensorNatIndex Index>
245struct IsContravariant<Contravariant<Index>>
246{
247 static constexpr bool value = true;
248};
249
250} // namespace detail
251
252template <class T>
253bool constexpr is_contravariant_v = detail::IsContravariant<T>::value;
254
255namespace detail {
256
257template <class Seq>
258struct AreContravariant;
259
260template <TensorNatIndex... Index>
261struct AreContravariant<ddc::detail::TypeSeq<Index...>>
262{
263 static constexpr bool value = (is_contravariant_v<Index> && ...);
264};
265
266} // namespace detail
267
268template <misc::Specialization<ddc::detail::TypeSeq> Seq>
269bool constexpr are_contravariant_v = detail::AreContravariant<Seq>::value;
270
271// check if characters are equal
272namespace detail {
273
274template <TensorNatIndex Index1, TensorNatIndex Index2>
275struct IsSameCharacter;
276
277template <TensorNatIndex Index1, TensorNatIndex Index2>
278struct IsSameCharacter<Covariant<Index1>, Covariant<Index2>>
279{
280 static constexpr bool value = true;
281};
282
283template <TensorNatIndex Index1, TensorNatIndex Index2>
284struct IsSameCharacter<Contravariant<Index1>, Covariant<Index2>>
285{
286 static constexpr bool value = false;
287};
288
289template <TensorNatIndex Index1, TensorNatIndex Index2>
290struct IsSameCharacter<Covariant<Index1>, Contravariant<Index2>>
291{
292 static constexpr bool value = false;
293};
294
295template <TensorNatIndex Index1, TensorNatIndex Index2>
296struct IsSameCharacter<Contravariant<Index1>, Contravariant<Index2>>
297{
298 static constexpr bool value = true;
299};
300
301} // namespace detail
302
303template <class T1, class T2>
304constexpr bool is_same_character_v = detail::IsSameCharacter<T1, T2>::value;
305
306namespace detail {
307
308template <class T1, class T2>
309struct AreSameCharacters;
310
311template <
312 template <TensorNatIndex...>
313 class T,
314 TensorNatIndex HeadIndex1,
315 TensorNatIndex... TailIndex1,
316 TensorNatIndex HeadIndex2,
317 TensorNatIndex... TailIndex2>
318struct AreSameCharacters<T<HeadIndex1, TailIndex1...>, T<HeadIndex2, TailIndex2...>>
319{
320 static constexpr bool value = IsSameCharacter<HeadIndex1, HeadIndex2>::value
321 && AreSameCharacters<T<TailIndex1...>, T<TailIndex2...>>::value;
322};
323
324} // namespace detail
325
326template <class T1, class T2>
327constexpr bool are_same_characters_v = detail::AreSameCharacters<T1, T2>::value;
328
329namespace detail {
330
331template <class T1, class T2>
332struct AreDifferentCharacters;
333
334template <template <TensorNatIndex...> class T>
335struct AreDifferentCharacters<T<>, T<>>
336{
337 static constexpr bool value = true;
338};
339
340template <
341 template <TensorNatIndex...>
342 class T,
343 TensorNatIndex HeadIndex1,
344 TensorNatIndex... TailIndex1,
345 TensorNatIndex HeadIndex2,
346 TensorNatIndex... TailIndex2>
347struct AreDifferentCharacters<T<HeadIndex1, TailIndex1...>, T<HeadIndex2, TailIndex2...>>
348{
349 static constexpr bool value
350 = !IsSameCharacter<HeadIndex1, HeadIndex2>::value
351 && AreDifferentCharacters<T<TailIndex1...>, T<TailIndex2...>>::value;
352};
353
354} // namespace detail
355
356template <class T1, class T2>
357constexpr bool are_different_characters_v = detail::AreDifferentCharacters<T1, T2>::value;
358
359} // namespace tensor
360
361} // namespace sil
constexpr uncharacterize_tensor_t< TensorType > uncharacterize_tensor(TensorType tensor)
constexpr bool are_same_characters_v
detail::RelabelizeIndicesOfType< TensorType, OldIndices, NewIndices >::type relabelize_indices_of_t
detail::Upper< T >::type upper_t
bool constexpr are_covariant_v
bool constexpr is_contravariant_v
constexpr bool is_same_character_v
constexpr bool are_different_characters_v
detail::SwapCharacter< T >::type swap_character_t
detail::Uncharacterize< Index >::type uncharacterize_t
detail::Lower< T >::type lower_t
Definition character.hpp:80
constexpr relabelize_indices_of_t< Tensor, OldIndices, NewIndices > relabelize_indices_of(Tensor tensor)
bool constexpr is_covariant_v
relabelize_indices_of_t< TensorType, ddc::to_type_seq_t< typename TensorType::accessor_t::natural_domain_t >, uncharacterize_t< ddc::to_type_seq_t< typename TensorType::accessor_t::natural_domain_t > > > uncharacterize_tensor_t
bool constexpr are_contravariant_v
The top-level namespace of SimiLie.
Definition csr.hpp:14