# Rewriting the set of equations in terms of new variables, using replacement rules when the expression contains partial derivatives [duplicate]

I am working with cumbersome equations, that contain a lot of terms. Here I will consider simple example:

v[x_, y_, t_] = {D[Ψ[x, y, t], {y, 1}], -D[Ψ[x, y, t], {x, 1}]};
r = {x, y};
II = {{1, 0}, {0, 1}};
Q = {{n1[x, y, t], n2[x, y, t]}, {n2[x, y, t], -n1[x, y, t]}};
p1 = D[Q, t] + v[x, y, t].Grad[Q, r];
eq1=p1[[1, 1]] + I*p1[[2, 1]]
eq2=p1[[1, 2]] + I*p1[[2, 2]]


As a result, I have two scalar equations (which I treat as expressions, omitting zero right hand side), eq1, eq2:

$n1_t+\Psi_y n1_x-n2_x\Psi_x+i(n2_t+\Psi_y n2_x+n1_x\Psi_x)$

$n2_t+\Psi_y n1_y-n2_y\Psi_x+i(-n1_t+\Psi_y n2_y+n1_y\Psi_x)$

I want to rewrite them in terms of a new variable, $n=n1+i*n2$. As a result, I should get:

$n_t+\Psi_y n_x+in_x\Psi_x$

$-in_t+\Psi_y n_y+in_y\Psi_x$

I calculated it on the paper, but it is easy to verify, that these are correct answers:

n[x_, y_, t_] = n1[x, y, t] + I*n2[x, y, t];
eq1r = D[n[x, y, t], t] +  D[Ψ[x, y, t], y]*D[n[x, y, t], x] +
I*D[Ψ[x, y, t], x] D[n[x, y, t], x];
eq2r = -I*D[n[x, y, t], t] + D[Ψ[x, y, t], y]*D[n[x, y, t], y] +
I*D[Ψ[x, y, t], x] D[n[x, y, t], y];
Simplify[eq1 - eq1r]
Simplify[eq2 - eq2r]


How can I do it automatically? Note, that it is not always possible to write the answer in terms of n and it's derivatives. Sometimes it could involve complex conjugate of n, or even Re[n] and Im[n], which are n1 and n2 correspondingly.

Hope it is clear what I want. I tried to use simple substitution ./, but I cannot make it work with eq1/.n1[x,y,t]->Re[n[x,y,t]].

For example, the following equation can be rewritten in terms of n and it's coplex conjugate (and derivatives):

v[x_, y_, t_] = {D[Ψ[x, y, t], {y, 1}], -D[Ψ[x, y, t], {x, 1}]};
r = {x, y};
II = {{1, 0}, {0, 1}};
Q = {{n1[x, y, t], n2[x, y, t]}, {n2[x, y, t], -n1[x, y, t]}};
A = 1/2*(D[v[x, y, t], {r}] + Transpose[D[v[x, y, t], {r}]]);
Ω = 1/2*(D[v[x, y, t], {r}] - Transpose[D[v[x, y, t], {r}]]);
p2 = Simplify[(ξ*A + Ω).(Q + II/2)];
eq=p2[[1, 1]] + I*p2[[2, 1]]

• Proposed duplicate: (3822) – Mr.Wizard Jan 27 '16 at 5:10
• I would disagree. On the one hand, my problem is more straight-forward: It is always possible to express equation in terms of new variables, so that final answer doesn't contain old variables, unlike the question you provided. From another hand, I use more complicated definitions, going from real equations to complex equations. I was looking for more straight-forward solution. – Mikhail Genkin Jan 27 '16 at 18:53
• You will notice that I did not close this question directly as I recognize room for disagreement. However there are already many answers to similar questions, and you did not indicate that you have tried any let alone all of these. In my opinion you should start there. If you were to edit your question to explain that you have tried solutions X, Y, Z from posts ..., and they do not work or are not optimal because ..., then I suspect your Question will be reopened. – Mr.Wizard Jan 27 '16 at 21:20

Your equations are filled with partial derivatives, and so your replacement rules also need to take this into account. You were trying to do this,

Derivative[0, 0, 1][n1][x, y, t] /. {n1[x, y, t] -> Re[n[x, y, t]]}
(* Derivative[0, 0, 1][n1][x, y, t] *)


Instead, you need to format your replacement rule in terms of a pattern,

Derivative[0, 0, 1][n1][x, y, t] /. {Derivative[a__][n1][x, y, t] :> Re[Derivative[a][n][x, y, t]]}
(* Re[Derivative[0, 0, 1][n][x, y, t]] *)


So you can apply this to your equation now,

replacementrules = {
Derivative[a__][n1][b__] :> Re[Derivative[a][n][b]],
Derivative[a__][n2][b__] :> Im[Derivative[a][n][b]],
n1[b__] :> Re[n[b]],
n2[b__] :> Im[n[b]]};
eq1 /. replacementrules
(* Re[Derivative[0, 0, 1][n][x, y, t]] + Re[Derivative[1, 0, 0][n][x, y, t]]*Derivative[0, 1, 0][Ψ][x, y, t] - Im[Derivative[1, 0, 0][n][x, y, t]]*Derivative[1, 0, 0][Ψ][x, y, t] +
I*(Im[Derivative[0, 0, 1][n][x, y, t]] + Im[Derivative[1, 0, 0][n][x, y, t]]*Derivative[0, 1, 0][Ψ][x, y, t] + Re[Derivative[1, 0, 0][n][x, y, t]]*Derivative[1, 0, 0][Ψ][x, y, t]) *)


You can get the simplest form by using ComplexExpand

ComplexExpand[
{eq1, eq2} /. replacementrules] You can apply this to your final equation as well,

ComplexExpand[
eq /. replacementrules] • Thanks! This answers my question. Btw, after you used replacementrules, we can use FullSimplify. It will convert it to the desired form. – Mikhail Genkin Jan 27 '16 at 19:05
• I would be even more happy, if you provide me a link to a resource that will explain, why Derivative[a__][n1][b__] :> Re[Derivative[a][n][b]],  works. I am not familiar, how this is supposed to work. – Mikhail Genkin Jan 27 '16 at 19:16
• I am at home now, so typing this on mobile. Essentially, when you dou a rule replacement it is very literal, only replacing exactly what you type. But you can use a pattern to make a replacement that is more general. You can start here, but you want to look at patterns and replacements, I'm sure there are good posts on here. – Jason B. Jan 27 '16 at 19:43
• Actually, your answer is not quite correct: if you take ComplexExpand[Re[n[x,y,t]]], you get n[x,y,t], which is not correct (in my context). Should use FullSimplify – Mikhail Genkin Feb 2 '16 at 0:59

Try this:

eq1 /. n1 -> (n[#1, #2, #3] - I*n2[#1, #2, #3] &) // Simplify


yielding and

eq2 /. n1 -> (n[#1, #2, #3] - I*n2[#1, #2, #3] &) // Simplify


returning Have fun!

• Thanks, very simple - but not completely what I need. Imagine, eq1=n1[x,y,t]. If I want to rewrite it in terms of a new variable, the answer should be Re[n[x,y,t]]. Your code will not generate this – Mikhail Genkin Jan 27 '16 at 19:02
• @Mikhail Genkin I did exactly what you asked above.There is no universal answer to the question of this type, that would allow you to completely automatize the calculations. Anyway you will need to find the approach corresponding to the task. Now you put a new question, and the approach must be different. For example: n1[x, y, t] /. n1 -> (Re[n[#1, #2, #3]] &). – Alexei Boulbitch Jan 28 '16 at 8:14
• Ok, may be I was not very clear with my question. – Mikhail Genkin Jan 29 '16 at 17:15