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
#[cfg(feature = "tch-bindings")]
mod torch;

use crate::{EinopsError, Operation, Rearrange, Reduce, Repeat};

pub trait Backend {
    fn shape(&self) -> Vec<usize>;
    fn reshape(&self, shape: &[usize]) -> Self;
    fn transpose(&self, axes: &[usize]) -> Self;
    fn reduce_axes(&self, operation: Operation, axes: &[usize]) -> Self;
    fn add_axes(&self, naxes: usize, pos2len: &[(usize, usize)]) -> Self;
}

/// Trait that allows calling the rearrange operation directly on the tensor
pub trait RearrangeFn {
    /// Apply rearrange operation using the pattern
    fn rearrange(&self, pattern: &str) -> Result<Self, EinopsError>
    where
        Self: Sized + Backend,
    {
        Rearrange::new(pattern)?.apply(self)
    }

    /// Apply rearrange operation using the pattern and additional axes lengths
    /// attribute
    fn rearrange_with_lengths(
        &self,
        pattern: &str,
        axes_lengths: &[(&str, usize)],
    ) -> Result<Self, EinopsError>
    where
        Self: Sized + Backend,
    {
        Rearrange::with_lengths(pattern, axes_lengths)?.apply(self)
    }
}

/// Trait that allows calling the reduce operation directly on the tensor
pub trait ReduceFn {
    /// Apply reduce operation using the pattern and [`Operation`]
    fn reduce(&self, pattern: &str, operation: Operation) -> Result<Self, EinopsError>
    where
        Self: Sized + Backend,
    {
        Reduce::new(pattern, operation)?.apply(self)
    }

    /// Apply rearrange operation using the pattern, [`Operation`], and additional axes
    /// lengths attribute
    fn reduce_with_lengths(
        &self,
        pattern: &str,
        operation: Operation,
        axes_lengths: &[(&str, usize)],
    ) -> Result<Self, EinopsError>
    where
        Self: Sized + Backend,
    {
        Reduce::with_lengths(pattern, operation, axes_lengths)?.apply(self)
    }
}

/// Trait that allows calling the repeat operation directly on the tensor
pub trait RepeatFn {
    /// Apply repeat operation using the pattern
    fn repeat(&self, pattern: &str) -> Result<Self, EinopsError>
    where
        Self: Sized + Backend,
    {
        Repeat::new(pattern)?.apply(self)
    }

    /// Apply repeat operation using the pattern and additional axes lengths attribute
    fn repeat_with_lengths(
        &self,
        pattern: &str,
        axes_lengths: &[(&str, usize)],
    ) -> Result<Self, EinopsError>
    where
        Self: Sized + Backend,
    {
        Repeat::with_lengths(pattern, axes_lengths)?.apply(self)
    }
}