Skip to content

Commit 3cf0e48

Browse files
Merge branch 'master' of github.com:JuliaLang/julia
2 parents a5cde57 + 8b3b4b2 commit 3cf0e48

File tree

4 files changed

+92
-25
lines changed

4 files changed

+92
-25
lines changed

base/array.jl

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -653,13 +653,40 @@ end
653653

654654
for f in (:+, :-, :.*, :div, :mod, :&, :|, :$)
655655
@eval begin
656-
function ($f)(x::Number, y::AbstractArray)
656+
function ($f){S,T}(A::AbstractArray{S}, B::AbstractArray{T})
657+
F = Array(promote_type(S,T), promote_shape(size(A),size(B)))
658+
for i=1:numel(A)
659+
F[i] = ($f)(A[i], B[i])
660+
end
661+
return F
662+
end
663+
function ($f){T}(A::Number, B::AbstractArray{T})
664+
F = similar(B, promote_type(typeof(A),T))
665+
for i=1:numel(B)
666+
F[i] = ($f)(A, B[i])
667+
end
668+
return F
669+
end
670+
function ($f){T}(A::AbstractArray{T}, B::Number)
671+
F = similar(A, promote_type(T,typeof(B)))
672+
for i=1:numel(A)
673+
F[i] = ($f)(A[i], B)
674+
end
675+
return F
676+
end
677+
end
678+
end
679+
680+
# functions that should give an Int result for Bool arrays
681+
for f in (:+, :-, :div)
682+
@eval begin
683+
function ($f)(x::Bool, y::Array{Bool})
657684
reshape([ ($f)(x, y[i]) for i=1:numel(y) ], size(y))
658685
end
659-
function ($f)(x::AbstractArray, y::Number)
686+
function ($f)(x::Array{Bool}, y::Bool)
660687
reshape([ ($f)(x[i], y) for i=1:numel(x) ], size(x))
661688
end
662-
function ($f)(x::AbstractArray, y::AbstractArray)
689+
function ($f)(x::Array{Bool}, y::Array{Bool})
663690
shp = promote_shape(size(x),size(y))
664691
reshape([ ($f)(x[i], y[i]) for i=1:numel(x) ], shp)
665692
end

base/darray.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -798,20 +798,20 @@ end
798798
for f in (:+, :-, :.*, :./, :.^, :&, :|, :$)
799799
@eval begin
800800
function ($f){T}(A::Number, B::SubOrDArray{T})
801-
S = typeof(($f)(one(A),one(T)))
801+
S = eltype(($f)([one(A)],[one(T)]))
802802
darray((T,lsz,da)->($f)(A, localize(B, da)),
803803
S, size(B), distdim(B), procs(B))
804804
end
805805
function ($f){T}(A::SubOrDArray{T}, B::Number)
806-
S = typeof(($f)(one(T),one(B)))
806+
S = eltype(($f)([one(T)],[one(B)]))
807807
darray((T,lsz,da)->($f)(localize(A, da), B),
808808
S, size(A), distdim(A), procs(A))
809809
end
810810
function ($f){T,S}(A::SubOrDArray{T}, B::SubOrDArray{S})
811811
if size(A) != size(B)
812812
error("argument dimensions must match")
813813
end
814-
R = typeof(($f)(one(T), one(S)))
814+
R = eltype(($f)([one(T)],[one(S)]))
815815
darray((T,lsz,da)->($f)(localize(A, da), localize(B, da)),
816816
R, size(A), distdim(A), procs(A))
817817
end

extras/sparse.jl

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ function SparseMatrixCSC(Tv::Type, m::Int, n::Int, numnz::Integer)
1919
SparseMatrixCSC{Tv,Ti}(m, n, colptr, rowval, nzval)
2020
end
2121

22+
function SparseMatrixCSC(m::Int32, n::Int32, colptr, rowval, nzval)
23+
return SparseMatrixCSC(int(m), int(n), colptr, rowval, nzval)
24+
end
25+
2226
issparse(A::AbstractArray) = false
2327
issparse(S::SparseMatrixCSC) = true
2428

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

141145
function sparse(A::Matrix)
142146
m, n = size(A)
143-
I, J, V = findn_nzs(A)
144-
_jl_sparse(int32(I), int32(J), V, m, n)
147+
(I, J, V) = findn_nzs(A)
148+
return _jl_sparse_sorted!(I,J,V,m,n,+)
145149
end
146150

147-
# _jl_sparse_sortbased uses sort to rearrange the input and construct the sparse matrix
151+
# _jl_sparse_sort uses sort to rearrange the input and construct the sparse matrix
148152

149-
_jl_sparse_sortbased(I,J,V) = _jl_sparse_sortbased(I, J, V, int(max(I)), int(max(J)), +)
153+
_jl_sparse_sort(I,J,V) = _jl_sparse_sort(I, J, V, int(max(I)), int(max(J)), +)
150154

151-
_jl_sparse_sortbased(I,J,V,m,n) = _jl_sparse_sortbased(I, J, V, m, n, +)
155+
_jl_sparse_sort(I,J,V,m,n) = _jl_sparse_sort(I, J, V, m, n, +)
152156

153-
_jl_sparse_sortbased(I,J,V,m,n,combine) = _jl_sparse_sortbased(I, J, V, m, n, combine)
154-
155-
function _jl_sparse_sortbased{Ti<:Union(Int32,Int64)}(I::AbstractVector{Ti}, J::AbstractVector{Ti},
156-
V::Union(Number, AbstractVector),
157-
m::Int, n::Int, combine::Function)
157+
function _jl_sparse_sort{Ti<:Union(Int32,Int64)}(I::AbstractVector{Ti}, J::AbstractVector{Ti},
158+
V::Union(Number, AbstractVector),
159+
m::Int, n::Int, combine::Function)
158160

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

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

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

189+
return _jl_sparse_sorted!(I,J,V,m,n,combine)
190+
end
191+
192+
_jl_sparse_sorted!(I,J,V,m,n) = _jl_sparse_sorted!(I,J,V,m,n,+)
193+
194+
function _jl_sparse_sorted!{Ti<:Union(Int32,Int64)}(I::AbstractVector{Ti}, J::AbstractVector{Ti},
195+
V::AbstractVector,
196+
m::Int, n::Int, combine::Function)
197+
187198
cols = zeros(Ti, n+1)
188199
cols[1] = 1 # For cumsum purposes
189200
cols[J[1] + 1] = 1
@@ -330,8 +341,37 @@ function sparse{Tv,Ti<:Union(Int32,Int64)}(I::AbstractVector{Ti}, J::AbstractVec
330341
return SparseMatrixCSC(nrow, ncol, RpT, RiT, RxT)
331342
end
332343

344+
function find(S::SparseMatrixCSC)
345+
sz = size(S)
346+
I, J = findn(S)
347+
return sub2ind(sz, I, J)
348+
end
349+
350+
function findn{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti})
351+
numnz = nnz(S)
352+
I = Array(Ti, numnz)
353+
J = Array(Ti, numnz)
354+
355+
count = 1
356+
for col = 1 : S.n, k = S.colptr[col] : (S.colptr[col+1]-1)
357+
if S.nzval[k] != 0
358+
I[count] = S.rowval[k]
359+
J[count] = col
360+
count += 1
361+
else
362+
println("Warning: sparse matrix contains explicit stored zeros.")
363+
end
364+
end
365+
366+
if numnz != count-1
367+
I = I[1:count]
368+
J = J[1:count]
369+
end
370+
371+
return (I, J)
372+
end
333373

334-
function find{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti})
374+
function findn_nzs{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti})
335375
numnz = nnz(S)
336376
I = Array(Ti, numnz)
337377
J = Array(Ti, numnz)
@@ -345,7 +385,7 @@ function find{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti})
345385
V[count] = S.nzval[k]
346386
count += 1
347387
else
348-
println("Warning: sparse matrix has explicit stored zeros.")
388+
println("Warning: sparse matrix contains explicit stored zeros.")
349389
end
350390
end
351391

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

389429
function speye(T::Type, m::Int, n::Int)
390430
x = int32(min(m,n))
391-
rowval = linspace(int32(1), x, x)
392-
colptr = [rowval, int32(x+1)*ones(Int32, n+1-x)]
431+
rowval = [int32(1):x]
432+
colptr = [rowval, int32((x+1)*ones(Int32, n+1-x))]
393433
nzval = ones(T, x)
394434
return SparseMatrixCSC(m, n, colptr, rowval, nzval)
395435
end

test/bitarray.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,14 @@ b2 = bitrand(TT, n1, n2)
228228
@check_bit_operation (&) BitArray{TT} (b1, b2)
229229
@check_bit_operation (|) BitArray{TT} (b1, b2)
230230
@check_bit_operation ($) BitArray{TT} (b1, b2)
231-
@check_bit_operation (-) Array{Uint} (b1, b2)
231+
@check_bit_operation (-) Array{TT} (b1, b2)
232232
@check_bit_operation (.*) BitArray{TT} (b1, b2)
233233
@check_bit_operation (./) Array{Float64} (b1, b2)
234234
@check_bit_operation (.^) Array{Float64} (b1, b2)
235235

236236
b2 = bitones(TT, n1, n2)
237-
@check_bit_operation div Array{Uint} (b1, b2)
238-
@check_bit_operation mod Array{Uint} (b1, b2)
237+
@check_bit_operation div Array{TT} (b1, b2)
238+
@check_bit_operation mod Array{TT} (b1, b2)
239239

240240
while true
241241
global b1
@@ -259,8 +259,8 @@ b2 = randi(10, n1, n2)
259259
@check_bit_operation (.*) Array{S} (b1, b2)
260260
@check_bit_operation (./) Array{Float64} (b1, b2)
261261
@check_bit_operation (.^) Array{Float64} (b1, b2)
262-
@check_bit_operation div Array{Uint} (b1, b2)
263-
@check_bit_operation mod Array{Int} (b1, b2)
262+
@check_bit_operation div Array{S} (b1, b2)
263+
@check_bit_operation mod Array{S} (b1, b2)
264264

265265
while true
266266
global b1

0 commit comments

Comments
 (0)