Python3:我怎样才能让这个公式可读?

2024-05-28 19:54:20 发布

您现在位置:Python中文网/ 问答频道 /正文

所以在这个实习中,我需要用积分(x^(9/2))/((1-x)^2)作为我画的方程的一部分。然而,我沿着x轴绘制的变量出现在积分的两个极限中。由于我是python的一个完全的新手,我的代码非常糟糕,但是我最终复制+粘贴了两次不定积分,插入了积分的极限,并进行了减法运算。我怎样才能使代码更好?你知道吗

import numpy as np
import matplotlib.pyplot as plt
from scipy import integrate

x = np.arange(0,2.5,0.00001)

zs = 8
zr = 6
rbub = 5
sig = 2.5
XHI = 0.5
sigma = 10**-sig
L = 10**-3
zb = zs - (((1 + zs)/8)**(3/2)) * (0.0275) * (rbub/10)
a = (1+zr)/((1+zs)*(x+1))
b = (1+zb)/((1+zs)*(x+1))

def f(x):
    ans = 0.000140092
    ans = ans * ((1+zs)**(3/2))
    ans = ans * ((x+1)**(3/2))
    ans = ans * XHI
    return ans * ((9/2)*(np.log(1-np.sqrt(b)) - np.log(np.sqrt(b)+1)) + (1/35 * (1/(b-1)) * (10*(b**(9/2)) + 18*(b**(7/2)) + 42*(b**(5/2)) + 210*(b**(3/2)) - 315*(b**(1/2))) - ((9/2)*(np.log(1-np.sqrt(a)) - np.log(np.sqrt(a)+1)) + (1/35 * (1/(a-1)) * (10*(a**(9/2)) + 18*(a**(7/2)) + 42*(a**(5/2)) + 210*(a**(3/2)) - 315*(a**(1/2)))))))

Tags: 代码importlogasnp绘制sqrtsig
2条回答

使用好的变量名对阅读代码的人甚至对您都有很大帮助,注释也会有帮助。 对于其他人来说,这是一个等式,没有好的方法来表达它。你知道吗

这里有一个镜头。当然,我不知道这是怎么回事。正如其他人指出的那样,您应该能够更好地添加注释/合理的变量名。不过,这是你能做的。你知道吗

首先,运行代码格式化程序,使代码更人性化。你知道吗

def f(x):

    ans = 0.000140092
    ans = ans * ((1 + zs) ** (3 / 2))
    ans = ans * ((x + 1) ** (3 / 2))
    ans = ans * XHI

    return ans * (
        (9 / 2) * (np.log(1 - np.sqrt(b)) - np.log(np.sqrt(b) + 1))
        + (
            1
            / 35
            * (1 / (b - 1))
            * (
                10 * (b ** (9 / 2))
                + 18 * (b ** (7 / 2))
                + 42 * (b ** (5 / 2))
                + 210 * (b ** (3 / 2))
                - 315 * (b ** (1 / 2))
            )
            - (
                (9 / 2) * (np.log(1 - np.sqrt(a)) - np.log(np.sqrt(a) + 1))
                + (
                    1
                    / 35
                    * (1 / (a - 1))
                    * (
                        10 * (a ** (9 / 2))
                        + 18 * (a ** (7 / 2))
                        + 42 * (a ** (5 / 2))
                        + 210 * (a ** (3 / 2))
                        - 315 * (a ** (1 / 2))
                    )
                )
            )
        )
    )

你马上就会看到一些符号。这一大块

10 * (b ** (9 / 2))
+ 18 * (b ** (7 / 2))
+ 42 * (b ** (5 / 2))
+ 210 * (b ** (3 / 2))
- 315 * (b ** (1 / 2))

是一些权重和b提升为幂向量的点积。如果b是标量,我们可以把它写成np.dot(weights, np.sqrt(b) ** powers)。也许我们甚至可以通过使用积分幂来获得一些优化点。你知道吗

把这些东西放在一起,我们可以得到这样的东西:

weights = np.array([10, 18, 42, 210, -315])
powers = np.array([9, 7, 5, 3, 1])


def log_term(x):
    return (9 / 2) * (np.log(1 - np.sqrt(x)) - np.log(np.sqrt(x) + 1))


def dot_term(x):
    return (1 / 35) * 1 / (x - 1) * np.dot(np.sqrt(x)[..., None] ** powers, weights)


def integrate(x):
    return log_term(x) + dot_term(x)


factor1 = integrate(b) - integrate(a)
factor2 = 0.000140092 * ((1 + zs) ** (3 / 2)) * XHI
factor = factor1 * factor2


def f(x):
    return factor * ((x + 1) ** (3 / 2))

有了更好的变量名和注释,这几乎是可读的。你知道吗


旁白。在原始代码和本版本中,都在脚本主体中定义x。还可以将几个变量定义为x的函数,例如ab。你知道吗

Python作用域规则意味着,如果传递不同的xf,这些变量将不会更改。如果希望所有变量都随x更改,则应该在函数中移动定义。你知道吗

相关问题 更多 >

    热门问题