comparison src/LazyTensors/lazy_tensor_operations.jl @ 450:ac6d22570a08 feature/inflated_tensormapping

Merge in feature/lazy_identity
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 19 Oct 2020 21:42:57 +0200
parents 14d60de71b72 e70e47fbfa7c
children 6cf234eef780
comparison
equal deleted inserted replaced
449:14d60de71b72 450:ac6d22570a08
14 # TODO: Do boundschecking on creation! 14 # TODO: Do boundschecking on creation!
15 export LazyTensorMappingApplication 15 export LazyTensorMappingApplication
16 16
17 # TODO: Go through and remove unneccerary type parameters on functions 17 # TODO: Go through and remove unneccerary type parameters on functions
18 18
19 Base.:*(tm::TensorMapping{T,R,D}, o::AbstractArray{T,D}) where {T,R,D} = LazyTensorMappingApplication(tm,o)
20 Base.getindex(ta::LazyTensorMappingApplication{T,R,D}, I::Vararg{Index,R}) where {T,R,D} = apply(ta.t, ta.o, I...) 19 Base.getindex(ta::LazyTensorMappingApplication{T,R,D}, I::Vararg{Index,R}) where {T,R,D} = apply(ta.t, ta.o, I...)
21 Base.getindex(ta::LazyTensorMappingApplication{T,R,D}, I::Vararg{Int,R}) where {T,R,D} = apply(ta.t, ta.o, Index{Unknown}.(I)...) 20 Base.getindex(ta::LazyTensorMappingApplication{T,R,D}, I::Vararg{Int,R}) where {T,R,D} = apply(ta.t, ta.o, Index{Unknown}.(I)...)
22 Base.size(ta::LazyTensorMappingApplication) = range_size(ta.t) 21 Base.size(ta::LazyTensorMappingApplication) = range_size(ta.t)
23 # TODO: What else is needed to implement the AbstractArray interface? 22 # TODO: What else is needed to implement the AbstractArray interface?
24 23
24 Base.:*(a::TensorMapping, v::AbstractArray) = LazyTensorMappingApplication(a,v)
25 Base.:*(a::TensorMapping, b::TensorMapping) = throw(MethodError(Base.:*,(a,b)))
26 Base.:*(a::TensorMapping, args::Union{TensorMapping, AbstractArray}...) = foldr(*,(a,args...))
27
25 # # We need the associativity to be a→b→c = a→(b→c), which is the case for '→' 28 # # We need the associativity to be a→b→c = a→(b→c), which is the case for '→'
26 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...))
27 # # Should we overload some other infix binary opesrator? 29 # # Should we overload some other infix binary opesrator?
28 # →(tm::TensorMapping{T,R,D}, o::AbstractArray{T,D}) where {T,R,D} = LazyTensorMappingApplication(tm,o) 30 # →(tm::TensorMapping{T,R,D}, o::AbstractArray{T,D}) where {T,R,D} = LazyTensorMappingApplication(tm,o)
29 # TODO: We need to be really careful about good error messages. 31 # TODO: We need to be really careful about good error messages.
30 # For example what happens if you try to multiply LazyTensorMappingApplication with a TensorMapping(wrong order)? 32 # For example what happens if you try to multiply LazyTensorMappingApplication with a TensorMapping(wrong order)?
31 33
36 38
37 If a mapping implements the the `apply_transpose` method this allows working with 39 If a mapping implements the the `apply_transpose` method this allows working with
38 the transpose of mapping `m` by using `m'`. `m'` will work as a regular TensorMapping lazily calling 40 the transpose of mapping `m` by using `m'`. `m'` will work as a regular TensorMapping lazily calling
39 the appropriate methods of `m`. 41 the appropriate methods of `m`.
40 """ 42 """
41 struct LazyTensorMappingTranspose{T,R,D} <: TensorMapping{T,D,R} 43 struct LazyTensorMappingTranspose{T,R,D, TM<:TensorMapping{T,R,D}} <: TensorMapping{T,D,R}
42 tm::TensorMapping{T,R,D} 44 tm::TM
43 end 45 end
44 export LazyTensorMappingTranspose 46 export LazyTensorMappingTranspose
45 47
46 # # TBD: Should this be implemented on a type by type basis or through a trait to provide earlier errors? 48 # # TBD: Should this be implemented on a type by type basis or through a trait to provide earlier errors?
47 # Jonatan 2020-09-25: Is the problem that you can take the transpose of any TensorMapping even if it doesn't implement `apply_transpose`? 49 # Jonatan 2020-09-25: Is the problem that you can take the transpose of any TensorMapping even if it doesn't implement `apply_transpose`?
157 size::NTuple{D,Int} 159 size::NTuple{D,Int}
158 end 160 end
159 export IdentityMapping 161 export IdentityMapping
160 162
161 IdentityMapping{T}(size::NTuple{D,Int}) where {T,D} = IdentityMapping{T,D}(size) 163 IdentityMapping{T}(size::NTuple{D,Int}) where {T,D} = IdentityMapping{T,D}(size)
164 IdentityMapping{T}(size::Vararg{Int,D}) where {T,D} = IdentityMapping{T,D}(size)
165 IdentityMapping(size::Vararg{Int,D}) where D = IdentityMapping{Float64,D}(size)
162 166
163 range_size(tmi::IdentityMapping) = tmi.size 167 range_size(tmi::IdentityMapping) = tmi.size
164 domain_size(tmi::IdentityMapping) = tmi.size 168 domain_size(tmi::IdentityMapping) = tmi.size
165 169
166 apply(tmi::IdentityMapping{T,D}, v::AbstractArray{T,D}, I::Vararg{Any,D}) where {T,D} = v[I...] 170 apply(tmi::IdentityMapping{T,D}, v::AbstractArray{T,D}, I::Vararg{Any,D}) where {T,D} = v[I...]