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 "dRowVector.h" |
457
|
29 #include "dbleCHOL.h" |
1847
|
30 #include "f77-fcn.h" |
457
|
31 #include "lo-error.h" |
|
32 |
|
33 extern "C" |
|
34 { |
4552
|
35 F77_RET_T |
5275
|
36 F77_FUNC (dpotrf, DPOTRF) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, |
|
37 double*, const octave_idx_type&, octave_idx_type& |
4552
|
38 F77_CHAR_ARG_LEN_DECL); |
5340
|
39 |
|
40 F77_RET_T |
|
41 F77_FUNC (dpotri, DPOTRI) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, |
|
42 double*, const octave_idx_type&, octave_idx_type& |
|
43 F77_CHAR_ARG_LEN_DECL); |
6486
|
44 |
|
45 F77_RET_T |
|
46 F77_FUNC (dpocon, DPOCON) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, |
|
47 double*, const octave_idx_type&, const double&, |
|
48 double&, double*, octave_idx_type*, |
|
49 octave_idx_type& F77_CHAR_ARG_LEN_DECL); |
457
|
50 } |
|
51 |
5275
|
52 octave_idx_type |
6486
|
53 CHOL::init (const Matrix& a, bool calc_cond) |
457
|
54 { |
5275
|
55 octave_idx_type a_nr = a.rows (); |
|
56 octave_idx_type a_nc = a.cols (); |
1944
|
57 |
457
|
58 if (a_nr != a_nc) |
|
59 { |
|
60 (*current_liboctave_error_handler) ("CHOL requires square matrix"); |
|
61 return -1; |
|
62 } |
|
63 |
5275
|
64 octave_idx_type n = a_nc; |
|
65 octave_idx_type info; |
457
|
66 |
1944
|
67 chol_mat = a; |
|
68 double *h = chol_mat.fortran_vec (); |
457
|
69 |
6486
|
70 // Calculate the norm of the matrix, for later use. |
|
71 double anorm = 0; |
|
72 if (calc_cond) |
|
73 anorm = chol_mat.abs().sum().row(static_cast<octave_idx_type>(0)).max(); |
|
74 |
4552
|
75 F77_XFCN (dpotrf, DPOTRF, (F77_CONST_CHAR_ARG2 ("U", 1), |
|
76 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 dpotrf"); |
|
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 dpocon_info = 0; |
|
89 |
|
90 // Now calculate the condition number for non-singular matrix. |
|
91 Array<double> z (3*n); |
|
92 double *pz = z.fortran_vec (); |
|
93 Array<octave_idx_type> iz (n); |
|
94 octave_idx_type *piz = iz.fortran_vec (); |
|
95 F77_XFCN (dpocon, DPOCON, (F77_CONST_CHAR_ARG2 ("U", 1), n, h, |
|
96 n, anorm, xrcond, pz, piz, dpocon_info |
|
97 F77_CHAR_ARG_LEN (1))); |
457
|
98 |
6486
|
99 if (f77_exception_encountered) |
|
100 (*current_liboctave_error_handler) |
|
101 ("unrecoverable error in dpocon"); |
|
102 |
|
103 if (dpocon_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 } |
457
|
117 |
|
118 return info; |
|
119 } |
|
120 |
5340
|
121 static Matrix |
|
122 chol2inv_internal (const Matrix& r) |
|
123 { |
|
124 Matrix 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; |
6486
|
132 octave_idx_type info = 0; |
5340
|
133 |
|
134 Matrix tmp = r; |
6486
|
135 double *v = tmp.fortran_vec(); |
5340
|
136 |
6486
|
137 if (info == 0) |
5340
|
138 { |
6486
|
139 F77_XFCN (dpotri, DPOTRI, (F77_CONST_CHAR_ARG2 ("U", 1), n, |
|
140 v, n, info |
|
141 F77_CHAR_ARG_LEN (1))); |
5340
|
142 |
6486
|
143 if (f77_exception_encountered) |
|
144 (*current_liboctave_error_handler) |
|
145 ("unrecoverable error in dpotri"); |
|
146 else |
|
147 { |
|
148 // If someone thinks of a more graceful way of doing this (or |
|
149 // faster for that matter :-)), please let me know! |
5340
|
150 |
6486
|
151 if (n > 1) |
|
152 for (octave_idx_type j = 0; j < r_nc; j++) |
|
153 for (octave_idx_type i = j+1; i < r_nr; i++) |
|
154 tmp.xelem (i, j) = tmp.xelem (j, i); |
|
155 |
|
156 retval = tmp; |
|
157 } |
5340
|
158 } |
|
159 } |
|
160 else |
|
161 (*current_liboctave_error_handler) ("chol2inv requires square matrix"); |
|
162 |
|
163 return retval; |
|
164 } |
|
165 |
|
166 // Compute the inverse of a matrix using the Cholesky factorization. |
|
167 Matrix |
|
168 CHOL::inverse (void) const |
|
169 { |
|
170 return chol2inv_internal (chol_mat); |
|
171 } |
|
172 |
|
173 Matrix |
|
174 chol2inv (const Matrix& r) |
|
175 { |
|
176 return chol2inv_internal (r); |
|
177 } |
|
178 |
457
|
179 /* |
|
180 ;;; Local Variables: *** |
|
181 ;;; mode: C++ *** |
|
182 ;;; End: *** |
|
183 */ |