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