Sophie

Sophie

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

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

<html lang="en">
<head>
<title>Return Types of Operators and Functions - 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="Operators-and-Functions.html#Operators-and-Functions" title="Operators and Functions">
<link rel="prev" href="Sparse-Functions.html#Sparse-Functions" title="Sparse Functions">
<link rel="next" href="Mathematical-Considerations.html#Mathematical-Considerations" title="Mathematical Considerations">
<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="Return-Types-of-Operators-and-Functions"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Mathematical-Considerations.html#Mathematical-Considerations">Mathematical Considerations</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Sparse-Functions.html#Sparse-Functions">Sparse Functions</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Operators-and-Functions.html#Operators-and-Functions">Operators and Functions</a>
<hr>
</div>

<h5 class="subsubsection">21.1.4.2 The Return Types of Operators and Functions</h5>

<p>The two basic reasons to use sparse matrices are to reduce the memory
usage and to not have to do calculations on zero elements.  The two are
closely related in that the computation time on a sparse matrix operator
or function is roughly linear with the number of non-zero elements.

   <p>Therefore, there is a certain density of non-zero elements of a matrix
where it no longer makes sense to store it as a sparse matrix, but rather
as a full matrix.  For this reason operators and functions that have a
high probability of returning a full matrix will always return one.  For
example adding a scalar constant to a sparse matrix will almost always
make it a full matrix, and so the example

<pre class="example">     speye(3) + 0
     &rArr;   1  0  0
       0  1  0
       0  0  1
</pre>
   <p>returns a full matrix as can be seen.

   <p>Additionally, if <code>sparse_auto_mutate</code> is true, all sparse functions
test the amount of memory occupied by the sparse matrix to see if the
amount of storage used is larger than the amount used by the full
equivalent.  Therefore <code>speye (2) * 1</code> will return a full matrix as
the memory used is smaller for the full version than the sparse version.

   <p>As all of the mixed operators and functions between full and sparse
matrices exist, in general this does not cause any problems.  However,
one area where it does cause a problem is where a sparse matrix is
promoted to a full matrix, where subsequent operations would resparsify
the matrix.  Such cases are rare, but can be artificially created, for
example <code>(fliplr(speye(3)) + speye(3)) - speye(3)</code> gives a full
matrix when it should give a sparse one.  In general, where such cases
occur, they impose only a small memory penalty.

   <p>There is however one known case where this behavior of Octave's
sparse matrices will cause a problem.  That is in the handling of the
<dfn>diag</dfn> function.  Whether <dfn>diag</dfn> returns a sparse or full matrix
depending on the type of its input arguments.  So

<pre class="example">      a = diag (sparse([1,2,3]), -1);
</pre>
   <p>should return a sparse matrix.  To ensure this actually happens, the
<dfn>sparse</dfn> function, and other functions based on it like <dfn>speye</dfn>,
always returns a sparse matrix, even if the memory used will be larger
than its full representation.

<!-- ov-base.cc -->
   <p><a name="doc_002dsparse_005fauto_005fmutate"></a>

<div class="defun">
&mdash; Built-in Function: <var>val</var> = <b>sparse_auto_mutate</b> ()<var><a name="index-sparse_005fauto_005fmutate-1684"></a></var><br>
&mdash; Built-in Function: <var>old_val</var> = <b>sparse_auto_mutate</b> (<var>new_val</var>)<var><a name="index-sparse_005fauto_005fmutate-1685"></a></var><br>
<blockquote><p>Query or set the internal variable that controls whether Octave will
automatically mutate sparse matrices to real matrices to save memory. 
For example,

     <pre class="example">          s = speye(3);
          sparse_auto_mutate (false)
          s (:, 1) = 1;
          typeinfo (s)
          &rArr; sparse matrix
          sparse_auto_mutate (true)
          s (1, :) = 1;
          typeinfo (s)
          &rArr; matrix
</pre>
        </blockquote></div>

   <p>Note that the <code>sparse_auto_mutate</code> option is incompatible with
<span class="sc">matlab</span>, and so it is off by default.

   </body></html>