def KramersKronigFFT(ImX_A): ''' Hilbert transform used to calculate real part of a function from its imaginary part uses piecewise cubic interpolated integral kernel of the Hilbert transform use only if len(ImX_A)=2**m-1, uses fft from scipy.fftpack ''' X_A = sp.copy(ImX_A) N = int(len(X_A)) ## be careful with the data type, orherwise it fails for large N if N > 3e6: A = sp.arange(3,N+1,dtype='float64') else: A = sp.arange(3,N+1) X1 = 4.0*sp.log(1.5) X2 = 10.0*sp.log(4.0/3.0)-6.0*sp.log(1.5) ## filling the kernel if N > 3e6: Kernel_A = sp.zeros(N-2,dtype='float64') else: Kernel_A = sp.zeros(N-2) Kernel_A = (1-A**2)*((A-2)*sp.arctanh(1.0/(1-2*A))+(A+2)*sp.arctanh(1.0/(1+2*A)))\ +((A**3-6*A**2+11*A-6)*sp.arctanh(1.0/(3-2*A))+(A+3)*(A**2+3*A+2)*sp.arctanh(1.0/(2*A+3)))/3.0 Kernel_A = sp.concatenate([-sp.flipud(Kernel_A),sp.array([-X2,-X1,0.0,X1,X2]),Kernel_A])/sp.pi ## zero-padding the functions for fft ImXExt_A = sp.concatenate([X_A[int((N-1)/2):],sp.zeros(N+2),X_A[:int((N-1)/2)]]) KernelExt_A = sp.concatenate([Kernel_A[N:],sp.zeros(1),Kernel_A[:N]]) ## performing the fft ftReXExt_A = -fft(ImXExt_A)*fft(KernelExt_A) ReXExt_A = sp.real(ifft(ftReXExt_A)) ReX_A = sp.concatenate([ReXExt_A[int((3*N+3)/2+1):],ReXExt_A[:int((N-1)/2+1)]]) return ReX_A
def hyperbolic_tangent(a, b, prime_offset=0.0, threshold=float('inf')): """Return an element-wise hyperbolic tangent with range 'a' and slope 'b'. i.e. f(x) = a * tanh(b * x) Arguments: a - Output range: [-a, +a]. (real) b - Slope parameter. (real) prime_offset - Add a constant value to the derivative, avoiding values to stuck on zero for large input. (real) threshold - When the output's magnitude is greater than the threshold then return 'a' (or '-a'). (real) Return: An ActivationFunction object. """ thr_fun = lambda X: (X < -threshold) * -a + (X > threshold) * a + ((X < -threshold) + (X > threshold) == 0) * X fun = lambda X: thr_fun(a * scipy.tanh(X * b)) # der = lambda X: scipy.ones(X.shape) - scipy.tanh(X)**2 ab = a * b der = lambda X: ab * (scipy.ones(X.shape) - scipy.tanh(X * b)**2) + scipy.ones(X.shape) * prime_offset inv = lambda X: scipy.arctanh(X / a) / b descr = "hyperbolic_tangent(%f, %f, %f, %f)" % (a, b, prime_offset, threshold) return ActivationFunction(fun, inv, der, descr)
def independentEntropyHomogeneous2(h, J, N): avgx = avgxHomogeneous(h, J, N) heff = scipy.arctanh(avgx) return N * (scipy.log(2.) + scipy.log(scipy.cosh(heff)) - avgx * heff)
def tanhFitCsInv(y, m, b): return scipy.arctanh(y / b - 1) / -m
def tanhFitInv(y, c, d): return (scipy.arctanh(y) / c)
def __dfrelax__(T,f = fm): w = f*2*pi f = lambda e:(1.0/(cosh(e))**2)*2.0*(-1.0 + real(sqrt(1.0 - (taumini(e, T)*w)*1j)*arctanh(1.0/sqrt(1.0-(taumini(e,T)*w)*1j)))) integ = real(quad(f,0,infty)[0]) return -Pbar*B**2/rho/2/cson**2*integ