changeset 257:d4cd4882ee9f boundary_conditions

Improve error messages when multiblying with TensorMappings and add some tests for TensorOperators
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 28 Jun 2019 14:10:35 +0200
parents d8f42733f392
children 3ea8c60ccef3
files LazyTensors/src/lazy_operations.jl LazyTensors/test/runtests.jl
diffstat 2 files changed, 17 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/LazyTensors/src/lazy_operations.jl	Fri Jun 28 14:08:30 2019 +0200
+++ b/LazyTensors/src/lazy_operations.jl	Fri Jun 28 14:10:35 2019 +0200
@@ -32,7 +32,7 @@
 # TODO: What else is needed to implement the AbstractArray interface?
 
 # # We need the associativity to be a→b→c = a→(b→c), which is the case for '→'
-Base.:*(args::Union{TensorMapping{T}, AbstractArray{T}}...) where T = foldr(*,args)
+Base.:*(a::TensorMapping{T,R,D}, b::TensorMapping{T,D,K}, args::Union{TensorMapping{T}, AbstractArray{T}}...) where {T,R,D,K} = foldr(*,(a,b,args...))
 # # Should we overload some other infix binary operator?
 # →(tm::TensorMapping{T,R,D}, o::AbstractArray{T,D}) where {T,R,D} = LazyTensorMappingApplication(tm,o)
 # TODO: We need to be really careful about good error messages.
--- a/LazyTensors/test/runtests.jl	Fri Jun 28 14:08:30 2019 +0200
+++ b/LazyTensors/test/runtests.jl	Fri Jun 28 14:10:35 2019 +0200
@@ -56,6 +56,22 @@
     @test (m*m*v)[6] == (:apply,m*v,(6,))
     @test_broken BoundsError == (m*m*v)[0]
     @test_broken BoundsError == (m*m*v)[7]
+
+    A = DummyMapping{Int, 2, 1}()
+
+    @test_throws MethodError A*ones(Int,2,2)
+    @test_throws MethodError A*A*v
+
+    struct ScalingOperator{T,D} <: TensorOperator{T,D}
+        λ::T
+    end
+
+    LazyTensors.apply(m::ScalingOperator{T,D}, v, I::Tuple{Int}) where {T,D} = m.λ*v[I...]
+
+    A = ScalingOperator{Int,1}(2)
+
+    @test A*[1,2,3] isa AbstractVector
+    @test A*[1,2,3] == [2,4,6]
 end
 
 @testset "TensorMapping binary operations" begin