Skip to content

Commit a61196e

Browse files
committed
Implement SquareMatrix for RcArray<A, Ix2>
1 parent 51680fe commit a61196e

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/square.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Define trait for Hermite matrices
22
3-
use ndarray::{Ix2, Array};
3+
use ndarray::{Ix2, Array, RcArray, ArrayBase, Data};
44
use lapack::c::Layout;
55

66
use matrix::{Matrix, MFloat};
@@ -32,6 +32,13 @@ pub trait SquareMatrix: Matrix {
3232
}
3333
}
3434

35+
fn trace<A: MFloat, S>(a: &ArrayBase<S, Ix2>) -> A
36+
where S: Data<Elem = A>
37+
{
38+
let n = a.rows();
39+
(0..n).fold(A::zero(), |sum, i| sum + a[(i, i)])
40+
}
41+
3542
impl<A: MFloat> SquareMatrix for Array<A, Ix2> {
3643
fn inv(self) -> Result<Self, LinalgError> {
3744
self.check_square()?;
@@ -47,7 +54,17 @@ impl<A: MFloat> SquareMatrix for Array<A, Ix2> {
4754
}
4855
fn trace(&self) -> Result<Self::Scalar, LinalgError> {
4956
self.check_square()?;
50-
let (n, _) = self.size();
51-
Ok((0..n).fold(A::zero(), |sum, i| sum + self[(i, i)]))
57+
Ok(trace(self))
58+
}
59+
}
60+
61+
impl<A: MFloat> SquareMatrix for RcArray<A, Ix2> {
62+
fn inv(self) -> Result<Self, LinalgError> {
63+
let i = self.to_owned().inv()?;
64+
Ok(i.into_shared())
65+
}
66+
fn trace(&self) -> Result<Self::Scalar, LinalgError> {
67+
self.check_square()?;
68+
Ok(trace(self))
5269
}
5370
}

0 commit comments

Comments
 (0)