5323
|
1 /* |
|
2 |
|
3 Copyright (C) 2005 David Bateman |
|
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 |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
5720
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
5323
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
5403
|
28 #include <algorithm> |
|
29 |
5323
|
30 #include "ov.h" |
|
31 #include "defun-dld.h" |
|
32 #include "error.h" |
|
33 #include "ov-re-sparse.h" |
|
34 #include "ov-cx-sparse.h" |
|
35 #include "SparseType.h" |
|
36 |
|
37 DEFUN_DLD (matrix_type, args, , |
|
38 "-*- texinfo -*-\n\ |
|
39 @deftypefn {Loadable Function} {@var{type} =} matrix_type (@var{a})\n\ |
|
40 @deftypefnx {Loadable Function} {@var{a} =} matrix_type (@var{a}, @var{type})\n\ |
|
41 @deftypefnx {Loadable Function} {@var{a} =} matrix_type (@var{a}, 'upper', @var{perm})\n\ |
|
42 @deftypefnx {Loadable Function} {@var{a} =} matrix_type (@var{a}, 'lower', @var{perm})\n\ |
|
43 @deftypefnx {Loadable Function} {@var{a} =} matrix_type (@var{a}, 'banded', @var{nl}, @var{nu})\n\ |
|
44 Identify the matrix type or mark a matrix as a particular type. This allows rapid\n\ |
|
45 for solutions of linear equations involving @var{a} to be performed. Called with a\n\ |
|
46 single argument, @code{matrix_type} returns the type of the matrix and caches it for\n\ |
|
47 future use. Called with more than one argument, @code{matrix_type} allows the type\n\ |
|
48 of the matrix to be defined.\n\ |
|
49 \n\ |
|
50 The possible matrix types depend on whether the matrix is full or sparse, and can be\n\ |
|
51 one of the following\n\ |
|
52 \n\ |
|
53 @table @asis\n\ |
|
54 @item 'unknown'\n\ |
|
55 Remove any previously cached matrix type, and mark type as unknown\n\ |
|
56 \n\ |
|
57 @item 'full'\n\ |
|
58 Mark the matrix as full.\n\ |
|
59 \n\ |
|
60 @item 'positive definite'\n\ |
|
61 Full positive definite matrix.\n\ |
|
62 \n\ |
|
63 @item 'diagonal'\n\ |
|
64 Diagonal Matrix. (Sparse matrices only)\n\ |
|
65 \n\ |
|
66 @item 'permuted diagonal'\n\ |
|
67 Permuted Diagonal matrix. The permutation does not need to be specifically\n\ |
|
68 indicated, as the structure of the matrix explicitly gives this. (Sparse matrices\n\ |
|
69 only)\n\ |
|
70 \n\ |
|
71 @item 'upper'\n\ |
|
72 Upper triangular. If the optional third argument @var{perm} is given, the matrix is\n\ |
|
73 assumed to be a permuted upper triangular with the permutations defined by the\n\ |
|
74 vector @var{perm}.\n\ |
|
75 \n\ |
|
76 @item 'lower'\n\ |
|
77 Lower triangular. If the optional third argument @var{perm} is given, the matrix is\n\ |
|
78 assumed to be a permuted lower triangular with the permutations defined by the\n\ |
|
79 vector @var{perm}.\n\ |
|
80 \n\ |
|
81 @item 'banded'\n\ |
|
82 @itemx 'banded positive definite'\n\ |
|
83 Banded matrix with the band size of @var{nl} below the diagonal and @var{nu} above\n\ |
|
84 it. If @var{nl} and @var{nu} are 1, then the matrix is tridiagonal and treated\n\ |
|
85 with specialized code. In addition the matrix can be marked as positive definite\n\ |
|
86 (Sparse matrices only)\n\ |
|
87 \n\ |
|
88 @item 'singular'\n\ |
|
89 The matrix is assumed to be singular and will be treated with a minimum norm solution\n\ |
|
90 \n\ |
|
91 @end table\n\ |
|
92 \n\ |
|
93 Note that the matrix type will be discovered automatically on the first attempt to\n\ |
|
94 solve a linear equation involving @var{a}. Therefore @code{matrix_type} is only\n\ |
5324
|
95 useful to give Octave hints of the matrix type. Incorrectly defining the\n\ |
5323
|
96 matrix type will result in incorrect results from solutions of linear equations,\n\ |
|
97 and so it is entirely the responsibility of the user to correctly indentify the\n\ |
|
98 matrix type.\n\ |
|
99 @end deftypefn") |
|
100 { |
|
101 int nargin = args.length (); |
|
102 octave_value retval; |
|
103 |
|
104 if (nargin == 0) |
|
105 print_usage ("matrix_type"); |
|
106 else if (nargin > 4) |
|
107 error ("matrix_type: incorrect number of arguments"); |
|
108 else |
|
109 { |
5631
|
110 if (args(0).is_sparse_type ()) |
5323
|
111 { |
|
112 if (nargin == 1) |
|
113 { |
|
114 SparseType mattyp; |
|
115 |
|
116 if (args(0).type_name () == "sparse complex matrix" ) |
|
117 { |
5760
|
118 const octave_sparse_complex_matrix& rep |
|
119 = dynamic_cast<const octave_sparse_complex_matrix&> (args(0).get_rep ()); |
|
120 |
|
121 mattyp = rep.sparse_type (); |
5323
|
122 |
|
123 if (mattyp.is_unknown ()) |
|
124 { |
5681
|
125 SparseComplexMatrix m = |
|
126 args(0).sparse_complex_matrix_value (); |
|
127 if (!error_state) |
|
128 { |
|
129 mattyp = SparseType (m); |
5760
|
130 rep.sparse_type (mattyp); |
5681
|
131 } |
5323
|
132 } |
|
133 } |
|
134 else |
|
135 { |
5760
|
136 const octave_sparse_matrix& rep |
|
137 = dynamic_cast<const octave_sparse_matrix&> (args(0).get_rep ()); |
|
138 |
|
139 mattyp = rep.sparse_type (); |
5323
|
140 |
|
141 if (mattyp.is_unknown ()) |
|
142 { |
5681
|
143 SparseMatrix m = args(0).sparse_matrix_value (); |
|
144 if (!error_state) |
|
145 { |
|
146 mattyp = SparseType (m); |
5760
|
147 rep.sparse_type (mattyp); |
5681
|
148 } |
5323
|
149 } |
|
150 } |
|
151 |
|
152 int typ = mattyp.type (); |
|
153 |
|
154 if (typ == SparseType::Diagonal) |
|
155 retval = octave_value ("Diagonal"); |
|
156 else if (typ == SparseType::Permuted_Diagonal) |
|
157 retval = octave_value ("Permuted Diagonal"); |
|
158 else if (typ == SparseType::Upper) |
|
159 retval = octave_value ("Upper"); |
|
160 else if (typ == SparseType::Permuted_Upper) |
|
161 retval = octave_value ("Permuted Upper"); |
|
162 else if (typ == SparseType::Lower) |
|
163 retval = octave_value ("Lower"); |
|
164 else if (typ == SparseType::Permuted_Lower) |
|
165 retval = octave_value ("Permuted Lower"); |
|
166 else if (typ == SparseType::Banded) |
|
167 retval = octave_value ("Banded"); |
|
168 else if (typ == SparseType::Banded_Hermitian) |
|
169 retval = octave_value ("Banded Positive Definite"); |
|
170 else if (typ == SparseType::Tridiagonal) |
|
171 retval = octave_value ("Tridiagonal"); |
|
172 else if (typ == SparseType::Tridiagonal_Hermitian) |
|
173 retval = octave_value ("Tridiagonal Positive Definite"); |
|
174 else if (typ == SparseType::Hermitian) |
|
175 retval = octave_value ("Positive Definite"); |
5610
|
176 else if (typ == SparseType::Rectangular) |
|
177 { |
|
178 if (args(0).rows() == args(0).columns()) |
|
179 retval = octave_value ("Singular"); |
|
180 else |
|
181 retval = octave_value ("Rectangular"); |
|
182 } |
5323
|
183 else if (typ == SparseType::Full) |
|
184 retval = octave_value ("Full"); |
|
185 else |
|
186 // This should never happen!!! |
|
187 retval = octave_value ("Unknown"); |
|
188 } |
|
189 else |
|
190 { |
|
191 // Ok, we're changing the matrix type |
|
192 std::string str_typ = args(1).string_value (); |
|
193 |
5775
|
194 // FIXME -- why do I have to explicitly call the constructor? |
5323
|
195 SparseType mattyp = SparseType (); |
|
196 |
5506
|
197 octave_idx_type nl = 0; |
|
198 octave_idx_type nu = 0; |
5323
|
199 |
|
200 if (error_state) |
|
201 error ("Matrix type must be a string"); |
|
202 else |
|
203 { |
|
204 // Use STL function to convert to lower case |
5403
|
205 std::transform (str_typ.begin (), str_typ.end (), |
5406
|
206 str_typ.begin (), tolower); |
5323
|
207 |
|
208 if (str_typ == "diagonal") |
|
209 mattyp.mark_as_diagonal (); |
|
210 if (str_typ == "permuted diagonal") |
|
211 mattyp.mark_as_permuted_diagonal (); |
|
212 else if (str_typ == "upper") |
|
213 mattyp.mark_as_upper_triangular (); |
|
214 else if (str_typ == "lower") |
|
215 mattyp.mark_as_lower_triangular (); |
|
216 else if (str_typ == "banded" || str_typ == "banded positive definite") |
|
217 { |
|
218 if (nargin != 4) |
|
219 error ("matrix_type: banded matrix type requires 4 arguments"); |
|
220 else |
|
221 { |
|
222 nl = args(2).nint_value (); |
|
223 nu = args(3).nint_value (); |
|
224 |
|
225 if (error_state) |
|
226 error ("matrix_type: band size must be integer"); |
|
227 else |
|
228 { |
|
229 if (nl == 1 && nu == 1) |
|
230 mattyp.mark_as_tridiagonal (); |
|
231 else |
|
232 mattyp.mark_as_banded (nu, nl); |
|
233 |
|
234 if (str_typ == "banded positive definite") |
|
235 mattyp.mark_as_symmetric (); |
|
236 } |
|
237 } |
|
238 } |
|
239 else if (str_typ == "positive definite") |
|
240 { |
|
241 mattyp.mark_as_full (); |
|
242 mattyp.mark_as_symmetric (); |
|
243 } |
|
244 else if (str_typ == "singular") |
|
245 mattyp.mark_as_rectangular (); |
|
246 else if (str_typ == "full") |
|
247 mattyp.mark_as_full (); |
|
248 else if (str_typ == "unknown") |
|
249 mattyp.invalidate_type (); |
|
250 else |
|
251 error ("matrix_type: Unknown matrix type %s", str_typ.c_str()); |
|
252 |
|
253 if (! error_state) |
|
254 { |
|
255 if (nargin == 3 && (str_typ == "upper" || str_typ == "lower")) |
|
256 { |
|
257 const ColumnVector perm = |
|
258 ColumnVector (args (2).vector_value ()); |
|
259 |
|
260 if (error_state) |
|
261 error ("matrix_type: Invalid permutation vector"); |
|
262 else |
|
263 { |
5506
|
264 octave_idx_type len = perm.length (); |
5323
|
265 dim_vector dv = args(0).dims (); |
|
266 |
|
267 if (len != dv(0)) |
|
268 error ("matrix_type: Invalid permutation vector"); |
|
269 else |
|
270 { |
|
271 OCTAVE_LOCAL_BUFFER (octave_idx_type, p, len); |
|
272 |
5506
|
273 for (octave_idx_type i = 0; i < len; i++) |
5760
|
274 p[i] = static_cast<octave_idx_type> (perm (i)) - 1; |
5323
|
275 |
|
276 if (str_typ == "upper") |
|
277 mattyp.mark_as_permuted (len, p); |
|
278 else |
|
279 mattyp.mark_as_permuted (len, p); |
|
280 } |
|
281 } |
|
282 } |
|
283 else if (nargin != 2 && str_typ != "banded positive definite" && |
|
284 str_typ != "banded") |
|
285 error ("matrix_type: Invalid number of arguments"); |
|
286 |
|
287 if (! error_state) |
|
288 { |
|
289 // Set the matrix type |
|
290 if (args(0).type_name () == "sparse complex matrix" ) |
|
291 retval = |
|
292 octave_value (args(0).sparse_complex_matrix_value (), |
|
293 mattyp); |
|
294 else |
|
295 retval = octave_value (args(0).sparse_matrix_value (), |
|
296 mattyp); |
|
297 } |
|
298 } |
|
299 } |
|
300 } |
|
301 } |
|
302 else |
|
303 error ("matrix_type: Only sparse matrices treated at the moment"); |
|
304 } |
|
305 |
|
306 return retval; |
|
307 } |
|
308 |
|
309 /* |
5681
|
310 |
5775
|
311 ## FIXME |
5681
|
312 ## Disable tests for lower under-determined and upper over-determined |
|
313 ## matrices and this detection is disabled in SparseType due to issues |
|
314 ## of non minimum norm solution being found. |
5610
|
315 |
|
316 %!assert(matrix_type(speye(10,10)),"Diagonal"); |
|
317 %!assert(matrix_type(speye(10,10)([2:10,1],:)),"Permuted Diagonal"); |
|
318 %!assert(matrix_type([[speye(10,10);sparse(1,10)],[1;sparse(9,1);1]]),"Upper"); |
|
319 %!assert(matrix_type([[speye(10,10);sparse(1,10)],[1;sparse(9,1);1]](:,[2,1,3:11])),"Permuted Upper"); |
|
320 %!assert(matrix_type([speye(10,10),sparse(10,1);1,sparse(1,9),1]),"Lower"); |
|
321 %!assert(matrix_type([speye(10,10),sparse(10,1);1,sparse(1,9),1]([2,1,3:11],:)),"Permuted Lower"); |
|
322 %!test |
|
323 %! bnd=spparms("bandden"); |
|
324 %! spparms("bandden",0.5); |
|
325 %! a = spdiags(randn(10,3),[-1,0,1],10,10); |
|
326 %! assert(matrix_type(a),"Tridiagonal"); |
|
327 %! assert(matrix_type(abs(a')+abs(a)),"Tridiagonal Positive Definite"); |
|
328 %! spparms("bandden",bnd); |
|
329 %!test |
|
330 %! bnd=spparms("bandden"); |
|
331 %! spparms("bandden",0.5); |
|
332 %! a = spdiags(randn(10,4),[-2:1],10,10); |
|
333 %! assert(matrix_type(a),"Banded"); |
|
334 %! assert(matrix_type(a'*a),"Banded Positive Definite"); |
|
335 %! spparms("bandden",bnd); |
|
336 %!test |
|
337 %! a=[speye(10,10),[sparse(9,1);1];-1,sparse(1,9),1]; |
|
338 %! assert(matrix_type(a),"Full"); |
|
339 %! assert(matrix_type(a'*a),"Positive Definite"); |
5630
|
340 %!assert(matrix_type(speye(10,11)),"Diagonal"); |
|
341 %!assert(matrix_type(speye(10,11)([2:10,1],:)),"Permuted Diagonal"); |
|
342 %!assert(matrix_type(speye(11,10)),"Diagonal"); |
|
343 %!assert(matrix_type(speye(11,10)([2:11,1],:)),"Permuted Diagonal"); |
5681
|
344 %#!assert(matrix_type([[speye(10,10);sparse(1,10)],[[1,1];sparse(9,2);[1,1]]]),"Upper"); |
|
345 %#!assert(matrix_type([[speye(10,10);sparse(1,10)],[[1,1];sparse(9,2);[1,1]]](:,[2,1,3:12])),"Permuted Upper"); |
5630
|
346 %!assert(matrix_type([speye(11,9),[1;sparse(8,1);1;0]]),"Upper"); |
|
347 %!assert(matrix_type([speye(11,9),[1;sparse(8,1);1;0]](:,[2,1,3:10])),"Permuted Upper"); |
5681
|
348 %#!assert(matrix_type([speye(10,10),sparse(10,1);[1;1],sparse(2,9),[1;1]]),"Lower"); |
|
349 %#!assert(matrix_type([speye(10,10),sparse(10,1);[1;1],sparse(2,9),[1;1]]([2,1,3:12],:)),"Permuted Lower"); |
5630
|
350 %!assert(matrix_type([speye(9,11);[1,sparse(1,8),1,0]]),"Lower"); |
|
351 %!assert(matrix_type([speye(9,11);[1,sparse(1,8),1,0]]([2,1,3:10],:)),"Permuted Lower"); |
|
352 %!assert(matrix_type(spdiags(randn(10,4),[-2:1],10,9)),"Rectangular") |
5610
|
353 |
|
354 %!assert(matrix_type(1i*speye(10,10)),"Diagonal"); |
|
355 %!assert(matrix_type(1i*speye(10,10)([2:10,1],:)),"Permuted Diagonal"); |
|
356 %!assert(matrix_type([[speye(10,10);sparse(1,10)],[1i;sparse(9,1);1]]),"Upper"); |
|
357 %!assert(matrix_type([[speye(10,10);sparse(1,10)],[1i;sparse(9,1);1]](:,[2,1,3:11])),"Permuted Upper"); |
|
358 %!assert(matrix_type([speye(10,10),sparse(10,1);1i,sparse(1,9),1]),"Lower"); |
|
359 %!assert(matrix_type([speye(10,10),sparse(10,1);1i,sparse(1,9),1]([2,1,3:11],:)),"Permuted Lower"); |
|
360 %!test |
|
361 %! bnd=spparms("bandden"); |
|
362 %! spparms("bandden",0.5); |
|
363 %! assert(matrix_type(spdiags(1i*randn(10,3),[-1,0,1],10,10)),"Tridiagonal"); |
|
364 %! a = 1i*randn(9,1);a=[[a;0],ones(10,1),[0;-a]]; |
|
365 %! assert(matrix_type(spdiags(a,[-1,0,1],10,10)),"Tridiagonal Positive Definite"); |
|
366 %! spparms("bandden",bnd); |
|
367 %!test |
|
368 %! bnd=spparms("bandden"); |
|
369 %! spparms("bandden",0.5); |
|
370 %! assert(matrix_type(spdiags(1i*randn(10,4),[-2:1],10,10)),"Banded"); |
|
371 %! a = 1i*randn(9,2);a=[[a;[0,0]],ones(10,1),[[0;-a(:,2)],[0;0;-a(1:8,1)]]]; |
|
372 %! assert(matrix_type(spdiags(a,[-2:2],10,10)),"Banded Positive Definite"); |
|
373 %! spparms("bandden",bnd); |
|
374 %!test |
|
375 %! a=[speye(10,10),[sparse(9,1);1i];-1,sparse(1,9),1]; |
|
376 %! assert(matrix_type(a),"Full"); |
|
377 %! assert(matrix_type(a'*a),"Positive Definite"); |
5630
|
378 %!assert(matrix_type(1i*speye(10,11)),"Diagonal"); |
|
379 %!assert(matrix_type(1i*speye(10,11)([2:10,1],:)),"Permuted Diagonal"); |
|
380 %!assert(matrix_type(1i*speye(11,10)),"Diagonal"); |
|
381 %!assert(matrix_type(1i*speye(11,10)([2:11,1],:)),"Permuted Diagonal"); |
5681
|
382 %#!assert(matrix_type([[speye(10,10);sparse(1,10)],[[1i,1i];sparse(9,2);[1i,1i]]]),"Upper"); |
|
383 %#!assert(matrix_type([[speye(10,10);sparse(1,10)],[[1i,1i];sparse(9,2);[1i,1i]]](:,[2,1,3:12])),"Permuted Upper"); |
5630
|
384 %!assert(matrix_type([speye(11,9),[1i;sparse(8,1);1i;0]]),"Upper"); |
|
385 %!assert(matrix_type([speye(11,9),[1i;sparse(8,1);1i;0]](:,[2,1,3:10])),"Permuted Upper"); |
5681
|
386 %#!assert(matrix_type([speye(10,10),sparse(10,1);[1i;1i],sparse(2,9),[1i;1i]]),"Lower"); |
|
387 %#!assert(matrix_type([speye(10,10),sparse(10,1);[1i;1i],sparse(2,9),[1i;1i]]([2,1,3:12],:)),"Permuted Lower"); |
5630
|
388 %!assert(matrix_type([speye(9,11);[1i,sparse(1,8),1i,0]]),"Lower"); |
|
389 %!assert(matrix_type([speye(9,11);[1i,sparse(1,8),1i,0]]([2,1,3:10],:)),"Permuted Lower"); |
|
390 %!assert(matrix_type(1i*spdiags(randn(10,4),[-2:1],10,9)),"Rectangular") |
5610
|
391 |
|
392 %!test |
|
393 %! a = matrix_type(spdiags(randn(10,3),[-1,0,1],10,10),"Singular"); |
|
394 %! assert(matrix_type(a),"Singular"); |
|
395 |
|
396 */ |
|
397 |
|
398 /* |
5323
|
399 ;;; Local Variables: *** |
|
400 ;;; mode: C++ *** |
|
401 ;;; End: *** |
|
402 */ |