Mercurial > repos > public > sbplib_julia
comparison src/LazyTensors/lazy_array.jl @ 1049:3bb94ce74697 feature/variable_derivatives
Merge default
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 23 Mar 2022 12:54:45 +0100 |
parents | 4dd3c2312d9e |
children | 6104db60b7a3 |
comparison
equal
deleted
inserted
replaced
1048:86aa69ad3304 | 1049:3bb94ce74697 |
---|---|
34 | 34 |
35 Base.size(lfa::LazyFunctionArray) = lfa.size | 35 Base.size(lfa::LazyFunctionArray) = lfa.size |
36 | 36 |
37 function Base.getindex(lfa::LazyFunctionArray{F,T,D}, I::Vararg{Int,D}) where {F,T,D} | 37 function Base.getindex(lfa::LazyFunctionArray{F,T,D}, I::Vararg{Int,D}) where {F,T,D} |
38 @boundscheck checkbounds(lfa, I...) | 38 @boundscheck checkbounds(lfa, I...) |
39 return lfa.f(I...) | 39 return @inbounds lfa.f(I...) |
40 end | 40 end |
41 | 41 |
42 | 42 |
43 """ | 43 """ |
44 LazyElementwiseOperation{T,D,Op} <: LazyArray{T,D} | 44 LazyElementwiseOperation{T,D,Op} <: LazyArray{T,D} |
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 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...] | 68 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...] | 69 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...] | 70 Base.@propagate_inbounds evaluate(leo::LazyElementwiseOperation{T,D,:/}, I::Vararg{Int,D}) where {T,D} = 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 | 74 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 | 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 |
87 Base.@propagate_inbounds +̃(a::AbstractArray{T,D}, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:+}(a,b) | 79 Base.@propagate_inbounds +̃(a::AbstractArray{T,D}, b::AbstractArray{T,D}) where {T,D} = LazyElementwiseOperation{T,D,:+}(a,b) |