How can I create a function with several summations and Kronecker delta symbol in two dimension?
7 views (last 30 days)
Show older comments
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
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.
Accepted Answer
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
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
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
More Answers (2)
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
on 22 Apr 2022
Edited: Bruno Luong
on 22 Apr 2022
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.
Walter Roberson
on 22 Apr 2022
You are right, I overlooked part of the description in the link I posted.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!