Skip to content

Commit 16b60ba

Browse files
committed
Better definition of isapprox
1 parent 06d9939 commit 16b60ba

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "KroneckerArrays"
22
uuid = "05d0b138-81bc-4ff7-84be-08becefb1ccc"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.2.7"
4+
version = "0.2.9"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/kroneckerarray.jl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,25 @@ Base.view(a::KroneckerArray{<:Any, 0}) = view(arg1(a)) ⊗ view(arg2(a))
320320
function Base.:(==)(a::KroneckerArray, b::KroneckerArray)
321321
return arg1(a) == arg1(b) && arg2(a) == arg2(b)
322322
end
323-
function Base.isapprox(a::KroneckerArray, b::KroneckerArray; kwargs...)
324-
return isapprox(arg1(a), arg1(b); kwargs...) && isapprox(arg2(a), arg2(b); kwargs...)
323+
324+
using LinearAlgebra: promote_leaf_eltypes
325+
function Base.isapprox(
326+
a::KroneckerArray, b::KroneckerArray;
327+
atol::Real = 0,
328+
rtol::Real = Base.rtoldefault(promote_leaf_eltypes(a), promote_leaf_eltypes(b), atol),
329+
norm::Function = norm
330+
)
331+
a1, a2 = arg1(a), arg2(a)
332+
b1, b2 = arg1(b), arg2(b)
333+
# Approximation of:
334+
# norm(a - b) = norm(a1 ⊗ a2 - b1 ⊗ b2)
335+
# = norm((a1 - b1) ⊗ a2 + b1 ⊗ (a2 - b2) + (a1 - b1) ⊗ (a2 - b2))
336+
diff1 = norm(a1 - b1)
337+
diff2 = norm(a2 - b2)
338+
d = diff1 * norm(a2) + norm(b1) * diff2 + diff1 * diff2
339+
return iszero(rtol) ? d <= atol : d <= max(atol, rtol * max(norm(a), norm(b)))
325340
end
341+
326342
function Base.iszero(a::KroneckerArray)
327343
return iszero(arg1(a)) || iszero(arg2(a))
328344
end

0 commit comments

Comments
 (0)