# Rewrite simple equation with respect to a variable. How to get Equal instead of Rule when using Solve

I am a new user to Mathematica, and would like to accomplish a very straightforward and simple goal:

Given the equation x == (1/2)y, I would like to rewrite in terms of y.

The output I am looking for is y == 2x.

Attempting with the following input:

Solve[{x == (1/2) y}, y]


Yields the result output:

{{y -> 0}}


What should be the input to get the intended result in this case?

• @rhermans Thanks, I suppose the output I am looking for in that case is y==2x, I will update the question for clarity. Jun 3 '18 at 22:38
• Use Reduce instead of Solve Jun 3 '18 at 22:39
• Given that you got y->0, I suspect that you had defined x=0 before solving. Beware that if you transform a result of the form y->2x to y=2x you are defining y and you can no longer use it as a free variable in your calculations: y will generally be replaced by 2x everywhere. Jun 3 '18 at 22:40
• @JohnDoty That is very possible that I defined x as something equal to zero whilst trying to achieve the intended result. I will first try ClearAll[x,y] as just recently suggested. Jun 3 '18 at 22:45

Start by removing any OwnValues for x and y using ClearAll

ClearAll[x,y]


## Solve

Solve returns a List of Rule (->). Therefore, you could just use the First Rule to ReplaceAll (/.) y

y == (y /. First@Solve[{x == (1/2) y}, y])
(* y == 2 x *)


Or, somehow better replace the Rule Head itself with Equal (==)

Solve[{x == (1/2) y}, y] /. Rule -> Equal
(* {{y == 2 x}} *)


## Reduce

After the comment by Bob Hanlon, offering what I think is the best answer so far, you can use Reduce

Reduce[{x == (1/2) y}, y]
(* y == 2 x *)


Here I'm assuming you actually want and equation (Equal ==) and not to Set (=) the value of y.

• Is there an implied statement or initialization that I am missing? When I run either statement, the output is y==0 and {{y==0}}, respectively. Jun 3 '18 at 22:42
• Thank-you, that was the problem. The example you gave correctly produced y == 2x after running ClearAll[x,y]. Jun 3 '18 at 22:46
• @Lemonseed, rhermans: Just a note: despite the fact that it's widespread in use, I would very heavily warn against getting in the habit of using /. (or ever encouraging anyone else to) unless you are 100% sure this code will not be run again after today. It is oblivious to the general meaning of the expression you have, including scoping, so it can silently give wrong results except in the simplest cases. Whenever you write /., remind yourself that you are doing something dangerous. Jun 4 '18 at 3:43
• @mehrdad interesting comment. As elaborating here in the comments would be out of place, can you make it a Q&A? Otherwise we could discuss it on the Mathematica Chat. Jun 4 '18 at 8:30
• @rhermans: I could, but I probably won't get a chance to elaborate in the next 48 hours -- feel free to ask/ping/remind me afterward? But one example I can leave you with is the following: a=1; 1/.a->2 evaluates to 2, which is rather... absurd. And probably not what you want. (Though this is not to say you can always find a better substitute in 100% of cases, but you usually can.) Jun 4 '18 at 8:59