%pylab inline
from scipy.fftpack import dst, idst, dct, idct, fft, ifft, fftfreq
Populating the interactive namespace from numpy and matplotlib
help(ifft)
Help on function ifft in module scipy.fftpack.basic: ifft(x, n=None, axis=-1, overwrite_x=False) Return discrete inverse Fourier transform of real or complex sequence. The returned complex array contains ``y(0), y(1),..., y(n-1)`` where ``y(j) = (x * exp(2*pi*sqrt(-1)*j*np.arange(n)/n)).mean()``. Parameters ---------- x : array_like Transformed data to invert. n : int, optional Length of the inverse Fourier transform. If ``n < x.shape[axis]``, `x` is truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The default results in ``n = x.shape[axis]``. axis : int, optional Axis along which the ifft's are computed; the default is over the last axis (i.e., ``axis=-1``). overwrite_x : bool, optional If True, the contents of `x` can be destroyed; the default is False. Returns ------- ifft : ndarray of floats The inverse discrete Fourier transform. See Also -------- fft : Forward FFT Notes ----- This function is most efficient when `n` is a power of two, and least efficient when `n` is prime. If the data type of `x` is real, a "real IFFT" algorithm is automatically used, which roughly halves the computation time.
n = 99
N = (99 -1) /2
x = arange(n)/n * 2*pi
y = sin(3 * x)
k = fftfreq((n), 1/(n))
plot(x,y,label="sin 3x")
dy = real(ifft(fft(y) * k * 1j ))
plot(x,dy, label="fourier deriv")
legend()
<matplotlib.legend.Legend at 0x10e9dd908>
Fourier derivative doesn't work near boundaries, so we have to use Sin/Cos basis, which is more difficult to use
n = 100
x = arange(n+1)/n * pi
y = sin(3*x)
dy = real(ifft(fft(y) * fftfreq(n+1, 1/(n+1)) * 1j ))
plot(x,y, label='sin 3x')
plot(x,dy, label="fourier deriv")
legend()
<matplotlib.legend.Legend at 0x10eb85b70>
help(dst)
Help on function dst in module scipy.fftpack.realtransforms: dst(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False) Return the Discrete Sine Transform of arbitrary type sequence x. Parameters ---------- x : array_like The input array. type : {1, 2, 3}, optional Type of the DST (see Notes). Default type is 2. n : int, optional Length of the transform. If ``n < x.shape[axis]``, `x` is truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The default results in ``n = x.shape[axis]``. axis : int, optional Axis along which the dst is computed; the default is over the last axis (i.e., ``axis=-1``). norm : {None, 'ortho'}, optional Normalization mode (see Notes). Default is None. overwrite_x : bool, optional If True, the contents of `x` can be destroyed; the default is False. Returns ------- dst : ndarray of reals The transformed input array. See Also -------- idst : Inverse DST Notes ----- For a single dimension array ``x``. There are theoretically 8 types of the DST for different combinations of even/odd boundary conditions and boundary off sets [1]_, only the first 3 types are implemented in scipy. **Type I** There are several definitions of the DST-I; we use the following for ``norm=None``. DST-I assumes the input is odd around n=-1 and n=N. :: N-1 y[k] = 2 * sum x[n]*sin(pi*(k+1)*(n+1)/(N+1)) n=0 Only None is supported as normalization mode for DCT-I. Note also that the DCT-I is only supported for input size > 1 The (unnormalized) DCT-I is its own inverse, up to a factor `2(N+1)`. **Type II** There are several definitions of the DST-II; we use the following for ``norm=None``. DST-II assumes the input is odd around n=-1/2 and n=N-1/2; the output is odd around k=-1 and even around k=N-1 :: N-1 y[k] = 2* sum x[n]*sin(pi*(k+1)*(n+0.5)/N), 0 <= k < N. n=0 if ``norm='ortho'``, ``y[k]`` is multiplied by a scaling factor `f` :: f = sqrt(1/(4*N)) if k == 0 f = sqrt(1/(2*N)) otherwise. **Type III** There are several definitions of the DST-III, we use the following (for ``norm=None``). DST-III assumes the input is odd around n=-1 and even around n=N-1 :: N-2 y[k] = x[N-1]*(-1)**k + 2* sum x[n]*sin(pi*(k+0.5)*(n+1)/N), 0 <= k < N. n=0 The (unnormalized) DCT-III is the inverse of the (unnormalized) DCT-II, up to a factor `2N`. The orthonormalized DST-III is exactly the inverse of the orthonormalized DST-II. .. versionadded:: 0.11.0 References ---------- .. [1] Wikipedia, "Discrete sine transform", http://en.wikipedia.org/wiki/Discrete_sine_transform
Which of the three DST types looks like it can be written as $f(x) = \sum_k sin(x k)$?
By the way, there are also three DCTs:
help(dct)
Help on function dct in module scipy.fftpack.realtransforms: dct(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False) Return the Discrete Cosine Transform of arbitrary type sequence x. Parameters ---------- x : array_like The input array. type : {1, 2, 3}, optional Type of the DCT (see Notes). Default type is 2. n : int, optional Length of the transform. If ``n < x.shape[axis]``, `x` is truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The default results in ``n = x.shape[axis]``. axis : int, optional Axis along which the dct is computed; the default is over the last axis (i.e., ``axis=-1``). norm : {None, 'ortho'}, optional Normalization mode (see Notes). Default is None. overwrite_x : bool, optional If True, the contents of `x` can be destroyed; the default is False. Returns ------- y : ndarray of real The transformed input array. See Also -------- idct : Inverse DCT Notes ----- For a single dimension array ``x``, ``dct(x, norm='ortho')`` is equal to MATLAB ``dct(x)``. There are theoretically 8 types of the DCT, only the first 3 types are implemented in scipy. 'The' DCT generally refers to DCT type 2, and 'the' Inverse DCT generally refers to DCT type 3. **Type I** There are several definitions of the DCT-I; we use the following (for ``norm=None``):: N-2 y[k] = x[0] + (-1)**k x[N-1] + 2 * sum x[n]*cos(pi*k*n/(N-1)) n=1 Only None is supported as normalization mode for DCT-I. Note also that the DCT-I is only supported for input size > 1 **Type II** There are several definitions of the DCT-II; we use the following (for ``norm=None``):: N-1 y[k] = 2* sum x[n]*cos(pi*k*(2n+1)/(2*N)), 0 <= k < N. n=0 If ``norm='ortho'``, ``y[k]`` is multiplied by a scaling factor `f`:: f = sqrt(1/(4*N)) if k = 0, f = sqrt(1/(2*N)) otherwise. Which makes the corresponding matrix of coefficients orthonormal (``OO' = Id``). **Type III** There are several definitions, we use the following (for ``norm=None``):: N-1 y[k] = x[0] + 2 * sum x[n]*cos(pi*(k+0.5)*n/N), 0 <= k < N. n=1 or, for ``norm='ortho'`` and 0 <= k < N:: N-1 y[k] = x[0] / sqrt(N) + sqrt(2/N) * sum x[n]*cos(pi*(k+0.5)*n/N) n=1 The (unnormalized) DCT-III is the inverse of the (unnormalized) DCT-II, up to a factor `2N`. The orthonormalized DCT-III is exactly the inverse of the orthonormalized DCT-II. References ---------- .. [1] 'A Fast Cosine Transform in One and Two Dimensions', by J. Makhoul, `IEEE Transactions on acoustics, speech and signal processing` vol. 28(1), pp. 27-34, http://dx.doi.org/10.1109/TASSP.1980.1163351 (1980). .. [2] Wikipedia, "Discrete cosine transform", http://en.wikipedia.org/wiki/Discrete_cosine_transform Examples -------- The Type 1 DCT is equivalent to the FFT (though faster) for real, even-symmetrical inputs. The output is also real and even-symmetrical. Half of the FFT input is used to generate half of the FFT output: >>> from scipy.fftpack import fft, dct >>> fft(np.array([4., 3., 5., 10., 5., 3.])).real array([ 30., -8., 6., -2., 6., -8.]) >>> dct(np.array([4., 3., 5., 10.]), 1) array([ 30., -8., 6., -2.])
# remove end points
yy = y[1:-1]
# idst type-1
y_dst = idst(yy, type=1)
# take deriv
dy_dct = arange(1,len(y_dst)+1) * y_dst
# dct type-1 needs boundaries
dy_dct = hstack((0, dy_dct, 0))
# need to normalize properly
# normalization is not handled automatically
# in other words:
# dst(idst(x)) != x
dy = dct(dy_dct, type=1)/dy_dct.shape[0]/2
plot(x,y, label='sin 3x')
plot(x,dy, label='Sin/Cos deriv')
legend()
<matplotlib.legend.Legend at 0x10ecc2dd8>
The variable are defined by $$\nabla \psi = (-u, v)$$ $$ J(a,b) = b_x a_y - a_x b_y$$
This jacobian is hard to approximate, and Arakawa (1966) is a famous finite difference scheme.
%%bash
open /Users/noah/Dropbox/Papers/Journal\ Article/Arakawa_1966_Computational\ design\ for\ long-term\ numerical\ integration\ of\ the\ equations\ of.pdf /Users/noah/Dropbox/Papers/Journal\ Article/Fix_1975_Finite\ Element\ Models\ for\ Ocean\ Circulation\ Problems.pdf /Users/noah/Dropbox/Papers/Journal\ Article/Jespersen_1974_Arakawa\'s\ method\ is\ a\ finite-element\ method.pdf /Users/noah/Dropbox/Papers/Journal\ Article/Naulin_Nielsen_2003_Accuracy\ of\ Spectral\ and\ Finite\ Difference\ Schemes\ in\ 2D\ Advection\ Problems.pdf
Spectral code
python -m gnl.pdes.barotropic_spec
FD code
python -m gnl.pdes.barotropic_fd_par
!open -a ITerm.app