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