9.3 Identity Matrix

   

 
Java Number Cruncher: The Java Programmer's Guide to Numerical Computing
By Ronald  Mak

Table of Contents
Chapter  9.   Basic Matrix Operations

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
 


Java Number Cruncher. The Java Programmer's Guide to Numerical Computing
Java Number Cruncher: The Java Programmers Guide to Numerical Computing
ISBN: 0130460419
EAN: 2147483647
Year: 2001
Pages: 141
Authors: Ronald Mak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net