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