SimiLie
Loading...
Searching...
No Matches
character.hpp
1// SPDX-FileCopyrightText: 2024 Baptiste Legouix
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#pragma once
5
6#include <ddc/ddc.hpp>
7
8#include <similie/misc/specialization.hpp>
9#include <similie/tensor/tensor_impl.hpp>
10#include <similie/tensor/tensor_indices/relabelization.hpp>
11
12namespace sil {
13
14namespace tensor {
15
17{
18};
19
21{
22};
23
24namespace detail {
25
26template <class NaturalIndices>
27struct TensorNaturalIndexFromTypeSeqDim;
28
29template <class... CDim>
30struct TensorNaturalIndexFromTypeSeqDim<ddc::TypeSeq<CDim...>>
31{
32 using type = TensorNaturalIndex<CDim...>;
33};
34
35} // namespace detail
36
37// struct representing an index mu or nu in a tensor Tmunu.
38template <TensorNatIndex NaturalIndex>
40 : detail::TensorNaturalIndexFromTypeSeqDim<typename NaturalIndex::type_seq_dimensions>::type
41{
43};
44
45template <TensorNatIndex NaturalIndex>
47 : detail::TensorNaturalIndexFromTypeSeqDim<typename NaturalIndex::type_seq_dimensions>::type
48{
50};
51
52// helpes to lower, upper or uncharacterize indices
53namespace detail {
54
55template <class Index>
56struct Lower;
57
58template <TensorNatIndex NaturalIndex>
59struct Lower<Covariant<NaturalIndex>>
60{
61 using type = Covariant<NaturalIndex>;
62};
63
64template <TensorNatIndex NaturalIndex>
65struct Lower<Contravariant<NaturalIndex>>
66{
67 using type = Covariant<NaturalIndex>;
68};
69
70template <template <class...> class T, TensorIndex... Index>
71struct Lower<T<Index...>>
72{
73 using type = T<typename Lower<Index>::type...>;
74};
75
76} // namespace detail
77
78template <class T>
79using lower_t = detail::Lower<T>::type;
80
81namespace detail {
82
83template <class Index>
84struct Upper;
85
86template <TensorNatIndex NaturalIndex>
87struct Upper<Covariant<NaturalIndex>>
88{
89 using type = Contravariant<NaturalIndex>;
90};
91
92template <TensorNatIndex NaturalIndex>
93struct Upper<Contravariant<NaturalIndex>>
94{
95 using type = Contravariant<NaturalIndex>;
96};
97
98template <template <class...> class T, TensorIndex... Index>
99struct Upper<T<Index...>>
100{
101 using type = T<typename Upper<Index>::type...>;
102};
103
104} // namespace detail
105
106template <class T>
107using upper_t = detail::Upper<T>::type;
108
109namespace detail {
110
111template <class Index>
112struct SwapCharacter;
113
114template <TensorNatIndex NaturalIndex>
115struct SwapCharacter<Covariant<NaturalIndex>>
116{
117 using type = Contravariant<NaturalIndex>;
118};
119
120template <TensorNatIndex NaturalIndex>
121struct SwapCharacter<Contravariant<NaturalIndex>>
122{
123 using type = Covariant<NaturalIndex>;
124};
125
126template <template <class...> class T, TensorIndex... Index>
127struct SwapCharacter<T<Index...>>
128{
129 using type = T<typename SwapCharacter<Index>::type...>;
130};
131
132} // namespace detail
133
134template <class T>
135using swap_character_t = detail::SwapCharacter<T>::type;
136
137namespace detail {
138
139template <class Index>
140struct Uncharacterize;
141
142template <class NaturalIndex>
143struct Uncharacterize<Covariant<NaturalIndex>>
144{
145 using type = NaturalIndex;
146};
147
148template <class NaturalIndex>
149struct Uncharacterize<Contravariant<NaturalIndex>>
150{
151 using type = NaturalIndex;
152};
153
154template <template <class...> class T, TensorIndex... Index>
155struct Uncharacterize<T<Index...>>
156{
157 using type = T<typename Uncharacterize<Index>::type...>;
158};
159
160template <class Index>
161struct Uncharacterize
162{
163 using type = detail::RelabelizeIndices<
164 Index,
165 ddc::to_type_seq_t<typename Index::subindices_domain_t>,
166 typename Uncharacterize<
167 ddc::to_type_seq_t<typename Index::subindices_domain_t>>::type>::type;
168};
169
170} // namespace detail
171
172template <class Index>
173using uncharacterize_t = detail::Uncharacterize<Index>::type;
174
175
176// uncharacterize a tensor
177template <misc::Specialization<Tensor> TensorType>
179 TensorType,
180 ddc::to_type_seq_t<typename TensorType::accessor_t::natural_domain_t>,
182
183template <misc::Specialization<Tensor> TensorType>
185 TensorType const& tensor)
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::TypeSeq<Index...>>
223{
224 static constexpr bool value = (is_covariant_v<Index> && ...);
225};
226
227} // namespace detail
228
229template <misc::Specialization<ddc::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::TypeSeq<Index...>>
262{
263 static constexpr bool value = (is_contravariant_v<Index> && ...);
264};
265
266} // namespace detail
267
268template <misc::Specialization<ddc::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...> class T,
313 TensorNatIndex HeadIndex1,
314 TensorNatIndex... TailIndex1,
315 TensorNatIndex HeadIndex2,
316 TensorNatIndex... TailIndex2>
317struct AreSameCharacters<T<HeadIndex1, TailIndex1...>, T<HeadIndex2, TailIndex2...>>
318{
319 static constexpr bool value = IsSameCharacter<HeadIndex1, HeadIndex2>::value
320 && AreSameCharacters<T<TailIndex1...>, T<TailIndex2...>>::value;
321};
322
323} // namespace detail
324
325template <class T1, class T2>
326constexpr bool are_same_characters_v = detail::AreSameCharacters<T1, T2>::value;
327
328namespace detail {
329
330template <class T1, class T2>
331struct AreDifferentCharacters;
332
333template <template <TensorNatIndex...> class T>
334struct AreDifferentCharacters<T<>, T<>>
335{
336 static constexpr bool value = true;
337};
338
339template <
340 template <TensorNatIndex...> class T,
341 TensorNatIndex HeadIndex1,
342 TensorNatIndex... TailIndex1,
343 TensorNatIndex HeadIndex2,
344 TensorNatIndex... TailIndex2>
345struct AreDifferentCharacters<T<HeadIndex1, TailIndex1...>, T<HeadIndex2, TailIndex2...>>
346{
347 static constexpr bool value
348 = !IsSameCharacter<HeadIndex1, HeadIndex2>::value
349 && AreDifferentCharacters<T<TailIndex1...>, T<TailIndex2...>>::value;
350};
351
352} // namespace detail
353
354template <class T1, class T2>
355constexpr bool are_different_characters_v = detail::AreDifferentCharacters<T1, T2>::value;
356
357} // namespace tensor
358
359} // namespace sil
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
KOKKOS_FUNCTION constexpr uncharacterize_tensor_t< TensorType > uncharacterize_tensor(TensorType const &tensor)
detail::SwapCharacter< T >::type swap_character_t
KOKKOS_FUNCTION constexpr relabelize_indices_of_t< Tensor, OldIndices, NewIndices > relabelize_indices_of(Tensor const &tensor)
detail::Uncharacterize< Index >::type uncharacterize_t
detail::Lower< T >::type lower_t
Definition character.hpp:79
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:15