-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TensorAlgebra] Matricized QR tensor decomposition (#1266)
- Loading branch information
Showing
7 changed files
with
94 additions
and
21 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
NDTensors/src/TensorAlgebra/src/LinearAlgebraExtensions/LinearAlgebraExtensions.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module LinearAlgebraExtensions | ||
using LinearAlgebra: LinearAlgebra, qr | ||
using ..TensorAlgebra: | ||
TensorAlgebra, | ||
BipartitionedPermutation, | ||
bipartition, | ||
bipartitioned_permutations, | ||
matricize, | ||
unmatricize | ||
|
||
include("qr.jl") | ||
end |
21 changes: 21 additions & 0 deletions
21
NDTensors/src/TensorAlgebra/src/LinearAlgebraExtensions/qr.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function LinearAlgebra.qr(a::AbstractArray, labels_a, labels_q, labels_r) | ||
return qr(a, bipartitioned_permutations(qr, labels_a, labels_q, labels_r)...) | ||
end | ||
|
||
function LinearAlgebra.qr(a::AbstractArray, biperm::BipartitionedPermutation) | ||
# TODO: Use a thin QR, define `qr_thin`. | ||
a_matricized = matricize(a, biperm) | ||
q_matricized, r_matricized = qr(a_matricized) | ||
q_matricized_thin = typeof(a_matricized)(q_matricized) | ||
axes_codomain, axes_domain = bipartition(axes(a), biperm) | ||
q = unmatricize(q_matricized_thin, axes_codomain, (axes(q_matricized_thin, 2),)) | ||
r = unmatricize(r_matricized, (axes(r_matricized, 1),), axes_domain) | ||
return q, r | ||
end | ||
|
||
function TensorAlgebra.bipartitioned_permutations(qr, labels_a, labels_q, labels_r) | ||
# TODO: Use something like `findall`? | ||
pos_q = map(l -> findfirst(isequal(l), labels_a), labels_q) | ||
pos_r = map(l -> findfirst(isequal(l), labels_a), labels_r) | ||
return (BipartitionedPermutation(pos_q, pos_r),) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,51 @@ | ||
using Combinatorics: permutations | ||
using LinearAlgebra: qr | ||
using NDTensors.TensorAlgebra: TensorAlgebra | ||
using TensorOperations: TensorOperations | ||
using Test: @test, @testset | ||
using Test: @test, @test_broken, @testset | ||
|
||
@testset "TensorAlgebra" begin | ||
dims = (2, 3, 4, 5) | ||
labels = (:a, :b, :c, :d) | ||
for (d1s, d2s) in (((1, 2), (2, 3)), ((1, 2, 3), (2, 3, 4)), ((1, 2, 3), (3, 4))) | ||
a1 = randn(map(i -> dims[i], d1s)) | ||
labels1 = map(i -> labels[i], d1s) | ||
a2 = randn(map(i -> dims[i], d2s)) | ||
labels2 = map(i -> labels[i], d2s) | ||
for perm1 in permutations(1:ndims(a1)), perm2 in permutations(1:ndims(a2)) | ||
a1′ = permutedims(a1, perm1) | ||
a2′ = permutedims(a2, perm2) | ||
labels1′ = map(i -> labels1[i], perm1) | ||
labels2′ = map(i -> labels2[i], perm2) | ||
a_dest, labels_dest = TensorAlgebra.contract(a1′, labels1′, a2′, labels2′) | ||
@test labels_dest == symdiff(labels1′, labels2′) | ||
a_dest_tensoroperations = TensorOperations.tensorcontract( | ||
labels_dest, a1′, labels1′, a2′, labels2′ | ||
) | ||
@test a_dest ≈ a_dest_tensoroperations | ||
elts = (Float32, ComplexF32, Float64, ComplexF64) | ||
@testset "contract (eltype1=$elt1, eltype2=$elt2)" for elt1 in elts, elt2 in elts | ||
dims = (2, 3, 4, 5) | ||
labels = (:a, :b, :c, :d) | ||
for (d1s, d2s) in (((1, 2), (2, 3)), ((1, 2, 3), (2, 3, 4)), ((1, 2, 3), (3, 4))) | ||
a1 = randn(elt1, map(i -> dims[i], d1s)) | ||
labels1 = map(i -> labels[i], d1s) | ||
a2 = randn(elt2, map(i -> dims[i], d2s)) | ||
labels2 = map(i -> labels[i], d2s) | ||
for perm1 in permutations(1:ndims(a1)), perm2 in permutations(1:ndims(a2)) | ||
a1′ = permutedims(a1, perm1) | ||
a2′ = permutedims(a2, perm2) | ||
labels1′ = map(i -> labels1[i], perm1) | ||
labels2′ = map(i -> labels2[i], perm2) | ||
a_dest, labels_dest = TensorAlgebra.contract(a1′, labels1′, a2′, labels2′) | ||
@test labels_dest == symdiff(labels1′, labels2′) | ||
a_dest_tensoroperations = TensorOperations.tensorcontract( | ||
labels_dest, a1′, labels1′, a2′, labels2′ | ||
) | ||
@test a_dest ≈ a_dest_tensoroperations | ||
end | ||
end | ||
end | ||
@testset "contract broken" begin | ||
a1 = randn(3, 5, 8) | ||
a2 = randn(8, 2, 4) | ||
labels_dest = (:a, :b, :c, :d) | ||
labels1 = (:c, :a, :x) | ||
labels2 = (:x, :d, :b) | ||
@test_broken a′ = TensorAlgebra.contract(labels_dest, a1, labels1, a2, labels2) | ||
end | ||
@testset "qr" begin | ||
a = randn(5, 4, 3, 2) | ||
labels_a = (:a, :b, :c, :d) | ||
labels_q = (:b, :a) | ||
labels_r = (:d, :c) | ||
q, r = qr(a, labels_a, labels_q, labels_r) | ||
label_qr = :qr | ||
a′ = TensorAlgebra.contract( | ||
labels_a, q, (labels_q..., label_qr), r, (label_qr, labels_r...) | ||
) | ||
@test a ≈ a′ | ||
end | ||
end |