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