JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.62k stars 5.48k forks source link

bug in hcat for sparse matrices #4938

Closed joachimdahl closed 10 years ago

joachimdahl commented 10 years ago

I noticed the following bug concatenating sparse matrices. I could not reproduce it using simpler matrices...

julia> A=SparseMatrixCSC(6,3,[1,2,3,4],[6,1,6],[-1.0,-1.0,1.0]) 6x3 sparse matrix with 3 Float64 nonzeros: [6, 1] = -1.0 [1, 2] = -1.0 [6, 3] = 1.0

julia> B=SparseMatrixCSC(6,5,[2,6,10,14,18,23], [6,1,2,3,6,1,4,5,6,1,4,5,6,1,2,3,6,1,2,3,4,5], [1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0]) 6x5 sparse matrix with 22 Float64 nonzeros: [1, 1] = 1.0 [2, 1] = 1.0 [3, 1] = 1.0 [6, 1] = 1.0 [1, 2] = 1.0 [4, 2] = 1.0 [5, 2] = 1.0 [6, 2] = 1.0 [1, 3] = 1.0 [4, 3] = 1.0 [5, 3] = 1.0 [6, 3] = 1.0 [1, 4] = 1.0 [2, 4] = 1.0 [3, 4] = 1.0 [6, 4] = 1.0 [1, 5] = 1.0 [2, 5] = 1.0 [3, 5] = 1.0 [4, 5] = 1.0 [5, 5] = 1.0

julia> [A B] ERROR: tried to assign 23 elements to 22 destinations in error at error.jl:21 in setindex! at array.jl:435 in hcat at sparse/sparsematrix.jl:1265

ViralBShah commented 10 years ago

Sparse indexing and concatenation need to be fully revisited. At least someone is using these now!

joachimdahl commented 10 years ago

Sorry, my example was buggy. I created the compressed structure directly, and the indices were not sorted...

ViralBShah commented 10 years ago

Of course - in such a case you should use sparse to construct the sparse matrix.

joachimdahl commented 10 years ago

I agree... I had a compressed column-storage from C (unsorted), which I was importing...

On Wed, Nov 27, 2013 at 9:50 AM, Viral B. Shah notifications@github.comwrote:

Of course - in such a case you should use sparse to construct the sparse matrix.

— Reply to this email directly or view it on GitHubhttps://github.com/JuliaLang/julia/issues/4938#issuecomment-29368411 .

ViralBShah commented 10 years ago

You can do a double transpose if you have an unsorted SparseMatrixCSC and you will end up with a sorted one.

joachimdahl commented 10 years ago

thanks, that actually ended up being my solution.

Let me congratulate with the efforts you have made so far with Julia. I have used it for a few weeks, and love everything about it. Before I used Python or Matlab to prototype numerical algorithms we work on at MOSEK, and Julia seems to be the perfect match for me.

On Wed, Nov 27, 2013 at 10:07 AM, Viral B. Shah notifications@github.comwrote:

You can do a double transpose if you have an unsorted SparseMatrixCSC and you will end up with a sorted one.

— Reply to this email directly or view it on GitHubhttps://github.com/JuliaLang/julia/issues/4938#issuecomment-29369295 .

ViralBShah commented 10 years ago

Thanks. What is MOSEK? Great to see a pull request from you too, and all the stuff you are asking for is good stuff.

ViralBShah commented 10 years ago

Is this www.mosek.com? In which case, I must Cc: @iainnz and @mlubin.

IainNZ commented 10 years ago

Glad to see Julia making a splash in the OR world!

joachimdahl commented 10 years ago

Yes, we specialize in interior-point solvers for conic optimization.

On Wed, Nov 27, 2013 at 2:54 PM, Iain Dunning notifications@github.comwrote:

Glad to see Julia making a splash in the OR world!

— Reply to this email directly or view it on GitHubhttps://github.com/JuliaLang/julia/issues/4938#issuecomment-29385574 .