0
$\begingroup$

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?

$\endgroup$
6
  • $\begingroup$ 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] $\endgroup$
    – TimRias
    Jan 25, 2019 at 16:12
  • $\begingroup$ No, $x\rightarrow x+1$ only when $L_z+1$ is in the argument and not for just $z=L_z$ $\endgroup$
    – cleanplay
    Jan 25, 2019 at 16:15
  • $\begingroup$ $L_z +1$ in what argument exactly? I see no $L_z +1$ anywhere in your example. $\endgroup$
    – TimRias
    Jan 25, 2019 at 16:17
  • $\begingroup$ 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 $\endgroup$
    – cleanplay
    Jan 25, 2019 at 16:20
  • $\begingroup$ And how is that related to Lz ? $\endgroup$
    – TimRias
    Jan 25, 2019 at 16:25

1 Answer 1

1
$\begingroup$

Perhap the following modification will work for you

f[S_, x_, y_, z_, Lx_, Ly_, Lz_] := With[{w=Boole[z == Lz]},
    S[[x,y,z]]=1; S[[x+1,y,z]]=2; S[[x,y+1,z]]=3; S[[x+w,y,z+1]]=4];
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.