简体   繁体   English

如何将一个 function 合并到另一个中?

[英]How do I incorporate one function into another?

I have to define two separate functions z(x, mu, c) and landau(x, A, mu, c) .我必须定义两个单独的函数z(x, mu, c)landau(x, A, mu, c) z = (x-mu)/c and landau = 1.64872*A*np.exp(-0.5(z+np.exp(-z))) z = (x-mu)/clandau = 1.64872*A*np.exp(-0.5(z+np.exp(-z)))

As they share variables I have tried to define z inside the definition of landau as a function itself and not as a function.由于它们共享变量,我试图在landau的定义中将z定义为landau本身,而不是function。 Also, I have tried to define z as a function outside the definition of landau .另外,我试图将z定义为 function 定义之外的landau However, nothing I try seems to work.但是,我尝试的任何方法似乎都不起作用。 Either python tells me "'float' object is not callable" or "bad operant type for unary -: 'function'" . python 告诉我"'float' object is not callable""bad operant type for unary -: 'function'" Is there a quick fix for this?有没有快速解决这个问题的方法?

landau(1,1,1,1) should give an answer that's roughly equal to 1 landau(1,1,1,1)应该给出一个大致等于 1 的答案

def landau(x, A, mu, c):    # Functie Landau met variabelen x, A, c en mu
# A  = amplitude
# c  = schaalparameter
# mu = positieparameter
def z(x, mu, c):
    return (x-mu)/c
return 1.64872*A*np.exp(-0.5(z(x, mu, c)+np.exp(-z(x, mu, c))))

You missed a * in -0.5 * (z(x . At least I'm assuming it's supposed to be a multiplication.您错过了-0.5 * (z(x ) 中的 * 。至少我假设它应该是乘法。

import numpy as np

def landau(x, A, mu, c):    # Functie Landau met variabelen x, A, c en mu
     # A  = amplitude
     # c  = schaalparameter
     # mu = positieparameter
     return 1.64872 * A * np.exp(-0.5 * (z(x, mu, c) + np.exp(-z(x, mu, c))))

def z(x, mu, c):
     return (x - mu) / c
     
landau(1, 1, 1, 1)
0.9999992292814129

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

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