comparison armadillo-2.4.4/include/armadillo_bits/op_inv_meat.hpp @ 0:8b6102e2a9b0

Armadillo Library
author maxzanoni76 <max.zanoni@eecs.qmul.ac.uk>
date Wed, 11 Apr 2012 09:27:06 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:8b6102e2a9b0
1 // Copyright (C) 2008-2011 NICTA (www.nicta.com.au)
2 // Copyright (C) 2008-2011 Conrad Sanderson
3 //
4 // This file is part of the Armadillo C++ library.
5 // It is provided without any warranty of fitness
6 // for any purpose. You can redistribute this file
7 // and/or modify it under the terms of the GNU
8 // Lesser General Public License (LGPL) as published
9 // by the Free Software Foundation, either version 3
10 // of the License or (at your option) any later version.
11 // (see http://www.opensource.org/licenses for more info)
12
13
14 //! \addtogroup op_inv
15 //! @{
16
17
18 //! immediate inverse of a matrix, storing the result in a dense matrix
19 template<typename eT>
20 inline
21 void
22 op_inv::apply(Mat<eT>& out, const Mat<eT>& A, const bool slow)
23 {
24 arma_extra_debug_sigprint();
25
26 // no need to check for aliasing, due to:
27 // - auxlib::inv() copies A to out before inversion
28 // - for 2x2 and 3x3 matrices the code is alias safe
29
30 bool status = auxlib::inv(out, A, slow);
31
32 if(status == false)
33 {
34 out.reset();
35 arma_bad("inv(): matrix appears to be singular");
36 }
37 }
38
39
40
41 //! immediate inverse of T1, storing the result in a dense matrix
42 template<typename T1>
43 inline
44 void
45 op_inv::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_inv>& X)
46 {
47 arma_extra_debug_sigprint();
48
49 typedef typename T1::elem_type eT;
50
51 const strip_diagmat<T1> strip(X.m);
52
53 if(strip.do_diagmat == true)
54 {
55 op_inv::apply_diag(out, strip.M);
56 }
57 else
58 {
59 const uword mode = X.aux_uword_a;
60
61 const bool status = (mode == 0) ? auxlib::inv(out, X.m) : auxlib::inv(out, X.m, true);
62
63 if(status == false)
64 {
65 out.reset();
66 arma_bad("inv(): matrix appears to be singular");
67 }
68 }
69 }
70
71
72
73 template<typename T1>
74 inline
75 void
76 op_inv::apply_diag(Mat<typename T1::elem_type>& out, const Base<typename T1::elem_type, T1>& X)
77 {
78 arma_extra_debug_sigprint();
79
80 typedef typename T1::elem_type eT;
81
82 const diagmat_proxy_check<T1> A(X.get_ref(), out);
83
84 const uword N = A.n_elem;
85
86 out.set_size(N,N);
87
88 for(uword col=0; col<N; ++col)
89 {
90 for(uword row=0; row<col; ++row) { out.at(row,col) = eT(0); }
91
92 out.at(col,col) = eT(1) / A[col];
93
94 for(uword row=col+1; row<N; ++row) { out.at(row,col) = eT(0); }
95 }
96
97 }
98
99
100
101 //! inverse of T1 (triangular matrices)
102 template<typename T1>
103 inline
104 void
105 op_inv_tr::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_inv_tr>& X)
106 {
107 arma_extra_debug_sigprint();
108
109 const bool status = auxlib::inv_tr(out, X.m, X.aux_uword_a);
110
111 if(status == false)
112 {
113 out.reset();
114 arma_bad("inv(): matrix appears to be singular");
115 }
116 }
117
118
119
120 //! inverse of T1 (symmetric positive definite matrices)
121 template<typename T1>
122 inline
123 void
124 op_inv_sympd::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_inv_sympd>& X)
125 {
126 arma_extra_debug_sigprint();
127
128 const bool status = auxlib::inv_sympd(out, X.m, X.aux_uword_a);
129
130 if(status == false)
131 {
132 out.reset();
133 arma_bad("inv(): matrix appears to be singular");
134 }
135 }
136
137
138
139 //! @}