[julia-users] why is there no colvals function for sparse matrices in base?
There is a rowals function, and then there is a find function, and the find function actually allows you to write a one line colvals function: colvals(S::SparseMatrixCSC) = round(Int, floor(find(S)/(size(S, 1)+0.1))+1) shouldn't someone add this to base?
Re: [julia-users] why is there no colvals function for sparse matrices in base?
The reason rowvals exists is to access the vector of row-indices of a CSC-matrix, i.e. one of the internals of CSC, to allow efficient iteration over the non-zeros. However, there is no equivalent colvals internal, so there is little reason to do this and even less reason to encourage it. Consider: julia> s = sprand(10^7, 10^7, 1e-7); julia> @time rowvals(s); 0.04 seconds (4 allocations: 160 bytes) julia> @time colvals(s); 0.508060 seconds (20 allocations: 533.748 MB, 14.61% gc time) On Fri, 2016-04-01 at 10:48, Anonymous wrote: > There is a rowals function, and then there is a find function, and the find > function actually allows you to write a one line colvals function: > > colvals(S::SparseMatrixCSC) = round(Int, floor(find(S)/(size(S, 1)+0.1))+1) > > shouldn't someone add this to base?
Re: [julia-users] why is there no colvals function for sparse matrices in base?
but what if I need to access the coordinates of the nonzero entries of a sparse matrix for some reason? Is there a faster way to do it than using my colvals function? On Friday, April 1, 2016 at 2:03:31 AM UTC-7, Mauro wrote: > > The reason rowvals exists is to access the vector of row-indices of a > CSC-matrix, i.e. one of the internals of CSC, to allow efficient > iteration over the non-zeros. However, there is no equivalent colvals > internal, so there is little reason to do this and even less reason to > encourage it. Consider: > > julia> s = sprand(10^7, 10^7, 1e-7); > > julia> @time rowvals(s); > 0.04 seconds (4 allocations: 160 bytes) > > julia> @time colvals(s); > 0.508060 seconds (20 allocations: 533.748 MB, 14.61% gc time) > > > On Fri, 2016-04-01 at 10:48, Anonymous > > wrote: > > There is a rowals function, and then there is a find function, and the > find > > function actually allows you to write a one line colvals function: > > > > colvals(S::SparseMatrixCSC) = round(Int, floor(find(S)/(size(S, > 1)+0.1))+1) > > > > shouldn't someone add this to base? >
Re: [julia-users] why is there no colvals function for sparse matrices in base?
On Fri, 2016-04-01 at 11:07, Anonymous wrote: > but what if I need to access the coordinates of the nonzero entries of a > sparse matrix for some reason? Is there a faster way to do it than using > my colvals function? help?> nzrange search: nzrange nzrange(A, col) Return the range of indices to the structural nonzero values of a sparse matrix column. In conjunction with nonzeros(A) and rowvals(A), this allows for convenient iterating over a sparse matrix : A = sparse(I,J,V) rows = rowvals(A) vals = nonzeros(A) m, n = size(A) for i = 1:n for j in nzrange(A, i) row = rows[j] val = vals[j] # perform sparse wizardry... end end So this gives all the indices of the non-zeros: A = sparse(I,J,V) rows = rowvals(A) m, n = size(A) for i = 1:n col_ind = i for j in nzrange(A, i) row_ind = rows[j] # perform sparse wizardry... end end > On Friday, April 1, 2016 at 2:03:31 AM UTC-7, Mauro wrote: >> >> The reason rowvals exists is to access the vector of row-indices of a >> CSC-matrix, i.e. one of the internals of CSC, to allow efficient >> iteration over the non-zeros. However, there is no equivalent colvals >> internal, so there is little reason to do this and even less reason to >> encourage it. Consider: >> >> julia> s = sprand(10^7, 10^7, 1e-7); >> >> julia> @time rowvals(s); >> 0.04 seconds (4 allocations: 160 bytes) >> >> julia> @time colvals(s); >> 0.508060 seconds (20 allocations: 533.748 MB, 14.61% gc time) >> >> >> On Fri, 2016-04-01 at 10:48, Anonymous > >> wrote: >> > There is a rowals function, and then there is a find function, and the >> find >> > function actually allows you to write a one line colvals function: >> > >> > colvals(S::SparseMatrixCSC) = round(Int, floor(find(S)/(size(S, >> 1)+0.1))+1) >> > >> > shouldn't someone add this to base? >>
Re: [julia-users] why is there no colvals function for sparse matrices in base?
oh cool that's convenient, for some reason the nzrange function isn't mentioned in the documentation. On Friday, April 1, 2016 at 2:46:53 AM UTC-7, Mauro wrote: > > On Fri, 2016-04-01 at 11:07, Anonymous > > wrote: > > but what if I need to access the coordinates of the nonzero entries of a > > sparse matrix for some reason? Is there a faster way to do it than > using > > my colvals function? > > help?> nzrange > search: nzrange > > nzrange(A, col) > > Return the range of indices to the structural nonzero values of a sparse > matrix column. In conjunction with nonzeros(A) and rowvals(A), this allows > for convenient > iterating over a sparse matrix : > > A = sparse(I,J,V) > rows = rowvals(A) > vals = nonzeros(A) > m, n = size(A) > for i = 1:n > for j in nzrange(A, i) > row = rows[j] > val = vals[j] > # perform sparse wizardry... > end > end > > So this gives all the indices of the non-zeros: > > A = sparse(I,J,V) > rows = rowvals(A) > m, n = size(A) > for i = 1:n > col_ind = i > for j in nzrange(A, i) > row_ind = rows[j] > # perform sparse wizardry... > end > end > > > > On Friday, April 1, 2016 at 2:03:31 AM UTC-7, Mauro wrote: > >> > >> The reason rowvals exists is to access the vector of row-indices of a > >> CSC-matrix, i.e. one of the internals of CSC, to allow efficient > >> iteration over the non-zeros. However, there is no equivalent colvals > >> internal, so there is little reason to do this and even less reason to > >> encourage it. Consider: > >> > >> julia> s = sprand(10^7, 10^7, 1e-7); > >> > >> julia> @time rowvals(s); > >> 0.04 seconds (4 allocations: 160 bytes) > >> > >> julia> @time colvals(s); > >> 0.508060 seconds (20 allocations: 533.748 MB, 14.61% gc time) > >> > >> > >> On Fri, 2016-04-01 at 10:48, Anonymous > > >> wrote: > >> > There is a rowals function, and then there is a find function, and > the > >> find > >> > function actually allows you to write a one line colvals function: > >> > > >> > colvals(S::SparseMatrixCSC) = round(Int, floor(find(S)/(size(S, > >> 1)+0.1))+1) > >> > > >> > shouldn't someone add this to base? > >> >