changeset 417:4c6604b7d990 feature/tensor_composition

Add dimension checking in the constructor
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 16 Oct 2020 20:32:09 +0200
parents ebc9b2383dae
children 264af2bb646f
files src/LazyTensors/lazy_tensor_operations.jl test/testLazyTensors.jl
diffstat 2 files changed, 10 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/LazyTensors/lazy_tensor_operations.jl	Fri Oct 16 09:37:35 2020 +0200
+++ b/src/LazyTensors/lazy_tensor_operations.jl	Fri Oct 16 20:32:09 2020 +0200
@@ -83,6 +83,12 @@
     t1::TM1
     t2::TM2
 
+    @inline function TensorMappingComposition(t1::TensorMapping{T,R,K}, t2::TensorMapping{T,K,D}) where {T,R,K,D}
+        @boundscheck if domain_size(t1) != range_size(t2)
+            throw(DimensionMismatch("The first argument has domain size $(domain_size(t1)) while the second has range size $(range_size(t2)) "))
+        end
+        return new{T,R,K,D, typeof(t1), typeof(t2)}(t1,t2)
+    end
     # Add check for matching sizes as a boundscheck
 end
 export TensorMappingComposition
@@ -98,7 +104,7 @@
     apply_transpose(c.t2, LazyTensorMappingApplication(c.t1',v), I...)
 end
 
-Base.:∘(s::TensorMapping, t::TensorMapping) = TensorMappingComposition(s,t)
+Base.@propagate_inbounds Base.:∘(s::TensorMapping, t::TensorMapping) = TensorMappingComposition(s,t)
 
 """
     LazyLinearMap{T,R,D,...}(A, range_indicies, domain_indicies)
--- a/test/testLazyTensors.jl	Fri Oct 16 09:37:35 2020 +0200
+++ b/test/testLazyTensors.jl	Fri Oct 16 20:32:09 2020 +0200
@@ -223,7 +223,9 @@
     @test Ã∘B̃ isa TensorMappingComposition
     @test range_size(Ã∘B̃) == (2,)
     @test domain_size(Ã∘B̃) == (4,)
-    # @test_throws DimensionMismatch B̃∘Ã
+    @test_throws DimensionMismatch B̃∘Ã
+
+    # @test @inbounds B̃∘Ã # Should not error even though dimensions don't match. (Since ]test runs with forced boundschecking this is currently not testable 2020-10-16)
 
     v = rand(4)
     @test Ã∘B̃*v ≈ A*B*v