diff --git a/NEWS.md b/NEWS.md index 01fe405f7cd52..ee8af0028af76 100644 --- a/NEWS.md +++ b/NEWS.md @@ -158,6 +158,8 @@ Library improvements * `expm1` and `log1p` now support complex arguments ([#3141]). + * Broadcasting `.//` is now included ([#7094]). + * `String` improvements * Triple-quoted regex strings, `r"""..."""` ([#4934]). diff --git a/base/broadcast.jl b/base/broadcast.jl index e51f679586c78..2ae6f511e23e1 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -4,7 +4,7 @@ using ..Cartesian import Base.promote_eltype import Base.@get! import Base.num_bit_chunks, Base.@_msk_end, Base.unsafe_bitgetindex -import Base.(.+), Base.(.-), Base.(.*), Base.(./), Base.(.\) +import Base.(.+), Base.(.-), Base.(.*), Base.(./), Base.(.\), Base.(.//) import Base.(.==), Base.(.<), Base.(.!=), Base.(.<=) export broadcast, broadcast!, broadcast_function, broadcast!_function, bitbroadcast export broadcast_getindex, broadcast_setindex! @@ -304,6 +304,16 @@ function .\(A::AbstractArray, B::AbstractArray) broadcast!(\, Array(type_div(eltype(A), eltype(B)), broadcast_shape(A, B)), A, B) end +typealias RatIntT{T<:Integer} Union(Type{Rational{T}},Type{T}) +typealias CRatIntT{T<:Integer} Union(Type{Complex{Rational{T}}},Type{Complex{T}},Type{Rational{T}},Type{T}) +type_rdiv{T<:Integer,S<:Integer}(::RatIntT{T}, ::RatIntT{S}) = + Rational{promote_type(T,S)} +type_rdiv{T<:Integer,S<:Integer}(::CRatIntT{T}, ::CRatIntT{S}) = + Complex{Rational{promote_type(T,S)}} +function .//(A::AbstractArray, B::AbstractArray) + broadcast!(//, Array(type_rdiv(eltype(A), eltype(B)), broadcast_shape(A, B)), A, B) +end + type_pow(T,S) = promote_type(T,S) type_pow{S<:Integer}(::Type{Bool},::Type{S}) = Bool type_pow{S}(T,::Type{Rational{S}}) = type_pow(T, type_div(S, S)) diff --git a/base/exports.jl b/base/exports.jl index 319b5958c2836..975bffff7e3dc 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -217,6 +217,7 @@ export .^, /, //, + .//, <, <:, <<, diff --git a/base/rational.jl b/base/rational.jl index 8dc8d07367026..5f57a34eee876 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -27,6 +27,10 @@ function //(x::Complex, y::Complex) complex(real(xy)//yy, imag(xy)//yy) end +//(X::AbstractArray, y::Number) = X .// y +.//(X::AbstractArray, y::Number) = reshape([ x // y for x in X ], size(X)) +.//(y::Number, X::AbstractArray) = reshape([ y // x for x in X ], size(X)) + function show(io::IO, x::Rational) if isinf(x) print(io, x.num > 0 ? "Inf" : "-Inf") diff --git a/test/numbers.jl b/test/numbers.jl index 4fa9bc9c91e75..1c89df0b9ef1f 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -1776,3 +1776,9 @@ end @test widen(BigInt) === BigInt @test widemul(typemax(Int64),typemax(Int64)) == 85070591730234615847396907784232501249 + +# .// +@test [1,2,3] // 4 == [1//4, 2//4, 3//4] +@test [1,2,3] .// [4,5,6] == [1//4, 2//5, 3//6] +@test [1+2im,3+4im] .// [5,6] == [(1+2im)//5,(3+4im)//6] +@test [1//3+2im,3+4im] .// [5,6] == [(1//3+2im)//5,(3+4im)//6]