Mercurial > repos > public > sbplib_julia
changeset 470:0c3decc04649 feature/outer_product
Add some tests and fix some bugs
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Sun, 25 Oct 2020 14:01:27 +0100 |
parents | 481e86e77c22 |
children | 3ad327378b2d |
files | src/LazyTensors/lazy_tensor_operations.jl test/testLazyTensors.jl |
diffstat | 2 files changed, 36 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/src/LazyTensors/lazy_tensor_operations.jl Sat Oct 24 20:40:28 2020 +0200 +++ b/src/LazyTensors/lazy_tensor_operations.jl Sun Oct 25 14:01:27 2020 +0100 @@ -317,17 +317,16 @@ function LazyOuterProduct end export LazyOuterProduct -function LazyOuterProduct(tm1::TensorMapping, tm2::TensorMapping) - itm1 = InflatedTensorMapping(tm1, IdentityMapping(range_size(tm2))) - itm2 = InflatedTensorMapping(IdentityMapping(domain_size(tm1)),tm2) +function LazyOuterProduct(tm1::TensorMapping{T}, tm2::TensorMapping{T}) where T + itm1 = InflatedTensorMapping(tm1, IdentityMapping{T}(range_size(tm2))) + itm2 = InflatedTensorMapping(IdentityMapping{T}(domain_size(tm1)),tm2) return itm1∘itm2 end -# length(tms) is always >= 1 since the two argument method is more specific. Right?? -LazyOuterProduct(tm::TensorMapping, tms::Vararg{TensorMapping}) = tm∘LazyOuterProduct(tms...) +LazyOuterProduct(tms::Vararg{TensorMapping}) = foldl(LazyOuterProduct, tms) -⊗(a::TensorMapping,b::TensorMapping) = LazyOuterProduct(a,b) -⊗(a,b,cs::Vararg{TensorMapping}) = ⊗(a⊗b, cs...) +⊗(tms::Vararg{TensorMapping}) = LazyOuterProduct(tms...) +export ⊗ # TODO: Can we implement compositions and kroneckers of LazyIdentities to just return new LazyIdentities?
--- a/test/testLazyTensors.jl Sat Oct 24 20:40:28 2020 +0200 +++ b/test/testLazyTensors.jl Sun Oct 25 14:01:27 2020 +0100 @@ -387,7 +387,37 @@ @testset "LazyIdentityOuterProduct" begin + struct ScalingOperator{T,D} <: TensorMapping{T,D,D} + λ::T + size::NTuple{D,Int} + end + LazyTensors.apply(m::ScalingOperator{T,D}, v, I::Vararg{Index,D}) where {T,D} = m.λ*v[I] + LazyTensors.range_size(m::ScalingOperator) = m.size + LazyTensors.domain_size(m::ScalingOperator) = m.size + + A = ScalingOperator(2.0, (5,)) + B = ScalingOperator(3.0, (3,)) + C = ScalingOperator(5.0, (3,2)) + + AB = LazyOuterProduct(A,B) + @test AB isa TensorMapping{T,2,2} where T + @test range_size(AB) == (5,3) + @test domain_size(AB) == (5,3) + + v = rand(range_size(AB)...) + @test AB*v == 6*v + + ABC = LazyOuterProduct(A,B,C) + + @test ABC isa TensorMapping{T,4,4} where T + @test range_size(ABC) == (5,3,3,2) + @test domain_size(ABC) == (5,3,3,2) + + @test A⊗B == AB + @test A⊗B⊗C == ABC + + # TODO: Include some tests where the domain has different size and dimension end