comparison test/LazyTensors/lazy_tensor_operations_test.jl @ 960:e79debd10f7d feature/variable_derivatives

Merge feature/tensormapping_application_promotion
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 14 Mar 2022 10:14:38 +0100
parents 86889fc5b63f
children 043d13ef8898
comparison
equal deleted inserted replaced
959:e9752c1e92f8 960:e79debd10f7d
78 78
79 m = ScalingOperator{Int,2}(2,(2,2)) 79 m = ScalingOperator{Int,2}(2,(2,2))
80 v = [[1 2];[3 4]] 80 v = [[1 2];[3 4]]
81 @test m*v == [[2 4];[6 8]] 81 @test m*v == [[2 4];[6 8]]
82 @test (m*v)[2,1] == 6 82 @test (m*v)[2,1] == 6
83
84 @testset "Promotion" begin
85 m = ScalingOperator{Int,1}(2,(3,))
86 v = [1.,2.,3.]
87 @test m*v isa AbstractVector{Float64}
88 @test m*v == [2.,4.,6.]
89
90 m = ScalingOperator{Int,2}(2,(2,2))
91 v = [[1. 2.];[3. 4.]]
92 @test m*v == [[2. 4.];[6. 8.]]
93 @test (m*v)[2,1] == 6.
94
95 m = ScalingOperator{ComplexF64,1}(2. +2. *im,(3,))
96 v = [1.,2.,3.]
97 @test m*v isa AbstractVector{ComplexF64}
98 @test m*v == [2. + 2. *im, 4. + 4. *im, 6. + 6. *im]
99
100 m = ScalingOperator{ComplexF64,1}(1,(3,))
101 v = [2. + 2. *im, 4. + 4. *im, 6. + 6. *im]
102 @test m*v isa AbstractVector{ComplexF64}
103 @test m*v == [2. + 2. *im, 4. + 4. *im, 6. + 6. *im]
104 end
83 end 105 end
84 106
85 @testset "TensorMapping binary operations" begin 107 @testset "TensorMapping binary operations" begin
86 struct ScalarMapping{T,R,D} <: TensorMapping{T,R,D} 108 struct ScalarMapping{T,R,D} <: TensorMapping{T,R,D}
87 λ::T 109 λ::T
105 @test ((A-B)*v)[i] == 2*v[i] - 3*v[i] 127 @test ((A-B)*v)[i] == 2*v[i] - 3*v[i]
106 end 128 end
107 129
108 @test range_size(A+B) == range_size(A) == range_size(B) 130 @test range_size(A+B) == range_size(A) == range_size(B)
109 @test domain_size(A+B) == domain_size(A) == domain_size(B) 131 @test domain_size(A+B) == domain_size(A) == domain_size(B)
132
133 @test ((A+B)*ComplexF64[1.1,1.2,1.3])[3] isa ComplexF64
110 end 134 end
111 135
112 136
113 @testset "TensorMappingComposition" begin 137 @testset "TensorMappingComposition" begin
114 A = rand(2,3) 138 A = rand(2,3)
127 v = rand(4) 151 v = rand(4)
128 @test Ã∘B̃*v ≈ A*B*v rtol=1e-14 152 @test Ã∘B̃*v ≈ A*B*v rtol=1e-14
129 153
130 v = rand(2) 154 v = rand(2)
131 @test (Ã∘B̃)'*v ≈ B'*A'*v rtol=1e-14 155 @test (Ã∘B̃)'*v ≈ B'*A'*v rtol=1e-14
156
157 @test (Ã∘B̃*ComplexF64[1.,2.,3.,4.])[1] isa ComplexF64
158 @test ((Ã∘B̃)'*ComplexF64[1.,2.])[1] isa ComplexF64
132 end 159 end
133 160
134 @testset "LazyLinearMap" begin 161 @testset "LazyLinearMap" begin
135 # Test a standard matrix-vector product 162 # Test a standard matrix-vector product
136 # mapping vectors of size 4 to vectors of size 3. 163 # mapping vectors of size 4 to vectors of size 3.
189 @test IdentityMapping(3,2) isa IdentityMapping{Float64,2} 216 @test IdentityMapping(3,2) isa IdentityMapping{Float64,2}
190 217
191 for sz ∈ [(4,5),(3,),(5,6,4)] 218 for sz ∈ [(4,5),(3,),(5,6,4)]
192 I = IdentityMapping{Float64}(sz) 219 I = IdentityMapping{Float64}(sz)
193 v = rand(sz...) 220 v = rand(sz...)
221 @test I*v == v
222 @test I'*v == v
223
224 v = rand(ComplexF64,sz...)
194 @test I*v == v 225 @test I*v == v
195 @test I'*v == v 226 @test I'*v == v
196 227
197 @test range_size(I) == sz 228 @test range_size(I) == sz
198 @test domain_size(I) == sz 229 @test domain_size(I) == sz
320 true_value = tests[i][3](v) 351 true_value = tests[i][3](v)
321 @test tm'*v ≈ true_value rtol=1e-14 352 @test tm'*v ≈ true_value rtol=1e-14
322 end 353 end
323 end 354 end
324 355
356 @testset "application to other type" begin
357 tm = InflatedTensorMapping(I(3,2), A, I(4))
358
359 v = rand(ComplexF64, domain_size(tm)...)
360 @test (tm*v)[1,2,3,1] isa ComplexF64
361
362 v = rand(ComplexF64, domain_size(tm')...)
363 @test (tm'*v)[1,2,2,1] isa ComplexF64
364 end
365
325 @testset "Inference of application" begin 366 @testset "Inference of application" begin
326 struct ScalingOperator{T,D} <: TensorMapping{T,D,D} 367 struct ScalingOperator{T,D} <: TensorMapping{T,D,D}
327 λ::T 368 λ::T
328 size::NTuple{D,Int} 369 size::NTuple{D,Int}
329 end 370 end