如何将一个函数合并到另一个函数中?

2024-10-01 17:23:10 发布

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

我必须定义两个独立的函数z(x, mu, c)landau(x, A, mu, c)z = (x-mu)/clandau = 1.64872*A*np.exp(-0.5(z+np.exp(-z)))

由于它们共享变量,我试图在landau的定义中将z定义为函数本身,而不是函数。此外,我还尝试将z定义为landau定义之外的函数。然而,我尝试的东西似乎都不管用。python告诉我"'float' object is not callable""bad operant type for unary -: 'function'"。有没有快速解决这个问题的方法

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))))

Tags: 函数return定义objectisdefnpnot
1条回答
网友
1楼 · 发布于 2024-10-01 17:23:10

你在-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

相关问题 更多 >

    热门问题