Skip to content

Commit c0e8dcc

Browse files
committed
Iterator norm in isapprox for Arrays
1 parent b265fea commit c0e8dcc

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/generic.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2004,7 +2004,7 @@ function isapprox(x::AbstractArray, y::AbstractArray;
20042004
atol::Real=0,
20052005
rtol::Real=Base.rtoldefault(promote_leaf_eltypes(x),promote_leaf_eltypes(y),atol),
20062006
nans::Bool=false, norm::Function=norm)
2007-
d = norm(x - y)
2007+
d = norm_x_minus_y(x, y)
20082008
if isfinite(d)
20092009
return iszero(rtol) ? d <= atol : d <= max(atol, rtol*max(norm(x), norm(y)))
20102010
else
@@ -2014,6 +2014,12 @@ function isapprox(x::AbstractArray, y::AbstractArray;
20142014
end
20152015
end
20162016

2017+
norm_x_minus_y(x, y) = norm(x - y)
2018+
function norm_x_minus_y(x::Array, y::Array)
2019+
Base.promote_shape(size(x), size(y)) # ensure same size
2020+
norm(Iterators.map(splat(-), zip(x,y)))
2021+
end
2022+
20172023
"""
20182024
normalize!(a::AbstractArray, p::Real=2)
20192025

test/generic.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,4 +938,11 @@ end
938938
@test B == A2
939939
end
940940

941+
@testset "isapprox alloc" begin
942+
A = rand(3,3)
943+
isapprox(A, A)
944+
n = @allocated isapprox(A, A)
945+
@test n == 0
946+
end
947+
941948
end # module TestGeneric

0 commit comments

Comments
 (0)