4

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"];
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
freevillage
  • 167
  • 3
  • Would it be prohibitive for you to do tmp = Partition[ps, wSize, 1, -1]; MapThread[Dot, {tmp, tmp.Transpose[Σinv]}]? – J. M.'s missing motivation Mar 20 '18 at 02:36
  • If you include partitioning into the total computation, do you get any significant gain time-wise? – freevillage Mar 20 '18 at 03:32
  • 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. – J. M.'s missing motivation Mar 20 '18 at 03:36
  • 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.

    – freevillage Mar 20 '18 at 16:54
  • Let me offer you a tip: if you are using a function that is not in this list, then your function won't benefit from compilation. – J. M.'s missing motivation Mar 20 '18 at 22:04
  • Dot is in the list. Plus there is a related issue of how to do it in the first place, the effect notwithstanding. – freevillage Mar 21 '18 at 17:59
  • 1
    Yes, but you stuffed things like Inverse[] and CovarianceFunction[] within Compile[], which you now know to be pointless. – J. M.'s missing motivation Mar 21 '18 at 18:09

0 Answers0