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