1
$\begingroup$

In the previous question, https://mathematica.stackexchange.com/a/216251/51448, I asked how to speed up a code of an equation.

The answer (the function "ffast" in the above link) is using ListCorrelate and very fast.

Now, I want to convert an equation (that would be similar to "ffast") to mathematica code, but I could not get it.

The equation, A(m), is here:

enter image description here

where s_i and t_i are list having n points, and 1<=m<=n-1. For example,

n=1000000;
s=RandomReal[{-1,1},n];
t=RandomReal[{-1,1},n];

n is a large number, thus I need fast code.

Is it possible to write a fast code like as "ffast"?

Thanks in advance for any help.

Best regards.

Here is the additional equation

enter image description here

$\endgroup$
3
  • 1
    $\begingroup$ By the way, you might find it helpful to try using Compile to speed your code up, especially with these kinds of operations! (I don’t think ListCorrelate compiles, but the basic operations like Dot and Part should, I think. You can check by using CompilePrint and making sure there are no MainEvaluates.) $\endgroup$ – thorimur Mar 23 at 9:41
  • $\begingroup$ (You might (separately) also find it helpful to use parallelism, though it can be tricky.) $\endgroup$ – thorimur Mar 23 at 9:43
  • $\begingroup$ >thorimur Thank you for the useful information, especially CompilePrint. That's new to me. But, is Compile applicable to the "Dot and Part"? The code I wrote is below (n, la, lb are the same) and got errors: func = Compile[{{m,_Integer}}, (la[[m + 1 ;;]] - la[[;; n - m]]).(lb[[m + 1 ;;]] - lb[[;; n - m]])/(n - m)] $\endgroup$ – GaAs Mar 23 at 11:43
1
$\begingroup$

What you write is not a correlation. But you may calculate this e.g. by using Part and Dot. Here is a small example:

n = 5;
la = Array[Subscript[a, #] &, n];
lb = Array[Subscript[b, #] &, n];
fun[m_] := (la[[m + 1 ;;]] - la[[;; n - m]]).(lb[[m + 1 ;;]] - 
     lb[[;; n - m]])/(n - m)

fun[2]

enter image description here

$\endgroup$
3
  • $\begingroup$ Thank you very much! It may be difficult to apply the large n (e.g. 1,000,000), but it works well. The question I posted at the beginning lacked explanation and was not clear, but I looked at the expanded equation ("additional equation") and imagined that this was related to ListCorrelation. $\endgroup$ – GaAs Mar 23 at 4:53
  • $\begingroup$ When n=1000000 and la = Array[Subscript[a, #] &, n]; is used, it takes very long time. However, when la = Table[RandomReal[], {1000000}]; is used, it is done immediately (0.0001 s, in my case). I don't know why, but the code works practically! $\endgroup$ – GaAs Mar 23 at 11:53
  • $\begingroup$ It looks like symbolic arrays are not as efficiently implemented like numeric arrays. This makes sense as symbolic arrays are often much smaller than numeric ones. $\endgroup$ – Daniel Huber Mar 23 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.