How can I create a function with several summations and Kronecker delta symbol in two dimension?

7 views (last 30 days)
I have tried to create a several summations function including Kronecker delta symbol.
The function is the follwing.
where
I am working with n=2 which is two dimensional space and theta=-1/2.
Here alpha and beta are two dimensional point. For example, alpha=(2,0), beta=(2,0) and ei's are standard unit normal vector in two dimension.
I have tried to create the Kronecker delta symbol in two dimension, but everything didn't work.
I would really appreciate it, if you can help me.
  4 Comments
Bruno Luong
Bruno Luong on 21 Apr 2022
Then your notation is not standard: the parenthesis should not be a subsribe but in the same level than the delta symbol.

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 21 Apr 2022
Edited: Bruno Luong on 5 May 2022
Here is the code, I do not check careful though
% Dummy data
alpha = [0 1;
1, 0;
1, 1;
2, 0;
0, 2];
beta = [1, 1;
2, 0;
0, 2];
theta = [1; 2; 3; 4];
n = 2;
[i, j] = ndgrid(1:n);
ij = [i(:) j(:)];
% Tensor calculation start here
m = size(alpha,1);
n = size(beta,1);
p = size(theta,1);
Res = zeros(m,n,p);
for k=1:p
Pa = GetPTesnsor(alpha, ij, theta(k));
Pb = GetPTesnsor(beta, ij, theta(k));
T = tensorprod(Pa,Pb,[3 4],[3 4]);
Res(:,:,k) = reshape(T,[m n]);
end
Res
Res =
Res(:,:,1) = 0 0 0 0 0 0 2 0 0 0 5 4 0 4 5 Res(:,:,2) = 0 0 0 0 0 0 2 0 0 0 13 12 0 12 13 Res(:,:,3) = 0 0 0 0 0 0 2 0 0 0 25 24 0 24 25 Res(:,:,4) = 0 0 0 0 0 0 2 0 0 0 41 40 0 40 41
function P = GetPTesnsor(alpha, ij, theta)
ALPHA = reshape(alpha,[size(alpha),1,1,1,1]);
IJ = reshape(ij,[1,1,size(ij),1,1]);
I = IJ(1,1,:,1,1,1);
J = IJ(1,1,:,2,1,1);
THETA = reshape(theta,[1,1,1,1,size(theta)]);
n = max(ij,[],'all');
delta = @(x1,x2) x1==x2;
k = reshape((1:n),[1 1 n]);
P = deltav(ALPHA,getei(I,n)+getei(J,n)) + ...
THETA.*delta(I,J).*sum(deltav(ALPHA,2*getei(k,n)),3);
end
function ei = getei(i,n)
ei = accumarray([(1:length(i))' i(:)],1,[length(i) n]);
szei = [size(i) n];
ei = reshape(ei, szei);
end
function x = deltav(a, b)
sza = size(a);
szb = size(b);
szar = [sza(1:end-1) ones(1,length(szb)-length(sza)), sza(end)];
ar = reshape(a, szar);
x = all(ar == b, length(szar));
end
  3 Comments
matlab beginner
matlab beginner on 4 May 2022
Edited: Bruno Luong on 5 May 2022
Hi Bruno,
I really apprecaite this code perfectly worked!!!
I have tried this to expand a little bit but my code is not working now.
Could you please look at my code if you don't mind?
Here is the code:
% Dummy data
format rat
alpha = [2 0 0 ;
0 2 0 ;
0 0 2 ;
1 1 0 ;
1 0 1 ;
0 1 1];
beta = [2 0 0 ;
0 2 0 ;
0 0 2 ;
1 1 0 ;
1 0 1 ;
0 1 1];
n = 3;
theta = -1/4;
c=1/(1+2*theta+n*theta.^2);
[i, j, l] = ndgrid(1:n);
ijl = [i(:) j(:) l(:)];
% Tensor calculation start here
m = size(alpha,1);
n = size(beta,1);
p = size(theta,1);
Res = zeros(m,n,p);
for k=1:p
Pa = GetPTesnsor(alpha, ijl, theta(k));
Pb = GetPTesnsor(beta, ijl, theta(k));
T = tensorprod(Pa,Pb,[3 4],[3 4]);
Res(:,:,k) = c*reshape(T,[m n]);
end
Res
function P = GetPTesnsor(alpha, ijl, theta)
ALPHA = reshape(alpha,[size(alpha),1,1,1,1]);
IJL = reshape(ijl,[1,1,size(ijl),1,1]);
I = IJL(1,1,:,1,1,1);
J = IJL(1,1,:,2,1,1);
L = IJL(1,1,:,3,1,1);
THETA = reshape(theta,[1,1,1,1,size(theta)]);
n = max(ijl,[],'all');
delta = @(x1,x2) x1==x2;
k = reshape((1:n),[1 1 n]);
P = deltav(ALPHA,getei(I,n)+getei(J,n)+getei(L,n)) + ...
THETA.*delta(I,J).*sum(deltav(ALPHA,2*getei(k,n)+getei(L,n)),3)+ ...
THETA.*delta(J,L).*sum(deltav(ALPHA,2*getei(k,n)+getei(I,n)),3)+ ...
THETA.*delta(I,L).*sum(deltav(ALPHA,2*getei(k,n)+getei(J,n)),3);
end
function ei = getei(i,n)
ei = accumarray([(1:length(i))' i(:)],1,[length(i) n]);
szei = [size(i) n];
ei = reshape(ei, szei);
end
function x = deltav(a, b)
sza = size(a);
szb = size(b);
szar = [sza(1:end-1) ones(1,length(szb)-length(sza)), sza(end)];
ar = reshape(a, szar);
x = all(ar == b, length(szar));
end
Bruno Luong
Bruno Luong on 5 May 2022
Try this change
function P = GetPTesnsor(alpha, ijl, theta)
ALPHA = reshape(alpha,[size(alpha),1,1,1,1]);
IJL = reshape(ijl,[1,1,size(ijl),1,1]);
I = IJL(1,1,:,1,1,1);
J = IJL(1,1,:,2,1,1);
L = IJL(1,1,:,3,1,1);
THETA = reshape(theta,[1,1,1,1,size(theta)]);
n = max(ijl,[],'all');
delta = @(x1,x2) x1==x2;
k = reshape((1:n),[1 1 1 1 n]); % change
P = deltav(ALPHA,getei(I,n)+getei(J,n)+getei(L,n)) + ...
THETA.*delta(I,J).*sum(deltav(ALPHA,2*getei(k,n)+getei(L,n)),5)+ ...
THETA.*delta(J,L).*sum(deltav(ALPHA,2*getei(k,n)+getei(I,n)),5)+ ...
THETA.*delta(I,L).*sum(deltav(ALPHA,2*getei(k,n)+getei(J,n)),5); % change
end

Sign in to comment.

More Answers (2)

Sam Chak
Sam Chak on 20 Apr 2022
  1 Comment
matlab beginner
matlab beginner on 20 Apr 2022
Thank you very much for your comment.
Yes, I did try it but I have no idea how to apply it in two dimension. For example, delta_{(2,0),(2,1)} like this.
I first tried to use delta_{(2,2)} delta_{(0,1)} but this doesn't work when I work with index like delta_{alpha,(e_{i}+e_{j})}.

Sign in to comment.


Walter Roberson
Walter Roberson on 20 Apr 2022
Edited: Walter Roberson on 22 Apr 2022
Your expressions involve delta subscript something that is a multiplication, rather than delta subscript something, all multiplied by something. That means that the entire subscript is the arguments to the delta function.
The below defines a multidimensional delta
in short it is 1 if and only if all of the arguments are 0.
But your second arguments in each case are vectors in 3 space (not two space like you said) and it is not obvious that you can compare a vector such as e_j to a scalar 0. The definition does not say anything about comparing magnitude, only about comparing values.
You could potentially rewrite your expressions in terms of magnitude in each of 4 dimensions (scalar, i, j, k) and then it might make sense to take the delta... but I am not convinced that your expressions are meaningful in their current form.
  7 Comments
Bruno Luong
Bruno Luong on 22 Apr 2022
Edited: Bruno Luong on 22 Apr 2022
@Walter Roberson " which would be defined as 1 if alpha == 0 and ei+ej== 0. "
Not what I read, according to Wolfram link
"KroneckerDelta[n1,n2,] gives the Kronecker delta , equal to 1 if all the ni are equal, and 0 otherwise. "
it is equal to 1 if alpha = ei+ej.
Anyway OP has stated exactly what he wants.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!