1% Sparse operator shifting a field in a given dimension.
2%
3% function S = sparse_shift(sz1,m,cyclic)
4%
5% Sparse operator shifting a field in the dimension m. The field is a
6% "collapsed" matrix of the size sz1.
7%
8% Input:
9%   sz1: size of rhs
10%   m: dimension to shift
11%   cyclic: true if domain is cyclic along dimension m. False is the
12%     default value
13
14function S = sparse_shift(sz1,m,cyclic)
15
16if nargin == 2
17    cyclic = false;
18end
19
20n1 = prod(sz1);
21
22sz2 = sz1;
23
24if ~cyclic
25    sz2(m) = sz2(m)-1;
26end
27
28n2 = prod(sz2);
29
30n = length(sz1);
31
32vi = cell(1,n);
33for i=1:n
34    vi{i} = 1:sz2(i);
35end
36
37IJ = cell(n,1);
38
39[IJ{:}] = ndgrid(vi{:});
40
41for i=1:n
42    IJ{i} = IJ{i}(:);
43end
44
45L1 = [1:n2]';
46one = ones(size(L1));
47
48IJ{m} = IJ{m} + 1;
49
50if cyclic
51    IJ{m} = mod(IJ{m}-1,sz1(m))+1;
52end
53
54
55L2 = sub2ind(sz1,IJ{:});
56
57S = sparse(L1,L2,one,n2,n1);
58
59
60
61% Copyright (C) 2012 Alexander Barth <a.barth@ulg.ac.be>
62%
63% This program is free software; you can redistribute it and/or modify
64% it under the terms of the GNU General Public License as published by
65% the Free Software Foundation; either version 2 of the License, or
66% (at your option) any later version.
67%
68% This program is distributed in the hope that it will be useful,
69% but WITHOUT ANY WARRANTY; without even the implied warranty of
70% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
71% GNU General Public License for more details.
72%
73% You should have received a copy of the GNU General Public License
74% along with this program; If not, see <http://www.gnu.org/licenses/>.
75
76