# imposing conditions on an array within a function

Let's say I have the following function

ClearAll[f];
SetAttributes[f, HoldFirst];
f[S_, x_, y_, z_, Lx_, Ly_, Lz_] := (
S[[x, y, z]] = 1;
S[[x+1, y, z]] = 2;
S[[x, y+1, z]] = 3;
S[[x, y, z+1]] = 4;
)


Sometimes I need to have a modified function which has an extra condition that when $$z=L_z$$ and you have $$L_z+1$$ inside the argument, then $$x\rightarrow x+1$$

ClearAll[f];
SetAttributes[f, HoldFirst];
f[S_, x_, y_, z_, Lx_, Ly_, Lz_] := (
If[z == Lz,
{
S[[x, y, z]] = 1;
S[[x+1, y, z]] = 2;
S[[x, y+1, z]] = 3;
S[[x+1, y, z+1]] = 4;
}
, (* else *)
{
S[[x, y, z]] = 1;
S[[x+1, y, z]] = 2;
S[[x, y+1, z]] = 3;
S[[x, y, z+1]] = 4;
}
];
)


Since my actual function is much bigger than this example and I need to make such small changes many times, I was wondering if I can impose such a change directly in the original function in some manner?

• Does adding this definition do what you expect? f[S_, x_, y_, Lz_, Lx_, Ly_, Lz_] := f[S, x+1, y, Lz, Lx, Ly, Lz] Jan 25, 2019 at 16:12
• No, $x\rightarrow x+1$ only when $L_z+1$ is in the argument and not for just $z=L_z$ Jan 25, 2019 at 16:15
• $L_z +1$ in what argument exactly? I see no $L_z +1$ anywhere in your example. Jan 25, 2019 at 16:17
• If you see the If statement in the second snippet, then for $z=L_z$, I take $x\rightarrow x+1$ (in comparison to the first snippet) when there is $z+1$ in the argument Jan 25, 2019 at 16:20
• And how is that related to Lz ? Jan 25, 2019 at 16:25

f[S_, x_, y_, z_, Lx_, Ly_, Lz_] := With[{w=Boole[z == Lz]},