Skip to content

Commit d162408

Browse files
committed
Add QR implementation (without slice drop)
1 parent bafa1e6 commit d162408

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/impl2/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ pub mod qr;
44
pub use self::opnorm::*;
55
pub use self::qr::*;
66

7-
pub trait LapackScalar: OperatorNorm_ {}
8-
impl<A> LapackScalar for A where A: OperatorNorm_ {}
7+
pub trait LapackScalar: OperatorNorm_ + QR_ {}
8+
impl<A> LapackScalar for A where A: OperatorNorm_ + QR_ {}

src/layout.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ pub trait AllocatedArray {
4444
fn as_allocated(&self) -> Result<&[Self::Scalar]>;
4545
}
4646

47+
pub trait AllocatedArrayMut: AllocatedArray {
48+
fn as_allocated_mut(&mut self) -> Result<&mut [Self::Scalar]>;
49+
}
50+
4751
impl<A, S> AllocatedArray for ArrayBase<S, Ix2>
4852
where S: Data<Elem = A>
4953
{
@@ -76,3 +80,21 @@ impl<A, S> AllocatedArray for ArrayBase<S, Ix2>
7680
Ok(slice)
7781
}
7882
}
83+
84+
impl<A, S> AllocatedArrayMut for ArrayBase<S, Ix2>
85+
where S: DataMut<Elem = A>
86+
{
87+
fn as_allocated_mut(&mut self) -> Result<&mut [A]> {
88+
let slice = self.as_slice_memory_order_mut().ok_or(MemoryContError::new())?;
89+
Ok(slice)
90+
}
91+
}
92+
93+
pub fn reconstruct<A, S>(l: Layout, a: Vec<A>) -> Result<ArrayBase<S, Ix2>>
94+
where S: DataOwned<Elem = A>
95+
{
96+
Ok(match l {
97+
Layout::C((row, col)) => ArrayBase::from_shape_vec((row as usize, col as usize), a)?,
98+
Layout::F((col, row)) => ArrayBase::from_shape_vec((row as usize, col as usize).f(), a)?,
99+
})
100+
}

src/traits.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,19 @@ impl<A, S> OperationNorm for ArrayBase<S, Ix2>
3434
Ok(A::opnorm(t, l, a))
3535
}
3636
}
37+
38+
pub trait QR<Q, R> {
39+
fn qr(self) -> Result<(Q, R)>;
40+
}
41+
42+
impl<A, Sq, Sr> QR<ArrayBase<Sq, Ix2>, ArrayBase<Sr, Ix2>> for ArrayBase<Sq, Ix2>
43+
where A: LapackScalar,
44+
Sq: DataMut<Elem = A>,
45+
Sr: DataOwned<Elem = A>
46+
{
47+
fn qr(mut self) -> Result<(ArrayBase<Sq, Ix2>, ArrayBase<Sr, Ix2>)> {
48+
let l = self.layout()?;
49+
let r = A::qr(l, self.as_allocated_mut()?)?;
50+
Ok((self, reconstruct(l, r)?))
51+
}
52+
}

0 commit comments

Comments
 (0)