开发者

Mathematica: Getting rid of the "x ->" in FindInstance results

开发者 https://www.devze.com 2022-12-09 02:22 出处:网络
Suppose I have the following results: a=FindInstance[2*b^2 + b^3 == b^4 + t && t < 10 && t > -1, {b, t},

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 ca开发者_如何学运维n 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


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.


Though Will's answer is the canonical way to do it, I'll provide a few alternatives just for fun.

In[37]:= ans={{b -> -1, t -> 0},{b -> 0, t -> 0},{b -> 1, t -> 2},{b -> 2, t -> 0}};

In[38]:= Cases[ans, (b -> a_) :> a, Infinity]

Out[38]= {-1, 0, 1, 2}

In[39]:= ans[[All, 1]][[All, 2]]

Out[39]= {-1, 0, 1, 2}

In[40]:= ans /. {b -> a_, _} :> a

Out[40]= {-1, 0, 1, 2}

In[41]:= (ans /. Rule -> List)[[All, 1, 2]]

Out[41]= {-1, 0, 1, 2}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号