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? |
7079
|
2107 octave_idx_type smlsiz = 25; |
|
2108 |
|
2109 // We compute the size of iwork because DGELSD in older versions |
|
2110 // of LAPACK does not return it on a query call. |
7124
|
2111 double dminmn = static_cast<double> (minmn); |
|
2112 double dsmlsizp1 = static_cast<double> (smlsiz+1); |
7079
|
2113 #if defined (HAVE_LOG2) |
7124
|
2114 double tmp = log2 (dminmn) / dsmlsizp1 + 1; |
7079
|
2115 #else |
7124
|
2116 double tmp = log (dminmn) / dsmlsizp1 / log (2.0) + 1; |
7079
|
2117 #endif |
|
2118 octave_idx_type nlvl = static_cast<int> (tmp); |
|
2119 if (nlvl < 0) |
|
2120 nlvl = 0; |
|
2121 |
|
2122 octave_idx_type liwork = 3 * minmn * nlvl + 11 * minmn; |
|
2123 if (liwork < 1) |
|
2124 liwork = 1; |
7072
|
2125 Array<octave_idx_type> iwork (liwork); |
|
2126 octave_idx_type* piwork = iwork.fortran_vec (); |
|
2127 |
|
2128 F77_XFCN (dgelsd, DGELSD, (m, n, nrhs, tmp_data, m, pretval, maxmn, |
|
2129 ps, rcond, rank, work.fortran_vec (), |
|
2130 lwork, piwork, info)); |
1948
|
2131 |
|
2132 if (f77_exception_encountered) |
7072
|
2133 (*current_liboctave_error_handler) |
|
2134 ("unrecoverable error in dgelsd"); |
1948
|
2135 else |
|
2136 { |
5275
|
2137 lwork = static_cast<octave_idx_type> (work(0)); |
3752
|
2138 work.resize (lwork); |
|
2139 |
7072
|
2140 F77_XFCN (dgelsd, DGELSD, (m, n, nrhs, tmp_data, m, pretval, |
|
2141 maxmn, ps, rcond, rank, |
|
2142 work.fortran_vec (), lwork, |
|
2143 piwork, info)); |
3752
|
2144 |
|
2145 if (f77_exception_encountered) |
7072
|
2146 (*current_liboctave_error_handler) |
|
2147 ("unrecoverable error in dgelsd"); |
7076
|
2148 else |
|
2149 { |
|
2150 if (rank < minmn) |
|
2151 (*current_liboctave_warning_handler) |
|
2152 ("dgelsd: rank deficient %dx%d matrix, rank = %d", m, n, rank); |
|
2153 if (s.elem (0) == 0.0) |
|
2154 rcond = 0.0; |
|
2155 else |
|
2156 rcond = s.elem (minmn - 1) / s.elem (0); |
7079
|
2157 |
|
2158 retval.resize (n, nrhs); |
7076
|
2159 } |
1948
|
2160 } |
458
|
2161 } |
|
2162 |
|
2163 return retval; |
|
2164 } |
|
2165 |
|
2166 ComplexMatrix |
|
2167 Matrix::lssolve (const ComplexMatrix& b) const |
|
2168 { |
|
2169 ComplexMatrix tmp (*this); |
5275
|
2170 octave_idx_type info; |
|
2171 octave_idx_type rank; |
7076
|
2172 double rcond; |
|
2173 return tmp.lssolve (b, info, rank, rcond); |
458
|
2174 } |
|
2175 |
|
2176 ComplexMatrix |
5275
|
2177 Matrix::lssolve (const ComplexMatrix& b, octave_idx_type& info) const |
458
|
2178 { |
|
2179 ComplexMatrix tmp (*this); |
5275
|
2180 octave_idx_type rank; |
7076
|
2181 double rcond; |
|
2182 return tmp.lssolve (b, info, rank, rcond); |
458
|
2183 } |
|
2184 |
|
2185 ComplexMatrix |
7076
|
2186 Matrix::lssolve (const ComplexMatrix& b, octave_idx_type& info, |
|
2187 octave_idx_type& rank) const |
458
|
2188 { |
|
2189 ComplexMatrix tmp (*this); |
7076
|
2190 double rcond; |
|
2191 return tmp.lssolve (b, info, rank, rcond); |
|
2192 } |
|
2193 |
|
2194 ComplexMatrix |
|
2195 Matrix::lssolve (const ComplexMatrix& b, octave_idx_type& info, |
|
2196 octave_idx_type& rank, double& rcond) const |
|
2197 { |
|
2198 ComplexMatrix tmp (*this); |
|
2199 return tmp.lssolve (b, info, rank, rcond); |
458
|
2200 } |
|
2201 |
|
2202 ColumnVector |
|
2203 Matrix::lssolve (const ColumnVector& b) const |
|
2204 { |
5275
|
2205 octave_idx_type info; |
|
2206 octave_idx_type rank; |
7076
|
2207 double rcond; |
|
2208 return lssolve (b, info, rank, rcond); |
458
|
2209 } |
|
2210 |
|
2211 ColumnVector |
5275
|
2212 Matrix::lssolve (const ColumnVector& b, octave_idx_type& info) const |
458
|
2213 { |
5275
|
2214 octave_idx_type rank; |
7076
|
2215 double rcond; |
|
2216 return lssolve (b, info, rank, rcond); |
458
|
2217 } |
|
2218 |
|
2219 ColumnVector |
7072
|
2220 Matrix::lssolve (const ColumnVector& b, octave_idx_type& info, |
|
2221 octave_idx_type& rank) const |
458
|
2222 { |
7076
|
2223 double rcond; |
|
2224 return lssolve (b, info, rank, rcond); |
|
2225 } |
|
2226 |
|
2227 ColumnVector |
|
2228 Matrix::lssolve (const ColumnVector& b, octave_idx_type& info, |
|
2229 octave_idx_type& rank, double &rcond) const |
|
2230 { |
1948
|
2231 ColumnVector retval; |
|
2232 |
5275
|
2233 octave_idx_type nrhs = 1; |
|
2234 |
|
2235 octave_idx_type m = rows (); |
|
2236 octave_idx_type n = cols (); |
458
|
2237 |
6924
|
2238 if (m != b.length ()) |
1948
|
2239 (*current_liboctave_error_handler) |
6924
|
2240 ("matrix dimension mismatch solution of linear equations"); |
|
2241 else if (m == 0 || n == 0) |
|
2242 retval = ColumnVector (n, 0.0); |
1948
|
2243 else |
458
|
2244 { |
7072
|
2245 volatile octave_idx_type minmn = (m < n ? m : n); |
|
2246 octave_idx_type maxmn = m > n ? m : n; |
7076
|
2247 rcond = -1.0; |
7072
|
2248 |
|
2249 if (m != n) |
|
2250 { |
|
2251 retval = ColumnVector (maxmn, 0.0); |
|
2252 |
|
2253 for (octave_idx_type i = 0; i < m; i++) |
|
2254 retval.elem (i) = b.elem (i); |
|
2255 } |
|
2256 else |
|
2257 retval = b; |
|
2258 |
1948
|
2259 Matrix atmp = *this; |
|
2260 double *tmp_data = atmp.fortran_vec (); |
|
2261 |
7072
|
2262 double *pretval = retval.fortran_vec (); |
|
2263 Array<double> s (minmn); |
7071
|
2264 double *ps = s.fortran_vec (); |
1948
|
2265 |
7072
|
2266 // Ask DGELSD what the dimension of WORK should be. |
5275
|
2267 octave_idx_type lwork = -1; |
3752
|
2268 |
|
2269 Array<double> work (1); |
|
2270 |
7072
|
2271 // FIXME: Can SMLSIZ be other than 25? |
7079
|
2272 octave_idx_type smlsiz = 25; |
|
2273 |
|
2274 // We compute the size of iwork because DGELSD in older versions |
|
2275 // of LAPACK does not return it on a query call. |
7124
|
2276 double dminmn = static_cast<double> (minmn); |
|
2277 double dsmlsizp1 = static_cast<double> (smlsiz+1); |
7079
|
2278 #if defined (HAVE_LOG2) |
7124
|
2279 double tmp = log2 (dminmn) / dsmlsizp1 + 1; |
7079
|
2280 #else |
7124
|
2281 double tmp = log (dminmn) / dsmlsizp1 / log (2.0) + 1; |
7079
|
2282 #endif |
|
2283 octave_idx_type nlvl = static_cast<int> (tmp); |
|
2284 if (nlvl < 0) |
|
2285 nlvl = 0; |
|
2286 |
|
2287 octave_idx_type liwork = 3 * minmn * nlvl + 11 * minmn; |
|
2288 if (liwork < 1) |
|
2289 liwork = 1; |
7072
|
2290 Array<octave_idx_type> iwork (liwork); |
|
2291 octave_idx_type* piwork = iwork.fortran_vec (); |
|
2292 |
|
2293 F77_XFCN (dgelsd, DGELSD, (m, n, nrhs, tmp_data, m, pretval, maxmn, |
|
2294 ps, rcond, rank, work.fortran_vec (), |
|
2295 lwork, piwork, info)); |
1948
|
2296 |
|
2297 if (f77_exception_encountered) |
7072
|
2298 (*current_liboctave_error_handler) |
|
2299 ("unrecoverable error in dgelsd"); |
1948
|
2300 else |
|
2301 { |
5275
|
2302 lwork = static_cast<octave_idx_type> (work(0)); |
3752
|
2303 work.resize (lwork); |
|
2304 |
7072
|
2305 F77_XFCN (dgelsd, DGELSD, (m, n, nrhs, tmp_data, m, pretval, |
|
2306 maxmn, ps, rcond, rank, |
|
2307 work.fortran_vec (), lwork, |
|
2308 piwork, info)); |
3752
|
2309 |
|
2310 if (f77_exception_encountered) |
7072
|
2311 (*current_liboctave_error_handler) |
|
2312 ("unrecoverable error in dgelsd"); |
|
2313 else if (rank < minmn) |
7076
|
2314 { |
|
2315 if (rank < minmn) |
|
2316 (*current_liboctave_warning_handler) |
|
2317 ("dgelsd: rank deficient %dx%d matrix, rank = %d", m, n, rank); |
|
2318 if (s.elem (0) == 0.0) |
|
2319 rcond = 0.0; |
|
2320 else |
|
2321 rcond = s.elem (minmn - 1) / s.elem (0); |
|
2322 } |
7079
|
2323 |
|
2324 retval.resize (n, nrhs); |
1948
|
2325 } |
458
|
2326 } |
|
2327 |
|
2328 return retval; |
|
2329 } |
|
2330 |
|
2331 ComplexColumnVector |
|
2332 Matrix::lssolve (const ComplexColumnVector& b) const |
|
2333 { |
|
2334 ComplexMatrix tmp (*this); |
7076
|
2335 octave_idx_type info; |
|
2336 octave_idx_type rank; |
|
2337 double rcond; |
|
2338 return tmp.lssolve (b, info, rank, rcond); |
458
|
2339 } |
|
2340 |
|
2341 ComplexColumnVector |
5275
|
2342 Matrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info) const |
458
|
2343 { |
|
2344 ComplexMatrix tmp (*this); |
7076
|
2345 octave_idx_type rank; |
|
2346 double rcond; |
|
2347 return tmp.lssolve (b, info, rank, rcond); |
458
|
2348 } |
|
2349 |
|
2350 ComplexColumnVector |
7076
|
2351 Matrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info, |
|
2352 octave_idx_type& rank) const |
458
|
2353 { |
|
2354 ComplexMatrix tmp (*this); |
7076
|
2355 double rcond; |
|
2356 return tmp.lssolve (b, info, rank, rcond); |
|
2357 } |
|
2358 |
|
2359 ComplexColumnVector |
|
2360 Matrix::lssolve (const ComplexColumnVector& b, octave_idx_type& info, |
|
2361 octave_idx_type& rank, double &rcond) const |
|
2362 { |
|
2363 ComplexMatrix tmp (*this); |
|
2364 return tmp.lssolve (b, info, rank, rcond); |
458
|
2365 } |
|
2366 |
1819
|
2367 // Constants for matrix exponential calculation. |
|
2368 |
|
2369 static double padec [] = |
|
2370 { |
|
2371 5.0000000000000000e-1, |
|
2372 1.1666666666666667e-1, |
|
2373 1.6666666666666667e-2, |
|
2374 1.6025641025641026e-3, |
|
2375 1.0683760683760684e-4, |
|
2376 4.8562548562548563e-6, |
|
2377 1.3875013875013875e-7, |
|
2378 1.9270852604185938e-9, |
|
2379 }; |
|
2380 |
|
2381 Matrix |
|
2382 Matrix::expm (void) const |
|
2383 { |
|
2384 Matrix retval; |
|
2385 |
|
2386 Matrix m = *this; |
|
2387 |
6699
|
2388 if (numel () == 1) |
|
2389 return Matrix (1, 1, exp (m(0))); |
|
2390 |
5275
|
2391 octave_idx_type nc = columns (); |
1819
|
2392 |
3130
|
2393 // Preconditioning step 1: trace normalization to reduce dynamic |
|
2394 // range of poles, but avoid making stable eigenvalues unstable. |
|
2395 |
1819
|
2396 // trace shift value |
3331
|
2397 volatile double trshift = 0.0; |
1819
|
2398 |
5275
|
2399 for (octave_idx_type i = 0; i < nc; i++) |
1819
|
2400 trshift += m.elem (i, i); |
|
2401 |
|
2402 trshift /= nc; |
|
2403 |
3130
|
2404 if (trshift > 0.0) |
|
2405 { |
5275
|
2406 for (octave_idx_type i = 0; i < nc; i++) |
3130
|
2407 m.elem (i, i) -= trshift; |
|
2408 } |
1819
|
2409 |
3331
|
2410 // Preconditioning step 2: balancing; code follows development |
|
2411 // in AEPBAL |
|
2412 |
|
2413 double *p_m = m.fortran_vec (); |
|
2414 |
5275
|
2415 octave_idx_type info, ilo, ihi, ilos, ihis; |
3468
|
2416 Array<double> dpermute (nc); |
|
2417 Array<double> dscale (nc); |
3466
|
2418 |
3468
|
2419 // permutation first |
|
2420 char job = 'P'; |
4552
|
2421 F77_XFCN (dgebal, DGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
2422 nc, p_m, nc, ilo, ihi, |
|
2423 dpermute.fortran_vec (), info |
|
2424 F77_CHAR_ARG_LEN (1))); |
3466
|
2425 |
3468
|
2426 // then scaling |
|
2427 job = 'S'; |
4552
|
2428 F77_XFCN (dgebal, DGEBAL, (F77_CONST_CHAR_ARG2 (&job, 1), |
|
2429 nc, p_m, nc, ilos, ihis, |
|
2430 dscale.fortran_vec (), info |
|
2431 F77_CHAR_ARG_LEN (1))); |
3331
|
2432 |
|
2433 if (f77_exception_encountered) |
|
2434 { |
|
2435 (*current_liboctave_error_handler) ("unrecoverable error in dgebal"); |
|
2436 return retval; |
|
2437 } |
|
2438 |
1819
|
2439 // Preconditioning step 3: scaling. |
3331
|
2440 |
1819
|
2441 ColumnVector work(nc); |
3130
|
2442 double inf_norm; |
3331
|
2443 |
4552
|
2444 F77_XFCN (xdlange, XDLANGE, (F77_CONST_CHAR_ARG2 ("I", 1), |
|
2445 nc, nc, m.fortran_vec (), nc, |
|
2446 work.fortran_vec (), inf_norm |
|
2447 F77_CHAR_ARG_LEN (1))); |
3331
|
2448 |
|
2449 if (f77_exception_encountered) |
|
2450 { |
|
2451 (*current_liboctave_error_handler) ("unrecoverable error in dlange"); |
|
2452 return retval; |
|
2453 } |
1819
|
2454 |
5275
|
2455 octave_idx_type sqpow = static_cast<octave_idx_type> (inf_norm > 0.0 |
1819
|
2456 ? (1.0 + log (inf_norm) / log (2.0)) |
|
2457 : 0.0); |
3331
|
2458 |
1819
|
2459 // Check whether we need to square at all. |
3331
|
2460 |
1819
|
2461 if (sqpow < 0) |
|
2462 sqpow = 0; |
3331
|
2463 |
1819
|
2464 if (sqpow > 0) |
|
2465 { |
|
2466 double scale_factor = 1.0; |
5275
|
2467 for (octave_idx_type i = 0; i < sqpow; i++) |
1819
|
2468 scale_factor *= 2.0; |
3331
|
2469 |
1819
|
2470 m = m / scale_factor; |
|
2471 } |
3331
|
2472 |
1819
|
2473 // npp, dpp: pade' approx polynomial matrices. |
3331
|
2474 |
1819
|
2475 Matrix npp (nc, nc, 0.0); |
|
2476 Matrix dpp = npp; |
3331
|
2477 |
1819
|
2478 // Now powers a^8 ... a^1. |
3331
|
2479 |
5275
|
2480 octave_idx_type minus_one_j = -1; |
|
2481 for (octave_idx_type j = 7; j >= 0; j--) |
1819
|
2482 { |
3573
|
2483 npp = m * npp + padec[j] * m; |
|
2484 dpp = m * dpp + (minus_one_j * padec[j]) * m; |
1819
|
2485 minus_one_j *= -1; |
|
2486 } |
3331
|
2487 |
1819
|
2488 // Zero power. |
3331
|
2489 |
1819
|
2490 dpp = -dpp; |
5275
|
2491 for (octave_idx_type j = 0; j < nc; j++) |
1819
|
2492 { |
|
2493 npp.elem (j, j) += 1.0; |
|
2494 dpp.elem (j, j) += 1.0; |
|
2495 } |
3331
|
2496 |
1819
|
2497 // Compute pade approximation = inverse (dpp) * npp. |
|
2498 |
3331
|
2499 retval = dpp.solve (npp, info); |
|
2500 |
1819
|
2501 // Reverse preconditioning step 3: repeated squaring. |
3331
|
2502 |
1819
|
2503 while (sqpow) |
|
2504 { |
|
2505 retval = retval * retval; |
|
2506 sqpow--; |
|
2507 } |
3331
|
2508 |
1819
|
2509 // Reverse preconditioning step 2: inverse balancing. |
3466
|
2510 // apply inverse scaling to computed exponential |
5275
|
2511 for (octave_idx_type i = 0; i < nc; i++) |
|
2512 for (octave_idx_type j = 0; j < nc; j++) |
3468
|
2513 retval(i,j) *= dscale(i) / dscale(j); |
3466
|
2514 |
4153
|
2515 OCTAVE_QUIT; |
|
2516 |
3466
|
2517 // construct balancing permutation vector |
5275
|
2518 Array<octave_idx_type> iperm (nc); |
|
2519 for (octave_idx_type i = 0; i < nc; i++) |
4593
|
2520 iperm(i) = i; // identity permutation |
3466
|
2521 |
|
2522 // leading permutations in forward order |
5275
|
2523 for (octave_idx_type i = 0; i < (ilo-1); i++) |
3468
|
2524 { |
5275
|
2525 octave_idx_type swapidx = static_cast<octave_idx_type> (dpermute(i)) - 1; |
|
2526 octave_idx_type tmp = iperm(i); |
4593
|
2527 iperm(i) = iperm (swapidx); |
|
2528 iperm(swapidx) = tmp; |
3468
|
2529 } |
3466
|
2530 |
|
2531 // trailing permutations must be done in reverse order |
5275
|
2532 for (octave_idx_type i = nc - 1; i >= ihi; i--) |
3468
|
2533 { |
5275
|
2534 octave_idx_type swapidx = static_cast<octave_idx_type> (dpermute(i)) - 1; |
|
2535 octave_idx_type tmp = iperm(i); |
4593
|
2536 iperm(i) = iperm(swapidx); |
|
2537 iperm(swapidx) = tmp; |
3468
|
2538 } |
3466
|
2539 |
|
2540 // construct inverse balancing permutation vector |
5275
|
2541 Array<octave_idx_type> invpvec (nc); |
|
2542 for (octave_idx_type i = 0; i < nc; i++) |
4593
|
2543 invpvec(iperm(i)) = i; // Thanks to R. A. Lippert for this method |
4153
|
2544 |
|
2545 OCTAVE_QUIT; |
3466
|
2546 |
|
2547 Matrix tmpMat = retval; |
5275
|
2548 for (octave_idx_type i = 0; i < nc; i++) |
|
2549 for (octave_idx_type j = 0; j < nc; j++) |
3468
|
2550 retval(i,j) = tmpMat(invpvec(i),invpvec(j)); |
3466
|
2551 |
1819
|
2552 // Reverse preconditioning step 1: fix trace normalization. |
3331
|
2553 |
3130
|
2554 if (trshift > 0.0) |
|
2555 retval = exp (trshift) * retval; |
|
2556 |
|
2557 return retval; |
1819
|
2558 } |
|
2559 |
458
|
2560 Matrix& |
|
2561 Matrix::operator += (const DiagMatrix& a) |
|
2562 { |
5275
|
2563 octave_idx_type nr = rows (); |
|
2564 octave_idx_type nc = cols (); |
|
2565 |
|
2566 octave_idx_type a_nr = a.rows (); |
|
2567 octave_idx_type a_nc = a.cols (); |
2385
|
2568 |
|
2569 if (nr != a_nr || nc != a_nc) |
458
|
2570 { |
2385
|
2571 gripe_nonconformant ("operator +=", nr, nc, a_nr, a_nc); |
458
|
2572 return *this; |
|
2573 } |
|
2574 |
5275
|
2575 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
2576 elem (i, i) += a.elem (i, i); |
|
2577 |
|
2578 return *this; |
|
2579 } |
|
2580 |
|
2581 Matrix& |
|
2582 Matrix::operator -= (const DiagMatrix& a) |
|
2583 { |
5275
|
2584 octave_idx_type nr = rows (); |
|
2585 octave_idx_type nc = cols (); |
|
2586 |
|
2587 octave_idx_type a_nr = a.rows (); |
|
2588 octave_idx_type a_nc = a.cols (); |
2385
|
2589 |
|
2590 if (nr != a_nr || nc != a_nc) |
458
|
2591 { |
2385
|
2592 gripe_nonconformant ("operator -=", nr, nc, a_nr, a_nc); |
458
|
2593 return *this; |
|
2594 } |
|
2595 |
5275
|
2596 for (octave_idx_type i = 0; i < a.length (); i++) |
458
|
2597 elem (i, i) -= a.elem (i, i); |
|
2598 |
|
2599 return *this; |
|
2600 } |
|
2601 |
|
2602 // unary operations |
|
2603 |
2964
|
2604 boolMatrix |
458
|
2605 Matrix::operator ! (void) const |
|
2606 { |
5275
|
2607 octave_idx_type nr = rows (); |
|
2608 octave_idx_type nc = cols (); |
458
|
2609 |
2964
|
2610 boolMatrix b (nr, nc); |
458
|
2611 |
5275
|
2612 for (octave_idx_type j = 0; j < nc; j++) |
|
2613 for (octave_idx_type i = 0; i < nr; i++) |
458
|
2614 b.elem (i, j) = ! elem (i, j); |
|
2615 |
|
2616 return b; |
|
2617 } |
|
2618 |
1205
|
2619 // column vector by row vector -> matrix operations |
458
|
2620 |
1205
|
2621 Matrix |
|
2622 operator * (const ColumnVector& v, const RowVector& a) |
458
|
2623 { |
1948
|
2624 Matrix retval; |
|
2625 |
5275
|
2626 octave_idx_type len = v.length (); |
3233
|
2627 |
|
2628 if (len != 0) |
1205
|
2629 { |
5275
|
2630 octave_idx_type a_len = a.length (); |
3233
|
2631 |
|
2632 retval.resize (len, a_len); |
|
2633 double *c = retval.fortran_vec (); |
|
2634 |
4552
|
2635 F77_XFCN (dgemm, DGEMM, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
2636 F77_CONST_CHAR_ARG2 ("N", 1), |
|
2637 len, a_len, 1, 1.0, v.data (), len, |
|
2638 a.data (), 1, 0.0, c, len |
|
2639 F77_CHAR_ARG_LEN (1) |
|
2640 F77_CHAR_ARG_LEN (1))); |
3233
|
2641 |
|
2642 if (f77_exception_encountered) |
|
2643 (*current_liboctave_error_handler) |
|
2644 ("unrecoverable error in dgemm"); |
1205
|
2645 } |
458
|
2646 |
1948
|
2647 return retval; |
458
|
2648 } |
|
2649 |
|
2650 // other operations. |
|
2651 |
|
2652 Matrix |
2676
|
2653 Matrix::map (d_d_Mapper f) const |
1205
|
2654 { |
2676
|
2655 Matrix b (*this); |
|
2656 return b.apply (f); |
1205
|
2657 } |
|
2658 |
3248
|
2659 boolMatrix |
|
2660 Matrix::map (b_d_Mapper f) const |
|
2661 { |
5275
|
2662 octave_idx_type nr = rows (); |
|
2663 octave_idx_type nc = cols (); |
3248
|
2664 |
|
2665 boolMatrix retval (nr, nc); |
|
2666 |
5275
|
2667 for (octave_idx_type j = 0; j < nc; j++) |
|
2668 for (octave_idx_type i = 0; i < nr; i++) |
3248
|
2669 retval(i,j) = f (elem(i,j)); |
|
2670 |
|
2671 return retval; |
|
2672 } |
|
2673 |
2676
|
2674 Matrix& |
|
2675 Matrix::apply (d_d_Mapper f) |
458
|
2676 { |
|
2677 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
2678 |
5275
|
2679 for (octave_idx_type i = 0; i < length (); i++) |
458
|
2680 d[i] = f (d[i]); |
2676
|
2681 |
|
2682 return *this; |
458
|
2683 } |
|
2684 |
2385
|
2685 bool |
4431
|
2686 Matrix::any_element_is_negative (bool neg_zero) const |
2385
|
2687 { |
5275
|
2688 octave_idx_type nel = nelem (); |
2385
|
2689 |
4431
|
2690 if (neg_zero) |
|
2691 { |
5275
|
2692 for (octave_idx_type i = 0; i < nel; i++) |
4634
|
2693 if (lo_ieee_signbit (elem (i))) |
|
2694 return true; |
4431
|
2695 } |
|
2696 else |
|
2697 { |
5275
|
2698 for (octave_idx_type i = 0; i < nel; i++) |
4634
|
2699 if (elem (i) < 0) |
|
2700 return true; |
4431
|
2701 } |
2385
|
2702 |
|
2703 return false; |
|
2704 } |
|
2705 |
|
2706 |
|
2707 bool |
|
2708 Matrix::any_element_is_inf_or_nan (void) const |
|
2709 { |
5275
|
2710 octave_idx_type nel = nelem (); |
|
2711 |
|
2712 for (octave_idx_type i = 0; i < nel; i++) |
4634
|
2713 { |
|
2714 double val = elem (i); |
|
2715 if (xisinf (val) || xisnan (val)) |
|
2716 return true; |
|
2717 } |
|
2718 |
|
2719 return false; |
2385
|
2720 } |
|
2721 |
|
2722 bool |
5943
|
2723 Matrix::any_element_not_one_or_zero (void) const |
|
2724 { |
|
2725 octave_idx_type nel = nelem (); |
|
2726 |
|
2727 for (octave_idx_type i = 0; i < nel; i++) |
|
2728 { |
|
2729 double val = elem (i); |
|
2730 if (val != 0 && val != 1) |
|
2731 return true; |
|
2732 } |
|
2733 |
|
2734 return false; |
|
2735 } |
|
2736 |
|
2737 bool |
2385
|
2738 Matrix::all_elements_are_int_or_inf_or_nan (void) const |
|
2739 { |
5275
|
2740 octave_idx_type nel = nelem (); |
|
2741 |
|
2742 for (octave_idx_type i = 0; i < nel; i++) |
4634
|
2743 { |
|
2744 double val = elem (i); |
|
2745 if (xisnan (val) || D_NINT (val) == val) |
|
2746 continue; |
|
2747 else |
|
2748 return false; |
|
2749 } |
2385
|
2750 |
|
2751 return true; |
|
2752 } |
|
2753 |
1968
|
2754 // Return nonzero if any element of M is not an integer. Also extract |
|
2755 // the largest and smallest values and return them in MAX_VAL and MIN_VAL. |
|
2756 |
2385
|
2757 bool |
1968
|
2758 Matrix::all_integers (double& max_val, double& min_val) const |
|
2759 { |
5275
|
2760 octave_idx_type nel = nelem (); |
4634
|
2761 |
|
2762 if (nel > 0) |
1968
|
2763 { |
4634
|
2764 max_val = elem (0); |
|
2765 min_val = elem (0); |
1968
|
2766 } |
|
2767 else |
2385
|
2768 return false; |
1968
|
2769 |
5275
|
2770 for (octave_idx_type i = 0; i < nel; i++) |
4634
|
2771 { |
|
2772 double val = elem (i); |
|
2773 |
|
2774 if (val > max_val) |
|
2775 max_val = val; |
|
2776 |
|
2777 if (val < min_val) |
|
2778 min_val = val; |
|
2779 |
|
2780 if (D_NINT (val) != val) |
|
2781 return false; |
|
2782 } |
2385
|
2783 |
|
2784 return true; |
1968
|
2785 } |
|
2786 |
2385
|
2787 bool |
1968
|
2788 Matrix::too_large_for_float (void) const |
|
2789 { |
5275
|
2790 octave_idx_type nel = nelem (); |
|
2791 |
|
2792 for (octave_idx_type i = 0; i < nel; i++) |
4634
|
2793 { |
|
2794 double val = elem (i); |
|
2795 |
5389
|
2796 if (! (xisnan (val) || xisinf (val)) |
5387
|
2797 && fabs (val) > FLT_MAX) |
4634
|
2798 return true; |
|
2799 } |
1968
|
2800 |
2385
|
2801 return false; |
1968
|
2802 } |
|
2803 |
5775
|
2804 // FIXME Do these really belong here? Maybe they should be |
4015
|
2805 // in a base class? |
458
|
2806 |
2832
|
2807 boolMatrix |
4015
|
2808 Matrix::all (int dim) const |
458
|
2809 { |
4015
|
2810 MX_ALL_OP (dim); |
458
|
2811 } |
|
2812 |
2832
|
2813 boolMatrix |
4015
|
2814 Matrix::any (int dim) const |
458
|
2815 { |
4015
|
2816 MX_ANY_OP (dim); |
458
|
2817 } |
|
2818 |
|
2819 Matrix |
3723
|
2820 Matrix::cumprod (int dim) const |
458
|
2821 { |
4015
|
2822 MX_CUMULATIVE_OP (Matrix, double, *=); |
458
|
2823 } |
|
2824 |
|
2825 Matrix |
3723
|
2826 Matrix::cumsum (int dim) const |
458
|
2827 { |
4015
|
2828 MX_CUMULATIVE_OP (Matrix, double, +=); |
458
|
2829 } |
|
2830 |
|
2831 Matrix |
3723
|
2832 Matrix::prod (int dim) const |
458
|
2833 { |
3864
|
2834 MX_REDUCTION_OP (Matrix, *=, 1.0, 1.0); |
458
|
2835 } |
|
2836 |
|
2837 Matrix |
3723
|
2838 Matrix::sum (int dim) const |
458
|
2839 { |
3864
|
2840 MX_REDUCTION_OP (Matrix, +=, 0.0, 0.0); |
458
|
2841 } |
|
2842 |
|
2843 Matrix |
3723
|
2844 Matrix::sumsq (int dim) const |
458
|
2845 { |
3864
|
2846 #define ROW_EXPR \ |
|
2847 double d = elem (i, j); \ |
|
2848 retval.elem (i, 0) += d * d |
|
2849 |
|
2850 #define COL_EXPR \ |
|
2851 double d = elem (i, j); \ |
|
2852 retval.elem (0, j) += d * d |
|
2853 |
|
2854 MX_BASE_REDUCTION_OP (Matrix, ROW_EXPR, COL_EXPR, 0.0, 0.0); |
|
2855 |
|
2856 #undef ROW_EXPR |
|
2857 #undef COL_EXPR |
458
|
2858 } |
|
2859 |
2385
|
2860 Matrix |
|
2861 Matrix::abs (void) const |
|
2862 { |
5275
|
2863 octave_idx_type nr = rows (); |
|
2864 octave_idx_type nc = cols (); |
2385
|
2865 |
|
2866 Matrix retval (nr, nc); |
|
2867 |
5275
|
2868 for (octave_idx_type j = 0; j < nc; j++) |
|
2869 for (octave_idx_type i = 0; i < nr; i++) |
2385
|
2870 retval (i, j) = fabs (elem (i, j)); |
|
2871 |
|
2872 return retval; |
|
2873 } |
|
2874 |
458
|
2875 ColumnVector |
|
2876 Matrix::diag (void) const |
|
2877 { |
|
2878 return diag (0); |
|
2879 } |
|
2880 |
|
2881 ColumnVector |
5275
|
2882 Matrix::diag (octave_idx_type k) const |
458
|
2883 { |
5275
|
2884 octave_idx_type nnr = rows (); |
|
2885 octave_idx_type nnc = cols (); |
458
|
2886 if (k > 0) |
|
2887 nnc -= k; |
|
2888 else if (k < 0) |
|
2889 nnr += k; |
|
2890 |
|
2891 ColumnVector d; |
|
2892 |
|
2893 if (nnr > 0 && nnc > 0) |
|
2894 { |
5275
|
2895 octave_idx_type ndiag = (nnr < nnc) ? nnr : nnc; |
458
|
2896 |
|
2897 d.resize (ndiag); |
|
2898 |
|
2899 if (k > 0) |
|
2900 { |
5275
|
2901 for (octave_idx_type i = 0; i < ndiag; i++) |
458
|
2902 d.elem (i) = elem (i, i+k); |
|
2903 } |
4509
|
2904 else if (k < 0) |
458
|
2905 { |
5275
|
2906 for (octave_idx_type i = 0; i < ndiag; i++) |
458
|
2907 d.elem (i) = elem (i-k, i); |
|
2908 } |
|
2909 else |
|
2910 { |
5275
|
2911 for (octave_idx_type i = 0; i < ndiag; i++) |
458
|
2912 d.elem (i) = elem (i, i); |
|
2913 } |
|
2914 } |
|
2915 else |
4513
|
2916 (*current_liboctave_error_handler) |
|
2917 ("diag: requested diagonal out of range"); |
458
|
2918 |
|
2919 return d; |
|
2920 } |
|
2921 |
|
2922 ColumnVector |
|
2923 Matrix::row_min (void) const |
|
2924 { |
5275
|
2925 Array<octave_idx_type> dummy_idx; |
4587
|
2926 return row_min (dummy_idx); |
458
|
2927 } |
|
2928 |
|
2929 ColumnVector |
5275
|
2930 Matrix::row_min (Array<octave_idx_type>& idx_arg) const |
458
|
2931 { |
|
2932 ColumnVector result; |
|
2933 |
5275
|
2934 octave_idx_type nr = rows (); |
|
2935 octave_idx_type nc = cols (); |
458
|
2936 |
|
2937 if (nr > 0 && nc > 0) |
|
2938 { |
|
2939 result.resize (nr); |
4587
|
2940 idx_arg.resize (nr); |
458
|
2941 |
5275
|
2942 for (octave_idx_type i = 0; i < nr; i++) |
458
|
2943 { |
5275
|
2944 octave_idx_type idx_j; |
4469
|
2945 |
|
2946 double tmp_min = octave_NaN; |
|
2947 |
|
2948 for (idx_j = 0; idx_j < nc; idx_j++) |
2354
|
2949 { |
4469
|
2950 tmp_min = elem (i, idx_j); |
|
2951 |
5389
|
2952 if (! xisnan (tmp_min)) |
4469
|
2953 break; |
|
2954 } |
|
2955 |
5275
|
2956 for (octave_idx_type j = idx_j+1; j < nc; j++) |
4469
|
2957 { |
|
2958 double tmp = elem (i, j); |
|
2959 |
5389
|
2960 if (xisnan (tmp)) |
4469
|
2961 continue; |
|
2962 else if (tmp < tmp_min) |
2354
|
2963 { |
4469
|
2964 idx_j = j; |
|
2965 tmp_min = tmp; |
2354
|
2966 } |
|
2967 } |
|
2968 |
4469
|
2969 result.elem (i) = tmp_min; |
5389
|
2970 idx_arg.elem (i) = xisnan (tmp_min) ? 0 : idx_j; |
458
|
2971 } |
|
2972 } |
|
2973 |
|
2974 return result; |
|
2975 } |
|
2976 |
|
2977 ColumnVector |
|
2978 Matrix::row_max (void) const |
|
2979 { |
5275
|
2980 Array<octave_idx_type> dummy_idx; |
4587
|
2981 return row_max (dummy_idx); |
458
|
2982 } |
|
2983 |
|
2984 ColumnVector |
5275
|
2985 Matrix::row_max (Array<octave_idx_type>& idx_arg) const |
458
|
2986 { |
|
2987 ColumnVector result; |
|
2988 |
5275
|
2989 octave_idx_type nr = rows (); |
|
2990 octave_idx_type nc = cols (); |
458
|
2991 |
|
2992 if (nr > 0 && nc > 0) |
|
2993 { |
|
2994 result.resize (nr); |
4587
|
2995 idx_arg.resize (nr); |
458
|
2996 |
5275
|
2997 for (octave_idx_type i = 0; i < nr; i++) |
458
|
2998 { |
5275
|
2999 octave_idx_type idx_j; |
4469
|
3000 |
|
3001 double tmp_max = octave_NaN; |
|
3002 |
|
3003 for (idx_j = 0; idx_j < nc; idx_j++) |
2354
|
3004 { |
4469
|
3005 tmp_max = elem (i, idx_j); |
|
3006 |
5389
|
3007 if (! xisnan (tmp_max)) |
4469
|
3008 break; |
|
3009 } |
|
3010 |
5275
|
3011 for (octave_idx_type j = idx_j+1; j < nc; j++) |
4469
|
3012 { |
|
3013 double tmp = elem (i, j); |
|
3014 |
5389
|
3015 if (xisnan (tmp)) |
4469
|
3016 continue; |
|
3017 else if (tmp > tmp_max) |
2354
|
3018 { |
4469
|
3019 idx_j = j; |
|
3020 tmp_max = tmp; |
2354
|
3021 } |
|
3022 } |
|
3023 |
4469
|
3024 result.elem (i) = tmp_max; |
5389
|
3025 idx_arg.elem (i) = xisnan (tmp_max) ? 0 : idx_j; |
458
|
3026 } |
|
3027 } |
|
3028 |
|
3029 return result; |
|
3030 } |
|
3031 |
|
3032 RowVector |
|
3033 Matrix::column_min (void) const |
|
3034 { |
5275
|
3035 Array<octave_idx_type> dummy_idx; |
4587
|
3036 return column_min (dummy_idx); |
458
|
3037 } |
2354
|
3038 |
458
|
3039 RowVector |
5275
|
3040 Matrix::column_min (Array<octave_idx_type>& idx_arg) const |
458
|
3041 { |
|
3042 RowVector result; |
|
3043 |
5275
|
3044 octave_idx_type nr = rows (); |
|
3045 octave_idx_type nc = cols (); |
458
|
3046 |
|
3047 if (nr > 0 && nc > 0) |
|
3048 { |
|
3049 result.resize (nc); |
4587
|
3050 idx_arg.resize (nc); |
458
|
3051 |
5275
|
3052 for (octave_idx_type j = 0; j < nc; j++) |
458
|
3053 { |
5275
|
3054 octave_idx_type idx_i; |
4469
|
3055 |
|
3056 double tmp_min = octave_NaN; |
|
3057 |
|
3058 for (idx_i = 0; idx_i < nr; idx_i++) |
2354
|
3059 { |
4469
|
3060 tmp_min = elem (idx_i, j); |
|
3061 |
5389
|
3062 if (! xisnan (tmp_min)) |
4469
|
3063 break; |
|
3064 } |
|
3065 |
5275
|
3066 for (octave_idx_type i = idx_i+1; i < nr; i++) |
4469
|
3067 { |
|
3068 double tmp = elem (i, j); |
|
3069 |
5389
|
3070 if (xisnan (tmp)) |
4469
|
3071 continue; |
|
3072 else if (tmp < tmp_min) |
2354
|
3073 { |
4469
|
3074 idx_i = i; |
|
3075 tmp_min = tmp; |
2354
|
3076 } |
|
3077 } |
|
3078 |
4469
|
3079 result.elem (j) = tmp_min; |
5389
|
3080 idx_arg.elem (j) = xisnan (tmp_min) ? 0 : idx_i; |
458
|
3081 } |
|
3082 } |
|
3083 |
|
3084 return result; |
|
3085 } |
|
3086 |
2354
|
3087 RowVector |
|
3088 Matrix::column_max (void) const |
|
3089 { |
5275
|
3090 Array<octave_idx_type> dummy_idx; |
4587
|
3091 return column_max (dummy_idx); |
2354
|
3092 } |
458
|
3093 |
|
3094 RowVector |
5275
|
3095 Matrix::column_max (Array<octave_idx_type>& idx_arg) const |
458
|
3096 { |
|
3097 RowVector result; |
|
3098 |
5275
|
3099 octave_idx_type nr = rows (); |
|
3100 octave_idx_type nc = cols (); |
458
|
3101 |
|
3102 if (nr > 0 && nc > 0) |
|
3103 { |
|
3104 result.resize (nc); |
4587
|
3105 idx_arg.resize (nc); |
458
|
3106 |
5275
|
3107 for (octave_idx_type j = 0; j < nc; j++) |
458
|
3108 { |
5275
|
3109 octave_idx_type idx_i; |
4469
|
3110 |
|
3111 double tmp_max = octave_NaN; |
|
3112 |
|
3113 for (idx_i = 0; idx_i < nr; idx_i++) |
2354
|
3114 { |
4469
|
3115 tmp_max = elem (idx_i, j); |
|
3116 |
5389
|
3117 if (! xisnan (tmp_max)) |
4469
|
3118 break; |
|
3119 } |
|
3120 |
5275
|
3121 for (octave_idx_type i = idx_i+1; i < nr; i++) |
4469
|
3122 { |
|
3123 double tmp = elem (i, j); |
|
3124 |
5389
|
3125 if (xisnan (tmp)) |
4469
|
3126 continue; |
|
3127 else if (tmp > tmp_max) |
2354
|
3128 { |
4469
|
3129 idx_i = i; |
|
3130 tmp_max = tmp; |
2354
|
3131 } |
|
3132 } |
|
3133 |
4469
|
3134 result.elem (j) = tmp_max; |
5389
|
3135 idx_arg.elem (j) = xisnan (tmp_max) ? 0 : idx_i; |
458
|
3136 } |
|
3137 } |
|
3138 |
|
3139 return result; |
|
3140 } |
|
3141 |
3504
|
3142 std::ostream& |
|
3143 operator << (std::ostream& os, const Matrix& a) |
458
|
3144 { |
5275
|
3145 for (octave_idx_type i = 0; i < a.rows (); i++) |
458
|
3146 { |
5275
|
3147 for (octave_idx_type j = 0; j < a.cols (); j++) |
4130
|
3148 { |
|
3149 os << " "; |
|
3150 octave_write_double (os, a.elem (i, j)); |
|
3151 } |
458
|
3152 os << "\n"; |
|
3153 } |
|
3154 return os; |
|
3155 } |
|
3156 |
3504
|
3157 std::istream& |
|
3158 operator >> (std::istream& is, Matrix& a) |
458
|
3159 { |
5275
|
3160 octave_idx_type nr = a.rows (); |
|
3161 octave_idx_type nc = a.cols (); |
458
|
3162 |
|
3163 if (nr < 1 || nc < 1) |
3504
|
3164 is.clear (std::ios::badbit); |
458
|
3165 else |
|
3166 { |
|
3167 double tmp; |
5275
|
3168 for (octave_idx_type i = 0; i < nr; i++) |
|
3169 for (octave_idx_type j = 0; j < nc; j++) |
458
|
3170 { |
4130
|
3171 tmp = octave_read_double (is); |
458
|
3172 if (is) |
|
3173 a.elem (i, j) = tmp; |
|
3174 else |
2795
|
3175 goto done; |
458
|
3176 } |
|
3177 } |
|
3178 |
2795
|
3179 done: |
|
3180 |
458
|
3181 return is; |
|
3182 } |
|
3183 |
1819
|
3184 Matrix |
|
3185 Givens (double x, double y) |
|
3186 { |
|
3187 double cc, s, temp_r; |
|
3188 |
3887
|
3189 F77_FUNC (dlartg, DLARTG) (x, y, cc, s, temp_r); |
1819
|
3190 |
|
3191 Matrix g (2, 2); |
|
3192 |
|
3193 g.elem (0, 0) = cc; |
|
3194 g.elem (1, 1) = cc; |
|
3195 g.elem (0, 1) = s; |
|
3196 g.elem (1, 0) = -s; |
|
3197 |
|
3198 return g; |
|
3199 } |
|
3200 |
|
3201 Matrix |
|
3202 Sylvester (const Matrix& a, const Matrix& b, const Matrix& c) |
|
3203 { |
|
3204 Matrix retval; |
|
3205 |
5775
|
3206 // FIXME -- need to check that a, b, and c are all the same |
1819
|
3207 // size. |
|
3208 |
|
3209 // Compute Schur decompositions. |
|
3210 |
|
3211 SCHUR as (a, "U"); |
|
3212 SCHUR bs (b, "U"); |
|
3213 |
|
3214 // Transform c to new coordinates. |
|
3215 |
|
3216 Matrix ua = as.unitary_matrix (); |
|
3217 Matrix sch_a = as.schur_matrix (); |
|
3218 |
|
3219 Matrix ub = bs.unitary_matrix (); |
|
3220 Matrix sch_b = bs.schur_matrix (); |
|
3221 |
|
3222 Matrix cx = ua.transpose () * c * ub; |
|
3223 |
|
3224 // Solve the sylvester equation, back-transform, and return the |
|
3225 // solution. |
|
3226 |
5275
|
3227 octave_idx_type a_nr = a.rows (); |
|
3228 octave_idx_type b_nr = b.rows (); |
1819
|
3229 |
|
3230 double scale; |
5275
|
3231 octave_idx_type info; |
1819
|
3232 |
1950
|
3233 double *pa = sch_a.fortran_vec (); |
|
3234 double *pb = sch_b.fortran_vec (); |
|
3235 double *px = cx.fortran_vec (); |
|
3236 |
4552
|
3237 F77_XFCN (dtrsyl, DTRSYL, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
3238 F77_CONST_CHAR_ARG2 ("N", 1), |
|
3239 1, a_nr, b_nr, pa, a_nr, pb, |
|
3240 b_nr, px, a_nr, scale, info |
|
3241 F77_CHAR_ARG_LEN (1) |
|
3242 F77_CHAR_ARG_LEN (1))); |
1950
|
3243 |
|
3244 |
|
3245 if (f77_exception_encountered) |
|
3246 (*current_liboctave_error_handler) ("unrecoverable error in dtrsyl"); |
|
3247 else |
|
3248 { |
5775
|
3249 // FIXME -- check info? |
1819
|
3250 |
1950
|
3251 retval = -ua*cx*ub.transpose (); |
|
3252 } |
1819
|
3253 |
|
3254 return retval; |
|
3255 } |
|
3256 |
2828
|
3257 // matrix by matrix -> matrix operations |
|
3258 |
6162
|
3259 /* Simple Dot Product, Matrix-Vector and Matrix-Matrix Unit tests |
|
3260 %!assert([1 2 3] * [ 4 ; 5 ; 6], 32, 1e-14) |
|
3261 %!assert([1 2 ; 3 4 ] * [5 ; 6], [17 ; 39 ], 1e-14) |
|
3262 %!assert([1 2 ; 3 4 ] * [5 6 ; 7 8], [19 22; 43 50], 1e-14) |
|
3263 */ |
|
3264 |
|
3265 /* Test some simple identities |
|
3266 %!shared M, cv, rv |
|
3267 %! M = randn(10,10); |
|
3268 %! cv = randn(10,1); |
|
3269 %! rv = randn(1,10); |
|
3270 %!assert([M*cv,M*cv],M*[cv,cv],1e-14) |
|
3271 %!assert([rv*M;rv*M],[rv;rv]*M,1e-14) |
|
3272 %!assert(2*rv*cv,[rv,rv]*[cv;cv],1e-14) |
|
3273 */ |
|
3274 |
|
3275 |
2828
|
3276 Matrix |
|
3277 operator * (const Matrix& m, const Matrix& a) |
|
3278 { |
|
3279 Matrix retval; |
|
3280 |
5275
|
3281 octave_idx_type nr = m.rows (); |
|
3282 octave_idx_type nc = m.cols (); |
|
3283 |
|
3284 octave_idx_type a_nr = a.rows (); |
|
3285 octave_idx_type a_nc = a.cols (); |
2828
|
3286 |
|
3287 if (nc != a_nr) |
|
3288 gripe_nonconformant ("operator *", nr, nc, a_nr, a_nc); |
|
3289 else |
|
3290 { |
|
3291 if (nr == 0 || nc == 0 || a_nc == 0) |
|
3292 retval.resize (nr, a_nc, 0.0); |
|
3293 else |
|
3294 { |
5275
|
3295 octave_idx_type ld = nr; |
|
3296 octave_idx_type lda = a_nr; |
2828
|
3297 |
|
3298 retval.resize (nr, a_nc); |
|
3299 double *c = retval.fortran_vec (); |
|
3300 |
5983
|
3301 if (a_nc == 1) |
|
3302 { |
|
3303 if (nr == 1) |
|
3304 F77_FUNC (xddot, XDDOT) (nc, m.data (), 1, a.data (), 1, *c); |
|
3305 else |
6390
|
3306 { |
|
3307 F77_XFCN (dgemv, DGEMV, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
3308 nr, nc, 1.0, m.data (), ld, |
|
3309 a.data (), 1, 0.0, c, 1 |
|
3310 F77_CHAR_ARG_LEN (1))); |
|
3311 |
|
3312 if (f77_exception_encountered) |
|
3313 (*current_liboctave_error_handler) |
|
3314 ("unrecoverable error in dgemv"); |
|
3315 } |
5983
|
3316 } |
|
3317 else |
6390
|
3318 { |
|
3319 F77_XFCN (dgemm, DGEMM, (F77_CONST_CHAR_ARG2 ("N", 1), |
|
3320 F77_CONST_CHAR_ARG2 ("N", 1), |
|
3321 nr, a_nc, nc, 1.0, m.data (), |
|
3322 ld, a.data (), lda, 0.0, c, nr |
|
3323 F77_CHAR_ARG_LEN (1) |
|
3324 F77_CHAR_ARG_LEN (1))); |
|
3325 |
|
3326 if (f77_exception_encountered) |
|
3327 (*current_liboctave_error_handler) |
|
3328 ("unrecoverable error in dgemm"); |
|
3329 } |
2828
|
3330 } |
|
3331 } |
|
3332 |
|
3333 return retval; |
|
3334 } |
|
3335 |
5775
|
3336 // FIXME -- it would be nice to share code among the min/max |
4309
|
3337 // functions below. |
|
3338 |
|
3339 #define EMPTY_RETURN_CHECK(T) \ |
|
3340 if (nr == 0 || nc == 0) \ |
|
3341 return T (nr, nc); |
|
3342 |
|
3343 Matrix |
|
3344 min (double d, const Matrix& m) |
|
3345 { |
5275
|
3346 octave_idx_type nr = m.rows (); |
|
3347 octave_idx_type nc = m.columns (); |
4309
|
3348 |
|
3349 EMPTY_RETURN_CHECK (Matrix); |
|
3350 |
|
3351 Matrix result (nr, nc); |
|
3352 |
5275
|
3353 for (octave_idx_type j = 0; j < nc; j++) |
|
3354 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3355 { |
|
3356 OCTAVE_QUIT; |
|
3357 result (i, j) = xmin (d, m (i, j)); |
|
3358 } |
|
3359 |
|
3360 return result; |
|
3361 } |
|
3362 |
|
3363 Matrix |
|
3364 min (const Matrix& m, double d) |
|
3365 { |
5275
|
3366 octave_idx_type nr = m.rows (); |
|
3367 octave_idx_type nc = m.columns (); |
4309
|
3368 |
|
3369 EMPTY_RETURN_CHECK (Matrix); |
|
3370 |
|
3371 Matrix result (nr, nc); |
|
3372 |
5275
|
3373 for (octave_idx_type j = 0; j < nc; j++) |
|
3374 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3375 { |
|
3376 OCTAVE_QUIT; |
|
3377 result (i, j) = xmin (m (i, j), d); |
|
3378 } |
|
3379 |
|
3380 return result; |
|
3381 } |
|
3382 |
|
3383 Matrix |
|
3384 min (const Matrix& a, const Matrix& b) |
|
3385 { |
5275
|
3386 octave_idx_type nr = a.rows (); |
|
3387 octave_idx_type nc = a.columns (); |
4309
|
3388 |
|
3389 if (nr != b.rows () || nc != b.columns ()) |
|
3390 { |
|
3391 (*current_liboctave_error_handler) |
|
3392 ("two-arg min expecting args of same size"); |
|
3393 return Matrix (); |
|
3394 } |
|
3395 |
|
3396 EMPTY_RETURN_CHECK (Matrix); |
|
3397 |
|
3398 Matrix result (nr, nc); |
|
3399 |
5275
|
3400 for (octave_idx_type j = 0; j < nc; j++) |
|
3401 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3402 { |
|
3403 OCTAVE_QUIT; |
|
3404 result (i, j) = xmin (a (i, j), b (i, j)); |
|
3405 } |
|
3406 |
|
3407 return result; |
|
3408 } |
|
3409 |
|
3410 Matrix |
|
3411 max (double d, const Matrix& m) |
|
3412 { |
5275
|
3413 octave_idx_type nr = m.rows (); |
|
3414 octave_idx_type nc = m.columns (); |
4309
|
3415 |
|
3416 EMPTY_RETURN_CHECK (Matrix); |
|
3417 |
|
3418 Matrix result (nr, nc); |
|
3419 |
5275
|
3420 for (octave_idx_type j = 0; j < nc; j++) |
|
3421 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3422 { |
|
3423 OCTAVE_QUIT; |
|
3424 result (i, j) = xmax (d, m (i, j)); |
|
3425 } |
|
3426 |
|
3427 return result; |
|
3428 } |
|
3429 |
|
3430 Matrix |
|
3431 max (const Matrix& m, double d) |
|
3432 { |
5275
|
3433 octave_idx_type nr = m.rows (); |
|
3434 octave_idx_type nc = m.columns (); |
4309
|
3435 |
|
3436 EMPTY_RETURN_CHECK (Matrix); |
|
3437 |
|
3438 Matrix result (nr, nc); |
|
3439 |
5275
|
3440 for (octave_idx_type j = 0; j < nc; j++) |
|
3441 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3442 { |
|
3443 OCTAVE_QUIT; |
|
3444 result (i, j) = xmax (m (i, j), d); |
|
3445 } |
|
3446 |
|
3447 return result; |
|
3448 } |
|
3449 |
|
3450 Matrix |
|
3451 max (const Matrix& a, const Matrix& b) |
|
3452 { |
5275
|
3453 octave_idx_type nr = a.rows (); |
|
3454 octave_idx_type nc = a.columns (); |
4309
|
3455 |
|
3456 if (nr != b.rows () || nc != b.columns ()) |
|
3457 { |
|
3458 (*current_liboctave_error_handler) |
|
3459 ("two-arg max expecting args of same size"); |
|
3460 return Matrix (); |
|
3461 } |
|
3462 |
|
3463 EMPTY_RETURN_CHECK (Matrix); |
|
3464 |
|
3465 Matrix result (nr, nc); |
|
3466 |
5275
|
3467 for (octave_idx_type j = 0; j < nc; j++) |
|
3468 for (octave_idx_type i = 0; i < nr; i++) |
4309
|
3469 { |
|
3470 OCTAVE_QUIT; |
|
3471 result (i, j) = xmax (a (i, j), b (i, j)); |
|
3472 } |
|
3473 |
|
3474 return result; |
|
3475 } |
|
3476 |
2870
|
3477 MS_CMP_OPS(Matrix, , double, ) |
3504
|
3478 MS_BOOL_OPS(Matrix, double, 0.0) |
2870
|
3479 |
|
3480 SM_CMP_OPS(double, , Matrix, ) |
3504
|
3481 SM_BOOL_OPS(double, Matrix, 0.0) |
2870
|
3482 |
|
3483 MM_CMP_OPS(Matrix, , Matrix, ) |
3504
|
3484 MM_BOOL_OPS(Matrix, Matrix, 0.0) |
2870
|
3485 |
458
|
3486 /* |
|
3487 ;;; Local Variables: *** |
|
3488 ;;; mode: C++ *** |
|
3489 ;;; End: *** |
|
3490 */ |