# Using the result of Solve in subsequent calculations

I'd like to know how to use the results of Solve in later calculations. Here's what I'm doing now:

• This is my expression

g == Solve[g a - b d - f == g c - b e, g]


which gives the result {{g -> (b d - b e + f)/(a - c)}}

• Next, using the above result, I do

Solve[{g == (b d + f - e b)/(a - c), b == 0}, {g, b}]


which yields the final result: {{g -> f/(a - c), b -> 0}}

I want to do the same calculation without putting the first result into the second calculation "manually". There must be way to tell Mathematica that it has to take g and insert it into the next calculation by itself. However, I failed. I tried things like % or /. but it didn't work.

-
just Solve[{g == (b d + f - e b)/(a - c), b == 0}, {g, b}] gives your final result –  belisarius Jun 14 '12 at 11:53
I know, see above. But my question was how to use the previous result (I need this for longer calculations). As the result to be used is not just g=5, I don't know how to do this. If it was g=5, eg, I could do it using %.. –  Stefanie Schubert Jun 14 '12 at 12:01
In this particular example you could do sol=Solve[g a - b d - f == g c - b e, g], sol /. b -> 0 –  Heike Jun 14 '12 at 12:20

Are you looking for something like this:

g1 = Solve[g a - b d - f == g c - b e, g][[1, 1]] /. Rule -> Equal
Solve[{g1, b == 0}, {g, b}]


g1 is the solution of your first equation, made from a Rule into an Equal. You can even nest the two steps into one:

Solve[{Solve[g a - b d - f == g c - b e, g][[1, 1]] /. Rule -> Equal, b == 0}, {g, b}]


Out= {{g -> f/(a - c), b -> 0}}

-
result = Solve[g a - b d - f == g c - b e, g]
Solve[{g==(g/.First@result),b==0},{b,g}]

-
Yes, that'it, thanks to you both, Peter and celtschk!! –  Stefanie Schubert Jun 14 '12 at 12:11

First it is resonable to do a simplification, adding the second argument to Simplify or to Refine i.e. an assumption :

Simplify[g a - b d - f == g c - b e, b == 0]

a g == f + c g


So to make the task in one step try this :

Solve[ Simplify[ g a - b d - f == g c - b e, b == 0], g]

{{g -> f/(a - c)}}


or directly solve a system, e.g.

Solve[{g a - b d - f == g c - b e, b == 0}, {g, b}]

-

You might use a new function toEquals in (inverse) analogy to the built-in ToRules:

toEquals[x : {{__Rule} ..}] := Or @@ And @@@ (x /. Rule -> Equal)
toEquals[x_] := x


This will convert the output of Solve into a form that may be used as input.

Then:

Solve[g a - b d - f == g c - b e, g]

Solve[toEquals[%] && b == 0, {g, b}]


You could potentially use Fold to automate this substitution:

FoldList[
Solve[toEquals[#] && #2[[1]], #2[[2]]] &,
True,
{
{g a - b d - f == g c - b e, g},
{b == 0, {g, b}}
}
] // Rest

{
{{g -> (b d - b e + f)/(a - c)}},
{{g -> f/(a - c), b -> 0}}
}


That's a lot of code for a simple operation but if you need many layers of substitution it may help.

-