Mercurial > repos > public > sbplib_julia
comparison src/LazyTensors/lazy_array.jl @ 1858:4a9be96f2569 feature/documenter_logo
Merge default
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Sun, 12 Jan 2025 21:18:44 +0100 |
parents | dfb43fdac9fc |
children |
comparison
equal
deleted
inserted
replaced
1857:ffde7dad9da5 | 1858:4a9be96f2569 |
---|---|
1 """ | 1 """ |
2 LazyArray{T,D} <: AbstractArray{T,D} | 2 LazyArray{T,D} <: AbstractArray{T,D} |
3 | 3 |
4 Array which is calcualted lazily when indexing. | 4 Array which is calculated lazily when indexing. |
5 | 5 |
6 A subtype of `LazyArray` will use lazy version of `+`, `-`, `*`, `/`. | 6 A subtype of `LazyArray` will use lazy version of `+`, `-`, `*`, `/`. |
7 """ | 7 """ |
8 abstract type LazyArray{T,D} <: AbstractArray{T,D} end | 8 abstract type LazyArray{T,D} <: AbstractArray{T,D} end |
9 export LazyArray | |
10 | 9 |
11 struct LazyConstantArray{T,D} <: LazyArray{T,D} | 10 struct LazyConstantArray{T,D} <: LazyArray{T,D} |
12 val::T | 11 val::T |
13 size::NTuple{D,Int} | 12 size::NTuple{D,Int} |
14 end | 13 end |
23 """ | 22 """ |
24 struct LazyFunctionArray{F<:Function,T, D} <: LazyArray{T,D} | 23 struct LazyFunctionArray{F<:Function,T, D} <: LazyArray{T,D} |
25 f::F | 24 f::F |
26 size::NTuple{D,Int} | 25 size::NTuple{D,Int} |
27 end | 26 end |
28 export LazyFunctionArray | |
29 | 27 |
30 function LazyFunctionArray(f::F, size::NTuple{D,Int}) where {F<:Function,D} | 28 function LazyFunctionArray(f::F, size::NTuple{D,Int}) where {F<:Function,D} |
31 T = typeof(f(ones(D)...)) | 29 T = typeof(f(ones(Int, D)...)) |
32 return LazyFunctionArray{F,T,D}(f,size) | 30 return LazyFunctionArray{F,T,D}(f,size) |
33 end | 31 end |
34 | 32 |
35 Base.size(lfa::LazyFunctionArray) = lfa.size | 33 Base.size(lfa::LazyFunctionArray) = lfa.size |
36 | 34 |
37 function Base.getindex(lfa::LazyFunctionArray{F,T,D}, I::Vararg{Int,D}) where {F,T,D} | 35 function Base.getindex(lfa::LazyFunctionArray{F,T,D}, I::Vararg{Int,D}) where {F,T,D} |
38 @boundscheck checkbounds(lfa, I...) | 36 @boundscheck checkbounds(lfa, I...) |
39 return lfa.f(I...) | 37 return @inbounds @inline lfa.f(I...) |
40 end | 38 end |
41 | 39 |
42 | 40 |
43 """ | 41 """ |
44 LazyElementwiseOperation{T,D,Op} <: LazyArray{T,D} | 42 LazyElementwiseOperation{T,D,Op} <: LazyArray{T,D} |
45 Struct allowing for lazy evaluation of elementwise operations on `AbstractArray`s. | 43 Struct allowing for lazy evaluation of element-wise operations on `AbstractArray`s. |
46 | 44 |
47 A `LazyElementwiseOperation` contains two arrays together with an operation. | 45 A `LazyElementwiseOperation` contains two arrays together with an operation. |
48 The operations are carried out when the `LazyElementwiseOperation` is indexed. | 46 The operations are carried out when the `LazyElementwiseOperation` is indexed. |
49 """ | 47 """ |
50 struct LazyElementwiseOperation{T,D,Op,T1<:AbstractArray{T,D},T2<:AbstractArray{T,D}} <: LazyArray{T,D} | 48 struct LazyElementwiseOperation{T,D,Op,T1<:AbstractArray{T,D},T2<:AbstractArray{T,D}} <: LazyArray{T,D} |
59 end | 57 end |
60 | 58 |
61 end | 59 end |
62 LazyElementwiseOperation{T,D,Op}(a::AbstractArray{T,D},b::T) where {T,D,Op} = LazyElementwiseOperation{T,D,Op}(a, LazyConstantArray(b, size(a))) | 60 LazyElementwiseOperation{T,D,Op}(a::AbstractArray{T,D},b::T) where {T,D,Op} = LazyElementwiseOperation{T,D,Op}(a, LazyConstantArray(b, size(a))) |
63 LazyElementwiseOperation{T,D,Op}(a::T,b::AbstractArray{T,D}) where {T,D,Op} = LazyElementwiseOperation{T,D,Op}(LazyConstantArray(a, size(b)), b) | 61 LazyElementwiseOperation{T,D,Op}(a::T,b::AbstractArray{T,D}) where {T,D,Op} = LazyElementwiseOperation{T,D,Op}(LazyConstantArray(a, size(b)), b) |
64 # TODO: Move Op to be the first parameter? Compare to Binary operations | |
65 | 62 |
66 Base.size(v::LazyElementwiseOperation) = size(v.a) | 63 Base.size(v::LazyElementwiseOperation) = size(v.a) |
67 | 64 |
68 evaluate(leo::LazyElementwiseOperation{T,D,:+}, I::Vararg{Int,D}) where {T,D} = leo.a[I...] + leo.b[I...] | 65 Base.@propagate_inbounds evaluate(leo::LazyElementwiseOperation{T,D,:+}, I::Vararg{Int,D}) where {T,D} = leo.a[I...] + leo.b[I...] |
69 evaluate(leo::LazyElementwiseOperation{T,D,:-}, I::Vararg{Int,D}) where {T,D} = leo.a[I...] - leo.b[I...] | 66 Base.@propagate_inbounds evaluate(leo::LazyElementwiseOperation{T,D,:-}, I::Vararg{Int,D}) where {T,D} = leo.a[I...] - leo.b[I...] |
70 evaluate(leo::LazyElementwiseOperation{T,D,:*}, I::Vararg{Int,D}) where {T,D} = leo.a[I...] * leo.b[I...] | 67 Base.@propagate_inbounds evaluate(leo::LazyElementwiseOperation{T,D,:*}, I::Vararg{Int,D}) where {T,D} = leo.a[I...] * leo.b[I...] |
71 evaluate(leo::LazyElementwiseOperation{T,D,:/}, I::Vararg{Int,D}) where {T,D} = leo.a[I...] / leo.b[I...] | 68 Base.@propagate_inbounds evaluate(leo::LazyElementwiseOperation{T,D,:/}, I::Vararg{Int,D}) where {T,D} = leo.a[I...] / leo.b[I...] |
72 | 69 |
73 # TODO: Make sure boundschecking is done properly and that the lenght of the vectors are equal | 70 function Base.getindex(leo::LazyElementwiseOperation{T,D}, I::Vararg{Int,D}) where {T,D} |
74 # NOTE: Boundschecking in getindex functions now assumes that the size of the | 71 @boundscheck checkbounds(leo.a, I...) |
75 # vectors in the LazyElementwiseOperation are the same size. If we remove the | 72 return @inbounds evaluate(leo, I...) |
76 # size assertion in the constructor we might have to handle | |
77 # boundschecking differently. | |
78 Base.@propagate_inbounds @inline function Base.getindex(leo::LazyElementwiseOperation{T,D}, I::Vararg{Int,D}) where {T,D} | |
79 @boundscheck if !checkbounds(Bool, leo.a, I...) | |
80 throw(BoundsError([leo], I...)) | |
81 end | |
82 return evaluate(leo, I...) | |
83 end | 73 end |
84 | 74 |
85 # Define lazy operations for AbstractArrays. Operations constructs a LazyElementwiseOperation which | 75 # Define lazy operations for AbstractArrays. Operations constructs a LazyElementwiseOperation which |
86 # can later be indexed into. Lazy operations are denoted by the usual operator followed by a tilde | 76 # can later be indexed into. Lazy operations are denoted by the usual operator followed by a tilde |
87 Base.@propagate_inbounds +̃(a::AbstractArray{T,D}, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:+}(a,b) | 77 Base.@propagate_inbounds +̃(a::AbstractArray{T,D}, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:+}(a,b) |
99 Base.@propagate_inbounds *̃(a::T, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:*}(a,b) | 89 Base.@propagate_inbounds *̃(a::T, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:*}(a,b) |
100 Base.@propagate_inbounds /̃(a::T, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:/}(a,b) | 90 Base.@propagate_inbounds /̃(a::T, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:/}(a,b) |
101 | 91 |
102 | 92 |
103 | 93 |
104 # NOTE: Är det knas att vi har till exempel * istället för .* ?? | 94 # Overload +,-,*,/ for LazyArrays |
105 # Oklart om det ens går att lösa.. | 95 # Element wise operation for `*` and `/` are not overloaded for due to conflicts with the behavior |
96 # of regular `*` and `/` for AbstractArrays. Use tilde versions instead. | |
106 Base.@propagate_inbounds Base.:+(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b | 97 Base.@propagate_inbounds Base.:+(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b |
98 Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b | |
99 | |
107 Base.@propagate_inbounds Base.:+(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a +̃ b | 100 Base.@propagate_inbounds Base.:+(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a +̃ b |
101 Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a -̃ b | |
102 | |
108 Base.@propagate_inbounds Base.:+(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b | 103 Base.@propagate_inbounds Base.:+(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a +̃ b |
109 | |
110 Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b | |
111 Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::AbstractArray{T,D}) where {T,D} = a -̃ b | |
112 Base.@propagate_inbounds Base.:-(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b | 104 Base.@propagate_inbounds Base.:-(a::AbstractArray{T,D}, b::LazyArray{T,D}) where {T,D} = a -̃ b |
113 | 105 |
114 # Element wise operation for `*` and `\` are not overloaded due to conflicts with the behavior | 106 Base.@propagate_inbounds Base.:+(a::LazyArray{T,D}, b::T) where {T,D} = a +̃ b |
115 # of regular `*` and `/` for AbstractArrays. Use tilde versions instead. | 107 Base.@propagate_inbounds Base.:-(a::LazyArray{T,D}, b::T) where {T,D} = a -̃ b |
116 | 108 |
117 export +̃, -̃, *̃, /̃ | 109 Base.@propagate_inbounds Base.:+(a::T, b::LazyArray{T,D}) where {T,D} = a +̃ b |
110 Base.@propagate_inbounds Base.:-(a::T, b::LazyArray{T,D}) where {T,D} = a -̃ b |