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