Skip to content

Commit 7559e4a

Browse files
committed
Merge branch 'jb/working8_10_11' of github.com:JuliaLang/julia
Conflicts: j/intfuncs.j
2 parents 2795cad + dda35c7 commit 7559e4a

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

j/abstractarray.j

+37-6
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,40 @@ end
149149
\(A::Number, B::AbstractArray) = B ./ A
150150
\(A::AbstractArray, B::Number) = B ./ A
151151

152+
# ^ is difficult, since negative exponents give a different type
153+
152154
.^(x::AbstractArray, y::AbstractArray) = reshape( [ x[i] ^ y[i] | i=1:numel(x) ], size(x) )
153155
.^(x::Number, y::AbstractArray) = reshape( [ x ^ y[i] | i=1:numel(y) ], size(y) )
154156
.^(x::AbstractArray, y::Number ) = reshape( [ x[i] ^ y | i=1:numel(x) ], size(x) )
155157

158+
function .^{S<:Int,T<:Int}(A::AbstractArray{S}, B::AbstractArray{T})
159+
F = similar(A, Float64)
160+
for i=1:numel(A)
161+
F[i] = A[i]^B[i]
162+
end
163+
return F
164+
end
165+
166+
function .^{T<:Int}(A::Int, B::AbstractArray{T})
167+
F = similar(B, Float64)
168+
for i=1:numel(B)
169+
F[i] = A^B[i]
170+
end
171+
return F
172+
end
173+
174+
function power_array_int_body(F, A, B)
175+
for i=1:numel(A)
176+
F[i] = A[i]^B
177+
end
178+
return F
179+
end
180+
181+
function .^{T<:Int}(A::AbstractArray{T}, B::Int)
182+
F = similar(A, B < 0 ? Float64 : promote_type(T,typeof(B)))
183+
power_array_int_body(F, A, B)
184+
end
185+
156186
macro binary_arithmetic_op(f)
157187
quote
158188

@@ -810,12 +840,13 @@ function map(f, A::AbstractArray)
810840
return A
811841
end
812842
first = f(A[1])
813-
dest = Array(typeof(first), size(A))
814-
dest[1] = first
815-
for i=2:numel(A)
816-
dest[i] = f(A[i])
817-
end
818-
return dest
843+
dest = similar(A, typeof(first))
844+
map_to(dest, f, A)
845+
# dest[1] = first
846+
# for i=2:numel(A)
847+
# dest[i] = f(A[i])
848+
# end
849+
# return dest
819850
end
820851

821852
#obsolete code, mainly here for reference purposes, use gen_cartesian_map

j/darray.j

+21
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,27 @@ end
490490

491491
## elementwise operators ##
492492

493+
function .^{T}(A::Int, B::DArray{T})
494+
S = promote_type(typeof(A),T)
495+
darray((T,lsz,da)->.^(A, localize(B)),
496+
S, size(B), B.distdim, B.pmap)
497+
end
498+
function .^{T}(A::DArray{T}, B::Int)
499+
S = promote_type(T,typeof(B))
500+
darray((T,lsz,da)->.^(localize(A), B),
501+
S, size(A), A.distdim, A.pmap)
502+
end
503+
504+
function .^{T<:Int}(A::Int, B::DArray{T})
505+
darray((T,lsz,da)->.^(A, localize(B)),
506+
Float64, size(B), B.distdim, B.pmap)
507+
end
508+
function .^{T<:Int}(A::DArray{T}, B::Int)
509+
S = B < 0 ? Float64 : promote_type(T,typeof(B))
510+
darray((T,lsz,da)->.^(localize(A), B),
511+
S, size(A), A.distdim, A.pmap)
512+
end
513+
493514
macro binary_darray_op(f)
494515
quote
495516
function ($f){T}(A::Number, B::DArray{T})

j/intfuncs.j

+1-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ function power_by_squaring(x, p::Int)
101101
end
102102

103103
^(x, p::Int) = power_by_squaring(x,p)
104-
^(x::Int, p::Int) = power_by_squaring(float64(x),p)
105-
^{T<:Int}(x::T, p::T) = power_by_squaring(float64(x),p)
104+
^{T<:Int}(x::T, p::T) = power_by_squaring(x,p)
106105

107106
# x^p mod m
108107
function powermod(x::Int, p::Int, m::Int)

0 commit comments

Comments
 (0)