1
$\begingroup$

If I have an expression that looks like the following:

$$ \frac{i \sqrt{\frac{\sigma _2}{\sigma _1}} \left(-\sigma _2 q_1(t){}^2+\sigma _1 q_2(t){}^2+\left(\omega _2-\omega _1\right) q_2(t) q_1(t)\right)}{q_2(t){}^2}$$

And I'm defining a new variable:

$$ \mathcal{z}\to \frac{\sqrt{\frac{\sigma _2}{\sigma _1}} q_1(t)}{q_2(t)}$$

Is there any way to show the expression using the new defined variable $\mathcal{Z}$?

I tried using replace:

% /. ZZ -> Sqrt[Subscript[\[Sigma], 2]/Subscript[\[Sigma], 1]]*
  Subscript[q, 1][t]/Subscript[q, 2][t]

But the expression stays the same

The input version for the expression is:

(I Sqrt[Subscript[\[Sigma], 2]/Subscript[\[Sigma], 1]] (-Subscript[\
\[Sigma], 2] Subscript[q, 1][
     t]^2 + (-Subscript[\[Omega], 1] + Subscript[\[Omega], 
      2]) Subscript[q, 1][t] Subscript[q, 2][t] + 
   Subscript[\[Sigma], 1] Subscript[q, 2][t]^2))/Subscript[q, 2][t]^2
$\endgroup$
3
  • 1
    $\begingroup$ Try q1->q2 z /Sqrt[...] (with your symbols of course) and then Simplify $\endgroup$ Jun 27, 2021 at 21:50
  • $\begingroup$ My guess is you can't get better than I z((1-z^2)q2/q1 \[Sigma]1+(\[Omega]2-\[Omega]1)). That is, there is no expression in terms of only z that is equivalent. (I found this by hand, what you're asking is sort of the inverse of /.) $\endgroup$
    – Adam
    Jun 27, 2021 at 22:36
  • $\begingroup$ You cannot replace a variable that is not present in the expression, which is what you are attempting to do here. Instead, you must do a process like what Craig Carter indicates, wherein you are replacing a variable that is present in the expression. $\endgroup$ Jun 27, 2021 at 23:00

1 Answer 1

2
$\begingroup$

You can rewrite expressions in this way using a bare symbol, Eliminate, and Solve:

ClearAll[f];

expr = (I Sqrt[Subscript[\[Sigma], 2]/Subscript[\[Sigma], 1]] (-Subscript[\
\[Sigma], 2] Subscript[q, 1][
     t]^2 + (-Subscript[\[Omega], 1] + Subscript[\[Omega], 
      2]) Subscript[q, 1][t] Subscript[q, 2][t] + 
   Subscript[\[Sigma], 1] Subscript[q, 2][t]^2))/Subscript[q, 2][t]^2;

zexpr = Sqrt[Subscript[\[Sigma], 2]/Subscript[\[Sigma], 1]]*
  Subscript[q, 1][t]/Subscript[q, 2][t];

f /. First@Solve[
            Eliminate[f == expr && ZZ == zexpr, {Subscript[q, 1][t], Subscript[q, 2][t]}],
            f]

(* Out:

-I (-Subscript[\[Sigma], 1] Sqrt[Subscript[\[Sigma], 2]/
    Subscript[\[Sigma], 1]] + 
   ZZ^2 Subscript[\[Sigma], 1] Sqrt[Subscript[\[Sigma], 2]/
    Subscript[\[Sigma], 1]] + ZZ Subscript[\[Omega], 1] - 
   ZZ Subscript[\[Omega], 2])

*)

(You might want to also apply a Simplify to the result, perhaps with second argument (assumption) Subscript[\[Sigma], 1] > 0.)

You can also have Mathematica attempt this procedure in general by packaging it in a function Rewrite!

Rewrite::nosol = "The expression `1` cannot be rewritten in terms of `2`.";
Rewrite::multisol = "The expression `1` cannot be rewritten in terms of `2` in just one way.";
Rewrite::fail = "Something went wrong.";

Rewrite[expr_, z_ == zexpr_, vars_] :=
  Block[{f},
    Replace[{
      {{f -> x_}} :> x,
      {{}}|{_,__} :> (Message[Rewrite::multisol, expr, z]; expr),
      {} :> (Message[Rewrite::nosol, expr, z]; expr),
      _ :> (Message[Rewrite::fail]; expr)
    }] @ Solve[Eliminate[f == expr && z == zexpr, vars], f]
  ]

Rewrite[expr, ZZ == zexpr, {Subscript[q, 1][t], Subscript[q, 2][t]}]

At the moment you still have to specify which variables you want to eliminate using the last argument. A more sophisticated version would extract these variables from zexpr automatically, or simply do as much rewriting as possible by traversing the expression.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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