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
use matrix::{Matrix, MatrixSlice, MatrixSliceMut};
use std::marker::PhantomData;

impl<'a, T> MatrixSlice<'a, T> {
    /// Produce a `MatrixSlice` from a `Matrix`
    ///
    /// # Examples
    ///
    /// ```
    /// use rulinalg::matrix::{Matrix, MatrixSlice};
    ///
    /// let a = Matrix::new(3,3, (0..9).collect::<Vec<usize>>());
    /// let slice = MatrixSlice::from_matrix(&a, [1,1], 2, 2);
    /// ```
    pub fn from_matrix(mat: &'a Matrix<T>,
                       start: [usize; 2],
                       rows: usize,
                       cols: usize)
                       -> MatrixSlice<T> {
        assert!(start[0] + rows <= mat.rows,
                "View dimensions exceed matrix dimensions.");
        assert!(start[1] + cols <= mat.cols,
                "View dimensions exceed matrix dimensions.");
        unsafe {
            MatrixSlice {
                ptr: mat.data().get_unchecked(start[0] * mat.cols + start[1]) as *const T,
                rows: rows,
                cols: cols,
                row_stride: mat.cols,
                marker: PhantomData::<&'a T>,
            }
        }
    }

    /// Creates a `MatrixSlice` from raw parts.
    ///
    /// # Examples
    ///
    /// ```
    /// use rulinalg::matrix::MatrixSlice;
    ///
    /// let mut a = vec![4.0; 16];
    ///
    /// unsafe {
    ///     // Create a matrix slice with 3 rows, and 3 cols
    ///     // The row stride of 4 specifies the distance between the start of each row in the data.
    ///     let b = MatrixSlice::from_raw_parts(a.as_ptr(), 3, 3, 4);
    /// }
    /// ```
    ///
    /// # Safety
    ///
    /// The pointer must be followed by a contiguous slice of data larger than `row_stride * rows`.
    /// If not then other operations will produce undefined behaviour.
    ///
    /// Additionally `cols` should be less than the `row_stride`. It is possible to use this
    /// function safely whilst violating this condition. So long as
    /// `max(cols, row_stride) * rows` is less than the data size.
    pub unsafe fn from_raw_parts(ptr: *const T,
                                 rows: usize,
                                 cols: usize,
                                 row_stride: usize)
                                 -> MatrixSlice<'a, T> {
        MatrixSlice {
            ptr: ptr,
            rows: rows,
            cols: cols,
            row_stride: row_stride,
            marker: PhantomData::<&'a T>,
        }
    }
}

impl<'a, T> MatrixSliceMut<'a, T> {
    /// Produce a `MatrixSliceMut` from a mutable `Matrix`
    ///
    /// # Examples
    ///
    /// ```
    /// use rulinalg::matrix::{Matrix, MatrixSliceMut};
    ///
    /// let mut a = Matrix::new(3,3, (0..9).collect::<Vec<usize>>());
    /// let slice = MatrixSliceMut::from_matrix(&mut a, [1,1], 2, 2);
    /// ```
    pub fn from_matrix(mat: &'a mut Matrix<T>,
                       start: [usize; 2],
                       rows: usize,
                       cols: usize)
                       -> MatrixSliceMut<T> {
        assert!(start[0] + rows <= mat.rows,
                "View dimensions exceed matrix dimensions.");
        assert!(start[1] + cols <= mat.cols,
                "View dimensions exceed matrix dimensions.");

        let mat_cols = mat.cols;

        unsafe {
            MatrixSliceMut {
                ptr: mat.mut_data().get_unchecked_mut(start[0] * mat_cols + start[1]) as *mut T,
                rows: rows,
                cols: cols,
                row_stride: mat_cols,
                marker: PhantomData::<&'a mut T>,
            }
        }
    }

    /// Creates a `MatrixSliceMut` from raw parts.
    ///
    /// # Examples
    ///
    /// ```
    /// use rulinalg::matrix::MatrixSliceMut;
    ///
    /// let mut a = vec![4.0; 16];
    ///
    /// unsafe {
    ///     // Create a mutable matrix slice with 3 rows, and 3 cols
    ///     // The row stride of 4 specifies the distance between the start of each row in the data.
    ///     let b = MatrixSliceMut::from_raw_parts(a.as_mut_ptr(), 3, 3, 4);
    /// }
    /// ```
    ///
    /// # Safety
    ///
    /// The pointer must be followed by a contiguous slice of data larger than `row_stride * rows`.
    /// If not then other operations will produce undefined behaviour.
    ///
    /// Additionally `cols` should be less than the `row_stride`. It is possible to use this
    /// function safely whilst violating this condition. So long as
    /// `max(cols, row_stride) * rows` is less than the data size.
    pub unsafe fn from_raw_parts(ptr: *mut T,
                                 rows: usize,
                                 cols: usize,
                                 row_stride: usize)
                                 -> MatrixSliceMut<'a, T> {
        MatrixSliceMut {
            ptr: ptr,
            rows: rows,
            cols: cols,
            row_stride: row_stride,
            marker: PhantomData::<&'a mut T>,
        }
    }
}

#[cfg(test)]
mod tests {

    use matrix::{Matrix, MatrixSlice, MatrixSliceMut, BaseMatrix, Axes};

    #[test]
    #[should_panic]
    fn make_slice_bad_dim() {
        let a = Matrix::ones(3, 3) * 2.0;
        let _ = MatrixSlice::from_matrix(&a, [1, 1], 3, 2);
    }

    #[test]
    fn make_slice() {
        let a = Matrix::ones(3, 3) * 2.0;
        let b = MatrixSlice::from_matrix(&a, [1, 1], 2, 2);

        assert_eq!(b.rows(), 2);
        assert_eq!(b.cols(), 2);
    }

    #[test]
    fn make_slice_mut() {
        let mut a = Matrix::ones(3, 3) * 2.0;
        {
            let mut b = MatrixSliceMut::from_matrix(&mut a, [1, 1], 2, 2);
            assert_eq!(b.rows(), 2);
            assert_eq!(b.cols(), 2);
            b += 2.0;
        }
        let exp = matrix![2.0, 2.0, 2.0;
                          2.0, 4.0, 4.0;
                          2.0, 4.0, 4.0];
        assert_matrix_eq!(a, exp);

    }

    #[test]
    fn matrix_min_max() {
        let a = matrix![1., 3., 5., 4.;
                        2., 4., 7., 1.;
                        1., 1., 0., 0.];
        assert_eq!(a.min(Axes::Col), vector![1., 1., 0.]);
        assert_eq!(a.min(Axes::Row), vector![1., 1., 0., 0.]);

        assert_eq!(a.max(Axes::Col), vector![5., 7., 1.]);
        assert_eq!(a.max(Axes::Row), vector![2., 4., 7., 4.]);

        let r = matrix![1., 3., 5., 4.];
        assert_eq!(r.min(Axes::Col), vector![1.]);
        assert_eq!(r.min(Axes::Row), vector![1., 3., 5., 4.]);

        assert_eq!(r.max(Axes::Col), vector![5.]);
        assert_eq!(r.max(Axes::Row), vector![1., 3., 5., 4.]);

        let c = matrix![1.; 2.; 3.];
        assert_eq!(c.min(Axes::Col), vector![1., 2., 3.]);
        assert_eq!(c.min(Axes::Row), vector![1.]);

        assert_eq!(c.max(Axes::Col), vector![1., 2., 3.]);
        assert_eq!(c.max(Axes::Row), vector![3.]);

        let t = matrix![1., 2.; 0., 1.];
        assert_eq!(t.min(Axes::Col), vector![1., 0.]);
        assert_eq!(t.min(Axes::Row), vector![0., 1.]);

        assert_eq!(t.max(Axes::Col), vector![2., 1.]);
        assert_eq!(t.max(Axes::Row), vector![1., 2.]);
    }
}