Added central differences fold

Mon, 18 Nov 2019 18:21:00 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Mon, 18 Nov 2019 18:21:00 -0500
changeset 4
5c0f579a5d0f
parent 3
957b62860ab4
child 5
29b38780d52b

Added central differences fold

src/Gradient.jl file | annotate | diff | comparison | revisions
--- a/src/Gradient.jl	Mon Nov 18 17:37:23 2019 -0500
+++ b/src/Gradient.jl	Mon Nov 18 18:21:00 2019 -0500
@@ -11,7 +11,7 @@
 export ∇₂!, ∇₂ᵀ!, ∇₂fold!,
        ∇₂_norm₂₂_est, ∇₂_norm₂₂_est²,
        ∇₂_norm₂∞_est, ∇₂_norm₂∞_est²,
-       ∇₂c!,
+       ∇₂c!, ∇₂cfold!,
        ∇₃!, ∇₃ᵀ!,
        vec∇₃!, vec∇₃ᵀ!
 
@@ -129,6 +129,25 @@
     end
 end
 
+@inline function ∇₂cfold!(f!::Function, u, state)
+    n, m = size(u)
+    g! = (state, pt) -> begin
+        (i, j) = pt
+        g = @inbounds [(u[i+1, j]-u[i-1, j])/2, (u[i, j+1]-u[i, j-1])/2]
+        return f!(g, state, pt)
+    end
+    gb! = (state, pt) -> begin
+        (i, j) = pt
+        g = @inbounds [(u[min(i+1,n), j]-u[max(i-1,1), j])/2,
+                       (u[i, min(j+1,m)]-u[i, max(j-1,1)])/2]
+        return f!(g, state, pt)
+    end
+    return imfold₂′!(gb!, gb!, gb!,
+                     gb!, g!,  gb!,
+                     gb!, gb!, gb!,
+                     size(u, 1), size(u, 2), state)
+end
+
 #########################
 # 3D forward differences
 #########################

mercurial