Mercurial > repos > public > sbplib_julia
annotate LazyTensors/src/Lazy.jl @ 192:4799bc46eaa9 boundary_conditions
Remove assert in Lazy.jl. Add todo for fixing the bounds checking in getindex methods
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Thu, 20 Jun 2019 23:02:37 +0200 |
parents | e8e21db70112 |
children | 2d97c54fd1f1 |
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 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
|
13 end |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
14 end |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
15 |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
16 Base.size(v::LazyElementwiseOperation) = size(v.a) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
17 |
192
4799bc46eaa9
Remove assert in Lazy.jl. Add todo for fixing the bounds checking in getindex methods
Jonatan Werpers <jonatan@werpers.com>
parents:
189
diff
changeset
|
18 # TODO: Make sure boundschecking is done properly and that the lenght of the vectors are equal |
188
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
19 # 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
|
20 # 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
|
21 # 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
|
22 # boundschecking differently. |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
23 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
|
24 @boundscheck if !checkbounds(Bool,leo.a,I...) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
25 throw(BoundsError([leo],[I...])) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
26 end |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
27 return leo.a[I...] + leo.b[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 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
|
30 @boundscheck if !checkbounds(Bool,leo.a,I...) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
31 throw(BoundsError([leo],[I...])) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
32 end |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
33 return leo.a[I...] - leo.b[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 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
|
36 @boundscheck if !checkbounds(Bool,leo.a,I...) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
37 throw(BoundsError([leo],[I...])) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
38 end |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
39 return leo.a[I...] * leo.b[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 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
|
42 @boundscheck if !checkbounds(Bool,leo.a,I...) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
43 throw(BoundsError([leo],[I...])) |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
44 end |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
45 return leo.a[I...] / leo.b[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 |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
48 # 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
|
49 # 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
|
50 @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
|
51 @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
|
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 |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
55 # 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
|
56 # lazy counterparts |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
57 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
|
58 |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
59 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
|
60 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
|
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} = a -̃ b |
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} = b * a |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
65 # TODO: / seems to be ambiguous |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
66 # 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
|
67 # 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
|
68 |
4558789b5948
Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
69 export +̃, -̃, *̃, /̃, +, -, * #, / |
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 end |