从数学到Python
如何将这个 Mathematica 代码移植到 Python 中?我不知道 Mathematica 语法,并且很难理解如何用更传统的语言来描述它。
来源(第 5 页):http://subjoin.net/misc/m496pres1.nb.pdf
How can this Mathematica code be ported to Python? I do not know the Mathematica syntax and am having a hard time understanding how this is described in a more traditional language.
Source (pg 5): http://subjoin.net/misc/m496pres1.nb.pdf
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不能直接移植到 Python,因为定义
a[j]
使用 Mathematica 的符号算术功能。a[j]
基本上是 Apart 内有理函数的级数展开中 xj 的系数。假设您有
a[j]
,那么f[n]
就很容易了。 Mathematica 中的块基本上引入了变量的范围。第一个列表初始化变量,剩下的就是代码的执行。所以(
二项式
是二项式系数。)This cannot be ported to Python directly as the definition
a[j]
uses the Symbolic Arithmetic feature of Mathematica.a[j]
is basically the coefficient of xj in the series expansion of that rational function inside Apart.Assume you have
a[j]
, thenf[n]
is easy. A Block in Mathematica basically introduces a scope for variables. The first list initializes the variable, and the rest is the execution of the code. So(
binomial
is the Binomial coefficient.)使用之前答案中提出的解决方案,我发现 sympy 遗憾地没有立即计算有理数的 apart() 。它不知何故变得混乱。此外,*Poly.all_coeffs()* 返回的 Python 系数列表与 Mathmatica 列表具有不同的语义。因此 a() 定义中的 try- except 子句。
以下代码确实有效,并且对于某些测试值,输出与 Mathematica 7 中的 Mathematica 公式给出的答案一致:
Using the proposed solutions from the previous answers I found that sympy sadly doesn't compute the apart() of the rational immediatly. It somehow gets confused. Moreover, the python list of coefficients returned by *Poly.all_coeffs()* has a different semantics than a Mathmatica list. Hence the try-except-clause in the definition of a().
The following code does work and the output, for some tested values, concurs with the answers given by the Mathematica formula in Mathematica 7:
符号可以通过 sympy 来完成。结合KennyTM的答案,类似这样的东西可能就是你想要的:
虽然我不得不承认f(n)不起作用(我不太擅长Python)。
The symbolics can be done with sympy. Combined with KennyTM's answer, something like this might be what you want:
Although I have to admit that f(n) does not work (I'm not very good at Python).