def div(X): if X.shape==(1,): shape=() else: shape=X.shape[:-1] assert(X.shape[-1]==X.dim) assert(X.order==1) dX=Tensor(shape=shape, N=X.N, Fourier=True, fft_form=X.fft_form) if X.Fourier: FX=X else: F=DFT(N=X.N, fft_form=X.fft_form) FX=F(X) dim=len(X.N) freq=Grid.get_freq(X.N, X.Y, fft_form=FX.fft_form) strfreq='xyz' coef=2*np.pi*1j for ii in range(X.dim): mul_str='{0},...{1}->...{1}'.format(strfreq[ii], strfreq[:dim]) dX.val+=np.einsum(mul_str, coef*freq[ii], FX.val[ii], dtype=np.complex) if not X.Fourier: iF=DFT(N=X.N, inverse=True, fft_form=dX.fft_form) dX=iF(dX) dX.name='div({0})'.format(X.name[:10]) return dX
def div(X): if X.shape == (1, ): shape = () else: shape = X.shape[:-1] assert (X.shape[-1] == X.dim) assert (X.order == 1) dX = Tensor(shape=shape, N=X.N, Fourier=True, fft_form=X.fft_form) if X.Fourier: FX = X else: F = DFT(N=X.N, fft_form=X.fft_form) FX = F(X) dim = len(X.N) freq = Grid.get_freq(X.N, X.Y, fft_form=FX.fft_form) strfreq = 'xyz' coef = 2 * np.pi * 1j for ii in range(X.dim): mul_str = '{0},...{1}->...{1}'.format(strfreq[ii], strfreq[:dim]) dX.val += np.einsum(mul_str, coef * freq[ii], FX.val[ii], dtype=np.complex) if not X.Fourier: iF = DFT(N=X.N, inverse=True, fft_form=dX.fft_form) dX = iF(dX) dX.name = 'div({0})'.format(X.name[:10]) return dX
def grad(X): if X.shape==(1,): shape=(X.dim,) else: shape=X.shape+(X.dim,) name='grad({0})'.format(X.name[:10]) gX=Tensor(name=name, shape=shape, N=X.N, Fourier=True, fft_form=X.fft_form) if X.Fourier: FX=X else: F=DFT(N=X.N, fft_form=X.fft_form) # TODO:change to X.fourier() FX=F(X) dim=len(X.N) freq=Grid.get_freq(X.N, X.Y, fft_form=X.fft_form) strfreq='xyz' coef=2*np.pi*1j val=np.empty((X.dim,)+X.shape+X.N_fft, dtype=np.complex) for ii in range(X.dim): mul_str='{0},...{1}->...{1}'.format(strfreq[ii], strfreq[:dim]) val[ii]=np.einsum(mul_str, coef*freq[ii], FX.val, dtype=np.complex) if X.shape==(1,): gX.val=np.squeeze(val) else: gX.val=np.moveaxis(val, 0, X.order) if not X.Fourier: iF=DFT(N=X.N, inverse=True, fft_form=gX.fft_form) gX=iF(gX) gX.name='grad({0})'.format(X.name[:10]) return gX