[Rcpp-devel] sugar: x+y and y+x gives different results when there are NA's in y

2014-11-22 Thread Søren Højsgaard
Dear all,
Came across this:

#include Rcpp.h
using namespace Rcpp;

//[[Rcpp::export]]
List  sumxy(NumericVector x, NumericVector y){
  NumericVector xy = x+y;
  NumericVector yx = y+x;
  return List::create(xy, yx);
}

/*** R
x - c(1,3,5,7)
y - c(NA,2,4,6,8)
x+y
y+x
sumxy(x, y)
 */

 x - c(1,3,5,7)
 y - c(NA,2,4,6,8)
 x+y
[1] NA  5  9 13  9
 y+x
[1] NA  5  9 13  9
 sumxy(x, y)
[[1]]
[1] NA  5  9 13
[[2]]
[1] NA  5  9 13  8

Is this intentional? Apologies if this has been documented anywhere; was unable 
to find it.

Regards
Søren


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


Re: [Rcpp-devel] sugar: x+y and y+x gives different results when there are NA's in y

2014-11-22 Thread Romain Francois
This has nothing to do with NA. It's just about the size difference. sugar does 
not make attempt at recycling. Your responsibility. 

Romain

 Le 22 nov. 2014 à 11:48, Søren Højsgaard sor...@math.aau.dk a écrit :
 
 Dear all,
 Came across this:
 
 #include Rcpp.h
 using namespace Rcpp;
 
 //[[Rcpp::export]]
 List  sumxy(NumericVector x, NumericVector y){
  NumericVector xy = x+y;
  NumericVector yx = y+x;
  return List::create(xy, yx);
 }
 
 /*** R
 x - c(1,3,5,7)
 y - c(NA,2,4,6,8)
 x+y
 y+x
 sumxy(x, y)
 */
 
 x - c(1,3,5,7)
 y - c(NA,2,4,6,8)
 x+y
 [1] NA  5  9 13  9
 y+x
 [1] NA  5  9 13  9
 sumxy(x, y)
 [[1]]
 [1] NA  5  9 13
 [[2]]
 [1] NA  5  9 13  8
 
 Is this intentional? Apologies if this has been documented anywhere; was 
 unable to find it.
 
 Regards
 Søren
 
 
 ___
 Rcpp-devel mailing list
 Rcpp-devel@lists.r-forge.r-project.org
 https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

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


Re: [Rcpp-devel] sugar: x+y and y+x gives different results when there are NA's in y

2014-11-22 Thread Søren Højsgaard
OK; thanks! Shall I read this such as the behaviour is undefined?
Søren

|-Original Message-
|From: Romain Francois [mailto:rom...@r-enthusiasts.com]
|Sent: 22. november 2014 13:25
|To: Søren Højsgaard
|Cc: rcpp-devel@lists.r-forge.r-project.org
|Subject: Re: [Rcpp-devel] sugar: x+y and y+x gives different results when
|there are NA's in y
|
|This has nothing to do with NA. It's just about the size difference.
|sugar does not make attempt at recycling. Your responsibility.
|
|Romain
|
| Le 22 nov. 2014 à 11:48, Søren Højsgaard sor...@math.aau.dk a écrit :
|
| Dear all,
| Came across this:
|
| #include Rcpp.h
| using namespace Rcpp;
|
| //[[Rcpp::export]]
| List  sumxy(NumericVector x, NumericVector y){  NumericVector xy =
| x+y;  NumericVector yx = y+x;  return List::create(xy, yx); }
|
| /*** R
| x - c(1,3,5,7)
| y - c(NA,2,4,6,8)
| x+y
| y+x
| sumxy(x, y)
| */
|
| x - c(1,3,5,7)
| y - c(NA,2,4,6,8)
| x+y
| [1] NA  5  9 13  9
| y+x
| [1] NA  5  9 13  9
| sumxy(x, y)
| [[1]]
| [1] NA  5  9 13
| [[2]]
| [1] NA  5  9 13  8
|
| Is this intentional? Apologies if this has been documented anywhere;
|was unable to find it.
|
| Regards
| Søren
|
|
| ___
| Rcpp-devel mailing list
| Rcpp-devel@lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-deve
| l

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


Re: [Rcpp-devel] sugar: x+y and y+x gives different results when there are NA's in y

2014-11-22 Thread Romain Francois
You get UB if the first is bigger than the second. 

Envoyé de mon iPhone

 Le 22 nov. 2014 à 14:30, Søren Højsgaard sor...@math.aau.dk a écrit :
 
 OK; thanks! Shall I read this such as the behaviour is undefined?
 Søren
 
 |-Original Message-
 |From: Romain Francois [mailto:rom...@r-enthusiasts.com]
 |Sent: 22. november 2014 13:25
 |To: Søren Højsgaard
 |Cc: rcpp-devel@lists.r-forge.r-project.org
 |Subject: Re: [Rcpp-devel] sugar: x+y and y+x gives different results when
 |there are NA's in y
 |
 |This has nothing to do with NA. It's just about the size difference.
 |sugar does not make attempt at recycling. Your responsibility.
 |
 |Romain
 |
 | Le 22 nov. 2014 à 11:48, Søren Højsgaard sor...@math.aau.dk a écrit :
 |
 | Dear all,
 | Came across this:
 |
 | #include Rcpp.h
 | using namespace Rcpp;
 |
 | //[[Rcpp::export]]
 | List  sumxy(NumericVector x, NumericVector y){  NumericVector xy =
 | x+y;  NumericVector yx = y+x;  return List::create(xy, yx); }
 |
 | /*** R
 | x - c(1,3,5,7)
 | y - c(NA,2,4,6,8)
 | x+y
 | y+x
 | sumxy(x, y)
 | */
 |
 | x - c(1,3,5,7)
 | y - c(NA,2,4,6,8)
 | x+y
 | [1] NA  5  9 13  9
 | y+x
 | [1] NA  5  9 13  9
 | sumxy(x, y)
 | [[1]]
 | [1] NA  5  9 13
 | [[2]]
 | [1] NA  5  9 13  8
 |
 | Is this intentional? Apologies if this has been documented anywhere;
 |was unable to find it.
 |
 | Regards
 | Søren
 |
 |
 | ___
 | Rcpp-devel mailing list
 | Rcpp-devel@lists.r-forge.r-project.org
 | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-deve
 | l
 
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Re: [Rcpp-devel] tracking volatile bug

2014-11-22 Thread Dirk Eddelbuettel

On 21 November 2014 at 23:50, Sokol Serguei wrote:
| If I did not use the protection in my Rcpp code it's because
| I did not see it in any example of quick ref guide:
| http://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-quickref.pdf
| I didn't see any mention of PROTECT necessity neither in
| http://dirk.eddelbuettel.com/code/rcpp/Rcpp-introduction.pdf
| especially in the part dedicated to the use of wrap().

I am sorry but that is somewhat uncalled for. 

We rarely advocate a programming style advocating SEXP manipulation (apart
from rare cases, or in the guts of our code). You programmed the plain C API
for R, apart from using some utility functions from Rcpp to access functions.

For this programming style, you should go to r-devel rather than coming here.
What you do is pretty different from standard Rcpp use, or what we recommend.

Blaming us for not documenting what we consider inappropriate style is not
only circular but also continues to misunderstand why we do what we do here,
and how we do it.

| I supposed (very naively, it is clear now) that necessary wrapping and
| protection was automagicaly added behind the stage by Rcpp.

It of course happes automagically if and only if you use Rcpp datatypes.

From what I understand you did not use those, and hence got no magic. 

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


Re: [Rcpp-devel] tracking volatile bug

2014-11-22 Thread Sokol Serguei

Dirk Eddelbuettel has written at  Sat, 22 Nov 2014 09:57:36 -0600

On 21 November 2014 at 23:50, Sokol Serguei wrote:
| If I did not use the protection in my Rcpp code it's because
| I did not see it in any example of quick ref guide:
| http://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-quickref.pdf
| I didn't see any mention of PROTECT necessity neither in
| http://dirk.eddelbuettel.com/code/rcpp/Rcpp-introduction.pdf
| especially in the part dedicated to the use of wrap().

I am sorry but that is somewhat uncalled for.

We rarely advocate a programming style advocating SEXP manipulation (apart
from rare cases, or in the guts of our code). You programmed the plain C API
for R, apart from using some utility functions from Rcpp to access functions.

For this programming style, you should go to r-devel rather than coming here.
What you do is pretty different from standard Rcpp use, or what we recommend.
If I moved to this style it's because an Rcpp style (at least how I 
understood it) did not work for me.
Obviously, if I can get a pure Rcpp code working in this case I would 
be perfectly happy

with that.



Blaming us

It was not a blaming, just citing my sources.

  for not documenting what we consider inappropriate style is not
only circular but also continues to misunderstand why we do what we do here,
and how we do it.

May be I still misunderstand something but from what I understood
it is perfectly regular to call an R function from rcpp. Well, you say 
it is not

recommended but if there is no rcpp counterpart neither CCallable interface
what are the alternatives?



| I supposed (very naively, it is clear now) that necessary wrapping and
| protection was automagicaly added behind the stage by Rcpp.

It of course happes automagically if and only if you use Rcpp datatypes.

From what I understand you did not use those, and hence got no magic.

Let try to stick with regular rcpp code
(file: matrix_norm.cpp):

//[[Rcpp::depends(RcppArmadillo)]]
#include RcppArmadillo.h
using namespace Rcpp;
using namespace arma;

// [[Rcpp::export]]
double nmat(mat A) {
   Function Matrix_norm_r_=Environment(package:Matrix)[norm];
   double res=asdouble(Matrix_norm_r_(A, 1));
   return res;
}

When called in R as:

library(Rcpp)
library(Matrix)
sourceCpp(matrix_norm.cpp)
gctorture(TRUE)
nmat(as.matrix(pi))

it gives an error:
Erreur : 'getCharCE' doit être appelé sur un CHARSXP
(my English translation: Error: 'getCharCE' must be called on CHARSXP)

Something was irregular on my side here?
Serguei.
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Re: [Rcpp-devel] sugar: x+y and y+x gives different results when there are NA's in y

2014-11-22 Thread Søren Højsgaard
Thanks. May I suggest adding a remark about this in the sugar-vignette (if it 
is not already there - in which case I apologize for not having seen it!)

Cheers
Søren

|-Original Message-
|From: Romain Francois [mailto:rom...@r-enthusiasts.com]
|Sent: 22. november 2014 14:36
|To: Søren Højsgaard
|Cc: rcpp-devel@lists.r-forge.r-project.org
|Subject: Re: [Rcpp-devel] sugar: x+y and y+x gives different results when
|there are NA's in y
|
|You get UB if the first is bigger than the second.
|
|Envoyé de mon iPhone
|
| Le 22 nov. 2014 à 14:30, Søren Højsgaard sor...@math.aau.dk a écrit :
|
| OK; thanks! Shall I read this such as the behaviour is undefined?
| Søren
|
| |-Original Message-
| |From: Romain Francois [mailto:rom...@r-enthusiasts.com]
| |Sent: 22. november 2014 13:25
| |To: Søren Højsgaard
| |Cc: rcpp-devel@lists.r-forge.r-project.org
| |Subject: Re: [Rcpp-devel] sugar: x+y and y+x gives different results
| |when there are NA's in y
| |
| |This has nothing to do with NA. It's just about the size difference.
| |sugar does not make attempt at recycling. Your responsibility.
| |
| |Romain
| |
| | Le 22 nov. 2014 à 11:48, Søren Højsgaard sor...@math.aau.dk a
|écrit :
| |
| | Dear all,
| | Came across this:
| |
| | #include Rcpp.h
| | using namespace Rcpp;
| |
| | //[[Rcpp::export]]
| | List  sumxy(NumericVector x, NumericVector y){  NumericVector xy =
| | x+y;  NumericVector yx = y+x;  return List::create(xy, yx); }
| |
| | /*** R
| | x - c(1,3,5,7)
| | y - c(NA,2,4,6,8)
| | x+y
| | y+x
| | sumxy(x, y)
| | */
| |
| | x - c(1,3,5,7)
| | y - c(NA,2,4,6,8)
| | x+y
| | [1] NA  5  9 13  9
| | y+x
| | [1] NA  5  9 13  9
| | sumxy(x, y)
| | [[1]]
| | [1] NA  5  9 13
| | [[2]]
| | [1] NA  5  9 13  8
| |
| | Is this intentional? Apologies if this has been documented
| | anywhere;
| |was unable to find it.
| |
| | Regards
| | Søren
| |
| |
| | ___
| | Rcpp-devel mailing list
| | Rcpp-devel@lists.r-forge.r-project.org
| | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-d
| | eve
| | l
|
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Re: [Rcpp-devel] tracking volatile bug

2014-11-22 Thread Gabor Grothendieck
On Sat, Nov 22, 2014 at 1:43 PM, Sokol Serguei serguei.so...@gmail.com wrote:
 Let try to stick with regular rcpp code
 (file: matrix_norm.cpp):

 //[[Rcpp::depends(RcppArmadillo)]]
 #include RcppArmadillo.h
 using namespace Rcpp;
 using namespace arma;

 // [[Rcpp::export]]
 double nmat(mat A) {
Function Matrix_norm_r_=Environment(package:Matrix)[norm];
double res=asdouble(Matrix_norm_r_(A, 1));
return res;
 }

 When called in R as:

 library(Rcpp)
 library(Matrix)
 sourceCpp(matrix_norm.cpp)
 gctorture(TRUE)
 nmat(as.matrix(pi))

 it gives an error:
 Erreur : 'getCharCE' doit être appelé sur un CHARSXP
 (my English translation: Error: 'getCharCE' must be called on CHARSXP)

 Something was irregular on my side here?
 Serguei.

Try replacing the line that sets res with:

   double res=asdouble(Matrix_norm_r_(NumericMatrix(wrap(A)),
CharacterVector::create(1)));

Be sure to try it on a fresh session since errors from prior runs
could have messed up R.

-- 
Statistics  Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel