39 DEFUN (matrix_type, args, ,
41 @deftypefn {Built-in Function} {@var{type} =} matrix_type (@var{A})\n\
42 @deftypefnx {Built-in Function} {@var{type} =} matrix_type (@var{A}, \"nocompute\")\n\
43 @deftypefnx {Built-in Function} {@var{A} =} matrix_type (@var{A}, @var{type})\n\
44 @deftypefnx {Built-in Function} {@var{A} =} matrix_type (@var{A}, \"upper\", @var{perm})\n\
45 @deftypefnx {Built-in Function} {@var{A} =} matrix_type (@var{A}, \"lower\", @var{perm})\n\
46 @deftypefnx {Built-in Function} {@var{A} =} matrix_type (@var{A}, \"banded\", @var{nl}, @var{nu})\n\
47 Identify the matrix type or mark a matrix as a particular type.\n\
49 This allows more rapid solutions of linear equations involving @var{A} to be\n\
52 Called with a single argument, @code{matrix_type} returns the type of the\n\
53 matrix and caches it for future use.\n\
55 Called with more than one argument, @code{matrix_type} allows the type of\n\
56 the matrix to be defined.\n\
58 If the option @qcode{\"nocompute\"} is given, the function will not attempt\n\
59 to guess the type if it is still unknown. This is useful for debugging\n\
62 The possible matrix types depend on whether the matrix is full or sparse, and\n\
63 can be one of the following\n\
66 @item @qcode{\"unknown\"}\n\
67 Remove any previously cached matrix type, and mark type as unknown.\n\
69 @item @qcode{\"full\"}\n\
70 Mark the matrix as full.\n\
72 @item @qcode{\"positive definite\"}\n\
73 Probable full positive definite matrix.\n\
75 @item @qcode{\"diagonal\"}\n\
76 Diagonal matrix. (Sparse matrices only)\n\
78 @item @qcode{\"permuted diagonal\"}\n\
79 Permuted Diagonal matrix. The permutation does not need to be specifically\n\
80 indicated, as the structure of the matrix explicitly gives this. (Sparse\n\
83 @item @qcode{\"upper\"}\n\
84 Upper triangular. If the optional third argument @var{perm} is given, the\n\
85 matrix is assumed to be a permuted upper triangular with the permutations\n\
86 defined by the vector @var{perm}.\n\
88 @item @qcode{\"lower\"}\n\
89 Lower triangular. If the optional third argument @var{perm} is given, the\n\
90 matrix is assumed to be a permuted lower triangular with the permutations\n\
91 defined by the vector @var{perm}.\n\
93 @item @qcode{\"banded\"}\n\
94 @itemx @qcode{\"banded positive definite\"}\n\
95 Banded matrix with the band size of @var{nl} below the diagonal and @var{nu}\n\
96 above it. If @var{nl} and @var{nu} are 1, then the matrix is tridiagonal and\n\
97 treated with specialized code. In addition the matrix can be marked as\n\
98 probably a positive definite. (Sparse matrices only)\n\
100 @item @qcode{\"singular\"}\n\
101 The matrix is assumed to be singular and will be treated with a minimum norm\n\
106 Note that the matrix type will be discovered automatically on the first\n\
107 attempt to solve a linear equation involving @var{A}. Therefore\n\
108 @code{matrix_type} is only useful to give Octave hints of the matrix type.\n\
109 Incorrectly defining the matrix type will result in incorrect results from\n\
110 solutions of linear equations; it is entirely @strong{the responsibility of\n\
111 the user} to correctly identify the matrix type.\n\
113 Also, the test for positive definiteness is a low-cost test for a Hermitian\n\
114 matrix with a real positive diagonal. This does not guarantee that the\n\
115 matrix is positive definite, but only that it is a probable candidate. When\n\
116 such a matrix is factorized, a Cholesky@tie{}factorization is first\n\
117 attempted, and if that fails the matrix is then treated with an\n\
118 LU@tie{}factorization. Once the matrix has been factorized,\n\
119 @code{matrix_type} will return the correct classification of the matrix.\n\
122 int nargin = args.length ();
128 error (
"matrix_type: incorrect number of arguments");
131 bool autocomp =
true;
132 if (nargin == 2 && args(1).is_string ()
133 && args(1).string_value () ==
"nocompute")
139 if (args(0).is_scalar_type ())
146 else if (args(0).is_sparse_type ())
152 if (args(0).is_complex_type ())
159 args(0).sparse_complex_matrix_value ();
163 args(0).matrix_type (mattyp);
177 args(0).matrix_type (mattyp);
182 int typ = mattyp.
type ();
203 retval =
octave_value (
"Tridiagonal Positive Definite");
208 if (args(0).rows () == args(0).columns ())
221 if (! args(1).is_string ())
222 error (
"matrix_type: TYPE must be a string");
225 std::string str_typ = args(1).string_value ();
235 str_typ.begin (), tolower);
237 if (str_typ ==
"diagonal")
239 if (str_typ ==
"permuted diagonal")
241 else if (str_typ ==
"upper")
243 else if (str_typ ==
"lower")
245 else if (str_typ ==
"banded"
246 || str_typ ==
"banded positive definite")
249 error (
"matrix_type: banded matrix type requires 4 arguments");
252 nl = args(2).nint_value ();
253 nu = args(3).nint_value ();
256 error (
"matrix_type: band size NL, NU must be integers");
259 if (nl == 1 && nu == 1)
264 if (str_typ ==
"banded positive definite")
269 else if (str_typ ==
"positive definite")
274 else if (str_typ ==
"singular")
276 else if (str_typ ==
"full")
278 else if (str_typ ==
"unknown")
281 error (
"matrix_type: Unknown matrix type %s", str_typ.c_str ());
286 && (str_typ ==
"upper" || str_typ ==
"lower"))
292 error (
"matrix_type: Invalid permutation vector PERM");
299 error (
"matrix_type: Invalid permutation vector PERM");
305 p[i] = static_cast<octave_idx_type>
309 if (str_typ ==
"upper")
317 && str_typ !=
"banded positive definite"
318 && str_typ !=
"banded")
319 error (
"matrix_type: Invalid number of arguments");
324 if (args(0).is_complex_type ())
343 if (args(0).is_complex_type ())
349 if (args(0).is_single_type ())
352 m = args(0).float_complex_matrix_value ();
356 args(0).matrix_type (mattyp);
365 args(0).matrix_type (mattyp);
376 if (args(0).is_single_type ())
382 args(0).matrix_type (mattyp);
387 Matrix m = args(0).matrix_value ();
391 args(0).matrix_type (mattyp);
397 int typ = mattyp.
type ();
411 if (args(0).rows () == args(0).columns ())
424 if (! args(1).is_string ())
425 error (
"matrix_type: TYPE must be a string");
428 std::string str_typ = args(1).string_value ();
435 str_typ.begin (), tolower);
437 if (str_typ ==
"upper")
439 else if (str_typ ==
"lower")
441 else if (str_typ ==
"positive definite")
446 else if (str_typ ==
"singular")
448 else if (str_typ ==
"full")
450 else if (str_typ ==
"unknown")
453 error (
"matrix_type: Unknown matrix type %s",
458 if (nargin == 3 && (str_typ ==
"upper"
459 || str_typ ==
"lower"))
465 error (
"matrix_type: Invalid permutation vector PERM");
472 error (
"matrix_type: Invalid permutation vector PERM");
478 p[i] = static_cast<octave_idx_type>
482 if (str_typ ==
"upper")
489 else if (nargin != 2)
490 error (
"matrix_type: Invalid number of arguments");
495 if (args(0).is_single_type ())
497 if (args(0).is_complex_type ())
499 (args(0).float_complex_matrix_value (),
503 (args(0).float_matrix_value (),
508 if (args(0).is_complex_type ())
510 (args(0).complex_matrix_value (),
514 (args(0).matrix_value (),
void mark_as_lower_triangular(void)
void mark_as_diagonal(void)
bool is_unknown(void) const
OCTINTERP_API void print_usage(void)
void mark_as_symmetric(void)
#define DEFUN(name, args_name, nargout_name, doc)
void mark_as_permuted_diagonal(void)
void error(const char *fmt,...)
int type(bool quiet=true)
void mark_as_permuted(const octave_idx_type np, const octave_idx_type *p)
void mark_as_upper_triangular(void)
void mark_as_rectangular(void)
void invalidate_type(void)
octave_idx_type length(void) const
Number of elements in the array.
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
ColumnVector transform(const Matrix &m, double x, double y, double z)
void mark_as_tridiagonal(void)
void mark_as_banded(const octave_idx_type ku, const octave_idx_type kl)
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))