Added identity matrix
This commit is contained in:
parent
9562d8615a
commit
41a70d45b8
2 changed files with 27 additions and 0 deletions
|
|
@ -196,4 +196,20 @@ public class MatrixMath {
|
||||||
"matrix1 " + matrix1.length + "x" + matrix1[0].length + ", " +
|
"matrix1 " + matrix1.length + "x" + matrix1[0].length + ", " +
|
||||||
"matrix2 " + matrix2.length + "x" + matrix2[0].length + ", ");
|
"matrix2 " + matrix2.length + "x" + matrix2[0].length + ", ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/***********************************************************************
|
||||||
|
* Util Methods
|
||||||
|
**********************************************************************/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return a identity matrix (n x n) where the diagonal elements have the value 1
|
||||||
|
*/
|
||||||
|
public static double[][] identity(int n){
|
||||||
|
double[][] result = new double[n][n];
|
||||||
|
|
||||||
|
for (int i=0; i < n; ++i) {
|
||||||
|
result[i][i] = 1;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -80,4 +80,15 @@ public class MatrixMathTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void identity(){
|
||||||
|
assertArrayEquals(
|
||||||
|
new double[][]{{1}},
|
||||||
|
MatrixMath.identity(1));
|
||||||
|
|
||||||
|
assertArrayEquals(
|
||||||
|
new double[][]{{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}},
|
||||||
|
MatrixMath.identity(4));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue