1
1
//! Define trait for Hermite matrices
2
2
3
- use ndarray:: { Ix2 , Array } ;
3
+ use ndarray:: { Ix2 , Array , RcArray , ArrayBase , Data } ;
4
4
use lapack:: c:: Layout ;
5
5
6
6
use matrix:: { Matrix , MFloat } ;
@@ -32,6 +32,13 @@ pub trait SquareMatrix: Matrix {
32
32
}
33
33
}
34
34
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
+
35
42
impl < A : MFloat > SquareMatrix for Array < A , Ix2 > {
36
43
fn inv ( self ) -> Result < Self , LinalgError > {
37
44
self . check_square ( ) ?;
@@ -47,7 +54,17 @@ impl<A: MFloat> SquareMatrix for Array<A, Ix2> {
47
54
}
48
55
fn trace ( & self ) -> Result < Self :: Scalar , LinalgError > {
49
56
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 ) )
52
69
}
53
70
}
0 commit comments