annotate LazyTensors/src/lazy_array.jl @ 325:41c3c25e4e3b

LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
author Jonatan Werpers <jonatan@werpers.com>
date Thu, 24 Sep 2020 22:31:04 +0200
parents 634453a4e1d8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
267
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
1 """
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
2 LazyArray{T,D} <: AbstractArray{T,D}
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
3
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
4 Array which is calcualted lazily when indexing.
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
5
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
6 A subtype of `LazyArray` will use lazy version of `+`, `-`, `*`, `/`.
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
7 """
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
8 abstract type LazyArray{T,D} <: AbstractArray{T,D} end
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
9 export LazyArray
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
10
325
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
11 struct LazyConstantArray{T,D} <: LazyArray{T,D}
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
12 val::T
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
13 size::NTuple{D,Int}
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
14 end
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
15
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
16 Base.size(lca::LazyConstantArray) = lca.size
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
17 Base.getindex(lca::LazyConstantArray{T,D}, I::Vararg{Int,D}) where {T,D} = lca.val
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
18
267
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
19 """
325
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
20 LazyElementwiseOperation{T,D,Op} <: LazyArray{T,D}
267
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
21 Struct allowing for lazy evaluation of elementwise operations on AbstractArrays.
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
22
325
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
23 A LazyElementwiseOperation contains two arrays together with an operation.
267
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
24 The operations are carried out when the LazyElementwiseOperation is indexed.
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
25 """
325
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
26 struct LazyElementwiseOperation{T,D,Op} <: LazyArray{T,D}
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
27 a::AbstractArray{T,D}
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
28 b::AbstractArray{T,D}
267
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
29
325
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
30 function LazyElementwiseOperation{T,D,Op}(a::AbstractArray{T,D},b::AbstractArray{T,D}) where {T,D,Op}
267
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
31 @boundscheck if size(a) != size(b)
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
32 throw(DimensionMismatch("dimensions must match"))
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
33 end
325
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
34 return new{T,D,Op}(a,b)
267
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
35 end
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
36
325
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
37 LazyElementwiseOperation{T,D,Op}(a::AbstractArray{T,D},b::T) where {T,D,Op} = new{T,D,Op}(a, LazyConstantArray(b, size(a)))
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
38 LazyElementwiseOperation{T,D,Op}(a::T,b::AbstractArray{T,D}) where {T,D,Op} = new{T,D,Op}(LazyConstantArray(a, size(b)), b)
267
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
39 end
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
40 # TODO: Move Op to be the first parameter? Compare to Binary operations
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
41
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
42 Base.size(v::LazyElementwiseOperation) = size(v.a)
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
43
325
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
44 evaluate(leo::LazyElementwiseOperation{T,D,:+}, I::Vararg{Int,D}) where {T,D} = leo.a[I...] + leo.b[I...]
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
45 evaluate(leo::LazyElementwiseOperation{T,D,:-}, I::Vararg{Int,D}) where {T,D} = leo.a[I...] - leo.b[I...]
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
46 evaluate(leo::LazyElementwiseOperation{T,D,:*}, I::Vararg{Int,D}) where {T,D} = leo.a[I...] * leo.b[I...]
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
47 evaluate(leo::LazyElementwiseOperation{T,D,:/}, I::Vararg{Int,D}) where {T,D} = leo.a[I...] / leo.b[I...]
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
48
267
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
49 # TODO: Make sure boundschecking is done properly and that the lenght of the vectors are equal
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
50 # NOTE: Boundschecking in getindex functions now assumes that the size of the
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
51 # vectors in the LazyElementwiseOperation are the same size. If we remove the
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
52 # size assertion in the constructor we might have to handle
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
53 # boundschecking differently.
325
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
54 Base.@propagate_inbounds @inline function Base.getindex(leo::LazyElementwiseOperation{T,D}, I::Vararg{Int,D}) where {T,D}
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
55 @boundscheck if !checkbounds(Bool, leo.a, I...)
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
56 throw(BoundsError([leo], I...))
267
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
57 end
325
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
58 return evaluate(leo, I...)
267
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
59 end
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
60
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
61 # Define lazy operations for AbstractArrays. Operations constructs a LazyElementwiseOperation which
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
62 # can later be indexed into. Lazy operations are denoted by the usual operator followed by a tilde
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
63 Base.@propagate_inbounds +̃(a::AbstractArray{T,D}, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:+}(a,b)
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
64 Base.@propagate_inbounds -̃(a::AbstractArray{T,D}, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:-}(a,b)
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
65 Base.@propagate_inbounds *̃(a::AbstractArray{T,D}, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:*}(a,b)
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
66 Base.@propagate_inbounds /̃(a::AbstractArray{T,D}, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:/}(a,b)
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
67
325
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
68 Base.@propagate_inbounds +̃(a::AbstractArray{T,D}, b::T) where {T,D} = LazyElementwiseOperation{T,D,:+}(a,b)
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
69 Base.@propagate_inbounds -̃(a::AbstractArray{T,D}, b::T) where {T,D} = LazyElementwiseOperation{T,D,:-}(a,b)
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
70 Base.@propagate_inbounds *̃(a::AbstractArray{T,D}, b::T) where {T,D} = LazyElementwiseOperation{T,D,:*}(a,b)
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
71 Base.@propagate_inbounds /̃(a::AbstractArray{T,D}, b::T) where {T,D} = LazyElementwiseOperation{T,D,:/}(a,b)
267
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
72
325
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
73 Base.@propagate_inbounds +̃(a::T, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:+}(a,b)
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
74 Base.@propagate_inbounds -̃(a::T, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:-}(a,b)
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
75 Base.@propagate_inbounds *̃(a::T, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:*}(a,b)
41c3c25e4e3b LazyTensors: Simplify the LazyElementwiseOperation type by restricting it and introducing a LazyConstantArray to handle scalars.
Jonatan Werpers <jonatan@werpers.com>
parents: 267
diff changeset
76 Base.@propagate_inbounds /̃(a::T, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:/}(a,b)
267
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
77
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
78
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
79
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
80 # NOTE: Är det knas att vi har till exempel * istället för .* ??
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
81 # Oklart om det ens går att lösa..
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
82 Base.@propagate_inbounds Base.:+(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
83 Base.@propagate_inbounds Base.:+(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a +̃ b
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
84 Base.@propagate_inbounds Base.:+(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
85
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
86 Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
87 Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a -̃ b
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
88 Base.@propagate_inbounds Base.:-(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
89
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
90 # Element wise operation for `*` and `\` are not overloaded due to conflicts with the behavior
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
91 # of regular `*` and `/` for AbstractArrays. Use tilde versions instead.
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
92
634453a4e1d8 Restructure code in LazyTensors
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
93 export +̃, -̃, *̃, /̃