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
//! QR decomposition

use num_traits::Zero;
use ndarray::*;

use super::error::*;
use super::layout::*;

use lapack_traits::LapackScalar;

pub trait QR<Q, R> {
    fn qr(self) -> Result<(Q, R)>;
}

impl<A, S, Sq, Sr> QR<ArrayBase<Sq, Ix2>, ArrayBase<Sr, Ix2>> for ArrayBase<S, Ix2>
    where A: LapackScalar + Copy + Zero,
          S: DataMut<Elem = A>,
          Sq: DataOwned<Elem = A> + DataMut,
          Sr: DataOwned<Elem = A> + DataMut
{
    fn qr(mut self) -> Result<(ArrayBase<Sq, Ix2>, ArrayBase<Sr, Ix2>)> {
        (&mut self).qr()
    }
}

fn take_slice<A, S1, S2>(a: &ArrayBase<S1, Ix2>, n: usize, m: usize) -> ArrayBase<S2, Ix2>
    where A: Copy,
          S1: Data<Elem = A>,
          S2: DataMut<Elem = A> + DataOwned
{
    let av = a.slice(s![..n as isize, ..m as isize]);
    let mut a = unsafe { ArrayBase::uninitialized((n, m)) };
    a.assign(&av);
    a
}

fn take_slice_upper<A, S1, S2>(a: &ArrayBase<S1, Ix2>, n: usize, m: usize) -> ArrayBase<S2, Ix2>
    where A: Copy + Zero,
          S1: Data<Elem = A>,
          S2: DataMut<Elem = A> + DataOwned
{
    let av = a.slice(s![..n as isize, ..m as isize]);
    let mut a = unsafe { ArrayBase::uninitialized((n, m)) };
    for ((i, j), val) in a.indexed_iter_mut() {
        *val = if i <= j { av[(i, j)] } else { A::zero() };
    }
    a
}

impl<'a, A, S, Sq, Sr> QR<ArrayBase<Sq, Ix2>, ArrayBase<Sr, Ix2>> for &'a mut ArrayBase<S, Ix2>
    where A: LapackScalar + Copy + Zero,
          S: DataMut<Elem = A>,
          Sq: DataOwned<Elem = A> + DataMut,
          Sr: DataOwned<Elem = A> + DataMut
{
    fn qr(mut self) -> Result<(ArrayBase<Sq, Ix2>, ArrayBase<Sr, Ix2>)> {
        let n = self.rows();
        let m = self.cols();
        let k = ::std::cmp::min(n, m);
        let l = self.layout()?;
        let r = A::qr(l, self.as_allocated_mut()?)?;
        let r: Array2<_> = reconstruct(l, r)?;
        let q = self;
        Ok((take_slice(q, n, k), take_slice_upper(&r, k, m)))
    }
}

impl<'a, A, S, Sq, Sr> QR<ArrayBase<Sq, Ix2>, ArrayBase<Sr, Ix2>> for &'a ArrayBase<S, Ix2>
    where A: LapackScalar + Copy + Zero,
          S: Data<Elem = A>,
          Sq: DataOwned<Elem = A> + DataMut,
          Sr: DataOwned<Elem = A> + DataMut
{
    fn qr(self) -> Result<(ArrayBase<Sq, Ix2>, ArrayBase<Sr, Ix2>)> {
        let n = self.rows();
        let m = self.cols();
        let k = ::std::cmp::min(n, m);
        let l = self.layout()?;
        let mut q = self.to_owned();
        let r = A::qr(l, q.as_allocated_mut()?)?;
        let r: Array2<_> = reconstruct(l, r)?;
        Ok((take_slice(&q, n, k), take_slice_upper(&r, k, m)))
    }
}