# Composing multiple functions [closed]

I am trying to compose three functions in the following manner:

Adx[z_] = (z[x + dx, t] + z[x, t])/2
Ddt[z_] = (z[x, t + dt] - z[x, t])/dt


I get the following output:

1/2 (z[x, t] + z[dx + x, t])
(-z[x, t] + z[x, dt + t])/dt
(-(1/2 (z[x, t] + z[dx + x, t]))[x, t] + (1/2 (z[x, t] + z[dx + x, t]))[x, dt + t])/dt


I was expecting

(-(1/2 (z[x, t] + z[dx + x, t])) + (1/2 (z[x, dt+ t] + z[dx + x, dt+ t])))/dt


I want to be able to compose the two functions Adx and Ddtin any order I want and get the answer as in the last line of code.

I have also looked at this question, it, too, suffers from the same problem.

## closed as unclear what you're asking by Dr. belisarius, dr.blochwave, MarcoB, user9660, Sjoerd C. de VriesOct 13 '15 at 20:51

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• The first function seems unused and perhaps you mean to be using SetDelayed for the other functions? – Kyle Keane Oct 13 '15 at 15:19
• The issue persists. – user30850 Oct 13 '15 at 15:29
• Ok, once I get into my office I'll take a closer look, sorry my guess wasn't correct – Kyle Keane Oct 13 '15 at 15:31
• The things you have defined are not pure functions. Also, it's pretty unclear what the purpose of the first definition is. Finally: see @belisarius's answer and meditate on the results of f[x_] = x[t]; f[g[y]]. – march Oct 13 '15 at 15:36
• Welcome to Mathematica.SE! I suggest the following: 0) Browse the common pitfalls question 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Dr. belisarius Oct 13 '15 at 16:05

I am going to presume that you do not actually need pure functions and that your main goal is simply to perform the symbolic operation. With these assumptions, what you are trying to do can be accomplished using a different approach using patterns and substitutions as follows:

Adx = {z[x_, t_] -> (z[x + dx, t] + z[x, t])/2};
Ddt = {z[x_, t_] -> (z[x, t + dt] - z[x, t])/dt};
z[x, t] /. Adx /. Ddt


I believe this outputs what you expect

(-z[x, t] + z[x, dt + t] - z[dx + x, t] + z[dx + x, dt + t])/(2 dt)


Hope this is helpful. You will also need to be careful when performing these types of calculations since neither of our approaches using any variable scoping such as Block, With, or Module.

To apply the operations in the opposite order you can perform the substitutions in the opposite order

z[x, t] /. Ddt /. Adx

• Thanks @Kyle. This is almost what I need except it is not a function composition. I need to compose several functions like these (upto 5) and substitution will make it harder to read and debug. – user30850 Oct 14 '15 at 9:27
Adx[z_[x_, t_]] := (z[x + dx, t] + z[x, t])/2
Ddt[z_[x_, t_]] := (z[x, t + dt] - z[x, t])/dt

• Almost but not quite. dt is not being added to the argument of z. – user30850 Oct 14 '15 at 9:05