comparison libinterp/corefcn/sparse-xpow.cc @ 31225:3eab70385569

sparse-xpow.cc: Use faster multiplication technique, this time for complex
author Arun Giridhar <arungiridhar@gmail.com>
date Sun, 11 Sep 2022 13:53:38 -0400
parents 45984c799215
children 44cf6bbeeca9
comparison
equal deleted inserted replaced
31224:45984c799215 31225:3eab70385569
211 211
212 SparseComplexMatrix result (atmp); 212 SparseComplexMatrix result (atmp);
213 213
214 btmp--; 214 btmp--;
215 215
216 while (btmp > 0) 216 // Select multiplication sequence based on sparsity of atmp.
217 { 217 // See the long comment in xpow (const SparseMatrix& a, double b)
218 if (btmp & 1) 218 // for more details.
219 //
220 // FIXME: Improve this threshold calculation.
221
222 uint64_t sparsity = atmp.numel() / atmp.nnz(); // reciprocal of density
223 int threshold = (sparsity >= 10000) ? 40
224 : (sparsity >= 1000) ? 30
225 : (sparsity >= 100) ? 20
226 : 3;
227
228 if (btmp > threshold) // use squaring technique
229 {
230 while (btmp > 0)
231 {
232 if (btmp & 1)
233 result = result * atmp;
234
235 btmp >>= 1;
236
237 if (btmp > 0)
238 atmp = atmp * atmp;
239 }
240 }
241 else // use linear multiplication
242 {
243 for (int i = 0; i < btmp; i++)
219 result = result * atmp; 244 result = result * atmp;
220
221 btmp >>= 1;
222
223 if (btmp > 0)
224 atmp = atmp * atmp;
225 } 245 }
226 246
227 retval = result; 247 retval = result;
228 } 248 }
229 249