1- /* ******************************************************************
1+ /* ****************************************************************************************
22 * File: lagg.hpp
33 *
44 * Lagged aggregation utilities for matrices.
4444 * lagg(mat, lag, byrow)
4545 * Temporal lag for time series
4646 *
47+ * Parameter Constraints
48+ *
49+ * Lattice graphs
50+ * large lag values beyond the graph diameter have no additional effect.
51+ * The algorithm automatically stops when no further neighbors are reachable.
52+ *
53+ * Regular grids
54+ * lag must not exceed the maximum grid dimension
55+ *
56+ * Time series
57+ * lag must be smaller than the number of observations
58+ *
4759 * Author: Wenbo Lyu (Github: @SpatLyu)
4860 * License: GPL-3
49- *******************************************************************/
61+ **************************************************************************************** * /
5062
5163#ifndef INFOXTR_LAGG_HPP
5264#define INFOXTR_LAGG_HPP
5567#include < cmath>
5668#include < limits>
5769#include < algorithm>
58- #include < unordered_set>
5970#include < stdexcept>
6071#include < cstddef>
6172
@@ -89,6 +100,9 @@ namespace lagg
89100 bool byrow = true
90101 )
91102 {
103+ if (mat.empty ())
104+ throw std::invalid_argument (" Input matrix is empty." );
105+
92106 const size_t n = nb.size ();
93107 const size_t p = mat.front ().size ();
94108
@@ -182,7 +196,10 @@ namespace lagg
182196 size_t lag = 1 ,
183197 bool byrow = true
184198 )
185- {
199+ {
200+ if (mat.empty ())
201+ throw std::invalid_argument (" Input matrix is empty." );
202+
186203 const size_t N = mat.size ();
187204 const size_t p = mat.front ().size ();
188205
@@ -191,6 +208,9 @@ namespace lagg
191208
192209 const size_t ncols = N / nrows;
193210
211+ if (lag > std::max (nrows, ncols))
212+ throw std::invalid_argument (" lag too large for grid dimensions." );
213+
194214 Matrix out;
195215 if (byrow)
196216 out.assign (N, Vector (p, NaN));
@@ -275,9 +295,15 @@ namespace lagg
275295 size_t lag = 1 ,
276296 bool byrow = true
277297 )
278- {
298+ {
299+ if (mat.empty ())
300+ throw std::invalid_argument (" Input matrix is empty." );
301+
279302 const size_t n = mat.size ();
280303 const size_t p = mat.front ().size ();
304+
305+ if (lag >= n)
306+ throw std::invalid_argument (" lag must be smaller than number of observations." );
281307
282308 Matrix out;
283309 if (byrow)
0 commit comments