The average amount of time required to travel between two points in a torus-like space I have recently started to work with C++ and I do consider myself quite basic in C++ coding.
I have made the following two functions and I was wondering whether someone could give me some hints on how to improve and speed up this code.
Please, note that the second function is then used in R through Rcpp R package.
#include &lt;Rcpp.h&gt;
using namespace Rcpp;
using namespace std;

double sumAll(double i, double j, int a, int b, double coeff) {

  double pi = 2 * acos(0.0);

  double total = 0;
  int half_a =  a / 2;
  int half_b =  b / 2;

  for (int k = 0; k &lt;= half_a; ++k) {

    for (int l = 0; l &lt;= half_b; ++l) {

      double fkl;
      if (k == 0 &amp;&amp; l == 0) {
        fkl = 0;
      } else {
        fkl = pow((1 - (coeff * (1 - cos(2.0 * pi * k / a)))), 2.0) *
              pow((1 - (coeff * (1 - cos(2.0 * pi * l / b)))), 2.0);
      }

      double a1 = 1 - (cos(2.0 * pi * i * k / a) * cos(2.0 * pi * j * l / b));

      double deltaa;
      if (k == 0) {
        deltaa = 1;
      } else {
        deltaa = 0.5;
      }

      double deltab;
      if (l == 0) {
        deltab = 1;
      } else {
        deltab = 0.5;
      }

      if (k == 0 &amp;&amp; l == 0) {

        total = total + 0;

      } else {

        double res = (fkl * a1) / (deltaa * deltab * (1 - fkl));

        total = total + res;
      }
    }
  }
  return total;
}

// [[Rcpp::export]]
NumericVector series_Sum(NumericVector xvec, NumericVector yvec, int a, int b,
                         double coeff) {

  int size = xvec.length();
  NumericVector series_res(size);

  for (double i = 0; i &lt; size; i++) {
    series_res[i] = sumAll(xvec[i], yvec[i], a, b, coeff);
  }

  return series_res;
}

Consider that in sumAll :

i and j: are integers, which can get values between 0 and a/2 or 0 and b/2, respectively.
coeff: can be a value between 0 and 1.0.
a and b: can be any integer values higher than 5 (arbitrary choice).

In series_Sum :

xvec and yvec: are vectors of i and j values.

In other words, series_Sum use sumAll over several possible combinations of i and j.
Don't write using namespace std;.
The constants including pi are provided in a standard header already.  If you did need to define a constant,  use constexpr.
double deltaa;
if (k == 0) {
        deltaa = 1;
      } else {
        deltaa = 0.5;
      }

That is much clear to write as:
const double deltaa = k==0 ? 1 : 0.5;

which also allows you to initialize the value in the declaration and make it const.
The block for deltaa and deltab are the same except for the parameter.  You should make a small inline function for that.
total = total + 0;

Why?
total = total + res;

Better to write total += res;

Generally:
Use const where you can.  Initialize variables in the statement where you declare them.
In addition to using const, which may allow the optimizer to better figure such things out on its own, consider hoisting subexpressions out of the loop.  For example pi * k does not change inside the nested loop so compute it once in the outer loop only.
Express your comments as preconditions (e.g. assert or Expect statements).

for (double i = 0; i &lt; size; i++) {
    series_res[i] = sumSij(half_a, half_b, xvec[i], yvec[i], a, b, coeff);
  }

Why is i of type double?  It's an index into the NumericArrays.
I see a call to sumSij but no call to sumAll.  The code is not complete, and doesn't agree with your comment (unless the former calls the latter, but I don't know how).