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.

  • 1
    $\begingroup$ The first function seems unused and perhaps you mean to be using SetDelayed for the other functions? $\endgroup$
    – Kyle Keane
    Oct 13 '15 at 15:19
  • $\begingroup$ The issue persists. $\endgroup$
    – user30850
    Oct 13 '15 at 15:29
  • $\begingroup$ Ok, once I get into my office I'll take a closer look, sorry my guess wasn't correct $\endgroup$
    – Kyle Keane
    Oct 13 '15 at 15:31
  • $\begingroup$ 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]]. $\endgroup$
    – march
    Oct 13 '15 at 15:36
  • $\begingroup$ 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! $\endgroup$ 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
  • $\begingroup$ 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. $\endgroup$
    – 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
Ddt[Adx[z[x, t]]]

(* (1/2 (-z[x, t] - z[dx + x, t]) + 1/2 (dt + z[x, t] + z[dx + x, t]))/dt *)
  • $\begingroup$ But I'm not quite sure of your intended usage $\endgroup$ Oct 13 '15 at 17:25
  • $\begingroup$ Almost but not quite. dt is not being added to the argument of z. $\endgroup$
    – user30850
    Oct 14 '15 at 9:05
  • $\begingroup$ I am using them for numerical analysis. $\endgroup$
    – user30850
    Oct 14 '15 at 9:15

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