Skip to content

Commit

Permalink
added a Jacobian test using Matlab's gradient function.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathantompson committed Mar 17, 2015
1 parent ba046c0 commit deeef26
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
8 changes: 8 additions & 0 deletions compile_mex_unix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# Jonathan Tompson 3/16/2015

echo compiling the mex files...
matlab -nodisplay -nosplash -nodesktop -r "run('compile_mex.m'); exit();"

echo 'All done!'
8 changes: 8 additions & 0 deletions ndgrid_normalized.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function [ X, Y, xstep, ystep ] = ndgrid_normalized( xdim, ydim )
[v, u] = ndgrid(1:ydim, 1:xdim);
xstep = 1 / (0.5 * (xdim - 1));
ystep = 1 / (0.5 * (ydim - 1));
X = (u - 1) .* xstep - 1; % [-1, 1]
Y = (v - 1) .* ystep - 1; % [-1, 1]
end

38 changes: 19 additions & 19 deletions test_mex.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@

ydim = 512;
xdim = 511;
frequency = 1;

% 2D Grid
[V, U] = ndgrid(1:ydim, 1:xdim);
X = 2 * (U - 1) ./ (xdim - 1) - 1; % [-1, 1]
Y = 2 * (V - 1) ./ (ydim - 1) - 1; % [-1, 1]
X = X * frequency;
Y = Y * frequency;
[ X, Y, xstep, ystep ] = ndgrid_normalized( xdim, ydim );

figure;
noise = Perlin2D(X, Y);
Expand All @@ -33,16 +28,22 @@

% Check the gradient using finite differences (central)
epsilon = 1e-6;
[pos, posGrad] = Perlin2DDeriv(X + epsilon, Y);
[neg, negGrad] = Perlin2DDeriv(X - epsilon, Y);
[pos, ~] = Perlin2DDeriv(X + epsilon, Y);
[neg, ~] = Perlin2DDeriv(X - epsilon, Y);
dx = (pos - neg) / (2 * epsilon);
[pos, posGrad] = Perlin2DDeriv(X, Y + epsilon);
[neg, negGrad] = Perlin2DDeriv(X, Y - epsilon);
[pos, ~] = Perlin2DDeriv(X, Y + epsilon);
[neg, ~] = Perlin2DDeriv(X, Y - epsilon);
dy = (pos - neg) / (2 * epsilon);
gradNoiseFEM = permute(cat(3, dx, dy), [3 1 2]);
err = abs(gradNoiseFEM - gradNoise);
assert(max(err(:)) < 1e-8, 'FEM derivative does not match!');

% Also check the gradient using Matla's approximation
[dx, dy] = gradient(noise_deriv, xstep, ystep);
gradNoiseFEM = permute(cat(3, dx, dy), [3 1 2]);
err = abs(gradNoiseFEM - gradNoise);
assert(max(err(:)) < 1e-3, 'FEM derivative does not match!');

% 3D Grid
xdim = 63;
ydim = 64;
Expand All @@ -51,9 +52,6 @@
X = 2 * (U - 1) ./ (xdim - 1) - 1; % [-1, 1]
Y = 2 * (V - 1) ./ (ydim - 1) - 1; % [-1, 1]
Z = 2 * (D - 1) ./ (zdim - 1) - 1; % [-1, 1]
X = X * frequency;
Y = Y * frequency;
Z = Z * frequency;
figure;
noise = Perlin3D(X, Y, Z);
plot_3D_scalar(noise);
Expand All @@ -73,14 +71,14 @@

% Check the gradient using finite differences (central)
epsilon = 1e-6;
[pos, posGrad] = Perlin3DDeriv(X + epsilon, Y, Z);
[neg, negGrad] = Perlin3DDeriv(X - epsilon, Y, Z);
[pos, ~] = Perlin3DDeriv(X + epsilon, Y, Z);
[neg, ~] = Perlin3DDeriv(X - epsilon, Y, Z);
dx = (pos - neg) / (2 * epsilon);
[pos, posGrad] = Perlin3DDeriv(X, Y + epsilon, Z);
[neg, negGrad] = Perlin3DDeriv(X, Y - epsilon, Z);
[pos, ~] = Perlin3DDeriv(X, Y + epsilon, Z);
[neg, ~] = Perlin3DDeriv(X, Y - epsilon, Z);
dy = (pos - neg) / (2 * epsilon);
[pos, posGrad] = Perlin3DDeriv(X, Y, Z + epsilon);
[neg, negGrad] = Perlin3DDeriv(X, Y, Z - epsilon);
[pos, ~] = Perlin3DDeriv(X, Y, Z + epsilon);
[neg, ~] = Perlin3DDeriv(X, Y, Z - epsilon);
dz = (pos - neg) / (2 * epsilon);
gradNoiseFEM = permute(cat(4, dx, dy, dz), [4 1 2 3]);
err = abs(gradNoiseFEM - gradNoise);
Expand All @@ -96,3 +94,5 @@
plot_3D_scalar(noise);
title(['Perlin4D w=', num2str(i)]);
end

disp('Tests pass!');

0 comments on commit deeef26

Please sign in to comment.