9.2 Square Matrix

   

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

Table of Contents
Chapter  9.   Basic Matrix Operations

9.2 Square Matrix

Listing 9-0c shows class SquareMatrix , a subclass of Matrix . This subclass overrides the add() , subtract() , and multiply() methods to each take a SquareMatrix argument and return a SquareMatrix value.

The overriding operations methods use the private SquareMatrix(Matrix m) constructor, which in turn invokes the private void set(Matrix m) method. These methods "convert" a general matrix into a square matrix by having the square matrix reference the general matrix's values. The SquareMatrix class does this safely because, in each case, the general matrix is only an intermediate value generated during a matrix operation.

Listing 9-0c The SquareMatrix class.
 package numbercruncher.matrix; /**  * A square matrix.  */ public class SquareMatrix extends Matrix {     //--------------//     // Constructors //     //--------------//     /**      * Constructor.      * @param n the number of rows == the number of columns      */     public SquareMatrix(int n) { super(n, n); }     /**      * Constructor.      * @param m the matrix (only the upper left square used)      */     private SquareMatrix(Matrix m) { set(m); }     /**      * Constructor.      * @param values the array of values      */     public SquareMatrix(float values[][]) { set(values); }     //---------//     // Setters //     //---------//     /**      * Set this square matrix from another matrix.  Note that this      * matrix will reference the values of the argument matrix.  If      * the values are not square, only the upper left square is used.      * @param values the 2-d array of values      */     private void set(Matrix m)     {         this.nRows  = this.nCols = Math.min(m.nRows, m.nCols);         this.values = m.values;     }     /**      * Set this square matrix from a 2-d array of values.  If the      * values are not square, only the upper left square is used.      * @param values the 2-d array of values      */     protected void set(float values[][])     {         super.set(values);         nRows = nCols = Math.min(nRows, nCols);     }     //-------------------//     // Matrix operations //     //-------------------//     /**      * Add another square matrix to this matrix.      * @param sm the square matrix addend      * @return the sum matrix      * @throws numbercruncher.MatrixException for invalid size      */     public SquareMatrix add(SquareMatrix sm) throws MatrixException     {         return new SquareMatrix(super.add(sm));     }     /**      * Subtract another square matrix from this matrix.      * @param sm the square matrix subrrahend      * @return the difference matrix      * @throws numbercruncher.MatrixException for invalid size      */     public SquareMatrix subtract(SquareMatrix sm)         throws MatrixException     {         return new SquareMatrix(super.subtract(sm));     }     /**      * Multiply this square matrix by another square matrix.      * @param sm the square matrix multiplier      * @return the product matrix      * @throws numbercruncher.MatrixException for invalid size      */     public SquareMatrix multiply(SquareMatrix sm)         throws MatrixException     {         return new SquareMatrix(super.multiply(sm));     } } 

   
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