11//! QR decomposition
2+ //!
3+ //! https://en.wikipedia.org/wiki/QR_decomposition
24
35use ndarray:: * ;
46use num_traits:: Zero ;
@@ -7,50 +9,80 @@ use super::convert::*;
79use super :: error:: * ;
810use super :: layout:: * ;
911use super :: triangular:: * ;
12+ use super :: types:: * ;
1013
11- use lapack_traits:: { LapackScalar , UPLO } ;
14+ pub use lapack_traits:: UPLO ;
1215
16+ /// QR decomposition for matrix reference
17+ ///
18+ /// This creates copy due for reshaping array.
19+ /// To avoid copy and the matrix is square, please use `QRSquare*` traits.
1320pub trait QR {
1421 type Q ;
1522 type R ;
1623 fn qr ( & self ) -> Result < ( Self :: Q , Self :: R ) > ;
1724}
1825
26+ /// QR decomposition
27+ ///
28+ /// This creates copy due for reshaping array.
29+ /// To avoid copy and the matrix is square, please use `QRSquare*` traits.
1930pub trait QRInto : Sized {
2031 type Q ;
2132 type R ;
2233 fn qr_into ( self ) -> Result < ( Self :: Q , Self :: R ) > ;
2334}
2435
36+ /// QR decomposition for square matrix reference
2537pub trait QRSquare : Sized {
2638 type Q ;
2739 type R ;
2840 fn qr_square ( & self ) -> Result < ( Self :: Q , Self :: R ) > ;
2941}
3042
43+ /// QR decomposition for square matrix
3144pub trait QRSquareInto : Sized {
3245 type R ;
3346 fn qr_square_into ( self ) -> Result < ( Self , Self :: R ) > ;
3447}
3548
36- impl < A , S > QRSquareInto for ArrayBase < S , Ix2 >
49+ /// QR decomposition for mutable reference of square matrix
50+ pub trait QRSquareMut : Sized {
51+ type R ;
52+ fn qr_square_mut < ' a > ( & ' a mut self ) -> Result < ( & ' a mut Self , Self :: R ) > ;
53+ }
54+
55+ impl < A , S > QRSquareMut for ArrayBase < S , Ix2 >
3756where
38- A : LapackScalar + Copy + Zero ,
57+ A : Scalar ,
3958 S : DataMut < Elem = A > ,
4059{
4160 type R = Array2 < A > ;
4261
43- fn qr_square_into ( mut self ) -> Result < ( Self , Self :: R ) > {
62+ fn qr_square_mut < ' a > ( & ' a mut self ) -> Result < ( & ' a mut Self , Self :: R ) > {
4463 let l = self . square_layout ( ) ?;
4564 let r = A :: qr ( l, self . as_allocated_mut ( ) ?) ?;
4665 let r: Array2 < _ > = into_matrix ( l, r) ?;
4766 Ok ( ( self , r. into_triangular ( UPLO :: Upper ) ) )
4867 }
4968}
5069
70+ impl < A , S > QRSquareInto for ArrayBase < S , Ix2 >
71+ where
72+ A : Scalar ,
73+ S : DataMut < Elem = A > ,
74+ {
75+ type R = Array2 < A > ;
76+
77+ fn qr_square_into ( mut self ) -> Result < ( Self , Self :: R ) > {
78+ let ( _, r) = self . qr_square_mut ( ) ?;
79+ Ok ( ( self , r) )
80+ }
81+ }
82+
5183impl < A , S > QRSquare for ArrayBase < S , Ix2 >
5284where
53- A : LapackScalar + Copy + Zero ,
85+ A : Scalar ,
5486 S : Data < Elem = A > ,
5587{
5688 type Q = Array2 < A > ;
6597
6698impl < A , S > QRInto for ArrayBase < S , Ix2 >
6799where
68- A : LapackScalar + Copy + Zero ,
100+ A : Scalar ,
69101 S : DataMut < Elem = A > ,
70102{
71103 type Q = Array2 < A > ;
85117
86118impl < A , S > QR for ArrayBase < S , Ix2 >
87119where
88- A : LapackScalar + Copy + Zero ,
120+ A : Scalar ,
89121 S : Data < Elem = A > ,
90122{
91123 type Q = Array2 < A > ;
0 commit comments