Skip to content

Commit

Permalink
Merge pull request JuliaLang#12 from JuliaLang/master
Browse files Browse the repository at this point in the history
update to 4e1e0ab
  • Loading branch information
tkelman committed Mar 10, 2014
2 parents 132fd49 + 4e1e0ab commit 6ca7b0a
Show file tree
Hide file tree
Showing 29 changed files with 976 additions and 379 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ Library improvements
the same length. This generalizes and replaces `normfro` ([#6057]),
and `norm` is now type-stable ([#6056]).

* + and - now only works when the sizes of the arrays are the same, i.e. the
operations no longer do broadcasting. New `UniformScaling` type and identity
`I` constant (#5810).

* Sparse linear algebra

* Faster sparse `kron` ([#4958]).
Expand Down
15 changes: 5 additions & 10 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,14 @@ end
bool(x::AbstractArray{Bool}) = x
bool(x::AbstractArray) = copy!(similar(x,Bool), x)

for (f,t) in ((:float16, Float16),
convert{T,S,N}(::Type{AbstractArray{T,N}}, A::Array{S,N}) = convert(Array{T,N}, A)

for (f,T) in ((:float16, Float16),
(:float32, Float32),
(:float64, Float64),
(:complex64, Complex64),
(:complex128, Complex128))
@eval ($f)(x::AbstractArray{$t}) = x
@eval ($f)(x::AbstractArray) = copy!(similar(x,$t), x)
@eval ($f)(x::AbstractArray) = convert(AbstractArray{$T}, x)
end

float{T<:FloatingPoint}(x::AbstractArray{T}) = x
Expand Down Expand Up @@ -325,11 +326,9 @@ imag{T<:Real}(x::AbstractArray{T}) = zero(x)
*(A::Number, B::AbstractArray) = A .* B
*(A::AbstractArray, B::Number) = A .* B

/(A::Number, B::AbstractArray) = A ./ B
/(A::AbstractArray, B::Number) = A ./ B

\(A::Number, B::AbstractArray) = B ./ A
\(A::AbstractArray, B::Number) = B ./ A

./(x::Number,y::AbstractArray ) = throw(MethodError(./, (x,y)))
./(x::AbstractArray, y::Number) = throw(MethodError(./, (x,y)))
Expand Down Expand Up @@ -397,7 +396,7 @@ function circshift(a, shiftamts)
for i=1:n
s = size(a,i)
d = i<=length(shiftamts) ? shiftamts[i] : 0
I[i] = d==0 ? (1:s) : mod([-d:s-1-d], s)+1
I[i] = d==0 ? (1:s) : mod([-d:s-1-d], s).+1
end
a[I...]::typeof(a)
end
Expand Down Expand Up @@ -1183,10 +1182,6 @@ function mapslices(f::Function, A::AbstractArray, dims::AbstractVector)
ndimsA = ndims(A)
alldims = [1:ndimsA]

if dims == alldims
return f(A)
end

otherdims = setdiff(alldims, dims)

idx = cell(ndimsA)
Expand Down
12 changes: 9 additions & 3 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ convert{T,n}(::Type{Array{T,n}}, x::Array{T,n}) = x
convert{T,n,S}(::Type{Array{T}}, x::Array{S,n}) = convert(Array{T,n}, x)
convert{T,n,S}(::Type{Array{T,n}}, x::Array{S,n}) = copy!(similar(x,T), x)

convert{T,S,N}(::Type{AbstractArray{T,N}}, B::StridedArray{S,N}) = copy!(similar(B,T), B)

function collect{C}(T::Type, itr::C)
if method_exists(length,(C,))
a = Array(T,length(itr))
Expand Down Expand Up @@ -780,7 +782,7 @@ for f in (:+, :-, :div, :mod, :&, :|, :$)
end
end
end
for f in (:+, :-, :.*, :./, :.%, :div, :mod, :rem, :&, :|, :$)
for f in (:.+, :.-, :.*, :./, :.%, :div, :mod, :rem, :&, :|, :$)
@eval begin
function ($f){T}(A::Number, B::StridedArray{T})
F = similar(B, promote_array_type(typeof(A),T))
Expand All @@ -800,7 +802,7 @@ for f in (:+, :-, :.*, :./, :.%, :div, :mod, :rem, :&, :|, :$)
end

# functions that should give an Int result for Bool arrays
for f in (:+, :-)
for f in (:.+, :.-)
@eval begin
function ($f)(A::Bool, B::StridedArray{Bool})
F = Array(Int, size(B))
Expand All @@ -816,12 +818,16 @@ for f in (:+, :-)
end
return F
end
end
end
for f in (:+, :-)
@eval begin
function ($f)(A::StridedArray{Bool}, B::StridedArray{Bool})
F = Array(Int, promote_shape(size(A), size(B)))
for i=1:length(A)
@inbounds F[i] = ($f)(A[i], B[i])
end
return F
return F
end
end
end
Expand Down
128 changes: 5 additions & 123 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ macro _div64(l) :($(esc(l)) >>> 6) end
macro _mod64(l) :($(esc(l)) & 63) end
macro _msk_end(l) :(@_mskr @_mod64 $(esc(l))) end
num_bit_chunks(n::Int) = @_div64 (n+63)
const bitcache_chunks = 64 # this can be changed
const bitcache_size = 64 * bitcache_chunks # do not change this

## BitArray

Expand Down Expand Up @@ -324,6 +322,7 @@ function convert{T,N}(::Type{BitArray{N}}, A::AbstractArray{T,N})
end

convert{N}(::Type{BitArray{N}}, B::BitArray{N}) = B
convert{T,N}(::Type{AbstractArray{T,N}}, B::BitArray{N}) = convert(Array{Bool,N}, B)

reinterpret{N}(::Type{Bool}, B::BitArray, dims::NTuple{N,Int}) = reinterpret(B, dims)
reinterpret{N}(B::BitArray, dims::NTuple{N,Int}) = reshape(B, dims)
Expand Down Expand Up @@ -899,7 +898,7 @@ for f in (:+, :-)
return r
end
end
for f in (:+, :-),
for f in (:.+, :.-),
(arg1, arg2, T, fargs) in ((:(B::BitArray), :(x::Bool) , Int , :(b, x)),
(:(B::BitArray), :(x::Number) , :(promote_array_type(typeof(x), Bool)), :(b, x)),
(:(x::Bool) , :(B::BitArray), Int , :(x, b)),
Expand All @@ -917,11 +916,6 @@ for f in (:+, :-),
end
end

function (./)(A::BitArray, B::BitArray)
shp = promote_shape(size(A),size(B))
reshape([ A[i] ./ B[i] for i=1:length(A) ], shp)
end

for f in (:/, :\)
@eval begin
($f)(A::BitArray, B::BitArray) = ($f)(bitunpack(A), bitunpack(B))
Expand Down Expand Up @@ -1022,22 +1016,6 @@ for f in (:&, :|, :$)
end
end

function (.^)(A::BitArray, B::BitArray)
F = BitArray(promote_shape(size(A),size(B))...)
Fc = F.chunks
Ac = A.chunks
Bc = B.chunks
if !isempty(Ac) && !isempty(Bc)
for i = 1:length(Fc) - 1
Fc[i] = Ac[i] | ~Bc[i]
end
msk = @_msk_end length(F)
Fc[end] = msk & (Ac[end] | ~Bc[end])
end
return F
end
(.^)(A::Array{Bool}, B::BitArray) = (.^)(bitpack(A), B)
(.^)(B::BitArray, A::Array{Bool}) = (.^)(B, bitpack(A))
function (.^)(B::BitArray, x::Bool)
x ? copy(B) : trues(size(B))
end
Expand Down Expand Up @@ -1069,12 +1047,12 @@ function (.^){T<:Number}(B::BitArray, x::T)
zerr = nothing
uerr = nothing
try
z = false .^ x
z = false^x
catch err
zerr = err
end
try
u = true .^ x
u = true^x
catch err
uerr = err
end
Expand Down Expand Up @@ -1105,37 +1083,6 @@ function (.^){T<:Number}(B::BitArray, x::T)
end
end

function bitcache_pow{T}(A::BitArray, B::Array{T}, l::Int, ind::Int, C::Vector{Bool})
left = l - ind + 1
for j = 1:min(bitcache_size, left)
C[j] = bool(A[ind] .^ B[ind])
ind += 1
end
C[left+1:bitcache_size] = false
return ind
end
function (.^){T<:Integer}(A::BitArray, B::Array{T})
F = BitArray(promote_shape(size(A),size(B)))
Fc = F.chunks
l = length(F)
if l == 0
return F
end
C = Array(Bool, bitcache_size)
ind = 1
cind = 1
nFc = num_bit_chunks(l)
for i = 1:div(l + bitcache_size - 1, bitcache_size)
ind = bitcache_pow(A, B, l, ind, C)
dumpbitcache(Fc, cind, C)
cind += bitcache_chunks
end
return F
end

(.*)(A::BitArray, B::BitArray) = A & B
(.*)(A::Array{Bool}, B::BitArray) = A & B
(.*)(B::BitArray, A::Array{Bool}) = A & B
(.*)(x::Bool, B::BitArray) = x & B
(.*)(B::BitArray, x::Bool) = B & x
(.*)(x::Number, B::BitArray) = x .* bitunpack(B)
Expand All @@ -1145,72 +1092,7 @@ end

# TODO?

## element-wise comparison operators returning BitArray{Bool} ##

function dumpbitcache(Bc::Vector{Uint64}, bind::Int, C::Vector{Bool})
ind = 1
nc = min(@_div64(length(C)), length(Bc)-bind+1)
for i = 1:nc
u = uint64(1)
c = uint64(0)
for j = 1:64
if C[ind]
c |= u
end
ind += 1
u <<= 1
end
Bc[bind] = c
bind += 1
end
end

for (f, cachef, scalarf) in ((:.==, :bitcache_eq , :(==)),
(:.< , :bitcache_lt , :< ),
(:.!=, :bitcache_neq, :!= ),
(:.<=, :bitcache_le , :<= ))
for (sigA, sigB, expA, expB, shape) in ((:AbstractArray, :AbstractArray,
:(A[ind]), :(B[ind]),
:(promote_shape(size(A), size(B)))),
(:Any, :AbstractArray,
:A, :(B[ind]),
:(size(B))),
(:AbstractArray, :Any,
:(A[ind]), :B,
:(size(A))))
@eval begin
function ($cachef)(A::$sigA, B::$sigB, l::Int, ind::Int, C::Vector{Bool})
left = l - ind + 1
@inbounds begin
for j = 1:min(bitcache_size, left)
C[j] = ($scalarf)($expA, $expB)
ind += 1
end
C[left+1:bitcache_size] = false
end
return ind
end
function ($f)(A::$sigA, B::$sigB)
F = BitArray($shape)
Fc = F.chunks
l = length(F)
if l == 0
return F
end
C = Array(Bool, bitcache_size)
ind = 1
cind = 1
nFc = num_bit_chunks(l)
for i = 1:div(l + bitcache_size - 1, bitcache_size)
ind = ($cachef)(A, B, l, ind, C)
dumpbitcache(Fc, cind, C)
cind += bitcache_chunks
end
return F
end
end
end
end
## comparison operators ##

function (==)(A::BitArray, B::BitArray)
if size(A) != size(B)
Expand Down
Loading

0 comments on commit 6ca7b0a

Please sign in to comment.