Previous: ReturnType, Up: Operators and Functions


22.1.3.3 Mathematical Considerations

The attempt has been made to make sparse matrices behave in exactly the same manner as there full counterparts. However, there are certain differences and especially differences with other products sparse implementations.

Firstly, the "./" and ".^" operators must be used with care. Consider what the examples

       s = speye (4);
       a1 = s .^ 2;
       a2 = s .^ s;
       a3 = s .^ -2;
       a4 = s ./ 2;
       a5 = 2 ./ s;
       a6 = s ./ s;

will give. The first example of s raised to the power of 2 causes no problems. However s raised element-wise to itself involves a a large number of terms 0 .^ 0 which is 1. There s .^ s is a full matrix.

Likewise s .^ -2 involves terms terms like 0 .^ -2 which is infinity, and so s .^ -2 is equally a full matrix.

For the "./" operator s ./ 2 has no problems, but 2 ./ s involves a large number of infinity terms as well and is equally a full matrix. The case of s ./ s involves terms like 0 ./ 0 which is a NaN and so this is equally a full matrix with the zero elements of s filled with NaN values.

The above behaviour is consistent with full matrices, but is not consistent with sparse implementations in other products.

A particular problem of sparse matrices comes about due to the fact that as the zeros are not stored, the sign-bit of these zeros is equally not stored. In certain cases the sign-bit of zero is important. For example

      a = 0 ./ [-1, 1; 1, -1];
      b = 1 ./ a
      => -Inf            Inf
          Inf           -Inf
      c = 1 ./ sparse (a)
      =>  Inf            Inf
          Inf            Inf

To correct this behaviour would mean that zero elements with a negative sign-bit would need to be stored in the matrix to ensure that their sign-bit was respected. This is not done at this time, for reasons of efficient, and so the user is warned that calculations where the sign-bit of zero is important must not be done using sparse matrices.

In general any function or operator used on a sparse matrix will result in a sparse matrix with the same or a larger number of non-zero elements than the original matrix. This is particularly true for the important case of sparse matrix factorizations.

Also discuss issues of fill-in. Discuss symamd etc, and mention randperm that is included elsewhere in the docs...

WRITE ME