1
1
//! Define methods for triangular matrices
2
2
3
3
use ndarray:: * ;
4
+ use num_traits:: Zero ;
5
+ use super :: impl2:: UPLO ;
6
+
4
7
use super :: matrix:: { Matrix , MFloat } ;
5
8
use super :: square:: SquareMatrix ;
6
9
use super :: error:: LinalgError ;
@@ -87,7 +90,7 @@ impl<A: MFloat> SolveTriangular<RcArray<A, Ix2>> for RcArray<A, Ix2> {
87
90
}
88
91
}
89
92
90
- pub fn drop_upper < A : NdFloat , S > ( mut a : ArrayBase < S , Ix2 > ) -> ArrayBase < S , Ix2 >
93
+ pub fn drop_upper < A : Zero , S > ( mut a : ArrayBase < S , Ix2 > ) -> ArrayBase < S , Ix2 >
91
94
where S : DataMut < Elem = A >
92
95
{
93
96
for ( ( i, j) , val) in a. indexed_iter_mut ( ) {
@@ -98,7 +101,7 @@ pub fn drop_upper<A: NdFloat, S>(mut a: ArrayBase<S, Ix2>) -> ArrayBase<S, Ix2>
98
101
a
99
102
}
100
103
101
- pub fn drop_lower < A : NdFloat , S > ( mut a : ArrayBase < S , Ix2 > ) -> ArrayBase < S , Ix2 >
104
+ pub fn drop_lower < A : Zero , S > ( mut a : ArrayBase < S , Ix2 > ) -> ArrayBase < S , Ix2 >
102
105
where S : DataMut < Elem = A >
103
106
{
104
107
for ( ( i, j) , val) in a. indexed_iter_mut ( ) {
@@ -108,3 +111,42 @@ pub fn drop_lower<A: NdFloat, S>(mut a: ArrayBase<S, Ix2>) -> ArrayBase<S, Ix2>
108
111
}
109
112
a
110
113
}
114
+
115
+ pub trait IntoTriangular < T > {
116
+ fn into_triangular ( self , UPLO ) -> T ;
117
+ }
118
+
119
+ impl < ' a , A , S > IntoTriangular < & ' a mut ArrayBase < S , Ix2 > > for & ' a mut ArrayBase < S , Ix2 >
120
+ where A : Zero ,
121
+ S : DataMut < Elem = A >
122
+ {
123
+ fn into_triangular ( self , uplo : UPLO ) -> & ' a mut ArrayBase < S , Ix2 > {
124
+ match uplo {
125
+ UPLO :: Upper => {
126
+ for ( ( i, j) , val) in self . indexed_iter_mut ( ) {
127
+ if i > j {
128
+ * val = A :: zero ( ) ;
129
+ }
130
+ }
131
+ }
132
+ UPLO :: Lower => {
133
+ for ( ( i, j) , val) in self . indexed_iter_mut ( ) {
134
+ if i < j {
135
+ * val = A :: zero ( ) ;
136
+ }
137
+ }
138
+ }
139
+ }
140
+ self
141
+ }
142
+ }
143
+
144
+ impl < A , S > IntoTriangular < ArrayBase < S , Ix2 > > for ArrayBase < S , Ix2 >
145
+ where A : Zero ,
146
+ S : DataMut < Elem = A >
147
+ {
148
+ fn into_triangular ( mut self , uplo : UPLO ) -> ArrayBase < S , Ix2 > {
149
+ ( & mut self ) . into_triangular ( uplo) ;
150
+ self
151
+ }
152
+ }
0 commit comments