Mathematica:使用 Solve 时提取数值

发布于 2024-12-01 03:27:48 字数 354 浏览 1 评论 0原文

在 Mathematica 中,调用 Solve 会返回规则列表,例如,

In[1]:= g = Solve[(x - 1) (x - 2) == 0, x]
Out[1]= {{x -> 1}, {x -> 2}}

如何从 g 中提取数值 12

我尝试使用 Part 例如 g[[1]] 但它返回 {x -> 1} 而不是 1

请指教。

In Mathematica, calling Solve, returns a list of rules, e.g.,

In[1]:= g = Solve[(x - 1) (x - 2) == 0, x]
Out[1]= {{x -> 1}, {x -> 2}}

How can I extract the numerical values 1 or 2 from g?

I tried using Part e.g., g[[1]] but it returns {x -> 1} and not 1.

Please advise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北风几吹夏 2024-12-08 03:27:48

为了补充贝利撒留的答案,

x /. g

g = {{x -> 1},{x-> 2}},返回列表

{1, 2}

因此要提取第一个值,1,我们可以使用

First[x /. g]

其他替代方案是

x /. g[[1]]
(x /. g)[[1]]    (* this is equivalent to the version using First *)
g[[1,1,2]]

To complement Belisarius' answer,

x /. g

with g = {{x -> 1}, {x -> 2}}, returns the list

{1, 2}

So to extract the first value, 1, we could use

First[x /. g]

Other alternatives are

x /. g[[1]]
(x /. g)[[1]]    (* this is equivalent to the version using First *)
g[[1,1,2]]
孤檠 2024-12-08 03:27:48
x /. g[[1]]

填料->最少三十个字符

x /. g[[1]]

Filler -> Thirty chars minimum

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文