comparison LazyTensors/src/Lazy.jl @ 189:e8e21db70112 boundary_conditions

Merge
author Jonatan Werpers <jonatan@werpers.com>
date Thu, 20 Jun 2019 22:22:43 +0200
parents LazyTensor/src/Lazy.jl@4558789b5948
children 4799bc46eaa9
comparison
equal deleted inserted replaced
186:715ff09bb2ce 189:e8e21db70112
1 module Lazy
2
3 # Struct allowing for lazy evaluation of operations on AbstractArrays
4 # A LazyElementwiseOperation is defined by two same-sized AbstractArrays
5 # together with an operation. The operations are carried out when the
6 # LazyElementwiseOperation is indexed.
7 struct LazyElementwiseOperation{T,D,Op, T1<:AbstractArray{T,D}, T2 <: AbstractArray{T,D}} <: AbstractArray{T,D}
8 a::T1
9 b::T2
10
11 function LazyElementwiseOperation{T,D,Op}(a::T1,b::T2) where {T,D,Op, T1<:AbstractArray{T,D}, T2<:AbstractArray{T,D}}
12 #TODO: Remove assert? Asserts are not removed when compiling with
13 # optimization flags. If so, need to handle boundschecking proparly.
14 @assert size(a) == size(b)
15 return new{T,D,Op,T1,T2}(a,b)
16 end
17 end
18
19 Base.size(v::LazyElementwiseOperation) = size(v.a)
20
21 # NOTE: Boundschecking in getindex functions now assumes that the size of the
22 # vectors in the LazyElementwiseOperation are the same size. If we remove the
23 # size assertion in the constructor we might have to handle
24 # boundschecking differently.
25 Base.@propagate_inbounds @inline function Base.getindex(leo::LazyElementwiseOperation{T,D,:+}, I...) where {T,D}
26 @boundscheck if !checkbounds(Bool,leo.a,I...)
27 throw(BoundsError([leo],[I...]))
28 end
29 return leo.a[I...] + leo.b[I...]
30 end
31 Base.@propagate_inbounds @inline function Base.getindex(leo::LazyElementwiseOperation{T,D,:-}, I...) where {T,D}
32 @boundscheck if !checkbounds(Bool,leo.a,I...)
33 throw(BoundsError([leo],[I...]))
34 end
35 return leo.a[I...] - leo.b[I...]
36 end
37 Base.@propagate_inbounds @inline function Base.getindex(leo::LazyElementwiseOperation{T,D,:*}, I...) where {T,D}
38 @boundscheck if !checkbounds(Bool,leo.a,I...)
39 throw(BoundsError([leo],[I...]))
40 end
41 return leo.a[I...] * leo.b[I...]
42 end
43 Base.@propagate_inbounds @inline function Base.getindex(leo::LazyElementwiseOperation{T,D,:/}, I...) where {T,D}
44 @boundscheck if !checkbounds(Bool,leo.a,I...)
45 throw(BoundsError([leo],[I...]))
46 end
47 return leo.a[I...] / leo.b[I...]
48 end
49
50 # Define lazy operations for AbstractArrays. Operations constructs a LazyElementwiseOperation which
51 # can later be indexed into. Lazy operations are denoted by the usual operator followed by a tilde
52 @inline +̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:+}(a,b)
53 @inline -̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:-}(a,b)
54 @inline *̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:*}(a,b)
55 @inline /̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:/}(a,b)
56
57 # Abstract type for which the normal operations are defined by their
58 # lazy counterparts
59 abstract type LazyArray{T,D} <: AbstractArray{T,D} end;
60
61 Base.:+(a::LazyArray{T,D},b::AbstractArray{T,D}) where {T,D} = a +̃ b
62 Base.:+(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = b + a
63 Base.:-(a::LazyArray{T,D},b::AbstractArray{T,D}) where {T,D} = a -̃ b
64 Base.:-(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b
65 Base.:*(a::LazyArray{T,D},b::AbstractArray{T,D}) where {T,D} = a *̃ b
66 Base.:*(a::AbstractArray{T,D},b::LazyArray{T,D}) where {T,D} = b * a
67 # TODO: / seems to be ambiguous
68 # Base.:/(a::LazyArray{T,D},b::AbstractArray{T,D}) where {T,D} = a /̃ b
69 # Base.:/(a::AbstractArray{T,D},b::LazyArray{T,D}) where {T,D} = a /̃ b
70
71 export +̃, -̃, *̃, /̃, +, -, * #, /
72
73 end