5323
|
1 /* |
|
2 |
|
3 Copyright (C) 2005 David Bateman |
|
4 |
|
5 Octave is free software; you can redistribute it and/or modify it |
|
6 under the terms of the GNU General Public License as published by the |
|
7 Free Software Foundation; either version 2, or (at your option) any |
|
8 later version. |
|
9 |
|
10 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 for more details. |
|
14 |
|
15 You should have received a copy of the GNU General Public License |
|
16 along with this program; see the file COPYING. If not, write to the Free |
|
17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
18 |
|
19 */ |
|
20 |
|
21 #ifdef HAVE_CONFIG_H |
|
22 #include <config.h> |
|
23 #endif |
|
24 |
5403
|
25 #include <algorithm> |
|
26 |
5323
|
27 #include "ov.h" |
|
28 #include "defun-dld.h" |
|
29 #include "error.h" |
|
30 #include "ov-re-sparse.h" |
|
31 #include "ov-cx-sparse.h" |
|
32 #include "SparseType.h" |
|
33 |
|
34 DEFUN_DLD (matrix_type, args, , |
|
35 "-*- texinfo -*-\n\ |
|
36 @deftypefn {Loadable Function} {@var{type} =} matrix_type (@var{a})\n\ |
|
37 @deftypefnx {Loadable Function} {@var{a} =} matrix_type (@var{a}, @var{type})\n\ |
|
38 @deftypefnx {Loadable Function} {@var{a} =} matrix_type (@var{a}, 'upper', @var{perm})\n\ |
|
39 @deftypefnx {Loadable Function} {@var{a} =} matrix_type (@var{a}, 'lower', @var{perm})\n\ |
|
40 @deftypefnx {Loadable Function} {@var{a} =} matrix_type (@var{a}, 'banded', @var{nl}, @var{nu})\n\ |
|
41 Identify the matrix type or mark a matrix as a particular type. This allows rapid\n\ |
|
42 for solutions of linear equations involving @var{a} to be performed. Called with a\n\ |
|
43 single argument, @code{matrix_type} returns the type of the matrix and caches it for\n\ |
|
44 future use. Called with more than one argument, @code{matrix_type} allows the type\n\ |
|
45 of the matrix to be defined.\n\ |
|
46 \n\ |
|
47 The possible matrix types depend on whether the matrix is full or sparse, and can be\n\ |
|
48 one of the following\n\ |
|
49 \n\ |
|
50 @table @asis\n\ |
|
51 @item 'unknown'\n\ |
|
52 Remove any previously cached matrix type, and mark type as unknown\n\ |
|
53 \n\ |
|
54 @item 'full'\n\ |
|
55 Mark the matrix as full.\n\ |
|
56 \n\ |
|
57 @item 'positive definite'\n\ |
|
58 Full positive definite matrix.\n\ |
|
59 \n\ |
|
60 @item 'diagonal'\n\ |
|
61 Diagonal Matrix. (Sparse matrices only)\n\ |
|
62 \n\ |
|
63 @item 'permuted diagonal'\n\ |
|
64 Permuted Diagonal matrix. The permutation does not need to be specifically\n\ |
|
65 indicated, as the structure of the matrix explicitly gives this. (Sparse matrices\n\ |
|
66 only)\n\ |
|
67 \n\ |
|
68 @item 'upper'\n\ |
|
69 Upper triangular. If the optional third argument @var{perm} is given, the matrix is\n\ |
|
70 assumed to be a permuted upper triangular with the permutations defined by the\n\ |
|
71 vector @var{perm}.\n\ |
|
72 \n\ |
|
73 @item 'lower'\n\ |
|
74 Lower triangular. If the optional third argument @var{perm} is given, the matrix is\n\ |
|
75 assumed to be a permuted lower triangular with the permutations defined by the\n\ |
|
76 vector @var{perm}.\n\ |
|
77 \n\ |
|
78 @item 'banded'\n\ |
|
79 @itemx 'banded positive definite'\n\ |
|
80 Banded matrix with the band size of @var{nl} below the diagonal and @var{nu} above\n\ |
|
81 it. If @var{nl} and @var{nu} are 1, then the matrix is tridiagonal and treated\n\ |
|
82 with specialized code. In addition the matrix can be marked as positive definite\n\ |
|
83 (Sparse matrices only)\n\ |
|
84 \n\ |
|
85 @item 'singular'\n\ |
|
86 The matrix is assumed to be singular and will be treated with a minimum norm solution\n\ |
|
87 \n\ |
|
88 @end table\n\ |
|
89 \n\ |
|
90 Note that the matrix type will be discovered automatically on the first attempt to\n\ |
|
91 solve a linear equation involving @var{a}. Therefore @code{matrix_type} is only\n\ |
5324
|
92 useful to give Octave hints of the matrix type. Incorrectly defining the\n\ |
5323
|
93 matrix type will result in incorrect results from solutions of linear equations,\n\ |
|
94 and so it is entirely the responsibility of the user to correctly indentify the\n\ |
|
95 matrix type.\n\ |
|
96 @end deftypefn") |
|
97 { |
|
98 int nargin = args.length (); |
|
99 octave_value retval; |
|
100 |
|
101 if (nargin == 0) |
|
102 print_usage ("matrix_type"); |
|
103 else if (nargin > 4) |
|
104 error ("matrix_type: incorrect number of arguments"); |
|
105 else |
|
106 { |
|
107 if (args(0).class_name () == "sparse") |
|
108 { |
|
109 if (nargin == 1) |
|
110 { |
|
111 SparseType mattyp; |
|
112 const octave_value& rep = args(0).get_rep (); |
|
113 |
|
114 if (args(0).type_name () == "sparse complex matrix" ) |
|
115 { |
|
116 mattyp = |
|
117 ((const octave_sparse_complex_matrix &)rep).sparse_type (); |
|
118 |
|
119 if (mattyp.is_unknown ()) |
|
120 { |
|
121 mattyp = SparseType (args(0).sparse_complex_matrix_value ()); |
|
122 ((octave_sparse_complex_matrix &)rep).sparse_type (mattyp); |
|
123 } |
|
124 } |
|
125 else |
|
126 { |
|
127 mattyp = ((const octave_sparse_matrix &)rep).sparse_type (); |
|
128 |
|
129 if (mattyp.is_unknown ()) |
|
130 { |
|
131 mattyp = SparseType (args(0).sparse_matrix_value ()); |
|
132 ((octave_sparse_matrix &)rep).sparse_type (mattyp); |
|
133 } |
|
134 } |
|
135 |
|
136 int typ = mattyp.type (); |
|
137 |
|
138 if (typ == SparseType::Diagonal) |
|
139 retval = octave_value ("Diagonal"); |
|
140 else if (typ == SparseType::Permuted_Diagonal) |
|
141 retval = octave_value ("Permuted Diagonal"); |
|
142 else if (typ == SparseType::Upper) |
|
143 retval = octave_value ("Upper"); |
|
144 else if (typ == SparseType::Permuted_Upper) |
|
145 retval = octave_value ("Permuted Upper"); |
|
146 else if (typ == SparseType::Lower) |
|
147 retval = octave_value ("Lower"); |
|
148 else if (typ == SparseType::Permuted_Lower) |
|
149 retval = octave_value ("Permuted Lower"); |
|
150 else if (typ == SparseType::Banded) |
|
151 retval = octave_value ("Banded"); |
|
152 else if (typ == SparseType::Banded_Hermitian) |
|
153 retval = octave_value ("Banded Positive Definite"); |
|
154 else if (typ == SparseType::Tridiagonal) |
|
155 retval = octave_value ("Tridiagonal"); |
|
156 else if (typ == SparseType::Tridiagonal_Hermitian) |
|
157 retval = octave_value ("Tridiagonal Positive Definite"); |
|
158 else if (typ == SparseType::Hermitian) |
|
159 retval = octave_value ("Positive Definite"); |
|
160 else if (typ == SparseType::Full) |
|
161 retval = octave_value ("Full"); |
|
162 else |
|
163 // This should never happen!!! |
|
164 retval = octave_value ("Unknown"); |
|
165 } |
|
166 else |
|
167 { |
|
168 // Ok, we're changing the matrix type |
|
169 std::string str_typ = args(1).string_value (); |
|
170 |
|
171 // XXX FIXME, why do I have to explicitly call the constructor? |
|
172 SparseType mattyp = SparseType (); |
|
173 |
|
174 int nl = 0; |
|
175 int nu = 0; |
|
176 |
|
177 if (error_state) |
|
178 error ("Matrix type must be a string"); |
|
179 else |
|
180 { |
|
181 // Use STL function to convert to lower case |
5403
|
182 std::transform (str_typ.begin (), str_typ.end (), |
|
183 str_typ.begin (), std::tolower); |
5323
|
184 |
|
185 if (str_typ == "diagonal") |
|
186 mattyp.mark_as_diagonal (); |
|
187 if (str_typ == "permuted diagonal") |
|
188 mattyp.mark_as_permuted_diagonal (); |
|
189 else if (str_typ == "upper") |
|
190 mattyp.mark_as_upper_triangular (); |
|
191 else if (str_typ == "lower") |
|
192 mattyp.mark_as_lower_triangular (); |
|
193 else if (str_typ == "banded" || str_typ == "banded positive definite") |
|
194 { |
|
195 if (nargin != 4) |
|
196 error ("matrix_type: banded matrix type requires 4 arguments"); |
|
197 else |
|
198 { |
|
199 nl = args(2).nint_value (); |
|
200 nu = args(3).nint_value (); |
|
201 |
|
202 if (error_state) |
|
203 error ("matrix_type: band size must be integer"); |
|
204 else |
|
205 { |
|
206 if (nl == 1 && nu == 1) |
|
207 mattyp.mark_as_tridiagonal (); |
|
208 else |
|
209 mattyp.mark_as_banded (nu, nl); |
|
210 |
|
211 if (str_typ == "banded positive definite") |
|
212 mattyp.mark_as_symmetric (); |
|
213 } |
|
214 } |
|
215 } |
|
216 else if (str_typ == "positive definite") |
|
217 { |
|
218 mattyp.mark_as_full (); |
|
219 mattyp.mark_as_symmetric (); |
|
220 } |
|
221 else if (str_typ == "singular") |
|
222 mattyp.mark_as_rectangular (); |
|
223 else if (str_typ == "full") |
|
224 mattyp.mark_as_full (); |
|
225 else if (str_typ == "unknown") |
|
226 mattyp.invalidate_type (); |
|
227 else |
|
228 error ("matrix_type: Unknown matrix type %s", str_typ.c_str()); |
|
229 |
|
230 if (! error_state) |
|
231 { |
|
232 if (nargin == 3 && (str_typ == "upper" || str_typ == "lower")) |
|
233 { |
|
234 const ColumnVector perm = |
|
235 ColumnVector (args (2).vector_value ()); |
|
236 |
|
237 if (error_state) |
|
238 error ("matrix_type: Invalid permutation vector"); |
|
239 else |
|
240 { |
|
241 int len = perm.length (); |
|
242 dim_vector dv = args(0).dims (); |
|
243 |
|
244 if (len != dv(0)) |
|
245 error ("matrix_type: Invalid permutation vector"); |
|
246 else |
|
247 { |
|
248 OCTAVE_LOCAL_BUFFER (octave_idx_type, p, len); |
|
249 |
|
250 for (int i = 0; i < len; i++) |
|
251 p[i] = (int) (perm (i)); |
|
252 |
|
253 if (str_typ == "upper") |
|
254 mattyp.mark_as_permuted (len, p); |
|
255 else |
|
256 mattyp.mark_as_permuted (len, p); |
|
257 } |
|
258 } |
|
259 } |
|
260 else if (nargin != 2 && str_typ != "banded positive definite" && |
|
261 str_typ != "banded") |
|
262 error ("matrix_type: Invalid number of arguments"); |
|
263 |
|
264 if (! error_state) |
|
265 { |
|
266 // Set the matrix type |
|
267 if (args(0).type_name () == "sparse complex matrix" ) |
|
268 retval = |
|
269 octave_value (args(0).sparse_complex_matrix_value (), |
|
270 mattyp); |
|
271 else |
|
272 retval = octave_value (args(0).sparse_matrix_value (), |
|
273 mattyp); |
|
274 } |
|
275 } |
|
276 } |
|
277 } |
|
278 } |
|
279 else |
|
280 error ("matrix_type: Only sparse matrices treated at the moment"); |
|
281 } |
|
282 |
|
283 return retval; |
|
284 } |
|
285 |
|
286 /* |
|
287 ;;; Local Variables: *** |
|
288 ;;; mode: C++ *** |
|
289 ;;; End: *** |
|
290 */ |