我尝试编写一个使我的函数'cw_2_ZMA'反转的函数,其中从输入数据'ZMA'计算出输出数据'cw'。我创建了函数“ ZMA_2_cw”,如下所示,但是我的打印数组显示此函数并非完全相反。我一直在盯着这个,但没有弄清楚我在做什么错。这里有人可以帮助我吗?
#load packages
import numpy as np
#define input arrays and variables
cw_array=np.arange(0,10,1)
bs = 1.592278e-4
#functions
def cw_2_ZMA(cw):
PCW = np.add(cw, bs)
ZMA = -(1000 * np.log(0.05) / 144.7)* np.power(PCW, -0.88)
return ZMA
def ZMA_2_cw(ZMA):
PCW=(144.7/(-1000*np.log(0.05)))*np.power(ZMA,-1/0.88)
cw=PCW*1000-bs
return cw
#execute functions
ZMA_calc=cw_2_ZMA(cw_array)
cw_calc=ZMA_2_cw(ZMA_calc)
#print arrays
print("cw_array=", cw_array[:])
print("cw_calc=", np.round(cw_calc[:],1))
----------------------------------------------------------------------------------------------------
Output in the terminal:
('cw_array=', array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
('cw_calc=', array([ 0. , 1.5, 3.1, 4.6, 6.2, 7.7, 9.3, 10.8, 12.3, 13.9]))
您的转换错误第二个功能应该是
def ZMA_2_cw(ZMA):
PCW = (ZMA*(-144.7/(1000*np.log(0.05))))**(-1/0.88)
cw = pcw-bs
return cw