changeset 433:7327a3e41df0 feature/lazy_identity

Merge in default
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 19 Oct 2020 09:50:16 +0200
parents f24daf573bc6 (diff) 51870d80fbb9 (current diff)
children 648a36ebac99
files src/LazyTensors/lazy_tensor_operations.jl test/testLazyTensors.jl
diffstat 2 files changed, 43 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/LazyTensors/lazy_tensor_operations.jl	Mon Oct 19 09:47:33 2020 +0200
+++ b/src/LazyTensors/lazy_tensor_operations.jl	Mon Oct 19 09:50:16 2020 +0200
@@ -145,3 +145,24 @@
 function apply_transpose(llm::LazyLinearMap{T,R,D}, v::AbstractArray{T,R}, I::Vararg{Index,D}) where {T,R,D}
     apply(LazyLinearMap(llm.A, llm.domain_indicies, llm.range_indicies), v, I...)
 end
+
+
+"""
+    LazyIdentity{T,D} <: TensorMapping{T,D,D}
+
+The lazy identity TensorMapping for a given size. Usefull for building up higher dimensional tensor mappings from lower
+dimensional ones through outer products. Also used in the Implementation for InflatedTensorMapping.
+"""
+struct LazyIdentity{T,D} <: TensorMapping{T,D,D}
+    size::NTuple{D,Int}
+end
+export LazyIdentity
+
+LazyIdentity{T}(size::NTuple{D,Int}) where {T,D} = LazyIdentity{T,D}(size)
+
+range_size(tmi::LazyIdentity) = tmi.size
+domain_size(tmi::LazyIdentity) = tmi.size
+
+apply(tmi::LazyIdentity{T,D}, v::AbstractArray{T,D}, I::Vararg{Any,D}) where {T,D} = v[I...]
+apply_transpose(tmi::LazyIdentity{T,D}, v::AbstractArray{T,D}, I::Vararg{Any,D}) where {T,D} = v[I...]
+
--- a/test/testLazyTensors.jl	Mon Oct 19 09:47:33 2020 +0200
+++ b/test/testLazyTensors.jl	Mon Oct 19 09:50:16 2020 +0200
@@ -280,4 +280,26 @@
 
 end
 
+
+@testset "LazyIdentity" begin
+    @test LazyIdentity{Float64}((4,5)) isa LazyIdentity{T,2} where T
+    @test LazyIdentity{Float64}((4,5)) isa TensorMapping{T,2,2} where T
+
+    for sz ∈ [(4,5),(3,),(5,6,4)]
+        I = LazyIdentity{Float64}(sz)
+        v = rand(sz...)
+        @test I*v == v
+        @test I'*v == v
+
+        @test range_size(I) == sz
+        @test domain_size(I) == sz
+    end
+
+    I = LazyIdentity{Float64}((4,5))
+    v = rand(4,5)
+    @inferred (I*v)[3,2]
+    @test_broken @inferred (I'*v)[3,2] # TODO: Should fix the index typing before investigating this
+    @inferred range_size(I)
 end
+
+end