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
use {Dense, Element, Sparse};

/// A band matrix.
///
/// The storage is suitable for matrices with a small number of superdiagonals
/// and/or subdiagonals relative to the smallest dimension. Data are stored in
/// the [format][1] adopted by [LAPACK][2].
///
/// [1]: http://www.netlib.org/lapack/lug/node124.html
/// [2]: http://www.netlib.org/lapack
#[derive(Clone, Debug, PartialEq)]
pub struct Band<T: Element> {
    /// The number of rows.
    pub rows: usize,
    /// The number of columns.
    pub columns: usize,
    /// The number of superdiagonals.
    pub superdiagonals: usize,
    /// The number of subdiagonals.
    pub subdiagonals: usize,
    /// The values of the diagonal elements stored as a `(superdiagonals + 1 +
    /// subdiagonals) × columns` matrix such that the first row corresponds to
    /// the uppermost superdiagonal whereas the last row corresponds to the
    /// lowest supdiagonal.
    pub data: Vec<T>,
}

matrix!(Band);

impl<T: Element> Sparse for Band<T> {
    fn nonzeros(&self) -> usize {
        let &Band { rows, columns, superdiagonals, subdiagonals, .. } = self;
        let mut count = 0;
        for k in 0..(superdiagonals + 1) {
            count += min!(columns - k, rows);
        }
        for k in 1..(subdiagonals + 1) {
            count += min!(columns, rows - k);
        }
        count
    }
}

impl<'l, T: Element> From<&'l Band<T>> for Dense<T> {
    fn from(band: &'l Band<T>) -> Dense<T> {
        let &Band { rows, columns, superdiagonals, subdiagonals, ref data } = band;

        let diagonals = superdiagonals + 1 + subdiagonals;
        debug_assert_eq!(data.len(), diagonals * columns);

        let mut dense = Dense {
            rows: rows,
            columns: columns,
            data: vec![T::zero(); rows * columns],
        };

        for k in 0..(superdiagonals + 1) {
            for i in 0..min!(columns - k, rows) {
                let j = i + k;
                dense.data[j * rows + i] = data[j * diagonals + superdiagonals - k];
            }
        }
        for k in 1..(subdiagonals + 1) {
            for i in k..min!(columns + k, rows) {
                let j = i - k;
                dense.data[j * rows + i] = data[j * diagonals + superdiagonals + k];
            }
        }

        dense
    }
}

impl<T: Element> From<Band<T>> for Dense<T> {
    fn from(band: Band<T>) -> Dense<T> {
        (&band).into()
    }
}

#[cfg(test)]
mod tests {
    use {Band, Dense, Sparse};

    macro_rules! new(
        ($rows:expr, $columns:expr, $superdiagonals:expr, $subdiagonals:expr, $data:expr) => (
            Band {
                rows: $rows,
                columns: $columns,
                superdiagonals: $superdiagonals,
                subdiagonals: $subdiagonals,
                data: $data,
            }
        );
        ($rows:expr, $columns:expr, $superdiagonals:expr, $subdiagonals:expr) => (
            new!($rows, $columns, $superdiagonals, $subdiagonals,
                 vec![0.0; ($superdiagonals + 1 + $subdiagonals) * $columns])
        );
    );

    #[test]
    fn into_tall_dense() {
        let band = new!(7, 4, 2, 2, vec![
            0.0,  0.0,  1.0,  4.0,  8.0,
            0.0,  2.0,  5.0,  9.0, 12.0,
            3.0,  6.0, 10.0, 13.0, 15.0,
            7.0, 11.0, 14.0, 16.0, 17.0,
        ]);

        let dense: Dense<_> = band.into();

        assert_eq!(&dense[..], &[
            1.0, 4.0,  8.0,  0.0,  0.0,  0.0, 0.0,
            2.0, 5.0,  9.0, 12.0,  0.0,  0.0, 0.0,
            3.0, 6.0, 10.0, 13.0, 15.0,  0.0, 0.0,
            0.0, 7.0, 11.0, 14.0, 16.0, 17.0, 0.0,
        ]);
    }

    #[test]
    fn into_wide_dense() {
        let band = new!(4, 7, 2, 2, vec![
             0.0,  0.0,  1.0,  4.0,  8.0,
             0.0,  2.0,  5.0,  9.0, 13.0,
             3.0,  6.0, 10.0, 14.0,  0.0,
             7.0, 11.0, 15.0,  0.0,  0.0,
            12.0, 16.0,  0.0,  0.0,  0.0,
            17.0,  0.0,  0.0,  0.0,  0.0,
             0.0,  0.0,  0.0,  0.0,  0.0,
        ]);

        let dense: Dense<_> = band.into();

        assert_eq!(&dense[..], &[
            1.0, 4.0,  8.0,  0.0,
            2.0, 5.0,  9.0, 13.0,
            3.0, 6.0, 10.0, 14.0,
            0.0, 7.0, 11.0, 15.0,
            0.0, 0.0, 12.0, 16.0,
            0.0, 0.0,  0.0, 17.0,
            0.0, 0.0,  0.0,  0.0,
        ]);
    }

    #[test]
    fn nonzeros() {
        assert_eq!(new!(4, 4, 0, 0).nonzeros(), 4);
        assert_eq!(new!(4, 4, 1, 0).nonzeros(), 7);
        assert_eq!(new!(4, 4, 2, 0).nonzeros(), 9);
        assert_eq!(new!(4, 4, 0, 1).nonzeros(), 7);
        assert_eq!(new!(4, 4, 0, 2).nonzeros(), 9);

        assert_eq!(new!(4, 5, 0, 0).nonzeros(), 4);
        assert_eq!(new!(4, 5, 1, 0).nonzeros(), 8);
        assert_eq!(new!(4, 5, 2, 0).nonzeros(), 11);
        assert_eq!(new!(4, 5, 0, 1).nonzeros(), 7);
        assert_eq!(new!(4, 5, 0, 2).nonzeros(), 9);

        assert_eq!(new!(5, 4, 0, 0).nonzeros(), 4);
        assert_eq!(new!(5, 4, 1, 0).nonzeros(), 7);
        assert_eq!(new!(5, 4, 2, 0).nonzeros(), 9);
        assert_eq!(new!(5, 4, 0, 1).nonzeros(), 8);
        assert_eq!(new!(5, 4, 0, 2).nonzeros(), 11);
    }
}