diff 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
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 19:48:17 2020 +0200
@@ -17,6 +17,29 @@
 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.