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 |
5275
|
120 F77_FUNC (dgelss, DGELSS) (const octave_idx_type&, const octave_idx_type&, const octave_idx_type&, |
|
121 double*, const octave_idx_type&, double*, |
|
122 const octave_idx_type&, double*, double&, octave_idx_type&, |
|
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); |
|
854 else if (typ != MatrixType::Rectangular) |
|
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); |
|
873 } |
|
874 |
|
875 return ret; |
|
876 } |
|
877 |
|
878 Matrix |
4384
|
879 Matrix::pseudo_inverse (double tol) const |
740
|
880 { |
3480
|
881 SVD result (*this, SVD::economy); |
740
|
882 |
|
883 DiagMatrix S = result.singular_values (); |
|
884 Matrix U = result.left_singular_matrix (); |
|
885 Matrix V = result.right_singular_matrix (); |
|
886 |
|
887 ColumnVector sigma = S.diag (); |
|
888 |
5275
|
889 octave_idx_type r = sigma.length () - 1; |
|
890 octave_idx_type nr = rows (); |
|
891 octave_idx_type nc = cols (); |
740
|
892 |
|
893 if (tol <= 0.0) |
|
894 { |
|
895 if (nr > nc) |
|
896 tol = nr * sigma.elem (0) * DBL_EPSILON; |
|
897 else |
|
898 tol = nc * sigma.elem (0) * DBL_EPSILON; |
|
899 } |
|
900 |
|
901 while (r >= 0 && sigma.elem (r) < tol) |
|
902 r--; |
|
903 |
|
904 if (r < 0) |
|
905 return Matrix (nc, nr, 0.0); |
|
906 else |
|
907 { |
|
908 Matrix Ur = U.extract (0, 0, nr-1, r); |
|
909 DiagMatrix D = DiagMatrix (sigma.extract (0, r)) . inverse (); |
|
910 Matrix Vr = V.extract (0, 0, nc-1, r); |
|
911 return Vr * D * Ur.transpose (); |
|
912 } |
|
913 } |
|
914 |
4773
|
915 #if defined (HAVE_FFTW3) |
3827
|
916 |
|
917 ComplexMatrix |
|
918 Matrix::fourier (void) const |
|
919 { |
|
920 size_t nr = rows (); |
|
921 size_t nc = cols (); |
|
922 |
|
923 ComplexMatrix retval (nr, nc); |
|
924 |
|
925 size_t npts, nsamples; |
|
926 |
|
927 if (nr == 1 || nc == 1) |
|
928 { |
|
929 npts = nr > nc ? nr : nc; |
|
930 nsamples = 1; |
|
931 } |
|
932 else |
|
933 { |
|
934 npts = nr; |
|
935 nsamples = nc; |
|
936 } |
|
937 |
4773
|
938 const double *in (fortran_vec ()); |
3827
|
939 Complex *out (retval.fortran_vec ()); |
|
940 |
4773
|
941 octave_fftw::fft (in, out, npts, nsamples); |
3827
|
942 |
|
943 return retval; |
|
944 } |
|
945 |
|
946 ComplexMatrix |
|
947 Matrix::ifourier (void) const |
|
948 { |
|
949 size_t nr = rows (); |
|
950 size_t nc = cols (); |
|
951 |
|
952 ComplexMatrix retval (nr, nc); |
|
953 |
|
954 size_t npts, nsamples; |
|
955 |
|
956 if (nr == 1 || nc == 1) |
|
957 { |
|
958 npts = nr > nc ? nr : nc; |
|
959 nsamples = 1; |
|
960 } |
|
961 else |
|
962 { |
|
963 npts = nr; |
|
964 nsamples = nc; |
|
965 } |
|
966 |
|
967 ComplexMatrix tmp (*this); |
|
968 Complex *in (tmp.fortran_vec ()); |
|
969 Complex *out (retval.fortran_vec ()); |
|
970 |
4773
|
971 octave_fftw::ifft (in, out, npts, nsamples); |
3827
|
972 |
|
973 return retval; |
|
974 } |
|
975 |
|
976 ComplexMatrix |
|
977 Matrix::fourier2d (void) const |
|
978 { |
4773
|
979 dim_vector dv(rows (), cols ()); |
|
980 |
|
981 const double *in = fortran_vec (); |
|
982 ComplexMatrix retval (rows (), cols ()); |
|
983 octave_fftw::fftNd (in, retval.fortran_vec (), 2, dv); |
3827
|
984 |
|
985 return retval; |
|
986 } |
|
987 |
|
988 ComplexMatrix |
|
989 Matrix::ifourier2d (void) const |
|
990 { |
4773
|
991 dim_vector dv(rows (), cols ()); |
3827
|
992 |
|
993 ComplexMatrix retval (*this); |
4773
|
994 Complex *out (retval.fortran_vec ()); |
|
995 |
|
996 octave_fftw::ifftNd (out, out, 2, dv); |
3827
|
997 |
|
998 return retval; |
|
999 } |
|
1000 |
|
1001 #else |
|
1002 |
458
|
1003 ComplexMatrix |
|
1004 Matrix::fourier (void) const |
|
1005 { |
1948
|
1006 ComplexMatrix retval; |
|
1007 |
5275
|
1008 octave_idx_type nr = rows (); |
|
1009 octave_idx_type nc = cols (); |
|
1010 |
|
1011 octave_idx_type npts, nsamples; |
1948
|
1012 |
458
|
1013 if (nr == 1 || nc == 1) |
|
1014 { |
|
1015 npts = nr > nc ? nr : nc; |
|
1016 nsamples = 1; |
|
1017 } |
|
1018 else |
|
1019 { |
|
1020 npts = nr; |
|
1021 nsamples = nc; |
|
1022 } |
|
1023 |
5275
|
1024 octave_idx_type nn = 4*npts+15; |
1948
|
1025 |
|
1026 Array<Complex> wsave (nn); |
|
1027 Complex *pwsave = wsave.fortran_vec (); |
|
1028 |
3585
|
1029 retval = ComplexMatrix (*this); |
1948
|
1030 Complex *tmp_data = retval.fortran_vec (); |
|
1031 |
3887
|
1032 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
458
|
1033 |
5275
|
1034 for (octave_idx_type j = 0; j < nsamples; j++) |
4153
|
1035 { |
|
1036 OCTAVE_QUIT; |
|
1037 |
|
1038 F77_FUNC (cfftf, CFFTF) (npts, &tmp_data[npts*j], pwsave); |
|
1039 } |
1948
|
1040 |
|
1041 return retval; |
458
|
1042 } |
|
1043 |
|
1044 ComplexMatrix |
|
1045 Matrix::ifourier (void) const |
|
1046 { |
1948
|
1047 ComplexMatrix retval; |
|
1048 |
5275
|
1049 octave_idx_type nr = rows (); |
|
1050 octave_idx_type nc = cols (); |
|
1051 |
|
1052 octave_idx_type npts, nsamples; |
1948
|
1053 |
458
|
1054 if (nr == 1 || nc == 1) |
|
1055 { |
|
1056 npts = nr > nc ? nr : nc; |
|
1057 nsamples = 1; |
|
1058 } |
|
1059 else |
|
1060 { |
|
1061 npts = nr; |
|
1062 nsamples = nc; |
|
1063 } |
|
1064 |
5275
|
1065 octave_idx_type nn = 4*npts+15; |
1948
|
1066 |
|
1067 Array<Complex> wsave (nn); |
|
1068 Complex *pwsave = wsave.fortran_vec (); |
|
1069 |
3585
|
1070 retval = ComplexMatrix (*this); |
1948
|
1071 Complex *tmp_data = retval.fortran_vec (); |
|
1072 |
3887
|
1073 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
458
|
1074 |
5275
|
1075 for (octave_idx_type j = 0; j < nsamples; j++) |
4153
|
1076 { |
|
1077 OCTAVE_QUIT; |
|
1078 |
|
1079 F77_FUNC (cfftb, CFFTB) (npts, &tmp_data[npts*j], pwsave); |
|
1080 } |
458
|
1081 |
5275
|
1082 for (octave_idx_type j = 0; j < npts*nsamples; j++) |
3572
|
1083 tmp_data[j] = tmp_data[j] / static_cast<double> (npts); |
458
|
1084 |
1948
|
1085 return retval; |
458
|
1086 } |
|
1087 |
677
|
1088 ComplexMatrix |
|
1089 Matrix::fourier2d (void) const |
|
1090 { |
1948
|
1091 ComplexMatrix retval; |
|
1092 |
5275
|
1093 octave_idx_type nr = rows (); |
|
1094 octave_idx_type nc = cols (); |
|
1095 |
|
1096 octave_idx_type npts, nsamples; |
1948
|
1097 |
677
|
1098 if (nr == 1 || nc == 1) |
|
1099 { |
|
1100 npts = nr > nc ? nr : nc; |
|
1101 nsamples = 1; |
|
1102 } |
|
1103 else |
|
1104 { |
|
1105 npts = nr; |
|
1106 nsamples = nc; |
|
1107 } |
|
1108 |
5275
|
1109 octave_idx_type nn = 4*npts+15; |
1948
|
1110 |
|
1111 Array<Complex> wsave (nn); |
|
1112 Complex *pwsave = wsave.fortran_vec (); |
|
1113 |
3585
|
1114 retval = ComplexMatrix (*this); |
1948
|
1115 Complex *tmp_data = retval.fortran_vec (); |
|
1116 |
3887
|
1117 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1118 |
5275
|
1119 for (octave_idx_type j = 0; j < nsamples; j++) |
4153
|
1120 { |
|
1121 OCTAVE_QUIT; |
|
1122 |
|
1123 F77_FUNC (cfftf, CFFTF) (npts, &tmp_data[npts*j], pwsave); |
|
1124 } |
677
|
1125 |
|
1126 npts = nc; |
|
1127 nsamples = nr; |
|
1128 nn = 4*npts+15; |
1948
|
1129 |
|
1130 wsave.resize (nn); |
|
1131 pwsave = wsave.fortran_vec (); |
|
1132 |
4773
|
1133 Array<Complex> tmp (npts); |
|
1134 Complex *prow = tmp.fortran_vec (); |
1948
|
1135 |
3887
|
1136 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1137 |
5275
|
1138 for (octave_idx_type j = 0; j < nsamples; j++) |
677
|
1139 { |
4153
|
1140 OCTAVE_QUIT; |
|
1141 |
5275
|
1142 for (octave_idx_type i = 0; i < npts; i++) |
1948
|
1143 prow[i] = tmp_data[i*nr + j]; |
|
1144 |
3887
|
1145 F77_FUNC (cfftf, CFFTF) (npts, prow, pwsave); |
677
|
1146 |
5275
|
1147 for (octave_idx_type i = 0; i < npts; i++) |
1948
|
1148 tmp_data[i*nr + j] = prow[i]; |
677
|
1149 } |
|
1150 |
1948
|
1151 return retval; |
677
|
1152 } |
|
1153 |
|
1154 ComplexMatrix |
|
1155 Matrix::ifourier2d (void) const |
|
1156 { |
1948
|
1157 ComplexMatrix retval; |
|
1158 |
5275
|
1159 octave_idx_type nr = rows (); |
|
1160 octave_idx_type nc = cols (); |
|
1161 |
|
1162 octave_idx_type npts, nsamples; |
1948
|
1163 |
677
|
1164 if (nr == 1 || nc == 1) |
|
1165 { |
|
1166 npts = nr > nc ? nr : nc; |
|
1167 nsamples = 1; |
|
1168 } |
|
1169 else |
|
1170 { |
|
1171 npts = nr; |
|
1172 nsamples = nc; |
|
1173 } |
|
1174 |
5275
|
1175 octave_idx_type nn = 4*npts+15; |
1948
|
1176 |
|
1177 Array<Complex> wsave (nn); |
|
1178 Complex *pwsave = wsave.fortran_vec (); |
|
1179 |
3585
|
1180 retval = ComplexMatrix (*this); |
1948
|
1181 Complex *tmp_data = retval.fortran_vec (); |
|
1182 |
3887
|
1183 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1184 |
5275
|
1185 for (octave_idx_type j = 0; j < nsamples; j++) |
4153
|
1186 { |
|
1187 OCTAVE_QUIT; |
|
1188 |
|
1189 F77_FUNC (cfftb, CFFTB) (npts, &tmp_data[npts*j], pwsave); |
|
1190 } |
677
|
1191 |
5275
|
1192 for (octave_idx_type j = 0; j < npts*nsamples; j++) |
3572
|
1193 tmp_data[j] = tmp_data[j] / static_cast<double> (npts); |
677
|
1194 |
|
1195 npts = nc; |
|
1196 nsamples = nr; |
|
1197 nn = 4*npts+15; |
1948
|
1198 |
|
1199 wsave.resize (nn); |
|
1200 pwsave = wsave.fortran_vec (); |
|
1201 |
4773
|
1202 Array<Complex> tmp (npts); |
|
1203 Complex *prow = tmp.fortran_vec (); |
1948
|
1204 |
3887
|
1205 F77_FUNC (cffti, CFFTI) (npts, pwsave); |
677
|
1206 |
5275
|
1207 for (octave_idx_type j = 0; j < nsamples; j++) |
677
|
1208 { |
4153
|
1209 OCTAVE_QUIT; |
|
1210 |
5275
|
1211 for (octave_idx_type i = 0; i < npts; i++) |
1948
|
1212 prow[i] = tmp_data[i*nr + j]; |
|
1213 |
3887
|
1214 F77_FUNC (cfftb, CFFTB) (npts, prow, pwsave); |
677
|
1215 |
5275
|
1216 for (octave_idx_type i = 0; i < npts; i++) |
3572
|
1217 tmp_data[i*nr + j] = prow[i] / static_cast<double> (npts); |
677
|
1218 } |
|
1219 |
1948
|
1220 return retval; |
677
|
1221 } |
|
1222 |
3827
|
1223 #endif |
|
1224 |
458
|
1225 DET |
|
1226 Matrix::determinant (void) const |
|
1227 { |
5275
|
1228 octave_idx_type info; |
458
|
1229 double rcond; |
4329
|
1230 return determinant (info, rcond, 0); |
458
|
1231 } |
|
1232 |
|
1233 DET |
5275
|
1234 Matrix::determinant (octave_idx_type& info) const |
458
|
1235 { |
|
1236 double rcond; |
4329
|
1237 return determinant (info, rcond, 0); |
458
|
1238 } |
|
1239 |
|
1240 DET |
5275
|
1241 Matrix::determinant (octave_idx_type& info, double& rcond, int calc_cond) const |
458
|
1242 { |
|
1243 DET retval; |
|
1244 |
5275
|
1245 octave_idx_type nr = rows (); |
|
1246 octave_idx_type nc = cols (); |
458
|
1247 |
|
1248 if (nr == 0 || nc == 0) |
|
1249 { |
5634
|
1250 retval = DET (1.0, 0); |
458
|
1251 } |
|
1252 else |
|
1253 { |
5275
|
1254 Array<octave_idx_type> ipvt (nr); |
|
1255 octave_idx_type *pipvt = ipvt.fortran_vec (); |
1948
|
1256 |
|
1257 Matrix atmp = *this; |
|
1258 double *tmp_data = atmp.fortran_vec (); |
|
1259 |
4329
|
1260 info = 0; |
|
1261 |
4330
|
1262 // Calculate the norm of the matrix, for later use. |
4329
|
1263 double anorm = 0; |
|
1264 if (calc_cond) |
5275
|
1265 anorm = atmp.abs().sum().row(static_cast<octave_idx_type>(0)).max(); |
4329
|
1266 |
|
1267 F77_XFCN (dgetrf, DGETRF, (nr, nr, tmp_data, nr, pipvt, info)); |
1948
|
1268 |
|
1269 if (f77_exception_encountered) |
4329
|
1270 (*current_liboctave_error_handler) ("unrecoverable error in dgetrf"); |
458
|
1271 else |
|
1272 { |
4330
|
1273 // Throw-away extra info LAPACK gives so as to not change output. |
4509
|
1274 rcond = 0.0; |
|
1275 if (info != 0) |
1948
|
1276 { |
4509
|
1277 info = -1; |
|
1278 retval = DET (); |
4329
|
1279 } |
|
1280 else |
1948
|
1281 { |
4329
|
1282 if (calc_cond) |
|
1283 { |
4330
|
1284 // Now calc the condition number for non-singular matrix. |
4329
|
1285 char job = '1'; |
|
1286 Array<double> z (4 * nc); |
|
1287 double *pz = z.fortran_vec (); |
5275
|
1288 Array<octave_idx_type> iz (nc); |
|
1289 octave_idx_type *piz = iz.fortran_vec (); |
4329
|
1290 |
4552
|
1291 F77_XFCN (dgecon, DGECON, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1292 nc, tmp_data, nr, anorm, |
|
1293 rcond, pz, piz, info |
|
1294 F77_CHAR_ARG_LEN (1))); |
4329
|
1295 |
|
1296 if (f77_exception_encountered) |
|
1297 (*current_liboctave_error_handler) |
|
1298 ("unrecoverable error in dgecon"); |
|
1299 } |
|
1300 |
4509
|
1301 if (info != 0) |
4329
|
1302 { |
|
1303 info = -1; |
|
1304 retval = DET (); |
|
1305 } |
|
1306 else |
|
1307 { |
5634
|
1308 double c = 1.0; |
|
1309 int e = 0; |
|
1310 |
|
1311 for (octave_idx_type i = 0; i < nc; i++) |
4329
|
1312 { |
5634
|
1313 if (ipvt(i) != (i+1)) |
|
1314 c = -c; |
|
1315 |
|
1316 c *= atmp(i,i); |
|
1317 |
|
1318 if (c == 0.0) |
|
1319 break; |
|
1320 |
|
1321 while (fabs (c) < 0.5) |
4329
|
1322 { |
5634
|
1323 c *= 2.0; |
|
1324 e--; |
4329
|
1325 } |
5634
|
1326 |
|
1327 while (fabs (c) >= 2.0) |
4329
|
1328 { |
5634
|
1329 c /= 2.0; |
|
1330 e++; |
4329
|
1331 } |
|
1332 } |
5634
|
1333 |
|
1334 retval = DET (c, e); |
4329
|
1335 } |
1948
|
1336 } |
458
|
1337 } |
|
1338 } |
|
1339 |
|
1340 return retval; |
|
1341 } |
|
1342 |
|
1343 Matrix |
5785
|
1344 Matrix::utsolve (MatrixType &mattype, const Matrix& b, octave_idx_type& info, |
|
1345 double& rcond, solve_singularity_handler sing_handler, |
|
1346 bool calc_cond) const |
|
1347 { |
|
1348 Matrix retval; |
|
1349 |
|
1350 octave_idx_type nr = rows (); |
|
1351 octave_idx_type nc = cols (); |
|
1352 |
|
1353 if (nr == 0 || nc == 0 || nr != b.rows ()) |
|
1354 (*current_liboctave_error_handler) |
|
1355 ("matrix dimension mismatch solution of linear equations"); |
|
1356 else |
|
1357 { |
|
1358 volatile int typ = mattype.type (); |
|
1359 |
|
1360 if (typ == MatrixType::Permuted_Upper || |
|
1361 typ == MatrixType::Upper) |
|
1362 { |
|
1363 octave_idx_type b_nc = b.cols (); |
|
1364 rcond = 1.; |
|
1365 info = 0; |
|
1366 |
|
1367 if (typ == MatrixType::Permuted_Upper) |
|
1368 { |
|
1369 (*current_liboctave_error_handler) |
6390
|
1370 ("permuted triangular matrix not implemented"); |
5785
|
1371 } |
|
1372 else |
|
1373 { |
|
1374 const double *tmp_data = fortran_vec (); |
|
1375 |
|
1376 if (calc_cond) |
|
1377 { |
|
1378 char norm = '1'; |
|
1379 char uplo = 'U'; |
|
1380 char dia = 'N'; |
|
1381 |
|
1382 Array<double> z (3 * nc); |
|
1383 double *pz = z.fortran_vec (); |
|
1384 Array<octave_idx_type> iz (nc); |
|
1385 octave_idx_type *piz = iz.fortran_vec (); |
|
1386 |
|
1387 F77_XFCN (dtrcon, DTRCON, (F77_CONST_CHAR_ARG2 (&norm, 1), |
|
1388 F77_CONST_CHAR_ARG2 (&uplo, 1), |
|
1389 F77_CONST_CHAR_ARG2 (&dia, 1), |
|
1390 nr, tmp_data, nr, rcond, |
|
1391 pz, piz, info |
|
1392 F77_CHAR_ARG_LEN (1) |
|
1393 F77_CHAR_ARG_LEN (1) |
|
1394 F77_CHAR_ARG_LEN (1))); |
|
1395 |
|
1396 if (f77_exception_encountered) |
|
1397 (*current_liboctave_error_handler) |
|
1398 ("unrecoverable error in dtrcon"); |
|
1399 |
|
1400 if (info != 0) |
|
1401 info = -2; |
|
1402 |
|
1403 volatile double rcond_plus_one = rcond + 1.0; |
|
1404 |
|
1405 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
1406 { |
|
1407 info = -2; |
|
1408 |
|
1409 if (sing_handler) |
|
1410 sing_handler (rcond); |
|
1411 else |
|
1412 (*current_liboctave_error_handler) |
|
1413 ("matrix singular to machine precision, rcond = %g", |
|
1414 rcond); |
|
1415 } |
|
1416 } |
|
1417 |
|
1418 if (info == 0) |
|
1419 { |
|
1420 retval = b; |
|
1421 double *result = retval.fortran_vec (); |
|
1422 |
|
1423 char uplo = 'U'; |
|
1424 char trans = 'N'; |
|
1425 char dia = 'N'; |
|
1426 |
|
1427 F77_XFCN (dtrtrs, DTRTRS, (F77_CONST_CHAR_ARG2 (&uplo, 1), |
|
1428 F77_CONST_CHAR_ARG2 (&trans, 1), |
|
1429 F77_CONST_CHAR_ARG2 (&dia, 1), |
|
1430 nr, b_nc, tmp_data, nr, |
|
1431 result, nr, info |
|
1432 F77_CHAR_ARG_LEN (1) |
|
1433 F77_CHAR_ARG_LEN (1) |
|
1434 F77_CHAR_ARG_LEN (1))); |
|
1435 |
|
1436 if (f77_exception_encountered) |
|
1437 (*current_liboctave_error_handler) |
|
1438 ("unrecoverable error in dtrtrs"); |
|
1439 } |
|
1440 } |
|
1441 } |
|
1442 else |
|
1443 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
1444 } |
|
1445 |
|
1446 return retval; |
|
1447 } |
|
1448 |
|
1449 Matrix |
|
1450 Matrix::ltsolve (MatrixType &mattype, const Matrix& b, octave_idx_type& info, |
|
1451 double& rcond, solve_singularity_handler sing_handler, |
|
1452 bool calc_cond) const |
|
1453 { |
|
1454 Matrix retval; |
|
1455 |
|
1456 octave_idx_type nr = rows (); |
|
1457 octave_idx_type nc = cols (); |
|
1458 |
|
1459 if (nr == 0 || nc == 0 || nr != b.rows ()) |
|
1460 (*current_liboctave_error_handler) |
|
1461 ("matrix dimension mismatch solution of linear equations"); |
|
1462 else |
|
1463 { |
|
1464 volatile int typ = mattype.type (); |
|
1465 |
|
1466 if (typ == MatrixType::Permuted_Lower || |
|
1467 typ == MatrixType::Lower) |
|
1468 { |
|
1469 octave_idx_type b_nc = b.cols (); |
|
1470 rcond = 1.; |
|
1471 info = 0; |
|
1472 |
|
1473 if (typ == MatrixType::Permuted_Lower) |
|
1474 { |
|
1475 (*current_liboctave_error_handler) |
6390
|
1476 ("permuted triangular matrix not implemented"); |
5785
|
1477 } |
|
1478 else |
|
1479 { |
|
1480 const double *tmp_data = fortran_vec (); |
|
1481 |
|
1482 if (calc_cond) |
|
1483 { |
|
1484 char norm = '1'; |
|
1485 char uplo = 'L'; |
|
1486 char dia = 'N'; |
|
1487 |
|
1488 Array<double> z (3 * nc); |
|
1489 double *pz = z.fortran_vec (); |
|
1490 Array<octave_idx_type> iz (nc); |
|
1491 octave_idx_type *piz = iz.fortran_vec (); |
|
1492 |
|
1493 F77_XFCN (dtrcon, DTRCON, (F77_CONST_CHAR_ARG2 (&norm, 1), |
|
1494 F77_CONST_CHAR_ARG2 (&uplo, 1), |
|
1495 F77_CONST_CHAR_ARG2 (&dia, 1), |
|
1496 nr, tmp_data, nr, rcond, |
|
1497 pz, piz, info |
|
1498 F77_CHAR_ARG_LEN (1) |
|
1499 F77_CHAR_ARG_LEN (1) |
|
1500 F77_CHAR_ARG_LEN (1))); |
|
1501 |
|
1502 if (f77_exception_encountered) |
|
1503 (*current_liboctave_error_handler) |
|
1504 ("unrecoverable error in dtrcon"); |
|
1505 |
|
1506 if (info != 0) |
|
1507 info = -2; |
|
1508 |
|
1509 volatile double rcond_plus_one = rcond + 1.0; |
|
1510 |
|
1511 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
1512 { |
|
1513 info = -2; |
|
1514 |
|
1515 if (sing_handler) |
|
1516 sing_handler (rcond); |
|
1517 else |
|
1518 (*current_liboctave_error_handler) |
|
1519 ("matrix singular to machine precision, rcond = %g", |
|
1520 rcond); |
|
1521 } |
|
1522 } |
|
1523 |
|
1524 if (info == 0) |
|
1525 { |
|
1526 retval = b; |
|
1527 double *result = retval.fortran_vec (); |
|
1528 |
|
1529 char uplo = 'L'; |
|
1530 char trans = 'N'; |
|
1531 char dia = 'N'; |
|
1532 |
|
1533 F77_XFCN (dtrtrs, DTRTRS, (F77_CONST_CHAR_ARG2 (&uplo, 1), |
|
1534 F77_CONST_CHAR_ARG2 (&trans, 1), |
|
1535 F77_CONST_CHAR_ARG2 (&dia, 1), |
|
1536 nr, b_nc, tmp_data, nr, |
|
1537 result, nr, info |
|
1538 F77_CHAR_ARG_LEN (1) |
|
1539 F77_CHAR_ARG_LEN (1) |
|
1540 F77_CHAR_ARG_LEN (1))); |
|
1541 |
|
1542 if (f77_exception_encountered) |
|
1543 (*current_liboctave_error_handler) |
|
1544 ("unrecoverable error in dtrtrs"); |
|
1545 } |
|
1546 } |
|
1547 } |
|
1548 else |
|
1549 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
1550 } |
|
1551 |
|
1552 return retval; |
|
1553 } |
|
1554 |
|
1555 Matrix |
|
1556 Matrix::fsolve (MatrixType &mattype, const Matrix& b, octave_idx_type& info, |
|
1557 double& rcond, solve_singularity_handler sing_handler, |
|
1558 bool calc_cond) const |
|
1559 { |
|
1560 Matrix retval; |
|
1561 |
|
1562 octave_idx_type nr = rows (); |
|
1563 octave_idx_type nc = cols (); |
|
1564 |
|
1565 if (nr == 0 || nc == 0 || nr != nc || nr != b.rows ()) |
|
1566 (*current_liboctave_error_handler) |
|
1567 ("matrix dimension mismatch solution of linear equations"); |
|
1568 else |
|
1569 { |
|
1570 volatile int typ = mattype.type (); |
|
1571 |
|
1572 // Calculate the norm of the matrix, for later use. |
|
1573 double anorm = -1.; |
|
1574 |
|
1575 if (typ == MatrixType::Hermitian) |
|
1576 { |
|
1577 info = 0; |
|
1578 char job = 'L'; |
|
1579 Matrix atmp = *this; |
|
1580 double *tmp_data = atmp.fortran_vec (); |
|
1581 anorm = atmp.abs().sum().row(static_cast<octave_idx_type>(0)).max(); |
|
1582 |
|
1583 F77_XFCN (dpotrf, DPOTRF, (F77_CONST_CHAR_ARG2 (&job, 1), nr, |
|
1584 tmp_data, nr, info |
|
1585 F77_CHAR_ARG_LEN (1))); |
|
1586 |
|
1587 if (f77_exception_encountered) |
|
1588 (*current_liboctave_error_handler) |
|
1589 ("unrecoverable error in dpotrf"); |
|
1590 else |
|
1591 { |
|
1592 // Throw-away extra info LAPACK gives so as to not change output. |
|
1593 rcond = 0.0; |
|
1594 if (info != 0) |
|
1595 { |
|
1596 info = -2; |
|
1597 |
|
1598 mattype.mark_as_unsymmetric (); |
|
1599 typ = MatrixType::Full; |
|
1600 } |
|
1601 else |
|
1602 { |
|
1603 if (calc_cond) |
|
1604 { |
|
1605 Array<double> z (3 * nc); |
|
1606 double *pz = z.fortran_vec (); |
|
1607 Array<octave_idx_type> iz (nc); |
|
1608 octave_idx_type *piz = iz.fortran_vec (); |
|
1609 |
|
1610 F77_XFCN (dpocon, DPOCON, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1611 nr, tmp_data, nr, anorm, |
|
1612 rcond, pz, piz, info |
|
1613 F77_CHAR_ARG_LEN (1))); |
|
1614 |
|
1615 if (f77_exception_encountered) |
|
1616 (*current_liboctave_error_handler) |
|
1617 ("unrecoverable error in dpocon"); |
|
1618 |
|
1619 if (info != 0) |
|
1620 info = -2; |
|
1621 |
|
1622 volatile double rcond_plus_one = rcond + 1.0; |
|
1623 |
|
1624 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
1625 { |
|
1626 info = -2; |
|
1627 |
|
1628 if (sing_handler) |
|
1629 sing_handler (rcond); |
|
1630 else |
|
1631 (*current_liboctave_error_handler) |
|
1632 ("matrix singular to machine precision, rcond = %g", |
|
1633 rcond); |
|
1634 } |
|
1635 } |
|
1636 |
|
1637 if (info == 0) |
|
1638 { |
|
1639 retval = b; |
|
1640 double *result = retval.fortran_vec (); |
|
1641 |
|
1642 octave_idx_type b_nc = b.cols (); |
|
1643 |
|
1644 F77_XFCN (dpotrs, DPOTRS, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1645 nr, b_nc, tmp_data, nr, |
|
1646 result, b.rows(), info |
|
1647 F77_CHAR_ARG_LEN (1))); |
|
1648 |
|
1649 if (f77_exception_encountered) |
|
1650 (*current_liboctave_error_handler) |
|
1651 ("unrecoverable error in dpotrs"); |
|
1652 } |
|
1653 else |
|
1654 { |
|
1655 mattype.mark_as_unsymmetric (); |
|
1656 typ = MatrixType::Full; |
|
1657 } |
|
1658 } |
|
1659 } |
|
1660 } |
|
1661 |
|
1662 if (typ == MatrixType::Full) |
|
1663 { |
|
1664 info = 0; |
|
1665 |
|
1666 Array<octave_idx_type> ipvt (nr); |
|
1667 octave_idx_type *pipvt = ipvt.fortran_vec (); |
|
1668 |
|
1669 Matrix atmp = *this; |
|
1670 double *tmp_data = atmp.fortran_vec (); |
|
1671 if(anorm < 0.) |
|
1672 anorm = atmp.abs().sum().row(static_cast<octave_idx_type>(0)).max(); |
|
1673 |
|
1674 Array<double> z (4 * nc); |
|
1675 double *pz = z.fortran_vec (); |
|
1676 Array<octave_idx_type> iz (nc); |
|
1677 octave_idx_type *piz = iz.fortran_vec (); |
|
1678 |
|
1679 F77_XFCN (dgetrf, DGETRF, (nr, nr, tmp_data, nr, pipvt, info)); |
|
1680 |
|
1681 if (f77_exception_encountered) |
|
1682 (*current_liboctave_error_handler) |
|
1683 ("unrecoverable error in dgetrf"); |
|
1684 else |
|
1685 { |
|
1686 // Throw-away extra info LAPACK gives so as to not change output. |
|
1687 rcond = 0.0; |
|
1688 if (info != 0) |
|
1689 { |
|
1690 info = -2; |
|
1691 |
|
1692 if (sing_handler) |
|
1693 sing_handler (rcond); |
|
1694 else |
|
1695 (*current_liboctave_error_handler) |
|
1696 ("matrix singular to machine precision"); |
|
1697 |
|
1698 mattype.mark_as_rectangular (); |
|
1699 } |
|
1700 else |
|
1701 { |
|
1702 if (calc_cond) |
|
1703 { |
|
1704 // Now calculate the condition number for |
|
1705 // non-singular matrix. |
|
1706 char job = '1'; |
|
1707 F77_XFCN (dgecon, DGECON, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1708 nc, tmp_data, nr, anorm, |
|
1709 rcond, pz, piz, info |
|
1710 F77_CHAR_ARG_LEN (1))); |
|
1711 |
|
1712 if (f77_exception_encountered) |
|
1713 (*current_liboctave_error_handler) |
|
1714 ("unrecoverable error in dgecon"); |
|
1715 |
|
1716 if (info != 0) |
|
1717 info = -2; |
|
1718 |
|
1719 volatile double rcond_plus_one = rcond + 1.0; |
|
1720 |
|
1721 if (rcond_plus_one == 1.0 || xisnan (rcond)) |
|
1722 { |
|
1723 info = -2; |
|
1724 |
|
1725 if (sing_handler) |
|
1726 sing_handler (rcond); |
|
1727 else |
|
1728 (*current_liboctave_error_handler) |
|
1729 ("matrix singular to machine precision, rcond = %g", |
|
1730 rcond); |
|
1731 } |
|
1732 } |
|
1733 |
|
1734 if (info == 0) |
|
1735 { |
|
1736 retval = b; |
|
1737 double *result = retval.fortran_vec (); |
|
1738 |
|
1739 octave_idx_type b_nc = b.cols (); |
|
1740 |
|
1741 char job = 'N'; |
|
1742 F77_XFCN (dgetrs, DGETRS, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
1743 nr, b_nc, tmp_data, nr, |
|
1744 pipvt, result, b.rows(), info |
|
1745 F77_CHAR_ARG_LEN (1))); |
|
1746 |
|
1747 if (f77_exception_encountered) |
|
1748 (*current_liboctave_error_handler) |
|
1749 ("unrecoverable error in dgetrs"); |
|
1750 } |
|
1751 else |
|
1752 mattype.mark_as_rectangular (); |
|
1753 } |
|
1754 } |
|
1755 } |
|
1756 else if (typ != MatrixType::Hermitian) |
|
1757 (*current_liboctave_error_handler) ("incorrect matrix type"); |
|
1758 } |
|
1759 |
|
1760 return retval; |
|
1761 } |
|
1762 |
|
1763 Matrix |
|
1764 Matrix::solve (MatrixType &typ, const Matrix& b) const |
|
1765 { |
|
1766 octave_idx_type info; |
|
1767 double rcond; |
|
1768 return solve (typ, b, info, rcond, 0); |
|
1769 } |
|
1770 |
|
1771 Matrix |
|
1772 Matrix::solve (MatrixType &typ, const Matrix& b, octave_idx_type& info, |
|
1773 double& rcond) const |
|
1774 { |
|
1775 return solve (typ, b, info, rcond, 0); |
|
1776 } |
|
1777 |
|
1778 Matrix |
|
1779 Matrix::solve (MatrixType &mattype, const Matrix& b, octave_idx_type& info, |
|
1780 double& rcond, solve_singularity_handler sing_handler, |
|
1781 bool singular_fallback) const |
|
1782 { |
|
1783 Matrix retval; |
|
1784 int typ = mattype.type (); |
|
1785 |
|
1786 if (typ == MatrixType::Unknown) |
|
1787 typ = mattype.type (*this); |
|
1788 |
|
1789 // Only calculate the condition number for LU/Cholesky |
|
1790 if (typ == MatrixType::Upper || typ == MatrixType::Permuted_Upper) |
|
1791 retval = utsolve (mattype, b, info, rcond, sing_handler, false); |
|
1792 else if (typ == MatrixType::Lower || typ == MatrixType::Permuted_Lower) |
|
1793 retval = ltsolve (mattype, b, info, rcond, sing_handler, false); |
|
1794 else if (typ == MatrixType::Full || typ == MatrixType::Hermitian) |
|
1795 retval = fsolve (mattype, b, info, rcond, sing_handler, true); |
|
1796 else if (typ != MatrixType::Rectangular) |
|
1797 { |
|
1798 (*current_liboctave_error_handler) ("unknown matrix type"); |
|
1799 return Matrix (); |
|
1800 } |
|
1801 |
|
1802 // Rectangular or one of the above solvers flags a singular matrix |
|
1803 if (singular_fallback && mattype.type () == MatrixType::Rectangular) |
|
1804 { |
|
1805 octave_idx_type rank; |
|
1806 retval = lssolve (b, info, rank); |
|
1807 } |
|
1808 |
|
1809 return retval; |
|
1810 } |
|
1811 |
|
1812 ComplexMatrix |
|
1813 Matrix::solve (MatrixType &typ, const ComplexMatrix& b) const |
|
1814 { |
|
1815 ComplexMatrix tmp (*this); |
|
1816 return tmp.solve (typ, b); |
|
1817 } |
|
1818 |
|
1819 ComplexMatrix |
|
1820 Matrix::solve (MatrixType &typ, const ComplexMatrix& b, |
|
1821 octave_idx_type& info) const |
|
1822 { |
|
1823 ComplexMatrix tmp (*this); |
|
1824 return tmp.solve (typ, b, info); |
|
1825 } |
|
1826 |
|
1827 ComplexMatrix |
|
1828 Matrix::solve (MatrixType &typ, const ComplexMatrix& b, octave_idx_type& info, |
|
1829 double& rcond) const |
|
1830 { |
|
1831 ComplexMatrix tmp (*this); |
|
1832 return tmp.solve (typ, b, info, rcond); |
|
1833 } |
|
1834 |
|
1835 ComplexMatrix |
|
1836 Matrix::solve (MatrixType &typ, const ComplexMatrix& b, octave_idx_type& info, |
|
1837 double& rcond, solve_singularity_handler sing_handler, |
|
1838 bool singular_fallback) const |
|
1839 { |
|
1840 ComplexMatrix tmp (*this); |
|
1841 return tmp.solve (typ, b, info, rcond, sing_handler, singular_fallback); |
|
1842 } |
|
1843 |
|
1844 ColumnVector |
|
1845 Matrix::solve (MatrixType &typ, const ColumnVector& b) const |
|
1846 { |
|
1847 octave_idx_type info; double rcond; |
|
1848 return solve (typ, b, info, rcond); |
|
1849 } |
|
1850 |
|
1851 ColumnVector |
|
1852 Matrix::solve (MatrixType &typ, const ColumnVector& b, |
|
1853 octave_idx_type& info) const |
|
1854 { |
|
1855 double rcond; |
|
1856 return solve (typ, b, info, rcond); |
|
1857 } |
|
1858 |
|
1859 ColumnVector |
|
1860 Matrix::solve (MatrixType &typ, const ColumnVector& b, octave_idx_type& info, |
|
1861 double& rcond) const |
|
1862 { |
|
1863 return solve (typ, b, info, rcond, 0); |
|
1864 } |
|
1865 |
|
1866 ColumnVector |
|
1867 Matrix::solve (MatrixType &typ, const ColumnVector& b, octave_idx_type& info, |
|
1868 double& rcond, solve_singularity_handler sing_handler) const |
|
1869 { |
|
1870 Matrix tmp (b); |
|
1871 return solve (typ, tmp, info, rcond, sing_handler).column(static_cast<octave_idx_type> (0)); |
|
1872 } |
|
1873 |
|
1874 ComplexColumnVector |
|
1875 Matrix::solve (MatrixType &typ, const ComplexColumnVector& b) const |
|
1876 { |
|
1877 ComplexMatrix tmp (*this); |
|
1878 return tmp.solve (typ, b); |
|
1879 } |
|
1880 |
|
1881 ComplexColumnVector |
|
1882 Matrix::solve (MatrixType &typ, const ComplexColumnVector& b, |
|
1883 octave_idx_type& info) const |
|
1884 { |
|
1885 ComplexMatrix tmp (*this); |
|
1886 return tmp.solve (typ, b, info); |
|
1887 } |
|
1888 |
|
1889 ComplexColumnVector |
|
1890 Matrix::solve (MatrixType &typ, const ComplexColumnVector& b, |
|
1891 octave_idx_type& info, double& rcond) const |
|
1892 { |
|
1893 ComplexMatrix tmp (*this); |
|
1894 return tmp.solve (typ, b, info, rcond); |
|
1895 } |
|
1896 |
|
1897 ComplexColumnVector |
|
1898 Matrix::solve (MatrixType &typ, const ComplexColumnVector& b, |
|
1899 octave_idx_type& info, double& rcond, |
|
1900 solve_singularity_handler sing_handler) const |
|
1901 { |
|
1902 ComplexMatrix tmp (*this); |
|
1903 return tmp.solve(typ, b, info, rcond, sing_handler); |
|
1904 } |
|
1905 |
|
1906 Matrix |
458
|
1907 Matrix::solve (const Matrix& b) const |
|
1908 { |
5275
|
1909 octave_idx_type info; |
458
|
1910 double rcond; |
4329
|
1911 return solve (b, info, rcond, 0); |
458
|
1912 } |
|
1913 |
|
1914 Matrix |
5275
|
1915 Matrix::solve (const Matrix& b, octave_idx_type& info) const |
458
|
1916 { |
|
1917 double rcond; |
4329
|
1918 return solve (b, info, rcond, 0); |
458
|
1919 } |
|
1920 |
|
1921 Matrix |
5275
|
1922 Matrix::solve (const Matrix& b, octave_idx_type& info, double& rcond) const |
458
|
1923 { |
3480
|
1924 return solve (b, info, rcond, 0); |
|
1925 } |
|
1926 |
|
1927 Matrix |
5785
|
1928 Matrix::solve (const Matrix& b, octave_idx_type& info, |
|
1929 double& rcond, solve_singularity_handler sing_handler) const |
3480
|
1930 { |
5785
|
1931 MatrixType mattype (*this); |
|
1932 return solve (mattype, b, info, rcond, sing_handler); |
458
|
1933 } |
|
1934 |
|
1935 ComplexMatrix |
|
1936 Matrix::solve (const ComplexMatrix& b) const |
|
1937 { |
|
1938 ComplexMatrix tmp (*this); |
|
1939 return tmp.solve (b); |
|
1940 } |
|
1941 |
|
1942 ComplexMatrix |
5275
|
1943 Matrix::solve (const ComplexMatrix& b, octave_idx_type& info) const |
458
|
1944 { |
|
1945 ComplexMatrix tmp (*this); |
|
1946 return tmp.solve (b, info); |
|
1947 } |
|
1948 |
|
1949 ComplexMatrix |
5275
|
1950 Matrix::solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond) const |
458
|
1951 { |
|
1952 ComplexMatrix tmp (*this); |
|
1953 return tmp.solve (b, info, rcond); |
|
1954 } |
|
1955 |
3480
|
1956 ComplexMatrix |
5275
|
1957 Matrix::solve (const ComplexMatrix& b, octave_idx_type& info, double& rcond, |
3480
|
1958 solve_singularity_handler sing_handler) const |
|
1959 { |
|
1960 ComplexMatrix tmp (*this); |
|
1961 return tmp.solve (b, info, rcond, sing_handler); |
|
1962 } |
|
1963 |
458
|
1964 ColumnVector |
|
1965 Matrix::solve (const ColumnVector& b) const |
|
1966 { |
5275
|
1967 octave_idx_type info; double rcond; |
458
|
1968 return solve (b, info, rcond); |
|
1969 } |
|
1970 |
|
1971 ColumnVector |
5275
|
1972 Matrix::solve (const ColumnVector& b, octave_idx_type& info) const |
458
|
1973 { |
|
1974 double rcond; |
|
1975 return solve (b, info, rcond); |
|
1976 } |
|
1977 |
|
1978 ColumnVector |
5275
|
1979 Matrix::solve (const ColumnVector& b, octave_idx_type& info, double& rcond) const |
458
|
1980 { |
3480
|
1981 return solve (b, info, rcond, 0); |
|
1982 } |
|
1983 |
|
1984 ColumnVector |
5275
|
1985 Matrix::solve (const ColumnVector& b, octave_idx_type& info, double& rcond, |
3480
|
1986 solve_singularity_handler sing_handler) const |
|
1987 { |
5785
|
1988 MatrixType mattype (*this); |
|
1989 return solve (mattype, b, info, rcond, sing_handler); |
458
|
1990 } |
|
1991 |
|
1992 ComplexColumnVector |
|
1993 Matrix::solve (const ComplexColumnVector& b) const |
|
1994 { |
|
1995 ComplexMatrix tmp (*this); |
|
1996 return tmp.solve (b); |
|
1997 } |
|
1998 |
|
1999 ComplexColumnVector |
5275
|
2000 Matrix::solve (const ComplexColumnVector& b, octave_idx_type& info) const |
458
|
2001 { |
|
2002 ComplexMatrix tmp (*this); |
|
2003 return tmp.solve (b, info); |
|
2004 } |
|
2005 |
|
2006 ComplexColumnVector |
5275
|
2007 Matrix::solve (const ComplexColumnVector& b, octave_idx_type& info, double& rcond) const |
458
|
2008 { |
|
2009 ComplexMatrix tmp (*this); |
|
2010 return tmp.solve (b, info, rcond); |
|
2011 } |
|
2012 |
3480
|
2013 ComplexColumnVector |
5275
|
2014 Matrix::solve (const ComplexColumnVector& b, octave_idx_type& info, double& rcond, |
3480
|
2015 solve_singularity_handler sing_handler) const |
|
2016 { |
|
2017 ComplexMatrix tmp (*this); |
|
2018 return tmp.solve (b, info, rcond, sing_handler); |
|
2019 } |
|
2020 |
458
|
2021 Matrix |
|
2022 Matrix::lssolve (const Matrix& b) const |
|
2023 { |
5275
|
2024 octave_idx_type info; |
|
2025 octave_idx_type rank; |
458
|
2026 return lssolve (b, info, rank); |
|
2027 } |
|
2028 |
|
2029 Matrix |
5275
|
2030 Matrix::lssolve (const Matrix& b, octave_idx_type& info) const |
458
|
2031 { |
5275
|
2032 octave_idx_type rank; |
458
|
2033 return lssolve (b, info, rank); |
|
2034 } |
|
2035 |
|
2036 Matrix |
5275
|
2037 Matrix::lssolve (const Matrix& b, octave_idx_type& info, octave_idx_type& rank) const |
458
|
2038 { |
1948
|
2039 Matrix retval; |
|
2040 |
5275
|
2041 octave_idx_type nrhs = b.cols (); |
|
2042 |
|
2043 octave_idx_type m = rows (); |
|
2044 octave_idx_type n = cols (); |
458
|
2045 |
|
2046 if (m == 0 || n == 0 || m != b.rows ()) |
1948
|
2047 (*current_liboctave_error_handler) |
|
2048 ("matrix dimension mismatch in solution of least squares problem"); |
|
2049 else |
458
|
2050 { |
1948
|
2051 Matrix atmp = *this; |
|
2052 double *tmp_data = atmp.fortran_vec (); |
|
2053 |
5275
|
2054 octave_idx_type nrr = m > n ? m : n; |
3754
|
2055 Matrix result (nrr, nrhs, 0.0); |
1948
|
2056 |
5275
|
2057 for (octave_idx_type j = 0; j < nrhs; j++) |
|
2058 for (octave_idx_type i = 0; i < m; i++) |
1948
|
2059 result.elem (i, j) = b.elem (i, j); |
|
2060 |
|
2061 double *presult = result.fortran_vec (); |
|
2062 |
5275
|
2063 octave_idx_type len_s = m < n ? m : n; |
1948
|
2064 Array<double> s (len_s); |
|
2065 double *ps = s.fortran_vec (); |
|
2066 |
|
2067 double rcond = -1.0; |
|
2068 |
3752
|
2069 // Ask DGELSS what the dimension of WORK should be. |
|
2070 |
5275
|
2071 octave_idx_type lwork = -1; |
3752
|
2072 |
|
2073 Array<double> work (1); |
1948
|
2074 |
|
2075 F77_XFCN (dgelss, DGELSS, (m, n, nrhs, tmp_data, m, presult, nrr, ps, |
3752
|
2076 rcond, rank, work.fortran_vec (), |
|
2077 lwork, info)); |
1948
|
2078 |
|
2079 if (f77_exception_encountered) |
|
2080 (*current_liboctave_error_handler) ("unrecoverable error in dgelss"); |
|
2081 else |
|
2082 { |
5275
|
2083 lwork = static_cast<octave_idx_type> (work(0)); |
3752
|
2084 work.resize (lwork); |
|
2085 |
|
2086 F77_XFCN (dgelss, DGELSS, (m, n, nrhs, tmp_data, m, presult, |
|
2087 nrr, ps, rcond, rank, |
|
2088 work.fortran_vec (), lwork, info)); |
|
2089 |
|
2090 if (f77_exception_encountered) |
|
2091 (*current_liboctave_error_handler) |
|
2092 ("unrecoverable error in dgelss"); |
|
2093 else |
|
2094 { |
|
2095 retval.resize (n, nrhs); |
5275
|
2096 for (octave_idx_type j = 0; j < nrhs; j++) |
|
2097 for (octave_idx_type i = 0; i < n; i++) |
3752
|
2098 retval.elem (i, j) = result.elem (i, j); |
|
2099 } |
1948
|
2100 } |
458
|
2101 } |
|
2102 |
|
2103 return retval; |
|
2104 } |
|
2105 |
|
2106 ComplexMatrix |
|
2107 Matrix::lssolve (const ComplexMatrix& b) const |
|
2108 { |
|
2109 ComplexMatrix tmp (*this); |
5275
|
2110 octave_idx_type info; |
|
2111 octave_idx_type rank; |
1484
|
2112 return tmp.lssolve (b, info, rank); |
458
|
2113 } |
|
2114 |
|
2115 ComplexMatrix |
5275
|
2116 Matrix::lssolve (const ComplexMatrix& b, octave_idx_type& info) const |
458
|
2117 { |
|
2118 ComplexMatrix tmp (*this); |
5275
|
2119 octave_idx_type rank; |
1484
|
2120 return tmp.lssolve (b, info, rank); |
458
|
2121 } |
|
2122 |
|
2123 ComplexMatrix |
5275
|
2124 Matrix::lssolve (const ComplexMatrix& b, octave_idx_type& info, octave_idx_type& rank) const |
458
|
2125 { |
|
2126 ComplexMatrix tmp (*this); |
1484
|
2127 return tmp.lssolve (b, info, rank); |
458
|
2128 } |
|
2129 |
|
2130 ColumnVector |
|
2131 Matrix::lssolve (const ColumnVector& b) const |
|
2132 { |
5275
|
2133 octave_idx_type info; |
|
2134 octave_idx_type rank; |
1484
|
2135 return lssolve (b, info, rank); |
458
|
2136 } |
|
2137 |
|
2138 ColumnVector |
5275
|
2139 Matrix::lssolve (const ColumnVector& b, octave_idx_type& info) const |
458
|
2140 { |
5275
|
2141 octave_idx_type rank; |
458
|
2142 return lssolve (b, info, rank); |
|
2143 } |
|
2144 |
|
2145 ColumnVector |
5275
|
2146 Matrix::lssolve (const ColumnVector& b, octave_idx_type& info, octave_idx_type& rank) const |
458
|
2147 { |
1948
|
2148 ColumnVector retval; |
|
2149 |
5275
|
2150 octave_idx_type nrhs = 1; |
|
2151 |
|
2152 octave_idx_type m = rows (); |
|
2153 octave_idx_type n = cols (); |
458
|
2154 |
|
2155 if (m == 0 || n == 0 || m != b.length ()) |
1948
|
2156 (*current_liboctave_error_handler) |
|
2157 ("matrix dimension mismatch in solution of least squares problem"); |
|
2158 else |
458
|
2159 { |
1948
|
2160 Matrix atmp = *this; |
|
2161 double *tmp_data = atmp.fortran_vec (); |
|
2162 |
5275
|
2163 octave_idx_type nrr = m > n ? m : n; |
1948
|
2164 ColumnVector result (nrr); |
|
2165 |
5275
|
2166 for (octave_idx_type i = 0; i < m; i++) |
1948
|
2167 result.elem (i) = b.elem (i); |
|
2168 |
|
2169 double *presult = result.fortran_vec (); |
|
2170 |
5275
|
2171 octave_idx_type len_s = m < n ? m : n; |
1948
|
2172 Array<double> s (len_s); |
|
2173 double *ps = s.fortran_vec (); |
|
2174 |
|
2175 double rcond = -1.0; |
|
2176 |
3752
|
2177 // Ask DGELSS what the dimension of WORK should be. |
|
2178 |
5275
|
2179 octave_idx_type lwork = -1; |
3752
|
2180 |
|
2181 Array<double> work (1); |
|
2182 |
|
2183 F77_XFCN (dgelss, DGELSS, (m, n, nrhs, tmp_data, m, presult, nrr, ps, |
|
2184 rcond, rank, work.fortran_vec (), |
|
2185 lwork, info)); |
1948
|
2186 |
|
2187 if (f77_exception_encountered) |
|
2188 (*current_liboctave_error_handler) ("unrecoverable error in dgelss"); |
|
2189 else |
|
2190 { |
5275
|
2191 lwork = static_cast<octave_idx_type> (work(0)); |
3752
|
2192 work.resize (lwork); |
|
2193 |
|
2194 F77_XFCN (dgelss, DGELSS, (m, n, nrhs, tmp_data, m, presult, |
|
2195 nrr, ps, rcond, rank, |
|
2196 work.fortran_vec (), lwork, info)); |
|
2197 |
|
2198 if (f77_exception_encountered) |
|
2199 (*current_liboctave_error_handler) |
|
2200 ("unrecoverable error in dgelss"); |
|
2201 else |
|
2202 { |
|
2203 retval.resize (n); |
5275
|
2204 for (octave_idx_type i = 0; i < n; i++) |
3752
|
2205 retval.elem (i) = result.elem (i); |
|
2206 } |
1948
|
2207 } |
458
|
2208 } |
|
2209 |
|
2210 return retval; |
|
2211 } |
|
2212 |
|
2213 ComplexColumnVector |
|
2214 Matrix::lssolve (const ComplexColumnVector& b) const |
|
2215 { |
|
2216 ComplexMatrix tmp (*this); |
|
2217 return tmp.lssolve (b); |
|
2218 } |
|
2219 |
|
2220 ComplexColumnVector |
5275
|
2221 Matrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info) const |
458
|
2222 { |
|
2223 ComplexMatrix tmp (*this); |
|
2224 return tmp.lssolve (b, info); |
|
2225 } |
|
2226 |
|
2227 ComplexColumnVector |
5275
|
2228 Matrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info, octave_idx_type& rank) const |
458
|
2229 { |
|
2230 ComplexMatrix tmp (*this); |
|
2231 return tmp.lssolve (b, info, rank); |
|
2232 } |
|
2233 |
1819
|
2234 // Constants for matrix exponential calculation. |
|
2235 |
|
2236 static double padec [] = |
|
2237 { |
|
2238 5.0000000000000000e-1, |
|
2239 1.1666666666666667e-1, |
|
2240 1.6666666666666667e-2, |
|
2241 1.6025641025641026e-3, |
|
2242 1.0683760683760684e-4, |
|
2243 4.8562548562548563e-6, |
|
2244 1.3875013875013875e-7, |
|
2245 1.9270852604185938e-9, |
|
2246 }; |
|
2247 |
|
2248 Matrix |
|
2249 Matrix::expm (void) const |
|
2250 { |
|
2251 Matrix retval; |
|
2252 |
|
2253 Matrix m = *this; |
|
2254 |
6699
|
2255 if (numel () == 1) |
|
2256 return Matrix (1, 1, exp (m(0))); |
|
2257 |
5275
|
2258 octave_idx_type nc = columns (); |
1819
|
2259 |
3130
|
2260 // Preconditioning step 1: trace normalization to reduce dynamic |
|
2261 // range of poles, but avoid making stable eigenvalues unstable. |
|
2262 |
1819
|
2263 // trace shift value |
3331
|
2264 volatile double trshift = 0.0; |
1819
|
2265 |
5275
|
2266 for (octave_idx_type i = 0; i < nc; i++) |
1819
|
2267 trshift += m.elem (i, i); |
|
2268 |
|
2269 trshift /= nc; |
|
2270 |
3130
|
2271 if (trshift > 0.0) |
|
2272 { |
5275
|
2273 for (octave_idx_type i = 0; i < nc; i++) |
3130
|
2274 m.elem (i, i) -= trshift; |
|
2275 } |
1819
|
2276 |
3331
|
2277 // Preconditioning step 2: balancing; code follows development |
|
2278 // in AEPBAL |
|
2279 |
|
2280 double *p_m = m.fortran_vec (); |
|
2281 |
5275
|
2282 octave_idx_type info, ilo, ihi, ilos, ihis; |
3468
|
2283 Array<double> dpermute (nc); |
|
2284 Array<double> dscale (nc); |
3466
|
2285 |
3468
|
2286 // permutation first |
|
2287 char job = 'P'; |
4552
|
2288 F77_XFCN (dgebal, DGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
2289 nc, p_m, nc, ilo, ihi, |
|
2290 dpermute.fortran_vec (), info |
|
2291 F77_CHAR_ARG_LEN (1))); |
3466
|
2292 |
3468
|
2293 // then scaling |
|
2294 job = 'S'; |
4552
|
2295 F77_XFCN (dgebal, DGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
2296 nc, p_m, nc, ilos, ihis, |
|
2297 dscale.fortran_vec (), info |
|
2298 F77_CHAR_ARG_LEN (1))); |
3331
|
2299 |
|
2300 if (f77_exception_encountered) |
|
2301 { |
|
2302 (*current_liboctave_error_handler) ("unrecoverable error in dgebal"); |
|
2303 return retval; |
|
2304 } |
|
2305 |
1819
|
2306 // Preconditioning step 3: scaling. |
3331
|
2307 |
1819
|
2308 ColumnVector work(nc); |
3130
|
2309 double inf_norm; |
3331
|
2310 |
4552
|
2311 F77_XFCN (xdlange, XDLANGE, (F77_CONST_CHAR_ARG2 ("I", 1), |
|
2312 nc, nc, m.fortran_vec (), nc, |
|
2313 work.fortran_vec (), inf_norm |
|
2314 F77_CHAR_ARG_LEN (1))); |
3331
|
2315 |
|
2316 if (f77_exception_encountered) |
|
2317 { |
|
2318 (*current_liboctave_error_handler) ("unrecoverable error in dlange"); |
|
2319 return retval; |
|
2320 } |
1819
|
2321 |
5275
|
2322 octave_idx_type sqpow = static_cast<octave_idx_type> (inf_norm > 0.0 |
1819
|
2323 ? (1.0 + log (inf_norm) / log (2.0)) |
|
2324 : 0.0); |
3331
|
2325 |
1819
|
2326 // Check whether we need to square at all. |
3331
|
2327 |
1819
|
2328 if (sqpow < 0) |
|
2329 sqpow = 0; |
3331
|
2330 |
1819
|
2331 if (sqpow > 0) |
|
2332 { |
|
2333 double scale_factor = 1.0; |
5275
|
2334 for (octave_idx_type i = 0; i < sqpow; i++) |
1819
|
2335 scale_factor *= 2.0; |
3331
|
2336 |
1819
|
2337 m = m / scale_factor; |
|
2338 } |
3331
|
2339 |
1819
|
2340 // npp, dpp: pade' approx polynomial matrices. |
3331
|
2341 |
1819
|
2342 Matrix npp (nc, nc, 0.0); |
|
2343 Matrix dpp = npp; |
3331
|
2344 |
1819
|
2345 // Now powers a^8 ... a^1. |
3331
|
2346 |
5275
|
2347 octave_idx_type minus_one_j = -1; |
|
2348 for (octave_idx_type j = 7; j >= 0; j--) |
1819
|
2349 { |
3573
|
2350 npp = m * npp + padec[j] * m; |
|
2351 dpp = m * dpp + (minus_one_j * padec[j]) * m; |
1819
|
2352 minus_one_j *= -1; |
|
2353 } |
3331
|
2354 |
1819
|
2355 // Zero power. |
3331
|
2356 |
1819
|
2357 dpp = -dpp; |
5275
|
2358 for (octave_idx_type j = 0; j < nc; j++) |
1819
|
2359 { |
|
2360 npp.elem (j, j) += 1.0; |
|
2361 dpp.elem (j, j) += 1.0; |
|
2362 } |
3331
|
2363 |
1819
|
2364 // Compute pade approximation = inverse (dpp) * npp. |
|
2365 |
3331
|
2366 retval = dpp.solve (npp, info); |
|
2367 |
1819
|
2368 // Reverse preconditioning step 3: repeated squaring. |
3331
|
2369 |
1819
|
2370 while (sqpow) |
|
2371 { |
|
2372 retval = retval * retval; |
|
2373 sqpow--; |
|
2374 } |
3331
|
2375 |
1819
|
2376 // Reverse preconditioning step 2: inverse balancing. |
3466
|
2377 // apply inverse scaling to computed exponential |
5275
|
2378 for (octave_idx_type i = 0; i < nc; i++) |
|
2379 for (octave_idx_type j = 0; j < nc; j++) |
3468
|
2380 retval(i,j) *= dscale(i) / dscale(j); |
3466
|
2381 |
4153
|
2382 OCTAVE_QUIT; |
|
2383 |
3466
|
2384 // construct balancing permutation vector |
5275
|
2385 Array<octave_idx_type> iperm (nc); |
|
2386 for (octave_idx_type i = 0; i < nc; i++) |
4593
|
2387 iperm(i) = i; // identity permutation |
3466
|
2388 |
|
2389 // leading permutations in forward order |
5275
|
2390 for (octave_idx_type i = 0; i < (ilo-1); i++) |
3468
|
2391 { |
5275
|
2392 octave_idx_type swapidx = static_cast<octave_idx_type> (dpermute(i)) - 1; |
|
2393 octave_idx_type tmp = iperm(i); |
4593
|
2394 iperm(i) = iperm (swapidx); |
|
2395 iperm(swapidx) = tmp; |
3468
|
2396 } |
3466
|
2397 |
|
2398 // trailing permutations must be done in reverse order |
5275
|
2399 for (octave_idx_type i = nc - 1; i >= ihi; i--) |
3468
|
2400 { |
5275
|
2401 octave_idx_type swapidx = static_cast<octave_idx_type> (dpermute(i)) - 1; |
|
2402 octave_idx_type tmp = iperm(i); |
4593
|
2403 iperm(i) = iperm(swapidx); |
|
2404 iperm(swapidx) = tmp; |
3468
|
2405 } |
3466
|
2406 |
|
2407 // construct inverse balancing permutation vector |
5275
|
2408 Array<octave_idx_type> invpvec (nc); |
|
2409 for (octave_idx_type i = 0; i < nc; i++) |
4593
|
2410 invpvec(iperm(i)) = i; // Thanks to R. A. Lippert for this method |
4153
|
2411 |
|
2412 OCTAVE_QUIT; |
3466
|
2413 |
|
2414 Matrix tmpMat = retval; |
5275
|
2415 for (octave_idx_type i = 0; i < nc; i++) |
|
2416 for (octave_idx_type j = 0; j < nc; j++) |
3468
|
2417 retval(i,j) = tmpMat(invpvec(i),invpvec(j)); |
3466
|
2418 |
1819
|
2419 // Reverse preconditioning step 1: fix trace normalization. |
3331
|
2420 |
3130
|
2421 if (trshift > 0.0) |
|
2422 retval = exp (trshift) * retval; |
|
2423 |
|
2424 return retval; |
1819
|
2425 } |
|
2426 |
458
|
2427 Matrix& |
|
2428 Matrix::operator += (const DiagMatrix& a) |
|
2429 { |
5275
|
2430 octave_idx_type nr = rows (); |
|
2431 octave_idx_type nc = cols (); |
|
2432 |
|
2433 octave_idx_type a_nr = a.rows (); |
|
2434 octave_idx_type a_nc = a.cols (); |
2385
|
2435 |
|
2436 if (nr != a_nr || nc != a_nc) |
458
|
2437 { |
2385
|
2438 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458
|
2439 return *this; |
|
2440 } |
|
2441 |
5275
|
2442 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
2443 elem (i, i) += a.elem (i, i); |
|
2444 |
|
2445 return *this; |
|
2446 } |
|
2447 |
|
2448 Matrix& |
|
2449 Matrix::operator -= (const DiagMatrix& a) |
|
2450 { |
5275
|
2451 octave_idx_type nr = rows (); |
|
2452 octave_idx_type nc = cols (); |
|
2453 |
|
2454 octave_idx_type a_nr = a.rows (); |
|
2455 octave_idx_type a_nc = a.cols (); |
2385
|
2456 |
|
2457 if (nr != a_nr || nc != a_nc) |
458
|
2458 { |
2385
|
2459 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458
|
2460 return *this; |
|
2461 } |
|
2462 |
5275
|
2463 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
2464 elem (i, i) -= a.elem (i, i); |
|
2465 |
|
2466 return *this; |
|
2467 } |
|
2468 |
|
2469 // unary operations |
|
2470 |
2964
|
2471 boolMatrix |
458
|
2472 Matrix::operator ! (void) const |
|
2473 { |
5275
|
2474 octave_idx_type nr = rows (); |
|
2475 octave_idx_type nc = cols (); |
458
|
2476 |
2964
|
2477 boolMatrix b (nr, nc); |
458
|
2478 |
5275
|
2479 for (octave_idx_type j = 0; j < nc; j++) |
|
2480 for (octave_idx_type i = 0; i < nr; i++) |
458
|
2481 b.elem (i, j) = ! elem (i, j); |
|
2482 |
|
2483 return b; |
|
2484 } |
|
2485 |
1205
|
2486 // column vector by row vector -> matrix operations |
458
|
2487 |
1205
|
2488 Matrix |
|
2489 operator * (const ColumnVector& v, const RowVector& a) |
458
|
2490 { |
1948
|
2491 Matrix retval; |
|
2492 |
5275
|
2493 octave_idx_type len = v.length (); |
3233
|
2494 |
|
2495 if (len != 0) |
1205
|
2496 { |
5275
|
2497 octave_idx_type a_len = a.length (); |
3233
|
2498 |
|
2499 retval.resize (len, a_len); |
|
2500 double *c = retval.fortran_vec (); |
|
2501 |
4552
|
2502 F77_XFCN (dgemm, DGEMM, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
2503 F77_CONST_CHAR_ARG2 ("N", 1), |
|
2504 len, a_len, 1, 1.0, v.data (), len, |
|
2505 a.data (), 1, 0.0, c, len |
|
2506 F77_CHAR_ARG_LEN (1) |
|
2507 F77_CHAR_ARG_LEN (1))); |
3233
|
2508 |
|
2509 if (f77_exception_encountered) |
|
2510 (*current_liboctave_error_handler) |
|
2511 ("unrecoverable error in dgemm"); |
1205
|
2512 } |
458
|
2513 |
1948
|
2514 return retval; |
458
|
2515 } |
|
2516 |
|
2517 // other operations. |
|
2518 |
|
2519 Matrix |
2676
|
2520 Matrix::map (d_d_Mapper f) const |
1205
|
2521 { |
2676
|
2522 Matrix b (*this); |
|
2523 return b.apply (f); |
1205
|
2524 } |
|
2525 |
3248
|
2526 boolMatrix |
|
2527 Matrix::map (b_d_Mapper f) const |
|
2528 { |
5275
|
2529 octave_idx_type nr = rows (); |
|
2530 octave_idx_type nc = cols (); |
3248
|
2531 |
|
2532 boolMatrix retval (nr, nc); |
|
2533 |
5275
|
2534 for (octave_idx_type j = 0; j < nc; j++) |
|
2535 for (octave_idx_type i = 0; i < nr; i++) |
3248
|
2536 retval(i,j) = f (elem(i,j)); |
|
2537 |
|
2538 return retval; |
|
2539 } |
|
2540 |
2676
|
2541 Matrix& |
|
2542 Matrix::apply (d_d_Mapper f) |
458
|
2543 { |
|
2544 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2545 |
5275
|
2546 for (octave_idx_type i = 0; i < length (); i++) |
458
|
2547 d[i] = f (d[i]); |
2676
|
2548 |
|
2549 return *this; |
458
|
2550 } |
|
2551 |
2385
|
2552 bool |
4431
|
2553 Matrix::any_element_is_negative (bool neg_zero) const |
2385
|
2554 { |
5275
|
2555 octave_idx_type nel = nelem (); |
2385
|
2556 |
4431
|
2557 if (neg_zero) |
|
2558 { |
5275
|
2559 for (octave_idx_type i = 0; i < nel; i++) |
4634
|
2560 if (lo_ieee_signbit (elem (i))) |
|
2561 return true; |
4431
|
2562 } |
|
2563 else |
|
2564 { |
5275
|
2565 for (octave_idx_type i = 0; i < nel; i++) |
4634
|
2566 if (elem (i) < 0) |
|
2567 return true; |
4431
|
2568 } |
2385
|
2569 |
|
2570 return false; |
|
2571 } |
|
2572 |
|
2573 |
|
2574 bool |
|
2575 Matrix::any_element_is_inf_or_nan (void) const |
|
2576 { |
5275
|
2577 octave_idx_type nel = nelem (); |
|
2578 |
|
2579 for (octave_idx_type i = 0; i < nel; i++) |
4634
|
2580 { |
|
2581 double val = elem (i); |
|
2582 if (xisinf (val) || xisnan (val)) |
|
2583 return true; |
|
2584 } |
|
2585 |
|
2586 return false; |
2385
|
2587 } |
|
2588 |
|
2589 bool |
5943
|
2590 Matrix::any_element_not_one_or_zero (void) const |
|
2591 { |
|
2592 octave_idx_type nel = nelem (); |
|
2593 |
|
2594 for (octave_idx_type i = 0; i < nel; i++) |
|
2595 { |
|
2596 double val = elem (i); |
|
2597 if (val != 0 && val != 1) |
|
2598 return true; |
|
2599 } |
|
2600 |
|
2601 return false; |
|
2602 } |
|
2603 |
|
2604 bool |
2385
|
2605 Matrix::all_elements_are_int_or_inf_or_nan (void) const |
|
2606 { |
5275
|
2607 octave_idx_type nel = nelem (); |
|
2608 |
|
2609 for (octave_idx_type i = 0; i < nel; i++) |
4634
|
2610 { |
|
2611 double val = elem (i); |
|
2612 if (xisnan (val) || D_NINT (val) == val) |
|
2613 continue; |
|
2614 else |
|
2615 return false; |
|
2616 } |
2385
|
2617 |
|
2618 return true; |
|
2619 } |
|
2620 |
1968
|
2621 // Return nonzero if any element of M is not an integer. Also extract |
|
2622 // the largest and smallest values and return them in MAX_VAL and MIN_VAL. |
|
2623 |
2385
|
2624 bool |
1968
|
2625 Matrix::all_integers (double& max_val, double& min_val) const |
|
2626 { |
5275
|
2627 octave_idx_type nel = nelem (); |
4634
|
2628 |
|
2629 if (nel > 0) |
1968
|
2630 { |
4634
|
2631 max_val = elem (0); |
|
2632 min_val = elem (0); |
1968
|
2633 } |
|
2634 else |
2385
|
2635 return false; |
1968
|
2636 |
5275
|
2637 for (octave_idx_type i = 0; i < nel; i++) |
4634
|
2638 { |
|
2639 double val = elem (i); |
|
2640 |
|
2641 if (val > max_val) |
|
2642 max_val = val; |
|
2643 |
|
2644 if (val < min_val) |
|
2645 min_val = val; |
|
2646 |
|
2647 if (D_NINT (val) != val) |
|
2648 return false; |
|
2649 } |
2385
|
2650 |
|
2651 return true; |
1968
|
2652 } |
|
2653 |
2385
|
2654 bool |
1968
|
2655 Matrix::too_large_for_float (void) const |
|
2656 { |
5275
|
2657 octave_idx_type nel = nelem (); |
|
2658 |
|
2659 for (octave_idx_type i = 0; i < nel; i++) |
4634
|
2660 { |
|
2661 double val = elem (i); |
|
2662 |
5389
|
2663 if (! (xisnan (val) || xisinf (val)) |
5387
|
2664 && fabs (val) > FLT_MAX) |
4634
|
2665 return true; |
|
2666 } |
1968
|
2667 |
2385
|
2668 return false; |
1968
|
2669 } |
|
2670 |
5775
|
2671 // FIXME Do these really belong here? Maybe they should be |
4015
|
2672 // in a base class? |
458
|
2673 |
2832
|
2674 boolMatrix |
4015
|
2675 Matrix::all (int dim) const |
458
|
2676 { |
4015
|
2677 MX_ALL_OP (dim); |
458
|
2678 } |
|
2679 |
2832
|
2680 boolMatrix |
4015
|
2681 Matrix::any (int dim) const |
458
|
2682 { |
4015
|
2683 MX_ANY_OP (dim); |
458
|
2684 } |
|
2685 |
|
2686 Matrix |
3723
|
2687 Matrix::cumprod (int dim) const |
458
|
2688 { |
4015
|
2689 MX_CUMULATIVE_OP (Matrix, double, *=); |
458
|
2690 } |
|
2691 |
|
2692 Matrix |
3723
|
2693 Matrix::cumsum (int dim) const |
458
|
2694 { |
4015
|
2695 MX_CUMULATIVE_OP (Matrix, double, +=); |
458
|
2696 } |
|
2697 |
|
2698 Matrix |
3723
|
2699 Matrix::prod (int dim) const |
458
|
2700 { |
3864
|
2701 MX_REDUCTION_OP (Matrix, *=, 1.0, 1.0); |
458
|
2702 } |
|
2703 |
|
2704 Matrix |
3723
|
2705 Matrix::sum (int dim) const |
458
|
2706 { |
3864
|
2707 MX_REDUCTION_OP (Matrix, +=, 0.0, 0.0); |
458
|
2708 } |
|
2709 |
|
2710 Matrix |
3723
|
2711 Matrix::sumsq (int dim) const |
458
|
2712 { |
3864
|
2713 #define ROW_EXPR \ |
|
2714 double d = elem (i, j); \ |
|
2715 retval.elem (i, 0) += d * d |
|
2716 |
|
2717 #define COL_EXPR \ |
|
2718 double d = elem (i, j); \ |
|
2719 retval.elem (0, j) += d * d |
|
2720 |
|
2721 MX_BASE_REDUCTION_OP (Matrix, ROW_EXPR, COL_EXPR, 0.0, 0.0); |
|
2722 |
|
2723 #undef ROW_EXPR |
|
2724 #undef COL_EXPR |
458
|
2725 } |
|
2726 |
2385
|
2727 Matrix |
|
2728 Matrix::abs (void) const |
|
2729 { |
5275
|
2730 octave_idx_type nr = rows (); |
|
2731 octave_idx_type nc = cols (); |
2385
|
2732 |
|
2733 Matrix retval (nr, nc); |
|
2734 |
5275
|
2735 for (octave_idx_type j = 0; j < nc; j++) |
|
2736 for (octave_idx_type i = 0; i < nr; i++) |
2385
|
2737 retval (i, j) = fabs (elem (i, j)); |
|
2738 |
|
2739 return retval; |
|
2740 } |
|
2741 |
458
|
2742 ColumnVector |
|
2743 Matrix::diag (void) const |
|
2744 { |
|
2745 return diag (0); |
|
2746 } |
|
2747 |
|
2748 ColumnVector |
5275
|
2749 Matrix::diag (octave_idx_type k) const |
458
|
2750 { |
5275
|
2751 octave_idx_type nnr = rows (); |
|
2752 octave_idx_type nnc = cols (); |
458
|
2753 if (k > 0) |
|
2754 nnc -= k; |
|
2755 else if (k < 0) |
|
2756 nnr += k; |
|
2757 |
|
2758 ColumnVector d; |
|
2759 |
|
2760 if (nnr > 0 && nnc > 0) |
|
2761 { |
5275
|
2762 octave_idx_type ndiag = (nnr < nnc) ? nnr : nnc; |
458
|
2763 |
|
2764 d.resize (ndiag); |
|
2765 |
|
2766 if (k > 0) |
|
2767 { |
5275
|
2768 for (octave_idx_type i = 0; i < ndiag; i++) |
458
|
2769 d.elem (i) = elem (i, i+k); |
|
2770 } |
4509
|
2771 else if (k < 0) |
458
|
2772 { |
5275
|
2773 for (octave_idx_type i = 0; i < ndiag; i++) |
458
|
2774 d.elem (i) = elem (i-k, i); |
|
2775 } |
|
2776 else |
|
2777 { |
5275
|
2778 for (octave_idx_type i = 0; i < ndiag; i++) |
458
|
2779 d.elem (i) = elem (i, i); |
|
2780 } |
|
2781 } |
|
2782 else |
4513
|
2783 (*current_liboctave_error_handler) |
|
2784 ("diag: requested diagonal out of range"); |
458
|
2785 |
|
2786 return d; |
|
2787 } |
|
2788 |
|
2789 ColumnVector |
|
2790 Matrix::row_min (void) const |
|
2791 { |
5275
|
2792 Array<octave_idx_type> dummy_idx; |
4587
|
2793 return row_min (dummy_idx); |
458
|
2794 } |
|
2795 |
|
2796 ColumnVector |
5275
|
2797 Matrix::row_min (Array<octave_idx_type>& idx_arg) const |
458
|
2798 { |
|
2799 ColumnVector result; |
|
2800 |
5275
|
2801 octave_idx_type nr = rows (); |
|
2802 octave_idx_type nc = cols (); |
458
|
2803 |
|
2804 if (nr > 0 && nc > 0) |
|
2805 { |
|
2806 result.resize (nr); |
4587
|
2807 idx_arg.resize (nr); |
458
|
2808 |
5275
|
2809 for (octave_idx_type i = 0; i < nr; i++) |
458
|
2810 { |
5275
|
2811 octave_idx_type idx_j; |
4469
|
2812 |
|
2813 double tmp_min = octave_NaN; |
|
2814 |
|
2815 for (idx_j = 0; idx_j < nc; idx_j++) |
2354
|
2816 { |
4469
|
2817 tmp_min = elem (i, idx_j); |
|
2818 |
5389
|
2819 if (! xisnan (tmp_min)) |
4469
|
2820 break; |
|
2821 } |
|
2822 |
5275
|
2823 for (octave_idx_type j = idx_j+1; j < nc; j++) |
4469
|
2824 { |
|
2825 double tmp = elem (i, j); |
|
2826 |
5389
|
2827 if (xisnan (tmp)) |
4469
|
2828 continue; |
|
2829 else if (tmp < tmp_min) |
2354
|
2830 { |
4469
|
2831 idx_j = j; |
|
2832 tmp_min = tmp; |
2354
|
2833 } |
|
2834 } |
|
2835 |
4469
|
2836 result.elem (i) = tmp_min; |
5389
|
2837 idx_arg.elem (i) = xisnan (tmp_min) ? 0 : idx_j; |
458
|
2838 } |
|
2839 } |
|
2840 |
|
2841 return result; |
|
2842 } |
|
2843 |
|
2844 ColumnVector |
|
2845 Matrix::row_max (void) const |
|
2846 { |
5275
|
2847 Array<octave_idx_type> dummy_idx; |
4587
|
2848 return row_max (dummy_idx); |
458
|
2849 } |
|
2850 |
|
2851 ColumnVector |
5275
|
2852 Matrix::row_max (Array<octave_idx_type>& idx_arg) const |
458
|
2853 { |
|
2854 ColumnVector result; |
|
2855 |
5275
|
2856 octave_idx_type nr = rows (); |
|
2857 octave_idx_type nc = cols (); |
458
|
2858 |
|
2859 if (nr > 0 && nc > 0) |
|
2860 { |
|
2861 result.resize (nr); |
4587
|
2862 idx_arg.resize (nr); |
458
|
2863 |
5275
|
2864 for (octave_idx_type i = 0; i < nr; i++) |
458
|
2865 { |
5275
|
2866 octave_idx_type idx_j; |
4469
|
2867 |
|
2868 double tmp_max = octave_NaN; |
|
2869 |
|
2870 for (idx_j = 0; idx_j < nc; idx_j++) |
2354
|
2871 { |
4469
|
2872 tmp_max = elem (i, idx_j); |
|
2873 |
5389
|
2874 if (! xisnan (tmp_max)) |
4469
|
2875 break; |
|
2876 } |
|
2877 |
5275
|
2878 for (octave_idx_type j = idx_j+1; j < nc; j++) |
4469
|
2879 { |
|
2880 double tmp = elem (i, j); |
|
2881 |
5389
|
2882 if (xisnan (tmp)) |
4469
|
2883 continue; |
|
2884 else if (tmp > tmp_max) |
2354
|
2885 { |
4469
|
2886 idx_j = j; |
|
2887 tmp_max = tmp; |
2354
|
2888 } |
|
2889 } |
|
2890 |
4469
|
2891 result.elem (i) = tmp_max; |
5389
|
2892 idx_arg.elem (i) = xisnan (tmp_max) ? 0 : idx_j; |
458
|
2893 } |
|
2894 } |
|
2895 |
|
2896 return result; |
|
2897 } |
|
2898 |
|
2899 RowVector |
|
2900 Matrix::column_min (void) const |
|
2901 { |
5275
|
2902 Array<octave_idx_type> dummy_idx; |
4587
|
2903 return column_min (dummy_idx); |
458
|
2904 } |
2354
|
2905 |
458
|
2906 RowVector |
5275
|
2907 Matrix::column_min (Array<octave_idx_type>& idx_arg) const |
458
|
2908 { |
|
2909 RowVector result; |
|
2910 |
5275
|
2911 octave_idx_type nr = rows (); |
|
2912 octave_idx_type nc = cols (); |
458
|
2913 |
|
2914 if (nr > 0 && nc > 0) |
|
2915 { |
|
2916 result.resize (nc); |
4587
|
2917 idx_arg.resize (nc); |
458
|
2918 |
5275
|
2919 for (octave_idx_type j = 0; j < nc; j++) |
458
|
2920 { |
5275
|
2921 octave_idx_type idx_i; |
4469
|
2922 |
|
2923 double tmp_min = octave_NaN; |
|
2924 |
|
2925 for (idx_i = 0; idx_i < nr; idx_i++) |
2354
|
2926 { |
4469
|
2927 tmp_min = elem (idx_i, j); |
|
2928 |
5389
|
2929 if (! xisnan (tmp_min)) |
4469
|
2930 break; |
|
2931 } |
|
2932 |
5275
|
2933 for (octave_idx_type i = idx_i+1; i < nr; i++) |
4469
|
2934 { |
|
2935 double tmp = elem (i, j); |
|
2936 |
5389
|
2937 if (xisnan (tmp)) |
4469
|
2938 continue; |
|
2939 else if (tmp < tmp_min) |
2354
|
2940 { |
4469
|
2941 idx_i = i; |
|
2942 tmp_min = tmp; |
2354
|
2943 } |
|
2944 } |
|
2945 |
4469
|
2946 result.elem (j) = tmp_min; |
5389
|
2947 idx_arg.elem (j) = xisnan (tmp_min) ? 0 : idx_i; |
458
|
2948 } |
|
2949 } |
|
2950 |
|
2951 return result; |
|
2952 } |
|
2953 |
2354
|
2954 RowVector |
|
2955 Matrix::column_max (void) const |
|
2956 { |
5275
|
2957 Array<octave_idx_type> dummy_idx; |
4587
|
2958 return column_max (dummy_idx); |
2354
|
2959 } |
458
|
2960 |
|
2961 RowVector |
5275
|
2962 Matrix::column_max (Array<octave_idx_type>& idx_arg) const |
458
|
2963 { |
|
2964 RowVector result; |
|
2965 |
5275
|
2966 octave_idx_type nr = rows (); |
|
2967 octave_idx_type nc = cols (); |
458
|
2968 |
|
2969 if (nr > 0 && nc > 0) |
|
2970 { |
|
2971 result.resize (nc); |
4587
|
2972 idx_arg.resize (nc); |
458
|
2973 |
5275
|
2974 for (octave_idx_type j = 0; j < nc; j++) |
458
|
2975 { |
5275
|
2976 octave_idx_type idx_i; |
4469
|
2977 |
|
2978 double tmp_max = octave_NaN; |
|
2979 |
|
2980 for (idx_i = 0; idx_i < nr; idx_i++) |
2354
|
2981 { |
4469
|
2982 tmp_max = elem (idx_i, j); |
|
2983 |
5389
|
2984 if (! xisnan (tmp_max)) |
4469
|
2985 break; |
|
2986 } |
|
2987 |
5275
|
2988 for (octave_idx_type i = idx_i+1; i < nr; i++) |
4469
|
2989 { |
|
2990 double tmp = elem (i, j); |
|
2991 |
5389
|
2992 if (xisnan (tmp)) |
4469
|
2993 continue; |
|
2994 else if (tmp > tmp_max) |
2354
|
2995 { |
4469
|
2996 idx_i = i; |
|
2997 tmp_max = tmp; |
2354
|
2998 } |
|
2999 } |
|
3000 |
4469
|
3001 result.elem (j) = tmp_max; |
5389
|
3002 idx_arg.elem (j) = xisnan (tmp_max) ? 0 : idx_i; |
458
|
3003 } |
|
3004 } |
|
3005 |
|
3006 return result; |
|
3007 } |
|
3008 |
3504
|
3009 std::ostream& |
|
3010 operator << (std::ostream& os, const Matrix& a) |
458
|
3011 { |
5275
|
3012 for (octave_idx_type i = 0; i < a.rows (); i++) |
458
|
3013 { |
5275
|
3014 for (octave_idx_type j = 0; j < a.cols (); j++) |
4130
|
3015 { |
|
3016 os << " "; |
|
3017 octave_write_double (os, a.elem (i, j)); |
|
3018 } |
458
|
3019 os << "\n"; |
|
3020 } |
|
3021 return os; |
|
3022 } |
|
3023 |
3504
|
3024 std::istream& |
|
3025 operator >> (std::istream& is, Matrix& a) |
458
|
3026 { |
5275
|
3027 octave_idx_type nr = a.rows (); |
|
3028 octave_idx_type nc = a.cols (); |
458
|
3029 |
|
3030 if (nr < 1 || nc < 1) |
3504
|
3031 is.clear (std::ios::badbit); |
458
|
3032 else |
|
3033 { |
|
3034 double tmp; |
5275
|
3035 for (octave_idx_type i = 0; i < nr; i++) |
|
3036 for (octave_idx_type j = 0; j < nc; j++) |
458
|
3037 { |
4130
|
3038 tmp = octave_read_double (is); |
458
|
3039 if (is) |
|
3040 a.elem (i, j) = tmp; |
|
3041 else |
2795
|
3042 goto done; |
458
|
3043 } |
|
3044 } |
|
3045 |
2795
|
3046 done: |
|
3047 |
458
|
3048 return is; |
|
3049 } |
|
3050 |
1819
|
3051 Matrix |
|
3052 Givens (double x, double y) |
|
3053 { |
|
3054 double cc, s, temp_r; |
|
3055 |
3887
|
3056 F77_FUNC (dlartg, DLARTG) (x, y, cc, s, temp_r); |
1819
|
3057 |
|
3058 Matrix g (2, 2); |
|
3059 |
|
3060 g.elem (0, 0) = cc; |
|
3061 g.elem (1, 1) = cc; |
|
3062 g.elem (0, 1) = s; |
|
3063 g.elem (1, 0) = -s; |
|
3064 |
|
3065 return g; |
|
3066 } |
|
3067 |
|
3068 Matrix |
|
3069 Sylvester (const Matrix& a, const Matrix& b, const Matrix& c) |
|
3070 { |
|
3071 Matrix retval; |
|
3072 |
5775
|
3073 // FIXME -- need to check that a, b, and c are all the same |
1819
|
3074 // size. |
|
3075 |
|
3076 // Compute Schur decompositions. |
|
3077 |
|
3078 SCHUR as (a, "U"); |
|
3079 SCHUR bs (b, "U"); |
|
3080 |
|
3081 // Transform c to new coordinates. |
|
3082 |
|
3083 Matrix ua = as.unitary_matrix (); |
|
3084 Matrix sch_a = as.schur_matrix (); |
|
3085 |
|
3086 Matrix ub = bs.unitary_matrix (); |
|
3087 Matrix sch_b = bs.schur_matrix (); |
|
3088 |
|
3089 Matrix cx = ua.transpose () * c * ub; |
|
3090 |
|
3091 // Solve the sylvester equation, back-transform, and return the |
|
3092 // solution. |
|
3093 |
5275
|
3094 octave_idx_type a_nr = a.rows (); |
|
3095 octave_idx_type b_nr = b.rows (); |
1819
|
3096 |
|
3097 double scale; |
5275
|
3098 octave_idx_type info; |
1819
|
3099 |
1950
|
3100 double *pa = sch_a.fortran_vec (); |
|
3101 double *pb = sch_b.fortran_vec (); |
|
3102 double *px = cx.fortran_vec (); |
|
3103 |
4552
|
3104 F77_XFCN (dtrsyl, DTRSYL, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
3105 F77_CONST_CHAR_ARG2 ("N", 1), |
|
3106 1, a_nr, b_nr, pa, a_nr, pb, |
|
3107 b_nr, px, a_nr, scale, info |
|
3108 F77_CHAR_ARG_LEN (1) |
|
3109 F77_CHAR_ARG_LEN (1))); |
1950
|
3110 |
|
3111 |
|
3112 if (f77_exception_encountered) |
|
3113 (*current_liboctave_error_handler) ("unrecoverable error in dtrsyl"); |
|
3114 else |
|
3115 { |
5775
|
3116 // FIXME -- check info? |
1819
|
3117 |
1950
|
3118 retval = -ua*cx*ub.transpose (); |
|
3119 } |
1819
|
3120 |
|
3121 return retval; |
|
3122 } |
|
3123 |
2828
|
3124 // matrix by matrix -> matrix operations |
|
3125 |
6162
|
3126 /* Simple Dot Product, Matrix-Vector and Matrix-Matrix Unit tests |
|
3127 %!assert([1 2 3] * [ 4 ; 5 ; 6], 32, 1e-14) |
|
3128 %!assert([1 2 ; 3 4 ] * [5 ; 6], [17 ; 39 ], 1e-14) |
|
3129 %!assert([1 2 ; 3 4 ] * [5 6 ; 7 8], [19 22; 43 50], 1e-14) |
|
3130 */ |
|
3131 |
|
3132 /* Test some simple identities |
|
3133 %!shared M, cv, rv |
|
3134 %! M = randn(10,10); |
|
3135 %! cv = randn(10,1); |
|
3136 %! rv = randn(1,10); |
|
3137 %!assert([M*cv,M*cv],M*[cv,cv],1e-14) |
|
3138 %!assert([rv*M;rv*M],[rv;rv]*M,1e-14) |
|
3139 %!assert(2*rv*cv,[rv,rv]*[cv;cv],1e-14) |
|
3140 */ |
|
3141 |
|
3142 |
2828
|
3143 Matrix |
|
3144 operator * (const Matrix& m, const Matrix& a) |
|
3145 { |
|
3146 Matrix retval; |
|
3147 |
5275
|
3148 octave_idx_type nr = m.rows (); |
|
3149 octave_idx_type nc = m.cols (); |
|
3150 |
|
3151 octave_idx_type a_nr = a.rows (); |
|
3152 octave_idx_type a_nc = a.cols (); |
2828
|
3153 |
|
3154 if (nc != a_nr) |
|
3155 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
|
3156 else |
|
3157 { |
|
3158 if (nr == 0 || nc == 0 || a_nc == 0) |
|
3159 retval.resize (nr, a_nc, 0.0); |
|
3160 else |
|
3161 { |
5275
|
3162 octave_idx_type ld = nr; |
|
3163 octave_idx_type lda = a_nr; |
2828
|
3164 |
|
3165 retval.resize (nr, a_nc); |
|
3166 double *c = retval.fortran_vec (); |
|
3167 |
5983
|
3168 if (a_nc == 1) |
|
3169 { |
|
3170 if (nr == 1) |
|
3171 F77_FUNC (xddot, XDDOT) (nc, m.data (), 1, a.data (), 1, *c); |
|
3172 else |
6390
|
3173 { |
|
3174 F77_XFCN (dgemv, DGEMV, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
3175 nr, nc, 1.0, m.data (), ld, |
|
3176 a.data (), 1, 0.0, c, 1 |
|
3177 F77_CHAR_ARG_LEN (1))); |
|
3178 |
|
3179 if (f77_exception_encountered) |
|
3180 (*current_liboctave_error_handler) |
|
3181 ("unrecoverable error in dgemv"); |
|
3182 } |
5983
|
3183 } |
|
3184 else |
6390
|
3185 { |
|
3186 F77_XFCN (dgemm, DGEMM, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
3187 F77_CONST_CHAR_ARG2 ("N", 1), |
|
3188 nr, a_nc, nc, 1.0, m.data (), |
|
3189 ld, a.data (), lda, 0.0, c, nr |
|
3190 F77_CHAR_ARG_LEN (1) |
|
3191 F77_CHAR_ARG_LEN (1))); |
|
3192 |
|
3193 if (f77_exception_encountered) |
|
3194 (*current_liboctave_error_handler) |
|
3195 ("unrecoverable error in dgemm"); |
|
3196 } |
2828
|
3197 } |
|
3198 } |
|
3199 |
|
3200 return retval; |
|
3201 } |
|
3202 |
5775
|
3203 // FIXME -- it would be nice to share code among the min/max |
4309
|
3204 // functions below. |
|
3205 |
|
3206 #define EMPTY_RETURN_CHECK(T) \ |
|
3207 if (nr == 0 || nc == 0) \ |
|
3208 return T (nr, nc); |
|
3209 |
|
3210 Matrix |
|
3211 min (double d, const Matrix& m) |
|
3212 { |
5275
|
3213 octave_idx_type nr = m.rows (); |
|
3214 octave_idx_type nc = m.columns (); |
4309
|
3215 |
|
3216 EMPTY_RETURN_CHECK (Matrix); |
|
3217 |
|
3218 Matrix result (nr, nc); |
|
3219 |
5275
|
3220 for (octave_idx_type j = 0; j < nc; j++) |
|
3221 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3222 { |
|
3223 OCTAVE_QUIT; |
|
3224 result (i, j) = xmin (d, m (i, j)); |
|
3225 } |
|
3226 |
|
3227 return result; |
|
3228 } |
|
3229 |
|
3230 Matrix |
|
3231 min (const Matrix& m, double d) |
|
3232 { |
5275
|
3233 octave_idx_type nr = m.rows (); |
|
3234 octave_idx_type nc = m.columns (); |
4309
|
3235 |
|
3236 EMPTY_RETURN_CHECK (Matrix); |
|
3237 |
|
3238 Matrix result (nr, nc); |
|
3239 |
5275
|
3240 for (octave_idx_type j = 0; j < nc; j++) |
|
3241 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3242 { |
|
3243 OCTAVE_QUIT; |
|
3244 result (i, j) = xmin (m (i, j), d); |
|
3245 } |
|
3246 |
|
3247 return result; |
|
3248 } |
|
3249 |
|
3250 Matrix |
|
3251 min (const Matrix& a, const Matrix& b) |
|
3252 { |
5275
|
3253 octave_idx_type nr = a.rows (); |
|
3254 octave_idx_type nc = a.columns (); |
4309
|
3255 |
|
3256 if (nr != b.rows () || nc != b.columns ()) |
|
3257 { |
|
3258 (*current_liboctave_error_handler) |
|
3259 ("two-arg min expecting args of same size"); |
|
3260 return Matrix (); |
|
3261 } |
|
3262 |
|
3263 EMPTY_RETURN_CHECK (Matrix); |
|
3264 |
|
3265 Matrix result (nr, nc); |
|
3266 |
5275
|
3267 for (octave_idx_type j = 0; j < nc; j++) |
|
3268 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3269 { |
|
3270 OCTAVE_QUIT; |
|
3271 result (i, j) = xmin (a (i, j), b (i, j)); |
|
3272 } |
|
3273 |
|
3274 return result; |
|
3275 } |
|
3276 |
|
3277 Matrix |
|
3278 max (double d, const Matrix& m) |
|
3279 { |
5275
|
3280 octave_idx_type nr = m.rows (); |
|
3281 octave_idx_type nc = m.columns (); |
4309
|
3282 |
|
3283 EMPTY_RETURN_CHECK (Matrix); |
|
3284 |
|
3285 Matrix result (nr, nc); |
|
3286 |
5275
|
3287 for (octave_idx_type j = 0; j < nc; j++) |
|
3288 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3289 { |
|
3290 OCTAVE_QUIT; |
|
3291 result (i, j) = xmax (d, m (i, j)); |
|
3292 } |
|
3293 |
|
3294 return result; |
|
3295 } |
|
3296 |
|
3297 Matrix |
|
3298 max (const Matrix& m, double d) |
|
3299 { |
5275
|
3300 octave_idx_type nr = m.rows (); |
|
3301 octave_idx_type nc = m.columns (); |
4309
|
3302 |
|
3303 EMPTY_RETURN_CHECK (Matrix); |
|
3304 |
|
3305 Matrix result (nr, nc); |
|
3306 |
5275
|
3307 for (octave_idx_type j = 0; j < nc; j++) |
|
3308 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3309 { |
|
3310 OCTAVE_QUIT; |
|
3311 result (i, j) = xmax (m (i, j), d); |
|
3312 } |
|
3313 |
|
3314 return result; |
|
3315 } |
|
3316 |
|
3317 Matrix |
|
3318 max (const Matrix& a, const Matrix& b) |
|
3319 { |
5275
|
3320 octave_idx_type nr = a.rows (); |
|
3321 octave_idx_type nc = a.columns (); |
4309
|
3322 |
|
3323 if (nr != b.rows () || nc != b.columns ()) |
|
3324 { |
|
3325 (*current_liboctave_error_handler) |
|
3326 ("two-arg max expecting args of same size"); |
|
3327 return Matrix (); |
|
3328 } |
|
3329 |
|
3330 EMPTY_RETURN_CHECK (Matrix); |
|
3331 |
|
3332 Matrix result (nr, nc); |
|
3333 |
5275
|
3334 for (octave_idx_type j = 0; j < nc; j++) |
|
3335 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3336 { |
|
3337 OCTAVE_QUIT; |
|
3338 result (i, j) = xmax (a (i, j), b (i, j)); |
|
3339 } |
|
3340 |
|
3341 return result; |
|
3342 } |
|
3343 |
2870
|
3344 MS_CMP_OPS(Matrix, , double, ) |
3504
|
3345 MS_BOOL_OPS(Matrix, double, 0.0) |
2870
|
3346 |
|
3347 SM_CMP_OPS(double, , Matrix, ) |
3504
|
3348 SM_BOOL_OPS(double, Matrix, 0.0) |
2870
|
3349 |
|
3350 MM_CMP_OPS(Matrix, , Matrix, ) |
3504
|
3351 MM_BOOL_OPS(Matrix, Matrix, 0.0) |
2870
|
3352 |
458
|
3353 /* |
|
3354 ;;; Local Variables: *** |
|
3355 ;;; mode: C++ *** |
|
3356 ;;; End: *** |
|
3357 */ |