diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 5a839d9826185..80ce357c2cc17 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -35,7 +35,7 @@ julia> indices(A,2) Base.OneTo(6) ``` """ -function indices{T,N}(A::AbstractArray{T,N}, d) +function indices(A::AbstractArray{T,N}, d) where {T,N} @_inline_meta d <= N ? indices(A)[d] : OneTo(1) end @@ -105,9 +105,9 @@ julia> ndims(A) 3 ``` """ -ndims{T,N}(::AbstractArray{T,N}) = N -ndims{T,N}(::Type{AbstractArray{T,N}}) = N -ndims{T<:AbstractArray}(::Type{T}) = ndims(supertype(T)) +ndims(::AbstractArray{T,N}) where {T,N} = N +ndims(::Type{AbstractArray{T,N}}) where {T,N} = N +ndims(::Type{T}) where {T<:AbstractArray} = ndims(supertype(T)) """ length(A::AbstractArray) -> Integer @@ -509,14 +509,14 @@ julia> similar(falses(10), Float64, 2, 4) ``` """ -similar{T}(a::AbstractArray{T}) = similar(a, T) -similar{T}(a::AbstractArray, ::Type{T}) = similar(a, T, to_shape(indices(a))) -similar{T}(a::AbstractArray{T}, dims::Tuple) = similar(a, T, to_shape(dims)) -similar{T}(a::AbstractArray{T}, dims::DimOrInd...) = similar(a, T, to_shape(dims)) -similar{T}(a::AbstractArray, ::Type{T}, dims::DimOrInd...) = similar(a, T, to_shape(dims)) -similar{T}(a::AbstractArray, ::Type{T}, dims::NeedsShaping) = similar(a, T, to_shape(dims)) +similar(a::AbstractArray{T}) where {T} = similar(a, T) +similar(a::AbstractArray, ::Type{T}) where {T} = similar(a, T, to_shape(indices(a))) +similar(a::AbstractArray{T}, dims::Tuple) where {T} = similar(a, T, to_shape(dims)) +similar(a::AbstractArray{T}, dims::DimOrInd...) where {T} = similar(a, T, to_shape(dims)) +similar(a::AbstractArray, ::Type{T}, dims::DimOrInd...) where {T} = similar(a, T, to_shape(dims)) +similar(a::AbstractArray, ::Type{T}, dims::NeedsShaping) where {T} = similar(a, T, to_shape(dims)) # similar creates an Array by default -similar{T,N}(a::AbstractArray, ::Type{T}, dims::Dims{N}) = Array{T,N}(dims) +similar(a::AbstractArray, ::Type{T}, dims::Dims{N}) where {T,N} = Array{T,N}(dims) to_shape(::Tuple{}) = () to_shape(dims::Dims) = dims @@ -740,7 +740,7 @@ function copymutable(a::AbstractArray) end copymutable(itr) = collect(itr) -zero{T}(x::AbstractArray{T}) = fill!(similar(x), zero(T)) +zero(x::AbstractArray{T}) where {T} = fill!(similar(x), zero(T)) ## iteration support for arrays by iterating over `eachindex` in the array ## # Allows fast iteration by default for both IndexLinear and IndexCartesian arrays @@ -841,10 +841,10 @@ full(x::AbstractArray) = x ## range conversions ## -map{T<:Real}(::Type{T}, r::StepRange) = T(r.start):T(r.step):T(last(r)) -map{T<:Real}(::Type{T}, r::UnitRange) = T(r.start):T(last(r)) -map{T<:AbstractFloat}(::Type{T}, r::StepRangeLen) = convert(StepRangeLen{T}, r) -function map{T<:AbstractFloat}(::Type{T}, r::LinSpace) +map(::Type{T}, r::StepRange) where {T<:Real} = T(r.start):T(r.step):T(last(r)) +map(::Type{T}, r::UnitRange) where {T<:Real} = T(r.start):T(last(r)) +map(::Type{T}, r::StepRangeLen) where {T<:AbstractFloat} = convert(StepRangeLen{T}, r) +function map(::Type{T}, r::LinSpace) where T<:AbstractFloat LinSpace(T(r.start), T(r.stop), length(r)) end @@ -901,7 +901,7 @@ function _getindex(::IndexLinear, A::AbstractArray, I::Int...) end _to_linear_index(A::AbstractArray, i::Int) = i _to_linear_index(A::AbstractVector, i::Int, I::Int...) = i # TODO: DEPRECATE FOR #14770 -_to_linear_index{T,N}(A::AbstractArray{T,N}, I::Vararg{Int,N}) = (@_inline_meta; sub2ind(A, I...)) +_to_linear_index(A::AbstractArray{T,N}, I::Vararg{Int,N}) where {T,N} = (@_inline_meta; sub2ind(A, I...)) _to_linear_index(A::AbstractArray) = 1 # TODO: DEPRECATE FOR #14770 _to_linear_index(A::AbstractArray, I::Int...) = (@_inline_meta; sub2ind(A, I...)) # TODO: DEPRECATE FOR #14770 @@ -916,16 +916,16 @@ function _getindex(::IndexCartesian, A::AbstractArray, I::Int...) @inbounds r = getindex(A, _to_subscript_indices(A, I...)...) r end -function _getindex{T,N}(::IndexCartesian, A::AbstractArray{T,N}, I::Vararg{Int, N}) +function _getindex(::IndexCartesian, A::AbstractArray{T,N}, I::Vararg{Int, N}) where {T,N} @_propagate_inbounds_meta getindex(A, I...) end _to_subscript_indices(A::AbstractArray, i::Int) = (@_inline_meta; _unsafe_ind2sub(A, i)) -_to_subscript_indices{T,N}(A::AbstractArray{T,N}) = (@_inline_meta; fill_to_length((), 1, Val{N})) # TODO: DEPRECATE FOR #14770 -_to_subscript_indices{T}(A::AbstractArray{T,0}) = () # TODO: REMOVE FOR #14770 -_to_subscript_indices{T}(A::AbstractArray{T,0}, i::Int) = () # TODO: REMOVE FOR #14770 -_to_subscript_indices{T}(A::AbstractArray{T,0}, I::Int...) = () # TODO: DEPRECATE FOR #14770 -function _to_subscript_indices{T,N}(A::AbstractArray{T,N}, I::Int...) # TODO: DEPRECATE FOR #14770 +_to_subscript_indices(A::AbstractArray{T,N}) where {T,N} = (@_inline_meta; fill_to_length((), 1, Val{N})) # TODO: DEPRECATE FOR #14770 +_to_subscript_indices(A::AbstractArray{T,0}) where {T} = () # TODO: REMOVE FOR #14770 +_to_subscript_indices(A::AbstractArray{T,0}, i::Int) where {T} = () # TODO: REMOVE FOR #14770 +_to_subscript_indices(A::AbstractArray{T,0}, I::Int...) where {T} = () # TODO: DEPRECATE FOR #14770 +function _to_subscript_indices(A::AbstractArray{T,N}, I::Int...) where {T,N} # TODO: DEPRECATE FOR #14770 @_inline_meta J, Jrem = IteratorsMD.split(I, Val{N}) _to_subscript_indices(A, J, Jrem) @@ -946,7 +946,7 @@ function __to_subscript_indices(A::AbstractArray, (J..., map(first, tail(_remaining_size(J, indices(A))))...) end _to_subscript_indices(A, J::Tuple, Jrem::Tuple) = J # already bounds-checked, safe to drop -_to_subscript_indices{T,N}(A::AbstractArray{T,N}, I::Vararg{Int,N}) = I +_to_subscript_indices(A::AbstractArray{T,N}, I::Vararg{Int,N}) where {T,N} = I _remaining_size(::Tuple{Any}, t::Tuple) = t _remaining_size(h::Tuple, t::Tuple) = (@_inline_meta; _remaining_size(tail(h), tail(t))) _unsafe_ind2sub(::Tuple{}, i) = () # ind2sub may throw(BoundsError()) in this case @@ -979,7 +979,7 @@ function _setindex!(::IndexLinear, A::AbstractArray, v, I::Int...) end # IndexCartesian Scalar indexing -function _setindex!{T,N}(::IndexCartesian, A::AbstractArray{T,N}, v, I::Vararg{Int, N}) +function _setindex!(::IndexCartesian, A::AbstractArray{T,N}, v, I::Vararg{Int, N}) where {T,N} @_propagate_inbounds_meta setindex!(A, v, I...) end @@ -1003,7 +1003,7 @@ get(A::AbstractArray, i::Integer, default) = checkbounds(Bool, A, i) ? A[i] : de get(A::AbstractArray, I::Tuple{}, default) = similar(A, typeof(default), 0) get(A::AbstractArray, I::Dims, default) = checkbounds(Bool, A, I...) ? A[I...] : default -function get!{T}(X::AbstractVector{T}, A::AbstractVector, I::Union{Range, AbstractVector{Int}}, default::T) +function get!(X::AbstractVector{T}, A::AbstractVector, I::Union{Range, AbstractVector{Int}}, default::T) where T # 1d is not linear indexing ind = findin(I, indices1(A)) X[ind] = A[I[ind]] @@ -1012,7 +1012,7 @@ function get!{T}(X::AbstractVector{T}, A::AbstractVector, I::Union{Range, Abstra X[last(ind)+1:last(Xind)] = default X end -function get!{T}(X::AbstractArray{T}, A::AbstractArray, I::Union{Range, AbstractVector{Int}}, default::T) +function get!(X::AbstractArray{T}, A::AbstractArray, I::Union{Range, AbstractVector{Int}}, default::T) where T # Linear indexing ind = findin(I, 1:length(A)) X[ind] = A[I[ind]] @@ -1024,7 +1024,7 @@ end get(A::AbstractArray, I::Range, default) = get!(similar(A, typeof(default), index_shape(I)), A, I, default) # TODO: DEPRECATE FOR #14770 (just the partial linear indexing part) -function get!{T}(X::AbstractArray{T}, A::AbstractArray, I::RangeVecIntList, default::T) +function get!(X::AbstractArray{T}, A::AbstractArray, I::RangeVecIntList, default::T) where T fill!(X, default) dst, src = indcopy(size(A), I) X[dst...] = A[src...] @@ -1051,24 +1051,24 @@ promote_eltype(v1, vs...) = promote_type(eltype(v1), promote_eltype(vs...)) #TODO: ERROR CHECK cat(catdim::Integer) = Array{Any,1}(0) -typed_vcat{T}(::Type{T}) = Array{T,1}(0) -typed_hcat{T}(::Type{T}) = Array{T,1}(0) +typed_vcat(::Type{T}) where {T} = Array{T,1}(0) +typed_hcat(::Type{T}) where {T} = Array{T,1}(0) ## cat: special cases -vcat{T}(X::T...) = T[ X[i] for i=1:length(X) ] -vcat{T<:Number}(X::T...) = T[ X[i] for i=1:length(X) ] -hcat{T}(X::T...) = T[ X[j] for i=1:1, j=1:length(X) ] -hcat{T<:Number}(X::T...) = T[ X[j] for i=1:1, j=1:length(X) ] +vcat(X::T...) where {T} = T[ X[i] for i=1:length(X) ] +vcat(X::T...) where {T<:Number} = T[ X[i] for i=1:length(X) ] +hcat(X::T...) where {T} = T[ X[j] for i=1:1, j=1:length(X) ] +hcat(X::T...) where {T<:Number} = T[ X[j] for i=1:1, j=1:length(X) ] vcat(X::Number...) = hvcat_fill(Array{promote_typeof(X...)}(length(X)), X) hcat(X::Number...) = hvcat_fill(Array{promote_typeof(X...)}(1,length(X)), X) -typed_vcat{T}(::Type{T}, X::Number...) = hvcat_fill(Array{T,1}(length(X)), X) -typed_hcat{T}(::Type{T}, X::Number...) = hvcat_fill(Array{T,2}(1,length(X)), X) +typed_vcat(::Type{T}, X::Number...) where {T} = hvcat_fill(Array{T,1}(length(X)), X) +typed_hcat(::Type{T}, X::Number...) where {T} = hvcat_fill(Array{T,2}(1,length(X)), X) vcat(V::AbstractVector...) = typed_vcat(promote_eltype(V...), V...) -vcat{T}(V::AbstractVector{T}...) = typed_vcat(T, V...) +vcat(V::AbstractVector{T}...) where {T} = typed_vcat(T, V...) -function typed_vcat{T}(::Type{T}, V::AbstractVector...) +function typed_vcat(::Type{T}, V::AbstractVector...) where T n::Int = 0 for Vk in V n += length(Vk) @@ -1085,9 +1085,9 @@ function typed_vcat{T}(::Type{T}, V::AbstractVector...) end hcat(A::AbstractVecOrMat...) = typed_hcat(promote_eltype(A...), A...) -hcat{T}(A::AbstractVecOrMat{T}...) = typed_hcat(T, A...) +hcat(A::AbstractVecOrMat{T}...) where {T} = typed_hcat(T, A...) -function typed_hcat{T}(::Type{T}, A::AbstractVecOrMat...) +function typed_hcat(::Type{T}, A::AbstractVecOrMat...) where T nargs = length(A) nrows = size(A[1], 1) ncols = 0 @@ -1122,9 +1122,9 @@ function typed_hcat{T}(::Type{T}, A::AbstractVecOrMat...) end vcat(A::AbstractMatrix...) = typed_vcat(promote_eltype(A...), A...) -vcat{T}(A::AbstractMatrix{T}...) = typed_vcat(T, A...) +vcat(A::AbstractMatrix{T}...) where {T} = typed_vcat(T, A...) -function typed_vcat{T}(::Type{T}, A::AbstractMatrix...) +function typed_vcat(::Type{T}, A::AbstractMatrix...) where T nargs = length(A) nrows = sum(a->size(a, 1), A)::Int ncols = size(A[1], 2) @@ -1200,7 +1200,7 @@ function cat_t(dims, T::Type, X...) return _cat(A, shape, catdims, X...) end -function _cat{N}(A, shape::NTuple{N}, catdims, X...) +function _cat(A, shape::NTuple{N}, catdims, X...) where N offsets = zeros(Int, N) inds = Vector{UnitRange{Int}}(N) concat = copy!(zeros(Bool, N), catdims) @@ -1295,7 +1295,7 @@ hcat(X...) = cat(Val{2}, X...) typed_vcat(T::Type, X...) = cat_t(Val{1}, T, X...) typed_hcat(T::Type, X...) = cat_t(Val{2}, T, X...) -cat{T}(catdims, A::AbstractArray{T}...) = cat_t(catdims, T, A...) +cat(catdims, A::AbstractArray{T}...) where {T} = cat_t(catdims, T, A...) # The specializations for 1 and 2 inputs are important # especially when running with --inline=no, see #11158 @@ -1362,9 +1362,9 @@ If the first argument is a single integer `n`, then all block rows are assumed t block columns. """ hvcat(rows::Tuple{Vararg{Int}}, xs::AbstractVecOrMat...) = typed_hvcat(promote_eltype(xs...), rows, xs...) -hvcat{T}(rows::Tuple{Vararg{Int}}, xs::AbstractVecOrMat{T}...) = typed_hvcat(T, rows, xs...) +hvcat(rows::Tuple{Vararg{Int}}, xs::AbstractVecOrMat{T}...) where {T} = typed_hvcat(T, rows, xs...) -function typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}, as::AbstractVecOrMat...) +function typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, as::AbstractVecOrMat...) where T nbr = length(rows) # number of block rows nc = 0 @@ -1408,9 +1408,9 @@ function typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}, as::AbstractVecOrMa end hvcat(rows::Tuple{Vararg{Int}}) = [] -typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}) = Array{T,1}(0) +typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}) where {T} = Array{T,1}(0) -function hvcat{T<:Number}(rows::Tuple{Vararg{Int}}, xs::T...) +function hvcat(rows::Tuple{Vararg{Int}}, xs::T...) where T<:Number nr = length(rows) nc = rows[1] @@ -1445,7 +1445,7 @@ end hvcat(rows::Tuple{Vararg{Int}}, xs::Number...) = typed_hvcat(promote_typeof(xs...), rows, xs...) -function typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}, xs::Number...) +function typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, xs::Number...) where T nr = length(rows) nc = rows[1] for i = 2:nr @@ -1472,7 +1472,7 @@ function hvcat(rows::Tuple{Vararg{Int}}, as...) vcat(rs...) end -function typed_hvcat{T}(::Type{T}, rows::Tuple{Vararg{Int}}, as...) +function typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, as...) where T nbr = length(rows) # number of block rows rs = Array{Any,1}(nbr) a = 1 @@ -1847,7 +1847,7 @@ end ## 1 argument -function map!{F}(f::F, dest::AbstractArray, A::AbstractArray) +function map!(f::F, dest::AbstractArray, A::AbstractArray) where F for (i,j) in zip(eachindex(dest),eachindex(A)) dest[i] = f(A[j]) end @@ -1881,7 +1881,7 @@ julia> map(+, [1, 2, 3], [10, 20, 30]) map(f, A) = collect(Generator(f,A)) ## 2 argument -function map!{F}(f::F, dest::AbstractArray, A::AbstractArray, B::AbstractArray) +function map!(f::F, dest::AbstractArray, A::AbstractArray, B::AbstractArray) where F for (i, j, k) in zip(eachindex(dest), eachindex(A), eachindex(B)) dest[i] = f(A[j], B[k]) end @@ -1918,7 +1918,7 @@ julia> x 6.0 ``` """ -map!{F}(f::F, dest::AbstractArray, As::AbstractArray...) = map_n!(f, dest, As) +map!(f::F, dest::AbstractArray, As::AbstractArray...) where {F} = map_n!(f, dest, As) map(f) = f() map(f, iters...) = collect(Generator(f, iters...)) diff --git a/base/abstractarraymath.jl b/base/abstractarraymath.jl index ec0b6440de291..07b55a77bd767 100644 --- a/base/abstractarraymath.jl +++ b/base/abstractarraymath.jl @@ -210,7 +210,7 @@ function circshift(a::AbstractArray, shiftamt) end # Uses K-B-N summation -function cumsum_kbn{T<:AbstractFloat}(v::AbstractVector{T}) +function cumsum_kbn(v::AbstractVector{T}) where T<:AbstractFloat r = similar(v) if isempty(v); return r; end @@ -241,7 +241,7 @@ end Cumulative sum along a dimension, using the Kahan-Babuska-Neumaier compensated summation algorithm for additional accuracy. The dimension defaults to 1. """ -function cumsum_kbn{T<:AbstractFloat}(A::AbstractArray{T}, axis::Integer=1) +function cumsum_kbn(A::AbstractArray{T}, axis::Integer=1) where T<:AbstractFloat dimsA = size(A) ndimsA = ndims(A) axis_size = dimsA[axis] diff --git a/base/array.jl b/base/array.jl index 450f35db02128..a6961462f0ef2 100644 --- a/base/array.jl +++ b/base/array.jl @@ -113,7 +113,7 @@ function unsafe_copy!(dest::Array{T}, doffs, src::Array{T}, soffs, n) where T return dest end -function copy!{T}(dest::Array{T}, doffs::Integer, src::Array{T}, soffs::Integer, n::Integer) +function copy!(dest::Array{T}, doffs::Integer, src::Array{T}, soffs::Integer, n::Integer) where T n == 0 && return dest n > 0 || throw(ArgumentError(string("tried to copy n=", n, " elements, but n should be nonnegative"))) if soffs < 1 || doffs < 1 || soffs+n-1 > length(src) || doffs+n-1 > length(dest) @@ -126,20 +126,20 @@ copy!(dest::Array{T}, src::Array{T}) where {T} = copy!(dest, 1, src, 1, length(s copy(a::T) where {T<:Array} = ccall(:jl_array_copy, Ref{T}, (Any,), a) -function reinterpret{T,S}(::Type{T}, a::Array{S,1}) +function reinterpret(::Type{T}, a::Array{S,1}) where T where S nel = Int(div(length(a)*sizeof(S),sizeof(T))) # TODO: maybe check that remainder is zero? return reinterpret(T, a, (nel,)) end -function reinterpret{T,S}(::Type{T}, a::Array{S}) +function reinterpret(::Type{T}, a::Array{S}) where T where S if sizeof(S) != sizeof(T) throw(ArgumentError("result shape not specified")) end reinterpret(T, a, size(a)) end -function reinterpret{T,S,N}(::Type{T}, a::Array{S}, dims::NTuple{N,Int}) +function reinterpret(::Type{T}, a::Array{S}, dims::NTuple{N,Int}) where T where S where N if !isbits(T) throw(ArgumentError("cannot reinterpret Array{$(S)} to ::Type{Array{$(T)}}, type $(T) is not a bits type")) end @@ -154,7 +154,7 @@ function reinterpret{T,S,N}(::Type{T}, a::Array{S}, dims::NTuple{N,Int}) end # reshaping to same # of dimensions -function reshape{T,N}(a::Array{T,N}, dims::NTuple{N,Int}) +function reshape(a::Array{T,N}, dims::NTuple{N,Int}) where T where N if prod(dims) != length(a) throw(DimensionMismatch("new dimensions $(dims) must be consistent with array size $(length(a))")) end @@ -165,7 +165,7 @@ function reshape{T,N}(a::Array{T,N}, dims::NTuple{N,Int}) end # reshaping to different # of dimensions -function reshape{T,N}(a::Array{T}, dims::NTuple{N,Int}) +function reshape(a::Array{T}, dims::NTuple{N,Int}) where T where N if prod(dims) != length(a) throw(DimensionMismatch("new dimensions $(dims) must be consistent with array size $(length(a))")) end @@ -174,16 +174,16 @@ end ## Constructors ## -similar{T}(a::Array{T,1}) = Array{T,1}(size(a,1)) -similar{T}(a::Array{T,2}) = Array{T,2}(size(a,1), size(a,2)) -similar{T}(a::Array{T,1}, S::Type) = Array{S,1}(size(a,1)) -similar{T}(a::Array{T,2}, S::Type) = Array{S,2}(size(a,1), size(a,2)) -similar{T}(a::Array{T}, m::Int) = Array{T,1}(m) -similar{N}(a::Array, T::Type, dims::Dims{N}) = Array{T,N}(dims) -similar{T,N}(a::Array{T}, dims::Dims{N}) = Array{T,N}(dims) +similar(a::Array{T,1}) where {T} = Array{T,1}(size(a,1)) +similar(a::Array{T,2}) where {T} = Array{T,2}(size(a,1), size(a,2)) +similar(a::Array{T,1}, S::Type) where {T} = Array{S,1}(size(a,1)) +similar(a::Array{T,2}, S::Type) where {T} = Array{S,2}(size(a,1), size(a,2)) +similar(a::Array{T}, m::Int) where {T} = Array{T,1}(m) +similar(a::Array, T::Type, dims::Dims{N}) where {N} = Array{T,N}(dims) +similar(a::Array{T}, dims::Dims{N}) where {T,N} = Array{T,N}(dims) # T[x...] constructs Array{T,1} -function getindex{T}(::Type{T}, vals...) +function getindex(::Type{T}, vals...) where T a = Array{T,1}(length(vals)) @inbounds for i = 1:length(vals) a[i] = vals[i] @@ -191,10 +191,10 @@ function getindex{T}(::Type{T}, vals...) return a end -getindex{T}(::Type{T}) = (@_inline_meta; Array{T,1}(0)) -getindex{T}(::Type{T}, x) = (@_inline_meta; a = Array{T,1}(1); @inbounds a[1] = x; a) -getindex{T}(::Type{T}, x, y) = (@_inline_meta; a = Array{T,1}(2); @inbounds (a[1] = x; a[2] = y); a) -getindex{T}(::Type{T}, x, y, z) = (@_inline_meta; a = Array{T,1}(3); @inbounds (a[1] = x; a[2] = y; a[3] = z); a) +getindex(::Type{T}) where {T} = (@_inline_meta; Array{T,1}(0)) +getindex(::Type{T}, x) where {T} = (@_inline_meta; a = Array{T,1}(1); @inbounds a[1] = x; a) +getindex(::Type{T}, x, y) where {T} = (@_inline_meta; a = Array{T,1}(2); @inbounds (a[1] = x; a[2] = y); a) +getindex(::Type{T}, x, y, z) where {T} = (@_inline_meta; a = Array{T,1}(3); @inbounds (a[1] = x; a[2] = y; a[3] = z); a) function getindex(::Type{Any}, vals::ANY...) a = Array{Any,1}(length(vals)) @@ -210,7 +210,7 @@ function fill!(a::Union{Array{UInt8}, Array{Int8}}, x::Integer) return a end -function fill!{T<:Union{Integer,AbstractFloat}}(a::Array{T}, x) +function fill!(a::Array{T}, x) where T<:Union{Integer,AbstractFloat} xT = convert(T, x) for i in eachindex(a) @inbounds a[i] = xT @@ -261,7 +261,7 @@ end `m`-by-`n` identity matrix. The default element type is `Float64`. """ -function eye{T}(::Type{T}, m::Integer, n::Integer) +function eye(::Type{T}, m::Integer, n::Integer) where T a = zeros(T,m,n) for i = 1:min(m,n) a[i,i] = oneunit(T) @@ -275,7 +275,7 @@ end `m`-by-`n` identity matrix. """ eye(m::Integer, n::Integer) = eye(Float64, m, n) -eye{T}(::Type{T}, n::Integer) = eye(T, n, n) +eye(::Type{T}, n::Integer) where {T} = eye(T, n, n) """ eye([T::Type=Float64,] n::Integer) @@ -305,16 +305,16 @@ julia> eye(A) Note the difference from [`ones`](@ref). """ -eye{T}(x::AbstractMatrix{T}) = eye(typeof(one(T)), size(x, 1), size(x, 2)) +eye(x::AbstractMatrix{T}) where {T} = eye(typeof(one(T)), size(x, 1), size(x, 2)) -function _one{T}(unit::T, x::AbstractMatrix) +function _one(unit::T, x::AbstractMatrix) where T m,n = size(x) m==n || throw(DimensionMismatch("multiplicative identity defined only for square matrices")) eye(T, m) end -one{T}(x::AbstractMatrix{T}) = _one(one(T), x) -oneunit{T}(x::AbstractMatrix{T}) = _one(oneunit(T), x) +one(x::AbstractMatrix{T}) where {T} = _one(one(T), x) +oneunit(x::AbstractMatrix{T}) where {T} = _one(oneunit(T), x) ## Conversions ## @@ -327,7 +327,7 @@ convert(::Type{Array{T,n}}, x::Array{T,n}) where {T,n} = x convert(::Type{Array{T}}, x::AbstractArray{S,n}) where {T,n,S} = convert(Array{T,n}, x) convert(::Type{Array{T,n}}, x::AbstractArray{S,n}) where {T,n,S} = copy!(Array{T,n}(size(x)), x) -promote_rule{T,n,S}(::Type{Array{T,n}}, ::Type{Array{S,n}}) = Array{promote_type(T,S),n} +promote_rule(::Type{Array{T,n}}, ::Type{Array{S,n}}) where {T,n,S} = Array{promote_type(T,S),n} ## copying iterators to containers @@ -345,11 +345,11 @@ julia> collect(Float64, 1:2:5) 5.0 ``` """ -collect{T}(::Type{T}, itr) = _collect(T, itr, iteratorsize(itr)) +collect(::Type{T}, itr) where {T} = _collect(T, itr, iteratorsize(itr)) -_collect{T}(::Type{T}, itr, isz::HasLength) = copy!(Array{T,1}(Int(length(itr)::Integer)), itr) -_collect{T}(::Type{T}, itr, isz::HasShape) = copy!(similar(Array{T}, indices(itr)), itr) -function _collect{T}(::Type{T}, itr, isz::SizeUnknown) +_collect(::Type{T}, itr, isz::HasLength) where {T} = copy!(Array{T,1}(Int(length(itr)::Integer)), itr) +_collect(::Type{T}, itr, isz::HasShape) where {T} = copy!(similar(Array{T}, indices(itr)), itr) +function _collect(::Type{T}, itr, isz::SizeUnknown) where T a = Array{T,1}(0) for x in itr push!(a,x) @@ -413,8 +413,8 @@ else _default_eltype(itr::ANY) = Any end -_array_for{T}(::Type{T}, itr, ::HasLength) = Array{T,1}(Int(length(itr)::Integer)) -_array_for{T}(::Type{T}, itr, ::HasShape) = similar(Array{T}, indices(itr)) +_array_for(::Type{T}, itr, ::HasLength) where {T} = Array{T,1}(Int(length(itr)::Integer)) +_array_for(::Type{T}, itr, ::HasShape) where {T} = similar(Array{T}, indices(itr)) function collect(itr::Generator) isz = iteratorsize(itr.iter) @@ -454,7 +454,7 @@ function collect_to_with_first!(dest, v1, itr, st) return grow_to!(dest, itr, st) end -function collect_to!{T}(dest::AbstractArray{T}, itr, offs, st) +function collect_to!(dest::AbstractArray{T}, itr, offs, st) where T # collect to dest array, checking the type of each result. if a result does not # match, widen the result type and re-dispatch. i = offs @@ -529,13 +529,13 @@ function getindex(A::Array, c::Colon) end # This is redundant with the abstract fallbacks, but needed for bootstrap -function getindex{S}(A::Array{S}, I::Range{Int}) +function getindex(A::Array{S}, I::Range{Int}) where S return S[ A[i] for i in I ] end ## Indexing: setindex! ## -setindex!{T}(A::Array{T}, x, i1::Int) = arrayset(A, convert(T,x)::T, i1) -setindex!{T}(A::Array{T}, x, i1::Int, i2::Int, I::Int...) = (@_inline_meta; arrayset(A, convert(T,x)::T, i1, i2, I...)) # TODO: REMOVE FOR #14770 +setindex!(A::Array{T}, x, i1::Int) where {T} = arrayset(A, convert(T,x)::T, i1) +setindex!(A::Array{T}, x, i1::Int, i2::Int, I::Int...) where {T} = (@_inline_meta; arrayset(A, convert(T,x)::T, i1, i2, I...)) # TODO: REMOVE FOR #14770 # These are redundant with the abstract fallbacks but needed for bootstrap function setindex!(A::Array, x, I::AbstractVector{Int}) @@ -565,7 +565,7 @@ function setindex!(A::Array, X::AbstractArray, I::AbstractVector{Int}) end # Faster contiguous setindex! with copy! -function setindex!{T}(A::Array{T}, X::Array{T}, I::UnitRange{Int}) +function setindex!(A::Array{T}, X::Array{T}, I::UnitRange{Int}) where T @_inline_meta @boundscheck checkbounds(A, I) lI = length(I) @@ -575,7 +575,7 @@ function setindex!{T}(A::Array{T}, X::Array{T}, I::UnitRange{Int}) end return A end -function setindex!{T}(A::Array{T}, X::Array{T}, c::Colon) +function setindex!(A::Array{T}, X::Array{T}, c::Colon) where T @_inline_meta lI = length(A) @boundscheck setindex_shape_check(X, lI) @@ -586,7 +586,7 @@ function setindex!{T}(A::Array{T}, X::Array{T}, c::Colon) end setindex!(A::Array, x::Number, ::Colon) = fill!(A, x) -setindex!{T, N}(A::Array{T, N}, x::Number, ::Vararg{Colon, N}) = fill!(A, x) +setindex!(A::Array{T, N}, x::Number, ::Vararg{Colon, N}) where {T, N} = fill!(A, x) # efficiently grow an array @@ -600,7 +600,7 @@ _deleteat!(a::Vector, i::Integer, delta::Integer) = ## Dequeue functionality ## -function push!{T}(a::Array{T,1}, item) +function push!(a::Array{T,1}, item) where T # convert first so we don't grow the array if the assignment won't work itemT = convert(T, item) ccall(:jl_array_grow_end, Void, (Any, UInt), a, 1) @@ -762,7 +762,7 @@ julia> unshift!([1, 2, 3, 4], 5, 6) 4 ``` """ -function unshift!{T}(a::Array{T,1}, item) +function unshift!(a::Array{T,1}, item) where T item = convert(T, item) ccall(:jl_array_grow_beg, Void, (Any, UInt), a, 1) a[1] = item @@ -795,7 +795,7 @@ julia> insert!([6, 5, 4, 2, 1], 4, 3) 1 ``` """ -function insert!{T}(a::Array{T,1}, i::Integer, item) +function insert!(a::Array{T,1}, i::Integer, item) where T # Throw convert error before changing the shape of the array _item = convert(T, item) _growat!(a, i, 1) @@ -1039,13 +1039,13 @@ function lexcmp(a::Array{UInt8,1}, b::Array{UInt8,1}) end # use memcmp for == on bit integer types -function =={T<:BitInteger,N}(a::Array{T,N}, b::Array{T,N}) +function ==(a::Array{T,N}, b::Array{T,N}) where T<:BitInteger where N size(a) == size(b) && 0 == ccall( :memcmp, Int32, (Ptr{T}, Ptr{T}, UInt), a, b, sizeof(T) * length(a)) end # this is ~20% faster than the generic implementation above for very small arrays -function =={T<:BitInteger}(a::Array{T,1}, b::Array{T,1}) +function ==(a::Array{T,1}, b::Array{T,1}) where T<:BitInteger len = length(a) len == length(b) && 0 == ccall( :memcmp, Int32, (Ptr{T}, Ptr{T}, UInt), a, b, sizeof(T) * len) @@ -1091,7 +1091,7 @@ end vcat() = Array{Any,1}(0) hcat() = Array{Any,1}(0) -function hcat{T}(V::Vector{T}...) +function hcat(V::Vector{T}...) where T height = length(V[1]) for j = 2:length(V) if length(V[j]) != height @@ -1101,7 +1101,7 @@ function hcat{T}(V::Vector{T}...) return [ V[j][i]::T for i=1:length(V[1]), j=1:length(V) ] end -function vcat{T}(arrays::Vector{T}...) +function vcat(arrays::Vector{T}...) where T n = 0 for a in arrays n += length(a) @@ -1551,7 +1551,7 @@ julia> findnz(A) ([1, 1, 3, 2], [1, 2, 2, 3], [1, 2, 4, 3]) ``` """ -function findnz{T}(A::AbstractMatrix{T}) +function findnz(A::AbstractMatrix{T}) where T nnzA = countnz(A) I = zeros(Int, nnzA) J = zeros(Int, nnzA) diff --git a/base/arraymath.jl b/base/arraymath.jl index 762a582b53791..1d286bda5063b 100644 --- a/base/arraymath.jl +++ b/base/arraymath.jl @@ -50,7 +50,7 @@ end ## data movement ## -function flipdim{T}(A::Array{T}, d::Integer) +function flipdim(A::Array{T}, d::Integer) where T nd = ndims(A) 1 ≤ d ≤ nd || throw(ArgumentError("dimension $d is not 1 ≤ $d ≤ $nd")) sd = size(A, d) diff --git a/base/atomics.jl b/base/atomics.jl index 09bd9788637cd..1fc7f7a64d3ed 100644 --- a/base/atomics.jl +++ b/base/atomics.jl @@ -303,7 +303,7 @@ julia> x[] function atomic_min! end unsafe_convert(::Type{Ptr{T}}, x::Atomic{T}) where {T} = convert(Ptr{T}, pointer_from_objref(x)) -setindex!{T}(x::Atomic{T}, v) = setindex!(x, convert(T, v)) +setindex!(x::Atomic{T}, v) where {T} = setindex!(x, convert(T, v)) const llvmtypes = Dict( Bool => "i1", @@ -316,13 +316,13 @@ const llvmtypes = Dict( Float32 => "float", Float64 => "double", ) -inttype{T<:Integer}(::Type{T}) = T +inttype(::Type{T}) where {T<:Integer} = T inttype(::Type{Float16}) = Int16 inttype(::Type{Float32}) = Int32 inttype(::Type{Float64}) = Int64 -alignment{T}(::Type{T}) = ccall(:jl_alignment, Cint, (Csize_t,), sizeof(T)) +alignment(::Type{T}) where {T} = ccall(:jl_alignment, Cint, (Csize_t,), sizeof(T)) # All atomic operations have acquire and/or release semantics, depending on # whether the load or store values. Most of the time, this is what one wants @@ -447,7 +447,7 @@ end const opnames = Dict{Symbol, Symbol}(:+ => :add, :- => :sub) for op in [:+, :-, :max, :min] opname = get(opnames, op, op) - @eval function $(Symbol("atomic_", opname, "!")){T<:FloatTypes}(var::Atomic{T}, val::T) + @eval function $(Symbol("atomic_", opname, "!"))(var::Atomic{T}, val::T) where T<:FloatTypes IT = inttype(T) old = var[] while true diff --git a/base/base.jl b/base/base.jl index 652b9106a18c1..fd04ea496b6ad 100644 --- a/base/base.jl +++ b/base/base.jl @@ -129,7 +129,7 @@ function finalizer(o::ANY, f::ANY) ccall(:jl_gc_add_finalizer_th, Void, (Ptr{Void}, Any, Any), Core.getptls(), o, f) end -function finalizer{T}(o::T, f::Ptr{Void}) +function finalizer(o::T, f::Ptr{Void}) where T @_inline_meta if isimmutable(T) error("objects of type ", T, " cannot be finalized") diff --git a/base/bitarray.jl b/base/bitarray.jl index 99faeebf38183..1fa5700044656 100644 --- a/base/bitarray.jl +++ b/base/bitarray.jl @@ -472,7 +472,7 @@ function copy!(dest::BitArray, src::Array) return unsafe_copy!(dest, 1, src, 1, length(src)) end -function reshape{N}(B::BitArray, dims::NTuple{N,Int}) +function reshape(B::BitArray, dims::NTuple{N,Int}) where N prod(dims) == length(B) || throw(DimensionMismatch("new dimensions $(dims) must be consistent with array size $(length(B))")) dims == size(B) && return B @@ -487,7 +487,7 @@ end convert(::Type{Array{T}}, B::BitArray{N}) where {T,N} = convert(Array{T,N}, B) convert(::Type{Array{T,N}}, B::BitArray{N}) where {T,N} = _convert(Array{T,N}, B) # see #15801 -function _convert{T,N}(::Type{Array{T,N}}, B::BitArray{N}) +function _convert(::Type{Array{T,N}}, B::BitArray{N}) where {T,N} A = Array{T}(size(B)) Bc = B.chunks @inbounds for i = 1:length(A) @@ -534,8 +534,8 @@ end convert(::Type{BitArray{N}}, B::BitArray{N}) where {N} = B convert(::Type{AbstractArray{T,N}}, B::BitArray{N}) where {T,N} = convert(Array{T,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) +reinterpret(::Type{Bool}, B::BitArray, dims::NTuple{N,Int}) where {N} = reinterpret(B, dims) +reinterpret(B::BitArray, dims::NTuple{N,Int}) where {N} = reshape(B, dims) ## Constructors from generic iterables ## diff --git a/base/bool.jl b/base/bool.jl index a874de0ca6bec..b73ccee81a45e 100644 --- a/base/bool.jl +++ b/base/bool.jl @@ -7,7 +7,7 @@ convert(::Type{Bool}, x::Float16) = x==0 ? false : x==1 ? true : throw(InexactEr convert(::Type{Bool}, x::Real) = x==0 ? false : x==1 ? true : throw(InexactError()) # promote Bool to any other numeric type -promote_rule{T<:Number}(::Type{Bool}, ::Type{T}) = T +promote_rule(::Type{Bool}, ::Type{T}) where {T<:Number} = T typemin(::Type{Bool}) = false typemax(::Type{Bool}) = true @@ -92,15 +92,15 @@ iszero(x::Bool) = !x ^(x::Bool, y::Bool) = x | !y ^(x::Integer, y::Bool) = ifelse(y, x, one(x)) -function +{T<:AbstractFloat}(x::Bool, y::T)::promote_type(Bool,T) +function +(x::Bool, y::T)::promote_type(Bool,T) where T<:AbstractFloat return ifelse(x, oneunit(y) + y, y) end +(y::AbstractFloat, x::Bool) = x + y -function *{T<:Number}(x::Bool, y::T)::promote_type(Bool,T) +function *(x::Bool, y::T)::promote_type(Bool,T) where T<:Number return ifelse(x, y, copysign(zero(y), y)) end -function *{T<:Unsigned}(x::Bool, y::T)::promote_type(Bool,T) +function *(x::Bool, y::T)::promote_type(Bool,T) where T<:Unsigned return ifelse(x, y, zero(y)) end *(y::Number, x::Bool) = x * y diff --git a/base/boot.jl b/base/boot.jl index 1e01b32f45aa2..32de033195dbc 100644 --- a/base/boot.jl +++ b/base/boot.jl @@ -295,32 +295,32 @@ Task(f::ANY) = ccall(:jl_new_task, Ref{Task}, (Any, Int), f, 0) # so the methods and ccall's in Core aren't permitted to use convert convert(::Type{Any}, x::ANY) = x convert(::Type{T}, x::T) where {T} = x -cconvert{T}(::Type{T}, x) = convert(T, x) +cconvert(::Type{T}, x) where {T} = convert(T, x) unsafe_convert(::Type{T}, x::T) where {T} = x NTuple{N,T} = Tuple{Vararg{T,N}} # primitive array constructors -(::Type{Array{T,N}}){T,N}(d::NTuple{N,Int}) = +(::Type{Array{T,N}})(d::NTuple{N,Int}) where {T,N} = ccall(:jl_new_array, Array{T,N}, (Any,Any), Array{T,N}, d) -(::Type{Array{T,1}}){T}(d::NTuple{1,Int}) = Array{T,1}(getfield(d,1)) -(::Type{Array{T,2}}){T}(d::NTuple{2,Int}) = Array{T,2}(getfield(d,1), getfield(d,2)) -(::Type{Array{T,3}}){T}(d::NTuple{3,Int}) = Array{T,3}(getfield(d,1), getfield(d,2), getfield(d,3)) -(::Type{Array{T,N}}){T,N}(d::Vararg{Int, N}) = ccall(:jl_new_array, Array{T,N}, (Any,Any), Array{T,N}, d) -(::Type{Array{T,1}}){T}(m::Int) = +(::Type{Array{T,1}})(d::NTuple{1,Int}) where {T} = Array{T,1}(getfield(d,1)) +(::Type{Array{T,2}})(d::NTuple{2,Int}) where {T} = Array{T,2}(getfield(d,1), getfield(d,2)) +(::Type{Array{T,3}})(d::NTuple{3,Int}) where {T} = Array{T,3}(getfield(d,1), getfield(d,2), getfield(d,3)) +(::Type{Array{T,N}})(d::Vararg{Int, N}) where {T,N} = ccall(:jl_new_array, Array{T,N}, (Any,Any), Array{T,N}, d) +(::Type{Array{T,1}})(m::Int) where {T} = ccall(:jl_alloc_array_1d, Array{T,1}, (Any,Int), Array{T,1}, m) -(::Type{Array{T,2}}){T}(m::Int, n::Int) = +(::Type{Array{T,2}})(m::Int, n::Int) where {T} = ccall(:jl_alloc_array_2d, Array{T,2}, (Any,Int,Int), Array{T,2}, m, n) -(::Type{Array{T,3}}){T}(m::Int, n::Int, o::Int) = +(::Type{Array{T,3}})(m::Int, n::Int, o::Int) where {T} = ccall(:jl_alloc_array_3d, Array{T,3}, (Any,Int,Int,Int), Array{T,3}, m, n, o) -(::Type{Array{T}}){T,N}(d::NTuple{N,Int}) = Array{T,N}(d) -(::Type{Array{T}}){T}(m::Int) = Array{T,1}(m) -(::Type{Array{T}}){T}(m::Int, n::Int) = Array{T,2}(m, n) -(::Type{Array{T}}){T}(m::Int, n::Int, o::Int) = Array{T,3}(m, n, o) +(::Type{Array{T}})(d::NTuple{N,Int}) where {T,N} = Array{T,N}(d) +(::Type{Array{T}})(m::Int) where {T} = Array{T,1}(m) +(::Type{Array{T}})(m::Int, n::Int) where {T} = Array{T,2}(m, n) +(::Type{Array{T}})(m::Int, n::Int, o::Int) where {T} = Array{T,3}(m, n, o) -(::Type{Array{T,1}}){T}() = Array{T,1}(0) +(::Type{Array{T,1}})() where {T} = Array{T,1}(0) # primitive Symbol constructors function Symbol(s::String) diff --git a/base/broadcast.jl b/base/broadcast.jl index 53927084de7f9..436bed3068d59 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -14,7 +14,7 @@ const ScalarType = Union{Type{Any}, Type{Nullable}} ## Broadcasting utilities ## # fallbacks for some special cases @inline broadcast(f, x::Number...) = f(x...) -@inline broadcast{N}(f, t::NTuple{N,Any}, ts::Vararg{NTuple{N,Any}}) = map(f, t, ts...) +@inline broadcast(f, t::NTuple{N,Any}, ts::Vararg{NTuple{N,Any}}) where {N} = map(f, t, ts...) broadcast!{T,S,N}(::typeof(identity), x::Array{T,N}, y::Array{S,N}) = size(x) == size(y) ? copy!(x, y) : broadcast_c!(identity, Array, Array, x, y) @@ -201,9 +201,9 @@ Note that `dest` is only used to store the result, and does not supply arguments to `f` unless it is also listed in the `As`, as in `broadcast!(f, A, A, B)` to perform `A[:] = broadcast(f, A, B)`. """ -@inline broadcast!{N}(f, C::AbstractArray, A, Bs::Vararg{Any,N}) = +@inline broadcast!(f, C::AbstractArray, A, Bs::Vararg{Any,N}) where {N} = broadcast_c!(f, containertype(C), containertype(A, Bs...), C, A, Bs...) -@inline function broadcast_c!{N}(f, ::Type, ::Type, C, A, Bs::Vararg{Any,N}) +@inline function broadcast_c!(f, ::Type, ::Type, C, A, Bs::Vararg{Any,N}) where N shape = indices(C) @boundscheck check_broadcast_indices(shape, A, Bs...) keeps, Idefaults = map_newindexer(shape, A, Bs) @@ -335,9 +335,9 @@ end @inline broadcast_c(f, ::Type{Any}, a...) = f(a...) @inline broadcast_c(f, ::Type{Tuple}, A, Bs...) = tuplebroadcast(f, first_tuple(A, Bs...), A, Bs...) -@inline tuplebroadcast{N}(f, ::NTuple{N,Any}, As...) = +@inline tuplebroadcast(f, ::NTuple{N,Any}, As...) where {N} = ntuple(k -> f(tuplebroadcast_getargs(As, k)...), Val{N}) -@inline tuplebroadcast{N,T}(f, ::NTuple{N,Any}, ::Type{T}, As...) = +@inline tuplebroadcast(f, ::NTuple{N,Any}, ::Type{T}, As...) where {N,T} = ntuple(k -> f(T, tuplebroadcast_getargs(As, k)...), Val{N}) first_tuple(A::Tuple, Bs...) = A @inline first_tuple(A, Bs...) = first_tuple(Bs...) diff --git a/base/c.jl b/base/c.jl index 34d4f86c80c4a..dba82413d88bb 100644 --- a/base/c.jl +++ b/base/c.jl @@ -63,11 +63,11 @@ end # construction from typed pointers convert(::Type{Cstring}, p::Ptr{<:Union{Int8,UInt8}}) = bitcast(Cstring, p) convert(::Type{Cwstring}, p::Ptr{Cwchar_t}) = bitcast(Cwstring, p) -convert{T<:Union{Int8,UInt8}}(::Type{Ptr{T}}, p::Cstring) = bitcast(Ptr{T}, p) +convert(::Type{Ptr{T}}, p::Cstring) where {T<:Union{Int8,UInt8}} = bitcast(Ptr{T}, p) convert(::Type{Ptr{Cwchar_t}}, p::Cwstring) = bitcast(Ptr{Cwchar_t}, p) # construction from untyped pointers -convert{T<:Union{Cstring,Cwstring}}(::Type{T}, p::Ptr{Void}) = bitcast(T, p) +convert(::Type{T}, p::Ptr{Void}) where {T<:Union{Cstring,Cwstring}} = bitcast(T, p) pointer(p::Cstring) = convert(Ptr{UInt8}, p) pointer(p::Cwstring) = convert(Ptr{Cwchar_t}, p) diff --git a/base/channels.jl b/base/channels.jl index a6c8859b20abe..403dc73190bc2 100644 --- a/base/channels.jl +++ b/base/channels.jl @@ -390,7 +390,7 @@ mutable struct ChannelIterState{T} ChannelIterState{T}(has::Bool) where {T} = new(has) end -start{T}(c::Channel{T}) = ChannelIterState{T}(false) +start(c::Channel{T}) where {T} = ChannelIterState{T}(false) function done(c::Channel, state::ChannelIterState) try # we are waiting either for more data or channel to be closed diff --git a/base/complex.jl b/base/complex.jl index 3d0bf6cebcf84..eb27aeff7b7fa 100644 --- a/base/complex.jl +++ b/base/complex.jl @@ -26,9 +26,9 @@ convert(::Type{T}, z::Complex) where {T<:Real} = convert(::Type{Complex}, z::Complex) = z convert(::Type{Complex}, x::Real) = Complex(x) -promote_rule{T<:Real,S<:Real}(::Type{Complex{T}}, ::Type{S}) = +promote_rule(::Type{Complex{T}}, ::Type{S}) where {T<:Real,S<:Real} = Complex{promote_type(T,S)} -promote_rule{T<:Real,S<:Real}(::Type{Complex{T}}, ::Type{Complex{S}}) = +promote_rule(::Type{Complex{T}}, ::Type{Complex{S}}) where {T<:Real,S<:Real} = Complex{promote_type(T,S)} widen{T}(::Type{Complex{T}}) = Complex{widen(T)} @@ -267,10 +267,10 @@ muladd(z::Complex, w::Complex, x::Real) = Complex(muladd(real(z), real(w), x) - imag(z)*imag(w), # TODO: use mulsub given #15985 muladd(real(z), imag(w), imag(z) * real(w))) -/{R<:Real,S<:Complex}(a::R, z::S) = (T = promote_type(R,S); a*inv(T(z))) +/(a::R, z::S) where {R<:Real,S<:Complex} = (T = promote_type(R,S); a*inv(T(z))) /(z::Complex, x::Real) = Complex(real(z)/x, imag(z)/x) -function /{T<:Real}(a::Complex{T}, b::Complex{T}) +function /(a::Complex{T}, b::Complex{T}) where T<:Real are = real(a); aim = imag(a); bre = real(b); bim = imag(b) if abs(bre) <= abs(bim) if isinf(bre) && isinf(bim) @@ -294,7 +294,7 @@ end inv(z::Complex{<:Union{Float16,Float32}}) = oftype(z, conj(widen(z))/abs2(widen(z))) -/{T<:Union{Float16,Float32}}(z::Complex{T}, w::Complex{T}) = +/(z::Complex{T}, w::Complex{T}) where {T<:Union{Float16,Float32}} = oftype(z, widen(z)*inv(widen(w))) # robust complex division for double precision @@ -554,7 +554,7 @@ function log1p{T}(z::Complex{T}) end end -function ^{T<:AbstractFloat}(z::Complex{T}, p::Complex{T})::Complex{T} +function ^(z::Complex{T}, p::Complex{T})::Complex{T} where T<:AbstractFloat if p == 2 #square zr, zi = reim(z) x = (zr-zi)*(zr+zi) @@ -601,7 +601,7 @@ function exp10{T}(z::Complex{T}) Complex(er*cos(theta), er*sin(theta)) end -function ^{T<:Complex}(z::T, p::T) +function ^(z::T, p::T) where T<:Complex if isinteger(p) rp = real(p) if rp < 0 diff --git a/base/datafmt.jl b/base/datafmt.jl index 7637e7d36ab84..a80bd1f5328d2 100644 --- a/base/datafmt.jl +++ b/base/datafmt.jl @@ -222,8 +222,8 @@ _chrinstr(sbuff::String, chr::UInt8, startpos::Int, endpos::Int) = (endpos >= startpos) && (C_NULL != ccall(:memchr, Ptr{UInt8}, (Ptr{UInt8}, Int32, Csize_t), pointer(sbuff)+startpos-1, chr, endpos-startpos+1)) -function store_cell{T}(dlmstore::DLMStore{T}, row::Int, col::Int, - quoted::Bool, startpos::Int, endpos::Int) +function store_cell(dlmstore::DLMStore{T}, row::Int, col::Int, + quoted::Bool, startpos::Int, endpos::Int) where T drow = row - dlmstore.hdr_offset ncols = dlmstore.ncols @@ -291,7 +291,7 @@ function store_cell{T}(dlmstore::DLMStore{T}, row::Int, col::Int, nothing end -function result{T}(dlmstore::DLMStore{T}) +function result(dlmstore::DLMStore{T}) where T nrows = dlmstore.nrows - dlmstore.hdr_offset ncols = dlmstore.ncols lastcol = dlmstore.lastcol @@ -456,9 +456,9 @@ function colval(sbuff::String, startpos::Int, endpos::Int, cells::Array{<:Char,2 end colval(sbuff::String, startpos::Int, endpos::Int, cells::Array, row::Int, col::Int) = true -function dlm_parse{D}(dbuff::String, eol::D, dlm::D, qchar::D, cchar::D, +function dlm_parse(dbuff::String, eol::D, dlm::D, qchar::D, cchar::D, ign_adj_dlm::Bool, allow_quote::Bool, allow_comments::Bool, - skipstart::Int, skipblanks::Bool, dh::DLMHandler) + skipstart::Int, skipblanks::Bool, dh::DLMHandler) where D ncols = nrows = col = 0 is_default_dlm = (dlm == invalid_dlm(D)) error_str = "" diff --git a/base/dates/arithmetic.jl b/base/dates/arithmetic.jl index ac59b4971e3ab..e1a611af0828f 100644 --- a/base/dates/arithmetic.jl +++ b/base/dates/arithmetic.jl @@ -2,11 +2,11 @@ # Instant arithmetic (+)(x::Instant) = x -(-){T<:Instant}(x::T, y::T) = x.periods - y.periods +(-)(x::T, y::T) where {T<:Instant} = x.periods - y.periods # TimeType arithmetic (+)(x::TimeType) = x -(-){T<:TimeType}(x::T, y::T) = x.instant - y.instant +(-)(x::T, y::T) where {T<:TimeType} = x.instant - y.instant # Date-Time arithmetic """ @@ -90,9 +90,9 @@ end (-)(x::StridedArray{<:GeneralPeriod}, y::TimeType) = x .- y # TimeType, AbstractArray{TimeType} -(-){T<:TimeType}(x::AbstractArray{T}, y::T) = x .- y -(-){T<:TimeType}(y::T, x::AbstractArray{T}) = y .- x +(-)(x::AbstractArray{T}, y::T) where {T<:TimeType} = x .- y +(-)(y::T, x::AbstractArray{T}) where {T<:TimeType} = y .- x # AbstractArray{TimeType}, AbstractArray{TimeType} -(-){T<:TimeType}(x::OrdinalRange{T}, y::OrdinalRange{T}) = collect(x) - collect(y) -(-){T<:TimeType}(x::Range{T}, y::Range{T}) = collect(x) - collect(y) +(-)(x::OrdinalRange{T}, y::OrdinalRange{T}) where {T<:TimeType} = collect(x) - collect(y) +(-)(x::Range{T}, y::Range{T}) where {T<:TimeType} = collect(x) - collect(y) diff --git a/base/dates/periods.jl b/base/dates/periods.jl index 0779d8446fed6..e2c40726e7adf 100644 --- a/base/dates/periods.jl +++ b/base/dates/periods.jl @@ -58,9 +58,9 @@ function default end default{T<:DatePeriod}(p::Union{T,Type{T}}) = T(1) default{T<:TimePeriod}(p::Union{T,Type{T}}) = T(0) -(-){P<:Period}(x::P) = P(-value(x)) +(-)(x::P) where {P<:Period} = P(-value(x)) Base.isless{P<:Period}(x::P, y::P) = isless(value(x), value(y)) -=={P<:Period}(x::P, y::P) = value(x) == value(y) +==(x::P, y::P) where {P<:Period} = value(x) == value(y) # Period Arithmetic, grouped by dimensionality: import Base: div, fld, mod, rem, gcd, lcm, +, -, *, /, % @@ -82,7 +82,7 @@ for op in (:rem, :mod) end end -*{P<:Period}(x::P, y::Real) = P(value(x) * Int64(y)) +*(x::P, y::Real) where {P<:Period} = P(value(x) * Int64(y)) *(y::Real, x::Period) = x * y for (op, Ty, Tz) in ((:*, Real, :P), (:/, :P, Float64), (:/, Real, :P)) diff --git a/base/dates/types.jl b/base/dates/types.jl index 3a2d67415339f..4bfa5c5339d8f 100644 --- a/base/dates/types.jl +++ b/base/dates/types.jl @@ -294,11 +294,11 @@ Base.typemin(::Union{Date, Type{Date}}) = Date(-252522163911150, 1, 1) Base.typemax(::Union{Time, Type{Time}}) = Time(23, 59, 59, 999, 999, 999) Base.typemin(::Union{Time, Type{Time}}) = Time(0) # Date-DateTime promotion, isless, == -Base.eltype{T<:Period}(::Type{T}) = T +Base.eltype(::Type{T}) where {T<:Period} = T Base.promote_rule(::Type{Date}, x::Type{DateTime}) = DateTime -Base.isless{T<:TimeType}(x::T, y::T) = isless(value(x), value(y)) +Base.isless(x::T, y::T) where {T<:TimeType} = isless(value(x), value(y)) Base.isless(x::TimeType, y::TimeType) = isless(Base.promote_noncircular(x, y)...) -=={T<:TimeType}(x::T, y::T) = ==(value(x), value(y)) +==(x::T, y::T) where {T<:TimeType} = ==(value(x), value(y)) function ==(a::Time, b::Time) return hour(a) == hour(b) && minute(a) == minute(b) && second(a) == second(b) && millisecond(a) == millisecond(b) && diff --git a/base/deprecated.jl b/base/deprecated.jl index eda5caa42f303..fe3d2e7e5e655 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1196,10 +1196,10 @@ last{T}(r::Use_StepRangeLen_Instead{T}) = convert(T, (r.start + (r.len-1)*r.step start(r::Use_StepRangeLen_Instead) = 0 done(r::Use_StepRangeLen_Instead, i::Int) = length(r) <= i -next{T}(r::Use_StepRangeLen_Instead{T}, i::Int) = +next(r::Use_StepRangeLen_Instead{T}, i::Int) where {T} = (convert(T, (r.start + i*r.step)/r.divisor), i+1) -function getindex{T}(r::Use_StepRangeLen_Instead{T}, i::Integer) +function getindex(r::Use_StepRangeLen_Instead{T}, i::Integer) where T @_inline_meta @boundscheck checkbounds(r, i) convert(T, (r.start + (i-1)*r.step)/r.divisor) @@ -1218,20 +1218,20 @@ end *(x::Real, r::Use_StepRangeLen_Instead) = Use_StepRangeLen_Instead(x*r.start, x*r.step, r.len, r.divisor) *(r::Use_StepRangeLen_Instead, x::Real) = x * r /(r::Use_StepRangeLen_Instead, x::Real) = Use_StepRangeLen_Instead(r.start/x, r.step/x, r.len, r.divisor) -promote_rule{T1,T2}(::Type{Use_StepRangeLen_Instead{T1}},::Type{Use_StepRangeLen_Instead{T2}}) = +promote_rule(::Type{Use_StepRangeLen_Instead{T1}},::Type{Use_StepRangeLen_Instead{T2}}) where {T1,T2} = Use_StepRangeLen_Instead{promote_type(T1,T2)} convert(::Type{Use_StepRangeLen_Instead{T}}, r::Use_StepRangeLen_Instead{T}) where {T<:AbstractFloat} = r convert(::Type{Use_StepRangeLen_Instead{T}}, r::Use_StepRangeLen_Instead) where {T<:AbstractFloat} = Use_StepRangeLen_Instead{T}(r.start,r.step,r.len,r.divisor) -promote_rule{F,OR<:OrdinalRange}(::Type{Use_StepRangeLen_Instead{F}}, ::Type{OR}) = +promote_rule(::Type{Use_StepRangeLen_Instead{F}}, ::Type{OR}) where {F,OR<:OrdinalRange} = Use_StepRangeLen_Instead{promote_type(F,eltype(OR))} convert(::Type{Use_StepRangeLen_Instead{T}}, r::OrdinalRange) where {T<:AbstractFloat} = Use_StepRangeLen_Instead{T}(first(r), step(r), length(r), one(T)) convert(::Type{Use_StepRangeLen_Instead}, r::OrdinalRange{T}) where {T} = Use_StepRangeLen_Instead{typeof(float(first(r)))}(first(r), step(r), length(r), one(T)) -promote_rule{F,OR<:Use_StepRangeLen_Instead}(::Type{LinSpace{F}}, ::Type{OR}) = +promote_rule(::Type{LinSpace{F}}, ::Type{OR}) where {F,OR<:Use_StepRangeLen_Instead} = LinSpace{promote_type(F,eltype(OR))} convert(::Type{LinSpace{T}}, r::Use_StepRangeLen_Instead) where {T<:AbstractFloat} = linspace(convert(T, first(r)), convert(T, last(r)), convert(T, length(r))) diff --git a/base/dft.jl b/base/dft.jl index 6239a3b4e23ba..3f40878c51bae 100644 --- a/base/dft.jl +++ b/base/dft.jl @@ -22,24 +22,24 @@ export fft, ifft, bfft, fft!, ifft!, bfft!, const FFTWFloat = Union{Float32,Float64} fftwfloat(x) = _fftwfloat(float(x)) -_fftwfloat{T<:FFTWFloat}(::Type{T}) = T +_fftwfloat(::Type{T}) where {T<:FFTWFloat} = T _fftwfloat(::Type{Float16}) = Float32 -_fftwfloat{T}(::Type{Complex{T}}) = Complex{_fftwfloat(T)} -_fftwfloat{T}(::Type{T}) = error("type $T not supported") -_fftwfloat{T}(x::T) = _fftwfloat(T)(x) +_fftwfloat(::Type{Complex{T}}) where {T} = Complex{_fftwfloat(T)} +_fftwfloat(::Type{T}) where {T} = error("type $T not supported") +_fftwfloat(x::T) where {T} = _fftwfloat(T)(x) complexfloat(x::StridedArray{Complex{<:FFTWFloat}}) = x realfloat(x::StridedArray{<:FFTWFloat}) = x # return an Array, rather than similar(x), to avoid an extra copy for FFTW # (which only works on StridedArray types). -complexfloat{T<:Complex}(x::AbstractArray{T}) = copy1(typeof(fftwfloat(zero(T))), x) -complexfloat{T<:Real}(x::AbstractArray{T}) = copy1(typeof(complex(fftwfloat(zero(T)))), x) +complexfloat(x::AbstractArray{T}) where {T<:Complex} = copy1(typeof(fftwfloat(zero(T))), x) +complexfloat(x::AbstractArray{T}) where {T<:Real} = copy1(typeof(complex(fftwfloat(zero(T)))), x) -realfloat{T<:Real}(x::AbstractArray{T}) = copy1(typeof(fftwfloat(zero(T))), x) +realfloat(x::AbstractArray{T}) where {T<:Real} = copy1(typeof(fftwfloat(zero(T))), x) # copy to a 1-based array, using circular permutation -function copy1{T}(::Type{T}, x) +function copy1(::Type{T}, x) where T y = Array{T}(map(length, indices(x))) Base.circcopy!(y, x) end @@ -211,7 +211,7 @@ rfft(x::AbstractArray{<:Union{Integer,Rational}}, region=1:ndims(x)) = rfft(real plan_rfft(x::AbstractArray, region; kws...) = plan_rfft(realfloat(x), region; kws...) # only require implementation to provide *(::Plan{T}, ::Array{T}) -*{T}(p::Plan{T}, x::AbstractArray) = p * copy1(T, x) +*(p::Plan{T}, x::AbstractArray) where {T} = p * copy1(T, x) # Implementations should also implement A_mul_B!(Y, plan, X) so as to support # pre-allocated output arrays. We don't define * in terms of A_mul_B! diff --git a/base/dict.jl b/base/dict.jl index 202f844050cb4..bfe6ca4322a73 100644 --- a/base/dict.jl +++ b/base/dict.jl @@ -23,7 +23,7 @@ function _truncate_at_width_or_chars(str, width, chars="", truncmark="…") end end -function show{K,V}(io::IO, t::Associative{K,V}) +function show(io::IO, t::Associative{K,V}) where V where K recur_io = IOContext(io, :SHOWN_SET => t) limit::Bool = get(io, :limit, false) if !haskey(io, :compact) @@ -154,12 +154,12 @@ end TP{K,V} = Union{Type{Tuple{K,V}},Type{Pair{K,V}}} -associative_with_eltype{K,V}(DT_apply, kv, ::TP{K,V}) = DT_apply(K, V)(kv) -associative_with_eltype{K,V}(DT_apply, kv::Generator, ::TP{K,V}) = DT_apply(K, V)(kv) -associative_with_eltype{K,V}(DT_apply, ::Type{Pair{K,V}}) = DT_apply(K, V)() +associative_with_eltype(DT_apply, kv, ::TP{K,V}) where {K,V} = DT_apply(K, V)(kv) +associative_with_eltype(DT_apply, kv::Generator, ::TP{K,V}) where {K,V} = DT_apply(K, V)(kv) +associative_with_eltype(DT_apply, ::Type{Pair{K,V}}) where {K,V} = DT_apply(K, V)() associative_with_eltype(DT_apply, ::Type) = DT_apply(Any, Any)() -associative_with_eltype{F}(DT_apply::F, kv, t) = grow_to!(associative_with_eltype(DT_apply, _default_eltype(typeof(kv))), kv) -function associative_with_eltype{F}(DT_apply::F, kv::Generator, t) +associative_with_eltype(DT_apply::F, kv, t) where {F} = grow_to!(associative_with_eltype(DT_apply, _default_eltype(typeof(kv))), kv) +function associative_with_eltype(DT_apply::F, kv::Generator, t) where F T = _default_eltype(typeof(kv)) if T <: Union{Pair, Tuple{Any, Any}} && isleaftype(T) return associative_with_eltype(DT_apply, kv, T) @@ -174,7 +174,7 @@ function grow_to!(dest::Associative, itr) return isempty(out) ? dest : out end -function grow_to!{K,V}(dest::Associative{K,V}, itr, st) +function grow_to!(dest::Associative{K,V}, itr, st) where V where K while !done(itr, st) (k,v), st = next(itr, st) if isa(k,K) && isa(v,V) @@ -189,8 +189,8 @@ function grow_to!{K,V}(dest::Associative{K,V}, itr, st) return dest end -similar{K,V}(d::Dict{K,V}) = Dict{K,V}() -similar{K,V}(d::Dict, ::Type{Pair{K,V}}) = Dict{K,V}() +similar(d::Dict{K,V}) where {K,V} = Dict{K,V}() +similar(d::Dict, ::Type{Pair{K,V}}) where {K,V} = Dict{K,V}() # conversion between Dict types function convert(::Type{Dict{K,V}},d::Associative) where V where K @@ -300,7 +300,7 @@ julia> A Dict{String,Int64} with 0 entries ``` """ -function empty!{K,V}(h::Dict{K,V}) +function empty!(h::Dict{K,V}) where V where K fill!(h.slots, 0x0) sz = length(h.slots) empty!(h.keys) @@ -315,7 +315,7 @@ function empty!{K,V}(h::Dict{K,V}) end # get the index where a key is stored, or -1 if not present -function ht_keyindex{K,V}(h::Dict{K,V}, key) +function ht_keyindex(h::Dict{K,V}, key) where V where K sz = length(h.keys) iter = 0 maxprobe = h.maxprobe @@ -340,7 +340,7 @@ end # get the index where a key is stored, or -pos if not present # and the key would be inserted at pos # This version is for use by setindex! and get! -function ht_keyindex2{K,V}(h::Dict{K,V}, key) +function ht_keyindex2(h::Dict{K,V}, key) where V where K age0 = h.age sz = length(h.keys) iter = 0 @@ -408,7 +408,7 @@ function _setindex!(h::Dict, v, key, index) end end -function setindex!{K,V}(h::Dict{K,V}, v0, key0) +function setindex!(h::Dict{K,V}, v0, key0) where V where K key = convert(K, key0) if !isequal(key, key0) throw(ArgumentError("$key0 is not a valid key for type $K")) @@ -416,7 +416,7 @@ function setindex!{K,V}(h::Dict{K,V}, v0, key0) setindex!(h, v0, key) end -function setindex!{K,V}(h::Dict{K,V}, v0, key::K) +function setindex!(h::Dict{K,V}, v0, key::K) where V where K v = convert(V, v0) index = ht_keyindex2(h, key) @@ -431,8 +431,8 @@ function setindex!{K,V}(h::Dict{K,V}, v0, key::K) return h end -get!{K,V}(h::Dict{K,V}, key0, default) = get!(()->default, h, key0) -function get!{K,V}(default::Callable, h::Dict{K,V}, key0) +get!(h::Dict{K,V}, key0, default) where {K,V} = get!(()->default, h, key0) +function get!(default::Callable, h::Dict{K,V}, key0) where V where K key = convert(K, key0) if !isequal(key, key0) throw(ArgumentError("$key0 is not a valid key for type $K")) @@ -440,7 +440,7 @@ function get!{K,V}(default::Callable, h::Dict{K,V}, key0) return get!(default, h, key) end -function get!{K,V}(default::Callable, h::Dict{K,V}, key::K) +function get!(default::Callable, h::Dict{K,V}, key::K) where V where K index = ht_keyindex2(h, key) index > 0 && return h.vals[index] @@ -469,17 +469,17 @@ macro get!(h, key0, default) end -function getindex{K,V}(h::Dict{K,V}, key) +function getindex(h::Dict{K,V}, key) where V where K index = ht_keyindex(h, key) return (index < 0) ? throw(KeyError(key)) : h.vals[index]::V end -function get{K,V}(h::Dict{K,V}, key, default) +function get(h::Dict{K,V}, key, default) where V where K index = ht_keyindex(h, key) return (index < 0) ? default : h.vals[index]::V end -function get{K,V}(default::Callable, h::Dict{K,V}, key) +function get(default::Callable, h::Dict{K,V}, key) where V where K index = ht_keyindex(h, key) return (index < 0) ? default() : h.vals[index]::V end @@ -523,7 +523,7 @@ julia> getkey(a,'d','a') 'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase) ``` """ -function getkey{K,V}(h::Dict{K,V}, key, default) +function getkey(h::Dict{K,V}, key, default) where V where K index = ht_keyindex(h, key) return (index<0) ? default : h.keys[index]::K end @@ -576,7 +576,7 @@ function start(t::Dict) return i end done(t::Dict, i) = i > length(t.vals) -next{K,V}(t::Dict{K,V}, i) = (Pair{K,V}(t.keys[i],t.vals[i]), skip_deleted(t,i+1)) +next(t::Dict{K,V}, i) where {K,V} = (Pair{K,V}(t.keys[i],t.vals[i]), skip_deleted(t,i+1)) isempty(t::Dict) = (t.count == 0) length(t::Dict) = t.count @@ -595,8 +595,8 @@ function filter!(f, d::Union{ObjectIdDict,Dict}) return d end -struct ImmutableDict{K, V} <: Associative{K,V} - parent::ImmutableDict{K, V} +struct ImmutableDict{K,V} <: Associative{K,V} + parent::ImmutableDict{K,V} key::K value::V ImmutableDict{K,V}() where {K,V} = new() # represents an empty dictionary @@ -660,7 +660,7 @@ end # this actually defines reverse iteration (e.g. it should not be used for merge/copy/filter type operations) start(t::ImmutableDict) = t -next{K,V}(::ImmutableDict{K,V}, t) = (Pair{K,V}(t.key, t.value), t.parent) +next(::ImmutableDict{K,V}, t) where {K,V} = (Pair{K,V}(t.key, t.value), t.parent) done(::ImmutableDict, t) = !isdefined(t, :parent) length(t::ImmutableDict) = count(x->true, t) isempty(t::ImmutableDict) = done(t, start(t)) diff --git a/base/docs/utils.jl b/base/docs/utils.jl index 973c49eb2ee06..2802e5bf8c4c5 100644 --- a/base/docs/utils.jl +++ b/base/docs/utils.jl @@ -72,8 +72,8 @@ print(io::IO, t::Text) = print(io, t.content) print(io::IO, t::Text{<:Function}) = t.content(io) show(io::IO, t::Text) = print(io, t) -=={T<:Union{HTML, Text}}(t1::T, t2::T) = t1.content == t2.content -hash{T<:Union{HTML, Text}}(t::T, h::UInt) = hash(T, hash(t.content, h)) +==(t1::T, t2::T) where {T<:Union{HTML,Text}} = t1.content == t2.content +hash(t::T, h::UInt) where {T<:Union{HTML,Text}} = hash(T, hash(t.content, h)) """ @text_str -> Docs.Text diff --git a/base/essentials.jl b/base/essentials.jl index f2f5da38907c4..a944e84d07416 100644 --- a/base/essentials.jl +++ b/base/essentials.jl @@ -133,9 +133,9 @@ function typename(a::Union) end typename(union::UnionAll) = typename(union.body) -convert{T<:Tuple{Any,Vararg{Any}}}(::Type{T}, x::Tuple{Any, Vararg{Any}}) = +convert(::Type{T}, x::Tuple{Any,Vararg{Any}}) where {T<:Tuple{Any,Vararg{Any}}} = tuple(convert(tuple_type_head(T),x[1]), convert(tuple_type_tail(T), tail(x))...) -convert{T<:Tuple{Any,Vararg{Any}}}(::Type{T}, x::T) = x +convert(::Type{T}, x::T) where {T<:Tuple{Any,Vararg{Any}}} = x oftype(x,c) = convert(typeof(x),c) @@ -143,8 +143,8 @@ unsigned(x::Int) = reinterpret(UInt, x) signed(x::UInt) = reinterpret(Int, x) # conversions used by ccall -ptr_arg_cconvert{T}(::Type{Ptr{T}}, x) = cconvert(T, x) -ptr_arg_unsafe_convert{T}(::Type{Ptr{T}}, x) = unsafe_convert(T, x) +ptr_arg_cconvert(::Type{Ptr{T}}, x) where {T} = cconvert(T, x) +ptr_arg_unsafe_convert(::Type{Ptr{T}}, x) where {T} = unsafe_convert(T, x) ptr_arg_unsafe_convert(::Type{Ptr{Void}}, x) = x cconvert(T::Type, x) = convert(T, x) # do the conversion eagerly in most cases @@ -153,7 +153,7 @@ unsafe_convert(::Type{T}, x::T) where {T} = x # unsafe_convert (like convert) de unsafe_convert(::Type{T}, x::T) where {T<:Ptr} = x # to resolve ambiguity with the next method unsafe_convert(::Type{P}, x::Ptr) where {P<:Ptr} = convert(P, x) -reinterpret{T}(::Type{T}, x) = bitcast(T, x) +reinterpret(::Type{T}, x) where {T} = bitcast(T, x) reinterpret(::Type{Unsigned}, x::Float16) = reinterpret(UInt16,x) reinterpret(::Type{Signed}, x::Float16) = reinterpret(Int16,x) diff --git a/base/fft/FFTW.jl b/base/fft/FFTW.jl index 712f7e6ebe850..bcf93a7e05a80 100644 --- a/base/fft/FFTW.jl +++ b/base/fft/FFTW.jl @@ -74,7 +74,7 @@ struct FakeArray{T, N} <: DenseArray{T, N} end size(a::FakeArray) = a.sz strides(a::FakeArray) = a.st -unsafe_convert{T}(::Type{Ptr{T}}, a::FakeArray{T}) = convert(Ptr{T}, C_NULL) +unsafe_convert(::Type{Ptr{T}}, a::FakeArray{T}) where {T} = convert(Ptr{T}, C_NULL) pointer{T}(a::FakeArray{T}) = convert(Ptr{T}, C_NULL) FakeArray{T, N}(::Type{T}, sz::NTuple{N, Int}) = FakeArray{T, N}(sz, colmajorstrides(sz)) @@ -299,14 +299,14 @@ function sprint_plan(plan::FFTWPlan) return str end -function show{T,K,inplace}(io::IO, p::cFFTWPlan{T,K,inplace}) +function show(io::IO, p::cFFTWPlan{T,K,inplace}) where {T,K,inplace} print(io, inplace ? "FFTW in-place " : "FFTW ", K < 0 ? "forward" : "backward", " plan for ") showfftdims(io, p.sz, p.istride, T) version >= v"3.3.4" && print(io, "\n", sprint_plan(p)) end -function show{T,K,inplace}(io::IO, p::rFFTWPlan{T,K,inplace}) +function show(io::IO, p::rFFTWPlan{T,K,inplace}) where {T,K,inplace} print(io, inplace ? "FFTW in-place " : "FFTW ", K < 0 ? "real-to-complex" : "complex-to-real", " plan for ") @@ -314,7 +314,7 @@ function show{T,K,inplace}(io::IO, p::rFFTWPlan{T,K,inplace}) version >= v"3.3.4" && print(io, "\n", sprint_plan(p)) end -function show{T,K,inplace}(io::IO, p::r2rFFTWPlan{T,K,inplace}) +function show(io::IO, p::r2rFFTWPlan{T,K,inplace}) where {T,K,inplace} print(io, inplace ? "FFTW in-place r2r " : "FFTW r2r ") if isempty(K) print(io, "0-dimensional") @@ -618,20 +618,20 @@ for (f,direction) in ((:fft,FORWARD), (:bfft,BACKWARD)) end end -function A_mul_B!{T}(y::StridedArray{T}, p::cFFTWPlan{T}, x::StridedArray{T}) +function A_mul_B!(y::StridedArray{T}, p::cFFTWPlan{T}, x::StridedArray{T}) where T assert_applicable(p, x, y) unsafe_execute!(p, x, y) return y end -function *{T,K,N}(p::cFFTWPlan{T,K,false}, x::StridedArray{T,N}) +function *(p::cFFTWPlan{T,K,false}, x::StridedArray{T,N}) where {T,K,N} assert_applicable(p, x) y = Array{T}(p.osz)::Array{T,N} unsafe_execute!(p, x, y) return y end -function *{T,K}(p::cFFTWPlan{T,K,true}, x::StridedArray{T}) +function *(p::cFFTWPlan{T,K,true}, x::StridedArray{T}) where {T,K} assert_applicable(p, x) unsafe_execute!(p, x, x) return x @@ -781,20 +781,20 @@ function plan_inv{T<:fftwNumber,K,inplace,N}(p::r2rFFTWPlan{T,K,inplace,N}) 1:length(iK))) end -function A_mul_B!{T}(y::StridedArray{T}, p::r2rFFTWPlan{T}, x::StridedArray{T}) +function A_mul_B!(y::StridedArray{T}, p::r2rFFTWPlan{T}, x::StridedArray{T}) where T assert_applicable(p, x, y) unsafe_execute!(p, x, y) return y end -function *{T,K,N}(p::r2rFFTWPlan{T,K,false}, x::StridedArray{T,N}) +function *(p::r2rFFTWPlan{T,K,false}, x::StridedArray{T,N}) where {T,K,N} assert_applicable(p, x) y = Array{T}(p.osz)::Array{T,N} unsafe_execute!(p, x, y) return y end -function *{T,K}(p::r2rFFTWPlan{T,K,true}, x::StridedArray{T}) +function *(p::r2rFFTWPlan{T,K,true}, x::StridedArray{T}) where {T,K} assert_applicable(p, x) unsafe_execute!(p, x, x) return x diff --git a/base/fft/dct.jl b/base/fft/dct.jl index 6ce51e5ab25c3..d548c265fbc49 100644 --- a/base/fft/dct.jl +++ b/base/fft/dct.jl @@ -20,7 +20,7 @@ end size(p::DCTPlan) = size(p.plan) -function show{T,K,inplace}(io::IO, p::DCTPlan{T,K,inplace}) +function show(io::IO, p::DCTPlan{T,K,inplace}) where {T,K,inplace} print(io, inplace ? "FFTW in-place " : "FFTW ", K == REDFT10 ? "DCT (DCT-II)" : "IDCT (DCT-III)", " plan for ") showfftdims(io, p.plan.sz, p.plan.istride, eltype(p)) @@ -94,10 +94,10 @@ function A_mul_B!{T}(y::StridedArray{T}, p::DCTPlan{T,REDFT01}, return y end -*{T}(p::DCTPlan{T,REDFT10,false}, x::StridedArray{T}) = +*(p::DCTPlan{T,REDFT10,false}, x::StridedArray{T}) where {T} = A_mul_B!(Array{T}(p.plan.osz), p, x) -*{T}(p::DCTPlan{T,REDFT01,false}, x::StridedArray{T}) = +*(p::DCTPlan{T,REDFT01,false}, x::StridedArray{T}) where {T} = A_mul_B!(Array{T}(p.plan.osz), p, copy(x)) # need copy to preserve input -*{T,K}(p::DCTPlan{T,K,true}, x::StridedArray{T}) = A_mul_B!(x, p, x) +*(p::DCTPlan{T,K,true}, x::StridedArray{T}) where {T,K} = A_mul_B!(x, p, x) diff --git a/base/generator.jl b/base/generator.jl index 53d69e9adde54..378419c13b6ea 100644 --- a/base/generator.jl +++ b/base/generator.jl @@ -109,10 +109,10 @@ iteratoreltype(x) = iteratoreltype(typeof(x)) iteratoreltype(::Type) = HasEltype() # HasEltype is the default iteratorsize(::Type{<:AbstractArray}) = HasShape() -iteratorsize{I,F}(::Type{Generator{I,F}}) = iteratorsize(I) +iteratorsize(::Type{Generator{I,F}}) where {I,F} = iteratorsize(I) length(g::Generator) = length(g.iter) size(g::Generator) = size(g.iter) indices(g::Generator) = indices(g.iter) ndims(g::Generator) = ndims(g.iter) -iteratoreltype{I,T}(::Type{Generator{I,T}}) = EltypeUnknown() +iteratoreltype(::Type{Generator{I,T}}) where {I,T} = EltypeUnknown() diff --git a/base/int.jl b/base/int.jl index 88b20efc85995..3c1084b2dc7e9 100644 --- a/base/int.jl +++ b/base/int.jl @@ -25,17 +25,17 @@ const BitUnsigned64T = Union{Type{UInt8}, Type{UInt16}, Type{UInt32}, Type{UInt6 ## integer comparisons ## -<{T<:BitSigned}(x::T, y::T) = slt_int(x, y) +(<)(x::T, y::T) where {T<:BitSigned} = slt_int(x, y) --(x::BitInteger) = neg_int(x) --{T<:BitInteger}(x::T, y::T) = sub_int(x, y) -+{T<:BitInteger}(x::T, y::T) = add_int(x, y) -*{T<:BitInteger}(x::T, y::T) = mul_int(x, y) +(-)(x::BitInteger) = neg_int(x) +(-)(x::T, y::T) where {T<:BitInteger} = sub_int(x, y) +(+)(x::T, y::T) where {T<:BitInteger} = add_int(x, y) +(*)(x::T, y::T) where {T<:BitInteger} = mul_int(x, y) inv(x::Integer) = float(one(x)) / float(x) -/{T<:Integer}(x::T, y::T) = float(x) / float(y) +(/)(x::T, y::T) where {T<:Integer} = float(x) / float(y) # skip promotion for system integer types -/(x::BitInteger, y::BitInteger) = float(x) / float(y) +(/)(x::BitInteger, y::BitInteger) = float(x) / float(y) """ isodd(x::Integer) -> Bool @@ -70,7 +70,7 @@ iseven(n::Integer) = !isodd(n) signbit(x::Integer) = x < 0 signbit(x::Unsigned) = false -flipsign{T<:BitSigned}(x::T, y::T) = flipsign_int(x, y) +flipsign(x::T, y::T) where {T<:BitSigned} = flipsign_int(x, y) flipsign(x::Signed, y::Signed) = convert(typeof(x), flipsign(promote_noncircular(x, y)...)) flipsign(x::Signed, y::Float16) = flipsign(x, bitcast(Int16, y)) @@ -166,38 +166,38 @@ julia> mod(-eps(), 3) 3.0 ``` """ -function mod{T<:Integer}(x::T, y::T) +function mod(x::T, y::T) where T<:Integer y == -1 && return T(0) # avoid potential overflow in fld return x - fld(x, y) * y end mod(x::Signed, y::Unsigned) = rem(y + unsigned(rem(x, y)), y) mod(x::Unsigned, y::Signed) = rem(y + signed(rem(x, y)), y) -mod{T<:Unsigned}(x::T, y::T) = rem(x, y) +mod(x::T, y::T) where {T<:Unsigned} = rem(x, y) cld(x::Signed, y::Unsigned) = div(x, y) + (!signbit(x) & (rem(x, y) != 0)) cld(x::Unsigned, y::Signed) = div(x, y) + (!signbit(y) & (rem(x, y) != 0)) # Don't promote integers for div/rem/mod since there is no danger of overflow, # while there is a substantial performance penalty to 64-bit promotion. -div{T<:BitSigned64}(x::T, y::T) = checked_sdiv_int(x, y) -rem{T<:BitSigned64}(x::T, y::T) = checked_srem_int(x, y) -div{T<:BitUnsigned64}(x::T, y::T) = checked_udiv_int(x, y) -rem{T<:BitUnsigned64}(x::T, y::T) = checked_urem_int(x, y) +div(x::T, y::T) where {T<:BitSigned64} = checked_sdiv_int(x, y) +rem(x::T, y::T) where {T<:BitSigned64} = checked_srem_int(x, y) +div(x::T, y::T) where {T<:BitUnsigned64} = checked_udiv_int(x, y) +rem(x::T, y::T) where {T<:BitUnsigned64} = checked_urem_int(x, y) # fld(x,y) == div(x,y) - ((x>=0) != (y>=0) && rem(x,y) != 0 ? 1 : 0) -fld{T<:Unsigned}(x::T, y::T) = div(x,y) -function fld{T<:Integer}(x::T, y::T) +fld(x::T, y::T) where {T<:Unsigned} = div(x,y) +function fld(x::T, y::T) where T<:Integer d = div(x, y) return d - (signbit(x ⊻ y) & (d * y != x)) end # cld(x,y) = div(x,y) + ((x>0) == (y>0) && rem(x,y) != 0 ? 1 : 0) -function cld{T<:Unsigned}(x::T, y::T) +function cld(x::T, y::T) where T<:Unsigned d = div(x, y) return d + (d * y != x) end -function cld{T<:Integer}(x::T, y::T) +function cld(x::T, y::T) where T<:Integer d = div(x, y) return d + (((x > 0) == (y > 0)) & (d * y != x)) end @@ -205,9 +205,9 @@ end ## integer bitwise operations ## (~)(x::BitInteger) = not_int(x) -(&){T<:BitInteger}(x::T, y::T) = and_int(x, y) -(|){T<:BitInteger}(x::T, y::T) = or_int(x, y) -xor{T<:BitInteger}(x::T, y::T) = xor_int(x, y) +(&)(x::T, y::T) where {T<:BitInteger} = and_int(x, y) +(|)(x::T, y::T) where {T<:BitInteger} = or_int(x, y) +xor(x::T, y::T) where {T<:BitInteger} = xor_int(x, y) bswap(x::Union{Int8, UInt8}) = x bswap(x::Union{Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128}) = @@ -287,9 +287,9 @@ trailing_ones(x::Integer) = trailing_zeros(~x) ## integer comparisons ## -<{T<:BitUnsigned}(x::T, y::T) = ult_int(x, y) -<={T<:BitSigned}(x::T, y::T) = sle_int(x, y) -<={T<:BitUnsigned}(x::T, y::T) = ule_int(x, y) +(< )(x::T, y::T) where {T<:BitUnsigned} = ult_int(x, y) +(<=)(x::T, y::T) where {T<:BitSigned} = sle_int(x, y) +(<=)(x::T, y::T) where {T<:BitUnsigned} = ule_int(x, y) ==(x::Signed, y::Unsigned) = (x >= 0) & (unsigned(x) == y) ==(x::Unsigned, y::Signed ) = (y >= 0) & (x == unsigned(y)) @@ -380,11 +380,11 @@ isdefined(Main, :Base) && for fname in (:mod, :rem) """ -> $fname(x::Integer, T::Type{<:Integer}) end -rem{T<:Integer}(x::T, ::Type{T}) = x +rem(x::T, ::Type{T}) where {T<:Integer} = x rem(x::Integer, ::Type{Bool}) = ((x & 1) != 0) -mod{T<:Integer}(x::Integer, ::Type{T}) = rem(x, T) +mod(x::Integer, ::Type{T}) where {T<:Integer} = rem(x, T) -unsafe_trunc{T<:Integer}(::Type{T}, x::Integer) = rem(x, T) +unsafe_trunc(::Type{T}, x::Integer) where {T<:Integer} = rem(x, T) for (Ts, Tu) in ((Int8, UInt8), (Int16, UInt16), (Int32, UInt32), (Int64, UInt64), (Int128, UInt128)) @eval convert(::Type{Signed}, x::$Tu) = convert($Ts, x) @eval convert(::Type{Unsigned}, x::$Ts) = convert($Tu, x) @@ -401,10 +401,10 @@ trunc(x::Integer) = x floor(x::Integer) = x ceil(x::Integer) = x -round{T<:Integer}(::Type{T}, x::Integer) = convert(T, x) -trunc{T<:Integer}(::Type{T}, x::Integer) = convert(T, x) -floor{T<:Integer}(::Type{T}, x::Integer) = convert(T, x) - ceil{T<:Integer}(::Type{T}, x::Integer) = convert(T, x) +round(::Type{T}, x::Integer) where {T<:Integer} = convert(T, x) +trunc(::Type{T}, x::Integer) where {T<:Integer} = convert(T, x) +floor(::Type{T}, x::Integer) where {T<:Integer} = convert(T, x) + ceil(::Type{T}, x::Integer) where {T<:Integer} = convert(T, x) ## integer construction ## @@ -564,7 +564,7 @@ if Core.sizeof(Int) == 4 return Int128(mod(BigInt(x), BigInt(y))) end else - *{T<:Union{Int128,UInt128}}(x::T, y::T) = mul_int(x, y) + *(x::T, y::T) where {T<:Union{Int128,UInt128}} = mul_int(x, y) div(x::Int128, y::Int128) = checked_sdiv_int(x, y) div(x::UInt128, y::UInt128) = checked_udiv_int(x, y) diff --git a/base/intfuncs.jl b/base/intfuncs.jl index 53b46e4f1ff5e..48de70ca99ac4 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -15,7 +15,7 @@ julia> gcd(6,-9) 3 ``` """ -function gcd{T<:Integer}(a::T, b::T) +function gcd(a::T, b::T) where T<:Integer while b != 0 t = b b = rem(a, b) @@ -26,7 +26,7 @@ end # binary GCD (aka Stein's) algorithm # about 1.7x (2.1x) faster for random Int64s (Int128s) -function gcd{T<:Union{Int64,UInt64,Int128,UInt128}}(a::T, b::T) +function gcd(a::T, b::T) where T<:Union{Int64,UInt64,Int128,UInt128} a == 0 && return abs(b) b == 0 && return abs(a) za = trailing_zeros(a) @@ -59,7 +59,7 @@ julia> lcm(-2,3) 6 ``` """ -function lcm{T<:Integer}(a::T, b::T) +function lcm(a::T, b::T) where T<:Integer # explicit a==0 test is to handle case of lcm(0,0) correctly if a == 0 return a @@ -107,7 +107,7 @@ julia> gcdx(240, 46) their `typemax`, and the identity then holds only via the unsigned integers' modulo arithmetic. """ -function gcdx{T<:Integer}(a::T, b::T) +function gcdx(a::T, b::T) where T<:Integer # a0, b0 = a, b s0, s1 = oneunit(T), zero(T) t0, t1 = s1, s0 @@ -142,7 +142,7 @@ julia> invmod(5,6) 5 ``` """ -function invmod{T<:Integer}(n::T, m::T) +function invmod(n::T, m::T) where T<:Integer g, x, y = gcdx(n, m) (g != 1 || m == 0) && throw(DomainError()) # Note that m might be negative here. @@ -191,7 +191,7 @@ function power_by_squaring(x::Bool, p::Integer) return (p==0) | x end -^{T<:Integer}(x::T, p::T) = power_by_squaring(x,p) +^(x::T, p::T) where {T<:Integer} = power_by_squaring(x,p) ^(x::Number, p::Integer) = power_by_squaring(x,p) ^(x, p::Integer) = power_by_squaring(x,p) @@ -202,7 +202,7 @@ end # We mark these @inline since if the target is marked @inline, # we want to make sure that gets propagated, # even if it is over the inlining threshold. -@inline literal_pow{p}(f, x, ::Type{Val{p}}) = f(x,p) +@inline literal_pow(f, x, ::Type{Val{p}}) where {p} = f(x,p) # Restrict inlining to hardware-supported arithmetic types, which # are fast enough to benefit from inlining. @@ -225,7 +225,7 @@ const HWNumber = Union{HWReal, Complex{<:HWReal}, Rational{<:HWReal}} Compute ``x^p \\pmod m``. """ -function powermod{T<:Integer}(x::Integer, p::Integer, m::T) +function powermod(x::Integer, p::Integer, m::T) where T<:Integer p < 0 && return powermod(invmod(x, m), -p, m) p == 0 && return mod(one(m),m) (m == 1 || m == -1) && return zero(m) @@ -542,9 +542,9 @@ Returns an array with element type `T` (default `Int`) of the digits of `n` in t base, optionally padded with zeros to a specified size. More significant digits are at higher indexes, such that `n == sum([digits[k]*base^(k-1) for k=1:length(digits)])`. """ -digits{T<:Integer}(n::Integer, base::T=10, pad::Integer=1) = digits(T, n, base, pad) +digits(n::Integer, base::T=10, pad::Integer=1) where {T<:Integer} = digits(T, n, base, pad) -function digits{T<:Integer}(::Type{T}, n::Integer, base::Integer=10, pad::Integer=1) +function digits(::Type{T}, n::Integer, base::Integer=10, pad::Integer=1) where T<:Integer 2 <= base || throw(ArgumentError("base must be ≥ 2, got $base")) digits!(zeros(T, max(pad, ndigits0z(n,base))), n, base) end @@ -556,7 +556,7 @@ Fills an array of the digits of `n` in the given base. More significant digits a indexes. If the array length is insufficient, the least significant digits are filled up to the array length. If the array length is excessive, the excess portion is filled with zeros. """ -function digits!{T<:Integer}(a::AbstractArray{T,1}, n::Integer, base::Integer=10) +function digits!(a::AbstractArray{T,1}, n::Integer, base::Integer=10) where T<:Integer 2 <= base || throw(ArgumentError("base must be ≥ 2, got $base")) base - 1 <= typemax(T) || throw(ArgumentError("type $T too small for base $base")) for i in eachindex(a) @@ -602,7 +602,7 @@ end Number of ways to choose `k` out of `n` items. """ -function binomial{T<:Integer}(n::T, k::T) +function binomial(n::T, k::T) where T<:Integer k < 0 && return zero(T) sgn = one(T) if n < 0 diff --git a/base/iobuffer.jl b/base/iobuffer.jl index dd061652a3785..496977a0faee0 100644 --- a/base/iobuffer.jl +++ b/base/iobuffer.jl @@ -107,7 +107,7 @@ function unsafe_read(from::AbstractIOBuffer, p::Ptr{UInt8}, nb::UInt) nothing end -function read_sub{T}(from::AbstractIOBuffer, a::AbstractArray{T}, offs, nel) +function read_sub(from::AbstractIOBuffer, a::AbstractArray{T}, offs, nel) where T from.readable || throw(ArgumentError("read failed, IOBuffer is not readable")) if offs+nel-1 > length(a) || offs < 1 || nel < 0 throw(BoundsError()) diff --git a/base/irrationals.jl b/base/irrationals.jl index f02be49e83524..c9fd585e79f94 100644 --- a/base/irrationals.jl +++ b/base/irrationals.jl @@ -4,18 +4,18 @@ struct Irrational{sym} <: Real end -show{sym}(io::IO, x::Irrational{sym}) = print(io, "$sym = $(string(float(x))[1:15])...") +show(io::IO, x::Irrational{sym}) where {sym} = print(io, "$sym = $(string(float(x))[1:15])...") promote_rule(::Type{<:Irrational}, ::Type{Float16}) = Float16 promote_rule(::Type{<:Irrational}, ::Type{Float32}) = Float32 promote_rule(::Type{<:Irrational}, ::Type{<:Irrational}) = Float64 -promote_rule{T<:Number}(::Type{<:Irrational}, ::Type{T}) = promote_type(Float64, T) +promote_rule(::Type{<:Irrational}, ::Type{T}) where {T<:Number} = promote_type(Float64, T) convert(::Type{AbstractFloat}, x::Irrational) = Float64(x) convert(::Type{Float16}, x::Irrational) = Float16(Float32(x)) convert(::Type{Complex{T}}, x::Irrational) where {T<:Real} = convert(Complex{T}, convert(T,x)) -@pure function convert{T<:Integer}(::Type{Rational{T}}, x::Irrational) +@pure function convert(::Type{Rational{T}}, x::Irrational) where T<:Integer o = precision(BigFloat) p = 256 while true @@ -31,13 +31,13 @@ convert(::Type{Complex{T}}, x::Irrational) where {T<:Real} = convert(Complex{T}, end convert(::Type{Rational{BigInt}}, x::Irrational) = throw(ArgumentError("Cannot convert an Irrational to a Rational{BigInt}: use rationalize(Rational{BigInt}, x) instead")) -@pure function (t::Type{T}){T<:Union{Float32,Float64}}(x::Irrational, r::RoundingMode) +@pure function (t::Type{T})(x::Irrational, r::RoundingMode) where T<:Union{Float32,Float64} setprecision(BigFloat, 256) do T(BigFloat(x), r) end end -=={s}(::Irrational{s}, ::Irrational{s}) = true +==(::Irrational{s}, ::Irrational{s}) where {s} = true ==(::Irrational, ::Irrational) = false # Irrationals, by definition, can't have a finite representation equal them exactly @@ -62,7 +62,7 @@ end <=(x::AbstractFloat, y::Irrational) = x < y # Irrational vs Rational -@pure function rationalize{T<:Integer}(::Type{T}, x::Irrational; tol::Real=0) +@pure function rationalize(::Type{T}, x::Irrational; tol::Real=0) where T return rationalize(T, big(x), tol=tol) end @pure function lessrational(rx::Rational{<:Integer}, x::Irrational) @@ -70,7 +70,7 @@ end # an irrational number required rounding up or down return rx < big(x) end -function <{T}(x::Irrational, y::Rational{T}) +function <(x::Irrational, y::Rational{T}) where T T <: Unsigned && x < 0.0 && return true rx = rationalize(T, x) if lessrational(rx, x) @@ -79,7 +79,7 @@ function <{T}(x::Irrational, y::Rational{T}) return rx <= y end end -function <{T}(x::Rational{T}, y::Irrational) +function <(x::Rational{T}, y::Irrational) where T T <: Unsigned && y < 0.0 && return false ry = rationalize(T, y) if lessrational(ry, y) diff --git a/base/iterators.jl b/base/iterators.jl index e9c2426079ff8..08fc609ced843 100644 --- a/base/iterators.jl +++ b/base/iterators.jl @@ -23,7 +23,7 @@ and_iteratorsize(::HasLength, ::HasShape) = HasLength() and_iteratorsize(::HasShape, ::HasLength) = HasLength() and_iteratorsize(a, b) = SizeUnknown() -and_iteratoreltype{T}(iel::T, ::T) = iel +and_iteratoreltype(iel::T, ::T) where {T} = iel and_iteratoreltype(a, b) = EltypeUnknown() # enumerate @@ -67,8 +67,8 @@ end eltype(::Type{Enumerate{I}}) where {I} = Tuple{Int, eltype(I)} -iteratorsize{I}(::Type{Enumerate{I}}) = iteratorsize(I) -iteratoreltype{I}(::Type{Enumerate{I}}) = iteratoreltype(I) +iteratorsize(::Type{Enumerate{I}}) where {I} = iteratorsize(I) +iteratoreltype(::Type{Enumerate{I}}) where {I} = iteratoreltype(I) struct IndexValue{I,A<:AbstractArray} data::A @@ -137,8 +137,8 @@ end eltype(::Type{IndexValue{I,A}}) where {I,A} = Tuple{eltype(I), eltype(A)} -iteratorsize{I}(::Type{IndexValue{I}}) = iteratorsize(I) -iteratoreltype{I}(::Type{IndexValue{I}}) = iteratoreltype(I) +iteratorsize(::Type{IndexValue{I}}) where {I} = iteratorsize(I) +iteratoreltype(::Type{IndexValue{I}}) where {I} = iteratoreltype(I) # zip @@ -166,8 +166,8 @@ eltype(::Type{Zip1{I}}) where {I} = Tuple{eltype(I)} end @inline done(z::Zip1, st) = done(z.a,st) -iteratorsize{I}(::Type{Zip1{I}}) = iteratorsize(I) -iteratoreltype{I}(::Type{Zip1{I}}) = iteratoreltype(I) +iteratorsize(::Type{Zip1{I}}) where {I} = iteratorsize(I) +iteratoreltype(::Type{Zip1{I}}) where {I} = iteratoreltype(I) struct Zip2{I1, I2} <: AbstractZipIterator a::I1 @@ -186,8 +186,8 @@ eltype(::Type{Zip2{I1,I2}}) where {I1,I2} = Tuple{eltype(I1), eltype(I2)} end @inline done(z::Zip2, st) = done(z.a,st[1]) | done(z.b,st[2]) -iteratorsize{I1,I2}(::Type{Zip2{I1,I2}}) = zip_iteratorsize(iteratorsize(I1),iteratorsize(I2)) -iteratoreltype{I1,I2}(::Type{Zip2{I1,I2}}) = and_iteratoreltype(iteratoreltype(I1),iteratoreltype(I2)) +iteratorsize(::Type{Zip2{I1,I2}}) where {I1,I2} = zip_iteratorsize(iteratorsize(I1),iteratorsize(I2)) +iteratoreltype(::Type{Zip2{I1,I2}}) where {I1,I2} = and_iteratoreltype(iteratoreltype(I1),iteratoreltype(I2)) struct Zip{I, Z<:AbstractZipIterator} <: AbstractZipIterator a::I @@ -237,8 +237,8 @@ eltype(::Type{Zip{I,Z}}) where {I,Z} = tuple_type_cons(eltype(I), eltype(Z)) end @inline done(z::Zip, st) = done(z.a,st[1]) | done(z.z,st[2]) -iteratorsize{I1,I2}(::Type{Zip{I1,I2}}) = zip_iteratorsize(iteratorsize(I1),iteratorsize(I2)) -iteratoreltype{I1,I2}(::Type{Zip{I1,I2}}) = and_iteratoreltype(iteratoreltype(I1),iteratoreltype(I2)) +iteratorsize(::Type{Zip{I1,I2}}) where {I1,I2} = zip_iteratorsize(iteratorsize(I1),iteratorsize(I2)) +iteratoreltype(::Type{Zip{I1,I2}}) where {I1,I2} = and_iteratoreltype(iteratoreltype(I1),iteratoreltype(I2)) # filter @@ -278,7 +278,7 @@ end done(f::Filter, s) = s[1] eltype(::Type{Filter{F,I}}) where {F,I} = eltype(I) -iteratoreltype{F,I}(::Type{Filter{F,I}}) = iteratoreltype(I) +iteratoreltype(::Type{Filter{F,I}}) where {F,I} = iteratoreltype(I) iteratorsize(::Type{<:Filter}) = SizeUnknown() # Rest -- iterate starting at the given state @@ -300,10 +300,10 @@ next(i::Rest, st) = next(i.itr, st) done(i::Rest, st) = done(i.itr, st) eltype(::Type{Rest{I}}) where {I} = eltype(I) -iteratoreltype{I,S}(::Type{Rest{I,S}}) = iteratoreltype(I) +iteratoreltype(::Type{Rest{I,S}}) where {I,S} = iteratoreltype(I) rest_iteratorsize(a) = SizeUnknown() rest_iteratorsize(::IsInfinite) = IsInfinite() -iteratorsize{I,S}(::Type{Rest{I,S}}) = rest_iteratorsize(iteratorsize(I)) +iteratorsize(::Type{Rest{I,S}}) where {I,S} = rest_iteratorsize(iteratorsize(I)) # Count -- infinite counting @@ -366,10 +366,10 @@ take(xs, n::Integer) = Take(xs, Int(n)) take(xs::Take, n::Integer) = Take(xs.xs, min(Int(n), xs.n)) eltype(::Type{Take{I}}) where {I} = eltype(I) -iteratoreltype{I}(::Type{Take{I}}) = iteratoreltype(I) +iteratoreltype(::Type{Take{I}}) where {I} = iteratoreltype(I) take_iteratorsize(a) = HasLength() take_iteratorsize(::SizeUnknown) = SizeUnknown() -iteratorsize{I}(::Type{Take{I}}) = take_iteratorsize(iteratorsize(I)) +iteratorsize(::Type{Take{I}}) where {I} = take_iteratorsize(iteratorsize(I)) length(t::Take) = _min_length(t.xs, 1:t.n, iteratorsize(t.xs), HasLength()) start(it::Take) = (it.n, start(it.xs)) @@ -421,11 +421,11 @@ drop(xs::Take, n::Integer) = Take(drop(xs.xs, Int(n)), max(0, xs.n - Int(n))) drop(xs::Drop, n::Integer) = Drop(xs.xs, Int(n) + xs.n) eltype(::Type{Drop{I}}) where {I} = eltype(I) -iteratoreltype{I}(::Type{Drop{I}}) = iteratoreltype(I) +iteratoreltype(::Type{Drop{I}}) where {I} = iteratoreltype(I) drop_iteratorsize(::SizeUnknown) = SizeUnknown() drop_iteratorsize(::Union{HasShape, HasLength}) = HasLength() drop_iteratorsize(::IsInfinite) = IsInfinite() -iteratorsize{I}(::Type{Drop{I}}) = drop_iteratorsize(iteratorsize(I)) +iteratorsize(::Type{Drop{I}}) where {I} = drop_iteratorsize(iteratorsize(I)) length(d::Drop) = _diff_length(d.xs, 1:d.n, iteratorsize(d.xs), HasLength()) function start(it::Drop) @@ -457,8 +457,8 @@ An iterator that cycles through `iter` forever. cycle(xs) = Cycle(xs) eltype(::Type{Cycle{I}}) where {I} = eltype(I) -iteratoreltype{I}(::Type{Cycle{I}}) = iteratoreltype(I) -iteratorsize{I}(::Type{Cycle{I}}) = IsInfinite() +iteratoreltype(::Type{Cycle{I}}) where {I} = iteratoreltype(I) +iteratorsize(::Type{Cycle{I}}) where {I} = IsInfinite() function start(it::Cycle) s = start(it.xs) @@ -563,8 +563,8 @@ indices(p::Prod1) = _prod_indices(p.a, iteratorsize(p.a)) end @inline done(p::Prod1, st) = done(p.a, st) -iteratoreltype{I}(::Type{Prod1{I}}) = iteratoreltype(I) -iteratorsize{I}(::Type{Prod1{I}}) = iteratorsize(I) +iteratoreltype(::Type{Prod1{I}}) where {I} = iteratoreltype(I) +iteratorsize(::Type{Prod1{I}}) where {I} = iteratorsize(I) # two iterators struct Prod2{I1, I2} <: AbstractProdIterator @@ -590,8 +590,8 @@ product(a, b) = Prod2(a, b) eltype(::Type{Prod2{I1,I2}}) where {I1,I2} = Tuple{eltype(I1), eltype(I2)} -iteratoreltype{I1,I2}(::Type{Prod2{I1,I2}}) = and_iteratoreltype(iteratoreltype(I1),iteratoreltype(I2)) -iteratorsize{I1,I2}(::Type{Prod2{I1,I2}}) = prod_iteratorsize(iteratorsize(I1),iteratorsize(I2)) +iteratoreltype(::Type{Prod2{I1,I2}}) where {I1,I2} = and_iteratoreltype(iteratoreltype(I1),iteratoreltype(I2)) +iteratorsize(::Type{Prod2{I1,I2}}) where {I1,I2} = prod_iteratorsize(iteratorsize(I1),iteratorsize(I2)) function start(p::AbstractProdIterator) s1, s2 = start(p.a), start(p.b) @@ -627,8 +627,8 @@ product(a, b, c...) = Prod(a, product(b, c...)) eltype(::Type{Prod{I1,I2}}) where {I1,I2} = tuple_type_cons(eltype(I1), eltype(I2)) -iteratoreltype{I1,I2}(::Type{Prod{I1,I2}}) = and_iteratoreltype(iteratoreltype(I1),iteratoreltype(I2)) -iteratorsize{I1,I2}(::Type{Prod{I1,I2}}) = prod_iteratorsize(iteratorsize(I1),iteratorsize(I2)) +iteratoreltype(::Type{Prod{I1,I2}}) where {I1,I2} = and_iteratoreltype(iteratoreltype(I1),iteratoreltype(I2)) +iteratorsize(::Type{Prod{I1,I2}}) where {I1,I2} = prod_iteratorsize(iteratorsize(I1),iteratorsize(I2)) @inline function next{I1,I2}(p::Prod{I1,I2}, st) x = prod_next(p, st) @@ -668,8 +668,8 @@ julia> collect(Iterators.flatten((1:2, 8:9))) flatten(itr) = Flatten(itr) eltype(::Type{Flatten{I}}) where {I} = eltype(eltype(I)) -iteratorsize{I}(::Type{Flatten{I}}) = SizeUnknown() -iteratoreltype{I}(::Type{Flatten{I}}) = _flatteneltype(I, iteratoreltype(I)) +iteratorsize(::Type{Flatten{I}}) where {I} = SizeUnknown() +iteratoreltype(::Type{Flatten{I}}) where {I} = _flatteneltype(I, iteratoreltype(I)) _flatteneltype(I, ::HasEltype) = iteratoreltype(eltype(I)) _flatteneltype(I, et) = EltypeUnknown() @@ -717,7 +717,7 @@ julia> collect(Iterators.partition([1,2,3,4,5], 2)) [5] ``` """ -partition{T}(c::T, n::Integer) = PartitionIterator{T}(c, Int(n)) +partition(c::T, n::Integer) where {T} = PartitionIterator{T}(c, Int(n)) mutable struct PartitionIterator{T} diff --git a/base/libgit2/config.jl b/base/libgit2/config.jl index 7c01664042cc4..dcb4045c797e9 100644 --- a/base/libgit2/config.jl +++ b/base/libgit2/config.jl @@ -82,7 +82,7 @@ function get(::Type{Int64}, c::GitConfig, name::AbstractString) return val_ptr[] end -function get{T}(c::GitConfig, name::AbstractString, default::T) +function get(c::GitConfig, name::AbstractString, default::T) where T res = default try res = get(T,c,name) end return res diff --git a/base/linalg/arnoldi.jl b/base/linalg/arnoldi.jl index 935d6fd79ea2a..e01e3ff31987f 100644 --- a/base/linalg/arnoldi.jl +++ b/base/linalg/arnoldi.jl @@ -315,7 +315,7 @@ function SVDOperator(A::AbstractMatrix{T}) where T SVDOperator{Tnew,typeof(Anew)}(Anew) end -function A_mul_B!{T}(u::StridedVector{T}, s::SVDOperator{T}, v::StridedVector{T}) +function A_mul_B!(u::StridedVector{T}, s::SVDOperator{T}, v::StridedVector{T}) where T a, b = s.m, length(v) A_mul_B!(view(u,1:a), s.X, view(v,a+1:b)) # left singular vector Ac_mul_B!(view(u,a+1:b), s.X, view(v,1:a)) # right singular vector diff --git a/base/linalg/bidiag.jl b/base/linalg/bidiag.jl index ae566dda9a5c1..9448bec2dee09 100644 --- a/base/linalg/bidiag.jl +++ b/base/linalg/bidiag.jl @@ -144,7 +144,7 @@ julia> Bidiagonal(A, false) #contains the main diagonal and first subdiagonal of """ Bidiagonal(A::AbstractMatrix, isupper::Bool)=Bidiagonal(diag(A), diag(A, isupper?1:-1), isupper) -function getindex{T}(A::Bidiagonal{T}, i::Integer, j::Integer) +function getindex(A::Bidiagonal{T}, i::Integer, j::Integer) where T if !((1 <= i <= size(A,2)) && (1 <= j <= size(A,2))) throw(BoundsError(A,(i,j))) end @@ -182,7 +182,7 @@ function Base.replace_in_print_matrix(A::Bidiagonal,i::Integer,j::Integer,s::Abs end #Converting from Bidiagonal to dense Matrix -function convert{T}(::Type{Matrix{T}}, A::Bidiagonal) +function convert(::Type{Matrix{T}}, A::Bidiagonal) where T n = size(A, 1) B = zeros(T, n, n) for i = 1:n - 1 @@ -196,29 +196,29 @@ function convert{T}(::Type{Matrix{T}}, A::Bidiagonal) B[n,n] = A.dv[n] return B end -convert{T}(::Type{Matrix}, A::Bidiagonal{T}) = convert(Matrix{T}, A) +convert(::Type{Matrix}, A::Bidiagonal{T}) where {T} = convert(Matrix{T}, A) convert(::Type{Array}, A::Bidiagonal) = convert(Matrix, A) full(A::Bidiagonal) = convert(Array, A) -promote_rule{T,S}(::Type{Matrix{T}}, ::Type{Bidiagonal{S}})=Matrix{promote_type(T,S)} +promote_rule(::Type{Matrix{T}}, ::Type{Bidiagonal{S}}) where {T,S} = Matrix{promote_type(T,S)} #Converting from Bidiagonal to Tridiagonal Tridiagonal(M::Bidiagonal{T}) where {T} = convert(Tridiagonal{T}, M) -function convert{T}(::Type{Tridiagonal{T}}, A::Bidiagonal) +function convert(::Type{Tridiagonal{T}}, A::Bidiagonal) where T z = zeros(T, size(A)[1]-1) A.isupper ? Tridiagonal(z, convert(Vector{T},A.dv), convert(Vector{T},A.ev)) : Tridiagonal(convert(Vector{T},A.ev), convert(Vector{T},A.dv), z) end -promote_rule{T,S}(::Type{Tridiagonal{T}}, ::Type{Bidiagonal{S}})=Tridiagonal{promote_type(T,S)} +promote_rule(::Type{Tridiagonal{T}}, ::Type{Bidiagonal{S}}) where {T,S} = Tridiagonal{promote_type(T,S)} # No-op for trivial conversion Bidiagonal{T} -> Bidiagonal{T} -convert{T}(::Type{Bidiagonal{T}}, A::Bidiagonal{T}) = A +convert(::Type{Bidiagonal{T}}, A::Bidiagonal{T}) where {T} = A # Convert Bidiagonal to Bidiagonal{T} by constructing a new instance with converted elements -convert{T}(::Type{Bidiagonal{T}}, A::Bidiagonal) = Bidiagonal(convert(Vector{T}, A.dv), convert(Vector{T}, A.ev), A.isupper) +convert(::Type{Bidiagonal{T}}, A::Bidiagonal) where {T} = Bidiagonal(convert(Vector{T}, A.dv), convert(Vector{T}, A.ev), A.isupper) # When asked to convert Bidiagonal to AbstractMatrix{T}, preserve structure by converting to Bidiagonal{T} <: AbstractMatrix{T} -convert{T}(::Type{AbstractMatrix{T}}, A::Bidiagonal) = convert(Bidiagonal{T}, A) +convert(::Type{AbstractMatrix{T}}, A::Bidiagonal) where {T} = convert(Bidiagonal{T}, A) broadcast(::typeof(big), B::Bidiagonal) = Bidiagonal(big.(B.dv), big.(B.ev), B.isupper) -similar{T}(B::Bidiagonal, ::Type{T}) = Bidiagonal{T}(similar(B.dv, T), similar(B.ev, T), B.isupper) +similar(B::Bidiagonal, ::Type{T}) where {T} = Bidiagonal{T}(similar(B.dv, T), similar(B.ev, T), B.isupper) ################### # LAPACK routines # @@ -327,7 +327,7 @@ function diag{T}(M::Bidiagonal{T}, n::Integer=0) end function +(A::Bidiagonal, B::Bidiagonal) - if A.isupper==B.isupper + if A.isupper == B.isupper Bidiagonal(A.dv+B.dv, A.ev+B.ev, A.isupper) else Tridiagonal((A.isupper ? (B.ev,A.dv+B.dv,A.ev) : (A.ev,A.dv+B.dv,B.ev))...) @@ -335,7 +335,7 @@ function +(A::Bidiagonal, B::Bidiagonal) end function -(A::Bidiagonal, B::Bidiagonal) - if A.isupper==B.isupper + if A.isupper == B.isupper Bidiagonal(A.dv-B.dv, A.ev-B.ev, A.isupper) else Tridiagonal((A.isupper ? (-B.ev,A.dv-B.dv,A.ev) : (A.ev,A.dv-B.dv,-B.ev))...) @@ -373,11 +373,11 @@ function check_A_mul_B!_sizes(C, A, B) nA, mA = size(A) nB, mB = size(B) nC, mC = size(C) - if !(nA == nC) + if nA != nC throw(DimensionMismatch("sizes size(A)=$(size(A)) and size(C) = $(size(C)) must match at first entry.")) - elseif !(mA == nB) + elseif mA != nB throw(DimensionMismatch("second entry of size(A)=$(size(A)) and first entry of size(B) = $(size(B)) must match.")) - elseif !(mB == mC) + elseif mB != mC throw(DimensionMismatch("sizes size(B)=$(size(B)) and size(C) = $(size(C)) must match at first second entry.")) end end diff --git a/base/linalg/bunchkaufman.jl b/base/linalg/bunchkaufman.jl index 8d6cbf8f32a21..5c52e6441dba6 100644 --- a/base/linalg/bunchkaufman.jl +++ b/base/linalg/bunchkaufman.jl @@ -77,11 +77,11 @@ bkfact{T}(A::StridedMatrix{T}, uplo::Symbol=:U, symmetric::Bool=issymmetric(A), bkfact!(convert(Matrix{promote_type(Float32, typeof(sqrt(one(T))))}, A), uplo, symmetric, rook) -convert{T}(::Type{BunchKaufman{T}}, B::BunchKaufman{T}) = B -convert{T}(::Type{BunchKaufman{T}}, B::BunchKaufman) = +convert(::Type{BunchKaufman{T}}, B::BunchKaufman{T}) where {T} = B +convert(::Type{BunchKaufman{T}}, B::BunchKaufman) where {T} = BunchKaufman(convert(Matrix{T}, B.LD), B.ipiv, B.uplo, B.symmetric, B.rook, B.info) -convert{T}(::Type{Factorization{T}}, B::BunchKaufman{T}) = B -convert{T}(::Type{Factorization{T}}, B::BunchKaufman) = convert(BunchKaufman{T}, B) +convert(::Type{Factorization{T}}, B::BunchKaufman{T}) where {T} = B +convert(::Type{Factorization{T}}, B::BunchKaufman) where {T} = convert(BunchKaufman{T}, B) size(B::BunchKaufman) = size(B.LD) size(B::BunchKaufman, d::Integer) = size(B.LD, d) diff --git a/base/linalg/cholesky.jl b/base/linalg/cholesky.jl index 5e4bdfd199b03..e9fb53b556023 100644 --- a/base/linalg/cholesky.jl +++ b/base/linalg/cholesky.jl @@ -386,17 +386,17 @@ function cholfact(x::Number, uplo::Symbol=:U) end -function convert{T}(::Type{Cholesky{T}}, C::Cholesky) +function convert(::Type{Cholesky{T}}, C::Cholesky) where T Cnew = convert(AbstractMatrix{T}, C.factors) Cholesky{T, typeof(Cnew)}(Cnew, C.uplo) end -convert{T}(::Type{Factorization{T}}, C::Cholesky{T}) = C -convert{T}(::Type{Factorization{T}}, C::Cholesky) = convert(Cholesky{T}, C) -convert{T}(::Type{CholeskyPivoted{T}},C::CholeskyPivoted{T}) = C -convert{T}(::Type{CholeskyPivoted{T}},C::CholeskyPivoted) = +convert(::Type{Factorization{T}}, C::Cholesky{T}) where {T} = C +convert(::Type{Factorization{T}}, C::Cholesky) where {T} = convert(Cholesky{T}, C) +convert(::Type{CholeskyPivoted{T}},C::CholeskyPivoted{T}) where {T} = C +convert(::Type{CholeskyPivoted{T}},C::CholeskyPivoted) where {T} = CholeskyPivoted(AbstractMatrix{T}(C.factors),C.uplo,C.piv,C.rank,C.tol,C.info) -convert{T}(::Type{Factorization{T}}, C::CholeskyPivoted{T}) = C -convert{T}(::Type{Factorization{T}}, C::CholeskyPivoted) = convert(CholeskyPivoted{T}, C) +convert(::Type{Factorization{T}}, C::CholeskyPivoted{T}) where {T} = C +convert(::Type{Factorization{T}}, C::CholeskyPivoted) where {T} = convert(CholeskyPivoted{T}, C) convert(::Type{AbstractMatrix}, C::Cholesky) = C.uplo == 'U' ? C[:U]'C[:U] : C[:L]*C[:L]' convert(::Type{AbstractArray}, C::Cholesky) = convert(AbstractMatrix, C) @@ -425,7 +425,7 @@ function getindex(C::Cholesky, d::Symbol) d == :UL && return Symbol(C.uplo) == :U ? UpperTriangular(C.factors) : LowerTriangular(C.factors) throw(KeyError(d)) end -function getindex{T<:BlasFloat}(C::CholeskyPivoted{T}, d::Symbol) +function getindex(C::CholeskyPivoted{T}, d::Symbol) where T<:BlasFloat d == :U && return UpperTriangular(Symbol(C.uplo) == d ? C.factors : C.factors') d == :L && return LowerTriangular(Symbol(C.uplo) == d ? C.factors : C.factors') d == :p && return C.piv diff --git a/base/linalg/diagonal.jl b/base/linalg/diagonal.jl index 421328e9a1044..c52ef944d96b4 100644 --- a/base/linalg/diagonal.jl +++ b/base/linalg/diagonal.jl @@ -48,14 +48,14 @@ julia> Diagonal(V) """ Diagonal(V::AbstractVector) = Diagonal(collect(V)) -convert{T}(::Type{Diagonal{T}}, D::Diagonal{T}) = D -convert{T}(::Type{Diagonal{T}}, D::Diagonal) = Diagonal{T}(convert(Vector{T}, D.diag)) -convert{T}(::Type{AbstractMatrix{T}}, D::Diagonal) = convert(Diagonal{T}, D) +convert(::Type{Diagonal{T}}, D::Diagonal{T}) where {T} = D +convert(::Type{Diagonal{T}}, D::Diagonal) where {T} = Diagonal{T}(convert(Vector{T}, D.diag)) +convert(::Type{AbstractMatrix{T}}, D::Diagonal) where {T} = convert(Diagonal{T}, D) convert(::Type{Matrix}, D::Diagonal) = diagm(D.diag) convert(::Type{Array}, D::Diagonal) = convert(Matrix, D) full(D::Diagonal) = convert(Array, D) -function similar{T}(D::Diagonal, ::Type{T}) +function similar(D::Diagonal, ::Type{T}) where T return Diagonal{T}(similar(D.diag, T)) end @@ -79,8 +79,8 @@ end end r end -diagzero{T}(::Diagonal{T},i,j) = zero(T) -diagzero{T}(D::Diagonal{Matrix{T}},i,j) = zeros(T, size(D.diag[i], 1), size(D.diag[j], 2)) +diagzero(::Diagonal{T},i,j) where {T} = zero(T) +diagzero(D::Diagonal{Matrix{T}},i,j) where {T} = zeros(T, size(D.diag[i], 1), size(D.diag[j], 2)) function setindex!(D::Diagonal, v, i::Int, j::Int) @boundscheck checkbounds(D, i, j) @@ -286,7 +286,7 @@ function logdet(D::Diagonal{<:Complex}) # make sure branch cut is correct complex(real(z), rem2pi(imag(z), RoundNearest)) end # identity matrices via eye(Diagonal{type},n) -eye{T}(::Type{Diagonal{T}}, n::Int) = Diagonal(ones(T,n)) +eye(::Type{Diagonal{T}}, n::Int) where {T} = Diagonal(ones(T,n)) expm(D::Diagonal) = Diagonal(exp.(D.diag)) expm(D::Diagonal{<:AbstractMatrix}) = Diagonal(expm.(D.diag)) diff --git a/base/linalg/factorization.jl b/base/linalg/factorization.jl index 7a3a2d881fc61..d160f2c3cd839 100644 --- a/base/linalg/factorization.jl +++ b/base/linalg/factorization.jl @@ -4,7 +4,7 @@ abstract type Factorization{T} end -eltype{T}(::Type{Factorization{T}}) = T +eltype(::Type{Factorization{T}}) where {T} = T transpose(F::Factorization) = error("transpose not implemented for $(typeof(F))") ctranspose(F::Factorization) = error("ctranspose not implemented for $(typeof(F))") @@ -27,7 +27,7 @@ function det(F::Factorization) end ### General promotion rules -convert{T}(::Type{Factorization{T}}, F::Factorization{T}) = F +convert(::Type{Factorization{T}}, F::Factorization{T}) where {T} = F inv{T}(F::Factorization{T}) = A_ldiv_B!(F, eye(T, size(F,1))) # With a real lhs and complex rhs with the same precision, we can reinterpret diff --git a/base/linalg/givens.jl b/base/linalg/givens.jl index dc98cfece3bd8..f372710dc9787 100644 --- a/base/linalg/givens.jl +++ b/base/linalg/givens.jl @@ -5,11 +5,11 @@ abstract type AbstractRotation{T} end transpose(R::AbstractRotation) = error("transpose not implemented for $(typeof(R)). Consider using conjugate transpose (') instead of transpose (.').") -function *{T,S}(R::AbstractRotation{T}, A::AbstractVecOrMat{S}) +function *(R::AbstractRotation{T}, A::AbstractVecOrMat{S}) where {T,S} TS = typeof(zero(T)*zero(S) + zero(T)*zero(S)) A_mul_B!(convert(AbstractRotation{TS}, R), TS == S ? copy(A) : convert(AbstractArray{TS}, A)) end -function A_mul_Bc{T,S}(A::AbstractVecOrMat{T}, R::AbstractRotation{S}) +function A_mul_Bc(A::AbstractVecOrMat{T}, R::AbstractRotation{S}) where {T,S} TS = typeof(zero(T)*zero(S) + zero(T)*zero(S)) A_mul_Bc!(TS == T ? copy(A) : convert(AbstractArray{TS}, A), convert(AbstractRotation{TS}, R)) end @@ -34,12 +34,12 @@ mutable struct Rotation{T} <: AbstractRotation{T} rotations::Vector{Givens{T}} end -convert{T}(::Type{Givens{T}}, G::Givens{T}) = G -convert{T}(::Type{Givens{T}}, G::Givens) = Givens(G.i1, G.i2, convert(T, G.c), convert(T, G.s)) -convert{T}(::Type{Rotation{T}}, R::Rotation{T}) = R -convert{T}(::Type{Rotation{T}}, R::Rotation) = Rotation{T}([convert(Givens{T}, g) for g in R.rotations]) -convert{T}(::Type{AbstractRotation{T}}, G::Givens) = convert(Givens{T}, G) -convert{T}(::Type{AbstractRotation{T}}, R::Rotation) = convert(Rotation{T}, R) +convert(::Type{Givens{T}}, G::Givens{T}) where {T} = G +convert(::Type{Givens{T}}, G::Givens) where {T} = Givens(G.i1, G.i2, convert(T, G.c), convert(T, G.s)) +convert(::Type{Rotation{T}}, R::Rotation{T}) where {T} = R +convert(::Type{Rotation{T}}, R::Rotation) where {T} = Rotation{T}([convert(Givens{T}, g) for g in R.rotations]) +convert(::Type{AbstractRotation{T}}, G::Givens) where {T} = convert(Givens{T}, G) +convert(::Type{AbstractRotation{T}}, R::Rotation) where {T} = convert(Rotation{T}, R) ctranspose(G::Givens) = Givens(G.i1, G.i2, conj(G.c), -G.s) ctranspose{T}(R::Rotation{T}) = Rotation{T}(reverse!([ctranspose(r) for r in R.rotations])) @@ -360,4 +360,4 @@ function A_mul_Bc!(A::AbstractMatrix, R::Rotation) end return A end -*{T}(G1::Givens{T}, G2::Givens{T}) = Rotation(push!(push!(Givens{T}[], G2), G1)) +*(G1::Givens{T}, G2::Givens{T}) where {T} = Rotation(push!(push!(Givens{T}[], G2), G1)) diff --git a/base/linalg/hessenberg.jl b/base/linalg/hessenberg.jl index 87743b87c0230..658338b009d07 100644 --- a/base/linalg/hessenberg.jl +++ b/base/linalg/hessenberg.jl @@ -48,7 +48,7 @@ julia> F[:Q] * F[:H] * F[:Q]' 4.0 3.0 2.0 ``` """ -function hessfact{T}(A::StridedMatrix{T}) +function hessfact(A::StridedMatrix{T}) where T S = promote_type(Float32, typeof(zero(T)/norm(one(T)))) return hessfact!(copy_oftype(A, S)) end @@ -87,29 +87,29 @@ convert(::Type{Matrix}, F::Hessenberg) = convert(Array, convert(AbstractArray, F convert(::Type{Array}, F::Hessenberg) = convert(Matrix, F) full(F::Hessenberg) = convert(AbstractArray, F) -A_mul_B!{T<:BlasFloat}(Q::HessenbergQ{T}, X::StridedVecOrMat{T}) = +A_mul_B!(Q::HessenbergQ{T}, X::StridedVecOrMat{T}) where {T<:BlasFloat} = LAPACK.ormhr!('L', 'N', 1, size(Q.factors, 1), Q.factors, Q.τ, X) -A_mul_B!{T<:BlasFloat}(X::StridedMatrix{T}, Q::HessenbergQ{T}) = +A_mul_B!(X::StridedMatrix{T}, Q::HessenbergQ{T}) where {T<:BlasFloat} = LAPACK.ormhr!('R', 'N', 1, size(Q.factors, 1), Q.factors, Q.τ, X) -Ac_mul_B!{T<:BlasFloat}(Q::HessenbergQ{T}, X::StridedVecOrMat{T}) = +Ac_mul_B!(Q::HessenbergQ{T}, X::StridedVecOrMat{T}) where {T<:BlasFloat} = LAPACK.ormhr!('L', ifelse(T<:Real, 'T', 'C'), 1, size(Q.factors, 1), Q.factors, Q.τ, X) -A_mul_Bc!{T<:BlasFloat}(X::StridedMatrix{T}, Q::HessenbergQ{T}) = +A_mul_Bc!(X::StridedMatrix{T}, Q::HessenbergQ{T}) where {T<:BlasFloat} = LAPACK.ormhr!('R', ifelse(T<:Real, 'T', 'C'), 1, size(Q.factors, 1), Q.factors, Q.τ, X) -function (*){T,S}(Q::HessenbergQ{T}, X::StridedVecOrMat{S}) +function (*)(Q::HessenbergQ{T}, X::StridedVecOrMat{S}) where {T,S} TT = typeof(zero(T)*zero(S) + zero(T)*zero(S)) return A_mul_B!(Q, copy_oftype(X, TT)) end -function (*){T,S}(X::StridedVecOrMat{S}, Q::HessenbergQ{T}) +function (*)(X::StridedVecOrMat{S}, Q::HessenbergQ{T}) where {T,S} TT = typeof(zero(T)*zero(S) + zero(T)*zero(S)) return A_mul_B!(copy_oftype(X, TT), Q) end -function Ac_mul_B{T,S}(Q::HessenbergQ{T}, X::StridedVecOrMat{S}) +function Ac_mul_B(Q::HessenbergQ{T}, X::StridedVecOrMat{S}) where {T,S} TT = typeof(zero(T)*zero(S) + zero(T)*zero(S)) return Ac_mul_B!(Q, copy_oftype(X, TT)) end -function A_mul_Bc{T,S}(X::StridedVecOrMat{S}, Q::HessenbergQ{T}) +function A_mul_Bc(X::StridedVecOrMat{S}, Q::HessenbergQ{T}) where {T,S} TT = typeof(zero(T)*zero(S) + zero(T)*zero(S)) return A_mul_Bc!(copy_oftype(X, TT), Q) end diff --git a/base/linalg/ldlt.jl b/base/linalg/ldlt.jl index 55260cc46142e..e4d1e0c1236b8 100644 --- a/base/linalg/ldlt.jl +++ b/base/linalg/ldlt.jl @@ -7,13 +7,13 @@ end size(S::LDLt) = size(S.data) size(S::LDLt, i::Integer) = size(S.data, i) -convert{T,S}(::Type{LDLt{T,S}}, F::LDLt) = LDLt{T,S}(convert(S, F.data)) +convert(::Type{LDLt{T,S}}, F::LDLt) where {T,S} = LDLt{T,S}(convert(S, F.data)) # NOTE: the annotaion <:AbstractMatrix shouldn't be necessary, it is introduced # to avoid an ambiguity warning (see issue #6383) -convert{T,S,U<:AbstractMatrix}(::Type{LDLt{T}}, F::LDLt{S,U}) = convert(LDLt{T,U}, F) +convert(::Type{LDLt{T}}, F::LDLt{S,U}) where {T,S,U<:AbstractMatrix} = convert(LDLt{T,U}, F) -convert{T}(::Type{Factorization{T}}, F::LDLt{T}) = F -convert{T,S,U}(::Type{Factorization{T}}, F::LDLt{S,U}) = convert(LDLt{T,U}, F) +convert(::Type{Factorization{T}}, F::LDLt{T}) where {T} = F +convert(::Type{Factorization{T}}, F::LDLt{S,U}) where {T,S,U} = convert(LDLt{T,U}, F) # SymTridiagonal """ diff --git a/base/linalg/lq.jl b/base/linalg/lq.jl index 6ee36ffadde3f..42844b41294f7 100644 --- a/base/linalg/lq.jl +++ b/base/linalg/lq.jl @@ -47,9 +47,9 @@ end copy(A::LQ) = LQ(copy(A.factors), copy(A.τ)) -convert{T}(::Type{LQ{T}},A::LQ) = LQ(convert(AbstractMatrix{T}, A.factors), convert(Vector{T}, A.τ)) -convert{T}(::Type{Factorization{T}}, A::LQ{T}) = A -convert{T}(::Type{Factorization{T}}, A::LQ) = convert(LQ{T}, A) +convert(::Type{LQ{T}},A::LQ) where {T} = LQ(convert(AbstractMatrix{T}, A.factors), convert(Vector{T}, A.τ)) +convert(::Type{Factorization{T}}, A::LQ{T}) where {T} = A +convert(::Type{Factorization{T}}, A::LQ) where {T} = convert(LQ{T}, A) convert(::Type{AbstractMatrix}, A::LQ) = A[:L]*A[:Q] convert(::Type{AbstractArray}, A::LQ) = convert(AbstractMatrix, A) convert(::Type{Matrix}, A::LQ) = convert(Array, convert(AbstractArray, A)) @@ -86,8 +86,8 @@ function show(io::IO, C::LQ) show(io, C[:Q]) end -convert{T}(::Type{LQPackedQ{T}}, Q::LQPackedQ) = LQPackedQ(convert(AbstractMatrix{T}, Q.factors), convert(Vector{T}, Q.τ)) -convert{T}(::Type{AbstractMatrix{T}}, Q::LQPackedQ) = convert(LQPackedQ{T}, Q) +convert(::Type{LQPackedQ{T}}, Q::LQPackedQ) where {T} = LQPackedQ(convert(AbstractMatrix{T}, Q.factors), convert(Vector{T}, Q.τ)) +convert(::Type{AbstractMatrix{T}}, Q::LQPackedQ) where {T} = convert(LQPackedQ{T}, Q) convert(::Type{Matrix}, A::LQPackedQ) = LAPACK.orglq!(copy(A.factors),A.τ) convert(::Type{Array}, A::LQPackedQ) = convert(Matrix, A) function full{T}(A::LQPackedQ{T}; thin::Bool = true) @@ -119,33 +119,33 @@ end size(A::LQPackedQ) = size(A.factors) ## Multiplication by LQ -A_mul_B!{T<:BlasFloat}(A::LQ{T}, B::StridedVecOrMat{T}) = A[:L]*LAPACK.ormlq!('L','N',A.factors,A.τ,B) -A_mul_B!{T<:BlasFloat}(A::LQ{T}, B::QR{T}) = A[:L]*LAPACK.ormlq!('L','N',A.factors,A.τ,full(B)) -A_mul_B!{T<:BlasFloat}(A::QR{T}, B::LQ{T}) = A_mul_B!(zeros(full(A)), full(A), full(B)) -function *{TA,TB}(A::LQ{TA},B::StridedVecOrMat{TB}) +A_mul_B!(A::LQ{T}, B::StridedVecOrMat{T}) where {T<:BlasFloat} = A[:L]*LAPACK.ormlq!('L','N',A.factors,A.τ,B) +A_mul_B!(A::LQ{T}, B::QR{T}) where {T<:BlasFloat} = A[:L]*LAPACK.ormlq!('L','N',A.factors,A.τ,full(B)) +A_mul_B!(A::QR{T}, B::LQ{T}) where {T<:BlasFloat} = A_mul_B!(zeros(full(A)), full(A), full(B)) +function *(A::LQ{TA},B::StridedVecOrMat{TB}) where {TA,TB} TAB = promote_type(TA, TB) A_mul_B!(convert(Factorization{TAB},A), copy_oftype(B, TAB)) end -function *{TA,TB}(A::LQ{TA},B::QR{TB}) +function *(A::LQ{TA},B::QR{TB}) where {TA,TB} TAB = promote_type(TA, TB) A_mul_B!(convert(Factorization{TAB},A), convert(Factorization{TAB},B)) end -function *{TA,TB}(A::QR{TA},B::LQ{TB}) +function *(A::QR{TA},B::LQ{TB}) where {TA,TB} TAB = promote_type(TA, TB) A_mul_B!(convert(Factorization{TAB},A), convert(Factorization{TAB},B)) end ## Multiplication by Q ### QB -A_mul_B!{T<:BlasFloat}(A::LQPackedQ{T}, B::StridedVecOrMat{T}) = LAPACK.ormlq!('L','N',A.factors,A.τ,B) +A_mul_B!(A::LQPackedQ{T}, B::StridedVecOrMat{T}) where {T<:BlasFloat} = LAPACK.ormlq!('L','N',A.factors,A.τ,B) function (*)(A::LQPackedQ,B::StridedVecOrMat) TAB = promote_type(eltype(A), eltype(B)) A_mul_B!(convert(AbstractMatrix{TAB}, A), copy_oftype(B, TAB)) end ### QcB -Ac_mul_B!{T<:BlasReal}(A::LQPackedQ{T}, B::StridedVecOrMat{T}) = LAPACK.ormlq!('L','T',A.factors,A.τ,B) -Ac_mul_B!{T<:BlasComplex}(A::LQPackedQ{T}, B::StridedVecOrMat{T}) = LAPACK.ormlq!('L','C',A.factors,A.τ,B) +Ac_mul_B!(A::LQPackedQ{T}, B::StridedVecOrMat{T}) where {T<:BlasReal} = LAPACK.ormlq!('L','T',A.factors,A.τ,B) +Ac_mul_B!(A::LQPackedQ{T}, B::StridedVecOrMat{T}) where {T<:BlasComplex} = LAPACK.ormlq!('L','C',A.factors,A.τ,B) function Ac_mul_B(A::LQPackedQ, B::StridedVecOrMat) TAB = promote_type(eltype(A), eltype(B)) if size(B,1) == size(A.factors,2) @@ -171,8 +171,8 @@ for (f1, f2) in ((:A_mul_Bc, :A_mul_B!), end ### AQ -A_mul_B!{T<:BlasFloat}(A::StridedMatrix{T}, B::LQPackedQ{T}) = LAPACK.ormlq!('R', 'N', B.factors, B.τ, A) -function *{TA,TB}(A::StridedMatrix{TA},B::LQPackedQ{TB}) +A_mul_B!(A::StridedMatrix{T}, B::LQPackedQ{T}) where {T<:BlasFloat} = LAPACK.ormlq!('R', 'N', B.factors, B.τ, A) +function *(A::StridedMatrix{TA},B::LQPackedQ{TB}) where {TA,TB} TAB = promote_type(TA,TB) if size(B.factors,2) == size(A,2) A_mul_B!(copy_oftype(A, TAB),convert(AbstractMatrix{TAB},B)) @@ -184,9 +184,9 @@ function *{TA,TB}(A::StridedMatrix{TA},B::LQPackedQ{TB}) end ### AQc -A_mul_Bc!{T<:BlasReal}(A::StridedMatrix{T}, B::LQPackedQ{T}) = LAPACK.ormlq!('R','T',B.factors,B.τ,A) -A_mul_Bc!{T<:BlasComplex}(A::StridedMatrix{T}, B::LQPackedQ{T}) = LAPACK.ormlq!('R','C',B.factors,B.τ,A) -function A_mul_Bc{TA<:Number,TB<:Number}( A::StridedVecOrMat{TA}, B::LQPackedQ{TB}) +A_mul_Bc!(A::StridedMatrix{T}, B::LQPackedQ{T}) where {T<:BlasReal} = LAPACK.ormlq!('R','T',B.factors,B.τ,A) +A_mul_Bc!(A::StridedMatrix{T}, B::LQPackedQ{T}) where {T<:BlasComplex} = LAPACK.ormlq!('R','C',B.factors,B.τ,A) +function A_mul_Bc( A::StridedVecOrMat{TA}, B::LQPackedQ{TB}) where {TA<:Number,TB<:Number} TAB = promote_type(TA,TB) A_mul_Bc!(copy_oftype(A, TAB), convert(AbstractMatrix{TAB},(B))) end @@ -204,7 +204,7 @@ for (f1, f2) in ((:Ac_mul_B, :A_mul_B!), end end -function \{TA,Tb}(A::LQ{TA}, b::StridedVector{Tb}) +function (\)(A::LQ{TA}, b::StridedVector{Tb}) where {TA,Tb} S = promote_type(TA,Tb) m = checksquare(A) m == length(b) || throw(DimensionMismatch("left hand side has $m rows, but right hand side has length $(length(b))")) @@ -212,7 +212,7 @@ function \{TA,Tb}(A::LQ{TA}, b::StridedVector{Tb}) x = A_ldiv_B!(AA, copy_oftype(b, S)) return x end -function \{TA,TB}(A::LQ{TA},B::StridedMatrix{TB}) +function (\)(A::LQ{TA},B::StridedMatrix{TB}) where {TA,TB} S = promote_type(TA,TB) m = checksquare(A) m == size(B,1) || throw(DimensionMismatch("left hand side has $m rows, but right hand side has $(size(B,1)) rows")) @@ -222,7 +222,7 @@ function \{TA,TB}(A::LQ{TA},B::StridedMatrix{TB}) end # With a real lhs and complex rhs with the same precision, we can reinterpret # the complex rhs as a real rhs with twice the number of columns -function (\){T<:BlasReal}(F::LQ{T}, B::VecOrMat{Complex{T}}) +function (\)(F::LQ{T}, B::VecOrMat{Complex{T}}) where T<:BlasReal c2r = reshape(transpose(reinterpret(T, B, (2, length(B)))), size(B, 1), 2*size(B, 2)) x = A_ldiv_B!(F, c2r) return reinterpret(Complex{T}, transpose(reshape(x, div(length(x), 2), 2)), diff --git a/base/linalg/lu.jl b/base/linalg/lu.jl index ff5d3202a8130..acedcc395ad72 100644 --- a/base/linalg/lu.jl +++ b/base/linalg/lu.jl @@ -190,13 +190,13 @@ function lu(A::AbstractMatrix, pivot::Union{Type{Val{false}}, Type{Val{true}}} = F[:L], F[:U], F[:p] end -function convert{T}(::Type{LU{T}}, F::LU) +function convert(::Type{LU{T}}, F::LU) where T M = convert(AbstractMatrix{T}, F.factors) LU{T,typeof(M)}(M, F.ipiv, F.info) end -convert{T,S}(::Type{LU{T,S}}, F::LU) = LU{T,S}(convert(S, F.factors), F.ipiv, F.info) -convert{T}(::Type{Factorization{T}}, F::LU{T}) = F -convert{T}(::Type{Factorization{T}}, F::LU) = convert(LU{T}, F) +convert(::Type{LU{T,S}}, F::LU) where {T,S} = LU{T,S}(convert(S, F.factors), F.ipiv, F.info) +convert(::Type{Factorization{T}}, F::LU{T}) where {T} = F +convert(::Type{Factorization{T}}, F::LU) where {T} = convert(LU{T}, F) size(A::LU) = size(A.factors) @@ -383,7 +383,7 @@ end factorize(A::Tridiagonal) = lufact(A) -function getindex{T}(F::Base.LinAlg.LU{T,Tridiagonal{T}}, d::Symbol) +function getindex(F::Base.LinAlg.LU{T,Tridiagonal{T}}, d::Symbol) where T m, n = size(F) if d == :L L = Array(Bidiagonal(ones(T, n), F.factors.dl, false)) @@ -517,7 +517,7 @@ convert(::Type{Matrix}, F::LU) = convert(Array, convert(AbstractArray, F)) convert(::Type{Array}, F::LU) = convert(Matrix, F) full(F::LU) = convert(AbstractArray, F) -function convert{T}(::Type{Tridiagonal}, F::Base.LinAlg.LU{T,Tridiagonal{T}}) +function convert(::Type{Tridiagonal}, F::Base.LinAlg.LU{T,Tridiagonal{T}}) where T n = size(F, 1) dl = copy(F.factors.dl) @@ -551,12 +551,12 @@ function convert{T}(::Type{Tridiagonal}, F::Base.LinAlg.LU{T,Tridiagonal{T}}) end return Tridiagonal(dl, d, du) end -convert{T}(::Type{AbstractMatrix}, F::Base.LinAlg.LU{T,Tridiagonal{T}}) = +convert(::Type{AbstractMatrix}, F::Base.LinAlg.LU{T,Tridiagonal{T}}) where {T} = convert(Tridiagonal, F) -convert{T}(::Type{AbstractArray}, F::Base.LinAlg.LU{T,Tridiagonal{T}}) = +convert(::Type{AbstractArray}, F::Base.LinAlg.LU{T,Tridiagonal{T}}) where {T} = convert(AbstractMatrix, F) -convert{T}(::Type{Matrix}, F::Base.LinAlg.LU{T,Tridiagonal{T}}) = +convert(::Type{Matrix}, F::Base.LinAlg.LU{T,Tridiagonal{T}}) where {T} = convert(Array, convert(AbstractArray, F)) -convert{T}(::Type{Array}, F::Base.LinAlg.LU{T,Tridiagonal{T}}) = +convert(::Type{Array}, F::Base.LinAlg.LU{T,Tridiagonal{T}}) where {T} = convert(Matrix, F) full{T}(F::Base.LinAlg.LU{T,Tridiagonal{T}}) = convert(AbstractArray, F) diff --git a/base/linalg/matmul.jl b/base/linalg/matmul.jl index d688811173245..f7dd618f0178e 100644 --- a/base/linalg/matmul.jl +++ b/base/linalg/matmul.jl @@ -40,10 +40,10 @@ end # Dot products -vecdot{T<:BlasReal}(x::Union{DenseArray{T},StridedVector{T}}, y::Union{DenseArray{T},StridedVector{T}}) = BLAS.dot(x, y) -vecdot{T<:BlasComplex}(x::Union{DenseArray{T},StridedVector{T}}, y::Union{DenseArray{T},StridedVector{T}}) = BLAS.dotc(x, y) +vecdot(x::Union{DenseArray{T},StridedVector{T}}, y::Union{DenseArray{T},StridedVector{T}}) where {T<:BlasReal} = BLAS.dot(x, y) +vecdot(x::Union{DenseArray{T},StridedVector{T}}, y::Union{DenseArray{T},StridedVector{T}}) where {T<:BlasComplex} = BLAS.dotc(x, y) -function dot{T<:BlasReal, TI<:Integer}(x::Vector{T}, rx::Union{UnitRange{TI},Range{TI}}, y::Vector{T}, ry::Union{UnitRange{TI},Range{TI}}) +function dot(x::Vector{T}, rx::Union{UnitRange{TI},Range{TI}}, y::Vector{T}, ry::Union{UnitRange{TI},Range{TI}}) where {T<:BlasReal,TI<:Integer} if length(rx) != length(ry) throw(DimensionMismatch("length of rx, $(length(rx)), does not equal length of ry, $(length(ry))")) end @@ -56,7 +56,7 @@ function dot{T<:BlasReal, TI<:Integer}(x::Vector{T}, rx::Union{UnitRange{TI},Ran BLAS.dot(length(rx), pointer(x)+(first(rx)-1)*sizeof(T), step(rx), pointer(y)+(first(ry)-1)*sizeof(T), step(ry)) end -function dot{T<:BlasComplex, TI<:Integer}(x::Vector{T}, rx::Union{UnitRange{TI},Range{TI}}, y::Vector{T}, ry::Union{UnitRange{TI},Range{TI}}) +function dot(x::Vector{T}, rx::Union{UnitRange{TI},Range{TI}}, y::Vector{T}, ry::Union{UnitRange{TI},Range{TI}}) where {T<:BlasComplex,TI<:Integer} if length(rx) != length(ry) throw(DimensionMismatch("length of rx, $(length(rx)), does not equal length of ry, $(length(ry))")) end @@ -69,14 +69,14 @@ function dot{T<:BlasComplex, TI<:Integer}(x::Vector{T}, rx::Union{UnitRange{TI}, BLAS.dotc(length(rx), pointer(x)+(first(rx)-1)*sizeof(T), step(rx), pointer(y)+(first(ry)-1)*sizeof(T), step(ry)) end -At_mul_B{T<:BlasComplex}(x::StridedVector{T}, y::StridedVector{T}) = BLAS.dotu(x, y) +At_mul_B(x::StridedVector{T}, y::StridedVector{T}) where {T<:BlasComplex} = BLAS.dotu(x, y) # Matrix-vector multiplication -function (*){T<:BlasFloat,S}(A::StridedMatrix{T}, x::StridedVector{S}) +function (*)(A::StridedMatrix{T}, x::StridedVector{S}) where {T<:BlasFloat,S} TS = promote_op(matprod, T, S) A_mul_B!(similar(x, TS, size(A,1)), A, convert(AbstractVector{TS}, x)) end -function (*){T,S}(A::AbstractMatrix{T}, x::AbstractVector{S}) +function (*)(A::AbstractMatrix{T}, x::AbstractVector{S}) where {T,S} TS = promote_op(matprod, T, S) A_mul_B!(similar(x,TS,size(A,1)),A,x) end @@ -88,7 +88,7 @@ A_mul_Bc(a::AbstractVector, B::AbstractMatrix) = A_mul_Bc(reshape(a,length(a),1) A_mul_Bc(A::AbstractMatrix, b::AbstractVector) = A_mul_Bc(A,reshape(b,length(b),1)) (*)(a::AbstractVector, B::AbstractMatrix) = reshape(a,length(a),1)*B -A_mul_B!{T<:BlasFloat}(y::StridedVector{T}, A::StridedVecOrMat{T}, x::StridedVector{T}) = gemv!(y, 'N', A, x) +A_mul_B!(y::StridedVector{T}, A::StridedVecOrMat{T}, x::StridedVector{T}) where {T<:BlasFloat} = gemv!(y, 'N', A, x) for elty in (Float32,Float64) @eval begin function A_mul_B!(y::StridedVector{Complex{$elty}}, A::StridedVecOrMat{Complex{$elty}}, x::StridedVector{$elty}) @@ -101,28 +101,28 @@ for elty in (Float32,Float64) end A_mul_B!(y::AbstractVector, A::AbstractVecOrMat, x::AbstractVector) = generic_matvecmul!(y, 'N', A, x) -function At_mul_B{T<:BlasFloat,S}(A::StridedMatrix{T}, x::StridedVector{S}) +function At_mul_B(A::StridedMatrix{T}, x::StridedVector{S}) where {T<:BlasFloat,S} TS = promote_op(matprod, T, S) At_mul_B!(similar(x,TS,size(A,2)), A, convert(AbstractVector{TS}, x)) end -function At_mul_B{T,S}(A::AbstractMatrix{T}, x::AbstractVector{S}) +function At_mul_B(A::AbstractMatrix{T}, x::AbstractVector{S}) where {T,S} TS = promote_op(matprod, T, S) At_mul_B!(similar(x,TS,size(A,2)), A, x) end -At_mul_B!{T<:BlasFloat}(y::StridedVector{T}, A::StridedVecOrMat{T}, x::StridedVector{T}) = gemv!(y, 'T', A, x) +At_mul_B!(y::StridedVector{T}, A::StridedVecOrMat{T}, x::StridedVector{T}) where {T<:BlasFloat} = gemv!(y, 'T', A, x) At_mul_B!(y::AbstractVector, A::AbstractVecOrMat, x::AbstractVector) = generic_matvecmul!(y, 'T', A, x) -function Ac_mul_B{T<:BlasFloat,S}(A::StridedMatrix{T}, x::StridedVector{S}) +function Ac_mul_B(A::StridedMatrix{T}, x::StridedVector{S}) where {T<:BlasFloat,S} TS = promote_op(matprod, T, S) Ac_mul_B!(similar(x,TS,size(A,2)),A,convert(AbstractVector{TS},x)) end -function Ac_mul_B{T,S}(A::AbstractMatrix{T}, x::AbstractVector{S}) +function Ac_mul_B(A::AbstractMatrix{T}, x::AbstractVector{S}) where {T,S} TS = promote_op(matprod, T, S) Ac_mul_B!(similar(x,TS,size(A,2)), A, x) end -Ac_mul_B!{T<:BlasReal}(y::StridedVector{T}, A::StridedVecOrMat{T}, x::StridedVector{T}) = At_mul_B!(y, A, x) -Ac_mul_B!{T<:BlasComplex}(y::StridedVector{T}, A::StridedVecOrMat{T}, x::StridedVector{T}) = gemv!(y, 'C', A, x) +Ac_mul_B!(y::StridedVector{T}, A::StridedVecOrMat{T}, x::StridedVector{T}) where {T<:BlasReal} = At_mul_B!(y, A, x) +Ac_mul_B!(y::StridedVector{T}, A::StridedVecOrMat{T}, x::StridedVector{T}) where {T<:BlasComplex} = gemv!(y, 'C', A, x) Ac_mul_B!(y::AbstractVector, A::AbstractVecOrMat, x::AbstractVector) = generic_matvecmul!(y, 'C', A, x) # Matrix-matrix multiplication @@ -143,11 +143,11 @@ julia> [1 1; 0 1] * [1 0; 1 1] 1 1 ``` """ -function (*){T,S}(A::AbstractMatrix{T}, B::AbstractMatrix{S}) +function (*)(A::AbstractMatrix{T}, B::AbstractMatrix{S}) where {T,S} TS = promote_op(matprod, T, S) A_mul_B!(similar(B, TS, (size(A,1), size(B,2))), A, B) end -A_mul_B!{T<:BlasFloat}(C::StridedMatrix{T}, A::StridedVecOrMat{T}, B::StridedVecOrMat{T}) = gemm_wrapper!(C, 'N', 'N', A, B) +A_mul_B!(C::StridedMatrix{T}, A::StridedVecOrMat{T}, B::StridedVecOrMat{T}) where {T<:BlasFloat} = gemm_wrapper!(C, 'N', 'N', A, B) for elty in (Float32,Float64) @eval begin function A_mul_B!(C::StridedMatrix{Complex{$elty}}, A::StridedVecOrMat{Complex{$elty}}, B::StridedVecOrMat{$elty}) @@ -179,18 +179,18 @@ julia> Y """ A_mul_B!(C::AbstractMatrix, A::AbstractVecOrMat, B::AbstractVecOrMat) = generic_matmatmul!(C, 'N', 'N', A, B) -function At_mul_B{T,S}(A::AbstractMatrix{T}, B::AbstractMatrix{S}) +function At_mul_B(A::AbstractMatrix{T}, B::AbstractMatrix{S}) where {T,S} TS = promote_op(matprod, T, S) At_mul_B!(similar(B, TS, (size(A,2), size(B,2))), A, B) end -At_mul_B!{T<:BlasFloat}(C::StridedMatrix{T}, A::StridedVecOrMat{T}, B::StridedVecOrMat{T}) = A===B ? syrk_wrapper!(C, 'T', A) : gemm_wrapper!(C, 'T', 'N', A, B) +At_mul_B!(C::StridedMatrix{T}, A::StridedVecOrMat{T}, B::StridedVecOrMat{T}) where {T<:BlasFloat} = A===B ? syrk_wrapper!(C, 'T', A) : gemm_wrapper!(C, 'T', 'N', A, B) At_mul_B!(C::AbstractMatrix, A::AbstractVecOrMat, B::AbstractVecOrMat) = generic_matmatmul!(C, 'T', 'N', A, B) -function A_mul_Bt{T,S}(A::AbstractMatrix{T}, B::AbstractMatrix{S}) +function A_mul_Bt(A::AbstractMatrix{T}, B::AbstractMatrix{S}) where {T,S} TS = promote_op(matprod, T, S) A_mul_Bt!(similar(B, TS, (size(A,1), size(B,1))), A, B) end -A_mul_Bt!{T<:BlasFloat}(C::StridedMatrix{T}, A::StridedVecOrMat{T}, B::StridedVecOrMat{T}) = A===B ? syrk_wrapper!(C, 'N', A) : gemm_wrapper!(C, 'N', 'T', A, B) +A_mul_Bt!(C::StridedMatrix{T}, A::StridedVecOrMat{T}, B::StridedVecOrMat{T}) where {T<:BlasFloat} = A===B ? syrk_wrapper!(C, 'N', A) : gemm_wrapper!(C, 'N', 'T', A, B) for elty in (Float32,Float64) @eval begin function A_mul_Bt!(C::StridedMatrix{Complex{$elty}}, A::StridedVecOrMat{Complex{$elty}}, B::StridedVecOrMat{$elty}) @@ -203,34 +203,34 @@ for elty in (Float32,Float64) end A_mul_Bt!(C::AbstractVecOrMat, A::AbstractVecOrMat, B::AbstractVecOrMat) = generic_matmatmul!(C, 'N', 'T', A, B) -function At_mul_Bt{T,S}(A::AbstractMatrix{T}, B::AbstractVecOrMat{S}) +function At_mul_Bt(A::AbstractMatrix{T}, B::AbstractVecOrMat{S}) where {T,S} TS = promote_op(matprod, T, S) At_mul_Bt!(similar(B, TS, (size(A,2), size(B,1))), A, B) end -At_mul_Bt!{T<:BlasFloat}(C::StridedMatrix{T}, A::StridedVecOrMat{T}, B::StridedVecOrMat{T}) = gemm_wrapper!(C, 'T', 'T', A, B) +At_mul_Bt!(C::StridedMatrix{T}, A::StridedVecOrMat{T}, B::StridedVecOrMat{T}) where {T<:BlasFloat} = gemm_wrapper!(C, 'T', 'T', A, B) At_mul_Bt!(C::AbstractMatrix, A::AbstractVecOrMat, B::AbstractVecOrMat) = generic_matmatmul!(C, 'T', 'T', A, B) -Ac_mul_B{T<:BlasReal}(A::StridedMatrix{T}, B::StridedMatrix{T}) = At_mul_B(A, B) -Ac_mul_B!{T<:BlasReal}(C::StridedMatrix{T}, A::StridedVecOrMat{T}, B::StridedVecOrMat{T}) = At_mul_B!(C, A, B) -function Ac_mul_B{T,S}(A::AbstractMatrix{T}, B::AbstractMatrix{S}) +Ac_mul_B(A::StridedMatrix{T}, B::StridedMatrix{T}) where {T<:BlasReal} = At_mul_B(A, B) +Ac_mul_B!(C::StridedMatrix{T}, A::StridedVecOrMat{T}, B::StridedVecOrMat{T}) where {T<:BlasReal} = At_mul_B!(C, A, B) +function Ac_mul_B(A::AbstractMatrix{T}, B::AbstractMatrix{S}) where {T,S} TS = promote_op(matprod, T, S) Ac_mul_B!(similar(B, TS, (size(A,2), size(B,2))), A, B) end -Ac_mul_B!{T<:BlasComplex}(C::StridedMatrix{T}, A::StridedVecOrMat{T}, B::StridedVecOrMat{T}) = A===B ? herk_wrapper!(C,'C',A) : gemm_wrapper!(C,'C', 'N', A, B) +Ac_mul_B!(C::StridedMatrix{T}, A::StridedVecOrMat{T}, B::StridedVecOrMat{T}) where {T<:BlasComplex} = A===B ? herk_wrapper!(C,'C',A) : gemm_wrapper!(C,'C', 'N', A, B) Ac_mul_B!(C::AbstractMatrix, A::AbstractVecOrMat, B::AbstractVecOrMat) = generic_matmatmul!(C, 'C', 'N', A, B) A_mul_Bc(A::StridedMatrix{<:BlasFloat}, B::StridedMatrix{<:BlasReal}) = A_mul_Bt(A, B) -A_mul_Bc!{T<:BlasFloat}(C::StridedMatrix{T}, A::StridedVecOrMat{T}, B::StridedVecOrMat{<:BlasReal}) = A_mul_Bt!(C, A, B) -function A_mul_Bc{T,S}(A::AbstractMatrix{T}, B::AbstractMatrix{S}) +A_mul_Bc!(C::StridedMatrix{T}, A::StridedVecOrMat{T}, B::StridedVecOrMat{<:BlasReal}) where {T<:BlasFloat} = A_mul_Bt!(C, A, B) +function A_mul_Bc(A::AbstractMatrix{T}, B::AbstractMatrix{S}) where {T,S} TS = promote_op(matprod, T, S) A_mul_Bc!(similar(B,TS,(size(A,1),size(B,1))),A,B) end -A_mul_Bc!{T<:BlasComplex}(C::StridedMatrix{T}, A::StridedVecOrMat{T}, B::StridedVecOrMat{T}) = A===B ? herk_wrapper!(C, 'N', A) : gemm_wrapper!(C, 'N', 'C', A, B) +A_mul_Bc!(C::StridedMatrix{T}, A::StridedVecOrMat{T}, B::StridedVecOrMat{T}) where {T<:BlasComplex} = A===B ? herk_wrapper!(C, 'N', A) : gemm_wrapper!(C, 'N', 'C', A, B) A_mul_Bc!(C::AbstractMatrix, A::AbstractVecOrMat, B::AbstractVecOrMat) = generic_matmatmul!(C, 'N', 'C', A, B) -Ac_mul_Bc{T,S}(A::AbstractMatrix{T}, B::AbstractMatrix{S}) = +Ac_mul_Bc(A::AbstractMatrix{T}, B::AbstractMatrix{S}) where {T,S} = Ac_mul_Bc!(similar(B, promote_op(matprod, T, S), (size(A,2), size(B,1))), A, B) -Ac_mul_Bc!{T<:BlasFloat}(C::StridedMatrix{T}, A::StridedVecOrMat{T}, B::StridedVecOrMat{T}) = gemm_wrapper!(C, 'C', 'C', A, B) +Ac_mul_Bc!(C::StridedMatrix{T}, A::StridedVecOrMat{T}, B::StridedVecOrMat{T}) where {T<:BlasFloat} = gemm_wrapper!(C, 'C', 'C', A, B) Ac_mul_Bc!(C::AbstractMatrix, A::AbstractVecOrMat, B::AbstractVecOrMat) = generic_matmatmul!(C, 'C', 'C', A, B) Ac_mul_Bt!(C::AbstractMatrix, A::AbstractVecOrMat, B::AbstractVecOrMat) = generic_matmatmul!(C, 'C', 'T', A, B) # Supporting functions for matrix multiplication @@ -251,7 +251,7 @@ function copytri!(A::AbstractMatrix, uplo::Char, conjugate::Bool=false) A end -function gemv!{T<:BlasFloat}(y::StridedVector{T}, tA::Char, A::StridedVecOrMat{T}, x::StridedVector{T}) +function gemv!(y::StridedVector{T}, tA::Char, A::StridedVecOrMat{T}, x::StridedVector{T}) where T<:BlasFloat mA, nA = lapack_size(tA, A) if nA != length(x) throw(DimensionMismatch("second dimension of A, $nA, does not match length of x, $(length(x))")) @@ -269,7 +269,7 @@ function gemv!{T<:BlasFloat}(y::StridedVector{T}, tA::Char, A::StridedVecOrMat{T return generic_matvecmul!(y, tA, A, x) end -function syrk_wrapper!{T<:BlasFloat}(C::StridedMatrix{T}, tA::Char, A::StridedVecOrMat{T}) +function syrk_wrapper!(C::StridedMatrix{T}, tA::Char, A::StridedVecOrMat{T}) where T<:BlasFloat nC = checksquare(C) if tA == 'T' (nA, mA) = size(A,1), size(A,2) @@ -297,7 +297,7 @@ function syrk_wrapper!{T<:BlasFloat}(C::StridedMatrix{T}, tA::Char, A::StridedVe return generic_matmatmul!(C, tA, tAt, A, A) end -function herk_wrapper!{T<:BlasReal}(C::Union{StridedMatrix{T}, StridedMatrix{Complex{T}}}, tA::Char, A::Union{StridedVecOrMat{T}, StridedVecOrMat{Complex{T}}}) +function herk_wrapper!(C::Union{StridedMatrix{T}, StridedMatrix{Complex{T}}}, tA::Char, A::Union{StridedVecOrMat{T}, StridedVecOrMat{Complex{T}}}) where T<:BlasReal nC = checksquare(C) if tA == 'C' (nA, mA) = size(A,1), size(A,2) @@ -328,18 +328,18 @@ function herk_wrapper!{T<:BlasReal}(C::Union{StridedMatrix{T}, StridedMatrix{Com return generic_matmatmul!(C,tA, tAt, A, A) end -function gemm_wrapper{T<:BlasFloat}(tA::Char, tB::Char, - A::StridedVecOrMat{T}, - B::StridedVecOrMat{T}) +function gemm_wrapper(tA::Char, tB::Char, + A::StridedVecOrMat{T}, + B::StridedVecOrMat{T}) where T<:BlasFloat mA, nA = lapack_size(tA, A) mB, nB = lapack_size(tB, B) C = similar(B, T, mA, nB) gemm_wrapper!(C, tA, tB, A, B) end -function gemm_wrapper!{T<:BlasFloat}(C::StridedVecOrMat{T}, tA::Char, tB::Char, - A::StridedVecOrMat{T}, - B::StridedVecOrMat{T}) +function gemm_wrapper!(C::StridedVecOrMat{T}, tA::Char, tB::Char, + A::StridedVecOrMat{T}, + B::StridedVecOrMat{T}) where T<:BlasFloat mA, nA = lapack_size(tA, A) mB, nB = lapack_size(tB, B) @@ -402,7 +402,7 @@ end # NOTE: the generic version is also called as fallback for # strides != 1 cases -function generic_matvecmul!{R}(C::AbstractVector{R}, tA, A::AbstractVecOrMat, B::AbstractVector) +function generic_matvecmul!(C::AbstractVector{R}, tA, A::AbstractVecOrMat, B::AbstractVector) where R mB = length(B) mA, nA = lapack_size(tA, A) if mB != nA @@ -459,7 +459,7 @@ function generic_matvecmul!{R}(C::AbstractVector{R}, tA, A::AbstractVecOrMat, B: C end -function generic_matmatmul{T,S}(tA, tB, A::AbstractVecOrMat{T}, B::AbstractMatrix{S}) +function generic_matmatmul(tA, tB, A::AbstractVecOrMat{T}, B::AbstractMatrix{S}) where {T,S} mA, nA = lapack_size(tA, A) mB, nB = lapack_size(tB, B) C = similar(B, promote_op(matprod, T, S), mA, nB) @@ -487,7 +487,7 @@ end generic_matmatmul!(C::AbstractVecOrMat, tA, tB, A::AbstractVecOrMat, B::AbstractVecOrMat) = _generic_matmatmul!(C, tA, tB, A, B) -function _generic_matmatmul!{T,S,R}(C::AbstractVecOrMat{R}, tA, tB, A::AbstractVecOrMat{T}, B::AbstractVecOrMat{S}) +function _generic_matmatmul!(C::AbstractVecOrMat{R}, tA, tB, A::AbstractVecOrMat{T}, B::AbstractVecOrMat{S}) where {T,S,R} mA, nA = lapack_size(tA, A) mB, nB = lapack_size(tB, B) if mB != nA @@ -655,7 +655,7 @@ end # multiply 2x2 matrices -function matmul2x2{T,S}(tA, tB, A::AbstractMatrix{T}, B::AbstractMatrix{S}) +function matmul2x2(tA, tB, A::AbstractMatrix{T}, B::AbstractMatrix{S}) where {T,S} matmul2x2!(similar(B, promote_op(matprod, T, S), 2, 2), tA, tB, A, B) end @@ -687,7 +687,7 @@ function matmul2x2!(C::AbstractMatrix, tA, tB, A::AbstractMatrix, B::AbstractMat end # Multiply 3x3 matrices -function matmul3x3{T,S}(tA, tB, A::AbstractMatrix{T}, B::AbstractMatrix{S}) +function matmul3x3(tA, tB, A::AbstractMatrix{T}, B::AbstractMatrix{S}) where {T,S} matmul3x3!(similar(B, promote_op(matprod, T, S), 3, 3), tA, tB, A, B) end diff --git a/base/linalg/qr.jl b/base/linalg/qr.jl index 191d138e3a271..e693496c7c052 100644 --- a/base/linalg/qr.jl +++ b/base/linalg/qr.jl @@ -271,20 +271,20 @@ function qr!(v::AbstractVector) end # Conversions -convert{T}(::Type{QR{T}}, A::QR) = QR(convert(AbstractMatrix{T}, A.factors), convert(Vector{T}, A.τ)) -convert{T}(::Type{Factorization{T}}, A::QR{T}) = A -convert{T}(::Type{Factorization{T}}, A::QR) = convert(QR{T}, A) -convert{T}(::Type{QRCompactWY{T}}, A::QRCompactWY) = QRCompactWY(convert(AbstractMatrix{T}, A.factors), convert(AbstractMatrix{T}, A.T)) -convert{T}(::Type{Factorization{T}}, A::QRCompactWY{T}) = A -convert{T}(::Type{Factorization{T}}, A::QRCompactWY) = convert(QRCompactWY{T}, A) +convert(::Type{QR{T}}, A::QR) where {T} = QR(convert(AbstractMatrix{T}, A.factors), convert(Vector{T}, A.τ)) +convert(::Type{Factorization{T}}, A::QR{T}) where {T} = A +convert(::Type{Factorization{T}}, A::QR) where {T} = convert(QR{T}, A) +convert(::Type{QRCompactWY{T}}, A::QRCompactWY) where {T} = QRCompactWY(convert(AbstractMatrix{T}, A.factors), convert(AbstractMatrix{T}, A.T)) +convert(::Type{Factorization{T}}, A::QRCompactWY{T}) where {T} = A +convert(::Type{Factorization{T}}, A::QRCompactWY) where {T} = convert(QRCompactWY{T}, A) convert(::Type{AbstractMatrix}, F::Union{QR,QRCompactWY}) = F[:Q] * F[:R] convert(::Type{AbstractArray}, F::Union{QR,QRCompactWY}) = convert(AbstractMatrix, F) convert(::Type{Matrix}, F::Union{QR,QRCompactWY}) = convert(Array, convert(AbstractArray, F)) convert(::Type{Array}, F::Union{QR,QRCompactWY}) = convert(Matrix, F) full(F::Union{QR,QRCompactWY}) = convert(AbstractArray, F) -convert{T}(::Type{QRPivoted{T}}, A::QRPivoted) = QRPivoted(convert(AbstractMatrix{T}, A.factors), convert(Vector{T}, A.τ), A.jpvt) -convert{T}(::Type{Factorization{T}}, A::QRPivoted{T}) = A -convert{T}(::Type{Factorization{T}}, A::QRPivoted) = convert(QRPivoted{T}, A) +convert(::Type{QRPivoted{T}}, A::QRPivoted) where {T} = QRPivoted(convert(AbstractMatrix{T}, A.factors), convert(Vector{T}, A.τ), A.jpvt) +convert(::Type{Factorization{T}}, A::QRPivoted{T}) where {T} = A +convert(::Type{Factorization{T}}, A::QRPivoted) where {T} = convert(QRPivoted{T}, A) convert(::Type{AbstractMatrix}, F::QRPivoted) = (F[:Q] * F[:R])[:,invperm(F[:p])] convert(::Type{AbstractArray}, F::QRPivoted) = convert(AbstractMatrix, F) convert(::Type{Matrix}, F::QRPivoted) = convert(Array, convert(AbstractArray, F)) @@ -318,7 +318,7 @@ function getindex(A::QRCompactWY, d::Symbol) throw(KeyError(d)) end end -function getindex{T}(A::QRPivoted{T}, d::Symbol) +function getindex(A::QRPivoted{T}, d::Symbol) where T m, n = size(A) if d == :R return triu!(A.factors[1:min(m,n), 1:n]) @@ -357,13 +357,13 @@ struct QRCompactWYQ{S, M<:AbstractMatrix} <: AbstractMatrix{S} end QRCompactWYQ(factors::AbstractMatrix{S}, T::Matrix{S}) where {S} = QRCompactWYQ{S,typeof(factors)}(factors, T) -convert{T}(::Type{QRPackedQ{T}}, Q::QRPackedQ) = QRPackedQ(convert(AbstractMatrix{T}, Q.factors), convert(Vector{T}, Q.τ)) -convert{T}(::Type{AbstractMatrix{T}}, Q::QRPackedQ{T}) = Q -convert{T}(::Type{AbstractMatrix{T}}, Q::QRPackedQ) = convert(QRPackedQ{T}, Q) -convert{S}(::Type{QRCompactWYQ{S}}, Q::QRCompactWYQ) = QRCompactWYQ(convert(AbstractMatrix{S}, Q.factors), convert(AbstractMatrix{S}, Q.T)) -convert{S}(::Type{AbstractMatrix{S}}, Q::QRCompactWYQ{S}) = Q -convert{S}(::Type{AbstractMatrix{S}}, Q::QRCompactWYQ) = convert(QRCompactWYQ{S}, Q) -convert{T}(::Type{Matrix}, A::Union{QRPackedQ{T},QRCompactWYQ{T}}) = A_mul_B!(A, eye(T, size(A.factors, 1), minimum(size(A.factors)))) +convert(::Type{QRPackedQ{T}}, Q::QRPackedQ) where {T} = QRPackedQ(convert(AbstractMatrix{T}, Q.factors), convert(Vector{T}, Q.τ)) +convert(::Type{AbstractMatrix{T}}, Q::QRPackedQ{T}) where {T} = Q +convert(::Type{AbstractMatrix{T}}, Q::QRPackedQ) where {T} = convert(QRPackedQ{T}, Q) +convert(::Type{QRCompactWYQ{S}}, Q::QRCompactWYQ) where {S} = QRCompactWYQ(convert(AbstractMatrix{S}, Q.factors), convert(AbstractMatrix{S}, Q.T)) +convert(::Type{AbstractMatrix{S}}, Q::QRCompactWYQ{S}) where {S} = Q +convert(::Type{AbstractMatrix{S}}, Q::QRCompactWYQ) where {S} = convert(QRCompactWYQ{S}, Q) +convert(::Type{Matrix}, A::Union{QRPackedQ{T},QRCompactWYQ{T}}) where {T} = A_mul_B!(A, eye(T, size(A.factors, 1), minimum(size(A.factors)))) convert(::Type{Array}, A::Union{QRPackedQ,QRCompactWYQ}) = convert(Matrix, A) """ @@ -401,8 +401,8 @@ end ## Multiplication by Q ### QB -A_mul_B!{T<:BlasFloat}(A::QRCompactWYQ{T}, B::StridedVecOrMat{T}) = LAPACK.gemqrt!('L','N',A.factors,A.T,B) -A_mul_B!{T<:BlasFloat}(A::QRPackedQ{T}, B::StridedVecOrMat{T}) = LAPACK.ormqr!('L','N',A.factors,A.τ,B) +A_mul_B!(A::QRCompactWYQ{T}, B::StridedVecOrMat{T}) where {T<:BlasFloat} = LAPACK.gemqrt!('L','N',A.factors,A.T,B) +A_mul_B!(A::QRPackedQ{T}, B::StridedVecOrMat{T}) where {T<:BlasFloat} = LAPACK.ormqr!('L','N',A.factors,A.τ,B) function A_mul_B!(A::QRPackedQ, B::AbstractVecOrMat) mA, nA = size(A.factors) mB, nB = size(B,1), size(B,2) @@ -454,10 +454,10 @@ function (*)(A::Union{QRPackedQ,QRCompactWYQ}, B::StridedMatrix) end ### QcB -Ac_mul_B!{T<:BlasReal}(A::QRCompactWYQ{T}, B::StridedVecOrMat{T}) = LAPACK.gemqrt!('L','T',A.factors,A.T,B) -Ac_mul_B!{T<:BlasComplex}(A::QRCompactWYQ{T}, B::StridedVecOrMat{T}) = LAPACK.gemqrt!('L','C',A.factors,A.T,B) -Ac_mul_B!{T<:BlasReal}(A::QRPackedQ{T}, B::StridedVecOrMat{T}) = LAPACK.ormqr!('L','T',A.factors,A.τ,B) -Ac_mul_B!{T<:BlasComplex}(A::QRPackedQ{T}, B::StridedVecOrMat{T}) = LAPACK.ormqr!('L','C',A.factors,A.τ,B) +Ac_mul_B!(A::QRCompactWYQ{T}, B::StridedVecOrMat{T}) where {T<:BlasReal} = LAPACK.gemqrt!('L','T',A.factors,A.T,B) +Ac_mul_B!(A::QRCompactWYQ{T}, B::StridedVecOrMat{T}) where {T<:BlasComplex} = LAPACK.gemqrt!('L','C',A.factors,A.T,B) +Ac_mul_B!(A::QRPackedQ{T}, B::StridedVecOrMat{T}) where {T<:BlasReal} = LAPACK.ormqr!('L','T',A.factors,A.τ,B) +Ac_mul_B!(A::QRPackedQ{T}, B::StridedVecOrMat{T}) where {T<:BlasComplex} = LAPACK.ormqr!('L','C',A.factors,A.τ,B) function Ac_mul_B!(A::QRPackedQ, B::AbstractVecOrMat) mA, nA = size(A.factors) mB, nB = size(B,1), size(B,2) @@ -501,8 +501,8 @@ for (f1, f2) in ((:A_mul_Bc, :A_mul_B!), end ### AQ -A_mul_B!{T<:BlasFloat}(A::StridedVecOrMat{T}, B::QRCompactWYQ{T}) = LAPACK.gemqrt!('R','N', B.factors, B.T, A) -A_mul_B!{T<:BlasFloat}(A::StridedVecOrMat{T}, B::QRPackedQ{T}) = LAPACK.ormqr!('R', 'N', B.factors, B.τ, A) +A_mul_B!(A::StridedVecOrMat{T}, B::QRCompactWYQ{T}) where {T<:BlasFloat} = LAPACK.gemqrt!('R','N', B.factors, B.T, A) +A_mul_B!(A::StridedVecOrMat{T}, B::QRPackedQ{T}) where {T<:BlasFloat} = LAPACK.ormqr!('R', 'N', B.factors, B.τ, A) function A_mul_B!(A::StridedMatrix,Q::QRPackedQ) mQ, nQ = size(Q.factors) mA, nA = size(A,1), size(A,2) @@ -534,10 +534,10 @@ function (*)(A::StridedMatrix, Q::Union{QRPackedQ,QRCompactWYQ}) end ### AQc -A_mul_Bc!{T<:BlasReal}(A::StridedVecOrMat{T}, B::QRCompactWYQ{T}) = LAPACK.gemqrt!('R','T',B.factors,B.T,A) -A_mul_Bc!{T<:BlasComplex}(A::StridedVecOrMat{T}, B::QRCompactWYQ{T}) = LAPACK.gemqrt!('R','C',B.factors,B.T,A) -A_mul_Bc!{T<:BlasReal}(A::StridedVecOrMat{T}, B::QRPackedQ{T}) = LAPACK.ormqr!('R','T',B.factors,B.τ,A) -A_mul_Bc!{T<:BlasComplex}(A::StridedVecOrMat{T}, B::QRPackedQ{T}) = LAPACK.ormqr!('R','C',B.factors,B.τ,A) +A_mul_Bc!(A::StridedVecOrMat{T}, B::QRCompactWYQ{T}) where {T<:BlasReal} = LAPACK.gemqrt!('R','T',B.factors,B.T,A) +A_mul_Bc!(A::StridedVecOrMat{T}, B::QRCompactWYQ{T}) where {T<:BlasComplex} = LAPACK.gemqrt!('R','C',B.factors,B.T,A) +A_mul_Bc!(A::StridedVecOrMat{T}, B::QRPackedQ{T}) where {T<:BlasReal} = LAPACK.ormqr!('R','T',B.factors,B.τ,A) +A_mul_Bc!(A::StridedVecOrMat{T}, B::QRPackedQ{T}) where {T<:BlasComplex} = LAPACK.ormqr!('R','C',B.factors,B.τ,A) function A_mul_Bc!(A::AbstractMatrix,Q::QRPackedQ) mQ, nQ = size(Q.factors) mA, nA = size(A,1), size(A,2) diff --git a/base/linalg/rowvector.jl b/base/linalg/rowvector.jl index dd8607fad9349..3e33d1c43af12 100644 --- a/base/linalg/rowvector.jl +++ b/base/linalg/rowvector.jl @@ -45,7 +45,7 @@ ConjRowVector{T, CV <: ConjVector} = RowVector{T, CV} error("RowVector expects 1×N size, got $n") # Conversion of underlying storage -convert{T,V<:AbstractVector}(::Type{RowVector{T,V}}, rowvec::RowVector) = +convert(::Type{RowVector{T,V}}, rowvec::RowVector) where {T,V<:AbstractVector} = RowVector{T,V}(convert(V,rowvec.vec)) # similar tries to maintain the RowVector wrapper and the parent type @@ -204,7 +204,7 @@ At_mul_B(::RowVector, ::AbstractVector) = throw(DimensionMismatch("Cannot multip @inline At_mul_B(rowvec1::RowVector, rowvec2::RowVector) = transpose(rowvec1) * rowvec2 At_mul_B(vec::AbstractVector, rowvec::RowVector) = throw(DimensionMismatch( "Cannot multiply two transposed vectors")) -@inline At_mul_B{T<:Real}(vec1::AbstractVector{T}, vec2::AbstractVector{T}) = +@inline At_mul_B(vec1::AbstractVector{T}, vec2::AbstractVector{T}) where {T<:Real} = reduce(+, map(At_mul_B, vec1, vec2)) # Seems to be overloaded... @inline At_mul_B(vec1::AbstractVector, vec2::AbstractVector) = transpose(vec1) * vec2 diff --git a/base/linalg/special.jl b/base/linalg/special.jl index 662637ec1b9f4..c4fd2c6e09bb1 100644 --- a/base/linalg/special.jl +++ b/base/linalg/special.jl @@ -3,11 +3,11 @@ # Methods operating on different special matrix types # Interconversion between special matrix types -convert{T}(::Type{Bidiagonal}, A::Diagonal{T}) = +convert(::Type{Bidiagonal}, A::Diagonal{T}) where {T} = Bidiagonal(A.diag, zeros(T, size(A.diag,1)-1), true) -convert{T}(::Type{SymTridiagonal}, A::Diagonal{T}) = +convert(::Type{SymTridiagonal}, A::Diagonal{T}) where {T} = SymTridiagonal(A.diag, zeros(T, size(A.diag,1)-1)) -convert{T}(::Type{Tridiagonal}, A::Diagonal{T}) = +convert(::Type{Tridiagonal}, A::Diagonal{T}) where {T} = Tridiagonal(zeros(T, size(A.diag,1)-1), A.diag, zeros(T, size(A.diag,1)-1)) function convert(::Type{Diagonal}, A::Union{Bidiagonal, SymTridiagonal}) @@ -24,7 +24,7 @@ function convert(::Type{SymTridiagonal}, A::Bidiagonal) SymTridiagonal(A.dv, A.ev) end -convert{T}(::Type{Tridiagonal}, A::Bidiagonal{T}) = +convert(::Type{Tridiagonal}, A::Bidiagonal{T}) where {T} = Tridiagonal(A.isupper ? zeros(T, size(A.dv,1)-1) : A.ev, A.dv, A.isupper ? A.ev:zeros(T, size(A.dv,1)-1)) diff --git a/base/linalg/svd.jl b/base/linalg/svd.jl index 96df0b82a89fc..7bc1a42f45486 100644 --- a/base/linalg/svd.jl +++ b/base/linalg/svd.jl @@ -249,7 +249,7 @@ function svd(A::AbstractMatrix, B::AbstractMatrix) F[:U], F[:V], F[:Q], F[:D1], F[:D2], F[:R0] end -function getindex{T}(obj::GeneralizedSVD{T}, d::Symbol) +function getindex(obj::GeneralizedSVD{T}, d::Symbol) where T if d == :U return obj.U elseif d == :V diff --git a/base/linalg/symmetric.jl b/base/linalg/symmetric.jl index 4664ad505354a..10e2dd5a5fc6a 100644 --- a/base/linalg/symmetric.jl +++ b/base/linalg/symmetric.jl @@ -114,10 +114,10 @@ function setindex!(A::Hermitian, v, i::Integer, j::Integer) end end -similar{T}(A::Symmetric, ::Type{T}) = Symmetric(similar(A.data, T)) +similar(A::Symmetric, ::Type{T}) where {T} = Symmetric(similar(A.data, T)) # Hermitian version can be simplified when check for imaginary part of # diagonal in Hermitian has been removed -function similar{T}(A::Hermitian, ::Type{T}) +function similar(A::Hermitian, ::Type{T}) where T B = similar(A.data, T) for i = 1:size(A,1) B[i,i] = 0 @@ -131,12 +131,12 @@ convert(::Type{Matrix}, A::Hermitian) = copytri!(convert(Matrix, copy(A.data)), convert(::Type{Array}, A::Union{Symmetric,Hermitian}) = convert(Matrix, A) full(A::Union{Symmetric,Hermitian}) = convert(Array, A) parent(A::HermOrSym) = A.data -convert{T,S<:AbstractMatrix}(::Type{Symmetric{T,S}},A::Symmetric{T,S}) = A -convert{T,S<:AbstractMatrix}(::Type{Symmetric{T,S}},A::Symmetric) = Symmetric{T,S}(convert(S,A.data),A.uplo) -convert{T}(::Type{AbstractMatrix{T}}, A::Symmetric) = Symmetric(convert(AbstractMatrix{T}, A.data), Symbol(A.uplo)) -convert{T,S<:AbstractMatrix}(::Type{Hermitian{T,S}},A::Hermitian{T,S}) = A -convert{T,S<:AbstractMatrix}(::Type{Hermitian{T,S}},A::Hermitian) = Hermitian{T,S}(convert(S,A.data),A.uplo) -convert{T}(::Type{AbstractMatrix{T}}, A::Hermitian) = Hermitian(convert(AbstractMatrix{T}, A.data), Symbol(A.uplo)) +convert(::Type{Symmetric{T,S}},A::Symmetric{T,S}) where {T,S<:AbstractMatrix} = A +convert(::Type{Symmetric{T,S}},A::Symmetric) where {T,S<:AbstractMatrix} = Symmetric{T,S}(convert(S,A.data),A.uplo) +convert(::Type{AbstractMatrix{T}}, A::Symmetric) where {T} = Symmetric(convert(AbstractMatrix{T}, A.data), Symbol(A.uplo)) +convert(::Type{Hermitian{T,S}},A::Hermitian{T,S}) where {T,S<:AbstractMatrix} = A +convert(::Type{Hermitian{T,S}},A::Hermitian) where {T,S<:AbstractMatrix} = Hermitian{T,S}(convert(S,A.data),A.uplo) +convert(::Type{AbstractMatrix{T}}, A::Hermitian) where {T} = Hermitian(convert(AbstractMatrix{T}, A.data), Symbol(A.uplo)) copy{T,S}(A::Symmetric{T,S}) = (B = copy(A.data); Symmetric{T,typeof(B)}(B,A.uplo)) copy{T,S}(A::Hermitian{T,S}) = (B = copy(A.data); Hermitian{T,typeof(B)}(B,A.uplo)) @@ -230,7 +230,7 @@ function triu(A::Symmetric, k::Integer=0) end end --{Tv,S<:AbstractMatrix}(A::Symmetric{Tv,S}) = Symmetric{Tv,S}(-A.data, A.uplo) +(-)(A::Symmetric{Tv,S}) where {Tv,S<:AbstractMatrix} = Symmetric{Tv,S}(-A.data, A.uplo) ## Matvec A_mul_B!{T<:BlasFloat}(y::StridedVector{T}, A::Symmetric{T,<:StridedMatrix}, x::StridedVector{T}) = BLAS.symv!(A.uplo, one(T), A.data, x, zero(T), y) diff --git a/base/linalg/triangular.jl b/base/linalg/triangular.jl index 53dc720c2138c..dec4a33a91978 100644 --- a/base/linalg/triangular.jl +++ b/base/linalg/triangular.jl @@ -61,13 +61,13 @@ parent(A::AbstractTriangular) = A.data # then handle all methods that requires specific handling of upper/lower and unit diagonal -function convert{T}(::Type{Matrix{T}}, A::LowerTriangular) +function convert(::Type{Matrix{T}}, A::LowerTriangular) where T B = Array{T}(size(A, 1), size(A, 1)) copy!(B, A.data) tril!(B) B end -function convert{T}(::Type{Matrix{T}}, A::UnitLowerTriangular) +function convert(::Type{Matrix{T}}, A::UnitLowerTriangular) where T B = Array{T}(size(A, 1), size(A, 1)) copy!(B, A.data) tril!(B) @@ -76,13 +76,13 @@ function convert{T}(::Type{Matrix{T}}, A::UnitLowerTriangular) end B end -function convert{T}(::Type{Matrix{T}}, A::UpperTriangular) +function convert(::Type{Matrix{T}}, A::UpperTriangular) where T B = Array{T}(size(A, 1), size(A, 1)) copy!(B, A.data) triu!(B) B end -function convert{T}(::Type{Matrix{T}}, A::UnitUpperTriangular) +function convert(::Type{Matrix{T}}, A::UnitUpperTriangular) where T B = Array{T}(size(A, 1), size(A, 1)) copy!(B, A.data) triu!(B) @@ -119,11 +119,11 @@ function full!(A::UnitUpperTriangular) B end -getindex{T}(A::UnitLowerTriangular{T}, i::Integer, j::Integer) = +getindex(A::UnitLowerTriangular{T}, i::Integer, j::Integer) where {T} = i > j ? A.data[i,j] : ifelse(i == j, oneunit(T), zero(T)) getindex(A::LowerTriangular, i::Integer, j::Integer) = i >= j ? A.data[i,j] : zero(A.data[j,i]) -getindex{T}(A::UnitUpperTriangular{T}, i::Integer, j::Integer) = +getindex(A::UnitUpperTriangular{T}, i::Integer, j::Integer) where {T} = i < j ? A.data[i,j] : ifelse(i == j, oneunit(T), zero(T)) getindex(A::UpperTriangular, i::Integer, j::Integer) = i <= j ? A.data[i,j] : zero(A.data[j,i]) @@ -324,7 +324,7 @@ function -(A::UnitUpperTriangular) end # copy and scale -function copy!{T<:Union{UpperTriangular, UnitUpperTriangular}}(A::T, B::T) +function copy!(A::T, B::T) where T<:Union{UpperTriangular,UnitUpperTriangular} n = size(B,1) for j = 1:n for i = 1:(isa(B, UnitUpperTriangular)?j-1:j) @@ -333,7 +333,7 @@ function copy!{T<:Union{UpperTriangular, UnitUpperTriangular}}(A::T, B::T) end return A end -function copy!{T<:Union{LowerTriangular, UnitLowerTriangular}}(A::T, B::T) +function copy!(A::T, B::T) where T<:Union{LowerTriangular,UnitLowerTriangular} n = size(B,1) for j = 1:n for i = (isa(B, UnitLowerTriangular)?j+1:j):n @@ -343,7 +343,7 @@ function copy!{T<:Union{LowerTriangular, UnitLowerTriangular}}(A::T, B::T) return A end -function scale!(A::UpperTriangular, B::Union{UpperTriangular, UnitUpperTriangular}, c::Number) +function scale!(A::UpperTriangular, B::Union{UpperTriangular,UnitUpperTriangular}, c::Number) n = checksquare(B) for j = 1:n if isa(B, UnitUpperTriangular) @@ -355,7 +355,7 @@ function scale!(A::UpperTriangular, B::Union{UpperTriangular, UnitUpperTriangula end return A end -function scale!(A::LowerTriangular, B::Union{LowerTriangular, UnitLowerTriangular}, c::Number) +function scale!(A::LowerTriangular, B::Union{LowerTriangular,UnitLowerTriangular}, c::Number) n = checksquare(B) for j = 1:n if isa(B, UnitLowerTriangular) diff --git a/base/linalg/tridiag.jl b/base/linalg/tridiag.jl index 3ac3810aba47d..faa5d53624706 100644 --- a/base/linalg/tridiag.jl +++ b/base/linalg/tridiag.jl @@ -61,11 +61,11 @@ function SymTridiagonal(A::AbstractMatrix) end end -convert{T}(::Type{SymTridiagonal{T}}, S::SymTridiagonal) = +convert(::Type{SymTridiagonal{T}}, S::SymTridiagonal) where {T} = SymTridiagonal(convert(Vector{T}, S.dv), convert(Vector{T}, S.ev)) -convert{T}(::Type{AbstractMatrix{T}}, S::SymTridiagonal) = +convert(::Type{AbstractMatrix{T}}, S::SymTridiagonal) where {T} = SymTridiagonal(convert(Vector{T}, S.dv), convert(Vector{T}, S.ev)) -function convert{T}(::Type{Matrix{T}}, M::SymTridiagonal{T}) +function convert(::Type{Matrix{T}}, M::SymTridiagonal{T}) where T n = size(M, 1) Mf = zeros(T, n, n) @inbounds begin @@ -78,7 +78,7 @@ function convert{T}(::Type{Matrix{T}}, M::SymTridiagonal{T}) end return Mf end -convert{T}(::Type{Matrix}, M::SymTridiagonal{T}) = convert(Matrix{T}, M) +convert(::Type{Matrix}, M::SymTridiagonal{T}) where {T} = convert(Matrix{T}, M) convert(::Type{Array}, M::SymTridiagonal) = convert(Matrix, M) full(M::SymTridiagonal) = convert(Array, M) @@ -93,7 +93,7 @@ function size(A::SymTridiagonal, d::Integer) end end -similar{T}(S::SymTridiagonal, ::Type{T}) = SymTridiagonal{T}(similar(S.dv, T), similar(S.ev, T)) +similar(S::SymTridiagonal, ::Type{T}) where {T} = SymTridiagonal{T}(similar(S.dv, T), similar(S.ev, T)) #Elementary operations broadcast(::typeof(abs), M::SymTridiagonal) = SymTridiagonal(abs.(M.dv), abs.(M.ev)) @@ -359,7 +359,7 @@ end inv(A::SymTridiagonal) = inv_usmani(A.ev, A.dv, A.ev) det(A::SymTridiagonal) = det_usmani(A.ev, A.dv, A.ev) -function getindex{T}(A::SymTridiagonal{T}, i::Integer, j::Integer) +function getindex(A::SymTridiagonal{T}, i::Integer, j::Integer) where T if !(1 <= i <= size(A,2) && 1 <= j <= size(A,2)) throw(BoundsError(A, (i,j))) end @@ -485,7 +485,7 @@ function size(M::Tridiagonal, d::Integer) end end -function convert{T}(::Type{Matrix{T}}, M::Tridiagonal{T}) +function convert(::Type{Matrix{T}}, M::Tridiagonal{T}) where T A = zeros(T, size(M)) for i = 1:length(M.d) A[i,i] = M.d[i] @@ -496,10 +496,10 @@ function convert{T}(::Type{Matrix{T}}, M::Tridiagonal{T}) end A end -convert{T}(::Type{Matrix}, M::Tridiagonal{T}) = convert(Matrix{T}, M) +convert(::Type{Matrix}, M::Tridiagonal{T}) where {T} = convert(Matrix{T}, M) convert(::Type{Array}, M::Tridiagonal) = convert(Matrix, M) full(M::Tridiagonal) = convert(Array, M) -function similar{T}(M::Tridiagonal, ::Type{T}) +function similar(M::Tridiagonal, ::Type{T}) where T Tridiagonal{T}(similar(M.dl, T), similar(M.d, T), similar(M.du, T), similar(M.du2, T)) end @@ -543,7 +543,7 @@ function diag{T}(M::Tridiagonal{T}, n::Integer=0) end end -function getindex{T}(A::Tridiagonal{T}, i::Integer, j::Integer) +function getindex(A::Tridiagonal{T}, i::Integer, j::Integer) where T if !(1 <= i <= size(A,2) && 1 <= j <= size(A,2)) throw(BoundsError(A, (i,j))) end @@ -634,10 +634,10 @@ end inv(A::Tridiagonal) = inv_usmani(A.dl, A.d, A.du) det(A::Tridiagonal) = det_usmani(A.dl, A.d, A.du) -convert{T}(::Type{Tridiagonal{T}},M::Tridiagonal) = Tridiagonal(convert(Vector{T}, M.dl), convert(Vector{T}, M.d), convert(Vector{T}, M.du), convert(Vector{T}, M.du2)) -convert{T}(::Type{AbstractMatrix{T}},M::Tridiagonal) = convert(Tridiagonal{T}, M) -convert{T}(::Type{Tridiagonal{T}}, M::SymTridiagonal{T}) = Tridiagonal(M) -function convert{T}(::Type{SymTridiagonal{T}}, M::Tridiagonal) +convert(::Type{Tridiagonal{T}},M::Tridiagonal) where {T} = Tridiagonal(convert(Vector{T}, M.dl), convert(Vector{T}, M.d), convert(Vector{T}, M.du), convert(Vector{T}, M.du2)) +convert(::Type{AbstractMatrix{T}},M::Tridiagonal) where {T} = convert(Tridiagonal{T}, M) +convert(::Type{Tridiagonal{T}}, M::SymTridiagonal{T}) where {T} = Tridiagonal(M) +function convert(::Type{SymTridiagonal{T}}, M::Tridiagonal) where T if M.dl == M.du return SymTridiagonal(convert(Vector{T},M.d), convert(Vector{T},M.dl)) else diff --git a/base/linalg/uniformscaling.jl b/base/linalg/uniformscaling.jl index d976aacc43f6f..fb4d2fd661bef 100644 --- a/base/linalg/uniformscaling.jl +++ b/base/linalg/uniformscaling.jl @@ -27,7 +27,7 @@ julia> [1 2im 3; 1im 2 3] * I """ const I = UniformScaling(1) -eltype{T}(::Type{UniformScaling{T}}) = T +eltype(::Type{UniformScaling{T}}) where {T} = T ndims(J::UniformScaling) = 2 getindex(J::UniformScaling, i::Integer,j::Integer) = ifelse(i==j,J.λ,zero(J.λ)) @@ -37,12 +37,12 @@ copy(J::UniformScaling) = UniformScaling(J.λ) transpose(J::UniformScaling) = J ctranspose(J::UniformScaling) = UniformScaling(conj(J.λ)) -one{T}(::Type{UniformScaling{T}}) = UniformScaling(one(T)) -one{T}(J::UniformScaling{T}) = one(UniformScaling{T}) -oneunit{T}(::Type{UniformScaling{T}}) = UniformScaling(oneunit(T)) -oneunit{T}(J::UniformScaling{T}) = oneunit(UniformScaling{T}) -zero{T}(::Type{UniformScaling{T}}) = UniformScaling(zero(T)) -zero{T}(J::UniformScaling{T}) = zero(UniformScaling{T}) +one(::Type{UniformScaling{T}}) where {T} = UniformScaling(one(T)) +one(J::UniformScaling{T}) where {T} = one(UniformScaling{T}) +oneunit(::Type{UniformScaling{T}}) where {T} = UniformScaling(oneunit(T)) +oneunit(J::UniformScaling{T}) where {T} = oneunit(UniformScaling{T}) +zero(::Type{UniformScaling{T}}) where {T} = UniformScaling(zero(T)) +zero(J::UniformScaling{T}) where {T} = zero(UniformScaling{T}) istriu(::UniformScaling) = true istril(::UniformScaling) = true @@ -109,7 +109,7 @@ function (-)(J::UniformScaling, UL::Union{LowerTriangular,UnitLowerTriangular}) return LowerTriangular(ULnew) end -function (+){TA,TJ}(A::AbstractMatrix{TA}, J::UniformScaling{TJ}) +function (+)(A::AbstractMatrix{TA}, J::UniformScaling{TJ}) where {TA,TJ} n = checksquare(A) B = similar(A, promote_type(TA,TJ)) copy!(B,A) @@ -119,7 +119,7 @@ function (+){TA,TJ}(A::AbstractMatrix{TA}, J::UniformScaling{TJ}) B end -function (-){TA,TJ<:Number}(A::AbstractMatrix{TA}, J::UniformScaling{TJ}) +function (-)(A::AbstractMatrix{TA}, J::UniformScaling{TJ}) where {TA,TJ<:Number} n = checksquare(A) B = similar(A, promote_type(TA,TJ)) copy!(B, A) @@ -128,7 +128,7 @@ function (-){TA,TJ<:Number}(A::AbstractMatrix{TA}, J::UniformScaling{TJ}) end B end -function (-){TA,TJ<:Number}(J::UniformScaling{TJ}, A::AbstractMatrix{TA}) +function (-)(J::UniformScaling{TJ}, A::AbstractMatrix{TA}) where {TA,TJ<:Number} n = checksquare(A) B = convert(AbstractMatrix{promote_type(TJ,TA)}, -A) @inbounds for j = 1:n @@ -154,7 +154,7 @@ inv(J::UniformScaling) = UniformScaling(inv(J.λ)) /(J::UniformScaling, x::Number) = UniformScaling(J.λ/x) \(J1::UniformScaling, J2::UniformScaling) = J1.λ == 0 ? throw(SingularException(1)) : UniformScaling(J1.λ\J2.λ) -\{T<:Number}(A::Union{Bidiagonal{T},AbstractTriangular{T}}, J::UniformScaling) = scale!(inv(A), J.λ) +\(A::Union{Bidiagonal{T},AbstractTriangular{T}}, J::UniformScaling) where {T<:Number} = scale!(inv(A), J.λ) \(J::UniformScaling, A::AbstractVecOrMat) = J.λ == 0 ? throw(SingularException(1)) : J.λ\A \(A::AbstractMatrix, J::UniformScaling) = scale!(inv(A), J.λ) @@ -167,8 +167,8 @@ broadcast(::typeof(/), J::UniformScaling,x::Number) = UniformScaling(J.λ/x) ==(J1::UniformScaling,J2::UniformScaling) = (J1.λ == J2.λ) -function isapprox{T<:Number,S<:Number}(J1::UniformScaling{T}, J2::UniformScaling{S}; - rtol::Real=Base.rtoldefault(T,S), atol::Real=0, nans::Bool=false) +function isapprox(J1::UniformScaling{T}, J2::UniformScaling{S}; + rtol::Real=Base.rtoldefault(T,S), atol::Real=0, nans::Bool=false) where {T<:Number,S<:Number} isapprox(J1.λ, J2.λ, rtol=rtol, atol=atol, nans=nans) end @@ -182,7 +182,7 @@ function copy!(A::AbstractMatrix, J::UniformScaling) return A end -function cond{T}(J::UniformScaling{T}) +function cond(J::UniformScaling{T}) where T onereal = inv(one(real(J.λ))) return J.λ ≠ zero(T) ? onereal : oftype(onereal, Inf) end diff --git a/base/markdown/render/html.jl b/base/markdown/render/html.jl index 7e2f256f4aa5c..fc1962270da52 100644 --- a/base/markdown/render/html.jl +++ b/base/markdown/render/html.jl @@ -39,12 +39,12 @@ end function htmlesc(io::IO, s::Symbol) htmlesc(io, string(s)) end -function htmlesc(io::IO, xs::Union{AbstractString, Symbol}...) +function htmlesc(io::IO, xs::Union{AbstractString,Symbol}...) for s in xs htmlesc(io, s) end end -function htmlesc(s::Union{AbstractString, Symbol}) +function htmlesc(s::Union{AbstractString,Symbol}) sprint(htmlesc, s) end @@ -139,7 +139,7 @@ function htmlinline(io::IO, code::Code) end end -function htmlinline(io::IO, md::Union{Symbol, AbstractString}) +function htmlinline(io::IO, md::Union{Symbol,AbstractString}) htmlesc(io, md) end diff --git a/base/multidimensional.jl b/base/multidimensional.jl index e519f293c834d..bf275c92701b0 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -59,33 +59,33 @@ module IteratorsMD getindex(index::CartesianIndex, i::Integer) = index.I[i] # zeros and ones - zero{N}(::CartesianIndex{N}) = zero(CartesianIndex{N}) - zero{N}(::Type{CartesianIndex{N}}) = CartesianIndex(ntuple(x -> 0, Val{N})) - one{N}(::CartesianIndex{N}) = one(CartesianIndex{N}) - one{N}(::Type{CartesianIndex{N}}) = CartesianIndex(ntuple(x -> 1, Val{N})) + zero(::CartesianIndex{N}) where {N} = zero(CartesianIndex{N}) + zero(::Type{CartesianIndex{N}}) where {N} = CartesianIndex(ntuple(x -> 0, Val{N})) + one(::CartesianIndex{N}) where {N} = one(CartesianIndex{N}) + one(::Type{CartesianIndex{N}}) where {N} = CartesianIndex(ntuple(x -> 1, Val{N})) # arithmetic, min/max - @inline (-){N}(index::CartesianIndex{N}) = + @inline (-)(index::CartesianIndex{N}) where {N} = CartesianIndex{N}(map(-, index.I)) - @inline (+){N}(index1::CartesianIndex{N}, index2::CartesianIndex{N}) = + @inline (+)(index1::CartesianIndex{N}, index2::CartesianIndex{N}) where {N} = CartesianIndex{N}(map(+, index1.I, index2.I)) - @inline (-){N}(index1::CartesianIndex{N}, index2::CartesianIndex{N}) = + @inline (-)(index1::CartesianIndex{N}, index2::CartesianIndex{N}) where {N} = CartesianIndex{N}(map(-, index1.I, index2.I)) - @inline min{N}(index1::CartesianIndex{N}, index2::CartesianIndex{N}) = + @inline min(index1::CartesianIndex{N}, index2::CartesianIndex{N}) where {N} = CartesianIndex{N}(map(min, index1.I, index2.I)) - @inline max{N}(index1::CartesianIndex{N}, index2::CartesianIndex{N}) = + @inline max(index1::CartesianIndex{N}, index2::CartesianIndex{N}) where {N} = CartesianIndex{N}(map(max, index1.I, index2.I)) @inline (+)(i::Integer, index::CartesianIndex) = index+i - @inline (+){N}(index::CartesianIndex{N}, i::Integer) = CartesianIndex{N}(map(x->x+i, index.I)) - @inline (-){N}(index::CartesianIndex{N}, i::Integer) = CartesianIndex{N}(map(x->x-i, index.I)) - @inline (-){N}(i::Integer, index::CartesianIndex{N}) = CartesianIndex{N}(map(x->i-x, index.I)) - @inline (*){N}(a::Integer, index::CartesianIndex{N}) = CartesianIndex{N}(map(x->a*x, index.I)) - @inline (*)(index::CartesianIndex,a::Integer)=*(a,index) + @inline (+)(index::CartesianIndex{N}, i::Integer) where {N} = CartesianIndex{N}(map(x->x+i, index.I)) + @inline (-)(index::CartesianIndex{N}, i::Integer) where {N} = CartesianIndex{N}(map(x->x-i, index.I)) + @inline (-)(i::Integer, index::CartesianIndex{N}) where {N} = CartesianIndex{N}(map(x->i-x, index.I)) + @inline (*)(a::Integer, index::CartesianIndex{N}) where {N} = CartesianIndex{N}(map(x->a*x, index.I)) + @inline (*)(index::CartesianIndex, a::Integer) = *(a,index) # comparison - @inline isless{N}(I1::CartesianIndex{N}, I2::CartesianIndex{N}) = _isless(0, I1.I, I2.I) - @inline function _isless{N}(ret, I1::NTuple{N,Int}, I2::NTuple{N,Int}) + @inline isless(I1::CartesianIndex{N}, I2::CartesianIndex{N}) where {N} = _isless(0, I1.I, I2.I) + @inline function _isless(ret, I1::NTuple{N,Int}, I2::NTuple{N,Int}) where N newret = ifelse(ret==0, icmp(I1[N], I2[N]), ret) _isless(newret, Base.front(I1), Base.front(I2)) end @@ -133,7 +133,7 @@ module IteratorsMD convert(Tuple{Vararg{UnitRange{Int}}}, R) ndims(R::CartesianRange) = length(R.start) - ndims{I<:CartesianIndex}(::Type{CartesianRange{I}}) = length(I) + ndims(::Type{CartesianRange{I}}) where {I<:CartesianIndex} = length(I) eachindex(::IndexCartesian, A::AbstractArray) = CartesianRange(indices(A)) @@ -155,7 +155,7 @@ module IteratorsMD end iter.start end - @inline function next{I<:CartesianIndex}(iter::CartesianRange{I}, state) + @inline function next(iter::CartesianRange{I}, state) where I<:CartesianIndex state, I(inc(state.I, iter.start.I, iter.stop.I)) end # increment & carry @@ -283,7 +283,7 @@ end @inline function index_ndims(i1::CartesianIndex, I...) (map(x->true, i1.I)..., index_ndims(I...)...) end -@inline function index_ndims{N}(i1::AbstractArray{CartesianIndex{N}}, I...) +@inline function index_ndims(i1::AbstractArray{CartesianIndex{N}}, I...) where N (ntuple(x->true, Val{N})..., index_ndims(I...)...) end index_ndims() = () @@ -528,13 +528,13 @@ end # see discussion in #18364 ... we try not to widen type of the resulting array # from cumsum or cumprod, but in some cases (+, Bool) we may not have a choice. -rcum_promote_type{T,S<:Number}(op, ::Type{T}, ::Type{S}) = promote_op(op, T, S) -rcum_promote_type{T<:Number}(op, ::Type{T}) = rcum_promote_type(op, T,T) -rcum_promote_type{T}(op, ::Type{T}) = T +rcum_promote_type(op, ::Type{T}, ::Type{S}) where {T,S<:Number} = promote_op(op, T, S) +rcum_promote_type(op, ::Type{T}) where {T<:Number} = rcum_promote_type(op, T,T) +rcum_promote_type(op, ::Type{T}) where {T} = T # handle sums of Vector{Bool} and similar. it would be nice to handle # any AbstractArray here, but it's not clear how that would be possible -rcum_promote_type{T,N}(op, ::Type{Array{T,N}}) = Array{rcum_promote_type(op,T), N} +rcum_promote_type(op, ::Type{Array{T,N}}) where {T,N} = Array{rcum_promote_type(op,T), N} # accumulate_pairwise slightly slower then accumulate, but more numerically # stable in certain situations (e.g. sums). @@ -784,7 +784,7 @@ end ### from abstractarray.jl -function fill!{T}(A::AbstractArray{T}, x) +function fill!(A::AbstractArray{T}, x) where T xT = convert(T, x) for I in eachindex(A) @inbounds A[I] = xT @@ -799,7 +799,7 @@ Copy all elements from collection `src` to array `dest`. """ copy!(dest, src) -function copy!{T,N}(dest::AbstractArray{T,N}, src::AbstractArray{T,N}) +function copy!(dest::AbstractArray{T,N}, src::AbstractArray{T,N}) where {T,N} @boundscheck checkbounds(dest, indices(src)...) for I in eachindex(IndexStyle(src,dest), src) @inbounds dest[I] = src[I] @@ -1195,7 +1195,7 @@ end ## findn -@generated function findn{N}(B::BitArray{N}) +@generated function findn(B::BitArray{N}) where N quote nnzB = countnz(B) I = ntuple(x->Vector{Int}(nnzB), Val{$N}) @@ -1255,7 +1255,7 @@ function checkdims_perm{TP,TB,N}(P::AbstractArray{TP,N}, B::AbstractArray{TB,N}, end for (V, PT, BT) in [((:N,), BitArray, BitArray), ((:T,:N), Array, StridedArray)] - @eval @generated function permutedims!{$(V...)}(P::$PT{$(V...)}, B::$BT{$(V...)}, perm) + @eval @generated function permutedims!(P::$PT{$(V...)}, B::$BT{$(V...)}, perm) where $(V...) quote checkdims_perm(P, B, perm) diff --git a/base/multimedia.jl b/base/multimedia.jl index 9314d4b5ab553..d4c1b15f7b58a 100644 --- a/base/multimedia.jl +++ b/base/multimedia.jl @@ -18,7 +18,7 @@ import Base: MIME, @MIME_str import Base: show, print, string, convert MIME(s) = MIME{Symbol(s)}() -show{mime}(io::IO, ::MIME{mime}) = print(io, "MIME type ", string(mime)) +show(io::IO, ::MIME{mime}) where {mime} = print(io, "MIME type ", string(mime)) print{mime}(io::IO, ::MIME{mime}) = print(io, mime) ########################################################################### diff --git a/base/multinverses.jl b/base/multinverses.jl index 39109d16bcac8..cf11ca296d9d8 100644 --- a/base/multinverses.jl +++ b/base/multinverses.jl @@ -134,21 +134,21 @@ struct UnsignedMultiplicativeInverse{T<:Unsigned} <: MultiplicativeInverse{T} end UnsignedMultiplicativeInverse(x::Unsigned) = UnsignedMultiplicativeInverse{typeof(x)}(x) -function div{T}(a::T, b::SignedMultiplicativeInverse{T}) +function div(a::T, b::SignedMultiplicativeInverse{T}) where T x = ((widen(a)*b.multiplier) >>> sizeof(a)*8) % T x += (a*b.addmul) % T ifelse(abs(b.divisor) == 1, a*b.divisor, (signbit(x) + (x >> b.shift)) % T) end -function div{T}(a::T, b::UnsignedMultiplicativeInverse{T}) +function div(a::T, b::UnsignedMultiplicativeInverse{T}) where T x = ((widen(a)*b.multiplier) >>> sizeof(a)*8) % T x = ifelse(b.add, convert(T, convert(T, (convert(T, a - x) >>> 1)) + x), x) ifelse(b.divisor == 1, a, x >>> b.shift) end -rem{T}(a::T, b::MultiplicativeInverse{T}) = +rem(a::T, b::MultiplicativeInverse{T}) where {T} = a - div(a, b)*b.divisor -function divrem{T}(a::T, b::MultiplicativeInverse{T}) +function divrem(a::T, b::MultiplicativeInverse{T}) where T d = div(a, b) (d, a - d*b.divisor) end diff --git a/base/nullable.jl b/base/nullable.jl index 478e41da2d058..bda2004e39111 100644 --- a/base/nullable.jl +++ b/base/nullable.jl @@ -39,7 +39,7 @@ eltype(::Type{Nullable{T}}) where {T} = T convert(::Type{Nullable{T}}, x::Nullable{T}) where {T} = x convert(::Type{Nullable }, x::Nullable ) = x -convert{T}(t::Type{Nullable{T}}, x::Any) = convert(t, convert(T, x)) +convert(t::Type{Nullable{T}}, x::Any) where {T} = convert(t, convert(T, x)) function convert(::Type{Nullable{T}}, x::Nullable) where T return isnull(x) ? Nullable{T}() : Nullable{T}(convert(T, get(x))) @@ -52,8 +52,8 @@ convert(::Type{Nullable }, x::T) where {T} = Nullable{T}(x) convert(::Type{Nullable{T}}, ::Void) where {T} = Nullable{T}() convert(::Type{Nullable }, ::Void) = Nullable{Union{}}() -promote_rule{S,T}(::Type{Nullable{S}}, ::Type{T}) = Nullable{promote_type(S, T)} -promote_rule{S,T}(::Type{Nullable{S}}, ::Type{Nullable{T}}) = Nullable{promote_type(S, T)} +promote_rule(::Type{Nullable{S}}, ::Type{T}) where {S,T} = Nullable{promote_type(S, T)} +promote_rule(::Type{Nullable{S}}, ::Type{Nullable{T}}) where {S,T} = Nullable{promote_type(S, T)} promote_op{S,T}(op::Any, ::Type{Nullable{S}}, ::Type{Nullable{T}}) = Nullable{promote_op(op, S, T)} promote_op{S,T}(op::Type, ::Type{Nullable{S}}, ::Type{Nullable{T}}) = Nullable{promote_op(op, S, T)} @@ -277,7 +277,7 @@ implementation detail, but typically the choice that maximizes performance would be used. If `x` has a value, then the return type is guaranteed to be of type `Nullable{typeof(f(x))}`. """ -function map{T}(f, x::Nullable{T}) +function map(f, x::Nullable{T}) where T S = promote_op(f, T) if isleaftype(S) && null_safe_op(f, T) Nullable(f(unsafe_get(x)), !isnull(x)) diff --git a/base/number.jl b/base/number.jl index bd5deb2bca776..b5bf38622785d 100644 --- a/base/number.jl +++ b/base/number.jl @@ -128,7 +128,7 @@ map(f, x::Number, ys::Number...) = f(x, ys...) Get the additive identity element for the type of `x` (`x` can also specify the type itself). """ zero(x::Number) = oftype(x,0) -zero{T<:Number}(::Type{T}) = convert(T,0) +zero(::Type{T}) where {T<:Number} = convert(T,0) """ one(x) @@ -160,8 +160,8 @@ julia> one(Dates.Day(1)) 1 ``` """ -one{T<:Number}(::Type{T}) = convert(T,1) -one{T<:Number}(x::T) = one(T) +one(::Type{T}) where {T<:Number} = convert(T,1) +one(x::T) where {T<:Number} = one(T) # note that convert(T, 1) should throw an error if T is dimensionful, # so this fallback definition should be okay. @@ -182,8 +182,8 @@ julia> oneunit(Dates.Day) 1 day ``` """ -oneunit{T}(x::T) = T(one(x)) -oneunit{T}(::Type{T}) = T(one(T)) +oneunit(x::T) where {T} = T(one(x)) +oneunit(::Type{T}) where {T} = T(one(T)) _default_type(::Type{Number}) = Int diff --git a/base/pair.jl b/base/pair.jl index 0236fbbe482a5..22d496f40c8ed 100644 --- a/base/pair.jl +++ b/base/pair.jl @@ -10,7 +10,7 @@ const => = Pair start(p::Pair) = 1 done(p::Pair, i) = i>2 next(p::Pair, i) = (getfield(p,i), i+1) -eltype{A,B}(p::Pair{A,B}) = Union{A,B} +eltype(p::Pair{A,B}) where {A,B} = Union{A,B} indexed_next(p::Pair, i::Int, state) = (getfield(p,i), i+1) @@ -31,9 +31,9 @@ first(p::Pair) = p.first last(p::Pair) = p.second convert(::Type{Pair{A,B}}, x::Pair{A,B}) where {A,B} = x -function convert(::Type{Pair{A,B}}, x::Pair) where A where B - Pair{A, B}(convert(A, x[1]), convert(B, x[2])) +function convert(::Type{Pair{A,B}}, x::Pair) where {A,B} + Pair{A,B}(convert(A, x[1]), convert(B, x[2])) end -promote_rule{A1, B1, A2, B2}(::Type{Pair{A1, B1}}, ::Type{Pair{A2, B2}}) = +promote_rule(::Type{Pair{A1,B1}}, ::Type{Pair{A2,B2}}) where {A1,B1,A2,B2} = Pair{promote_type(A1, A2), promote_type(B1, B2)} diff --git a/base/pointer.jl b/base/pointer.jl index 4f14eb562431b..20f3b05c40785 100644 --- a/base/pointer.jl +++ b/base/pointer.jl @@ -20,7 +20,7 @@ const C_NULL = bitcast(Ptr{Void}, 0) # TODO: deprecate these conversions. C doesn't even allow them. # pointer to integer -convert{T<:Union{Int,UInt}}(::Type{T}, x::Ptr) = bitcast(T, x) +convert(::Type{T}, x::Ptr) where {T<:Union{Int,UInt}} = bitcast(T, x) convert(::Type{T}, x::Ptr) where {T<:Integer} = convert(T, convert(UInt, x)) # integer to pointer diff --git a/base/profile.jl b/base/profile.jl index 55816bda55ce9..7ec228c19c427 100644 --- a/base/profile.jl +++ b/base/profile.jl @@ -316,7 +316,7 @@ const btskip = 0 ## Print as a flat list # Counts the number of times each line appears, at any nesting level -function count_flat{T<:Unsigned}(data::Vector{T}) +function count_flat(data::Vector{T}) where T<:Unsigned linecount = Dict{T,Int}() toskip = btskip for ip in data diff --git a/base/promotion.jl b/base/promotion.jl index 2f78d66354aec..f7918422262e8 100644 --- a/base/promotion.jl +++ b/base/promotion.jl @@ -127,9 +127,9 @@ promote_type(T) = (@_pure_meta; T) promote_type(T, S, U, V...) = (@_pure_meta; promote_type(T, promote_type(S, U, V...))) promote_type(::Type{Bottom}, ::Type{Bottom}) = (@_pure_meta; Bottom) -promote_type{T}(::Type{T}, ::Type{T}) = (@_pure_meta; T) -promote_type{T}(::Type{T}, ::Type{Bottom}) = (@_pure_meta; T) -promote_type{T}(::Type{Bottom}, ::Type{T}) = (@_pure_meta; T) +promote_type(::Type{T}, ::Type{T}) where {T} = (@_pure_meta; T) +promote_type(::Type{T}, ::Type{Bottom}) where {T} = (@_pure_meta; T) +promote_type(::Type{Bottom}, ::Type{T}) where {T} = (@_pure_meta; T) """ promote_type(type1, type2) @@ -151,7 +151,7 @@ julia> promote_type(Float32, BigInt) BigFloat ``` """ -function promote_type{T,S}(::Type{T}, ::Type{S}) +function promote_type(::Type{T}, ::Type{S}) where {T,S} @_pure_meta # Try promote_rule in both orders. Typically only one is defined, # and there is a fallback returning Bottom below, so the common case is @@ -166,11 +166,11 @@ promote_rule(T, S) = (@_pure_meta; Bottom) promote_result(t,s,T,S) = (@_pure_meta; promote_type(T,S)) # If no promote_rule is defined, both directions give Bottom. In that # case use typejoin on the original types instead. -promote_result{T,S}(::Type{T},::Type{S},::Type{Bottom},::Type{Bottom}) = (@_pure_meta; typejoin(T, S)) +promote_result(::Type{T},::Type{S},::Type{Bottom},::Type{Bottom}) where {T,S} = (@_pure_meta; typejoin(T, S)) promote() = () promote(x) = (x,) -function promote{T,S}(x::T, y::S) +function promote(x::T, y::S) where {T,S} (convert(promote_type(T,S),x), convert(promote_type(T,S),y)) end promote_typeof(x) = (@_pure_meta; typeof(x)) @@ -194,7 +194,7 @@ end # Otherwise, typejoin(T,S) is called (returning Number) so no conversion # happens, and +(promote(x,y)...) is called again, causing a stack # overflow. -function promote_result{T<:Number,S<:Number}(::Type{T},::Type{S},::Type{Bottom},::Type{Bottom}) +function promote_result(::Type{T},::Type{S},::Type{Bottom},::Type{Bottom}) where {T<:Number,S<:Number} @_pure_meta promote_to_supertype(T, S, typejoin(T,S)) end @@ -202,10 +202,10 @@ end # promote numeric types T and S to typejoin(T,S) if T<:S or S<:T # for example this makes promote_type(Integer,Real) == Real without # promoting arbitrary pairs of numeric types to Number. -promote_to_supertype{T<:Number }(::Type{T}, ::Type{T}, ::Type{T}) = (@_pure_meta; T) -promote_to_supertype{T<:Number,S<:Number}(::Type{T}, ::Type{S}, ::Type{T}) = (@_pure_meta; T) -promote_to_supertype{T<:Number,S<:Number}(::Type{T}, ::Type{S}, ::Type{S}) = (@_pure_meta; S) -promote_to_supertype{T<:Number,S<:Number}(::Type{T}, ::Type{S}, ::Type) = +promote_to_supertype(::Type{T}, ::Type{T}, ::Type{T}) where {T<:Number} = (@_pure_meta; T) +promote_to_supertype(::Type{T}, ::Type{S}, ::Type{T}) where {T<:Number,S<:Number} = (@_pure_meta; T) +promote_to_supertype(::Type{T}, ::Type{S}, ::Type{S}) where {T<:Number,S<:Number} = (@_pure_meta; S) +promote_to_supertype(::Type{T}, ::Type{S}, ::Type) where {T<:Number,S<:Number} = error("no promotion exists for ", T, " and ", S) # promotion with a check for circularity. Can be used to catch what @@ -229,9 +229,9 @@ function promote_noncircular(x, y, z, a...) end not_all_sametype(x, y) = nothing not_all_sametype(x, y, z) = nothing -not_all_sametype{S,T}(x::Tuple{S,S}, y::Tuple{T,T}) = sametype_error(x[1], y[1]) -not_all_sametype{R,S,T}(x::Tuple{R,R}, y::Tuple{S,S}, z::Tuple{T,T}) = sametype_error(x[1], y[1], z[1]) -function not_all_sametype{R,S,T}(::Tuple{R,R}, y::Tuple{S,S}, z::Tuple{T,T}, args...) +not_all_sametype(x::Tuple{S,S}, y::Tuple{T,T}) where {S,T} = sametype_error(x[1], y[1]) +not_all_sametype(x::Tuple{R,R}, y::Tuple{S,S}, z::Tuple{T,T}) where {R,S,T} = sametype_error(x[1], y[1], z[1]) +function not_all_sametype(::Tuple{R,R}, y::Tuple{S,S}, z::Tuple{T,T}, args...) where {R,S,T} @_inline_meta not_all_sametype(y, z, args...) end @@ -329,31 +329,31 @@ end ## catch-alls to prevent infinite recursion when definitions are missing ## no_op_err(name, T) = error(name," not defined for ",T) -+{T<:Number}(x::T, y::T) = no_op_err("+", T) -*{T<:Number}(x::T, y::T) = no_op_err("*", T) --{T<:Number}(x::T, y::T) = no_op_err("-", T) -/{T<:Number}(x::T, y::T) = no_op_err("/", T) -^{T<:Number}(x::T, y::T) = no_op_err("^", T) +(+)(x::T, y::T) where {T<:Number} = no_op_err("+", T) +(*)(x::T, y::T) where {T<:Number} = no_op_err("*", T) +(-)(x::T, y::T) where {T<:Number} = no_op_err("-", T) +(/)(x::T, y::T) where {T<:Number} = no_op_err("/", T) +(^)(x::T, y::T) where {T<:Number} = no_op_err("^", T) -fma{T<:Number}(x::T, y::T, z::T) = no_op_err("fma", T) +fma(x::T, y::T, z::T) where {T<:Number} = no_op_err("fma", T) fma(x::Integer, y::Integer, z::Integer) = x*y+z -muladd{T<:Number}(x::T, y::T, z::T) = x*y+z +muladd(x::T, y::T, z::T) where {T<:Number} = x*y+z -(&){T<:Integer}(x::T, y::T) = no_op_err("&", T) -(|){T<:Integer}(x::T, y::T) = no_op_err("|", T) -xor{T<:Integer}(x::T, y::T) = no_op_err("xor", T) +(&)(x::T, y::T) where {T<:Integer} = no_op_err("&", T) +(|)(x::T, y::T) where {T<:Integer} = no_op_err("|", T) +xor(x::T, y::T) where {T<:Integer} = no_op_err("xor", T) -=={T<:Number}(x::T, y::T) = x === y - <{T<:Real}(x::T, y::T) = no_op_err("<" , T) -<={T<:Real}(x::T, y::T) = no_op_err("<=", T) +(==)(x::T, y::T) where {T<:Number} = x === y +(< )(x::T, y::T) where {T<:Real} = no_op_err("<" , T) +(<=)(x::T, y::T) where {T<:Real} = no_op_err("<=", T) -rem{T<:Real}(x::T, y::T) = no_op_err("rem", T) -mod{T<:Real}(x::T, y::T) = no_op_err("mod", T) +rem(x::T, y::T) where {T<:Real} = no_op_err("rem", T) +mod(x::T, y::T) where {T<:Real} = no_op_err("mod", T) min(x::Real) = x max(x::Real) = x minmax(x::Real) = (x, x) -max{T<:Real}(x::T, y::T) = select_value(y < x, x, y) -min{T<:Real}(x::T, y::T) = select_value(y < x, y, x) -minmax{T<:Real}(x::T, y::T) = y < x ? (y, x) : (x, y) +max(x::T, y::T) where {T<:Real} = select_value(y < x, x, y) +min(x::T, y::T) where {T<:Real} = select_value(y < x, y, x) +minmax(x::T, y::T) where {T<:Real} = y < x ? (y, x) : (x, y) diff --git a/base/range.jl b/base/range.jl index f3d5cfca90c5c..393b8cd9d3dd7 100644 --- a/base/range.jl +++ b/base/range.jl @@ -2,17 +2,17 @@ colon(a::Real, b::Real) = colon(promote(a,b)...) -colon{T<:Real}(start::T, stop::T) = UnitRange{T}(start, stop) +colon(start::T, stop::T) where {T<:Real} = UnitRange{T}(start, stop) range(a::Real, len::Integer) = UnitRange{typeof(a)}(a, oftype(a, a+len-1)) -colon{T}(start::T, stop::T) = colon(start, oftype(stop-start, 1), stop) +colon(start::T, stop::T) where {T} = colon(start, oftype(stop-start, 1), stop) range(a, len::Integer) = range(a, oftype(a-a, 1), len) # first promote start and stop, leaving step alone -colon{A<:Real,C<:Real}(start::A, step, stop::C) = colon(convert(promote_type(A,C),start), step, convert(promote_type(A,C),stop)) -colon{T<:Real}(start::T, step::Real, stop::T) = colon(promote(start, step, stop)...) +colon(start::A, step, stop::C) where {A<:Real,C<:Real} = colon(convert(promote_type(A,C),start), step, convert(promote_type(A,C),stop)) +colon(start::T, step::Real, stop::T) where {T<:Real} = colon(promote(start, step, stop)...) """ colon(start, [step], stop) @@ -24,8 +24,8 @@ julia> colon(1, 2, 5) 1:2:5 ``` """ -colon{T<:AbstractFloat}(start::T, step::T, stop::T) = _colon(TypeOrder(T), TypeArithmetic(T), start, step, stop) -colon{T<:Real}(start::T, step::T, stop::T) = _colon(TypeOrder(T), TypeArithmetic(T), start, step, stop) +colon(start::T, step::T, stop::T) where {T<:AbstractFloat} = _colon(TypeOrder(T), TypeArithmetic(T), start, step, stop) +colon(start::T, step::T, stop::T) where {T<:Real} = _colon(TypeOrder(T), TypeArithmetic(T), start, step, stop) _colon{T}(::HasOrder, ::Any, start::T, step, stop::T) = StepRange(start, step, stop) # for T<:Union{Float16,Float32,Float64} see twiceprecision.jl _colon{T}(::HasOrder, ::ArithmeticRounds, start::T, step, stop::T) = StepRangeLen(start, step, floor(Int, (stop-start)/step)+1) @@ -38,8 +38,8 @@ Range operator. `a:b` constructs a range from `a` to `b` with a step size of 1, is similar but uses a step size of `s`. These syntaxes call the function `colon`. The colon is also used in indexing to select whole dimensions. """ -colon{T}(start::T, step, stop::T) = _colon(start, step, stop) -colon{T<:Real}(start::T, step, stop::T) = _colon(start, step, stop) +colon(start::T, step, stop::T) where {T} = _colon(start, step, stop) +colon(start::T, step, stop::T) where {T<:Real} = _colon(start, step, stop) # without the second method above, the first method above is ambiguous with # colon{A<:Real,C<:Real}(start::A, step, stop::C) function _colon{T}(start::T, step, stop::T) @@ -57,12 +57,12 @@ _range{T,S}(::HasOrder, ::ArithmeticOverflows, a::T, step::S, len::Integer) = St _range{T,S}(::Any, ::Any, a::T, step::S, len::Integer) = StepRangeLen{typeof(a+0*step),T,S}(a, step, len) # AbstractFloat specializations -colon{T<:AbstractFloat}(a::T, b::T) = colon(a, T(1), b) +colon(a::T, b::T) where {T<:AbstractFloat} = colon(a, T(1), b) range(a::AbstractFloat, len::Integer) = range(a, oftype(a, 1), len) -colon{T<:Real}(a::T, b::AbstractFloat, c::T) = colon(promote(a,b,c)...) -colon{T<:AbstractFloat}(a::T, b::AbstractFloat, c::T) = colon(promote(a,b,c)...) -colon{T<:AbstractFloat}(a::T, b::Real, c::T) = colon(promote(a,b,c)...) +colon(a::T, b::AbstractFloat, c::T) where {T<:Real} = colon(promote(a,b,c)...) +colon(a::T, b::AbstractFloat, c::T) where {T<:AbstractFloat} = colon(promote(a,b,c)...) +colon(a::T, b::Real, c::T) where {T<:AbstractFloat} = colon(promote(a,b,c)...) range(a::AbstractFloat, st::AbstractFloat, len::Integer) = range(promote(a, st)..., len) range(a::Real, st::AbstractFloat, len::Integer) = range(float(a), st, len) @@ -140,9 +140,9 @@ StepRange(start::T, step::S, stop::T) where {T,S} = StepRange{T,S}(start, step, struct UnitRange{T<:Real} <: AbstractUnitRange{T} start::T stop::T - UnitRange{T}(start, stop) where T<:Real = new(start, unitrange_last(start,stop)) + UnitRange{T}(start, stop) where {T<:Real} = new(start, unitrange_last(start,stop)) end -UnitRange(start::T, stop::T) where T<:Real = UnitRange{T}(start, stop) +UnitRange(start::T, stop::T) where {T<:Real} = UnitRange{T}(start, stop) unitrange_last(::Bool, stop::Bool) = stop unitrange_last{T<:Integer}(start::T, stop::T) = @@ -165,9 +165,9 @@ be 1. """ struct OneTo{T<:Integer} <: AbstractUnitRange{T} stop::T - OneTo{T}(stop) where T<:Integer = new(max(zero(T), stop)) + OneTo{T}(stop) where {T<:Integer} = new(max(zero(T), stop)) end -OneTo(stop::T) where T<:Integer = OneTo{T}(stop) +OneTo(stop::T) where {T<:Integer} = OneTo{T}(stop) ## Step ranges parametrized by length @@ -432,21 +432,21 @@ function next(r::LinSpace, i::Int) end start(r::StepRange) = oftype(r.start + r.step, r.start) -next{T}(r::StepRange{T}, i) = (convert(T,i), i+r.step) +next(r::StepRange{T}, i) where {T} = (convert(T,i), i+r.step) done(r::StepRange, i) = isempty(r) | (i < min(r.start, r.stop)) | (i > max(r.start, r.stop)) done(r::StepRange, i::Integer) = isempty(r) | (i == oftype(i, r.stop) + r.step) # see also twiceprecision.jl start(r::StepRangeLen) = (unsafe_getindex(r, 1), 1) -next{T}(r::StepRangeLen{T}, s) = s[1], (T(s[1]+r.step), s[2]+1) +next(r::StepRangeLen{T}, s) where {T} = s[1], (T(s[1]+r.step), s[2]+1) done(r::StepRangeLen, s) = s[2] > length(r) -start{T}(r::UnitRange{T}) = oftype(r.start + oneunit(T), r.start) -next{T}(r::AbstractUnitRange{T}, i) = (convert(T, i), i + oneunit(T)) -done{T}(r::AbstractUnitRange{T}, i) = i == oftype(i, r.stop) + oneunit(T) +start(r::UnitRange{T}) where {T} = oftype(r.start + oneunit(T), r.start) +next(r::AbstractUnitRange{T}, i) where {T} = (convert(T, i), i + oneunit(T)) +done(r::AbstractUnitRange{T}, i) where {T} = i == oftype(i, r.stop) + oneunit(T) -start{T}(r::OneTo{T}) = oneunit(T) +start(r::OneTo{T}) where {T} = oneunit(T) # some special cases to favor default Int type to avoid overflow let smallint = (Int === Int64 ? @@ -455,28 +455,28 @@ let smallint = (Int === Int64 ? global start global next start(r::StepRange{<:smallint}) = convert(Int, r.start) - next{T<:smallint}(r::StepRange{T}, i) = (i % T, i + r.step) + next(r::StepRange{T}, i) where {T<:smallint} = (i % T, i + r.step) start(r::UnitRange{<:smallint}) = convert(Int, r.start) - next{T<:smallint}(r::AbstractUnitRange{T}, i) = (i % T, i + 1) + next(r::AbstractUnitRange{T}, i) where {T<:smallint} = (i % T, i + 1) start(r::OneTo{<:smallint}) = 1 end ## indexing -function getindex{T}(v::UnitRange{T}, i::Integer) +function getindex(v::UnitRange{T}, i::Integer) where T @_inline_meta ret = convert(T, first(v) + i - 1) @boundscheck ((i > 0) & (ret <= v.stop) & (ret >= v.start)) || throw_boundserror(v, i) ret end -function getindex{T}(v::OneTo{T}, i::Integer) +function getindex(v::OneTo{T}, i::Integer) where T @_inline_meta @boundscheck ((i > 0) & (i <= v.stop)) || throw_boundserror(v, i) convert(T, i) end -function getindex{T}(v::Range{T}, i::Integer) +function getindex(v::Range{T}, i::Integer) where T @_inline_meta ret = convert(T, first(v) + (i - 1)*step(v)) ok = ifelse(step(v) > zero(step(v)), @@ -493,7 +493,7 @@ function getindex(r::Union{StepRangeLen,LinSpace}, i::Integer) end # This is separate to make it useful even when running with --check-bounds=yes -function unsafe_getindex{T}(r::StepRangeLen{T}, i::Integer) +function unsafe_getindex(r::StepRangeLen{T}, i::Integer) where T u = i - r.offset T(r.ref + u*r.step) end @@ -518,7 +518,7 @@ function getindex(r::AbstractUnitRange, s::AbstractUnitRange{<:Integer}) range(st, length(s)) end -function getindex{T}(r::OneTo{T}, s::OneTo) +function getindex(r::OneTo{T}, s::OneTo) where T @_inline_meta @boundscheck checkbounds(r, s) OneTo(T(s.stop)) @@ -557,13 +557,13 @@ show(io::IO, r::Range) = print(io, repr(first(r)), ':', repr(step(r)), ':', repr show(io::IO, r::UnitRange) = print(io, repr(first(r)), ':', repr(last(r))) show(io::IO, r::OneTo) = print(io, "Base.OneTo(", r.stop, ")") -=={T<:Range}(r::T, s::T) = +==(r::T, s::T) where {T<:Range} = (first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s)) ==(r::OrdinalRange, s::OrdinalRange) = (first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s)) -=={T<:Union{StepRangeLen,LinSpace}}(r::T, s::T) = +==(r::T, s::T) where {T<:Union{StepRangeLen,LinSpace}} = (first(r) == first(s)) & (length(r) == length(s)) & (last(r) == last(s)) -=={T}(r::Union{StepRange{T},StepRangeLen{T,T}}, s::Union{StepRange{T},StepRangeLen{T,T}}) = +==(r::Union{StepRange{T},StepRangeLen{T,T}}, s::Union{StepRange{T},StepRangeLen{T,T}}) where {T} = (first(r) == first(s)) & (last(r) == last(s)) & (step(r) == step(s)) function ==(r::Range, s::Range) @@ -745,33 +745,33 @@ end /(x::Number, r::Range) = [ x/y for y=r ] -promote_rule{T1,T2}(::Type{UnitRange{T1}},::Type{UnitRange{T2}}) = +promote_rule(::Type{UnitRange{T1}},::Type{UnitRange{T2}}) where {T1,T2} = UnitRange{promote_type(T1,T2)} convert(::Type{UnitRange{T}}, r::UnitRange{T}) where {T<:Real} = r convert(::Type{UnitRange{T}}, r::UnitRange) where {T<:Real} = UnitRange{T}(r.start, r.stop) -promote_rule{T1,T2}(::Type{OneTo{T1}},::Type{OneTo{T2}}) = +promote_rule(::Type{OneTo{T1}},::Type{OneTo{T2}}) where {T1,T2} = OneTo{promote_type(T1,T2)} convert(::Type{OneTo{T}}, r::OneTo{T}) where {T<:Real} = r convert(::Type{OneTo{T}}, r::OneTo) where {T<:Real} = OneTo{T}(r.stop) -promote_rule{T1,UR<:AbstractUnitRange}(::Type{UnitRange{T1}}, ::Type{UR}) = +promote_rule(::Type{UnitRange{T1}}, ::Type{UR}) where {T1,UR<:AbstractUnitRange} = UnitRange{promote_type(T1,eltype(UR))} convert(::Type{UnitRange{T}}, r::AbstractUnitRange) where {T<:Real} = UnitRange{T}(first(r), last(r)) convert(::Type{UnitRange}, r::AbstractUnitRange) = UnitRange(first(r), last(r)) -promote_rule{T1a,T1b,T2a,T2b}(::Type{StepRange{T1a,T1b}},::Type{StepRange{T2a,T2b}}) = +promote_rule(::Type{StepRange{T1a,T1b}},::Type{StepRange{T2a,T2b}}) where {T1a,T1b,T2a,T2b} = StepRange{promote_type(T1a,T2a),promote_type(T1b,T2b)} convert(::Type{StepRange{T1,T2}}, r::StepRange{T1,T2}) where {T1,T2} = r -promote_rule{T1a,T1b,UR<:AbstractUnitRange}(::Type{StepRange{T1a,T1b}},::Type{UR}) = +promote_rule(::Type{StepRange{T1a,T1b}},::Type{UR}) where {T1a,T1b,UR<:AbstractUnitRange} = StepRange{promote_type(T1a,eltype(UR)),promote_type(T1b,eltype(UR))} convert(::Type{StepRange{T1,T2}}, r::Range) where {T1,T2} = StepRange{T1,T2}(convert(T1, first(r)), convert(T2, step(r)), convert(T1, last(r))) convert(::Type{StepRange}, r::AbstractUnitRange{T}) where {T} = StepRange{T,T}(first(r), step(r), last(r)) -promote_rule{T1,T2,R1,R2,S1,S2}(::Type{StepRangeLen{T1,R1,S1}},::Type{StepRangeLen{T2,R2,S2}}) = +promote_rule(::Type{StepRangeLen{T1,R1,S1}},::Type{StepRangeLen{T2,R2,S2}}) where {T1,T2,R1,R2,S1,S2} = StepRangeLen{promote_type(T1,T2), promote_type(R1,R2), promote_type(S1,S2)} convert(::Type{StepRangeLen{T,R,S}}, r::StepRangeLen{T,R,S}) where {T,R,S} = r convert(::Type{StepRangeLen{T,R,S}}, r::StepRangeLen) where {T,R,S} = @@ -779,7 +779,7 @@ convert(::Type{StepRangeLen{T,R,S}}, r::StepRangeLen) where {T,R,S} = convert(::Type{StepRangeLen{T}}, r::StepRangeLen) where {T} = StepRangeLen(convert(T, r.ref), convert(T, r.step), length(r), r.offset) -promote_rule{T,R,S,OR<:Range}(::Type{StepRangeLen{T,R,S}}, ::Type{OR}) = +promote_rule(::Type{StepRangeLen{T,R,S}}, ::Type{OR}) where {T,R,S,OR<:Range} = StepRangeLen{promote_type(T,eltype(OR)),promote_type(R,eltype(OR)),promote_type(S,eltype(OR))} convert(::Type{StepRangeLen{T,R,S}}, r::Range) where {T,R,S} = StepRangeLen{T,R,S}(R(first(r)), S(step(r)), length(r)) @@ -787,7 +787,7 @@ convert(::Type{StepRangeLen{T}}, r::Range) where {T} = StepRangeLen(T(first(r)), T(step(r)), length(r)) convert(::Type{StepRangeLen}, r::Range) = convert(StepRangeLen{eltype(r)}, r) -promote_rule{T1,T2}(::Type{LinSpace{T1}},::Type{LinSpace{T2}}) = +promote_rule(::Type{LinSpace{T1}},::Type{LinSpace{T2}}) where {T1,T2} = LinSpace{promote_type(T1,T2)} convert(::Type{LinSpace{T}}, r::LinSpace{T}) where {T} = r convert(::Type{LinSpace{T}}, r::Range) where {T} = @@ -795,17 +795,17 @@ convert(::Type{LinSpace{T}}, r::Range) where {T} = convert(::Type{LinSpace}, r::Range{T}) where {T} = convert(LinSpace{T}, r) -promote_rule{T,OR<:OrdinalRange}(::Type{LinSpace{T}}, ::Type{OR}) = +promote_rule(::Type{LinSpace{T}}, ::Type{OR}) where {T,OR<:OrdinalRange} = LinSpace{promote_type(T,eltype(OR))} -promote_rule{L,T,R,S}(::Type{LinSpace{L}}, ::Type{StepRangeLen{T,R,S}}) = +promote_rule(::Type{LinSpace{L}}, ::Type{StepRangeLen{T,R,S}}) where {L,T,R,S} = StepRangeLen{promote_type(L,T),promote_type(L,R),promote_type(L,S)} # +/- of ranges is defined in operators.jl (to be able to use @eval etc.) ## concatenation ## -function vcat{T}(rs::Range{T}...) +function vcat(rs::Range{T}...) where T n::Int = 0 for ra in rs n += length(ra) @@ -889,7 +889,7 @@ end _define_range_op(:+) _define_range_op(:-) -function +{T,S}(r1::StepRangeLen{T,S}, r2::StepRangeLen{T,S}) +function +(r1::StepRangeLen{T,S}, r2::StepRangeLen{T,S}) where {T,S} len = length(r1) (len == length(r2) || throw(DimensionMismatch("argument dimensions must match"))) diff --git a/base/rational.jl b/base/rational.jl index 1760a6ab343ef..fa13a8e14e980 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -95,9 +95,9 @@ convert(::Type{Rational}, x::Float32) = convert(Rational{Int}, x) big(z::Complex{<:Rational{<:Integer}}) = Complex{Rational{BigInt}}(z) -promote_rule{T<:Integer,S<:Integer}(::Type{Rational{T}}, ::Type{S}) = Rational{promote_type(T,S)} -promote_rule{T<:Integer,S<:Integer}(::Type{Rational{T}}, ::Type{Rational{S}}) = Rational{promote_type(T,S)} -promote_rule{T<:Integer,S<:AbstractFloat}(::Type{Rational{T}}, ::Type{S}) = promote_type(T,S) +promote_rule(::Type{Rational{T}}, ::Type{S}) where {T<:Integer,S<:Integer} = Rational{promote_type(T,S)} +promote_rule(::Type{Rational{T}}, ::Type{Rational{S}}) where {T<:Integer,S<:Integer} = Rational{promote_type(T,S)} +promote_rule(::Type{Rational{T}}, ::Type{S}) where {T<:Integer,S<:AbstractFloat} = promote_type(T,S) widen{T}(::Type{Rational{T}}) = Rational{widen(T)} @@ -224,11 +224,11 @@ typemax{T<:Integer}(::Type{Rational{T}}) = one(T)//zero(T) isinteger(x::Rational) = x.den == 1 -(x::Rational) = (-x.num) // x.den -function -{T<:Signed}(x::Rational{T}) +function -(x::Rational{T}) where T<:Signed x.num == typemin(T) && throw(OverflowError()) (-x.num) // x.den end -function -{T<:Unsigned}(x::Rational{T}) +function -(x::Rational{T}) where T<:Unsigned x.num != zero(T) && throw(OverflowError()) x end @@ -416,8 +416,8 @@ function ^(x::Rational, n::Integer) end ^(x::Number, y::Rational) = x^(y.num/y.den) -^{T<:AbstractFloat}(x::T, y::Rational) = x^convert(T,y) -^{T<:AbstractFloat}(x::Complex{T}, y::Rational) = x^convert(T,y) +^(x::T, y::Rational) where {T<:AbstractFloat} = x^convert(T,y) +^(x::Complex{T}, y::Rational) where {T<:AbstractFloat} = x^convert(T,y) ^(z::Complex{<:Rational}, n::Bool) = n ? z : one(z) # to resolve ambiguity function ^(z::Complex{<:Rational}, n::Integer) diff --git a/base/reduce.jl b/base/reduce.jl index 188d9c52eff9a..7c13c8cc104ae 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -17,19 +17,19 @@ const WidenReduceResult = Union{SmallSigned, SmallUnsigned, Float16} # r_promote_type: promote T to the type of reduce(op, ::Array{T}) # (some "extra" methods are required here to avoid ambiguity warnings) -r_promote_type{T}(op, ::Type{T}) = T -r_promote_type{T<:WidenReduceResult}(op, ::Type{T}) = widen(T) -r_promote_type{T<:WidenReduceResult}(::typeof(+), ::Type{T}) = widen(T) -r_promote_type{T<:WidenReduceResult}(::typeof(*), ::Type{T}) = widen(T) -r_promote_type{T<:Number}(::typeof(+), ::Type{T}) = typeof(zero(T)+zero(T)) -r_promote_type{T<:Number}(::typeof(*), ::Type{T}) = typeof(one(T)*one(T)) -r_promote_type{T<:WidenReduceResult}(::typeof(scalarmax), ::Type{T}) = T -r_promote_type{T<:WidenReduceResult}(::typeof(scalarmin), ::Type{T}) = T -r_promote_type{T<:WidenReduceResult}(::typeof(max), ::Type{T}) = T -r_promote_type{T<:WidenReduceResult}(::typeof(min), ::Type{T}) = T +r_promote_type(op, ::Type{T}) where {T} = T +r_promote_type(op, ::Type{T}) where {T<:WidenReduceResult} = widen(T) +r_promote_type(::typeof(+), ::Type{T}) where {T<:WidenReduceResult} = widen(T) +r_promote_type(::typeof(*), ::Type{T}) where {T<:WidenReduceResult} = widen(T) +r_promote_type(::typeof(+), ::Type{T}) where {T<:Number} = typeof(zero(T)+zero(T)) +r_promote_type(::typeof(*), ::Type{T}) where {T<:Number} = typeof(one(T)*one(T)) +r_promote_type(::typeof(scalarmax), ::Type{T}) where {T<:WidenReduceResult} = T +r_promote_type(::typeof(scalarmin), ::Type{T}) where {T<:WidenReduceResult} = T +r_promote_type(::typeof(max), ::Type{T}) where {T<:WidenReduceResult} = T +r_promote_type(::typeof(min), ::Type{T}) where {T<:WidenReduceResult} = T # r_promote: promote x to the type of reduce(op, [x]) -r_promote{T}(op, x::T) = convert(r_promote_type(op, T), x) +r_promote(op, x::T) where {T} = convert(r_promote_type(op, T), x) ## foldl && mapfoldl @@ -258,7 +258,7 @@ mr_empty_iter(f, op, itr, ::EltypeUnknown) = _empty_reduce_error() _mapreduce(f, op, A::AbstractArray) = _mapreduce(f, op, IndexStyle(A), A) -function _mapreduce{T}(f, op, ::IndexLinear, A::AbstractArray{T}) +function _mapreduce(f, op, ::IndexLinear, A::AbstractArray{T}) where T inds = linearindices(A) n = length(inds) if n == 0 diff --git a/base/reducedim.jl b/base/reducedim.jl index b6592764952b1..1095fcf41f79d 100644 --- a/base/reducedim.jl +++ b/base/reducedim.jl @@ -8,7 +8,7 @@ reduced_indices(a::AbstractArray, region) = reduced_indices(indices(a), region) # for reductions that keep 0 dims as 0 reduced_indices0(a::AbstractArray, region) = reduced_indices0(indices(a), region) -function reduced_indices{N}(inds::Indices{N}, d::Int, rd::AbstractUnitRange) +function reduced_indices(inds::Indices{N}, d::Int, rd::AbstractUnitRange) where N d < 1 && throw(ArgumentError("dimension must be ≥ 1, got $d")) if d == 1 return (oftype(inds[1], rd), tail(inds)...) @@ -29,7 +29,7 @@ function reduced_indices0{N}(inds::Indices{N}, d::Int) end end -function reduced_indices{N}(inds::Indices{N}, region) +function reduced_indices(inds::Indices{N}, region) where N rinds = [inds...] for i in region isa(i, Integer) || throw(ArgumentError("reduced dimension(s) must be integers")) diff --git a/base/refpointer.jl b/base/refpointer.jl index 8671f215c595e..11ef66c0d3148 100644 --- a/base/refpointer.jl +++ b/base/refpointer.jl @@ -27,7 +27,7 @@ end ### General Methods for Ref{T} type -eltype{T}(x::Type{Ref{T}}) = T +eltype(x::Type{Ref{T}}) where {T} = T convert(::Type{Ref{T}}, x::Ref{T}) where {T} = x # create Ref objects for general object conversion @@ -48,11 +48,11 @@ Ref(x::Ref) = x Ref(x::Any) = RefValue(x) Ref{T}(x::Ptr{T}, i::Integer=1) = x + (i-1)*Core.sizeof(T) Ref(x, i::Integer) = (i != 1 && error("Object only has one element"); Ref(x)) -(::Type{Ref{T}}){T}() = RefValue{T}() # Ref{T}() -(::Type{Ref{T}}){T}(x) = RefValue{T}(x) # Ref{T}(x) +(::Type{Ref{T}})() where {T} = RefValue{T}() # Ref{T}() +(::Type{Ref{T}})(x) where {T} = RefValue{T}(x) # Ref{T}(x) convert(::Type{Ref{T}}, x) where {T} = RefValue{T}(x) -function unsafe_convert{T}(P::Type{Ptr{T}}, b::RefValue{T}) +function unsafe_convert(P::Type{Ptr{T}}, b::RefValue{T}) where T if isbits(T) return convert(P, data_pointer_from_objref(b)) else @@ -76,7 +76,7 @@ RefArray{T}(x::AbstractArray{T},i::Int=1,roots::Void=nothing) = RefArray{T,typeo convert(::Type{Ref{T}}, x::AbstractArray{T}) where {T} = RefArray(x, 1) Ref(x::AbstractArray, i::Integer=1) = RefArray(x, i) -function unsafe_convert{T}(P::Type{Ptr{T}}, b::RefArray{T}) +function unsafe_convert(P::Type{Ptr{T}}, b::RefArray{T}) where T if isbits(T) convert(P, pointer(b.x, b.i)) else @@ -108,10 +108,10 @@ function (::Type{Ref{P}}){P<:Union{Ptr,Cwstring,Cstring},T}(a::Array{T}) # Ref{P return RefArray(ptrs,1,roots) end end -cconvert{P<:Ptr}(::Type{Ptr{P}}, a::Array{<:Ptr}) = a -cconvert{P<:Ptr}(::Type{Ref{P}}, a::Array{<:Ptr}) = a -cconvert{P<:Union{Ptr,Cwstring,Cstring}}(::Type{Ptr{P}}, a::Array) = Ref{P}(a) -cconvert{P<:Union{Ptr,Cwstring,Cstring}}(::Type{Ref{P}}, a::Array) = Ref{P}(a) +cconvert(::Type{Ptr{P}}, a::Array{<:Ptr}) where {P<:Ptr} = a +cconvert(::Type{Ref{P}}, a::Array{<:Ptr}) where {P<:Ptr} = a +cconvert(::Type{Ptr{P}}, a::Array) where {P<:Union{Ptr,Cwstring,Cstring}} = Ref{P}(a) +cconvert(::Type{Ref{P}}, a::Array) where {P<:Union{Ptr,Cwstring,Cstring}} = Ref{P}(a) ### diff --git a/base/reshapedarray.jl b/base/reshapedarray.jl index 6681681471d07..3b8aff398b15a 100644 --- a/base/reshapedarray.jl +++ b/base/reshapedarray.jl @@ -18,7 +18,7 @@ struct ReshapedArrayIterator{I,M} mi::NTuple{M,SignedMultiplicativeInverse{Int}} end ReshapedArrayIterator(A::ReshapedArray) = _rs_iterator(parent(A), A.mi) -function _rs_iterator{M}(P, mi::NTuple{M}) +function _rs_iterator(P, mi::NTuple{M}) where M iter = eachindex(P) ReshapedArrayIterator{typeof(iter),M}(iter, mi) end @@ -109,20 +109,20 @@ end _throw_reshape_colon_dimmismatch(A, dims) = throw(DimensionMismatch("array size $(length(A)) must be divisible by the product of the new dimensions $dims")) -reshape{T,N}(parent::AbstractArray{T,N}, ndims::Type{Val{N}}) = parent -function reshape{N}(parent::AbstractArray, ndims::Type{Val{N}}) +reshape(parent::AbstractArray{T,N}, ndims::Type{Val{N}}) where {T,N} = parent +function reshape(parent::AbstractArray, ndims::Type{Val{N}}) where N reshape(parent, rdims((), indices(parent), Val{N})) end # Move elements from inds to out until out reaches the desired # dimensionality N, either filling with OneTo(1) or collapsing the # product of trailing dims into the last element -@pure rdims{N}(out::NTuple{N,Any}, inds::Tuple{}, ::Type{Val{N}}) = out -@pure function rdims{N}(out::NTuple{N,Any}, inds::Tuple{Any, Vararg{Any}}, ::Type{Val{N}}) +@pure rdims(out::NTuple{N,Any}, inds::Tuple{}, ::Type{Val{N}}) where {N} = out +@pure function rdims(out::NTuple{N,Any}, inds::Tuple{Any, Vararg{Any}}, ::Type{Val{N}}) where N l = length(last(out)) * prod(map(length, inds)) (front(out)..., OneTo(l)) end -@pure rdims{N}(out::Tuple, inds::Tuple{}, ::Type{Val{N}}) = rdims((out..., OneTo(1)), (), Val{N}) -@pure rdims{N}(out::Tuple, inds::Tuple{Any, Vararg{Any}}, ::Type{Val{N}}) = rdims((out..., first(inds)), tail(inds), Val{N}) +@pure rdims(out::Tuple, inds::Tuple{}, ::Type{Val{N}}) where {N} = rdims((out..., OneTo(1)), (), Val{N}) +@pure rdims(out::Tuple, inds::Tuple{Any, Vararg{Any}}, ::Type{Val{N}}) where {N} = rdims((out..., first(inds)), tail(inds), Val{N}) # _reshape on Array returns an Array _reshape(parent::Vector, dims::Dims{1}) = parent @@ -168,7 +168,7 @@ similar(A::ReshapedArray, eltype::Type, dims::Dims) = similar(parent(A), eltype, IndexStyle(::Type{<:ReshapedArrayLF}) = IndexLinear() parent(A::ReshapedArray) = A.parent parentindexes(A::ReshapedArray) = map(s->1:s, size(parent(A))) -reinterpret{T}(::Type{T}, A::ReshapedArray, dims::Dims) = reinterpret(T, parent(A), dims) +reinterpret(::Type{T}, A::ReshapedArray, dims::Dims) where {T} = reinterpret(T, parent(A), dims) @inline ind2sub_rs(::Tuple{}, i::Int) = i @inline ind2sub_rs(strds, i) = ind2sub_rs((), strds, i-1) diff --git a/base/rounding.jl b/base/rounding.jl index b56a13c482530..984848b1b14f4 100644 --- a/base/rounding.jl +++ b/base/rounding.jl @@ -142,8 +142,8 @@ See [`RoundingMode`](@ref) for available modes. setrounding_raw(::Type{<:Union{Float32,Float64}},i::Integer) = ccall(:fesetround, Int32, (Int32,), i) rounding_raw(::Type{<:Union{Float32,Float64}}) = ccall(:fegetround, Int32, ()) -setrounding{T<:Union{Float32,Float64}}(::Type{T},r::RoundingMode) = setrounding_raw(T,to_fenv(r)) -rounding{T<:Union{Float32,Float64}}(::Type{T}) = from_fenv(rounding_raw(T)) +setrounding(::Type{T},r::RoundingMode) where {T<:Union{Float32,Float64}} = setrounding_raw(T,to_fenv(r)) +rounding(::Type{T}) where {T<:Union{Float32,Float64}} = from_fenv(rounding_raw(T)) """ setrounding(f::Function, T, mode) @@ -180,7 +180,7 @@ See [`RoundingMode`](@ref) for available rounding modes. end 1.2 """ -function setrounding{T}(f::Function, ::Type{T}, rounding::RoundingMode) +function setrounding(f::Function, ::Type{T}, rounding::RoundingMode) where T old_rounding_raw = rounding_raw(T) setrounding(T,rounding) try @@ -201,16 +201,16 @@ end # To avoid ambiguous dispatch with methods in mpfr.jl: (::Type{T})(x::Real,r::RoundingMode) where {T<:AbstractFloat} = _convert_rounding(T,x,r) -_convert_rounding{T<:AbstractFloat}(::Type{T},x::Real,r::RoundingMode{:Nearest}) = convert(T,x) -function _convert_rounding{T<:AbstractFloat}(::Type{T},x::Real,r::RoundingMode{:Down}) +_convert_rounding(::Type{T},x::Real,r::RoundingMode{:Nearest}) where {T<:AbstractFloat} = convert(T,x) +function _convert_rounding(::Type{T},x::Real,r::RoundingMode{:Down}) where T<:AbstractFloat y = convert(T,x) y > x ? prevfloat(y) : y end -function _convert_rounding{T<:AbstractFloat}(::Type{T},x::Real,r::RoundingMode{:Up}) +function _convert_rounding(::Type{T},x::Real,r::RoundingMode{:Up}) where T<:AbstractFloat y = convert(T,x) y < x ? nextfloat(y) : y end -function _convert_rounding{T<:AbstractFloat}(::Type{T},x::Real,r::RoundingMode{:ToZero}) +function _convert_rounding(::Type{T},x::Real,r::RoundingMode{:ToZero}) where T<:AbstractFloat y = convert(T,x) if x > 0.0 y > x ? prevfloat(y) : y diff --git a/base/set.jl b/base/set.jl index eae1e45a53402..63cc3b83f67ae 100644 --- a/base/set.jl +++ b/base/set.jl @@ -15,7 +15,7 @@ function Set(g::Generator) end eltype(::Type{Set{T}}) where {T} = T -similar{T}(s::Set{T}) = Set{T}() +similar(s::Set{T}) where {T} = Set{T}() similar(s::Set, T::Type) = Set{T}() function show(io::IO, s::Set) diff --git a/base/sharedarray.jl b/base/sharedarray.jl index ef3d973fc8766..1e41b2e10e9ad 100644 --- a/base/sharedarray.jl +++ b/base/sharedarray.jl @@ -77,7 +77,7 @@ beginning of the file. """ SharedArray -function (::Type{SharedArray{T,N}}){T,N}(dims::Dims{N}; init=false, pids=Int[]) +function (::Type{SharedArray{T,N}})(dims::Dims{N}; init=false, pids=Int[]) where T where N isbits(T) || throw(ArgumentError("type of SharedArray elements must be bits types, got $(T)")) pids, onlocalhost = shared_pids(pids) @@ -134,21 +134,21 @@ function (::Type{SharedArray{T,N}}){T,N}(dims::Dims{N}; init=false, pids=Int[]) S end -(::Type{SharedArray{T,N}}){T,N}(I::Integer...; kwargs...) = +(::Type{SharedArray{T,N}})(I::Integer...; kwargs...) where {T,N} = SharedArray{T,N}(I; kwargs...) -(::Type{SharedArray{T}}){T}(d::NTuple; kwargs...) = +(::Type{SharedArray{T}})(d::NTuple; kwargs...) where {T} = SharedArray{T,length(d)}(d; kwargs...) -(::Type{SharedArray{T}}){T}(I::Integer...; kwargs...) = +(::Type{SharedArray{T}})(I::Integer...; kwargs...) where {T} = SharedArray{T,length(I)}(I; kwargs...) -(::Type{SharedArray{T}}){T}(m::Integer; kwargs...) = +(::Type{SharedArray{T}})(m::Integer; kwargs...) where {T} = SharedArray{T,1}(m; kwargs...) -(::Type{SharedArray{T}}){T}(m::Integer, n::Integer; kwargs...) = +(::Type{SharedArray{T}})(m::Integer, n::Integer; kwargs...) where {T} = SharedArray{T,2}(m, n; kwargs...) -(::Type{SharedArray{T}}){T}(m::Integer, n::Integer, o::Integer; kwargs...) = +(::Type{SharedArray{T}})(m::Integer, n::Integer, o::Integer; kwargs...) where {T} = SharedArray{T,3}(m, n, o; kwargs...) -function (::Type{SharedArray{T,N}}){T,N}(filename::AbstractString, dims::NTuple{N,Int}, - offset::Integer=0; mode=nothing, init=false, pids::Vector{Int}=Int[]) +function (::Type{SharedArray{T,N}})(filename::AbstractString, dims::NTuple{N,Int}, + offset::Integer=0; mode=nothing, init=false, pids::Vector{Int}=Int[]) where T where N if !isabspath(filename) throw(ArgumentError("$filename is not an absolute path; try abspath(filename)?")) end @@ -213,8 +213,8 @@ function (::Type{SharedArray{T,N}}){T,N}(filename::AbstractString, dims::NTuple{ S end -(::Type{SharedArray{T}}){T,N}(filename::AbstractString, dims::NTuple{N,Int}, offset::Integer=0; - mode=nothing, init=false, pids::Vector{Int}=Int[]) = +(::Type{SharedArray{T}})(filename::AbstractString, dims::NTuple{N,Int}, offset::Integer=0; + mode=nothing, init=false, pids::Vector{Int}=Int[]) where {T,N} = SharedArray{T,N}(filename, dims, offset; mode=mode, init=init, pids=pids) function initialize_shared_array(S, onlocalhost, init, pids) @@ -258,7 +258,7 @@ size(S::SharedArray) = S.dims ndims(S::SharedArray) = length(S.dims) IndexStyle(::Type{<:SharedArray}) = IndexLinear() -function reshape{T,N}(a::SharedArray{T}, dims::NTuple{N,Int}) +function reshape(a::SharedArray{T}, dims::NTuple{N,Int}) where T where N if length(a) != prod(dims) throw(DimensionMismatch("dimensions must be consistent with array size")) end @@ -460,7 +460,7 @@ function fill!(S::SharedArray, v) return S end -function rand!{T}(S::SharedArray{T}) +function rand!(S::SharedArray{T}) where T f = S->map!(x -> rand(T), S.loc_subarr_1d, S.loc_subarr_1d) @sync for p in procs(S) @async remotecall_wait(f, p, S) diff --git a/base/sparse/cholmod.jl b/base/sparse/cholmod.jl index 1586a0a838018..31800d3039c68 100644 --- a/base/sparse/cholmod.jl +++ b/base/sparse/cholmod.jl @@ -331,7 +331,7 @@ Factor(p::Ptr{C_Factor{Tv}}) where {Tv<:VTypes} = Factor{Tv}(p) # should be wrapped in get to make sure that SuiteSparse is not called with # a C_NULL pointer which could cause a segfault. Pointers are set to null # when serialized so this can happen when mutiple processes are in use. -function get{T<:SuiteSparseStruct}(p::Ptr{T}) +function get(p::Ptr{T}) where T<:SuiteSparseStruct if p == C_NULL throw(ArgumentError("pointer to the $T object is null. This can " * "happen if the object has been serialized.")) @@ -410,7 +410,7 @@ function ones{T<:VTypes}(m::Integer, n::Integer, ::Type{T}) end ones(m::Integer, n::Integer) = ones(m, n, Float64) -function eye{T<:VTypes}(m::Integer, n::Integer, ::Type{T}) +function eye(m::Integer, n::Integer, ::Type{T}) where T<:VTypes d = Dense(ccall((:cholmod_l_eye, :libcholmod), Ptr{C_Dense{T}}, (Csize_t, Csize_t, Cint, Ptr{UInt8}), m, n, xtyp(T), common())) @@ -571,7 +571,7 @@ function nnz{Tv<:VTypes}(A::Sparse{Tv}) get(A.p), common()) end -function speye{Tv<:VTypes}(m::Integer, n::Integer, ::Type{Tv}) +function speye(m::Integer, n::Integer, ::Type{Tv}) where Tv<:VTypes s = Sparse(ccall((@cholmod_name("speye", SuiteSparse_long), :libcholmod), Ptr{C_Sparse{Tv}}, (Csize_t, Csize_t, Cint, Ptr{UInt8}), @@ -669,7 +669,7 @@ function norm_sparse{Tv<:VTypes}(A::Sparse{Tv}, norm::Integer) get(A.p), norm, common()) end -function horzcat{Tv<:VRealTypes}(A::Sparse{Tv}, B::Sparse{Tv}, values::Bool) +function horzcat(A::Sparse{Tv}, B::Sparse{Tv}, values::Bool) where Tv<:VRealTypes s = Sparse(ccall((@cholmod_name("horzcat", SuiteSparse_long), :libcholmod), Ptr{C_Sparse{Tv}}, (Ptr{C_Sparse{Tv}}, Ptr{C_Sparse{Tv}}, Cint, Ptr{UInt8}), @@ -724,7 +724,7 @@ function sdmult!{Tv<:VTypes}(A::Sparse{Tv}, transpose::Bool, Y end -function vertcat{Tv<:VRealTypes}(A::Sparse{Tv}, B::Sparse{Tv}, values::Bool) +function vertcat(A::Sparse{Tv}, B::Sparse{Tv}, values::Bool) where Tv<:VRealTypes s = Sparse(ccall((@cholmod_name("vertcat", SuiteSparse_long), :libcholmod), Ptr{C_Sparse{Tv}}, (Ptr{C_Sparse{Tv}}, Ptr{C_Sparse{Tv}}, Cint, Ptr{UInt8}), @@ -842,7 +842,7 @@ get_perm(FC::FactorComponent) = get_perm(Factor(FC)) ######################### # Convertion/construction -function convert{T<:VTypes}(::Type{Dense{T}}, A::StridedVecOrMat) +function convert(::Type{Dense{T}}, A::StridedVecOrMat) where T<:VTypes d = allocate_dense(size(A, 1), size(A, 2), stride(A, 2), T) s = unsafe_load(d.p) for i in eachindex(A) @@ -950,7 +950,7 @@ convert(::Type{Sparse}, A::SparseMatrixCSC{Complex{Float32},<:ITypes}) = convert(Sparse, convert(SparseMatrixCSC{Complex{Float64},SuiteSparse_long}, A)) convert(::Type{Sparse}, A::Symmetric{Float64,SparseMatrixCSC{Float64,SuiteSparse_long}}) = Sparse(A.data, A.uplo == 'L' ? -1 : 1) -convert{Tv<:VTypes}(::Type{Sparse}, A::Hermitian{Tv,SparseMatrixCSC{Tv,SuiteSparse_long}}) = +convert(::Type{Sparse}, A::Hermitian{Tv,SparseMatrixCSC{Tv,SuiteSparse_long}}) where {Tv<:VTypes} = Sparse(A.data, A.uplo == 'L' ? -1 : 1) function convert{Ti<:ITypes}(::Type{Sparse}, A::Union{SparseMatrixCSC{BigFloat,Ti}, @@ -1022,7 +1022,7 @@ function (::Type{Sparse})(filename::String) end ## convertion back to base Julia types -function convert{T}(::Type{Matrix{T}}, D::Dense{T}) +function convert(::Type{Matrix{T}}, D::Dense{T}) where T s = unsafe_load(D.p) a = Matrix{T}(s.nrow, s.ncol) copy!(a, D) @@ -1050,16 +1050,16 @@ function _copy!(dest::AbstractArray, D::Dense) end dest end -convert{T}(::Type{Matrix}, D::Dense{T}) = convert(Matrix{T}, D) -function convert{T}(::Type{Vector{T}}, D::Dense{T}) +convert(::Type{Matrix}, D::Dense{T}) where {T} = convert(Matrix{T}, D) +function convert(::Type{Vector{T}}, D::Dense{T}) where T if size(D, 2) > 1 throw(DimensionMismatch("input must be a vector but had $(size(D, 2)) columns")) end copy!(Array{T}(size(D, 1)), D) end -convert{T}(::Type{Vector}, D::Dense{T}) = convert(Vector{T}, D) +convert(::Type{Vector}, D::Dense{T}) where {T} = convert(Vector{T}, D) -function convert{Tv}(::Type{SparseMatrixCSC{Tv,SuiteSparse_long}}, A::Sparse{Tv}) +function convert(::Type{SparseMatrixCSC{Tv,SuiteSparse_long}}, A::Sparse{Tv}) where Tv s = unsafe_load(A.p) if s.stype != 0 throw(ArgumentError("matrix has stype != 0. Convert to matrix " * @@ -1094,7 +1094,7 @@ function convert(::Type{Symmetric{Float64,SparseMatrixCSC{Float64,SuiteSparse_lo return B end end -function convert{Tv<:VTypes}(::Type{Hermitian{Tv,SparseMatrixCSC{Tv,SuiteSparse_long}}}, A::Sparse{Tv}) +function convert(::Type{Hermitian{Tv,SparseMatrixCSC{Tv,SuiteSparse_long}}}, A::Sparse{Tv}) where Tv<:VTypes s = unsafe_load(A.p) if !ishermitian(A) throw(ArgumentError("matrix is not Hermitian")) @@ -1173,9 +1173,9 @@ free!(A::Dense) = free_dense!(A.p) free!(A::Sparse) = free_sparse!(A.p) free!(F::Factor) = free_factor!(F.p) -eltype{T<:VTypes}(::Type{Dense{T}}) = T -eltype{T<:VTypes}(::Type{Factor{T}}) = T -eltype{T<:VTypes}(::Type{Sparse{T}}) = T +eltype(::Type{Dense{T}}) where {T<:VTypes} = T +eltype(::Type{Factor{T}}) where {T<:VTypes} = T +eltype(::Type{Sparse{T}}) where {T<:VTypes} = T nnz(F::Factor) = nnz(Sparse(F)) @@ -1247,7 +1247,7 @@ function getindex(A::Dense, i::Integer) end IndexStyle(::Sparse) = IndexCartesian() -function getindex{T}(A::Sparse{T}, i0::Integer, i1::Integer) +function getindex(A::Sparse{T}, i0::Integer, i1::Integer) where T s = unsafe_load(get(A.p)) !(1 <= i0 <= s.nrow && 1 <= i1 <= s.ncol) && throw(BoundsError()) s.stype < 0 && i0 < i1 && return conj(A[i1,i0]) @@ -1287,7 +1287,7 @@ end (*)(A::Sparse, B::Dense) = sdmult!(A, false, 1., 0., B, zeros(size(A, 1), size(B, 2))) (*)(A::Sparse, B::VecOrMat) = (*)(A, Dense(B)) -function A_mul_Bc{Tv<:VRealTypes}(A::Sparse{Tv}, B::Sparse{Tv}) +function A_mul_Bc(A::Sparse{Tv}, B::Sparse{Tv}) where Tv<:VRealTypes cm = common() if A !== B diff --git a/base/sparse/higherorderfns.jl b/base/sparse/higherorderfns.jl index abc148485a195..5f8a4f477ddec 100644 --- a/base/sparse/higherorderfns.jl +++ b/base/sparse/higherorderfns.jl @@ -65,23 +65,23 @@ end # (2) map[!] entry points -map{Tf}(f::Tf, A::SparseVector) = _noshapecheck_map(f, A) -map{Tf}(f::Tf, A::SparseMatrixCSC) = _noshapecheck_map(f, A) -map{Tf,N}(f::Tf, A::SparseMatrixCSC, Bs::Vararg{SparseMatrixCSC,N}) = +map(f::Tf, A::SparseVector) where {Tf} = _noshapecheck_map(f, A) +map(f::Tf, A::SparseMatrixCSC) where {Tf} = _noshapecheck_map(f, A) +map(f::Tf, A::SparseMatrixCSC, Bs::Vararg{SparseMatrixCSC,N}) where {Tf,N} = (_checksameshape(A, Bs...); _noshapecheck_map(f, A, Bs...)) -map{Tf,N}(f::Tf, A::SparseVecOrMat, Bs::Vararg{SparseVecOrMat,N}) = +map(f::Tf, A::SparseVecOrMat, Bs::Vararg{SparseVecOrMat,N}) where {Tf,N} = (_checksameshape(A, Bs...); _noshapecheck_map(f, A, Bs...)) -map!{Tf,N}(f::Tf, C::SparseMatrixCSC, A::SparseMatrixCSC, Bs::Vararg{SparseMatrixCSC,N}) = +map!(f::Tf, C::SparseMatrixCSC, A::SparseMatrixCSC, Bs::Vararg{SparseMatrixCSC,N}) where {Tf,N} = (_checksameshape(C, A, Bs...); _noshapecheck_map!(f, C, A, Bs...)) -map!{Tf,N}(f::Tf, C::SparseVecOrMat, A::SparseVecOrMat, Bs::Vararg{SparseVecOrMat,N}) = +map!(f::Tf, C::SparseVecOrMat, A::SparseVecOrMat, Bs::Vararg{SparseVecOrMat,N}) where {Tf,N} = (_checksameshape(C, A, Bs...); _noshapecheck_map!(f, C, A, Bs...)) -function _noshapecheck_map!{Tf,N}(f::Tf, C::SparseVecOrMat, A::SparseVecOrMat, Bs::Vararg{SparseVecOrMat,N}) +function _noshapecheck_map!(f::Tf, C::SparseVecOrMat, A::SparseVecOrMat, Bs::Vararg{SparseVecOrMat,N}) where {Tf,N} fofzeros = f(_zeros_eltypes(A, Bs...)...) fpreszeros = _iszero(fofzeros) return fpreszeros ? _map_zeropres!(f, C, A, Bs...) : _map_notzeropres!(f, fofzeros, C, A, Bs...) end -function _noshapecheck_map{Tf,N}(f::Tf, A::SparseVecOrMat, Bs::Vararg{SparseVecOrMat,N}) +function _noshapecheck_map(f::Tf, A::SparseVecOrMat, Bs::Vararg{SparseVecOrMat,N}) where {Tf,N} fofzeros = f(_zeros_eltypes(A, Bs...)...) fpreszeros = _iszero(fofzeros) maxnnzC = fpreszeros ? min(length(A), _sumnnzs(A, Bs...)) : length(A) @@ -92,9 +92,9 @@ function _noshapecheck_map{Tf,N}(f::Tf, A::SparseVecOrMat, Bs::Vararg{SparseVecO _map_notzeropres!(f, fofzeros, C, A, Bs...) end # (3) broadcast[!] entry points -broadcast{Tf}(f::Tf, A::SparseVector) = _noshapecheck_map(f, A) -broadcast{Tf}(f::Tf, A::SparseMatrixCSC) = _noshapecheck_map(f, A) -function broadcast!{Tf}(f::Tf, C::SparseVecOrMat) +broadcast(f::Tf, A::SparseVector) where {Tf} = _noshapecheck_map(f, A) +broadcast(f::Tf, A::SparseMatrixCSC) where {Tf} = _noshapecheck_map(f, A) +function broadcast!(f::Tf, C::SparseVecOrMat) where Tf isempty(C) && return _finishempty!(C) fofnoargs = f() if _iszero(fofnoargs) # f() is zero, so empty C @@ -107,7 +107,7 @@ function broadcast!{Tf}(f::Tf, C::SparseVecOrMat) end return C end -function broadcast!{Tf,N}(f::Tf, C::SparseVecOrMat, A::SparseVecOrMat, Bs::Vararg{SparseVecOrMat,N}) +function broadcast!(f::Tf, C::SparseVecOrMat, A::SparseVecOrMat, Bs::Vararg{SparseVecOrMat,N}) where {Tf,N} _aresameshape(C, A, Bs...) && return _noshapecheck_map!(f, C, A, Bs...) Base.Broadcast.check_broadcast_indices(indices(C), A, Bs...) fofzeros = f(_zeros_eltypes(A, Bs...)...) @@ -116,13 +116,13 @@ function broadcast!{Tf,N}(f::Tf, C::SparseVecOrMat, A::SparseVecOrMat, Bs::Varar _broadcast_notzeropres!(f, fofzeros, C, A, Bs...) end # the following three similar defs are necessary for type stability in the mixed vector/matrix case -broadcast{Tf,N}(f::Tf, A::SparseVector, Bs::Vararg{SparseVector,N}) = +broadcast(f::Tf, A::SparseVector, Bs::Vararg{SparseVector,N}) where {Tf,N} = _aresameshape(A, Bs...) ? _noshapecheck_map(f, A, Bs...) : _diffshape_broadcast(f, A, Bs...) -broadcast{Tf,N}(f::Tf, A::SparseMatrixCSC, Bs::Vararg{SparseMatrixCSC,N}) = +broadcast(f::Tf, A::SparseMatrixCSC, Bs::Vararg{SparseMatrixCSC,N}) where {Tf,N} = _aresameshape(A, Bs...) ? _noshapecheck_map(f, A, Bs...) : _diffshape_broadcast(f, A, Bs...) -broadcast{Tf,N}(f::Tf, A::SparseVecOrMat, Bs::Vararg{SparseVecOrMat,N}) = +broadcast(f::Tf, A::SparseVecOrMat, Bs::Vararg{SparseVecOrMat,N}) where {Tf,N} = _diffshape_broadcast(f, A, Bs...) -function _diffshape_broadcast{Tf,N}(f::Tf, A::SparseVecOrMat, Bs::Vararg{SparseVecOrMat,N}) +function _diffshape_broadcast(f::Tf, A::SparseVecOrMat, Bs::Vararg{SparseVecOrMat,N}) where {Tf,N} fofzeros = f(_zeros_eltypes(A, Bs...)...) fpreszeros = _iszero(fofzeros) indextypeC = _promote_indtype(A, Bs...) @@ -170,7 +170,7 @@ end return SparseMatrixCSC(shape..., pointers, storedinds, storedvals) end # Ambiguity killers, TODO: nix conflicting specializations -ambiguityfunnel{Tf}(f::Tf, x, y) = _aresameshape(x, y) ? _noshapecheck_map(f, x, y) : _diffshape_broadcast(f, x, y) +ambiguityfunnel(f::Tf, x, y) where {Tf} = _aresameshape(x, y) ? _noshapecheck_map(f, x, y) : _diffshape_broadcast(f, x, y) broadcast(::typeof(+), x::SparseVector, y::SparseVector) = ambiguityfunnel(+, x, y) # base/sparse/sparsevectors.jl:1266 broadcast(::typeof(-), x::SparseVector, y::SparseVector) = ambiguityfunnel(-, x, y) # base/sparse/sparsevectors.jl:1266 broadcast(::typeof(*), x::SparseVector, y::SparseVector) = ambiguityfunnel(*, x, y) # base/sparse/sparsevectors.jl:1266 @@ -178,7 +178,7 @@ broadcast(::typeof(*), x::SparseVector, y::SparseVector) = ambiguityfunnel(*, x, # (4) _map_zeropres!/_map_notzeropres! specialized for a single sparse vector/matrix "Stores only the nonzero entries of `map(f, Array(A))` in `C`." -function _map_zeropres!{Tf}(f::Tf, C::SparseVecOrMat, A::SparseVecOrMat) +function _map_zeropres!(f::Tf, C::SparseVecOrMat, A::SparseVecOrMat) where Tf spaceC::Int = min(length(storedinds(C)), length(storedvals(C))) Ck = 1 @inbounds for j in columns(C) @@ -201,7 +201,7 @@ end Densifies `C`, storing `fillvalue` in place of each unstored entry in `A` and `f(A[i])`/`f(A[i,j])` in place of each stored entry `A[i]`/`A[i,j]` in `A`. """ -function _map_notzeropres!{Tf}(f::Tf, fillvalue, C::SparseVecOrMat, A::SparseVecOrMat) +function _map_notzeropres!(f::Tf, fillvalue, C::SparseVecOrMat, A::SparseVecOrMat) where Tf # Build dense matrix structure in C, expanding storage if necessary _densestructure!(C) # Populate values @@ -236,7 +236,7 @@ end # (5) _map_zeropres!/_map_notzeropres! specialized for a pair of sparse vectors/matrices -function _map_zeropres!{Tf}(f::Tf, C::SparseVecOrMat, A::SparseVecOrMat, B::SparseVecOrMat) +function _map_zeropres!(f::Tf, C::SparseVecOrMat, A::SparseVecOrMat, B::SparseVecOrMat) where Tf spaceC::Int = min(length(storedinds(C)), length(storedvals(C))) rowsentinelA = convert(indtype(A), numrows(C) + 1) rowsentinelB = convert(indtype(B), numrows(C) + 1) @@ -278,7 +278,7 @@ function _map_zeropres!{Tf}(f::Tf, C::SparseVecOrMat, A::SparseVecOrMat, B::Spar trimstorage!(C, Ck - 1) return C end -function _map_notzeropres!{Tf}(f::Tf, fillvalue, C::SparseVecOrMat, A::SparseVecOrMat, B::SparseVecOrMat) +function _map_notzeropres!(f::Tf, fillvalue, C::SparseVecOrMat, A::SparseVecOrMat, B::SparseVecOrMat) where Tf # Build dense matrix structure in C, expanding storage if necessary _densestructure!(C) # Populate values @@ -313,7 +313,7 @@ end # (6) _map_zeropres!/_map_notzeropres! for more than two sparse matrices / vectors -function _map_zeropres!{Tf,N}(f::Tf, C::SparseVecOrMat, As::Vararg{SparseVecOrMat,N}) +function _map_zeropres!(f::Tf, C::SparseVecOrMat, As::Vararg{SparseVecOrMat,N}) where {Tf,N} spaceC::Int = min(length(storedinds(C)), length(storedvals(C))) rowsentinel = numrows(C) + 1 Ck = 1 @@ -344,7 +344,7 @@ function _map_zeropres!{Tf,N}(f::Tf, C::SparseVecOrMat, As::Vararg{SparseVecOrMa trimstorage!(C, Ck - 1) return C end -function _map_notzeropres!{Tf,N}(f::Tf, fillvalue, C::SparseVecOrMat, As::Vararg{SparseVecOrMat,N}) +function _map_notzeropres!(f::Tf, fillvalue, C::SparseVecOrMat, As::Vararg{SparseVecOrMat,N}) where {Tf,N} # Build dense matrix structure in C, expanding storage if necessary _densestructure!(C) # Populate values @@ -431,7 +431,7 @@ end # (7) _broadcast_zeropres!/_broadcast_notzeropres! specialized for a single (input) sparse vector/matrix -function _broadcast_zeropres!{Tf}(f::Tf, C::SparseVecOrMat, A::SparseVecOrMat) +function _broadcast_zeropres!(f::Tf, C::SparseVecOrMat, A::SparseVecOrMat) where Tf isempty(C) && return _finishempty!(C) spaceC::Int = min(length(storedinds(C)), length(storedvals(C))) # C and A cannot have the same shape, as we directed that case to map in broadcast's @@ -482,7 +482,7 @@ function _broadcast_zeropres!{Tf}(f::Tf, C::SparseVecOrMat, A::SparseVecOrMat) trimstorage!(C, Ck - 1) return C end -function _broadcast_notzeropres!{Tf}(f::Tf, fillvalue, C::SparseVecOrMat, A::SparseVecOrMat) +function _broadcast_notzeropres!(f::Tf, fillvalue, C::SparseVecOrMat, A::SparseVecOrMat) where Tf # For information on this code, see comments in similar code in _broadcast_zeropres! above # Build dense matrix structure in C, expanding storage if necessary _densestructure!(C) @@ -511,7 +511,7 @@ end # (8) _broadcast_zeropres!/_broadcast_notzeropres! specialized for a pair of (input) sparse vectors/matrices -function _broadcast_zeropres!{Tf}(f::Tf, C::SparseVecOrMat, A::SparseVecOrMat, B::SparseVecOrMat) +function _broadcast_zeropres!(f::Tf, C::SparseVecOrMat, A::SparseVecOrMat, B::SparseVecOrMat) where Tf isempty(C) && return _finishempty!(C) spaceC::Int = min(length(storedinds(C)), length(storedvals(C))) rowsentinelA = convert(indtype(A), numrows(C) + 1) @@ -679,7 +679,7 @@ function _broadcast_zeropres!{Tf}(f::Tf, C::SparseVecOrMat, A::SparseVecOrMat, B trimstorage!(C, Ck - 1) return C end -function _broadcast_notzeropres!{Tf}(f::Tf, fillvalue, C::SparseVecOrMat, A::SparseVecOrMat, B::SparseVecOrMat) +function _broadcast_notzeropres!(f::Tf, fillvalue, C::SparseVecOrMat, A::SparseVecOrMat, B::SparseVecOrMat) where Tf # For information on this code, see comments in similar code in _broadcast_zeropres! above # Build dense matrix structure in C, expanding storage if necessary _densestructure!(C) @@ -783,7 +783,7 @@ _finishempty!(C::SparseMatrixCSC) = (fill!(C.colptr, 1); C) # (9) _broadcast_zeropres!/_broadcast_notzeropres! for more than two (input) sparse vectors/matrices -function _broadcast_zeropres!{Tf,N}(f::Tf, C::SparseVecOrMat, As::Vararg{SparseVecOrMat,N}) +function _broadcast_zeropres!(f::Tf, C::SparseVecOrMat, As::Vararg{SparseVecOrMat,N}) where {Tf,N} isempty(C) && return _finishempty!(C) spaceC::Int = min(length(storedinds(C)), length(storedvals(C))) expandsverts = _expandsvert_all(C, As) @@ -843,7 +843,7 @@ function _broadcast_zeropres!{Tf,N}(f::Tf, C::SparseVecOrMat, As::Vararg{SparseV trimstorage!(C, Ck - 1) return C end -function _broadcast_notzeropres!{Tf,N}(f::Tf, fillvalue, C::SparseVecOrMat, As::Vararg{SparseVecOrMat,N}) +function _broadcast_notzeropres!(f::Tf, fillvalue, C::SparseVecOrMat, As::Vararg{SparseVecOrMat,N}) where {Tf,N} isempty(C) && return _finishempty!(C) # Build dense matrix structure in C, expanding storage if necessary _densestructure!(C) @@ -983,11 +983,11 @@ promote_containertype(::Type{Tuple}, ::Type{AbstractSparseArray}) = Array promote_containertype(::Type{AbstractSparseArray}, ::Type{Tuple}) = Array # broadcast[!] entry points for combinations of sparse arrays and other (scalar) types -@inline function broadcast_c{N}(f, ::Type{AbstractSparseArray}, mixedargs::Vararg{Any,N}) +@inline function broadcast_c(f, ::Type{AbstractSparseArray}, mixedargs::Vararg{Any,N}) where N parevalf, passedargstup = capturescalars(f, mixedargs) return broadcast(parevalf, passedargstup...) end -@inline function broadcast_c!{N}(f, ::Type{AbstractSparseArray}, dest::SparseVecOrMat, mixedsrcargs::Vararg{Any,N}) +@inline function broadcast_c!(f, ::Type{AbstractSparseArray}, dest::SparseVecOrMat, mixedsrcargs::Vararg{Any,N}) where N parevalf, passedsrcargstup = capturescalars(f, mixedsrcargs) return broadcast!(parevalf, dest, passedsrcargstup...) end @@ -1014,8 +1014,8 @@ end @inline capturelastscalar(f, scalararg) = (passed...) -> f(passed, (scalararg,)) # NOTE: The following two method definitions work around #19096. -broadcast{Tf,T}(f::Tf, ::Type{T}, A::SparseMatrixCSC) = broadcast(y -> f(T, y), A) -broadcast{Tf,T}(f::Tf, A::SparseMatrixCSC, ::Type{T}) = broadcast(x -> f(x, T), A) +broadcast(f::Tf, ::Type{T}, A::SparseMatrixCSC) where {Tf,T} = broadcast(y -> f(T, y), A) +broadcast(f::Tf, A::SparseMatrixCSC, ::Type{T}) where {Tf,T} = broadcast(x -> f(x, T), A) # (11) broadcast[!] over combinations of scalars, sparse vectors/matrices, structured matrices, @@ -1074,7 +1074,7 @@ spcontainertype(x) = _spcontainertype(typeof(x)) spcontainertype(ct1, ct2) = promote_spcontainertype(spcontainertype(ct1), spcontainertype(ct2)) @inline spcontainertype(ct1, ct2, cts...) = promote_spcontainertype(spcontainertype(ct1), spcontainertype(ct2, cts...)) -promote_spcontainertype{T}(::Type{T}, ::Type{T}) = T +promote_spcontainertype(::Type{T}, ::Type{T}) where {T} = T # combinations involving AbstractArrays and/or Tuples divert to the generic AbstractArray broadcast code DivertToAbsArrayBC = Union{Type{AbstractArray},Type{Tuple}} promote_spcontainertype(::DivertToAbsArrayBC, ct) = AbstractArray @@ -1088,23 +1088,23 @@ promote_spcontainertype(::FunnelToSparseBC, ::FunnelToSparseBC) = PromoteToSpars # first (Broadcast containertype) dispatch layer # (broadcast_c[!], containertype, promote_containertype) -@inline broadcast_c{N}(f, ::Type{PromoteToSparse}, As::Vararg{Any,N}) = +@inline broadcast_c(f, ::Type{PromoteToSparse}, As::Vararg{Any,N}) where {N} = spbroadcast_c(f, spcontainertype(As...), As...) -@inline broadcast_c!{N}(f, ::Type{AbstractSparseArray}, ::Type{PromoteToSparse}, C, B, As::Vararg{Any,N}) = +@inline broadcast_c!(f, ::Type{AbstractSparseArray}, ::Type{PromoteToSparse}, C, B, As::Vararg{Any,N}) where {N} = spbroadcast_c!(f, AbstractSparseArray, spcontainertype(B, As...), C, B, As...) # where destination C is not an AbstractSparseArray, divert to generic AbstractArray broadcast code -@inline broadcast_c!{N}(f, CT::Type, ::Type{PromoteToSparse}, C, B, As::Vararg{Any,N}) = +@inline broadcast_c!(f, CT::Type, ::Type{PromoteToSparse}, C, B, As::Vararg{Any,N}) where {N} = broadcast_c!(f, CT, Array, C, B, As...) # second (internal sparse broadcast containertype) dispatch layer # (spbroadcast_c[!], spcontainertype, promote_spcontainertype) -@inline spbroadcast_c{N}(f, ::Type{PromoteToSparse}, As::Vararg{Any,N}) = +@inline spbroadcast_c(f, ::Type{PromoteToSparse}, As::Vararg{Any,N}) where {N} = broadcast(f, map(_sparsifystructured, As)...) -@inline spbroadcast_c{N}(f, ::Type{AbstractArray}, As::Vararg{Any,N}) = +@inline spbroadcast_c(f, ::Type{AbstractArray}, As::Vararg{Any,N}) where {N} = broadcast_c(f, Array, As...) -@inline spbroadcast_c!{N}(f, ::Type{AbstractSparseArray}, ::Type{PromoteToSparse}, C, B, As::Vararg{Any,N}) = +@inline spbroadcast_c!(f, ::Type{AbstractSparseArray}, ::Type{PromoteToSparse}, C, B, As::Vararg{Any,N}) where {N} = broadcast!(f, C, _sparsifystructured(B), map(_sparsifystructured, As)...) -@inline spbroadcast_c!{N}(f, ::Type{AbstractSparseArray}, ::Type{AbstractArray}, C, B, As::Vararg{Any,N}) = +@inline spbroadcast_c!(f, ::Type{AbstractSparseArray}, ::Type{AbstractArray}, C, B, As::Vararg{Any,N}) where {N} = broadcast_c!(f, Array, Array, C, B, As...) @inline _sparsifystructured(M::AbstractMatrix) = SparseMatrixCSC(M) @@ -1118,10 +1118,10 @@ promote_spcontainertype(::FunnelToSparseBC, ::FunnelToSparseBC) = PromoteToSpars # (12) map[!] over combinations of sparse and structured matrices StructuredMatrix = Union{Diagonal,Bidiagonal,Tridiagonal,SymTridiagonal} SparseOrStructuredMatrix = Union{SparseMatrixCSC,StructuredMatrix} -map{Tf}(f::Tf, A::StructuredMatrix) = _noshapecheck_map(f, _sparsifystructured(A)) -map{Tf,N}(f::Tf, A::SparseOrStructuredMatrix, Bs::Vararg{SparseOrStructuredMatrix,N}) = +map(f::Tf, A::StructuredMatrix) where {Tf} = _noshapecheck_map(f, _sparsifystructured(A)) +map(f::Tf, A::SparseOrStructuredMatrix, Bs::Vararg{SparseOrStructuredMatrix,N}) where {Tf,N} = (_checksameshape(A, Bs...); _noshapecheck_map(f, _sparsifystructured(A), map(_sparsifystructured, Bs)...)) -map!{Tf,N}(f::Tf, C::SparseMatrixCSC, A::SparseOrStructuredMatrix, Bs::Vararg{SparseOrStructuredMatrix,N}) = +map!(f::Tf, C::SparseMatrixCSC, A::SparseOrStructuredMatrix, Bs::Vararg{SparseOrStructuredMatrix,N}) where {Tf,N} = (_checksameshape(C, A, Bs...); _noshapecheck_map!(f, C, _sparsifystructured(A), map(_sparsifystructured, Bs)...)) end diff --git a/base/sparse/linalg.jl b/base/sparse/linalg.jl index 4ec09cc01d8eb..4b27f8fc92410 100644 --- a/base/sparse/linalg.jl +++ b/base/sparse/linalg.jl @@ -20,7 +20,7 @@ increment(A::AbstractArray{<:Integer}) = increment!(copy(A)) ## sparse matrix multiplication -function (*){TvA,TiA,TvB,TiB}(A::SparseMatrixCSC{TvA,TiA}, B::SparseMatrixCSC{TvB,TiB}) +function (*)(A::SparseMatrixCSC{TvA,TiA}, B::SparseMatrixCSC{TvB,TiB}) where {TvA,TiA,TvB,TiB} (*)(sppromote(A, B)...) end for f in (:A_mul_Bt, :A_mul_Bc, @@ -98,7 +98,7 @@ Ac_mul_B!(C::StridedVecOrMat, A::SparseMatrixCSC, B::StridedVecOrMat) = Ac_mul_B At_mul_B!(C::StridedVecOrMat, A::SparseMatrixCSC, B::StridedVecOrMat) = At_mul_B!(one(eltype(B)), A, B, zero(eltype(C)), C) -function (*){TX,TvA,TiA}(X::StridedMatrix{TX}, A::SparseMatrixCSC{TvA,TiA}) +function (*)(X::StridedMatrix{TX}, A::SparseMatrixCSC{TvA,TiA}) where {TX,TvA,TiA} mX, nX = size(X) nX == A.m || throw(DimensionMismatch()) Y = zeros(promote_type(TX,TvA), mX, A.n) @@ -122,7 +122,7 @@ end # Sparse matrix multiplication as described in [Gustavson, 1978]: # http://dl.acm.org/citation.cfm?id=355796 -(*){Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}, B::SparseMatrixCSC{Tv,Ti}) = spmatmul(A,B) +(*)(A::SparseMatrixCSC{Tv,Ti}, B::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti} = spmatmul(A,B) for (f, opA, opB) in ((:A_mul_Bt, :identity, :transpose), (:A_mul_Bc, :identity, :ctranspose), (:At_mul_B, :transpose, :identity), diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index f2826d7fa88c0..34a554a854285 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -163,7 +163,7 @@ end ## Reinterpret and Reshape -function reinterpret{T,Tv}(::Type{T}, a::SparseMatrixCSC{Tv}) +function reinterpret(::Type{T}, a::SparseMatrixCSC{Tv}) where {T,Tv} if sizeof(T) != sizeof(Tv) throw(ArgumentError("SparseMatrixCSC reinterpret is only supported for element types of the same size")) end @@ -206,7 +206,7 @@ function sparse_compute_reshaped_colptr_and_rowval{Ti}(colptrS::Vector{Ti}, rowv end end -function reinterpret{T,Tv,Ti,N}(::Type{T}, a::SparseMatrixCSC{Tv,Ti}, dims::NTuple{N,Int}) +function reinterpret(::Type{T}, a::SparseMatrixCSC{Tv,Ti}, dims::NTuple{N,Int}) where {T,Tv,Ti,N} if sizeof(T) != sizeof(Tv) throw(ArgumentError("SparseMatrixCSC reinterpret is only supported for element types of the same size")) end @@ -293,7 +293,7 @@ function copy!(A::SparseMatrixCSC, B::SparseMatrixCSC) end similar(S::SparseMatrixCSC, Tv::Type=eltype(S)) = SparseMatrixCSC(S.m, S.n, copy(S.colptr), copy(S.rowval), Array{Tv}(length(S.nzval))) -function similar{Tv,Ti}(S::SparseMatrixCSC, ::Type{Tv}, ::Type{Ti}) +function similar(S::SparseMatrixCSC, ::Type{Tv}, ::Type{Ti}) where {Tv,Ti} new_colptr = copy!(similar(S.colptr, Ti), S.colptr) new_rowval = copy!(similar(S.rowval, Ti), S.rowval) new_nzval = copy!(similar(S.nzval, Tv), S.nzval) @@ -302,12 +302,12 @@ end @inline similar{Tv}(S::SparseMatrixCSC, ::Type{Tv}, d::Dims) = spzeros(Tv, d...) # convert'ing between SparseMatrixCSC types -convert{Tv}(::Type{AbstractMatrix{Tv}}, A::SparseMatrixCSC{Tv}) = A -convert{Tv}(::Type{AbstractMatrix{Tv}}, A::SparseMatrixCSC) = convert(SparseMatrixCSC{Tv}, A) -convert{Tv}(::Type{SparseMatrixCSC{Tv}}, S::SparseMatrixCSC{Tv}) = S -convert{Tv}(::Type{SparseMatrixCSC{Tv}}, S::SparseMatrixCSC) = convert(SparseMatrixCSC{Tv,eltype(S.colptr)}, S) -convert{Tv,Ti}(::Type{SparseMatrixCSC{Tv,Ti}}, S::SparseMatrixCSC{Tv,Ti}) = S -function convert{Tv,Ti}(::Type{SparseMatrixCSC{Tv,Ti}}, S::SparseMatrixCSC) +convert(::Type{AbstractMatrix{Tv}}, A::SparseMatrixCSC{Tv}) where {Tv} = A +convert(::Type{AbstractMatrix{Tv}}, A::SparseMatrixCSC) where {Tv} = convert(SparseMatrixCSC{Tv}, A) +convert(::Type{SparseMatrixCSC{Tv}}, S::SparseMatrixCSC{Tv}) where {Tv} = S +convert(::Type{SparseMatrixCSC{Tv}}, S::SparseMatrixCSC) where {Tv} = convert(SparseMatrixCSC{Tv,eltype(S.colptr)}, S) +convert(::Type{SparseMatrixCSC{Tv,Ti}}, S::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti} = S +function convert(::Type{SparseMatrixCSC{Tv,Ti}}, S::SparseMatrixCSC) where {Tv,Ti} eltypeTicolptr = convert(Vector{Ti}, S.colptr) eltypeTirowval = convert(Vector{Ti}, S.rowval) eltypeTvnzval = convert(Vector{Tv}, S.nzval) @@ -315,9 +315,9 @@ function convert{Tv,Ti}(::Type{SparseMatrixCSC{Tv,Ti}}, S::SparseMatrixCSC) end # convert'ing from other matrix types to SparseMatrixCSC (also see sparse()) convert(::Type{SparseMatrixCSC}, M::Matrix) = sparse(M) -convert{Tv}(::Type{SparseMatrixCSC}, M::AbstractMatrix{Tv}) = convert(SparseMatrixCSC{Tv,Int}, M) -convert{Tv}(::Type{SparseMatrixCSC{Tv}}, M::AbstractMatrix{Tv}) = convert(SparseMatrixCSC{Tv,Int}, M) -function convert{Tv,Ti}(::Type{SparseMatrixCSC{Tv,Ti}}, M::AbstractMatrix) +convert(::Type{SparseMatrixCSC}, M::AbstractMatrix{Tv}) where {Tv} = convert(SparseMatrixCSC{Tv,Int}, M) +convert(::Type{SparseMatrixCSC{Tv}}, M::AbstractMatrix{Tv}) where {Tv} = convert(SparseMatrixCSC{Tv,Int}, M) +function convert(::Type{SparseMatrixCSC{Tv,Ti}}, M::AbstractMatrix) where {Tv,Ti} (I, J, V) = findnz(M) eltypeTiI = convert(Vector{Ti}, I) eltypeTiJ = convert(Vector{Ti}, J) @@ -325,7 +325,7 @@ function convert{Tv,Ti}(::Type{SparseMatrixCSC{Tv,Ti}}, M::AbstractMatrix) return sparse_IJ_sorted!(eltypeTiI, eltypeTiJ, eltypeTvV, size(M)...) end # convert'ing from SparseMatrixCSC to other matrix types -function convert{Tv}(::Type{Matrix}, S::SparseMatrixCSC{Tv}) +function convert(::Type{Matrix}, S::SparseMatrixCSC{Tv}) where Tv # Handle cases where zero(Tv) is not defined but the array is dense. A = length(S) == nnz(S) ? Array{Tv}(S.m, S.n) : zeros(Tv, S.m, S.n) for Sj in 1:S.n @@ -1405,7 +1405,7 @@ julia> speye(A) Note the difference from [`spones`](@ref). """ -speye{T}(S::SparseMatrixCSC{T}) = speye(T, size(S, 1), size(S, 2)) +speye(S::SparseMatrixCSC{T}) where {T} = speye(T, size(S, 1), size(S, 2)) eye(S::SparseMatrixCSC) = speye(S) """ @@ -1421,7 +1421,7 @@ multiple `α` of the identity matrix. """ speye(T::Type, m::Integer, n::Integer) = speye_scaled(T, oneunit(T), m, n) -function one{T}(S::SparseMatrixCSC{T}) +function one(S::SparseMatrixCSC{T}) where T m,n = size(S) if m != n; throw(DimensionMismatch("multiplicative identity only defined for square matrices")); end speye(T, m) @@ -1790,7 +1790,7 @@ end getindex(A::SparseMatrixCSC, I::Tuple{Integer,Integer}) = getindex(A, I[1], I[2]) -function getindex{T}(A::SparseMatrixCSC{T}, i0::Integer, i1::Integer) +function getindex(A::SparseMatrixCSC{T}, i0::Integer, i1::Integer) where T if !(1 <= i0 <= A.m && 1 <= i1 <= A.n); throw(BoundsError()); end r1 = Int(A.colptr[i1]) r2 = Int(A.colptr[i1+1]-1) @@ -1837,7 +1837,7 @@ function getindex_cols{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}, J::AbstractVector) return SparseMatrixCSC(m, nJ, colptrS, rowvalS, nzvalS) end -function getindex{Tv,Ti<:Integer}(A::SparseMatrixCSC{Tv,Ti}, I::Range, J::AbstractVector) +function getindex(A::SparseMatrixCSC{Tv,Ti}, I::Range, J::AbstractVector) where {Tv,Ti<:Integer} # Ranges for indexing rows (m, n) = size(A) # whole columns: @@ -2130,7 +2130,7 @@ function getindex_general(A::SparseMatrixCSC, I::AbstractVector, J::AbstractVect end # the general case: -function getindex{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}, I::AbstractVector, J::AbstractVector) +function getindex(A::SparseMatrixCSC{Tv,Ti}, I::AbstractVector, J::AbstractVector) where {Tv,Ti} (m, n) = size(A) if !isempty(J) @@ -2154,7 +2154,7 @@ function getindex{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}, I::AbstractVector, J::Abstra end end -function getindex{Tv}(A::SparseMatrixCSC{Tv}, I::AbstractArray) +function getindex(A::SparseMatrixCSC{Tv}, I::AbstractArray) where Tv szA = size(A) nA = szA[1]*szA[2] colptrA = A.colptr @@ -2208,10 +2208,10 @@ getindex(A::SparseMatrixCSC, I::AbstractVector{<:Integer}, J::AbstractVector{Boo getindex(A::SparseMatrixCSC, I::AbstractVector{Bool}, J::AbstractVector{<:Integer}) = A[find(I),J] ## setindex! -function setindex!{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}, v, i::Integer, j::Integer) +function setindex!(A::SparseMatrixCSC{Tv,Ti}, v, i::Integer, j::Integer) where Tv where Ti setindex!(A, convert(Tv, v), convert(Ti, i), convert(Ti, j)) end -function setindex!{Tv,Ti<:Integer}(A::SparseMatrixCSC{Tv,Ti}, v::Tv, i::Ti, j::Ti) +function setindex!(A::SparseMatrixCSC{Tv,Ti}, v::Tv, i::Ti, j::Ti) where Tv where Ti<:Integer if !((1 <= i <= A.m) & (1 <= j <= A.n)) throw(BoundsError(A, (i,j))) end @@ -2408,17 +2408,17 @@ function _spsetnz_setindex!{Tv}(A::SparseMatrixCSC{Tv}, x::Tv, return A end -setindex!{Tv,Ti,T<:Integer}(A::SparseMatrixCSC{Tv,Ti}, S::Matrix, I::AbstractVector{T}, J::AbstractVector{T}) = +setindex!(A::SparseMatrixCSC{Tv,Ti}, S::Matrix, I::AbstractVector{T}, J::AbstractVector{T}) where {Tv,Ti,T<:Integer} = setindex!(A, convert(SparseMatrixCSC{Tv,Ti}, S), I, J) setindex!(A::SparseMatrixCSC, v::AbstractVector, I::AbstractVector{<:Integer}, j::Integer) = setindex!(A, v, I, [j]) setindex!(A::SparseMatrixCSC, v::AbstractVector, i::Integer, J::AbstractVector{<:Integer}) = setindex!(A, v, [i], J) -setindex!{T<:Integer}(A::SparseMatrixCSC, v::AbstractVector, I::AbstractVector{T}, J::AbstractVector{T}) = +setindex!(A::SparseMatrixCSC, v::AbstractVector, I::AbstractVector{T}, J::AbstractVector{T}) where {T<:Integer} = setindex!(A, reshape(v, length(I), length(J)), I, J) # A[I,J] = B -function setindex!{Tv,Ti,T<:Integer}(A::SparseMatrixCSC{Tv,Ti}, B::SparseMatrixCSC{Tv,Ti}, I::AbstractVector{T}, J::AbstractVector{T}) +function setindex!(A::SparseMatrixCSC{Tv,Ti}, B::SparseMatrixCSC{Tv,Ti}, I::AbstractVector{T}, J::AbstractVector{T}) where {Tv,Ti,T<:Integer} if size(B,1) != length(I) || size(B,2) != length(J) throw(DimensionMismatch("")) end @@ -3238,7 +3238,7 @@ length(d::SpDiagIterator) = d.n start(d::SpDiagIterator) = 1 done(d::SpDiagIterator, j) = j > d.n -function next{Tv}(d::SpDiagIterator{Tv}, j) +function next(d::SpDiagIterator{Tv}, j) where Tv A = d.A r1 = Int(A.colptr[j]) r2 = Int(A.colptr[j+1]-1) diff --git a/base/sparse/sparsevector.jl b/base/sparse/sparsevector.jl index 722f064a0ec14..c6bf69aee00bc 100644 --- a/base/sparse/sparsevector.jl +++ b/base/sparse/sparsevector.jl @@ -36,10 +36,10 @@ nonzeros(x::SparseVector) = x.nzval nonzeroinds(x::SparseVector) = x.nzind similar(x::SparseVector, Tv::Type=eltype(x)) = SparseVector(x.n, copy(x.nzind), Vector{Tv}(length(x.nzval))) -function similar{Tv,Ti}(x::SparseVector, ::Type{Tv}, ::Type{Ti}) +function similar(x::SparseVector, ::Type{Tv}, ::Type{Ti}) where {Tv,Ti} return SparseVector(x.n, copy!(similar(x.nzind, Ti), x.nzind), copy!(similar(x.nzval, Tv), x.nzval)) end -similar{T}(x::SparseVector, ::Type{T}, D::Dims) = spzeros(T, D...) +similar(x::SparseVector, ::Type{T}, D::Dims) where {T} = spzeros(T, D...) ### Construct empty sparse vector @@ -225,7 +225,7 @@ end ### Element access -function setindex!{Tv,Ti<:Integer}(x::SparseVector{Tv,Ti}, v::Tv, i::Ti) +function setindex!(x::SparseVector{Tv,Ti}, v::Tv, i::Ti) where {Tv,Ti<:Integer} checkbounds(x, i) nzind = nonzeroinds(x) nzval = nonzeros(x) @@ -243,7 +243,7 @@ function setindex!{Tv,Ti<:Integer}(x::SparseVector{Tv,Ti}, v::Tv, i::Ti) x end -setindex!{Tv, Ti<:Integer}(x::SparseVector{Tv,Ti}, v, i::Integer) = +setindex!(x::SparseVector{Tv,Ti}, v, i::Integer) where {Tv,Ti<:Integer} = setindex!(x, convert(Tv, v), convert(Ti, i)) @@ -287,15 +287,15 @@ end ### Conversion # convert SparseMatrixCSC to SparseVector -function convert{Tv,Ti<:Integer}(::Type{SparseVector{Tv,Ti}}, s::SparseMatrixCSC{Tv,Ti}) +function convert(::Type{SparseVector{Tv,Ti}}, s::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti<:Integer} size(s, 2) == 1 || throw(ArgumentError("The input argument must have a single-column.")) SparseVector(s.m, s.rowval, s.nzval) end -convert{Tv,Ti}(::Type{SparseVector{Tv}}, s::SparseMatrixCSC{Tv,Ti}) = +convert(::Type{SparseVector{Tv}}, s::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti} = convert(SparseVector{Tv,Ti}, s) -convert{Tv,Ti}(::Type{SparseVector}, s::SparseMatrixCSC{Tv,Ti}) = +convert(::Type{SparseVector}, s::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti} = convert(SparseVector{Tv,Ti}, s) # convert Vector to SparseVector @@ -345,20 +345,20 @@ function _dense2sparsevec{Tv,Ti}(s::AbstractArray{Tv}, initcap::Ti) SparseVector(n, nzind, nzval) end -convert{Tv,Ti}(::Type{SparseVector{Tv,Ti}}, s::AbstractVector{Tv}) = +convert(::Type{SparseVector{Tv,Ti}}, s::AbstractVector{Tv}) where {Tv,Ti} = _dense2sparsevec(s, convert(Ti, max(8, div(length(s), 8)))) -convert{Tv}(::Type{SparseVector{Tv}}, s::AbstractVector{Tv}) = +convert(::Type{SparseVector{Tv}}, s::AbstractVector{Tv}) where {Tv} = convert(SparseVector{Tv,Int}, s) -convert{Tv}(::Type{SparseVector}, s::AbstractVector{Tv}) = +convert(::Type{SparseVector}, s::AbstractVector{Tv}) where {Tv} = convert(SparseVector{Tv,Int}, s) # convert between different types of SparseVector -convert{Tv}(::Type{SparseVector{Tv}}, s::SparseVector{Tv}) = s -convert{Tv,Ti}(::Type{SparseVector{Tv,Ti}}, s::SparseVector{Tv,Ti}) = s -convert{Tv,Ti}(::Type{SparseVector{Tv,Ti}}, s::SparseVector) = +convert(::Type{SparseVector{Tv}}, s::SparseVector{Tv}) where {Tv} = s +convert(::Type{SparseVector{Tv,Ti}}, s::SparseVector{Tv,Ti}) where {Tv,Ti} = s +convert(::Type{SparseVector{Tv,Ti}}, s::SparseVector) where {Tv,Ti} = SparseVector{Tv,Ti}(s.n, convert(Vector{Ti}, s.nzind), convert(Vector{Tv}, s.nzval)) convert{Tv,Ti}(::Type{SparseVector{Tv}}, s::SparseVector{<:Any,Ti}) = @@ -415,7 +415,7 @@ function copy!(A::SparseVector, B::SparseMatrixCSC) return A end -copy!{TvB, TiB}(A::SparseMatrixCSC, B::SparseVector{TvB,TiB}) = +copy!(A::SparseMatrixCSC, B::SparseVector{TvB,TiB}) where {TvB,TiB} = copy!(A, SparseMatrixCSC{TvB,TiB}(B.n, 1, TiB[1, length(B.nzind)+1], B.nzind, B.nzval)) @@ -506,7 +506,7 @@ end # Logical and linear indexing into SparseMatrices getindex(A::SparseMatrixCSC, I::AbstractVector{Bool}) = _logical_index(A, I) # Ambiguities getindex(A::SparseMatrixCSC, I::AbstractArray{Bool}) = _logical_index(A, I) -function _logical_index{Tv}(A::SparseMatrixCSC{Tv}, I::AbstractArray{Bool}) +function _logical_index(A::SparseMatrixCSC{Tv}, I::AbstractArray{Bool}) where Tv checkbounds(A, I) n = sum(I) nnzB = min(n, nnz(A)) @@ -547,7 +547,7 @@ end # TODO: further optimizations are available for ::Colon and other types of Range getindex(A::SparseMatrixCSC, ::Colon) = A[1:end] -function getindex{Tv}(A::SparseMatrixCSC{Tv}, I::UnitRange) +function getindex(A::SparseMatrixCSC{Tv}, I::UnitRange) where Tv checkbounds(A, I) szA = size(A) nA = szA[1]*szA[2] @@ -583,7 +583,7 @@ function getindex{Tv}(A::SparseMatrixCSC{Tv}, I::UnitRange) SparseVector(n, rowvalB, nzvalB) end -function getindex{Tv}(A::SparseMatrixCSC{Tv}, I::AbstractVector) +function getindex(A::SparseMatrixCSC{Tv}, I::AbstractVector) where Tv szA = size(A) nA = szA[1]*szA[2] colptrA = A.colptr @@ -620,7 +620,7 @@ function getindex{Tv}(A::SparseMatrixCSC{Tv}, I::AbstractVector) SparseVector(n, rowvalB, nzvalB) end -function find{Ti}(x::SparseVector{<:Any,Ti}) +function find(x::SparseVector{<:Any,Ti}) where Ti numnz = nnz(x) I = Vector{Ti}(numnz) @@ -643,7 +643,7 @@ function find{Ti}(x::SparseVector{<:Any,Ti}) return I end -function findnz{Tv,Ti}(x::SparseVector{Tv,Ti}) +function findnz(x::SparseVector{Tv,Ti}) where {Tv,Ti} numnz = nnz(x) I = Vector{Ti}(numnz) @@ -674,7 +674,7 @@ end ### getindex -function _spgetindex{Tv,Ti}(m::Int, nzind::AbstractVector{Ti}, nzval::AbstractVector{Tv}, i::Integer) +function _spgetindex(m::Int, nzind::AbstractVector{Ti}, nzval::AbstractVector{Tv}, i::Integer) where {Tv,Ti} ii = searchsortedfirst(nzind, convert(Ti, i)) (ii <= m && nzind[ii] == i) ? nzval[ii] : zero(Tv) end @@ -684,7 +684,7 @@ function getindex(x::AbstractSparseVector, i::Integer) _spgetindex(nnz(x), nonzeroinds(x), nonzeros(x), i) end -function getindex{Tv,Ti}(x::AbstractSparseVector{Tv,Ti}, I::UnitRange) +function getindex(x::AbstractSparseVector{Tv,Ti}, I::UnitRange) where {Tv,Ti} checkbounds(x, I) xlen = length(x) i0 = first(I) @@ -776,7 +776,7 @@ end ### Conversion to matrix -function convert{Tv,Ti}(::Type{SparseMatrixCSC{Tv,Ti}}, x::AbstractSparseVector) +function convert(::Type{SparseMatrixCSC{Tv,Ti}}, x::AbstractSparseVector) where {Tv,Ti} n = length(x) xnzind = nonzeroinds(x) xnzval = nonzeros(x) @@ -792,10 +792,10 @@ end convert{Tv,Ti}(::Type{SparseMatrixCSC{Tv}}, x::AbstractSparseVector{<:Any,Ti}) = convert(SparseMatrixCSC{Tv,Ti}, x) -convert{Tv,Ti}(::Type{SparseMatrixCSC}, x::AbstractSparseVector{Tv,Ti}) = +convert(::Type{SparseMatrixCSC}, x::AbstractSparseVector{Tv,Ti}) where {Tv,Ti} = convert(SparseMatrixCSC{Tv,Ti}, x) -function convert{Tv}(::Type{Vector}, x::AbstractSparseVector{Tv}) +function convert(::Type{Vector}, x::AbstractSparseVector{Tv}) where Tv n = length(x) n == 0 && return Vector{Tv}(0) nzind = nonzeroinds(x) @@ -817,7 +817,7 @@ vec(x::AbstractSparseVector) = x copy(x::AbstractSparseVector) = SparseVector(length(x), copy(nonzeroinds(x)), copy(nonzeros(x))) -function reinterpret{T,Tv}(::Type{T}, x::AbstractSparseVector{Tv}) +function reinterpret(::Type{T}, x::AbstractSparseVector{Tv}) where {T,Tv} sizeof(T) == sizeof(Tv) || throw(ArgumentError("reinterpret of sparse vectors only supports element types of the same size.")) SparseVector(length(x), copy(nonzeroinds(x)), reinterpret(T, nonzeros(x))) @@ -838,9 +838,9 @@ complex(x::AbstractSparseVector) = # back to the horizontal concatenation method that ensures that combinations of # sparse/special/dense matrix/vector types concatenate to SparseMatrixCSCs, instead # of _absspvec_hcat below. The <:Integer qualifications are necessary for correct dispatch. -hcat{Tv,Ti<:Integer}(X::SparseVector{Tv,Ti}...) = _absspvec_hcat(X...) -hcat{Tv,Ti<:Integer}(X::AbstractSparseVector{Tv,Ti}...) = _absspvec_hcat(X...) -function _absspvec_hcat{Tv,Ti}(X::AbstractSparseVector{Tv,Ti}...) +hcat(X::SparseVector{Tv,Ti}...) where {Tv,Ti<:Integer} = _absspvec_hcat(X...) +hcat(X::AbstractSparseVector{Tv,Ti}...) where {Tv,Ti<:Integer} = _absspvec_hcat(X...) +function _absspvec_hcat(X::AbstractSparseVector{Tv,Ti}...) where {Tv,Ti} # check sizes n = length(X) m = length(X[1]) @@ -873,9 +873,9 @@ end # back to the vertical concatenation method that ensures that combinations of # sparse/special/dense matrix/vector types concatenate to SparseMatrixCSCs, instead # of _absspvec_vcat below. The <:Integer qualifications are necessary for correct dispatch. -vcat{Tv,Ti<:Integer}(X::SparseVector{Tv,Ti}...) = _absspvec_vcat(X...) -vcat{Tv,Ti<:Integer}(X::AbstractSparseVector{Tv,Ti}...) = _absspvec_vcat(X...) -function _absspvec_vcat{Tv,Ti}(X::AbstractSparseVector{Tv,Ti}...) +vcat(X::SparseVector{Tv,Ti}...) where {Tv,Ti<:Integer} = _absspvec_vcat(X...) +vcat(X::AbstractSparseVector{Tv,Ti}...) where {Tv,Ti<:Integer} = _absspvec_vcat(X...) +function _absspvec_vcat(X::AbstractSparseVector{Tv,Ti}...) where {Tv,Ti} # check sizes n = length(X) tnnz = 0 @@ -973,10 +973,10 @@ hcat(A::Vector...) = Base.typed_hcat(promote_eltype(A...), A...) hcat(A::_DenseConcatGroup...) = Base.typed_hcat(promote_eltype(A...), A...) hvcat(rows::Tuple{Vararg{Int}}, xs::_DenseConcatGroup...) = Base.typed_hvcat(promote_eltype(xs...), rows, xs...) # For performance, specially handle the case where the matrices/vectors have homogeneous eltype -cat{T}(catdims, xs::_TypedDenseConcatGroup{T}...) = Base.cat_t(catdims, T, xs...) -vcat{T}(A::_TypedDenseConcatGroup{T}...) = Base.typed_vcat(T, A...) -hcat{T}(A::_TypedDenseConcatGroup{T}...) = Base.typed_hcat(T, A...) -hvcat{T}(rows::Tuple{Vararg{Int}}, xs::_TypedDenseConcatGroup{T}...) = Base.typed_hvcat(T, rows, xs...) +cat(catdims, xs::_TypedDenseConcatGroup{T}...) where {T} = Base.cat_t(catdims, T, xs...) +vcat(A::_TypedDenseConcatGroup{T}...) where {T} = Base.typed_vcat(T, A...) +hcat(A::_TypedDenseConcatGroup{T}...) where {T} = Base.typed_hcat(T, A...) +hvcat(rows::Tuple{Vararg{Int}}, xs::_TypedDenseConcatGroup{T}...) where {T} = Base.typed_hvcat(T, rows, xs...) ### math functions @@ -993,7 +993,7 @@ conj(x::SparseVector) = SparseVector(length(x), copy(nonzeroinds(x)), conj(nonze # macro unarymap_nz2z_z2z(op, TF) esc(quote - function $(op){Tv<:$(TF),Ti<:Integer}(x::AbstractSparseVector{Tv,Ti}) + function $(op)(x::AbstractSparseVector{Tv,Ti}) where Tv<:$(TF) where Ti<:Integer R = typeof($(op)(zero(Tv))) xnzind = nonzeroinds(x) xnzval = nonzeros(x) @@ -1039,7 +1039,7 @@ end macro unarymap_z2nz(op, TF) esc(quote - function $(op){Tv<:$(TF)}(x::AbstractSparseVector{Tv,<:Integer}) + function $(op)(x::AbstractSparseVector{Tv,<:Integer}) where Tv<:$(TF) v0 = $(op)(zero(Tv)) R = typeof(v0) xnzind = nonzeroinds(x) @@ -1500,7 +1500,7 @@ end # A_mul_B -function *{Ta,Tx}(A::StridedMatrix{Ta}, x::AbstractSparseVector{Tx}) +function *(A::StridedMatrix{Ta}, x::AbstractSparseVector{Tx}) where {Ta,Tx} m, n = size(A) length(x) == n || throw(DimensionMismatch()) Ty = promote_type(Ta, Tx) @@ -1508,7 +1508,7 @@ function *{Ta,Tx}(A::StridedMatrix{Ta}, x::AbstractSparseVector{Tx}) A_mul_B!(y, A, x) end -A_mul_B!{Tx,Ty}(y::StridedVector{Ty}, A::StridedMatrix, x::AbstractSparseVector{Tx}) = +A_mul_B!(y::StridedVector{Ty}, A::StridedMatrix, x::AbstractSparseVector{Tx}) where {Tx,Ty} = A_mul_B!(one(Tx), A, x, zero(Ty), y) function A_mul_B!(α::Number, A::StridedMatrix, x::AbstractSparseVector, β::Number, y::StridedVector) @@ -1537,7 +1537,7 @@ end # At_mul_B -function At_mul_B{Ta,Tx}(A::StridedMatrix{Ta}, x::AbstractSparseVector{Tx}) +function At_mul_B(A::StridedMatrix{Ta}, x::AbstractSparseVector{Tx}) where {Ta,Tx} m, n = size(A) length(x) == m || throw(DimensionMismatch()) Ty = promote_type(Ta, Tx) @@ -1545,7 +1545,7 @@ function At_mul_B{Ta,Tx}(A::StridedMatrix{Ta}, x::AbstractSparseVector{Tx}) At_mul_B!(y, A, x) end -At_mul_B!{Tx,Ty}(y::StridedVector{Ty}, A::StridedMatrix, x::AbstractSparseVector{Tx}) = +At_mul_B!(y::StridedVector{Ty}, A::StridedMatrix, x::AbstractSparseVector{Tx}) where {Tx,Ty} = At_mul_B!(one(Tx), A, x, zero(Ty), y) function At_mul_B!(α::Number, A::StridedMatrix, x::AbstractSparseVector, β::Number, y::StridedVector) @@ -1603,7 +1603,7 @@ end # A_mul_B -A_mul_B!{Tx,Ty}(y::StridedVector{Ty}, A::SparseMatrixCSC, x::AbstractSparseVector{Tx}) = +A_mul_B!(y::StridedVector{Ty}, A::SparseMatrixCSC, x::AbstractSparseVector{Tx}) where {Tx,Ty} = A_mul_B!(one(Tx), A, x, zero(Ty), y) function A_mul_B!(α::Number, A::SparseMatrixCSC, x::AbstractSparseVector, β::Number, y::StridedVector) @@ -1636,13 +1636,13 @@ end # At_mul_B -At_mul_B!{Tx,Ty}(y::StridedVector{Ty}, A::SparseMatrixCSC, x::AbstractSparseVector{Tx}) = +At_mul_B!(y::StridedVector{Ty}, A::SparseMatrixCSC, x::AbstractSparseVector{Tx}) where {Tx,Ty} = At_mul_B!(one(Tx), A, x, zero(Ty), y) At_mul_B!(α::Number, A::SparseMatrixCSC, x::AbstractSparseVector, β::Number, y::StridedVector) = _At_or_Ac_mul_B!(*, α, A, x, β, y) -Ac_mul_B!{Tx,Ty}(y::StridedVector{Ty}, A::SparseMatrixCSC, x::AbstractSparseVector{Tx}) = +Ac_mul_B!(y::StridedVector{Ty}, A::SparseMatrixCSC, x::AbstractSparseVector{Tx}) where {Tx,Ty} = Ac_mul_B!(one(Tx), A, x, zero(Ty), y) Ac_mul_B!(α::Number, A::SparseMatrixCSC, x::AbstractSparseVector, β::Number, y::StridedVector) = @@ -1690,7 +1690,7 @@ At_mul_B(A::SparseMatrixCSC, x::AbstractSparseVector) = Ac_mul_B(A::SparseMatrixCSC, x::AbstractSparseVector) = _At_or_Ac_mul_B(dot, A, x) -function _At_or_Ac_mul_B{TvA,TiA,TvX,TiX}(tfun::Function, A::SparseMatrixCSC{TvA,TiA}, x::AbstractSparseVector{TvX,TiX}) +function _At_or_Ac_mul_B(tfun::Function, A::SparseMatrixCSC{TvA,TiA}, x::AbstractSparseVector{TvX,TiX}) where {TvA,TiA,TvX,TiX} m, n = size(A) length(x) == m || throw(DimensionMismatch()) Tv = promote_type(TvA, TvX) @@ -1921,7 +1921,7 @@ For an in-place version and algorithmic information, see [`dropzeros!`](@ref). dropzeros(x::SparseVector, trim::Bool = true) = dropzeros!(copy(x), trim) -function _fillnonzero!{Tv,Ti}(arr::SparseMatrixCSC{Tv, Ti}, val) +function _fillnonzero!(arr::SparseMatrixCSC{Tv, Ti}, val) where {Tv,Ti} m, n = size(arr) resize!(arr.colptr, n+1) resize!(arr.rowval, m*n) @@ -1938,7 +1938,7 @@ function _fillnonzero!{Tv,Ti}(arr::SparseMatrixCSC{Tv, Ti}, val) arr end -function _fillnonzero!{Tv,Ti}(arr::SparseVector{Tv,Ti}, val) +function _fillnonzero!(arr::SparseVector{Tv,Ti}, val) where {Tv,Ti} n = arr.n resize!(arr.nzind, n) resize!(arr.nzval, n) diff --git a/base/statistics.jl b/base/statistics.jl index 12bf64921a026..073638f3a0630 100644 --- a/base/statistics.jl +++ b/base/statistics.jl @@ -41,7 +41,7 @@ function mean!(R::AbstractArray, A::AbstractArray) return R end -momenttype{T}(::Type{T}) = typeof((zero(T)*zero(T) + zero(T)*zero(T)) / 2) +momenttype(::Type{T}) where {T} = typeof((zero(T)*zero(T) + zero(T)*zero(T)) / 2) momenttype(::Type{Float32}) = Float32 momenttype(::Type{<:Union{Float64,Int32,Int64,UInt32,UInt64}}) = Float64 @@ -54,7 +54,7 @@ Compute the mean of whole array `v`, or optionally along the dimensions in `regi Julia does not ignore `NaN` values in the computation. For applications requiring the handling of missing data, the `DataArrays.jl` package is recommended. """ -mean{T}(A::AbstractArray{T}, region) = +mean(A::AbstractArray{T}, region) where {T} = mean!(reducedim_initarray(A, region, 0, momenttype(T)), A) @@ -108,7 +108,7 @@ centralize_sumabs2(A::AbstractArray, m::Number) = centralize_sumabs2(A::AbstractArray, m::Number, ifirst::Int, ilast::Int) = mapreduce_impl(centralizedabs2fun(m), +, A, ifirst, ilast) -function centralize_sumabs2!{S}(R::AbstractArray{S}, A::AbstractArray, means::AbstractArray) +function centralize_sumabs2!(R::AbstractArray{S}, A::AbstractArray, means::AbstractArray) where S # following the implementation of _mapreducedim! at base/reducedim.jl lsiz = check_reducedims(R,A) isempty(R) || fill!(R, zero(S)) @@ -147,14 +147,14 @@ function centralize_sumabs2!{S}(R::AbstractArray{S}, A::AbstractArray, means::Ab return R end -function varm{T}(A::AbstractArray{T}, m::Number; corrected::Bool=true) +function varm(A::AbstractArray{T}, m::Number; corrected::Bool=true) where T n = _length(A) n == 0 && return convert(real(momenttype(T)), NaN) n == 1 && return convert(real(momenttype(T)), abs2(A[1] - m)/(1 - Int(corrected))) return centralize_sumabs2(A, m) / (n - Int(corrected)) end -function varm!{S}(R::AbstractArray{S}, A::AbstractArray, m::AbstractArray; corrected::Bool=true) +function varm!(R::AbstractArray{S}, A::AbstractArray, m::AbstractArray; corrected::Bool=true) where S if isempty(A) fill!(R, convert(S, NaN)) else @@ -177,11 +177,11 @@ whereas the sum is scaled with `n` if `corrected` is `false` where `n = length(x applications requiring the handling of missing data, the `DataArrays.jl` package is recommended. """ -varm{T}(A::AbstractArray{T}, m::AbstractArray, region; corrected::Bool=true) = +varm(A::AbstractArray{T}, m::AbstractArray, region; corrected::Bool=true) where {T} = varm!(reducedim_initarray(A, region, 0, real(momenttype(T))), A, m; corrected=corrected) -var{T}(A::AbstractArray{T}; corrected::Bool=true, mean=nothing) = +var(A::AbstractArray{T}; corrected::Bool=true, mean=nothing) where {T} = convert(real(momenttype(T)), varm(A, mean === nothing ? Base.mean(A) : mean; corrected=corrected)) @@ -399,7 +399,7 @@ clampcor(x) = x # cov2cor! -function cov2cor!{T}(C::AbstractMatrix{T}, xsd::AbstractArray) +function cov2cor!(C::AbstractMatrix{T}, xsd::AbstractArray) where T nx = length(xsd) size(C) == (nx, nx) || throw(DimensionMismatch("inconsistent dimensions")) for j = 1:nx @@ -447,7 +447,7 @@ end # corzm (non-exported, with centered data) -corzm{T}(x::AbstractVector{T}) = one(real(T)) +corzm(x::AbstractVector{T}) where {T} = one(real(T)) function corzm(x::AbstractMatrix, vardim::Int=1) c = unscaled_covzm(x, vardim) return cov2cor!(c, sqrt!(diag(c))) @@ -461,7 +461,7 @@ corzm(x::AbstractMatrix, y::AbstractMatrix, vardim::Int=1) = # corm -corm{T}(x::AbstractVector{T}, xmean) = one(real(T)) +corm(x::AbstractVector{T}, xmean) where {T} = one(real(T)) corm(x::AbstractMatrix, xmean, vardim::Int=1) = corzm(x .- xmean, vardim) function corm(x::AbstractVector, mx::Number, y::AbstractVector, my::Number) n = length(x) diff --git a/base/strings/basic.jl b/base/strings/basic.jl index c4cfcf280e27d..4a6c4fa17c73b 100644 --- a/base/strings/basic.jl +++ b/base/strings/basic.jl @@ -69,7 +69,7 @@ julia> "Hello " * "world" """ (*)(s1::AbstractString, ss::AbstractString...) = string(s1, ss...) -one{T<:AbstractString}(::Union{T,Type{T}}) = convert(T, "") +one(::Union{T,Type{T}}) where {T<:AbstractString} = convert(T, "") length(s::DirectIndexString) = endof(s) diff --git a/base/strings/types.jl b/base/strings/types.jl index 6b51d60176527..a57345e8600b6 100644 --- a/base/strings/types.jl +++ b/base/strings/types.jl @@ -73,7 +73,7 @@ chr2ind(s::SubString{<:DirectIndexString}, i::Integer) = begin checkbounds(s,i); nextind(s::SubString, i::Integer) = nextind(s.string, i+s.offset)-s.offset prevind(s::SubString, i::Integer) = prevind(s.string, i+s.offset)-s.offset -convert{T<:AbstractString}(::Type{SubString{T}}, s::T) = SubString(s, 1, endof(s)) +convert(::Type{SubString{T}}, s::T) where {T<:AbstractString} = SubString(s, 1, endof(s)) String(p::SubString{String}) = unsafe_string(pointer(p.string, p.offset+1), nextind(p, p.endof)-1) @@ -94,7 +94,7 @@ end # don't make unnecessary copies when passing substrings to C functions cconvert(::Type{Ptr{UInt8}}, s::SubString{String}) = s cconvert(::Type{Ptr{Int8}}, s::SubString{String}) = s -function unsafe_convert{R<:Union{Int8, UInt8}}(::Type{Ptr{R}}, s::SubString{String}) +function unsafe_convert(::Type{Ptr{R}}, s::SubString{String}) where R<:Union{Int8, UInt8} convert(Ptr{R}, pointer(s.string)) + s.offset end diff --git a/base/strings/utf8proc.jl b/base/strings/utf8proc.jl index 591d0b5793dd8..078ab703e2939 100644 --- a/base/strings/utf8proc.jl +++ b/base/strings/utf8proc.jl @@ -352,7 +352,7 @@ letter combined with an accent mark is a single grapheme.) """ graphemes(s::AbstractString) = GraphemeIterator{typeof(s)}(s) -eltype{S}(::Type{GraphemeIterator{S}}) = SubString{S} +eltype(::Type{GraphemeIterator{S}}) where {S} = SubString{S} function length(g::GraphemeIterator) c0 = Char(0x00ad) # soft hyphen (grapheme break always allowed after this) @@ -387,9 +387,9 @@ end hash(g::GraphemeIterator, h::UInt) = hash(g.s, h) isless(g1::GraphemeIterator, g2::GraphemeIterator) = isless(g1.s, g2.s) -convert{S<:AbstractString}(::Type{S}, g::GraphemeIterator) = convert(S, g.s) +convert(::Type{S}, g::GraphemeIterator) where {S<:AbstractString} = convert(S, g.s) -show{S}(io::IO, g::GraphemeIterator{S}) = print(io, "length-$(length(g)) GraphemeIterator{$S} for \"$(g.s)\"") +show(io::IO, g::GraphemeIterator{S}) where {S} = print(io, "length-$(length(g)) GraphemeIterator{$S} for \"$(g.s)\"") ############################################################################ diff --git a/base/subarray.jl b/base/subarray.jl index 536c569f11920..5c1d6b9e7263c 100644 --- a/base/subarray.jl +++ b/base/subarray.jl @@ -179,7 +179,7 @@ end # In general, we simply re-index the parent indices by the provided ones SlowSubArray{T,N,P,I} = SubArray{T,N,P,I,false} -function getindex{T,N}(V::SlowSubArray{T,N}, I::Vararg{Int,N}) +function getindex(V::SlowSubArray{T,N}, I::Vararg{Int,N}) where {T,N} @_inline_meta @boundscheck checkbounds(V, I...) @inbounds r = V.parent[reindex(V, V.indexes, I)...] @@ -202,7 +202,7 @@ function getindex(V::FastContiguousSubArray, i::Int) r end -function setindex!{T,N}(V::SlowSubArray{T,N}, x, I::Vararg{Int,N}) +function setindex!(V::SlowSubArray{T,N}, x, I::Vararg{Int,N}) where {T,N} @_inline_meta @boundscheck checkbounds(V, I...) @inbounds V.parent[reindex(V, V.indexes, I)...] = x @@ -270,7 +270,7 @@ compute_offset1(parent, stride1::Integer, dims::Tuple{Int}, inds::Tuple{Slice}, compute_offset1(parent, stride1::Integer, dims, inds, I::Tuple) = (@_inline_meta; compute_linindex(parent, I) - stride1) # linear indexing starts with 1 -function compute_linindex{N}(parent, I::NTuple{N,Any}) +function compute_linindex(parent, I::NTuple{N,Any}) where N @_inline_meta IP = fill_to_length(indices(parent), OneTo(1), Val{N}) compute_linindex(1, 1, IP, I) diff --git a/base/test.jl b/base/test.jl index fa639f97b3863..6fdaa48057bd5 100644 --- a/base/test.jl +++ b/base/test.jl @@ -1105,10 +1105,8 @@ end # # Raises an error if any columnwise vector norm exceeds err. Otherwise, returns # nothing. -function test_approx_eq_modphase{S<:Real,T<:Real}( - a::StridedVecOrMat{S}, b::StridedVecOrMat{T}, - err = length(indices(a,1))^3*(eps(S)+eps(T)) - ) +function test_approx_eq_modphase(a::StridedVecOrMat{S}, b::StridedVecOrMat{T}, + err = length(indices(a,1))^3*(eps(S)+eps(T))) where {S<:Real,T<:Real} @test indices(a,1) == indices(b,1) && indices(a,2) == indices(b,2) for i in indices(a,2) v1, v2 = a[:, i], b[:, i] @@ -1129,11 +1127,10 @@ elsewhere. want to set this to `false`. See [`Base.isambiguous`](@ref). """ function detect_ambiguities(mods...; - imported::Bool = false, - ambiguous_bottom::Bool = false, - allow_bottom::Union{Bool,Void} = nothing - ) - if allow_bottom != nothing + imported::Bool = false, + ambiguous_bottom::Bool = false, + allow_bottom::Union{Bool,Void} = nothing) + if allow_bottom !== nothing Base.depwarn("the `allow_bottom` keyword to detect_ambiguities has been renamed to `ambiguous_bottom`", :detect_ambiguities) ambiguous_bottom = allow_bottom end diff --git a/base/twiceprecision.jl b/base/twiceprecision.jl index 74f0829a49261..beb5383301d59 100644 --- a/base/twiceprecision.jl +++ b/base/twiceprecision.jl @@ -62,9 +62,9 @@ nbitslen(len, offset) = len < 2 ? 0 : ceil(Int, log2(max(offset-1, len-offset))) eltype(::Type{TwicePrecision{T}}) where {T} = T -promote_rule{R,S}(::Type{TwicePrecision{R}}, ::Type{TwicePrecision{S}}) = +promote_rule(::Type{TwicePrecision{R}}, ::Type{TwicePrecision{S}}) where {R,S} = TwicePrecision{promote_type(R,S)} -promote_rule{R,S}(::Type{TwicePrecision{R}}, ::Type{S}) = +promote_rule(::Type{TwicePrecision{R}}, ::Type{S}) where {R,S} = TwicePrecision{promote_type(R,S)} convert(::Type{TwicePrecision{T}}, x::TwicePrecision{T}) where {T} = x @@ -81,7 +81,7 @@ big(x::TwicePrecision) = big(x.hi) + big(x.lo) -(x::TwicePrecision) = TwicePrecision(-x.hi, -x.lo) -zero{T}(::Type{TwicePrecision{T}}) = TwicePrecision{T}(0, 0) +zero(::Type{TwicePrecision{T}}) where {T} = TwicePrecision{T}(0, 0) ## StepRangeLen @@ -120,7 +120,7 @@ function floatrange(a::AbstractFloat, st::AbstractFloat, len::Real, divisor::Abs TwicePrecision{T}((st,divisor), nbitslen(T, len, 1)), Int(len), 1) end -function colon{T<:Union{Float16,Float32,Float64}}(start::T, step::T, stop::T) +function colon(start::T, step::T, stop::T) where T<:Union{Float16,Float32,Float64} step == 0 && throw(ArgumentError("range step cannot be zero")) # see if the inputs have exact rational approximations (and if so, # perform all computations in terms of the rationals) @@ -234,10 +234,10 @@ convert(::Type{StepRangeLen{T,R,S}}, r::StepRangeLen{T,R,S}) where {T<:AbstractF convert(::Type{StepRangeLen{T,R,S}}, r::StepRangeLen) where {T<:AbstractFloat,R<:TwicePrecision,S<:TwicePrecision} = _convertSRL(StepRangeLen{T,R,S}, r) -convert{T<:Union{Float16,Float32,Float64}}(::Type{StepRangeLen{T}}, r::StepRangeLen) = +convert(::Type{StepRangeLen{T}}, r::StepRangeLen) where {T<:Union{Float16,Float32,Float64}} = _convertSRL(StepRangeLen{T,TwicePrecision{T},TwicePrecision{T}}, r) -convert{T<:Union{Float16,Float32,Float64}}(::Type{StepRangeLen{T}}, r::Range) = +convert(::Type{StepRangeLen{T}}, r::Range) where {T<:Union{Float16,Float32,Float64}} = _convertSRL(StepRangeLen{T,TwicePrecision{T},TwicePrecision{T}}, r) function _convertSRL{T,R,S}(::Type{StepRangeLen{T,R,S}}, r::StepRangeLen{<:Integer}) @@ -296,7 +296,7 @@ end # sum(1:n) as a product of two integers sumpair(n::Integer) = iseven(n) ? (n+1, n>>1) : (n, (n+1)>>1) -function +{T,R<:TwicePrecision}(r1::StepRangeLen{T,R}, r2::StepRangeLen{T,R}) +function +(r1::StepRangeLen{T,R}, r2::StepRangeLen{T,R}) where T where R<:TwicePrecision len = length(r1) (len == length(r2) || throw(DimensionMismatch("argument dimensions must match"))) @@ -456,7 +456,7 @@ function +(x::TwicePrecision, y::Number) end +(x::Number, y::TwicePrecision) = y+x -function +{T}(x::TwicePrecision{T}, y::TwicePrecision{T}) +function +(x::TwicePrecision{T}, y::TwicePrecision{T}) where T r = x.hi + y.hi s = abs(x.hi) > abs(y.hi) ? (((x.hi - r) + y.hi) + y.lo) + x.lo : (((y.hi - r) + x.hi) + x.lo) + y.lo TwicePrecision(r, s) @@ -483,7 +483,7 @@ end _mul2(x::TwicePrecision, v::Number) = TwicePrecision(x.hi*v, x.lo*v) -function *{R,S<:Number}(x::TwicePrecision{R}, v::S) +function *(x::TwicePrecision{R}, v::S) where R where S<:Number T = promote_type(R, S) _mul2(convert(TwicePrecision{T}, x), convert(T, v)) end diff --git a/base/util.jl b/base/util.jl index 57b6b5fc9f363..116ee5704b635 100644 --- a/base/util.jl +++ b/base/util.jl @@ -844,10 +844,10 @@ The default value for a type for use with the `@kwdef` macro. Returns: """ function kwdef_val end -kwdef_val{T}(::Type{Ptr{T}}) = Ptr{T}(C_NULL) +kwdef_val(::Type{Ptr{T}}) where {T} = Ptr{T}(C_NULL) kwdef_val(::Type{Cstring}) = Cstring(C_NULL) kwdef_val(::Type{Cwstring}) = Cwstring(C_NULL) -kwdef_val{T<:Integer}(::Type{T}) = zero(T) +kwdef_val(::Type{T}) where {T<:Integer} = zero(T) -kwdef_val{T}(::Type{T}) = T() +kwdef_val(::Type{T}) where {T} = T() diff --git a/base/weakkeydict.jl b/base/weakkeydict.jl index bffa5737f115f..232c4d88ac7fd 100644 --- a/base/weakkeydict.jl +++ b/base/weakkeydict.jl @@ -65,8 +65,8 @@ function WeakKeyDict(kv) end end -similar{K,V}(d::WeakKeyDict{K,V}) = WeakKeyDict{K,V}() -similar{K,V}(d::WeakKeyDict, ::Type{Pair{K,V}}) = WeakKeyDict{K,V}() +similar(d::WeakKeyDict{K,V}) where {K,V} = WeakKeyDict{K,V}() +similar(d::WeakKeyDict, ::Type{Pair{K,V}}) where {K,V} = WeakKeyDict{K,V}() # conversion between Dict types function convert(::Type{WeakKeyDict{K,V}},d::Associative) where V where K @@ -87,7 +87,7 @@ islocked(wkh::WeakKeyDict) = islocked(wkh.lock) lock(f, wkh::WeakKeyDict) = lock(f, wkh.lock) trylock(f, wkh::WeakKeyDict) = trylock(f, wkh.lock) -function setindex!{K}(wkh::WeakKeyDict{K}, v, key) +function setindex!(wkh::WeakKeyDict{K}, v, key) where K k = convert(K, key) finalizer(k, wkh.finalizer) lock(wkh) do @@ -96,7 +96,7 @@ function setindex!{K}(wkh::WeakKeyDict{K}, v, key) return wkh end -function getkey{K}(wkh::WeakKeyDict{K}, kk, default) +function getkey(wkh::WeakKeyDict{K}, kk, default) where K return lock(wkh) do k = getkey(wkh.ht, kk, secret_table_token) k === secret_table_token && return default @@ -104,20 +104,20 @@ function getkey{K}(wkh::WeakKeyDict{K}, kk, default) end end -get{K}(wkh::WeakKeyDict{K}, key, default) = lock(() -> get(wkh.ht, key, default), wkh) -get{K}(default::Callable, wkh::WeakKeyDict{K}, key) = lock(() -> get(default, wkh.ht, key), wkh) -get!{K}(wkh::WeakKeyDict{K}, key, default) = lock(() -> get!(wkh.ht, key, default), wkh) -get!{K}(default::Callable, wkh::WeakKeyDict{K}, key) = lock(() -> get!(default, wkh.ht, key), wkh) -pop!{K}(wkh::WeakKeyDict{K}, key) = lock(() -> pop!(wkh.ht, key), wkh) -pop!{K}(wkh::WeakKeyDict{K}, key, default) = lock(() -> pop!(wkh.ht, key, default), wkh) +get(wkh::WeakKeyDict{K}, key, default) where {K} = lock(() -> get(wkh.ht, key, default), wkh) +get(default::Callable, wkh::WeakKeyDict{K}, key) where {K} = lock(() -> get(default, wkh.ht, key), wkh) +get!(wkh::WeakKeyDict{K}, key, default) where {K} = lock(() -> get!(wkh.ht, key, default), wkh) +get!(default::Callable, wkh::WeakKeyDict{K}, key) where {K} = lock(() -> get!(default, wkh.ht, key), wkh) +pop!(wkh::WeakKeyDict{K}, key) where {K} = lock(() -> pop!(wkh.ht, key), wkh) +pop!(wkh::WeakKeyDict{K}, key, default) where {K} = lock(() -> pop!(wkh.ht, key, default), wkh) delete!{K}(wkh::WeakKeyDict{K}, key) = lock(() -> delete!(wkh.ht, key), wkh) empty!(wkh::WeakKeyDict) = (lock(() -> empty!(wkh.ht), wkh); wkh) -haskey{K}(wkh::WeakKeyDict{K}, key) = lock(() -> haskey(wkh.ht, key), wkh) -getindex{K}(wkh::WeakKeyDict{K}, key) = lock(() -> getindex(wkh.ht, key), wkh) +haskey(wkh::WeakKeyDict{K}, key) where {K} = lock(() -> haskey(wkh.ht, key), wkh) +getindex(wkh::WeakKeyDict{K}, key) where {K} = lock(() -> getindex(wkh.ht, key), wkh) isempty(wkh::WeakKeyDict) = isempty(wkh.ht) length(t::WeakKeyDict) = length(t.ht) -function start{K,V}(t::WeakKeyDict{K,V}) +function start(t::WeakKeyDict{K,V}) where V where K gc_token = Ref{Bool}(false) # no keys will be deleted via finalizers until this token is gc'd finalizer(gc_token, function(r) if r[] @@ -130,7 +130,7 @@ function start{K,V}(t::WeakKeyDict{K,V}) return (start(t.ht), gc_token) end done(t::WeakKeyDict, i) = done(t.ht, i[1]) -function next{K,V}(t::WeakKeyDict{K,V}, i) +function next(t::WeakKeyDict{K,V}, i) where V where K gc_token = i[2] wkv, i = next(t.ht, i[1]) kv = Pair{K,V}(wkv[1].value::K, wkv[2])