mahout-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ka...@apache.org
Subject svn commit: r647178 - in /lucene/mahout/trunk/src: main/java/org/apache/mahout/matrix/AbstractMatrix.java main/java/org/apache/mahout/matrix/Matrix.java test/java/org/apache/mahout/matrix/MatrixTest.java
Date Fri, 11 Apr 2008 14:15:54 GMT
Author: kalle
Date: Fri Apr 11 07:15:51 2008
New Revision: 647178

URL: http://svn.apache.org/viewvc?rev=647178&view=rev
Log:
MAHOUT-26, Laplace

Modified:
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Matrix.java
    lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/MatrixTest.java

Modified: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java?rev=647178&r1=647177&r2=647178&view=diff
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java (original)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java Fri Apr
11 07:15:51 2008
@@ -200,6 +200,45 @@
 
   /*
    * (non-Javadoc)
+   *
+   * @see org.apache.mahout.matrix.Matrix#determinant()
+   */
+   public double determinant() throws CardinalityException {
+   int card[] = cardinality();
+   int rowSize = card[ROW];
+   int columnSize = card[COL];
+   if(rowSize!=columnSize) throw new CardinalityException();
+
+   if(rowSize==2)
+       return getQuick(0,0)*getQuick(1,1)-getQuick(0,1) * getQuick(1,0);
+   else {
+       int sign = 1;
+       double ret = 0;
+
+       for(int i = 0; i<columnSize; i++){
+               Matrix minor = new DenseMatrix(rowSize-1,columnSize-1);
+               for(int j = 1; j<rowSize; j++){
+                   boolean flag = false; /* column offset flag */
+                   for(int k = 0; k<columnSize; k++){
+                       if(k==i) {
+                           flag = true;
+                           continue;
+                       }
+                       minor.set(j-1,flag ? k-1 : k, getQuick(j,k));
+                   }
+               }
+               ret += getQuick(0,i)*sign*minor.determinant();
+               sign*=-1;
+
+           }
+
+           return ret;
+       }
+
+   }
+
+  /*
+   * (non-Javadoc)
    * 
    * @see org.apache.mahout.matrix.Matrix#divide(double)
    */
@@ -284,6 +323,15 @@
     if (row < 0 || column < 0 || row >= c[ROW] || column >= c[COL])
       throw new IndexException();
     setQuick(row, column, value);
+  }
+
+  public void set(int row, double[] data) throws IndexException,CardinalityException {
+    int[] c = cardinality();
+      if(c[COL] < data.length) throw new CardinalityException();
+      if( (c[ROW] < row) || (row < 0) ) throw new IndexException();
+
+    for(int i = 0; i<c[COL]; i++)
+        setQuick(row,i,data[i]);
   }
 
   /*

Modified: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Matrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Matrix.java?rev=647178&r1=647177&r2=647178&view=diff
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Matrix.java (original)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Matrix.java Fri Apr 11 07:15:51
2008
@@ -114,6 +114,13 @@
    */
   Matrix copy();
 
+    /**
+     * Returns matrix determinator using Laplace theorem
+     *
+     * @return a matrix determinator
+     */
+  double determinant();
+
   /**
    * Return a new matrix containing the values of the recipient divided by the
    * argument
@@ -223,6 +230,8 @@
    * @throws IndexException if the index is out of bounds
    */
   void set(int row, int column, double value) throws IndexException;
+
+  void set(int row, double[] data) throws IndexException,CardinalityException; 
 
   /**
    * Set the value at the given index, without checking bounds

Modified: lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/MatrixTest.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/MatrixTest.java?rev=647178&r1=647177&r2=647178&view=diff
==============================================================================
--- lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/MatrixTest.java (original)
+++ lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/MatrixTest.java Fri Apr 11
07:15:51 2008
@@ -472,4 +472,10 @@
       assertTrue(true);
     }
   }
+
+  public void testDetermitant() {
+    Matrix m = matrixFactory(new double[][] { {1,3,4},{5,2,3},{1,4,2} });
+    assertEquals("determinant", 43.0, m.determinant());
+  }
+
 }



Mime
View raw message