TransWikia.com

extract values from replacement list

Mathematica Asked by sjdh on January 8, 2021

Solve returns a list of replacement rules

In: Solve[x + y == 3 && x - y == 6, {x, y}]
Out: {{x -> 9/2, y -> -(3/2)}}

I am only interested in the right hand side of these rules. To extract the right hand side I use substitution:

({x, y} /. sol)[[1]]

It gives

{9/2, -(3/2)}

This works, but it is not very elegant. You have to adapt the list of the variables, each time you solve for different variables. Is there a more general way to extract the right hand sides form a list of replacements?

Edit The number of variables and the number of solutions may both differ.

8 Answers

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)}

Correct answer by Mr.Wizard on January 8, 2021

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.

Answered by PlatoManiac on January 8, 2021

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]] &

Answered by kglr on January 8, 2021

It would be better to use the following :

{#[[1, 2]], #[[2, 2]]} & /@ Solve[x^2 + y == 4 && x - y == 2, {x, y}]
{{-3, -5}, {2, 0}}

Since in general given a system of equations may have more than only one solution.

Another more general approach is to use Table, because of different number of variables. For example when we have 3 variables in a system :

x^2 + y == 4 && x - y == 2 && x^3 + y - z^3 == 5

We could write :

Table[ #[[a, 2]], {a, 3}] & /@ 
       Solve[ x^2 + y == 4 && x - y == 2 && x^3 + y - z^3 == 5, {x, y, z}]

Edit

Instead of specifying how many variables there are we can just use this :

Column@Apply[List, #, {2}] & @ Solve[
             x^2 + y == 4 && x - y == 2 && x^3 + y - z^3 == 5, {x, y, z}]

enter image description here

or

Column@
   Apply[Composition[Part[#, 2] &, List], 
         Solve[x^2 + y == 4 && x - y == 2 && x^3 + y - z^3 == 5, {x, y, z}], {2}]

enter image description here

Answered by Artes on January 8, 2021

As others have already echoed, using ReplaceAll is in fact, the most commonly used way (and not considered inelegant). You can accommodate for varying variables by keeping a list of them separately. For example, you could do

vars = {x, y};
sol = Solve[x + y == 3 && x - y == 6, vars];
var /. sol // First

Out[1]= {9/2, -(3/2)}

However, if you feel using ReplaceAll like that is kludgy, then you can also use OptionValue which does exactly what you want:

OptionValue[sol, vars]

Out[2]= {9/2, -(3/2)}

Answered by rm -rf on January 8, 2021

You can always just use indexing to get the rhs, though I find it makes your code less readable. For your example:

sol[[All,All,2]] 

will give {9/2, -(3/2)}. Which should work in general as the second "column" of a rule list is the rhs.

Answered by Gabriel on January 8, 2021

Since V10, we have Values:

Solve[x + y == 3 && x - y == 6, {x, y}]
Values@ %
(*
  {{x -> 9/2, y -> -(3/2)}}
  {{9/2, -(3/2)}}
*)

Or simply

Values@ Solve[x + y == 3 && x - y == 6, {x, y}]

Answered by Michael E2 on January 8, 2021

In this solution,you can don't care about the structure of expressions

Clear[x, y]
Solve[x + y == 3 && x - y == 6, {x, y}] /. Rule -> Set

{{9/2, -(3/2)}}

Answered by yode on January 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP