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 |
|
28 #include "dbleCHOL.h" |
1847
|
29 #include "f77-fcn.h" |
457
|
30 #include "lo-error.h" |
|
31 |
|
32 extern "C" |
|
33 { |
4552
|
34 F77_RET_T |
5275
|
35 F77_FUNC (dpotrf, DPOTRF) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, |
|
36 double*, const octave_idx_type&, octave_idx_type& |
4552
|
37 F77_CHAR_ARG_LEN_DECL); |
5340
|
38 |
|
39 F77_RET_T |
|
40 F77_FUNC (dpotri, DPOTRI) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, |
|
41 double*, const octave_idx_type&, octave_idx_type& |
|
42 F77_CHAR_ARG_LEN_DECL); |
457
|
43 } |
|
44 |
5275
|
45 octave_idx_type |
457
|
46 CHOL::init (const Matrix& a) |
|
47 { |
5275
|
48 octave_idx_type a_nr = a.rows (); |
|
49 octave_idx_type a_nc = a.cols (); |
1944
|
50 |
457
|
51 if (a_nr != a_nc) |
|
52 { |
|
53 (*current_liboctave_error_handler) ("CHOL requires square matrix"); |
|
54 return -1; |
|
55 } |
|
56 |
5275
|
57 octave_idx_type n = a_nc; |
|
58 octave_idx_type info; |
457
|
59 |
1944
|
60 chol_mat = a; |
|
61 double *h = chol_mat.fortran_vec (); |
457
|
62 |
4552
|
63 F77_XFCN (dpotrf, DPOTRF, (F77_CONST_CHAR_ARG2 ("U", 1), |
|
64 n, h, n, info |
|
65 F77_CHAR_ARG_LEN (1))); |
457
|
66 |
1944
|
67 if (f77_exception_encountered) |
|
68 (*current_liboctave_error_handler) ("unrecoverable error in dpotrf"); |
|
69 else |
|
70 { |
|
71 // If someone thinks of a more graceful way of doing this (or |
|
72 // faster for that matter :-)), please let me know! |
457
|
73 |
1944
|
74 if (n > 1) |
5275
|
75 for (octave_idx_type j = 0; j < a_nc; j++) |
|
76 for (octave_idx_type i = j+1; i < a_nr; i++) |
5340
|
77 chol_mat.xelem (i, j) = 0.0; |
1944
|
78 } |
457
|
79 |
|
80 return info; |
|
81 } |
|
82 |
5340
|
83 static Matrix |
|
84 chol2inv_internal (const Matrix& r) |
|
85 { |
|
86 Matrix retval; |
|
87 |
|
88 octave_idx_type r_nr = r.rows (); |
|
89 octave_idx_type r_nc = r.cols (); |
|
90 |
|
91 if (r_nr == r_nc) |
|
92 { |
|
93 octave_idx_type n = r_nc; |
|
94 octave_idx_type info; |
|
95 |
|
96 Matrix tmp = r; |
|
97 |
|
98 F77_XFCN (dpotri, DPOTRI, (F77_CONST_CHAR_ARG2 ("U", 1), n, |
|
99 tmp.fortran_vec (), n, info |
|
100 F77_CHAR_ARG_LEN (1))); |
|
101 |
|
102 if (f77_exception_encountered) |
|
103 (*current_liboctave_error_handler) ("unrecoverable error in dpotri"); |
|
104 else |
|
105 { |
|
106 // If someone thinks of a more graceful way of doing this (or |
|
107 // faster for that matter :-)), please let me know! |
|
108 |
|
109 if (n > 1) |
|
110 for (octave_idx_type j = 0; j < r_nc; j++) |
|
111 for (octave_idx_type i = j+1; i < r_nr; i++) |
|
112 tmp.xelem (i, j) = tmp.xelem (j, i); |
|
113 |
|
114 retval = tmp; |
|
115 } |
|
116 } |
|
117 else |
|
118 (*current_liboctave_error_handler) ("chol2inv requires square matrix"); |
|
119 |
|
120 return retval; |
|
121 } |
|
122 |
|
123 // Compute the inverse of a matrix using the Cholesky factorization. |
|
124 Matrix |
|
125 CHOL::inverse (void) const |
|
126 { |
|
127 return chol2inv_internal (chol_mat); |
|
128 } |
|
129 |
|
130 Matrix |
|
131 chol2inv (const Matrix& r) |
|
132 { |
|
133 return chol2inv_internal (r); |
|
134 } |
|
135 |
457
|
136 /* |
|
137 ;;; Local Variables: *** |
|
138 ;;; mode: C++ *** |
|
139 ;;; End: *** |
|
140 */ |