Like the discrete Fourier transform, the Chirp Z transform is an algorithm that, given a polynomial and , computes ; it does not require to be a root of unity. It can also be used for number-theoretic transforms. The Chirp Z transform and its inverse are introduced below.
Chirp Z transform
By definition, the Chirp Z transform can be written as
where and .
Bluestein algorithm
Consider
where . We can construct
Here , and for we have
Also, and . The computation can be completed with one polynomial multiplication; this algorithm is called the Bluestein algorithm.
Template (P6800 [Template] Chirp Z-Transform)
std::vector<uint> CZT(const std::vector<uint>& f, uint q, int n) {
if (f.empty() || n == 0) return std::vector<uint>(n);
const int m = f.size();
if (q == 0) {
std::vector<uint> res(n, f[0]);
for (int i = 1; i < m; ++i)
if ((res[0] += f[i]) >= MOD) res[0] -= MOD;
return res;
}
// H[i] = q^(binom(i + 1, 2))
std::vector<uint> H(std::max(m, n - 1));
H[0] = 1; // H[0] = q^0
uint qi = 1; // qi = q^i
for (int i = 1; i < (int)H.size(); ++i) {
qi = (ull)qi * q % MOD;
// H[i] = q^(binom(i, 2)) * q^i
H[i] = (ull)H[i - 1] * qi % MOD;
}
// F[i] = f[i] * q^(binom(i + 1, 2))
std::vector<uint> F(m);
for (int i = 0; i < m; ++i) F[i] = (ull)f[i] * H[i] % MOD;
std::vector<uint> G_p(m + n - 1);
// G[i] = q^(-binom(i, 2)), -(m - 1) ≤ i < n
uint* const G = G_p.data() + (m - 1);
const uint iq = InvMod(q);
// G[-(m - 1)] = q^(-binom(-(m - 1), 2)),
// binom(-(m - 1), 2) = (-(m - 1)) * (-(m - 1) - 1) / 2
// = (m - 1) * m / 2
G[-(m - 1)] = PowMod(iq, (ull)(m - 1) * m / 2);
uint qmi = PowMod(q, m - 1); // q^(-i), -(m - 1) ≤ i < n
for (int i = -(m - 1) + 1; i < n; ++i) {
// q^(-binom(i, 2)) = q^(-binom(i - 1, 2)) * q^(-(i - 1))
G[i] = (ull)G[i - 1] * qmi % MOD;
// q^(-i) = q^(-(i - 1)) * q^(-1)
qmi = (ull)qmi * iq % MOD;
}
// res[i] = q^(-binom(i, 2)) * f(q^i), 0 ≤ i < n
std::vector<uint> res = MiddleProduct(G_p, F);
for (int i = 1; i < n; ++i) res[i] = (ull)res[i] * H[i - 1] % MOD;
return res;
}
Inverse Chirp Z transform
The inverse Chirp Z transform can be written as
where and , and for all . This is the condition for polynomial interpolation.
Bostan–Schost algorithm
Recall that the Lagrange interpolation formula is
where for all . As in fast polynomial interpolation, let . By L'Hôpital's rule,
The modified Lagrange interpolation formula is
Therefore,
where . If is even, let and . Then
which lets us compute quickly. We then use the Bluestein algorithm to compute . Let . Then
Since , we only need to compute , where . That is,
where . We can use the Bluestein algorithm to compute .
In short, we perform the following computations:
- Compute using decrease and conquer.
- Use the Bluestein algorithm to compute .
- Use the Bluestein algorithm to compute .
- Compute with one polynomial multiplication.
The time complexity of every step equals the time complexity of multiplying two polynomials of degree at most .
Template implementation
std::vector<uint> InvCZT(const std::vector<uint>& f, uint q) {
if (f.empty()) return {};
const int n = f.size();
if (q == 0) {
// f[0] = f(1), f[1] = f(0)
assert(n <= 2);
if (n == 1) return {f[0]}; // deg(f) < 1
return {f[1], (f[0] + MOD - f[1]) % MOD}; // deg(f) < 2
}
// prod[0 ≤ i < n] (x - q^i)
const auto DaC = [q](auto&& DaC, int n) -> std::vector<uint> {
if (n == 1) return {MOD - 1, 1u};
// H = prod[0 ≤ i < ⌊n/2⌋] (x - q^i)
const std::vector<uint> H = DaC(DaC, n / 2);
// HH = H(x / q^(⌊n/2⌋)) * q^(⌊n/2⌋^2)
std::vector<uint> HH = H;
const uint iqn = InvMod(PowMod(q, n / 2));
uint qq = PowMod(q, (ull)(n / 2) * (n / 2));
for (int i = 0; i <= n / 2; ++i)
HH[i] = (ull)HH[i] * qq % MOD, qq = (ull)qq * iqn % MOD;
std::vector<uint> res = Product(H, HH);
if (n & 1) {
res.resize(n + 1);
const uint qnm1 = MOD - PowMod(q, n - 1);
for (int i = n; i > 0; --i) {
if ((res[i] += res[i - 1]) >= MOD) res[i] -= MOD;
res[i - 1] = (ull)res[i - 1] * qnm1 % MOD;
}
}
return res;
};
const std::vector<uint> M = DaC(DaC, n);
// C[i] = (M'(q^i))^(-1)
std::vector<uint> C = InvModBatch(CZT(Deriv(M), q, n));
// C[i] = f(q^i) / M'(q^i)
for (int i = 0; i < n; ++i) C[i] = (ull)f[i] * C[i] % MOD;
// C(x) ← C(q^(-1)x)
const uint iq = InvMod(q);
uint qmi = 1;
for (int i = 0; i < n; ++i)
C[i] = (ull)C[i] * qmi % MOD, qmi = (ull)qmi * iq % MOD;
C = CZT(C, iq, n);
for (int i = 0; i < n; ++i)
if (C[i] != 0) C[i] = MOD - C[i];
std::vector<uint> res = Product(M, C);
res.resize(n);
return res;
}