Mercurial > octave
annotate src/DLD-FUNCTIONS/matrix_type.cc @ 8325:b93ac0586e4b
spelling corrections
Here is a patch with some spelling corrections to the manual.
changeset: 8308:aeaf884ea9af
user: Brian Gough <bjg@gnu.org>
date: Fri Nov 07 09:26:17 2008 -0500
summary: [docs] assoicated => associated
author | Brian Gough<bjg@network-theory.co.uk> |
---|---|
date | Mon, 17 Nov 2008 11:38:39 +0100 |
parents | 42c42c640108 |
children | 25bc2d31e1bf |
rev | line source |
---|---|
5323 | 1 /* |
2 | |
7017 | 3 Copyright (C) 2005, 2006, 2007 David Bateman |
5323 | 4 |
5720 | 5 This file is part of Octave. |
6 | |
5323 | 7 Octave is free software; you can redistribute it and/or modify it |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
5323 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
5323 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 | |
5403 | 27 #include <algorithm> |
28 | |
5323 | 29 #include "ov.h" |
30 #include "defun-dld.h" | |
31 #include "error.h" | |
5785 | 32 #include "ov-re-mat.h" |
33 #include "ov-cx-mat.h" | |
5323 | 34 #include "ov-re-sparse.h" |
35 #include "ov-cx-sparse.h" | |
5785 | 36 #include "MatrixType.h" |
5323 | 37 |
38 DEFUN_DLD (matrix_type, args, , | |
39 "-*- texinfo -*-\n\ | |
40 @deftypefn {Loadable Function} {@var{type} =} matrix_type (@var{a})\n\ | |
41 @deftypefnx {Loadable Function} {@var{a} =} matrix_type (@var{a}, @var{type})\n\ | |
42 @deftypefnx {Loadable Function} {@var{a} =} matrix_type (@var{a}, 'upper', @var{perm})\n\ | |
43 @deftypefnx {Loadable Function} {@var{a} =} matrix_type (@var{a}, 'lower', @var{perm})\n\ | |
44 @deftypefnx {Loadable Function} {@var{a} =} matrix_type (@var{a}, 'banded', @var{nl}, @var{nu})\n\ | |
45 Identify the matrix type or mark a matrix as a particular type. This allows rapid\n\ | |
46 for solutions of linear equations involving @var{a} to be performed. Called with a\n\ | |
47 single argument, @code{matrix_type} returns the type of the matrix and caches it for\n\ | |
48 future use. Called with more than one argument, @code{matrix_type} allows the type\n\ | |
49 of the matrix to be defined.\n\ | |
50 \n\ | |
51 The possible matrix types depend on whether the matrix is full or sparse, and can be\n\ | |
52 one of the following\n\ | |
53 \n\ | |
54 @table @asis\n\ | |
55 @item 'unknown'\n\ | |
56 Remove any previously cached matrix type, and mark type as unknown\n\ | |
57 \n\ | |
58 @item 'full'\n\ | |
59 Mark the matrix as full.\n\ | |
60 \n\ | |
61 @item 'positive definite'\n\ | |
7567
6b07c15eb8e1
Document positive definite behavior of matrix_type
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
62 Probable full positive definite matrix.\n\ |
5323 | 63 \n\ |
64 @item 'diagonal'\n\ | |
65 Diagonal Matrix. (Sparse matrices only)\n\ | |
66 \n\ | |
67 @item 'permuted diagonal'\n\ | |
68 Permuted Diagonal matrix. The permutation does not need to be specifically\n\ | |
69 indicated, as the structure of the matrix explicitly gives this. (Sparse matrices\n\ | |
70 only)\n\ | |
71 \n\ | |
72 @item 'upper'\n\ | |
73 Upper triangular. If the optional third argument @var{perm} is given, the matrix is\n\ | |
74 assumed to be a permuted upper triangular with the permutations defined by the\n\ | |
75 vector @var{perm}.\n\ | |
76 \n\ | |
77 @item 'lower'\n\ | |
78 Lower triangular. If the optional third argument @var{perm} is given, the matrix is\n\ | |
79 assumed to be a permuted lower triangular with the permutations defined by the\n\ | |
80 vector @var{perm}.\n\ | |
81 \n\ | |
82 @item 'banded'\n\ | |
83 @itemx 'banded positive definite'\n\ | |
84 Banded matrix with the band size of @var{nl} below the diagonal and @var{nu} above\n\ | |
85 it. If @var{nl} and @var{nu} are 1, then the matrix is tridiagonal and treated\n\ | |
7567
6b07c15eb8e1
Document positive definite behavior of matrix_type
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
86 with specialized code. In addition the matrix can be marked as probably a\n\ |
6b07c15eb8e1
Document positive definite behavior of matrix_type
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
87 positive definite (Sparse matrices only)\n\ |
5323 | 88 \n\ |
89 @item 'singular'\n\ | |
90 The matrix is assumed to be singular and will be treated with a minimum norm solution\n\ | |
91 \n\ | |
92 @end table\n\ | |
93 \n\ | |
94 Note that the matrix type will be discovered automatically on the first attempt to\n\ | |
95 solve a linear equation involving @var{a}. Therefore @code{matrix_type} is only\n\ | |
5324 | 96 useful to give Octave hints of the matrix type. Incorrectly defining the\n\ |
5323 | 97 matrix type will result in incorrect results from solutions of linear equations,\n\ |
7001 | 98 and so it is entirely the responsibility of the user to correctly identify the\n\ |
5323 | 99 matrix type.\n\ |
7567
6b07c15eb8e1
Document positive definite behavior of matrix_type
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
100 \n\ |
6b07c15eb8e1
Document positive definite behavior of matrix_type
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
101 Also the test for positive definiteness is a low cost test for a hermitian\n\ |
8325
b93ac0586e4b
spelling corrections
Brian Gough<bjg@network-theory.co.uk>
parents:
7798
diff
changeset
|
102 matrix with a real positive diagonal. This does not guarantee that the matrix\n\ |
7567
6b07c15eb8e1
Document positive definite behavior of matrix_type
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
103 is positive definite, but only that it is a probable candidate. When such a\n\ |
6b07c15eb8e1
Document positive definite behavior of matrix_type
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
104 matrix is factorized, a Cholesky factorization is first attempted, and if\n\ |
6b07c15eb8e1
Document positive definite behavior of matrix_type
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
105 that fails the matrix is then treated with an LU factorization. Once the\n\ |
6b07c15eb8e1
Document positive definite behavior of matrix_type
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
106 matrix has been factorized, @code{matrix_type} will return the correct\n\ |
6b07c15eb8e1
Document positive definite behavior of matrix_type
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
107 classification of the matrix.\n\ |
5323 | 108 @end deftypefn") |
109 { | |
110 int nargin = args.length (); | |
111 octave_value retval; | |
112 | |
113 if (nargin == 0) | |
5823 | 114 print_usage (); |
5323 | 115 else if (nargin > 4) |
116 error ("matrix_type: incorrect number of arguments"); | |
117 else | |
118 { | |
6371 | 119 if (args(0).is_scalar_type()) |
120 { | |
121 if (nargin == 1) | |
122 retval = octave_value ("Full"); | |
123 else | |
124 retval = args(0); | |
125 } | |
126 else if (args(0).is_sparse_type ()) | |
5323 | 127 { |
128 if (nargin == 1) | |
129 { | |
5785 | 130 MatrixType mattyp; |
5323 | 131 |
6371 | 132 if (args(0).is_complex_type ()) |
5323 | 133 { |
5785 | 134 mattyp = args(0).matrix_type (); |
5323 | 135 |
136 if (mattyp.is_unknown ()) | |
137 { | |
5681 | 138 SparseComplexMatrix m = |
139 args(0).sparse_complex_matrix_value (); | |
140 if (!error_state) | |
141 { | |
5785 | 142 mattyp = MatrixType (m); |
143 args(0).matrix_type (mattyp); | |
5681 | 144 } |
5323 | 145 } |
146 } | |
147 else | |
148 { | |
5785 | 149 mattyp = args(0).matrix_type (); |
5323 | 150 |
151 if (mattyp.is_unknown ()) | |
152 { | |
5681 | 153 SparseMatrix m = args(0).sparse_matrix_value (); |
154 if (!error_state) | |
155 { | |
5785 | 156 mattyp = MatrixType (m); |
157 args(0).matrix_type (mattyp); | |
5681 | 158 } |
5323 | 159 } |
160 } | |
161 | |
162 int typ = mattyp.type (); | |
163 | |
5785 | 164 if (typ == MatrixType::Diagonal) |
5323 | 165 retval = octave_value ("Diagonal"); |
5785 | 166 else if (typ == MatrixType::Permuted_Diagonal) |
5323 | 167 retval = octave_value ("Permuted Diagonal"); |
5785 | 168 else if (typ == MatrixType::Upper) |
5323 | 169 retval = octave_value ("Upper"); |
5785 | 170 else if (typ == MatrixType::Permuted_Upper) |
5323 | 171 retval = octave_value ("Permuted Upper"); |
5785 | 172 else if (typ == MatrixType::Lower) |
5323 | 173 retval = octave_value ("Lower"); |
5785 | 174 else if (typ == MatrixType::Permuted_Lower) |
5323 | 175 retval = octave_value ("Permuted Lower"); |
5785 | 176 else if (typ == MatrixType::Banded) |
5323 | 177 retval = octave_value ("Banded"); |
5785 | 178 else if (typ == MatrixType::Banded_Hermitian) |
5323 | 179 retval = octave_value ("Banded Positive Definite"); |
5785 | 180 else if (typ == MatrixType::Tridiagonal) |
5323 | 181 retval = octave_value ("Tridiagonal"); |
5785 | 182 else if (typ == MatrixType::Tridiagonal_Hermitian) |
5323 | 183 retval = octave_value ("Tridiagonal Positive Definite"); |
5785 | 184 else if (typ == MatrixType::Hermitian) |
5323 | 185 retval = octave_value ("Positive Definite"); |
5785 | 186 else if (typ == MatrixType::Rectangular) |
5610 | 187 { |
188 if (args(0).rows() == args(0).columns()) | |
189 retval = octave_value ("Singular"); | |
190 else | |
191 retval = octave_value ("Rectangular"); | |
192 } | |
5785 | 193 else if (typ == MatrixType::Full) |
5323 | 194 retval = octave_value ("Full"); |
195 else | |
196 // This should never happen!!! | |
197 retval = octave_value ("Unknown"); | |
198 } | |
199 else | |
200 { | |
201 // Ok, we're changing the matrix type | |
202 std::string str_typ = args(1).string_value (); | |
203 | |
5775 | 204 // FIXME -- why do I have to explicitly call the constructor? |
5785 | 205 MatrixType mattyp = MatrixType (); |
5323 | 206 |
5506 | 207 octave_idx_type nl = 0; |
208 octave_idx_type nu = 0; | |
5323 | 209 |
210 if (error_state) | |
211 error ("Matrix type must be a string"); | |
212 else | |
213 { | |
214 // Use STL function to convert to lower case | |
5403 | 215 std::transform (str_typ.begin (), str_typ.end (), |
5406 | 216 str_typ.begin (), tolower); |
5323 | 217 |
218 if (str_typ == "diagonal") | |
219 mattyp.mark_as_diagonal (); | |
220 if (str_typ == "permuted diagonal") | |
221 mattyp.mark_as_permuted_diagonal (); | |
222 else if (str_typ == "upper") | |
223 mattyp.mark_as_upper_triangular (); | |
224 else if (str_typ == "lower") | |
225 mattyp.mark_as_lower_triangular (); | |
226 else if (str_typ == "banded" || str_typ == "banded positive definite") | |
227 { | |
228 if (nargin != 4) | |
229 error ("matrix_type: banded matrix type requires 4 arguments"); | |
230 else | |
231 { | |
232 nl = args(2).nint_value (); | |
233 nu = args(3).nint_value (); | |
234 | |
235 if (error_state) | |
236 error ("matrix_type: band size must be integer"); | |
237 else | |
238 { | |
239 if (nl == 1 && nu == 1) | |
240 mattyp.mark_as_tridiagonal (); | |
241 else | |
242 mattyp.mark_as_banded (nu, nl); | |
243 | |
244 if (str_typ == "banded positive definite") | |
245 mattyp.mark_as_symmetric (); | |
246 } | |
247 } | |
248 } | |
249 else if (str_typ == "positive definite") | |
250 { | |
251 mattyp.mark_as_full (); | |
252 mattyp.mark_as_symmetric (); | |
253 } | |
254 else if (str_typ == "singular") | |
255 mattyp.mark_as_rectangular (); | |
256 else if (str_typ == "full") | |
257 mattyp.mark_as_full (); | |
258 else if (str_typ == "unknown") | |
259 mattyp.invalidate_type (); | |
260 else | |
261 error ("matrix_type: Unknown matrix type %s", str_typ.c_str()); | |
262 | |
263 if (! error_state) | |
264 { | |
265 if (nargin == 3 && (str_typ == "upper" || str_typ == "lower")) | |
266 { | |
267 const ColumnVector perm = | |
268 ColumnVector (args (2).vector_value ()); | |
269 | |
270 if (error_state) | |
271 error ("matrix_type: Invalid permutation vector"); | |
272 else | |
273 { | |
5506 | 274 octave_idx_type len = perm.length (); |
5323 | 275 dim_vector dv = args(0).dims (); |
276 | |
277 if (len != dv(0)) | |
278 error ("matrix_type: Invalid permutation vector"); | |
279 else | |
280 { | |
281 OCTAVE_LOCAL_BUFFER (octave_idx_type, p, len); | |
282 | |
5506 | 283 for (octave_idx_type i = 0; i < len; i++) |
5760 | 284 p[i] = static_cast<octave_idx_type> (perm (i)) - 1; |
5323 | 285 |
286 if (str_typ == "upper") | |
287 mattyp.mark_as_permuted (len, p); | |
288 else | |
289 mattyp.mark_as_permuted (len, p); | |
290 } | |
291 } | |
292 } | |
293 else if (nargin != 2 && str_typ != "banded positive definite" && | |
294 str_typ != "banded") | |
295 error ("matrix_type: Invalid number of arguments"); | |
296 | |
297 if (! error_state) | |
298 { | |
299 // Set the matrix type | |
6371 | 300 if (args(0).is_complex_type ()) |
5323 | 301 retval = |
302 octave_value (args(0).sparse_complex_matrix_value (), | |
303 mattyp); | |
304 else | |
305 retval = octave_value (args(0).sparse_matrix_value (), | |
306 mattyp); | |
307 } | |
308 } | |
309 } | |
310 } | |
311 } | |
312 else | |
5785 | 313 { |
314 if (nargin == 1) | |
315 { | |
316 MatrixType mattyp; | |
317 | |
6371 | 318 if (args(0).is_complex_type ()) |
5785 | 319 { |
320 mattyp = args(0).matrix_type (); | |
321 | |
322 if (mattyp.is_unknown ()) | |
323 { | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
324 if (args(0).is_single_type ()) |
5785 | 325 { |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
326 FloatComplexMatrix m = args(0).float_complex_matrix_value (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
327 if (!error_state) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
328 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
329 mattyp = MatrixType (m); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
330 args(0).matrix_type (mattyp); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
331 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
332 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
333 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
334 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
335 ComplexMatrix m = args(0).complex_matrix_value (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
336 if (!error_state) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
337 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
338 mattyp = MatrixType (m); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
339 args(0).matrix_type (mattyp); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
340 } |
5785 | 341 } |
342 } | |
343 } | |
344 else | |
345 { | |
346 mattyp = args(0).matrix_type (); | |
347 | |
348 if (mattyp.is_unknown ()) | |
349 { | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
350 if (args(0).is_single_type ()) |
5785 | 351 { |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
352 FloatMatrix m = args(0).float_matrix_value (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
353 if (!error_state) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
354 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
355 mattyp = MatrixType (m); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
356 args(0).matrix_type (mattyp); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
357 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
358 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
359 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
360 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
361 Matrix m = args(0).matrix_value (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
362 if (!error_state) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
363 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
364 mattyp = MatrixType (m); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
365 args(0).matrix_type (mattyp); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
366 } |
5785 | 367 } |
368 } | |
369 } | |
370 | |
371 int typ = mattyp.type (); | |
372 | |
373 if (typ == MatrixType::Upper) | |
374 retval = octave_value ("Upper"); | |
375 else if (typ == MatrixType::Permuted_Upper) | |
376 retval = octave_value ("Permuted Upper"); | |
377 else if (typ == MatrixType::Lower) | |
378 retval = octave_value ("Lower"); | |
379 else if (typ == MatrixType::Permuted_Lower) | |
380 retval = octave_value ("Permuted Lower"); | |
381 else if (typ == MatrixType::Hermitian) | |
382 retval = octave_value ("Positive Definite"); | |
383 else if (typ == MatrixType::Rectangular) | |
384 { | |
385 if (args(0).rows() == args(0).columns()) | |
386 retval = octave_value ("Singular"); | |
387 else | |
388 retval = octave_value ("Rectangular"); | |
389 } | |
390 else if (typ == MatrixType::Full) | |
391 retval = octave_value ("Full"); | |
392 else | |
393 // This should never happen!!! | |
394 retval = octave_value ("Unknown"); | |
395 } | |
396 else | |
397 { | |
398 // Ok, we're changing the matrix type | |
399 std::string str_typ = args(1).string_value (); | |
400 | |
401 // FIXME -- why do I have to explicitly call the constructor? | |
402 MatrixType mattyp = MatrixType (MatrixType::Unknown, true); | |
403 | |
404 if (error_state) | |
405 error ("Matrix type must be a string"); | |
406 else | |
407 { | |
408 // Use STL function to convert to lower case | |
409 std::transform (str_typ.begin (), str_typ.end (), | |
410 str_typ.begin (), tolower); | |
411 | |
412 if (str_typ == "upper") | |
413 mattyp.mark_as_upper_triangular (); | |
414 else if (str_typ == "lower") | |
415 mattyp.mark_as_lower_triangular (); | |
416 else if (str_typ == "positive definite") | |
417 { | |
418 mattyp.mark_as_full (); | |
419 mattyp.mark_as_symmetric (); | |
420 } | |
421 else if (str_typ == "singular") | |
422 mattyp.mark_as_rectangular (); | |
423 else if (str_typ == "full") | |
424 mattyp.mark_as_full (); | |
425 else if (str_typ == "unknown") | |
426 mattyp.invalidate_type (); | |
427 else | |
428 error ("matrix_type: Unknown matrix type %s", str_typ.c_str()); | |
429 | |
430 if (! error_state) | |
431 { | |
432 if (nargin == 3 && (str_typ == "upper" | |
433 || str_typ == "lower")) | |
434 { | |
435 const ColumnVector perm = | |
436 ColumnVector (args (2).vector_value ()); | |
437 | |
438 if (error_state) | |
439 error ("matrix_type: Invalid permutation vector"); | |
440 else | |
441 { | |
442 octave_idx_type len = perm.length (); | |
443 dim_vector dv = args(0).dims (); | |
444 | |
445 if (len != dv(0)) | |
446 error ("matrix_type: Invalid permutation vector"); | |
447 else | |
448 { | |
449 OCTAVE_LOCAL_BUFFER (octave_idx_type, p, len); | |
450 | |
451 for (octave_idx_type i = 0; i < len; i++) | |
452 p[i] = static_cast<octave_idx_type> (perm (i)) - 1; | |
453 | |
454 if (str_typ == "upper") | |
455 mattyp.mark_as_permuted (len, p); | |
456 else | |
457 mattyp.mark_as_permuted (len, p); | |
458 } | |
459 } | |
460 } | |
461 else if (nargin != 2) | |
462 error ("matrix_type: Invalid number of arguments"); | |
463 | |
464 if (! error_state) | |
465 { | |
466 // Set the matrix type | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
467 if (args(0).is_single_type ()) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
468 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
469 if (args(0).is_complex_type()) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
470 retval = octave_value |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
471 (args(0).float_complex_matrix_value (), |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
472 mattyp); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
473 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
474 retval = octave_value |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
475 (args(0).float_matrix_value (), |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
476 mattyp); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
477 } |
5785 | 478 else |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
479 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
480 if (args(0).is_complex_type()) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
481 retval = octave_value |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
482 (args(0).complex_matrix_value (), |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
483 mattyp); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
484 else |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
485 retval = octave_value |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
486 (args(0).matrix_value (), |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
487 mattyp); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
488 } |
5785 | 489 } |
490 } | |
491 } | |
492 } | |
493 } | |
5323 | 494 } |
495 | |
496 return retval; | |
497 } | |
498 | |
499 /* | |
5681 | 500 |
5775 | 501 ## FIXME |
5681 | 502 ## Disable tests for lower under-determined and upper over-determined |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7567
diff
changeset
|
503 ## matrices as this detection is disabled in MatrixType due to issues |
5681 | 504 ## of non minimum norm solution being found. |
5610 | 505 |
506 %!assert(matrix_type(speye(10,10)),"Diagonal"); | |
507 %!assert(matrix_type(speye(10,10)([2:10,1],:)),"Permuted Diagonal"); | |
508 %!assert(matrix_type([[speye(10,10);sparse(1,10)],[1;sparse(9,1);1]]),"Upper"); | |
509 %!assert(matrix_type([[speye(10,10);sparse(1,10)],[1;sparse(9,1);1]](:,[2,1,3:11])),"Permuted Upper"); | |
510 %!assert(matrix_type([speye(10,10),sparse(10,1);1,sparse(1,9),1]),"Lower"); | |
511 %!assert(matrix_type([speye(10,10),sparse(10,1);1,sparse(1,9),1]([2,1,3:11],:)),"Permuted Lower"); | |
512 %!test | |
513 %! bnd=spparms("bandden"); | |
514 %! spparms("bandden",0.5); | |
7798
42c42c640108
improved matrix_type check
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
515 %! a = spdiags(rand(10,3)-0.5,[-1,0,1],10,10); |
5610 | 516 %! assert(matrix_type(a),"Tridiagonal"); |
7798
42c42c640108
improved matrix_type check
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
517 %! assert(matrix_type(a'+a+2*speye(10)),"Tridiagonal Positive Definite"); |
5610 | 518 %! spparms("bandden",bnd); |
519 %!test | |
520 %! bnd=spparms("bandden"); | |
521 %! spparms("bandden",0.5); | |
522 %! a = spdiags(randn(10,4),[-2:1],10,10); | |
523 %! assert(matrix_type(a),"Banded"); | |
524 %! assert(matrix_type(a'*a),"Banded Positive Definite"); | |
525 %! spparms("bandden",bnd); | |
526 %!test | |
527 %! a=[speye(10,10),[sparse(9,1);1];-1,sparse(1,9),1]; | |
528 %! assert(matrix_type(a),"Full"); | |
529 %! assert(matrix_type(a'*a),"Positive Definite"); | |
5630 | 530 %!assert(matrix_type(speye(10,11)),"Diagonal"); |
531 %!assert(matrix_type(speye(10,11)([2:10,1],:)),"Permuted Diagonal"); | |
532 %!assert(matrix_type(speye(11,10)),"Diagonal"); | |
533 %!assert(matrix_type(speye(11,10)([2:11,1],:)),"Permuted Diagonal"); | |
5681 | 534 %#!assert(matrix_type([[speye(10,10);sparse(1,10)],[[1,1];sparse(9,2);[1,1]]]),"Upper"); |
535 %#!assert(matrix_type([[speye(10,10);sparse(1,10)],[[1,1];sparse(9,2);[1,1]]](:,[2,1,3:12])),"Permuted Upper"); | |
5630 | 536 %!assert(matrix_type([speye(11,9),[1;sparse(8,1);1;0]]),"Upper"); |
537 %!assert(matrix_type([speye(11,9),[1;sparse(8,1);1;0]](:,[2,1,3:10])),"Permuted Upper"); | |
5681 | 538 %#!assert(matrix_type([speye(10,10),sparse(10,1);[1;1],sparse(2,9),[1;1]]),"Lower"); |
539 %#!assert(matrix_type([speye(10,10),sparse(10,1);[1;1],sparse(2,9),[1;1]]([2,1,3:12],:)),"Permuted Lower"); | |
5630 | 540 %!assert(matrix_type([speye(9,11);[1,sparse(1,8),1,0]]),"Lower"); |
541 %!assert(matrix_type([speye(9,11);[1,sparse(1,8),1,0]]([2,1,3:10],:)),"Permuted Lower"); | |
542 %!assert(matrix_type(spdiags(randn(10,4),[-2:1],10,9)),"Rectangular") | |
5610 | 543 |
544 %!assert(matrix_type(1i*speye(10,10)),"Diagonal"); | |
545 %!assert(matrix_type(1i*speye(10,10)([2:10,1],:)),"Permuted Diagonal"); | |
546 %!assert(matrix_type([[speye(10,10);sparse(1,10)],[1i;sparse(9,1);1]]),"Upper"); | |
547 %!assert(matrix_type([[speye(10,10);sparse(1,10)],[1i;sparse(9,1);1]](:,[2,1,3:11])),"Permuted Upper"); | |
548 %!assert(matrix_type([speye(10,10),sparse(10,1);1i,sparse(1,9),1]),"Lower"); | |
549 %!assert(matrix_type([speye(10,10),sparse(10,1);1i,sparse(1,9),1]([2,1,3:11],:)),"Permuted Lower"); | |
550 %!test | |
551 %! bnd=spparms("bandden"); | |
552 %! spparms("bandden",0.5); | |
553 %! assert(matrix_type(spdiags(1i*randn(10,3),[-1,0,1],10,10)),"Tridiagonal"); | |
7798
42c42c640108
improved matrix_type check
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
554 %! a = 1i*(rand(9,1)-0.5);a=[[a;0],ones(10,1),[0;-a]]; |
5610 | 555 %! assert(matrix_type(spdiags(a,[-1,0,1],10,10)),"Tridiagonal Positive Definite"); |
556 %! spparms("bandden",bnd); | |
557 %!test | |
558 %! bnd=spparms("bandden"); | |
559 %! spparms("bandden",0.5); | |
560 %! assert(matrix_type(spdiags(1i*randn(10,4),[-2:1],10,10)),"Banded"); | |
7798
42c42c640108
improved matrix_type check
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
561 %! a = 1i*(rand(9,2)-0.5);a=[[a;[0,0]],ones(10,1),[[0;-a(:,2)],[0;0;-a(1:8,1)]]]; |
5610 | 562 %! assert(matrix_type(spdiags(a,[-2:2],10,10)),"Banded Positive Definite"); |
563 %! spparms("bandden",bnd); | |
564 %!test | |
565 %! a=[speye(10,10),[sparse(9,1);1i];-1,sparse(1,9),1]; | |
566 %! assert(matrix_type(a),"Full"); | |
567 %! assert(matrix_type(a'*a),"Positive Definite"); | |
5630 | 568 %!assert(matrix_type(1i*speye(10,11)),"Diagonal"); |
569 %!assert(matrix_type(1i*speye(10,11)([2:10,1],:)),"Permuted Diagonal"); | |
570 %!assert(matrix_type(1i*speye(11,10)),"Diagonal"); | |
571 %!assert(matrix_type(1i*speye(11,10)([2:11,1],:)),"Permuted Diagonal"); | |
5681 | 572 %#!assert(matrix_type([[speye(10,10);sparse(1,10)],[[1i,1i];sparse(9,2);[1i,1i]]]),"Upper"); |
573 %#!assert(matrix_type([[speye(10,10);sparse(1,10)],[[1i,1i];sparse(9,2);[1i,1i]]](:,[2,1,3:12])),"Permuted Upper"); | |
5630 | 574 %!assert(matrix_type([speye(11,9),[1i;sparse(8,1);1i;0]]),"Upper"); |
575 %!assert(matrix_type([speye(11,9),[1i;sparse(8,1);1i;0]](:,[2,1,3:10])),"Permuted Upper"); | |
5681 | 576 %#!assert(matrix_type([speye(10,10),sparse(10,1);[1i;1i],sparse(2,9),[1i;1i]]),"Lower"); |
577 %#!assert(matrix_type([speye(10,10),sparse(10,1);[1i;1i],sparse(2,9),[1i;1i]]([2,1,3:12],:)),"Permuted Lower"); | |
5630 | 578 %!assert(matrix_type([speye(9,11);[1i,sparse(1,8),1i,0]]),"Lower"); |
579 %!assert(matrix_type([speye(9,11);[1i,sparse(1,8),1i,0]]([2,1,3:10],:)),"Permuted Lower"); | |
580 %!assert(matrix_type(1i*spdiags(randn(10,4),[-2:1],10,9)),"Rectangular") | |
5610 | 581 |
582 %!test | |
583 %! a = matrix_type(spdiags(randn(10,3),[-1,0,1],10,10),"Singular"); | |
584 %! assert(matrix_type(a),"Singular"); | |
585 | |
5785 | 586 %!assert(matrix_type(triu(ones(10,10))),"Upper"); |
587 %!assert(matrix_type(triu(ones(10,10),-1)),"Full"); | |
588 %!assert(matrix_type(tril(ones(10,10))),"Lower"); | |
589 %!assert(matrix_type(tril(ones(10,10),1)),"Full"); | |
590 %!assert(matrix_type(10*eye(10,10) + ones(10,10)), "Positive Definite"); | |
591 %!assert(matrix_type(ones(11,10)),"Rectangular") | |
592 %!test | |
593 %! a = matrix_type(ones(10,10),"Singular"); | |
594 %! assert(matrix_type(a),"Singular"); | |
595 | |
596 %!assert(matrix_type(triu(1i*ones(10,10))),"Upper"); | |
597 %!assert(matrix_type(triu(1i*ones(10,10),-1)),"Full"); | |
598 %!assert(matrix_type(tril(1i*ones(10,10))),"Lower"); | |
599 %!assert(matrix_type(tril(1i*ones(10,10),1)),"Full"); | |
600 %!assert(matrix_type(10*eye(10,10) + 1i*triu(ones(10,10),1) -1i*tril(ones(10,10),-1)), "Positive Definite"); | |
601 %!assert(matrix_type(ones(11,10)),"Rectangular") | |
602 %!test | |
603 %! a = matrix_type(ones(10,10),"Singular"); | |
604 %! assert(matrix_type(a),"Singular"); | |
605 | |
5610 | 606 */ |
607 | |
608 /* | |
5323 | 609 ;;; Local Variables: *** |
610 ;;; mode: C++ *** | |
611 ;;; End: *** | |
612 */ |