简体   繁体   English

编写此数学公式的Python方法是什么?

[英]What is the pythonic way to write this mathematical formulas?

This is my first post in stackoverflow.com . 这是我在stackoverflow.com上的第一篇文章。 I am beginner in python and in programming in general. 我是python和一般编程的初学者。 I read everywhere that the best way to learn programming is to start programming. 我到处都读到,学习编程的最好方法是开始编程。 Therefore, i have encountered the following table and i would like to make a function so i can calculate the relative motion in relation to the x-coordinate. 因此,我遇到了下表,我想做一个函数,以便我可以计算相对于x坐标的相对运动。

relative motion table 相对运动表

在此处输入图片说明

assuming that {C_b , n, C, L} are known i have coded the table as following: 假设已知{C_b,n,C,L},则我将该表编码如下:

if x = 0:
    h1_M = 0.42*n*C*(C_b+0.7)

    if C_b < 0.875:
        h1 = 0.7*((4.35/sqrt(C_b))-3.25)*h1_M
    else:
        h1 = h1_M

elif x>0 and x<0.3*L:

    h1_M = 0.42*n*C*(C_b+0.7)
    h1_AE = 0.7*((4.35/sqrt(C_b))-3.25)*h1_M
    h1 = h1_AE - ((h1_AE-h1_M)/0.3)*(x/L)

elif x>=0.3*L and x<0.7*L:

    h1 = 0.42*n*C*(C_b+0.7)

elif x>0.7*L and x<L:
    h1_M = 0.42*n*C*(C_b+0.7)
    h1 = h1_M + ((h1_FE - h1_M)/0.3)*((x/L)-0.7)

elif x==L:
    h1_M = 0.42*n*C*(C_b+0.7)
    h1 = ((4.35/sqrt(C_b))-3.25)*h1_M

Is this the pythonic way to structure my function? 这是构造函数的pythonic方法吗? Thank you for you reply 谢谢您的回复

It not just about “Pythonic” way to write this function, but a problem regarding the programming practice. 这不仅与编写此函数的“ Pythonic”方式有关,还与编程实践有关。 From my perspective, I would suggest you to break each step to a properly named routine, and name each value according to its meaning instead of a single letter variable. 从我的角度来看,我建议您将每个步骤都分解为一个正确命名的例程,并根据其含义而不是单个字母变量来命名每个值。

def your_funcion(C_b, n, C, L):
    h1_M = 0.42*n*C*(C_b+0.7)
    if x == 0:
        if C_b < 0.875:
            return 0.7*((4.35/sqrt(C_b))-3.25)*h1_M
        return h1_M
    if x < 0.3*L:
        h1_AE = 0.7*((4.35/sqrt(C_b))-3.25)*h1_M
        return h1_AE - ((h1_AE-h1_M)/0.3)*(x/L)
    if x < 0.7*L:
        return 0.42*n*C*(C_b+0.7)
    if x < L:
        return h1_M + ((h1_FE - h1_M)/0.3)*((x/L)-0.7)
    return ((4.35/sqrt(C_b))-3.25)*h1_M

h1 = your_function(C_b, n, C, L)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM