/
compute_tensorial_operator.m
58 lines (44 loc) · 1.33 KB
/
compute_tensorial_operator.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function M = compute_tensorial_operator(T,n,p)
% compute_tensorial_operator - compute a 2D convolution operator that act in a tensorial way
%
% M is a n²xn² matrix that represent an operator
% M : (R^n x R^p) ---> (R^n x R^p)
%
% Example for the discretized Laplacian :
%
% T = [0 -1 0; -1 4 -1; 0 -1 0];
% M = compute_tensorial_operator(T,5);
%
% Copyright (c) 2005 Gabriel Peyré
if nargin<3
p = n;
end
k = size(T,1);
if size(T,2)~=k || mod(k,2)~=1
errro('T should be a square matrix of odd size');
end
k = (k-1)/2;
[dY,dX] = meshgrid(-k:k,-k:k);
[Y,X] = meshgrid(1:p,1:n);
M = sparse(n*p,n*p);
for s=1:(2*k+1)^2
dx = dX(s);
dy = dY(s);
v = T(s);
Z = X(:) + n*( Y(:)-1 );
Z1 = X(:)+dx + n*( Y(:)+dy-1 );
% remove out of bound points
I = find( X(:)+dx>n | X(:)+dx<1 | Y(:)+dy>n | Y(:)+dy<1 );
Z1(I) = []; Z(I) = [];
M = matrix_sampling_set(M, v, [Z';Z1']);
if 1
I = find( (X(:)+dx>n | X(:)+dx<1) & Y(:)+dy<=n & Y(:)+dy>0 );
Z = X(I) + n*( Y(I)-1 );
Z1 = X(I)-dx + n*( Y(I)+dy-1 );
M = matrix_sampling_add(M, v, [Z';Z1']);
I = find( (Y(:)+dy>n | Y(:)+dy<1) & X(:)+dx<=n & X(:)+dx>0 );
Z = X(I) + n*( Y(I)-1 );
Z1 = X(I)+dx + n*( Y(I)-dy-1 );
M = matrix_sampling_add(M, v, [Z';Z1']);
end
end