Added matrix transpose
This commit is contained in:
parent
41a70d45b8
commit
acc3ccfe1f
2 changed files with 24 additions and 0 deletions
|
|
@ -189,6 +189,20 @@ public class MatrixMath {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return a new matrix with the transpose of the input matrix.
|
||||||
|
*/
|
||||||
|
public static double[][] transpose(double[][] matrix){
|
||||||
|
double[][] result = new double[matrix[0].length][matrix.length];
|
||||||
|
|
||||||
|
for (int y=0; y < result.length; ++y) {
|
||||||
|
for (int x=0; x < result[y].length; ++x){
|
||||||
|
result[y][x] = matrix[x][y];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private static void matrixPreCheck(double[][] matrix1, double[][] matrix2) {
|
private static void matrixPreCheck(double[][] matrix1, double[][] matrix2) {
|
||||||
if (matrix1[0].length != matrix2.length)
|
if (matrix1[0].length != matrix2.length)
|
||||||
|
|
@ -212,4 +226,5 @@ public class MatrixMath {
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -79,6 +79,15 @@ public class MatrixMathTest {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void matrixTranspose(){
|
||||||
|
assertArrayEquals(
|
||||||
|
new double[][]{{1,3},{2,5},{0,9}},
|
||||||
|
MatrixMath.transpose(
|
||||||
|
new double[][]{{1,2,0},{3,5,9}})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue