Skip to content

Commit

Permalink
Merge branch 'master' of github.com:JuliaLang/julia
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKarpinski committed Jun 19, 2012
2 parents a5cde57 + 8b3b4b2 commit 3cf0e48
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 25 deletions.
33 changes: 30 additions & 3 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -653,13 +653,40 @@ end

for f in (:+, :-, :.*, :div, :mod, :&, :|, :$)
@eval begin
function ($f)(x::Number, y::AbstractArray)
function ($f){S,T}(A::AbstractArray{S}, B::AbstractArray{T})
F = Array(promote_type(S,T), promote_shape(size(A),size(B)))
for i=1:numel(A)
F[i] = ($f)(A[i], B[i])
end
return F
end
function ($f){T}(A::Number, B::AbstractArray{T})
F = similar(B, promote_type(typeof(A),T))
for i=1:numel(B)
F[i] = ($f)(A, B[i])
end
return F
end
function ($f){T}(A::AbstractArray{T}, B::Number)
F = similar(A, promote_type(T,typeof(B)))
for i=1:numel(A)
F[i] = ($f)(A[i], B)
end
return F
end
end
end

# functions that should give an Int result for Bool arrays
for f in (:+, :-, :div)
@eval begin
function ($f)(x::Bool, y::Array{Bool})
reshape([ ($f)(x, y[i]) for i=1:numel(y) ], size(y))
end
function ($f)(x::AbstractArray, y::Number)
function ($f)(x::Array{Bool}, y::Bool)
reshape([ ($f)(x[i], y) for i=1:numel(x) ], size(x))
end
function ($f)(x::AbstractArray, y::AbstractArray)
function ($f)(x::Array{Bool}, y::Array{Bool})
shp = promote_shape(size(x),size(y))
reshape([ ($f)(x[i], y[i]) for i=1:numel(x) ], shp)
end
Expand Down
6 changes: 3 additions & 3 deletions base/darray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -798,20 +798,20 @@ end
for f in (:+, :-, :.*, :./, :.^, :&, :|, :$)
@eval begin
function ($f){T}(A::Number, B::SubOrDArray{T})
S = typeof(($f)(one(A),one(T)))
S = eltype(($f)([one(A)],[one(T)]))
darray((T,lsz,da)->($f)(A, localize(B, da)),
S, size(B), distdim(B), procs(B))
end
function ($f){T}(A::SubOrDArray{T}, B::Number)
S = typeof(($f)(one(T),one(B)))
S = eltype(($f)([one(T)],[one(B)]))
darray((T,lsz,da)->($f)(localize(A, da), B),
S, size(A), distdim(A), procs(A))
end
function ($f){T,S}(A::SubOrDArray{T}, B::SubOrDArray{S})
if size(A) != size(B)
error("argument dimensions must match")
end
R = typeof(($f)(one(T), one(S)))
R = eltype(($f)([one(T)],[one(S)]))
darray((T,lsz,da)->($f)(localize(A, da), localize(B, da)),
R, size(A), distdim(A), procs(A))
end
Expand Down
68 changes: 54 additions & 14 deletions extras/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ function SparseMatrixCSC(Tv::Type, m::Int, n::Int, numnz::Integer)
SparseMatrixCSC{Tv,Ti}(m, n, colptr, rowval, nzval)
end

function SparseMatrixCSC(m::Int32, n::Int32, colptr, rowval, nzval)
return SparseMatrixCSC(int(m), int(n), colptr, rowval, nzval)
end

issparse(A::AbstractArray) = false
issparse(S::SparseMatrixCSC) = true

Expand Down Expand Up @@ -140,21 +144,19 @@ full{T}(S::SparseMatrixCSC{T}) = convert(Matrix{T}, S)

function sparse(A::Matrix)
m, n = size(A)
I, J, V = findn_nzs(A)
_jl_sparse(int32(I), int32(J), V, m, n)
(I, J, V) = findn_nzs(A)
return _jl_sparse_sorted!(I,J,V,m,n,+)
end

# _jl_sparse_sortbased uses sort to rearrange the input and construct the sparse matrix
# _jl_sparse_sort uses sort to rearrange the input and construct the sparse matrix

_jl_sparse_sortbased(I,J,V) = _jl_sparse_sortbased(I, J, V, int(max(I)), int(max(J)), +)
_jl_sparse_sort(I,J,V) = _jl_sparse_sort(I, J, V, int(max(I)), int(max(J)), +)

_jl_sparse_sortbased(I,J,V,m,n) = _jl_sparse_sortbased(I, J, V, m, n, +)
_jl_sparse_sort(I,J,V,m,n) = _jl_sparse_sort(I, J, V, m, n, +)

_jl_sparse_sortbased(I,J,V,m,n,combine) = _jl_sparse_sortbased(I, J, V, m, n, combine)

function _jl_sparse_sortbased{Ti<:Union(Int32,Int64)}(I::AbstractVector{Ti}, J::AbstractVector{Ti},
V::Union(Number, AbstractVector),
m::Int, n::Int, combine::Function)
function _jl_sparse_sort{Ti<:Union(Int32,Int64)}(I::AbstractVector{Ti}, J::AbstractVector{Ti},
V::Union(Number, AbstractVector),
m::Int, n::Int, combine::Function)

if length(I) == 0; return spzeros(eltype(V),m,n); end

Expand Down Expand Up @@ -184,6 +186,15 @@ function _jl_sparse_sortbased{Ti<:Union(Int32,Int64)}(I::AbstractVector{Ti}, J::

if isa(V, Number); V = fill(V, length(I)); end

return _jl_sparse_sorted!(I,J,V,m,n,combine)
end

_jl_sparse_sorted!(I,J,V,m,n) = _jl_sparse_sorted!(I,J,V,m,n,+)

function _jl_sparse_sorted!{Ti<:Union(Int32,Int64)}(I::AbstractVector{Ti}, J::AbstractVector{Ti},
V::AbstractVector,
m::Int, n::Int, combine::Function)

cols = zeros(Ti, n+1)
cols[1] = 1 # For cumsum purposes
cols[J[1] + 1] = 1
Expand Down Expand Up @@ -330,8 +341,37 @@ function sparse{Tv,Ti<:Union(Int32,Int64)}(I::AbstractVector{Ti}, J::AbstractVec
return SparseMatrixCSC(nrow, ncol, RpT, RiT, RxT)
end

function find(S::SparseMatrixCSC)
sz = size(S)
I, J = findn(S)
return sub2ind(sz, I, J)
end

function findn{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti})
numnz = nnz(S)
I = Array(Ti, numnz)
J = Array(Ti, numnz)

count = 1
for col = 1 : S.n, k = S.colptr[col] : (S.colptr[col+1]-1)
if S.nzval[k] != 0
I[count] = S.rowval[k]
J[count] = col
count += 1
else
println("Warning: sparse matrix contains explicit stored zeros.")
end
end

if numnz != count-1
I = I[1:count]
J = J[1:count]
end

return (I, J)
end

function find{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti})
function findn_nzs{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti})
numnz = nnz(S)
I = Array(Ti, numnz)
J = Array(Ti, numnz)
Expand All @@ -345,7 +385,7 @@ function find{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti})
V[count] = S.nzval[k]
count += 1
else
println("Warning: sparse matrix has explicit stored zeros.")
println("Warning: sparse matrix contains explicit stored zeros.")
end
end

Expand Down Expand Up @@ -388,8 +428,8 @@ speye(m::Int, n::Int) = speye(Float64, m, n)

function speye(T::Type, m::Int, n::Int)
x = int32(min(m,n))
rowval = linspace(int32(1), x, x)
colptr = [rowval, int32(x+1)*ones(Int32, n+1-x)]
rowval = [int32(1):x]
colptr = [rowval, int32((x+1)*ones(Int32, n+1-x))]
nzval = ones(T, x)
return SparseMatrixCSC(m, n, colptr, rowval, nzval)
end
Expand Down
10 changes: 5 additions & 5 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,14 @@ b2 = bitrand(TT, n1, n2)
@check_bit_operation (&) BitArray{TT} (b1, b2)
@check_bit_operation (|) BitArray{TT} (b1, b2)
@check_bit_operation ($) BitArray{TT} (b1, b2)
@check_bit_operation (-) Array{Uint} (b1, b2)
@check_bit_operation (-) Array{TT} (b1, b2)
@check_bit_operation (.*) BitArray{TT} (b1, b2)
@check_bit_operation (./) Array{Float64} (b1, b2)
@check_bit_operation (.^) Array{Float64} (b1, b2)

b2 = bitones(TT, n1, n2)
@check_bit_operation div Array{Uint} (b1, b2)
@check_bit_operation mod Array{Uint} (b1, b2)
@check_bit_operation div Array{TT} (b1, b2)
@check_bit_operation mod Array{TT} (b1, b2)

while true
global b1
Expand All @@ -259,8 +259,8 @@ b2 = randi(10, n1, n2)
@check_bit_operation (.*) Array{S} (b1, b2)
@check_bit_operation (./) Array{Float64} (b1, b2)
@check_bit_operation (.^) Array{Float64} (b1, b2)
@check_bit_operation div Array{Uint} (b1, b2)
@check_bit_operation mod Array{Int} (b1, b2)
@check_bit_operation div Array{S} (b1, b2)
@check_bit_operation mod Array{S} (b1, b2)

while true
global b1
Expand Down

0 comments on commit 3cf0e48

Please sign in to comment.