Skip to content

[Rcpp-devel] error handling for computational singularity

3 messages · Xiao He, Dirk Eddelbuettel

#
Hi dear listers,

Take the mock function below for an example. What would be the best way to
handle computational singularity?

#include <R.h>
#include <stdio.h>
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]

using namespace Rcpp;
using namespace arma;

RcppExport SEXP foo(SEXP X, SEXP Y){
 arma::mat x=Rcpp::as<arma::mat>(X), y=Rcpp::as<arma::mat>(Y);
arma::mat output = arma::solve(x, y );
 return(wrap(output));
}


For instance, if I use the function on the dataset below, R would crash. I
read the error handling section of the document in
http://www.jstatsoft.org/v40/i08/paper, but I am not exactly sure what to
do with my situation. Thank you in advance for your help!

x = cbind(1:2, 1:2)
.Call("foo", x, x)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130706/822003c1/attachment.html>
#
On 6 July 2013 at 18:56, Xiao He wrote:
| Hi dear listers,
| 
| Take the mock function below for an example. What would be the best way to
| handle computational singularity?

That is essentially a _statistical_ issue and not a Rcpp problem so that is
not really the right forum.

| #include <R.h>
| #include <stdio.h>
| #include <RcppArmadillo.h>
| // [[Rcpp::depends(RcppArmadillo)]]
| 
| using namespace Rcpp;
| using namespace arma;
| 
| RcppExport SEXP foo(SEXP X, SEXP Y){
| arma::mat x=Rcpp::as<arma::mat>(X), y=Rcpp::as<arma::mat>(Y);
| arma::mat output = arma::solve(x, y );
| return(wrap(output));
| }
| 
| 
| For instance, if I use the function on the dataset below, R would crash. I read
| the error handling section of the document in http://www.jstatsoft.org/v40/i08/
| paper, but I am not exactly sure what to do with my situation. Thank you in

Rcpp offers support for exception handling -- meaning that _you_ could decide
to throw an exception to signal to the user that his data is not right /
properly conditioned.  

See the Armadillo documentation to learn how to test rank of matrices
etc. You may also learn that sole() is for a matrix and a vector, not two
vectors.

Dirk


| advance for your help!
| 
| x = cbind(1:2, 1:2)
| .Call("foo", x, x)
| 
| 
| 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
#
Thanks for the reply.

I tried the following:

#########################################################
RcppExport SEXP foo( SEXP X, SEXP Y) {
 try {
arma::mat x=Rcpp::as<arma::mat>(X), y=Rcpp::as<arma::mat>(Y);
 arma::mat output;
output = arma::solve( x, y );
return(wrap( output) );
 } catch( std::exception& __ex__ ) {
::Rf_error( "System is computationally singular" );
 //forward_exception_to_r( __ex__ );
} catch(...) {
::Rf_error( "c++ exception (unknown reason)" );
 }
}
#########################################################


This would give me the message below. I wonder if there is any way
to suppress the error message generated by Armadillo (" error: solve():
solution not found") and only output the custom error message (e.g.,
"Error: System is computationally singular").  Or conversely, is it
possible to only show the error message thrown by Armadillo? Thanks.


##########################################
[,1] [,2]
[1,]    1    1
[2,]    2    2
error: solve(): solution not found

Error: System is computationally singular

##########################################
On Sat, Jul 6, 2013 at 9:28 PM, Dirk Eddelbuettel <edd at debian.org> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130707/5e63b0de/attachment.html>