【问题标题】:Is there a way to divide a large mathematical equation into individual terms?有没有办法将一个大的数学方程分成单独的项?
【发布时间】:2021-11-13 22:51:26
【问题描述】:

您好,我想将一个相对较长的方程与 Python 集成,并且正在寻找一种将方程分成多个项并简化它的方法。

这是函数...

p = lambda r_st, b_st: 1j * np.exp(-1j*k*((np.sqrt(r**2 - 2*r*r_st * np.sqrt(1 - r_st**2/4*gross_r**2) * np.sin(alpha) * np.cos(b_st) + r_st**2))+(r_st**2/2*n*gross_r))) / (np.sqrt(r**2 - 2*r*r_st * np.sqrt(1 - r_st**2/4*gross_r**2) * np.sin(alpha) * np.cos(b_st) + r_st**2)) * r_st

p_int =  integrate.dblquad(p, np.pi, 2*np.pi, lambda r: 0.4, lambda r: r_m)

...非常相似,我想将它们分开,因为“l”一词出现了两次,可以更好地查看。

l = lambda r_st, b_st: np.sqrt(r**2 - 2*r*r_st * np.sqrt(1 - r_st**2/4*gross_r**2) * np.sin(alpha) * np.cos(b_st) + r_st**2)

p = lambda r_st, b_st: 1j * np.exp(-1j*k* l +(r_st**2/2*n*gross_r)) / l * r_st

p_int =  integrate.dblquad(p, np.pi, 2*np.pi, lambda r: 0.4, lambda r: r_m)

不幸的是,它不是那样工作的。谁能帮我找到类似的解决方案。

【问题讨论】:

  • 使用常规的def 函数格式。更容易编辑和分行。
  • 您在尝试运行代码时没有收到错误消息吗?如果您这样做了,请仔细阅读,并将其包含在您的问题帖子中。当您编写l 时,您没有在p 中使用l(r_st, b_st) 的值;相反,您使用“函数l”,这不是您想要的。
  • 尝试将l(r_st, b_st)的结果存储在一个变量中:def p(r_st, b_st): lrb = l(r_st, b_st); return 1j * np.exp(-1j*k* lrb +(r_st**2/2*n*gross_r)) / lrb * r_st
  • 我认为你在两个例子中都有不同的括号 - 第二个你有 ... * k * l + ... 这意味着 (... * k * l ) + ... 但首先运行 ... * k * ( l + ... ) 并且这个更改结果。

标签: python math scipy


【解决方案1】:

感谢cmets,我在调用l函数时忘记了参数。现在可以了。

l = lambda r_st, b_st: np.sqrt(r**2 - 2*r*r_st * np.sqrt(1 - r_st**2/4*gross_r**2) * np.sin(alpha) * np.cos(b_st) + r_st**2)

p = lambda r_st, b_st: 1j * np.exp(-1j*k* l(r_st, b_st) +(r_st**2/2*n*gross_r)) / l(r_st, bst) * r_st

p_int =  integrate.dblquad(p, np.pi, 2*np.pi, lambda r: 0.4, lambda r: r_m)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 2022-01-04
    • 1970-01-01
    • 2023-03-25
    • 2013-04-24
    相关资源
    最近更新 更多