2

I try to convert this MATLAB code: From:

(useful to compute some prox of some functions):

case 3
    %% 3D field %%
    V          = cat(4, ...
                U.M{1}(1:end-1,:,:) + U.M{1}(2:end,:,:), ...
                U.M{2}(:,1:end-1,:) + U.M{2}(:,2:end,:), ...
                U.M{3}(:,:,1:end-1) + U.M{3}(:,:,2:end));
case 4
    %% 4D field %%
    V          = cat(5, ...
                U.M{1}(1:end-1,:,:,:) + U.M{1}(2:end,:,:,:), ...
                U.M{2}(:,1:end-1,:,:) + U.M{2}(:,2:end,:,:), ...
                U.M{3}(:,:,1:end-1,:) + U.M{3}(:,:,2:end,:),...
                U.M{4}(:,:,:,1:end-1) + U.M{4}(:,:,:,2:end));

"The dimension of U.M{1} is (N+1,N) while that of U.M{2} is (N,N+1) for 2D field". I would like to have the same behavior on Mathematica, without a switch case, on arbitrary dimension. I try to do some mix with Take, Drop, ... Without any success.

rank = 4;
size = 30;
baseDim = ConstantArray[size, rank];
U = Table[Array[Subscript[m, ##] &, ReplacePart[baseDim, k -> size + 1]], {k, 1, rank}];

The idea of the MATLAB code is to concatenate N sub-tensor by taking the begining of and the end of each dimension and sum them up.

Here the '4' stand for the dimension where I would like to catenate, that do the same job as "Join". This is the intuition behind the Mathematica code above. In pure Mathematica for a Tensor with Rank 4 above the special case code can look like:

Join[U[[1]][[2 ;; ,   ;; ,   ;; ,   ;;]] + U[[1]][[;; -2, ;;   , ;;   , ;;   ]],
     U[[2]][[  ;; , 2 ;; ,   ;; ,   ;;]] + U[[2]][[;;   , ;; -2, ;;   , ;;   ]],
     U[[3]][[  ;; ,   ;; , 2 ;; ,   ;;]] + U[[3]][[;;   , ;;   , ;; -2, ;;   ]],
     U[[4]][[  ;; ,   ;; ,   ;; , 2 ;;]] + U[[4]][[;;   , ;;   , ;;   , ;; -2]], 5]

[Edit] The solution based on the answer of @Henrik-Schumacher:

With[{all = ConstantArray[All, rank]},
    Table[U[[k]][[Sequence @@ ReplacePart[all, k -> 2 ;;]]] + U[[k]][[Sequence @@ ReplacePart[all, k -> ;; -2]]], {k, 1, rank}]
]
chkone
  • 145
  • 6
  • 1
    Since most people here don't know Matlab, perhaps you could explain what the constructions do? For example, what are U, U.M, and what does "cat" do? – bill s Oct 18 '18 at 00:47
  • you're right I update the message, remove the irrelevant U.M and add more explanation. – chkone Oct 18 '18 at 01:05
  • In addition, a concrete MWE is appreciated. – Αλέξανδρος Ζεγγ Oct 18 '18 at 03:55
  • The last Mathematica code is an example which is the working example, which I'd to generalize for arbitrary rank. – chkone Oct 18 '18 at 05:54
  • 1
    I am not convinced that the last code example returns what you want. Are you aware that you try to join arrays of different Dimensions? – Henrik Schumacher Oct 18 '18 at 06:11
  • @chkone Well, I mean a concrete tensor. – Αλέξανδρος Ζεγγ Oct 18 '18 at 06:15
  • 2
    Since you're dealing with tensor, are you aware that the Mathematica and MATLAB represents tensor in different way?: https://mathematica.stackexchange.com/q/10582/1871 – xzczd Oct 18 '18 at 07:11
  • Also, neither of your Mathematica code sample works, please double-check it. – xzczd Oct 18 '18 at 07:40
  • @HenrikSchumacher yes, that is what Matlab code do too. – chkone Oct 19 '18 at 03:47
  • @xzczd that is for memory storage, in my understanding for tensor indexing seem the same m[[a, b, c, d]] will point out the same number in Matlab m(a,b,c,d). This one work: tmp = Array[Subscript[m, ##] &, {3, 3, 3, 3}]; Join[tmp[[2 ;; , ;; , ;; , ;;]] + tmp[[;; -2, ;; , ;; , ;;]], tmp[[;; , 2 ;; , ;; , ;;]] + tmp[[;; , ;; -2, ;; , ;;]], tmp[[;; , ;; , 2 ;; , ;;]] + tmp[[;; , ;; , ;; -2, ;;]], tmp[[;; , ;; , ;; , 2 ;;]] + tmp[[;; , ;; , ;; , ;; -2]], 5] – chkone Oct 19 '18 at 03:51
  • The dimession give me: Dimensions /@ {tmp[[2;;,;;,;;,;;]] + tmp[[;;-2,;;,;;,;;]], tmp[[;;,2;;,;;,;;]] + tmp[[;;,;;-2,;;,;;]], tmp[[;;,;;,2;;,;;]] + tmp[[;;,;;,;;-2,;;]], tmp[[;;,;;,;;,2;;]] + tmp[[;;,;;,;;,;;-2]]} {{2, 3, 3, 3}, {3, 2, 3, 3}, {3, 3, 2, 3}, {3, 3, 3, 2}} And the join: Join[tmp[[2 ;;, ;; , ;; , ;;]] + tmp[[;; -2, ;; , ;; , ;;]], tmp[[;; , 2 ;;, ;; , ;;]] + tmp[[;; , ;; -2, ;; , ;;]], tmp[[;; , ;; , 2 ;;, ;;]] + tmp[[;; , ;; , ;; -2, ;;]], tmp[[;; , ;; , ;; , 2 ;;]] + tmp[[;; , ;; , ;; , ;; -2]], 5] // Dimensions {3, 3, 3, 3} – chkone Oct 19 '18 at 03:52
  • 1
    Then your understanding is wrong, and your new sample is not working, please look at the output carefully, it contains terms like 4 Subscript[m, 1, 1, 1, 1] + Subscript[m, 1, 1, 1, 2] + Subscript[m, 1, 1, 2, 1] + Subscript[m, 1, 2, 1, 1] + Subscript[m, 2, 1, 1, 1], which indicates the output is not the one you expect. This behavior can be simplified to the following: Join[{A[x]}, {A[x]}, 2], and your mistake will be even more obvious if you replace tmp with tmp = RandomReal[1, {3, 3, 3, 3}];. – xzczd Oct 19 '18 at 07:25
  • BTW, in v9.0.1 the sample will cause Join::headsd: Expression {A[x]} at position 1 is expected to have head List for all subexpressions through level 2. warning, I'm not sure when the change happens though. I've posted a separate question about this here. And, your MATLAB sample doesn't seem to be correct, either. The following sample will cause error: cat: dimension mismatch warning and fail in Octave 4.2.0: U=[1 2 3;4 5 6;7 8 9];V= cat(3, U(1:end-1,:) + U(2:end,:), U(:,1:end-1,:) + U(:,2:end)). – xzczd Oct 19 '18 at 11:15
  • Maybe you can edit your post to mention where you got the MATLAB code from? – J. M.'s missing motivation Oct 19 '18 at 11:54
  • 1
    @J.M.iscomputer-less sure it's from here: https://github.com/gpeyre/2013-SIIMS-ot-splitting/blob/master/code/toolbox/%40staggered/interp.m https://github.com/gpeyre/2013-SIIMS-ot-splitting/blob/master/code/toolbox/interp_adj.m – chkone Oct 19 '18 at 18:02
  • @xzczd indeed I didn't notice that. – chkone Oct 19 '18 at 18:18
  • -1 for posting the wrong MATLAB sample and a wrong Mathematica sample (the first one). You cannot simplify the U.M{1}, U.M{2}, etc. in the original code to a single U, Notice e.g. the dimension of U.M{1} is $(N+1, N)$ while that of U.M{2} is $(N, N+1)$ for 2D field. Please test your code first before posting it in the question. – xzczd Oct 20 '18 at 08:11
  • @xzczd thanks for your feedback – chkone Oct 21 '18 at 22:50
  • Your samples still involve multiple mistakes. Once again, please test your code before posting it in the question. – xzczd Oct 22 '18 at 08:18
  • I fix few typo issue, the code were tested. If you find something else let me know cheers. – chkone Oct 22 '18 at 13:56
  • Some of the U isn't modified in the MATLAB sample. 2. You've chosen rank=4 for the 2nd Mathematica sample while in the 1st sample you've defined rank=3.
  • – xzczd Oct 22 '18 at 14:12
  • Thanks fixed. I didn't test the Matlab code. I work mainly on Mathematica. – chkone Oct 22 '18 at 15:15
  • Downvote retracted. – xzczd Oct 22 '18 at 15:35