@@ -119,6 +119,7 @@ may be noticeable in some use cases. This is amplified when the coefficients
119119are for instance ` BigInt ` or ` BigFloat ` which are mutable themself.
120120This can be avoided by modifying existing polynomials to contain the result
121121of the operation using the [ MutableArithmetics (MA) API] ( https://github.com/jump-dev/MutableArithmetics.jl ) .
122+
122123Consider for instance the following arrays of polynomials
123124``` julia
124125using Polynomials
@@ -127,34 +128,55 @@ p(d) = Polynomial(big.(1:d))
127128A = [p (d) for i in 1 : m, j in 1 : n]
128129b = [p (d) for i in 1 : n]
129130```
131+
130132In this case, the arrays are mutable objects for which the elements are mutable
131133polynomials which have mutable coefficients (` BigInt ` s).
132134These three nested levels of mutable objects communicate with the MA
133135API in order to reduce allocation.
134136Calling ` A * b ` requires approximately 40 MiB due to 2 M allocations
135- as it does not exploit any mutability. Using
137+ as it does not exploit any mutability.
138+
139+ Using
140+
141+ ``` julia
142+ using PolynomialsMutableArithmetics
143+ ```
144+
145+ to register ` Polynomials ` with ` MutableArithmetics ` , then multiplying with:
146+
136147``` julia
137148using MutableArithmetics
138149const MA = MutableArithmetics
139150MA. operate (* , A, b)
140151```
141- exploits the mutability and hence only allocate approximately 70 KiB due to 4 k
142- allocations. If the resulting vector is already allocated, e.g.,
152+
153+ exploits the mutability and hence only allocates approximately 70 KiB due to 4 k
154+ allocations.
155+
156+ If the resulting vector is already allocated, e.g.,
157+
143158``` julia
144159z (d) = Polynomial ([zero (BigInt) for i in 1 : d])
145160c = [z (2 d - 1 ) for i in 1 : m]
146161```
162+
147163then we can exploit its mutability with
164+
148165``` julia
149- MA. mutable_operate ! (MA. add_mul, c, A, b)
166+ MA. operate ! (MA. add_mul, c, A, b)
150167```
151- to reduce the allocation down to 48 bytes due to 3 allocations. These remaining
152- allocations are due to the ` BigInt ` buffer used to store the result of
153- intermediate multiplications. This buffer can be preallocated with
168+
169+ to reduce the allocation down to 48 bytes due to 3 allocations.
170+
171+ These remaining allocations are due to the ` BigInt ` buffer used to
172+ store the result of intermediate multiplications. This buffer can be
173+ preallocated with:
174+
154175``` julia
155176buffer = MA. buffer_for (MA. add_mul, typeof (c), typeof (A), typeof (b))
156- MA. mutable_buffered_operate ! (buffer, MA. add_mul, c, A, b)
177+ MA. buffered_operate ! (buffer, MA. add_mul, c, A, b)
157178```
179+
158180then the second line is allocation-free.
159181
160182The ` MA.@rewrite ` macro rewrite an expression into an equivalent code that
@@ -174,7 +196,7 @@ c = MA.operate(*, A1, b1)
174196MA. mutable_operate! (MA. add_mul, c, A2, b2)
175197```
176198
177- * Note that currently, only the ` Polynomial ` implements the API and it only
199+ * Note that currently, only the ` Polynomial ` type implements the API and it only
178200implements part of it.*
179201
180202### Integrals and Derivatives
0 commit comments