Mercurial > repos > public > sbplib_julia
comparison src/LazyTensors/lazy_tensor_operations.jl @ 545:ff412b29db31 feature/quadrature_as_outer_product
Merge with default
author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
---|---|
date | Thu, 26 Nov 2020 21:56:33 +0100 |
parents | 848dec405332 |
children | 62d96e2cd165 |
comparison
equal
deleted
inserted
replaced
507:576c6d1acc28 | 545:ff412b29db31 |
---|---|
238 InflatedTensorMapping(before::IdentityMapping, tm::TensorMapping{T}) where T = InflatedTensorMapping(before,tm,IdentityMapping{T}()) | 238 InflatedTensorMapping(before::IdentityMapping, tm::TensorMapping{T}) where T = InflatedTensorMapping(before,tm,IdentityMapping{T}()) |
239 InflatedTensorMapping(tm::TensorMapping{T}, after::IdentityMapping) where T = InflatedTensorMapping(IdentityMapping{T}(),tm,after) | 239 InflatedTensorMapping(tm::TensorMapping{T}, after::IdentityMapping) where T = InflatedTensorMapping(IdentityMapping{T}(),tm,after) |
240 # Resolve ambiguity between the two previous methods | 240 # Resolve ambiguity between the two previous methods |
241 InflatedTensorMapping(I1::IdentityMapping{T}, I2::IdentityMapping{T}) where T = InflatedTensorMapping(I1,I2,IdentityMapping{T}()) | 241 InflatedTensorMapping(I1::IdentityMapping{T}, I2::IdentityMapping{T}) where T = InflatedTensorMapping(I1,I2,IdentityMapping{T}()) |
242 | 242 |
243 # TODO: Implement syntax and constructors for products of different combinations of InflatedTensorMapping and IdentityMapping | |
244 | |
245 # TODO: Implement some pretty printing in terms of ⊗. E.g InflatedTensorMapping(I(3),B,I(2)) -> I(3)⊗B⊗I(2) | 243 # TODO: Implement some pretty printing in terms of ⊗. E.g InflatedTensorMapping(I(3),B,I(2)) -> I(3)⊗B⊗I(2) |
246 | 244 |
247 function range_size(itm::InflatedTensorMapping) | 245 function range_size(itm::InflatedTensorMapping) |
248 return flatten_tuple( | 246 return flatten_tuple( |
249 range_size(itm.before), | 247 range_size(itm.before), |
259 domain_size(itm.after), | 257 domain_size(itm.after), |
260 ) | 258 ) |
261 end | 259 end |
262 | 260 |
263 function apply(itm::InflatedTensorMapping{T,R,D}, v::AbstractArray{T,D}, I::Vararg{Any,R}) where {T,R,D} | 261 function apply(itm::InflatedTensorMapping{T,R,D}, v::AbstractArray{T,D}, I::Vararg{Any,R}) where {T,R,D} |
264 view_index, inner_index = split_index(itm, I...) | 262 dim_before = range_dim(itm.before) |
263 dim_domain = domain_dim(itm.tm) | |
264 dim_range = range_dim(itm.tm) | |
265 dim_after = range_dim(itm.after) | |
266 | |
267 view_index, inner_index = split_index(Val(dim_before), Val(dim_domain), Val(dim_range), Val(dim_after), I...) | |
265 | 268 |
266 v_inner = view(v, view_index...) | 269 v_inner = view(v, view_index...) |
267 return apply(itm.tm, v_inner, inner_index...) | 270 return apply(itm.tm, v_inner, inner_index...) |
268 end | 271 end |
269 | 272 |
270 | 273 function apply_transpose(itm::InflatedTensorMapping{T,R,D}, v::AbstractArray{T,R}, I::Vararg{Any,D}) where {T,R,D} |
271 """ | 274 dim_before = range_dim(itm.before) |
272 split_index(...) | 275 dim_domain = domain_dim(itm.tm) |
273 | 276 dim_range = range_dim(itm.tm) |
274 Splits the multi-index into two parts. One part for the view that the inner TensorMapping acts on, and one part for indexing the result | 277 dim_after = range_dim(itm.after) |
278 | |
279 view_index, inner_index = split_index(Val(dim_before), Val(dim_range), Val(dim_domain), Val(dim_after), I...) | |
280 | |
281 v_inner = view(v, view_index...) | |
282 return apply_transpose(itm.tm, v_inner, inner_index...) | |
283 end | |
284 | |
285 | |
286 """ | |
287 split_index(::Val{dim_before}, ::Val{dim_view}, ::Val{dim_index}, ::Val{dim_after}, I...) | |
288 | |
289 Splits the multi-index `I` into two parts. One part which is expected to be | |
290 used as a view, and one which is expected to be used as an index. | |
275 Eg. | 291 Eg. |
276 ``` | 292 ``` |
277 (1,2,3,4) -> (1,:,:,4), (2,3) | 293 split_index(Val(1),Val(3),Val(2),Val(1),(1,2,3,4)) -> (1,:,:,:,4), (2,3) |
278 ``` | 294 ``` |
279 """ | 295 |
280 function split_index(itm::InflatedTensorMapping{T,R,D}, I::Vararg{Any,R}) where {T,R,D} | 296 `dim_view` controls how many colons are in the view, and `dim_index` controls |
281 I_before = slice_tuple(I, Val(1), Val(range_dim(itm.before))) | 297 how many elements are extracted from the middle. |
282 I_after = slice_tuple(I, Val(R-range_dim(itm.after)+1), Val(R)) | 298 `dim_before` and `dim_after` decides the length of the index parts before and after the colons in the view index. |
283 | 299 |
284 view_index = (I_before..., ntuple((i)->:,domain_dim(itm.tm))..., I_after...) | 300 Arguments should satisfy `length(I) == dim_before+B_domain+dim_after`. |
285 inner_index = slice_tuple(I, Val(range_dim(itm.before)+1), Val(R-range_dim(itm.after))) | 301 |
286 | 302 The returned values satisfy |
287 return (view_index, inner_index) | 303 * `length(view_index) == dim_before + dim_view + dim_after` |
304 * `length(I_middle) == dim_index` | |
305 """ | |
306 function split_index(::Val{dim_before}, ::Val{dim_view}, ::Val{dim_index}, ::Val{dim_after}, I...) where {dim_before,dim_view, dim_index,dim_after} | |
307 I_before, I_middle, I_after = split_tuple(I, Val(dim_before), Val(dim_index)) | |
308 | |
309 view_index = (I_before..., ntuple((i)->:, dim_view)..., I_after...) | |
310 | |
311 return view_index, I_middle | |
288 end | 312 end |
289 | 313 |
290 # TODO: Can this be replaced by something more elegant while still being type stable? 2020-10-21 | 314 # TODO: Can this be replaced by something more elegant while still being type stable? 2020-10-21 |
291 # See: | 315 # See: |
292 # https://github.com/JuliaLang/julia/issues/34884 | 316 # https://github.com/JuliaLang/julia/issues/34884 |
298 Equivalent to t[l:u] but type stable. | 322 Equivalent to t[l:u] but type stable. |
299 """ | 323 """ |
300 function slice_tuple(t,::Val{L},::Val{U}) where {L,U} | 324 function slice_tuple(t,::Val{L},::Val{U}) where {L,U} |
301 return ntuple(i->t[i+L-1], U-L+1) | 325 return ntuple(i->t[i+L-1], U-L+1) |
302 end | 326 end |
327 | |
328 """ | |
329 split_tuple(t::Tuple{...}, ::Val{M}) where {N,M} | |
330 | |
331 Split the tuple `t` into two parts. the first part is `M` long. | |
332 E.g | |
333 ``` | |
334 split_tuple((1,2,3,4),Val(3)) -> (1,2,3), (4,) | |
335 ``` | |
336 """ | |
337 function split_tuple(t::NTuple{N},::Val{M}) where {N,M} | |
338 return slice_tuple(t,Val(1), Val(M)), slice_tuple(t,Val(M+1), Val(N)) | |
339 end | |
340 | |
341 """ | |
342 split_tuple(t::Tuple{...},::Val{M},::Val{K}) where {N,M,K} | |
343 | |
344 Same as `split_tuple(t::NTuple{N},::Val{M})` but splits the tuple in three parts. With the first | |
345 two parts having lenght `M` and `K`. | |
346 """ | |
347 function split_tuple(t::NTuple{N},::Val{M},::Val{K}) where {N,M,K} | |
348 p1, tail = split_tuple(t, Val(M)) | |
349 p2, p3 = split_tuple(tail, Val(K)) | |
350 return p1,p2,p3 | |
351 end | |
352 | |
303 | 353 |
304 """ | 354 """ |
305 flatten_tuple(t) | 355 flatten_tuple(t) |
306 | 356 |
307 Takes a nested tuple and flattens the whole structure | 357 Takes a nested tuple and flattens the whole structure |