[Rcpp-devel] Help for call R function

2012-06-15 Thread bbo...@tin.it

 Hello to everyboby  Im Gianluca and i m a statistic student.
I have one problem that i think  Dirk or some one  "good like dirk"  could help 
to resolve  :

iI would like to implement this  following function in R code  into inline code 
with Rccp plugin :

MHWG<-function (logpost, start, n.iter, burn, thin, scale, ...) 
{   
pb <- txtProgressBar(min = 0, max = n.iter, style = 3)
p = length(start)
vth = array(0, dim = c(n.iter/thin, p))
f0 = logpost(start, ...)
arate = array(0, dim = c(1, p))
th0 = start
for (i in (-burn):n.iter) {
setTxtProgressBar(pb, i)
for (j in 1:p) {
th1 = th0
th1[j] = th0[j] + rnorm(1) * scale[j]
f1 = logpost(th1, ...)
u = runif(1) <= min(exp(f1 - f0),1)
th0[j] = th1[j] * (u == 1) + th0[j] * (u == 0)
f0 = f1 * (u == 1) + f0 * (u == 0)
vth[floor(i/thin), j] = th0[j]
arate[j] = arate[j] + u
}
}
arate = arate/n.iter
stuff = list(par = vth, accept = arate)
return(stuff)
}


So  one argument of MHWG   is   logpost  and  is a R function with argument par 
where  par iso ne vector    for example logpost(par)   
Logpost<-function(par) { ...} (in r code).
How can i call  teh logpost function into the inline rccp code . In the MHWG  
there is a difference  f1-f0where fi=logpost(par1)  e f0=logpost(par 2)  
for exemple.  
help me please to implemet  my problem   ...and  also i m "open" about  
suggestion for implemeting  the function 
Sorry  for all noise and thank also for the past mail that  help me to learn 
more about  programming 
Best regards... sorrry fo rmy bad English
 ___
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

[Rcpp-devel] Call R function

2012-06-19 Thread bbo...@tin.it

Hello i would like to compute the following code ;

Where R_user_F is a R function  (i could write this function directly inin line 
but is only for example because i want to call an R function more complex):
so when compile appear this error  message:  error: 
"cannot convert 'Rcpp::sugar::Comparator_With_One_Value<14, 
Rcpp::sugar::greater_or_equal<14>, true, Rcpp::sugar::Minus_Vector_Vector<14, 
true, Rcpp::Vector<14>, true, Rcpp::Vector<14> > >' to 'bool' in 
initialization."

 I can do for solve this problem? Thank You.

The code:

R_user_F<-function (par) {
y<-par[1]
x<-par[2]
dnorm(x)*dnorm(y)
}

 

require(inline)
code <- '
NumericVector f1;
NumericVector f2;
NumericVector par1;
NumericVector par2;
NumericVector a=2;
NumericVector b=3;
NumericVector par =par;
par1=par*b;
par2=par*a;
Function R_userf(fun);
f1=R_userf(par1);
f2=R_userf(par2);
bool u = (Rf_runif(0.0,1.0) <= f2-f1  );

'
RcppGibbs <- cxxfunction(signature( par ="NumericVector", fun="function"),
code, 
include='#include ', 
plugin="Rcpp")<-function (par) {
y<-par[1]
x<-par[2]
dnorm(x)*dnorm(y)
}

  ___
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

[Rcpp-devel] Pointer Problem ?

2012-06-22 Thread bbo...@tin.it

i have another problem maby cause by my inexperience   in the following code in 
Rccp inline  in the middle of the double cicle  (just after  ...
 for (j=0; j(start);
int n=Rf_length(start_);
int N = as(nsim);
NumericVector tune = as(scale);
NumericMatrix U(N,n);
NumericVector init (n);
NumericVector th0 (n);
NumericVector th1 (n);
NumericMatrix store(N,n);
NumericVector acount (n);
NumericVector f0;
NumericVector f1;
Function ltd(fun);
int i,j;
init = start_;
bool u;
for( i=0; i___
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

[Rcpp-devel] "=" work in mathematical mode (if th0=th1 then th1=th0 and viceversa) not in"informatical mode (assigment) like i want ..WHY??

2012-06-24 Thread bbo...@tin.it
 

sorry for my stupid questions but the question is one : im ignorant but i know 
some one could help me.
Why This  appen?   pay attention  please for the following code. In this there 
is  a  loop  for (int k=0; k(start);

int n=Rf_length(start_);
int N = as(nsim);
NumericVector tune = as(scale);

NumericVector init (n);
NumericVector th0 (n);
NumericVector th1 (n);
NumericMatrix store(N,n);
NumericVector acount (n);
NumericVector f0;
NumericVector f1;
Function ltd(fun);
int i,j;
init = start_;
bool u;
for( i=0; i___
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

[Rcpp-devel] RcppProgress with RcppArmadillo

2013-08-02 Thread bbo...@tin.it
 Dear list,
 I have a problem:  i wold like to build up a package that use both 
RccpArmadillo and Rcppprogress together 
 following instruction on RcppProgress manual where the author's  suggest how 
to include the correct flags and libs and descriprion (linking and depends) for 
include 
 a progress bar (from RcppProgress)  into Rcpp package  . I have try to complie 
the package RcppProgressExample  (that is also into the RcppProgress.tar) and 
all comes good .. If i try to add  #include  inside the 
example.cpp function (inside RcppProgressExample )  and setting the linking and 
dependencies 
with RcppArmadillo and setting the flags  the complier return error ..
Some one could be so gentle to show me i can do to have success in compiling 
code? and also to use RcppPrpgress with RcppArmadillo with inline cxxfunction 
(also how i can build up plugin)
Many thank in advance
Sorry for noise 
Bonitta gianluca ___
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

[Rcpp-devel] RcppProgress RcppArmadillo

2013-08-13 Thread bbo...@tin.it

 Dear list good morning,
 can someone explain why the compiler return me an error when I add the 
following include 
#include  (without this inclusion the code is compiled in correct 
way)
(using // [[Rcpp::depends(Rcpp,RcppArmadillo,RcppProgress)]] for dependencies)

the example code (using attributes sourceCpp) :


// [[Rcpp::depends(Rcpp,RcppArmadillo,RcppProgress)]]

#include 
#include 

#include 


// [[Rcpp::export]]

arma::vec Mahalanobis(arma::mat x, arma::rowvec center, arma::mat cov){
int n = x.n_rows;
arma::mat x_cen;
x_cen.copy_size(x);
for (int i=0; i < n; i++) {
x_cen.row(i) = x.row(i) - center;
}
return sum((x_cen * cov.i()) % x_cen, 1);
}


require("Rcpp")
Sys.setenv("PKG_CXXFLAGS"="-fopenmp")
Sys.setenv("PKG_LIBS"="-fopenmp")
 sourceCpp("rr.cpp")


ERROR :
g++ -m32 -I"C:/PROGRA~1/R/R-30~1.1/include" -DNDEBUG 
-I"C:/Users/gianni/Documents/R/win-library/3.0/Rcpp/include" 
-I"C:/Users/gianni/Documents/R/win-library/3.0/RcppArmadillo/include" 
-I"C:/Users/gianni/Documents/R/win-library/3.0/RcppProgress/include"  
-I"d:/RCompile/CRANpkg/extralibs64/local/include"  -fopenmp   -O2 -Wall  
-mtune=core2 -c rr.cpp -o rr.o In file included from rr.cpp:10:0: 
C:/Users/gianni/Documents/R/win-library/3.0/RcppProgress/include/progress.hpp: 
In constructor 'Progress::Progress(long unsigned int, bool)': 
C:/Users/gianni/Documents/R/win-library/3.0/RcppProgress/include/progress.hpp:27:84:
 error: 'error' was not declared in this scope make: *** [rr.o] Error 1 
Errore in sourceCpp("rr.cpp") : Error 1 occurred building shared library.


many thanks in advance ___
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

[Rcpp-devel] Call C++ Function into R function called in Rcpp

2015-01-04 Thread bbo...@tin.it

Dear list first of all happy new year.
I have a question .. I would like to call rcpp function  (so c++ function) into 
R function (optim for example) called into rcpp. I had prototype an example 
code and post 
below.  (Using Rcpp::InternalFunction(&foo))
Is it correct the way I had used? There is another more efficient and safety 
way 
Thank You in advance list 
Best regards
Gianluca Bonitta


library(inline)

inc <- ' NumericVector foo(const Rcpp::NumericVector &x) { 
return - pow(x,2);
} '
code<- '
   Rcpp:: NumericVector start(Sexp_start);
   Rcpp:: Environment stats("package:stats"); 
   Rcpp:: Function optim = stats["optim"];
   Rcpp:: List Opt;

   Opt=optim(_["par"] = start,
 _["fn"]  = Rcpp::InternalFunction(&foo),
 _["method"]  = "BFGS");
return Opt;
'
fun.cpp <- cxxfunction(signature( Sexp_start = "NumericVector"),
   code, 
   include= inc,
   plugin = "Rcpp" )
fun.cpp (Sexp_start=0.1)

 R code 
fun. <- function(x) x^2
optim(0.1,fun.,method  = "BFGS")  ___
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

[Rcpp-devel] Call ++ funtion into R function called in rcpp

2015-01-04 Thread bbo...@tin.it
Dear list first of all happy new year.
I have a question .. I would like to call rcpp function  (so c++ 
function) into 
R function (optim for example) 
called into rcpp. I had prototype an example code and post 
below.  (Using Rcpp::InternalFunction(&foo))
Is it correct the way I had used? There is another more efficient and 
safety way 
Thank You in advance list 
Best regards
Gianluca Bonitta


library(inline)

inc <- ' NumericVector foo(const Rcpp::NumericVector &x) { 
return - pow(x,2);
} '
code<- '
   Rcpp:: NumericVector start(Sexp_start);
   Rcpp:: Environment stats("package:stats"); 
   Rcpp:: Function optim = stats["optim"];
   Rcpp:: List Opt;

   Opt=optim(_["par"] = start,
 _["fn"]  = Rcpp::InternalFunction(&foo),
 _["method"]  = "BFGS");
return Opt;
'
fun.cpp <- cxxfunction(signature( Sexp_start = "NumericVector"),
   code, 
   include= inc,
   plugin = "Rcpp" )
fun.cpp (Sexp_start=0.1)

 R code 
fun. <- function(x) x^2
optim(0.1,fun.,method  = "BFGS")
___
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel