9.4 Row Vector

   

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

Table of Contents
Chapter  9.   Basic Matrix Operations

9.4 Row Vector

The RowVector class, shown in Listing 9-0e, is a subclass of Matrix . In many respects, RowVector is similar to SquareMatrix . It overrides the add() and subtract() methods to take RowVector arguments and return RowVector values. (Recall that multiplying a row vector by a matrix was taken care of in the Matrix class.)

The norm() method computes and returns the Euclidean norm of a row vector. The norm of a vector v is written graphics/09inl01.gif . This scalar value is the square root of the sum of the squares of the vector's values. We can think of a vector norm as the "absolute value" of a vector, or its "distance" from the origin in vector space.

Listing 9-0e The RowVector class.
 package numbercruncher.matrix; public class RowVector extends Matrix {     //--------------//     // Constructors //     //--------------//     /**      * Constructor.      * @param n the number of elements      */     public RowVector(int n) { super(1, n); }     /**      * Constructor.      * @param values the array of values      */     public RowVector(float values[]) { set(values); }     /**      * Constructor.      * @param m the matrix (only the first row used)      */     private RowVector(Matrix m) { set(m); }     //---------//     // Getters //     //---------//     /**      * Return the row vector's size.      */     public int size() { return nCols; }     /**      * Copy the values of this matrix.      * @return the copied values      */     public float[] copyValues1D()     {         float v[] = new float[nCols];         for (int c = 0; c < nCols; ++c) {             v[c] = values[0][c];         }         return v;     }     /**      * Return the i'th value of the vector.      * @param i the index      * @return the value      */     public float at(int i) { return values[0][i]; }     //---------//     // Setters //     //---------//     /**      * Set this row vector from a matrix. Only the first row is used.      * @param m the matrix      */     private void set(Matrix m)     {         this.nRows  = 1;         this.nCols  = m.nCols;         this.values = m.values;     }     /**      * Set this row vector from an array of values.      * @param values the array of values      */     protected void set(float values[])     {         this.nRows  = 1;         this.nCols  = values.length;         this.values = new float[1][];         this.values[0] = values;     }     /**      * Set the i'th value of the vector.      * @param i the index      * @param value the value      */     public void set(int i, float value) { values[0][i] = value; }     //-------------------//     // Vector operations //     //-------------------//     /**      * Add another row vector to this row vector.      * @param rv the other row vector      * @return the sum row vector      * @throws numbercruncher.MatrixException for invalid size      */     public RowVector add(RowVector rv) throws MatrixException     {         return new RowVector(super.add(rv));     }     /**      * Subtract another row vector from this row vector.      * @param rv the other row vector      * @return the sum row vector      * @throws numbercruncher.MatrixException for invalid size      */     public RowVector subtract(RowVector rv) throws MatrixException     {         return new RowVector(super.subtract(rv));     }     /**      * Compute the Euclidean norm.      * @return the norm      */     public float norm()     {         double t = 0;         for (int c = 0; c < nCols; ++c) {             float v = values[0][c];             t += v*v;         }         return (float) Math.sqrt(t);     }     /**      * Print the vector values.      */     public void print()     {         for (int c = 0; c < nCols; ++c) {             System.out.print( "   " + values[0][c]);         }         System.out.println();     } } 

   
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