changeset 977:043d13ef8898 feature/tensormapping_application_promotion

Fix type calculation of LazyTensorMappingApplication element type
author Jonatan Werpers <jonatan@werpers.com>
date Tue, 15 Mar 2022 21:03:17 +0100
parents 089a1411dfc0
children df562695b1b5 3bceb4031753
files src/LazyTensors/lazy_tensor_operations.jl test/LazyTensors/lazy_tensor_operations_test.jl
diffstat 2 files changed, 18 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/LazyTensors/lazy_tensor_operations.jl	Tue Mar 15 07:40:13 2022 +0100
+++ b/src/LazyTensors/lazy_tensor_operations.jl	Tue Mar 15 21:03:17 2022 +0100
@@ -12,7 +12,8 @@
     o::AA
 
     function LazyTensorMappingApplication(t::TensorMapping{<:Any,R,D}, o::AbstractArray{<:Any,D}) where {R,D}
-        T = promote_type(eltype(t), eltype(o))
+        I = ntuple(i->1, range_dim(t))
+        T = typeof(apply(t,o,I...))
         return new{T,R,D,typeof(t), typeof(o)}(t,o)
     end
 end
--- a/test/LazyTensors/lazy_tensor_operations_test.jl	Tue Mar 15 07:40:13 2022 +0100
+++ b/test/LazyTensors/lazy_tensor_operations_test.jl	Tue Mar 15 21:03:17 2022 +0100
@@ -36,10 +36,8 @@
 
     m = SizeDoublingMapping{Int, 1, 1}((3,))
     v = [0,1,2]
-    @test m*v isa AbstractVector{Int}
     @test size(m*v) == 2 .*size(v)
     @test (m*v)[0] == (:apply,v,(0,))
-    @test m*m*v isa AbstractVector{Int}
     @test (m*m*v)[1] == (:apply,m*v,(1,))
     @test (m*m*v)[3] == (:apply,m*v,(3,))
     @test (m*m*v)[6] == (:apply,m*v,(6,))
@@ -81,26 +79,41 @@
     @test m*v == [[2 4];[6 8]]
     @test (m*v)[2,1] == 6
 
-    @testset "Promotion" begin
+    @testset "Type calculation" begin
         m = ScalingOperator{Int,1}(2,(3,))
         v = [1.,2.,3.]
         @test m*v isa AbstractVector{Float64}
         @test m*v == [2.,4.,6.]
+        @inferred m*v
+        @inferred (m*v)[1]
 
         m = ScalingOperator{Int,2}(2,(2,2))
         v = [[1. 2.];[3. 4.]]
         @test m*v == [[2. 4.];[6. 8.]]
         @test (m*v)[2,1] == 6.
+        @inferred m*v
+        @inferred (m*v)[1]
 
         m = ScalingOperator{ComplexF64,1}(2. +2. *im,(3,))
         v = [1.,2.,3.]
         @test m*v isa AbstractVector{ComplexF64}
         @test m*v == [2. + 2. *im, 4. + 4. *im, 6. + 6. *im]
+        @inferred m*v
+        @inferred (m*v)[1]
 
         m = ScalingOperator{ComplexF64,1}(1,(3,))
         v = [2. + 2. *im, 4. + 4. *im, 6. + 6. *im]
         @test m*v isa AbstractVector{ComplexF64}
         @test m*v == [2. + 2. *im, 4. + 4. *im, 6. + 6. *im]
+        @inferred m*v
+        @inferred (m*v)[1]
+
+        m = ScalingOperator{Float64,1}(2., (3,))
+        v = [[1,2,3], [3,2,1],[1,3,1]]
+        @test m*v isa AbstractVector{Vector{Float64}}
+        @test m*v == [[2.,4.,6.], [6.,4.,2.],[2.,6.,2.]]
+        @inferred m*v
+        @inferred (m*v)[1]
     end
 end