2 # lm() via GNU GSL -- building on something from the Intro to HPC tutorials
4 # Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
6 # This file is part of Rcpp.
8 # Rcpp is free software: you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 2 of the License, or
11 # (at your option) any later version.
13 # Rcpp is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21 suppressMessages(require(Rcpp))
23 ## NOTE: This is the old way to compile Rcpp code inline.
24 ## The code here has left as a historical artifact and tribute to the old way.
25 ## Please use the code under the "new" inline compilation section.
27 suppressMessages(require(inline))
29 lmGSL_old <- function() {
33 Rcpp::NumericVector Yr(Ysexp);
34 Rcpp::NumericMatrix Xr(Xsexp);
36 int i,j,n = Xr.nrow(), k = Xr.ncol();
39 gsl_matrix *X = gsl_matrix_alloc (n, k);
40 gsl_vector *y = gsl_vector_alloc (n);
41 gsl_vector *c = gsl_vector_alloc (k);
42 gsl_matrix *cov = gsl_matrix_alloc (k, k);
43 for (i = 0; i < n; i++) {
44 for (j = 0; j < k; j++)
45 gsl_matrix_set (X, i, j, Xr(i,j));
46 gsl_vector_set (y, i, Yr(i));
49 gsl_multifit_linear_workspace *work = gsl_multifit_linear_alloc (n, k);
50 gsl_multifit_linear (X, y, c, cov, &chisq, work);
51 gsl_multifit_linear_free (work);
53 Rcpp::NumericVector coefr(k), stderrestr(k);
54 for (i = 0; i < k; i++) {
55 coefr(i) = gsl_vector_get(c,i);
56 stderrestr(i) = sqrt(gsl_matrix_get(cov,i,i));
61 gsl_matrix_free (cov);
64 return Rcpp::List::create( Rcpp::Named( "coef", coefr),
65 Rcpp::Named( "stderr", stderrestr));
68 ## turn into a function that R can call
69 ## compileargs redundant on Debian/Ubuntu as gsl headers are found anyway
70 fun_old <- cxxfunction(signature(Ysexp="numeric", Xsexp="numeric"),
72 includes="#include <gsl/gsl_multifit.h>",
76 ## NOTE: Within this section, the new way to compile Rcpp code inline has been
77 ## written. Please use the code next as a template for your own project.
83 #include <gsl/gsl_multifit.h>
84 // [[Rcpp::depends(RcppGSL)]]
87 Rcpp::List fun(Rcpp::NumericVector Yr, Rcpp::NumericMatrix Xr){
89 int i, j, n = Xr.nrow(), k = Xr.ncol();
92 RcppGSL::Matrix X(n, k); // allocate a gsl_matrix<double> of dim n, k
93 RcppGSL::Vector y(n); // allocate a gsl_vector<double> of length n
94 RcppGSL::Vector c(k); // allocate a gsl_vector<double> of length k
95 RcppGSL::Matrix cov(k, k); // allocate a gsl_matrix<double> of dim k, k
97 for (i = 0; i < n; i++) {
98 for (j = 0; j < k; j++)
100 y[i] = Yr(i); // Note vector requires [] not ()
103 gsl_multifit_linear_workspace *work = gsl_multifit_linear_alloc (n, k);
104 gsl_multifit_linear (X, y, c, cov, &chisq, work);
105 gsl_multifit_linear_free (work);
107 Rcpp::NumericVector coefr(k), stderrestr(k);
108 for (i = 0; i < k; i++) {
110 stderrestr(i) = sqrt(cov(i,i));
114 return Rcpp::List::create( Rcpp::Named("coef") = coefr,
115 Rcpp::Named("stderr") = stderrestr);