There was some discussion about this in
https://github.com/JuliaLang/julia/issues/9928

There are merits to both approaches: keeping zeros and purging them.

On Fri, 2015-02-13 at 05:03, Zouhair Mahboubi <[email protected]> 
wrote:
> I'm trying to find the column indices of non-zero elements of a given row 
> in a sparse matrix A
>
> I figured using findn would be a fast way to do that, but I was curious to 
> see how the implementation was done since all I cared about were the 
> columns for a given row and I thought I could have my own version that only 
> checked for a given row.
>
> But when I took a peek in 
> https://github.com/JuliaLang/julia/blob/master/base/sparse/sparsematrix.jl#L310
> I was surprised to see this in findn (and findnz)
>
> S.nzval[k] != 0
>
>
>
> I would have expected nzval to always be != 0 by construction?
>
> Then I had a look at nnz and countnz, and I was again surprised to see that 
> they have slightly different implementations!
> Especially given the help for the two functions (which makes sense since 
> one expects all stored elements in the sparse matrix to be non-zero)
>
> help?> nnz
>> Base.nnz(A)
>>    Returns the number of stored (filled) elements in a sparse matrix.
>> help?> countnz
>> Base.countnz(A)
>>    Counts the number of nonzero values in array A (dense or sparse).
>>    Note that this is not a constant-time operation. For sparse
>>    matrices, one should usually use "nnz", which returns the number
>>    of stored values.
>
>
>
> The only way I can see that happening, is if the user manually modifies 
> nzval? i.e.
> A = spdiagm(ones(5));
> println(nnz(A), countnz(A)) #prints 55
> A[1,1] = 0
> println(nnz(A), countnz(A)) #prints 44
> A.nzval[1] = 0
> println(nnz(A), countnz(A)) #prints 43
>
>
> So few questions:
>
>
>    - what is the fastet way to do what I was initially trying to 
>    accomplish: find the column indices and values of a given row in a sparse 
>    matrix A 
>    - why the checking of nzval?
>    - is that because it's ok to change nzval manually? if not, why is nzval 
>       not "protected" ?
>    
>
>
> Thanks,
> -z

Reply via email to