You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2008/12/07 17:53:06 UTC

svn commit: r724158 - in /commons/proper/math/trunk/src: java/org/apache/commons/math/ java/org/apache/commons/math/linear/ site/xdoc/ test/org/apache/commons/math/linear/

Author: luc
Date: Sun Dec  7 08:53:05 2008
New Revision: 724158

URL: http://svn.apache.org/viewvc?rev=724158&view=rev
Log:
added setter methods for rows and columns in matrices
JIRA: MATH-234

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/MessagesResources_fr.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractRealMatrix.java
    commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrix.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/MessagesResources_fr.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/MessagesResources_fr.java?rev=724158&r1=724157&r2=724158&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/MessagesResources_fr.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/MessagesResources_fr.java Sun Dec  7 08:53:05 2008
@@ -194,6 +194,8 @@
       "nombre de colonnes invalide : {0} (doit \u00eatre positif)" },
     { "vector length mismatch: got {0} but expected {1}",
       "taille de vecteur invalide : {0} au lieu de {1} attendue" },
+    { "dimensions mismatch: got {0}x{1} but expected {2}x{3}",
+      "dimensions incoh\u00e9rentes : {0}x{1} \u00e0 la place de {2}x{3}" },
 
     // org.apache.commons.math.linear.BigMatrixImpl
     // org.apache.commons.math.linear.RealMatrixImpl

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractRealMatrix.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractRealMatrix.java?rev=724158&r1=724157&r2=724158&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractRealMatrix.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractRealMatrix.java Sun Dec  7 08:53:05 2008
@@ -346,6 +346,27 @@
     }
     
     /** {@inheritDoc} */
+    public void setRowMatrix(final int row, final RealMatrix matrix)
+        throws MatrixIndexException, InvalidMatrixException {
+
+        checkRowIndex(row);
+        final int nCols = getColumnDimension();
+        if ((matrix.getRowDimension() != 1) ||
+            (matrix.getColumnDimension() != nCols)) {
+            throw new InvalidMatrixException("dimensions mismatch: got {0}x{1} but expected {2}x{3}",
+                                             new Object[] {
+                                                 matrix.getRowDimension(),
+                                                 matrix.getColumnDimension(),
+                                                 1, nCols
+                                             });
+        }
+        for (int i = 0; i < nCols; ++i) {
+            setEntry(row, i, matrix.getEntry(0, i));
+        }
+
+    }
+    
+    /** {@inheritDoc} */
     public RealMatrix getColumnMatrix(final int column)
         throws MatrixIndexException {
 
@@ -361,11 +382,26 @@
     }
 
     /** {@inheritDoc} */
-    public RealVector getColumnVector(final int column)
-        throws MatrixIndexException {
-        return new RealVectorImpl(getColumn(column), false);
-    }
+    public void setColumnMatrix(final int column, final RealMatrix matrix)
+        throws MatrixIndexException, InvalidMatrixException {
+
+        checkColumnIndex(column);
+        final int nRows = getRowDimension();
+        if ((matrix.getRowDimension() != nRows) ||
+            (matrix.getColumnDimension() != 1)) {
+            throw new InvalidMatrixException("dimensions mismatch: got {0}x{1} but expected {2}x{3}",
+                                             new Object[] {
+                                                 matrix.getRowDimension(),
+                                                 matrix.getColumnDimension(),
+                                                 nRows, 1
+                                             });
+        }
+        for (int i = 0; i < nRows; ++i) {
+            setEntry(i, column, matrix.getEntry(i, 0));
+        }
 
+    }
+    
     /** {@inheritDoc} */
     public RealVector getRowVector(final int row)
         throws MatrixIndexException {
@@ -373,6 +409,50 @@
     }
 
     /** {@inheritDoc} */
+    public void setRowVector(final int row, final RealVector vector)
+        throws MatrixIndexException, InvalidMatrixException {
+
+        checkRowIndex(row);
+        final int nCols = getColumnDimension();
+        if (vector.getDimension() != nCols) {
+            throw new InvalidMatrixException("dimensions mismatch: got {0}x{1} but expected {2}x{3}",
+                                             new Object[] {
+                                                 1, vector.getDimension(),
+                                                 1, nCols
+                                             });
+        }
+        for (int i = 0; i < nCols; ++i) {
+            setEntry(row, i, vector.getEntry(i));
+        }
+
+    }
+    
+    /** {@inheritDoc} */
+    public RealVector getColumnVector(final int column)
+        throws MatrixIndexException {
+        return new RealVectorImpl(getColumn(column), false);
+    }
+
+    /** {@inheritDoc} */
+    public void setColumnVector(final int column, final RealVector vector)
+        throws MatrixIndexException, InvalidMatrixException {
+
+        checkColumnIndex(column);
+        final int nRows = getRowDimension();
+        if (vector.getDimension() != nRows) {
+            throw new InvalidMatrixException("dimensions mismatch: got {0}x{1} but expected {2}x{3}",
+                                             new Object[] {
+                                                 vector.getDimension(), 1,
+                                                 nRows, 1
+                                             });
+        }
+        for (int i = 0; i < nRows; ++i) {
+            setEntry(i, column, vector.getEntry(i));
+        }
+
+    }
+    
+    /** {@inheritDoc} */
     public double[] getRow(final int row)
         throws MatrixIndexException {
 
@@ -388,6 +468,25 @@
     }
 
     /** {@inheritDoc} */
+    public void setRow(final int row, final double[] array)
+        throws MatrixIndexException, InvalidMatrixException {
+
+        checkRowIndex(row);
+        final int nCols = getColumnDimension();
+        if (array.length != nCols) {
+            throw new InvalidMatrixException("dimensions mismatch: got {0}x{1} but expected {2}x{3}",
+                                             new Object[] {
+                                                 1, array.length,
+                                                 1, nCols
+                                             });
+        }
+        for (int i = 0; i < nCols; ++i) {
+            setEntry(row, i, array[i]);
+        }
+
+    }
+    
+    /** {@inheritDoc} */
     public double[] getColumn(final int column)
         throws MatrixIndexException {
 
@@ -403,6 +502,25 @@
     }
 
     /** {@inheritDoc} */
+    public void setColumn(final int column, final double[] array)
+        throws MatrixIndexException, InvalidMatrixException {
+
+        checkColumnIndex(column);
+        final int nRows = getRowDimension();
+        if (array.length != nRows) {
+            throw new InvalidMatrixException("dimensions mismatch: got {0}x{1} but expected {2}x{3}",
+                                             new Object[] {
+                                                 array.length, 1,
+                                                 nRows, 1
+                                             });
+        }
+        for (int i = 0; i < nRows; ++i) {
+            setEntry(i, column, array[i]);
+        }
+
+    }
+    
+    /** {@inheritDoc} */
     public abstract double getEntry(int row, int column)
         throws MatrixIndexException;
 

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrix.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrix.java?rev=724158&r1=724157&r2=724158&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrix.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/linear/RealMatrix.java Sun Dec  7 08:53:05 2008
@@ -190,6 +190,20 @@
    RealMatrix getRowMatrix(int row) throws MatrixIndexException;
    
    /**
+    * Sets the entries in row number <code>row</code>
+    * as a row matrix.  Row indices start at 0.
+    *
+    * @param row the row to be set
+    * @param matrix row matrix (must have one row and the same number of columns
+    * as the instance)
+    * @throws MatrixIndexException if the specified row index is invalid
+    * @throws InvalidMatrixException if the matrix dimensions do not match one
+    * instance row
+    */
+   void setRowMatrix(int row, RealMatrix matrix)
+       throws MatrixIndexException, InvalidMatrixException;
+   
+   /**
     * Returns the entries in column number <code>column</code>
     * as a column matrix.  Column indices start at 0.
     *
@@ -200,6 +214,20 @@
    RealMatrix getColumnMatrix(int column) throws MatrixIndexException;
 
    /**
+    * Sets the entries in column number <code>column</code>
+    * as a column matrix.  Column indices start at 0.
+    *
+    * @param column the column to be set
+    * @param matrix column matrix (must have one column and the same number of rows
+    * as the instance)
+    * @throws MatrixIndexException if the specified column index is invalid
+    * @throws InvalidMatrixException if the matrix dimensions do not match one
+    * instance column
+    */
+   void setColumnMatrix(int column, RealMatrix matrix)
+       throws MatrixIndexException, InvalidMatrixException;
+   
+   /**
     * Returns the entries in row number <code>row</code>
     * as a vector.  Row indices start at 0.
     *
@@ -208,6 +236,20 @@
     * @throws MatrixIndexException if the specified row index is invalid
     */
    RealVector getRowVector(int row) throws MatrixIndexException;
+
+   /**
+    * Sets the entries in row number <code>row</code>
+    * as a row matrix.  Row indices start at 0.
+    *
+    * @param row the row to be set
+    * @param vector row vector (must have the same number of columns
+    * as the instance)
+    * @throws MatrixIndexException if the specified row index is invalid
+    * @throws InvalidMatrixException if the vector dimension does not match one
+    * instance row
+    */
+   void setRowVector(int row, RealVector vector)
+       throws MatrixIndexException, InvalidMatrixException;
    
    /**
     * Returns the entries in column number <code>column</code>
@@ -218,7 +260,20 @@
     * @throws MatrixIndexException if the specified column index is invalid
     */
    RealVector getColumnVector(int column) throws MatrixIndexException;
-    
+
+   /**
+    * Sets the entries in column number <code>column</code>
+    * as a column matrix.  Column indices start at 0.
+    *
+    * @param column the column to be set
+    * @param vector column vector (must have the same number of rows as the instance)
+    * @throws MatrixIndexException if the specified column index is invalid
+    * @throws InvalidMatrixException if the vector dimension does not match one
+    * instance column
+    */
+   void setColumnVector(int column, RealVector vector)
+       throws MatrixIndexException, InvalidMatrixException;
+   
     /**
      * Returns the entries in row number <code>row</code> as an array.
      * <p>
@@ -232,6 +287,19 @@
     double[] getRow(int row) throws MatrixIndexException;
 
     /**
+     * Sets the entries in row number <code>row</code>
+     * as a row matrix.  Row indices start at 0.
+     *
+     * @param row the row to be set
+     * @param array row matrix (must have the same number of columns as the instance)
+     * @throws MatrixIndexException if the specified row index is invalid
+     * @throws InvalidMatrixException if the array size does not match one
+     * instance row
+     */
+    void setRow(int row, double[] array)
+        throws MatrixIndexException, InvalidMatrixException;
+    
+    /**
      * Returns the entries in column number <code>col</code> as an array.
      * <p>
      * Column indices start at 0.  A <code>MatrixIndexException</code> is thrown
@@ -244,6 +312,19 @@
     double[] getColumn(int column) throws MatrixIndexException;
 
     /**
+     * Sets the entries in column number <code>column</code>
+     * as a column matrix.  Column indices start at 0.
+     *
+     * @param column the column to be set
+     * @param array column array (must have the same number of rows as the instance)
+     * @throws MatrixIndexException if the specified column index is invalid
+     * @throws InvalidMatrixException if the array size does not match one
+     * instance column
+     */
+    void setColumn(int column, double[] array)
+        throws MatrixIndexException, InvalidMatrixException;
+    
+    /**
      * Returns the entry in the specified row and column.
      * <p>
      * Row and column indices start at 0 and must satisfy 

Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=724158&r1=724157&r2=724158&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Sun Dec  7 08:53:05 2008
@@ -39,6 +39,9 @@
   </properties>
   <body>
     <release version="2.0" date="TBD" description="TBD">
+      <action dev="luc" type="add" issue="MATH-234" >
+        Added setter methods for rows and columns in matrices.
+      </action>
       <action dev="luc" type="add" issue="MATH-232" >
         Added Frobenius matrix norm.
       </action>

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java?rev=724158&r1=724157&r2=724158&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java Sun Dec  7 08:53:05 2008
@@ -465,6 +465,26 @@
         }
     }
     
+    public void testSetRowMatrix() {
+        RealMatrix m = new RealMatrixImpl(subTestData);
+        RealMatrix mRow3 = new RealMatrixImpl(subRow3);
+        assertNotSame(mRow3, m.getRowMatrix(0));
+        m.setRowMatrix(0, mRow3);
+        assertEquals(mRow3, m.getRowMatrix(0));
+        try {
+            m.setRowMatrix(-1, mRow3);
+            fail("Expecting MatrixIndexException");
+        } catch (MatrixIndexException ex) {
+            // expected
+        }
+        try {
+            m.setRowMatrix(0, m);
+            fail("Expecting InvalidMatrixException");
+        } catch (InvalidMatrixException ex) {
+            // expected
+        }
+    }
+    
     public void testGetColumnMatrix() {
         RealMatrix m = new RealMatrixImpl(subTestData);
         RealMatrix mColumn1 = new RealMatrixImpl(subColumn1);
@@ -487,6 +507,26 @@
         }
     }
 
+    public void testSetColumnMatrix() {
+        RealMatrix m = new RealMatrixImpl(subTestData);
+        RealMatrix mColumn3 = new RealMatrixImpl(subColumn3);
+        assertNotSame(mColumn3, m.getColumnMatrix(1));
+        m.setColumnMatrix(1, mColumn3);
+        assertEquals(mColumn3, m.getColumnMatrix(1));
+        try {
+            m.setColumnMatrix(-1, mColumn3);
+            fail("Expecting MatrixIndexException");
+        } catch (MatrixIndexException ex) {
+            // expected
+        }
+        try {
+            m.setColumnMatrix(0, m);
+            fail("Expecting InvalidMatrixException");
+        } catch (InvalidMatrixException ex) {
+            // expected
+        }
+    }
+
     public void testGetRowVector() {
         RealMatrix m = new RealMatrixImpl(subTestData);
         RealVector mRow0 = new RealVectorImpl(subRow0[0]);
@@ -505,7 +545,27 @@
         } catch (MatrixIndexException ex) {
             // expected
         }
-   }
+    }
+
+    public void testSetRowVector() {
+        RealMatrix m = new RealMatrixImpl(subTestData);
+        RealVector mRow3 = new RealVectorImpl(subRow3[0]);
+        assertNotSame(mRow3, m.getRowMatrix(0));
+        m.setRowVector(0, mRow3);
+        assertEquals(mRow3, m.getRowVector(0));
+        try {
+            m.setRowVector(-1, mRow3);
+            fail("Expecting MatrixIndexException");
+        } catch (MatrixIndexException ex) {
+            // expected
+        }
+        try {
+            m.setRowVector(0, new RealVectorImpl(5));
+            fail("Expecting InvalidMatrixException");
+        } catch (InvalidMatrixException ex) {
+            // expected
+        }
+    }
     
     public void testGetColumnVector() {
         RealMatrix m = new RealMatrixImpl(subTestData);
@@ -527,6 +587,26 @@
         }
     }
 
+    public void testSetColumnVector() {
+        RealMatrix m = new RealMatrixImpl(subTestData);
+        RealVector mColumn3 = columnToVector(subColumn3);
+        assertNotSame(mColumn3, m.getColumnVector(1));
+        m.setColumnVector(1, mColumn3);
+        assertEquals(mColumn3, m.getColumnVector(1));
+        try {
+            m.setColumnVector(-1, mColumn3);
+            fail("Expecting MatrixIndexException");
+        } catch (MatrixIndexException ex) {
+            // expected
+        }
+        try {
+            m.setColumnVector(0, new RealVectorImpl(5));
+            fail("Expecting InvalidMatrixException");
+        } catch (InvalidMatrixException ex) {
+            // expected
+        }
+    }
+
     private RealVector columnToVector(double[][] column) {
         double[] data = new double[column.length];
         for (int i = 0; i < data.length; ++i) {
@@ -534,6 +614,98 @@
         }
         return new RealVectorImpl(data, false);
     }
+
+    public void testGetRow() {
+        RealMatrix m = new RealMatrixImpl(subTestData);
+        checkArrays(subRow0[0], m.getRow(0));
+        checkArrays(subRow3[0], m.getRow(3));
+        try {
+            m.getRow(-1);
+            fail("Expecting MatrixIndexException");
+        } catch (MatrixIndexException ex) {
+            // expected
+        }
+        try {
+            m.getRow(4);
+            fail("Expecting MatrixIndexException");
+        } catch (MatrixIndexException ex) {
+            // expected
+        }
+    }
+
+    public void testSetRow() {
+        RealMatrix m = new RealMatrixImpl(subTestData);
+        assertTrue(subRow3[0][0] != m.getRow(0)[0]);
+        m.setRow(0, subRow3[0]);
+        checkArrays(subRow3[0], m.getRow(0));
+        try {
+            m.setRow(-1, subRow3[0]);
+            fail("Expecting MatrixIndexException");
+        } catch (MatrixIndexException ex) {
+            // expected
+        }
+        try {
+            m.setRow(0, new double[5]);
+            fail("Expecting InvalidMatrixException");
+        } catch (InvalidMatrixException ex) {
+            // expected
+        }
+    }
+    
+    public void testGetColumn() {
+        RealMatrix m = new RealMatrixImpl(subTestData);
+        double[] mColumn1 = columnToArray(subColumn1);
+        double[] mColumn3 = columnToArray(subColumn3);
+        checkArrays(mColumn1, m.getColumn(1));
+        checkArrays(mColumn3, m.getColumn(3));
+        try {
+            m.getColumn(-1);
+            fail("Expecting MatrixIndexException");
+        } catch (MatrixIndexException ex) {
+            // expected
+        }
+        try {
+            m.getColumn(4);
+            fail("Expecting MatrixIndexException");
+        } catch (MatrixIndexException ex) {
+            // expected
+        }
+    }
+
+    public void testSetColumn() {
+        RealMatrix m = new RealMatrixImpl(subTestData);
+        double[] mColumn3 = columnToArray(subColumn3);
+        assertTrue(mColumn3[0] != m.getColumn(1)[0]);
+        m.setColumn(1, mColumn3);
+        checkArrays(mColumn3, m.getColumn(1));
+        try {
+            m.setColumn(-1, mColumn3);
+            fail("Expecting MatrixIndexException");
+        } catch (MatrixIndexException ex) {
+            // expected
+        }
+        try {
+            m.setColumn(0, new double[5]);
+            fail("Expecting InvalidMatrixException");
+        } catch (InvalidMatrixException ex) {
+            // expected
+        }
+    }
+
+    private double[] columnToArray(double[][] column) {
+        double[] data = new double[column.length];
+        for (int i = 0; i < data.length; ++i) {
+            data[i] = column[i][0];
+        }
+        return data;
+    }
+
+    private void checkArrays(double[] expected, double[] actual) {
+        assertEquals(expected.length, actual.length);
+        for (int i = 0; i < expected.length; ++i) {
+            assertEquals(expected[i], actual[i]);            
+        }
+    }
     
     public void testEqualsAndHashCode() {
         RealMatrixImpl m = new RealMatrixImpl(testData);