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