tags:

views:

228

answers:

2

Suppose I have the following results:

a=FindInstance[2*b^2 + b^3 == b^4 + t && t < 10 && t > -1, {b, t}, 
  Integers, 20]
{{b -> -1, t -> 0}, {b -> 0, t -> 0}, {b -> 1, t -> 2}, {b -> 2, 
  t -> 0}}

How can I get rid of the "b->" and just get the array of b answers? I can get halfway there with:

a[[All,1]]
{b -> -1, b -> 0, b -> 1, b -> 2}

but how can I get to just:

{-1, 0, 1, 2}

Thanks

A: 

The right answer, thanks to Will, is simply

b /. a

Following is my original, much more roundabout answer:

b /. # & /@ a

That's also robust to the order that the found instances are given in.

Explanation:

foo[... # ...]& is shorthand for Function[x, foo[... x ...]]

expr /. {a->b, c->d} is shorthand for ReplaceAll[expr, {a->b, c->d}]

func /@ list is shorthand for Map[func, list]

So the following, for example, is a more verbose version of b /. # & /@ a

Map[Function[x, b /. x], a]

Basically, it's going through the list that FindInstance returns and pulling the value of b in each element.

If you knew that b would be first in the returned list, you could also do this:

a[[All,1]][[All,2]]
dreeves
Please switch the accepted answer to Will's!
dreeves
Since my answer is still the Accepted Answer months later, I figured I should edit it to actually be correct. (My original answer wasn't actually wrong but was highly inelegant.)
dreeves
+6  A: 

I might be missing something from dreeves' answer, but the way I always believed you do this was simply by writing:

b /. a

There is an example of this in the "Basic Examples" section of the documentation for the Solve function, which uses the same output style.

Will Robertson
Wow, that's embarrassing. I just assumed you had to explicitly map over the list of rules to get each value of `b`. But, you're right, just `b /. a` knows to give a list of the b's for each rule in the list. You win this round, Will Robertson!
dreeves