changeset 1084:2e606d4c0ab1 feature/scalar_times_tensor

Add support for multiplying a LazyTensor with a scalar
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Thu, 14 Apr 2022 18:12:59 +0200
parents 9abb140a4636
children d5a82a142d6a
files src/LazyTensors/lazy_tensor_operations.jl test/LazyTensors/lazy_tensor_operations_test.jl
diffstat 2 files changed, 10 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/LazyTensors/lazy_tensor_operations.jl	Fri Apr 08 19:36:22 2022 +0200
+++ b/src/LazyTensors/lazy_tensor_operations.jl	Thu Apr 14 18:12:59 2022 +0200
@@ -98,7 +98,6 @@
     apply_transpose(c.t2, c.t1'*v, I...)
 end
 
-
 """
     TensorComposition(tm, tmi::IdentityTensor)
     TensorComposition(tmi::IdentityTensor, tm)
@@ -120,6 +119,8 @@
     return tmi
 end
 
+Base.:*(a::T, tm::LazyTensor{T}) where T = TensorComposition(ScalingTensor{T,range_dim(tm)}(a,range_size(tm)), tm)
+Base.:*(tm::LazyTensor{T}, a::T) where T = a*tm
 
 """
     InflatedTensor{T,R,D} <: LazyTensor{T,R,D}
--- a/test/LazyTensors/lazy_tensor_operations_test.jl	Fri Apr 08 19:36:22 2022 +0200
+++ b/test/LazyTensors/lazy_tensor_operations_test.jl	Thu Apr 14 18:12:59 2022 +0200
@@ -181,6 +181,14 @@
 
     @test (Ã∘B̃*ComplexF64[1.,2.,3.,4.])[1] isa ComplexF64
     @test ((Ã∘B̃)'*ComplexF64[1.,2.])[1] isa ComplexF64
+
+    a = 2.
+    v = rand(3)
+    @test a*Ã isa TensorComposition
+    @test a*Ã == Ã*a
+    @test range_size(a*Ã) == range_size(Ã)
+    @test domain_size(a*Ã) == domain_size(Ã)
+    @test a*Ã*v == a.*A*v
 end