@@ -119,6 +119,7 @@ may be noticeable in some use cases. This is amplified when the coefficients
119
119
are for instance ` BigInt ` or ` BigFloat ` which are mutable themself.
120
120
This can be avoided by modifying existing polynomials to contain the result
121
121
of the operation using the [ MutableArithmetics (MA) API] ( https://github.com/jump-dev/MutableArithmetics.jl ) .
122
+
122
123
Consider for instance the following arrays of polynomials
123
124
``` julia
124
125
using Polynomials
@@ -127,34 +128,55 @@ p(d) = Polynomial(big.(1:d))
127
128
A = [p (d) for i in 1 : m, j in 1 : n]
128
129
b = [p (d) for i in 1 : n]
129
130
```
131
+
130
132
In this case, the arrays are mutable objects for which the elements are mutable
131
133
polynomials which have mutable coefficients (` BigInt ` s).
132
134
These three nested levels of mutable objects communicate with the MA
133
135
API in order to reduce allocation.
134
136
Calling ` 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
+
136
147
``` julia
137
148
using MutableArithmetics
138
149
const MA = MutableArithmetics
139
150
MA. operate (* , A, b)
140
151
```
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
+
143
158
``` julia
144
159
z (d) = Polynomial ([zero (BigInt) for i in 1 : d])
145
160
c = [z (2 d - 1 ) for i in 1 : m]
146
161
```
162
+
147
163
then we can exploit its mutability with
164
+
148
165
``` julia
149
- MA. mutable_operate ! (MA. add_mul, c, A, b)
166
+ MA. operate ! (MA. add_mul, c, A, b)
150
167
```
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
+
154
175
``` julia
155
176
buffer = 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)
157
178
```
179
+
158
180
then the second line is allocation-free.
159
181
160
182
The ` MA.@rewrite ` macro rewrite an expression into an equivalent code that
@@ -174,7 +196,7 @@ c = MA.operate(*, A1, b1)
174
196
MA. mutable_operate! (MA. add_mul, c, A2, b2)
175
197
```
176
198
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
178
200
implements part of it.*
179
201
180
202
### Integrals and Derivatives
0 commit comments