comparison src/LazyTensors/lazy_tensor_operations.jl @ 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 ba46a952a450
children 418cfd945715
comparison
equal deleted inserted replaced
376:f65809a26a17 377:8414c2334393
98 # end 98 # end
99 99
100 # # Have i gone too crazy with the type parameters? Maybe they aren't all needed? 100 # # Have i gone too crazy with the type parameters? Maybe they aren't all needed?
101 101
102 # export → 102 # export →
103 """
104 LazyLinearMap{T,R,D,...}(A, range_indicies, )
105
106 TensorMapping defined by the AbstractArray A. `range_indicies` and `domain_indicies` define which indicies of A should
107 be considerd the range and domain of the TensorMapping.
108 """
109 struct LazyLinearMap{T,R,D, RD, AA<:AbstractArray{T,RD}} <: TensorMapping{T,R,D}
110 A::AA
111 range_indicies::NTuple{R,Int}
112 domain_indicies::NTuple{D,Int}
113 end
114 export LazyLinearMap
115
116 range_size(llm::LazyLinearMap) = size(llm.A)[llm.range_indicies...]
117 domain_size(llm::LazyLinearMap) = size(llm.A)[llm.domain_indicies...]
118
119 function apply(llm::LazyLinearMap{T,R,D}, v::AbstractArray{T,D}, I::Vararg{Index,R}) where {T,R,D}
120 view_index = ntuple(i->:,ndims(llm.A))
121 for i ∈ 1:R
122 view_index = Base.setindex(view_index, Int(I[i]), llm.range_indicies[i])
123 end
124
125 A_view = @view llm.A[view_index...]
126
127 return sum(A_view.*v)
128 end
129