Mercurial > repos > public > sbplib_julia
comparison src/LazyTensors/lazy_tensor_operations.jl @ 333:01b851161018 refactor/combine_to_one_package
Start converting to one package by moving all the files to their correct location
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Fri, 25 Sep 2020 13:06:02 +0200 |
parents | LazyTensors/src/lazy_tensor_operations.jl@ece3f6f8a1d4 |
children | 7fe43d902a27 |
comparison
equal
deleted
inserted
replaced
332:535f1bff4bcc | 333:01b851161018 |
---|---|
1 """ | |
2 LazyTensorMappingApplication{T,R,D} <: LazyArray{T,R} | |
3 | |
4 Struct for lazy application of a TensorMapping. Created using `*`. | |
5 | |
6 Allows the result of a `TensorMapping` applied to a vector to be treated as an `AbstractArray`. | |
7 With a mapping `m` and a vector `v` the LazyTensorMappingApplication object can be created by `m*v`. | |
8 The actual result will be calcualted when indexing into `m*v`. | |
9 """ | |
10 struct LazyTensorMappingApplication{T,R,D} <: LazyArray{T,R} | |
11 t::TensorMapping{T,R,D} | |
12 o::AbstractArray{T,D} | |
13 end | |
14 export LazyTensorMappingApplication | |
15 | |
16 Base.:*(tm::TensorMapping{T,R,D}, o::AbstractArray{T,D}) where {T,R,D} = LazyTensorMappingApplication(tm,o) | |
17 Base.getindex(ta::LazyTensorMappingApplication{T,R,D}, I::Vararg{Index,R}) where {T,R,D} = apply(ta.t, ta.o, I...) | |
18 Base.getindex(ta::LazyTensorMappingApplication{T,R,D}, I::Vararg{Int,R}) where {T,R,D} = apply(ta.t, ta.o, Index{Unknown}.(I)...) | |
19 Base.size(ta::LazyTensorMappingApplication{T,R,D}) where {T,R,D} = range_size(ta.t,size(ta.o)) | |
20 # TODO: What else is needed to implement the AbstractArray interface? | |
21 | |
22 # # We need the associativity to be a→b→c = a→(b→c), which is the case for '→' | |
23 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...)) | |
24 # # Should we overload some other infix binary opesrator? | |
25 # →(tm::TensorMapping{T,R,D}, o::AbstractArray{T,D}) where {T,R,D} = LazyTensorMappingApplication(tm,o) | |
26 # TODO: We need to be really careful about good error messages. | |
27 # For example what happens if you try to multiply LazyTensorMappingApplication with a TensorMapping(wrong order)? | |
28 | |
29 """ | |
30 LazyTensorMappingTranspose{T,R,D} <: TensorMapping{T,D,R} | |
31 | |
32 Struct for lazy transpose of a TensorMapping. | |
33 | |
34 If a mapping implements the the `apply_transpose` method this allows working with | |
35 the transpose of mapping `m` by using `m'`. `m'` will work as a regular TensorMapping lazily calling | |
36 the appropriate methods of `m`. | |
37 """ | |
38 struct LazyTensorMappingTranspose{T,R,D} <: TensorMapping{T,D,R} | |
39 tm::TensorMapping{T,R,D} | |
40 end | |
41 export LazyTensorMappingTranspose | |
42 | |
43 # # TBD: Should this be implemented on a type by type basis or through a trait to provide earlier errors? | |
44 Base.adjoint(tm::TensorMapping) = LazyTensorMappingTranspose(tm) | |
45 Base.adjoint(tmt::LazyTensorMappingTranspose) = tmt.tm | |
46 | |
47 apply(tmt::LazyTensorMappingTranspose{T,R,D}, v::AbstractArray{T,R}, I::Vararg{Index,D}) where {T,R,D} = apply_transpose(tmt.tm, v, I...) | |
48 apply_transpose(tmt::LazyTensorMappingTranspose{T,R,D}, v::AbstractArray{T,D}, I::Vararg{Index,R}) where {T,R,D} = apply(tmt.tm, v, I...) | |
49 | |
50 range_size(tmt::LazyTensorMappingTranspose{T,R,D}, d_size::NTuple{R,Integer}) where {T,R,D} = domain_size(tmt.tm, d_size) | |
51 domain_size(tmt::LazyTensorMappingTranspose{T,R,D}, r_size::NTuple{D,Integer}) where {T,R,D} = range_size(tmt.tm, r_size) | |
52 | |
53 | |
54 | |
55 | |
56 struct LazyTensorMappingBinaryOperation{Op,T,R,D,T1<:TensorMapping{T,R,D},T2<:TensorMapping{T,R,D}} <: TensorMapping{T,D,R} | |
57 tm1::T1 | |
58 tm2::T2 | |
59 | |
60 @inline function LazyTensorMappingBinaryOperation{Op,T,R,D}(tm1::T1,tm2::T2) where {Op,T,R,D, T1<:TensorMapping{T,R,D},T2<:TensorMapping{T,R,D}} | |
61 return new{Op,T,R,D,T1,T2}(tm1,tm2) | |
62 end | |
63 end | |
64 | |
65 apply(tmBinOp::LazyTensorMappingBinaryOperation{:+,T,R,D}, v::AbstractArray{T,D}, I::Vararg{Index,R}) where {T,R,D} = apply(tmBinOp.tm1, v, I...) + apply(tmBinOp.tm2, v, I...) | |
66 apply(tmBinOp::LazyTensorMappingBinaryOperation{:-,T,R,D}, v::AbstractArray{T,D}, I::Vararg{Index,R}) where {T,R,D} = apply(tmBinOp.tm1, v, I...) - apply(tmBinOp.tm2, v, I...) | |
67 | |
68 range_size(tmBinOp::LazyTensorMappingBinaryOperation{Op,T,R,D}, domain_size::NTuple{D,Integer}) where {Op,T,R,D} = range_size(tmBinOp.tm1, domain_size) | |
69 domain_size(tmBinOp::LazyTensorMappingBinaryOperation{Op,T,R,D}, range_size::NTuple{R,Integer}) where {Op,T,R,D} = domain_size(tmBinOp.tm2, range_size) | |
70 | |
71 Base.:+(tm1::TensorMapping{T,R,D}, tm2::TensorMapping{T,R,D}) where {T,R,D} = LazyTensorMappingBinaryOperation{:+,T,R,D}(tm1,tm2) | |
72 Base.:-(tm1::TensorMapping{T,R,D}, tm2::TensorMapping{T,R,D}) where {T,R,D} = LazyTensorMappingBinaryOperation{:-,T,R,D}(tm1,tm2) | |
73 | |
74 | |
75 # TODO: Write tests and documentation for LazyTensorMappingComposition | |
76 # struct LazyTensorMappingComposition{T,R,K,D} <: TensorMapping{T,R,D} | |
77 # t1::TensorMapping{T,R,K} | |
78 # t2::TensorMapping{T,K,D} | |
79 # end | |
80 | |
81 # Base.:∘(s::TensorMapping{T,R,K}, t::TensorMapping{T,K,D}) where {T,R,K,D} = LazyTensorMappingComposition(s,t) | |
82 | |
83 # function range_size(tm::LazyTensorMappingComposition{T,R,K,D}, domain_size::NTuple{D,Integer}) where {T,R,K,D} | |
84 # range_size(tm.t1, domain_size(tm.t2, domain_size)) | |
85 # end | |
86 | |
87 # function domain_size(tm::LazyTensorMappingComposition{T,R,K,D}, range_size::NTuple{R,Integer}) where {T,R,K,D} | |
88 # domain_size(tm.t1, domain_size(tm.t2, range_size)) | |
89 # end | |
90 | |
91 # function apply(c::LazyTensorMappingComposition{T,R,K,D}, v::AbstractArray{T,D}, I::NTuple{R,Int}) where {T,R,K,D} | |
92 # apply(c.t1, LazyTensorMappingApplication(c.t2,v), I...) | |
93 # end | |
94 | |
95 # function apply_transpose(c::LazyTensorMappingComposition{T,R,K,D}, v::AbstractArray{T,D}, I::NTuple{D,Int}) where {T,R,K,D} | |
96 # apply_transpose(c.t2, LazyTensorMappingApplication(c.t1',v), I...) | |
97 # end | |
98 | |
99 # # Have i gone too crazy with the type parameters? Maybe they aren't all needed? | |
100 | |
101 # export → |