changeset 1146:31041ef8092a refactor/grids

Specialize evalOn for EquidistantGrid to return a LazyArray
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 19 Oct 2022 23:23:36 +0200
parents 4da66d6d7bed
children bda0afcf8e52
files src/Grids/Grids.jl src/Grids/equidistant_grid.jl test/Grids/equidistant_grid_test.jl
diffstat 3 files changed, 16 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/Grids/Grids.jl	Wed Oct 19 23:14:00 2022 +0200
+++ b/src/Grids/Grids.jl	Wed Oct 19 23:23:36 2022 +0200
@@ -1,6 +1,7 @@
 module Grids
 
 using Sbplib.RegionIndices
+using Sbplib.LazyTensors
 
 # Grid
 export Grid
--- a/src/Grids/equidistant_grid.jl	Wed Oct 19 23:14:00 2022 +0200
+++ b/src/Grids/equidistant_grid.jl	Wed Oct 19 23:23:36 2022 +0200
@@ -1,4 +1,3 @@
-
 """
     EquidistantGrid{Dim,T<:Real} <: Grid
 
@@ -72,7 +71,11 @@
 # TBD: Can this method be removed if `EquidistantGrid` is an AbstractArray?
 
 
+function evalOn(grid::EquidistantGrid, f::Function)
+    F(I...) = f(grid[I...]...)
 
+    return LazyFunctionArray(F, size(grid))
+end
 
 """
     spacing(grid::EquidistantGrid)
--- a/test/Grids/equidistant_grid_test.jl	Wed Oct 19 23:14:00 2022 +0200
+++ b/test/Grids/equidistant_grid_test.jl	Wed Oct 19 23:23:36 2022 +0200
@@ -1,6 +1,7 @@
 using Sbplib.Grids
 using Test
 using Sbplib.RegionIndices
+using Sbplib.LazyTensors
 
 
 @testset "EquidistantGrid" begin
@@ -56,6 +57,16 @@
         @test g[CartesianIndex(1,3)] == (-1.0,7.11)
     end
 
+    @testset "evalOn" begin
+        g = EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))
+
+        @test evalOn(g, (x,y) -> 0.) isa LazyArray
+        @test evalOn(g, (x,y) -> 0.) == fill(0., (5,3))
+
+        f(x,y) = sin(x)*cos(y)
+        @test evalOn(g, f) == map(p->f(p...), points(g))
+    end
+
     @testset "restrict" begin
         g = EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))
         @test restrict(g, 1) == EquidistantGrid(5,0.0,2.0)