Search code examples
pythonfunctionnumpyloggingreverse

Reversing power function


I try to write a function that reverses my function 'cw_2_ZMA' where output data 'cw' is calculated from input data 'ZMA'. I created function 'ZMA_2_cw', as written below, but my printed array shows that this function is not the exact reverse. I have been staring at this, but did not manage to figure out what I am doing wrong. Anyone here who can help me out?

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


Solution

  • your conversion is wrong the second function should be

    def ZMA_2_cw(ZMA):
       PCW = (ZMA*(-144.7/(1000*np.log(0.05))))**(-1/0.88)
       cw = pcw-bs
       return cw