extract values from replacement list
I also think that what you are already using is the best way, but here is another one to toss into the mix:
Solve[x + y == 3 && x - y == 6, {x, y}][[1]] /. Rule -> (#2 &)
{9/2, -(3/2)}
What about
res=Solve[x + y == 3 && x - y == 6, {x, y}];
res[[1, All, 2]]
that gives
{9/2, -(3/2)}
as you wanted. This should work while using Solve
for any finite number of linear simultaneous equations.
Actually Rules in Mathematica has similar structure as list of Length
two. You can see that if you replace Rule
in a expression with List
.
a1 = {a -> 2, b -> 3};
a1 /. Rule -> List
resulting to
{{a, 2}, {b, 3}}
This is an example that shows List
is an intrinsic structure in Mathematica language and part specification simply works on rules. As expected
a2 = {{a, 2}, {b, 3}};
{a1[[1, 2]], a2[[1, 2]]}
{2, 2}
gives the same result for the List
as well as the list of Rule
.
Update: With Version 10 comes the convenient built-in function Values
which can be used as an alternative to Part
and ReplaceAll
:
Values@@Solve[x + y == 3 && x - y == 6, {x, y}]
(* {9/2,-(3/2)} *)
or
Values@Solve[x + y == 3 && x - y == 6, {x, y}]
(* {{9/2,-(3/2)}} *)
Another example - a ragged list of rules:
lst={{a->1,b->2},{c->3},{{d->4}},{e->5,{f->6,{g->7}}}};
Values[lst]
(* {{1,2},{3},{{4}},{5,{6,{7}}}} *)
Original post:
You can also use
Solve[x + y == 3 && x - y == 6, {x, y}] /. (_ -> b_) -> b
or
Solve[x + y == 3 && x - y == 6, {x, y}] /. Rule[_, b_] -> b
or
Solve[x + y == 3 && x - y == 6, {x, y}] // #[[All, All, 2]] &