<html lang="en"> <head> <title>Information - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Basics.html#Basics" title="Basics"> <link rel="prev" href="Creating-Sparse-Matrices.html#Creating-Sparse-Matrices" title="Creating Sparse Matrices"> <link rel="next" href="Operators-and-Functions.html#Operators-and-Functions" title="Operators and Functions"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Information"></a> <p> Next: <a rel="next" accesskey="n" href="Operators-and-Functions.html#Operators-and-Functions">Operators and Functions</a>, Previous: <a rel="previous" accesskey="p" href="Creating-Sparse-Matrices.html#Creating-Sparse-Matrices">Creating Sparse Matrices</a>, Up: <a rel="up" accesskey="u" href="Basics.html#Basics">Basics</a> <hr> </div> <h4 class="subsection">22.1.3 Finding out Information about Sparse Matrices</h4> <p>There are a number of functions that allow information concerning sparse matrices to be obtained. The most basic of these is <dfn>issparse</dfn> that identifies whether a particular Octave object is in fact a sparse matrix. <p>Another very basic function is <dfn>nnz</dfn> that returns the number of non-zero entries there are in a sparse matrix, while the function <dfn>nzmax</dfn> returns the amount of storage allocated to the sparse matrix. Note that Octave tends to crop unused memory at the first opportunity for sparse objects. There are some cases of user created sparse objects where the value returned by <dfn>nzmax</dfn> will not be the same as <dfn>nnz</dfn>, but in general they will give the same result. The function <dfn>spstats</dfn> returns some basic statistics on the columns of a sparse matrix including the number of elements, the mean and the variance of each column. <!-- issparse src/sparse.cc --> <p><a name="doc_002dissparse"></a> <div class="defun"> — Loadable Function: <b>issparse</b> (<var>x</var>)<var><a name="index-issparse-2198"></a></var><br> <blockquote><p>Return true if <var>x</var> is a sparse matrix. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dismatrix.html#doc_002dismatrix">ismatrix</a>. </p></blockquote></div> <!-- nnz src/data.cc --> <p><a name="doc_002dnnz"></a> <div class="defun"> — Built-in Function: <var>scalar</var> = <b>nnz</b> (<var>a</var>)<var><a name="index-nnz-2199"></a></var><br> <blockquote><p>Return the number of non zero elements in <var>a</var>. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dsparse.html#doc_002dsparse">sparse</a>. </p></blockquote></div> <!-- nonzeros scripts/sparse/nonzeros.m --> <p><a name="doc_002dnonzeros"></a> <div class="defun"> — Function File: <b>nonzeros</b> (<var>s</var>)<var><a name="index-nonzeros-2200"></a></var><br> <blockquote><p>Return a vector of the non-zero values of the sparse matrix <var>s</var>. </p></blockquote></div> <!-- nzmax src/data.cc --> <p><a name="doc_002dnzmax"></a> <div class="defun"> — Built-in Function: <var>scalar</var> = <b>nzmax</b> (<var>SM</var>)<var><a name="index-nzmax-2201"></a></var><br> <blockquote><p>Return the amount of storage allocated to the sparse matrix <var>SM</var>. Note that Octave tends to crop unused memory at the first opportunity for sparse objects. There are some cases of user created sparse objects where the value returned by <dfn>nzmax</dfn> will not be the same as <dfn>nnz</dfn>, but in general they will give the same result. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dsparse.html#doc_002dsparse">sparse</a>, <a href="doc_002dspalloc.html#doc_002dspalloc">spalloc</a>. </p></blockquote></div> <!-- spstats scripts/sparse/spstats.m --> <p><a name="doc_002dspstats"></a> <div class="defun"> — Function File: [<var>count</var>, <var>mean</var>, <var>var</var>] = <b>spstats</b> (<var>S</var>)<var><a name="index-spstats-2202"></a></var><br> — Function File: [<var>count</var>, <var>mean</var>, <var>var</var>] = <b>spstats</b> (<var>S, j</var>)<var><a name="index-spstats-2203"></a></var><br> <blockquote><p>Return the stats for the non-zero elements of the sparse matrix <var>S</var>. <var>count</var> is the number of non-zeros in each column, <var>mean</var> is the mean of the non-zeros in each column, and <var>var</var> is the variance of the non-zeros in each column. <p>Called with two input arguments, if <var>S</var> is the data and <var>j</var> is the bin number for the data, compute the stats for each bin. In this case, bins can contain data values of zero, whereas with <code>spstats (</code><var>S</var><code>)</code> the zeros may disappear. </p></blockquote></div> <p>When solving linear equations involving sparse matrices Octave determines the means to solve the equation based on the type of the matrix as discussed in <a href="Sparse-Linear-Algebra.html#Sparse-Linear-Algebra">Sparse Linear Algebra</a>. Octave probes the matrix type when the div (/) or ldiv (\) operator is first used with the matrix and then caches the type. However the <dfn>matrix_type</dfn> function can be used to determine the type of the sparse matrix prior to use of the div or ldiv operators. For example, <pre class="example"> a = tril (sprandn(1024, 1024, 0.02), -1) ... + speye(1024); matrix_type (a); ans = Lower </pre> <p class="noindent">shows that Octave correctly determines the matrix type for lower triangular matrices. <dfn>matrix_type</dfn> can also be used to force the type of a matrix to be a particular type. For example: <pre class="example"> a = matrix_type (tril (sprandn (1024, ... 1024, 0.02), -1) + speye(1024), 'Lower'); </pre> <p>This allows the cost of determining the matrix type to be avoided. However, incorrectly defining the matrix type will result in incorrect results from solutions of linear equations, and so it is entirely the responsibility of the user to correctly identify the matrix type <p>There are several graphical means of finding out information about sparse matrices. The first is the <dfn>spy</dfn> command, which displays the structure of the non-zero elements of the matrix. See <a href="fig_003aspmatrix.html#fig_003aspmatrix">fig:spmatrix</a>, for an example of the use of <dfn>spy</dfn>. More advanced graphical information can be obtained with the <dfn>treeplot</dfn>, <dfn>etreeplot</dfn> and <dfn>gplot</dfn> commands. <div class="float"> <a name="fig_003aspmatrix"></a><div align="center"><img src="spmatrix.png" alt="spmatrix.png"></div> <p><strong class="float-caption">Figure 22.1: Structure of simple sparse matrix.</strong></p></div> <p>One use of sparse matrices is in graph theory, where the interconnections between nodes are represented as an adjacency matrix. That is, if the i-th node in a graph is connected to the j-th node. Then the ij-th node (and in the case of undirected graphs the ji-th node) of the sparse adjacency matrix is non-zero. If each node is then associated with a set of coordinates, then the <dfn>gplot</dfn> command can be used to graphically display the interconnections between nodes. <p>As a trivial example of the use of <dfn>gplot</dfn> consider the example, <pre class="example"> A = sparse([2,6,1,3,2,4,3,5,4,6,1,5], [1,1,2,2,3,3,4,4,5,5,6,6],1,6,6); xy = [0,4,8,6,4,2;5,0,5,7,5,7]'; gplot(A,xy) </pre> <p class="noindent">which creates an adjacency matrix <code>A</code> where node 1 is connected to nodes 2 and 6, node 2 with nodes 1 and 3, etc. The coordinates of the nodes are given in the n-by-2 matrix <code>xy</code>. See <a href="fig_003agplot.html#fig_003agplot">fig:gplot</a>. <div class="float"> <a name="fig_003agplot"></a><div align="center"><img src="gplot.png" alt="gplot.png"></div> <p><strong class="float-caption">Figure 22.2: Simple use of the <dfn>gplot</dfn> command.</strong></p></div> <p>The dependencies between the nodes of a Cholesky factorization can be calculated in linear time without explicitly needing to calculate the Cholesky factorization by the <code>etree</code> command. This command returns the elimination tree of the matrix and can be displayed graphically by the command <code>treeplot(etree(A))</code> if <code>A</code> is symmetric or <code>treeplot(etree(A+A'))</code> otherwise. <!-- spy scripts/sparse/spy.m --> <p><a name="doc_002dspy"></a> <div class="defun"> — Function File: <b>spy</b> (<var>x</var>)<var><a name="index-spy-2204"></a></var><br> — Function File: <b>spy</b> (<var><small class="dots">...</small>, markersize</var>)<var><a name="index-spy-2205"></a></var><br> — Function File: <b>spy</b> (<var><small class="dots">...</small>, line_spec</var>)<var><a name="index-spy-2206"></a></var><br> <blockquote><p>Plot the sparsity pattern of the sparse matrix <var>x</var>. If the argument <var>markersize</var> is given as a scalar value, it is used to determine the point size in the plot. If the string <var>line_spec</var> is given it is passed to <code>plot</code> and determines the appearance of the plot. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dplot.html#doc_002dplot">plot</a>. </p></blockquote></div> <!-- etree src/DLD-FUNCTIONS/colamd.cc --> <p><a name="doc_002detree"></a> <div class="defun"> — Loadable Function: <var>p</var> = <b>etree</b> (<var>S</var>)<var><a name="index-etree-2207"></a></var><br> — Loadable Function: <var>p</var> = <b>etree</b> (<var>S, typ</var>)<var><a name="index-etree-2208"></a></var><br> — Loadable Function: [<var>p</var>, <var>q</var>] = <b>etree</b> (<var>S, typ</var>)<var><a name="index-etree-2209"></a></var><br> <blockquote> <p>Return the elimination tree for the matrix <var>S</var>. By default <var>S</var> is assumed to be symmetric and the symmetric elimination tree is returned. The argument <var>typ</var> controls whether a symmetric or column elimination tree is returned. Valid values of <var>typ</var> are 'sym' or 'col', for symmetric or column elimination tree respectively <p>Called with a second argument, <code>etree</code> also returns the postorder permutations on the tree. </p></blockquote></div> <!-- etreeplot scripts/sparse/etreeplot.m --> <p><a name="doc_002detreeplot"></a> <div class="defun"> — Function File: <b>etreeplot</b> (<var>A</var>)<var><a name="index-etreeplot-2210"></a></var><br> — Function File: <b>etreeplot</b> (<var>A, node_style, edge_style</var>)<var><a name="index-etreeplot-2211"></a></var><br> <blockquote><p>Plot the elimination tree of the matrix <var>A</var> or <var>A</var><code>+</code><var>A</var><code>'</code> if <var>A</var> in not symmetric. The optional parameters <var>node_style</var> and <var>edge_style</var> define the output style. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dtreeplot.html#doc_002dtreeplot">treeplot</a>, <a href="doc_002dgplot.html#doc_002dgplot">gplot</a>. </p></blockquote></div> <!-- gplot scripts/sparse/gplot.m --> <p><a name="doc_002dgplot"></a> <div class="defun"> — Function File: <b>gplot</b> (<var>A, xy</var>)<var><a name="index-gplot-2212"></a></var><br> — Function File: <b>gplot</b> (<var>A, xy, line_style</var>)<var><a name="index-gplot-2213"></a></var><br> — Function File: [<var>x</var>, <var>y</var>] = <b>gplot</b> (<var>A, xy</var>)<var><a name="index-gplot-2214"></a></var><br> <blockquote><p>Plot a graph defined by <var>A</var> and <var>xy</var> in the graph theory sense. <var>A</var> is the adjacency matrix of the array to be plotted and <var>xy</var> is an <var>n</var>-by-2 matrix containing the coordinates of the nodes of the graph. <p>The optional parameter <var>line_style</var> defines the output style for the plot. Called with no output arguments the graph is plotted directly. Otherwise, return the coordinates of the plot in <var>x</var> and <var>y</var>. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dtreeplot.html#doc_002dtreeplot">treeplot</a>, <a href="doc_002detreeplot.html#doc_002detreeplot">etreeplot</a>, <a href="doc_002dspy.html#doc_002dspy">spy</a>. </p></blockquote></div> <!-- treeplot scripts/sparse/treeplot.m --> <p><a name="doc_002dtreeplot"></a> <div class="defun"> — Function File: <b>treeplot</b> (<var>tree</var>)<var><a name="index-treeplot-2215"></a></var><br> — Function File: <b>treeplot</b> (<var>tree, node_style, edge_style</var>)<var><a name="index-treeplot-2216"></a></var><br> <blockquote><p>Produce a graph of tree or forest. The first argument is vector of predecessors, optional parameters <var>node_style</var> and <var>edge_style</var> define the output style. The complexity of the algorithm is O(n) in terms of is time and memory requirements. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002detreeplot.html#doc_002detreeplot">etreeplot</a>, <a href="doc_002dgplot.html#doc_002dgplot">gplot</a>. </p></blockquote></div> <!-- treelayout scripts/sparse/treelayout.m --> <p><a name="doc_002dtreelayout"></a> <div class="defun"> — Function File: <b>treelayout</b> (<var>tree</var>)<var><a name="index-treelayout-2217"></a></var><br> — Function File: <b>treelayout</b> (<var>tree, permutation</var>)<var><a name="index-treelayout-2218"></a></var><br> <blockquote><p>treelayout lays out a tree or a forest. The first argument <var>tree</var> is a vector of predecessors, optional parameter <var>permutation</var> is an optional postorder permutation. The complexity of the algorithm is O(n) in terms of time and memory requirements. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002detreeplot.html#doc_002detreeplot">etreeplot</a>, <a href="doc_002dgplot.html#doc_002dgplot">gplot</a>, <a href="doc_002dtreeplot.html#doc_002dtreeplot">treeplot</a>. </p></blockquote></div> </body></html>