457
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
457
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
457
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
457
|
26 #endif |
|
27 |
6486
|
28 #include "dMatrix.h" |
|
29 #include "dRowVector.h" |
457
|
30 #include "CmplxCHOL.h" |
1847
|
31 #include "f77-fcn.h" |
457
|
32 #include "lo-error.h" |
|
33 |
|
34 extern "C" |
|
35 { |
4552
|
36 F77_RET_T |
5340
|
37 F77_FUNC (zpotrf, ZPOTRF) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, |
|
38 Complex*, const octave_idx_type&, octave_idx_type& |
|
39 F77_CHAR_ARG_LEN_DECL); |
|
40 F77_RET_T |
|
41 F77_FUNC (zpotri, ZPOTRI) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, |
|
42 Complex*, const octave_idx_type&, octave_idx_type& |
4552
|
43 F77_CHAR_ARG_LEN_DECL); |
6486
|
44 |
|
45 F77_RET_T |
|
46 F77_FUNC (zpocon, ZPOCON) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, |
|
47 Complex*, const octave_idx_type&, const double&, |
|
48 double&, Complex*, double*, |
|
49 octave_idx_type& F77_CHAR_ARG_LEN_DECL); |
457
|
50 } |
|
51 |
5275
|
52 octave_idx_type |
6486
|
53 ComplexCHOL::init (const ComplexMatrix& a, bool calc_cond) |
457
|
54 { |
5275
|
55 octave_idx_type a_nr = a.rows (); |
|
56 octave_idx_type a_nc = a.cols (); |
457
|
57 |
1944
|
58 if (a_nr != a_nc) |
|
59 { |
|
60 (*current_liboctave_error_handler) |
|
61 ("ComplexCHOL requires square matrix"); |
|
62 return -1; |
|
63 } |
457
|
64 |
5275
|
65 octave_idx_type n = a_nc; |
|
66 octave_idx_type info; |
1944
|
67 |
|
68 chol_mat = a; |
|
69 Complex *h = chol_mat.fortran_vec (); |
457
|
70 |
6486
|
71 // Calculate the norm of the matrix, for later use. |
|
72 double anorm = 0; |
|
73 if (calc_cond) |
|
74 anorm = chol_mat.abs().sum().row(static_cast<octave_idx_type>(0)).max(); |
|
75 |
4552
|
76 F77_XFCN (zpotrf, ZPOTRF, (F77_CONST_CHAR_ARG2 ("U", 1), n, h, n, info |
|
77 F77_CHAR_ARG_LEN (1))); |
457
|
78 |
1944
|
79 if (f77_exception_encountered) |
|
80 (*current_liboctave_error_handler) ("unrecoverable error in zpotrf"); |
|
81 else |
|
82 { |
6486
|
83 xrcond = 0.0; |
|
84 if (info != 0) |
|
85 info = -1; |
|
86 else if (calc_cond) |
|
87 { |
|
88 octave_idx_type zpocon_info = 0; |
|
89 |
|
90 // Now calculate the condition number for non-singular matrix. |
|
91 Array<Complex> z (2*n); |
|
92 Complex *pz = z.fortran_vec (); |
|
93 Array<double> rz (n); |
|
94 double *prz = rz.fortran_vec (); |
|
95 F77_XFCN (zpocon, ZPOCON, (F77_CONST_CHAR_ARG2 ("U", 1), n, h, |
|
96 n, anorm, xrcond, pz, prz, zpocon_info |
|
97 F77_CHAR_ARG_LEN (1))); |
457
|
98 |
6486
|
99 if (f77_exception_encountered) |
|
100 (*current_liboctave_error_handler) |
|
101 ("unrecoverable error in zpocon"); |
|
102 |
|
103 if (zpocon_info != 0) |
|
104 info = -1; |
|
105 } |
|
106 else |
|
107 { |
|
108 // If someone thinks of a more graceful way of doing this (or |
|
109 // faster for that matter :-)), please let me know! |
|
110 |
|
111 if (n > 1) |
|
112 for (octave_idx_type j = 0; j < a_nc; j++) |
|
113 for (octave_idx_type i = j+1; i < a_nr; i++) |
|
114 chol_mat.xelem (i, j) = 0.0; |
|
115 } |
1944
|
116 } |
|
117 |
|
118 return info; |
457
|
119 } |
|
120 |
5340
|
121 static ComplexMatrix |
|
122 chol2inv_internal (const ComplexMatrix& r) |
|
123 { |
|
124 ComplexMatrix retval; |
|
125 |
|
126 octave_idx_type r_nr = r.rows (); |
|
127 octave_idx_type r_nc = r.cols (); |
|
128 |
|
129 if (r_nr == r_nc) |
|
130 { |
|
131 octave_idx_type n = r_nc; |
|
132 octave_idx_type info; |
|
133 |
|
134 ComplexMatrix tmp = r; |
|
135 |
|
136 F77_XFCN (zpotri, ZPOTRI, (F77_CONST_CHAR_ARG2 ("U", 1), n, |
|
137 tmp.fortran_vec (), n, info |
|
138 F77_CHAR_ARG_LEN (1))); |
|
139 |
|
140 if (f77_exception_encountered) |
|
141 (*current_liboctave_error_handler) ("unrecoverable error in zpotri"); |
|
142 else |
|
143 { |
|
144 // If someone thinks of a more graceful way of doing this (or |
|
145 // faster for that matter :-)), please let me know! |
|
146 |
|
147 if (n > 1) |
|
148 for (octave_idx_type j = 0; j < r_nc; j++) |
|
149 for (octave_idx_type i = j+1; i < r_nr; i++) |
|
150 tmp.xelem (i, j) = std::conj (tmp.xelem (j, i)); |
|
151 |
|
152 retval = tmp; |
|
153 } |
|
154 } |
|
155 else |
|
156 (*current_liboctave_error_handler) ("chol2inv requires square matrix"); |
|
157 |
|
158 return retval; |
|
159 } |
|
160 |
|
161 // Compute the inverse of a matrix using the Cholesky factorization. |
|
162 ComplexMatrix |
|
163 ComplexCHOL::inverse (void) const |
|
164 { |
|
165 return chol2inv_internal (chol_mat); |
|
166 } |
|
167 |
|
168 ComplexMatrix |
|
169 chol2inv (const ComplexMatrix& r) |
|
170 { |
|
171 return chol2inv_internal (r); |
|
172 } |
|
173 |
457
|
174 /* |
|
175 ;;; Local Variables: *** |
|
176 ;;; mode: C++ *** |
|
177 ;;; End: *** |
|
178 */ |