I have the following code (showing an insignificant simplification) that I really need to run many times for different ps (its length can be 10^7 or so) and wSize. Is there a way to speed this up using the fact that I have a special function inside MovingMap?

ps = RandomReal[{-1, 1}, 100000];
wSize = 100;
Σinv = Inverse@ToeplitzMatrix@CovarianceFunction[ps, {wSize - 1}];
MovingMap[#.Σinv.# &, ps, wSize - 1, "Periodic"];
  • $\begingroup$ Would it be prohibitive for you to do tmp = Partition[ps, wSize, 1, -1]; MapThread[Dot, {tmp, tmp.Transpose[Σinv]}]? $\endgroup$ Mar 20, 2018 at 2:36
  • $\begingroup$ If you include partitioning into the total computation, do you get any significant gain time-wise? $\endgroup$ Mar 20, 2018 at 3:32
  • $\begingroup$ I don't have a big computer to test this with (hence it's just a comment); I'm only going by the knowledge that it's more efficient to take matrix-matrix dot products than to incrementally build vector-by-vector. $\endgroup$ Mar 20, 2018 at 3:36
  • $\begingroup$ Is there a way to compile this function? movingDot = Compile[{{ps, _Real, 1}, {wSize, _Integer}}, With[{half[CapitalSigma]inv = Inverse@ToeplitzMatrix@CovarianceFunction[ps, {wSize - 1}]/2, halfWSize = wSize/2}, MovingMap[#.half[CapitalSigma]inv.# &, ps, wSize - 1, "Periodic"]] ] movingDot[ps, wSize]; CompiledFunction::cfse: Compiled expression {{2.9988,0.000143913,...,0.0015581,0.000759095,-0.000135168,<<37>>,-0.000601042,0.000877005,0.00026945,-0.000867925,0.00158341,0.000579952,<<50>>},{<<1>>},<<48>>,<<50>>} should be a machine-size real number. $\endgroup$ Mar 20, 2018 at 16:54
  • 1
    $\begingroup$ Yes, but you stuffed things like Inverse[] and CovarianceFunction[] within Compile[], which you now know to be pointless. $\endgroup$ Mar 21, 2018 at 18:09


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.