Video failed to load

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

519 words
3 minutes
FPS Log and Exp: Turn Products of Choices into Additive Structure
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 pp, the required precision is smaller than pp, and fast multiplication and series inversion are available.

Derive log from a derivative#

Normalize A0=1A_0=1 and define the logarithm with constant zero. Then

log⁡A=∫A′(x)A(x)−1 dx.\log A=\int A'(x)A(x)^{-1}\,dx.

Differentiate by multiplying coefficient AiA_i by ii. Multiply by the inverse, keep degrees below n−1n-1, then integrate by dividing coefficient i−1i-1 by ii. Only one integration constant is missing, and our normalization fixes it to zero.

This division explains the restriction n<pn<p: degree pp would require division by zero in the field. It is not a floating-point precision issue.

Derive exp by solving an equation#

For F0=0F_0=0, seek B0=1B_0=1 satisfying log⁡B=F\log B=F. Suppose it is already correct modulo xmx^m. Let D=F−log⁡BD=F-\log B, which starts at degree mm. Set

Bnew=B(1+D)(modx2m).B_{\text{new}}=B(1+D)\pmod{x^{2m}}.

Indeed, log⁡(1+D)=D\log(1+D)=D modulo x2mx^{2m} because all higher powers of DD start at degree 2m2m. Therefore the error in log⁡B−F\log B-F 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 O(nlog⁡n)O(n\log n) 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 DD. A size may be used any number of times, and order does not matter. Its generating function is

G(x)=∏d∈D(1−xd)−1.G(x)=\prod_{d\in D}(1-x^d)^{-1}.

Each factor chooses zero, one, two, or more copies of a piece. Taking the logarithm gives

log⁡G=∑d∈D∑j≥1xdjj.\log G=\sum_{d\in D}\sum_{j\ge1}\frac{x^{dj}}j.

The coefficient at xkx^k is therefore

Lk=1k∑d∣k, d∈Dd.L_k=\frac1k\sum_{d\mid k,\ d\in D}d.

Build these divisor sums by looping through multiples of every allowed dd, multiply by modular inverses of kk, and compute G=exp⁡LG=\exp L. For an arbitrary allowed-size set bounded by nn, the multiple loops cost O(nlog⁡n)O(n\log n) in the worst case.

For D={2,3}D=\{2,3\}, the first seven coefficients are [1,0,1,1,1,1,2][1,0,1,1,1,1,2]. The two constructions of total six are three twos and two threes. The logarithm coefficients through degree six are [0,0,1,1,1/2,0,5/6][0,0,1,1,1/2,0,5/6]. Substituting these into the exponential recovers the same count.

This example also distinguishes sequences from multisets. If order matters, the generating function is 1/(1−∑d∈Dxd)1/(1-\sum_{d\in D}x^d) instead. For total five, the ordered arrangements (2,3)(2,3) and (3,2)(3,2) are different, while the multiset construction counts them once.

Generalization and failure cases#

If there are cdc_d independent colors of size dd, the factor is (1−xd)−cd(1-x^d)^{-c_d} and the divisor contribution becomes dcdd c_d. 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 kGk=∑i=1kiLiGk−ikG_k=\sum_{i=1}^k iL_iG_{k-i} for small random inputs. The companion checks include algebraic round trips; the partition example supplies an independent combinatorial check.

Exercise. Each size dd may be used at most once. Derive the logarithm of ∏d(1+xd)\prod_d(1+x^d), identify the alternating signs, and compute the answer for allowed sizes {1,3,4}\{1,3,4\} through degree eight.

Support & Share

If this article helped you, please share or support!

Sponsor
FPS Log and Exp: Turn Products of Choices into Additive Structure
https://blog.wsh.moe/posts/fps-log-exp/
Author
Wsh
Published at
2026-10-24

Comments

LEAVE A NOTE

Join the conversation

Markdown supported

Be kind. Good conversations start with you.

Conversation

Loading conversation…

Comments powered by Waline

Profile Image of the Author
Wsh
Nice to meet you! Hello, I'm Wsh.
Announcement
Any news or updates will be shared here, so check back anytime! Thanks for stopping by, meow >.<
Categories
Tags
Site Statistics
Posts
27
Categories
7
Tags
69
Total Words
124,615
Running Days
0 days
Last Activity
0 days ago
Site Info
Build Platform
Vercel
Blog Version
Firefly v6.16.8
License
CC BY-NC-SA 4.0
Table of Contents