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