2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
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. |
2928
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include "CmplxCHOL.h" |
|
29 #include "dbleCHOL.h" |
|
30 |
|
31 #include "defun-dld.h" |
|
32 #include "error.h" |
|
33 #include "gripes.h" |
|
34 #include "oct-obj.h" |
|
35 #include "utils.h" |
|
36 |
|
37 DEFUN_DLD (chol, args, nargout, |
3548
|
38 "-*- texinfo -*-\n\ |
3372
|
39 @deftypefn {Loadable Function} {} chol (@var{a})\n\ |
|
40 @cindex Cholesky factorization\n\ |
|
41 Compute the Cholesky factor, @var{r}, of the symmetric positive definite\n\ |
|
42 matrix @var{a}, where\n\ |
|
43 @iftex\n\ |
|
44 @tex\n\ |
|
45 $ R^T R = A $.\n\ |
|
46 @end tex\n\ |
|
47 @end iftex\n\ |
|
48 @ifinfo\n\ |
|
49 \n\ |
|
50 @example\n\ |
|
51 r' * r = a.\n\ |
|
52 @end example\n\ |
|
53 @end ifinfo\n\ |
5340
|
54 @seealso{cholinv, chol2inv}\n\ |
3372
|
55 @end deftypefn") |
2928
|
56 { |
|
57 octave_value_list retval; |
|
58 |
|
59 int nargin = args.length (); |
|
60 |
3243
|
61 if (nargin != 1 || nargout > 2) |
2928
|
62 { |
5823
|
63 print_usage (); |
2928
|
64 return retval; |
|
65 } |
|
66 |
|
67 octave_value arg = args(0); |
|
68 |
5275
|
69 octave_idx_type nr = arg.rows (); |
|
70 octave_idx_type nc = arg.columns (); |
2928
|
71 |
|
72 int arg_is_empty = empty_arg ("chol", nr, nc); |
|
73 |
|
74 if (arg_is_empty < 0) |
|
75 return retval; |
|
76 if (arg_is_empty > 0) |
4233
|
77 return octave_value (Matrix ()); |
2928
|
78 |
|
79 if (arg.is_real_type ()) |
|
80 { |
|
81 Matrix m = arg.matrix_value (); |
|
82 |
|
83 if (! error_state) |
|
84 { |
5275
|
85 octave_idx_type info; |
2928
|
86 CHOL fact (m, info); |
3244
|
87 if (nargout == 2 || info == 0) |
3243
|
88 { |
|
89 retval(1) = static_cast<double> (info); |
|
90 retval(0) = fact.chol_matrix (); |
|
91 } |
3244
|
92 else |
|
93 error ("chol: matrix not positive definite"); |
2928
|
94 } |
|
95 } |
|
96 else if (arg.is_complex_type ()) |
|
97 { |
|
98 ComplexMatrix m = arg.complex_matrix_value (); |
|
99 |
|
100 if (! error_state) |
|
101 { |
5275
|
102 octave_idx_type info; |
2928
|
103 ComplexCHOL fact (m, info); |
3244
|
104 if (nargout == 2 || info == 0) |
3243
|
105 { |
|
106 retval(1) = static_cast<double> (info); |
|
107 retval(0) = fact.chol_matrix (); |
|
108 } |
3244
|
109 else |
|
110 error ("chol: matrix not positive definite"); |
2928
|
111 } |
|
112 } |
|
113 else |
|
114 { |
|
115 gripe_wrong_type_arg ("chol", arg); |
|
116 } |
|
117 |
|
118 return retval; |
|
119 } |
|
120 |
5760
|
121 DEFUN_DLD (cholinv, args, , |
5340
|
122 "-*- texinfo -*-\n\ |
|
123 @deftypefn {Loadable Function} {} cholinv (@var{a})\n\ |
5448
|
124 Use the Cholesky factorization to compute the inverse of the\n\ |
5340
|
125 symmetric positive definite matrix @var{a}.\n\ |
|
126 @seealso{chol, chol2inv}\n\ |
|
127 @end deftypefn") |
|
128 { |
|
129 octave_value retval; |
|
130 |
|
131 int nargin = args.length (); |
|
132 |
|
133 if (nargin == 1) |
|
134 { |
|
135 octave_value arg = args(0); |
|
136 |
|
137 octave_idx_type nr = arg.rows (); |
|
138 octave_idx_type nc = arg.columns (); |
|
139 |
|
140 if (nr == 0 || nc == 0) |
|
141 retval = Matrix (); |
|
142 else |
|
143 { |
|
144 if (arg.is_real_type ()) |
|
145 { |
|
146 Matrix m = arg.matrix_value (); |
|
147 |
|
148 if (! error_state) |
|
149 { |
|
150 octave_idx_type info; |
|
151 CHOL chol (m, info); |
|
152 if (info == 0) |
|
153 retval = chol.inverse (); |
|
154 else |
|
155 error ("cholinv: matrix not positive definite"); |
|
156 } |
|
157 } |
|
158 else if (arg.is_complex_type ()) |
|
159 { |
|
160 ComplexMatrix m = arg.complex_matrix_value (); |
|
161 |
|
162 if (! error_state) |
|
163 { |
|
164 octave_idx_type info; |
|
165 ComplexCHOL chol (m, info); |
|
166 if (info == 0) |
|
167 retval = chol.inverse (); |
|
168 else |
|
169 error ("cholinv: matrix not positive definite"); |
|
170 } |
|
171 } |
|
172 else |
|
173 gripe_wrong_type_arg ("chol", arg); |
|
174 } |
|
175 } |
|
176 else |
5823
|
177 print_usage (); |
5340
|
178 |
|
179 return retval; |
|
180 } |
|
181 |
5760
|
182 DEFUN_DLD (chol2inv, args, , |
5340
|
183 "-*- texinfo -*-\n\ |
5343
|
184 @deftypefn {Loadable Function} {} chol2inv (@var{u})\n\ |
5340
|
185 Invert a symmetric, positive definite square matrix from its Cholesky\n\ |
5343
|
186 decomposition, @var{u}. Note that @var{u} should be an upper-triangular\n\ |
|
187 matrix with positive diagonal elements. @code{chol2inv (@var{u})}\n\ |
|
188 provides @code{inv (@var{u}'*@var{u})} but it is much faster than\n\ |
|
189 using @code{inv}.\n\ |
5340
|
190 @seealso{chol, cholinv}\n\ |
|
191 @end deftypefn") |
|
192 { |
|
193 octave_value retval; |
|
194 |
|
195 int nargin = args.length (); |
|
196 |
|
197 if (nargin == 1) |
|
198 { |
|
199 octave_value arg = args(0); |
|
200 |
|
201 octave_idx_type nr = arg.rows (); |
|
202 octave_idx_type nc = arg.columns (); |
|
203 |
|
204 if (nr == 0 || nc == 0) |
|
205 retval = Matrix (); |
|
206 else |
|
207 { |
|
208 if (arg.is_real_type ()) |
|
209 { |
|
210 Matrix r = arg.matrix_value (); |
|
211 |
|
212 if (! error_state) |
|
213 retval = chol2inv (r); |
|
214 } |
|
215 else if (arg.is_complex_type ()) |
|
216 { |
|
217 ComplexMatrix r = arg.complex_matrix_value (); |
|
218 |
|
219 if (! error_state) |
|
220 retval = chol2inv (r); |
|
221 } |
|
222 else |
|
223 gripe_wrong_type_arg ("chol2inv", arg); |
|
224 } |
|
225 } |
|
226 else |
5823
|
227 print_usage (); |
5340
|
228 |
|
229 return retval; |
|
230 } |
|
231 |
2928
|
232 /* |
|
233 ;;; Local Variables: *** |
|
234 ;;; mode: C++ *** |
|
235 ;;; End: *** |
|
236 */ |
|
237 |