Mercurial > repos > public > sbplib_julia
comparison src/LazyTensors/lazy_array.jl @ 371:241bd2512c20 feature/lazy_function
Add a LazyFunctionArray that evaluates a function for each index.
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 30 Sep 2020 19:48:17 +0200 |
parents | 01b851161018 |
children | 33c360c3c6bc |
comparison
equal
deleted
inserted
replaced
370:8e55dee6a1a1 | 371:241bd2512c20 |
---|---|
13 size::NTuple{D,Int} | 13 size::NTuple{D,Int} |
14 end | 14 end |
15 | 15 |
16 Base.size(lca::LazyConstantArray) = lca.size | 16 Base.size(lca::LazyConstantArray) = lca.size |
17 Base.getindex(lca::LazyConstantArray{T,D}, I::Vararg{Int,D}) where {T,D} = lca.val | 17 Base.getindex(lca::LazyConstantArray{T,D}, I::Vararg{Int,D}) where {T,D} = lca.val |
18 | |
19 """ | |
20 LazyFunctionArray{F<:Function,T, D} <: LazyArray{T,D} | |
21 | |
22 A lazy array where each element is defined by a function f(i,j,...) | |
23 """ | |
24 struct LazyFunctionArray{F<:Function,T, D} <: LazyArray{T,D} | |
25 f::F | |
26 size::NTuple{D,Int} | |
27 end | |
28 export LazyFunctionArray | |
29 | |
30 function LazyFunctionArray(f::F, size::NTuple{D,Int}) where {F<:Function,D} | |
31 T = typeof(f(ones(D)...)) | |
32 return LazyFunctionArray{F,T,D}(f,size) | |
33 end | |
34 | |
35 Base.size(lfa::LazyFunctionArray) = lfa.size | |
36 function Base.getindex(lfa::LazyFunctionArray{F,T,D}, I::Vararg{Int,D}) where {F,T,D} | |
37 @boundscheck checkbounds(lfa, I...) | |
38 return lfa.f(I...) | |
39 end | |
40 | |
18 | 41 |
19 """ | 42 """ |
20 LazyElementwiseOperation{T,D,Op} <: LazyArray{T,D} | 43 LazyElementwiseOperation{T,D,Op} <: LazyArray{T,D} |
21 Struct allowing for lazy evaluation of elementwise operations on AbstractArrays. | 44 Struct allowing for lazy evaluation of elementwise operations on AbstractArrays. |
22 | 45 |