Mercurial > repos > public > sbplib_julia
annotate 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 |
rev | line source |
---|---|
188
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
1 module Lazy |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
2 |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
3 # Struct allowing for lazy evaluation of operations on AbstractArrays |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
4 # A LazyElementwiseOperation is defined by two same-sized AbstractArrays |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
5 # together with an operation. The operations are carried out when the |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
6 # LazyElementwiseOperation is indexed. |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
7 struct LazyElementwiseOperation{T,D,Op, T1<:AbstractArray{T,D}, T2 <: AbstractArray{T,D}} <: AbstractArray{T,D} |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
8 a::T1 |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
9 b::T2 |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
10 |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
11 function LazyElementwiseOperation{T,D,Op}(a::T1,b::T2) where {T,D,Op, T1<:AbstractArray{T,D}, T2<:AbstractArray{T,D}} |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
12 #TODO: Remove assert? Asserts are not removed when compiling with |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
13 # optimization flags. If so, need to handle boundschecking proparly. |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
14 @assert size(a) == size(b) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
15 return new{T,D,Op,T1,T2}(a,b) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
16 end |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
17 end |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
18 |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
19 Base.size(v::LazyElementwiseOperation) = size(v.a) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
20 |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
21 # NOTE: Boundschecking in getindex functions now assumes that the size of the |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
22 # vectors in the LazyElementwiseOperation are the same size. If we remove the |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
23 # size assertion in the constructor we might have to handle |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
24 # boundschecking differently. |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
25 Base.@propagate_inbounds @inline function Base.getindex(leo::LazyElementwiseOperation{T,D,:+}, I...) where {T,D} |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
26 @boundscheck if !checkbounds(Bool,leo.a,I...) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
27 throw(BoundsError([leo],[I...])) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
28 end |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
29 return leo.a[I...] + leo.b[I...] |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
30 end |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
31 Base.@propagate_inbounds @inline function Base.getindex(leo::LazyElementwiseOperation{T,D,:-}, I...) where {T,D} |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
32 @boundscheck if !checkbounds(Bool,leo.a,I...) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
33 throw(BoundsError([leo],[I...])) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
34 end |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
35 return leo.a[I...] - leo.b[I...] |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
36 end |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
37 Base.@propagate_inbounds @inline function Base.getindex(leo::LazyElementwiseOperation{T,D,:*}, I...) where {T,D} |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
38 @boundscheck if !checkbounds(Bool,leo.a,I...) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
39 throw(BoundsError([leo],[I...])) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
40 end |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
41 return leo.a[I...] * leo.b[I...] |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
42 end |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
43 Base.@propagate_inbounds @inline function Base.getindex(leo::LazyElementwiseOperation{T,D,:/}, I...) where {T,D} |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
44 @boundscheck if !checkbounds(Bool,leo.a,I...) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
45 throw(BoundsError([leo],[I...])) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
46 end |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
47 return leo.a[I...] / leo.b[I...] |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
48 end |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
49 |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
50 # Define lazy operations for AbstractArrays. Operations constructs a LazyElementwiseOperation which |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
51 # can later be indexed into. Lazy operations are denoted by the usual operator followed by a tilde |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
52 @inline +̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:+}(a,b) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
53 @inline -̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:-}(a,b) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
54 @inline *̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:*}(a,b) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
55 @inline /̃(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:/}(a,b) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
56 |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
57 # Abstract type for which the normal operations are defined by their |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
58 # lazy counterparts |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
59 abstract type LazyArray{T,D} <: AbstractArray{T,D} end; |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
60 |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
61 Base.:+(a::LazyArray{T,D},b::AbstractArray{T,D}) where {T,D} = a +̃ b |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
62 Base.:+(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = b + a |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
63 Base.:-(a::LazyArray{T,D},b::AbstractArray{T,D}) where {T,D} = a -̃ b |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
64 Base.:-(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
65 Base.:*(a::LazyArray{T,D},b::AbstractArray{T,D}) where {T,D} = a *̃ b |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
66 Base.:*(a::AbstractArray{T,D},b::LazyArray{T,D}) where {T,D} = b * a |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
67 # TODO: / seems to be ambiguous |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
68 # Base.:/(a::LazyArray{T,D},b::AbstractArray{T,D}) where {T,D} = a /̃ b |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
69 # Base.:/(a::AbstractArray{T,D},b::LazyArray{T,D}) where {T,D} = a /̃ b |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
70 |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
71 export +̃, -̃, *̃, /̃, +, -, * #, / |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
72 |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
73 end |