annotate LazyTensors/src/Lazy.jl @ 193:2d97c54fd1f1 boundary_conditions

Change documentation of LazyElementWiseOperation to docstring
author Jonatan Werpers <jonatan@werpers.com>
date Thu, 20 Jun 2019 23:05:12 +0200
parents 4799bc46eaa9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
193
2d97c54fd1f1 Change documentation of LazyElementWiseOperation to docstring
Jonatan Werpers <jonatan@werpers.com>
parents: 192
diff changeset
3
2d97c54fd1f1 Change documentation of LazyElementWiseOperation to docstring
Jonatan Werpers <jonatan@werpers.com>
parents: 192
diff changeset
4 """
2d97c54fd1f1 Change documentation of LazyElementWiseOperation to docstring
Jonatan Werpers <jonatan@werpers.com>
parents: 192
diff changeset
5 LazyElementwiseOperation{T,D,Op, T1<:AbstractArray{T,D}, T2 <: AbstractArray{T,D}} <: AbstractArray{T,D}
2d97c54fd1f1 Change documentation of LazyElementWiseOperation to docstring
Jonatan Werpers <jonatan@werpers.com>
parents: 192
diff changeset
6
2d97c54fd1f1 Change documentation of LazyElementWiseOperation to docstring
Jonatan Werpers <jonatan@werpers.com>
parents: 192
diff changeset
7 Struct allowing for lazy evaluation of elementwise operations on AbstractArrays.
2d97c54fd1f1 Change documentation of LazyElementWiseOperation to docstring
Jonatan Werpers <jonatan@werpers.com>
parents: 192
diff changeset
8
2d97c54fd1f1 Change documentation of LazyElementWiseOperation to docstring
Jonatan Werpers <jonatan@werpers.com>
parents: 192
diff changeset
9 A LazyElementwiseOperation contains two AbstractArrays of equal size,
2d97c54fd1f1 Change documentation of LazyElementWiseOperation to docstring
Jonatan Werpers <jonatan@werpers.com>
parents: 192
diff changeset
10 together with an operation. The operations are carried out when the
2d97c54fd1f1 Change documentation of LazyElementWiseOperation to docstring
Jonatan Werpers <jonatan@werpers.com>
parents: 192
diff changeset
11 LazyElementwiseOperation is indexed.
2d97c54fd1f1 Change documentation of LazyElementWiseOperation to docstring
Jonatan Werpers <jonatan@werpers.com>
parents: 192
diff changeset
12 """
188
4558789b5948 Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
13 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
14 a::T1
4558789b5948 Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
15 b::T2
4558789b5948 Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
16
4558789b5948 Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
17 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
18 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
19 end
4558789b5948 Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
20 end
4558789b5948 Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
21
4558789b5948 Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
22 Base.size(v::LazyElementwiseOperation) = size(v.a)
4558789b5948 Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
23
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
24 # 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
25 # 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
26 # 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
27 # 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
28 # boundschecking differently.
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 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
48 @boundscheck if !checkbounds(Bool,leo.a,I...)
4558789b5948 Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
49 throw(BoundsError([leo],[I...]))
4558789b5948 Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
50 end
4558789b5948 Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
51 return leo.a[I...] / leo.b[I...]
4558789b5948 Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
52 end
4558789b5948 Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
53
4558789b5948 Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
54 # 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
55 # 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
56 @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
57 @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
58 @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
59 @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
60
4558789b5948 Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
61 # 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
62 # lazy counterparts
4558789b5948 Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
63 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
64
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 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
68 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
69 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
70 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
71 # TODO: / seems to be ambiguous
4558789b5948 Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
72 # 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
73 # 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
74
4558789b5948 Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
75 export +̃, -̃, *̃, /̃, +, -, * #, /
4558789b5948 Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
76
4558789b5948 Add module for Lazy operations
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
77 end