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