5164
|
1 @c Copyright (C) 2004, 2005 David Bateman |
|
2 @c This is part of the Octave manual. |
|
3 @c For copying conditions, see the file gpl.texi. |
|
4 |
5648
|
5 @ifhtml |
|
6 @set htmltex |
|
7 @end ifhtml |
|
8 @iftex |
|
9 @set htmltex |
|
10 @end iftex |
|
11 |
5164
|
12 @node Sparse Matrices |
|
13 @chapter Sparse Matrices |
|
14 |
|
15 @menu |
|
16 * Basics:: The Creation and Manipulation of Sparse Matrices |
|
17 * Sparse Linear Algebra:: Linear Algebra on Sparse Matrices |
|
18 * Iterative Techniques:: Iterative Techniques applied to Sparse Matrices |
5648
|
19 * Real Life Example:: Real Life Example of the use of Sparse Matrices |
5164
|
20 * Function Reference:: Documentation from the Specific Sparse Functions |
|
21 @end menu |
|
22 |
5648
|
23 @node Basics, Sparse Linear Algebra, Sparse Matrices, Sparse Matrices |
5164
|
24 @section The Creation and Manipulation of Sparse Matrices |
|
25 |
|
26 The size of mathematical problems that can be treated at any particular |
|
27 time is generally limited by the available computing resources. Both, |
|
28 the speed of the computer and its available memory place limitation on |
|
29 the problem size. |
|
30 |
5506
|
31 There are many classes of mathematical problems which give rise to |
5164
|
32 matrices, where a large number of the elements are zero. In this case |
|
33 it makes sense to have a special matrix type to handle this class of |
|
34 problems where only the non-zero elements of the matrix are |
5506
|
35 stored. Not only does this reduce the amount of memory to store the |
5164
|
36 matrix, but it also means that operations on this type of matrix can |
|
37 take advantage of the a-priori knowledge of the positions of the |
|
38 non-zero elements to accelerate their calculations. |
|
39 |
|
40 A matrix type that stores only the non-zero elements is generally called |
|
41 sparse. It is the purpose of this document to discuss the basics of the |
|
42 storage and creation of sparse matrices and the fundamental operations |
|
43 on them. |
|
44 |
|
45 @menu |
|
46 * Storage:: Storage of Sparse Matrices |
|
47 * Creation:: Creating Sparse Matrices |
5648
|
48 * Information:: Finding out Information about Sparse Matrices |
5164
|
49 * Operators and Functions:: Basic Operators and Functions on Sparse Matrices |
|
50 @end menu |
|
51 |
|
52 @node Storage, Creation, Basics, Basics |
|
53 @subsection Storage of Sparse Matrices |
|
54 |
|
55 It is not strictly speaking necessary for the user to understand how |
|
56 sparse matrices are stored. However, such an understanding will help |
|
57 to get an understanding of the size of sparse matrices. Understanding |
|
58 the storage technique is also necessary for those users wishing to |
|
59 create their own oct-files. |
|
60 |
|
61 There are many different means of storing sparse matrix data. What all |
5648
|
62 of the methods have in common is that they attempt to reduce the complexity |
5164
|
63 and storage given a-priori knowledge of the particular class of problems |
|
64 that will be solved. A good summary of the available techniques for storing |
|
65 sparse matrix is given by Saad @footnote{Youcef Saad "SPARSKIT: A basic toolkit |
|
66 for sparse matrix computation", 1994, |
|
67 @url{ftp://ftp.cs.umn.edu/dept/sparse/SPARSKIT2/DOC/paper.ps}}. |
|
68 With full matrices, knowledge of the point of an element of the matrix |
|
69 within the matrix is implied by its position in the computers memory. |
|
70 However, this is not the case for sparse matrices, and so the positions |
|
71 of the non-zero elements of the matrix must equally be stored. |
|
72 |
|
73 An obvious way to do this is by storing the elements of the matrix as |
5506
|
74 triplets, with two elements being their position in the array |
5164
|
75 (rows and column) and the third being the data itself. This is conceptually |
|
76 easy to grasp, but requires more storage than is strictly needed. |
|
77 |
5648
|
78 The storage technique used within Octave is the compressed column |
5164
|
79 format. In this format the position of each element in a row and the |
|
80 data are stored as previously. However, if we assume that all elements |
|
81 in the same column are stored adjacent in the computers memory, then |
|
82 we only need to store information on the number of non-zero elements |
|
83 in each column, rather than their positions. Thus assuming that the |
|
84 matrix has more non-zero elements than there are columns in the |
|
85 matrix, we win in terms of the amount of memory used. |
|
86 |
|
87 In fact, the column index contains one more element than the number of |
|
88 columns, with the first element always being zero. The advantage of |
5648
|
89 this is a simplification in the code, in that their is no special case |
5164
|
90 for the first or last columns. A short example, demonstrating this in |
|
91 C is. |
|
92 |
|
93 @example |
|
94 for (j = 0; j < nc; j++) |
|
95 for (i = cidx (j); i < cidx(j+1); i++) |
5648
|
96 printf ("non-zero element (%i,%i) is %d\n", |
|
97 ridx(i), j, data(i)); |
5164
|
98 @end example |
|
99 |
|
100 A clear understanding might be had by considering an example of how the |
|
101 above applies to an example matrix. Consider the matrix |
|
102 |
|
103 @example |
|
104 @group |
|
105 1 2 0 0 |
|
106 0 0 0 3 |
|
107 0 0 0 4 |
|
108 @end group |
|
109 @end example |
|
110 |
|
111 The non-zero elements of this matrix are |
|
112 |
|
113 @example |
|
114 @group |
|
115 (1, 1) @result{} 1 |
|
116 (1, 2) @result{} 2 |
|
117 (2, 4) @result{} 3 |
|
118 (3, 4) @result{} 4 |
|
119 @end group |
|
120 @end example |
|
121 |
|
122 This will be stored as three vectors @var{cidx}, @var{ridx} and @var{data}, |
|
123 representing the column indexing, row indexing and data respectively. The |
|
124 contents of these three vectors for the above matrix will be |
|
125 |
|
126 @example |
|
127 @group |
5506
|
128 @var{cidx} = [0, 1, 2, 2, 4] |
5164
|
129 @var{ridx} = [0, 0, 1, 2] |
|
130 @var{data} = [1, 2, 3, 4] |
|
131 @end group |
|
132 @end example |
|
133 |
|
134 Note that this is the representation of these elements with the first row |
5648
|
135 and column assumed to start at zero, while in Octave itself the row and |
|
136 column indexing starts at one. Thus the number of elements in the |
5164
|
137 @var{i}-th column is given by @code{@var{cidx} (@var{i} + 1) - |
|
138 @var{cidx} (@var{i})}. |
|
139 |
5648
|
140 Although Octave uses a compressed column format, it should be noted |
|
141 that compressed row formats are equally possible. However, in the |
|
142 context of mixed operations between mixed sparse and dense matrices, |
|
143 it makes sense that the elements of the sparse matrices are in the |
|
144 same order as the dense matrices. Octave stores dense matrices in |
|
145 column major ordering, and so sparse matrices are equally stored in |
|
146 this manner. |
5164
|
147 |
5324
|
148 A further constraint on the sparse matrix storage used by Octave is that |
5164
|
149 all elements in the rows are stored in increasing order of their row |
5506
|
150 index, which makes certain operations faster. However, it imposes |
5164
|
151 the need to sort the elements on the creation of sparse matrices. Having |
|
152 dis-ordered elements is potentially an advantage in that it makes operations |
|
153 such as concatenating two sparse matrices together easier and faster, however |
|
154 it adds complexity and speed problems elsewhere. |
|
155 |
5648
|
156 @node Creation, Information, Storage, Basics |
5164
|
157 @subsection Creating Sparse Matrices |
|
158 |
|
159 There are several means to create sparse matrix. |
|
160 |
|
161 @table @asis |
|
162 @item Returned from a function |
|
163 There are many functions that directly return sparse matrices. These include |
|
164 @dfn{speye}, @dfn{sprand}, @dfn{spdiag}, etc. |
|
165 @item Constructed from matrices or vectors |
|
166 The function @dfn{sparse} allows a sparse matrix to be constructed from |
5506
|
167 three vectors representing the row, column and data. Alternatively, the |
5164
|
168 function @dfn{spconvert} uses a three column matrix format to allow easy |
|
169 importation of data from elsewhere. |
|
170 @item Created and then filled |
|
171 The function @dfn{sparse} or @dfn{spalloc} can be used to create an empty |
|
172 matrix that is then filled by the user |
|
173 @item From a user binary program |
|
174 The user can directly create the sparse matrix within an oct-file. |
|
175 @end table |
|
176 |
|
177 There are several basic functions to return specific sparse |
|
178 matrices. For example the sparse identity matrix, is a matrix that is |
|
179 often needed. It therefore has its own function to create it as |
|
180 @code{speye (@var{n})} or @code{speye (@var{r}, @var{c})}, which |
|
181 creates an @var{n}-by-@var{n} or @var{r}-by-@var{c} sparse identity |
|
182 matrix. |
|
183 |
|
184 Another typical sparse matrix that is often needed is a random distribution |
|
185 of random elements. The functions @dfn{sprand} and @dfn{sprandn} perform |
5506
|
186 this for uniform and normal random distributions of elements. They have exactly |
|
187 the same calling convention, where @code{sprand (@var{r}, @var{c}, @var{d})}, |
|
188 creates an @var{r}-by-@var{c} sparse matrix with a density of filled |
5164
|
189 elements of @var{d}. |
|
190 |
5506
|
191 Other functions of interest that directly creates a sparse matrices, are |
5164
|
192 @dfn{spdiag} or its generalization @dfn{spdiags}, that can take the |
|
193 definition of the diagonals of the matrix and create the sparse matrix |
|
194 that corresponds to this. For example |
|
195 |
|
196 @example |
6411
|
197 s = spdiag (sparse(randn(1,n)), -1); |
5164
|
198 @end example |
|
199 |
|
200 creates a sparse (@var{n}+1)-by-(@var{n}+1) sparse matrix with a single |
|
201 diagonal defined. |
|
202 |
|
203 The recommended way for the user to create a sparse matrix, is to create |
5648
|
204 two vectors containing the row and column index of the data and a third |
5164
|
205 vector of the same size containing the data to be stored. For example |
|
206 |
|
207 @example |
6421
|
208 ri = ci = d = []; |
|
209 for j = 1:c |
|
210 ri = [ri; randperm(r)(1:n)']; |
|
211 ci = [ci; j*ones(n,1)]; |
|
212 d = [d; rand(n,1)]; |
|
213 endfor |
|
214 s = sparse (ri, ci, d, r, c); |
5164
|
215 @end example |
|
216 |
6421
|
217 creates an @var{r}-by-@var{c} sparse matrix with a random distribution |
|
218 of @var{n} (<@var{r}) elements per column. The elements of the vectors |
|
219 do not need to be sorted in any particular order as Octave will sort |
|
220 them prior to storing the data. However, pre-sorting the data will |
|
221 make the creation of the sparse matrix faster. |
5164
|
222 |
|
223 The function @dfn{spconvert} takes a three or four column real matrix. |
|
224 The first two columns represent the row and column index respectively and |
|
225 the third and four columns, the real and imaginary parts of the sparse |
|
226 matrix. The matrix can contain zero elements and the elements can be |
|
227 sorted in any order. Adding zero elements is a convenient way to define |
|
228 the size of the sparse matrix. For example |
|
229 |
|
230 @example |
|
231 s = spconvert ([1 2 3 4; 1 3 4 4; 1 2 3 0]') |
|
232 @result{} Compressed Column Sparse (rows=4, cols=4, nnz=3) |
|
233 (1 , 1) -> 1 |
|
234 (2 , 3) -> 2 |
|
235 (3 , 4) -> 3 |
|
236 @end example |
|
237 |
|
238 An example of creating and filling a matrix might be |
|
239 |
|
240 @example |
|
241 k = 5; |
|
242 nz = r * k; |
|
243 s = spalloc (r, c, nz) |
|
244 for j = 1:c |
|
245 idx = randperm (r); |
5648
|
246 s (:, j) = [zeros(r - k, 1); ... |
|
247 rand(k, 1)] (idx); |
5164
|
248 endfor |
|
249 @end example |
|
250 |
5324
|
251 It should be noted, that due to the way that the Octave |
5164
|
252 assignment functions are written that the assignment will reallocate |
5506
|
253 the memory used by the sparse matrix at each iteration of the above loop. |
|
254 Therefore the @dfn{spalloc} function ignores the @var{nz} argument and |
|
255 does not preassign the memory for the matrix. Therefore, it is vitally |
5648
|
256 important that code using to above structure should be vectorized |
|
257 as much as possible to minimize the number of assignments and reduce the |
5164
|
258 number of memory allocations. |
|
259 |
6570
|
260 The above problem can be avoided in oct-files. However, the construction |
|
261 of a sparse matrix from an oct-file is more complex than can be |
|
262 discussed in this brief introduction, and you are referred to chapter |
|
263 @ref{Dynamically Linked Functions}, to have a full description of the |
|
264 techniques involved. |
5164
|
265 |
5648
|
266 @node Information, Operators and Functions, Creation, Basics |
|
267 @subsection Finding out Information about Sparse Matrices |
|
268 |
|
269 There are a number of functions that allow information concerning |
|
270 sparse matrices to be obtained. The most basic of these is |
|
271 @dfn{issparse} that identifies whether a particular Octave object is |
|
272 in fact a sparse matrix. |
|
273 |
|
274 Another very basic function is @dfn{nnz} that returns the number of |
|
275 non-zero entries there are in a sparse matrix, while the function |
|
276 @dfn{nzmax} returns the amount of storage allocated to the sparse |
|
277 matrix. Note that Octave tends to crop unused memory at the first |
|
278 opportunity for sparse objects. There are some cases of user created |
|
279 sparse objects where the value returned by @dfn{nzmaz} will not be |
|
280 the same as @dfn{nnz}, but in general they will give the same |
|
281 result. The function @dfn{spstats} returns some basic statistics on |
|
282 the columns of a sparse matrix including the number of elements, the |
|
283 mean and the variance of each column. |
|
284 |
|
285 When solving linear equations involving sparse matrices Octave |
|
286 determines the means to solve the equation based on the type of the |
|
287 matrix as discussed in @ref{Sparse Linear Algebra}. Octave probes the |
|
288 matrix type when the div (/) or ldiv (\) operator is first used with |
|
289 the matrix and then caches the type. However the @dfn{matrix_type} |
|
290 function can be used to determine the type of the sparse matrix prior |
|
291 to use of the div or ldiv operators. For example |
|
292 |
|
293 @example |
|
294 a = tril (sprandn(1024, 1024, 0.02), -1) ... |
|
295 + speye(1024); |
|
296 matrix_type (a); |
|
297 ans = Lower |
|
298 @end example |
|
299 |
|
300 show that Octave correctly determines the matrix type for lower |
|
301 triangular matrices. @dfn{matrix_type} can also be used to force |
|
302 the type of a matrix to be a particular type. For example |
|
303 |
|
304 @example |
|
305 a = matrix_type (tril (sprandn (1024, ... |
|
306 1024, 0.02), -1) + speye(1024), 'Lower'); |
|
307 @end example |
|
308 |
|
309 This allows the cost of determining the matrix type to be |
|
310 avoided. However, incorrectly defining the matrix type will result in |
|
311 incorrect results from solutions of linear equations, and so it is |
|
312 entirely the responsibility of the user to correctly identify the |
|
313 matrix type |
|
314 |
|
315 There are several graphical means of finding out information about |
|
316 sparse matrices. The first is the @dfn{spy} command, which displays |
|
317 the structure of the non-zero elements of the |
|
318 matrix. @xref{fig:spmatrix}, for an exaple of the use of |
5704
|
319 @dfn{spy}. More advanced graphical information can be obtained with the |
5648
|
320 @dfn{treeplot}, @dfn{etreeplot} and @dfn{gplot} commands. |
|
321 |
|
322 @float Figure,fig:spmatrix |
|
323 @image{spmatrix,8cm} |
|
324 @caption{Structure of simple sparse matrix.} |
|
325 @end float |
|
326 |
|
327 One use of sparse matrices is in graph theory, where the |
|
328 interconnections between nodes is represented as an adjacency |
|
329 matrix. That is, if the i-th node in a graph is connected to the j-th |
|
330 node. Then the ij-th node (and in the case of undirected graphs the |
|
331 ji-th node) of the sparse adjacency matrix is non-zero. If each node |
|
332 is then associated with a set of co-ordinates, then the @dfn{gplot} |
|
333 command can be used to graphically display the interconnections |
|
334 between nodes. |
|
335 |
|
336 As a trivial example of the use of @dfn{gplot}, consider the example |
|
337 |
|
338 @example |
|
339 A = sparse([2,6,1,3,2,4,3,5,4,6,1,5], |
|
340 [1,1,2,2,3,3,4,4,5,5,6,6],1,6,6); |
|
341 xy = [0,4,8,6,4,2;5,0,5,7,5,7]'; |
|
342 gplot(A,xy) |
|
343 @end example |
|
344 |
|
345 which creates an adjacency matrix @code{A} where node 1 is connected |
|
346 to nodes 2 and 6, node 2 with nodes 1 and 3, etc. The co-ordinates of |
|
347 the nodes are given in the n-by-2 matrix @code{xy}. |
|
348 @ifset htmltex |
|
349 @xref{fig:gplot}. |
|
350 |
|
351 @float Figure,fig:gplot |
|
352 @image{gplot,8cm} |
|
353 @caption{Simple use of the @dfn{gplot} command.} |
|
354 @end float |
|
355 @end ifset |
|
356 |
|
357 The dependencies between the nodes of a Cholesky factorization can be |
|
358 calculated in linear time without explicitly needing to calculate the |
|
359 Cholesky factorization by the @code{etree} command. This command |
|
360 returns the elimination tree of the matrix and can be displayed |
|
361 graphically by the command @code{treeplot(etree(A))} if @code{A} is |
|
362 symmetric or @code{treeplot(etree(A+A'))} otherwise. |
|
363 |
|
364 @node Operators and Functions, , Information, Basics |
5164
|
365 @subsection Basic Operators and Functions on Sparse Matrices |
|
366 |
|
367 @menu |
5648
|
368 * Functions:: Sparse Functions |
5164
|
369 * ReturnType:: The Return Types of Operators and Functions |
|
370 * MathConsiderations:: Mathematical Considerations |
|
371 @end menu |
|
372 |
|
373 @node Functions, ReturnType, Operators and Functions, Operators and Functions |
5648
|
374 @subsubsection Sparse Functions |
|
375 |
|
376 An important consideration in the use of the sparse functions of |
|
377 Octave is that many of the internal functions of Octave, such as |
|
378 @dfn{diag}, can not accept sparse matrices as an input. The sparse |
|
379 implementation in Octave therefore uses the @dfn{dispatch} |
|
380 function to overload the normal Octave functions with equivalent |
|
381 functions that work with sparse matrices. However, at any time the |
|
382 sparse matrix specific version of the function can be used by |
|
383 explicitly calling its function name. |
|
384 |
|
385 The table below lists all of the sparse functions of Octave |
|
386 together (with possible future extensions that are currently |
|
387 unimplemented, listed last). Note that in this specific sparse forms |
|
388 of the functions are typically the same as the general versions with a |
|
389 @dfn{sp} prefix. In the table below, and the rest of this article |
|
390 the specific sparse versions of the functions are used. |
|
391 |
|
392 @table @asis |
|
393 @item Generate sparse matrices: |
|
394 @dfn{spalloc}, @dfn{spdiags}, @dfn{speye}, @dfn{sprand}, |
|
395 @dfn{sprandn}, @dfn{sprandsym} |
|
396 |
|
397 @item Sparse matrix conversion: |
|
398 @dfn{full}, @dfn{sparse}, @dfn{spconvert}, @dfn{spfind} |
|
399 |
|
400 @item Manipulate sparse matrices |
|
401 @dfn{issparse}, @dfn{nnz}, @dfn{nonzeros}, @dfn{nzmax}, |
|
402 @dfn{spfun}, @dfn{spones}, @dfn{spy}, |
5164
|
403 |
5648
|
404 @item Graph Theory: |
|
405 @dfn{etree}, @dfn{etreeplot}, @dfn{gplot}, |
|
406 @dfn{treeplot}, (treelayout) |
|
407 |
|
408 @item Sparse matrix reordering: |
|
409 @dfn{ccolamd}, @dfn{colamd}, @dfn{colperm}, |
|
410 @dfn{csymamd}, @dfn{dmperm}, @dfn{symamd}, @dfn{randperm}, (symrcm) |
|
411 |
|
412 @item Linear algebra: |
|
413 @dfn{matrix\_type}, @dfn{spchol}, @dfn{cpcholinv}, |
|
414 @dfn{spchol2inv}, @dfn{spdet}, @dfn{spinv}, @dfn{spkron}, |
|
415 @dfn{splchol}, @dfn{splu}, @dfn{spqr}, (condest, eigs, normest, |
|
416 sprank, svds, spaugment) |
|
417 |
|
418 @item Iterative techniques: |
5837
|
419 @dfn{luinc}, @dfn{pcg}, @dfn{pcr}, (bicg, bicgstab, cholinc, cgs, |
|
420 gmres, lsqr, minres, qmr, symmlq) |
5648
|
421 |
|
422 @item Miscellaneous: |
|
423 @dfn{spparms}, @dfn{symbfact}, @dfn{spstats}, |
|
424 @dfn{spprod}, @dfn{spcumsum}, @dfn{spsum}, |
|
425 @dfn{spsumsq}, @dfn{spmin}, @dfn{spmax}, @dfn{spatan2}, |
|
426 @dfn{spdiag} |
|
427 @end table |
|
428 |
|
429 In addition all of the standard Octave mapper functions (ie. basic |
|
430 math functions that take a single argument) such as @dfn{abs}, etc |
|
431 can accept sparse matrices. The reader is referred to the documentation |
|
432 supplied with these functions within Octave itself for further |
|
433 details. |
5164
|
434 |
|
435 @node ReturnType, MathConsiderations, Functions, Operators and Functions |
|
436 @subsubsection The Return Types of Operators and Functions |
|
437 |
5506
|
438 The two basic reasons to use sparse matrices are to reduce the memory |
5164
|
439 usage and to not have to do calculations on zero elements. The two are |
|
440 closely related in that the computation time on a sparse matrix operator |
5506
|
441 or function is roughly linear with the number of non-zero elements. |
5164
|
442 |
|
443 Therefore, there is a certain density of non-zero elements of a matrix |
|
444 where it no longer makes sense to store it as a sparse matrix, but rather |
|
445 as a full matrix. For this reason operators and functions that have a |
|
446 high probability of returning a full matrix will always return one. For |
|
447 example adding a scalar constant to a sparse matrix will almost always |
|
448 make it a full matrix, and so the example |
|
449 |
|
450 @example |
|
451 speye(3) + 0 |
|
452 @result{} 1 0 0 |
|
453 0 1 0 |
|
454 0 0 1 |
|
455 @end example |
|
456 |
|
457 returns a full matrix as can be seen. Additionally all sparse functions |
|
458 test the amount of memory occupied by the sparse matrix to see if the |
|
459 amount of storage used is larger than the amount used by the full |
|
460 equivalent. Therefore @code{speye (2) * 1} will return a full matrix as |
|
461 the memory used is smaller for the full version than the sparse version. |
|
462 |
|
463 As all of the mixed operators and functions between full and sparse |
|
464 matrices exist, in general this does not cause any problems. However, |
|
465 one area where it does cause a problem is where a sparse matrix is |
|
466 promoted to a full matrix, where subsequent operations would resparsify |
5648
|
467 the matrix. Such cases are rare, but can be artificially created, for |
5164
|
468 example @code{(fliplr(speye(3)) + speye(3)) - speye(3)} gives a full |
|
469 matrix when it should give a sparse one. In general, where such cases |
|
470 occur, they impose only a small memory penalty. |
|
471 |
5648
|
472 There is however one known case where this behavior of Octave's |
5164
|
473 sparse matrices will cause a problem. That is in the handling of the |
|
474 @dfn{diag} function. Whether @dfn{diag} returns a sparse or full matrix |
|
475 depending on the type of its input arguments. So |
|
476 |
|
477 @example |
|
478 a = diag (sparse([1,2,3]), -1); |
|
479 @end example |
|
480 |
|
481 should return a sparse matrix. To ensure this actually happens, the |
|
482 @dfn{sparse} function, and other functions based on it like @dfn{speye}, |
|
483 always returns a sparse matrix, even if the memory used will be larger |
|
484 than its full representation. |
|
485 |
|
486 @node MathConsiderations, , ReturnType, Operators and Functions |
|
487 @subsubsection Mathematical Considerations |
|
488 |
|
489 The attempt has been made to make sparse matrices behave in exactly the |
|
490 same manner as there full counterparts. However, there are certain differences |
|
491 and especially differences with other products sparse implementations. |
|
492 |
|
493 Firstly, the "./" and ".^" operators must be used with care. Consider what |
|
494 the examples |
|
495 |
|
496 @example |
|
497 s = speye (4); |
|
498 a1 = s .^ 2; |
|
499 a2 = s .^ s; |
|
500 a3 = s .^ -2; |
|
501 a4 = s ./ 2; |
|
502 a5 = 2 ./ s; |
|
503 a6 = s ./ s; |
|
504 @end example |
|
505 |
|
506 will give. The first example of @var{s} raised to the power of 2 causes |
|
507 no problems. However @var{s} raised element-wise to itself involves a |
6431
|
508 large number of terms @code{0 .^ 0} which is 1. There @code{@var{s} .^ |
5164
|
509 @var{s}} is a full matrix. |
|
510 |
|
511 Likewise @code{@var{s} .^ -2} involves terms terms like @code{0 .^ -2} which |
|
512 is infinity, and so @code{@var{s} .^ -2} is equally a full matrix. |
|
513 |
|
514 For the "./" operator @code{@var{s} ./ 2} has no problems, but |
|
515 @code{2 ./ @var{s}} involves a large number of infinity terms as well |
|
516 and is equally a full matrix. The case of @code{@var{s} ./ @var{s}} |
|
517 involves terms like @code{0 ./ 0} which is a @code{NaN} and so this |
|
518 is equally a full matrix with the zero elements of @var{s} filled with |
|
519 @code{NaN} values. |
|
520 |
5648
|
521 The above behavior is consistent with full matrices, but is not |
5164
|
522 consistent with sparse implementations in other products. |
|
523 |
|
524 A particular problem of sparse matrices comes about due to the fact that |
|
525 as the zeros are not stored, the sign-bit of these zeros is equally not |
5506
|
526 stored. In certain cases the sign-bit of zero is important. For example |
5164
|
527 |
|
528 @example |
|
529 a = 0 ./ [-1, 1; 1, -1]; |
|
530 b = 1 ./ a |
|
531 @result{} -Inf Inf |
|
532 Inf -Inf |
|
533 c = 1 ./ sparse (a) |
|
534 @result{} Inf Inf |
|
535 Inf Inf |
|
536 @end example |
|
537 |
5648
|
538 To correct this behavior would mean that zero elements with a negative |
5164
|
539 sign-bit would need to be stored in the matrix to ensure that their |
|
540 sign-bit was respected. This is not done at this time, for reasons of |
|
541 efficient, and so the user is warned that calculations where the sign-bit |
|
542 of zero is important must not be done using sparse matrices. |
|
543 |
5648
|
544 In general any function or operator used on a sparse matrix will |
|
545 result in a sparse matrix with the same or a larger number of non-zero |
|
546 elements than the original matrix. This is particularly true for the |
|
547 important case of sparse matrix factorizations. The usual way to |
|
548 address this is to reorder the matrix, such that its factorization is |
|
549 sparser than the factorization of the original matrix. That is the |
|
550 factorization of @code{L * U = P * S * Q} has sparser terms @code{L} |
|
551 and @code{U} than the equivalent factorization @code{L * U = S}. |
|
552 |
|
553 Several functions are available to reorder depending on the type of the |
|
554 matrix to be factorized. If the matrix is symmetric positive-definite, |
|
555 then @dfn{symamd} or @dfn{csymamd} should be used. Otherwise |
|
556 @dfn{colamd} or @dfn{ccolamd} should be used. For completeness |
|
557 the reordering functions @dfn{colperm} and @dfn{randperm} are |
|
558 also available. |
|
559 |
|
560 @xref{fig:simplematrix}, for an example of the structure of a simple |
|
561 positive definite matrix. |
5506
|
562 |
5648
|
563 @float Figure,fig:simplematrix |
|
564 @image{spmatrix,8cm} |
|
565 @caption{Structure of simple sparse matrix.} |
|
566 @end float |
5506
|
567 |
5648
|
568 The standard Cholesky factorization of this matrix, can be |
|
569 obtained by the same command that would be used for a full |
5652
|
570 matrix. This can be visualized with the command |
|
571 @code{r = chol(A); spy(r);}. |
|
572 @ifset HAVE_CHOLMOD |
|
573 @ifset HAVE_COLAMD |
|
574 @xref{fig:simplechol}. |
|
575 @end ifset |
|
576 @end ifset |
|
577 The original matrix had |
5648
|
578 @ifinfo |
|
579 @ifnothtml |
|
580 43 |
|
581 @end ifnothtml |
|
582 @end ifinfo |
|
583 @ifset htmltex |
|
584 598 |
|
585 @end ifset |
|
586 non-zero terms, while this Cholesky factorization has |
|
587 @ifinfo |
|
588 @ifnothtml |
|
589 71, |
|
590 @end ifnothtml |
|
591 @end ifinfo |
|
592 @ifset htmltex |
|
593 10200, |
|
594 @end ifset |
|
595 with only half of the symmetric matrix being stored. This |
|
596 is a significant level of fill in, and although not an issue |
|
597 for such a small test case, can represents a large overhead |
|
598 in working with other sparse matrices. |
5164
|
599 |
5648
|
600 The appropriate sparsity preserving permutation of the original |
|
601 matrix is given by @dfn{symamd} and the factorization using this |
|
602 reordering can be visualized using the command @code{q = symamd(A); |
|
603 r = chol(A(q,q)); spy(r)}. This gives |
|
604 @ifinfo |
|
605 @ifnothtml |
|
606 29 |
|
607 @end ifnothtml |
|
608 @end ifinfo |
|
609 @ifset htmltex |
|
610 399 |
|
611 @end ifset |
|
612 non-zero terms which is a significant improvement. |
5164
|
613 |
5648
|
614 The Cholesky factorization itself can be used to determine the |
|
615 appropriate sparsity preserving reordering of the matrix during the |
|
616 factorization, In that case this might be obtained with three return |
|
617 arguments as r@code{[r, p, q] = chol(A); spy(r)}. |
5164
|
618 |
5648
|
619 @ifset HAVE_CHOLMOD |
|
620 @ifset HAVE_COLAMD |
|
621 @float Figure,fig:simplechol |
|
622 @image{spchol,8cm} |
|
623 @caption{Structure of the un-permuted Cholesky factorization of the above matrix.} |
|
624 @end float |
5164
|
625 |
5648
|
626 @float Figure,fig:simplecholperm |
|
627 @image{spcholperm,8cm} |
|
628 @caption{Structure of the permuted Cholesky factorization of the above matrix.} |
|
629 @end float |
|
630 @end ifset |
|
631 @end ifset |
5164
|
632 |
5648
|
633 In the case of an asymmetric matrix, the appropriate sparsity |
|
634 preserving permutation is @dfn{colamd} and the factorization using |
|
635 this reordering can be visualized using the command @code{q = |
|
636 colamd(A); [l, u, p] = lu(A(:,q)); spy(l+u)}. |
5164
|
637 |
5648
|
638 Finally, Octave implicitly reorders the matrix when using the div (/) |
|
639 and ldiv (\) operators, and so no the user does not need to explicitly |
|
640 reorder the matrix to maximize performance. |
|
641 |
|
642 @node Sparse Linear Algebra, Iterative Techniques, Basics, Sparse Matrices |
5164
|
643 @section Linear Algebra on Sparse Matrices |
|
644 |
5324
|
645 Octave includes a poly-morphic solver for sparse matrices, where |
5164
|
646 the exact solver used to factorize the matrix, depends on the properties |
5648
|
647 of the sparse matrix itself. Generally, the cost of determining the matrix type |
5322
|
648 is small relative to the cost of factorizing the matrix itself, but in any |
|
649 case the matrix type is cached once it is calculated, so that it is not |
|
650 re-determined each time it is used in a linear equation. |
5164
|
651 |
|
652 The selection tree for how the linear equation is solve is |
|
653 |
|
654 @enumerate 1 |
5648
|
655 @item If the matrix is diagonal, solve directly and goto 8 |
5164
|
656 |
|
657 @item If the matrix is a permuted diagonal, solve directly taking into |
5648
|
658 account the permutations. Goto 8 |
5164
|
659 |
5648
|
660 @item If the matrix is square, banded and if the band density is less |
|
661 than that given by @code{spparms ("bandden")} continue, else goto 4. |
5164
|
662 |
|
663 @enumerate a |
|
664 @item If the matrix is tridiagonal and the right-hand side is not sparse |
5648
|
665 continue, else goto 3b. |
5164
|
666 |
|
667 @enumerate |
|
668 @item If the matrix is hermitian, with a positive real diagonal, attempt |
|
669 Cholesky factorization using @sc{Lapack} xPTSV. |
|
670 |
|
671 @item If the above failed or the matrix is not hermitian with a positive |
|
672 real diagonal use Gaussian elimination with pivoting using |
5648
|
673 @sc{Lapack} xGTSV, and goto 8. |
5164
|
674 @end enumerate |
|
675 |
|
676 @item If the matrix is hermitian with a positive real diagonal, attempt |
|
677 Cholesky factorization using @sc{Lapack} xPBTRF. |
|
678 |
|
679 @item if the above failed or the matrix is not hermitian with a positive |
|
680 real diagonal use Gaussian elimination with pivoting using |
5648
|
681 @sc{Lapack} xGBTRF, and goto 8. |
5164
|
682 @end enumerate |
|
683 |
|
684 @item If the matrix is upper or lower triangular perform a sparse forward |
5648
|
685 or backward substitution, and goto 8 |
5164
|
686 |
5322
|
687 @item If the matrix is a upper triangular matrix with column permutations |
|
688 or lower triangular matrix with row permutations, perform a sparse forward |
5648
|
689 or backward substitution, and goto 8 |
5164
|
690 |
5648
|
691 @item If the matrix is square, hermitian with a real positive diagonal, attempt |
5506
|
692 sparse Cholesky factorization using CHOLMOD. |
5164
|
693 |
|
694 @item If the sparse Cholesky factorization failed or the matrix is not |
5648
|
695 hermitian with a real positive diagonal, and the matrix is square, factorize |
|
696 using UMFPACK. |
5164
|
697 |
|
698 @item If the matrix is not square, or any of the previous solvers flags |
5648
|
699 a singular or near singular matrix, find a minimum norm solution using |
|
700 CXSPARSE@footnote{CHOLMOD, UMFPACK and CXSPARSE are written by Tim Davis |
|
701 and are available at http://www.cise.ufl.edu/research/sparse/}. |
5164
|
702 @end enumerate |
|
703 |
|
704 The band density is defined as the number of non-zero values in the matrix |
|
705 divided by the number of non-zero values in the matrix. The banded matrix |
|
706 solvers can be entirely disabled by using @dfn{spparms} to set @code{bandden} |
|
707 to 1 (i.e. @code{spparms ("bandden", 1)}). |
|
708 |
5681
|
709 The QR solver factorizes the problem with a Dulmage-Mendhelsohn, to |
|
710 seperate the problem into blocks that can be treated as over-determined, |
|
711 multiple well determined blocks, and a final over-determined block. For |
|
712 matrices with blocks of strongly connectted nodes this is a big win as |
|
713 LU decomposition can be used for many blocks. It also significantly |
|
714 improves the chance of finding a solution to over-determined problems |
|
715 rather than just returning a vector of @dfn{NaN}'s. |
|
716 |
|
717 All of the solvers above, can calculate an estimate of the condition |
|
718 number. This can be used to detect numerical stability problems in the |
|
719 solution and force a minimum norm solution to be used. However, for |
|
720 narrow banded, triangular or diagonal matrices, the cost of |
|
721 calculating the condition number is significant, and can in fact |
|
722 exceed the cost of factoring the matrix. Therefore the condition |
|
723 number is not calculated in these case, and octave relies on simplier |
|
724 techniques to detect sinular matrices or the underlying LAPACK code in |
|
725 the case of banded matrices. |
5164
|
726 |
5322
|
727 The user can force the type of the matrix with the @code{matrix_type} |
|
728 function. This overcomes the cost of discovering the type of the matrix. |
|
729 However, it should be noted incorrectly identifying the type of the matrix |
|
730 will lead to unpredictable results, and so @code{matrix_type} should be |
5506
|
731 used with care. |
5322
|
732 |
5648
|
733 @node Iterative Techniques, Real Life Example, Sparse Linear Algebra, Sparse Matrices |
5164
|
734 @section Iterative Techniques applied to sparse matrices |
|
735 |
5837
|
736 There are three functions currently to document here, these being |
|
737 @dfn{luinc}, @dfn{pcg} and @dfn{pcr}. |
|
738 |
|
739 WRITE ME. |
5648
|
740 |
6570
|
741 @node Real Life Example, Function Reference, Iterative Techniques, Sparse Matrices |
5648
|
742 @section Real Life Example of the use of Sparse Matrices |
|
743 |
|
744 A common application for sparse matrices is in the solution of Finite |
|
745 Element Models. Finite element models allow numerical solution of |
|
746 partial differential equations that do not have closed form solutions, |
|
747 typically because of the complex shape of the domain. |
|
748 |
|
749 In order to motivate this application, we consider the boundary value |
|
750 Laplace equation. This system can model scalar potential fields, such |
|
751 as heat or electrical potential. Given a medium |
|
752 @iftex |
|
753 @tex |
|
754 $\Omega$ |
|
755 @end tex |
|
756 @end iftex |
|
757 @ifinfo |
|
758 Omega |
|
759 @end ifinfo |
|
760 with boundary |
|
761 @iftex |
|
762 @tex |
|
763 $\partial\Omega$ |
|
764 @end tex |
|
765 @end iftex |
|
766 @ifinfo |
|
767 dOmega |
|
768 @end ifinfo |
|
769 . At all points on the |
|
770 @iftex |
|
771 @tex |
|
772 $\partial\Omega$ |
|
773 @end tex |
|
774 @end iftex |
|
775 @ifinfo |
|
776 dOmega |
|
777 @end ifinfo |
|
778 the boundary conditions are known, and we wish to calculate the potential in |
|
779 @iftex |
|
780 @tex |
|
781 $\Omega$ |
|
782 @end tex |
|
783 @end iftex |
|
784 @ifinfo |
|
785 Omega |
|
786 @end ifinfo |
|
787 . Boundary conditions may specify the potential (Dirichlet |
|
788 boundary condition), its normal derivative across the boundary |
|
789 (Neumann boundary condition), or a weighted sum of the potential and |
|
790 its derivative (Cauchy boundary condition). |
|
791 |
|
792 In a thermal model, we want to calculate the temperature in |
|
793 @iftex |
|
794 @tex |
|
795 $\Omega$ |
|
796 @end tex |
|
797 @end iftex |
|
798 @ifinfo |
|
799 Omega |
|
800 @end ifinfo |
|
801 and know the boundary temperature (Dirichlet condition) |
|
802 or heat flux (from which we can calculate the Neumann condition |
|
803 by dividing by the thermal conductivity at the boundary). Similarly, |
|
804 in an electrical model, we want to calculate the voltage in |
|
805 @iftex |
|
806 @tex |
|
807 $\Omega$ |
|
808 @end tex |
|
809 @end iftex |
|
810 @ifinfo |
|
811 Omega |
|
812 @end ifinfo |
|
813 and know the boundary voltage (Dirichlet) or current |
|
814 (Neumann condition after diving by the electrical conductivity). |
|
815 In an electrical model, it is common for much of the boundary |
|
816 to be electrically isolated; this is a Neumann boundary condition |
|
817 with the current equal to zero. |
|
818 |
|
819 The simplest finite element models will divide |
|
820 @iftex |
|
821 @tex |
|
822 $\Omega$ |
|
823 @end tex |
|
824 @end iftex |
|
825 @ifinfo |
|
826 Omega |
|
827 @end ifinfo |
|
828 into simplexes (triangles in 2D, pyramids in 3D). |
|
829 @ifset htmltex |
|
830 We take as an 3D example a cylindrical liquid filled tank with a small |
|
831 non-conductive ball from the EIDORS project@footnote{EIDORS - Electrical |
|
832 Impedance Tomography and Diffuse optical Tomography Reconstruction Software |
|
833 @url{http://eidors3d.sourceforge.net}}. This is model is designed to reflect |
|
834 an application of electrical impedance tomography, where current patterns |
|
835 are applied to such a tank in order to image the internal conductivity |
|
836 distribution. In order to describe the FEM geometry, we have a matrix of |
|
837 vertices @code{nodes} and simplices @code{elems}. |
|
838 @end ifset |
|
839 |
|
840 The following example creates a simple rectangular 2D electrically |
|
841 conductive medium with 10 V and 20 V imposed on opposite sides |
|
842 (Dirichlet boundary conditions). All other edges are electrically |
|
843 isolated. |
|
844 |
|
845 @example |
|
846 node_y= [1;1.2;1.5;1.8;2]*ones(1,11); |
|
847 node_x= ones(5,1)*[1,1.05,1.1,1.2, ... |
|
848 1.3,1.5,1.7,1.8,1.9,1.95,2]; |
|
849 nodes= [node_x(:), node_y(:)]; |
|
850 |
|
851 [h,w]= size(node_x); |
|
852 elems= []; |
|
853 for idx= 1:w-1 |
|
854 widx= (idx-1)*h; |
|
855 elems= [elems; ... |
|
856 widx+[(1:h-1);(2:h);h+(1:h-1)]'; ... |
|
857 widx+[(2:h);h+(2:h);h+(1:h-1)]' ]; |
|
858 endfor |
|
859 |
|
860 E= size(elems,1); # No. of simplices |
|
861 N= size(nodes,1); # No. of vertices |
|
862 D= size(elems,2); # dimensions+1 |
|
863 @end example |
|
864 |
|
865 This creates a N-by-2 matrix @code{nodes} and a E-by-3 matrix |
|
866 @code{elems} with values, which define finite element triangles: |
5164
|
867 |
5648
|
868 @example |
|
869 nodes(1:7,:)' |
|
870 1.00 1.00 1.00 1.00 1.00 1.05 1.05 ... |
|
871 1.00 1.20 1.50 1.80 2.00 1.00 1.20 ... |
|
872 |
|
873 elems(1:7,:)' |
|
874 1 2 3 4 2 3 4 ... |
|
875 2 3 4 5 7 8 9 ... |
|
876 6 7 8 9 6 7 8 ... |
|
877 @end example |
|
878 |
|
879 Using a first order FEM, we approximate the electrical conductivity |
|
880 distribution in |
|
881 @iftex |
|
882 @tex |
|
883 $\Omega$ |
|
884 @end tex |
|
885 @end iftex |
|
886 @ifinfo |
|
887 Omega |
|
888 @end ifinfo |
|
889 as constant on each simplex (represented by the vector @code{conductivity}). |
|
890 Based on the finite element geometry, we first calculate a system (or |
|
891 stiffness) matrix for each simplex (represented as 3-by-3 elements on the |
|
892 diagonal of the element-wise system matrix @code{SE}. Based on @code{SE} |
|
893 and a N-by-DE connectivity matrix @code{C}, representing the connections |
|
894 between simplices and vectices, the global connectivity matrix @code{S} is |
|
895 calculated. |
|
896 |
|
897 @example |
|
898 # Element conductivity |
|
899 conductivity= [1*ones(1,16), ... |
|
900 2*ones(1,48), 1*ones(1,16)]; |
|
901 |
|
902 # Connectivity matrix |
|
903 C = sparse ((1:D*E), reshape (elems', ... |
|
904 D*E, 1), 1, D*E, N); |
|
905 |
|
906 # Calculate system matrix |
|
907 Siidx = floor ([0:D*E-1]'/D) * D * ... |
|
908 ones(1,D) + ones(D*E,1)*(1:D) ; |
|
909 Sjidx = [1:D*E]'*ones(1,D); |
|
910 Sdata = zeros(D*E,D); |
|
911 dfact = factorial(D-1); |
|
912 for j=1:E |
|
913 a = inv([ones(D,1), ... |
|
914 nodes(elems(j,:), :)]); |
|
915 const = conductivity(j) * 2 / ... |
|
916 dfact / abs(det(a)); |
|
917 Sdata(D*(j-1)+(1:D),:) = const * ... |
|
918 a(2:D,:)' * a(2:D,:); |
|
919 endfor |
|
920 # Element-wise system matrix |
|
921 SE= sparse(Siidx,Sjidx,Sdata); |
|
922 # Global system matrix |
|
923 S= C'* SE *C; |
|
924 @end example |
|
925 |
|
926 The system matrix acts like the conductivity |
|
927 @iftex |
|
928 @tex |
|
929 $S$ |
|
930 @end tex |
|
931 @end iftex |
|
932 @ifinfo |
|
933 @code{S} |
|
934 @end ifinfo |
|
935 in Ohm's law |
|
936 @iftex |
|
937 @tex |
|
938 $SV = I$. |
|
939 @end tex |
|
940 @end iftex |
|
941 @ifinfo |
|
942 @code{S * V = I}. |
|
943 @end ifinfo |
|
944 Based on the Dirichlet and Neumann boundary conditions, we are able to |
|
945 solve for the voltages at each vertex @code{V}. |
|
946 |
|
947 @example |
|
948 # Dirichlet boundary conditions |
|
949 D_nodes=[1:5, 51:55]; |
|
950 D_value=[10*ones(1,5), 20*ones(1,5)]; |
|
951 |
|
952 V= zeros(N,1); |
|
953 V(D_nodes) = D_value; |
|
954 idx = 1:N; # vertices without Dirichlet |
|
955 # boundary condns |
|
956 idx(D_nodes) = []; |
|
957 |
|
958 # Neumann boundary conditions. Note that |
|
959 # N_value must be normalized by the |
|
960 # boundary length and element conductivity |
|
961 N_nodes=[]; |
|
962 N_value=[]; |
|
963 |
|
964 Q = zeros(N,1); |
|
965 Q(N_nodes) = N_value; |
|
966 |
|
967 V(idx) = S(idx,idx) \ ( Q(idx) - ... |
|
968 S(idx,D_nodes) * V(D_nodes)); |
|
969 @end example |
|
970 |
|
971 Finally, in order to display the solution, we show each solved voltage |
|
972 value in the z-axis for each simplex vertex. |
|
973 @ifset htmltex |
5652
|
974 @ifset HAVE_CHOLMOD |
|
975 @ifset HAVE_UMFPACK |
|
976 @ifset HAVE_COLAMD |
5648
|
977 @xref{fig:femmodel}. |
|
978 @end ifset |
5652
|
979 @end ifset |
|
980 @end ifset |
|
981 @end ifset |
5648
|
982 |
|
983 @example |
|
984 elemx = elems(:,[1,2,3,1])'; |
|
985 xelems = reshape (nodes(elemx, 1), 4, E); |
|
986 yelems = reshape (nodes(elemx, 2), 4, E); |
|
987 velems = reshape (V(elemx), 4, E); |
|
988 plot3 (xelems,yelems,velems,'k'); |
|
989 print ('grid.eps'); |
|
990 @end example |
|
991 |
|
992 |
|
993 @ifset htmltex |
|
994 @ifset HAVE_CHOLMOD |
|
995 @ifset HAVE_UMFPACK |
|
996 @ifset HAVE_COLAMD |
|
997 @float Figure,fig:femmodel |
|
998 @image{grid,8cm} |
|
999 @caption{Example finite element model the showing triangular elements. |
|
1000 The height of each vertex corresponds to the solution value.} |
|
1001 @end float |
|
1002 @end ifset |
|
1003 @end ifset |
|
1004 @end ifset |
|
1005 @end ifset |
|
1006 |
6570
|
1007 @node Function Reference, , Real Life Example, Sparse Matrices |
5164
|
1008 @section Function Reference |
|
1009 |
5648
|
1010 @ifset htmltex |
5164
|
1011 @subsection Functions by Category |
|
1012 @subsubsection Generate sparse matrix |
|
1013 @table @asis |
5648
|
1014 @item @ref{spdiags} |
5164
|
1015 A generalization of the function `spdiag'. |
5648
|
1016 @item @ref{speye} |
5164
|
1017 Returns a sparse identity matrix. |
5648
|
1018 @item @ref{sprand} |
5164
|
1019 Generate a random sparse matrix. |
5648
|
1020 @item @ref{sprandn} |
5164
|
1021 Generate a random sparse matrix. |
5648
|
1022 @item @ref{sprandsym} |
|
1023 Generate a symmetric random sparse matrix. |
5164
|
1024 @end table |
|
1025 @subsubsection Sparse matrix conversion |
|
1026 @table @asis |
5648
|
1027 @item @ref{full} |
5164
|
1028 returns a full storage matrix from a sparse one See also: sparse |
5648
|
1029 @item @ref{sparse} |
5164
|
1030 SPARSE: create a sparse matrix |
5648
|
1031 @item @ref{spconvert} |
5164
|
1032 This function converts for a simple sparse matrix format easily produced by other programs into Octave's internal sparse format. |
5648
|
1033 @item @ref{spfind} |
5164
|
1034 SPFIND: a sparse version of the find operator 1. |
|
1035 @end table |
|
1036 @subsubsection Manipulate sparse matrices |
|
1037 @table @asis |
5648
|
1038 @item @ref{issparse} |
5164
|
1039 Return 1 if the value of the expression EXPR is a sparse matrix. |
5648
|
1040 @item @ref{nnz} |
5164
|
1041 returns number of non zero elements in SM See also: sparse |
5648
|
1042 @item @ref{nonzeros} |
5164
|
1043 Returns a vector of the non-zero values of the sparse matrix S |
5648
|
1044 @item @ref{nzmax} |
5164
|
1045 Returns the amount of storage allocated to the sparse matrix SM. |
5648
|
1046 @item @ref{spalloc} |
5164
|
1047 Returns an empty sparse matrix of size R-by-C. |
5648
|
1048 @item @ref{spfun} |
5164
|
1049 Compute `f(X)' for the non-zero values of X This results in a sparse matrix with the same structure as X. |
5648
|
1050 @item @ref{spones} |
5164
|
1051 Replace the non-zero entries of X with ones. |
5648
|
1052 @item @ref{spy} |
5164
|
1053 Plot the sparsity pattern of the sparse matrix X |
|
1054 @end table |
|
1055 @subsubsection Graph Theory |
|
1056 @table @asis |
5648
|
1057 @item @ref{etree} |
5164
|
1058 Returns the elimination tree for the matrix S. |
5648
|
1059 @item @ref{etreeplot} |
5576
|
1060 Plots the elimination tree of the matrix @var{s} or @code{@var{s}+@var{s}'} |
|
1061 if @var{s} in non-symmetric. |
5648
|
1062 @item @ref{gplot} |
5576
|
1063 Plots a graph defined by @var{A} and @var{xy} in the graph theory sense. |
5164
|
1064 @item treelayout |
|
1065 @emph{Not implemented} |
5648
|
1066 @item @ref{treeplot} |
5576
|
1067 Produces a graph of a tree or forest. |
5164
|
1068 @end table |
|
1069 @subsubsection Sparse matrix reordering |
|
1070 @table @asis |
5648
|
1071 @item @ref{ccolamd} |
5506
|
1072 Constrained column approximate minimum degree permutation. |
5648
|
1073 @item @ref{colamd} |
5164
|
1074 Column approximate minimum degree permutation. |
5648
|
1075 @item @ref{colperm} |
5164
|
1076 Returns the column permutations such that the columns of `S (:, P)' are ordered in terms of increase number of non-zero elements. |
5648
|
1077 @item @ref{csymamd} |
5506
|
1078 For a symmetric positive definite matrix S, returns the permutation vector p such that `S (P, P)' tends to have a sparser Cholesky factor than S. |
5648
|
1079 @item @ref{dmperm} |
5322
|
1080 Perform a Deulmage-Mendelsohn permutation on the sparse matrix S. |
5648
|
1081 @item @ref{symamd} |
5164
|
1082 For a symmetric positive definite matrix S, returns the permutation vector p such that `S (P, P)' tends to have a sparser Cholesky factor than S. |
|
1083 @item symrcm |
5648
|
1084 @emph{Not implemented} |
5164
|
1085 @end table |
|
1086 @subsubsection Linear algebra |
|
1087 @table @asis |
|
1088 @item cholinc |
|
1089 @emph{Not implemented} |
|
1090 @item condest |
|
1091 @emph{Not implemented} |
|
1092 @item eigs |
|
1093 @emph{Not implemented} |
6334
|
1094 @item @ref{normest} |
|
1095 Estimates the 2-norm of the matrix @var{a} using a power series analysis. |
5648
|
1096 @item @ref{spchol} |
5506
|
1097 Compute the Cholesky factor, R, of the symmetric positive definite. |
5648
|
1098 @item @ref{spcholinv} |
5506
|
1099 Use the Cholesky factorization to compute the inverse of the |
|
1100 sparse symmetric positive definite matrix A. |
5648
|
1101 @item @ref{spchol2inv} |
5506
|
1102 Invert a sparse symmetric, positive definite square matrix from its |
|
1103 Cholesky decomposition, U. |
5648
|
1104 @item @ref{spdet} |
5164
|
1105 Compute the determinant of sparse matrix A using UMFPACK. |
5648
|
1106 @item @ref{spinv} |
5164
|
1107 Compute the inverse of the square matrix A. |
5648
|
1108 @item @ref{spkron} |
5322
|
1109 Form the kronecker product of two sparse matrices. |
5648
|
1110 @item @ref{splchol} |
5506
|
1111 Compute the Cholesky factor, L, of the symmetric positive definite. |
5648
|
1112 @item @ref{splu} |
5164
|
1113 Compute the LU decomposition of the sparse matrix A, using subroutines from UMFPACK. |
5648
|
1114 @item @ref{spqr} |
|
1115 Compute the sparse QR factorization of @var{a}, using CSPARSE. |
6334
|
1116 @item @ref{sprank} |
|
1117 Calculates the structural rank of a sparse matrix @var{s}. |
5164
|
1118 @item svds |
|
1119 @emph{Not implemented} |
|
1120 @end table |
|
1121 @subsubsection Iterative techniques |
|
1122 @table @asis |
|
1123 @item bicg |
|
1124 @emph{Not implemented} |
|
1125 @item bicgstab |
|
1126 @emph{Not implemented} |
|
1127 @item cgs |
|
1128 @emph{Not implemented} |
|
1129 @item gmres |
|
1130 @emph{Not implemented} |
5648
|
1131 @item @ref{luinc} |
5282
|
1132 Produce the incomplete LU factorization of the sparse matrix A. |
5164
|
1133 @item lsqr |
|
1134 @emph{Not implemented} |
|
1135 @item minres |
|
1136 @emph{Not implemented} |
|
1137 @item pcg |
5837
|
1138 Solves the linear system of equations @code{@var{A} * @var{x} = |
|
1139 @var{b}} by means of the Preconditioned Conjugate Gradient iterative |
|
1140 method. |
5164
|
1141 @item pcr |
5837
|
1142 Solves the linear system of equations @code{@var{A} * @var{x} = |
|
1143 @var{b}} by means of the Preconditioned Conjugate Residual iterative |
|
1144 method. |
5164
|
1145 @item qmr |
|
1146 @emph{Not implemented} |
|
1147 @item symmlq |
|
1148 @emph{Not implemented} |
|
1149 @end table |
|
1150 @subsubsection Miscellaneous |
|
1151 @table @asis |
|
1152 @item spaugment |
|
1153 @emph{Not implemented} |
5648
|
1154 @item @ref{spparms} |
5164
|
1155 Sets or displays the parameters used by the sparse solvers and factorization functions. |
5648
|
1156 @item @ref{symbfact} |
5164
|
1157 Performs a symbolic factorization analysis on the sparse matrix S. |
5648
|
1158 @item @ref{spstats} |
5164
|
1159 Return the stats for the non-zero elements of the sparse matrix S COUNT is the number of non-zeros in each column, MEAN is the mean of the non-zeros in each column, and VAR is the variance of the non-zeros in each column |
5648
|
1160 @item @ref{spprod} |
5164
|
1161 Product of elements along dimension DIM. |
5648
|
1162 @item @ref{spcumprod} |
5164
|
1163 Cumulative product of elements along dimension DIM. |
5648
|
1164 @item @ref{spcumsum} |
5164
|
1165 Cumulative sum of elements along dimension DIM. |
5648
|
1166 @item @ref{spsum} |
5164
|
1167 Sum of elements along dimension DIM. |
5648
|
1168 @item @ref{spsumsq} |
5164
|
1169 Sum of squares of elements along dimension DIM. |
5648
|
1170 @item @ref{spmin} |
5164
|
1171 For a vector argument, return the minimum value. |
5648
|
1172 @item @ref{spmax} |
5164
|
1173 For a vector argument, return the maximum value. |
5648
|
1174 @item @ref{spatan2} |
5164
|
1175 Compute atan (Y / X) for corresponding sparse matrix elements of Y and X. |
5648
|
1176 @item @ref{spdiag} |
5164
|
1177 Return a diagonal matrix with the sparse vector V on diagonal K. |
|
1178 @end table |
|
1179 |
|
1180 @subsection Functions Alphabetically |
5648
|
1181 @end ifset |
5164
|
1182 |
|
1183 @menu |
5506
|
1184 * ccolamd:: Constrained column approximate minimum degree permutation. |
5164
|
1185 * colamd:: Column approximate minimum degree permutation. |
|
1186 * colperm:: Returns the column permutations such that the columns of `S |
|
1187 (:, P)' are ordered in terms of increase number of non-zero |
|
1188 elements. |
5506
|
1189 * csymamd:: For a symmetric positive definite matrix S, returns the |
|
1190 permutation vector p such that `S (P, P)' tends to have a |
|
1191 sparser Cholesky factor than S. |
5164
|
1192 * dmperm:: Perfrom a Deulmage-Mendelsohn permutation on the sparse |
|
1193 matrix S. |
|
1194 * etree:: Returns the elimination tree for the matrix S. |
5576
|
1195 * etreeplot:: Plots the elimination tree of the matrix @var{s} or |
|
1196 @code{@var{s}+@var{s}'} if @var{s} in non-symmetric. |
5164
|
1197 * full:: returns a full storage matrix from a sparse one See also: |
|
1198 sparse |
5576
|
1199 * gplot:: Plots a graph defined by @var{A} and @var{xy} in the graph |
|
1200 theory sense. |
5164
|
1201 * issparse:: Return 1 if the value of the expression EXPR is a sparse |
|
1202 matrix. |
5282
|
1203 * luinc:: Produce the incomplete LU factorization of the sparse |
|
1204 A. |
6334
|
1205 * normest:: Estimates the 2-norm of the matrix @var{a} using a power |
|
1206 series analysis. |
5164
|
1207 * nnz:: returns number of non zero elements in SM See also: sparse |
|
1208 * nonzeros:: Returns a vector of the non-zero values of the sparse |
|
1209 matrix S |
|
1210 * nzmax:: Returns the amount of storage allocated to the sparse |
|
1211 matrix SM. |
5837
|
1212 * pcg:: Solves linear system of equations by means of the |
|
1213 Preconditioned Conjugate Gradient iterative method. |
|
1214 * pcr:: Solves linear system of equations by means of the |
|
1215 Preconditioned Conjugate Residual iterative method. |
5164
|
1216 * spalloc:: Returns an empty sparse matrix of size R-by-C. |
|
1217 * sparse:: SPARSE: create a sparse matrix |
|
1218 * spatan2:: Compute atan (Y / X) for corresponding sparse matrix |
|
1219 elements of Y and X. |
5506
|
1220 * spchol:: Compute the Cholesky factor, R, of the symmetric |
|
1221 positive definite. |
|
1222 * spcholinv:: Use the Cholesky factorization to compute the inverse of the |
|
1223 sparse symmetric positive definite matrix A. |
|
1224 * spchol2inv:: Invert a sparse symmetric, positive definite square matrix |
|
1225 from its Cholesky decomposition, U. |
5164
|
1226 * spconvert:: This function converts for a simple sparse matrix format |
|
1227 easily produced by other programs into Octave's internal |
|
1228 sparse format. |
|
1229 * spcumprod:: Cumulative product of elements along dimension DIM. |
|
1230 * spcumsum:: Cumulative sum of elements along dimension DIM. |
|
1231 * spdet:: Compute the determinant of sparse matrix A using UMFPACK. |
|
1232 * spdiag:: Return a diagonal matrix with the sparse vector V on |
|
1233 diagonal K. |
|
1234 * spdiags:: A generalization of the function `spdiag'. |
|
1235 * speye:: Returns a sparse identity matrix. |
|
1236 * spfind:: SPFIND: a sparse version of the find operator 1. |
|
1237 * spfun:: Compute `f(X)' for the non-zero values of X This results in |
|
1238 a sparse matrix with the same structure as X. |
|
1239 * spinv:: Compute the inverse of the square matrix A. |
5322
|
1240 * spkron:: Form the kronecker product of two sparse matrices. |
5506
|
1241 * splchol:: Compute the Cholesky factor, L, of the symmetric positive |
|
1242 definite. |
5164
|
1243 * splu:: Compute the LU decomposition of the sparse matrix A, using |
|
1244 subroutines from UMFPACK. |
|
1245 * spmax:: For a vector argument, return the maximum value. |
|
1246 * spmin:: For a vector argument, return the minimum value. |
|
1247 * spones:: Replace the non-zero entries of X with ones. |
|
1248 * spparms:: Sets or displays the parameters used by the sparse solvers |
|
1249 and factorization functions. |
|
1250 * spprod:: Product of elements along dimension DIM. |
5648
|
1251 * spqr:: Compute the sparse QR factorization of @var{a}, using CSPARSE. |
5164
|
1252 * sprand:: Generate a random sparse matrix. |
|
1253 * sprandn:: Generate a random sparse matrix. |
5648
|
1254 * sprandsym:: Generate a symmetric random sparse matrix. |
6334
|
1255 * sprank:: Calculates the structural rank of a sparse matrix @var{s}. |
5164
|
1256 * spstats:: Return the stats for the non-zero elements of the sparse |
|
1257 matrix S COUNT is the number of non-zeros in each column, |
|
1258 MEAN is the mean of the non-zeros in each column, and VAR |
|
1259 is the variance of the non-zeros in each column |
|
1260 * spsum:: Sum of elements along dimension DIM. |
|
1261 * spsumsq:: Sum of squares of elements along dimension DIM. |
|
1262 * spy:: Plot the sparsity pattern of the sparse matrix X |
|
1263 * symamd:: For a symmetric positive definite matrix S, returns the |
|
1264 permutation vector p such that `S (P, P)' tends to have a |
|
1265 sparser Cholesky factor than S. |
|
1266 * symbfact:: Performs a symbolic factorization analysis on the sparse |
|
1267 matrix S. |
5576
|
1268 * treeplot:: Produces a graph of a tree or forest. |
5164
|
1269 @end menu |
|
1270 |
5506
|
1271 @node colamd, ccolamd, , Function Reference |
5164
|
1272 @subsubsection colamd |
|
1273 |
|
1274 @DOCSTRING(colamd) |
|
1275 |
5506
|
1276 @node ccolamd, colperm, colamd, Function Reference |
|
1277 @subsubsection ccolamd |
|
1278 |
|
1279 @DOCSTRING(ccolamd) |
|
1280 |
|
1281 @node colperm, csymamd, ccolamd, Function Reference |
5164
|
1282 @subsubsection colperm |
|
1283 |
|
1284 @DOCSTRING(colperm) |
|
1285 |
5506
|
1286 @node csymamd, dmperm, colperm, Function Reference |
|
1287 @subsubsection csymamd |
|
1288 |
|
1289 @DOCSTRING(csymamd) |
|
1290 |
|
1291 @node dmperm, etree, csymamd, Function Reference |
5164
|
1292 @subsubsection dmperm |
|
1293 |
|
1294 @DOCSTRING(dmperm) |
|
1295 |
5576
|
1296 @node etree, etreeplot, dmperm, Function Reference |
5164
|
1297 @subsubsection etree |
|
1298 |
|
1299 @DOCSTRING(etree) |
|
1300 |
5576
|
1301 @node etreeplot, full, etree, Function Reference |
|
1302 @subsubsection etreeplot |
|
1303 |
|
1304 @DOCSTRING(etreeplot) |
|
1305 |
|
1306 @node full, gplot, etreeplot, Function Reference |
5164
|
1307 @subsubsection full |
|
1308 |
|
1309 @DOCSTRING(full) |
|
1310 |
5576
|
1311 @node gplot, issparse, full, Function Reference |
|
1312 @subsubsection gplot |
|
1313 |
|
1314 @DOCSTRING(gplot) |
|
1315 |
|
1316 @node issparse, luinc, gplot, Function Reference |
5164
|
1317 @subsubsection issparse |
|
1318 |
|
1319 @DOCSTRING(issparse) |
|
1320 |
6531
|
1321 @node luinc, normest, issparse, Function Reference |
5282
|
1322 @subsubsection luinc |
|
1323 |
|
1324 @DOCSTRING(luinc) |
|
1325 |
6531
|
1326 @node normest, nnz, luinc, Function Reference |
6334
|
1327 @subsubsection normest |
|
1328 |
|
1329 @DOCSTRING(normest) |
|
1330 |
|
1331 @node nnz, nonzeros, normest, Function Reference |
5164
|
1332 @subsubsection nnz |
|
1333 |
|
1334 @DOCSTRING(nnz) |
|
1335 |
|
1336 @node nonzeros, nzmax, nnz, Function Reference |
|
1337 @subsubsection nonzeros |
|
1338 |
|
1339 @DOCSTRING(nonzeros) |
|
1340 |
5837
|
1341 @node nzmax, pcg, nonzeros, Function Reference |
5164
|
1342 @subsubsection nzmax |
|
1343 |
|
1344 @DOCSTRING(nzmax) |
|
1345 |
5837
|
1346 @node pcg, pcr, nzmax, Function Reference |
|
1347 @subsubsection pcg |
|
1348 |
|
1349 @DOCSTRING(pcg) |
|
1350 |
|
1351 @node pcr, spalloc, pcg, Function Reference |
|
1352 @subsubsection pcr |
|
1353 |
|
1354 @DOCSTRING(pcr) |
|
1355 |
|
1356 @node spalloc, sparse, pcr, Function Reference |
5164
|
1357 @subsubsection spalloc |
|
1358 |
|
1359 @DOCSTRING(spalloc) |
|
1360 |
|
1361 @node sparse, spatan2, spalloc, Function Reference |
|
1362 @subsubsection sparse |
|
1363 |
|
1364 @DOCSTRING(sparse) |
|
1365 |
5506
|
1366 @node spatan2, spchol, sparse, Function Reference |
5164
|
1367 @subsubsection spatan2 |
|
1368 |
|
1369 @DOCSTRING(spatan2) |
|
1370 |
5506
|
1371 @node spchol, spcholinv, spatan2, Function Reference |
|
1372 @subsubsection spchol |
|
1373 |
|
1374 @DOCSTRING(spchol) |
|
1375 |
|
1376 @node spcholinv, spchol2inv, spchol, Function Reference |
|
1377 @subsubsection spcholinv |
|
1378 |
|
1379 @DOCSTRING(spcholinv) |
|
1380 |
|
1381 @node spchol2inv, spconvert, spcholinv, Function Reference |
|
1382 @subsubsection spchol2inv |
|
1383 |
|
1384 @DOCSTRING(spchol2inv) |
|
1385 |
|
1386 @node spconvert, spcumprod, spchol2inv, Function Reference |
5164
|
1387 @subsubsection spconvert |
|
1388 |
|
1389 @DOCSTRING(spconvert) |
|
1390 |
|
1391 @node spcumprod, spcumsum, spconvert, Function Reference |
|
1392 @subsubsection spcumprod |
|
1393 |
|
1394 @DOCSTRING(spcumprod) |
|
1395 |
|
1396 @node spcumsum, spdet, spcumprod, Function Reference |
|
1397 @subsubsection spcumsum |
|
1398 |
|
1399 @DOCSTRING(spcumsum) |
|
1400 |
|
1401 @node spdet, spdiag, spcumsum, Function Reference |
|
1402 @subsubsection spdet |
|
1403 |
|
1404 @DOCSTRING(spdet) |
|
1405 |
|
1406 @node spdiag, spdiags, spdet, Function Reference |
|
1407 @subsubsection spdiag |
|
1408 |
|
1409 @DOCSTRING(spdiag) |
|
1410 |
|
1411 @node spdiags, speye, spdiag, Function Reference |
|
1412 @subsubsection spdiags |
|
1413 |
|
1414 @DOCSTRING(spdiags) |
|
1415 |
|
1416 @node speye, spfind, spdiags, Function Reference |
|
1417 @subsubsection speye |
|
1418 |
|
1419 @DOCSTRING(speye) |
|
1420 |
|
1421 @node spfind, spfun, speye, Function Reference |
|
1422 @subsubsection spfind |
|
1423 |
|
1424 @DOCSTRING(spfind) |
|
1425 |
|
1426 @node spfun, spinv, spfind, Function Reference |
|
1427 @subsubsection spfun |
|
1428 |
|
1429 @DOCSTRING(spfun) |
|
1430 |
5322
|
1431 @node spinv, spkron, spfun, Function Reference |
5164
|
1432 @subsubsection spinv |
|
1433 |
|
1434 @DOCSTRING(spinv) |
|
1435 |
5506
|
1436 @node spkron, splchol, spinv, Function Reference |
5322
|
1437 @subsubsection spkron |
|
1438 |
|
1439 @DOCSTRING(spkron) |
|
1440 |
5506
|
1441 @node splchol, splu, spkron, Function Reference |
|
1442 @subsubsection splchol |
|
1443 |
|
1444 @DOCSTRING(splchol) |
|
1445 |
|
1446 @node splu, spmax, splchol, Function Reference |
5164
|
1447 @subsubsection splu |
|
1448 |
|
1449 @DOCSTRING(splu) |
|
1450 |
|
1451 @node spmax, spmin, splu, Function Reference |
|
1452 @subsubsection spmax |
|
1453 |
|
1454 @DOCSTRING(spmax) |
|
1455 |
|
1456 @node spmin, spones, spmax, Function Reference |
|
1457 @subsubsection spmin |
|
1458 |
|
1459 @DOCSTRING(spmin) |
|
1460 |
|
1461 @node spones, spparms, spmin, Function Reference |
|
1462 @subsubsection spones |
|
1463 |
|
1464 @DOCSTRING(spones) |
|
1465 |
|
1466 @node spparms, spprod, spones, Function Reference |
|
1467 @subsubsection spparms |
|
1468 |
|
1469 @DOCSTRING(spparms) |
|
1470 |
5648
|
1471 @node spprod, spqr, spparms, Function Reference |
5164
|
1472 @subsubsection spprod |
|
1473 |
|
1474 @DOCSTRING(spprod) |
|
1475 |
5648
|
1476 @node spqr, sprand, spprod, Function Reference |
|
1477 @subsubsection spqr |
|
1478 |
|
1479 @DOCSTRING(spqr) |
|
1480 |
|
1481 @node sprand, sprandn, spqr, Function Reference |
5164
|
1482 @subsubsection sprand |
|
1483 |
|
1484 @DOCSTRING(sprand) |
|
1485 |
5648
|
1486 @node sprandn, sprandsym, sprand, Function Reference |
5164
|
1487 @subsubsection sprandn |
|
1488 |
|
1489 @DOCSTRING(sprandn) |
|
1490 |
6334
|
1491 @node sprandsym, sprank, sprandn, Function Reference |
5648
|
1492 @subsubsection sprandsym |
|
1493 |
|
1494 @DOCSTRING(sprandsym) |
|
1495 |
6334
|
1496 @node sprank, spstats, sprandsym, Function Reference |
|
1497 @subsubsection sprank |
|
1498 |
|
1499 @DOCSTRING(sprank) |
|
1500 |
|
1501 @node spstats, spsum, sprank, Function Reference |
5164
|
1502 @subsubsection spstats |
|
1503 |
|
1504 @DOCSTRING(spstats) |
|
1505 |
|
1506 @node spsum, spsumsq, spstats, Function Reference |
|
1507 @subsubsection spsum |
|
1508 |
|
1509 @DOCSTRING(spsum) |
|
1510 |
|
1511 @node spsumsq, spy, spsum, Function Reference |
|
1512 @subsubsection spsumsq |
|
1513 |
|
1514 @DOCSTRING(spsumsq) |
|
1515 |
|
1516 @node spy, symamd, spsumsq, Function Reference |
|
1517 @subsubsection spy |
|
1518 |
|
1519 @DOCSTRING(spy) |
|
1520 |
|
1521 @node symamd, symbfact, spy, Function Reference |
|
1522 @subsubsection symamd |
|
1523 |
|
1524 @DOCSTRING(symamd) |
|
1525 |
5648
|
1526 @node symbfact, treeplot, symamd, Function Reference |
5164
|
1527 @subsubsection symbfact |
|
1528 |
|
1529 @DOCSTRING(symbfact) |
|
1530 |
5648
|
1531 @node treeplot, ,symbfact, Function Reference |
5576
|
1532 @subsubsection treeplot |
|
1533 |
|
1534 @DOCSTRING(treeplot) |
|
1535 |
5164
|
1536 @c Local Variables: *** |
|
1537 @c Mode: texinfo *** |
|
1538 @c End: *** |