Skip to content

Commit cb15d63

Browse files
committed
replace FusionTensor with to_fusiontensor
1 parent 7342477 commit cb15d63

File tree

3 files changed

+45
-50
lines changed

3 files changed

+45
-50
lines changed

src/fusiontensor/array_cast.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using TensorAlgebra: contract
1010
# ================================= High level interface =================================
1111

1212
#### cast from array to symmetric
13-
function FusionTensor(
13+
function to_fusiontensor(
1414
array::AbstractArray,
1515
codomain_legs::Tuple{Vararg{AbstractGradedUnitRange}},
1616
domain_legs::Tuple{Vararg{AbstractGradedUnitRange}},

src/fusiontensor/fusiontensor.jl

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,14 @@ end
7676
# BlockArrays interface
7777
function BlockArrays.findblock(ft::FusionTensor, f1::SectorFusionTree, f2::SectorFusionTree)
7878
# find outer block corresponding to fusion trees
79-
@assert ndims_codomain(ft) == length(f1)
80-
@assert ndims_domain(ft) == length(f2)
81-
@assert sector_type(ft) == sector_type(f1)
82-
@assert sector_type(ft) == sector_type(f2)
83-
b1 = ntuple(
84-
i -> findfirst(==(leaves(f1)[i]), blocklabels(codomain_axes(ft)[i])), ndims_codomain(ft)
85-
)
86-
b2 = ntuple(
87-
i -> findfirst(==(leaves(f2)[i]), blocklabels(dual(domain_axes(ft)[i]))),
88-
ndims_domain(ft),
89-
)
79+
@assert typeof((f1, f2)) === keytype(trees_block_mapping(ft))
80+
b1 = find_sector_block.(leaves(f1), codomain_axes(ft))
81+
b2 = find_sector_block.(leaves(f2), dual.(domain_axes(ft)))
9082
return Block(b1..., b2...)
9183
end
84+
function find_sector_block(s::AbstractSector, l::AbstractGradedUnitRange)
85+
return findfirst(==(s), blocklabels(l))
86+
end
9287

9388
function sanitize_axes(raw_legs::Tuple{Vararg{AbstractGradedUnitRange}})
9489
legs = promote_sectors(typeof(first(raw_legs)), raw_legs)
@@ -135,7 +130,7 @@ function promote_sector_type(legs)
135130
end
136131

137132
# initialize with already computed data_matrix
138-
function to_fusiontensor(
133+
function FusionTensor(
139134
mat::AbstractMatrix,
140135
codomain_legs::Tuple{Vararg{AbstractGradedUnitRange}},
141136
domain_legs::Tuple{Vararg{AbstractGradedUnitRange}},

test/basics/test_array_cast.jl

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using Test: @test, @test_broken, @test_throws, @testset
44

55
using BlockArrays: Block, BlockedArray, blocksize
66

7-
using FusionTensors: FusionTensor, data_matrix
7+
using FusionTensors: FusionTensor, data_matrix, to_fusiontensor
88
using GradedUnitRanges: dual, fusion_product, gradedrange
99
using SymmetrySectors: O2, SectorProduct, SU2, TrivialSector, U1
1010

@@ -15,7 +15,7 @@ include("setup.jl")
1515
g = gradedrange([TrivialSector() => 1])
1616
gb = dual(g)
1717
m = ones((1, 1))
18-
ft = FusionTensor(m, (g,), (gb,))
18+
ft = to_fusiontensor(m, (g,), (gb,))
1919
@test size(data_matrix(ft)) == (1, 1)
2020
@test blocksize(data_matrix(ft)) == (1, 1)
2121
@test data_matrix(ft)[1, 1] 1.0
@@ -25,14 +25,14 @@ include("setup.jl")
2525

2626
for elt in (Int, UInt32, Float32)
2727
m = ones(elt, (1, 1))
28-
ft = FusionTensor(m, (g,), (gb,))
28+
ft = to_fusiontensor(m, (g,), (gb,))
2929
@test eltype(ft) === Float64
3030
@test Array(ft) m
3131
end
3232

3333
for elt in (ComplexF32, ComplexF64)
3434
m = ones(elt, (1, 1))
35-
ft = FusionTensor(m, (g,), (gb,))
35+
ft = to_fusiontensor(m, (g,), (gb,))
3636
@test eltype(ft) === ComplexF64
3737
@test Array(ft) m
3838
end
@@ -46,7 +46,7 @@ include("setup.jl")
4646
codomain_legs = (g1, g2)
4747
domain_legs = dual.((g3, g4))
4848
t = convert.(Float64, reshape(collect(1:48), (2, 3, 4, 2)))
49-
ft = FusionTensor(t, codomain_legs, domain_legs)
49+
ft = to_fusiontensor(t, codomain_legs, domain_legs)
5050
@test size(data_matrix(ft)) == (6, 8)
5151
@test blocksize(data_matrix(ft)) == (1, 1)
5252
@test data_matrix(ft)[Block(1, 1)] reshape(t, (6, 8))
@@ -61,7 +61,7 @@ end
6161
g = gradedrange([U1(0) => 1])
6262
gb = dual(g)
6363
m = ones((1, 1))
64-
ft = FusionTensor(m, (g,), (gb,))
64+
ft = to_fusiontensor(m, (g,), (gb,))
6565
@test size(data_matrix(ft)) == (1, 1)
6666
@test blocksize(data_matrix(ft)) == (1, 1)
6767
@test data_matrix(ft)[1, 1] 1.0
@@ -74,7 +74,7 @@ end
7474
g = gradedrange([U1(1) => 2])
7575
gb = dual(g)
7676
m = ones((2, 2))
77-
ft = FusionTensor(m, (g,), (gb,))
77+
ft = to_fusiontensor(m, (g,), (gb,))
7878
@test size(data_matrix(ft)) == (2, 2)
7979
@test blocksize(data_matrix(ft)) == (1, 1)
8080
@test data_matrix(ft)[Block(1, 1)] m
@@ -88,7 +88,7 @@ end
8888
codomain_legs = (g,)
8989
domain_legs = (dual(g),)
9090
dense = Array{Float64}(LinearAlgebra.I(3))
91-
ft = FusionTensor(dense, codomain_legs, domain_legs)
91+
ft = to_fusiontensor(dense, codomain_legs, domain_legs)
9292
@test size(data_matrix(ft)) == (3, 3)
9393
@test blocksize(data_matrix(ft)) == (2, 2)
9494
@test data_matrix(ft)[Block(1, 1)] ones((1, 1))
@@ -98,18 +98,18 @@ end
9898
@test Array(ft) dense
9999
@test Array(adjoint(ft)) adjoint(dense)
100100

101-
@test_throws BoundsError FusionTensor(
101+
@test_throws BoundsError to_fusiontensor(
102102
dense, (gradedrange([U1(1) => 1, U1(2) => 3]),), domain_legs
103103
)
104-
@test_throws MethodError FusionTensor(dense, (g, g), domain_legs)
104+
@test_throws MethodError to_fusiontensor(dense, (g, g), domain_legs)
105105

106106
ba = BlockedArray(dense, [1, 2], [1, 2])
107-
@test_throws DomainError FusionTensor(
107+
@test_throws DomainError to_fusiontensor(
108108
ba, (gradedrange([U1(1) => 1, U1(2) => 3]),), domain_legs
109109
)
110-
@test_throws DomainError FusionTensor(ba, (g, g), domain_legs)
110+
@test_throws DomainError to_fusiontensor(ba, (g, g), domain_legs)
111111
dense[1, 2] = 1 # forbidden
112-
@test_throws InexactError FusionTensor(dense, codomain_legs, domain_legs)
112+
@test_throws InexactError to_fusiontensor(dense, codomain_legs, domain_legs)
113113
end
114114

115115
@testset "several axes, one block" begin
@@ -120,7 +120,7 @@ end
120120
codomain_legs = (g1, g2)
121121
domain_legs = dual.((g3, g4))
122122
t = convert.(Float64, reshape(collect(1:48), (2, 3, 4, 2)))
123-
ft = FusionTensor(t, codomain_legs, domain_legs)
123+
ft = to_fusiontensor(t, codomain_legs, domain_legs)
124124
@test size(data_matrix(ft)) == (6, 8)
125125
@test blocksize(data_matrix(ft)) == (1, 1)
126126
@test data_matrix(ft)[Block(1, 1)] reshape(t, (6, 8))
@@ -141,7 +141,7 @@ end
141141
dense[3:4, 1:3, 5:5, 1:2] .= 2.0
142142
dense[1:2, 4:5, 5:5, 1:2] .= 3.0
143143
dense[3:4, 4:5, 1:4, 3:3] .= 4.0
144-
ft = FusionTensor(dense, codomain_legs, domain_legs)
144+
ft = to_fusiontensor(dense, codomain_legs, domain_legs)
145145
@test size(data_matrix(ft)) == (20, 15)
146146
@test blocksize(data_matrix(ft)) == (3, 4)
147147
@test norm(ft) norm(dense)
@@ -159,7 +159,7 @@ end
159159
domain_legs = (dual(g2), dual(g3), g4)
160160
dense = zeros(ComplexF64, (3, 6, 5, 4))
161161
dense[2:2, 1:1, 1:2, 2:3] .= 1.0im
162-
ft = FusionTensor(dense, codomain_legs, domain_legs)
162+
ft = to_fusiontensor(dense, codomain_legs, domain_legs)
163163
@test size(data_matrix(ft)) == (3, 120)
164164
@test blocksize(data_matrix(ft)) == (3, 8)
165165
@test norm(ft) norm(dense)
@@ -173,36 +173,36 @@ end
173173
v = zeros((6,))
174174
v[1] = 1.0
175175

176-
ft1 = FusionTensor(v, (g,), ())
176+
ft1 = to_fusiontensor(v, (g,), ())
177177
@test isnothing(check_sanity(ft1))
178178
@test ndims(ft1) == 1
179179
@test vec(Array(data_matrix(ft1))) v
180180
@test Array(ft1) v
181181
@test Array(adjoint(ft1)) v
182182

183-
ft2 = FusionTensor(v, (), (dual(g),))
183+
ft2 = to_fusiontensor(v, (), (dual(g),))
184184
@test isnothing(check_sanity(ft2))
185185
@test ndims(ft2) == 1
186186
@test vec(Array(data_matrix(ft2))) v
187187
@test Array(ft2) v
188188
@test Array(adjoint(ft2)) v
189189

190-
ft3 = FusionTensor(v, (dual(g),), ())
190+
ft3 = to_fusiontensor(v, (dual(g),), ())
191191
@test isnothing(check_sanity(ft3))
192192
@test Array(ft3) v
193193
@test Array(adjoint(ft3)) v
194194

195-
ft4 = FusionTensor(v, (), (g,))
195+
ft4 = to_fusiontensor(v, (), (g,))
196196
@test isnothing(check_sanity(ft4))
197197
@test Array(ft4) v
198198
@test Array(adjoint(ft4)) v
199199

200200
zerodim = ones(())
201201
if VERSION < v"1.11"
202-
@test_broken FusionTensor(zerodim, (), ()) isa FusionTensor # https://github.com/JuliaLang/julia/issues/52615
202+
@test_broken to_fusiontensor(zerodim, (), ()) isa FusionTensor # https://github.com/JuliaLang/julia/issues/52615
203203
else
204204
# TODO fix: add specialized method, maybe fix TensorAlgebra
205-
@test_broken FusionTensor(zerodim, (), ())
205+
@test_broken to_fusiontensor(zerodim, (), ())
206206
#@test ft isa FusionTensor
207207
#@test ndims(ft) == 0
208208
#@test isnothing(check_sanity(ft))
@@ -218,7 +218,7 @@ end
218218
g = gradedrange([O2(0) => 1])
219219
gb = dual(g)
220220
m = ones((1, 1))
221-
ft = FusionTensor(m, (gb,), (g,))
221+
ft = to_fusiontensor(m, (gb,), (g,))
222222
@test size(data_matrix(ft)) == (1, 1)
223223
@test blocksize(data_matrix(ft)) == (1, 1)
224224
@test data_matrix(ft)[1, 1] 1.0
@@ -233,7 +233,7 @@ end
233233

234234
# identity
235235
id2 = LinearAlgebra.I((2))
236-
ft = FusionTensor(id2, (g2,), (g2b,))
236+
ft = to_fusiontensor(id2, (g2,), (g2b,))
237237
@test norm(ft) 2
238238
@test isnothing(check_sanity(ft))
239239
@test Array(ft) id2
@@ -250,7 +250,7 @@ end
250250
(2, 2, 2, 2),
251251
)
252252
dense, codomain_legs, domain_legs = sds22, (g2, g2), (g2b, g2b)
253-
ft = FusionTensor(dense, codomain_legs, domain_legs)
253+
ft = to_fusiontensor(dense, codomain_legs, domain_legs)
254254
@test norm(ft) 3 / 2
255255
@test isnothing(check_sanity(ft))
256256
@test Array(ft) sds22
@@ -268,22 +268,22 @@ end
268268
)
269269
sds22b_codomain_legs = (g2, g2b)
270270
dense, codomain_legs, domain_legs = sds22b, (g2, g2b), (g2b, g2)
271-
ftb = FusionTensor(dense, codomain_legs, domain_legs)
271+
ftb = to_fusiontensor(dense, codomain_legs, domain_legs)
272272
@test norm(ftb) 3 / 2
273273
@test isnothing(check_sanity(ft))
274274
@test Array(ftb) sds22b
275275
@test Array(adjoint(ftb)) sds22b
276276

277277
# no domain axis
278278
dense, codomain_legs, domain_legs = sds22, (g2, g2, g2b, g2b), ()
279-
ft = FusionTensor(dense, codomain_legs, domain_legs)
279+
ft = to_fusiontensor(dense, codomain_legs, domain_legs)
280280
@test isnothing(check_sanity(ft))
281281
@test Array(ft) sds22
282282
@test Array(adjoint(ft)) sds22
283283

284284
# no codomain axis
285285
dense, codomain_legs, domain_legs = sds22, (), (g2, g2, g2b, g2b)
286-
ft = FusionTensor(dense, codomain_legs, domain_legs)
286+
ft = to_fusiontensor(dense, codomain_legs, domain_legs)
287287
@test isnothing(check_sanity(ft))
288288
@test Array(ft) sds22
289289
@test Array(adjoint(ft)) sds22
@@ -295,7 +295,7 @@ end
295295
g = gradedrange([SU2(0) => 1])
296296
gb = dual(g)
297297
m = ones((1, 1))
298-
ft = FusionTensor(m, (gb,), (g,))
298+
ft = to_fusiontensor(m, (gb,), (g,))
299299
@test size(data_matrix(ft)) == (1, 1)
300300
@test blocksize(data_matrix(ft)) == (1, 1)
301301
@test data_matrix(ft)[1, 1] 1.0
@@ -310,7 +310,7 @@ end
310310

311311
# identity
312312
id2 = LinearAlgebra.I((2))
313-
ft = FusionTensor(id2, (g2,), (g2b,))
313+
ft = to_fusiontensor(id2, (g2,), (g2b,))
314314
@test norm(ft) 2
315315
@test isnothing(check_sanity(ft))
316316
@test Array(ft) id2
@@ -327,7 +327,7 @@ end
327327
(2, 2, 2, 2),
328328
)
329329
dense, codomain_legs, domain_legs = sds22, (g2, g2), (g2b, g2b)
330-
ft = FusionTensor(dense, codomain_legs, domain_legs)
330+
ft = to_fusiontensor(dense, codomain_legs, domain_legs)
331331
@test norm(ft) 3 / 2
332332
@test isnothing(check_sanity(ft))
333333
@test Array(ft) sds22
@@ -345,22 +345,22 @@ end
345345
)
346346
sds22b_codomain_legs = (g2, g2b)
347347
dense, codomain_legs, domain_legs = sds22b, (g2, g2b), (g2b, g2)
348-
ftb = FusionTensor(dense, codomain_legs, domain_legs)
348+
ftb = to_fusiontensor(dense, codomain_legs, domain_legs)
349349
@test norm(ftb) 3 / 2
350350
@test isnothing(check_sanity(ft))
351351
@test Array(ftb) sds22b
352352
@test Array(adjoint(ftb)) sds22b
353353

354354
# no domain axis
355355
dense, codomain_legs, domain_legs = sds22, (g2b, g2b, g2, g2), ()
356-
ft = FusionTensor(dense, codomain_legs, domain_legs)
356+
ft = to_fusiontensor(dense, codomain_legs, domain_legs)
357357
@test isnothing(check_sanity(ft))
358358
@test Array(ft) sds22
359359
@test Array(adjoint(ft)) sds22
360360

361361
# no codomain axis
362362
dense, codomain_legs, domain_legs = sds22, (), (g2b, g2b, g2, g2)
363-
ft = FusionTensor(dense, codomain_legs, domain_legs)
363+
ft = to_fusiontensor(dense, codomain_legs, domain_legs)
364364
@test isnothing(check_sanity(ft))
365365
@test Array(ft) sds22
366366
@test Array(adjoint(ft)) sds22
@@ -373,7 +373,7 @@ end
373373
domain_legs = dual.(codomain_legs)
374374
d = 8
375375
dense = reshape(LinearAlgebra.I(d^N), ntuple(_ -> d, 2 * N))
376-
ft = FusionTensor(dense, codomain_legs, domain_legs)
376+
ft = to_fusiontensor(dense, codomain_legs, domain_legs)
377377
@test isnothing(check_sanity(ft))
378378
@test Array(ft) dense
379379
@test Array(adjoint(ft)) dense
@@ -396,7 +396,7 @@ end
396396
codomain_legs = (dual(gd),)
397397
gD = gradedrange([SectorProduct(SU2(0), U1(1)) => 1, SectorProduct(s, U1(0)) => 1])
398398
domain_legs = (gD, gD, gD, gD)
399-
ft = FusionTensor(tRVB, codomain_legs, domain_legs)
399+
ft = to_fusiontensor(tRVB, codomain_legs, domain_legs)
400400
@test isnothing(check_sanity(ft))
401401
@test Array(ft) tRVB
402402

@@ -407,7 +407,7 @@ end
407407
SectorProduct(; S=SU2(0), N=U1(1)) => 1, SectorProduct(; S=s, N=U1(0)) => 1
408408
])
409409
domain_legs_nt = (gD_nt, gD_nt, gD_nt, gD_nt)
410-
ft_nt = FusionTensor(tRVB, codomain_legs_nt, domain_legs_nt)
410+
ft_nt = to_fusiontensor(tRVB, codomain_legs_nt, domain_legs_nt)
411411
@test isnothing(check_sanity(ft_nt))
412412
@test Array(ft_nt) tRVB
413413
end

0 commit comments

Comments
 (0)