Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2004,7 +2004,7 @@ 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)
d = norm_x_minus_y(x, y)
if isfinite(d)
return iszero(rtol) ? d <= atol : d <= max(atol, rtol*max(norm(x), norm(y)))
else
Expand All @@ -2014,6 +2014,18 @@ 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)
Base.promote_shape(size(x), size(y)) # ensure compatible size
if isempty(x) && isempty(y)
norm(zero(eltype(x)) - zero(eltype(y)))
else
norm(Iterators.map(splat(-), zip(x,y)))
end
end

"""
normalize!(a::AbstractArray, p::Real=2)

Expand Down
7 changes: 7 additions & 0 deletions test/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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