从数学到Python

发布于 2024-09-27 13:26:55 字数 288 浏览 0 评论 0原文

如何将这个 Mathematica 代码移植到 Python 中?我不知道 Mathematica 语法,并且很难理解如何用更传统的语言来描述它。

mathematica code

来源(第 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.

mathematica code

Source (pg 5): http://subjoin.net/misc/m496pres1.nb.pdf

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

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

发布评论

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

评论(3

小瓶盖 2024-10-04 13:26:55

这不能直接移植到 Python,因为定义 a[j] 使用 Mathematica 的符号算术功能。

a[j] 基本上是 Apart 内有理函数的级数展开中 xj 的系数。

假设您有 a[j],那么 f[n] 就很容易了。 Mathematica 中的块基本上引入了变量的范围。第一个列表初始化变量,剩下的就是代码的执行。所以

from __future__ import division
def f(n):
  v = n // 5
  q = v // 20
  r = v % 20
  return sum(binomial(q+5-j, 5) * a[r+20*j] for j in range(5))

二项式二项式系数。)

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], then f[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

from __future__ import division
def f(n):
  v = n // 5
  q = v // 20
  r = v % 20
  return sum(binomial(q+5-j, 5) * a[r+20*j] for j in range(5))

(binomial is the Binomial coefficient.)

三五鸿雁 2024-10-04 13:26:55

使用之前答案中提出的解决方案,我发现 sympy 遗憾地没有立即计算有理数的 apart() 。它不知何故变得混乱。此外,*Poly.all_coeffs()* 返回的 Python 系数列表与 Mathmatica 列表具有不同的语义。因此 a() 定义中的 try- except 子句。

以下代码确实有效,并且对于某些测试值,输出与 Mathematica 7 中的 Mathematica 公式给出的答案一致:

from __future__ import division
from sympy import expand, Poly, binomial, apart
from sympy.abc import x

A = Poly(apart(expand(((1-x**20)**5)) / expand((((1-x)**2)*(1-x**2)*(1-x**5)*(1-x**10))))).all_coeffs()

def a(n):
    try:
        return A[n]
    except IndexError:
        return 0

def f(n):
    v = n // 5
    q = v // 20
    r = v % 20
    return sum(a[r+20*j]* binomial(q+5-j, 5) for j in range(5))

print map(f, [100, 50, 1000, 150])

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:

from __future__ import division
from sympy import expand, Poly, binomial, apart
from sympy.abc import x

A = Poly(apart(expand(((1-x**20)**5)) / expand((((1-x)**2)*(1-x**2)*(1-x**5)*(1-x**10))))).all_coeffs()

def a(n):
    try:
        return A[n]
    except IndexError:
        return 0

def f(n):
    v = n // 5
    q = v // 20
    r = v % 20
    return sum(a[r+20*j]* binomial(q+5-j, 5) for j in range(5))

print map(f, [100, 50, 1000, 150])
左耳近心 2024-10-04 13:26:55

符号可以通过 sympy 来完成。结合KennyTM的答案,类似这样的东西可能就是你想要的:

from __future__ import division
from sympy import Symbol, apart, binomial

x = Symbol('x')
poly = (1-x**20)**5 / ((1-x)**2 * (1-x**2) * (1-x**5) * (1-x**10))
poly2 = apart(poly,x)

def a(j):
    return poly2.coeff(x**j)

def f(n):
    v = n // 5
    q = v // 20
    r = v % 20
    return sum(binomial(q+5-j, 5)*a(r+20*j) for j in range(5))

虽然我不得不承认f(n)不起作用(我不太擅长Python)。

The symbolics can be done with sympy. Combined with KennyTM's answer, something like this might be what you want:

from __future__ import division
from sympy import Symbol, apart, binomial

x = Symbol('x')
poly = (1-x**20)**5 / ((1-x)**2 * (1-x**2) * (1-x**5) * (1-x**10))
poly2 = apart(poly,x)

def a(j):
    return poly2.coeff(x**j)

def f(n):
    v = n // 5
    q = v // 20
    r = v % 20
    return sum(binomial(q+5-j, 5)*a(r+20*j) for j in range(5))

Although I have to admit that f(n) does not work (I'm not very good at Python).

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