2
$\begingroup$

This question already has an answer here:

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]]
$\endgroup$

marked as duplicate by Daniel Lichtblau, user9660, MarcoB, m_goldberg, rcollyer Jan 27 '16 at 20:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 5
    $\begingroup$ Proposed duplicate: (3822) $\endgroup$ – Mr.Wizard Jan 27 '16 at 5:10
  • $\begingroup$ 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. $\endgroup$ – Mikhail Genkin Jan 27 '16 at 18:53
  • $\begingroup$ 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. $\endgroup$ – Mr.Wizard Jan 27 '16 at 21:20
3
$\begingroup$

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]

enter image description here

You can apply this to your final equation as well,

ComplexExpand[
 eq /. replacementrules]

enter image description here

$\endgroup$
  • $\begingroup$ Thanks! This answers my question. Btw, after you used replacementrules, we can use FullSimplify. It will convert it to the desired form. $\endgroup$ – Mikhail Genkin Jan 27 '16 at 19:05
  • $\begingroup$ 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. $\endgroup$ – Mikhail Genkin Jan 27 '16 at 19:16
  • $\begingroup$ 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. $\endgroup$ – Jason B. Jan 27 '16 at 19:43
  • $\begingroup$ 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 $\endgroup$ – Mikhail Genkin Feb 2 '16 at 0:59
2
$\begingroup$

Try this:

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

yielding

enter image description here

and

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

returning

enter image description here

Have fun!

$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – Mikhail Genkin Jan 27 '16 at 19:02
  • $\begingroup$ @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]] &). $\endgroup$ – Alexei Boulbitch Jan 28 '16 at 8:14
  • $\begingroup$ Ok, may be I was not very clear with my question. $\endgroup$ – Mikhail Genkin Jan 29 '16 at 17:15

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