diff --git a/main/ejml-ddense/src/org/ejml/dense/row/CommonOps_DDRM.java b/main/ejml-ddense/src/org/ejml/dense/row/CommonOps_DDRM.java index ca3c75b70..274176ca8 100644 --- a/main/ejml-ddense/src/org/ejml/dense/row/CommonOps_DDRM.java +++ b/main/ejml-ddense/src/org/ejml/dense/row/CommonOps_DDRM.java @@ -2770,6 +2770,48 @@ public static DMatrixRMaj permuteRowInv( int[] pinv, DMatrixRMaj input, DMatrixR return output; } + /** + * Fills all rows in the specified range with zeros + * + * @param start Index of the first row (inclusivce) to be filled + * @param end Index of the last row (exclusive) to be filled + */ + public static void zeroRows(DMatrixRMaj a, int start, int end) { + Arrays.fill(a.data, start * a.numCols, end * a.numCols, 0); + } + + /** + * Shifts all elements up by the specified number of rows. + * Vacated rows are set to zero. + * + * @param a A matrix. Modified. + * @param c Number of rows to shift by. + */ + public static void shiftRowsUp(DMatrixRMaj a, int c) { + if (c < a.numRows) { + System.arraycopy(a.data, c * a.numCols, a.data, 0, (a.numRows - c) * a.numCols); + zeroRows(a, a.numRows - c, a.numRows); + } else { + a.zero(); + } + } + + /** + * Shifts all elements down by the specified number of rows + * Vacated rows are set to zero. + * + * @param a A matrix. Modified. + * @param c Number of rows to shift by. + */ + public static void shiftRowsDown(DMatrixRMaj a, int c) { + if (c < a.numRows) { + System.arraycopy(a.data, 0, a.data, c * a.numCols, (a.numRows - c) * a.numCols); + zeroRows(a, 0, c); + } else { + a.zero(); + } + } + /** *
Performs absolute value of a matrix:
*
diff --git a/main/ejml-ddense/test/org/ejml/dense/row/TestCommonOps_DDRM.java b/main/ejml-ddense/test/org/ejml/dense/row/TestCommonOps_DDRM.java
index c35f9e576..736609f34 100644
--- a/main/ejml-ddense/test/org/ejml/dense/row/TestCommonOps_DDRM.java
+++ b/main/ejml-ddense/test/org/ejml/dense/row/TestCommonOps_DDRM.java
@@ -921,6 +921,50 @@ public void insert() {
}
}
+ @Test
+ public void shiftRowsUp() {
+ DMatrixRMaj A = RandomMatrices_DDRM.rectangle(5, 6, 0, 1, rand);
+
+ DMatrixRMaj B = new DMatrixRMaj(5, 6);
+
+ for (int n = 0; n <= A.numRows; n++) {
+ B.set(A);
+ CommonOps_DDRM.shiftRowsUp(B, n);
+ for (int row = 0; row < A.numRows - n; row++) {
+ for (int col = 0; col < A.numCols; col++) {
+ assertEquals(A.get(row + n, col), B.get(row, col), UtilEjml.TEST_F64);
+ }
+ }
+ for (int row = A.numRows - n; row < A.numRows; row++) {
+ for (int col = 0; col < A.numCols; col++) {
+ assertEquals(0.0, B.get(row, col), UtilEjml.TEST_F64);
+ }
+ }
+ }
+ }
+
+ @Test
+ public void shiftRowsDown() {
+ DMatrixRMaj A = RandomMatrices_DDRM.rectangle(5, 6, 0, 1, rand);
+
+ DMatrixRMaj B = new DMatrixRMaj(5, 6);
+
+ for (int n = 0; n <= A.numRows; n++) {
+ B.set(A);
+ CommonOps_DDRM.shiftRowsDown(B, n);
+ for (int row = 0; row < n; row++) {
+ for (int col = 0; col < A.numCols; col++) {
+ assertEquals(0.0, B.get(row, col), UtilEjml.TEST_F64);
+ }
+ }
+ for (int row = n; row < A.numRows; row++) {
+ for (int col = 0; col < A.numCols; col++) {
+ assertEquals(A.get(row - n, col), B.get(row, col), UtilEjml.TEST_F64);
+ }
+ }
+ }
+ }
+
@Test
public void addEquals() {
DMatrixRMaj a = new DMatrixRMaj(2, 3, true, 0, 1, 2, 3, 4, 5);