@@ -6,7 +6,7 @@ use ndarray::prelude::*;
6
6
use ndarray:: LinalgScalar ;
7
7
use lapack:: c:: Layout ;
8
8
9
- use error:: LapackError ;
9
+ use error:: { LinalgError , StrideError } ;
10
10
use qr:: ImplQR ;
11
11
use svd:: ImplSVD ;
12
12
use norm:: ImplNorm ;
@@ -20,19 +20,19 @@ pub trait Matrix: Sized {
20
20
/// number of (rows, columns)
21
21
fn size ( & self ) -> ( usize , usize ) ;
22
22
/// Layout (C/Fortran) of matrix
23
- fn layout ( & self ) -> Layout ;
23
+ fn layout ( & self ) -> Result < Layout , StrideError > ;
24
24
/// Operator norm for L-1 norm
25
25
fn norm_1 ( & self ) -> Self :: Scalar ;
26
26
/// Operator norm for L-inf norm
27
27
fn norm_i ( & self ) -> Self :: Scalar ;
28
28
/// Frobenius norm
29
29
fn norm_f ( & self ) -> Self :: Scalar ;
30
30
/// singular-value decomposition (SVD)
31
- fn svd ( self ) -> Result < ( Self , Self :: Vector , Self ) , LapackError > ;
31
+ fn svd ( self ) -> Result < ( Self , Self :: Vector , Self ) , LinalgError > ;
32
32
/// QR decomposition
33
- fn qr ( self ) -> Result < ( Self , Self ) , LapackError > ;
33
+ fn qr ( self ) -> Result < ( Self , Self ) , LinalgError > ;
34
34
/// LU decomposition
35
- fn lu ( self ) -> Result < ( Self :: Permutator , Self , Self ) , LapackError > ;
35
+ fn lu ( self ) -> Result < ( Self :: Permutator , Self , Self ) , LinalgError > ;
36
36
/// permutate matrix (inplace)
37
37
fn permutate ( & mut self , p : & Self :: Permutator ) ;
38
38
/// permutate matrix (outplace)
@@ -52,12 +52,12 @@ impl<A> Matrix for Array<A, Ix2>
52
52
fn size ( & self ) -> ( usize , usize ) {
53
53
( self . rows ( ) , self . cols ( ) )
54
54
}
55
- fn layout ( & self ) -> Layout {
55
+ fn layout ( & self ) -> Result < Layout , StrideError > {
56
56
let strides = self . strides ( ) ;
57
57
if strides[ 0 ] < strides[ 1 ] {
58
- Layout :: ColumnMajor
58
+ Ok ( Layout :: ColumnMajor )
59
59
} else {
60
- Layout :: RowMajor
60
+ Ok ( Layout :: RowMajor )
61
61
}
62
62
}
63
63
fn norm_1 ( & self ) -> Self :: Scalar {
@@ -82,7 +82,7 @@ impl<A> Matrix for Array<A, Ix2>
82
82
let ( m, n) = self . size ( ) ;
83
83
ImplNorm :: norm_f ( m, n, self . clone ( ) . into_raw_vec ( ) )
84
84
}
85
- fn svd ( self ) -> Result < ( Self , Self :: Vector , Self ) , LapackError > {
85
+ fn svd ( self ) -> Result < ( Self , Self :: Vector , Self ) , LinalgError > {
86
86
let strides = self . strides ( ) ;
87
87
let ( m, n) = if strides[ 0 ] > strides[ 1 ] {
88
88
self . size ( )
@@ -102,7 +102,7 @@ impl<A> Matrix for Array<A, Ix2>
102
102
Ok ( ( ua, sv, va) )
103
103
}
104
104
}
105
- fn qr ( self ) -> Result < ( Self , Self ) , LapackError > {
105
+ fn qr ( self ) -> Result < ( Self , Self ) , LinalgError > {
106
106
let ( n, m) = self . size ( ) ;
107
107
let strides = self . strides ( ) ;
108
108
let k = min ( n, m) ;
@@ -136,19 +136,19 @@ impl<A> Matrix for Array<A, Ix2>
136
136
}
137
137
Ok ( ( qm, rm) )
138
138
}
139
- fn lu ( self ) -> Result < ( Self :: Permutator , Self , Self ) , LapackError > {
139
+ fn lu ( self ) -> Result < ( Self :: Permutator , Self , Self ) , LinalgError > {
140
140
let ( n, m) = self . size ( ) ;
141
141
println ! ( "n={}, m={}" , n, m) ;
142
142
let k = min ( n, m) ;
143
- let ( p, mut a) = match self . layout ( ) {
143
+ let ( p, mut a) = match self . layout ( ) ? {
144
144
Layout :: ColumnMajor => {
145
145
println ! ( "ColumnMajor" ) ;
146
- let ( p, l) = ImplSolve :: lu ( self . layout ( ) , n, m, self . clone ( ) . into_raw_vec ( ) ) ?;
146
+ let ( p, l) = ImplSolve :: lu ( self . layout ( ) ? , n, m, self . clone ( ) . into_raw_vec ( ) ) ?;
147
147
( p, Array :: from_vec ( l) . into_shape ( ( m, n) ) . unwrap ( ) . reversed_axes ( ) )
148
148
}
149
149
Layout :: RowMajor => {
150
150
println ! ( "RowMajor" ) ;
151
- let ( p, l) = ImplSolve :: lu ( self . layout ( ) , n, m, self . clone ( ) . into_raw_vec ( ) ) ?;
151
+ let ( p, l) = ImplSolve :: lu ( self . layout ( ) ? , n, m, self . clone ( ) . into_raw_vec ( ) ) ?;
152
152
( p, Array :: from_vec ( l) . into_shape ( ( n, m) ) . unwrap ( ) )
153
153
}
154
154
} ;
0 commit comments