Mercurial > repos > public > sbplib_julia
changeset 377:8414c2334393 feature/lazy_linear_map
Start implementing LazyLinearMap
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 30 Sep 2020 21:15:42 +0200 |
parents | f65809a26a17 |
children | 418cfd945715 |
files | src/LazyTensors/lazy_tensor_operations.jl test/testLazyTensors.jl |
diffstat | 2 files changed, 45 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/LazyTensors/lazy_tensor_operations.jl Wed Sep 30 21:09:35 2020 +0200 +++ b/src/LazyTensors/lazy_tensor_operations.jl Wed Sep 30 21:15:42 2020 +0200 @@ -100,3 +100,30 @@ # # Have i gone too crazy with the type parameters? Maybe they aren't all needed? # export → +""" + LazyLinearMap{T,R,D,...}(A, range_indicies, ) + +TensorMapping defined by the AbstractArray A. `range_indicies` and `domain_indicies` define which indicies of A should +be considerd the range and domain of the TensorMapping. +""" +struct LazyLinearMap{T,R,D, RD, AA<:AbstractArray{T,RD}} <: TensorMapping{T,R,D} + A::AA + range_indicies::NTuple{R,Int} + domain_indicies::NTuple{D,Int} +end +export LazyLinearMap + +range_size(llm::LazyLinearMap) = size(llm.A)[llm.range_indicies...] +domain_size(llm::LazyLinearMap) = size(llm.A)[llm.domain_indicies...] + +function apply(llm::LazyLinearMap{T,R,D}, v::AbstractArray{T,D}, I::Vararg{Index,R}) where {T,R,D} + view_index = ntuple(i->:,ndims(llm.A)) + for i ∈ 1:R + view_index = Base.setindex(view_index, Int(I[i]), llm.range_indicies[i]) + end + + A_view = @view llm.A[view_index...] + + return sum(A_view.*v) +end +
--- a/test/testLazyTensors.jl Wed Sep 30 21:09:35 2020 +0200 +++ b/test/testLazyTensors.jl Wed Sep 30 21:15:42 2020 +0200 @@ -193,6 +193,9 @@ @test_throws DimensionMismatch v1 + v2 end +end + + @testset "LazyFunctionArray" begin @test LazyFunctionArray(i->i^2, (3,)) == [1,4,9] @test LazyFunctionArray((i,j)->i*j, (3,2)) == [ @@ -212,4 +215,19 @@ end +@testset "LazyLinearMap" begin + A = rand(3,4) + B = rand(3,4,2) + v = rand(4) + + @test LazyLinearMap(A, (1,), (2,)) isa LazyLinearMap{T,1,1} where T + @test LazyLinearMap(A, (1,), (2,)) isa TensorMapping{T,1,1} where T + @test LazyLinearMap(B, (1,2), (3,)) isa TensorMapping{T,2,1} where T + @test LazyLinearMap(B, (2), (3,1)) isa TensorMapping{T,1,2} where T + + + @test LazyLinearMap(A, (1,), (2,))*ones(4) == A*ones(4) + @test LazyLinearMap(A, (1,), (2,))*v == A*v end + +end