comparison src/LazyTensors/lazy_array.jl @ 376:f65809a26a17

Merge performance/lazy_elementwise_operation
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 30 Sep 2020 21:09:35 +0200
parents 33c360c3c6bc 0a95a829176c
children 895ec483d741 76e5682d0e52
comparison
equal deleted inserted replaced
374:99296cbb7bcd 376:f65809a26a17
45 Struct allowing for lazy evaluation of elementwise operations on AbstractArrays. 45 Struct allowing for lazy evaluation of elementwise operations on AbstractArrays.
46 46
47 A LazyElementwiseOperation contains two arrays together with an operation. 47 A LazyElementwiseOperation contains two arrays together with an operation.
48 The operations are carried out when the LazyElementwiseOperation is indexed. 48 The operations are carried out when the LazyElementwiseOperation is indexed.
49 """ 49 """
50 struct LazyElementwiseOperation{T,D,Op} <: LazyArray{T,D} 50 struct LazyElementwiseOperation{T,D,Op,T1<:AbstractArray{T,D},T2<:AbstractArray{T,D}} <: LazyArray{T,D}
51 a::AbstractArray{T,D} 51 a::T1
52 b::AbstractArray{T,D} 52 b::T2
53 53
54 function LazyElementwiseOperation{T,D,Op}(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D,Op} 54 function LazyElementwiseOperation{T,D,Op}(a::T1,b::T2) where {T,D,Op,T1<:AbstractArray{T,D},T2<:AbstractArray{T,D}}
55 @boundscheck if size(a) != size(b) 55 @boundscheck if size(a) != size(b)
56 throw(DimensionMismatch("dimensions must match")) 56 throw(DimensionMismatch("dimensions must match"))
57 end 57 end
58 return new{T,D,Op}(a,b) 58 return new{T,D,Op,T1,T2}(a,b)
59 end 59 end
60 60
61 LazyElementwiseOperation{T,D,Op}(a::AbstractArray{T,D},b::T) where {T,D,Op} = new{T,D,Op}(a, LazyConstantArray(b, size(a)))
62 LazyElementwiseOperation{T,D,Op}(a::T,b::AbstractArray{T,D}) where {T,D,Op} = new{T,D,Op}(LazyConstantArray(a, size(b)), b)
63 end 61 end
62 LazyElementwiseOperation{T,D,Op}(a::AbstractArray{T,D},b::T) where {T,D,Op} = LazyElementwiseOperation{T,D,Op}(a, LazyConstantArray(b, size(a)))
63 LazyElementwiseOperation{T,D,Op}(a::T,b::AbstractArray{T,D}) where {T,D,Op} = LazyElementwiseOperation{T,D,Op}(LazyConstantArray(a, size(b)), b)
64 # TODO: Move Op to be the first parameter? Compare to Binary operations 64 # TODO: Move Op to be the first parameter? Compare to Binary operations
65 65
66 Base.size(v::LazyElementwiseOperation) = size(v.a) 66 Base.size(v::LazyElementwiseOperation) = size(v.a)
67 67
68 evaluate(leo::LazyElementwiseOperation{T,D,:+}, I::Vararg{Int,D}) where {T,D} = leo.a[I...] + leo.b[I...] 68 evaluate(leo::LazyElementwiseOperation{T,D,:+}, I::Vararg{Int,D}) where {T,D} = leo.a[I...] + leo.b[I...]