Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shiftRowsUp/shiftRowsDown methods #127

Open
wants to merge 2 commits into
base: SNAPSHOT
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions main/ejml-ddense/src/org/ejml/dense/row/CommonOps_DDRM.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

/**
* <p>Performs absolute value of a matrix:<br>
* <br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down