FPS Log and Exp: Turn Products of Choices into Additive Structure

A generating function with thousands of factors is often easier to compute after taking a formal logarithm. The payoff is not symbolic elegance: multiplying many factors becomes summing their coefficient contributions, after which one fast exponential reconstructs the answer.
Assume coefficients lie in a field of characteristic , the required precision is smaller than , and fast multiplication and series inversion are available.
Derive log from a derivative
Normalize and define the logarithm with constant zero. Then
Differentiate by multiplying coefficient by . Multiply by the inverse, keep degrees below , then integrate by dividing coefficient by . Only one integration constant is missing, and our normalization fixes it to zero.
This division explains the restriction : degree would require division by zero in the field. It is not a floating-point precision issue.
Derive exp by solving an equation
For , seek satisfying . Suppose it is already correct modulo . Let , which starts at degree . Set
Indeed, modulo because all higher powers of start at degree . Therefore the error in disappears through the doubled precision.
B = [1]while length(B) < n: t = min(2 * length(B), n) correction = F[0:t] - log(B) modulo x^t correction[0] += 1 B = truncate(B * correction, t)Our C++ header provides logarithm and exponential. It checks the required constants. The baseline costs with NTT multiplication because all Newton stages have geometrically increasing sizes.
application: unrestricted copies of selected sizes
Suppose a construction is a multiset of pieces with allowed sizes in . A size may be used any number of times, and order does not matter. Its generating function is
Each factor chooses zero, one, two, or more copies of a piece. Taking the logarithm gives
The coefficient at is therefore
Build these divisor sums by looping through multiples of every allowed , multiply by modular inverses of , and compute . For an arbitrary allowed-size set bounded by , the multiple loops cost in the worst case.
For , the first seven coefficients are . The two constructions of total six are three twos and two threes. The logarithm coefficients through degree six are . Substituting these into the exponential recovers the same count.
This example also distinguishes sequences from multisets. If order matters, the generating function is instead. For total five, the ordered arrangements and are different, while the multiset construction counts them once.
Generalization and failure cases
If there are independent colors of size , the factor is and the divisor contribution becomes . Negative exponents in a formal product can encode inclusion-exclusion, but no longer automatically describe a family of objects with nonnegative counts.
Testing only exp(log(A)) = A can conceal two mutually compatible bugs. Also compare against the direct recurrence for small random inputs. The companion checks include algebraic round trips; the partition example supplies an independent combinatorial check.
Exercise. Each size may be used at most once. Derive the logarithm of , identify the alternating signs, and compute the answer for allowed sizes through degree eight.
Support & Share
If this article helped you, please share or support!




























































Conversation
Comments powered by Waline