| 1145 | |
| 1146 | template <typename EXP> |
| 1147 | const typename matrix_exp<EXP>::matrix_type inv_upper_triangular ( |
| 1148 | const matrix_exp<EXP>& A |
| 1149 | ) |
| 1150 | { |
| 1151 | DLIB_ASSERT(A.nr() == A.nc(), |
| 1152 | "\tconst matrix inv_upper_triangular(const matrix_exp& A)" |
| 1153 | << "\n\tA must be a square matrix" |
| 1154 | << "\n\tA.nr(): " << A.nr() |
| 1155 | << "\n\tA.nc(): " << A.nc() |
| 1156 | ); |
| 1157 | |
| 1158 | typedef typename matrix_exp<EXP>::matrix_type matrix_type; |
| 1159 | |
| 1160 | matrix_type m(A); |
| 1161 | |
| 1162 | for(long c = 0; c < m.nc(); ++c) |
| 1163 | { |
| 1164 | if( m(c,c) == 0 ) |
| 1165 | { |
| 1166 | // there isn't an inverse so just give up |
| 1167 | return m; |
| 1168 | } |
| 1169 | |
| 1170 | // compute m(c,c) |
| 1171 | m(c,c) = 1/m(c,c); |
| 1172 | |
| 1173 | // compute the values in column c that are above m(c,c) |
| 1174 | for(long r = 0; r < c; ++r) |
| 1175 | { |
| 1176 | const long n = c-r; |
| 1177 | m(r,c) = -m(c,c)*subm(m,r,r,1,n)*subm(m,r,c,n,1); |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | return m; |
| 1182 | |
| 1183 | } |
| 1184 | |
| 1185 | // ---------------------------------------------------------------------------------------- |
| 1186 | |