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 For
s only start on $g=3$).
I'd appreciate any insight here. Thank you!
Dop[g_Integer] := If[EvenQ[g], -dF[g/2] D[#, x] &, A'[z] dF[(g + 1)/2] # &]
? $\endgroup$For
loop, I guess. Could I possibly just writeDop[1]=
andDop[2]=
after what you wrote to overwrite the definition? $\endgroup$Dop[4][f[x]]
the even case does not seem to work very well. $\endgroup$