1% Testing sparse operators. 2 3clear d 4f = randn(21,30,10); 5 6S = sparse_diff(size(f),1); 7f1 = f(2:end,:,:) - f(1:end-1,:,:); 8f2 = S*f(:); 9d(1) = max(abs(f1(:) - f2(:))); 10 11S = sparse_diff(size(f),2); 12f1 = f(:,2:end,:) - f(:,1:end-1,:); 13f2 = S*f(:); 14d(2) = max(abs(f1(:) - f2(:))); 15 16 17S = sparse_diff(size(f),3); 18f1 = f(:,:,2:end) - f(:,:,1:end-1); 19f2 = S*f(:); 20d(3) = max(abs(f1(:) - f2(:))); 21 22 23% cyclic 24 25% dim 1 cyclic 26fc = cat(1,f,f(1,:,:)); 27dfc = fc(2:end,:,:) - fc(1:end-1,:,:); 28 29S = sparse_diff(size(f),1,true); 30f2 = S*f(:); 31d(4) = max(abs(dfc(:) - f2(:))); 32 33 34% dim 2 cyclic 35fc = cat(2,f,f(:,1,:)); 36f1 = fc(:,2:end,:) - fc(:,1:end-1,:); 37 38S = sparse_diff(size(f),2,true); 39f2 = S*f(:); 40d(5) = max(abs(f1(:) - f2(:))); 41 42 43% stagger 44 45S = sparse_stagger(size(f),1); 46f1 = (f(2:end,:,:) + f(1:end-1,:,:))/2; 47f2 = S*f(:); 48d(end+1) = max(abs(f1(:) - f2(:))); 49 50 51% dim 1 cyclic 52fc = cat(1,f,f(1,:,:)); 53f1 = (fc(2:end,:,:) + fc(1:end-1,:,:))/2; 54 55S = sparse_stagger(size(f),1,true); 56f2 = S*f(:); 57d(end+1) = max(abs(f1(:) - f2(:))); 58 59 60if any(d > 1e-6) 61 error('unexpected large difference with reference field'); 62end 63 64fprintf('(max difference=%g) ',max(d)); 65 66 67 68% Copyright (C) 2014 Alexander Barth <a.barth@ulg.ac.be> 69% 70% This program is free software; you can redistribute it and/or modify it under 71% the terms of the GNU General Public License as published by the Free Software 72% Foundation; either version 2 of the License, or (at your option) any later 73% version. 74% 75% This program is distributed in the hope that it will be useful, but WITHOUT 76% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 77% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 78% details. 79% 80% You should have received a copy of the GNU General Public License along with 81% this program; if not, see <http://www.gnu.org/licenses/>. 82