Skip to content

Commit

Permalink
Fixes and tests for sparse scale!
Browse files Browse the repository at this point in the history
- scale!(C, b, A) was checking incorrect matrix dimension
- scale!(b, A) and scale!(A, b) returned uninitialized memory
  • Loading branch information
simonster committed Jan 13, 2015
1 parent 7f915c7 commit 3e7f8bd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
18 changes: 11 additions & 7 deletions base/linalg/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,11 @@ function scale!{Tv,Ti}(C::SparseMatrixCSC{Tv,Ti}, A::SparseMatrixCSC, b::Vector)
m, n = size(A)
(n==length(b) && size(A)==size(C)) || throw(DimensionMismatch())
numnz = nnz(A)
C.colptr = convert(Array{Ti}, A.colptr)
C.rowval = convert(Array{Ti}, A.rowval)
C.nzval = Array(Tv, numnz)
if C !== A
C.colptr = convert(Array{Ti}, A.colptr)
C.rowval = convert(Array{Ti}, A.rowval)
C.nzval = Array(Tv, numnz)
end
for col = 1:n, p = A.colptr[col]:(A.colptr[col+1]-1)
C.nzval[p] = A.nzval[p] * b[col]
end
Expand All @@ -609,11 +611,13 @@ end

function scale!{Tv,Ti}(C::SparseMatrixCSC{Tv,Ti}, b::Vector, A::SparseMatrixCSC)
m, n = size(A)
(n==length(b) && size(A)==size(C)) || throw(DimensionMismatch())
(m==length(b) && size(A)==size(C)) || throw(DimensionMismatch())
numnz = nnz(A)
C.colptr = convert(Array{Ti}, A.colptr)
C.rowval = convert(Array{Ti}, A.rowval)
C.nzval = Array(Tv, numnz)
if C !== A
C.colptr = convert(Array{Ti}, A.colptr)
C.rowval = convert(Array{Ti}, A.rowval)
C.nzval = Array(Tv, numnz)
end
for col = 1:n, p = A.colptr[col]:(A.colptr[col+1]-1)
C.nzval[p] = A.nzval[p] * b[A.rowval[p]]
end
Expand Down
10 changes: 10 additions & 0 deletions test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ for i = 1:5
@test full(kron(a,b)) == kron(full(a), full(b))
end

# scale and scale!
sA = sprandn(3, 7, 0.5)
dA = full(sA)
b = randn(7)
@test scale(dA, b) == scale(sA, b)
@test scale(dA, b) == scale!(copy(sA), b)
b = randn(3)
@test scale(b, dA) == scale(b, sA)
@test scale(b, dA) == scale!(b, copy(sA))

# reductions
@test sum(se33)[1] == 3.0
@test sum(se33, 1) == [1.0 1.0 1.0]
Expand Down

2 comments on commit 3e7f8bd

@tkelman
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this also an issue on release-0.3? @JuliaBackports

@simonster
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cherry picked in e563717

Please sign in to comment.