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

Fix #13130 - concatenation of dense and sparse should be sparse. #15172

Merged
merged 2 commits into from
May 20, 2016
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ Library improvements

* There is now a default no-op `flush(io)` function for all `IO` types ([#16403]).

* Concatenating dense and sparse matrices now returns a sparse matrix ([#15172]).

Deprecated or removed
---------------------

Expand Down Expand Up @@ -231,3 +233,4 @@ Deprecated or removed
[#15609]: https://github.com/JuliaLang/julia/issues/15609
[#15763]: https://github.com/JuliaLang/julia/issues/15763
[#16403]: https://github.com/JuliaLang/julia/issues/16403
[#15172]: https://github.com/JuliaLang/julia/issues/15172
8 changes: 8 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,14 @@ function hcat{T}(V::Vector{T}...)
[ V[j][i]::T for i=1:length(V[1]), j=1:length(V) ]
end

hcat(A::Matrix...) = typed_hcat(promote_eltype(A...), A...)
hcat{T}(A::Matrix{T}...) = typed_hcat(T, A...)

vcat(A::Matrix...) = typed_vcat(promote_eltype(A...), A...)
vcat{T}(A::Matrix{T}...) = typed_vcat(T, A...)

hvcat(rows::Tuple{Vararg{Int}}, xs::Matrix...) = typed_hvcat(promote_eltype(xs...), rows, xs...)
hvcat{T}(rows::Tuple{Vararg{Int}}, xs::Matrix{T}...) = typed_hvcat(T, rows, xs...)

## find ##

Expand Down
22 changes: 11 additions & 11 deletions base/sparse/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const CHOLMOD_MIN_VERSION = v"2.1.1"
### These offsets are defined in SuiteSparse_wrapper.c
const common_size = ccall((:jl_cholmod_common_size,:libsuitesparse_wrapper),Int,())

const cholmod_com_offsets = Array(Csize_t, 19)
const cholmod_com_offsets = Array{Csize_t}(19)
ccall((:jl_cholmod_common_offsets, :libsuitesparse_wrapper),
Void, (Ptr{Csize_t},), cholmod_com_offsets)

Expand All @@ -56,15 +56,15 @@ end

common() = commonStruct

const build_version_array = Array(Cint, 3)
const build_version_array = Array{Cint}(3)
ccall((:jl_cholmod_version, :libsuitesparse_wrapper), Cint, (Ptr{Cint},), build_version_array)
const build_version = VersionNumber(build_version_array...)

function __init__()
try
### Check if the linked library is compatible with the Julia code
if Libdl.dlsym_e(Libdl.dlopen("libcholmod"), :cholmod_version) != C_NULL
current_version_array = Array(Cint, 3)
current_version_array = Array{Cint}(3)
ccall((:cholmod_version, :libcholmod), Cint, (Ptr{Cint},), current_version_array)
current_version = VersionNumber(current_version_array...)
else # CHOLMOD < 2.1.1 does not include cholmod_version()
Expand Down Expand Up @@ -704,10 +704,10 @@ function vertcat{Tv<:VRealTypes}(A::Sparse{Tv}, B::Sparse{Tv}, values::Bool)
end

function symmetry{Tv<:VTypes}(A::Sparse{Tv}, option::Integer)
xmatched = Array(SuiteSparse_long, 1)
pmatched = Array(SuiteSparse_long, 1)
nzoffdiag = Array(SuiteSparse_long, 1)
nzdiag = Array(SuiteSparse_long, 1)
xmatched = Array{SuiteSparse_long}(1)
pmatched = Array{SuiteSparse_long}(1)
nzoffdiag = Array{SuiteSparse_long}(1)
nzdiag = Array{SuiteSparse_long}(1)
rv = ccall((@cholmod_name("symmetry", SuiteSparse_long), :libcholmod), Cint,
(Ptr{C_Sparse{Tv}}, Cint, Ptr{SuiteSparse_long}, Ptr{SuiteSparse_long},
Ptr{SuiteSparse_long}, Ptr{SuiteSparse_long}, Ptr{UInt8}),
Expand Down Expand Up @@ -956,7 +956,7 @@ end
## convertion back to base Julia types
function convert{T}(::Type{Matrix{T}}, D::Dense{T})
s = unsafe_load(D.p)
a = Array(T, s.nrow, s.ncol)
a = Array{T}(s.nrow, s.ncol)
copy!(a, D)
end
function Base.copy!(dest::AbstractArray, D::Dense)
Expand All @@ -980,7 +980,7 @@ function convert{T}(::Type{Vector{T}}, D::Dense{T})
if size(D, 2) > 1
throw(DimensionMismatch("input must be a vector but had $(size(D, 2)) columns"))
end
copy!(Array(T, size(D, 1)), D)
copy!(Array{T}(size(D, 1)), D)
end
convert{T}(::Type{Vector}, D::Dense{T}) = convert(Vector{T}, D)

Expand Down Expand Up @@ -1032,7 +1032,7 @@ function sparse(F::Factor)
SparseArrays.sortSparseMatrixCSC!(A)
p = get_perm(F)
if p != [1:s.n;]
pinv = Array(Int, length(p))
pinv = Array{Int}(length(p))
for k = 1:length(p)
pinv[p[k]] = k
end
Expand Down Expand Up @@ -1154,7 +1154,7 @@ function getindex(F::Factor, sym::Symbol)
end

function getLd!(S::SparseMatrixCSC)
d = Array(eltype(S), size(S, 1))
d = Array{eltype(S)}(size(S, 1))
fill!(d, 0)
col = 1
for k = 1:length(S.nzval)
Expand Down
42 changes: 21 additions & 21 deletions base/sparse/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ function spmatmul{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}, B::SparseMatrixCSC{Tv,Ti};
colptrB = B.colptr; rowvalB = B.rowval; nzvalB = B.nzval
# TODO: Need better estimation of result space
nnzC = min(mA*nB, length(nzvalA) + length(nzvalB))
colptrC = Array(Ti, nB+1)
rowvalC = Array(Ti, nnzC)
nzvalC = Array(Tv, nnzC)
colptrC = Array{Ti}(nB+1)
rowvalC = Array{Ti}(nnzC)
nzvalC = Array{Tv}(nnzC)

@inbounds begin
ip = 1
Expand Down Expand Up @@ -302,7 +302,7 @@ function triu{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti}, k::Integer=0)
if (k > 0 && k > n) || (k < 0 && -k > m)
throw(BoundsError())
end
colptr = Array(Ti, n+1)
colptr = Array{Ti}(n+1)
nnz = 0
for col = 1 : min(max(k+1,1), n+1)
colptr[col] = 1
Expand All @@ -314,8 +314,8 @@ function triu{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti}, k::Integer=0)
end
colptr[col+1] = nnz+1
end
rowval = Array(Ti, nnz)
nzval = Array(Tv, nnz)
rowval = Array{Ti}(nnz)
nzval = Array{Tv}(nnz)
A = SparseMatrixCSC(m, n, colptr, rowval, nzval)
for col = max(k+1,1) : n
c1 = S.colptr[col]
Expand All @@ -333,7 +333,7 @@ function tril{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti}, k::Integer=0)
if (k > 0 && k > n) || (k < 0 && -k > m)
throw(BoundsError())
end
colptr = Array(Ti, n+1)
colptr = Array{Ti}(n+1)
nnz = 0
colptr[1] = 1
for col = 1 : min(n, m+k)
Expand All @@ -347,8 +347,8 @@ function tril{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti}, k::Integer=0)
for col = max(min(n, m+k)+2,1) : n+1
colptr[col] = nnz+1
end
rowval = Array(Ti, nnz)
nzval = Array(Tv, nnz)
rowval = Array{Ti}(nnz)
nzval = Array{Tv}(nnz)
A = SparseMatrixCSC(m, n, colptr, rowval, nzval)
for col = 1 : min(n, m+k)
c1 = S.colptr[col+1]-1
Expand All @@ -367,10 +367,10 @@ end
function sparse_diff1{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti})
m,n = size(S)
m > 1 || return SparseMatrixCSC(0, n, ones(Ti,n+1), Ti[], Tv[])
colptr = Array(Ti, n+1)
colptr = Array{Ti}(n+1)
numnz = 2 * nnz(S) # upper bound; will shrink later
rowval = Array(Ti, numnz)
nzval = Array(Tv, numnz)
rowval = Array{Ti}(numnz)
nzval = Array{Tv}(numnz)
numnz = 0
colptr[1] = 1
for col = 1 : n
Expand Down Expand Up @@ -406,10 +406,10 @@ end

function sparse_diff2{Tv,Ti}(a::SparseMatrixCSC{Tv,Ti})
m,n = size(a)
colptr = Array(Ti, max(n,1))
colptr = Array{Ti}(max(n,1))
numnz = 2 * nnz(a) # upper bound; will shrink later
rowval = Array(Ti, numnz)
nzval = Array(Tv, numnz)
rowval = Array{Ti}(numnz)
nzval = Array{Tv}(numnz)

z = zero(Tv)

Expand Down Expand Up @@ -561,8 +561,8 @@ function normestinv{T}(A::SparseMatrixCSC{T}, t::Integer = min(2,maximum(size(A)
if t > n
throw(ArgumentError("number of blocks must not be greater than $n"))
end
ind = Array(Int64, n)
ind_hist = Array(Int64, maxiter * t)
ind = Array{Int64}(n)
ind_hist = Array{Int64}(maxiter * t)

Ti = typeof(float(zero(T)))

Expand All @@ -584,7 +584,7 @@ function normestinv{T}(A::SparseMatrixCSC{T}, t::Integer = min(2,maximum(size(A)
end

# Generate the block matrix
X = Array(Ti, n, t)
X = Array{Ti}(n, t)
X[1:n,1] = 1
for j = 2:t
while true
Expand Down Expand Up @@ -734,9 +734,9 @@ function kron{Tv,Ti}(a::SparseMatrixCSC{Tv,Ti}, b::SparseMatrixCSC{Tv,Ti})

m,n = mA*mB, nA*nB

colptr = Array(Ti, n+1)
rowval = Array(Ti, numnz)
nzval = Array(Tv, numnz)
colptr = Array{Ti}(n+1)
rowval = Array{Ti}(numnz)
nzval = Array{Tv}(numnz)

colptr[1] = 1

Expand Down
Loading