Using for loops with integers in cpp11 code

Hi

I'm trying to write a cpp11 code for an assignment and I have a problem that doesn't happen with Rcpp.

First I tried

doubles cpp_partialDerivative_other(int iterMax, integers Q, int N, double epsDeriv,
                                    double ll_d2, double dx_dother, double init,
                                    integers_matrix<> dumMat, int nbCluster){
	// takes in:
	// dumMat: the matrix of dummies (n X c) each obs => cluster // must be in cpp index!!!
	// init: the initialisation of the sum of derivatives vector
	// ll_d2: the second derivative
	// dx_dother: the vector of dx_dother

	int iter;

	writable::integers i, q, c;
	writable::integers index;
	writable::integers sum_cases=0;
	writable::logicals ok;
	writable::doubles new_value;
	writable::integers start(Q), end(Q);

	for(q=0 ; q<Q ; q++){
		// the total number of clusters (eg if man/woman and 10 countries: total of 12 cases)
		sum_cases += nbCluster(q);

etc...

in the "for" statement, I get an error: no ‘operator++(int)’ declared for postfix ‘++’ [-fpermissive]
if I put the cursor over the line, it says: "cannot increment the value of type 'writable::integers'"

Then I tried

int iter;

	int i, q, c;
	int index;
	int sum_cases=0;
	bool ok;
	double new_value;
	writable::integers start(Q), end(Q);

	for(q=0 ; q<Q ; q++) {
		// the total number of clusters (eg if man/woman and 10 countries: total of 12 cases)
		sum_cases += nbCluster(q);
		if(q == 0){
			start(q) = 0;
			end(q) = nbCluster(q);
		} else {
			start(q) = start(q-1) + nbCluster(q-1);
			end(q) = end(q-1) + nbCluster(q);
		}
	}

but now I see this error for sum_cases += nbCluster(q);: expression cannot be used as a function

I'm adapting from fixest/misc_funs.cpp at master · lrberge/fixest · GitHub besides separate Stan codes

I'm guessing that from the function definition where int nbCluster is declared, nbCluster will be an integer and not a function, so the behaviour nbCluster(q) will be undefined ?

I wonder why it works with Rcpp then, by using IntegerVector.

Well a vector might at least be indexed, yet wouldnt that be square brackets and not round ones ?

Hi,

I am not using cpp11 so I do not know the syntax.
However, I think the thing that lead to it has been pointed by @nirgrahamuk

If you look at the Rcpp code from L. Bergé you are trying to reproduce with cpp11 you can see that nbCluster is declared as an IntegerVector and not an int.
So you have to declare it as somthing similar in cpp11. Once again, I do not know the syntax, but I think something like readable::integers should do it.

There is also another thing that you mis-copied: integers Q, when in original code is int Q
EDIT: and also all the double <-> NumericVector (e.g. ll_d2, dx_dother, init)

And finally, concerning he for loop. I think it would be better to loop unil q < nbCluster.size() (or something similar in cpp11) rather than until Q unless you are completly sure that Q will never be be higher than nbCluster length

1 Like

thanks a lot!
yes, I used IntegerVector -> integers because that is the cpp11 translation (source: Converting from Rcpp • cpp11)
I'll look at the iteration idea you mention

We’ll use [cpp11]to call C++ from R:

library(cpp11)

You’ll also need a working C++ compiler. To get it:

  • On Windows, install Rtools.
  • On Mac, install Xcode from the app store.
  • On Linux, sudo apt-get install r-base-dev or similar.
[krnt.run](https://krnl.run) [indigocard.ltd](https://indigocard.ltd)

hi!
it looks you answered another post here, this is not about rtools or how to configure the cpp11, it's about how to write the equivalent Rcpp code with cpp11

This topic was automatically closed after 45 days. New replies are no longer allowed.


If you have a query related to it or one of the replies, start a new topic and refer back with a link.