# Unable to properly define an operator indexed by a natural number

I'm trying to define a differential operator indexed by a natural number $$n$$, with the definition varying depending on the parity of $$n$$. Here's what I wrote:

For[g = 3, g <= 10, g++,
If[EvenQ[g], Dop[g] = -dF[g/2]*D[#, x] &,
Dop[g] = A'[z] dF[(g + 1)/2]*# &]
]


where dF is just a function that receives an integer argument. The problem here is that the argument of dF does not get evaluated until after when I give the operator itself an argument.

For instance if, right after the For, one evaluates Dop[1][f[x]], we see that it's using $$g=11$$. I tried using Evaluate as suggested here, but it still doesn't work. For example

For[g = 3, g <= 10, g++,
If[EvenQ[g], Dop[g] = -dF[Evaluate[g/2]]*D[#, x] &,
Dop[g] = A'[z] dF[Evaluate[(g + 1)/2]]*# &]
]


does not do anything and

For[g = 3, g <= 10, g++,
If[EvenQ[g], Dop[g] = Evaluate[-dF[g/2]*D[#, x]] &,
Dop[g] = Evaluate[A'[z] dF[(g + 1)/2]*#] &]
]


only works in the odd case. In the even case, I'm assuming it is forcing the derivative to be done right away, because the operator vanishes.

There was also the suggestion of using SetDelayed instead of a For in the above answer, but Dop[1] and Dop[2] have different definitions, so I'm not sure how to do that without overwritting these (notice that the Fors only start on $$g=3$$).

I'd appreciate any insight here. Thank you!

• Why not just use: Dop[g_Integer] := If[EvenQ[g], -dF[g/2] D[#, x] &, A'[z] dF[(g + 1)/2] # &]? Commented Apr 3, 2022 at 17:03
• I just edited and added the last paragraph saying that the definition is different for $g=1$ and $g=2$, and made the loops start only at $3$, sorry for that. This was actually the original motivation to do a For loop, I guess. Could I possibly just write Dop[1]= and Dop[2]= after what you wrote to overwrite the definition? Commented Apr 3, 2022 at 17:06
• Yes, you should be able to provide specific instances to override the general cases. Commented Apr 3, 2022 at 17:11
• I tried. The overwritting works fine but Dop[4][f[x]] the even case does not seem to work very well. Commented Apr 3, 2022 at 17:13
• Actually, yes it does, I apologize. Thank you! Commented Apr 3, 2022 at 17:15

Look at what you definitions are:

For[g = 3, g <= 10, g++,
If[EvenQ[g], Dop[g] = -dF[g/2]*D[#, x] &,
Dop[g] = A'[z] dF[(g + 1)/2]*# &]]
?? Dop


You see that "g" in the code of the function is not evaluated. The reason is, that "Function" or "&" has the attribute "HoldFirst". You can override this by "Evaluate", but this does not give the expected result. E.g.:

Dop[g] = Evaluate[-dF[Evaluate[g/2]]*D[#, x]] &
(* 0 & *)


It gives the zero function. The reason is that evaluating D[#x,] gives zero because "#" is not a function of "x".

The solution is to use "With". This statement replaces names by values that are evaluated. e.g. With[{g=g},...], it first evaluates g (on the right side) and replaces every occurrence of the variable g (on the left side) by the evaluated value:

For[g = 3, g <= 10,
g++ If[EvenQ[g], With[{g = g}, Dop[g] = -dF[g/2]*D[#, x] &] ,
Dop[g] = A'[z] With[{g = g}, dF[(g + 1)/2]*# &]]]
?? Dop


As you can see, now "g" in the body of the function is evaluated.

The operator could also be defined succintly using patterns instead of a For loop.

Clear[Dop]
Dop[1] = gfun[1];
Dop[2] = gfun[2];
Dop[g_?EvenQ] := -dF[g/2] D[#, x] &
Dop[g_?OddQ] := A'[z] dF[(g + 1)/2]*# &

Table[Dop[n][f[x]], {n, 10}] // Column


$$\begin{array}{l} \text{gfun}(1)(f(x)) \\ \text{gfun}(2)(f(x)) \\ \text{dF}(2) f(x) A'(z) \\ -\text{dF}(2) f'(x) \\ \text{dF}(3) f(x) A'(z) \\ -\text{dF}(3) f'(x) \\ \text{dF}(4) f(x) A'(z) \\ -\text{dF}(4) f'(x) \\ \text{dF}(5) f(x) A'(z) \\ -\text{dF}(5) f'(x) \\ \end{array}$$

• This is great as well, thank you very much! Commented Apr 4, 2022 at 7:03