Mercurial > repos > public > sbplib_julia
comparison LazyTensors/src/lazy_operations.jl @ 196:b3c252280a19 boundary_conditions
Move LazyArray and make LazyTensorMappingApplication and LazyElementwiseOperation subtypes of it
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Thu, 20 Jun 2019 23:25:38 +0200 |
parents | 5413067d2c4a |
children | a340fa91b1fc |
comparison
equal
deleted
inserted
replaced
195:5413067d2c4a | 196:b3c252280a19 |
---|---|
23 domain_size(tmt::LazyTensorMappingTranspose{T,R,D}, r_size::NTuple{D,Integer}) where {T,R,D} = range_size(tmt.tm, range_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 | 24 |
25 | 25 |
26 | 26 |
27 """ | 27 """ |
28 LazyTensorMappingApplication{T,R,D} <: AbstractArray{T,R} | 28 LazyArray{T,D} <: AbstractArray{T,D} |
29 | |
30 Array which is calcualted lazily when indexing. | |
31 | |
32 A subtype of `LazyArray` will use lazy version of `+`, `-`, `*`, `/`. | |
33 """ | |
34 abstract type LazyArray{T,D} <: AbstractArray{T,D} end | |
35 export LazyArray | |
36 | |
37 """ | |
38 LazyTensorMappingApplication{T,R,D} <: LazyArray{T,R} | |
29 | 39 |
30 Struct for lazy application of a TensorMapping. Created using `*`. | 40 Struct for lazy application of a TensorMapping. Created using `*`. |
31 | 41 |
32 Allows the result of a `TensorMapping` applied to a vector to be treated as an `AbstractArray`. | 42 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`. | 43 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`. | 44 The actual result will be calcualted when indexing into `m*v`. |
35 """ | 45 """ |
36 struct LazyTensorMappingApplication{T,R,D} <: AbstractArray{T,R} | 46 struct LazyTensorMappingApplication{T,R,D} <: LazyArray{T,R} |
37 t::TensorMapping{T,R,D} | 47 t::TensorMapping{T,R,D} |
38 o::AbstractArray{T,D} | 48 o::AbstractArray{T,D} |
39 end | 49 end |
40 export LazyTensorMappingApplication | 50 export LazyTensorMappingApplication |
41 | 51 |
62 | 72 |
63 A LazyElementwiseOperation contains two AbstractArrays of equal size, | 73 A LazyElementwiseOperation contains two AbstractArrays of equal size, |
64 together with an operation. The operations are carried out when the | 74 together with an operation. The operations are carried out when the |
65 LazyElementwiseOperation is indexed. | 75 LazyElementwiseOperation is indexed. |
66 """ | 76 """ |
67 struct LazyElementwiseOperation{T,D,Op, T1<:AbstractArray{T,D}, T2 <: AbstractArray{T,D}} <: AbstractArray{T,D} | 77 struct LazyElementwiseOperation{T,D,Op, T1<:AbstractArray{T,D}, T2 <: AbstractArray{T,D}} <: LazyArray{T,D} |
68 a::T1 | 78 a::T1 |
69 b::T2 | 79 b::T2 |
70 | 80 |
71 function LazyElementwiseOperation{T,D,Op}(a::T1,b::T2) where {T,D,Op, T1<:AbstractArray{T,D}, T2<:AbstractArray{T,D}} | 81 function LazyElementwiseOperation{T,D,Op}(a::T1,b::T2) where {T,D,Op, T1<:AbstractArray{T,D}, T2<:AbstractArray{T,D}} |
72 return new{T,D,Op,T1,T2}(a,b) | 82 return new{T,D,Op,T1,T2}(a,b) |
110 @inline +̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:+}(a,b) | 120 @inline +̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:+}(a,b) |
111 @inline -̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:-}(a,b) | 121 @inline -̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:-}(a,b) |
112 @inline *̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:*}(a,b) | 122 @inline *̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:*}(a,b) |
113 @inline /̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:/}(a,b) | 123 @inline /̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:/}(a,b) |
114 | 124 |
115 # Abstract type for which the normal operations are defined by their | |
116 # lazy counterparts | |
117 abstract type LazyArray{T,D} <: AbstractArray{T,D} end | |
118 | 125 |
119 Base.:+(a::LazyArray{T,D},b::AbstractArray{T,D}) where {T,D} = a +̃ b | 126 Base.:+(a::LazyArray{T,D},b::AbstractArray{T,D}) where {T,D} = a +̃ b |
120 Base.:+(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = b + a | 127 Base.:+(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = b + a |
121 Base.:-(a::LazyArray{T,D},b::AbstractArray{T,D}) where {T,D} = a -̃ b | 128 Base.:-(a::LazyArray{T,D},b::AbstractArray{T,D}) where {T,D} = a -̃ b |
122 Base.:-(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b | 129 Base.:-(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b |