[SciPy-user] Random sparse matrices

Anne Archibald peridot.faceted@gmail....
Fri Apr 25 15:04:00 CDT 2008


On 25/04/2008, Nathan Bell <wnbell@gmail.com> wrote:
> On Fri, Apr 25, 2008 at 9:17 AM, Mico Filós <elmico.filos@gmail.com> wrote:
>  > Dear all,
>  >
>  >  here is my first attempt. I basically use Nathan's suggested
>  >  functions, and _rand_sparse incorporates the algorithm proposed by
>  >  David to avoid ending up with fewer nonzero elements than expected. It
>  >  is the first time I propose an update
>  >  for scipy code, so be lenient with me :)
>
>
> Thanks for your contribution Mico.  Unfortunately, the line
>
>    rand_seq = permutation(m*n)[:nnz]
>
> is a *dense* MxN operation, so we cannot use this approach.
>
>  MATLAB's sprand() and sprandn() have the same artifact as the code I
>  presented, so I don't know whether it's worth trying to avoid the
>  duplicate entries.
>
>  If you can figure out an economial way to produce exactly nnz elements
>  in the result, then we would probably use it.  I wasn't able to come
>  up with anything better than the MATLAB approach.

Here's an approach that works. Not ideal, but still only O(nnz): pick
nnz distinct integers. Throw out any repeats and pick replacements.
Repeat until you have no repeats. Requires an average of just a few
iterations unless nnz>(m*n)/2 (say), in which case you can safely just
use permutation().

Anne
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sprand.py
Type: text/x-python
Size: 729 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/scipy-user/attachments/20080425/0e9bcb19/attachment.py 


More information about the SciPy-user mailing list