1993
|
1 // Matrix manipulations. |
458
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 John W. Eaton |
458
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
20 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
21 02110-1301, USA. |
458
|
22 |
|
23 */ |
|
24 |
|
25 #ifdef HAVE_CONFIG_H |
1192
|
26 #include <config.h> |
458
|
27 #endif |
|
28 |
1367
|
29 #include <cfloat> |
|
30 |
3503
|
31 #include <iostream> |
6209
|
32 #include <vector> |
1367
|
33 |
4669
|
34 #include "Array-util.h" |
2317
|
35 #include "byte-swap.h" |
2828
|
36 #include "dMatrix.h" |
1819
|
37 #include "dbleAEPBAL.h" |
458
|
38 #include "dbleDET.h" |
1819
|
39 #include "dbleSCHUR.h" |
740
|
40 #include "dbleSVD.h" |
6207
|
41 #include "dbleCHOL.h" |
1847
|
42 #include "f77-fcn.h" |
458
|
43 #include "lo-error.h" |
2354
|
44 #include "lo-ieee.h" |
|
45 #include "lo-mappers.h" |
1968
|
46 #include "lo-utils.h" |
1367
|
47 #include "mx-base.h" |
2828
|
48 #include "mx-m-dm.h" |
3176
|
49 #include "mx-dm-m.h" |
1367
|
50 #include "mx-inlines.cc" |
1650
|
51 #include "oct-cmplx.h" |
4153
|
52 #include "quit.h" |
458
|
53 |
4773
|
54 #if defined (HAVE_FFTW3) |
3827
|
55 #include "oct-fftw.h" |
|
56 #endif |
|
57 |
458
|
58 // Fortran functions we call. |
|
59 |
|
60 extern "C" |
|
61 { |
4552
|
62 F77_RET_T |
|
63 F77_FUNC (dgebal, DGEBAL) (F77_CONST_CHAR_ARG_DECL, |
5275
|
64 const octave_idx_type&, double*, const octave_idx_type&, octave_idx_type&, |
|
65 octave_idx_type&, double*, octave_idx_type& |
4552
|
66 F77_CHAR_ARG_LEN_DECL); |
|
67 |
|
68 F77_RET_T |
|
69 F77_FUNC (dgebak, DGEBAK) (F77_CONST_CHAR_ARG_DECL, |
|
70 F77_CONST_CHAR_ARG_DECL, |
5275
|
71 const octave_idx_type&, const octave_idx_type&, const octave_idx_type&, double*, |
|
72 const octave_idx_type&, double*, const octave_idx_type&, octave_idx_type& |
4552
|
73 F77_CHAR_ARG_LEN_DECL |
|
74 F77_CHAR_ARG_LEN_DECL); |
|
75 |
|
76 |
|
77 F77_RET_T |
|
78 F77_FUNC (dgemm, DGEMM) (F77_CONST_CHAR_ARG_DECL, |
|
79 F77_CONST_CHAR_ARG_DECL, |
5275
|
80 const octave_idx_type&, const octave_idx_type&, const octave_idx_type&, |
|
81 const double&, const double*, const octave_idx_type&, |
|
82 const double*, const octave_idx_type&, const double&, |
|
83 double*, const octave_idx_type& |
4552
|
84 F77_CHAR_ARG_LEN_DECL |
|
85 F77_CHAR_ARG_LEN_DECL); |
|
86 |
|
87 F77_RET_T |
5983
|
88 F77_FUNC (dgemv, DGEMV) (F77_CONST_CHAR_ARG_DECL, |
|
89 const octave_idx_type&, const octave_idx_type&, const double&, |
|
90 const double*, const octave_idx_type&, const double*, |
|
91 const octave_idx_type&, const double&, double*, |
|
92 const octave_idx_type& |
|
93 F77_CHAR_ARG_LEN_DECL); |
|
94 |
|
95 F77_RET_T |
|
96 F77_FUNC (xddot, XDDOT) (const octave_idx_type&, const double*, const octave_idx_type&, |
|
97 const double*, const octave_idx_type&, double&); |
|
98 |
|
99 F77_RET_T |
5275
|
100 F77_FUNC (dgetrf, DGETRF) (const octave_idx_type&, const octave_idx_type&, double*, const octave_idx_type&, |
|
101 octave_idx_type*, octave_idx_type&); |
4329
|
102 |
4552
|
103 F77_RET_T |
5275
|
104 F77_FUNC (dgetrs, DGETRS) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, const octave_idx_type&, |
|
105 const double*, const octave_idx_type&, |
|
106 const octave_idx_type*, double*, const octave_idx_type&, octave_idx_type& |
4552
|
107 F77_CHAR_ARG_LEN_DECL); |
|
108 |
|
109 F77_RET_T |
5275
|
110 F77_FUNC (dgetri, DGETRI) (const octave_idx_type&, double*, const octave_idx_type&, const octave_idx_type*, |
|
111 double*, const octave_idx_type&, octave_idx_type&); |
4552
|
112 |
|
113 F77_RET_T |
5275
|
114 F77_FUNC (dgecon, DGECON) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, double*, |
|
115 const octave_idx_type&, const double&, double&, |
|
116 double*, octave_idx_type*, octave_idx_type& |
4552
|
117 F77_CHAR_ARG_LEN_DECL); |
|
118 |
|
119 F77_RET_T |
6926
|
120 F77_FUNC (dgelsy, DGELSY) (const octave_idx_type&, const octave_idx_type&, const octave_idx_type&, |
5275
|
121 double*, const octave_idx_type&, double*, |
6926
|
122 const octave_idx_type&, octave_idx_type*, double&, octave_idx_type&, |
5275
|
123 double*, const octave_idx_type&, octave_idx_type&); |
458
|
124 |
5785
|
125 F77_RET_T |
|
126 F77_FUNC (dpotrf, DPOTRF) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, |
|
127 double *, const octave_idx_type&, |
|
128 octave_idx_type& F77_CHAR_ARG_LEN_DECL); |
|
129 |
|
130 F77_RET_T |
|
131 F77_FUNC (dpocon, DPOCON) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, |
|
132 double*, const octave_idx_type&, const double&, |
|
133 double&, double*, octave_idx_type*, |
|
134 octave_idx_type& F77_CHAR_ARG_LEN_DECL); |
|
135 F77_RET_T |
|
136 F77_FUNC (dpotrs, DPOTRS) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, |
|
137 const octave_idx_type&, const double*, |
|
138 const octave_idx_type&, double*, |
|
139 const octave_idx_type&, octave_idx_type& |
|
140 F77_CHAR_ARG_LEN_DECL); |
|
141 |
|
142 F77_RET_T |
6207
|
143 F77_FUNC (dtrtri, DTRTRI) (F77_CONST_CHAR_ARG_DECL, F77_CONST_CHAR_ARG_DECL, |
|
144 const octave_idx_type&, const double*, |
|
145 const octave_idx_type&, octave_idx_type& |
|
146 F77_CHAR_ARG_LEN_DECL |
|
147 F77_CHAR_ARG_LEN_DECL); |
|
148 F77_RET_T |
5785
|
149 F77_FUNC (dtrcon, DTRCON) (F77_CONST_CHAR_ARG_DECL, F77_CONST_CHAR_ARG_DECL, |
|
150 F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, |
|
151 const double*, const octave_idx_type&, double&, |
|
152 double*, octave_idx_type*, octave_idx_type& |
|
153 F77_CHAR_ARG_LEN_DECL |
|
154 F77_CHAR_ARG_LEN_DECL |
|
155 F77_CHAR_ARG_LEN_DECL); |
|
156 F77_RET_T |
|
157 F77_FUNC (dtrtrs, DTRTRS) (F77_CONST_CHAR_ARG_DECL, F77_CONST_CHAR_ARG_DECL, |
|
158 F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, |
|
159 const octave_idx_type&, const double*, |
|
160 const octave_idx_type&, double*, |
|
161 const octave_idx_type&, octave_idx_type& |
|
162 F77_CHAR_ARG_LEN_DECL |
|
163 F77_CHAR_ARG_LEN_DECL |
|
164 F77_CHAR_ARG_LEN_DECL); |
|
165 |
1360
|
166 // Note that the original complex fft routines were not written for |
|
167 // double complex arguments. They have been modified by adding an |
|
168 // implicit double precision (a-h,o-z) statement at the beginning of |
|
169 // each subroutine. |
458
|
170 |
4552
|
171 F77_RET_T |
5275
|
172 F77_FUNC (cffti, CFFTI) (const octave_idx_type&, Complex*); |
4552
|
173 |
|
174 F77_RET_T |
5275
|
175 F77_FUNC (cfftf, CFFTF) (const octave_idx_type&, Complex*, Complex*); |
4552
|
176 |
|
177 F77_RET_T |
5275
|
178 F77_FUNC (cfftb, CFFTB) (const octave_idx_type&, Complex*, Complex*); |
4552
|
179 |
|
180 F77_RET_T |
|
181 F77_FUNC (dlartg, DLARTG) (const double&, const double&, double&, |
|
182 double&, double&); |
|
183 |
|
184 F77_RET_T |
|
185 F77_FUNC (dtrsyl, DTRSYL) (F77_CONST_CHAR_ARG_DECL, |
|
186 F77_CONST_CHAR_ARG_DECL, |
5275
|
187 const octave_idx_type&, const octave_idx_type&, const octave_idx_type&, |
|
188 const double*, const octave_idx_type&, const double*, |
|
189 const octave_idx_type&, const double*, const octave_idx_type&, |
|
190 double&, octave_idx_type& |
4552
|
191 F77_CHAR_ARG_LEN_DECL |
|
192 F77_CHAR_ARG_LEN_DECL); |
|
193 |
|
194 F77_RET_T |
5275
|
195 F77_FUNC (xdlange, XDLANGE) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type&, |
|
196 const octave_idx_type&, const double*, |
|
197 const octave_idx_type&, double*, double& |
4552
|
198 F77_CHAR_ARG_LEN_DECL); |
458
|
199 } |
|
200 |
1360
|
201 // Matrix class. |
458
|
202 |
2349
|
203 Matrix::Matrix (const RowVector& rv) |
|
204 : MArray2<double> (1, rv.length (), 0.0) |
|
205 { |
5275
|
206 for (octave_idx_type i = 0; i < rv.length (); i++) |
2349
|
207 elem (0, i) = rv.elem (i); |
|
208 } |
|
209 |
|
210 Matrix::Matrix (const ColumnVector& cv) |
|
211 : MArray2<double> (cv.length (), 1, 0.0) |
|
212 { |
5275
|
213 for (octave_idx_type i = 0; i < cv.length (); i++) |
2349
|
214 elem (i, 0) = cv.elem (i); |
|
215 } |
|
216 |
458
|
217 Matrix::Matrix (const DiagMatrix& a) |
1214
|
218 : MArray2<double> (a.rows (), a.cols (), 0.0) |
458
|
219 { |
5275
|
220 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
221 elem (i, i) = a.elem (i, i); |
|
222 } |
|
223 |
5775
|
224 // FIXME -- could we use a templated mixed-type copy function |
1574
|
225 // here? |
|
226 |
2828
|
227 Matrix::Matrix (const boolMatrix& a) |
|
228 : MArray2<double> (a.rows (), a.cols ()) |
|
229 { |
5275
|
230 for (octave_idx_type i = 0; i < a.rows (); i++) |
|
231 for (octave_idx_type j = 0; j < a.cols (); j++) |
2828
|
232 elem (i, j) = a.elem (i, j); |
|
233 } |
|
234 |
1574
|
235 Matrix::Matrix (const charMatrix& a) |
|
236 : MArray2<double> (a.rows (), a.cols ()) |
|
237 { |
5275
|
238 for (octave_idx_type i = 0; i < a.rows (); i++) |
|
239 for (octave_idx_type j = 0; j < a.cols (); j++) |
1574
|
240 elem (i, j) = a.elem (i, j); |
|
241 } |
|
242 |
2385
|
243 bool |
458
|
244 Matrix::operator == (const Matrix& a) const |
|
245 { |
|
246 if (rows () != a.rows () || cols () != a.cols ()) |
2385
|
247 return false; |
458
|
248 |
3769
|
249 return mx_inline_equal (data (), a.data (), length ()); |
458
|
250 } |
|
251 |
2385
|
252 bool |
458
|
253 Matrix::operator != (const Matrix& a) const |
|
254 { |
|
255 return !(*this == a); |
|
256 } |
|
257 |
3354
|
258 bool |
|
259 Matrix::is_symmetric (void) const |
|
260 { |
|
261 if (is_square () && rows () > 0) |
|
262 { |
5275
|
263 for (octave_idx_type i = 0; i < rows (); i++) |
|
264 for (octave_idx_type j = i+1; j < cols (); j++) |
3354
|
265 if (elem (i, j) != elem (j, i)) |
|
266 return false; |
|
267 |
|
268 return true; |
|
269 } |
|
270 |
|
271 return false; |
|
272 } |
|
273 |
458
|
274 Matrix& |
5275
|
275 Matrix::insert (const Matrix& a, octave_idx_type r, octave_idx_type c) |
458
|
276 { |
1561
|
277 Array2<double>::insert (a, r, c); |
458
|
278 return *this; |
|
279 } |
|
280 |
|
281 Matrix& |
5275
|
282 Matrix::insert (const RowVector& a, octave_idx_type r, octave_idx_type c) |
458
|
283 { |
5275
|
284 octave_idx_type a_len = a.length (); |
4316
|
285 |
1698
|
286 if (r < 0 || r >= rows () || c < 0 || c + a_len > cols ()) |
458
|
287 { |
|
288 (*current_liboctave_error_handler) ("range error for insert"); |
|
289 return *this; |
|
290 } |
|
291 |
4316
|
292 if (a_len > 0) |
|
293 { |
|
294 make_unique (); |
|
295 |
5275
|
296 for (octave_idx_type i = 0; i < a_len; i++) |
4316
|
297 xelem (r, c+i) = a.elem (i); |
|
298 } |
458
|
299 |
|
300 return *this; |
|
301 } |
|
302 |
|
303 Matrix& |
5275
|
304 Matrix::insert (const ColumnVector& a, octave_idx_type r, octave_idx_type c) |
458
|
305 { |
5275
|
306 octave_idx_type a_len = a.length (); |
4316
|
307 |
1698
|
308 if (r < 0 || r + a_len > rows () || c < 0 || c >= cols ()) |
458
|
309 { |
|
310 (*current_liboctave_error_handler) ("range error for insert"); |
|
311 return *this; |
|
312 } |
|
313 |
4316
|
314 if (a_len > 0) |
|
315 { |
|
316 make_unique (); |
|
317 |
5275
|
318 for (octave_idx_type i = 0; i < a_len; i++) |
4316
|
319 xelem (r+i, c) = a.elem (i); |
|
320 } |
458
|
321 |
|
322 return *this; |
|
323 } |
|
324 |
|
325 Matrix& |
5275
|
326 Matrix::insert (const DiagMatrix& a, octave_idx_type r, octave_idx_type c) |
458
|
327 { |
5275
|
328 octave_idx_type a_nr = a.rows (); |
|
329 octave_idx_type a_nc = a.cols (); |
1697
|
330 |
1698
|
331 if (r < 0 || r + a_nr > rows () || c < 0 || c + a_nc > cols ()) |
458
|
332 { |
|
333 (*current_liboctave_error_handler) ("range error for insert"); |
|
334 return *this; |
|
335 } |
|
336 |
1697
|
337 fill (0.0, r, c, r + a_nr - 1, c + a_nc - 1); |
|
338 |
5275
|
339 octave_idx_type a_len = a.length (); |
4316
|
340 |
|
341 if (a_len > 0) |
|
342 { |
|
343 make_unique (); |
|
344 |
5275
|
345 for (octave_idx_type i = 0; i < a_len; i++) |
4316
|
346 xelem (r+i, c+i) = a.elem (i, i); |
|
347 } |
458
|
348 |
|
349 return *this; |
|
350 } |
|
351 |
|
352 Matrix& |
|
353 Matrix::fill (double val) |
|
354 { |
5275
|
355 octave_idx_type nr = rows (); |
|
356 octave_idx_type nc = cols (); |
4316
|
357 |
458
|
358 if (nr > 0 && nc > 0) |
4316
|
359 { |
|
360 make_unique (); |
|
361 |
5275
|
362 for (octave_idx_type j = 0; j < nc; j++) |
|
363 for (octave_idx_type i = 0; i < nr; i++) |
4316
|
364 xelem (i, j) = val; |
|
365 } |
458
|
366 |
|
367 return *this; |
|
368 } |
|
369 |
|
370 Matrix& |
5275
|
371 Matrix::fill (double val, octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) |
458
|
372 { |
5275
|
373 octave_idx_type nr = rows (); |
|
374 octave_idx_type nc = cols (); |
4316
|
375 |
458
|
376 if (r1 < 0 || r2 < 0 || c1 < 0 || c2 < 0 |
|
377 || r1 >= nr || r2 >= nr || c1 >= nc || c2 >= nc) |
|
378 { |
|
379 (*current_liboctave_error_handler) ("range error for fill"); |
|
380 return *this; |
|
381 } |
|
382 |
5275
|
383 if (r1 > r2) { octave_idx_type tmp = r1; r1 = r2; r2 = tmp; } |
|
384 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
458
|
385 |
4316
|
386 if (r2 >= r1 && c2 >= c1) |
|
387 { |
|
388 make_unique (); |
|
389 |
5275
|
390 for (octave_idx_type j = c1; j <= c2; j++) |
|
391 for (octave_idx_type i = r1; i <= r2; i++) |
4316
|
392 xelem (i, j) = val; |
|
393 } |
458
|
394 |
|
395 return *this; |
|
396 } |
|
397 |
|
398 Matrix |
|
399 Matrix::append (const Matrix& a) const |
|
400 { |
5275
|
401 octave_idx_type nr = rows (); |
|
402 octave_idx_type nc = cols (); |
458
|
403 if (nr != a.rows ()) |
|
404 { |
|
405 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
406 return Matrix (); |
|
407 } |
|
408 |
5275
|
409 octave_idx_type nc_insert = nc; |
458
|
410 Matrix retval (nr, nc + a.cols ()); |
|
411 retval.insert (*this, 0, 0); |
|
412 retval.insert (a, 0, nc_insert); |
|
413 return retval; |
|
414 } |
|
415 |
|
416 Matrix |
|
417 Matrix::append (const RowVector& a) const |
|
418 { |
5275
|
419 octave_idx_type nr = rows (); |
|
420 octave_idx_type nc = cols (); |
458
|
421 if (nr != 1) |
|
422 { |
|
423 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
424 return Matrix (); |
|
425 } |
|
426 |
5275
|
427 octave_idx_type nc_insert = nc; |
458
|
428 Matrix retval (nr, nc + a.length ()); |
|
429 retval.insert (*this, 0, 0); |
|
430 retval.insert (a, 0, nc_insert); |
|
431 return retval; |
|
432 } |
|
433 |
|
434 Matrix |
|
435 Matrix::append (const ColumnVector& a) const |
|
436 { |
5275
|
437 octave_idx_type nr = rows (); |
|
438 octave_idx_type nc = cols (); |
458
|
439 if (nr != a.length ()) |
|
440 { |
|
441 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
442 return Matrix (); |
|
443 } |
|
444 |
5275
|
445 octave_idx_type nc_insert = nc; |
458
|
446 Matrix retval (nr, nc + 1); |
|
447 retval.insert (*this, 0, 0); |
|
448 retval.insert (a, 0, nc_insert); |
|
449 return retval; |
|
450 } |
|
451 |
|
452 Matrix |
|
453 Matrix::append (const DiagMatrix& a) const |
|
454 { |
5275
|
455 octave_idx_type nr = rows (); |
|
456 octave_idx_type nc = cols (); |
458
|
457 if (nr != a.rows ()) |
|
458 { |
|
459 (*current_liboctave_error_handler) ("row dimension mismatch for append"); |
|
460 return *this; |
|
461 } |
|
462 |
5275
|
463 octave_idx_type nc_insert = nc; |
458
|
464 Matrix retval (nr, nc + a.cols ()); |
|
465 retval.insert (*this, 0, 0); |
|
466 retval.insert (a, 0, nc_insert); |
|
467 return retval; |
|
468 } |
|
469 |
|
470 Matrix |
|
471 Matrix::stack (const Matrix& a) const |
|
472 { |
5275
|
473 octave_idx_type nr = rows (); |
|
474 octave_idx_type nc = cols (); |
458
|
475 if (nc != a.cols ()) |
|
476 { |
|
477 (*current_liboctave_error_handler) |
|
478 ("column dimension mismatch for stack"); |
|
479 return Matrix (); |
|
480 } |
|
481 |
5275
|
482 octave_idx_type nr_insert = nr; |
458
|
483 Matrix retval (nr + a.rows (), nc); |
|
484 retval.insert (*this, 0, 0); |
|
485 retval.insert (a, nr_insert, 0); |
|
486 return retval; |
|
487 } |
|
488 |
|
489 Matrix |
|
490 Matrix::stack (const RowVector& a) const |
|
491 { |
5275
|
492 octave_idx_type nr = rows (); |
|
493 octave_idx_type nc = cols (); |
458
|
494 if (nc != a.length ()) |
|
495 { |
|
496 (*current_liboctave_error_handler) |
|
497 ("column dimension mismatch for stack"); |
|
498 return Matrix (); |
|
499 } |
|
500 |
5275
|
501 octave_idx_type nr_insert = nr; |
458
|
502 Matrix retval (nr + 1, nc); |
|
503 retval.insert (*this, 0, 0); |
|
504 retval.insert (a, nr_insert, 0); |
|
505 return retval; |
|
506 } |
|
507 |
|
508 Matrix |
|
509 Matrix::stack (const ColumnVector& a) const |
|
510 { |
5275
|
511 octave_idx_type nr = rows (); |
|
512 octave_idx_type nc = cols (); |
458
|
513 if (nc != 1) |
|
514 { |
|
515 (*current_liboctave_error_handler) |
|
516 ("column dimension mismatch for stack"); |
|
517 return Matrix (); |
|
518 } |
|
519 |
5275
|
520 octave_idx_type nr_insert = nr; |
458
|
521 Matrix retval (nr + a.length (), nc); |
|
522 retval.insert (*this, 0, 0); |
|
523 retval.insert (a, nr_insert, 0); |
|
524 return retval; |
|
525 } |
|
526 |
|
527 Matrix |
|
528 Matrix::stack (const DiagMatrix& a) const |
|
529 { |
5275
|
530 octave_idx_type nr = rows (); |
|
531 octave_idx_type nc = cols (); |
458
|
532 if (nc != a.cols ()) |
|
533 { |
|
534 (*current_liboctave_error_handler) |
|
535 ("column dimension mismatch for stack"); |
|
536 return Matrix (); |
|
537 } |
|
538 |
5275
|
539 octave_idx_type nr_insert = nr; |
458
|
540 Matrix retval (nr + a.rows (), nc); |
|
541 retval.insert (*this, 0, 0); |
|
542 retval.insert (a, nr_insert, 0); |
|
543 return retval; |
|
544 } |
|
545 |
|
546 Matrix |
1205
|
547 real (const ComplexMatrix& a) |
|
548 { |
5275
|
549 octave_idx_type a_len = a.length (); |
1205
|
550 Matrix retval; |
|
551 if (a_len > 0) |
3769
|
552 retval = Matrix (mx_inline_real_dup (a.data (), a_len), |
|
553 a.rows (), a.cols ()); |
1205
|
554 return retval; |
|
555 } |
|
556 |
|
557 Matrix |
|
558 imag (const ComplexMatrix& a) |
|
559 { |
5275
|
560 octave_idx_type a_len = a.length (); |
1205
|
561 Matrix retval; |
|
562 if (a_len > 0) |
3769
|
563 retval = Matrix (mx_inline_imag_dup (a.data (), a_len), |
|
564 a.rows (), a.cols ()); |
1205
|
565 return retval; |
|
566 } |
|
567 |
|
568 Matrix |
5275
|
569 Matrix::extract (octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const |
458
|
570 { |
5275
|
571 if (r1 > r2) { octave_idx_type tmp = r1; r1 = r2; r2 = tmp; } |
|
572 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
|
573 |
|
574 octave_idx_type new_r = r2 - r1 + 1; |
|
575 octave_idx_type new_c = c2 - c1 + 1; |
458
|
576 |
|
577 Matrix result (new_r, new_c); |
|
578 |
5275
|
579 for (octave_idx_type j = 0; j < new_c; j++) |
|
580 for (octave_idx_type i = 0; i < new_r; i++) |
4316
|
581 result.xelem (i, j) = elem (r1+i, c1+j); |
|
582 |
|
583 return result; |
|
584 } |
|
585 |
|
586 Matrix |
5275
|
587 Matrix::extract_n (octave_idx_type r1, octave_idx_type c1, octave_idx_type nr, octave_idx_type nc) const |
4316
|
588 { |
|
589 Matrix result (nr, nc); |
|
590 |
5275
|
591 for (octave_idx_type j = 0; j < nc; j++) |
|
592 for (octave_idx_type i = 0; i < nr; i++) |
4316
|
593 result.xelem (i, j) = elem (r1+i, c1+j); |
458
|
594 |
|
595 return result; |
|
596 } |
|
597 |
|
598 // extract row or column i. |
|
599 |
|
600 RowVector |
5275
|
601 Matrix::row (octave_idx_type i) const |
458
|
602 { |
5275
|
603 octave_idx_type nc = cols (); |
458
|
604 if (i < 0 || i >= rows ()) |
|
605 { |
|
606 (*current_liboctave_error_handler) ("invalid row selection"); |
|
607 return RowVector (); |
|
608 } |
|
609 |
|
610 RowVector retval (nc); |
5275
|
611 for (octave_idx_type j = 0; j < nc; j++) |
4316
|
612 retval.xelem (j) = elem (i, j); |
458
|
613 |
|
614 return retval; |
|
615 } |
|
616 |
|
617 ColumnVector |
5275
|
618 Matrix::column (octave_idx_type i) const |
458
|
619 { |
5275
|
620 octave_idx_type nr = rows (); |
458
|
621 if (i < 0 || i >= cols ()) |
|
622 { |
|
623 (*current_liboctave_error_handler) ("invalid column selection"); |
|
624 return ColumnVector (); |
|
625 } |
|
626 |
|
627 ColumnVector retval (nr); |
5275
|
628 for (octave_idx_type j = 0; j < nr; j++) |
4316
|
629 retval.xelem (j) = elem (j, i); |
458
|
630 |
|
631 return retval; |
|
632 } |
|
633 |
|
634 Matrix |
|
635 Matrix::inverse (void) const |
|
636 { |
5275
|
637 octave_idx_type info; |
458
|
638 double rcond; |
6207
|
639 MatrixType mattype (*this); |
|
640 return inverse (mattype, info, rcond, 0, 0); |
|
641 } |
|
642 |
|
643 Matrix |
6479
|
644 Matrix::inverse (octave_idx_type& info) const |
|
645 { |
|
646 double rcond; |
|
647 MatrixType mattype (*this); |
|
648 return inverse (mattype, info, rcond, 0, 0); |
|
649 } |
|
650 |
|
651 Matrix |
|
652 Matrix::inverse (octave_idx_type& info, double& rcond, int force, |
|
653 int calc_cond) const |
|
654 { |
|
655 MatrixType mattype (*this); |
|
656 return inverse (mattype, info, rcond, force, calc_cond); |
|
657 } |
|
658 |
|
659 Matrix |
6207
|
660 Matrix::inverse (MatrixType& mattype) const |
|
661 { |
|
662 octave_idx_type info; |
|
663 double rcond; |
|
664 return inverse (mattype, info, rcond, 0, 0); |
|
665 } |
|
666 |
|
667 Matrix |
|
668 Matrix::inverse (MatrixType &mattype, octave_idx_type& info) const |
|
669 { |
|
670 double rcond; |
|
671 return inverse (mattype, info, rcond, 0, 0); |
458
|
672 } |
|
673 |
|
674 Matrix |
6207
|
675 Matrix::tinverse (MatrixType &mattype, octave_idx_type& info, double& rcond, |
|
676 int force, int calc_cond) const |
458
|
677 { |
6207
|
678 Matrix retval; |
|
679 |
|
680 octave_idx_type nr = rows (); |
|
681 octave_idx_type nc = cols (); |
|
682 |
|
683 if (nr != nc || nr == 0 || nc == 0) |
|
684 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
|
685 else |
|
686 { |
|
687 int typ = mattype.type (); |
|
688 char uplo = (typ == MatrixType::Lower ? 'L' : 'U'); |
|
689 char udiag = 'N'; |
|
690 retval = *this; |
|
691 double *tmp_data = retval.fortran_vec (); |
|
692 |
|
693 F77_XFCN (dtrtri, DTRTRI, (F77_CONST_CHAR_ARG2 (&uplo, 1), |
|
694 F77_CONST_CHAR_ARG2 (&udiag, 1), |
|
695 nr, tmp_data, nr, info |
|
696 F77_CHAR_ARG_LEN (1) |
|
697 F77_CHAR_ARG_LEN (1))); |
|
698 |
|
699 if (f77_exception_encountered) |
|
700 (*current_liboctave_error_handler) ("unrecoverable error in dtrtri"); |
|
701 else |
|
702 { |
|
703 // Throw-away extra info LAPACK gives so as to not change output. |
|
704 rcond = 0.0; |
|
705 if (info != 0) |
|
706 info = -1; |
|
707 else if (calc_cond) |
|
708 { |
|
709 octave_idx_type dtrcon_info = 0; |
|
710 char job = '1'; |
|
711 |
|
712 OCTAVE_LOCAL_BUFFER (double, work, 3 * nr); |
|
713 OCTAVE_LOCAL_BUFFER (octave_idx_type, iwork, nr); |
|
714 |
|
715 F77_XFCN (dtrcon, DTRCON, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
716 F77_CONST_CHAR_ARG2 (&uplo, 1), |
|
717 F77_CONST_CHAR_ARG2 (&udiag, 1), |
|
718 nr, tmp_data, nr, rcond, |
|
719 work, iwork, dtrcon_info |
|
720 F77_CHAR_ARG_LEN (1) |
|
721 F77_CHAR_ARG_LEN (1) |
|
722 F77_CHAR_ARG_LEN (1))); |
|
723 |
|
724 if (f77_exception_encountered) |
|
725 (*current_liboctave_error_handler) |
|
726 ("unrecoverable error in dtrcon"); |
|
727 |
|
728 if (dtrcon_info != 0) |
|
729 info = -1; |
|
730 } |
|
731 } |
|
732 |
|
733 if (info == -1 && ! force) |
|
734 retval = *this; // Restore matrix contents. |
|
735 } |
|
736 |
|
737 return retval; |
458
|
738 } |
|
739 |
6207
|
740 |
458
|
741 Matrix |
6207
|
742 Matrix::finverse (MatrixType &mattype, octave_idx_type& info, double& rcond, |
|
743 int force, int calc_cond) const |
458
|
744 { |
1948
|
745 Matrix retval; |
|
746 |
5275
|
747 octave_idx_type nr = rows (); |
|
748 octave_idx_type nc = cols (); |
1948
|
749 |
458
|
750 if (nr != nc || nr == 0 || nc == 0) |
1948
|
751 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
458
|
752 else |
|
753 { |
5275
|
754 Array<octave_idx_type> ipvt (nr); |
|
755 octave_idx_type *pipvt = ipvt.fortran_vec (); |
1948
|
756 |
|
757 retval = *this; |
|
758 double *tmp_data = retval.fortran_vec (); |
|
759 |
4329
|
760 Array<double> z(1); |
5275
|
761 octave_idx_type lwork = -1; |
4329
|
762 |
4330
|
763 // Query the optimum work array size. |
4329
|
764 F77_XFCN (dgetri, DGETRI, (nc, tmp_data, nr, pipvt, |
|
765 z.fortran_vec (), lwork, info)); |
|
766 |
|
767 if (f77_exception_encountered) |
|
768 { |
|
769 (*current_liboctave_error_handler) |
|
770 ("unrecoverable error in dgetri"); |
|
771 return retval; |
|
772 } |
|
773 |
5275
|
774 lwork = static_cast<octave_idx_type> (z(0)); |
4329
|
775 lwork = (lwork < 2 *nc ? 2*nc : lwork); |
|
776 z.resize (lwork); |
|
777 double *pz = z.fortran_vec (); |
|
778 |
|
779 info = 0; |
|
780 |
4330
|
781 // Calculate the norm of the matrix, for later use. |
4329
|
782 double anorm = 0; |
|
783 if (calc_cond) |
5275
|
784 anorm = retval.abs().sum().row(static_cast<octave_idx_type>(0)).max(); |
4329
|
785 |
|
786 F77_XFCN (dgetrf, DGETRF, (nc, nc, tmp_data, nr, pipvt, info)); |
1948
|
787 |
|
788 if (f77_exception_encountered) |
4329
|
789 (*current_liboctave_error_handler) ("unrecoverable error in dgetrf"); |
1948
|
790 else |
|
791 { |
4330
|
792 // Throw-away extra info LAPACK gives so as to not change output. |
4509
|
793 rcond = 0.0; |
|
794 if (info != 0) |
1948
|
795 info = -1; |
4329
|
796 else if (calc_cond) |
|
797 { |
5275
|
798 octave_idx_type dgecon_info = 0; |
5061
|
799 |
4330
|
800 // Now calculate the condition number for non-singular matrix. |
4329
|
801 char job = '1'; |
5275
|
802 Array<octave_idx_type> iz (nc); |
|
803 octave_idx_type *piz = iz.fortran_vec (); |
4552
|
804 F77_XFCN (dgecon, DGECON, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
805 nc, tmp_data, nr, anorm, |
5061
|
806 rcond, pz, piz, dgecon_info |
4552
|
807 F77_CHAR_ARG_LEN (1))); |
4329
|
808 |
|
809 if (f77_exception_encountered) |
|
810 (*current_liboctave_error_handler) |
|
811 ("unrecoverable error in dgecon"); |
|
812 |
5061
|
813 if (dgecon_info != 0) |
4329
|
814 info = -1; |
|
815 } |
1948
|
816 |
|
817 if (info == -1 && ! force) |
|
818 retval = *this; // Restore matrix contents. |
|
819 else |
|
820 { |
5275
|
821 octave_idx_type dgetri_info = 0; |
5061
|
822 |
4329
|
823 F77_XFCN (dgetri, DGETRI, (nc, tmp_data, nr, pipvt, |
5061
|
824 pz, lwork, dgetri_info)); |
1948
|
825 |
|
826 if (f77_exception_encountered) |
|
827 (*current_liboctave_error_handler) |
4329
|
828 ("unrecoverable error in dgetri"); |
|
829 |
5061
|
830 if (dgetri_info != 0) |
4329
|
831 info = -1; |
1948
|
832 } |
|
833 } |
6207
|
834 |
|
835 if (info != 0) |
|
836 mattype.mark_as_rectangular(); |
458
|
837 } |
|
838 |
1948
|
839 return retval; |
458
|
840 } |
|
841 |
740
|
842 Matrix |
6207
|
843 Matrix::inverse (MatrixType &mattype, octave_idx_type& info, double& rcond, |
|
844 int force, int calc_cond) const |
|
845 { |
|
846 int typ = mattype.type (false); |
|
847 Matrix ret; |
|
848 |
|
849 if (typ == MatrixType::Unknown) |
|
850 typ = mattype.type (*this); |
|
851 |
|
852 if (typ == MatrixType::Upper || typ == MatrixType::Lower) |
|
853 ret = tinverse (mattype, info, rcond, force, calc_cond); |
6840
|
854 else |
6207
|
855 { |
|
856 if (mattype.is_hermitian ()) |
|
857 { |
6486
|
858 CHOL chol (*this, info, calc_cond); |
6207
|
859 if (info == 0) |
6486
|
860 { |
|
861 if (calc_cond) |
|
862 rcond = chol.rcond (); |
|
863 else |
|
864 rcond = 1.0; |
|
865 ret = chol.inverse (); |
|
866 } |
6207
|
867 else |
|
868 mattype.mark_as_unsymmetric (); |
|
869 } |
|
870 |
|
871 if (!mattype.is_hermitian ()) |
|
872 ret = finverse(mattype, info, rcond, force, calc_cond); |
6840
|
873 |
|
874 if (rcond == 0.) |
|
875 ret = Matrix (rows (), columns (), octave_Inf); |
6207
|
876 } |
|
877 |
|
878 return ret; |
|
879 } |
|
880 |
|
881 Matrix |
4384
|
882 Matrix::pseudo_inverse (double tol) const |
740
|
883 { |
3480
|
884 SVD result (*this, SVD::economy); |
740
|
885 |
|
886 DiagMatrix S = result.singular_values (); |
|
887 Matrix U = result.left_singular_matrix (); |
|
888 Matrix V = result.right_singular_matrix (); |
|
889 |
|
890 ColumnVector sigma = S.diag (); |
|
891 |
5275
|
892 octave_idx_type r = sigma.length () - 1; |
|
893 octave_idx_type nr = rows (); |
|
894 octave_idx_type nc = cols (); |
740
|
895 |
|
896 if (tol <= 0.0) |
|
897 { |
|
898 if (nr > nc) |
|
899 tol = nr * sigma.elem (0) * DBL_EPSILON; |
|
900 else |
|
901 tol = nc * sigma.elem (0) * DBL_EPSILON; |
|
902 } |
|
903 |
|
904 while (r >= 0 && sigma.elem (r) < tol) |
|
905 r--; |
|
906 |
|
907 if (r < 0) |
|
908 return Matrix (nc, nr, 0.0); |
|
909 else |
|
910 { |
|
911 Matrix Ur = U.extract (0, 0, nr-1, r); |
|
912 DiagMatrix D = DiagMatrix (sigma.extract (0, r)) . inverse (); |
|
913 Matrix Vr = V.extract (0, 0, nc-1, r); |
|
914 return Vr * D * Ur.transpose (); |
|
915 } |
|
916 } |
|
917 |
4773
|
918 #if defined (HAVE_FFTW3) |
3827
|
919 |
|
920 ComplexMatrix |
|
921 Matrix::fourier (void) const |
|
922 { |
|
923 size_t nr = rows (); |
|
924 size_t nc = cols (); |
|
925 |
|
926 ComplexMatrix retval (nr, nc); |
|
927 |
|
928 size_t npts, nsamples; |
|
929 |
|
930 if (nr == 1 || nc == 1) |
|
931 { |
|
932 npts = nr > nc ? nr : nc; |
|
933 nsamples = 1; |
|
934 } |
|
935 else |
|
936 { |
|
937 npts = nr; |
|
938 nsamples = nc; |
|
939 } |
|
940 |
4773
|
941 const double *in (fortran_vec ()); |
3827
|
942 Complex *out (retval.fortran_vec ()); |
|
943 |
4773
|
944 octave_fftw::fft (in, out, npts, nsamples); |
3827
|
945 |
|
946 return retval; |
|
947 } |
|
948 |
|
949 ComplexMatrix |
|
950 Matrix::ifourier (void) const |
|
951 { |
|
952 size_t nr = rows (); |
|
953 size_t nc = cols (); |
|
954 |
|
955 ComplexMatrix retval (nr, nc); |
|
956 |
|
957 size_t npts, nsamples; |
|
958 |
|
959 if (nr == 1 || nc == 1) |
|
960 { |
|
961 npts = nr > nc ? nr : nc; |
|
962 nsamples = 1; |
|
963 } |
|
964 else |
|
965 { |
|
966 npts = nr; |
|
967 nsamples = nc; |
|
968 } |
|
969 |
|
970 ComplexMatrix tmp (*this); |
|
971 Complex *in (tmp.fortran_vec ()); |
|
972 Complex *out (retval.fortran_vec ()); |
|
973 |
4773
|
974 octave_fftw::ifft (in, out, npts, nsamples); |
3827
|
975 |
|
976 return retval; |
|
977 } |
|
978 |
|
979 ComplexMatrix |
|
980 Matrix::fourier2d (void) const |
|
981 { |
4773
|
982 dim_vector dv(rows (), cols ()); |
|
983 |
|
984 const double *in = fortran_vec (); |
|
985 ComplexMatrix retval (rows (), cols ()); |
|
986 octave_fftw::fftNd (in, retval.fortran_vec (), 2, dv); |
3827
|
987 |
|
988 return retval; |
|
989 } |
|
990 |
|
991 ComplexMatrix |
|
992 Matrix::ifourier2d (void) const |
|
993 { |
4773
|
994 dim_vector dv(rows (), cols ()); |
3827
|
995 |
|
996 ComplexMatrix retval (*this); |
4773
|
997 Complex *out (retval.fortran_vec ()); |
|
998 |
|
999 octave_fftw::ifftNd (out, out, 2, dv); |
3827
|
1000 |
|
1001 return retval; |
|
1002 } |
|
1003 |
|
1004 #else |
|
1005 |
458
|
1006 ComplexMatrix |
|
1007 Matrix::fourier (void) const |
|
1008 { |
1948
|
1009 ComplexMatrix retval; |
|
1010 |
5275
|
1011 octave_idx_type nr = rows (); |
|
1012 octave_idx_type nc = cols (); |
|
1013 |
|
1014 octave_idx_type npts, nsamples; |
1948
|
1015 |
458
|
1016 if (nr == 1 || nc == 1) |
|
1017 { |
|
1018 npts = nr > nc ? nr : nc; |
|
1019 nsamples = 1; |
|
1020 } |
|
1021 else |
|
1022 { |
|
1023 npts = nr; |
|
1024 nsamples = nc; |
|
1025 } |
|
1026 |
5275
|
1027 octave_idx_type nn = 4*npts+15; |
1948
|
1028 |
|
1029 Array<Complex> wsave (nn); |
|
1030 Complex *pwsave = wsave.fortran_vec (); |
|
1031 |
3585
|
1032 retval = ComplexMatrix (*this); |
1948
|
1033 Complex *tmp_data = retval.fortran_vec (); |
|
1034 |
3887
|
1035 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
458
|
1036 |
5275
|
1037 for (octave_idx_type j = 0; j < nsamples; j++) |
4153
|
1038 { |
|
1039 OCTAVE_QUIT; |
|
1040 |
|
1041 F77_FUNC (cfftf, CFFTF) (npts, &tmp_data[npts*j], pwsave); |
|
1042 } |
1948
|
1043 |
|
1044 return retval; |
458
|
1045 } |
|
1046 |
|
1047 ComplexMatrix |
|
1048 Matrix::ifourier (void) const |
|
1049 { |
1948
|
1050 ComplexMatrix retval; |
|
1051 |
5275
|
1052 octave_idx_type nr = rows (); |
|
1053 octave_idx_type nc = cols (); |
|
1054 |
|
1055 octave_idx_type npts, nsamples; |
1948
|
1056 |
458
|
1057 if (nr == 1 || nc == 1) |
|
1058 { |
|
1059 npts = nr > nc ? nr : nc; |
|
1060 nsamples = 1; |
|
1061 } |
|
1062 else |
|
1063 { |
|
1064 npts = nr; |
|
1065 nsamples = nc; |
|
1066 } |
|
1067 |
5275
|
1068 octave_idx_type nn = 4*npts+15; |
1948
|
1069 |
|
1070 Array<Complex> wsave (nn); |
|
1071 Complex *pwsave = wsave.fortran_vec (); |
|
1072 |
3585
|
1073 retval = ComplexMatrix (*this); |
1948
|
1074 Complex *tmp_data = retval.fortran_vec (); |
|
1075 |
3887
|
1076 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
458
|
1077 |
5275
|
1078 for (octave_idx_type j = 0; j < nsamples; j++) |
4153
|
1079 { |
|
1080 OCTAVE_QUIT; |
|
1081 |
|
1082 F77_FUNC (cfftb, CFFTB) (npts, &tmp_data[npts*j], pwsave); |
|
1083 } |
458
|
1084 |
5275
|
1085 for (octave_idx_type j = 0; j < npts*nsamples; j++) |
3572
|
1086 tmp_data[j] = tmp_data[j] / static_cast<double> (npts); |
458
|
1087 |
1948
|
1088 return retval; |
458
|
1089 } |
|
1090 |
677
|
1091 ComplexMatrix |
|
1092 Matrix::fourier2d (void) const |
|
1093 { |
1948
|
1094 ComplexMatrix retval; |
|
1095 |
5275
|
1096 octave_idx_type nr = rows (); |
|
1097 octave_idx_type nc = cols (); |
|
1098 |
|
1099 octave_idx_type npts, nsamples; |
1948
|
1100 |
677
|
1101 if (nr == 1 || nc == 1) |
|
1102 { |
|
1103 npts = nr > nc ? nr : nc; |
|
1104 nsamples = 1; |
|
1105 } |
|
1106 else |
|
1107 { |
|
1108 npts = nr; |
|
1109 nsamples = nc; |
|
1110 } |
|
1111 |
5275
|
1112 octave_idx_type nn = 4*npts+15; |
1948
|
1113 |
|
1114 Array<Complex> wsave (nn); |
|
1115 Complex *pwsave = wsave.fortran_vec (); |
|
1116 |
3585
|
1117 retval = ComplexMatrix (*this); |
1948
|
1118 Complex *tmp_data = retval.fortran_vec (); |
|
1119 |
3887
|
1120 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1121 |
5275
|
1122 for (octave_idx_type j = 0; j < nsamples; j++) |
4153
|
1123 { |
|
1124 OCTAVE_QUIT; |
|
1125 |
|
1126 F77_FUNC (cfftf, CFFTF) (npts, &tmp_data[npts*j], pwsave); |
|
1127 } |
677
|
1128 |
|
1129 npts = nc; |
|
1130 nsamples = nr; |
|
1131 nn = 4*npts+15; |
1948
|
1132 |
|
1133 wsave.resize (nn); |
|
1134 pwsave = wsave.fortran_vec (); |
|
1135 |
4773
|
1136 Array<Complex> tmp (npts); |
|
1137 Complex *prow = tmp.fortran_vec (); |
1948
|
1138 |
3887
|
1139 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1140 |
5275
|
1141 for (octave_idx_type j = 0; j < nsamples; j++) |
677
|
1142 { |
4153
|
1143 OCTAVE_QUIT; |
|
1144 |
5275
|
1145 for (octave_idx_type i = 0; i < npts; i++) |
1948
|
1146 prow[i] = tmp_data[i*nr + j]; |
|
1147 |
3887
|
1148 F77_FUNC (cfftf, CFFTF) (npts, prow, pwsave); |
677
|
1149 |
5275
|
1150 for (octave_idx_type i = 0; i < npts; i++) |
1948
|
1151 tmp_data[i*nr + j] = prow[i]; |
677
|
1152 } |
|
1153 |
1948
|
1154 return retval; |
677
|
1155 } |
|
1156 |
|
1157 ComplexMatrix |
|
1158 Matrix::ifourier2d (void) const |
|
1159 { |
1948
|
1160 ComplexMatrix retval; |
|
1161 |
5275
|
1162 octave_idx_type nr = rows (); |
|
1163 octave_idx_type nc = cols (); |
|
1164 |
|
1165 octave_idx_type npts, nsamples; |
1948
|
1166 |
677
|
1167 if (nr == 1 || nc == 1) |
|
1168 { |
|
1169 npts = nr > nc ? nr : nc; |
|
1170 nsamples = 1; |
|
1171 } |
|
1172 else |
|
1173 { |
|
1174 npts = nr; |
|
1175 nsamples = nc; |
|
1176 } |
|
1177 |
5275
|
1178 octave_idx_type nn = 4*npts+15; |
1948
|
1179 |
|
1180 Array<Complex> wsave (nn); |
|
1181 Complex *pwsave = wsave.fortran_vec (); |
|
1182 |
3585
|
1183 retval = ComplexMatrix (*this); |
1948
|
1184 Complex *tmp_data = retval.fortran_vec (); |
|
1185 |
3887
|
1186 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1187 |
5275
|
1188 for (octave_idx_type j = 0; j < nsamples; j++) |
4153
|
1189 { |
|
1190 OCTAVE_QUIT; |
|
1191 |
|
1192 F77_FUNC (cfftb, CFFTB) (npts, &tmp_data[npts*j], pwsave); |
|
1193 } |
677
|
1194 |
5275
|
1195 for (octave_idx_type j = 0; j < npts*nsamples; j++) |
3572
|
1196 tmp_data[j] = tmp_data[j] / static_cast<double> (npts); |
677
|
1197 |
|
1198 npts = nc; |
|
1199 nsamples = nr; |
|
1200 nn = 4*npts+15; |
1948
|
1201 |
|
1202 wsave.resize (nn); |
|
1203 pwsave = wsave.fortran_vec (); |
|
1204 |
4773
|
1205 Array<Complex> tmp (npts); |
|
1206 Complex *prow = tmp.fortran_vec (); |
1948
|
1207 |
3887
|
1208 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1209 |
5275
|
1210 for (octave_idx_type j = 0; j < nsamples; j++) |
677
|
1211 { |
4153
|
1212 OCTAVE_QUIT; |
|
1213 |
5275
|
1214 for (octave_idx_type i = 0; i < npts; i++) |
1948
|
1215 prow[i] = tmp_data[i*nr + j]; |
|
1216 |
3887
|
1217 F77_FUNC (cfftb, CFFTB) (npts, prow, pwsave); |
677
|
1218 |
5275
|
1219 for (octave_idx_type i = 0; i < npts; i++) |
3572
|
1220 tmp_data[i*nr + j] = prow[i] / static_cast<double> (npts); |
677
|
1221 } |
|
1222 |
1948
|
1223 return retval; |
677
|
1224 } |
|
1225 |
3827
|
1226 #endif |
|
1227 |
458
|
1228 DET |
|
1229 Matrix::determinant (void) const |
|
1230 { |
5275
|
1231 octave_idx_type info; |
458
|
1232 double rcond; |
4329
|
1233 return determinant (info, rcond, 0); |
458
|
1234 } |
|
1235 |
|
1236 DET |
5275
|
1237 Matrix::determinant (octave_idx_type& info) const |
458
|
1238 { |
|
1239 double rcond; |
4329
|
1240 return determinant (info, rcond, 0); |
458
|
1241 } |
|
1242 |
|
1243 DET |
5275
|
1244 Matrix::determinant (octave_idx_type& info, double& rcond, int calc_cond) const |
458
|
1245 { |
|
1246 DET retval; |
|
1247 |
5275
|
1248 octave_idx_type nr = rows (); |
|
1249 octave_idx_type nc = cols (); |
458
|
1250 |
|
1251 if (nr == 0 || nc == 0) |
|
1252 { |
5634
|
1253 retval = DET (1.0, 0); |
458
|
1254 } |
|
1255 else |
|
1256 { |
5275
|
1257 Array<octave_idx_type> ipvt (nr); |
|
1258 octave_idx_type *pipvt = ipvt.fortran_vec (); |
1948
|
1259 |
|
1260 Matrix atmp = *this; |
|
1261 double *tmp_data = atmp.fortran_vec (); |
|
1262 |
4329
|
1263 info = 0; |
|
1264 |
4330
|
1265 // Calculate the norm of the matrix, for later use. |
4329
|
1266 double anorm = 0; |
|
1267 if (calc_cond) |
5275
|
1268 anorm = atmp.abs().sum().row(static_cast<octave_idx_type>(0)).max(); |
4329
|
1269 |
|
1270 F77_XFCN (dgetrf, DGETRF, (nr, nr, tmp_data, nr, pipvt, info)); |
1948
|
1271 |
|
1272 if (f77_exception_encountered) |
4329
|
1273 (*current_liboctave_error_handler) ("unrecoverable error in dgetrf"); |
458
|
1274 else |
|
1275 { |
4330
|
1276 // Throw-away extra info LAPACK gives so as to not change output. |
4509
|
1277 rcond = 0.0; |
|
1278 if (info != 0) |
1948
|
1279 { |
4509
|
1280 info = -1; |
|
1281 retval = DET (); |
4329
|
1282 } |
|
1283 else |
1948
|
1284 { |
4329
|
1285 if (calc_cond) |
|
1286 { |
4330
|
1287 // Now calc the condition number for non-singular matrix. |
4329
|
1288 char job = '1'; |
|
1289 Array<double> z (4 * nc); |
|
1290 double *pz = z.fortran_vec (); |
5275
|
1291 Array<octave_idx_type> iz (nc); |
|
1292 octave_idx_type *piz = iz.fortran_vec (); |
4329
|
1293 |
4552
|
1294 F77_XFCN (dgecon, DGECON, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1295 nc, tmp_data, nr, anorm, |
|
1296 rcond, pz, piz, info |
|
1297 F77_CHAR_ARG_LEN (1))); |
4329
|
1298 |
|
1299 if (f77_exception_encountered) |
|
1300 (*current_liboctave_error_handler) |
|
1301 ("unrecoverable error in dgecon"); |
|
1302 } |
|
1303 |
4509
|
1304 if (info != 0) |
4329
|
1305 { |
|
1306 info = -1; |
|
1307 retval = DET (); |
|
1308 } |
|
1309 else |
|
1310 { |
5634
|
1311 double c = 1.0; |
|
1312 int e = 0; |
|
1313 |
|
1314 for (octave_idx_type i = 0; i < nc; i++) |
4329
|
1315 { |
5634
|
1316 if (ipvt(i) != (i+1)) |
|
1317 c = -c; |
|
1318 |
|
1319 c *= atmp(i,i); |
|
1320 |
|
1321 if (c == 0.0) |
|
1322 break; |
|
1323 |
|
1324 while (fabs (c) < 0.5) |
4329
|
1325 { |
5634
|
1326 c *= 2.0; |
|
1327 e--; |
4329
|
1328 } |
5634
|
1329 |
|
1330 while (fabs (c) >= 2.0) |
4329
|
1331 { |
5634
|
1332 c /= 2.0; |
|
1333 e++; |
4329
|
1334 } |
|
1335 } |
5634
|
1336 |
|
1337 retval = DET (c, e); |
4329
|
1338 } |
1948
|
1339 } |
458
|
1340 } |
|
1341 } |
|
1342 |
|
1343 return retval; |
|
1344 } |
|
1345 |
|
1346 Matrix |
5785
|
1347 Matrix::utsolve (MatrixType &mattype, const Matrix& b, octave_idx_type& info, |
|
1348 double& rcond, solve_singularity_handler sing_handler, |
|
1349 bool calc_cond) const |
|
1350 { |
|
1351 Matrix retval; |
|
1352 |
|
1353 octave_idx_type nr = rows (); |
|
1354 octave_idx_type nc = cols (); |
|
1355 |
6924
|
1356 if (nr != b.rows ()) |
5785
|
1357 (*current_liboctave_error_handler) |
|
1358 ("matrix dimension mismatch solution of linear equations"); |
6924
|
1359 else if (nr == 0 || nc == 0 || b.cols () == 0) |
|
1360 retval = Matrix (nc, b.cols (), 0.0); |
5785
|
1361 else |
|
1362 { |
|
1363 volatile int typ = mattype.type (); |
|
1364 |
|
1365 if (typ == MatrixType::Permuted_Upper || |
|
1366 typ == MatrixType::Upper) |
|
1367 { |
|
1368 octave_idx_type b_nc = b.cols (); |
|
1369 rcond = 1.; |
|
1370 info = 0; |
|
1371 |
|
1372 if (typ == MatrixType::Permuted_Upper) |
|
1373 { |
|
1374 (*current_liboctave_error_handler) |
6390
|
1375 ("permuted triangular matrix not implemented"); |
5785
|
1376 } |
|
1377 else |
|
1378 { |
|
1379 const double *tmp_data = fortran_vec (); |
|
1380 |
|
1381 if (calc_cond) |
|
1382 { |
|
1383 char norm = '1'; |
|
1384 char uplo = 'U'; |
|
1385 char dia = 'N'; |
|
1386 |
|
1387 Array<double> z (3 * nc); |
|
1388 double *pz = z.fortran_vec (); |
|
1389 Array<octave_idx_type> iz (nc); |
|
1390 octave_idx_type *piz = iz.fortran_vec (); |
|
1391 |
|
1392 F77_XFCN (dtrcon, DTRCON, (F77_CONST_CHAR_ARG2 (&norm, 1), |
|
1393 F77_CONST_CHAR_ARG2 (&uplo, 1), |
|
1394 F77_CONST_CHAR_ARG2 (&dia, 1), |
|
1395 nr, tmp_data, nr, rcond, |
|
1396 pz, piz, info |
|
1397 F77_CHAR_ARG_LEN (1) |
|
1398 F77_CHAR_ARG_LEN (1) |
|
1399 F77_CHAR_ARG_LEN (1))); |
|
1400 |
|
1401 if (f77_exception_encountered) |
|
1402 (*current_liboctave_error_handler) |
|
1403 ("unrecoverable error in dtrcon"); |
|
1404 |
|
1405 if (info != 0) |
|
1406 info = -2; |
|
1407 |
|
1408 volatile double rcond_plus_one = rcond + 1.0; |
|
1409 |
|
1410 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
1411 { |
|
1412 info = -2; |
|
1413 |
|
1414 if (sing_handler) |
|
1415 sing_handler (rcond); |
|
1416 else |
|
1417 (*current_liboctave_error_handler) |
|
1418 ("matrix singular to machine precision, rcond = %g", |
|
1419 rcond); |
|
1420 } |
|
1421 } |
|
1422 |
|
1423 if (info == 0) |
|
1424 { |
|
1425 retval = b; |
|
1426 double *result = retval.fortran_vec (); |
|
1427 |
|
1428 char uplo = 'U'; |
|
1429 char trans = 'N'; |
|
1430 char dia = 'N'; |
|
1431 |
|
1432 F77_XFCN (dtrtrs, DTRTRS, (F77_CONST_CHAR_ARG2 (&uplo, 1), |
|
1433 F77_CONST_CHAR_ARG2 (&trans, 1), |
|
1434 F77_CONST_CHAR_ARG2 (&dia, 1), |
|
1435 nr, b_nc, tmp_data, nr, |
|
1436 result, nr, info |
|
1437 F77_CHAR_ARG_LEN (1) |
|
1438 F77_CHAR_ARG_LEN (1) |
|
1439 F77_CHAR_ARG_LEN (1))); |
|
1440 |
|
1441 if (f77_exception_encountered) |
|
1442 (*current_liboctave_error_handler) |
|
1443 ("unrecoverable error in dtrtrs"); |
|
1444 } |
|
1445 } |
|
1446 } |
|
1447 else |
|
1448 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
1449 } |
|
1450 |
|
1451 return retval; |
|
1452 } |
|
1453 |
|
1454 Matrix |
|
1455 Matrix::ltsolve (MatrixType &mattype, const Matrix& b, octave_idx_type& info, |
|
1456 double& rcond, solve_singularity_handler sing_handler, |
|
1457 bool calc_cond) const |
|
1458 { |
|
1459 Matrix retval; |
|
1460 |
|
1461 octave_idx_type nr = rows (); |
|
1462 octave_idx_type nc = cols (); |
|
1463 |
6924
|
1464 if (nr != b.rows ()) |
5785
|
1465 (*current_liboctave_error_handler) |
|
1466 ("matrix dimension mismatch solution of linear equations"); |
6924
|
1467 else if (nr == 0 || nc == 0 || b.cols () == 0) |
|
1468 retval = Matrix (nc, b.cols (), 0.0); |
5785
|
1469 else |
|
1470 { |
|
1471 volatile int typ = mattype.type (); |
|
1472 |
|
1473 if (typ == MatrixType::Permuted_Lower || |
|
1474 typ == MatrixType::Lower) |
|
1475 { |
|
1476 octave_idx_type b_nc = b.cols (); |
|
1477 rcond = 1.; |
|
1478 info = 0; |
|
1479 |
|
1480 if (typ == MatrixType::Permuted_Lower) |
|
1481 { |
|
1482 (*current_liboctave_error_handler) |
6390
|
1483 ("permuted triangular matrix not implemented"); |
5785
|
1484 } |
|
1485 else |
|
1486 { |
|
1487 const double *tmp_data = fortran_vec (); |
|
1488 |
|
1489 if (calc_cond) |
|
1490 { |
|
1491 char norm = '1'; |
|
1492 char uplo = 'L'; |
|
1493 char dia = 'N'; |
|
1494 |
|
1495 Array<double> z (3 * nc); |
|
1496 double *pz = z.fortran_vec (); |
|
1497 Array<octave_idx_type> iz (nc); |
|
1498 octave_idx_type *piz = iz.fortran_vec (); |
|
1499 |
|
1500 F77_XFCN (dtrcon, DTRCON, (F77_CONST_CHAR_ARG2 (&norm, 1), |
|
1501 F77_CONST_CHAR_ARG2 (&uplo, 1), |
|
1502 F77_CONST_CHAR_ARG2 (&dia, 1), |
|
1503 nr, tmp_data, nr, rcond, |
|
1504 pz, piz, info |
|
1505 F77_CHAR_ARG_LEN (1) |
|
1506 F77_CHAR_ARG_LEN (1) |
|
1507 F77_CHAR_ARG_LEN (1))); |
|
1508 |
|
1509 if (f77_exception_encountered) |
|
1510 (*current_liboctave_error_handler) |
|
1511 ("unrecoverable error in dtrcon"); |
|
1512 |
|
1513 if (info != 0) |
|
1514 info = -2; |
|
1515 |
|
1516 volatile double rcond_plus_one = rcond + 1.0; |
|
1517 |
|
1518 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
1519 { |
|
1520 info = -2; |
|
1521 |
|
1522 if (sing_handler) |
|
1523 sing_handler (rcond); |
|
1524 else |
|
1525 (*current_liboctave_error_handler) |
|
1526 ("matrix singular to machine precision, rcond = %g", |
|
1527 rcond); |
|
1528 } |
|
1529 } |
|
1530 |
|
1531 if (info == 0) |
|
1532 { |
|
1533 retval = b; |
|
1534 double *result = retval.fortran_vec (); |
|
1535 |
|
1536 char uplo = 'L'; |
|
1537 char trans = 'N'; |
|
1538 char dia = 'N'; |
|
1539 |
|
1540 F77_XFCN (dtrtrs, DTRTRS, (F77_CONST_CHAR_ARG2 (&uplo, 1), |
|
1541 F77_CONST_CHAR_ARG2 (&trans, 1), |
|
1542 F77_CONST_CHAR_ARG2 (&dia, 1), |
|
1543 nr, b_nc, tmp_data, nr, |
|
1544 result, nr, info |
|
1545 F77_CHAR_ARG_LEN (1) |
|
1546 F77_CHAR_ARG_LEN (1) |
|
1547 F77_CHAR_ARG_LEN (1))); |
|
1548 |
|
1549 if (f77_exception_encountered) |
|
1550 (*current_liboctave_error_handler) |
|
1551 ("unrecoverable error in dtrtrs"); |
|
1552 } |
|
1553 } |
|
1554 } |
|
1555 else |
|
1556 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
1557 } |
|
1558 |
|
1559 return retval; |
|
1560 } |
|
1561 |
|
1562 Matrix |
|
1563 Matrix::fsolve (MatrixType &mattype, const Matrix& b, octave_idx_type& info, |
|
1564 double& rcond, solve_singularity_handler sing_handler, |
|
1565 bool calc_cond) const |
|
1566 { |
|
1567 Matrix retval; |
|
1568 |
|
1569 octave_idx_type nr = rows (); |
|
1570 octave_idx_type nc = cols (); |
|
1571 |
6924
|
1572 if (nr != nc || nr != b.rows ()) |
5785
|
1573 (*current_liboctave_error_handler) |
|
1574 ("matrix dimension mismatch solution of linear equations"); |
6924
|
1575 else if (nr == 0 || b.cols () == 0) |
|
1576 retval = Matrix (nc, b.cols (), 0.0); |
5785
|
1577 else |
|
1578 { |
|
1579 volatile int typ = mattype.type (); |
|
1580 |
|
1581 // Calculate the norm of the matrix, for later use. |
|
1582 double anorm = -1.; |
|
1583 |
|
1584 if (typ == MatrixType::Hermitian) |
|
1585 { |
|
1586 info = 0; |
|
1587 char job = 'L'; |
|
1588 Matrix atmp = *this; |
|
1589 double *tmp_data = atmp.fortran_vec (); |
|
1590 anorm = atmp.abs().sum().row(static_cast<octave_idx_type>(0)).max(); |
|
1591 |
|
1592 F77_XFCN (dpotrf, DPOTRF, (F77_CONST_CHAR_ARG2 (&job, 1), nr, |
|
1593 tmp_data, nr, info |
|
1594 F77_CHAR_ARG_LEN (1))); |
|
1595 |
|
1596 if (f77_exception_encountered) |
|
1597 (*current_liboctave_error_handler) |
|
1598 ("unrecoverable error in dpotrf"); |
|
1599 else |
|
1600 { |
|
1601 // Throw-away extra info LAPACK gives so as to not change output. |
|
1602 rcond = 0.0; |
|
1603 if (info != 0) |
|
1604 { |
|
1605 info = -2; |
|
1606 |
|
1607 mattype.mark_as_unsymmetric (); |
|
1608 typ = MatrixType::Full; |
|
1609 } |
|
1610 else |
|
1611 { |
|
1612 if (calc_cond) |
|
1613 { |
|
1614 Array<double> z (3 * nc); |
|
1615 double *pz = z.fortran_vec (); |
|
1616 Array<octave_idx_type> iz (nc); |
|
1617 octave_idx_type *piz = iz.fortran_vec (); |
|
1618 |
|
1619 F77_XFCN (dpocon, DPOCON, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1620 nr, tmp_data, nr, anorm, |
|
1621 rcond, pz, piz, info |
|
1622 F77_CHAR_ARG_LEN (1))); |
|
1623 |
|
1624 if (f77_exception_encountered) |
|
1625 (*current_liboctave_error_handler) |
|
1626 ("unrecoverable error in dpocon"); |
|
1627 |
|
1628 if (info != 0) |
|
1629 info = -2; |
|
1630 |
|
1631 volatile double rcond_plus_one = rcond + 1.0; |
|
1632 |
|
1633 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
1634 { |
|
1635 info = -2; |
|
1636 |
|
1637 if (sing_handler) |
|
1638 sing_handler (rcond); |
|
1639 else |
|
1640 (*current_liboctave_error_handler) |
|
1641 ("matrix singular to machine precision, rcond = %g", |
|
1642 rcond); |
|
1643 } |
|
1644 } |
|
1645 |
|
1646 if (info == 0) |
|
1647 { |
|
1648 retval = b; |
|
1649 double *result = retval.fortran_vec (); |
|
1650 |
|
1651 octave_idx_type b_nc = b.cols (); |
|
1652 |
|
1653 F77_XFCN (dpotrs, DPOTRS, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1654 nr, b_nc, tmp_data, nr, |
|
1655 result, b.rows(), info |
|
1656 F77_CHAR_ARG_LEN (1))); |
|
1657 |
|
1658 if (f77_exception_encountered) |
|
1659 (*current_liboctave_error_handler) |
|
1660 ("unrecoverable error in dpotrs"); |
|
1661 } |
|
1662 else |
|
1663 { |
|
1664 mattype.mark_as_unsymmetric (); |
|
1665 typ = MatrixType::Full; |
|
1666 } |
|
1667 } |
|
1668 } |
|
1669 } |
|
1670 |
|
1671 if (typ == MatrixType::Full) |
|
1672 { |
|
1673 info = 0; |
|
1674 |
|
1675 Array<octave_idx_type> ipvt (nr); |
|
1676 octave_idx_type *pipvt = ipvt.fortran_vec (); |
|
1677 |
|
1678 Matrix atmp = *this; |
|
1679 double *tmp_data = atmp.fortran_vec (); |
|
1680 if(anorm < 0.) |
|
1681 anorm = atmp.abs().sum().row(static_cast<octave_idx_type>(0)).max(); |
|
1682 |
|
1683 Array<double> z (4 * nc); |
|
1684 double *pz = z.fortran_vec (); |
|
1685 Array<octave_idx_type> iz (nc); |
|
1686 octave_idx_type *piz = iz.fortran_vec (); |
|
1687 |
|
1688 F77_XFCN (dgetrf, DGETRF, (nr, nr, tmp_data, nr, pipvt, info)); |
|
1689 |
|
1690 if (f77_exception_encountered) |
|
1691 (*current_liboctave_error_handler) |
|
1692 ("unrecoverable error in dgetrf"); |
|
1693 else |
|
1694 { |
|
1695 // Throw-away extra info LAPACK gives so as to not change output. |
|
1696 rcond = 0.0; |
|
1697 if (info != 0) |
|
1698 { |
|
1699 info = -2; |
|
1700 |
|
1701 if (sing_handler) |
|
1702 sing_handler (rcond); |
|
1703 else |
|
1704 (*current_liboctave_error_handler) |
|
1705 ("matrix singular to machine precision"); |
|
1706 |
|
1707 mattype.mark_as_rectangular (); |
|
1708 } |
|
1709 else |
|
1710 { |
|
1711 if (calc_cond) |
|
1712 { |
|
1713 // Now calculate the condition number for |
|
1714 // non-singular matrix. |
|
1715 char job = '1'; |
|
1716 F77_XFCN (dgecon, DGECON, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1717 nc, tmp_data, nr, anorm, |
|
1718 rcond, pz, piz, info |
|
1719 F77_CHAR_ARG_LEN (1))); |
|
1720 |
|
1721 if (f77_exception_encountered) |
|
1722 (*current_liboctave_error_handler) |
|
1723 ("unrecoverable error in dgecon"); |
|
1724 |
|
1725 if (info != 0) |
|
1726 info = -2; |
|
1727 |
|
1728 volatile double rcond_plus_one = rcond + 1.0; |
|
1729 |
|
1730 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
1731 { |
|
1732 info = -2; |
|
1733 |
|
1734 if (sing_handler) |
|
1735 sing_handler (rcond); |
|
1736 else |
|
1737 (*current_liboctave_error_handler) |
|
1738 ("matrix singular to machine precision, rcond = %g", |
|
1739 rcond); |
|
1740 } |
|
1741 } |
|
1742 |
|
1743 if (info == 0) |
|
1744 { |
|
1745 retval = b; |
|
1746 double *result = retval.fortran_vec (); |
|
1747 |
|
1748 octave_idx_type b_nc = b.cols (); |
|
1749 |
|
1750 char job = 'N'; |
|
1751 F77_XFCN (dgetrs, DGETRS, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1752 nr, b_nc, tmp_data, nr, |
|
1753 pipvt, result, b.rows(), info |
|
1754 F77_CHAR_ARG_LEN (1))); |
|
1755 |
|
1756 if (f77_exception_encountered) |
|
1757 (*current_liboctave_error_handler) |
|
1758 ("unrecoverable error in dgetrs"); |
|
1759 } |
|
1760 else |
|
1761 mattype.mark_as_rectangular (); |
|
1762 } |
|
1763 } |
|
1764 } |
|
1765 else if (typ != MatrixType::Hermitian) |
|
1766 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
1767 } |
|
1768 |
|
1769 return retval; |
|
1770 } |
|
1771 |
|
1772 Matrix |
|
1773 Matrix::solve (MatrixType &typ, const Matrix& b) const |
|
1774 { |
|
1775 octave_idx_type info; |
|
1776 double rcond; |
|
1777 return solve (typ, b, info, rcond, 0); |
|
1778 } |
|
1779 |
|
1780 Matrix |
|
1781 Matrix::solve (MatrixType &typ, const Matrix& b, octave_idx_type& info, |
|
1782 double& rcond) const |
|
1783 { |
|
1784 return solve (typ, b, info, rcond, 0); |
|
1785 } |
|
1786 |
|
1787 Matrix |
|
1788 Matrix::solve (MatrixType &mattype, const Matrix& b, octave_idx_type& info, |
|
1789 double& rcond, solve_singularity_handler sing_handler, |
|
1790 bool singular_fallback) const |
|
1791 { |
|
1792 Matrix retval; |
|
1793 int typ = mattype.type (); |
|
1794 |
|
1795 if (typ == MatrixType::Unknown) |
|
1796 typ = mattype.type (*this); |
|
1797 |
|
1798 // Only calculate the condition number for LU/Cholesky |
|
1799 if (typ == MatrixType::Upper || typ == MatrixType::Permuted_Upper) |
|
1800 retval = utsolve (mattype, b, info, rcond, sing_handler, false); |
|
1801 else if (typ == MatrixType::Lower || typ == MatrixType::Permuted_Lower) |
|
1802 retval = ltsolve (mattype, b, info, rcond, sing_handler, false); |
|
1803 else if (typ == MatrixType::Full || typ == MatrixType::Hermitian) |
|
1804 retval = fsolve (mattype, b, info, rcond, sing_handler, true); |
|
1805 else if (typ != MatrixType::Rectangular) |
|
1806 { |
|
1807 (*current_liboctave_error_handler) ("unknown matrix type"); |
|
1808 return Matrix (); |
|
1809 } |
|
1810 |
|
1811 // Rectangular or one of the above solvers flags a singular matrix |
|
1812 if (singular_fallback && mattype.type () == MatrixType::Rectangular) |
|
1813 { |
|
1814 octave_idx_type rank; |
|
1815 retval = lssolve (b, info, rank); |
|
1816 } |
|
1817 |
|
1818 return retval; |
|
1819 } |
|
1820 |
|
1821 ComplexMatrix |
|
1822 Matrix::solve (MatrixType &typ, const ComplexMatrix& b) const |
|
1823 { |
|
1824 ComplexMatrix tmp (*this); |
|
1825 return tmp.solve (typ, b); |
|
1826 } |
|
1827 |
|
1828 ComplexMatrix |
|
1829 Matrix::solve (MatrixType &typ, const ComplexMatrix& b, |
|
1830 octave_idx_type& info) const |
|
1831 { |
|
1832 ComplexMatrix tmp (*this); |
|
1833 return tmp.solve (typ, b, info); |
|
1834 } |
|
1835 |
|
1836 ComplexMatrix |
|
1837 Matrix::solve (MatrixType &typ, const ComplexMatrix& b, octave_idx_type& info, |
|
1838 double& rcond) const |
|
1839 { |
|
1840 ComplexMatrix tmp (*this); |
|
1841 return tmp.solve (typ, b, info, rcond); |
|
1842 } |
|
1843 |
|
1844 ComplexMatrix |
|
1845 Matrix::solve (MatrixType &typ, const ComplexMatrix& b, octave_idx_type& info, |
|
1846 double& rcond, solve_singularity_handler sing_handler, |
|
1847 bool singular_fallback) const |
|
1848 { |
|
1849 ComplexMatrix tmp (*this); |
|
1850 return tmp.solve (typ, b, info, rcond, sing_handler, singular_fallback); |
|
1851 } |
|
1852 |
|
1853 ColumnVector |
|
1854 Matrix::solve (MatrixType &typ, const ColumnVector& b) const |
|
1855 { |
|
1856 octave_idx_type info; double rcond; |
|
1857 return solve (typ, b, info, rcond); |
|
1858 } |
|
1859 |
|
1860 ColumnVector |
|
1861 Matrix::solve (MatrixType &typ, const ColumnVector& b, |
|
1862 octave_idx_type& info) const |
|
1863 { |
|
1864 double rcond; |
|
1865 return solve (typ, b, info, rcond); |
|
1866 } |
|
1867 |
|
1868 ColumnVector |
|
1869 Matrix::solve (MatrixType &typ, const ColumnVector& b, octave_idx_type& info, |
|
1870 double& rcond) const |
|
1871 { |
|
1872 return solve (typ, b, info, rcond, 0); |
|
1873 } |
|
1874 |
|
1875 ColumnVector |
|
1876 Matrix::solve (MatrixType &typ, const ColumnVector& b, octave_idx_type& info, |
|
1877 double& rcond, solve_singularity_handler sing_handler) const |
|
1878 { |
|
1879 Matrix tmp (b); |
|
1880 return solve (typ, tmp, info, rcond, sing_handler).column(static_cast<octave_idx_type> (0)); |
|
1881 } |
|
1882 |
|
1883 ComplexColumnVector |
|
1884 Matrix::solve (MatrixType &typ, const ComplexColumnVector& b) const |
|
1885 { |
|
1886 ComplexMatrix tmp (*this); |
|
1887 return tmp.solve (typ, b); |
|
1888 } |
|
1889 |
|
1890 ComplexColumnVector |
|
1891 Matrix::solve (MatrixType &typ, const ComplexColumnVector& b, |
|
1892 octave_idx_type& info) const |
|
1893 { |
|
1894 ComplexMatrix tmp (*this); |
|
1895 return tmp.solve (typ, b, info); |
|
1896 } |
|
1897 |
|
1898 ComplexColumnVector |
|
1899 Matrix::solve (MatrixType &typ, const ComplexColumnVector& b, |
|
1900 octave_idx_type& info, double& rcond) const |
|
1901 { |
|
1902 ComplexMatrix tmp (*this); |
|
1903 return tmp.solve (typ, b, info, rcond); |
|
1904 } |
|
1905 |
|
1906 ComplexColumnVector |
|
1907 Matrix::solve (MatrixType &typ, const ComplexColumnVector& b, |
|
1908 octave_idx_type& info, double& rcond, |
|
1909 solve_singularity_handler sing_handler) const |
|
1910 { |
|
1911 ComplexMatrix tmp (*this); |
|
1912 return tmp.solve(typ, b, info, rcond, sing_handler); |
|
1913 } |
|
1914 |
|
1915 Matrix |
458
|
1916 Matrix::solve (const Matrix& b) const |
|
1917 { |
5275
|
1918 octave_idx_type info; |
458
|
1919 double rcond; |
4329
|
1920 return solve (b, info, rcond, 0); |
458
|
1921 } |
|
1922 |
|
1923 Matrix |
5275
|
1924 Matrix::solve (const Matrix& b, octave_idx_type& info) const |
458
|
1925 { |
|
1926 double rcond; |
4329
|
1927 return solve (b, info, rcond, 0); |
458
|
1928 } |
|
1929 |
|
1930 Matrix |
5275
|
1931 Matrix::solve (const Matrix& b, octave_idx_type& info, double& rcond) const |
458
|
1932 { |
3480
|
1933 return solve (b, info, rcond, 0); |
|
1934 } |
|
1935 |
|
1936 Matrix |
5785
|
1937 Matrix::solve (const Matrix& b, octave_idx_type& info, |
|
1938 double& rcond, solve_singularity_handler sing_handler) const |
3480
|
1939 { |
5785
|
1940 MatrixType mattype (*this); |
|
1941 return solve (mattype, b, info, rcond, sing_handler); |
458
|
1942 } |
|
1943 |
|
1944 ComplexMatrix |
|
1945 Matrix::solve (const ComplexMatrix& b) const |
|
1946 { |
|
1947 ComplexMatrix tmp (*this); |
|
1948 return tmp.solve (b); |
|
1949 } |
|
1950 |
|
1951 ComplexMatrix |
5275
|
1952 Matrix::solve (const ComplexMatrix& b, octave_idx_type& info) const |
458
|
1953 { |
|
1954 ComplexMatrix tmp (*this); |
|
1955 return tmp.solve (b, info); |
|
1956 } |
|
1957 |
|
1958 ComplexMatrix |
5275
|
1959 Matrix::solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond) const |
458
|
1960 { |
|
1961 ComplexMatrix tmp (*this); |
|
1962 return tmp.solve (b, info, rcond); |
|
1963 } |
|
1964 |
3480
|
1965 ComplexMatrix |
5275
|
1966 Matrix::solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond, |
3480
|
1967 solve_singularity_handler sing_handler) const |
|
1968 { |
|
1969 ComplexMatrix tmp (*this); |
|
1970 return tmp.solve (b, info, rcond, sing_handler); |
|
1971 } |
|
1972 |
458
|
1973 ColumnVector |
|
1974 Matrix::solve (const ColumnVector& b) const |
|
1975 { |
5275
|
1976 octave_idx_type info; double rcond; |
458
|
1977 return solve (b, info, rcond); |
|
1978 } |
|
1979 |
|
1980 ColumnVector |
5275
|
1981 Matrix::solve (const ColumnVector& b, octave_idx_type& info) const |
458
|
1982 { |
|
1983 double rcond; |
|
1984 return solve (b, info, rcond); |
|
1985 } |
|
1986 |
|
1987 ColumnVector |
5275
|
1988 Matrix::solve (const ColumnVector& b, octave_idx_type& info, double& rcond) const |
458
|
1989 { |
3480
|
1990 return solve (b, info, rcond, 0); |
|
1991 } |
|
1992 |
|
1993 ColumnVector |
5275
|
1994 Matrix::solve (const ColumnVector& b, octave_idx_type& info, double& rcond, |
3480
|
1995 solve_singularity_handler sing_handler) const |
|
1996 { |
5785
|
1997 MatrixType mattype (*this); |
|
1998 return solve (mattype, b, info, rcond, sing_handler); |
458
|
1999 } |
|
2000 |
|
2001 ComplexColumnVector |
|
2002 Matrix::solve (const ComplexColumnVector& b) const |
|
2003 { |
|
2004 ComplexMatrix tmp (*this); |
|
2005 return tmp.solve (b); |
|
2006 } |
|
2007 |
|
2008 ComplexColumnVector |
5275
|
2009 Matrix::solve (const ComplexColumnVector& b, octave_idx_type& info) const |
458
|
2010 { |
|
2011 ComplexMatrix tmp (*this); |
|
2012 return tmp.solve (b, info); |
|
2013 } |
|
2014 |
|
2015 ComplexColumnVector |
5275
|
2016 Matrix::solve (const ComplexColumnVector& b, octave_idx_type& info, double& rcond) const |
458
|
2017 { |
|
2018 ComplexMatrix tmp (*this); |
|
2019 return tmp.solve (b, info, rcond); |
|
2020 } |
|
2021 |
3480
|
2022 ComplexColumnVector |
5275
|
2023 Matrix::solve (const ComplexColumnVector& b, octave_idx_type& info, double& rcond, |
3480
|
2024 solve_singularity_handler sing_handler) const |
|
2025 { |
|
2026 ComplexMatrix tmp (*this); |
|
2027 return tmp.solve (b, info, rcond, sing_handler); |
|
2028 } |
|
2029 |
458
|
2030 Matrix |
|
2031 Matrix::lssolve (const Matrix& b) const |
|
2032 { |
5275
|
2033 octave_idx_type info; |
|
2034 octave_idx_type rank; |
458
|
2035 return lssolve (b, info, rank); |
|
2036 } |
|
2037 |
|
2038 Matrix |
5275
|
2039 Matrix::lssolve (const Matrix& b, octave_idx_type& info) const |
458
|
2040 { |
5275
|
2041 octave_idx_type rank; |
458
|
2042 return lssolve (b, info, rank); |
|
2043 } |
|
2044 |
|
2045 Matrix |
5275
|
2046 Matrix::lssolve (const Matrix& b, octave_idx_type& info, octave_idx_type& rank) const |
458
|
2047 { |
1948
|
2048 Matrix retval; |
|
2049 |
5275
|
2050 octave_idx_type nrhs = b.cols (); |
|
2051 |
|
2052 octave_idx_type m = rows (); |
|
2053 octave_idx_type n = cols (); |
458
|
2054 |
6924
|
2055 |
|
2056 if (m != b.rows ()) |
1948
|
2057 (*current_liboctave_error_handler) |
6924
|
2058 ("matrix dimension mismatch solution of linear equations"); |
|
2059 else if (m == 0 || n == 0 || b.cols () == 0) |
|
2060 retval = Matrix (n, b.cols (), 0.0); |
1948
|
2061 else |
458
|
2062 { |
1948
|
2063 Matrix atmp = *this; |
|
2064 double *tmp_data = atmp.fortran_vec (); |
|
2065 |
5275
|
2066 octave_idx_type nrr = m > n ? m : n; |
3754
|
2067 Matrix result (nrr, nrhs, 0.0); |
1948
|
2068 |
5275
|
2069 for (octave_idx_type j = 0; j < nrhs; j++) |
|
2070 for (octave_idx_type i = 0; i < m; i++) |
1948
|
2071 result.elem (i, j) = b.elem (i, j); |
|
2072 |
|
2073 double *presult = result.fortran_vec (); |
|
2074 |
6926
|
2075 Array<octave_idx_type> jpvt (n); |
|
2076 octave_idx_type *pjpvt = jpvt.fortran_vec (); |
1948
|
2077 |
|
2078 double rcond = -1.0; |
|
2079 |
6926
|
2080 // Ask DGELSY what the dimension of WORK should be. |
3752
|
2081 |
5275
|
2082 octave_idx_type lwork = -1; |
3752
|
2083 |
|
2084 Array<double> work (1); |
1948
|
2085 |
6926
|
2086 F77_XFCN (dgelsy, DGELSY, (m, n, nrhs, tmp_data, m, presult, nrr, pjpvt, |
3752
|
2087 rcond, rank, work.fortran_vec (), |
|
2088 lwork, info)); |
1948
|
2089 |
|
2090 if (f77_exception_encountered) |
6926
|
2091 (*current_liboctave_error_handler) ("unrecoverable error in dgelsy"); |
1948
|
2092 else |
|
2093 { |
5275
|
2094 lwork = static_cast<octave_idx_type> (work(0)); |
3752
|
2095 work.resize (lwork); |
|
2096 |
6926
|
2097 F77_XFCN (dgelsy, DGELSY, (m, n, nrhs, tmp_data, m, presult, |
|
2098 nrr, pjpvt, rcond, rank, |
3752
|
2099 work.fortran_vec (), lwork, info)); |
|
2100 |
|
2101 if (f77_exception_encountered) |
|
2102 (*current_liboctave_error_handler) |
6926
|
2103 ("unrecoverable error in dgelsy"); |
3752
|
2104 else |
|
2105 { |
|
2106 retval.resize (n, nrhs); |
5275
|
2107 for (octave_idx_type j = 0; j < nrhs; j++) |
|
2108 for (octave_idx_type i = 0; i < n; i++) |
3752
|
2109 retval.elem (i, j) = result.elem (i, j); |
|
2110 } |
1948
|
2111 } |
458
|
2112 } |
|
2113 |
|
2114 return retval; |
|
2115 } |
|
2116 |
|
2117 ComplexMatrix |
|
2118 Matrix::lssolve (const ComplexMatrix& b) const |
|
2119 { |
|
2120 ComplexMatrix tmp (*this); |
5275
|
2121 octave_idx_type info; |
|
2122 octave_idx_type rank; |
1484
|
2123 return tmp.lssolve (b, info, rank); |
458
|
2124 } |
|
2125 |
|
2126 ComplexMatrix |
5275
|
2127 Matrix::lssolve (const ComplexMatrix& b, octave_idx_type& info) const |
458
|
2128 { |
|
2129 ComplexMatrix tmp (*this); |
5275
|
2130 octave_idx_type rank; |
1484
|
2131 return tmp.lssolve (b, info, rank); |
458
|
2132 } |
|
2133 |
|
2134 ComplexMatrix |
5275
|
2135 Matrix::lssolve (const ComplexMatrix& b, octave_idx_type& info, octave_idx_type& rank) const |
458
|
2136 { |
|
2137 ComplexMatrix tmp (*this); |
1484
|
2138 return tmp.lssolve (b, info, rank); |
458
|
2139 } |
|
2140 |
|
2141 ColumnVector |
|
2142 Matrix::lssolve (const ColumnVector& b) const |
|
2143 { |
5275
|
2144 octave_idx_type info; |
|
2145 octave_idx_type rank; |
1484
|
2146 return lssolve (b, info, rank); |
458
|
2147 } |
|
2148 |
|
2149 ColumnVector |
5275
|
2150 Matrix::lssolve (const ColumnVector& b, octave_idx_type& info) const |
458
|
2151 { |
5275
|
2152 octave_idx_type rank; |
458
|
2153 return lssolve (b, info, rank); |
|
2154 } |
|
2155 |
|
2156 ColumnVector |
5275
|
2157 Matrix::lssolve (const ColumnVector& b, octave_idx_type& info, octave_idx_type& rank) const |
458
|
2158 { |
1948
|
2159 ColumnVector retval; |
|
2160 |
5275
|
2161 octave_idx_type nrhs = 1; |
|
2162 |
|
2163 octave_idx_type m = rows (); |
|
2164 octave_idx_type n = cols (); |
458
|
2165 |
6924
|
2166 if (m != b.length ()) |
1948
|
2167 (*current_liboctave_error_handler) |
6924
|
2168 ("matrix dimension mismatch solution of linear equations"); |
|
2169 else if (m == 0 || n == 0) |
|
2170 retval = ColumnVector (n, 0.0); |
1948
|
2171 else |
458
|
2172 { |
1948
|
2173 Matrix atmp = *this; |
|
2174 double *tmp_data = atmp.fortran_vec (); |
|
2175 |
5275
|
2176 octave_idx_type nrr = m > n ? m : n; |
1948
|
2177 ColumnVector result (nrr); |
|
2178 |
5275
|
2179 for (octave_idx_type i = 0; i < m; i++) |
1948
|
2180 result.elem (i) = b.elem (i); |
|
2181 |
|
2182 double *presult = result.fortran_vec (); |
|
2183 |
6926
|
2184 Array<octave_idx_type> jpvt (n); |
|
2185 octave_idx_type *pjpvt = jpvt.fortran_vec (); |
1948
|
2186 |
|
2187 double rcond = -1.0; |
|
2188 |
6926
|
2189 // Ask DGELSY what the dimension of WORK should be. |
3752
|
2190 |
5275
|
2191 octave_idx_type lwork = -1; |
3752
|
2192 |
|
2193 Array<double> work (1); |
|
2194 |
6926
|
2195 F77_XFCN (dgelsy, DGELSY, (m, n, nrhs, tmp_data, m, presult, nrr, pjpvt, |
3752
|
2196 rcond, rank, work.fortran_vec (), |
|
2197 lwork, info)); |
1948
|
2198 |
|
2199 if (f77_exception_encountered) |
6926
|
2200 (*current_liboctave_error_handler) ("unrecoverable error in dgelsy"); |
1948
|
2201 else |
|
2202 { |
5275
|
2203 lwork = static_cast<octave_idx_type> (work(0)); |
3752
|
2204 work.resize (lwork); |
|
2205 |
6926
|
2206 F77_XFCN (dgelsy, DGELSY, (m, n, nrhs, tmp_data, m, presult, |
|
2207 nrr, pjpvt, rcond, rank, |
3752
|
2208 work.fortran_vec (), lwork, info)); |
|
2209 |
|
2210 if (f77_exception_encountered) |
|
2211 (*current_liboctave_error_handler) |
6926
|
2212 ("unrecoverable error in dgelsy"); |
3752
|
2213 else |
|
2214 { |
|
2215 retval.resize (n); |
5275
|
2216 for (octave_idx_type i = 0; i < n; i++) |
3752
|
2217 retval.elem (i) = result.elem (i); |
|
2218 } |
1948
|
2219 } |
458
|
2220 } |
|
2221 |
|
2222 return retval; |
|
2223 } |
|
2224 |
|
2225 ComplexColumnVector |
|
2226 Matrix::lssolve (const ComplexColumnVector& b) const |
|
2227 { |
|
2228 ComplexMatrix tmp (*this); |
|
2229 return tmp.lssolve (b); |
|
2230 } |
|
2231 |
|
2232 ComplexColumnVector |
5275
|
2233 Matrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info) const |
458
|
2234 { |
|
2235 ComplexMatrix tmp (*this); |
|
2236 return tmp.lssolve (b, info); |
|
2237 } |
|
2238 |
|
2239 ComplexColumnVector |
5275
|
2240 Matrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info, octave_idx_type& rank) const |
458
|
2241 { |
|
2242 ComplexMatrix tmp (*this); |
|
2243 return tmp.lssolve (b, info, rank); |
|
2244 } |
|
2245 |
1819
|
2246 // Constants for matrix exponential calculation. |
|
2247 |
|
2248 static double padec [] = |
|
2249 { |
|
2250 5.0000000000000000e-1, |
|
2251 1.1666666666666667e-1, |
|
2252 1.6666666666666667e-2, |
|
2253 1.6025641025641026e-3, |
|
2254 1.0683760683760684e-4, |
|
2255 4.8562548562548563e-6, |
|
2256 1.3875013875013875e-7, |
|
2257 1.9270852604185938e-9, |
|
2258 }; |
|
2259 |
|
2260 Matrix |
|
2261 Matrix::expm (void) const |
|
2262 { |
|
2263 Matrix retval; |
|
2264 |
|
2265 Matrix m = *this; |
|
2266 |
6699
|
2267 if (numel () == 1) |
|
2268 return Matrix (1, 1, exp (m(0))); |
|
2269 |
5275
|
2270 octave_idx_type nc = columns (); |
1819
|
2271 |
3130
|
2272 // Preconditioning step 1: trace normalization to reduce dynamic |
|
2273 // range of poles, but avoid making stable eigenvalues unstable. |
|
2274 |
1819
|
2275 // trace shift value |
3331
|
2276 volatile double trshift = 0.0; |
1819
|
2277 |
5275
|
2278 for (octave_idx_type i = 0; i < nc; i++) |
1819
|
2279 trshift += m.elem (i, i); |
|
2280 |
|
2281 trshift /= nc; |
|
2282 |
3130
|
2283 if (trshift > 0.0) |
|
2284 { |
5275
|
2285 for (octave_idx_type i = 0; i < nc; i++) |
3130
|
2286 m.elem (i, i) -= trshift; |
|
2287 } |
1819
|
2288 |
3331
|
2289 // Preconditioning step 2: balancing; code follows development |
|
2290 // in AEPBAL |
|
2291 |
|
2292 double *p_m = m.fortran_vec (); |
|
2293 |
5275
|
2294 octave_idx_type info, ilo, ihi, ilos, ihis; |
3468
|
2295 Array<double> dpermute (nc); |
|
2296 Array<double> dscale (nc); |
3466
|
2297 |
3468
|
2298 // permutation first |
|
2299 char job = 'P'; |
4552
|
2300 F77_XFCN (dgebal, DGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
2301 nc, p_m, nc, ilo, ihi, |
|
2302 dpermute.fortran_vec (), info |
|
2303 F77_CHAR_ARG_LEN (1))); |
3466
|
2304 |
3468
|
2305 // then scaling |
|
2306 job = 'S'; |
4552
|
2307 F77_XFCN (dgebal, DGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
2308 nc, p_m, nc, ilos, ihis, |
|
2309 dscale.fortran_vec (), info |
|
2310 F77_CHAR_ARG_LEN (1))); |
3331
|
2311 |
|
2312 if (f77_exception_encountered) |
|
2313 { |
|
2314 (*current_liboctave_error_handler) ("unrecoverable error in dgebal"); |
|
2315 return retval; |
|
2316 } |
|
2317 |
1819
|
2318 // Preconditioning step 3: scaling. |
3331
|
2319 |
1819
|
2320 ColumnVector work(nc); |
3130
|
2321 double inf_norm; |
3331
|
2322 |
4552
|
2323 F77_XFCN (xdlange, XDLANGE, (F77_CONST_CHAR_ARG2 ("I", 1), |
|
2324 nc, nc, m.fortran_vec (), nc, |
|
2325 work.fortran_vec (), inf_norm |
|
2326 F77_CHAR_ARG_LEN (1))); |
3331
|
2327 |
|
2328 if (f77_exception_encountered) |
|
2329 { |
|
2330 (*current_liboctave_error_handler) ("unrecoverable error in dlange"); |
|
2331 return retval; |
|
2332 } |
1819
|
2333 |
5275
|
2334 octave_idx_type sqpow = static_cast<octave_idx_type> (inf_norm > 0.0 |
1819
|
2335 ? (1.0 + log (inf_norm) / log (2.0)) |
|
2336 : 0.0); |
3331
|
2337 |
1819
|
2338 // Check whether we need to square at all. |
3331
|
2339 |
1819
|
2340 if (sqpow < 0) |
|
2341 sqpow = 0; |
3331
|
2342 |
1819
|
2343 if (sqpow > 0) |
|
2344 { |
|
2345 double scale_factor = 1.0; |
5275
|
2346 for (octave_idx_type i = 0; i < sqpow; i++) |
1819
|
2347 scale_factor *= 2.0; |
3331
|
2348 |
1819
|
2349 m = m / scale_factor; |
|
2350 } |
3331
|
2351 |
1819
|
2352 // npp, dpp: pade' approx polynomial matrices. |
3331
|
2353 |
1819
|
2354 Matrix npp (nc, nc, 0.0); |
|
2355 Matrix dpp = npp; |
3331
|
2356 |
1819
|
2357 // Now powers a^8 ... a^1. |
3331
|
2358 |
5275
|
2359 octave_idx_type minus_one_j = -1; |
|
2360 for (octave_idx_type j = 7; j >= 0; j--) |
1819
|
2361 { |
3573
|
2362 npp = m * npp + padec[j] * m; |
|
2363 dpp = m * dpp + (minus_one_j * padec[j]) * m; |
1819
|
2364 minus_one_j *= -1; |
|
2365 } |
3331
|
2366 |
1819
|
2367 // Zero power. |
3331
|
2368 |
1819
|
2369 dpp = -dpp; |
5275
|
2370 for (octave_idx_type j = 0; j < nc; j++) |
1819
|
2371 { |
|
2372 npp.elem (j, j) += 1.0; |
|
2373 dpp.elem (j, j) += 1.0; |
|
2374 } |
3331
|
2375 |
1819
|
2376 // Compute pade approximation = inverse (dpp) * npp. |
|
2377 |
3331
|
2378 retval = dpp.solve (npp, info); |
|
2379 |
1819
|
2380 // Reverse preconditioning step 3: repeated squaring. |
3331
|
2381 |
1819
|
2382 while (sqpow) |
|
2383 { |
|
2384 retval = retval * retval; |
|
2385 sqpow--; |
|
2386 } |
3331
|
2387 |
1819
|
2388 // Reverse preconditioning step 2: inverse balancing. |
3466
|
2389 // apply inverse scaling to computed exponential |
5275
|
2390 for (octave_idx_type i = 0; i < nc; i++) |
|
2391 for (octave_idx_type j = 0; j < nc; j++) |
3468
|
2392 retval(i,j) *= dscale(i) / dscale(j); |
3466
|
2393 |
4153
|
2394 OCTAVE_QUIT; |
|
2395 |
3466
|
2396 // construct balancing permutation vector |
5275
|
2397 Array<octave_idx_type> iperm (nc); |
|
2398 for (octave_idx_type i = 0; i < nc; i++) |
4593
|
2399 iperm(i) = i; // identity permutation |
3466
|
2400 |
|
2401 // leading permutations in forward order |
5275
|
2402 for (octave_idx_type i = 0; i < (ilo-1); i++) |
3468
|
2403 { |
5275
|
2404 octave_idx_type swapidx = static_cast<octave_idx_type> (dpermute(i)) - 1; |
|
2405 octave_idx_type tmp = iperm(i); |
4593
|
2406 iperm(i) = iperm (swapidx); |
|
2407 iperm(swapidx) = tmp; |
3468
|
2408 } |
3466
|
2409 |
|
2410 // trailing permutations must be done in reverse order |
5275
|
2411 for (octave_idx_type i = nc - 1; i >= ihi; i--) |
3468
|
2412 { |
5275
|
2413 octave_idx_type swapidx = static_cast<octave_idx_type> (dpermute(i)) - 1; |
|
2414 octave_idx_type tmp = iperm(i); |
4593
|
2415 iperm(i) = iperm(swapidx); |
|
2416 iperm(swapidx) = tmp; |
3468
|
2417 } |
3466
|
2418 |
|
2419 // construct inverse balancing permutation vector |
5275
|
2420 Array<octave_idx_type> invpvec (nc); |
|
2421 for (octave_idx_type i = 0; i < nc; i++) |
4593
|
2422 invpvec(iperm(i)) = i; // Thanks to R. A. Lippert for this method |
4153
|
2423 |
|
2424 OCTAVE_QUIT; |
3466
|
2425 |
|
2426 Matrix tmpMat = retval; |
5275
|
2427 for (octave_idx_type i = 0; i < nc; i++) |
|
2428 for (octave_idx_type j = 0; j < nc; j++) |
3468
|
2429 retval(i,j) = tmpMat(invpvec(i),invpvec(j)); |
3466
|
2430 |
1819
|
2431 // Reverse preconditioning step 1: fix trace normalization. |
3331
|
2432 |
3130
|
2433 if (trshift > 0.0) |
|
2434 retval = exp (trshift) * retval; |
|
2435 |
|
2436 return retval; |
1819
|
2437 } |
|
2438 |
458
|
2439 Matrix& |
|
2440 Matrix::operator += (const DiagMatrix& a) |
|
2441 { |
5275
|
2442 octave_idx_type nr = rows (); |
|
2443 octave_idx_type nc = cols (); |
|
2444 |
|
2445 octave_idx_type a_nr = a.rows (); |
|
2446 octave_idx_type a_nc = a.cols (); |
2385
|
2447 |
|
2448 if (nr != a_nr || nc != a_nc) |
458
|
2449 { |
2385
|
2450 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458
|
2451 return *this; |
|
2452 } |
|
2453 |
5275
|
2454 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
2455 elem (i, i) += a.elem (i, i); |
|
2456 |
|
2457 return *this; |
|
2458 } |
|
2459 |
|
2460 Matrix& |
|
2461 Matrix::operator -= (const DiagMatrix& a) |
|
2462 { |
5275
|
2463 octave_idx_type nr = rows (); |
|
2464 octave_idx_type nc = cols (); |
|
2465 |
|
2466 octave_idx_type a_nr = a.rows (); |
|
2467 octave_idx_type a_nc = a.cols (); |
2385
|
2468 |
|
2469 if (nr != a_nr || nc != a_nc) |
458
|
2470 { |
2385
|
2471 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458
|
2472 return *this; |
|
2473 } |
|
2474 |
5275
|
2475 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
2476 elem (i, i) -= a.elem (i, i); |
|
2477 |
|
2478 return *this; |
|
2479 } |
|
2480 |
|
2481 // unary operations |
|
2482 |
2964
|
2483 boolMatrix |
458
|
2484 Matrix::operator ! (void) const |
|
2485 { |
5275
|
2486 octave_idx_type nr = rows (); |
|
2487 octave_idx_type nc = cols (); |
458
|
2488 |
2964
|
2489 boolMatrix b (nr, nc); |
458
|
2490 |
5275
|
2491 for (octave_idx_type j = 0; j < nc; j++) |
|
2492 for (octave_idx_type i = 0; i < nr; i++) |
458
|
2493 b.elem (i, j) = ! elem (i, j); |
|
2494 |
|
2495 return b; |
|
2496 } |
|
2497 |
1205
|
2498 // column vector by row vector -> matrix operations |
458
|
2499 |
1205
|
2500 Matrix |
|
2501 operator * (const ColumnVector& v, const RowVector& a) |
458
|
2502 { |
1948
|
2503 Matrix retval; |
|
2504 |
5275
|
2505 octave_idx_type len = v.length (); |
3233
|
2506 |
|
2507 if (len != 0) |
1205
|
2508 { |
5275
|
2509 octave_idx_type a_len = a.length (); |
3233
|
2510 |
|
2511 retval.resize (len, a_len); |
|
2512 double *c = retval.fortran_vec (); |
|
2513 |
4552
|
2514 F77_XFCN (dgemm, DGEMM, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
2515 F77_CONST_CHAR_ARG2 ("N", 1), |
|
2516 len, a_len, 1, 1.0, v.data (), len, |
|
2517 a.data (), 1, 0.0, c, len |
|
2518 F77_CHAR_ARG_LEN (1) |
|
2519 F77_CHAR_ARG_LEN (1))); |
3233
|
2520 |
|
2521 if (f77_exception_encountered) |
|
2522 (*current_liboctave_error_handler) |
|
2523 ("unrecoverable error in dgemm"); |
1205
|
2524 } |
458
|
2525 |
1948
|
2526 return retval; |
458
|
2527 } |
|
2528 |
|
2529 // other operations. |
|
2530 |
|
2531 Matrix |
2676
|
2532 Matrix::map (d_d_Mapper f) const |
1205
|
2533 { |
2676
|
2534 Matrix b (*this); |
|
2535 return b.apply (f); |
1205
|
2536 } |
|
2537 |
3248
|
2538 boolMatrix |
|
2539 Matrix::map (b_d_Mapper f) const |
|
2540 { |
5275
|
2541 octave_idx_type nr = rows (); |
|
2542 octave_idx_type nc = cols (); |
3248
|
2543 |
|
2544 boolMatrix retval (nr, nc); |
|
2545 |
5275
|
2546 for (octave_idx_type j = 0; j < nc; j++) |
|
2547 for (octave_idx_type i = 0; i < nr; i++) |
3248
|
2548 retval(i,j) = f (elem(i,j)); |
|
2549 |
|
2550 return retval; |
|
2551 } |
|
2552 |
2676
|
2553 Matrix& |
|
2554 Matrix::apply (d_d_Mapper f) |
458
|
2555 { |
|
2556 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2557 |
5275
|
2558 for (octave_idx_type i = 0; i < length (); i++) |
458
|
2559 d[i] = f (d[i]); |
2676
|
2560 |
|
2561 return *this; |
458
|
2562 } |
|
2563 |
2385
|
2564 bool |
4431
|
2565 Matrix::any_element_is_negative (bool neg_zero) const |
2385
|
2566 { |
5275
|
2567 octave_idx_type nel = nelem (); |
2385
|
2568 |
4431
|
2569 if (neg_zero) |
|
2570 { |
5275
|
2571 for (octave_idx_type i = 0; i < nel; i++) |
4634
|
2572 if (lo_ieee_signbit (elem (i))) |
|
2573 return true; |
4431
|
2574 } |
|
2575 else |
|
2576 { |
5275
|
2577 for (octave_idx_type i = 0; i < nel; i++) |
4634
|
2578 if (elem (i) < 0) |
|
2579 return true; |
4431
|
2580 } |
2385
|
2581 |
|
2582 return false; |
|
2583 } |
|
2584 |
|
2585 |
|
2586 bool |
|
2587 Matrix::any_element_is_inf_or_nan (void) const |
|
2588 { |
5275
|
2589 octave_idx_type nel = nelem (); |
|
2590 |
|
2591 for (octave_idx_type i = 0; i < nel; i++) |
4634
|
2592 { |
|
2593 double val = elem (i); |
|
2594 if (xisinf (val) || xisnan (val)) |
|
2595 return true; |
|
2596 } |
|
2597 |
|
2598 return false; |
2385
|
2599 } |
|
2600 |
|
2601 bool |
5943
|
2602 Matrix::any_element_not_one_or_zero (void) const |
|
2603 { |
|
2604 octave_idx_type nel = nelem (); |
|
2605 |
|
2606 for (octave_idx_type i = 0; i < nel; i++) |
|
2607 { |
|
2608 double val = elem (i); |
|
2609 if (val != 0 && val != 1) |
|
2610 return true; |
|
2611 } |
|
2612 |
|
2613 return false; |
|
2614 } |
|
2615 |
|
2616 bool |
2385
|
2617 Matrix::all_elements_are_int_or_inf_or_nan (void) const |
|
2618 { |
5275
|
2619 octave_idx_type nel = nelem (); |
|
2620 |
|
2621 for (octave_idx_type i = 0; i < nel; i++) |
4634
|
2622 { |
|
2623 double val = elem (i); |
|
2624 if (xisnan (val) || D_NINT (val) == val) |
|
2625 continue; |
|
2626 else |
|
2627 return false; |
|
2628 } |
2385
|
2629 |
|
2630 return true; |
|
2631 } |
|
2632 |
1968
|
2633 // Return nonzero if any element of M is not an integer. Also extract |
|
2634 // the largest and smallest values and return them in MAX_VAL and MIN_VAL. |
|
2635 |
2385
|
2636 bool |
1968
|
2637 Matrix::all_integers (double& max_val, double& min_val) const |
|
2638 { |
5275
|
2639 octave_idx_type nel = nelem (); |
4634
|
2640 |
|
2641 if (nel > 0) |
1968
|
2642 { |
4634
|
2643 max_val = elem (0); |
|
2644 min_val = elem (0); |
1968
|
2645 } |
|
2646 else |
2385
|
2647 return false; |
1968
|
2648 |
5275
|
2649 for (octave_idx_type i = 0; i < nel; i++) |
4634
|
2650 { |
|
2651 double val = elem (i); |
|
2652 |
|
2653 if (val > max_val) |
|
2654 max_val = val; |
|
2655 |
|
2656 if (val < min_val) |
|
2657 min_val = val; |
|
2658 |
|
2659 if (D_NINT (val) != val) |
|
2660 return false; |
|
2661 } |
2385
|
2662 |
|
2663 return true; |
1968
|
2664 } |
|
2665 |
2385
|
2666 bool |
1968
|
2667 Matrix::too_large_for_float (void) const |
|
2668 { |
5275
|
2669 octave_idx_type nel = nelem (); |
|
2670 |
|
2671 for (octave_idx_type i = 0; i < nel; i++) |
4634
|
2672 { |
|
2673 double val = elem (i); |
|
2674 |
5389
|
2675 if (! (xisnan (val) || xisinf (val)) |
5387
|
2676 && fabs (val) > FLT_MAX) |
4634
|
2677 return true; |
|
2678 } |
1968
|
2679 |
2385
|
2680 return false; |
1968
|
2681 } |
|
2682 |
5775
|
2683 // FIXME Do these really belong here? Maybe they should be |
4015
|
2684 // in a base class? |
458
|
2685 |
2832
|
2686 boolMatrix |
4015
|
2687 Matrix::all (int dim) const |
458
|
2688 { |
4015
|
2689 MX_ALL_OP (dim); |
458
|
2690 } |
|
2691 |
2832
|
2692 boolMatrix |
4015
|
2693 Matrix::any (int dim) const |
458
|
2694 { |
4015
|
2695 MX_ANY_OP (dim); |
458
|
2696 } |
|
2697 |
|
2698 Matrix |
3723
|
2699 Matrix::cumprod (int dim) const |
458
|
2700 { |
4015
|
2701 MX_CUMULATIVE_OP (Matrix, double, *=); |
458
|
2702 } |
|
2703 |
|
2704 Matrix |
3723
|
2705 Matrix::cumsum (int dim) const |
458
|
2706 { |
4015
|
2707 MX_CUMULATIVE_OP (Matrix, double, +=); |
458
|
2708 } |
|
2709 |
|
2710 Matrix |
3723
|
2711 Matrix::prod (int dim) const |
458
|
2712 { |
3864
|
2713 MX_REDUCTION_OP (Matrix, *=, 1.0, 1.0); |
458
|
2714 } |
|
2715 |
|
2716 Matrix |
3723
|
2717 Matrix::sum (int dim) const |
458
|
2718 { |
3864
|
2719 MX_REDUCTION_OP (Matrix, +=, 0.0, 0.0); |
458
|
2720 } |
|
2721 |
|
2722 Matrix |
3723
|
2723 Matrix::sumsq (int dim) const |
458
|
2724 { |
3864
|
2725 #define ROW_EXPR \ |
|
2726 double d = elem (i, j); \ |
|
2727 retval.elem (i, 0) += d * d |
|
2728 |
|
2729 #define COL_EXPR \ |
|
2730 double d = elem (i, j); \ |
|
2731 retval.elem (0, j) += d * d |
|
2732 |
|
2733 MX_BASE_REDUCTION_OP (Matrix, ROW_EXPR, COL_EXPR, 0.0, 0.0); |
|
2734 |
|
2735 #undef ROW_EXPR |
|
2736 #undef COL_EXPR |
458
|
2737 } |
|
2738 |
2385
|
2739 Matrix |
|
2740 Matrix::abs (void) const |
|
2741 { |
5275
|
2742 octave_idx_type nr = rows (); |
|
2743 octave_idx_type nc = cols (); |
2385
|
2744 |
|
2745 Matrix retval (nr, nc); |
|
2746 |
5275
|
2747 for (octave_idx_type j = 0; j < nc; j++) |
|
2748 for (octave_idx_type i = 0; i < nr; i++) |
2385
|
2749 retval (i, j) = fabs (elem (i, j)); |
|
2750 |
|
2751 return retval; |
|
2752 } |
|
2753 |
458
|
2754 ColumnVector |
|
2755 Matrix::diag (void) const |
|
2756 { |
|
2757 return diag (0); |
|
2758 } |
|
2759 |
|
2760 ColumnVector |
5275
|
2761 Matrix::diag (octave_idx_type k) const |
458
|
2762 { |
5275
|
2763 octave_idx_type nnr = rows (); |
|
2764 octave_idx_type nnc = cols (); |
458
|
2765 if (k > 0) |
|
2766 nnc -= k; |
|
2767 else if (k < 0) |
|
2768 nnr += k; |
|
2769 |
|
2770 ColumnVector d; |
|
2771 |
|
2772 if (nnr > 0 && nnc > 0) |
|
2773 { |
5275
|
2774 octave_idx_type ndiag = (nnr < nnc) ? nnr : nnc; |
458
|
2775 |
|
2776 d.resize (ndiag); |
|
2777 |
|
2778 if (k > 0) |
|
2779 { |
5275
|
2780 for (octave_idx_type i = 0; i < ndiag; i++) |
458
|
2781 d.elem (i) = elem (i, i+k); |
|
2782 } |
4509
|
2783 else if (k < 0) |
458
|
2784 { |
5275
|
2785 for (octave_idx_type i = 0; i < ndiag; i++) |
458
|
2786 d.elem (i) = elem (i-k, i); |
|
2787 } |
|
2788 else |
|
2789 { |
5275
|
2790 for (octave_idx_type i = 0; i < ndiag; i++) |
458
|
2791 d.elem (i) = elem (i, i); |
|
2792 } |
|
2793 } |
|
2794 else |
4513
|
2795 (*current_liboctave_error_handler) |
|
2796 ("diag: requested diagonal out of range"); |
458
|
2797 |
|
2798 return d; |
|
2799 } |
|
2800 |
|
2801 ColumnVector |
|
2802 Matrix::row_min (void) const |
|
2803 { |
5275
|
2804 Array<octave_idx_type> dummy_idx; |
4587
|
2805 return row_min (dummy_idx); |
458
|
2806 } |
|
2807 |
|
2808 ColumnVector |
5275
|
2809 Matrix::row_min (Array<octave_idx_type>& idx_arg) const |
458
|
2810 { |
|
2811 ColumnVector result; |
|
2812 |
5275
|
2813 octave_idx_type nr = rows (); |
|
2814 octave_idx_type nc = cols (); |
458
|
2815 |
|
2816 if (nr > 0 && nc > 0) |
|
2817 { |
|
2818 result.resize (nr); |
4587
|
2819 idx_arg.resize (nr); |
458
|
2820 |
5275
|
2821 for (octave_idx_type i = 0; i < nr; i++) |
458
|
2822 { |
5275
|
2823 octave_idx_type idx_j; |
4469
|
2824 |
|
2825 double tmp_min = octave_NaN; |
|
2826 |
|
2827 for (idx_j = 0; idx_j < nc; idx_j++) |
2354
|
2828 { |
4469
|
2829 tmp_min = elem (i, idx_j); |
|
2830 |
5389
|
2831 if (! xisnan (tmp_min)) |
4469
|
2832 break; |
|
2833 } |
|
2834 |
5275
|
2835 for (octave_idx_type j = idx_j+1; j < nc; j++) |
4469
|
2836 { |
|
2837 double tmp = elem (i, j); |
|
2838 |
5389
|
2839 if (xisnan (tmp)) |
4469
|
2840 continue; |
|
2841 else if (tmp < tmp_min) |
2354
|
2842 { |
4469
|
2843 idx_j = j; |
|
2844 tmp_min = tmp; |
2354
|
2845 } |
|
2846 } |
|
2847 |
4469
|
2848 result.elem (i) = tmp_min; |
5389
|
2849 idx_arg.elem (i) = xisnan (tmp_min) ? 0 : idx_j; |
458
|
2850 } |
|
2851 } |
|
2852 |
|
2853 return result; |
|
2854 } |
|
2855 |
|
2856 ColumnVector |
|
2857 Matrix::row_max (void) const |
|
2858 { |
5275
|
2859 Array<octave_idx_type> dummy_idx; |
4587
|
2860 return row_max (dummy_idx); |
458
|
2861 } |
|
2862 |
|
2863 ColumnVector |
5275
|
2864 Matrix::row_max (Array<octave_idx_type>& idx_arg) const |
458
|
2865 { |
|
2866 ColumnVector result; |
|
2867 |
5275
|
2868 octave_idx_type nr = rows (); |
|
2869 octave_idx_type nc = cols (); |
458
|
2870 |
|
2871 if (nr > 0 && nc > 0) |
|
2872 { |
|
2873 result.resize (nr); |
4587
|
2874 idx_arg.resize (nr); |
458
|
2875 |
5275
|
2876 for (octave_idx_type i = 0; i < nr; i++) |
458
|
2877 { |
5275
|
2878 octave_idx_type idx_j; |
4469
|
2879 |
|
2880 double tmp_max = octave_NaN; |
|
2881 |
|
2882 for (idx_j = 0; idx_j < nc; idx_j++) |
2354
|
2883 { |
4469
|
2884 tmp_max = elem (i, idx_j); |
|
2885 |
5389
|
2886 if (! xisnan (tmp_max)) |
4469
|
2887 break; |
|
2888 } |
|
2889 |
5275
|
2890 for (octave_idx_type j = idx_j+1; j < nc; j++) |
4469
|
2891 { |
|
2892 double tmp = elem (i, j); |
|
2893 |
5389
|
2894 if (xisnan (tmp)) |
4469
|
2895 continue; |
|
2896 else if (tmp > tmp_max) |
2354
|
2897 { |
4469
|
2898 idx_j = j; |
|
2899 tmp_max = tmp; |
2354
|
2900 } |
|
2901 } |
|
2902 |
4469
|
2903 result.elem (i) = tmp_max; |
5389
|
2904 idx_arg.elem (i) = xisnan (tmp_max) ? 0 : idx_j; |
458
|
2905 } |
|
2906 } |
|
2907 |
|
2908 return result; |
|
2909 } |
|
2910 |
|
2911 RowVector |
|
2912 Matrix::column_min (void) const |
|
2913 { |
5275
|
2914 Array<octave_idx_type> dummy_idx; |
4587
|
2915 return column_min (dummy_idx); |
458
|
2916 } |
2354
|
2917 |
458
|
2918 RowVector |
5275
|
2919 Matrix::column_min (Array<octave_idx_type>& idx_arg) const |
458
|
2920 { |
|
2921 RowVector result; |
|
2922 |
5275
|
2923 octave_idx_type nr = rows (); |
|
2924 octave_idx_type nc = cols (); |
458
|
2925 |
|
2926 if (nr > 0 && nc > 0) |
|
2927 { |
|
2928 result.resize (nc); |
4587
|
2929 idx_arg.resize (nc); |
458
|
2930 |
5275
|
2931 for (octave_idx_type j = 0; j < nc; j++) |
458
|
2932 { |
5275
|
2933 octave_idx_type idx_i; |
4469
|
2934 |
|
2935 double tmp_min = octave_NaN; |
|
2936 |
|
2937 for (idx_i = 0; idx_i < nr; idx_i++) |
2354
|
2938 { |
4469
|
2939 tmp_min = elem (idx_i, j); |
|
2940 |
5389
|
2941 if (! xisnan (tmp_min)) |
4469
|
2942 break; |
|
2943 } |
|
2944 |
5275
|
2945 for (octave_idx_type i = idx_i+1; i < nr; i++) |
4469
|
2946 { |
|
2947 double tmp = elem (i, j); |
|
2948 |
5389
|
2949 if (xisnan (tmp)) |
4469
|
2950 continue; |
|
2951 else if (tmp < tmp_min) |
2354
|
2952 { |
4469
|
2953 idx_i = i; |
|
2954 tmp_min = tmp; |
2354
|
2955 } |
|
2956 } |
|
2957 |
4469
|
2958 result.elem (j) = tmp_min; |
5389
|
2959 idx_arg.elem (j) = xisnan (tmp_min) ? 0 : idx_i; |
458
|
2960 } |
|
2961 } |
|
2962 |
|
2963 return result; |
|
2964 } |
|
2965 |
2354
|
2966 RowVector |
|
2967 Matrix::column_max (void) const |
|
2968 { |
5275
|
2969 Array<octave_idx_type> dummy_idx; |
4587
|
2970 return column_max (dummy_idx); |
2354
|
2971 } |
458
|
2972 |
|
2973 RowVector |
5275
|
2974 Matrix::column_max (Array<octave_idx_type>& idx_arg) const |
458
|
2975 { |
|
2976 RowVector result; |
|
2977 |
5275
|
2978 octave_idx_type nr = rows (); |
|
2979 octave_idx_type nc = cols (); |
458
|
2980 |
|
2981 if (nr > 0 && nc > 0) |
|
2982 { |
|
2983 result.resize (nc); |
4587
|
2984 idx_arg.resize (nc); |
458
|
2985 |
5275
|
2986 for (octave_idx_type j = 0; j < nc; j++) |
458
|
2987 { |
5275
|
2988 octave_idx_type idx_i; |
4469
|
2989 |
|
2990 double tmp_max = octave_NaN; |
|
2991 |
|
2992 for (idx_i = 0; idx_i < nr; idx_i++) |
2354
|
2993 { |
4469
|
2994 tmp_max = elem (idx_i, j); |
|
2995 |
5389
|
2996 if (! xisnan (tmp_max)) |
4469
|
2997 break; |
|
2998 } |
|
2999 |
5275
|
3000 for (octave_idx_type i = idx_i+1; i < nr; i++) |
4469
|
3001 { |
|
3002 double tmp = elem (i, j); |
|
3003 |
5389
|
3004 if (xisnan (tmp)) |
4469
|
3005 continue; |
|
3006 else if (tmp > tmp_max) |
2354
|
3007 { |
4469
|
3008 idx_i = i; |
|
3009 tmp_max = tmp; |
2354
|
3010 } |
|
3011 } |
|
3012 |
4469
|
3013 result.elem (j) = tmp_max; |
5389
|
3014 idx_arg.elem (j) = xisnan (tmp_max) ? 0 : idx_i; |
458
|
3015 } |
|
3016 } |
|
3017 |
|
3018 return result; |
|
3019 } |
|
3020 |
3504
|
3021 std::ostream& |
|
3022 operator << (std::ostream& os, const Matrix& a) |
458
|
3023 { |
5275
|
3024 for (octave_idx_type i = 0; i < a.rows (); i++) |
458
|
3025 { |
5275
|
3026 for (octave_idx_type j = 0; j < a.cols (); j++) |
4130
|
3027 { |
|
3028 os << " "; |
|
3029 octave_write_double (os, a.elem (i, j)); |
|
3030 } |
458
|
3031 os << "\n"; |
|
3032 } |
|
3033 return os; |
|
3034 } |
|
3035 |
3504
|
3036 std::istream& |
|
3037 operator >> (std::istream& is, Matrix& a) |
458
|
3038 { |
5275
|
3039 octave_idx_type nr = a.rows (); |
|
3040 octave_idx_type nc = a.cols (); |
458
|
3041 |
|
3042 if (nr < 1 || nc < 1) |
3504
|
3043 is.clear (std::ios::badbit); |
458
|
3044 else |
|
3045 { |
|
3046 double tmp; |
5275
|
3047 for (octave_idx_type i = 0; i < nr; i++) |
|
3048 for (octave_idx_type j = 0; j < nc; j++) |
458
|
3049 { |
4130
|
3050 tmp = octave_read_double (is); |
458
|
3051 if (is) |
|
3052 a.elem (i, j) = tmp; |
|
3053 else |
2795
|
3054 goto done; |
458
|
3055 } |
|
3056 } |
|
3057 |
2795
|
3058 done: |
|
3059 |
458
|
3060 return is; |
|
3061 } |
|
3062 |
1819
|
3063 Matrix |
|
3064 Givens (double x, double y) |
|
3065 { |
|
3066 double cc, s, temp_r; |
|
3067 |
3887
|
3068 F77_FUNC (dlartg, DLARTG) (x, y, cc, s, temp_r); |
1819
|
3069 |
|
3070 Matrix g (2, 2); |
|
3071 |
|
3072 g.elem (0, 0) = cc; |
|
3073 g.elem (1, 1) = cc; |
|
3074 g.elem (0, 1) = s; |
|
3075 g.elem (1, 0) = -s; |
|
3076 |
|
3077 return g; |
|
3078 } |
|
3079 |
|
3080 Matrix |
|
3081 Sylvester (const Matrix& a, const Matrix& b, const Matrix& c) |
|
3082 { |
|
3083 Matrix retval; |
|
3084 |
5775
|
3085 // FIXME -- need to check that a, b, and c are all the same |
1819
|
3086 // size. |
|
3087 |
|
3088 // Compute Schur decompositions. |
|
3089 |
|
3090 SCHUR as (a, "U"); |
|
3091 SCHUR bs (b, "U"); |
|
3092 |
|
3093 // Transform c to new coordinates. |
|
3094 |
|
3095 Matrix ua = as.unitary_matrix (); |
|
3096 Matrix sch_a = as.schur_matrix (); |
|
3097 |
|
3098 Matrix ub = bs.unitary_matrix (); |
|
3099 Matrix sch_b = bs.schur_matrix (); |
|
3100 |
|
3101 Matrix cx = ua.transpose () * c * ub; |
|
3102 |
|
3103 // Solve the sylvester equation, back-transform, and return the |
|
3104 // solution. |
|
3105 |
5275
|
3106 octave_idx_type a_nr = a.rows (); |
|
3107 octave_idx_type b_nr = b.rows (); |
1819
|
3108 |
|
3109 double scale; |
5275
|
3110 octave_idx_type info; |
1819
|
3111 |
1950
|
3112 double *pa = sch_a.fortran_vec (); |
|
3113 double *pb = sch_b.fortran_vec (); |
|
3114 double *px = cx.fortran_vec (); |
|
3115 |
4552
|
3116 F77_XFCN (dtrsyl, DTRSYL, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
3117 F77_CONST_CHAR_ARG2 ("N", 1), |
|
3118 1, a_nr, b_nr, pa, a_nr, pb, |
|
3119 b_nr, px, a_nr, scale, info |
|
3120 F77_CHAR_ARG_LEN (1) |
|
3121 F77_CHAR_ARG_LEN (1))); |
1950
|
3122 |
|
3123 |
|
3124 if (f77_exception_encountered) |
|
3125 (*current_liboctave_error_handler) ("unrecoverable error in dtrsyl"); |
|
3126 else |
|
3127 { |
5775
|
3128 // FIXME -- check info? |
1819
|
3129 |
1950
|
3130 retval = -ua*cx*ub.transpose (); |
|
3131 } |
1819
|
3132 |
|
3133 return retval; |
|
3134 } |
|
3135 |
2828
|
3136 // matrix by matrix -> matrix operations |
|
3137 |
6162
|
3138 /* Simple Dot Product, Matrix-Vector and Matrix-Matrix Unit tests |
|
3139 %!assert([1 2 3] * [ 4 ; 5 ; 6], 32, 1e-14) |
|
3140 %!assert([1 2 ; 3 4 ] * [5 ; 6], [17 ; 39 ], 1e-14) |
|
3141 %!assert([1 2 ; 3 4 ] * [5 6 ; 7 8], [19 22; 43 50], 1e-14) |
|
3142 */ |
|
3143 |
|
3144 /* Test some simple identities |
|
3145 %!shared M, cv, rv |
|
3146 %! M = randn(10,10); |
|
3147 %! cv = randn(10,1); |
|
3148 %! rv = randn(1,10); |
|
3149 %!assert([M*cv,M*cv],M*[cv,cv],1e-14) |
|
3150 %!assert([rv*M;rv*M],[rv;rv]*M,1e-14) |
|
3151 %!assert(2*rv*cv,[rv,rv]*[cv;cv],1e-14) |
|
3152 */ |
|
3153 |
|
3154 |
2828
|
3155 Matrix |
|
3156 operator * (const Matrix& m, const Matrix& a) |
|
3157 { |
|
3158 Matrix retval; |
|
3159 |
5275
|
3160 octave_idx_type nr = m.rows (); |
|
3161 octave_idx_type nc = m.cols (); |
|
3162 |
|
3163 octave_idx_type a_nr = a.rows (); |
|
3164 octave_idx_type a_nc = a.cols (); |
2828
|
3165 |
|
3166 if (nc != a_nr) |
|
3167 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
|
3168 else |
|
3169 { |
|
3170 if (nr == 0 || nc == 0 || a_nc == 0) |
|
3171 retval.resize (nr, a_nc, 0.0); |
|
3172 else |
|
3173 { |
5275
|
3174 octave_idx_type ld = nr; |
|
3175 octave_idx_type lda = a_nr; |
2828
|
3176 |
|
3177 retval.resize (nr, a_nc); |
|
3178 double *c = retval.fortran_vec (); |
|
3179 |
5983
|
3180 if (a_nc == 1) |
|
3181 { |
|
3182 if (nr == 1) |
|
3183 F77_FUNC (xddot, XDDOT) (nc, m.data (), 1, a.data (), 1, *c); |
|
3184 else |
6390
|
3185 { |
|
3186 F77_XFCN (dgemv, DGEMV, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
3187 nr, nc, 1.0, m.data (), ld, |
|
3188 a.data (), 1, 0.0, c, 1 |
|
3189 F77_CHAR_ARG_LEN (1))); |
|
3190 |
|
3191 if (f77_exception_encountered) |
|
3192 (*current_liboctave_error_handler) |
|
3193 ("unrecoverable error in dgemv"); |
|
3194 } |
5983
|
3195 } |
|
3196 else |
6390
|
3197 { |
|
3198 F77_XFCN (dgemm, DGEMM, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
3199 F77_CONST_CHAR_ARG2 ("N", 1), |
|
3200 nr, a_nc, nc, 1.0, m.data (), |
|
3201 ld, a.data (), lda, 0.0, c, nr |
|
3202 F77_CHAR_ARG_LEN (1) |
|
3203 F77_CHAR_ARG_LEN (1))); |
|
3204 |
|
3205 if (f77_exception_encountered) |
|
3206 (*current_liboctave_error_handler) |
|
3207 ("unrecoverable error in dgemm"); |
|
3208 } |
2828
|
3209 } |
|
3210 } |
|
3211 |
|
3212 return retval; |
|
3213 } |
|
3214 |
5775
|
3215 // FIXME -- it would be nice to share code among the min/max |
4309
|
3216 // functions below. |
|
3217 |
|
3218 #define EMPTY_RETURN_CHECK(T) \ |
|
3219 if (nr == 0 || nc == 0) \ |
|
3220 return T (nr, nc); |
|
3221 |
|
3222 Matrix |
|
3223 min (double d, const Matrix& m) |
|
3224 { |
5275
|
3225 octave_idx_type nr = m.rows (); |
|
3226 octave_idx_type nc = m.columns (); |
4309
|
3227 |
|
3228 EMPTY_RETURN_CHECK (Matrix); |
|
3229 |
|
3230 Matrix result (nr, nc); |
|
3231 |
5275
|
3232 for (octave_idx_type j = 0; j < nc; j++) |
|
3233 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3234 { |
|
3235 OCTAVE_QUIT; |
|
3236 result (i, j) = xmin (d, m (i, j)); |
|
3237 } |
|
3238 |
|
3239 return result; |
|
3240 } |
|
3241 |
|
3242 Matrix |
|
3243 min (const Matrix& m, double d) |
|
3244 { |
5275
|
3245 octave_idx_type nr = m.rows (); |
|
3246 octave_idx_type nc = m.columns (); |
4309
|
3247 |
|
3248 EMPTY_RETURN_CHECK (Matrix); |
|
3249 |
|
3250 Matrix result (nr, nc); |
|
3251 |
5275
|
3252 for (octave_idx_type j = 0; j < nc; j++) |
|
3253 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3254 { |
|
3255 OCTAVE_QUIT; |
|
3256 result (i, j) = xmin (m (i, j), d); |
|
3257 } |
|
3258 |
|
3259 return result; |
|
3260 } |
|
3261 |
|
3262 Matrix |
|
3263 min (const Matrix& a, const Matrix& b) |
|
3264 { |
5275
|
3265 octave_idx_type nr = a.rows (); |
|
3266 octave_idx_type nc = a.columns (); |
4309
|
3267 |
|
3268 if (nr != b.rows () || nc != b.columns ()) |
|
3269 { |
|
3270 (*current_liboctave_error_handler) |
|
3271 ("two-arg min expecting args of same size"); |
|
3272 return Matrix (); |
|
3273 } |
|
3274 |
|
3275 EMPTY_RETURN_CHECK (Matrix); |
|
3276 |
|
3277 Matrix result (nr, nc); |
|
3278 |
5275
|
3279 for (octave_idx_type j = 0; j < nc; j++) |
|
3280 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3281 { |
|
3282 OCTAVE_QUIT; |
|
3283 result (i, j) = xmin (a (i, j), b (i, j)); |
|
3284 } |
|
3285 |
|
3286 return result; |
|
3287 } |
|
3288 |
|
3289 Matrix |
|
3290 max (double d, const Matrix& m) |
|
3291 { |
5275
|
3292 octave_idx_type nr = m.rows (); |
|
3293 octave_idx_type nc = m.columns (); |
4309
|
3294 |
|
3295 EMPTY_RETURN_CHECK (Matrix); |
|
3296 |
|
3297 Matrix result (nr, nc); |
|
3298 |
5275
|
3299 for (octave_idx_type j = 0; j < nc; j++) |
|
3300 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3301 { |
|
3302 OCTAVE_QUIT; |
|
3303 result (i, j) = xmax (d, m (i, j)); |
|
3304 } |
|
3305 |
|
3306 return result; |
|
3307 } |
|
3308 |
|
3309 Matrix |
|
3310 max (const Matrix& m, double d) |
|
3311 { |
5275
|
3312 octave_idx_type nr = m.rows (); |
|
3313 octave_idx_type nc = m.columns (); |
4309
|
3314 |
|
3315 EMPTY_RETURN_CHECK (Matrix); |
|
3316 |
|
3317 Matrix result (nr, nc); |
|
3318 |
5275
|
3319 for (octave_idx_type j = 0; j < nc; j++) |
|
3320 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3321 { |
|
3322 OCTAVE_QUIT; |
|
3323 result (i, j) = xmax (m (i, j), d); |
|
3324 } |
|
3325 |
|
3326 return result; |
|
3327 } |
|
3328 |
|
3329 Matrix |
|
3330 max (const Matrix& a, const Matrix& b) |
|
3331 { |
5275
|
3332 octave_idx_type nr = a.rows (); |
|
3333 octave_idx_type nc = a.columns (); |
4309
|
3334 |
|
3335 if (nr != b.rows () || nc != b.columns ()) |
|
3336 { |
|
3337 (*current_liboctave_error_handler) |
|
3338 ("two-arg max expecting args of same size"); |
|
3339 return Matrix (); |
|
3340 } |
|
3341 |
|
3342 EMPTY_RETURN_CHECK (Matrix); |
|
3343 |
|
3344 Matrix result (nr, nc); |
|
3345 |
5275
|
3346 for (octave_idx_type j = 0; j < nc; j++) |
|
3347 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3348 { |
|
3349 OCTAVE_QUIT; |
|
3350 result (i, j) = xmax (a (i, j), b (i, j)); |
|
3351 } |
|
3352 |
|
3353 return result; |
|
3354 } |
|
3355 |
2870
|
3356 MS_CMP_OPS(Matrix, , double, ) |
3504
|
3357 MS_BOOL_OPS(Matrix, double, 0.0) |
2870
|
3358 |
|
3359 SM_CMP_OPS(double, , Matrix, ) |
3504
|
3360 SM_BOOL_OPS(double, Matrix, 0.0) |
2870
|
3361 |
|
3362 MM_CMP_OPS(Matrix, , Matrix, ) |
3504
|
3363 MM_BOOL_OPS(Matrix, Matrix, 0.0) |
2870
|
3364 |
458
|
3365 /* |
|
3366 ;;; Local Variables: *** |
|
3367 ;;; mode: C++ *** |
|
3368 ;;; End: *** |
|
3369 */ |