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
use Element;
use format::{Banded, Conventional, Diagonal};

impl<'l, T: Element> From<&'l Banded<T>> for Conventional<T> {
    fn from(matrix: &'l Banded<T>) -> Self {
        let &Banded {
            rows, columns, superdiagonals, subdiagonals, ref values
        } = validate!(matrix);

        let diagonals = matrix.diagonals();
        let mut matrix = Conventional::new((rows, columns));
        for j in 0..columns {
            for i in row_range!(rows, superdiagonals, subdiagonals, j) {
                let k = superdiagonals + i - j;
                matrix.values[j * rows + i] = values[j * diagonals + k];
            }
        }

        matrix
    }
}

impl<T: Element> From<Banded<T>> for Conventional<T> {
    #[inline]
    fn from(matrix: Banded<T>) -> Self {
        (&matrix).into()
    }
}

impl<'l, T: Element> From<&'l Diagonal<T>> for Banded<T> {
    #[inline]
    fn from(matrix: &'l Diagonal<T>) -> Self {
        matrix.clone().into()
    }
}

impl<T: Element> From<Diagonal<T>> for Banded<T> {
    fn from(matrix: Diagonal<T>) -> Self {
        let Diagonal { rows, columns, mut values } = validate!(matrix);
        for _ in rows..columns {
            values.push(T::zero());
        }
        new!(rows, columns, 0, 0, values)
    }
}

#[cfg(test)]
mod tests {
    use prelude::*;

    #[test]
    fn from_diagonal_tall() {
        let matrix = Banded::from(Diagonal::from_vec((5, 3), vec![1.0, 2.0, 3.0]));
        assert_eq!(&matrix.values, &[1.0, 2.0, 3.0]);
    }

    #[test]
    fn from_diagonal_wide() {
        let matrix = Banded::from(Diagonal::from_vec((3, 5), vec![1.0, 2.0, 3.0]));
        assert_eq!(&matrix.values, &[1.0, 2.0, 3.0, 0.0, 0.0]);
    }

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

        let matrix = Conventional::from(matrix);

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

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

        let matrix = Conventional::from(matrix);

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