Mercurial > repos > public > sbplib_julia
comparison LazyTensors/src/lazy_tensor_operations.jl @ 274:11010bb74260 boundary_conditions
Dispatch getindex for TensorMappingApplication on region indices. Dispatch apply
for TensorMappingBinaryOperation on region indices. Update tests. Update todo
author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
---|---|
date | Mon, 06 Jan 2020 10:54:48 +0100 |
parents | 634453a4e1d8 |
children | 591609cdcd9b |
comparison
equal
deleted
inserted
replaced
273:babc4288e6a6 | 274:11010bb74260 |
---|---|
12 o::AbstractArray{T,D} | 12 o::AbstractArray{T,D} |
13 end | 13 end |
14 export LazyTensorMappingApplication | 14 export LazyTensorMappingApplication |
15 | 15 |
16 Base.:*(tm::TensorMapping{T,R,D}, o::AbstractArray{T,D}) where {T,R,D} = LazyTensorMappingApplication(tm,o) | 16 Base.:*(tm::TensorMapping{T,R,D}, o::AbstractArray{T,D}) where {T,R,D} = LazyTensorMappingApplication(tm,o) |
17 | 17 Base.getindex(ta::LazyTensorMappingApplication{T,R,D}, I::Vararg{Index{<:Region},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, 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)) | 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? | 20 # TODO: What else is needed to implement the AbstractArray interface? |
21 | 21 |
22 # # We need the associativity to be a→b→c = a→(b→c), which is the case for '→' | 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...)) | 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 operator? | 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) | 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. | 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)? | 27 # For example what happens if you try to multiply LazyTensorMappingApplication with a TensorMapping(wrong order)? |
28 | 28 |
29 """ | 29 """ |
39 tm::TensorMapping{T,R,D} | 39 tm::TensorMapping{T,R,D} |
40 end | 40 end |
41 export LazyTensorMappingTranspose | 41 export LazyTensorMappingTranspose |
42 | 42 |
43 # # TBD: Should this be implemented on a type by type basis or through a trait to provide earlier errors? | 43 # # TBD: Should this be implemented on a type by type basis or through a trait to provide earlier errors? |
44 Base.adjoint(t::TensorMapping) = LazyTensorMappingTranspose(t) | 44 Base.adjoint(tm::TensorMapping) = LazyTensorMappingTranspose(tm) |
45 Base.adjoint(t::LazyTensorMappingTranspose) = t.tm | 45 Base.adjoint(tmt::LazyTensorMappingTranspose) = tmt.tm |
46 | 46 |
47 apply(tm::LazyTensorMappingTranspose{T,R,D}, v::AbstractArray{T,R}, I::NTuple{D,Int}) where {T,R,D} = apply_transpose(tm.tm, v, I) | 47 apply(tmt::LazyTensorMappingTranspose{T,R,D}, v::AbstractArray{T,R}, I::NTuple{D,Int}) where {T,R,D} = apply_transpose(tmt.tm, v, I) |
48 apply_transpose(tm::LazyTensorMappingTranspose{T,R,D}, v::AbstractArray{T,D}, I::NTuple{R,Int}) where {T,R,D} = apply(tm.tm, v, I) | 48 apply_transpose(tmt::LazyTensorMappingTranspose{T,R,D}, v::AbstractArray{T,D}, I::NTuple{R,Int}) where {T,R,D} = apply(tmt.tm, v, I) |
49 | 49 |
50 range_size(tmt::LazyTensorMappingTranspose{T,R,D}, d_size::NTuple{R,Integer}) where {T,R,D} = domain_size(tmt.tm, d_size) | 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) | 51 domain_size(tmt::LazyTensorMappingTranspose{T,R,D}, r_size::NTuple{D,Integer}) where {T,R,D} = range_size(tmt.tm, r_size) |
52 | 52 |
53 | 53 |
54 | 54 |
55 | 55 |
56 struct LazyTensorMappingBinaryOperation{Op,T,R,D,T1<:TensorMapping{T,R,D},T2<:TensorMapping{T,R,D}} <: TensorMapping{T,D,R} | 56 struct LazyTensorMappingBinaryOperation{Op,T,R,D,T1<:TensorMapping{T,R,D},T2<:TensorMapping{T,R,D}} <: TensorMapping{T,D,R} |
57 A::T1 | 57 tm1::T1 |
58 B::T2 | 58 tm2::T2 |
59 | 59 |
60 @inline function LazyTensorMappingBinaryOperation{Op,T,R,D}(A::T1,B::T2) where {Op,T,R,D, T1<:TensorMapping{T,R,D},T2<:TensorMapping{T,R,D}} | 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}(A,B) | 61 return new{Op,T,R,D,T1,T2}(tm1,tm2) |
62 end | 62 end |
63 end | 63 end |
64 | 64 |
65 apply(mb::LazyTensorMappingBinaryOperation{:+,T,R,D}, v::AbstractArray{T,D}, I::NTuple{R,Int}) where {T,R,D} = apply(mb.A, v, I...) + apply(mb.B,v,I...) | 65 apply(tmBinOp::LazyTensorMappingBinaryOperation{:+,T,R,D}, v::AbstractArray{T,D}, I::NTuple{R,Index{<:Region}}) where {T,R,D} = apply(tmBinOp.tm1, v, I) + apply(tmBinOp.tm2, v, I) |
66 apply(mb::LazyTensorMappingBinaryOperation{:-,T,R,D}, v::AbstractArray{T,D}, I::NTuple{R,Int}) where {T,R,D} = apply(mb.A, v, I...) - apply(mb.B,v,I...) | 66 apply(tmBinOp::LazyTensorMappingBinaryOperation{:-,T,R,D}, v::AbstractArray{T,D}, I::NTuple{R,Index{<:Region}}) where {T,R,D} = apply(tmBinOp.tm1, v, I) - apply(tmBinOp.tm2, v, I) |
67 | 67 |
68 range_size(mp::LazyTensorMappingBinaryOperation{Op,T,R,D}, domain_size::NTuple{D,Integer}) where {Op,T,R,D} = range_size(mp.A, domain_size) | 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(mp::LazyTensorMappingBinaryOperation{Op,T,R,D}, range_size::NTuple{R,Integer}) where {Op,T,R,D} = domain_size(mp.A, range_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 | 70 |
71 Base.:+(A::TensorMapping{T,R,D}, B::TensorMapping{T,R,D}) where {T,R,D} = LazyTensorMappingBinaryOperation{:+,T,R,D}(A,B) | 71 Base.:+(tm1::TensorMapping{T,R,D}, tm2::TensorMapping{T,R,D}) where {T,R,D} = LazyTensorMappingBinaryOperation{:+,T,R,D}(tm1,tm2) |
72 Base.:-(A::TensorMapping{T,R,D}, B::TensorMapping{T,R,D}) where {T,R,D} = LazyTensorMappingBinaryOperation{:-,T,R,D}(A,B) | 72 Base.:-(tm1::TensorMapping{T,R,D}, tm2::TensorMapping{T,R,D}) where {T,R,D} = LazyTensorMappingBinaryOperation{:-,T,R,D}(tm1,tm2) |
73 | 73 |
74 | 74 |
75 # TODO: Write tests and documentation for LazyTensorMappingComposition | 75 # TODO: Write tests and documentation for LazyTensorMappingComposition |
76 # struct LazyTensorMappingComposition{T,R,K,D} <: TensorMapping{T,R,D} | 76 # struct LazyTensorMappingComposition{T,R,K,D} <: TensorMapping{T,R,D} |
77 # t1::TensorMapping{T,R,K} | 77 # t1::TensorMapping{T,R,K} |