diff test/testLazyTensors.jl @ 403:618b7ee73b25 refactor/sbp_operators_tests/collect_and_compare

Merge in default and close branch before merge
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 07 Oct 2020 12:31:54 +0200
parents 3b4b1758a8ad
children 4aa59af074ef d94891b8dfca
line wrap: on
line diff
--- a/test/testLazyTensors.jl	Sun Oct 04 19:41:14 2020 +0200
+++ b/test/testLazyTensors.jl	Wed Oct 07 12:31:54 2020 +0200
@@ -118,15 +118,15 @@
 end
 
 @testset "LazyArray" begin
-	@testset "LazyConstantArray" begin
-	    @test LazyTensors.LazyConstantArray(3,(3,2)) isa LazyArray{Int,2}
+    @testset "LazyConstantArray" begin
+        @test LazyTensors.LazyConstantArray(3,(3,2)) isa LazyArray{Int,2}
 
-	    lca = LazyTensors.LazyConstantArray(3.0,(3,2))
-	    @test eltype(lca) == Float64
-	    @test ndims(lca) == 2
-	    @test size(lca) == (3,2)
-	    @test lca[2] == 3.0
-	end
+        lca = LazyTensors.LazyConstantArray(3.0,(3,2))
+        @test eltype(lca) == Float64
+        @test ndims(lca) == 2
+        @test size(lca) == (3,2)
+        @test lca[2] == 3.0
+    end
     struct DummyArray{T,D, T1<:AbstractArray{T,D}} <: LazyArray{T,D}
         data::T1
     end
@@ -193,6 +193,7 @@
     @test_throws DimensionMismatch v1 + v2
 end
 
+
 @testset "LazyFunctionArray" begin
     @test LazyFunctionArray(i->i^2, (3,)) == [1,4,9]
     @test LazyFunctionArray((i,j)->i*j, (3,2)) == [
@@ -212,4 +213,48 @@
 
 end
 
+@testset "LazyLinearMap" begin
+    # Test a standard matrix-vector product
+    # mapping vectors of size 4 to vectors of size 3.
+    A = rand(3,4)
+    Ã = LazyLinearMap(A, (1,), (2,))
+    v = rand(4)
+
+    @test à isa LazyLinearMap{T,1,1} where T
+    @test à isa TensorMapping{T,1,1} where T
+    @test range_size(Ã) == (3,)
+    @test domain_size(Ã) == (4,)
+
+    @test Ã*ones(4) ≈ A*ones(4) atol=5e-13
+    @test Ã*v ≈ A*v atol=5e-13
+
+    A = rand(2,3,4)
+    @test_throws DomainError LazyLinearMap(A, (3,1), (2,))
+
+    # Test more exotic mappings
+    B = rand(3,4,2)
+    # Map vectors of size 2 to matrices of size (3,4)
+    B̃ = LazyLinearMap(B, (1,2), (3,))
+    v = rand(2)
+
+    @test range_size(B̃) == (3,4)
+    @test domain_size(B̃) == (2,)
+    @test B̃ isa TensorMapping{T,2,1} where T
+    @test B̃*ones(2) ≈ B[:,:,1] + B[:,:,2] atol=5e-13
+    @test B̃*v ≈ B[:,:,1]*v[1] + B[:,:,2]*v[2] atol=5e-13
+
+    # Map matrices of size (3,2) to vectors of size 4
+    B̃ = LazyLinearMap(B, (2,), (1,3))
+    v = rand(3,2)
+
+    @test range_size(B̃) == (4,)
+    @test domain_size(B̃) == (3,2)
+    @test B̃ isa TensorMapping{T,1,2} where T
+    @test B̃*ones(3,2) ≈ B[1,:,1] + B[2,:,1] + B[3,:,1] +
+                        B[1,:,2] + B[2,:,2] + B[3,:,2] atol=5e-13
+    @test B̃*v ≈ B[1,:,1]*v[1,1] + B[2,:,1]*v[2,1] + B[3,:,1]*v[3,1] +
+                B[1,:,2]v[1,2] + B[2,:,2]*v[2,2] + B[3,:,2]*v[3,2] atol=5e-13
+
 end
+
+end