Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some issues with sparse matrix operations combinations: slice, sum and assignment #103

Closed
alsam opened this issue Apr 9, 2014 · 2 comments
Labels
sparse Sparse arrays

Comments

@alsam
Copy link

alsam commented Apr 9, 2014

While trying some sparse matrix operations stumbled upon

julia> y = spzeros(7,7)
7x7 sparse matrix with 0 Float64 entries:

julia> y[2,1] = y[2,3] = y[2,5] = 3
3
julia> z = spzeros(7,7)
7x7 sparse matrix with 0 Float64 entries:

julia> z = -y
7x7 sparse matrix with 3 Float64 entries:
    [2, 1]  =  -3.0
    [2, 3]  =  -3.0
    [2, 5]  =  -3.0

Ok, so far, so good

julia> for i = 1:7
       z[i,i] = sum(y[i,:])
       end
ERROR: no method convert(Type{Float64}, Array{Float64,1})
 in setindex! at sparse/sparsematrix.jl:969
 in anonymous at no file:2

but these operations applied separately are good:

julia> for i = 1:7
       z[i,i] = 7.0
       end

julia> for i = 1:7
       s = sum(y[i,:])
       end

Delving into it:

julia> typeof(sum(y[2,:]))

Array{Float64,1}

julia> sum(y[2,:])

1-element Array{Float64,1}:
 9.0

julia> sum([1,2,3])
6

julia> sum([1,2,3][1:2])
3

julia> typeof(sum([1,2,3][1:2]))
Int64

So why for sparse vector sum produced 1-element Array instead of a scalar value?

Thanks!

@jiahao
Copy link
Member

jiahao commented Apr 22, 2014

I could not reproduce the problem. Which version of Julia are you using?

@alsam
Copy link
Author

alsam commented Apr 23, 2014

I confirm that now it really works as expected with up-to-date Julia.

julia> y = spzeros(7,7)
7x7 sparse matrix with 0 Float64 entries:

julia> y[2,1] = y[2,3] = y[2,5] = 3
3

julia> z = -y
7x7 sparse matrix with 3 Float64 entries:
        [2, 1]  =  -3.0
        [2, 3]  =  -3.0
        [2, 5]  =  -3.0

julia> for i = 1:7
       z[i,i] = sum(y[i,:])
       end

julia> z
7x7 sparse matrix with 4 Float64 entries:
        [2, 1]  =  -3.0
        [2, 2]  =  9.0
        [2, 3]  =  -3.0
        [2, 5]  =  -3.0

So the issue was resolved.
Thanks!

@KristofferC KristofferC transferred this issue from JuliaLang/julia Nov 26, 2024
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sparse Sparse arrays
Projects
None yet
Development

No branches or pull requests

2 participants