Skip to content

Commit

Permalink
Apply @vkostyukov code review.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrille.savelief committed Jul 30, 2017
1 parent 62af1cf commit 49a62a8
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/main/java/org/la4j/Matrix.java
Expand Up @@ -1038,11 +1038,16 @@ public void setColumn(int j, Vector column) {
* @return matrix with row.
*/
public Matrix insertRow(int i, Vector row) {
if (i >= rows || i < 0) {
throw new IndexOutOfBoundsException("Illegal row number, must be 0.." + (rows - 1));
if (i > rows || i < 0) {
throw new IndexOutOfBoundsException("Illegal row number, must be 0.." + rows);
}

Matrix result = blankOfShape(rows + 1, columns);
Matrix result;
if (columns == 0) {
result = blankOfShape(rows + 1, row.length());
} else {
result = blankOfShape(rows + 1, columns);
}

for (int ii = 0; ii < i; ii++) {
result.setRow(ii, getRow(ii));
Expand All @@ -1063,11 +1068,16 @@ public Matrix insertRow(int i, Vector row) {
* @return matrix with column.
*/
public Matrix insertColumn(int j, Vector column) {
if (j >= columns || j < 0) {
throw new IndexOutOfBoundsException("Illegal column number, must be 0.." + (columns - 1));
if (j > columns || j < 0) {
throw new IndexOutOfBoundsException("Illegal column number, must be 0.." + columns);
}

Matrix result = blankOfShape(rows, columns + 1);
Matrix result;
if (rows == 0) {
result = blankOfShape(column.length(), columns + 1);
} else {
result = blankOfShape(rows, columns + 1);
}

for (int jj = 0; jj < j; jj++) {
result.setColumn(jj, getColumn(jj));
Expand Down
76 changes: 76 additions & 0 deletions src/test/java/org/la4j/matrix/MatrixTest.java
Expand Up @@ -52,6 +52,82 @@ public T mz(int rows, int columns) {
return M.mz(rows, columns).to(factory);
}

@Test
public void testInsertRowInEmptyMatrix() {

Matrix a = m(a(1.0, 0.0, 0.0),
a(0.0, 1.0, 0.0),
a(0.0, 0.0, 1.0));

Vector v1 = v(1.0, 0.0, 0.0);
Vector v2 = v(0.0, 1.0, 0.0);
Vector v3 = v(0.0, 0.0, 1.0);

Matrix b = mz(0, 0);
b = b.insertRow(0, v3);
b = b.insertRow(0, v2);
b = b.insertRow(0, v1);

Assert.assertEquals(a, b);
}

@Test
public void testInsertColumnInEmptyMatrix() {

Matrix a = m(a(1.0, 0.0, 0.0),
a(0.0, 1.0, 0.0),
a(0.0, 0.0, 1.0));

Vector v1 = v(1.0, 0.0, 0.0);
Vector v2 = v(0.0, 1.0, 0.0);
Vector v3 = v(0.0, 0.0, 1.0);

Matrix b = mz(0, 0);
b = b.insertColumn(0, v3);
b = b.insertColumn(0, v2);
b = b.insertColumn(0, v1);

Assert.assertEquals(a, b);
}

@Test
public void testInsertRowAtTheEnd() {

Matrix a = m(a(1.0, 0.0, 0.0),
a(0.0, 1.0, 0.0),
a(0.0, 0.0, 1.0));

Vector v1 = v(1.0, 0.0, 0.0);
Vector v2 = v(0.0, 1.0, 0.0);
Vector v3 = v(0.0, 0.0, 1.0);

Matrix b = mz(0, 0);
b = b.insertRow(0, v1);
b = b.insertRow(1, v2);
b = b.insertRow(2, v3);

Assert.assertEquals(a, b);
}

@Test
public void testInsertColumnAtTheEnd() {

Matrix a = m(a(1.0, 0.0, 0.0),
a(0.0, 1.0, 0.0),
a(0.0, 0.0, 1.0));

Vector v1 = v(1.0, 0.0, 0.0);
Vector v2 = v(0.0, 1.0, 0.0);
Vector v3 = v(0.0, 0.0, 1.0);

Matrix b = mz(0, 0);
b = b.insertColumn(0, v1);
b = b.insertColumn(1, v2);
b = b.insertColumn(2, v3);

Assert.assertEquals(a, b);
}

@Test
public void testInsert_3x3_into_3x3() {
Matrix a = m(a(1.0, 2.0, 3.0),
Expand Down

0 comments on commit 49a62a8

Please sign in to comment.