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
use faer::{mat, Mat, MatRef};

use crate::scalar::Rfloat;
use crate::*;

/// Convert a `faer::Mat<f64>` into an `RMatrix<f64>` which is not NA aware.
impl From<Mat<f64>> for RMatrix<f64> {
    fn from(value: Mat<f64>) -> Self {
        RMatrix::new_matrix(value.nrows(), value.ncols(), |i, j| value.read(i, j))
    }
}

impl From<Mat<f64>> for Robj {
    fn from(value: Mat<f64>) -> Self {
        RMatrix::<f64>::from(value).into()
    }
}

/// Convert a `faer::Mat<f64>` into an `RMatrix<f64>` which is not NA aware.
impl From<MatRef<'_, f64>> for RMatrix<f64> {
    /// Convert a faer MatRef<f64> into Robj.
    fn from(value: MatRef<'_, f64>) -> Self {
        RMatrix::new_matrix(value.nrows(), value.ncols(), |i, j| value.read(i, j))
    }
}

impl From<MatRef<'_, f64>> for Robj {
    fn from(value: MatRef<'_, f64>) -> Self {
        RMatrix::<f64>::from(value).into_robj()
    }
}

impl From<Mat<f64>> for RMatrix<Rfloat> {
    fn from(value: Mat<f64>) -> Self {
        RMatrix::new_matrix(value.nrows(), value.ncols(), |i, j| value.read(i, j).into())
    }
}

impl From<MatRef<'_, f64>> for RMatrix<Rfloat> {
    fn from(value: MatRef<f64>) -> Self {
        RMatrix::new_matrix(value.nrows(), value.ncols(), |i, j| {
            Rfloat::from(value.read(i, j))
        })
    }
}

impl From<RMatrix<f64>> for Mat<f64> {
    fn from(value: RMatrix<f64>) -> Self {
        let nrow = value.nrows();
        let ncol = value.ncols();
        let slice = value.as_real_slice().expect("RMatrix should be doubles");
        Mat::from_fn(nrow, ncol, |i, j| slice[i + j * nrow])
    }
}

impl<'a> From<&'_ RMatrix<f64>> for MatRef<'a, f64> {
    fn from(value: &RMatrix<f64>) -> Self {
        let nrow = value.nrows();
        let ncol = value.ncols();
        let slice = value.as_typed_slice().expect("RMatrix should be doubles");
        let mat_ref = faer::mat::from_column_major_slice::<f64>(slice, nrow, ncol);
        mat_ref
    }
}

impl TryFrom<&Robj> for Mat<f64> {
    type Error = Error;

    fn try_from(robj: &Robj) -> Result<Self> {
        let rmat = &RMatrix::<f64>::try_from(robj)?;
        let nrow = rmat.nrows();
        let ncol = rmat.ncols();

        if let Some(slice) = robj.as_real_slice() {
            let fmat = Mat::from_fn(nrow, ncol, |i, j| slice[i + j * nrow]);
            Ok(fmat)
        } else {
            Err(Error::ExpectedReal(robj.clone()))
        }
    }
}

impl<'a> TryFrom<&'_ Robj> for MatRef<'a, f64> {
    type Error = Error;

    fn try_from(robj: &Robj) -> Result<Self> {
        let rmat = &RMatrix::<f64>::try_from(robj)?;
        let nrows = rmat.nrows();
        let ncols = rmat.ncols();

        if let Some(slice) = robj.as_typed_slice() {
            let fmat = mat::from_column_major_slice::<f64>(slice, nrows, ncols);
            Ok(fmat)
        } else {
            Err(Error::ExpectedReal(robj.clone()))
        }
    }
}

impl TryFrom<Robj> for Mat<f64> {
    type Error = crate::Error;

    fn try_from(robj: Robj) -> Result<Self> {
        Self::try_from(&robj)
    }
}

impl<'a> TryFrom<Robj> for MatRef<'a, f64> {
    type Error = crate::Error;

    fn try_from(robj: Robj) -> Result<Self> {
        Self::try_from(&robj)
    }
}

impl From<RMatrix<i32>> for Mat<f64> {
    fn from(value: RMatrix<i32>) -> Self {
        let nrow = value.nrows();
        let ncol = value.ncols();
        let slice = value
            .as_integer_slice()
            .expect("RMatrix should be integers");
        Mat::from_fn(nrow, ncol, |i, j| slice[i + j * nrow] as f64)
    }
}

#[cfg(test)]
mod test {
    use crate::*;
    use faer::{mat, Mat, MatRef};

    #[test]
    fn test_rmatrix_to_faer_mat() {
        test! {
            let values = [
                [1.0, 5.0, 9.0],
                [2.0, 6.0, 10.0],
                [3.0, 7.0, 11.0],
                [4.0, 8.0, 12.0f64]
            ];
            let a = Mat::<f64>::from_fn(4, 3, |i, j| values[i][j] as f64);

            let rmatrix = RMatrix::new_matrix(4, 3, |i, j| values[i][j]);
            let b = Mat::<f64>::try_from(rmatrix);
            assert_eq!(a, b.expect("matrix to be converted"));
        }
    }

    #[test]
    fn test_rmatrix_to_faer_mat_with_nan() {
        test! {
            let values = [
                [1.0, 5.0, 9.0],
                [2.0, 6.0, 10.0],
                [3.0, 7.0, 11.0],
                [f64::NAN, 8.0, 12.0f64]
            ];

            let rmatrix = RMatrix::new_matrix(4, 3, |i, j| values[i][j]);
            let b = Mat::<f64>::try_from(rmatrix);
            assert!(b.expect("matrix to be converted").read(3, 0).is_nan());
        }
    }

    #[test]
    fn test_rmatrix_to_faer_mat_ref() {
        test! {
            let values = [
                [1.0, 5.0, 9.0],
                [2.0, 6.0, 10.0],
                [3.0, 7.0, 11.0],
                [4.0, 8.0, 12.0f64]
            ];
            let mat = Mat::<f64>::from_fn(4, 3, |i, j| values[i][j] as f64);
            let a = mat.as_ref();

            let rmatrix = RMatrix::new_matrix(4, 3, |i, j| values[i][j]);
            let b = MatRef::<f64>::try_from(&rmatrix);
            assert_eq!(a, b.expect("matrix to be converted"));
        }
    }

    #[test]
    fn test_faer_mat_to_rmatrix() {
        test! {
            let vec: Vec<f64> = (1..13).map(f64::from).collect();
            let a = mat![
                [1.0, 5.0, 9.0],
                [2.0, 6.0, 10.0],
                [3.0, 7.0, 11.0],
                [4.0, 8.0, 12.0f64],
            ];
            let rmatrix: RMatrix<f64> = a.into();
            assert_eq!(rmatrix.as_real_slice().expect("slice"), &vec);
        }
    }

    #[test]
    fn test_faer_mat_ref_to_rmatrix() {
        test! {
            let vec: Vec<f64> = (1..13).map(f64::from).collect();
            let a = mat![
                [1.0, 5.0, 9.0],
                [2.0, 6.0, 10.0],
                [3.0, 7.0, 11.0],
                [4.0, 8.0, 12.0f64],
            ];
            let rmatrix: RMatrix<f64> = a.as_ref().into();
            assert_eq!(rmatrix.as_real_slice().expect("slice"), &vec);
        }
    }

    #[test]
    fn test_try_from_robj_to_faer_mat() {
        test! {
            let values = [
                [1.0, 5.0, 9.0],
                [2.0, 6.0, 10.0],
                [3.0, 7.0, 11.0],
                [4.0, 8.0, 12.0f64]
            ];
            let a = Mat::<f64>::from_fn(4, 3, |i, j| values[i][j] as f64);

            let rmatrix = RMatrix::new_matrix(4, 3, |i, j| values[i][j]);
            let b = Mat::<f64>::try_from(&Robj::from(rmatrix));
            assert_eq!(a, b.expect("matrix to be converted"));
        }
    }

    #[test]
    fn test_try_from_robj_to_faer_mat_ref() {
        test! {
            let values = [
                [1.0, 5.0, 9.0],
                [2.0, 6.0, 10.0],
                [3.0, 7.0, 11.0],
                [4.0, 8.0, 12.0f64]
            ];
            let mat = Mat::<f64>::from_fn(4, 3, |i, j| values[i][j] as f64);
            let a = mat.as_ref();

            let rmatrix = RMatrix::new_matrix(4, 3, |i, j| values[i][j]);
            let robj = Robj::from(rmatrix);
            let b = MatRef::<f64>::try_from(&robj);
            assert_eq!(a, b.expect("matrix to be converted"));
        }
    }

    #[test]
    fn test_int_rmatrix_to_faer_mat() {
        test! {
            let values = [
                [1, 5, 9],
                [2, 6, 10],
                [3, 7, 11],
                [4, 8, 12]
            ];
            let a = Mat::<f64>::from_fn(4, 3, |i, j| values[i][j] as f64);

            let rmatrix = RMatrix::new_matrix(4, 3, |i, j| values[i][j]);
            let b = Mat::<f64>::try_from(rmatrix);
            assert_eq!(a, b.expect("matrix to be converted"));
        }
    }
}