9.3 Identity Matrix Class IdentityMatrix is a subclass of SquareMatrix . It initializes the matrix by placing 1.0 values along the diagonal. See Listing 9-0d. It also has a static method convert() that resets the values of a general square matrix to make it an identity matrix. Listing 9-0d The IdentityMatrix class.package numbercruncher.matrix; public class IdentityMatrix extends SquareMatrix { /** * Constructor. * @param n the number of rows == the number of columns */ public IdentityMatrix(int n) { super(n); for (int i = 0; i < n; ++i) values[i][i] = 1; } /** * Convert a square matrix into an identity matrix. * @param sm the square matrix to convert */ public static void convert(SquareMatrix sm) { for (int r = 0; r < sm.nRows; ++r) { for (int c = 0; c < sm.nCols; ++c) { sm.values[r][c] = (r == c) ? 1 : 0; } } } } |
Top |