comparison LazyTensors/src/lazy_operations.jl @ 190:8964b3165097 boundary_conditions

Break LazyTensors.jl into several files
author Jonatan Werpers <jonatan@werpers.com>
date Thu, 20 Jun 2019 22:48:07 +0200
parents
children d30d42a11566
comparison
equal deleted inserted replaced
189:e8e21db70112 190:8964b3165097
1 """
2 LazyTensorMappingTranspose{T,R,D} <: TensorMapping{T,D,R}
3
4 Struct for lazy transpose of a TensorMapping.
5
6 If a mapping implements the the `apply_transpose` method this allows working with
7 the transpose of mapping `m` by using `m'`. `m'` will work as a regular TensorMapping lazily calling
8 the appropriate methods of `m`.
9 """
10 struct LazyTensorMappingTranspose{T,R,D} <: TensorMapping{T,D,R}
11 tm::TensorMapping{T,R,D}
12 end
13 export LazyTensorMappingTranspose
14
15 # # TBD: Should this be implemented on a type by type basis or through a trait to provide earlier errors?
16 Base.adjoint(t::TensorMapping) = LazyTensorMappingTranspose(t)
17 Base.adjoint(t::LazyTensorMappingTranspose) = t.tm
18
19 apply(tm::LazyTensorMappingTranspose{T,R,D}, v::AbstractArray{T,R}, I::Vararg) where {T,R,D} = apply_transpose(tm.tm, v, I...)
20 apply_transpose(tm::LazyTensorMappingTranspose{T,R,D}, v::AbstractArray{T,D}, I::Vararg) where {T,R,D} = apply(tm.tm, v, I...)
21
22 range_size(tmt::LazyTensorMappingTranspose{T,R,D}, d_size::NTuple{R,Integer}) where {T,R,D} = domain_size(tmt.tm, domain_size)
23 domain_size(tmt::LazyTensorMappingTranspose{T,R,D}, r_size::NTuple{D,Integer}) where {T,R,D} = range_size(tmt.tm, range_size)
24
25
26
27 """
28 LazyTensorMappingApplication{T,R,D} <: AbstractArray{T,R}
29
30 Struct for lazy application of a TensorMapping. Created using `*`.
31
32 Allows the result of a `TensorMapping` applied to a vector to be treated as an `AbstractArray`.
33 With a mapping `m` and a vector `v` the LazyTensorMappingApplication object can be created by `m*v`.
34 The actual result will be calcualted when indexing into `m*v`.
35 """
36 struct LazyTensorMappingApplication{T,R,D} <: AbstractArray{T,R}
37 t::TensorMapping{T,R,D}
38 o::AbstractArray{T,D}
39 end
40 export LazyTensorMappingApplication
41
42 Base.:*(tm::TensorMapping{T,R,D}, o::AbstractArray{T,D}) where {T,R,D} = LazyTensorMappingApplication(tm,o)
43
44 Base.getindex(ta::LazyTensorMappingApplication{T,R,D}, I::Vararg) where {T,R,D} = apply(ta.t, ta.o, I...)
45 Base.size(ta::LazyTensorMappingApplication{T,R,D}) where {T,R,D} = range_size(ta.t,size(ta.o))
46 # TODO: What else is needed to implement the AbstractArray interface?
47
48
49 # # We need the associativity to be a→b→c = a→(b→c), which is the case for '→'
50 Base.:*(args::Union{TensorMapping{T}, AbstractArray{T}}...) where T = foldr(*,args)
51 # # Should we overload some other infix binary operator?
52 # →(tm::TensorMapping{T,R,D}, o::AbstractArray{T,D}) where {T,R,D} = LazyTensorMappingApplication(tm,o)
53 # TODO: We need to be really careful about good error messages.
54 # For example what happens if you try to multiply LazyTensorMappingApplication with a TensorMapping(wrong order)?
55
56
57
58 # TODO: Write tests and documentation for LazyTensorMappingComposition
59 # struct LazyTensorMappingComposition{T,R,K,D} <: TensorMapping{T,R,D}
60 # t1::TensorMapping{T,R,K}
61 # t2::TensorMapping{T,K,D}
62 # end
63
64 # Base.:∘(s::TensorMapping{T,R,K}, t::TensorMapping{T,K,D}) where {T,R,K,D} = LazyTensorMappingComposition(s,t)
65
66 # function range_size(tm::LazyTensorMappingComposition{T,R,K,D}, domain_size::NTuple{D,Integer}) where {T,R,K,D}
67 # range_size(tm.t1, domain_size(tm.t2, domain_size))
68 # end
69
70 # function domain_size(tm::LazyTensorMappingComposition{T,R,K,D}, range_size::NTuple{R,Integer}) where {T,R,K,D}
71 # domain_size(tm.t1, domain_size(tm.t2, range_size))
72 # end
73
74 # function apply(c::LazyTensorMappingComposition{T,R,K,D}, v::AbstractArray{T,D}, I::Vararg) where {T,R,K,D}
75 # apply(c.t1, LazyTensorMappingApplication(c.t2,v), I...)
76 # end
77
78 # function apply_transpose(c::LazyTensorMappingComposition{T,R,K,D}, v::AbstractArray{T,D}, I::Vararg) where {T,R,K,D}
79 # apply_transpose(c.t2, LazyTensorMappingApplication(c.t1',v), I...)
80 # end
81
82 # # Have i gone too crazy with the type parameters? Maybe they aren't all needed?
83
84 # export →