C++ -> Rcpp 写経その1『簡単な計算』

// sample01.cpp
#include <stdio.h>

main(void)
{
  printf("Computer in Physics\n");
}
// sample01R.cpp
#include <stdio.h>
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
CharacterVector sample01()
{
  //printf("Computer in Physics\n");
  CharacterVector x = "Computer in Physics\n";
  return x;
}

/*** R
sample01()
*/
    • それぞれの実行
> g++ sample01.cpp -o sample01
> sample01
Computer in Physics
> sourceCpp("sample01R.cpp",verbose=TRUE)

Generated extern "C" functions 
--------------------------------------------------------


#include <Rcpp.h>
// sample01
CharacterVector sample01();
RcppExport SEXP sourceCpp_97064_sample01() {
BEGIN_RCPP
    Rcpp::RObject __result;
    Rcpp::RNGScope __rngScope;
    __result = Rcpp::wrap(sample01());
    return __result;
END_RCPP
}

Generated R functions 
-------------------------------------------------------

`.sourceCpp_97064_DLLInfo` <- dyn.load('C:/Users/ryamada/AppData/Local/Temp/RtmpCiGfLb/sourcecpp_1c944b1a35c5/sourceCpp_37015.dll')

sample01 <- Rcpp:::sourceCppFunction(function() {}, FALSE, `.sourceCpp_97064_DLLInfo`, 'sourceCpp_97064_sample01')

rm(`.sourceCpp_97064_DLLInfo`)

Building shared library
--------------------------------------------------------

DIR: C:/Users/ryamada/AppData/Local/Temp/RtmpCiGfLb/sourcecpp_1c944b1a35c5

C:/PROGRA~1/R/R-31~1.0/bin/x64/R CMD SHLIB -o "sourceCpp_37015.dll" "" "sample01R.cpp"  
g++ -m64 -I"C:/PROGRA~1/R/R-31~1.0/include" -DNDEBUG     -I"C:/Users/ryamada/Documents/R/win-library/3.1/Rcpp/include" -I"c:/Users/ryamada/Desktop/Rcpp"  -I"d:/RCompile/CRANpkg/extralibs64/local/include"     -O2 -Wall  -mtune=core2 -c sample01R.cpp -o sample01R.o
g++ -m64 -shared -s -static-libgcc -o sourceCpp_37015.dll tmp.def sample01R.o -Ld:/RCompile/CRANpkg/extralibs64/local/lib/x64 -Ld:/RCompile/CRANpkg/extralibs64/local/lib -LC:/PROGRA~1/R/R-31~1.0/bin/x64 -lR

> sample01()
[1] "Computer in Physics\n"
  • その2 int + int
// sample02.cpp
#include <stdio.h>

main(void)
{
  int a, b, c;

  a=1;
  b=2;
  c=a+b;

  printf(" %d + %d = %d \n", a, b, c );
}
// sample02R.cpp
#include <stdio.h>
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
int sample02(int a, int b)
{
  //int a, b, c;

  //a=1;
  //b=2;
  int c=a+b;

  return c;
}

/*** R
a <- 1; b <- 2
sample02(a,b)
*/
  • その3 double の計算、doubleのベクトルを返す
// sample03.cpp
#include <stdio.h>
#include <math.h>

main(void)
{
  double a, b, c;
  double xp, xm;

  a =  1.0;
  b = -4.0;
  c =  2.0;

  xp = (-b + sqrt(b*b-4.0*a*c))/(2.0*a);
  xm = (-b - sqrt(b*b-4.0*a*c))/(2.0*a);

  printf("The solutions are %lf and %lf\n", xp, xm );
}
// sample03R.cpp
#include <stdio.h>
#include <math.h>
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector sample03(double a, double b, double c)
{
  //double a, b, c;
  double xp, xm;

  //a =  1.0;
  //b = -4.0;
  //c =  2.0;

  xp = (-b + sqrt(b*b-4.0*a*c))/(2.0*a);
  xm = (-b - sqrt(b*b-4.0*a*c))/(2.0*a);

  //printf("The solutions are %lf and %lf\n", xp, xm );
  NumericVector x = NumericVector::create(xp,xm);
  return(x);
}
/*** R
a <- 1; b <- -4; c. <- 2
sample03(a,b,c.)
*/