Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 3e60ff9d4d6f58c8fbd17208f14089fa > files > 54

octave-doc-3.2.3-3mdv2010.0.i586.rpm

<html lang="en">
<head>
<title>Array and Sparse Differences - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Sparse-Matrices-in-Oct_002dFiles.html#Sparse-Matrices-in-Oct_002dFiles" title="Sparse Matrices in Oct-Files">
<link rel="next" href="Creating-Sparse-Matrices-in-Oct_002dFiles.html#Creating-Sparse-Matrices-in-Oct_002dFiles" title="Creating Sparse Matrices in Oct-Files">
<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="Array-and-Sparse-Differences"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Creating-Sparse-Matrices-in-Oct_002dFiles.html#Creating-Sparse-Matrices-in-Oct_002dFiles">Creating Sparse Matrices in Oct-Files</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Sparse-Matrices-in-Oct_002dFiles.html#Sparse-Matrices-in-Oct_002dFiles">Sparse Matrices in Oct-Files</a>
<hr>
</div>

<h5 class="subsubsection">A.1.6.1 The Differences between the Array and Sparse Classes</h5>

<p>The number of elements in a sparse matrix is considered to be the number
of non-zero elements rather than the product of the dimensions.  Therefore

<pre class="example">     SparseMatrix sm;
     ...
     int nel = sm.nelem ();
</pre>
   <p>returns the number of non-zero elements.  If the user really requires the
number of elements in the matrix, including the non-zero elements, they
should use <code>numel</code> rather than <code>nelem</code>.  Note that for very
large matrices, where the product of the two dimensions is larger than
the representation of an unsigned int, then <code>numel</code> can overflow. 
An example is <code>speye(1e6)</code> which will create a matrix with a million
rows and columns, but only a million non-zero elements.  Therefore the
number of rows by the number of columns in this case is more than two
hundred times the maximum value that can be represented by an unsigned int. 
The use of <code>numel</code> should therefore be avoided useless it is known
it won't overflow.

   <p>Extreme care must be take with the elem method and the "()" operator,
which perform basically the same function.  The reason is that if a
sparse object is non-const, then Octave will assume that a
request for a zero element in a sparse matrix is in fact a request
to create this element so it can be filled.  Therefore a piece of
code like

<pre class="example">     SparseMatrix sm;
     ...
     for (int j = 0; j &lt; nc; j++)
       for (int i = 0; i &lt; nr; i++)
         std::cerr &lt;&lt; " (" &lt;&lt; i &lt;&lt; "," &lt;&lt; j &lt;&lt; "): " &lt;&lt; sm(i,j)
                   &lt;&lt; std::endl;
</pre>
   <p>is a great way of turning the sparse matrix into a dense one, and a
very slow way at that since it reallocates the sparse object at each
zero element in the matrix.

   <p>An easy way of preventing the above from happening is to create a temporary
constant version of the sparse matrix.  Note that only the container for
the sparse matrix will be copied, while the actual representation of the
data will be shared between the two versions of the sparse matrix.  So this
is not a costly operation.  For example, the above would become

<pre class="example">     SparseMatrix sm;
     ...
     const SparseMatrix tmp (sm);
     for (int j = 0; j &lt; nc; j++)
       for (int i = 0; i &lt; nr; i++)
         std::cerr &lt;&lt; " (" &lt;&lt; i &lt;&lt; "," &lt;&lt; j &lt;&lt; "): " &lt;&lt; tmp(i,j)
                   &lt;&lt; std::endl;
</pre>
   <p>Finally, as the sparse types aren't just represented as a contiguous
block of memory, the <code>fortran_vec</code> method of the <code>Array&lt;T&gt;</code>
is not available.  It is however replaced by three separate methods
<code>ridx</code>, <code>cidx</code> and <code>data</code>, that access the raw compressed
column format that the Octave sparse matrices are stored in. 
Additionally, these methods can be used in a manner similar to <code>elem</code>,
to allow the matrix to be accessed or filled.  However, in that case it is
up to the user to respect the sparse matrix compressed column format
discussed previous.

   </body></html>