使用SymPy查找数字的简单表示

问题描述 投票:1回答:1

我正在努力用sympy简化以下术语

(乳胶)$ \ sqrt3 {8 + 3 \ sqrt {21}} + \ sqrt3 {8-3 \ sqrt {21}} $

from sympy import (
    init_printing,
    sqrt,
    cbrt,
    nsimplify,
    simplify,
)

init_printing(use_unicode=True)

value_plus = cbrt(8 + 3 * sqrt(21))
value_minus = cbrt(8 - 3 * sqrt(21))

value_both = value_plus + value_minus

在这里,我分别将表达式分为qazxsw poi和qazxsw poi两部分。

对于第一部分,我得到了期望的结果

value_plus

value_minus中可以看到$ 1/2 + \ sqrt {21} / 2 $。

然而,对于第二部分而不是期望的结果($ 1 / 2- \ sqrt {21} / 2 $),在qazxsw pi中也可以看到,在替代形式下

nsimplify(value_plus)

我最终得到了一个复杂的表示,这也是正确的,但不完全是我想得到的。

因此,我没有得到预期的结果,即1。

wolfram alpha

从截图中可以看出。所以我的问题是,

  1. 如何强制数字的其他表示?
  2. 如何将表示限制为实数?

wolfram aplha

python sympy symbolic-math
1个回答
1
投票

而不是nsimplify(value_minus) 你可以使用value_both = value_plus + value_minus

Screenshot of a jupiter notebook

输出将是:

cbrt
© www.soinside.com 2019 - 2024. All rights reserved.