def test_period(self): for n in [17,64]: x = arange(n)/float(n) assert_array_almost_equal(diff(sin(2*pi*x),period=1), 2*pi*cos(2*pi*x)) assert_array_almost_equal(diff(sin(2*pi*x),3,period=1), -(2*pi)**3*cos(2*pi*x))
def test_int(self): n = 64 x = arange(n)*2*pi/n assert_array_almost_equal(diff(sin(x),-1),-cos(x)) assert_array_almost_equal(diff(sin(x),-2),-sin(x)) assert_array_almost_equal(diff(sin(x),-4),sin(x)) assert_array_almost_equal(diff(2*cos(2*x),-1),sin(2*x))
def dAdx(A): L = len(A) Re_A_x = diff(A.real, order=1, period=L) Im_A_x = diff(A.imag, order=1, period=L) A_x = Re_A_x + 1j * Im_A_x return A_x
def bench_random(self): print print 'Differentiation of periodic functions' print '=====================================' print ' size | convolve | naive' print '-------------------------------------' for size,repeat in [(100,1500),(1000,300), (256,1500), (512,1000), (1024,500), (2048,200), (2048*2,100), (2048*4,50), ]: print '%6s' % size, sys.stdout.flush() x = arange (size)*2*pi/size if size<2000: f = sin(x)*cos(4*x)+exp(sin(3*x)) else: f = sin(x)*cos(4*x) assert_array_almost_equal(diff(f,1),direct_diff(f,1)) assert_array_almost_equal(diff(f,2),direct_diff(f,2)) print '| %9.2f' % measure('diff(f,3)',repeat), sys.stdout.flush() print '| %9.2f' % measure('direct_diff(f,3)',repeat), sys.stdout.flush() print ' (secs for %s calls)' % (repeat)
def bench_random(self): print() print('Differentiation of periodic functions') print('=====================================') print(' size | convolve | naive') print('-------------------------------------') for size,repeat in [(100,1500),(1000,300), (256,1500), (512,1000), (1024,500), (2048,200), (2048*2,100), (2048*4,50), ]: print('%6s' % size, end=' ') sys.stdout.flush() x = arange(size)*2*pi/size if size < 2000: f = sin(x)*cos(4*x)+exp(sin(3*x)) else: f = sin(x)*cos(4*x) assert_array_almost_equal(diff(f,1),direct_diff(f,1)) assert_array_almost_equal(diff(f,2),direct_diff(f,2)) print('| %9.2f' % measure('diff(f,3)',repeat), end=' ') sys.stdout.flush() print('| %9.2f' % measure('direct_diff(f,3)',repeat), end=' ') sys.stdout.flush() print(' (secs for %s calls)' % (repeat))
def test_int(self): n = 64 x = arange(n) * 2 * pi / n assert_array_almost_equal(diff(sin(x), -1), -cos(x)) assert_array_almost_equal(diff(sin(x), -2), -sin(x)) assert_array_almost_equal(diff(sin(x), -4), sin(x)) assert_array_almost_equal(diff(2 * cos(2 * x), -1), sin(2 * x))
def test_period(self): for n in [17, 64]: x = arange(n) / float(n) assert_array_almost_equal(diff(sin(2 * pi * x), period=1), 2 * pi * cos(2 * pi * x)) assert_array_almost_equal(diff(sin(2 * pi * x), 3, period=1), -(2 * pi)**3 * cos(2 * pi * x))
def test_aspect_ratio_compare_with_cross_sectional_computation(self): """ This test validates the VMEC aspect ratio computation in the Surface class by comparing with an approximation based on cross section computations. """ s = get_exact_surface() vpr = s.quadpoints_phi.size + 20 tr = s.quadpoints_theta.size + 20 cs_area = np.zeros((vpr, )) from scipy import fftpack angle = np.linspace(-np.pi, np.pi, vpr, endpoint=False) for idx in range(angle.size): cs = s.cross_section(angle[idx], thetas=tr) R = np.sqrt(cs[:, 0]**2 + cs[:, 1]**2) Z = cs[:, 2] Rp = fftpack.diff(R, period=1.) Zp = fftpack.diff(Z, period=1.) ar = np.mean(Z * Rp) cs_area[idx] = ar mean_cross_sectional_area = np.mean(cs_area) R_minor = np.sqrt(mean_cross_sectional_area / np.pi) R_major = np.abs(s.volume()) / (2. * np.pi**2 * R_minor**2) AR_cs = R_major / R_minor AR = s.aspect_ratio() rel_err = np.abs(AR - AR_cs) / AR print(AR, AR_cs) print("AR rel error is:", rel_err) assert rel_err < 1e-5
def Biosech2D(Ao,Bo,x,loik,c,HH2): # Sech**2 initial condition xx = x-loik*np.pi #profile shift u0 = Ao/(np.cosh(Bo*xx))**2 u0x = diff(u0,period=2*np.pi*loik); #speed -c*u0x u0xx = diff(u0x,period=2*np.pi*loik);# second space derivative yx[:n] = u0-HH2*u0xx # space transformed yx[n:] = -c*u0x; #speed; return yx.real
def test_random_odd(self): for k in [0,1,2,3,4,5,6]: for n in [33,65,55]: f = random((n,)) af = sum(f,axis=0)/n f = f-af assert_almost_equal(sum(f,axis=0),0.0) assert_array_almost_equal(diff(diff(f,k),-k),f) assert_array_almost_equal(diff(diff(f,-k),k),f)
def test_random_odd(self): for k in [0, 1, 2, 3, 4, 5, 6]: for n in [33, 65, 55]: f = random((n, )) af = sum(f, axis=0) / n f = f - af assert_almost_equal(sum(f, axis=0), 0.0) assert_array_almost_equal(diff(diff(f, k), -k), f) assert_array_almost_equal(diff(diff(f, -k), k), f)
def test_random_even(self): for n in [32,64,56]: f = random((n,)) af = sum(f,axis=0)/n f = f-af # zeroing Nyquist mode: f = diff(diff(f,1),-1) assert_almost_equal(sum(f,axis=0),0.0) assert_array_almost_equal(direct_hilbert(direct_ihilbert(f)),f) assert_array_almost_equal(hilbert(ihilbert(f)),f)
def test_random_even(self): for n in [32, 64, 56]: f = random((n, )) af = sum(f, axis=0) / n f = f - af # zeroing Nyquist mode: f = diff(diff(f, 1), -1) assert_almost_equal(sum(f, axis=0), 0.0) assert_array_almost_equal(direct_hilbert(direct_ihilbert(f)), f) assert_array_almost_equal(hilbert(ihilbert(f)), f)
def dAdt(t, A): L = len(A) Re_A_xx = diff(A.real, order=2, period=L) Im_A_xx = diff(A.imag, order=2, period=L) A_xx = Re_A_xx + 1j * Im_A_xx abs_A2 = abs(A)**2 A_t = (1 + 1j * alpha) * A_xx + A - (1 + 1j * beta) * abs_A2 * A A_t_ = 1j * (A_xx + 2 * abs(A) * A) return A_t
def test_expr_large(self): for n in [2048,4096]: x = arange(n)*2*pi/n f=sin(x)*cos(4*x)+exp(sin(3*x)) df=cos(x)*cos(4*x)-4*sin(x)*sin(4*x)+3*cos(3*x)*exp(sin(3*x)) ddf=-17*sin(x)*cos(4*x)-8*cos(x)*sin(4*x)\ -9*sin(3*x)*exp(sin(3*x))+9*cos(3*x)**2*exp(sin(3*x)) assert_array_almost_equal(diff(f),df) assert_array_almost_equal(diff(df),ddf) assert_array_almost_equal(diff(ddf,-1),df) assert_array_almost_equal(diff(f,2),ddf)
def part2p(d13, d23, d33, u1, u2, u3, pz, w13, w23): for i in range(u1.shape[0]): aux = fftpack.diff(u1[i, :], period=pz) d13[i, :] = (d13[i, :] + aux) / 2 w13[i, :] = (w13[i, :] - aux) / 2 aux = fftpack.diff(u2[i, :], period=pz) d23[i, :] = (d23[i, :] + aux) / 2 w23[i, :] = (w23[i, :] - aux) / 2 d33[i, :] = fftpack.diff(u3[i, :], period=pz) return
def test_expr_large(self): for n in [2048,4096]: x = arange(n)*2*pi/n f = sin(x)*cos(4*x)+exp(sin(3*x)) df = cos(x)*cos(4*x)-4*sin(x)*sin(4*x)+3*cos(3*x)*exp(sin(3*x)) ddf = -17*sin(x)*cos(4*x)-8*cos(x)*sin(4*x)\ - 9*sin(3*x)*exp(sin(3*x))+9*cos(3*x)**2*exp(sin(3*x)) assert_array_almost_equal(diff(f),df) assert_array_almost_equal(diff(df),ddf) assert_array_almost_equal(diff(ddf,-1),df) assert_array_almost_equal(diff(f,2),ddf)
def test_expr(self): for n in [64,77,100,128,256,512,1024,2048,4096,8192][:5]: x = arange(n)*2*pi/n f = sin(x)*cos(4*x)+exp(sin(3*x)) df = cos(x)*cos(4*x)-4*sin(x)*sin(4*x)+3*cos(3*x)*exp(sin(3*x)) ddf = -17*sin(x)*cos(4*x)-8*cos(x)*sin(4*x)\ - 9*sin(3*x)*exp(sin(3*x))+9*cos(3*x)**2*exp(sin(3*x)) d1 = diff(f) assert_array_almost_equal(d1,df) assert_array_almost_equal(diff(df),ddf) assert_array_almost_equal(diff(f,2),ddf) assert_array_almost_equal(diff(ddf,-1),df)
def test_expr(self): for n in [64,77,100,128,256,512,1024,2048,4096,8192][:5]: x = arange(n)*2*pi/n f=sin(x)*cos(4*x)+exp(sin(3*x)) df=cos(x)*cos(4*x)-4*sin(x)*sin(4*x)+3*cos(3*x)*exp(sin(3*x)) ddf=-17*sin(x)*cos(4*x)-8*cos(x)*sin(4*x)\ -9*sin(3*x)*exp(sin(3*x))+9*cos(3*x)**2*exp(sin(3*x)) d1 = diff(f) assert_array_almost_equal(d1,df) assert_array_almost_equal(diff(df),ddf) assert_array_almost_equal(diff(f,2),ddf) assert_array_almost_equal(diff(ddf,-1),df)
def InitialCondition(Ao,Bo,x,loik,c): # Sech**2 initial condition xx = x-loik*np.pi #profile shift u0 = Ao/(cosh(Bo*xx))**2 #initial pulse u0x = diff(u0,period=2*np.pi*loik); #speed -c*u0x yx[:n] = u0 # space yx[n:] = -c*u0x; # speed return yx.real
def xdiff(self, pos_in_list): field = np.copy(self.dat[pos_in_list]) for iy in range(self.NY): for iz in range(self.NZ): field[:, iy, iz] = fft.diff(self.dat[pos_in_list][:, iy, iz], period=self.lx) self.dat.append(field) self.lut.append('[d/dx]' + self.lut[pos_in_list])
def ydiff(self, pos_in_list): field = np.copy(self.dat[pos_in_list]) for ix in range(self.NX): for iz in range(self.NZ): field[ix, :, iz] = fft.diff(self.dat[pos_in_list][ix, :, iz], period=self.ly) self.dat.append(field) self.lut.append('[d/dy]' + self.lut[pos_in_list])
def EQ(t,yx): # equation to be solved nn = len(yx)/2 ru = yx[:nn] # space displacement v = yx[nn:] # speed, or dr/dt, y[n] to y[2n-1] x_xx = diff(ru,2,period=2*np.pi*loik) # second spatial derivative ytx[:nn] = v #du/dt=v ytx[nn:] = x_xx # wave equation utt = c* uxx, c=1 in normalized equation return ytx
def computeDivergenceMap(S): """ Args: S: time-frequency representation from computeTimeFreqRep, shape (number of data points, number of channels) Returns: D: divergence map, shape (number of data points, number of channels) """ D_x = np.zeros(S.shape) D_y = np.zeros(S.shape) for i in range(S.shape[0]): D_x[i, :] = diff(S[i, :]) for j in range(S.shape[1]): D_y[:, j] = diff(S[:, j]) D = D_x + D_y return D
def integrate(self, method='fft'): """ """ if method == 'cum': self.data = integrate.cumtrapz(self.data, dx=self.delta, initial=0) elif method == 'fft': self.data = fftpack.diff(self.data, order=-1, period=self.duration()) else: raise NotImplementedError('method not implemented')
def differentiate(self, method='fft'): """ """ if method == 'grad': self.data = np.gradient(self.data, self.delta) elif method == 'fft': self.data = fftpack.diff(self.data, order=1, period=self.duration()) else: raise NotImplementedError('method not implemented')
def Bio2D(t,yx): #Improved HJ equation, dimensionless form nn = len(yx)/2 omega = oomega(nn) temp2 = [] ; ru = [] ; fft_r = [] temp2 = yx[:nn] #first part fft_r = fft(temp2,nn,-1) #r forward transform r_ajutine = np.arange(nn,dtype=np.complex128) # complex array ’D’ kkk = 0 while kkk < nn: #index 0 to n-1. r_ajutine[kkk] = fft_r[kkk]/(1+HH2*omega[kkk]**2) kkk = kkk+1 ru = ifft(r_ajutine).real #inverse transform end rr = ru*ru #nonlinear v = yx[nn:] #speed or dr/dt, y[n] to y[2n-1] x_x = diff(ru,1,period=2*np.pi*loik) # first derivative x2 = x_x*x_x # square of first derivative x_xx = diff(ru,2,period=2*np.pi*loik) # second derivative x_xxxx = diff(ru,4,period=2*np.pi*loik)# fourth derivative ytx[:nn] = v #du/dt=v ytx[nn:] = x_xx + PP * ru * x_xx + QQ * rr * x_xx + PP * x2 + 2 * QQ * ru * x2 - HH * x_xxxx return ytx
def __call__(self,u,dx): """Returns the derivative. Parameters --------- u : tslice.TimeSlice The data to be differentiated. dx : float The spatial step size. """ du = np.empty_like(u) du = fftpack.diff(u,self.order,self.period) return du
def find_peaks(signal, v_mph, v_mpd, v_cutMin, v_cutMax): #signal[-100:100] = 0.0 foierSignal = filterFourier(signal, v_cutMin, v_cutMax) diffFoierSignal = diff(foierSignal,1) dev_flag=True if (dev_flag == True): import matplotlib.pyplot as plt from random import randint plt.figure() plt.plot(np.linspace(100, len(signal)-100, num=len(signal.real[100:-100])), 40*signal.real[100:-100], '-r') plt.plot(np.linspace(100, len(diffFoierSignal)-100, num=len(diffFoierSignal.real[100:-100])), -diffFoierSignal.real[100:-100], '-g') plt.savefig('test/%sdifStress.png'%( randint(0,99) ), dpi=150) from detect_peaks import detect_peaks ind = detect_peaks(-diffFoierSignal.real[150:-150], mph=v_mph, mpd=v_mpd, show=False) vmax = [] vmin = [] id_vmax = [] id_vmin = [] i = 0 d_find = 50 while i < len(ind): s = signal if (len(ind) != 0 ): if ( np.fabs(len(s)-ind[i]) > d_find and ind[i] > d_find ): vmax.append(max( s[ ind[i] - d_find : ind[i] + d_find] ) ) id_vmax.append([k for k, j in enumerate(s[ind[i]-d_find:ind[i]+d_find]) if j == vmax[i]][0] + ind[i] - d_find) else: vmax.append(max( s[ ind[i] - d_find : ind[i]] ) ) id_vmax.append([k for k, j in enumerate(s[ind[i]-d_find:ind[i]]) if j == vmax[i]][0] + ind[i]-d_find) if ( np.fabs(len(s)-ind[i]) > d_find and ind[i] > d_find ): vmin.append(min( s[ ind[i] - d_find : ind[i] + d_find] ) ) id_vmin.append([k for k, j in enumerate(s[ind[i] - d_find : ind[i] + d_find]) if j == vmin[i]][0]+ind[i]-d_find) else: vmin.append(min( s[ ind[i] : ind[i] + d_find] ) ) id_vmin.append([k for k, j in enumerate(s[ ind[i] : ind[i] + d_find ] ) if j == vmin[i]][0]+ind[i]-d_find) i += 1 return [id_vmax,vmax,id_vmin,vmin]
def test_sin(self): for n in [32, 64, 77]: x = arange(n) * 2 * pi / n assert_array_almost_equal(diff(sin(x)), cos(x)) assert_array_almost_equal(diff(cos(x)), -sin(x)) assert_array_almost_equal(diff(sin(x), 2), -sin(x)) assert_array_almost_equal(diff(sin(x), 4), sin(x)) assert_array_almost_equal(diff(sin(4 * x)), 4 * cos(4 * x)) assert_array_almost_equal(diff(sin(sin(x))), cos(x) * cos(sin(x)))
def test_sin(self): for n in [32,64,77]: x = arange(n)*2*pi/n assert_array_almost_equal(diff(sin(x)),cos(x)) assert_array_almost_equal(diff(cos(x)),-sin(x)) assert_array_almost_equal(diff(sin(x),2),-sin(x)) assert_array_almost_equal(diff(sin(x),4),sin(x)) assert_array_almost_equal(diff(sin(4*x)),4*cos(4*x)) assert_array_almost_equal(diff(sin(sin(x))),cos(x)*cos(sin(x)))
def test_zero_nyquist(self): for k in [0,1,2,3,4,5,6]: for n in [32,33,64,56,55]: f = random((n,)) af = sum(f,axis=0)/n f = f-af # zeroing Nyquist mode: f = diff(diff(f,1),-1) assert_almost_equal(sum(f,axis=0),0.0) assert_array_almost_equal(diff(diff(f,k),-k),f) assert_array_almost_equal(diff(diff(f,-k),k),f)
def test_random_even(self): for k in [0,2,4,6]: for n in [60,32,64,56,55]: f = random((n,)) af = sum(f,axis=0)/n f = f-af # zeroing Nyquist mode: f = diff(diff(f,1),-1) assert_almost_equal(sum(f,axis=0),0.0) assert_array_almost_equal(diff(diff(f,k),-k),f) assert_array_almost_equal(diff(diff(f,-k),k),f)
def test_zero_nyquist(self): for k in [0, 1, 2, 3, 4, 5, 6]: for n in [32, 33, 64, 56, 55]: f = random((n, )) af = sum(f, axis=0) / n f = f - af # zeroing Nyquist mode: f = diff(diff(f, 1), -1) assert_almost_equal(sum(f, axis=0), 0.0) assert_array_almost_equal(diff(diff(f, k), -k), f) assert_array_almost_equal(diff(diff(f, -k), k), f)
def test_random_even(self): for k in [0, 2, 4, 6]: for n in [60, 32, 64, 56, 55]: f = random((n, )) af = sum(f, axis=0) / n f = f - af # zeroing Nyquist mode: f = diff(diff(f, 1), -1) assert_almost_equal(sum(f, axis=0), 0.0) assert_array_almost_equal(diff(diff(f, k), -k), f) assert_array_almost_equal(diff(diff(f, -k), k), f)
def harmonic_deriv(omega, r): r"""Return derivative of a harmonic function using frequency methods. Parameters ---------- omega: float Fundamendal frequency, in rad/sec, of repeating signal r: array_like | Array of rows of time histories to take the derivative of. | The 1 axis (each row) corresponds to a time history. | The length of the time histories *must be an odd integer*. Returns ------- s: array_like Function derivatives. The 1 axis (each row) corresponds to a time history. Examples -------- >>> import matplotlib.pyplot as plt >>> from mousai import * >>> import scipy as sp >>> from scipy import pi, sin, cos >>> f = 2 >>> omega = 2.*pi * f >>> numsteps = 11 >>> t = sp.arange(0,1/omega*2*pi,1/omega*2*pi/numsteps) >>> x = sp.array([sin(omega*t)]) >>> v = sp.array([omega*cos(omega*t)]) >>> states = sp.append(x,v,axis = 0) >>> state_derives = harmonic_deriv(omega,states) >>> plt.plot(t,states.T,t,state_derives.T,'x') [<matplotlib.line...] """ s = np.zeros_like(r) for i in np.arange(r.shape[0]): s[i, :] = fftp.diff(r[i, :]) * omega return np.real(s)
def part1p(d11, d12, d13, d22, d23, d33, u1, u2, u3, px, py, w12, w13, w23): for j in range(u1.shape[1]): d11[:, j] = fftpack.diff(u1[:, j], period=px) d12[:, j] = fftpack.diff(u2[:, j], period=px) w12[:, j] = d12[:, j] d13[:, j] = fftpack.diff(u3[:, j], period=px) w13[:, j] = d13[:, j] for i in range(u1.shape[0]): aux = fftpack.diff(u1[i, :], period=py) d12[i, :] = (d12[i, :] + aux) / 2 w12[i, :] = (w12[i, :] - aux) / 2 d22[i, :] = fftpack.diff(u2[i, :], period=py) d23[i, :] = fftpack.diff(u3[i, :], period=py) w23[i, :] = d23[i, :] d33[:, :] = -(d11[:, :] + d22[:, :]) return
def part1(d11, d12, d13, d22, d23, d33, u1, u2, u3, px, py, w12, w13, w23): for k in range(u1.shape[2]): for j in range(u1.shape[1]): d11[:, j, k] = fftpack.diff(u1[:, j, k], period=px) d12[:, j, k] = fftpack.diff(u2[:, j, k], period=px) w12[:, j, k] = d12[:, j, k] d13[:, j, k] = fftpack.diff(u3[:, j, k], period=px) w13[:, j, k] = d13[:, j, k] for i in range(u1.shape[0]): aux = fftpack.diff(u1[i, :, k], period=py) d12[i, :, k] = (d12[i, :, k] + aux) / 2 w12[i, :, k] = (w12[i, :, k] - aux) / 2 d22[i, :, k] = fftpack.diff(u2[i, :, k], period=py) d23[i, :, k] = fftpack.diff(u3[i, :, k], period=py) w23[i, :, k] = d23[i, :, k] return
def extract_features_single(dataset): fdataset = np.zeros((16)) from scipy.fftpack import diff, fft for i in range(len(dataset)): # x y z fdataset[0], fdataset[1], fdataset[2] = np.mean(dataset, axis=0) # mean fdataset[3], fdataset[4], fdataset[5] = np.std(dataset, axis=0) # stddev fdataset[6], fdataset[7], fdataset[8] = np.min(dataset, axis=0) # min fdataset[9], fdataset[10], fdataset[11] = np.max(dataset, axis=0) # max # corr fdataset[12] = sum((dataset[:, 0] - fdataset[0]) * (dataset[:, 1] - fdataset[1])) / sum( (dataset[:, 0] - fdataset[0])**2 * (dataset[:, 1] - fdataset[1])**2) fdataset[13] = sum((dataset[:, 0] - fdataset[0]) * (dataset[:, 2] - fdataset[2])) / sum( (dataset[:, 0] - fdataset[0])**2 * (dataset[:, 2] - fdataset[2])**2) fdataset[14] = sum((dataset[:, 2] - fdataset[2]) * (dataset[:, 1] - fdataset[1])) / sum( (dataset[:, 2] - fdataset[2])**2 * (dataset[:, 1] - fdataset[1])**2) fftdata = np.fft.fft(dataset[:]) fdataset[15] = np.sum(np.sqrt(fftdata.real**2 + fftdata.imag**2)) # mag fdataset[16] = np.sqrt(np.sum(fdataset[0], fdataset[1], fdataset[2])) # jerk fdataset[17] = diff(dataset[:, 0]) return np.array(fdataset)
def DDZ(var): #{{{docstring """ Calculates the first theta-derivative of a profile using spectral methods (assuming that we are using cylinder geometry). Also note that the profile must go from [0, 2*pi[ (which is standard output from BOUT++), and NOT [0, 2*pi] Parameters ---------- var : array The variable to take the z-derivative of J : array The Jacobian Returns ------- out : iterable The theta derivative of the profile """ #}}} if len(var.shape) != 4: raise ValueError("Input variable must be 4-dimensional") tLen, xLen, yLen, _ = var.shape out = np.zeros(var.shape) for tInd in range(tLen): for xInd in range(xLen): for yInd in range(yLen): out[tInd, xInd, yInd, :] =\ diff(var[tInd, xInd, yInd, :]) return out
def time_diff(self, size, soltype): if soltype == 'fft': diff(self.f, 3) else: direct_diff(self.f, 3)
def test_definition(self): for n in [16,17,64,127,32]: x = arange(n)*2*pi/n assert_array_almost_equal(diff(sin(x)),direct_diff(sin(x))) assert_array_almost_equal(diff(sin(x),2),direct_diff(sin(x),2)) assert_array_almost_equal(diff(sin(x),3),direct_diff(sin(x),3)) assert_array_almost_equal(diff(sin(x),4),direct_diff(sin(x),4)) assert_array_almost_equal(diff(sin(x),5),direct_diff(sin(x),5)) assert_array_almost_equal(diff(sin(2*x),3),direct_diff(sin(2*x),3)) assert_array_almost_equal(diff(sin(2*x),4),direct_diff(sin(2*x),4)) assert_array_almost_equal(diff(cos(x)),direct_diff(cos(x))) assert_array_almost_equal(diff(cos(x),2),direct_diff(cos(x),2)) assert_array_almost_equal(diff(cos(x),3),direct_diff(cos(x),3)) assert_array_almost_equal(diff(cos(x),4),direct_diff(cos(x),4)) assert_array_almost_equal(diff(cos(2*x)),direct_diff(cos(2*x))) assert_array_almost_equal(diff(sin(x*n/8)),direct_diff(sin(x*n/8))) assert_array_almost_equal(diff(cos(x*n/8)),direct_diff(cos(x*n/8))) for k in range(5): assert_array_almost_equal(diff(sin(4*x),k),direct_diff(sin(4*x),k)) assert_array_almost_equal(diff(cos(4*x),k),direct_diff(cos(4*x),k))
def test_definition(self): for n in [16, 17, 64, 127, 32]: x = arange(n) * 2 * pi / n assert_array_almost_equal(diff(sin(x)), direct_diff(sin(x))) assert_array_almost_equal(diff(sin(x), 2), direct_diff(sin(x), 2)) assert_array_almost_equal(diff(sin(x), 3), direct_diff(sin(x), 3)) assert_array_almost_equal(diff(sin(x), 4), direct_diff(sin(x), 4)) assert_array_almost_equal(diff(sin(x), 5), direct_diff(sin(x), 5)) assert_array_almost_equal(diff(sin(2 * x), 3), direct_diff(sin(2 * x), 3)) assert_array_almost_equal(diff(sin(2 * x), 4), direct_diff(sin(2 * x), 4)) assert_array_almost_equal(diff(cos(x)), direct_diff(cos(x))) assert_array_almost_equal(diff(cos(x), 2), direct_diff(cos(x), 2)) assert_array_almost_equal(diff(cos(x), 3), direct_diff(cos(x), 3)) assert_array_almost_equal(diff(cos(x), 4), direct_diff(cos(x), 4)) assert_array_almost_equal(diff(cos(2 * x)), direct_diff(cos(2 * x))) assert_array_almost_equal(diff(sin(x * n / 8)), direct_diff(sin(x * n / 8))) assert_array_almost_equal(diff(cos(x * n / 8)), direct_diff(cos(x * n / 8))) for k in range(5): assert_array_almost_equal(diff(sin(4 * x), k), direct_diff(sin(4 * x), k)) assert_array_almost_equal(diff(cos(4 * x), k), direct_diff(cos(4 * x), k))
# CsPbI3 - distorted PB_X=0.469972*NGX PB_Y=0.530081*NGY PB_Z=0.468559*NGZ # CsPbI3 - perfect cubic #PB_X=0.5*NGX #PB_Y=0.5*NGY #PB_Z=0.5*NGZ # MAPBI3 - pseudo cubic distorted #PB_X=0.476171*NGX #PB_Y=0.500031*NGY #PB_Z=0.475647*NGZ # Read out massive grad table, in {x,y,z} components print(grad_x[PB_X][PB_Y][PB_Z],grad_y[PB_X][PB_Y][PB_Z],grad_z[PB_X][PB_Y][PB_Z]) # Norm of electric field at this point print(np.linalg.norm([ grad_x[PB_X][PB_Y][PB_Z],grad_y[PB_X][PB_Y][PB_Z],grad_z[PB_X][PB_Y][PB_Z] ])) # OK, let's try this with a Spectral method (FFT) # JMF - Not currently working; unsure of data formats, need worked example from scipy import fftpack V_FFT=fftpack.fftn(grid_pot[:,:,:]) V_deriv=fftpack.diff(grid_pot[:,:,:]) #V_FFT,order=1) # Standard catch all to drop into ipython at end of script for variable inspection etc. from IPython import embed; embed() # End on an interactive ipython console to inspect variables etc.