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

Added sparse concatenation functions #585

Merged
merged 4 commits into from
Mar 15, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions jl/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ float32(x::AbstractArray) = copy_to(similar(x,Float32), x)
float64(x::AbstractArray) = copy_to(similar(x,Float64), x)
float (x::AbstractArray) = copy_to(similar(x,typeof(float(one(eltype(x))))), x)

full(x::AbstractArray) = x

## Unary operators ##

conj{T<:Real}(x::AbstractArray{T}) = x
Expand Down Expand Up @@ -350,7 +352,7 @@ function hcat{T}(A::Union(AbstractMatrix{T},AbstractVector{T})...)
ncols += (nd==2 ? size(Aj,2) : 1)
if size(Aj, 1) != nrows; error("hcat: mismatched dimensions"); end
end
B = similar(A[1], nrows, ncols)
B = similar(full(A[1]), nrows, ncols)
pos = 1
if dense
for k=1:nargs
Expand All @@ -377,7 +379,7 @@ function vcat{T}(A::AbstractMatrix{T}...)
for j = 2:nargs
if size(A[j], 2) != ncols; error("vcat: mismatched dimensions"); end
end
B = similar(A[1], nrows, ncols)
B = similar(full(A[1]), nrows, ncols)
pos = 1
for k=1:nargs
Ak = A[k]
Expand Down Expand Up @@ -532,7 +534,7 @@ function hvcat{T}(rows::(Int...), as::AbstractMatrix{T}...)
a += rows[i]
end

out = similar(as[1], T, nr, nc)
out = similar(full(as[1]), T, nr, nc)

a = 1
r = 1
Expand Down
79 changes: 79 additions & 0 deletions jl/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,85 @@ function (*){TvX,TiX,TvY,TiY}(X::SparseMatrixCSC{TvX,TiX}, Y::SparseMatrixCSC{Tv
SparseMatrixCSC(mX, nY, colptr, rowval, nzval)
end

# Sparse concatenation

function vcat{Tv, Ti}(X::SparseMatrixCSC{Tv, Ti}...)
num = length(X)
mX = [ size(x, 1) | x = X ]
nX = [ size(x, 2) | x = X ]
n = nX[1]
for i = 2 : num
if nX[i] != n; error("error in vcat: mismatched dimensions"); end
end
m = sum(mX)

colptr = Array(Ti, n + 1)
nnzX = [ nnz(x) | x = X ]
nnz_res = sum(nnzX)
rowval = Array(Ti, nnz_res)
nzval = Array(Tv, nnz_res)

colptr[1] = 1
for c = 1 : n
mX_sofar = 0
rr1 = colptr[c]
for i = 1 : num
rX1 = X[i].colptr[c]
rX2 = X[i].colptr[c + 1] - 1
rr2 = rr1 + (rX2 - rX1)

rowval[rr1 : rr2] = X[i].rowval[rX1 : rX2] + mX_sofar
nzval[rr1 : rr2] = X[i].nzval[rX1 : rX2]
mX_sofar += mX[i]
rr1 = rr2 + 1
end
colptr[c + 1] = rr1
end
SparseMatrixCSC(m, n, colptr, rowval, nzval)
end

function hcat{Tv, Ti}(X::SparseMatrixCSC{Tv, Ti}...)
num = length(X)
mX = [ size(x, 1) | x = X ]
nX = [ size(x, 2) | x = X ]
m = mX[1]
for i = 2 : num
if mX[i] != m; error("error in hcat: mismatched dimensions"); end
end
n = sum(nX)

colptr = Array(Ti, n + 1)
nnzX = [ nnz(x) | x = X ]
nnz_res = sum(nnzX)
rowval = Array(Ti, nnz_res)
nzval = Array(Tv, nnz_res)

nnz_sofar = 0
nX_sofar = 0
for i = 1 : num
colptr[(1 : nX[i] + 1) + nX_sofar] = X[i].colptr + nnz_sofar
rowval[(1 : nnzX[i]) + nnz_sofar] = X[i].rowval
nzval[(1 : nnzX[i]) + nnz_sofar] = X[i].nzval
nnz_sofar += nnzX[i]
nX_sofar += nX[i]
end

SparseMatrixCSC(m, n, colptr, rowval, nzval)
end

function hvcat{Tv, Ti}(rows::(Int...), X::SparseMatrixCSC{Tv, Ti}...)
nbr = length(rows) # number of block rows

tmp_rows = Array(SparseMatrixCSC{Tv,Ti}, nbr)
k = 0
for i = 1 : nbr
tmp_rows[i] = hcat(X[(1 : rows[i]) + k]...)
k += rows[i]
end
vcat(ntuple(nbr, x->tmp_rows[x])...)
end


## SparseAccumulator and related functions

type SparseAccumulator{Tv,Ti} <: AbstractVector{Tv}
Expand Down