Skip to content

Commit

Permalink
Merge pull request #18929 from JuliaLang/kc/arr_perf
Browse files Browse the repository at this point in the history
RFC: revert back to one index for some elementwise operations
  • Loading branch information
timholy authored Oct 25, 2016
2 parents 9a2f0f1 + 50dc3d7 commit d867128
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 18 deletions.
67 changes: 55 additions & 12 deletions base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ for f in (:-, :~, :conj, :sign)
@eval begin
function ($f)(A::AbstractArray)
F = similar(A)
for (iF, iA) in zip(eachindex(F), eachindex(A))
F[iF] = ($f)(A[iA])
RF, RA = eachindex(F), eachindex(A)
if RF == RA
for i in RA
F[i] = ($f)(A[i])
end
else
for (iF, iA) in zip(RF, RA)
F[iF] = ($f)(A[iA])
end
end
return F
end
Expand All @@ -35,8 +42,15 @@ imag(A::AbstractArray) = reshape([ imag(x) for x in A ], size(A))

function !(A::AbstractArray{Bool})
F = similar(A)
for (iF, iA) in zip(eachindex(F), eachindex(A))
F[iF] = !A[iA]
RF, RA = eachindex(F), eachindex(A)
if RF == RA
for i in RA
F[i] = !A[i]
end
else
for (iF, iA) in zip(RF, RA)
F[iF] = !A[iA]
end
end
return F
end
Expand All @@ -62,8 +76,15 @@ function _elementwise(op, ::Type{Any}, A::AbstractArray, B::AbstractArray)
end
function _elementwise{T}(op, ::Type{T}, A::AbstractArray, B::AbstractArray)
F = similar(A, T, promote_shape(A, B))
for (iF, iA, iB) in zip(eachindex(F), eachindex(A), eachindex(B))
@inbounds F[iF] = op(A[iA], B[iB])
RF, RA, RB = eachindex(F), eachindex(A), eachindex(B)
if RF == RA == RB
for i in RA
@inbounds F[i] = op(A[i], B[i])
end
else
for (iF, iA, iB) in zip(RF, RA, RB)
@inbounds F[iF] = op(A[iA], B[iB])
end
end
return F
end
Expand All @@ -75,8 +96,15 @@ for f in (:.+, :.-, :.*, :./, :.\, :.^, :.÷, :.%, :.<<, :.>>, :div, :mod, :rem,
S = promote_array_type($f, typeof(A), T, R)
S === Any && return [($f)(A, b) for b in B]
F = similar(B, S)
for (iF, iB) in zip(eachindex(F), eachindex(B))
@inbounds F[iF] = ($f)(A, B[iB])
RF, RB = eachindex(F), eachindex(B)
if RF == RB
for i in RB
@inbounds F[i] = ($f)(A, B[i])
end
else
for (iF, iB) in zip(RF, RB)
@inbounds F[iF] = ($f)(A, B[iB])
end
end
return F
end
Expand All @@ -85,8 +113,15 @@ for f in (:.+, :.-, :.*, :./, :.\, :.^, :.÷, :.%, :.<<, :.>>, :div, :mod, :rem,
S = promote_array_type($f, typeof(B), T, R)
S === Any && return [($f)(a, B) for a in A]
F = similar(A, S)
for (iF, iA) in zip(eachindex(F), eachindex(A))
@inbounds F[iF] = ($f)(A[iA], B)
RF, RA = eachindex(F), eachindex(A)
if RF == RA
for i in RA
@inbounds F[i] = ($f)(A[i], B)
end
else
for (iF, iA) in zip(RF, RA)
@inbounds F[iF] = ($f)(A[iA], B)
end
end
return F
end
Expand Down Expand Up @@ -420,9 +455,17 @@ function transposeblock!(f,B::AbstractMatrix,A::AbstractMatrix,m::Int,n::Int,off
end
return B
end

function ccopy!(B, A)
for (i,j) = zip(eachindex(B),eachindex(A))
B[i] = ctranspose(A[j])
RB, RA = eachindex(B), eachindex(A)
if RB == RA
for i = RB
B[i] = ctranspose(A[i])
end
else
for (i,j) = zip(RB, RA)
B[i] = ctranspose(A[j])
end
end
end

Expand Down
33 changes: 27 additions & 6 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -839,24 +839,45 @@ promote_array_type{S<:Union{Complex, Real}, T<:AbstractFloat}(F, ::Type{S}, ::Ty
function complex{S<:Real,T<:Real}(A::AbstractArray{S}, B::AbstractArray{T})
if size(A) != size(B); throw(DimensionMismatch()); end
F = similar(A, typeof(complex(zero(S),zero(T))))
for (iF, iA, iB) in zip(eachindex(F), eachindex(A), eachindex(B))
@inbounds F[iF] = complex(A[iA], B[iB])
RF, RA, RB = eachindex(F), eachindex(A), eachindex(B)
if RF == RA == RB
for i in RA
@inbounds F[i] = complex(A[i], B[i])
end
else
for (iF, iA, iB) in zip(RF, RA, RB)
@inbounds F[iF] = complex(A[iA], B[iB])
end
end
return F
end

function complex{T<:Real}(A::Real, B::AbstractArray{T})
F = similar(B, typeof(complex(A,zero(T))))
for (iF, iB) in zip(eachindex(F), eachindex(B))
@inbounds F[iF] = complex(A, B[iB])
RF, RB = eachindex(F), eachindex(B)
if RF == RB
for i in RB
@inbounds F[i] = complex(A, B[i])
end
else
for (iF, iB) in zip(RF, RB)
@inbounds F[iF] = complex(A, B[iB])
end
end
return F
end

function complex{T<:Real}(A::AbstractArray{T}, B::Real)
F = similar(A, typeof(complex(zero(T),B)))
for (iF, iA) in zip(eachindex(F), eachindex(A))
@inbounds F[iF] = complex(A[iA], B)
RF, RA = eachindex(F), eachindex(A)
if RF == RA
for i in RA
@inbounds F[i] = complex(A[i], B)
end
else
for (iF, iA) in zip(RF, RA)
@inbounds F[iF] = complex(A[iA], B)
end
end
return F
end

0 comments on commit d867128

Please sign in to comment.