Skip to content

Commit

Permalink
matrix_smooth.c rewritten in C++
Browse files Browse the repository at this point in the history
  • Loading branch information
dankelley committed Mar 3, 2018
1 parent 2bbc7c4 commit 649d864
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 49 deletions.
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

matrix_smooth <- function(mat) {
.Call(`_oce_matrix_smooth`, mat)
}

trap <- function(x, y, type) {
.Call(`_oce_trap`, x, y, type)
}
Expand Down
3 changes: 2 additions & 1 deletion R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -3906,7 +3906,8 @@ matrixSmooth <- function(m, passes=1)
storage.mode(m) <- "double"
if (passes > 0) {
for (pass in seq.int(1, passes, 1)) {
m <- .Call("matrix_smooth", m)
message("pass=", pass)
m <- .Call(`_oce_matrix_smooth`, m)
}
} else {
warning("matrixSmooth given passes<=0, so returning matrix unmodified")
Expand Down
11 changes: 11 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@

using namespace Rcpp;

// matrix_smooth
NumericMatrix matrix_smooth(NumericMatrix mat);
RcppExport SEXP _oce_matrix_smooth(SEXP matSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< NumericMatrix >::type mat(matSEXP);
rcpp_result_gen = Rcpp::wrap(matrix_smooth(mat));
return rcpp_result_gen;
END_RCPP
}
// trap
NumericVector trap(NumericVector x, NumericVector y, NumericVector type);
RcppExport SEXP _oce_trap(SEXP xSEXP, SEXP ySEXP, SEXP typeSEXP) {
Expand Down
46 changes: 0 additions & 46 deletions src/matrix_smooth.c

This file was deleted.

32 changes: 32 additions & 0 deletions src/matrix_smooth.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <Rcpp.h>
using namespace Rcpp;

// This is a utility function called by matrixSmooth,
// so it does not get a wrapper inserted into the R namespace;
// that's why there is no roxygen @export here, and no roxygen
// documentation, either. However, we need to tell Rcpp to export
// it, so matrixSmooth() can .Call() it.
//
// [[Rcpp::export]]

NumericMatrix matrix_smooth(NumericMatrix mat)
{
int ni = mat.nrow(), nj=mat.ncol();
NumericMatrix res(ni, nj);
// copy edges (FIXME: could use 1D smoother here)
for (int j = 0; j < nj; j++) {
res(0, j) = mat(0, j);
res(ni-1, j) = mat(ni-1, j);
}
for (int i = 0; i < ni; i++) {
res(i, 0) = mat(i, 0);
res(i, nj-1) = mat(i, nj-1);
}
// smooth middle
for (int i = 1; i < ni - 1; i++)
for (int j = 1; j < nj - 1; j++)
res(i, j) = (2.0*mat(i, j) +
mat(i-1, j) + mat(i+1, j) + mat(i, j-1) + mat(i, j+1)) / 6.0;
return(res);
}

2 changes: 2 additions & 0 deletions src/registerDynamicSymbol.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#include <R_ext/Rdynload.h>

extern SEXP _oce_trap(SEXP, SEXP, SEXP);
extern SEXP _oce_matrix_smooth(SEXP);

static const R_CallMethodDef CallEntries[] = {
{"_oce_matrix_smooth", (DL_FUNC) &_oce_matrix_smooth, 1},
{"_oce_trap", (DL_FUNC) &_oce_trap, 3},
{NULL, NULL, 0}
};
Expand Down
8 changes: 6 additions & 2 deletions src/trap.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#include <Rcpp.h>
using namespace Rcpp;

// This is a utility function called by \code{\link{integrateTrapezoid}},
// not intended for direct use.
// This is a utility function called by integrateTrapeziod(),
// so it does not get a wrapper inserted into the R namespace;
// that's why there is no roxygen @export here, and no roxygen
// documentation, either. However, we need to tell Rcpp to export
// it, so integreateTrapezoid() can .Call() it.
//
// [[Rcpp::export]]
NumericVector trap(NumericVector x, NumericVector y, NumericVector type)
{
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test_misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,21 @@ test_that("get_bit (unused in oce)", {
expect_equal(c(0, 0, 1, 1, 1, 0, 1, 0), bits)
})

test_that("matrixSmooth", {
data(volcano)
v <- matrixSmooth(volcano)
## These values are from the C version of src/matrix_smooth.c as it
## existed on 2018-03-03, and the point of the present check is to
## test the code rewriting in C++ as src/matrix_smooth.cpp, added
## on that date. The first check is to be sure the edge treatmet
## still works, and the others are somewhat arbitrary, built on the
## assumption that if the C++ translation is faulty, the results
## will be crazy-wrong (e.g. transposed matrix).
ve <- matrix(c(100, 101, 102, 100, 101.1666667, 102.1666667, 101,
101.8333333, 102.8333333), byrow=FALSE, nrow=3)
expect_equal(ve, v[1:3, 1:3])
expect_equal(mean(v), 130.1787262)
expect_equal(mean(v[,10]), 121.3429119)
expect_equal(mean(v[10,]), 127.9808743)
})

0 comments on commit 649d864

Please sign in to comment.