# Constructing sequence of function with elements depending on the previous one / recursion

Starting from a function $$\theta0[x\_ , y\_ , z\_ ]$$, the terms $$\varphi[x\_ , y\_ , z\_ ]$$ constitutes a sequence of functions that is constructed as follows:

Order 1 (one function and its reflection) $$\varphi1[x\_ , y\_ , z\_ ]=\mathcal{H[z-z1]}(\alpha \theta0[x,y,z]) + \mathcal{H[-(z-z1)]}(\alpha \theta0[x,y,2z1-z])$$ $$\varphi1 R[x\_ , y\_ , z\_ ]= \varphi1[x,y,-z]$$

\[CurlyPhi]1[x_, y_, z_] :=
HeavisideTheta[z - z1]*(\[Alpha]*\[Theta]0[x, y, z]) +
HeavisideTheta[-(z - z1)]*(\[Alpha]*\[Theta]0[x, y, 2*z1 - z])
\[CurlyPhi]1R[x_, y_, z_] := \[CurlyPhi]1[x, y, -z]


Higher orders use the results of previous one

Order 2 - use the results of order 1 $$\varphi 2$$ is obtained by substituting in $$\varphi 1$$, $$\theta0 \rightarrow \varphi 1R$$ so that is $$\varphi 2[x\_ , y\_ , z\_ ]=\mathcal{H[z-z1]}(\alpha \varphi 1R[x,y,z]) + \mathcal{H[-(z-z1)]}(\alpha \varphi 1R[x,y,2z1-z])$$ $$\varphi 2R[x\_ , y\_ , z\_ ]= \varphi 2[x,y,-z]$$

\[CurlyPhi]2[x_, y_, z_] :=
HeavisideTheta[z - z1]*(\[Alpha]*\[CurlyPhi]1R[x, y, z]) +
HeavisideTheta[-(z - z1)]*(\[Alpha]*\[CurlyPhi]1R[x, y, 2*z1 - z])
\[CurlyPhi]2R[x_, y_, z_] := \[CurlyPhi]2[x, y, -z]


and so on...

\[CurlyPhi]3[x_, y_, z_] :=
HeavisideTheta[z - z1]*(\[Alpha]*\[CurlyPhi]2R[x, y, z]) +
HeavisideTheta[-(z - z1)]*(\[Alpha]*\[CurlyPhi]2R[x, y, 2*z1 - z])
\[CurlyPhi]3R[x_, y_, z_] := \[CurlyPhi]3[x, y, -z]
\[CurlyPhi]4[x_, y_, z_] :=
HeavisideTheta[z - z1]*(\[Alpha]*\[CurlyPhi]3R[x, y, z]) +
HeavisideTheta[-(z - z1)]*(\[Alpha]*\[CurlyPhi]3R[x, y, 2*z1 - z])


The final goal is to obtain the sum of $$\theta0[x\_ , y\_ , z\_ ]$$ and the terms $$\varphi[x\_ , y\_ , z\_ ]$$ in which only the function $$\theta0[x\_ , y\_ , z\_ ]$$ appears. An example output truncated at order 4 is given by:

\[Theta]0[x, y, z] + \[CurlyPhi]1[x, y, z] + \[CurlyPhi]1R[x, y,
z] + \[CurlyPhi]2[x, y, z] + \[CurlyPhi]2R[x, y, z] + \[CurlyPhi]3[
x, y, z] + \[CurlyPhi]3R[x, y, z] + \[CurlyPhi]4[x, y, z]


producing

\[Alpha] HeavisideTheta[-z - z1] \[Theta]0[x, y, -z] + \[Theta]0[x, y,
z] + \[Alpha] HeavisideTheta[z - z1] \[Theta]0[x, y,
z] + \[Alpha] HeavisideTheta[-z + z1] \[Theta]0[x,
y, -z + 2 z1] + \[Alpha] HeavisideTheta[-z -
z1] (\[Alpha] HeavisideTheta[z - z1] \[Theta]0[x, y,
z] + \[Alpha] HeavisideTheta[-z + z1] \[Theta]0[x,
y, -z + 2 z1]) + \[Alpha] HeavisideTheta[z + z1] \[Theta]0[x, y,
z + 2 z1] + \[Alpha] HeavisideTheta[
z - z1] (\[Alpha] HeavisideTheta[-z - z1] \[Theta]0[x,
y, -z] + \[Alpha] HeavisideTheta[z + z1] \[Theta]0[x, y,
z + 2 z1]) + \[Alpha] HeavisideTheta[-z +
z1] (\[Alpha] HeavisideTheta[z - 3 z1] \[Theta]0[x, y,
z - 2 z1] + \[Alpha] HeavisideTheta[-z + 3 z1] \[Theta]0[x,
y, -z + 4 z1]) + \[Alpha] HeavisideTheta[-z -
z1] (\[Alpha] HeavisideTheta[
z - z1] (\[Alpha] HeavisideTheta[-z - z1] \[Theta]0[x,
y, -z] + \[Alpha] HeavisideTheta[z + z1] \[Theta]0[x, y,
z + 2 z1]) + \[Alpha] HeavisideTheta[-z +
z1] (\[Alpha] HeavisideTheta[z - 3 z1] \[Theta]0[x, y,
z - 2 z1] + \[Alpha] HeavisideTheta[-z + 3 z1] \[Theta]0[x,
y, -z + 4 z1])) + \[Alpha] HeavisideTheta[
z + z1] (\[Alpha] HeavisideTheta[-z - 3 z1] \[Theta]0[x,
y, -z - 2 z1] + \[Alpha] HeavisideTheta[z + 3 z1] \[Theta]0[x,
y, z + 4 z1]) + \[Alpha] HeavisideTheta[
z - z1] (\[Alpha] HeavisideTheta[-z -
z1] (\[Alpha] HeavisideTheta[z - z1] \[Theta]0[x, y,
z] + \[Alpha] HeavisideTheta[-z + z1] \[Theta]0[x,
y, -z + 2 z1]) + \[Alpha] HeavisideTheta[
z + z1] (\[Alpha] HeavisideTheta[-z - 3 z1] \[Theta]0[x,
y, -z - 2 z1] + \[Alpha] HeavisideTheta[z + 3 z1] \[Theta]0[
x, y, z + 4 z1])) + \[Alpha] HeavisideTheta[-z +
z1] (\[Alpha] HeavisideTheta[
z - 3 z1] (\[Alpha] HeavisideTheta[z - z1] \[Theta]0[x, y,
z] + \[Alpha] HeavisideTheta[-z + z1] \[Theta]0[x,
y, -z + 2 z1]) + \[Alpha] HeavisideTheta[-z +
3 z1] (\[Alpha] HeavisideTheta[z - 5 z1] \[Theta]0[x, y,
z - 4 z1] + \[Alpha] HeavisideTheta[-z + 5 z1] \[Theta]0[x,
y, -z + 6 z1])) + \[Alpha] HeavisideTheta[
z + z1] (\[Alpha] HeavisideTheta[-z -
3 z1] (\[Alpha] HeavisideTheta[-z - z1] \[Theta]0[x,
y, -z] + \[Alpha] HeavisideTheta[z + z1] \[Theta]0[x, y,
z + 2 z1]) + \[Alpha] HeavisideTheta[
z + 3 z1] (\[Alpha] HeavisideTheta[-z - 5 z1] \[Theta]0[x,
y, -z - 4 z1] + \[Alpha] HeavisideTheta[z + 5 z1] \[Theta]0[
x, y, z + 6 z1])) + \[Alpha] HeavisideTheta[
z - z1] (\[Alpha] HeavisideTheta[-z -
z1] (\[Alpha] HeavisideTheta[
z - z1] (\[Alpha] HeavisideTheta[-z - z1] \[Theta]0[x,
y, -z] + \[Alpha] HeavisideTheta[z + z1] \[Theta]0[x, y,
z + 2 z1]) + \[Alpha] HeavisideTheta[-z +
z1] (\[Alpha] HeavisideTheta[z - 3 z1] \[Theta]0[x, y,
z - 2 z1] + \[Alpha] HeavisideTheta[-z + 3 z1] \[Theta]0[
x, y, -z + 4 z1])) + \[Alpha] HeavisideTheta[
z + z1] (\[Alpha] HeavisideTheta[-z -
3 z1] (\[Alpha] HeavisideTheta[-z - z1] \[Theta]0[x,
y, -z] + \[Alpha] HeavisideTheta[z + z1] \[Theta]0[x, y,
z + 2 z1]) + \[Alpha] HeavisideTheta[
z + 3 z1] (\[Alpha] HeavisideTheta[-z - 5 z1] \[Theta]0[x,
y, -z - 4 z1] + \[Alpha] HeavisideTheta[
z + 5 z1] \[Theta]0[x, y,
z + 6 z1]))) + \[Alpha] HeavisideTheta[-z +
z1] (\[Alpha] HeavisideTheta[
z - 3 z1] (\[Alpha] HeavisideTheta[
z - z1] (\[Alpha] HeavisideTheta[-z - z1] \[Theta]0[x,
y, -z] + \[Alpha] HeavisideTheta[z + z1] \[Theta]0[x, y,
z + 2 z1]) + \[Alpha] HeavisideTheta[-z +
z1] (\[Alpha] HeavisideTheta[z - 3 z1] \[Theta]0[x, y,
z - 2 z1] + \[Alpha] HeavisideTheta[-z + 3 z1] \[Theta]0[
x, y, -z + 4 z1])) + \[Alpha] HeavisideTheta[-z +
3 z1] (\[Alpha] HeavisideTheta[
z - 5 z1] (\[Alpha] HeavisideTheta[z - 3 z1] \[Theta]0[x, y,
z - 2 z1] + \[Alpha] HeavisideTheta[-z + 3 z1] \[Theta]0[
x, y, -z + 4 z1]) + \[Alpha] HeavisideTheta[-z +
5 z1] (\[Alpha] HeavisideTheta[z - 7 z1] \[Theta]0[x, y,
z - 6 z1] + \[Alpha] HeavisideTheta[-z + 7 z1] \[Theta]0[
x, y, -z + 8 z1])))


My main issue is how to properly write this sequence of function.

Very naively, I first tried to simplify the expression by using the substitution operator, so that for example the second term can be written by using the replacement

\[CurlyPhi]2[x_, y_, z_] :=
HeavisideTheta[z - z1]*(\[Alpha]*\[Theta]0[x, y, z]) +
HeavisideTheta[-(z - z1)]*(\[Alpha]*\[Theta]0[x, y,
2*z1 - z]) /. {\[Theta]0 -> \[CurlyPhi]1R}


This might reduce the number of typos, but it is basically the same thing as copying and pasting and it does not get me very far.

Then I understood that I have to add an additional argument to the function to describe the order. As a newbie to programming, what I did was to define the symbol to be substituted as an argument.

\[CurlyPhi]0R = \[Theta]0;
\[CurlyPhi]symbol[\[CurlyPhi]order_, x_, y_, z_] :=
HeavisideTheta[z - z1]*(\[Alpha]*\[CurlyPhi]order[x, y, z]) +
HeavisideTheta[-(z - z1)]*(\[Alpha]*\[CurlyPhi]order[x, y, 2*z1 - z])
\[CurlyPhi]Rsymbol[\[CurlyPhi]0, x_, y_, z_] := \[Theta]0[x, y, z]
\[CurlyPhi]Rsymbol[\[CurlyPhi]order_, x_, y_, z_] := \[CurlyPhi]order[
x, y, -z]


That allows to construct the two terms of the sequence $$\varphi[x\_ , y\_ , z\_ ]$$ and $$\varphi R[x\_ , y\_ , z\_ ]$$ separately with \[CurlyPhi]symbol and \[CurlyPhi]Rsymbol, respectively, and I can use a table to write them:

Table[\[CurlyPhi]Rsymbol[
Symbol["\[CurlyPhi]" <> ToString@(order - 1)], x, y, z], {order, 1,
4}]

Table[\[CurlyPhi]symbol[
Symbol["\[CurlyPhi]" <> ToString@(order - 1) <> "R"], x, y,
z], {order, 1, 4}]


The produced output is indeed the symbolic required definition of each element separately.

Using \[CurlyPhi]symbol, for $$\varphi[x\_ , y\_ , z\_ ]$$ we have the following list, each element describing $$\varphi 1$$,$$\varphi 2$$,$$\varphi 3$$,$$\varphi 4$$

{\[Alpha] HeavisideTheta[z - z1] \[Theta]0[x, y,
z] + \[Alpha] HeavisideTheta[-z + z1] \[Theta]0[x,
y, -z + 2 z1], \[Alpha] HeavisideTheta[z - z1] \[CurlyPhi]1R[x, y,
z] + \[Alpha] HeavisideTheta[-z + z1] \[CurlyPhi]1R[x,
y, -z + 2 z1], \[Alpha] HeavisideTheta[z - z1] \[CurlyPhi]2R[x, y,
z] + \[Alpha] HeavisideTheta[-z + z1] \[CurlyPhi]2R[x,
y, -z + 2 z1], \[Alpha] HeavisideTheta[z - z1] \[CurlyPhi]3R[x, y,
z] + \[Alpha] HeavisideTheta[-z + z1] \[CurlyPhi]3R[x,
y, -z + 2 z1]}


and using \[CurlyPhi]Rsymbol for $$\varphi R[x\_ , y\_ , z\_ ]$$ we have the following list, each element describing $$\varphi R0$$,$$\varphi R1$$,$$\varphi R2$$,$$\varphi R3$$

{\[Theta]0[x, y, z], \[CurlyPhi]1[x, y, -z], \[CurlyPhi]2[x,
y, -z], \[CurlyPhi]3[x, y, -z]}


However I got stuck at this point since I would not now how to map the proper association of the elements or perform the required substitutions so that I end up with a list of functions only $$\theta0[x\_ , y\_ , z\_ ]$$.

How would that be possible to perform the substitution?

Or, even better, what would be a more elegant version of constructing this sequence of functions? I feel that this is a textbook definition of some form of recursion but I cannot figure it out how to write it out.

\[CurlyPhi][0, x_, y_, z_] := \[Theta]0[x, y, z];

• Thank you very much - this is great! I would only add \[CurlyPhi]R[0, x_, y_, z_] := \[Theta]0[x, y, z]; as a definition otherwise for n=1 [CurlyPhi][1, x_, y_, z_] gives \[Alpha] (HeavisideTheta[z - z1] \[Theta]0[x, y, -z] + HeavisideTheta[-z + z1] \[Theta]0[x, y, z - 2 z1]) instead of what I should expect \[Alpha] (HeavisideTheta[z - z1] \[Theta]0[x, y, z] + HeavisideTheta[-z + z1] \[Theta]0[x, y, z - 2 z1]). Does it seem right to you? May 30, 2019 at 12:26