3
$\begingroup$

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?

$\endgroup$
4
  • $\begingroup$ @rhermans Thanks, I suppose the output I am looking for in that case is y==2x, I will update the question for clarity. $\endgroup$
    – Lemonseed
    Jun 3 '18 at 22:38
  • 3
    $\begingroup$ Use Reduce instead of Solve $\endgroup$
    – Bob Hanlon
    Jun 3 '18 at 22:39
  • 3
    $\begingroup$ 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. $\endgroup$
    – John Doty
    Jun 3 '18 at 22:40
  • 1
    $\begingroup$ @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. $\endgroup$
    – Lemonseed
    Jun 3 '18 at 22:45
5
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ 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. $\endgroup$
    – Lemonseed
    Jun 3 '18 at 22:42
  • $\begingroup$ Thank-you, that was the problem. The example you gave correctly produced y == 2x after running ClearAll[x,y]. $\endgroup$
    – Lemonseed
    Jun 3 '18 at 22:46
  • $\begingroup$ @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. $\endgroup$
    – user541686
    Jun 4 '18 at 3:43
  • $\begingroup$ @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. $\endgroup$
    – rhermans
    Jun 4 '18 at 8:30
  • $\begingroup$ @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.) $\endgroup$
    – user541686
    Jun 4 '18 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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