Skip to content

Commit a72e030

Browse files
authored
Generalize printing (#102)
1 parent d20d66c commit a72e030

3 files changed

Lines changed: 28 additions & 3 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "KroneckerArrays"
22
uuid = "05d0b138-81bc-4ff7-84be-08becefb1ccc"
3-
version = "0.3.26"
3+
version = "0.3.27"
44
authors = ["ITensor developers <support@itensor.org> and contributors"]
55

66
[workspace]

src/kroneckerarray.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,20 +275,26 @@ function Base.axes(ab::AbstractKroneckerArray)
275275
return axes(a) axes(b)
276276
end
277277

278-
function Base.print_array(io::IO, ab::KroneckerArray)
278+
function Base.print_array(io::IO, ab::AbstractKroneckerArray)
279279
a, b = kroneckerfactors(ab)
280280
Base.print_array(io, a)
281281
println(io, "\n")
282282
Base.print_array(io, b)
283283
return nothing
284284
end
285-
function Base.show(io::IO, ab::KroneckerArray)
285+
function Base.show(io::IO, ab::AbstractKroneckerArray)
286286
a, b = kroneckerfactors(ab)
287287
show(io, a)
288288
print(io, "")
289289
show(io, b)
290290
return nothing
291291
end
292+
function Base.show(io::IO, ::MIME"text/plain", ab::AbstractKroneckerArray)
293+
summary(io, ab)
294+
println(io, ":")
295+
Base.print_array(io, ab)
296+
return nothing
297+
end
292298

293299
(a1::AbstractArray, a2::AbstractArray) = KroneckerArray(a1, a2)
294300
(a1::Number, a2::Number) = a1 * a2

test/test_basics.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ using StableRNGs: StableRNG
1212
using Test: @test, @test_broken, @test_throws, @testset
1313
using TestExtras: @constinferred
1414

15+
module TestBasicsUtils
16+
using KroneckerArrays: KroneckerArrays
17+
18+
struct WrappedKroneckerArray{T, N, A <: AbstractArray{T, N}, B <: AbstractArray{T, N}} <:
19+
KroneckerArrays.AbstractKroneckerArray{T, N}
20+
a::A
21+
b::B
22+
end
23+
function WrappedKroneckerArray(a::AbstractArray{T, N}, b::AbstractArray{T, N}) where {T, N}
24+
return WrappedKroneckerArray{T, N, typeof(a), typeof(b)}(a, b)
25+
end
26+
KroneckerArrays.kroneckerfactors(ab::WrappedKroneckerArray) = (ab.a, ab.b)
27+
end
28+
1529
elts = (Float32, Float64, ComplexF32, ComplexF64)
1630
@testset "KroneckerArrays (eltype=$elt)" for elt in elts
1731
p = [1, 2] × [3, 4, 5]
@@ -141,6 +155,11 @@ elts = (Float32, Float64, ComplexF32, ComplexF64)
141155
@test bc.style === style
142156
@test collect(copy(bc)) 2 * collect(a)
143157

158+
# Display
159+
aw = TestBasicsUtils.WrappedKroneckerArray(randn(elt, 2, 2), randn(elt, 3, 3))
160+
@test occursin("", sprint(show, aw))
161+
@test occursin("\n\n", sprint(show, MIME("text/plain"), aw))
162+
144163
# Mapping
145164
a = randn(elt, 2, 2) randn(elt, 3, 3)
146165
@test_throws "not supported" map(sin, a)

0 commit comments

Comments
 (0)