From 6fb1248e3a6ccbd8035337ba884ccfa25dfd594c Mon Sep 17 00:00:00 2001 From: Andy Ferris Date: Mon, 24 Jul 2017 14:30:06 +1000 Subject: [PATCH] Deprecate `+`/`-` methods for `array+scalar` etc The elementwise definition is incorrect for linear algebra. --- NEWS.md | 4 +++ base/abstractarray.jl | 2 +- base/arraymath.jl | 2 +- base/dates/periods.jl | 2 -- base/deprecated.jl | 11 ++++++ base/linalg/symmetric.jl | 2 +- base/random.jl | 2 +- base/range.jl | 71 +++++++++++++++++++++++++------------ base/repl/LineEdit.jl | 2 +- base/show.jl | 2 +- base/sparse/cholmod.jl | 2 +- base/sparse/sparsevector.jl | 2 +- test/arrayops.jl | 24 ++++++------- test/broadcast.jl | 5 ++- test/complex.jl | 4 +-- test/dates/periods.jl | 32 ++++++----------- test/hashing.jl | 8 ++--- test/inference.jl | 2 +- test/linalg/diagonal.jl | 2 +- test/linalg/symmetric.jl | 10 +++--- test/offsetarray.jl | 2 +- test/ranges.jl | 18 +++++----- test/reducedim.jl | 12 +++---- test/sparse/sparse.jl | 14 ++++---- test/stacktraces.jl | 2 +- test/statistics.jl | 2 +- 26 files changed, 135 insertions(+), 106 deletions(-) diff --git a/NEWS.md b/NEWS.md index 65dbd86392592..f25eeac049acf 100644 --- a/NEWS.md +++ b/NEWS.md @@ -309,6 +309,10 @@ Deprecated or removed * `Base.cpad` has been removed; use an appropriate combination of `rpad` and `lpad` instead ([#23187]). + * Automatically broadcasted `+` and `-` for `array + scalar`, `scalar - array`, and so-on have + been deprecated due to inconsistency with linear algebra. Use `.+` and `.-` for these operations + instead. + Julia v0.6.0 Release Notes ========================== diff --git a/base/abstractarray.jl b/base/abstractarray.jl index a74127c3c770f..fa83235dafd03 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -1256,7 +1256,7 @@ function _cat(A, shape::NTuple{N}, catdims, X...) where N for x in X for i = 1:N if concat[i] - inds[i] = offsets[i] + cat_indices(x, i) + inds[i] = offsets[i] .+ cat_indices(x, i) offsets[i] += cat_size(x, i) else inds[i] = 1:shape[i] diff --git a/base/arraymath.jl b/base/arraymath.jl index e71cd63faa89e..25aba9e9b96e6 100644 --- a/base/arraymath.jl +++ b/base/arraymath.jl @@ -40,7 +40,7 @@ for f in (:+, :-) end end -for f in (:/, :\, :*, :+, :-) +for f in (:/, :\, :*) if f != :/ @eval ($f)(A::Number, B::AbstractArray) = broadcast($f, A, B) end diff --git a/base/dates/periods.jl b/base/dates/periods.jl index 91391421c9b19..6f30613c31595 100644 --- a/base/dates/periods.jl +++ b/base/dates/periods.jl @@ -358,8 +358,6 @@ GeneralPeriod = Union{Period, CompoundPeriod} for op in (:+, :-) @eval begin - ($op)(x::GeneralPeriod, Y::StridedArray{<:GeneralPeriod}) = broadcast($op, x, Y) - ($op)(Y::StridedArray{<:GeneralPeriod}, x::GeneralPeriod) = broadcast($op, Y, x) ($op)(X::StridedArray{<:GeneralPeriod}, Y::StridedArray{<:GeneralPeriod}) = reshape(CompoundPeriod[($op)(x, y) for (x, y) in zip(X, Y)], promote_shape(size(X), size(Y))) end diff --git a/base/deprecated.jl b/base/deprecated.jl index a9a5c93e269e5..d45cdf7c23076 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1684,6 +1684,17 @@ end # PR #22742: change in isapprox semantics @deprecate rtoldefault(x,y) rtoldefault(x,y,0) false +# PR #22932 +@deprecate +(a::Number, b::AbstractArray) broadcast(+, a, b) +@deprecate +(a::AbstractArray, b::Number) broadcast(+, a, b) +@deprecate -(a::Number, b::AbstractArray) broadcast(-, a, b) +@deprecate -(a::AbstractArray, b::Number) broadcast(-, a, b) + +@deprecate +(a::Dates.GeneralPeriod, b::StridedArray{<:Dates.GeneralPeriod}) broadcast(+, a, b) +@deprecate +(a::StridedArray{<:Dates.GeneralPeriod}, b::Dates.GeneralPeriod) broadcast(+, a, b) +@deprecate -(a::Dates.GeneralPeriod, b::StridedArray{<:Dates.GeneralPeriod}) broadcast(-, a, b) +@deprecate -(a::StridedArray{<:Dates.GeneralPeriod}, b::Dates.GeneralPeriod) broadcast(-, a, b) + # END 0.7 deprecations # BEGIN 1.0 deprecations diff --git a/base/linalg/symmetric.jl b/base/linalg/symmetric.jl index 8d98709daaf7f..67972a5e5c917 100644 --- a/base/linalg/symmetric.jl +++ b/base/linalg/symmetric.jl @@ -321,7 +321,7 @@ A_mul_Bt(A::AbstractTriangular, B::RealHermSymComplexSym) = A*B Ac_mul_B(A::RealHermSymComplexHerm, B::AbstractTriangular) = A*B A_mul_Bc(A::AbstractTriangular, B::RealHermSymComplexHerm) = A*B -for T in (:Symmetric, :Hermitian), op in (:+, :-, :*, :/) +for T in (:Symmetric, :Hermitian), op in (:*, :/) # Deal with an ambiguous case @eval ($op)(A::$T, x::Bool) = ($T)(($op)(A.data, x), Symbol(A.uplo)) S = T == :Hermitian ? :Real : :Number diff --git a/base/random.jl b/base/random.jl index c8ca64d16eacf..d8cc54fd80d5e 100644 --- a/base/random.jl +++ b/base/random.jl @@ -284,7 +284,7 @@ srand(r::MersenneTwister, n::Integer) = srand(r, make_seed(n)) function dsfmt_gv_srand() # Temporary fix for #8874 and #9124: update global RNG for Rmath - dsfmt_gv_init_by_array(GLOBAL_RNG.seed+UInt32(1)) + dsfmt_gv_init_by_array(GLOBAL_RNG.seed .+ UInt32(1)) return GLOBAL_RNG end diff --git a/base/range.jl b/base/range.jl index 381efd9ea5f2f..c4e5bc8f99e94 100644 --- a/base/range.jl +++ b/base/range.jl @@ -728,27 +728,6 @@ end -(r::StepRangeLen) = StepRangeLen(-r.ref, -r.step, length(r), r.offset) -(r::LinSpace) = LinSpace(-r.start, -r.stop, length(r)) -+(x::Real, r::AbstractUnitRange) = range(x + first(r), length(r)) -# For #18336 we need to prevent promotion of the step type: -+(x::Number, r::AbstractUnitRange) = range(x + first(r), step(r), length(r)) -+(x::Number, r::Range) = (x+first(r)):step(r):(x+last(r)) -function +(x::Number, r::StepRangeLen) - newref = x + r.ref - StepRangeLen{eltype(newref),typeof(newref),typeof(r.step)}(newref, r.step, length(r), r.offset) -end -function +(x::Number, r::LinSpace) - LinSpace(x + r.start, x + r.stop, r.len) -end -+(r::Range, x::Number) = x + r # assumes addition is commutative - --(x::Number, r::Range) = (x-first(r)):-step(r):(x-last(r)) --(x::Number, r::StepRangeLen) = +(x, -r) -function -(x::Number, r::LinSpace) - LinSpace(x - r.start, x - r.stop, r.len) -end - --(r::Range, x::Number) = +(-x, r) - *(x::Number, r::Range) = range(x*first(r), x*step(r), length(r)) *(x::Number, r::StepRangeLen) = StepRangeLen(x*r.ref, x*r.step, length(r), r.offset) *(x::Number, r::LinSpace) = LinSpace(x * r.start, x * r.stop, r.len) @@ -760,8 +739,53 @@ end /(r::Range, x::Number) = range(first(r)/x, step(r)/x, length(r)) /(r::StepRangeLen, x::Number) = StepRangeLen(r.ref/x, r.step/x, length(r), r.offset) /(r::LinSpace, x::Number) = LinSpace(r.start / x, r.stop / x, r.len) +# also, separate in case of noncommutative multiplication (division) +\(x::Number, r::Range) = range(x\first(r), x\step(r), x\length(r)) +\(x::Number, r::StepRangeLen) = StepRangeLen(x\r.ref, x\r.step, length(r), r.offset) +\(x::Number, r::LinSpace) = LinSpace(x \ r.start, x \ r.stop, r.len) + +## scalar-range broadcast operations ## + +broadcast(::typeof(-), r::OrdinalRange) = range(-first(r), -step(r), length(r)) +broadcast(::typeof(-), r::StepRangeLen) = StepRangeLen(-r.ref, -r.step, length(r), r.offset) +broadcast(::typeof(-), r::LinSpace) = LinSpace(-r.start, -r.stop, length(r)) -/(x::Number, r::Range) = [ x/y for y=r ] +broadcast(::typeof(+), x::Real, r::AbstractUnitRange) = range(x + first(r), length(r)) +# For #18336 we need to prevent promotion of the step type: +broadcast(::typeof(+), x::Number, r::AbstractUnitRange) = range(x + first(r), step(r), length(r)) +broadcast(::typeof(+), x::Number, r::Range) = (x+first(r)):step(r):(x+last(r)) +function broadcast(::typeof(+), x::Number, r::StepRangeLen) + newref = x + r.ref + StepRangeLen{eltype(newref),typeof(newref),typeof(r.step)}(newref, r.step, length(r), r.offset) +end +function broadcast(::typeof(+), x::Number, r::LinSpace) + LinSpace(x + r.start, x + r.stop, r.len) +end +broadcast(::typeof(+), r::Range, x::Number) = broadcast(+, x, r) # assumes addition is commutative + +broadcast(::typeof(-), x::Number, r::Range) = (x-first(r)):-step(r):(x-last(r)) +broadcast(::typeof(-), x::Number, r::StepRangeLen) = broadcast(+, x, -r) +function broadcast(::typeof(-), x::Number, r::LinSpace) + LinSpace(x - r.start, x - r.stop, r.len) +end + +broadcast(::typeof(-), r::Range, x::Number) = broadcast(+, -x, r) # assumes addition is commutative + +broadcast(::typeof(*), x::Number, r::Range) = range(x*first(r), x*step(r), length(r)) +broadcast(::typeof(*), x::Number, r::StepRangeLen) = StepRangeLen(x*r.ref, x*r.step, length(r), r.offset) +broadcast(::typeof(*), x::Number, r::LinSpace) = LinSpace(x * r.start, x * r.stop, r.len) +# separate in case of noncommutative multiplication +broadcast(::typeof(*), r::Range, x::Number) = range(first(r)*x, step(r)*x, length(r)) +broadcast(::typeof(*), r::StepRangeLen, x::Number) = StepRangeLen(r.ref*x, r.step*x, length(r), r.offset) +broadcast(::typeof(*), r::LinSpace, x::Number) = LinSpace(r.start * x, r.stop * x, r.len) + +broadcast(::typeof(/), r::Range, x::Number) = range(first(r)/x, step(r)/x, length(r)) +broadcast(::typeof(/), r::StepRangeLen, x::Number) = StepRangeLen(r.ref/x, r.step/x, length(r), r.offset) +broadcast(::typeof(/), r::LinSpace, x::Number) = LinSpace(r.start / x, r.stop / x, r.len) +# also, separate in case of noncommutative multiplication (division) +broadcast(::typeof(\), x::Number, r::Range) = range(x\first(r), x\step(r), x\length(r)) +broadcast(::typeof(\), x::Number, r::StepRangeLen) = StepRangeLen(x\r.ref, x\r.step, length(r), r.offset) +broadcast(::typeof(\), x::Number, r::LinSpace) = LinSpace(x \ r.start, x \ r.stop, r.len) # promote eltype if at least one container wouldn't change, otherwise join container types. el_same(::Type{T}, a::Type{<:AbstractArray{T,n}}, b::Type{<:AbstractArray{T,n}}) where {T,n} = a @@ -945,3 +969,6 @@ function +(r1::StepRangeLen{T,S}, r2::StepRangeLen{T,S}) where {T,S} end -(r1::StepRangeLen, r2::StepRangeLen) = +(r1, -r2) + +broadcast(::typeof(+), r1::Range, r2::Range) = r1 + r2 +broadcast(::typeof(-), r1::Range, r2::Range) = r1 - r2 diff --git a/base/repl/LineEdit.jl b/base/repl/LineEdit.jl index 8c894bf32ea3f..78af65562e8fd 100644 --- a/base/repl/LineEdit.jl +++ b/base/repl/LineEdit.jl @@ -437,7 +437,7 @@ function splice_buffer!(buf::IOBuffer, r::UnitRange{<:Integer}, ins::AbstractStr elseif pos > last(r) seek(buf, pos - length(r)) end - splice!(buf.data, r + 1, Vector{UInt8}(ins)) # position(), etc, are 0-indexed + splice!(buf.data, broadcast(+, r, 1), Vector{UInt8}(ins)) # position(), etc, are 0-indexed buf.size = buf.size + sizeof(ins) - length(r) seek(buf, position(buf) + sizeof(ins)) end diff --git a/base/show.jl b/base/show.jl index 303055c80b633..e645742646ba2 100644 --- a/base/show.jl +++ b/base/show.jl @@ -1574,7 +1574,7 @@ function print_matrix(io::IO, X::AbstractVecOrMat, print(io, i == first(rowsA) ? pre : presp) print_matrix_row(io, X,Lalign,i,colsA[1:length(Lalign)],sep) print(io, (i - first(rowsA)) % hmod == 0 ? hdots : repeat(" ", length(hdots))) - print_matrix_row(io, X,Ralign,i,n-length(Ralign)+colsA,sep) + print_matrix_row(io, X, Ralign, i, (n - length(Ralign)) .+ colsA, sep) print(io, i == last(rowsA) ? post : postsp) if i != last(rowsA); println(io); end end diff --git a/base/sparse/cholmod.jl b/base/sparse/cholmod.jl index 5351a0717c041..d5fb5f2b4b34e 100644 --- a/base/sparse/cholmod.jl +++ b/base/sparse/cholmod.jl @@ -811,7 +811,7 @@ end function get_perm(F::Factor) s = unsafe_load(pointer(F)) p = unsafe_wrap(Array, s.Perm, s.n, false) - p + 1 + p .+ 1 end get_perm(FC::FactorComponent) = get_perm(Factor(FC)) diff --git a/base/sparse/sparsevector.jl b/base/sparse/sparsevector.jl index 80225e4d328c8..cead7751600b5 100644 --- a/base/sparse/sparsevector.jl +++ b/base/sparse/sparsevector.jl @@ -1892,7 +1892,7 @@ function sort(x::SparseVector{Tv,Ti}; kws...) where {Tv,Ti} n,k = length(x),length(allvals) z = findfirst(sinds,k) newnzind = collect(Ti,1:k-1) - newnzind[z:end]+= n-k+1 + newnzind[z:end] .+= n-k+1 newnzvals = allvals[deleteat!(sinds[1:k],z)] SparseVector(n,newnzind,newnzvals) end diff --git a/test/arrayops.jl b/test/arrayops.jl index 9055e55ba49df..57a29b4fa4cbd 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -1661,21 +1661,21 @@ end @testset "binary ops on bool arrays" begin A = Array(trues(5)) - @test A + true == [2,2,2,2,2] + @test A .+ true == [2,2,2,2,2] A = Array(trues(5)) - @test A + false == [1,1,1,1,1] + @test A .+ false == [1,1,1,1,1] A = Array(trues(5)) - @test true + A == [2,2,2,2,2] + @test true .+ A == [2,2,2,2,2] A = Array(trues(5)) - @test false + A == [1,1,1,1,1] + @test false .+ A == [1,1,1,1,1] A = Array(trues(5)) - @test A - true == [0,0,0,0,0] + @test A .- true == [0,0,0,0,0] A = Array(trues(5)) - @test A - false == [1,1,1,1,1] + @test A .- false == [1,1,1,1,1] A = Array(trues(5)) - @test true - A == [0,0,0,0,0] + @test true .- A == [0,0,0,0,0] A = Array(trues(5)) - @test false - A == [-1,-1,-1,-1,-1] + @test false .- A == [-1,-1,-1,-1,-1] end @testset "simple transposes" begin @@ -1731,8 +1731,8 @@ module RetTypeDecl broadcast(::typeof(*), x::MeterUnits{T,1}, y::MeterUnits{T,1}) where {T} = MeterUnits{T,2}(x.val*y.val) convert(::Type{MeterUnits{T,pow}}, y::Real) where {T,pow} = MeterUnits{T,pow}(convert(T,y)) - @test @inferred(m+[m,m]) == [m+m,m+m] - @test @inferred([m,m]+m) == [m+m,m+m] + @test @inferred(m .+ [m,m]) == [m+m,m+m] + @test @inferred([m,m] .+ m) == [m+m,m+m] @test @inferred(broadcast(*,m,[m,m])) == [m2,m2] @test @inferred(broadcast(*,[m,m],m)) == [m2,m2] @test @inferred([m 2m; m m]*[m,m]) == [3m2,2m2] @@ -1838,7 +1838,7 @@ copy!(S, A) @test flipdim(A, 1) == flipdim(B, 1) == flipdim(S, 2) @test flipdim(A, 2) == flipdim(B, 2) == flipdim(S, 2) -@test A + 1 == B + 1 == S + 1 +@test A .+ 1 == B .+ 1 == S .+ 1 @test 2*A == 2*B == 2*S @test A/3 == B/3 == S/3 @@ -1943,7 +1943,7 @@ end #issue #18336 @test cumsum([-0.0, -0.0])[1] === cumsum([-0.0, -0.0])[2] === -0.0 -@test cumprod(-0.0im + (0:0))[1] === Complex(0.0, -0.0) +@test cumprod(-0.0im .+ (0:0))[1] === Complex(0.0, -0.0) module TestNLoops15895 diff --git a/test/broadcast.jl b/test/broadcast.jl index aa84edb2c700f..7658c60bfceff 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -216,8 +216,8 @@ let A = [sqrt(i)+j for i = 1:3, j=1:4] @test atan2.(log.(A), sum(A,1)) == broadcast(atan2, broadcast(log, A), sum(A, 1)) end let x = sin.(1:10) - @test atan2.((x->x+1).(x), (x->x+2).(x)) == broadcast(atan2, x+1, x+2) == broadcast(atan2, x.+1, x.+2) - @test sin.(atan2.([x+1,x+2]...)) == sin.(atan2.(x+1,x+2)) == @. sin(atan2(x+1,x+2)) + @test atan2.((x->x+1).(x), (x->x+2).(x)) == broadcast(atan2, x.+1, x.+2) + @test sin.(atan2.([x.+1,x.+2]...)) == sin.(atan2.(x.+1 ,x.+2)) == @. sin(atan2(x+1,x+2)) @test sin.(atan2.(x, 3.7)) == broadcast(x -> sin(atan2(x,3.7)), x) @test atan2.(x, 3.7) == broadcast(x -> atan2(x,3.7), x) == broadcast(atan2, x, 3.7) end @@ -406,7 +406,6 @@ end @test (-).(C_NULL, C_NULL)::UInt == 0 @test (+).(1, Ref(2)) == fill(3) @test (+).(Ref(1), Ref(2)) == fill(3) -@test (+).([[0,2], [1,3]], [1,-1]) == [[1,3], [0,2]] @test (+).([[0,2], [1,3]], Ref{Vector{Int}}([1,-1])) == [[1,1], [2,2]] # Check that broadcast!(f, A) populates A via independent calls to f (#12277, #19722), diff --git a/test/complex.jl b/test/complex.jl index e1428a826573e..12ab48753fdaa 100644 --- a/test/complex.jl +++ b/test/complex.jl @@ -897,8 +897,8 @@ end @testset "round and float, PR #8291" begin @test round(Complex(1.125, 0.875), 2) == Complex(1.12, 0.88) @test round(Complex(1.5, 0.5), RoundDown, RoundUp) == Complex(1.0, 1.0) - @test round.([1:5;] + im) == [1:5;] + im - @test round.([1:5;] + 0.5im) == [1.0:5.0;] + @test round.([1:5;] .+ im) == [1:5;] .+ im + @test round.([1:5;] .+ 0.5im) == [1.0:5.0;] @test float(Complex(1, 2)) == Complex(1.0, 2.0) @test round(float(Complex(π, e)),3) == Complex(3.142, 2.718) diff --git a/test/dates/periods.jl b/test/dates/periods.jl index 86cf216733305..d6923bf136d16 100644 --- a/test/dates/periods.jl +++ b/test/dates/periods.jl @@ -29,9 +29,9 @@ t = Dates.Year(1) t2 = Dates.Year(2) -@test ([t, t, t, t, t] + Dates.Year(1)) == ([t2, t2, t2, t2, t2]) -@test (Dates.Year(1) + [t, t, t, t, t]) == ([t2, t2, t2, t2, t2]) -@test ([t2, t2, t2, t2, t2] - Dates.Year(1)) == ([t, t, t, t, t]) +@test ([t, t, t, t, t] .+ Dates.Year(1)) == ([t2, t2, t2, t2, t2]) +@test (Dates.Year(1) .+ [t, t, t, t, t]) == ([t2, t2, t2, t2, t2]) +@test ([t2, t2, t2, t2, t2] .- Dates.Year(1)) == ([t, t, t, t, t]) @test_throws MethodError ([t, t, t, t, t] .* Dates.Year(1)) == ([t, t, t, t, t]) @test ([t, t, t, t, t] * 1) == ([t, t, t, t, t]) @test ([t, t, t, t, t] .% t2) == ([t, t, t, t, t]) @@ -352,15 +352,15 @@ cpa = [1y + 1s 1m + 1s 1w + 1s 1d + 1s; 1h + 1s 1mi + 1s 2m + 1s 1s + 1ms] @test cpa .+ 1y == [2y + 1s 1y + 1m + 1s 1y + 1w + 1s 1y + 1d + 1s; 1y + 1h + 1s 1y + 1mi + 1s 1y + 2m + 1s 1y + 1ms + 1s] @test cpa .+ (1y + 1m) == [2y + 1m + 1s 1y + 2m + 1s 1y + 1m + 1w + 1s 1y + 1m + 1d + 1s; 1y + 1m + 1h + 1s 1y + 1m + 1mi + 1s 1y + 3m + 1s 1y + 1m + 1s + 1ms] -@test 1y + pa == [2y 1y + 1m 1y + 1w 1y + 1d; 1y + 1h 1y + 1mi 1y + 1s 1y + 1ms] -@test (1y + 1m) + pa == [2y + 1m 1y + 2m 1y + 1m + 1w 1y + 1m + 1d; 1y + 1m + 1h 1y + 1m + 1mi 1y + 1m + 1s 1y + 1m + 1ms] -@test pa + 1y == [2y 1y + 1m 1y + 1w 1y + 1d; 1y + 1h 1y + 1mi 1y + 1s 1y + 1ms] -@test pa + (1y + 1m) == [2y + 1m 1y + 2m 1y + 1m + 1w 1y + 1m + 1d; 1y + 1m + 1h 1y + 1m + 1mi 1y + 1m + 1s 1y + 1m + 1ms] +@test 1y .+ pa == [2y 1y + 1m 1y + 1w 1y + 1d; 1y + 1h 1y + 1mi 1y + 1s 1y + 1ms] +@test (1y + 1m) .+ pa == [2y + 1m 1y + 2m 1y + 1m + 1w 1y + 1m + 1d; 1y + 1m + 1h 1y + 1m + 1mi 1y + 1m + 1s 1y + 1m + 1ms] +@test pa .+ 1y == [2y 1y + 1m 1y + 1w 1y + 1d; 1y + 1h 1y + 1mi 1y + 1s 1y + 1ms] +@test pa .+ (1y + 1m) == [2y + 1m 1y + 2m 1y + 1m + 1w 1y + 1m + 1d; 1y + 1m + 1h 1y + 1m + 1mi 1y + 1m + 1s 1y + 1m + 1ms] -@test 1y + cpa == [2y + 1s 1y + 1m + 1s 1y + 1w + 1s 1y + 1d + 1s; 1y + 1h + 1s 1y + 1mi + 1s 1y + 2m + 1s 1y + 1ms + 1s] -@test (1y + 1m) + cpa == [2y + 1m + 1s 1y + 2m + 1s 1y + 1m + 1w + 1s 1y + 1m + 1d + 1s; 1y + 1m + 1h + 1s 1y + 1m + 1mi + 1s 1y + 3m + 1s 1y + 1m + 1s + 1ms] -@test cpa + 1y == [2y + 1s 1y + 1m + 1s 1y + 1w + 1s 1y + 1d + 1s; 1y + 1h + 1s 1y + 1mi + 1s 1y + 2m + 1s 1y + 1ms + 1s] -@test cpa + (1y + 1m) == [2y + 1m + 1s 1y + 2m + 1s 1y + 1m + 1w + 1s 1y + 1m + 1d + 1s; 1y + 1m + 1h + 1s 1y + 1m + 1mi + 1s 1y + 3m + 1s 1y + 1m + 1s + 1ms] +@test 1y .+ cpa == [2y + 1s 1y + 1m + 1s 1y + 1w + 1s 1y + 1d + 1s; 1y + 1h + 1s 1y + 1mi + 1s 1y + 2m + 1s 1y + 1ms + 1s] +@test (1y + 1m) .+ cpa == [2y + 1m + 1s 1y + 2m + 1s 1y + 1m + 1w + 1s 1y + 1m + 1d + 1s; 1y + 1m + 1h + 1s 1y + 1m + 1mi + 1s 1y + 3m + 1s 1y + 1m + 1s + 1ms] +@test cpa .+ 1y == [2y + 1s 1y + 1m + 1s 1y + 1w + 1s 1y + 1d + 1s; 1y + 1h + 1s 1y + 1mi + 1s 1y + 2m + 1s 1y + 1ms + 1s] +@test cpa .+ (1y + 1m) == [2y + 1m + 1s 1y + 2m + 1s 1y + 1m + 1w + 1s 1y + 1m + 1d + 1s; 1y + 1m + 1h + 1s 1y + 1m + 1mi + 1s 1y + 3m + 1s 1y + 1m + 1s + 1ms] @test 1y .- pa == [0y 1y-1m 1y-1w 1y-1d; 1y-1h 1y-1mi 1y-1s 1y-1ms] @test (1y + 1m) .- pa == [1m 1y 1y + 1m-1w 1y + 1m-1d; 1y + 1m-1h 1y + 1m-1mi 1y + 1m-1s 1y + 1m-1ms] @@ -372,16 +372,6 @@ cpa = [1y + 1s 1m + 1s 1w + 1s 1d + 1s; 1h + 1s 1mi + 1s 2m + 1s 1s + 1ms] @test cpa .- 1y == [1s -1y + 1m + 1s -1y + 1w + 1s -1y + 1d + 1s; -1y + 1h + 1s -1y + 1mi + 1s -1y + 2m + 1s -1y + 1ms + 1s] @test cpa .- (1y + 1m) == [-1m + 1s -1y + 1s -1y-1m + 1w + 1s -1y-1m + 1d + 1s; -1y-1m + 1h + 1s -1y-1m + 1mi + 1s -1y + 1m + 1s -1y + -1m + 1s + 1ms] -@test 1y - pa == [0y 1y-1m 1y-1w 1y-1d; 1y-1h 1y-1mi 1y-1s 1y-1ms] -@test (1y + 1m) - pa == [1m 1y 1y + 1m-1w 1y + 1m-1d; 1y + 1m-1h 1y + 1m-1mi 1y + 1m-1s 1y + 1m-1ms] -@test pa - (1y + 1m) == [-1m -1y -1y-1m + 1w -1y-1m + 1d; -1y-1m + 1h -1y-1m + 1mi -1y-1m + 1s -1y-1m + 1ms] -@test pa - 1y == [0y 1m-1y -1y + 1w -1y + 1d; -1y + 1h -1y + 1mi -1y + 1s -1y + 1ms] - -@test 1y - cpa == [-1s 1y-1m-1s 1y-1w-1s 1y-1d-1s; 1y-1h-1s 1y-1mi-1s 1y-2m-1s 1y-1ms-1s] -@test (1y + 1m) - cpa == [1m-1s 1y-1s 1y + 1m-1w-1s 1y + 1m-1d-1s; 1y + 1m-1h-1s 1y + 1m-1mi-1s 1y-1m-1s 1y + 1m-1s-1ms] -@test cpa - 1y == [1s -1y + 1m + 1s -1y + 1w + 1s -1y + 1d + 1s; -1y + 1h + 1s -1y + 1mi + 1s -1y + 2m + 1s -1y + 1ms + 1s] -@test cpa - (1y + 1m) == [-1m + 1s -1y + 1s -1y-1m + 1w + 1s -1y-1m + 1d + 1s; -1y-1m + 1h + 1s -1y-1m + 1mi + 1s -1y + 1m + 1s -1y + -1m + 1s + 1ms] - @test [1y 1m; 1w 1d] + [1h 1mi; 1s 1ms] == [1y + 1h 1m + 1mi; 1w + 1s 1d + 1ms] @test [1y 1m; 1w 1d] - [1h 1mi; 1s 1ms] == [1y-1h 1m-1mi; 1w-1s 1d-1ms] @test [1y 1m; 1w 1d] - [1h 1mi; 1s 1ms] - [1y-1h 1m-1mi; 1w-1s 1d-1ms] == [emptyperiod emptyperiod; emptyperiod emptyperiod] diff --git a/test/hashing.jl b/test/hashing.jl index de532dba449b7..488e2e60635f0 100644 --- a/test/hashing.jl +++ b/test/hashing.jl @@ -8,13 +8,13 @@ types = Any[ ] vals = vcat( typemin(Int64), - -Int64(maxintfloat(Float64))+Int64[-4:1;], + -Int64(maxintfloat(Float64)) .+ Int64[-4:1;], typemin(Int32), - -Integer(maxintfloat(Float32))+(-4:1), + -Integer(maxintfloat(Float32)) .+ (-4:1), -2:2, - Integer(maxintfloat(Float32))+(-1:4), + Integer(maxintfloat(Float32)) .+ (-1:4), typemax(Int32), - Int64(maxintfloat(Float64))+Int64[-1:4;], + Int64(maxintfloat(Float64)) .+ Int64[-1:4;], typemax(Int64), ) diff --git a/test/inference.jl b/test/inference.jl index 0d6458dba76b6..baedbca824a68 100644 --- a/test/inference.jl +++ b/test/inference.jl @@ -543,7 +543,7 @@ tpara18457(::Type{A}) where {A<:AbstractMyType18457} = tpara18457(supertype(A)) @test tpara18457(MyType18457{true}) === true @testset "type inference error #19322" begin - Y_19322 = reshape(round.(Int, abs.(randn(5*1000)))+1,1000,5) + Y_19322 = reshape(round.(Int, abs.(randn(5*1000))) .+ 1, 1000, 5) function FOO_19322(Y::AbstractMatrix; frac::Float64=0.3, nbins::Int=100, n_sims::Int=100) num_iters, num_chains = size(Y) diff --git a/test/linalg/diagonal.jl b/test/linalg/diagonal.jl index b1e8df0bd3ffb..7afbfeff5da56 100644 --- a/test/linalg/diagonal.jl +++ b/test/linalg/diagonal.jl @@ -271,7 +271,7 @@ end end @testset "isposdef" begin - @test isposdef(Diagonal(1.0 + rand(n))) + @test isposdef(Diagonal(1.0 .+ rand(n))) @test !isposdef(Diagonal(-1.0 * rand(n))) end diff --git a/test/linalg/symmetric.jl b/test/linalg/symmetric.jl index 54cfb2753af27..216ebbabcbf56 100644 --- a/test/linalg/symmetric.jl +++ b/test/linalg/symmetric.jl @@ -389,12 +389,12 @@ end @test Y - I == T([0 -1; -1 0]) @test Y * I == Y - @test Y + 1 == T([2 0; 0 2]) - @test Y - 1 == T([0 -2; -2 0]) + @test Y .+ 1 == T([2 0; 0 2]) + @test Y .- 1 == T([0 -2; -2 0]) @test Y * 2 == T([2 -2; -2 2]) @test Y / 1 == Y - @test T([true false; false true]) + true == T([2 1; 1 2]) + @test T([true false; false true]) .+ true == T([2 1; 1 2]) end @test_throws ArgumentError Hermitian(X) + 2im*I @@ -424,8 +424,8 @@ end @testset "inversion of Hilbert matrix" begin for T in (Float64, Complex128) H = T[1/(i + j - 1) for i in 1:8, j in 1:8] - @test norm(inv(Symmetric(H))*(H*ones(8))-1) ≈ 0 atol = 1e-5 - @test norm(inv(Hermitian(H))*(H*ones(8))-1) ≈ 0 atol = 1e-5 + @test norm(inv(Symmetric(H))*(H*ones(8)) .- 1) ≈ 0 atol = 1e-5 + @test norm(inv(Hermitian(H))*(H*ones(8)) .- 1) ≈ 0 atol = 1e-5 end end diff --git a/test/offsetarray.jl b/test/offsetarray.jl index 46ace5061822f..404e63e05e57c 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -383,7 +383,7 @@ v = OffsetArray(rand(8), (-2,)) @test flipdim(A, 1) == OffsetArray(flipdim(parent(A), 1), A.offsets) @test flipdim(A, 2) == OffsetArray(flipdim(parent(A), 2), A.offsets) -@test A+1 == OffsetArray(parent(A)+1, A.offsets) +@test A .+ 1 == OffsetArray(parent(A) .+ 1, A.offsets) @test 2*A == OffsetArray(2*parent(A), A.offsets) @test A+A == OffsetArray(parent(A)+parent(A), A.offsets) @test A.*A == OffsetArray(parent(A).*parent(A), A.offsets) diff --git a/test/ranges.jl b/test/ranges.jl index 6ec04c6f89038..18287a4ab4f93 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -546,8 +546,8 @@ r7484 = 0.1:0.1:1 # issue #7387 for r in (0:1, 0.0:1.0) - @test [r+im;] == [r;]+im - @test [r-im;] == [r;]-im + @test [r .+ im;] == [r;] .+ im + @test [r .- im;] == [r;] .- im @test [r*im;] == [r;]*im @test [r/im;] == [r;]/im end @@ -710,13 +710,13 @@ function test_linspace_identity(r::Range{T}, mr) where T @test -collect(r) == collect(mr) @test isa(-r, typeof(r)) - @test 1 + r + (-1) == r - @test 1 + collect(r) == collect(1 + r) == collect(r + 1) - @test isa(1 + r + (-1), typeof(r)) - @test 1 - r - 1 == mr - @test 1 - collect(r) == collect(1 - r) == collect(1 + mr) - @test collect(r) - 1 == collect(r - 1) == -collect(mr + 1) - @test isa(1 - r - 1, typeof(r)) + @test broadcast(+, broadcast(+, 1, r), -1) == r + @test 1 .+ collect(r) == collect(1 .+ r) == collect(r .+ 1) + @test isa(1 .+ r .+ (-1), typeof(r)) + @test broadcast(-, broadcast(-, 1, r), 1) == mr + @test 1 .- collect(r) == collect(1 .- r) == collect(1 .+ mr) + @test collect(r) .- 1 == collect(r .- 1) == -collect(mr .+ 1) + @test isa(broadcast(-, broadcast(-, 1, r), 1), typeof(r)) @test 1 * r * 1 == r @test 2 * r * T(0.5) == r diff --git a/test/reducedim.jl b/test/reducedim.jl index e5d6fb1fcdd54..e03270493295f 100644 --- a/test/reducedim.jl +++ b/test/reducedim.jl @@ -33,7 +33,7 @@ for region in Any[ # With init=false r2 = similar(r) fill!(r, 1) - @test sum!(r, Areduc, init=false) ≈ safe_sum(Areduc, region)+1 + @test sum!(r, Areduc, init=false) ≈ safe_sum(Areduc, region) .+ 1 fill!(r, 2.2) @test prod!(r, Areduc, init=false) ≈ safe_prod(Areduc, region)*2.2 fill!(r, 1.8) @@ -41,9 +41,9 @@ for region in Any[ fill!(r, -0.2) @test minimum!(r, Areduc, init=false) ≈ fill!(r2, -0.2) fill!(r, 8.1) - @test sum!(abs, r, Areduc, init=false) ≈ safe_sumabs(Areduc, region)+8.1 + @test sum!(abs, r, Areduc, init=false) ≈ safe_sumabs(Areduc, region) .+ 8.1 fill!(r, 8.1) - @test sum!(abs2, r, Areduc, init=false) ≈ safe_sumabs2(Areduc, region)+8.1 + @test sum!(abs2, r, Areduc, init=false) ≈ safe_sumabs2(Areduc, region) .+ 8.1 fill!(r, 1.5) @test maximum!(abs, r, Areduc, init=false) ≈ fill!(r2, 1.5) fill!(r, -1.5) @@ -71,11 +71,11 @@ r = fill(NaN, map(length, Base.reduced_indices(indices(Breduc), 1))) @test sum(abs2, Breduc, 1) ≈ safe_sumabs2(Breduc, 1) fill!(r, 4.2) -@test sum!(r, Breduc, init=false) ≈ safe_sum(Breduc, 1)+4.2 +@test sum!(r, Breduc, init=false) ≈ safe_sum(Breduc, 1) .+ 4.2 fill!(r, -6.3) -@test sum!(abs, r, Breduc, init=false) ≈ safe_sumabs(Breduc, 1)-6.3 +@test sum!(abs, r, Breduc, init=false) ≈ safe_sumabs(Breduc, 1) .- 6.3 fill!(r, -1.1) -@test sum!(abs2, r, Breduc, init=false) ≈ safe_sumabs2(Breduc, 1)-1.1 +@test sum!(abs2, r, Breduc, init=false) ≈ safe_sumabs2(Breduc, 1) .- 1.1 # Small arrays with init=false A = reshape(1:15, 3, 5) diff --git a/test/sparse/sparse.jl b/test/sparse/sparse.jl index 714c8fea0f582..b86e4252e6c5e 100644 --- a/test/sparse/sparse.jl +++ b/test/sparse/sparse.jl @@ -448,15 +448,15 @@ end for f in (sum, prod, minimum, maximum) # Test with a map function that maps to non-zero for arr in (se33, sA, pA) - @test f(x->x+1, arr) ≈ f(arr+1) + @test f(x->x+1, arr) ≈ f(arr .+ 1) end # case where f(0) would throw - @test f(x->sqrt(x-1), pA+1) ≈ f(sqrt.(pA)) + @test f(x->sqrt(x-1), pA .+ 1) ≈ f(sqrt.(pA)) # these actually throw due to #10533 - # @test f(x->sqrt(x-1), pA+1, 1) ≈ f(sqrt(pA), 1) - # @test f(x->sqrt(x-1), pA+1, 2) ≈ f(sqrt(pA), 2) - # @test f(x->sqrt(x-1), pA+1, 3) ≈ f(pA) + # @test f(x->sqrt(x-1), pA .+ 1, 1) ≈ f(sqrt(pA), 1) + # @test f(x->sqrt(x-1), pA .+ 1, 2) ≈ f(sqrt(pA), 2) + # @test f(x->sqrt(x-1), pA .+ 1, 3) ≈ f(pA) end @testset "empty cases" begin @@ -1713,8 +1713,8 @@ end @test Y - I == T(sparse([0 -1; -1 0])) @test Y * I == Y - @test Y + 1 == T(sparse([2 0; 0 2])) - @test Y - 1 == T(sparse([0 -2; -2 0])) + @test Y .+ 1 == T(sparse([2 0; 0 2])) + @test Y .- 1 == T(sparse([0 -2; -2 0])) @test Y * 2 == T(sparse([2 -2; -2 2])) @test Y / 1 == Y end diff --git a/test/stacktraces.jl b/test/stacktraces.jl index 8ad2eed101095..ee93ab6b79d7f 100644 --- a/test/stacktraces.jl +++ b/test/stacktraces.jl @@ -6,7 +6,7 @@ let @noinline child() = stacktrace() @noinline parent() = child() @noinline grandparent() = parent() - line_numbers = @__LINE__() - [3, 2, 1] + line_numbers = @__LINE__() .- [3, 2, 1] stack = grandparent() # Basic tests. diff --git a/test/statistics.jl b/test/statistics.jl index 55dd0fdf71c2b..eb2fb119f1dd2 100644 --- a/test/statistics.jl +++ b/test/statistics.jl @@ -349,7 +349,7 @@ y = [0.40003674665581906,0.4085630862624367,0.41662034698690303,0.41662034698690 # variance of complex arrays (#13309) let z = rand(Complex128, 10) - @test var(z) ≈ invoke(var, Tuple{Any}, z) ≈ cov(z) ≈ var(z,1)[1] ≈ sum(abs2, z - mean(z))/9 + @test var(z) ≈ invoke(var, Tuple{Any}, z) ≈ cov(z) ≈ var(z,1)[1] ≈ sum(abs2, z .- mean(z))/9 @test isa(var(z), Float64) @test isa(invoke(var, Tuple{Any}, z), Float64) @test isa(cov(z), Float64)