forked from johannesgerer/jburkardt-m
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparse.html
508 lines (455 loc) · 14.7 KB
/
sparse.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
<html>
<head>
<title>
SPARSE - MATLAB's Sparse Matrix Utility
</title>
</head>
<body bgcolor="#EEEEEE" link="#CC0000" alink="#FF3300" vlink="#000055">
<h1 align = "center">
SPARSE <br> MATLAB's Sparse Matrix Utility
</h1>
<hr>
<p>
<b>SPARSE</b>
is a directory of MATLAB programs which
demonstrate the use of the MATLAB "sparse" function
for creating a sparse matrix.
</p>
<h2 align = "center">
MATLAB's SPARSE function
</h2>
<p>
MATLAB provides a <b>sparse</b> function of the form
<blockquote><b>
matrix = sparse ( i, j, s, m, n, nz_max )
</b></blockquote>
This function can be used to create a sparse matrix.
The input arguments have the following meaning:
<dl>
<dt>
<b>i</b>
</dt>
<dd>
the row indices of the nonzero elements;
</dd>
<dt>
<b>j</b>
</dt>
<dd>
the column indices of the nonzero elements;
</dd>
<dt>
<b>s</b>
</dt>
<dd>
the values of the nonzero elements;
</dd>
<dt>
<b>m</b>
</dt>
<dd>
the number of rows in the matrix;
</dd>
<dt>
<b>n</b>
</dt>
<dd>
the number of columns in the matrix;
</dd>
<dt>
<b>nz_max</b>
</dt>
<dd>
the maximum number of nonzero elements in the matrix;
<b>nz_max</b> is commonly omitted, in which case its value
is taken from the length of <b>s</b>.
</dd>
</dl>
Note that <b>nz_max</b> is commonly omitted, in which case its value
is taken from the length of <b>s</b>. Moreover, if you specify
<b>nz_max</b> explicitly, or implicitly through the size of <b>s</b>,
MATLAB will allow you to increase the number of nonzero elements
at any time. Specifying <b>nz_max</b> correctly, then, is simply
a matter of efficiency.
</p>
<h3 align = "center">
Example: Creating a Sparse Matrix at Once
</h3>
<p>
Although this is not the usual way to use the <b>sparse</b>
command, the following example should help to understand what
is going on. We mean to define the following matrix:
<pre>
11 12 0 0 15
0 22 23 0 0
31 0 33 34 35
</pre>
by the following MATLAB commands:
<pre>
i = [ 1, 1, 1, 2, 2, 3, 3, 3, 3 ];
j = [ 1, 2, 5, 2, 3, 1, 3, 4, 5 ];
s = [ 11, 12, 15, 22, 23, 31, 33, 34, 35 ];
m = 3;
n = 5;
nz_max = 9;
a = sparse ( i, j, s, m, n, nz_max );
</pre>
</p>
<h3 align = "center">
Example: Building a Sparse Matrix in Steps
</h3>
<p>
Of course, in many applications, the matrix data is only
available one piece at a time, or has to be modified as you go.
This is easy to do, as well. You can start by defining the
matrix to be an "empty" sparse matrix of a particular size,
as, for example:
<pre>
i = [];
j = [];
s = [];
m = 100;
n = 100;
a = sparse ( i, j, s, m, n );
</pre>
The matrix will be empty, and entries of the matrix are
by default equal to zero. Then you can simply reference
entries of the matrix as you need them. For instance,
<pre>
a(1,1) = 3
a(3,8) = a(3,8) + 7
a(4,7) = a(9,5) + 2 * a(8,12)
a(4,7) = a(4,7) + 1
</pre>
If you reference, on the right hand side of these equations,
a matrix entry that doesn't exist, it is by default zero.
If you assign a value on the left hand side to a matrix entry
that doesn't exist, a space is created for it, and it is given
this value. If the entry already existed, it is overwritten.
</p>
<h3 align = "center">
Useful Commands
</h3>
<p>
In some cases, Matlab's sparse matrix structure allows you
to ignore the fact that you are using a sparse matrix. We
have already seen that you can reference the (i,j) element
of the matrix in the same way you would do for a full matrix,
and this is true whether you are simply asking to "read" the
current value of the element, or to "write" a new value for
the element.
</p>
<p>
A particular example of this is the fact that you can
solve a sparse linear system using the same "backslash"
formula that you would use if the matrix were full:
<blockquote><b>
x = A \ b;
</b></blockquote>
</p>
<p>
Matlab includes many commands specifically for dealing with
a sparse matrix. For our examples, we will be considering
<ul>
<li>
<b>nnz(A)</b> returns the number of nonzero matrix elements;
</li>
<li>
<b>nzmax(A)</b> returns the maximum number of nonzero
matrix elements allocated;
</li>
<li>
<b>find(A)</b> returns all (i,j) indices of nonzero elements;
</li>
<li>
<b>nonzeros(A)</b> returns all the nonzero elements;
</li>
</ul>
</p>
<p>
Note that, in a sense, MATLAB actually uses <i>two</i> formats
for sparse matrices. The user communicates with MATLAB by specifying
what is known as a <i>sparse triplet</i>, that is, a set of
row indices, column indices, and values. But internally, MATLAB
uses what is known as the <i>sparse compressed column</i> format.
This format allows MATLAB to access matrix entries rapidly.
</p>
<p>
To copy the nonzero entries from a sparse matrix, creating the sparse
triplet structure:
<pre>
[i,j,s] = sparse ( A );
[m,n] = size ( A );
</pre>
Correspondingly, to create the sparse matrix from this data:
<pre>
A = sparse ( i, j, s, m, n );
</pre>
</p>
<p>
When using MATLAB's sparse matrix format, it is possible to refer to
matrix entries directly, using the usual index notation like A(i,j).
However, accessing specific entries in this way, whether to initialize,
extract, increment, or zero them, is an expensive process. The
MathWorks suggests extracting the sparse triplet information, working
on it in the natural way, and then "repacking" it with the sparse()
command.
</p>
<h3 align = "center">
Languages:
</h3>
<p>
<b>SPARSE</b> is available in
<a href = "../../m_src/sparse/sparse.html">a MATLAB version</a>.
</p>
<h3 align = "center">
Related Data and Programs:
</h3>
<p>
<a href = "../../c_src/csparse/csparse.html">
CSPARSE</a>,
a C library which
implements methods for the direct solution of sparse linear systems.
</p>
<p>
<a href = "../../f_src/dlap/dlap.html">
DLAP</a>,
a FORTRAN90 library which
solves sparse linear systems.
</p>
<p>
<a href = "../../m_src/fem2d_heat_sparse/fem2d_heat_sparse.html">
FEM2D_HEAT_SPARSE</a>,
a MATLAB program which
solves the time dependent heat equation
in an arbitrary triangulated 2D region,
using MATLAB's sparse matrix storage format and solver.
</p>
<p>
<a href = "../../m_src/hb_io/hb_io.html">
HB_IO</a>,
a MATLAB library which
reads and writes sparse linear
systems stored in the Harwell-Boeing (HB) Sparse Matrix format.
</p>
<p>
<a href = "../../m_src/hb_to_msm/hb_to_msm.html">
HB_TO_MSM</a>,
a MATLAB program which
converts a Harwell Boeing (HB) sparse matrix file to
a MATLAB sparse matrix.
</p>
<p>
<a href = "../../m_src/hb_to_st/hb_to_st.html">
HB_TO_ST</a>,
a MATLAB program which
converts the sparse matrix information stored in a Harwell Boeing (HB)
file into a Sparse Triplet (ST) file.
</p>
<p>
<a href = "../../m_src/linplus/linplus.html">
LINPLUS</a>,
a MATLAB library which
carries out linear algebra operations,
which includes a set of operations for matrices in the "DCC"
format, a format which is equivalent to MATLAB's sparse format.
</p>
<p>
<a href = "../../m_src/mm_io/mm_io.html">
MM_IO</a>,
a MATLAB library which
reads and writes sparse linear
systems stored in the Matrix Market format.
</p>
<p>
<a href = "../../m_src/msm_to_hb/msm_to_hb.html">
MSM_TO_HB</a>,
a MATLAB program which
converts a MATLAB sparse
matrix into a Harwell Boeing (HB) file.
</p>
<p>
<a href = "../../c_src/nsasm/nsasm.html">
NSASM</a>,
a C library which
is intended to be used with a MATLAB
calling program, and which sets up the sparse matrix needed for
a Newton iteration to solve a finite element formulation of
the steady incompressible 2D Navier Stokes equations.
</p>
<p>
<a href = "../../data/sparse_cc/sparse_cc.html">
SPARSE_CC</a>,
a data directory which
contains examples of the sparse "compressed column" structure,
equivalent to MATLAB's sparse format, and
a file format suitable for storing such information.
</p>
<p>
<a href = "../../data/sparse_cr/sparse_cr.html">
SPARSE_CR</a>,
a data directory which
contains a description and examples of the CR format,
("compressed row") for storing a sparse matrix,
including a way to write the matrix as a set of three files.
</p>
<p>
<a href = "../../m_src/sparse_display/sparse_display.html">
SPARSE_DISPLAY</a>,
a MATLAB library which
can read information defining a matrix of numbers and display
the sparsity pattern or location of the nonzero elements using
gnuplot. This operation is already available in the built-in
MATLAB "spy" command.
</p>
<p>
<a href = "../../m_src/sparse_parfor/sparse_parfor.html">
SPARSE_PARFOR</a>,
a MATLAB library which
demonstrates how a sparse matrix can be constructed by
evaluating individual blocks in parallel with the <b>parfor</b>
command, and then assembled (on a single processor) using the
<b>sparse()</b> command.
</p>
<p>
<a href = "../../f_src/sparsekit/sparsekit.html">
SPARSEKIT</a>,
a FORTRAN90 library which
carries out sparse matrix
operations, by Yousef Saad.
</p>
<p>
<a href = "../../f_src/sparsepak/sparsepak.html">
SPARSEPAK</a>,
a FORTRAN90 library which
forms an obsolete version of
the Waterloo Sparse Matrix Package.
</p>
<p>
<a href = "../../data/st/st.html">
ST</a>,
a data directory which
contains examples of the "sparse triplet" format for storing sparse matrices. This
format is equivalent to the form in which sparse matrix data is
passed into MATLAB's sparse command (although the sparse compressed
column format is used internally).
</p>
<p>
<a href = "../../m_src/templates/templates.html">
TEMPLATES</a>,
a MATLAB library which
carries out the iterative solution of
linear systems. It includes a routine <b>mm_to_msm</b> which
can read a Matrix Market file and store it as a MATLAB sparse
matrix.
</p>
<p>
<a href = "../../m_src/wathen/wathen.html">
WATHEN</a>,
a MATLAB library which
compares storage schemes (full, banded, sparse triplet, sparse) and
solution strategies (A\x, Linpack, conjugate gradient) for linear systems
involving the Wathen matrix, which can arise when solving a
problem using the finite element method (FEM).
</p>
<h3 align = "center">
Reference:
</h3>
<p>
<ol>
<li>
Timothy Davis,<br>
Direct Methods for Sparse Linear Systems,<br>
SIAM, 2006,<br>
ISBN: 0898716136.
</li>
<li>
John Gilbert, Cleve Moler, Robert Schreiber,<br>
Sparse Matrices in MATLAB: Design and Implementation,<br>
SIAM Journal on Matrix Analysis and Applications,<br>
Volume 13, Number 1, 1992, pages 333-356.
</li>
<li>
George Lindfield, John Penny,<br>
Numerical Methods Using MATLAB,<br>
Second Edition,<br>
Prentice Hall, 1999,<br>
ISBN: 0-13-012641-1,<br>
LC: QA297.P45.
</li>
<li>
The Mathworks,<br>
MATLAB Mathematics.
</li>
</ol>
</p>
<h3 align = "center">
Source Code:
</h3>
<p>
<ul>
<li>
<a href = "timestamp.m">timestamp.m</a>,
prints the current YMDHMS date as a timestamp;
</li>
</ul>
</p>
<h3 align = "center">
Examples and Tests:
</h3>
<p>
<ul>
<li>
<a href = "sparse_test.m">sparse_test.m</a>,
runs all the tests;
</li>
<li>
<a href = "sparse_test_output.txt">sparse_test_output.txt</a>,
the output file;
</li>
<li>
<a href = "sparse_test01.m">sparse_test01.m</a>,
sets up a simple 3 by 5 matrix;
</li>
<li>
<a href = "sparse_test02.m">sparse_test02.m</a>,
sets up a sparse -1,2,-1 matrix of order 100;
</li>
<li>
<a href = "sparse_test03.m">sparse_test03.m</a>,
demonstrates the use of the SIZE, NNZ and FIND functions
to describe the structure of a sparse matrix;
</li>
<li>
<a href = "sparse_test04.m">sparse_test04.m</a>,
demonstrates that a sparse matrix can be constructed
and incremented one element at a time;
</li>
<li>
<a href = "sparse_test05.m">sparse_test05.m</a>,
demonstrates how to set up a sparse matrix associated with the
Poisson equation on a regular 2D grid;
</li>
<li>
<a href = "sparse_test06.m">sparse_test06.m</a>,
compares the speed of zeroing out many entries of a matrix
when it is stored in full or sparse mode;
</li>
<li>
<a href = "sparse_test07.m">sparse_test07.m</a>,
compares the speed of zeroing out many entries of a matrix
using full storage, sparse storage, or sparse triplet storage.
</li>
</ul>
</p>
<p>
You can go up one level to <a href = "../m_src.html">
the MATLAB source codes</a>.
</p>
<hr>
<i>
Last revised on 13 April 2014.
</i>
<!-- John Burkardt -->
</body>
</html>