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