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 { |
5631
|
107 if (args(0).is_sparse_type ()) |
5323
|
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"); |
5610
|
160 else if (typ == SparseType::Rectangular) |
|
161 { |
|
162 if (args(0).rows() == args(0).columns()) |
|
163 retval = octave_value ("Singular"); |
|
164 else |
|
165 retval = octave_value ("Rectangular"); |
|
166 } |
5323
|
167 else if (typ == SparseType::Full) |
|
168 retval = octave_value ("Full"); |
|
169 else |
|
170 // This should never happen!!! |
|
171 retval = octave_value ("Unknown"); |
|
172 } |
|
173 else |
|
174 { |
|
175 // Ok, we're changing the matrix type |
|
176 std::string str_typ = args(1).string_value (); |
|
177 |
|
178 // XXX FIXME, why do I have to explicitly call the constructor? |
|
179 SparseType mattyp = SparseType (); |
|
180 |
5506
|
181 octave_idx_type nl = 0; |
|
182 octave_idx_type nu = 0; |
5323
|
183 |
|
184 if (error_state) |
|
185 error ("Matrix type must be a string"); |
|
186 else |
|
187 { |
|
188 // Use STL function to convert to lower case |
5403
|
189 std::transform (str_typ.begin (), str_typ.end (), |
5406
|
190 str_typ.begin (), tolower); |
5323
|
191 |
|
192 if (str_typ == "diagonal") |
|
193 mattyp.mark_as_diagonal (); |
|
194 if (str_typ == "permuted diagonal") |
|
195 mattyp.mark_as_permuted_diagonal (); |
|
196 else if (str_typ == "upper") |
|
197 mattyp.mark_as_upper_triangular (); |
|
198 else if (str_typ == "lower") |
|
199 mattyp.mark_as_lower_triangular (); |
|
200 else if (str_typ == "banded" || str_typ == "banded positive definite") |
|
201 { |
|
202 if (nargin != 4) |
|
203 error ("matrix_type: banded matrix type requires 4 arguments"); |
|
204 else |
|
205 { |
|
206 nl = args(2).nint_value (); |
|
207 nu = args(3).nint_value (); |
|
208 |
|
209 if (error_state) |
|
210 error ("matrix_type: band size must be integer"); |
|
211 else |
|
212 { |
|
213 if (nl == 1 && nu == 1) |
|
214 mattyp.mark_as_tridiagonal (); |
|
215 else |
|
216 mattyp.mark_as_banded (nu, nl); |
|
217 |
|
218 if (str_typ == "banded positive definite") |
|
219 mattyp.mark_as_symmetric (); |
|
220 } |
|
221 } |
|
222 } |
|
223 else if (str_typ == "positive definite") |
|
224 { |
|
225 mattyp.mark_as_full (); |
|
226 mattyp.mark_as_symmetric (); |
|
227 } |
|
228 else if (str_typ == "singular") |
|
229 mattyp.mark_as_rectangular (); |
|
230 else if (str_typ == "full") |
|
231 mattyp.mark_as_full (); |
|
232 else if (str_typ == "unknown") |
|
233 mattyp.invalidate_type (); |
|
234 else |
|
235 error ("matrix_type: Unknown matrix type %s", str_typ.c_str()); |
|
236 |
|
237 if (! error_state) |
|
238 { |
|
239 if (nargin == 3 && (str_typ == "upper" || str_typ == "lower")) |
|
240 { |
|
241 const ColumnVector perm = |
|
242 ColumnVector (args (2).vector_value ()); |
|
243 |
|
244 if (error_state) |
|
245 error ("matrix_type: Invalid permutation vector"); |
|
246 else |
|
247 { |
5506
|
248 octave_idx_type len = perm.length (); |
5323
|
249 dim_vector dv = args(0).dims (); |
|
250 |
|
251 if (len != dv(0)) |
|
252 error ("matrix_type: Invalid permutation vector"); |
|
253 else |
|
254 { |
|
255 OCTAVE_LOCAL_BUFFER (octave_idx_type, p, len); |
|
256 |
5506
|
257 for (octave_idx_type i = 0; i < len; i++) |
|
258 p[i] = (octave_idx_type) (perm (i)) - 1; |
5323
|
259 |
|
260 if (str_typ == "upper") |
|
261 mattyp.mark_as_permuted (len, p); |
|
262 else |
|
263 mattyp.mark_as_permuted (len, p); |
|
264 } |
|
265 } |
|
266 } |
|
267 else if (nargin != 2 && str_typ != "banded positive definite" && |
|
268 str_typ != "banded") |
|
269 error ("matrix_type: Invalid number of arguments"); |
|
270 |
|
271 if (! error_state) |
|
272 { |
|
273 // Set the matrix type |
|
274 if (args(0).type_name () == "sparse complex matrix" ) |
|
275 retval = |
|
276 octave_value (args(0).sparse_complex_matrix_value (), |
|
277 mattyp); |
|
278 else |
|
279 retval = octave_value (args(0).sparse_matrix_value (), |
|
280 mattyp); |
|
281 } |
|
282 } |
|
283 } |
|
284 } |
|
285 } |
|
286 else |
|
287 error ("matrix_type: Only sparse matrices treated at the moment"); |
|
288 } |
|
289 |
|
290 return retval; |
|
291 } |
|
292 |
|
293 /* |
5610
|
294 |
|
295 %!assert(matrix_type(speye(10,10)),"Diagonal"); |
|
296 %!assert(matrix_type(speye(10,10)([2:10,1],:)),"Permuted Diagonal"); |
|
297 %!assert(matrix_type([[speye(10,10);sparse(1,10)],[1;sparse(9,1);1]]),"Upper"); |
|
298 %!assert(matrix_type([[speye(10,10);sparse(1,10)],[1;sparse(9,1);1]](:,[2,1,3:11])),"Permuted Upper"); |
|
299 %!assert(matrix_type([speye(10,10),sparse(10,1);1,sparse(1,9),1]),"Lower"); |
|
300 %!assert(matrix_type([speye(10,10),sparse(10,1);1,sparse(1,9),1]([2,1,3:11],:)),"Permuted Lower"); |
|
301 %!test |
|
302 %! bnd=spparms("bandden"); |
|
303 %! spparms("bandden",0.5); |
|
304 %! a = spdiags(randn(10,3),[-1,0,1],10,10); |
|
305 %! assert(matrix_type(a),"Tridiagonal"); |
|
306 %! assert(matrix_type(abs(a')+abs(a)),"Tridiagonal Positive Definite"); |
|
307 %! spparms("bandden",bnd); |
|
308 %!test |
|
309 %! bnd=spparms("bandden"); |
|
310 %! spparms("bandden",0.5); |
|
311 %! a = spdiags(randn(10,4),[-2:1],10,10); |
|
312 %! assert(matrix_type(a),"Banded"); |
|
313 %! assert(matrix_type(a'*a),"Banded Positive Definite"); |
|
314 %! spparms("bandden",bnd); |
|
315 %!test |
|
316 %! a=[speye(10,10),[sparse(9,1);1];-1,sparse(1,9),1]; |
|
317 %! assert(matrix_type(a),"Full"); |
|
318 %! assert(matrix_type(a'*a),"Positive Definite"); |
5630
|
319 %!assert(matrix_type(speye(10,11)),"Diagonal"); |
|
320 %!assert(matrix_type(speye(10,11)([2:10,1],:)),"Permuted Diagonal"); |
|
321 %!assert(matrix_type(speye(11,10)),"Diagonal"); |
|
322 %!assert(matrix_type(speye(11,10)([2:11,1],:)),"Permuted Diagonal"); |
|
323 %!assert(matrix_type([[speye(10,10);sparse(1,10)],[[1,1];sparse(9,2);[1,1]]]),"Upper"); |
|
324 %!assert(matrix_type([[speye(10,10);sparse(1,10)],[[1,1];sparse(9,2);[1,1]]](:,[2,1,3:12])),"Permuted Upper"); |
|
325 %!assert(matrix_type([speye(11,9),[1;sparse(8,1);1;0]]),"Upper"); |
|
326 %!assert(matrix_type([speye(11,9),[1;sparse(8,1);1;0]](:,[2,1,3:10])),"Permuted Upper"); |
|
327 %!assert(matrix_type([speye(10,10),sparse(10,1);[1;1],sparse(2,9),[1;1]]),"Lower"); |
|
328 %!assert(matrix_type([speye(10,10),sparse(10,1);[1;1],sparse(2,9),[1;1]]([2,1,3:12],:)),"Permuted Lower"); |
|
329 %!assert(matrix_type([speye(9,11);[1,sparse(1,8),1,0]]),"Lower"); |
|
330 %!assert(matrix_type([speye(9,11);[1,sparse(1,8),1,0]]([2,1,3:10],:)),"Permuted Lower"); |
|
331 %!assert(matrix_type(spdiags(randn(10,4),[-2:1],10,9)),"Rectangular") |
5610
|
332 |
|
333 %!assert(matrix_type(1i*speye(10,10)),"Diagonal"); |
|
334 %!assert(matrix_type(1i*speye(10,10)([2:10,1],:)),"Permuted Diagonal"); |
|
335 %!assert(matrix_type([[speye(10,10);sparse(1,10)],[1i;sparse(9,1);1]]),"Upper"); |
|
336 %!assert(matrix_type([[speye(10,10);sparse(1,10)],[1i;sparse(9,1);1]](:,[2,1,3:11])),"Permuted Upper"); |
|
337 %!assert(matrix_type([speye(10,10),sparse(10,1);1i,sparse(1,9),1]),"Lower"); |
|
338 %!assert(matrix_type([speye(10,10),sparse(10,1);1i,sparse(1,9),1]([2,1,3:11],:)),"Permuted Lower"); |
|
339 %!test |
|
340 %! bnd=spparms("bandden"); |
|
341 %! spparms("bandden",0.5); |
|
342 %! assert(matrix_type(spdiags(1i*randn(10,3),[-1,0,1],10,10)),"Tridiagonal"); |
|
343 %! a = 1i*randn(9,1);a=[[a;0],ones(10,1),[0;-a]]; |
|
344 %! assert(matrix_type(spdiags(a,[-1,0,1],10,10)),"Tridiagonal Positive Definite"); |
|
345 %! spparms("bandden",bnd); |
|
346 %!test |
|
347 %! bnd=spparms("bandden"); |
|
348 %! spparms("bandden",0.5); |
|
349 %! assert(matrix_type(spdiags(1i*randn(10,4),[-2:1],10,10)),"Banded"); |
|
350 %! a = 1i*randn(9,2);a=[[a;[0,0]],ones(10,1),[[0;-a(:,2)],[0;0;-a(1:8,1)]]]; |
|
351 %! assert(matrix_type(spdiags(a,[-2:2],10,10)),"Banded Positive Definite"); |
|
352 %! spparms("bandden",bnd); |
|
353 %!test |
|
354 %! a=[speye(10,10),[sparse(9,1);1i];-1,sparse(1,9),1]; |
|
355 %! assert(matrix_type(a),"Full"); |
|
356 %! assert(matrix_type(a'*a),"Positive Definite"); |
5630
|
357 %!assert(matrix_type(1i*speye(10,11)),"Diagonal"); |
|
358 %!assert(matrix_type(1i*speye(10,11)([2:10,1],:)),"Permuted Diagonal"); |
|
359 %!assert(matrix_type(1i*speye(11,10)),"Diagonal"); |
|
360 %!assert(matrix_type(1i*speye(11,10)([2:11,1],:)),"Permuted Diagonal"); |
|
361 %!assert(matrix_type([[speye(10,10);sparse(1,10)],[[1i,1i];sparse(9,2);[1i,1i]]]),"Upper"); |
|
362 %!assert(matrix_type([[speye(10,10);sparse(1,10)],[[1i,1i];sparse(9,2);[1i,1i]]](:,[2,1,3:12])),"Permuted Upper"); |
|
363 %!assert(matrix_type([speye(11,9),[1i;sparse(8,1);1i;0]]),"Upper"); |
|
364 %!assert(matrix_type([speye(11,9),[1i;sparse(8,1);1i;0]](:,[2,1,3:10])),"Permuted Upper"); |
|
365 %!assert(matrix_type([speye(10,10),sparse(10,1);[1i;1i],sparse(2,9),[1i;1i]]),"Lower"); |
|
366 %!assert(matrix_type([speye(10,10),sparse(10,1);[1i;1i],sparse(2,9),[1i;1i]]([2,1,3:12],:)),"Permuted Lower"); |
|
367 %!assert(matrix_type([speye(9,11);[1i,sparse(1,8),1i,0]]),"Lower"); |
|
368 %!assert(matrix_type([speye(9,11);[1i,sparse(1,8),1i,0]]([2,1,3:10],:)),"Permuted Lower"); |
|
369 %!assert(matrix_type(1i*spdiags(randn(10,4),[-2:1],10,9)),"Rectangular") |
5610
|
370 |
|
371 %!test |
|
372 %! a = matrix_type(spdiags(randn(10,3),[-1,0,1],10,10),"Singular"); |
|
373 %! assert(matrix_type(a),"Singular"); |
|
374 |
|
375 */ |
|
376 |
|
377 /* |
5323
|
378 ;;; Local Variables: *** |
|
379 ;;; mode: C++ *** |
|
380 ;;; End: *** |
|
381 */ |