# Simplifying expression with particular form of variables

I'm trying to express the expression in terms of ratio (wa/py) and (wb/py). Is there any command or function that could achieve this?

(81 py^4)/(16 (wa + 2 wb)^4)


I'm new to Mathematica and the community. Not sure if the question is answered before after some simple seraching.

Welcome to MMA and MMA SE! This just so happens to be a relatively common question, and rather uncharacteristically Mathematica doesn't have a built-in. I'm sure there are a duplicates somewhere, but the question is phrased a bit differently each time it's asked, making it hard to search for... (However, if anyone finds one, feel free to mark this question as a duplicate despite my answer.)

I've wanted to write a little package that makes it more convenient to "rephrase expressions in terms of other expressions" but have been dragging my feet. Basically, though: you probably want Eliminate, which eliminates variables from systems of equations. We can hijack this by just saying your expression is equal to a new variable—now it's an equation:

eq1 = (expr == (81 py^4)/(16 (wa + 2 wb)^4))


(Note the difference between = and ==. = is used to set variables; == is used to test equality. 1 == 2 will turn into False, but when Symbols are involved, such as a == b + 1, the expression doesn't evaluate any further, and represents an equation.)

And now Eliminate can handle it. To express that we want things in terms of these ratios, we make similar equations that we'll feed to Eliminate:

eq2 = (waOverpy == wa/py)
eq3 = (wbOverpy == wb/py)


Now, we eliminate wa, wb, and py from the system of equations:

rephrase = Eliminate[{eq1, eq2, eq3}, {wa, wb, py}]


We get a complicated result. It's still in the form of an equation, and worse, expr is on both sides! We now want to solve for our original expression expr:

sol = Solve[rephrase, {expr}]


The result we get is

{{expr -> 81/(16 (waOverpy + 2 wbOverpy)^4)}}


It's pretty clear what this is saying, and you can just copy-paste the result out of there manually if you wanted to! But if you're curious about how to use it directly and computationally (as Solve is pretty useful), read on...

### How to use the output of Solve

But if you're new to Mathematica, here's some background on what Solve is really giving you with that output. An expression of the form -> is called a "rule". (This is what it is "under the hood", too: a -> b is really Rule[a, b].) Solve returns a list of lists rules. If there's more than one solution, you'll get more than one list of rules; for example, Solve[x^2 - 1 == 0, {x}] gives {{x -> -1}, {x -> 1}}. One list of assignments for each solution. And we need a list of rules for each solution in case there are multiple variables, such as {x -> -2, y -> 5}. Here, there's one solution with one variable, so each list just has one element. So that's why there are two pairs of brackets around the result.

You can then USE the rules computationally, too! /. is a very useful operator in Mathematica, and means "apply all the rules on the right everywhere to the expression on the left". So a + a^2*c /. {a -> 1 + b, c -> f[x]} yields 1 + b + (1 + b)^2 f[x]. If you use a list of lists of rules, you'll get a list of results, one for each list of rules. So a + c /. {{a -> 5, c -> f[x]}, {a -> 1}} yields {5 + f[x], 1 + c}.

To take the first element of a list called somelist, you can use somelist[] or First[somelist]. We just want to use a list of rules, not a list of list of rules, so we'll take the first element of the result sol that Solve gave us. So we can write

result = expr /. First[sol]


and this will replace expr according to the list of rules {expr -> 81/(16 (waOverpy + 2 wbOverpy)^4)}. (Note that there's only one pair of brackets now, since we used First. That means we get

81/(16 (waOverpy + 2 wbOverpy)^4)


But we can go one further! We want this in terms of wa/py and wb/py, not our named variables waOverpy and wbOverpy, right? So let's replace them too!

newexpr = result /. {waOverpy -> wa/py, wbOverpy -> wb/py}


This gives

81/(16 (wa/py + (2 wb)/py)^4)


And there you go! As a check, let's see if it's equal to our original expression. Sometimes equalities with symbols need coaxing with Simplify to reduce to True if they're always true.

(81 py^4)/(16 (wa + 2 wb)^4) == 81/(16 (wa/py + (2 wb)/py)^4) // Simplify

(* Output: True *)


Note: // f means "then apply the function f". So x // f is the same as f[x]; sometimes it just flows better to write f as a thing you do next instead of in prefix notation. (* ... *) is an inline comment.

Also check out the help docs for Simplify and FullSimplify. (Mathematica has great documentation; the "Help" button in the top right hand corner of each notebook is your best friend.) Hope this helps; let me know if any part of what I've said is at all confusing! :)

You also might find What are the most common pitfalls awaiting new users? useful! It organizes and links to many different topics.

### Code

Here's all the code in a block, with some steps collapsed.

eq1 = (expr == (81 py^4)/(16 (wa + 2 wb)^4))
eq2 = (waOverpy == wa/py)
eq3 = (wbOverpy == wb/py)
rephrase = Eliminate[{eq1, eq2, eq3}, {wa, wb, py}]
newexpr =
expr /. First[Solve[rephrase, {expr}]] /. {waOverpy -> wa/py, wbOverpy -> wb/py}
(81 py^4)/(16 (wa + 2 wb)^4) == 81/(16 (wa/py + (2 wb)/py)^4) // Simplify

• Hi thorimur, thank you so much for your detailed explanation. Just want to know if there are ways to keep all my subsequential steps in the form of these ratio? It turns out to me that in the subsequential steps of derivation the expression get messy again. May 18 at 10:24
• @HopelessEconomist Hmm, is it just these particular variables you want to rewrite things in terms of? That's easily doable then, maybe by writing a function to perform these steps automatically. Alternatively, if feasible, consider using the symbols waOverpy and wbOverpy in the derivation so that they don't get separated, and then just replacing those symbols all at once. Also, what is the purpose of the derivation? Is it just for display, or do you hope to compute with it? May 18 at 20:55
• The expression above is one of the step of my whole computation and derivation. The purpose of keeping the variable in such a particular form is to make my steps clear enough to read. Also, at the very end of the derivation, I need to display the final expression with those variable ratios. May 22 at 2:46
expr = (81 py^4)/(16 (wa + 2 wb)^4)

(expr /. {wa -> py c1} /. {wb -> py c2} // Factor) /. {c1 -> wa/py,
c2 -> wb/py}


$$\frac{81}{16 \left(\frac{\text{wa}}{\text{py}}+\frac{2 \text{wb}}{\text{py}}\right)^4}$$