4331
|
1 /* |
|
2 |
|
3 Copyright (C) 2001 Ross Lippert and Paul Kienzle |
|
4 |
|
5 This program is free software; you can redistribute it and/or modify |
|
6 it under the terms of the GNU General Public License as published by |
|
7 the Free Software Foundation; either version 2 of the License, or |
|
8 (at your option) any later version. |
|
9 |
|
10 This program is distributed in the hope that it will be useful, |
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 GNU General Public License for more details. |
|
14 |
|
15 You should have received a copy of the GNU General Public License |
|
16 along with this program; if not, write to the Free Software |
5307
|
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 02110-1301 USA |
4331
|
19 |
|
20 */ |
|
21 |
|
22 #ifdef HAVE_CONFIG_H |
|
23 #include <config.h> |
|
24 #endif |
|
25 |
|
26 #include <float.h> |
|
27 |
|
28 #include "CmplxSCHUR.h" |
|
29 #include "lo-ieee.h" |
|
30 #include "lo-mappers.h" |
|
31 |
|
32 #include "defun-dld.h" |
|
33 #include "error.h" |
|
34 #include "gripes.h" |
|
35 #include "utils.h" |
|
36 |
|
37 static inline double |
|
38 getmin (double x, double y) |
|
39 { |
|
40 return x < y ? x : y; |
|
41 } |
|
42 |
|
43 static inline double |
|
44 getmax (double x, double y) |
|
45 { |
|
46 return x > y ? x : y; |
|
47 } |
|
48 |
|
49 static double |
|
50 frobnorm (const ComplexMatrix& A) |
|
51 { |
|
52 double sum = 0; |
|
53 |
5275
|
54 for (octave_idx_type i = 0; i < A.rows (); i++) |
|
55 for (octave_idx_type j = 0; j < A.columns (); j++) |
4331
|
56 sum += real (A(i,j) * conj (A(i,j))); |
|
57 |
|
58 return sqrt (sum); |
|
59 } |
|
60 |
|
61 static double |
|
62 frobnorm (const Matrix& A) |
|
63 { |
|
64 double sum = 0; |
5275
|
65 for (octave_idx_type i = 0; i < A.rows (); i++) |
|
66 for (octave_idx_type j = 0; j < A.columns (); j++) |
4331
|
67 sum += A(i,j) * A(i,j); |
|
68 |
|
69 return sqrt (sum); |
|
70 } |
|
71 |
|
72 |
|
73 static ComplexMatrix |
|
74 sqrtm_from_schur (const ComplexMatrix& U, const ComplexMatrix& T) |
|
75 { |
5275
|
76 const octave_idx_type n = U.rows (); |
4331
|
77 |
|
78 ComplexMatrix R (n, n, 0.0); |
|
79 |
5275
|
80 for (octave_idx_type j = 0; j < n; j++) |
4331
|
81 R(j,j) = sqrt (T(j,j)); |
|
82 |
|
83 const double fudge = sqrt (DBL_MIN); |
|
84 |
5275
|
85 for (octave_idx_type p = 0; p < n-1; p++) |
4331
|
86 { |
5275
|
87 for (octave_idx_type i = 0; i < n-(p+1); i++) |
4331
|
88 { |
5275
|
89 const octave_idx_type j = i + p + 1; |
4331
|
90 |
|
91 Complex s = T(i,j); |
|
92 |
5275
|
93 for (octave_idx_type k = i+1; k < j; k++) |
4331
|
94 s -= R(i,k) * R(k,j); |
|
95 |
|
96 // dividing |
|
97 // R(i,j) = s/(R(i,i)+R(j,j)); |
|
98 // screwing around to not / 0 |
|
99 |
|
100 const Complex d = R(i,i) + R(j,j) + fudge; |
|
101 const Complex conjd = conj (d); |
|
102 |
|
103 R(i,j) = (s*conjd)/(d*conjd); |
|
104 } |
|
105 } |
|
106 |
|
107 return U * R * U.hermitian (); |
|
108 } |
|
109 |
|
110 DEFUN_DLD (sqrtm, args, nargout, |
|
111 "-*- texinfo -*-\n\ |
|
112 @deftypefn {Loadable Function} {[@var{result}, @var{error_estimate}] =} sqrtm (@var{a})\n\ |
|
113 Compute the matrix square root of the square matrix @var{a}.\n\ |
|
114 \n\ |
|
115 Ref: Nicholas J. Higham. A new sqrtm for MATLAB. Numerical Analysis\n\ |
|
116 Report No. 336, Manchester Centre for Computational Mathematics,\n\ |
|
117 Manchester, England, January 1999.\n\ |
5642
|
118 @seealso{expm, logm, funm}\n\ |
|
119 @end deftypefn") |
4331
|
120 { |
|
121 octave_value_list retval; |
|
122 |
|
123 int nargin = args.length (); |
|
124 |
|
125 if (nargin != 1) |
|
126 { |
|
127 print_usage ("sqrtm"); |
|
128 return retval; |
|
129 } |
|
130 |
|
131 octave_value arg = args(0); |
|
132 |
5275
|
133 octave_idx_type n = arg.rows (); |
|
134 octave_idx_type nc = arg.columns (); |
4331
|
135 |
|
136 int arg_is_empty = empty_arg ("sqrtm", n, nc); |
|
137 |
|
138 if (arg_is_empty < 0) |
|
139 return retval; |
|
140 else if (arg_is_empty > 0) |
|
141 return octave_value (Matrix ()); |
|
142 |
|
143 if (n != nc) |
|
144 { |
|
145 gripe_square_matrix_required ("sqrtm"); |
|
146 return retval; |
|
147 } |
|
148 |
|
149 retval(1) = lo_ieee_inf_value (); |
|
150 retval(0) = lo_ieee_nan_value (); |
|
151 |
|
152 if (arg.is_real_scalar ()) |
|
153 { |
|
154 double d = arg.double_value (); |
|
155 if (d > 0.0) |
|
156 { |
|
157 retval(0) = sqrt (d); |
|
158 retval(1) = 0.0; |
|
159 } |
|
160 else |
|
161 { |
|
162 retval(0) = Complex (0.0, sqrt (d)); |
|
163 retval(1) = 0.0; |
|
164 } |
|
165 } |
|
166 else if (arg.is_complex_scalar ()) |
|
167 { |
|
168 Complex c = arg.complex_value (); |
|
169 retval(0) = sqrt (c); |
|
170 retval(1) = 0.0; |
|
171 } |
|
172 else if (arg.is_matrix_type ()) |
|
173 { |
|
174 double err, minT; |
|
175 |
|
176 if (arg.is_real_matrix ()) |
|
177 { |
|
178 Matrix A = arg.matrix_value(); |
|
179 |
|
180 if (error_state) |
|
181 return retval; |
|
182 |
|
183 // XXX FIXME XXX -- eventually, ComplexSCHUR will accept a |
|
184 // real matrix arg. |
|
185 |
|
186 ComplexMatrix Ac (A); |
|
187 |
|
188 const ComplexSCHUR schur (Ac, std::string ()); |
|
189 |
|
190 if (error_state) |
|
191 return retval; |
|
192 |
|
193 const ComplexMatrix U (schur.unitary_matrix ()); |
|
194 const ComplexMatrix T (schur.schur_matrix ()); |
|
195 const ComplexMatrix X (sqrtm_from_schur (U, T)); |
|
196 |
|
197 // Check for minimal imaginary part |
|
198 double normX = 0.0; |
|
199 double imagX = 0.0; |
5275
|
200 for (octave_idx_type i = 0; i < n; i++) |
|
201 for (octave_idx_type j = 0; j < n; j++) |
4331
|
202 { |
|
203 imagX = getmax (imagX, imag (X(i,j))); |
|
204 normX = getmax (normX, abs (X(i,j))); |
|
205 } |
|
206 |
|
207 if (imagX < normX * 100 * DBL_EPSILON) |
|
208 retval(0) = real (X); |
|
209 else |
|
210 retval(0) = X; |
|
211 |
|
212 // Compute error |
|
213 // XXX FIXME XXX can we estimate the error without doing the |
|
214 // matrix multiply? |
|
215 |
|
216 err = frobnorm (X*X - ComplexMatrix (A)) / frobnorm (A); |
|
217 |
|
218 if (xisnan (err)) |
|
219 err = lo_ieee_inf_value (); |
|
220 |
|
221 // Find min diagonal |
|
222 minT = lo_ieee_inf_value (); |
5275
|
223 for (octave_idx_type i=0; i < n; i++) |
4331
|
224 minT = getmin(minT, abs(T(i,i))); |
|
225 } |
|
226 else |
|
227 { |
|
228 ComplexMatrix A = arg.complex_matrix_value (); |
|
229 |
|
230 if (error_state) |
|
231 return retval; |
|
232 |
|
233 const ComplexSCHUR schur (A, std::string ()); |
|
234 |
|
235 if (error_state) |
|
236 return retval; |
|
237 |
|
238 const ComplexMatrix U (schur.unitary_matrix ()); |
|
239 const ComplexMatrix T (schur.schur_matrix ()); |
|
240 const ComplexMatrix X (sqrtm_from_schur (U, T)); |
|
241 |
|
242 retval(0) = X; |
|
243 |
|
244 err = frobnorm (X*X - A) / frobnorm (A); |
|
245 |
|
246 if (xisnan (err)) |
|
247 err = lo_ieee_inf_value (); |
|
248 |
|
249 minT = lo_ieee_inf_value (); |
5275
|
250 for (octave_idx_type i = 0; i < n; i++) |
4331
|
251 minT = getmin (minT, abs (T(i,i))); |
|
252 } |
|
253 |
|
254 retval(1) = err; |
|
255 |
|
256 if (nargout < 2) |
|
257 { |
|
258 if (err > 100*(minT+DBL_EPSILON)*n) |
|
259 { |
|
260 if (minT == 0.0) |
|
261 error ("sqrtm: A is singular, sqrt may not exist"); |
|
262 else if (minT <= sqrt (DBL_MIN)) |
|
263 error ("sqrtm: A is nearly singular, failed to find sqrt"); |
|
264 else |
|
265 error ("sqrtm: failed to find sqrt"); |
|
266 } |
|
267 } |
|
268 } |
|
269 else |
|
270 gripe_wrong_type_arg ("sqrtm", arg); |
|
271 |
|
272 return retval; |
|
273 } |
5307
|
274 |
|
275 /* |
|
276 ;;; Local Variables: *** |
|
277 ;;; mode: C++ *** |
|
278 ;;; End: *** |
|
279 */ |