1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//! Module defining variances (types of tensors)

use crate::typenum::bit::Bit;
use crate::typenum::consts::{B1, U0, U1};
use crate::typenum::uint::{UInt, Unsigned};
use crate::typenum::{Add1, Sub1};
use crate::typenum::{Cmp, Greater, Same};
use std::ops::{Add, Sub};

/// This enum serves to represent the type of a tensor. A tensor can have any number of indices,
/// and each one can be either covariant (a lower index), or contravariant (an upper index).
/// For example, a vector is a tensor with only one contravariant index.
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum IndexType {
    Covariant,
    Contravariant,
}

/// Trait identifying a type as representing a tensor variance. It is implemented for
/// `CovariantIndex`, `ContravariantIndex` and tuples (Index, Variance).
pub trait Variance {
    type Rank: Unsigned + Add<B1>;
    fn rank() -> usize {
        Self::Rank::to_usize()
    }
    fn variance() -> Vec<IndexType>;
}

impl Variance for () {
    type Rank = U0;

    fn variance() -> Vec<IndexType> {
        vec![]
    }
}

/// Trait identifying a type as representing a tensor index. It is implemented
/// for `CovariantIndex` and `ContravariantIndex`.
pub trait TensorIndex: Variance {
    fn index_type() -> IndexType;
}

/// Type representing a contravariant (upper) tensor index.
pub struct ContravariantIndex;
impl TensorIndex for ContravariantIndex {
    fn index_type() -> IndexType {
        IndexType::Contravariant
    }
}

/// Type representing a covariant (lower) tensor index.
pub struct CovariantIndex;
impl TensorIndex for CovariantIndex {
    fn index_type() -> IndexType {
        IndexType::Covariant
    }
}

impl Variance for ContravariantIndex {
    type Rank = U1;
    fn variance() -> Vec<IndexType> {
        vec![IndexType::Contravariant]
    }
}

impl Variance for CovariantIndex {
    type Rank = U1;
    fn variance() -> Vec<IndexType> {
        vec![IndexType::Covariant]
    }
}

/// Trait representing the other index type
///
/// Used for identifying indices that can be contracted
pub trait OtherIndex: TensorIndex {
    type Output: TensorIndex;
}

impl OtherIndex for CovariantIndex {
    type Output = ContravariantIndex;
}

impl OtherIndex for ContravariantIndex {
    type Output = CovariantIndex;
}

// Back to implementing Variance

impl<T, U> Variance for (T, U)
where
    U: Variance,
    Add1<U::Rank>: Unsigned + Add<B1>,
    T: TensorIndex,
{
    type Rank = Add1<U::Rank>;

    fn variance() -> Vec<IndexType> {
        let mut result = vec![T::index_type()];
        result.append(&mut U::variance());
        result
    }
}

/// Operator trait used for concatenating two variances.
///
/// Used in tensor outer product.
pub trait Concat<T: Variance>: Variance {
    type Output: Variance;
}

/// Helper type for variance concatenation.
pub type Joined<T, U> = <T as Concat<U>>::Output;

impl<T, U> Concat<U> for T
where
    T: TensorIndex,
    U: TensorIndex,
    Add1<<U as Variance>::Rank>: Unsigned + Add<B1>,
{
    type Output = (T, U);
}

impl<T> Concat<T> for ()
where
    T: TensorIndex,
{
    type Output = T;
}

impl<T, U, V> Concat<V> for (T, U)
where
    T: TensorIndex,
    V: TensorIndex,
    U: Variance + Concat<V>,
    <U as Concat<V>>::Output: Variance,
    Add1<<U as Variance>::Rank>: Unsigned + Add<B1>,
    Add1<<Joined<U, V> as Variance>::Rank>: Unsigned + Add<B1>,
{
    type Output = (T, <U as Concat<V>>::Output);
}

impl<T, U, V> Concat<(U, V)> for T
where
    T: TensorIndex,
    U: TensorIndex,
    V: Variance,
    Add1<<V as Variance>::Rank>: Unsigned + Add<B1>,
    Add1<Add1<<V as Variance>::Rank>>: Unsigned + Add<B1>,
{
    type Output = (T, (U, V));
}

impl<T, U, V, W> Concat<(V, W)> for (T, U)
where
    T: TensorIndex,
    U: Variance + Concat<(V, W)>,
    V: TensorIndex,
    W: Variance,
    Add1<<U as Variance>::Rank>: Unsigned + Add<B1>,
    Add1<<W as Variance>::Rank>: Unsigned + Add<B1>,
    Add1<<Joined<U, (V, W)> as Variance>::Rank>: Unsigned + Add<B1>,
{
    type Output = (T, Joined<U, (V, W)>);
}

/// Indexing operator trait: Output is equal to the index type at the given position
///
/// Warning: Indices are numbered starting from 0!
pub trait Index<T: Unsigned>: Variance {
    type Output: TensorIndex;
}

/// Helper type for variance indexing.
pub type At<T, U> = <T as Index<U>>::Output;

impl Index<U0> for CovariantIndex {
    type Output = CovariantIndex;
}

impl Index<U0> for ContravariantIndex {
    type Output = ContravariantIndex;
}

impl<T, V, U, B> Index<UInt<U, B>> for (V, T)
where
    V: TensorIndex,
    U: Unsigned,
    B: Bit,
    UInt<U, B>: Sub<B1>,
    Sub1<UInt<U, B>>: Unsigned,
    T: Variance + Index<Sub1<UInt<U, B>>>,
    Add1<<T as Variance>::Rank>: Unsigned + Add<B1>,
{
    type Output = At<T, Sub1<UInt<U, B>>>;
}

impl<T, V> Index<U0> for (V, T)
where
    V: TensorIndex,
    T: Variance,
    Add1<<T as Variance>::Rank>: Unsigned + Add<B1>,
{
    type Output = V;
}

/// An operator trait, removing the indicated index from a variance
pub trait RemoveIndex<T: Unsigned>: Variance {
    type Output: Variance;
}

/// Helper type for index removal
pub type Removed<T, U> = <T as RemoveIndex<U>>::Output;

impl RemoveIndex<U0> for CovariantIndex {
    type Output = ();
}

impl RemoveIndex<U0> for ContravariantIndex {
    type Output = ();
}

impl<U, V> RemoveIndex<U0> for (U, V)
where
    U: TensorIndex,
    V: Variance,
    Add1<<V as Variance>::Rank>: Unsigned + Add<B1>,
{
    type Output = V;
}

impl<T, B, U, V> RemoveIndex<UInt<T, B>> for (U, V)
where
    T: Unsigned,
    B: Bit,
    U: TensorIndex,
    UInt<T, B>: Sub<B1>,
    Sub1<UInt<T, B>>: Unsigned,
    V: Variance + RemoveIndex<Sub1<UInt<T, B>>>,
    (U, V): Variance,
    (U, Removed<V, Sub1<UInt<T, B>>>): Variance,
{
    type Output = (U, Removed<V, Sub1<UInt<T, B>>>);
}

/// An operator trait representing tensor contraction
///
/// Used in tensor inner product
pub trait Contract<Ul: Unsigned, Uh: Unsigned>: Variance {
    type Output: Variance;
}

/// Helper type for contraction
pub type Contracted<V, Ul, Uh> = <V as Contract<Ul, Uh>>::Output;

impl<Ul, Uh, V> Contract<Ul, Uh> for V
where
    Ul: Unsigned,
    Uh: Unsigned + Sub<B1> + Cmp<Ul>,
    Sub1<Uh>: Unsigned,
    <Uh as Cmp<Ul>>::Output: Same<Greater>,
    V: Index<Ul> + Index<Uh> + RemoveIndex<Ul>,
    At<V, Ul>: OtherIndex,
    At<V, Uh>: Same<<At<V, Ul> as OtherIndex>::Output>,
    Removed<V, Ul>: RemoveIndex<Sub1<Uh>>,
    Removed<Removed<V, Ul>, Sub1<Uh>>: Variance,
{
    type Output = Removed<Removed<V, Ul>, Sub1<Uh>>;
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::typenum::consts::{U0, U1, U2};

    #[test]
    fn test_variance() {
        assert_eq!(
            <(CovariantIndex, ContravariantIndex) as Variance>::variance(),
            vec![IndexType::Covariant, IndexType::Contravariant]
        );
    }

    #[test]
    fn test_variance_concat() {
        assert_eq!(
            <Joined<CovariantIndex, ContravariantIndex> as Variance>::variance(),
            vec![IndexType::Covariant, IndexType::Contravariant]
        );

        assert_eq!(
            <Joined<(CovariantIndex, CovariantIndex), ContravariantIndex> as Variance>::variance(),
            vec![
                IndexType::Covariant,
                IndexType::Covariant,
                IndexType::Contravariant
            ]
        );

        assert_eq!(
            <Joined<CovariantIndex, (CovariantIndex, ContravariantIndex)> as Variance>::variance(),
            vec![
                IndexType::Covariant,
                IndexType::Covariant,
                IndexType::Contravariant
            ]
        );

        assert_eq!(<Joined<(ContravariantIndex, CovariantIndex),
                          (CovariantIndex, ContravariantIndex)> as Variance>::variance(),
                   vec![IndexType::Contravariant,
                        IndexType::Covariant,
                        IndexType::Covariant,
                        IndexType::Contravariant]);
    }

    #[test]
    fn test_index() {
        assert_eq!(
            <At<CovariantIndex, U0> as TensorIndex>::index_type(),
            IndexType::Covariant
        );

        assert_eq!(
            <At<(CovariantIndex, ContravariantIndex), U0> as TensorIndex>::index_type(),
            IndexType::Covariant
        );

        assert_eq!(
            <At<(CovariantIndex, ContravariantIndex), U1> as TensorIndex>::index_type(),
            IndexType::Contravariant
        );

        assert_eq!(
            <At<(ContravariantIndex, (CovariantIndex, CovariantIndex)), U0> as TensorIndex>
                ::index_type(),
            IndexType::Contravariant);

        assert_eq!(
            <At<(ContravariantIndex, (CovariantIndex, CovariantIndex)), U2> as TensorIndex>
                ::index_type(),
            IndexType::Covariant);
    }

    #[test]
    fn test_remove() {
        assert_eq!(
            <Removed<CovariantIndex, U0> as Variance>::variance(),
            vec![]
        );

        assert_eq!(
            <Removed<(CovariantIndex, ContravariantIndex), U0> as Variance>::variance(),
            vec![IndexType::Contravariant]
        );

        assert_eq!(
            <Removed<(CovariantIndex, ContravariantIndex), U1> as Variance>::variance(),
            vec![IndexType::Covariant]
        );

        assert_eq!(
            <Removed<(ContravariantIndex, (CovariantIndex, CovariantIndex)), U1> as Variance>
                ::variance(),
            vec![IndexType::Contravariant, IndexType::Covariant]);
    }

    #[test]
    fn test_contract() {
        assert_eq!(
            <Contracted<(CovariantIndex, ContravariantIndex), U0, U1> as Variance>::variance(),
            vec![]
        );

        assert_eq!(
            <Contracted<(ContravariantIndex, CovariantIndex), U0, U1> as Variance>::variance(),
            vec![]
        );

        assert_eq!(
            <Contracted<(ContravariantIndex, (CovariantIndex, CovariantIndex)), U0, U1> as Variance>
                ::variance(),
            vec![IndexType::Covariant]);

        assert_eq!(
            <Contracted<(ContravariantIndex, (CovariantIndex, CovariantIndex)), U0, U2> as Variance>
                ::variance(),
            vec![IndexType::Covariant]);
    }
}