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
//! Basic traits applying to all types of matrices.

use std::ops::{Range, RangeFrom, RangeTo, RangeFull};


#[derive(RustcEncodable, RustcDecodable)]
#[derive(Clone, Debug)]
pub enum MatrixOrder {
    RowMajor,
    ColumnMajor,
}


/// Trait representing a shaped matrix whose entries can be accessed
/// at will using their row and column position.
pub trait IndexableMatrix {
    /// Return the number of rows of the matrix.
    fn rows(&self) -> usize;

    /// Return the number of columns of the matrix.
    fn cols(&self) -> usize;

    /// Get the value of the entry at (`row`, `column`) without bounds checking.
    unsafe fn get_unchecked(&self, row: usize, column: usize) -> f32;

    /// Get a mutable reference to the value of the entry at (`row`, `column`)
    /// without bounds checking.
    unsafe fn get_unchecked_mut(&mut self, row: usize, column: usize) -> &mut f32;

    /// Get the value of the entry at (`row`, `column`).
    ///
    /// # Panics
    /// Will panic if the element accessed is out of bounds.
    fn get(&self, row: usize, column: usize) -> f32 {
        assert!(row < self.rows());
        assert!(column < self.cols());

        unsafe { self.get_unchecked(row, column) }
    }

    /// Get a mutable reference to value of the entry at (`row`, `column`).
    ///
    /// # Panics
    /// Will panic if the element accessed is out of bounds.
    fn get_mut(&mut self, row: usize, column: usize) -> &mut f32 {
        assert!(row < self.rows());
        assert!(column < self.cols());

        unsafe { self.get_unchecked_mut(row, column) }
    }

    /// Set the value of the entry at (`row`, `column`) to `value`.
    ///
    /// # Panics
    /// Will panic if the element accessed is out of bounds.
    fn set(&mut self, row: usize, column: usize, value: f32) {
        assert!(row < self.rows());
        assert!(column < self.cols());

        unsafe {
            self.set_unchecked(row, column, value);
        }
    }

    /// Set the value of the entry at (`row`, `column`) to `value` without bounds checking.
    unsafe fn set_unchecked(&mut self, row: usize, column: usize, value: f32) {
        *self.get_unchecked_mut(row, column) = value;
    }
}


/// Trait representing a matrix that can be iterated over in
/// a row-wise fashion.
pub trait RowIterable {
    type Item: NonzeroIterable;
    type Output: Iterator<Item = Self::Item>;
    /// Iterate over rows of the matrix.
    fn iter_rows(self) -> Self::Output;
    /// Iterate over a subset of rows of the matrix.
    fn iter_rows_range(self, range: Range<usize>) -> Self::Output;
    /// View a row of the matrix.
    fn view_row(self, idx: usize) -> Self::Item;
}


/// Trait representing a matrix that can be iterated over in
/// a column-wise fashion.
pub trait ColumnIterable {
    type Item: NonzeroIterable;
    type Output: Iterator<Item = Self::Item>;
    /// Iterate over columns of a the matrix.
    fn iter_columns(self) -> Self::Output;
    /// Iterate over a subset of columns of the matrix.
    fn iter_columns_range(self, range: Range<usize>) -> Self::Output;
    /// View a column of the matrix.
    fn view_column(self, idx: usize) -> Self::Item;
}


/// Trait representing an object whose non-zero
/// entries can be iterated over.
pub trait NonzeroIterable {
    type Output: Iterator<Item = (usize, f32)>;
    fn iter_nonzero(&self) -> Self::Output;
}


/// Trait representing a matrix whose rows can be selected
/// to create a new matrix containing those rows.
pub trait RowIndex<Rhs> {
    type Output;
    fn get_rows(&self, index: &Rhs) -> Self::Output;
}


impl<T> RowIndex<usize> for T
    where T: RowIndex<Vec<usize>>
{
    type Output = T::Output;
    fn get_rows(&self, index: &usize) -> Self::Output {
        self.get_rows(&vec![*index])
    }
}


impl<T> RowIndex<Range<usize>> for T
    where T: RowIndex<Vec<usize>>
{
    type Output = T::Output;
    fn get_rows(&self, index: &Range<usize>) -> Self::Output {
        self.get_rows(&(index.start..index.end).collect::<Vec<usize>>())
    }
}


impl<T> RowIndex<RangeFrom<usize>> for T
    where T: RowIndex<Range<usize>> + IndexableMatrix
{
    type Output = T::Output;
    fn get_rows(&self, index: &RangeFrom<usize>) -> Self::Output {
        self.get_rows(&(index.start..self.rows()))
    }
}


impl<T> RowIndex<RangeTo<usize>> for T
    where T: RowIndex<Range<usize>> + IndexableMatrix
{
    type Output = T::Output;
    fn get_rows(&self, index: &RangeTo<usize>) -> Self::Output {
        self.get_rows(&(0..index.end))
    }
}


impl<T> RowIndex<RangeFull> for T
    where T: RowIndex<Range<usize>> + IndexableMatrix
{
    type Output = T::Output;
    fn get_rows(&self, _: &RangeFull) -> Self::Output {
        self.get_rows(&(0..self.rows()))
    }
}


/// Elementwise array operations trait.
pub trait ElementwiseArrayOps<Rhs> {
    type Output;
    fn add(&self, rhs: Rhs) -> Self::Output;
    fn add_inplace(&mut self, rhs: Rhs);
    fn sub(&self, rhs: Rhs) -> Self::Output;
    fn sub_inplace(&mut self, rhs: Rhs);
    fn times(&self, rhs: Rhs) -> Self::Output;
    fn times_inplace(&mut self, rhs: Rhs);
    fn div(&self, rhs: Rhs) -> Self::Output;
    fn div_inplace(&mut self, rhs: Rhs);
}

/// A matrix multiplication trait.
pub trait Dot<Rhs> {
    type Output;
    fn dot(&self, rhs: Rhs) -> Self::Output;
}