diff --git a/src/generic.jl b/src/generic.jl index c8336f87..c0090c10 100644 --- a/src/generic.jl +++ b/src/generic.jl @@ -2004,7 +2004,12 @@ function isapprox(x::AbstractArray, y::AbstractArray; atol::Real=0, rtol::Real=Base.rtoldefault(promote_leaf_eltypes(x),promote_leaf_eltypes(y),atol), nans::Bool=false, norm::Function=norm) - d = norm(x - y) + Base.promote_shape(size(x), size(y)) # ensure compatible size + d = if isempty(x) && isempty(y) + norm(zero(eltype(x)) - zero(eltype(y))) + else + norm_x_minus_y(x, y) + end if isfinite(d) return iszero(rtol) ? d <= atol : d <= max(atol, rtol*max(norm(x), norm(y))) else @@ -2014,6 +2019,13 @@ function isapprox(x::AbstractArray, y::AbstractArray; end end +norm_x_minus_y(x, y) = norm(x - y) +FastContiguousArrayView{T,N,P<:Array,I<:Tuple{AbstractUnitRange, Vararg{Any}}} = Base.SubArray{T,N,P,I,true} +const ArrayOrFastContiguousArrayView = Union{Array, FastContiguousArrayView} +function norm_x_minus_y(x::ArrayOrFastContiguousArrayView, y::ArrayOrFastContiguousArrayView) + norm(Iterators.map(splat(-), zip(x,y))) +end + """ normalize!(a::AbstractArray, p::Real=2) diff --git a/test/generic.jl b/test/generic.jl index 4fd48a6f..c6037315 100644 --- a/test/generic.jl +++ b/test/generic.jl @@ -938,4 +938,11 @@ end @test B == A2 end +@testset "isapprox for Arrays" begin + A = rand(3,3) + n = @allocated isapprox(A, A) + @test n == 0 + @test Int[] ≈ Int[] +end + end # module TestGeneric