Skip to content

Commit 1eb73c7

Browse files
committed
Use move in TriangularMatrix
1 parent 964e987 commit 1eb73c7

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

src/square.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ pub trait SquareMatrix: Matrix {
3030
})
3131
}
3232
}
33+
/// test matrix is square and return its size
34+
fn square_size(&self) -> Result<usize, NotSquareError> {
35+
self.check_square()?;
36+
let (n, _) = self.size();
37+
Ok(n)
38+
}
3339
}
3440

3541
fn trace<A: MFloat, S>(a: &ArrayBase<S, Ix2>) -> A

src/triangular.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
use ndarray::{Data, Ix1, Ix2, Array, RcArray, NdFloat, ArrayBase, DataMut};
2+
use ndarray::{Ix1, Ix2, Array, RcArray, NdFloat, ArrayBase, DataMut};
33

44
use matrix::{Matrix, MFloat};
55
use square::SquareMatrix;
@@ -9,49 +9,39 @@ use solve::ImplSolve;
99
pub trait TriangularMatrix<Rhs>: Matrix + SquareMatrix {
1010
type Output;
1111
/// solve a triangular system with upper triangular matrix
12-
fn solve_upper(&self, &Rhs) -> Result<Self::Output, LinalgError>;
12+
fn solve_upper(&self, Rhs) -> Result<Self::Output, LinalgError>;
1313
/// solve a triangular system with lower triangular matrix
14-
fn solve_lower(&self, &Rhs) -> Result<Self::Output, LinalgError>;
14+
fn solve_lower(&self, Rhs) -> Result<Self::Output, LinalgError>;
1515
}
1616

17-
impl<A, S> TriangularMatrix<ArrayBase<S, Ix1>> for Array<A, Ix2>
18-
where A: MFloat,
19-
S: Data<Elem = A>
20-
{
17+
impl<A: MFloat> TriangularMatrix<Array<A, Ix1>> for Array<A, Ix2> {
2118
type Output = Array<A, Ix1>;
22-
23-
fn solve_upper(&self, b: &ArrayBase<S, Ix1>) -> Result<Self::Output, LinalgError> {
24-
self.check_square()?;
25-
let (n, _) = self.size();
19+
fn solve_upper(&self, b: Array<A, Ix1>) -> Result<Self::Output, LinalgError> {
20+
let n = self.square_size()?;
2621
let layout = self.layout()?;
2722
let a = self.as_slice_memory_order().unwrap();
28-
let x = ImplSolve::solve_triangle(layout, 'U' as u8, n, a, b.to_owned().into_raw_vec(), 1)?;
23+
let x = ImplSolve::solve_triangle(layout, 'U' as u8, n, a, b.into_raw_vec(), 1)?;
2924
Ok(Array::from_vec(x))
3025
}
31-
fn solve_lower(&self, b: &ArrayBase<S, Ix1>) -> Result<Self::Output, LinalgError> {
32-
self.check_square()?;
33-
let (n, _) = self.size();
26+
fn solve_lower(&self, b: Array<A, Ix1>) -> Result<Self::Output, LinalgError> {
27+
let n = self.square_size()?;
3428
let layout = self.layout()?;
3529
let a = self.as_slice_memory_order().unwrap();
36-
let x = ImplSolve::solve_triangle(layout, 'L' as u8, n, a, b.to_owned().into_raw_vec(), 1)?;
30+
let x = ImplSolve::solve_triangle(layout, 'L' as u8, n, a, b.into_raw_vec(), 1)?;
3731
Ok(Array::from_vec(x))
3832
}
3933
}
4034

41-
impl<A, S> TriangularMatrix<ArrayBase<S, Ix1>> for RcArray<A, Ix2>
42-
where A: MFloat,
43-
S: Data<Elem = A>
44-
{
35+
impl<A: MFloat> TriangularMatrix<RcArray<A, Ix1>> for RcArray<A, Ix2> {
4536
type Output = RcArray<A, Ix1>;
46-
47-
fn solve_upper(&self, b: &ArrayBase<S, Ix1>) -> Result<Self::Output, LinalgError> {
37+
fn solve_upper(&self, b: RcArray<A, Ix1>) -> Result<Self::Output, LinalgError> {
4838
// XXX unnecessary clone
49-
let x = self.to_owned().solve_upper(&b)?;
39+
let x = self.to_owned().solve_upper(b.into_owned())?;
5040
Ok(x.into_shared())
5141
}
52-
fn solve_lower(&self, b: &ArrayBase<S, Ix1>) -> Result<Self::Output, LinalgError> {
42+
fn solve_lower(&self, b: RcArray<A, Ix1>) -> Result<Self::Output, LinalgError> {
5343
// XXX unnecessary clone
54-
let x = self.to_owned().solve_lower(&b)?;
44+
let x = self.to_owned().solve_lower(b.into_owned())?;
5545
Ok(x.into_shared())
5646
}
5747
}

0 commit comments

Comments
 (0)