Mercurial > repos > public > sbplib_julia
diff src/LazyTensors/lazy_array.jl @ 374:99296cbb7bcd
Merge feature/lazy_function
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 30 Sep 2020 21:08:03 +0200 |
parents | 33c360c3c6bc |
children | f65809a26a17 |
line wrap: on
line diff
--- a/src/LazyTensors/lazy_array.jl Mon Sep 28 22:56:54 2020 +0200 +++ b/src/LazyTensors/lazy_array.jl Wed Sep 30 21:08:03 2020 +0200 @@ -17,6 +17,30 @@ Base.getindex(lca::LazyConstantArray{T,D}, I::Vararg{Int,D}) where {T,D} = lca.val """ + LazyFunctionArray{F<:Function,T, D} <: LazyArray{T,D} + +A lazy array where each element is defined by a function f(i,j,...) +""" +struct LazyFunctionArray{F<:Function,T, D} <: LazyArray{T,D} + f::F + size::NTuple{D,Int} +end +export LazyFunctionArray + +function LazyFunctionArray(f::F, size::NTuple{D,Int}) where {F<:Function,D} + T = typeof(f(ones(D)...)) + return LazyFunctionArray{F,T,D}(f,size) +end + +Base.size(lfa::LazyFunctionArray) = lfa.size + +function Base.getindex(lfa::LazyFunctionArray{F,T,D}, I::Vararg{Int,D}) where {F,T,D} + @boundscheck checkbounds(lfa, I...) + return lfa.f(I...) +end + + +""" LazyElementwiseOperation{T,D,Op} <: LazyArray{T,D} Struct allowing for lazy evaluation of elementwise operations on AbstractArrays.