Mercurial > repos > public > sbplib_julia
comparison src/LazyTensors/lazy_array.jl @ 1023:52f07c77299d refactor/sbpoperators/inflation
Merge refactor/lazy_tensors
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Mon, 21 Mar 2022 09:51:07 +0100 |
parents | 7fd37aab84fe |
children | 4dd3c2312d9e |
comparison
equal
deleted
inserted
replaced
1022:bbbc31953367 | 1023:52f07c77299d |
---|---|
59 end | 59 end |
60 | 60 |
61 end | 61 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))) | 62 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) | 63 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 | 64 |
66 Base.size(v::LazyElementwiseOperation) = size(v.a) | 65 Base.size(v::LazyElementwiseOperation) = size(v.a) |
67 | 66 |
68 evaluate(leo::LazyElementwiseOperation{T,D,:+}, I::Vararg{Int,D}) where {T,D} = leo.a[I...] + leo.b[I...] | 67 evaluate(leo::LazyElementwiseOperation{T,D,:+}, I::Vararg{Int,D}) where {T,D} = @inbounds 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...] | 68 evaluate(leo::LazyElementwiseOperation{T,D,:-}, I::Vararg{Int,D}) where {T,D} = @inbounds 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...] | 69 evaluate(leo::LazyElementwiseOperation{T,D,:*}, I::Vararg{Int,D}) where {T,D} = @inbounds 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...] | 70 evaluate(leo::LazyElementwiseOperation{T,D,:/}, I::Vararg{Int,D}) where {T,D} = @inbounds leo.a[I...] / leo.b[I...] |
72 | 71 |
73 # TODO: Make sure boundschecking is done properly and that the lenght of the vectors are equal | 72 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 | 73 @boundscheck checkbounds(leo.a, I...) |
75 # vectors in the LazyElementwiseOperation are the same size. If we remove the | |
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...) | 74 return evaluate(leo, I...) |
83 end | 75 end |
84 | 76 |
85 # Define lazy operations for AbstractArrays. Operations constructs a LazyElementwiseOperation which | 77 # 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 | 78 # can later be indexed into. Lazy operations are denoted by the usual operator followed by a tilde |