-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupport.cpp
More file actions
114 lines (97 loc) · 2.7 KB
/
support.cpp
File metadata and controls
114 lines (97 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <RcppArmadillo.h>
#include <strings.h>
#include <cstdlib>
#include <iterator>
#include "support.h"
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::plugins("cpp11")]]
using namespace Rcpp;
// This is a simple example of exporting a C++ function to R. You can
// source this function into an R session using the Rcpp::sourceCpp
// function (or via the Source button on the editor toolbar). Learn
// more about Rcpp at:
//
// http://www.rcpp.org/
// http://adv-r.had.co.nz/Rcpp.html
// http://gallery.rcpp.org/
//'@name mat_exp
//'@description
//'Function to take the exponential of each element of a matrix individually.
//'If an element is x, this function returns exp(coef*x)
//' @param mat A numeric matrix
//' @param coef The exponent
//'
arma::mat mat_exp(const arma::mat& mat, double coef = 1) {
arma::mat res = mat*coef;
arma::mat::iterator it = res.begin();
for (;it != res.end(); ++it) {
// std::cout << *it << std::endl;
*it = std::exp(*it);
}
return res;
}
// function to turn a matrix into a sparse one using a threshold
// to use in order to avoid computing flows for unrealistic values, for active travel 15 km seems a good fit
arma::sp_mat mat_to_sparse(const arma::mat& x, double threshold = 15.0) {
arma::sp_mat res(x);
arma::sp_mat::iterator it = res.begin();
for(;it != res.end();++it) {
if (*it > threshold) {
*it = 0;
}
}
return res;
}
//'
//'@description
//'Apply function from R rewritten for sparse matrices in cpp to gain speed.
//'
//'
//'@param mat1 The matrix to which the functino is applied
//'@param vec the vector in which the result will be stored.
//'@param dim 1 to perform the operation rowwise, 2 to perform on columnwise.
//'
//'@returns
//'A vector, to which the sum function has been applied either by row or by column
//'
//'@examples
//'library(Matrix)
//'mat = matrix(data = c(1,2,3,1,2,3,1,2,3), nrow = 3,sparse = TRUE)
//'
//'res1 = apply_iter(mat,1)
//'res2 = apply_iter(mat,2)
//@export
// [[Rcpp::export]]
arma::vec apply_iter(const arma::sp_mat& x, int dim = 1) {
int n(0);
switch(dim) {
case 1:
n = x.n_rows;
break;
case 2:
n = x.n_cols;
break;
}
arma::vec result(n);
arma::sp_mat::const_iterator i = x.begin();
if (dim ==1){
for (; i != x.end(); ++i) {
result(i.row())+=*i;
}
} else if (dim == 2){
for (; i != x.end(); ++i) {
result(i.col())+=*i;
}
}
return result;
}
// [[Rcpp::export]]
double pearsoncoeff(const arma::mat& X, const arma::mat& Y)
{
return arma::as_scalar(arma::cor(X.as_col(),Y.as_col()));
}
// [[Rcpp::export]]
double abs_val(double x) {
if(x >= 0) return x;
else return -x;
}