changeset 526:be152486d136 feature/inflated_tensormapping_transpose

Implement apply_transpose with tests
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 25 Nov 2020 15:59:04 +0100
parents 7e6250c51eb2
children f9bc746f37da
files src/LazyTensors/lazy_tensor_operations.jl test/testLazyTensors.jl
diffstat 2 files changed, 38 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/LazyTensors/lazy_tensor_operations.jl	Wed Nov 25 15:24:55 2020 +0100
+++ b/src/LazyTensors/lazy_tensor_operations.jl	Wed Nov 25 15:59:04 2020 +0100
@@ -272,6 +272,18 @@
     return apply(itm.tm, v_inner, inner_index...)
 end
 
+function apply_transpose(itm::InflatedTensorMapping{T,R,D}, v::AbstractArray{T,R}, I::Vararg{Any,D}) where {T,R,D}
+    A = range_dim(itm.before)
+    B_domain = domain_dim(itm.tm)
+    B_range = range_dim(itm.tm)
+    C = range_dim(itm.after)
+
+    view_index, inner_index = split_index(Val(A), Val(B_range), Val(B_domain), Val(C), I...)
+
+    v_inner = view(v, view_index...)
+    return apply_transpose(itm.tm, v_inner, inner_index...)
+end
+
 
 """
     split_index(:Val{A}, ::Val{B_view}, ::Val{B_middle}, ::Val{C}, I...)
--- a/test/testLazyTensors.jl	Wed Nov 25 15:24:55 2020 +0100
+++ b/test/testLazyTensors.jl	Wed Nov 25 15:59:04 2020 +0100
@@ -367,47 +367,67 @@
         tests = [
             (
                 InflatedTensorMapping(I(3,2), A, I(4)),
-                (v-> @tullio res[a,b,c,d] := Ã[c,i]*v[a,b,i,d]),
+                (v-> @tullio res[a,b,c,d] := Ã[c,i]*v[a,b,i,d]), # Expected result of apply
+                (v-> @tullio res[a,b,c,d] := Ã[i,c]*v[a,b,i,d]), # Expected result of apply_transpose
             ),
             (
                 InflatedTensorMapping(I(3,2), B, I(4)),
                 (v-> @tullio res[a,b,c,d,e] := B̃[c,d,i]*v[a,b,i,e]),
+                (v-> @tullio res[a,b,c,d] := B̃[i,j,c]*v[a,b,i,j,d]),
             ),
             (
                 InflatedTensorMapping(I(3,2), C, I(4)),
                 (v-> @tullio res[a,b,c,d] := C̃[c,i,j]*v[a,b,i,j,d]),
+                (v-> @tullio res[a,b,c,d,e] := C̃[i,c,d]*v[a,b,i,e]),
             ),
             (
                 InflatedTensorMapping(I(3,2), A),
                 (v-> @tullio res[a,b,c] := Ã[c,i]*v[a,b,i]),
+                (v-> @tullio res[a,b,c] := Ã[i,c]*v[a,b,i]),
             ),
             (
                 InflatedTensorMapping(I(3,2), B),
                 (v-> @tullio res[a,b,c,d] := B̃[c,d,i]*v[a,b,i]),
+                (v-> @tullio res[a,b,c] := B̃[i,j,c]*v[a,b,i,j]),
             ),
             (
                 InflatedTensorMapping(I(3,2), C),
                 (v-> @tullio res[a,b,c] := C̃[c,i,j]*v[a,b,i,j]),
+                (v-> @tullio res[a,b,c,d] := C̃[i,c,d]*v[a,b,i]),
             ),
             (
                 InflatedTensorMapping(A,I(4)),
                 (v-> @tullio res[a,b] := Ã[a,i]*v[i,b]),
+                (v-> @tullio res[a,b] := Ã[i,a]*v[i,b]),
             ),
             (
                 InflatedTensorMapping(B,I(4)),
                 (v-> @tullio res[a,b,c] := B̃[a,b,i]*v[i,c]),
+                (v-> @tullio res[a,b] := B̃[i,j,a]*v[i,j,b]),
             ),
             (
                 InflatedTensorMapping(C,I(4)),
                 (v-> @tullio res[a,b] := C̃[a,i,j]*v[i,j,b]),
+                (v-> @tullio res[a,b,c] := C̃[i,a,b]*v[i,c]),
             ),
         ]
 
-        for i ∈ 1:length(tests)
-            tm = tests[i][1]
-            v = rand(domain_size(tm)...)
-            true_value = tests[i][2](v)
-            @test tm*v ≈ true_value rtol=1e-14
+        @testset "apply" begin
+            for i ∈ 1:length(tests)
+                tm = tests[i][1]
+                v = rand(domain_size(tm)...)
+                true_value = tests[i][2](v)
+                @test tm*v ≈ true_value rtol=1e-14
+            end
+        end
+
+        @testset "apply_transpose" begin
+            for i ∈ 1:length(tests)
+                tm = tests[i][1]
+                v = rand(range_size(tm)...)
+                true_value = tests[i][3](v)
+                @test tm'*v ≈ true_value rtol=1e-14
+            end
         end
 
         @testset "Inference of application" begin