1

I am just getting starting with Rcpp so this might be a very stupid question. Here is the specific question (context is provided below)

What is the Rcpp equivalent of

odes <- c(A = 1.0, B = 2.0, C = 3.0, D = 4.0, E = 5.0, F = 6.0, G = 7.0)
list(odes)

Context - I am trying to solve a system of Ordinary Differential Equation (ODEs) using the deSolve package's vode solver, but using Rcpp package to write the right hand side of ODEs in a compiled code. The solver expects the function which forms the RHS of ODEs to return a list, specifically in this case the RHS from a .R function (which the solver was able to intergrate successfully) was of the form

> X
[[1]]
     9000000.00     -9000000.00            0.00        19993.04       -19993.04       -19993.04     -9000000.00 

and I want my .cpp file to spit out odes as a list of similar form.

Any help here would be much appreciated!!

As suggested below, I am pasting the code to show exactly what I am doing

#include <Rcpp.h>
using namespace Rcpp;

// This is a simple example of exporting a C++ function to R. You can
// source this function into an R session using the Rcpp::sourceCpp 
// function (or via the Source button on the editor toolbar). Learn
// more about Rcpp at:
//
//   http://www.rcpp.org/
//   http://adv-r.had.co.nz/Rcpp.html
//   http://gallery.rcpp.org/
//

// [[Rcpp::export]]
List odes_gprotein(double t, NumericVector A, NumericVector p) {

  NumericVector odes_vec(A.length());
  List odes(1);

  double Flux1 = p[1] * A[4] * A[5] - p[0] * A[3];
  double Flux2 = p[2] * A[5] - p[3];
  double Flux3 = p[4] * A[3];
  double Flux4 = p[5] * A[1] * A[6];
  double Flux5 = p[6] * A[0] * A[3];
  double Flux6 = p[7] * A[2];

  odes_vec[0] = (Flux4 - Flux5);
  odes_vec[1] = (-Flux4 + Flux6);
  odes_vec[2] = (Flux5 - Flux6);
  odes_vec[3] = (Flux1 - Flux3);
  odes_vec[4] = (-Flux1);
  odes_vec[5] = (-Flux1 - Flux2);
  odes_vec[6] = (-Flux4 + Flux5);

  odes = List(odes_vec);
  return odes;
}

This function returns (when I supply some value of t, p and A) the following,

> Rcpp::sourceCpp('odes_gprotein.cpp')
> X <- odes_gprotein(0,IC,p)
> str(X)
List of 7
 $ : num 9e+06
 $ : num -9e+06
 $ : num 0
 $ : num 19993
 $ : num -19993
 $ : num -19993
 $ : num -9e+06

Whereas, what I need is the X as mentioned above

> X
[[1]]
     9000000.00     -9000000.00            0.00        19993.04       -19993.04       -19993.04     -9000000.00 

where

str(X)
List of 1
 $ : num [1:7] 9e+06 -9e+06 0e+00 2e+04 -2e+04 ...

Thank you for your suggestions!

Satya
  • 1,708
  • 1
  • 15
  • 39
  • What have you tried so far? Have you instantiated a `Rcpp::List` object yet? – Dirk Eddelbuettel May 08 '15 at 23:03
  • Thank you for your response, I have instantiated an Rcpp::List, but I think there is another problem, `odes` and `X` are not exactly equal in structure and I am not sure why`> str(odes) Named num [1:7] 1 2 3 4 5 6 7 - attr(*, "names")= chr [1:7] "A" "B" "C" "D" ... > str(X) List of 1 $ : Named num [1:7] 9e+06 -9e+06 0e+00 2e+04 -2e+04 ... ..- attr(*, "names")= chr [1:7] "A" "B" "C" "D" ...`, whereas `X` is a list of 1, `odes` is a `Named` – Satya May 08 '15 at 23:07
  • I tried using `List odes(1);`, then `odes[1](0) = 1.0`, I realize this gives a list with no `names`, but essentially I want a `list` of `1`, which is of the form `X` above – Satya May 08 '15 at 23:12
  • If you have tried something, show the relevant code (i.e., a minimal reproducible example) in your question. – Roland May 08 '15 at 23:15
  • @DirkEddelbuettel and Roland - Please see the edited question above, I have added code which should make is easier to understand what I am trying to do. – Satya May 09 '15 at 01:50

2 Answers2

3

We still do not really know what you want or tried, but here is a minimal existence proof for you:

R> cppFunction('List mylist(IntegerVector x) { return List(x); }')
R> mylist(c(2:4))
[[1]]
[1] 2

[[2]]
[1] 3

[[3]]
[1] 4

R> 

In goes a vector, out comes a list. Have a look at the Rcpp examples and eg the Rcpp Gallery site.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • @DirkEddebuettel - Thanks for your comment, I found a hint from your answere [here](http://stackoverflow.com/questions/3088650/how-do-i-create-a-list-of-vectors-in-rcpp). I had to remove the line `List odes(1);`, `List(odes_vec);` and `return odes;` and add the line `return Rcpp::List::create(odes_vec);` – Satya May 09 '15 at 17:58
1

Turns out I was creating the List in a wrong way, I had to remove the following

List odes(1); and List(odes_vec); and return (odes); and had to add the following statement at the end

return Rcpp::List::create(odes_vec);

A better explanation can be found here

Community
  • 1
  • 1
Satya
  • 1,708
  • 1
  • 15
  • 39