Skip to content

Commit 77eed7c

Browse files
authored
Merge pull request #44 from SpatLyu/dev
enhance surd utility
2 parents 532f939 + 542e59d commit 77eed7c

6 files changed

Lines changed: 316 additions & 28 deletions

File tree

R/RcppExports.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,7 @@ RcppNN4DistMatSub <- function(distmat, lib, pred, k, include_self = FALSE) {
101101
.Call(`_infoxtr_RcppNN4DistMatSub`, distmat, lib, pred, k, include_self)
102102
}
103103

104+
RcppSURD <- function(mat, target, agent, lag = 1L, n = 5L, max_order = 3L, threads = 1L, base = 2.0, normalize = TRUE, method = "equal", nb = NULL, nrows = NULL) {
105+
.Call(`_infoxtr_RcppSURD`, mat, target, agent, lag, n, max_order, threads, base, normalize, method, nb, nrows)
106+
}
107+

inst/include/infoxtr/lagg.hpp

Lines changed: 112 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,48 @@
11
/*******************************************************************
22
* File: lagg.hpp
33
*
4-
* Lag utilities for matrices.
4+
* Lagged aggregation utilities for matrices.
55
*
6-
* Provides lagged aggregation operators for:
6+
* This module provides lag operators for three common structures
77
*
8-
* - Arbitrary lattice graphs
9-
* - Regular grids
10-
* - Time series
8+
* 1. Arbitrary lattice graphs
9+
* 2. Regular spatial grids
10+
* 3. Time series
1111
*
12-
* Each column of the input matrix is treated as a variable.
12+
* The input matrix follows the internal representation used in
13+
* the infoxtr library
14+
*
15+
* Matrix = std::vector<std::vector<double>>
16+
*
17+
* Each inner vector represents one observation unit and each
18+
* column corresponds to a variable.
19+
*
20+
* Output Orientation
21+
*
22+
* A boolean parameter `byrow` controls the orientation of the
23+
* returned matrix.
24+
*
25+
* byrow = true
26+
* Output keeps the current layout
27+
* Each observation corresponds to one row vector
28+
*
29+
* byrow = false
30+
* Output is stored in column major orientation
31+
* Each inner vector corresponds to one variable
32+
*
33+
* This option allows direct compatibility with external matrix
34+
* layouts used by R or column oriented algorithms.
35+
*
36+
* Supported operations
37+
*
38+
* lagg(mat, nb, lag, byrow)
39+
* Lag aggregation on arbitrary neighbor graphs
40+
*
41+
* lagg(mat, nrows, lag, byrow)
42+
* Lag aggregation on regular grids
43+
*
44+
* lagg(mat, lag, byrow)
45+
* Temporal lag for time series
1346
*
1447
* Author: Wenbo Lyu (Github: @SpatLyu)
1548
* License: GPL-3
@@ -52,16 +85,29 @@ namespace lagg
5285
inline Matrix lagg(
5386
const Matrix& mat,
5487
const NeighborMat& nb,
55-
size_t lag
88+
size_t lag = 1,
89+
bool byrow = true
5690
)
5791
{
5892
const size_t n = nb.size();
5993
const size_t p = mat.front().size();
6094

61-
Matrix out(n, Vector(p, NaN));
95+
Matrix out;
96+
if (byrow)
97+
out.assign(n, Vector(p, NaN));
98+
else
99+
out.assign(p, Vector(n, NaN));
100+
101+
if (lag == 0)
102+
{
103+
if (byrow)
104+
return mat;
62105

63-
if (lag == 0) {
64-
return mat;
106+
for (size_t i = 0; i < n; ++i)
107+
for (size_t j = 0; j < p; ++j)
108+
out[j][i] = mat[i][j];
109+
110+
return out;
65111
}
66112

67113
for (size_t i = 0; i < n; ++i)
@@ -105,9 +151,14 @@ namespace lagg
105151
++cnt;
106152
}
107153
}
108-
154+
109155
if (cnt > 0)
110-
out[i][j] = sum / cnt;
156+
{
157+
if (byrow)
158+
out[i][j] = sum / cnt;
159+
else
160+
out[j][i] = sum / cnt;
161+
}
111162
}
112163
}
113164

@@ -128,7 +179,8 @@ namespace lagg
128179
inline Matrix lagg(
129180
const Matrix& mat,
130181
size_t nrows,
131-
size_t lag
182+
size_t lag = 1,
183+
bool byrow = true
132184
)
133185
{
134186
const size_t N = mat.size();
@@ -139,10 +191,23 @@ namespace lagg
139191

140192
const size_t ncols = N / nrows;
141193

142-
Matrix out(N, Vector(p, NaN));
194+
Matrix out;
195+
if (byrow)
196+
out.assign(N, Vector(p, NaN));
197+
else
198+
out.assign(p, Vector(N, NaN));
143199

144200
if (lag == 0)
145-
return mat;
201+
{
202+
if (byrow)
203+
return mat;
204+
205+
for (size_t i = 0; i < N; ++i)
206+
for (size_t j = 0; j < p; ++j)
207+
out[j][i] = mat[i][j];
208+
209+
return out;
210+
}
146211

147212
std::vector<std::pair<int,int>> offsets;
148213

@@ -186,9 +251,14 @@ namespace lagg
186251
}
187252
}
188253
}
189-
254+
190255
if (cnt > 0)
191-
out[id][j] = sum / cnt;
256+
{
257+
if (byrow)
258+
out[id][j] = sum / cnt;
259+
else
260+
out[j][id] = sum / cnt;
261+
}
192262
}
193263
}
194264
}
@@ -202,24 +272,44 @@ namespace lagg
202272

203273
inline Matrix lagg(
204274
const Matrix& mat,
205-
size_t lag
275+
size_t lag = 1,
276+
bool byrow = true
206277
)
207278
{
208279
const size_t n = mat.size();
209280
const size_t p = mat.front().size();
210-
211-
Matrix out(n, Vector(p, NaN));
281+
282+
Matrix out;
283+
if (byrow)
284+
out.assign(n, Vector(p, NaN));
285+
else
286+
out.assign(p, Vector(n, NaN));
212287

213288
if (lag == 0)
214-
return mat;
289+
{
290+
if (byrow)
291+
return mat;
292+
293+
for (size_t i = 0; i < n; ++i)
294+
for (size_t j = 0; j < p; ++j)
295+
out[j][i] = mat[i][j];
296+
297+
return out;
298+
}
215299

216300
for (size_t t = lag; t < n; ++t)
217301
{
218302
for (size_t j = 0; j < p; ++j)
219303
{
220304
double v = mat[t - lag][j];
305+
221306
if (!std::isnan(v))
222-
out[t][j] = v;
307+
{
308+
if (byrow)
309+
out[t][j] = v;
310+
else
311+
out[j][t] = v;
312+
}
223313
}
224314
}
225315

src/InfotheoExps.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ Rcpp::List RcppDiscSURD(SEXP mat,
589589

590590
// Wrapper function to preform SURD decomposition for continuous data
591591
// [[Rcpp::export(rng = false)]]
592-
Rcpp::List RcppContSURD(SEXP mat,
592+
Rcpp::List RcppContSURD(const Rcpp::NumericMatrix& mat,
593593
int max_order = 3,
594594
int k = 3,
595595
int alg = 0,

src/LagExps.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Rcpp::NumericMatrix RcppGenLatticeLag(const Rcpp::NumericMatrix& mat,
1818

1919
// Calculate lagged values
2020
std::vector<std::vector<double>> lagged_values =
21-
infoxtr::lagg::lagg(cppMat, nb_std, static_cast<size_t>(std::abs(lag)));
21+
infoxtr::lagg::lagg(cppMat, nb_std, static_cast<size_t>(std::abs(lag)), true);
2222

2323
return infoxtr::convert::mat_std2r(lagged_values, true);
2424
}
@@ -34,7 +34,7 @@ Rcpp::NumericMatrix RcppGenGridLag(const Rcpp::NumericMatrix& mat,
3434
std::vector<std::vector<double>> lagged_values =
3535
infoxtr::lagg::lagg(cppMat,
3636
static_cast<size_t>(std::abs(nrows)),
37-
static_cast<size_t>(std::abs(lag)));
37+
static_cast<size_t>(std::abs(lag)), true);
3838

3939
return infoxtr::convert::mat_std2r(lagged_values, true);
4040
}
@@ -48,7 +48,7 @@ Rcpp::NumericMatrix RcppGenTSLag(const Rcpp::NumericMatrix& mat,
4848

4949
// Calculate lagged values
5050
std::vector<std::vector<double>> lagged_values =
51-
infoxtr::lagg::lagg(cppMat, static_cast<size_t>(std::abs(lag)));
51+
infoxtr::lagg::lagg(cppMat, static_cast<size_t>(std::abs(lag)), true);
5252

5353
return infoxtr::convert::mat_std2r(lagged_values, true);
5454
}

src/RcppExports.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,11 @@ BEGIN_RCPP
269269
END_RCPP
270270
}
271271
// RcppContSURD
272-
Rcpp::List RcppContSURD(SEXP mat, int max_order, int k, int alg, int threads, double base, bool normalize);
272+
Rcpp::List RcppContSURD(const Rcpp::NumericMatrix& mat, int max_order, int k, int alg, int threads, double base, bool normalize);
273273
RcppExport SEXP _infoxtr_RcppContSURD(SEXP matSEXP, SEXP max_orderSEXP, SEXP kSEXP, SEXP algSEXP, SEXP threadsSEXP, SEXP baseSEXP, SEXP normalizeSEXP) {
274274
BEGIN_RCPP
275275
Rcpp::RObject rcpp_result_gen;
276-
Rcpp::traits::input_parameter< SEXP >::type mat(matSEXP);
276+
Rcpp::traits::input_parameter< const Rcpp::NumericMatrix& >::type mat(matSEXP);
277277
Rcpp::traits::input_parameter< int >::type max_order(max_orderSEXP);
278278
Rcpp::traits::input_parameter< int >::type k(kSEXP);
279279
Rcpp::traits::input_parameter< int >::type alg(algSEXP);
@@ -375,6 +375,27 @@ BEGIN_RCPP
375375
return rcpp_result_gen;
376376
END_RCPP
377377
}
378+
// RcppSURD
379+
Rcpp::List RcppSURD(const Rcpp::NumericMatrix& mat, const Rcpp::IntegerVector& target, const Rcpp::IntegerVector& agent, int lag, int n, int max_order, int threads, double base, bool normalize, const std::string& method, Rcpp::Nullable<Rcpp::List> nb, Rcpp::Nullable<int> nrows);
380+
RcppExport SEXP _infoxtr_RcppSURD(SEXP matSEXP, SEXP targetSEXP, SEXP agentSEXP, SEXP lagSEXP, SEXP nSEXP, SEXP max_orderSEXP, SEXP threadsSEXP, SEXP baseSEXP, SEXP normalizeSEXP, SEXP methodSEXP, SEXP nbSEXP, SEXP nrowsSEXP) {
381+
BEGIN_RCPP
382+
Rcpp::RObject rcpp_result_gen;
383+
Rcpp::traits::input_parameter< const Rcpp::NumericMatrix& >::type mat(matSEXP);
384+
Rcpp::traits::input_parameter< const Rcpp::IntegerVector& >::type target(targetSEXP);
385+
Rcpp::traits::input_parameter< const Rcpp::IntegerVector& >::type agent(agentSEXP);
386+
Rcpp::traits::input_parameter< int >::type lag(lagSEXP);
387+
Rcpp::traits::input_parameter< int >::type n(nSEXP);
388+
Rcpp::traits::input_parameter< int >::type max_order(max_orderSEXP);
389+
Rcpp::traits::input_parameter< int >::type threads(threadsSEXP);
390+
Rcpp::traits::input_parameter< double >::type base(baseSEXP);
391+
Rcpp::traits::input_parameter< bool >::type normalize(normalizeSEXP);
392+
Rcpp::traits::input_parameter< const std::string& >::type method(methodSEXP);
393+
Rcpp::traits::input_parameter< Rcpp::Nullable<Rcpp::List> >::type nb(nbSEXP);
394+
Rcpp::traits::input_parameter< Rcpp::Nullable<int> >::type nrows(nrowsSEXP);
395+
rcpp_result_gen = Rcpp::wrap(RcppSURD(mat, target, agent, lag, n, max_order, threads, base, normalize, method, nb, nrows));
396+
return rcpp_result_gen;
397+
END_RCPP
398+
}
378399

379400
static const R_CallMethodDef CallEntries[] = {
380401
{"_infoxtr_RcppDisc", (DL_FUNC) &_infoxtr_RcppDisc, 10},
@@ -402,6 +423,7 @@ static const R_CallMethodDef CallEntries[] = {
402423
{"_infoxtr_RcppNN4MatSub", (DL_FUNC) &_infoxtr_RcppNN4MatSub, 7},
403424
{"_infoxtr_RcppNN4DistMat", (DL_FUNC) &_infoxtr_RcppNN4DistMat, 3},
404425
{"_infoxtr_RcppNN4DistMatSub", (DL_FUNC) &_infoxtr_RcppNN4DistMatSub, 5},
426+
{"_infoxtr_RcppSURD", (DL_FUNC) &_infoxtr_RcppSURD, 12},
405427
{NULL, NULL, 0}
406428
};
407429

0 commit comments

Comments
 (0)