octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

the purpose of Sparse<T>::nzmax


From: Jaroslav Hajek
Subject: the purpose of Sparse<T>::nzmax
Date: Fri, 9 Apr 2010 15:02:54 +0200

hi,

I'm wondering about the purpose of Sparse<T>::nzmax. The comment in
the source says that it's supposed to be an alias for capacity(),
which should be the amount of storage allocated for nonzero elements,
whereas nnz() is the actual amount of nonzero elements (equivalent to
cidx(cols())).

However, it seems that contrary to its "documentation", nzmax is
generally used throughout both liboctave and interpreter as a synonym
for nnz. I believe this may be the reason behind bug #29483. Until my
recent changes, nnz() and capacity() were probably always equal, but
now that Sparse matrix tries to avoid some reallocations (mainly, if
maybe_compress discards just a few elements, the arrays are *not*
reallocated and copied to truncate the excess storage), this invariant
no longer holds.

For instance, consider the code

SparseBoolMatrix::operator ! (void) const
{
  octave_idx_type nr = rows ();
  octave_idx_type nc = cols ();
  octave_idx_type nz1 = nzmax ();  # GOTCHA
  octave_idx_type nz2 = nr*nc - nz1;

  SparseBoolMatrix r (nr, nc, nz2);

  octave_idx_type ii = 0;
  octave_idx_type jj = 0;
  r.cidx (0) = 0;
  for (octave_idx_type i = 0; i < nc; i++)
    {
      for (octave_idx_type j = 0; j < nr; j++)
        {
          if (jj < cidx(i+1) && ridx(jj) == j)
            jj++;
          else
            {
              r.data(ii) = true;
              r.ridx(ii++) = j;
            }
        }
      r.cidx (i+1) = ii;
    }

  return r;
}

It's all good except the GOTCHA, because obviously it is the true
number of nonzero elements that matters, not the amount of storage.

This particular line is due to John's 5604:2857357f9d3c, but that
seems to be just some renaming changeset, so it appears this stuff is
even older.
What do you think is the best solution? Shall I try to find invalid
nzmax usages, or can nzmax be made an alias of nnz() (which may
possibly break something else)?



-- 
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz


reply via email to

[Prev in Thread] Current Thread [Next in Thread]