# DCTIII

DCTIII is one of realizations of the DCT transform operator (Discrete Cosine transform); it is one of many discrete analogies of the integral operator CosFourier.

For a given natural number $$N$$, operator $$\mathrm{DCTIII}_N$$ converts any array $$F$$ of length $$N$$ to the array with elements

$$\!\!\!\!\!\!\!\!\!\!\!\!\!\! (1) ~ ~ ~ ~ \displaystyle (\mathrm{DCTIII}_N ~F) _k = \sum_{n=0}^{N-1} ~ F_n~ \cos \left(\frac{\pi}{N} \left(k\!+\!\frac{1}{2}\right) n \right) ~ ~ ~$$, $$~ ~ ~ k = 0, \dots, N\!-\!1$$

As in the case of other discrete Fourier transforms, the numeration of elements begins with zero. For the simple and efficient implementation, $$N=2^q$$ for some natural number $$q$$. Note that the size of the arrays is for unity smaller than in the case of DCTI.

## Inverse operator

Inverse of DCTIII can be easily expressed through the DCTII defined with

$$\!\!\!\!\!\!\!\!\!\!\!\!\!\! (2) ~ ~ ~ ~ \displaystyle (\mathrm{DCTIII}_N ~F) _k = \sum_{n=0}^{N-1} ~ F_n~ \cos \left(\frac{\pi}{N} \left(n\!+\!\frac{1}{2}\right) k \right) ~ ~ ~$$, $$~ ~ ~ k = 0, \dots, N\!-\!1$$

as

$$\!\!\!\!\!\!\!\!\!\!\!\!\!\! (3) ~ ~ ~ ~ \displaystyle (\mathrm {DCTII}_N ~ \mathrm {DCTIII}_N ~F)_n= (\mathrm {DCTIII}_N ~ \mathrm {DCTII}_N ~F)_n= \frac{N}{2} F_n$$

## Applications

DCTIII can be used for the assembling of a symmetric field indite the cavity of width $$\pi$$ with given coefficients $$F$$ in the series below. Let

$$\!\!\!\!\!\!\!\!\!\!\!\!\!\! (4) ~ ~ ~ ~ \displaystyle A(x)= \sum_{n=0}^\infty ~a_n~ \cos\left( \left(n+\frac{1}{2} \right) x \right)$$

Then at points $$x_m=\pi m/N$$, the field can be approximated with

$$\!\!\!\!\!\!\!\!\!\!\!\!\!\! (5) ~ ~ ~ ~ \displaystyle A(x_m)=A_m= (\mathrm{DCTIII}_N~ a)_m$$

where $$a$$ is array of coefficients above.

The inverse operation, id est, calculation of the Fourier coefficients for the symmetric field known at porints $$x_m$$, $$m=0..N-1$$, can be performed with DCTII as follows:

$$\!\!\!\!\!\!\!\!\!\!\!\!\!\! (6) ~ ~ ~ ~ \displaystyle a_m= \frac{2}{N} (\mathrm{DCTII}_N~ A)_m$$

These coefficients correspond to the expansion of the filed with symmetric modes of cavity of width $$\pi$$. Similar expansion using the only antisimmetric modes can be performed with the DST (Discrete sine transform).

## Numerical implementation and example

Numerilal implementation of the transform DCTIII consists of 3 files: zfour1.cin, zrealft.cin, zcosft2.cin.

In the example below, the field $$A(x)=\cos(x)+.1 \cos(3 x) + .01 \cos(5 x) + .001 \cos(7x)$$ is evaluated in the points with coordinates $$0$$, $$\pi/16$$, $$2\pi/16$$, $$3\pi/16$$, $$4\pi/16$$, $$5\pi/16$$, $$6\pi/16$$, $$7\pi/16$$. This field corresponds to the cavity of length $$\pi$$; the modes become zero at $$x=\pm \pi$$; the only symmetric modes are considered. Wave–numbers of symmetric modes are $$k=1$$, $$k=3$$, $$k=5$$, $$k=7$$; only 4 symmetric modes are excited in the example; the field $$A$$ is constructed with explicit expression above, and the coefficients are calculated (recovered) confirming the routine DCTII. Then, the field $$A$$ is reconstructed from the Fourier-coefficients with routine DCTII.

#include<math.h>
#include<stdio.h>
#include <stdlib.h>
using namespace std;
#include <complex>
#define z_type double
#include"zfour1.cin"
#include"zrealft.cin"
#include"zcosft2.cin"
main(){ z_type *a, *b, *c; double x; int j; unsigned long N=8;
a=(z_type *) malloc((size_t)((N)*sizeof(z_type)));
b=(z_type *) malloc((size_t)((N)*sizeof(z_type)));
c=(z_type *) malloc((size_t)((N)*sizeof(z_type)));
for(j=0;j<N;j++) {x=.5*M_PI/N*j; a[j]=b[j]=cos(x)+.1*cos(3*x) + .01*cos(5*x) + .001*cos(7*x);}
zcosft2(a-1,N,-1);
for(j=0;j<N;j++){a[j]*=2./N; c[j]=a[j];}
printf("back..\n");
zcosft2(a-1,N,1);
for(j=0;j<N;j++) printf("%12.8f%12.8f%12.8f%12.8f\n",(j+.5)*M_PI/N,b[j], c[j], a[j]);
free(a);
free(b);
free(c);
}


The output is copypasted below:

0.19634954  1.11100000  1.00000000  1.11100000
0.58904862  1.06968303  0.10000000  1.06968303
0.98174770  0.95739716  0.01000000  0.95739716
1.37444679  0.80159716  0.00100000  0.80159716
1.76714587  0.63003214  0.00000000  0.63003214
2.15984495  0.46027408  0.00000000  0.46027408
2.55254403  0.29915159  0.00000000  0.29915159
2.94524311  0.14686721  0.00000000  0.14686721


The 0th column shows the coordinates of nodes, at which the field is evaluated;

The first column shows the filed $$A$$ evaluated with the explicit formula above.

The second column shows the Fourier–coefficients: 1, 0.1, 0.01, 0.001 .

The last column shows that the field can be reconsructed with these coefficients with routine DCTIII.

While only few modes are excited, there is no need to use the special algorithm; the explicit formula is also fast. However, at large $$q$$, for $$N=2^q$$ components, the routine above is significantly faster than the explicit summation of harmonics.

For simulation of propagation of symmetric field, expanding it by harmonics, the Fourier coefficients should be complex; and the field is complex too. For this reason, the first argument of routine zcosft2 is z_type array, and z_type should be defined as [[Complex<fouble>]]. The second argument is unsigned integer, that indicates value $$N=2^q$$; where $$q$$ is assumed to be real number. The last argument should be $$-1$$ for evaluation of DCTIII and $$1$$ for evaluation of DCTII. The result is placed at the same array. In order to begin numeration of elements of array with zero, two characters $$-1$$ should be added to the name of the array at the call.