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
use crate::Matrix;
use custos::{number::Number, Alloc, MainMemory, Shape};

pub fn scalar_apply<'a, T, F, D, S, Host>(
    device: &'a Host,
    lhs: &Matrix<T, D, S>,
    scalar: T,
    f: F,
) -> Matrix<'a, T, Host, S>
where
    T: Number,
    F: Fn(&mut T, T, T),
    D: MainMemory,
    S: Shape,
    Host: for<'b> Alloc<'b, T, S> + MainMemory,
{
    let mut out = device.retrieve(lhs.len(), lhs.node.idx);
    scalar_apply_slice(lhs, &mut out, scalar, f);
    (out, lhs.dims()).into()
}

#[inline]
pub fn scalar_apply_slice<T, F>(lhs: &[T], out: &mut [T], scalar: T, f: F)
where
    T: Copy,
    F: Fn(&mut T, T, T),
{
    for (idx, value) in out.iter_mut().enumerate() {
        f(value, lhs[idx], scalar)
    }
}

pub fn row_op_slice_mut<T, F>(lhs: &[T], lrows: usize, lcols: usize, rhs: &[T], out: &mut [T], f: F)
where
    T: Copy,
    F: Fn(&mut T, T, T),
{
    for i in 0..lrows {
        let index = i * lcols;
        let x = &lhs[index..index + lcols];

        for (idx, value) in rhs.iter().enumerate() {
            f(&mut out[index + idx], x[idx], *value);
        }
    }
}

pub fn row_op_slice_lhs<T, F>(lhs: &mut [T], lhs_rows: usize, lhs_cols: usize, rhs: &[T], f: F)
where
    T: Copy,
    F: Fn(&mut T, T),
{
    for i in 0..lhs_rows {
        let index = i * lhs_cols;

        for (idx, value) in rhs.iter().enumerate() {
            f(&mut lhs[index + idx], *value);
        }
    }
}

pub fn row_op<'a, T, F, D, Host, LS: Shape, RS: Shape>(
    device: &'a Host,
    lhs: &Matrix<T, D, LS>,
    rhs: &Matrix<T, D, RS>,
    f: F,
) -> Matrix<'a, T, Host, LS>
where
    T: Number,
    F: Fn(&mut T, T, T),
    D: MainMemory,
    Host: for<'b> Alloc<'b, T, LS> + MainMemory,
{
    assert!(rhs.rows() == 1 && rhs.cols() == lhs.cols());

    let mut out = device.retrieve(lhs.len(), [lhs.node.idx, rhs.node.idx]);
    row_op_slice_mut(lhs, lhs.rows(), lhs.cols(), rhs, &mut out, f);
    (out, lhs.dims()).into()
}

pub fn col_op<'a, T, F, D, Host>(
    device: &'a Host,
    lhs: &Matrix<T, D>,
    rhs: &Matrix<T, D>,
    f: F,
) -> Matrix<'a, T, Host>
where
    T: Number,
    F: Fn(&mut T, T, T),
    D: MainMemory,
    Host: for<'b> Alloc<'b, T> + MainMemory,
{
    let mut out = device.retrieve(lhs.len(), [lhs.node.idx, rhs.node.idx]);
    col_op_slice_mut(lhs, lhs.rows(), lhs.cols(), rhs, &mut out, f);
    (out, lhs.dims()).into()
}

pub fn col_op_slice_mut<T, F>(lhs: &[T], lrows: usize, lcols: usize, rhs: &[T], out: &mut [T], f: F)
where
    T: Number,
    F: Fn(&mut T, T, T),
{
    let mut i = 0;
    for (idx, rdata_value) in rhs.iter().enumerate().take(lrows) {
        let index = idx * lcols;
        let row = &lhs[index..index + lcols];
        for data in row {
            f(&mut out[i], *data, *rdata_value);
            i += 1;
        }
    }
}

pub fn each_op<'a, T, F, D, S, Host>(
    device: &'a Host,
    x: &Matrix<T, D, S>,
    f: F,
) -> Matrix<'a, T, Host, S>
where
    T: Copy,
    F: Fn(T) -> T,
    D: MainMemory,
    Host: for<'b> Alloc<'b, T, S> + MainMemory,
    S: Shape,
{
    let mut out = device.retrieve(x.len(), x.node.idx);
    each_op_slice(x, &mut out, f);
    (out, x.dims()).into()
}

pub fn each_op_slice<T, F>(x: &[T], out: &mut [T], f: F)
where
    T: Copy,
    F: Fn(T) -> T,
{
    for (idx, value) in out.iter_mut().enumerate() {
        *value = f(x[idx]);
    }
}

pub fn each_op_slice_mut<T, F>(x: &mut [T], f: F)
where
    T: Copy,
    F: Fn(T) -> T,
{
    for value in x.iter_mut() {
        *value = f(*value);
    }
}