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