Skip to content

Commit

Permalink
Merge pull request JuliaLang#14139 from eschnett/eschnett/dot-div
Browse files Browse the repository at this point in the history
Implement dot-div (.÷) operator, corresponding to ./ or .%
  • Loading branch information
jakebolewski committed Dec 8, 2015
2 parents f5f3b57 + dbd7765 commit 8039d1b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ end
for (f,F) in ((:.+, DotAddFun()),
(:.-, DotSubFun()),
(:.*, DotMulFun()),
(:, DotIDivFun()),
(:.%, DotRemFun()),
(:.<<, DotLSFun()),
(:.>>, DotRSFun()),
Expand Down
3 changes: 2 additions & 1 deletion base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Broadcast
using ..Cartesian
using Base: promote_op, promote_eltype, promote_eltype_op, @get!, _msk_end, unsafe_bitgetindex
using Base: AddFun, SubFun, MulFun, LDivFun, RDivFun, PowFun
import Base: .+, .-, .*, ./, .\, .//, .==, .<, .!=, .<=, .%, .<<, .>>, .^
import Base: .+, .-, .*, ./, .\, .//, .==, .<, .!=, .<=, .÷, .%, .<<, .>>, .^
export broadcast, broadcast!, broadcast_function, broadcast!_function, bitbroadcast
export broadcast_getindex, broadcast_setindex!

Expand Down Expand Up @@ -303,6 +303,7 @@ end

## elementwise operators ##

(A::AbstractArray, B::AbstractArray) = broadcast(÷, A, B)
.%(A::AbstractArray, B::AbstractArray) = broadcast(%, A, B)
.<<(A::AbstractArray, B::AbstractArray) = broadcast(<<, A, B)
.>>(A::AbstractArray, B::AbstractArray) = broadcast(>>, A, B)
Expand Down
3 changes: 2 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export
,
$,
%,
÷,
&,
*,
+,
Expand All @@ -224,6 +225,7 @@ export
.-,
.*,
./,
,
.%,
.<,
.<=,
Expand Down Expand Up @@ -256,7 +258,6 @@ export
|>,
~,
:,
÷,
=>,
A_ldiv_B!,
A_ldiv_Bc,
Expand Down
8 changes: 7 additions & 1 deletion base/functors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,17 @@ call(::LDivFun, x, y) = x \ y
immutable IDivFun <: Func{2} end
call(::IDivFun, x, y) = div(x, y)

immutable DotIDivFun <: Func{2} end
call(::DotIDivFun, x, y) = x y

immutable ModFun <: Func{2} end
call(::ModFun, x, y) = mod(x, y)

immutable RemFun <: Func{2} end
call(::RemFun, x, y) = rem(x, y)

immutable DotRemFun <: Func{2} end
call(::RemFun, x, y) = x .% y
call(::DotRemFun, x, y) = x .% y

immutable PowFun <: Func{2} end
call(::PowFun, x, y) = x ^ y
Expand Down Expand Up @@ -190,6 +193,9 @@ function specialized_binary(f::Function)
is(f, ^) ? PowFun() :
is(f, &) ? AndFun() :
is(f, |) ? OrFun() :
is(f, %) ? RemFun() :
is(f, rem) ? RemFun() :
is(f, ÷) ? IDivFun() :
is(f, div) ? IDivFun() :
UnspecializedFun{2}(f)
end
Expand Down
8 changes: 5 additions & 3 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ modCeil{T<:Real}(x::T, y::T) = convert(T,x-y*ceil(x/y))
const % = rem
.%(x::Real, y::Real) = x%y
const ÷ = div
(x::Real, y::Real) = x÷y

# mod returns in [0,y) or (y,0] (for negative y),
# whereas mod1 returns in (0,y] or [y,0)
Expand Down Expand Up @@ -468,6 +469,8 @@ export
$,
%,
.%,
÷,
,
&,
*,
+,
Expand Down Expand Up @@ -510,7 +513,6 @@ export
|>,
<|,
~,
÷,
,
×,
,
Expand All @@ -534,10 +536,10 @@ export
ctranspose,
call

import ..this_module: !, !=, $, %, .%, &, *, +, -, .!=, .+, .-, .*, ./, .<, .<=, .==, .>,
import ..this_module: !, !=, $, %, .%, ÷, , &, *, +, -, .!=, .+, .-, .*, ./, .<, .<=, .==, .>,
.>=, .\, .^, /, //, <, <:, <<, <=, ==, >, >=, >>, .>>, .<<, >>>,
<|, |>, \, ^, |, ~, !==, ===, >:, colon, hcat, vcat, hvcat, getindex, setindex!,
transpose, ctranspose, call,
, , , .≥, .≤, .≠, ÷, , ×, , , , , , , , , , ,
, , , .≥, .≤, .≠, , ×, , , , , , , , , , ,

end
9 changes: 9 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ b = a+a
@test isequal(1./[1,2,5], [1.0,0.5,0.2])
@test isequal([1,2,3]/5, [0.2,0.4,0.6])

@test isequal(2.%[1,2,3], [0,0,2])
@test isequal([1,2,3].%2, [1,0,1])
@test isequal(2.÷[1,2,3], [2,1,0])
@test isequal([1,2,3].÷2, [0,1,1])
@test isequal(-2.%[1,2,3], [0,0,-2])
@test isequal([-1,-2,-3].%2, [-1,0,-1])
@test isequal(-2.÷[1,2,3], [-2,-1,0])
@test isequal([-1,-2,-3].÷2, [0,-1,-1])

@test isequal(1.<<[1,2,5], [2,4,32])
@test isequal(128.>>[1,2,5], [64,32,4])
@test isequal(2.>>1, 1)
Expand Down

0 comments on commit 8039d1b

Please sign in to comment.