A Julia package that registers the Polynomial type from the Polynomials package with the MutableArithmetics package
Author jverzani
1 Star
Updated Last
2 Years Ago
Started In
August 2022


Stable Dev Build Status Coverage

Add support for MutableArithmetics to Polynomials.

Based on work by @blegat in PR #331.

While polynomials of type Polynomial are mutable objects, operations such as +, -, *, always create new polynomials without modifying its arguments. The time needed for these allocations and copies of the polynomial coefficients may be noticeable in some use cases. This is amplified when the coefficients are for instance BigInt or BigFloat which are mutable themself. This can be avoided by modifying existing polynomials to contain the result of the operation using the MutableArithmetics (MA) API.

Consider for instance the following arrays of polynomials

using Polynomials
d, m, n = 30, 20, 20
p(d) = Polynomial(big.(1:d))
A = [p(d) for i in 1:m, j in 1:n]
b = [p(d) for i in 1:n]

In this case, the arrays are mutable objects for which the elements are mutable polynomials which have mutable coefficients (BigInts). These three nested levels of mutable objects communicate with the MA API in order to reduce allocation. Calling A * b requires approximately 40 MiB due to 2 M allocations as it does not exploit any mutability. Using

using PolynomialsMutableArithmetics

To register Polynomials with MutableArithmetics, then

using MutableArithmetics
const MA = MutableArithmetics
MA.operate(*, A, b)

exploits the mutability and hence only allocates approximately 70 KiB due to 4 k allocations. If the resulting vector is already allocated, e.g.,

z(d) = Polynomial([zero(BigInt) for i in 1:d])
c = [z(2d - 1) for i in 1:m]

then we can exploit its mutability with

MA.operate!(MA.add_mul, c, A, b)

to reduce the allocation down to 48 bytes due to 3 allocations. These remaining allocations are due to the BigInt buffer used to store the result of intermediate multiplications. This buffer can be preallocated with

buffer = MA.buffer_for(MA.add_mul, typeof(c), typeof(A), typeof(b))
MA.buffered_operate!(buffer, MA.add_mul, c, A, b)

then the second line is allocation-free.

The MA.@rewrite macro rewrite an expression into an equivalent code that exploit the mutability of the intermediate results. For instance

MA.@rewrite(A1 * b1 + A2 * b2)

is rewritten into

c = MA.operate!(MA.add_mul, MA.Zero(), A1, b1)
MA.operate!(MA.add_mul, c, A2, b2)

which is equivalent to

c = MA.operate(*, A1, b1)
MA.mutable_operate!(MA.add_mul, c, A2, b2)

Note that currently, only the Polynomial type implements the API and it only implements part of it.

Used By Packages

No packages found.