Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use function templates to accommodate other vector types #10

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/dspline.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ using namespace Rcpp;
// Simple utilities

int fact(int k);
void CumSum(NumericVector v, int i);
void RevCumSum(NumericVector v, int i);
void Diff(NumericVector v, int i);
void RevDiff(NumericVector v, int i);
void GapWeight(NumericVector v, int i, NumericVector xd);
void InvGapWeight(NumericVector v, int i, NumericVector xd);
template<typename T>
void CumSum(T& v, int i);
template<typename T>
void RevCumSum(T& v, int i);
template<typename T>
void Diff(T& v, int i);
template<typename T>
void RevDiff(T& v, int i);
template<typename T, typename U>
void GapWeight(T& v, int i, U& xd);
template<typename T, typename U>
void InvGapWeight(T& v, int i, U& xd);

/******************************************************************************/
// Divided differences, discrete derivatives, and discrete integrals
Expand Down
36 changes: 30 additions & 6 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Simple utilities

#include <Rcpp.h>
#include <RcppEigen.h>
#include <Eigen/Sparse>
using namespace Rcpp;

// Factorial
Expand All @@ -12,43 +14,65 @@ int fact(int k) {
}

// Overwrite v with cumulative sums, starting at i
void CumSum(NumericVector v, int i) {
template<typename T>
void CumSum(T& v, int i) {
for (int j = i+1; j < v.size(); j++) {
v[j] += v[j-1];
}
}
template void CumSum<NumericVector>(NumericVector& v, int i);
template void CumSum<Eigen::VectorXd>(Eigen::VectorXd& v, int i);

// Overwrite v with reverse cumulative sums, ending at i
void RevCumSum(NumericVector v, int i) {
template<typename T>
void RevCumSum(T& v, int i) {
for (int j = v.size()-2; j >= i; j--) {
v[j] += v[j+1];
}
}
template void RevCumSum<NumericVector>(NumericVector& v, int i);
template void RevCumSum<Eigen::VectorXd>(Eigen::VectorXd& v, int i);

// Overwrite v with pairwise differences, starting at i
void Diff(NumericVector v, int i) {
template<typename T>
void Diff(T& v, int i) {
for (int j = v.size()-1; j >= i; j--) {
v[j] -= v[j-1];
}
}
template void Diff<NumericVector>(NumericVector& v, int i);
template void Diff<Eigen::VectorXd>(Eigen::VectorXd& v, int i);

// Overwrite v with reverse pairwise differences, ending at i
void RevDiff(NumericVector v, int i) {
template<typename T>
void RevDiff(T& v, int i) {
for (int j = i; j < v.size()-1; j++) {
v[j] -= v[j+1];
}
}
template void RevDiff<NumericVector>(NumericVector& v, int i);
template void RevDiff<Eigen::VectorXd>(Eigen::VectorXd& v, int i);

// Apply gap weighting to v (wrt xd), starting at i
void GapWeight(NumericVector v, int i, NumericVector xd) {
template<typename T, typename U>
void GapWeight(T& v, int i, U& xd) {
for (int j = i; j < v.size(); j++) {
v[j] *= (xd[j] - xd[j-i]) / i;
}
}
template void GapWeight<NumericVector,NumericVector>(NumericVector& v, int i, NumericVector& xd);
template void GapWeight<NumericVector,Eigen::VectorXd>(NumericVector& v, int i, Eigen::VectorXd& xd);
template void GapWeight<Eigen::VectorXd,NumericVector>(Eigen::VectorXd& v, int i, NumericVector& xd);
template void GapWeight<Eigen::VectorXd,Eigen::VectorXd>(Eigen::VectorXd& v, int i, Eigen::VectorXd& xd);

// Apply invesre gap weighting to v (wrt xd), starting at i
void InvGapWeight(NumericVector v, int i, NumericVector xd) {
template<typename T, typename U>
void InvGapWeight(T& v, int i, U& xd) {
for (int j = i; j < v.size(); j++) {
v[j] /= (xd[j] - xd[j-i]) / i;
}
}
template void InvGapWeight<NumericVector,NumericVector>(NumericVector& v, int i, NumericVector& xd);
template void InvGapWeight<NumericVector,Eigen::VectorXd>(NumericVector& v, int i, Eigen::VectorXd& xd);
template void InvGapWeight<Eigen::VectorXd,NumericVector>(Eigen::VectorXd& v, int i, NumericVector& xd);
template void InvGapWeight<Eigen::VectorXd,Eigen::VectorXd>(Eigen::VectorXd& v, int i, Eigen::VectorXd& xd);