[Rcpp-commits] r4317 - in pkg/RcppArmadillo: . inst/examples/kalman

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu May 16 23:27:20 CEST 2013


Author: edd
Date: 2013-05-16 23:27:20 +0200 (Thu, 16 May 2013)
New Revision: 4317

Removed:
   pkg/RcppArmadillo/inst/examples/kalman/KalmanCpp.R
Modified:
   pkg/RcppArmadillo/ChangeLog
   pkg/RcppArmadillo/inst/examples/kalman/Kalman.cpp
   pkg/RcppArmadillo/inst/examples/kalman/benchmark.R
Log:
switch Kalman example from cxxfunction to sourceCpp


Modified: pkg/RcppArmadillo/ChangeLog
===================================================================
--- pkg/RcppArmadillo/ChangeLog	2013-05-14 18:59:07 UTC (rev 4316)
+++ pkg/RcppArmadillo/ChangeLog	2013-05-16 21:27:20 UTC (rev 4317)
@@ -1,3 +1,9 @@
+2013-05-16  Dirk Eddelbuettel  <edd at debian.org>
+
+	* inst/examples/kalman/benchmark.R: Switch from inline to sourceCpp
+	* inst/examples/kalman/Kalman.cpp: Added Rcpp attributes function
+	* inst/examples/kalman/KalmanCpp.R: Retired, no longer used
+
 2013-05-13  Dirk Eddelbuettel  <edd at debian.org>
 
 	* DESCRIPTION: Release 0.3.820

Modified: pkg/RcppArmadillo/inst/examples/kalman/Kalman.cpp
===================================================================
--- pkg/RcppArmadillo/inst/examples/kalman/Kalman.cpp	2013-05-14 18:59:07 UTC (rev 4316)
+++ pkg/RcppArmadillo/inst/examples/kalman/Kalman.cpp	2013-05-16 21:27:20 UTC (rev 4317)
@@ -1,3 +1,8 @@
+
+// [[Rcpp::depends(RcppArmadillo)]]
+
+#include <RcppArmadillo.h>
+
 using namespace arma;
 
 class Kalman {
@@ -44,3 +49,11 @@
        return Y;
     }
 };
+
+
+// [[Rcpp::export]]
+mat KalmanCpp(mat Z) {
+  Kalman K;
+  mat Y = K.estimate(Z);
+  return Y;
+}

Deleted: pkg/RcppArmadillo/inst/examples/kalman/KalmanCpp.R
===================================================================
--- pkg/RcppArmadillo/inst/examples/kalman/KalmanCpp.R	2013-05-14 18:59:07 UTC (rev 4316)
+++ pkg/RcppArmadillo/inst/examples/kalman/KalmanCpp.R	2013-05-16 21:27:20 UTC (rev 4317)
@@ -1,59 +0,0 @@
-kalmanClass <- '
-using namespace arma;
-
-class Kalman {
-private:
-    mat A, H, Q, R, xest, pest;
-    double dt;
-
-public:
-    // constructor, sets up data structures
-    Kalman() : dt(1.0) {
-        A.eye(6,6);
-        A(0,2) = A(1,3) = A(2,4) = A(3,5) = dt;
-        H.zeros(2,6);
-        H(0,0) = H(1,1) = 1.0;
-        Q.eye(6,6);
-        R = 1000 * eye(2,2);
-        xest.zeros(6,1);
-        pest.zeros(6,6);
-    }
-
-    // sole member function: estimate model
-    mat estimate(const mat & Z) {
-       unsigned int n = Z.n_rows, k = Z.n_cols;
-       mat Y = zeros(n, k);
-       mat xprd, pprd, S, B, kalmangain;
-       colvec z, y;
-
-       for (unsigned int i = 0; i<n; i++) {
-           z = Z.row(i).t();
-           // predicted state and covariance
-           xprd = A * xest;
-           pprd = A * pest * A.t() + Q;
-           // estimation
-           S = H * pprd.t() * H.t() + R;
-           B = H * pprd.t();
-           kalmangain = (solve(S, B)).t();
-           // estimated state and covariance
-           xest = xprd + kalmangain * (z - H * xprd);
-           pest = pprd - kalmangain * H * pprd;
-           // compute the estimated measurements
-           y = H * xest;
-           Y.row(i) = y.t();
-       }
-       return Y;
-    }
-};
-'
-
-suppressMessages(library(inline))
-kalmanSrc <- '
-  mat Z = as<mat>(ZS);       // passed from R
-  Kalman K;
-  mat Y = K.estimate(Z);
-  return wrap(Y);'
-
-KalmanCpp <- cxxfunction(signature(ZS="numeric"),
-                         body=kalmanSrc, include=kalmanClass,
-                         plugin="RcppArmadillo")

Modified: pkg/RcppArmadillo/inst/examples/kalman/benchmark.R
===================================================================
--- pkg/RcppArmadillo/inst/examples/kalman/benchmark.R	2013-05-14 18:59:07 UTC (rev 4316)
+++ pkg/RcppArmadillo/inst/examples/kalman/benchmark.R	2013-05-16 21:27:20 UTC (rev 4317)
@@ -1,13 +1,12 @@
 
 suppressMessages(library(Rcpp))
-suppressMessages(library(inline))
 suppressMessages(library(rbenchmark))
 suppressMessages(library(compiler))
 
 source("FirstKalmanR.R")
 source("KalmanR.R")
 source("KalmanRimp.R")
-source("KalmanCpp.R")
+sourceCpp("Kalman.cpp")
 
 FirstKalmanRC <- cmpfun(FirstKalmanR)
 KalmanRC <- cmpfun(KalmanR)



More information about the Rcpp-commits mailing list