FLINT3.7.0-dev
Algorithm

Ball matrix multiplication

Cost
For n×nn \times n matrices at pp bits: one exact integer matrix product of O(p)O(p)-bit entries per block (typically one block), using the fast algorithms of fmpz_mat, plus O(n3)O(n^3) double operations for the radii, against O(n3)O(n^3) multiplications at pp bits for the classical algorithm.

The product C=ABC = AB of an M×NM \times N matrix AA and an N×PN \times P matrix BB with ball entries A=[Am±Ar]A = [A_m \pm A_r], B=[Bm±Br]B = [B_m \pm B_r] must satisfy A~B~∈C\tilde A \tilde B \in C for all matrices A~∈A\tilde A \in A, B~∈B\tilde B \in B. Since

A~B~−AmBm=(A~−Am)B~+Am(B~−Bm),\tilde A \tilde B - A_m B_m = (\tilde A - A_m) \tilde B + A_m (\tilde B - B_m),

the entrywise bound

∣A~B~−AmBm∣≤Ar(∣Bm∣+Br)+∣Am∣Br|\tilde A \tilde B - A_m B_m| \le A_r (|B_m| + B_r) + |A_m| B_r

holds (with absolute values and inequalities taken entrywise), so it is enough to compute an enclosure of the midpoint product AmBmA_m B_m and an upper bound for the radius product on the right. The classical algorithm computes each entry as a dot product in ball arithmetic (arb_dot, see Dot products of balls), which does exactly this entry by entry with O(N)O(N) multiplications of pp-bit numbers. For large matrices, both parts can instead be done with fast matrix multiplication, as observed by Rump for midpoint-radius interval matrices in floating-point arithmetic [Rum1999]; FLINT does this in arb_mat_mul_block, with exact integer products for the midpoints.

Midpoints: exact integer matrix products#

Every midpoint is a binary floating-point number a=u⋅2ea = u \cdot 2^e with an odd integer uu of at most pp bits. If all entries in row ii of AmA_m are multiples of 2si2^{s_i} and have absolute value less than 2si+h2^{s_i + h}, then 2−si2^{-s_i} times the row is a row of integers of at most hh bits, and similarly for the columns of BmB_m with exponents tjt_j. The scaled product of integer matrices is then exact, and (AmBm)ij=2si+tj(A^B^)ij(A_m B_m)_{ij} = 2^{s_i + t_j} (\hat A \hat B)_{ij}.

Algorithm 1: Block multiplication of midpoints

Input: AmA_m, BmB_m, precision pp. Let bb be the largest number of bits of any midpoint and H=1.25min⁡(p,b)+192H = 1.25 \min(p, b) + 192.

  1. Split the inner index range 0≤k<N0 \le k < N into consecutive blocks KK, each as long as possible subject to the condition that in each row ii of AmA_m (restricted to the columns in KK) and in each column jj of BmB_m (restricted to the rows in KK), the nonzero entries span at most HH bits, from the lowest set bit of any of them to the highest.
  2. For a block with at least 30 indices, convert the block to integer matrices A^\hat A, B^\hat B with the row and column scalings above, compute A^B^\hat A \hat B exactly with fmpz_mat_mul, round each entry of 2si+tj(A^B^)ij2^{s_i + t_j} (\hat A \hat B)_{ij} to pp bits and add it to CijC_{ij} in ball arithmetic.
  3. For a shorter block (at least 30 indices are taken anyway), add the dot products of the midpoints over the block with arb_dot.

For matrices whose entries in each row and column have similar magnitudes, which is the common case, there is a single block, and the computed midpoint of each entry has a single rounding error of at most half an ulp at pp bits: the result is as accurate as the classical algorithm, which also rounds each dot product once. With kk blocks, each entry accumulates at most kk roundings. Entries of very different magnitude in the same row (for example, a matrix with a huge first column) create several blocks, and a matrix with huge dynamic range degenerates to classical multiplication in blocks of 30. The bound HH of about 1.25p1.25p bits allows the integer entries to be somewhat longer than the precision, so that ordinary dynamic range within a row does not split blocks; the constant 192 accommodates low-precision inputs.

The integer matrix multiplication fmpz_mat_mul chooses its algorithm from the dimension and the entry size (fmpz_mat.h). At precisions of a few hundred bits, its multimodular method, which replaces the pp-bit products by matrix products modulo word-size primes, gives most of the speedup over the classical algorithm.

Radii: products in double precision#

The radius term Ar(∣Bm∣+Br)+∣Am∣BrA_r(|B_m| + B_r) + |A_m| B_r needs only an upper bound of moderate accuracy, so it is computed with matrices of nonnegative double numbers. The magnitudes ∣Am∣+Ar|A_m| + A_r, ∣Bm∣|B_m|, ArA_r, BrB_r are converted to mag_t upper bounds (30-bit mantissas), and _arb_mat_addmul_rad_mag_fast again splits the inner dimension into blocks in which the exponents of each row and column span at most 900 bits. In a block, each row and each column is scaled by a power of two so that all entries are representable as normal double numbers without rounding, and the product is computed with ordinary double arithmetic in cache-friendly blocks.

Lemma 1. Let xk,yk≥0x_k, y_k \ge 0 be double numbers and let ss be the result of summing the products xkykx_k y_k, k=1,…,nk = 1, \ldots, n, in any order with rounding to nearest, without overflow or underflow. Then s(1+2.01(n+1)⋅2−52)≥∑kxkyks (1 + 2.01(n + 1) \cdot 2^{-52}) \ge \sum_k x_k y_k.

Proof. With unit roundoff u=2−53u = 2^{-53}, each computed product and each partial sum of nonnegative terms has relative error at most uu, and the standard error analysis of summation gives ∑kxkyk≤s(1−u)−n\sum_k x_k y_k \le s (1 - u)^{-n}, since each term passes through at most nn roundings (one multiplication and at most n−1n - 1 additions) [Hig2002]. For nu<0.01nu < 0.01 (larger nn would need matrices with about 101410^{14} columns), (1−u)−n≤1+nu/(1−nu)≤1+1.02nu(1 - u)^{-n} \le 1 + nu/(1 - nu) \le 1 + 1.02 nu, which is well below the stated factor; the margin also covers the rounding of the final multiplication by the factor.

The scaled double products are multiplied by this factor and added, with rounding upwards, to the radii of CC; blocks of at most 5 indices use mag_t arithmetic directly. The radius computation costs O(MNP)O(MNP) floating-point operations in the worst case, but these are much cheaper than pp-bit multiplications, and for exact inputs (all radii zero) it is skipped. If only one factor is exact, only one radius product is computed.

Theorem 2. The matrix CC computed by arb_mat_mul_block contains A~B~\tilde A \tilde B for all A~∈A\tilde A \in A and B~∈B\tilde B \in B.

Proof. The midpoint part encloses AmBmA_m B_m: in each block, the integer product is exact and the rounding to pp bits and the addition are performed in ball arithmetic, which adds the rounding errors to the radius. The radius part adds an upper bound for Ar(∣Bm∣+Br)+∣Am∣BrA_r(|B_m| + B_r) + |A_m| B_r by Lemma 1 and the upward rounding of the conversions between mag_t and double, which bounds ∣A~B~−AmBm∣|\tilde A \tilde B - A_m B_m| by the identity at the beginning of this section.

Algorithm selection#

arb_mat_mul uses the classical algorithm (threaded if several threads are available and the matrices are not tiny) when one of the dimensions is at most 60 (for p≤128p \le 128), 50 (for p≤512p \le 512) or 40, and arb_mat_mul_block otherwise. The block algorithm itself falls back to classical multiplication if some midpoint has more than 8000 bits, if both matrices have less than 10% nonzero entries and H>1024H > 1024, or if some exponent is too large for the fast integer exponent arithmetic.

For complex matrices, acb_mat_mul_reorder writes A=A1+iA2A = A_1 + i A_2 and B=B1+iB2B = B_1 + i B_2 and computes the real and imaginary parts A1B1−A2B2A_1 B_1 - A_2 B_2 and A1B2+A2B1A_1 B_2 + A_2 B_1 with four real block multiplications (two if one of the matrices is real, one if both are). acb_mat_mul uses it when the inner dimension NN and the number of columns PP of BB satisfy min⁡(N,P)≥max⁡(20,5+b/64)\min(N, P) \ge \max(20, 5 + b/64), where b<8000b < 8000 is the minimum of pp and the largest number of bits of a midpoint; otherwise it multiplies classically (threaded if possible).

Use in FLINT#

Referenced from#

References#

Cite this page

The FLINT team, “Ball matrix multiplication,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/ball-arithmetic/matrix-multiplication.html (generated 2026-11-02).