forked from GreenleafLab/ArchR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneral_Utils.cpp
91 lines (85 loc) · 2.4 KB
/
General_Utils.cpp
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
#include <Rcpp.h>
using namespace Rcpp;
using namespace std;
// // [[Rcpp::export]]
// IntegerMatrix tabulate1dCpp(IntegerVector x1, int xmin, int xmax){
// IntegerVector x = clone(x1);
// IntegerVector r = seq(xmin,xmax);
// IntegerMatrix out(r.size(),2);
// out(_, 0) = r;
// int n = x.size();
// int xi;
// for(int i = 0; i < n; i++){
// xi = (x[i] - xmin);
// if(xi >= 0 && xi <= r.size()){
// out( xi , 1 ) = out( xi , 1 ) + 1;
// }
// }
// return out;
// }
// [[Rcpp::export]]
IntegerMatrix tabulate2dCpp(IntegerVector &x, int &xmin, int &xmax, IntegerVector &y, int &ymin, int &ymax){
if(x.size() != y.size()){
stop("width must equal size!");
}
int n = x.size();
IntegerVector rx = seq(xmin, xmax);
IntegerVector ry = seq(ymin, ymax);
IntegerMatrix mat( ry.size() , rx.size() );
int rys = ry.size();
int rxs = rx.size();
int xi,yi;
for(int i = 0; i < n; i++){
xi = (x[i] - xmin);
yi = (y[i] - ymin);
if(yi >= 0 && yi < rys){
if(xi >= 0 && xi < rxs){
mat( yi , xi ) = mat( yi , xi ) + 1;
}
}
}
return mat;
}
// // [[Rcpp::export]]
// IntegerMatrix tabulate2dCpp(IntegerVector x1, int xmin, int xmax, IntegerVector y1, int ymin, int ymax){
// if(x1.size() != y1.size()){
// stop("width must equal size!");
// }
// IntegerVector x = clone(x1);
// IntegerVector y = clone(y1);
// int n = x.size();
// IntegerVector rx = seq(xmin,xmax);
// IntegerVector ry = seq(ymin,ymax);
// IntegerMatrix mat( ry.size() , rx.size() );
// int xi,yi;
// for(int i = 0; i < n; i++){
// xi = (x[i] - xmin);
// yi = (y[i] - ymin);
// if(yi >= 0 && yi < ry.size()){
// if(xi >= 0 && xi < rx.size()){
// mat( yi , xi ) = mat( yi , xi ) + 1;
// }
// }
// }
// return mat;
// }
// [[Rcpp::export]]
Rcpp::NumericVector computeSparseRowVariances(IntegerVector j, NumericVector val, NumericVector rm, int n) {
const int nv = j.size();
const int nm = rm.size();
Rcpp::NumericVector rv(nm);
Rcpp::NumericVector rit(nm);
int current;
// Calculate RowVars Initial
for (int i = 0; i < nv; ++i) {
current = j(i) - 1;
rv(current) = rv(current) + (val(i) - rm(current)) * (val(i) - rm(current));
rit(current) = rit(current) + 1;
}
// Calculate Remainder Variance
for (int i = 0; i < nm; ++i) {
rv(i) = rv(i) + (n - rit(i))*rm(i)*rm(i);
}
rv = rv / (n - 1);
return(rv);
}