I am interested in calculating the following quantity

`B(i) = \sum_{j < i}(x_i-x_j)exp^{-\beta(x_i - x_j)}`

which is part of computing the gradient wrt one of the parameters of a Hawk's process likelihood (more information can be found here: <http://www.ism.ac.jp/editsec/aism/pdf/031_1_0145.pdf>).

Beta is just a constant for the shake of the problem and x\_i is my i-th data point.

I am trying to calculate the above quantity in RCPP, using the following chunk of code:

```
for( int i = 1; i< x.size();i++) {
    double temp=0;
    for(int j=0; j<=i-1;j++){
      temp+=(x[i]-x[j])*exp(-beta*(x[i]-x[j]));

    }

```

but it is highly inefficient and slow. Any suggestion on how this formula could be speeded-up?