hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tjungb...@apache.org
Subject svn commit: r1361734 [1/2] - in /hama/trunk/ml/src/main/java/org/apache/hama/ml: math/ writable/
Date Sun, 15 Jul 2012 17:08:37 GMT
Author: tjungblut
Date: Sun Jul 15 17:08:36 2012
New Revision: 1361734

URL: http://svn.apache.org/viewvc?rev=1361734&view=rev
Log:
Cleanup of my math lib in the ml package

Removed:
    hama/trunk/ml/src/main/java/org/apache/hama/ml/math/BooleanMatrix.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/math/BooleanVector.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseBooleanMatrix.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseBooleanVector.java
Modified:
    hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseDoubleMatrix.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseDoubleVector.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleDoubleVectorFunction.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleMatrix.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVector.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVectorFunction.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/math/Tuple.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/writable/MatrixWritable.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/writable/VectorWritable.java

Modified: hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseDoubleMatrix.java
URL: http://svn.apache.org/viewvc/hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseDoubleMatrix.java?rev=1361734&r1=1361733&r2=1361734&view=diff
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseDoubleMatrix.java (original)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseDoubleMatrix.java Sun Jul 15 17:08:36 2012
@@ -27,790 +27,755 @@ import java.util.Random;
  */
 public final class DenseDoubleMatrix implements DoubleMatrix {
 
-    protected final double[][] matrix;
-    protected final int numRows;
-    protected final int numColumns;
-
-    /**
-     * Creates a new empty matrix from the rows and columns.
-     *
-     * @param rows the num of rows.
-     * @param columns the num of columns.
-     */
-    public DenseDoubleMatrix(int rows, int columns) {
-        this.numRows = rows;
-        this.numColumns = columns;
-        this.matrix = new double[rows][columns];
-    }
-
-    /**
-     * Creates a new empty matrix from the rows and columns filled with the given
-     * default value.
-     *
-     * @param rows the num of rows.
-     * @param columns the num of columns.
-     * @param defaultValue the default value.
-     */
-    public DenseDoubleMatrix(int rows, int columns, double defaultValue) {
-        this.numRows = rows;
-        this.numColumns = columns;
-        this.matrix = new double[rows][columns];
+  protected final double[][] matrix;
+  protected final int numRows;
+  protected final int numColumns;
+
+  /**
+   * Creates a new empty matrix from the rows and columns.
+   * 
+   * @param rows the num of rows.
+   * @param columns the num of columns.
+   */
+  public DenseDoubleMatrix(int rows, int columns) {
+    this.numRows = rows;
+    this.numColumns = columns;
+    this.matrix = new double[rows][columns];
+  }
+
+  /**
+   * Creates a new empty matrix from the rows and columns filled with the given
+   * default value.
+   * 
+   * @param rows the num of rows.
+   * @param columns the num of columns.
+   * @param defaultValue the default value.
+   */
+  public DenseDoubleMatrix(int rows, int columns, double defaultValue) {
+    this.numRows = rows;
+    this.numColumns = columns;
+    this.matrix = new double[rows][columns];
+
+    for (int i = 0; i < numRows; i++) {
+      Arrays.fill(matrix[i], defaultValue);
+    }
+  }
+
+  /**
+   * Creates a new empty matrix from the rows and columns filled with the given
+   * random values.
+   * 
+   * @param rows the num of rows.
+   * @param columns the num of columns.
+   * @param rand the random instance to use.
+   */
+  public DenseDoubleMatrix(int rows, int columns, Random rand) {
+    this.numRows = rows;
+    this.numColumns = columns;
+    this.matrix = new double[rows][columns];
+
+    for (int i = 0; i < numRows; i++) {
+      for (int j = 0; j < numColumns; j++) {
+        matrix[i][j] = rand.nextDouble();
+      }
+    }
+  }
+
+  /**
+   * Simple copy constructor, but does only bend the reference to this instance.
+   * 
+   * @param otherMatrix the other matrix.
+   */
+  public DenseDoubleMatrix(double[][] otherMatrix) {
+    this.matrix = otherMatrix;
+    this.numRows = otherMatrix.length;
+    if (matrix.length > 0)
+      this.numColumns = matrix[0].length;
+    else
+      this.numColumns = numRows;
+  }
+
+  /**
+   * Generates a matrix out of an vector array. it treats the array entries as
+   * rows and the vector itself contains the values of the columns.
+   * 
+   * @param vectorArray the array of vectors.
+   */
+  public DenseDoubleMatrix(DoubleVector[] vectorArray) {
+    this.matrix = new double[vectorArray.length][];
+    this.numRows = vectorArray.length;
+
+    for (int i = 0; i < vectorArray.length; i++) {
+      this.setRowVector(i, vectorArray[i]);
+    }
+
+    if (matrix.length > 0)
+      this.numColumns = matrix[0].length;
+    else
+      this.numColumns = numRows;
+  }
+
+  /**
+   * Sets the first column of this matrix to the given vector.
+   * 
+   * @param first the new first column of the given vector
+   */
+  public DenseDoubleMatrix(DenseDoubleVector first) {
+    this(first.getLength(), 1);
+    setColumn(0, first.toArray());
+  }
+
+  /**
+   * Copies the given double array v into the first row of this matrix, and
+   * creates this with the number of given rows and columns.
+   * 
+   * @param v the values to put into the first row.
+   * @param rows the number of rows.
+   * @param columns the number of columns.
+   */
+  public DenseDoubleMatrix(double[] v, int rows, int columns) {
+    this.matrix = new double[rows][columns];
+
+    for (int i = 0; i < rows; i++) {
+      System.arraycopy(v, i * columns, this.matrix[i], 0, columns);
+    }
+
+    int index = 0;
+    for (int col = 0; col < columns; col++) {
+      for (int row = 0; row < rows; row++) {
+        matrix[row][col] = v[index++];
+      }
+    }
+
+    this.numRows = rows;
+    this.numColumns = columns;
+  }
+
+  /**
+   * Creates a new matrix with the given vector into the first column and the
+   * other matrix to the other columns.
+   * 
+   * @param first the new first column.
+   * @param otherMatrix the other matrix to set on from the second column.
+   */
+  public DenseDoubleMatrix(DenseDoubleVector first, DoubleMatrix otherMatrix) {
+    this(otherMatrix.getRowCount(), otherMatrix.getColumnCount() + 1);
+    setColumn(0, first.toArray());
+    for (int col = 1; col < otherMatrix.getColumnCount() + 1; col++)
+      setColumnVector(col, otherMatrix.getColumnVector(col - 1));
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#get(int, int)
+   */
+  @Override
+  public final double get(int row, int col) {
+    return this.matrix[row][col];
+  }
+
+  /**
+   * Gets a whole column of the matrix as a double array.
+   */
+  public final double[] getColumn(int col) {
+    final double[] column = new double[numRows];
+    for (int r = 0; r < numRows; r++) {
+      column[r] = matrix[r][col];
+    }
+    return column;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#getColumnCount()
+   */
+  @Override
+  public final int getColumnCount() {
+    return numColumns;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#getColumnVector(int)
+   */
+  @Override
+  public final DoubleVector getColumnVector(int col) {
+    return new DenseDoubleVector(getColumn(col));
+  }
+
+  /**
+   * Get the matrix as 2-dimensional double array (first dimension is the row,
+   * second the column) to faster access the values.
+   */
+  public final double[][] getValues() {
+    return matrix;
+  }
+
+  /**
+   * Get a single row of the matrix as a double array.
+   */
+  public final double[] getRow(int row) {
+    return matrix[row];
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#getRowCount()
+   */
+  @Override
+  public final int getRowCount() {
+    return numRows;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#getRowVector(int)
+   */
+  @Override
+  public final DoubleVector getRowVector(int row) {
+    return new DenseDoubleVector(getRow(row));
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#set(int, int, double)
+   */
+  @Override
+  public final void set(int row, int col, double value) {
+    this.matrix[row][col] = value;
+  }
+
+  /**
+   * Sets the row to a given double array. This does not copy, rather than just
+   * bends the references.
+   */
+  public final void setRow(int row, double[] value) {
+    this.matrix[row] = value;
+  }
+
+  /**
+   * Sets the column to a given double array. This does not copy, rather than
+   * just bends the references.
+   */
+  public final void setColumn(int col, double[] values) {
+    for (int i = 0; i < getRowCount(); i++) {
+      this.matrix[i][col] = values[i];
+    }
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#setColumnVector(int,
+   * de.jungblut.math.DoubleVector)
+   */
+  @Override
+  public void setColumnVector(int col, DoubleVector column) {
+    this.setColumn(col, column.toArray());
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#setRowVector(int,
+   * de.jungblut.math.DoubleVector)
+   */
+  @Override
+  public void setRowVector(int rowIndex, DoubleVector row) {
+    this.setRow(rowIndex, row.toArray());
+  }
+
+  /**
+   * Returns the size of the matrix as string (ROWSxCOLUMNS).
+   */
+  public String sizeToString() {
+    return numRows + "x" + numColumns;
+  }
+
+  /**
+   * Splits the last column from this matrix. Usually used to get a prediction
+   * column from some machine learning problem.
+   * 
+   * @return a tuple of a new sliced matrix and a vector which was the last
+   *         column.
+   */
+  public final Tuple<DenseDoubleMatrix, DenseDoubleVector> splitLastColumn() {
+    DenseDoubleMatrix m = new DenseDoubleMatrix(getRowCount(),
+        getColumnCount() - 1);
+    for (int i = 0; i < getRowCount(); i++) {
+      for (int j = 0; j < getColumnCount() - 1; j++) {
+        m.set(i, j, get(i, j));
+      }
+    }
+    DenseDoubleVector v = new DenseDoubleVector(getColumn(getColumnCount() - 1));
+    return new Tuple<DenseDoubleMatrix, DenseDoubleVector>(m, v);
+  }
+
+  /**
+   * Creates two matrices out of this by the given percentage. It uses a random
+   * function to determine which rows should belong to the matrix including the
+   * given percentage amount of rows.
+   * 
+   * @param percentage A float value between 0.0f and 1.0f
+   * @return A tuple which includes two matrices, the first contains the
+   *         percentage of the rows from the original matrix (rows are chosen
+   *         randomly) and the second one contains all other rows.
+   */
+  public final Tuple<DenseDoubleMatrix, DenseDoubleMatrix> splitRandomMatrices(
+      float percentage) {
+    if (percentage < 0.0f || percentage > 1.0f) {
+      throw new IllegalArgumentException(
+          "Percentage must be between 0.0 and 1.0! Given " + percentage);
+    }
+
+    if (percentage == 1.0f) {
+      return new Tuple<DenseDoubleMatrix, DenseDoubleMatrix>(this, null);
+    } else if (percentage == 0.0f) {
+      return new Tuple<DenseDoubleMatrix, DenseDoubleMatrix>(null, this);
+    }
+
+    final Random rand = new Random(System.nanoTime());
+    int firstMatrixRowsCount = Math.round(percentage * numRows);
+
+    // we first choose needed rows number of items to pick
+    final HashSet<Integer> lowerMatrixRowIndices = new HashSet<Integer>();
+    int missingRows = firstMatrixRowsCount;
+    while (missingRows > 0) {
+      final int nextIndex = rand.nextInt(numRows);
+      if (lowerMatrixRowIndices.add(nextIndex)) {
+        missingRows--;
+      }
+    }
+
+    // make to new matrixes
+    final double[][] firstMatrix = new double[firstMatrixRowsCount][numColumns];
+    int firstMatrixIndex = 0;
+    final double[][] secondMatrix = new double[numRows - firstMatrixRowsCount][numColumns];
+    int secondMatrixIndex = 0;
+
+    // then we loop over all items and put split the matrix
+    for (int r = 0; r < numRows; r++) {
+      if (lowerMatrixRowIndices.contains(r)) {
+        firstMatrix[firstMatrixIndex++] = matrix[r];
+      } else {
+        secondMatrix[secondMatrixIndex++] = matrix[r];
+      }
+    }
+
+    return new Tuple<DenseDoubleMatrix, DenseDoubleMatrix>(
+        new DenseDoubleMatrix(firstMatrix), new DenseDoubleMatrix(secondMatrix));
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#multiply(double)
+   */
+  @Override
+  public final DenseDoubleMatrix multiply(double scalar) {
+    DenseDoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns);
+    for (int i = 0; i < numRows; i++) {
+      for (int j = 0; j < numColumns; j++) {
+        m.set(i, j, this.matrix[i][j] * scalar);
+      }
+    }
+    return m;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#multiply(de.jungblut.math.DoubleMatrix)
+   */
+  @Override
+  public final DoubleMatrix multiply(DoubleMatrix other) {
+    DenseDoubleMatrix matrix = new DenseDoubleMatrix(this.getRowCount(),
+        other.getColumnCount());
+
+    final int m = this.numRows;
+    final int n = this.numColumns;
+    final int p = other.getColumnCount();
+
+    for (int j = p; --j >= 0;) {
+      for (int i = m; --i >= 0;) {
+        double s = 0;
+        for (int k = n; --k >= 0;) {
+          s += get(i, k) * other.get(k, j);
+        }
+        matrix.set(i, j, s + matrix.get(i, j));
+      }
+    }
+
+    return matrix;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see
+   * de.jungblut.math.DoubleMatrix#multiplyElementWise(de.jungblut.math.DoubleMatrix
+   * )
+   */
+  @Override
+  public final DoubleMatrix multiplyElementWise(DoubleMatrix other) {
+    DenseDoubleMatrix matrix = new DenseDoubleMatrix(this.numRows,
+        this.numColumns);
+
+    for (int i = 0; i < numRows; i++) {
+      for (int j = 0; j < numColumns; j++) {
+        matrix.set(i, j, this.get(i, j) * (other.get(i, j)));
+      }
+    }
+
+    return matrix;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see
+   * de.jungblut.math.DoubleMatrix#multiplyVector(de.jungblut.math.DoubleVector)
+   */
+  @Override
+  public final DoubleVector multiplyVector(DoubleVector v) {
+    DoubleVector vector = new DenseDoubleVector(this.getRowCount());
+    for (int row = 0; row < numRows; row++) {
+      double sum = 0.0d;
+      for (int col = 0; col < numColumns; col++) {
+        sum += (matrix[row][col] * v.get(col));
+      }
+      vector.set(row, sum);
+    }
+
+    return vector;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#transpose()
+   */
+  @Override
+  public DenseDoubleMatrix transpose() {
+    DenseDoubleMatrix m = new DenseDoubleMatrix(this.numColumns, this.numRows);
+    for (int i = 0; i < numRows; i++) {
+      for (int j = 0; j < numColumns; j++) {
+        m.set(j, i, this.matrix[i][j]);
+      }
+    }
+    return m;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#subtractBy(double)
+   */
+  @Override
+  public DenseDoubleMatrix subtractBy(double amount) {
+    DenseDoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns);
+    for (int i = 0; i < numRows; i++) {
+      for (int j = 0; j < numColumns; j++) {
+        m.set(i, j, amount - this.matrix[i][j]);
+      }
+    }
+    return m;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#subtract(double)
+   */
+  @Override
+  public DenseDoubleMatrix subtract(double amount) {
+    DenseDoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns);
+    for (int i = 0; i < numRows; i++) {
+      for (int j = 0; j < numColumns; j++) {
+        m.set(i, j, this.matrix[i][j] - amount);
+      }
+    }
+    return m;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#subtract(de.jungblut.math.DoubleMatrix)
+   */
+  @Override
+  public DoubleMatrix subtract(DoubleMatrix other) {
+    DoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns);
+    for (int i = 0; i < numRows; i++) {
+      for (int j = 0; j < numColumns; j++) {
+        m.set(i, j, this.matrix[i][j] - other.get(i, j));
+      }
+    }
+    return m;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#subtract(de.jungblut.math.DoubleVector)
+   */
+  @Override
+  public DenseDoubleMatrix subtract(DoubleVector vec) {
+    DenseDoubleMatrix cop = new DenseDoubleMatrix(this.getRowCount(),
+        this.getColumnCount());
+    for (int i = 0; i < this.getColumnCount(); i++) {
+      cop.setColumn(i, getColumnVector(i).subtract(vec.get(i)).toArray());
+    }
+    return cop;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#divide(de.jungblut.math.DoubleVector)
+   */
+  @Override
+  public DoubleMatrix divide(DoubleVector vec) {
+    DoubleMatrix cop = new DenseDoubleMatrix(this.getRowCount(),
+        this.getColumnCount());
+    for (int i = 0; i < this.getColumnCount(); i++) {
+      cop.setColumnVector(i, getColumnVector(i).divide(vec.get(i)));
+    }
+    return cop;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#divide(de.jungblut.math.DoubleMatrix)
+   */
+  @Override
+  public DoubleMatrix divide(DoubleMatrix other) {
+    DoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns);
+    for (int i = 0; i < numRows; i++) {
+      for (int j = 0; j < numColumns; j++) {
+        m.set(i, j, this.matrix[i][j] / other.get(i, j));
+      }
+    }
+    return m;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#divide(double)
+   */
+  @Override
+  public DoubleMatrix divide(double scalar) {
+    DoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns);
+    for (int i = 0; i < numRows; i++) {
+      for (int j = 0; j < numColumns; j++) {
+        m.set(i, j, this.matrix[i][j] / scalar);
+      }
+    }
+    return m;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#add(de.jungblut.math.DoubleMatrix)
+   */
+  @Override
+  public DoubleMatrix add(DoubleMatrix other) {
+    DoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns);
+    for (int i = 0; i < numRows; i++) {
+      for (int j = 0; j < numColumns; j++) {
+        m.set(i, j, this.matrix[i][j] + other.get(i, j));
+      }
+    }
+    return m;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#pow(int)
+   */
+  @Override
+  public DoubleMatrix pow(int x) {
+    DoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns);
+    for (int i = 0; i < numRows; i++) {
+      for (int j = 0; j < numColumns; j++) {
+        m.set(i, j, Math.pow(matrix[i][j], x));
+      }
+    }
+    return m;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#max(int)
+   */
+  @Override
+  public double max(int column) {
+    double max = Double.MIN_VALUE;
+    for (int i = 0; i < getRowCount(); i++) {
+      double d = matrix[i][column];
+      if (d > max) {
+        max = d;
+      }
+    }
+    return max;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#min(int)
+   */
+  @Override
+  public double min(int column) {
+    double min = Double.MAX_VALUE;
+    for (int i = 0; i < getRowCount(); i++) {
+      double d = matrix[i][column];
+      if (d < min) {
+        min = d;
+      }
+    }
+    return min;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#slice(int, int)
+   */
+  @Override
+  public DoubleMatrix slice(int rows, int cols) {
+    return slice(0, rows, 0, cols);
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#slice(int, int, int, int)
+   */
+  @Override
+  public DoubleMatrix slice(int rowOffset, int rowMax, int colOffset, int colMax) {
+    DenseDoubleMatrix m = new DenseDoubleMatrix(rowMax - rowOffset, colMax
+        - colOffset);
+    for (int row = rowOffset; row < rowMax; row++) {
+      for (int col = colOffset; col < colMax; col++) {
+        m.set(row - rowOffset, col - colOffset, this.get(row, col));
+      }
+    }
+    return m;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#isSparse()
+   */
+  @Override
+  public boolean isSparse() {
+    return false;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#sum()
+   */
+  @Override
+  public double sum() {
+    double x = 0.0d;
+    for (int i = 0; i < numRows; i++) {
+      for (int j = 0; j < numColumns; j++) {
+        x += Math.abs(matrix[i][j]);
+      }
+    }
+    return x;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#columnIndices()
+   */
+  @Override
+  public int[] columnIndices() {
+    int[] x = new int[getColumnCount()];
+    for (int i = 0; i < getColumnCount(); i++)
+      x[i] = i;
+    return x;
+  }
+
+  @Override
+  public int hashCode() {
+    final int prime = 31;
+    int result = 1;
+    result = prime * result + Arrays.hashCode(matrix);
+    result = prime * result + numColumns;
+    result = prime * result + numRows;
+    return result;
+  }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (this == obj)
+      return true;
+    if (obj == null)
+      return false;
+    if (getClass() != obj.getClass())
+      return false;
+    DenseDoubleMatrix other = (DenseDoubleMatrix) obj;
+    if (!Arrays.deepEquals(matrix, other.matrix))
+      return false;
+    if (numColumns != other.numColumns)
+      return false;
+    return numRows == other.numRows;
+  }
+
+  @Override
+  public String toString() {
+    if (numRows < 10) {
+      StringBuilder sb = new StringBuilder();
+      for (int i = 0; i < numRows; i++) {
+        sb.append(Arrays.toString(matrix[i]));
+        sb.append('\n');
+      }
+      return sb.toString();
+    } else {
+      return numRows + "x" + numColumns;
+    }
+  }
+
+  /**
+   * Gets the eye matrix (ones on the main diagonal) with a given dimension.
+   */
+  public static DenseDoubleMatrix eye(int dimension) {
+    DenseDoubleMatrix m = new DenseDoubleMatrix(dimension, dimension);
+
+    for (int i = 0; i < dimension; i++) {
+      m.set(i, i, 1);
+    }
+
+    return m;
+  }
+
+  /**
+   * Deep copies the given matrix into a new returned one.
+   */
+  public static DenseDoubleMatrix copy(DenseDoubleMatrix matrix) {
+    final double[][] src = matrix.getValues();
+    final double[][] dest = new double[matrix.getRowCount()][matrix
+        .getColumnCount()];
+
+    for (int i = 0; i < dest.length; i++)
+      System.arraycopy(src[i], 0, dest[i], 0, src[i].length);
+
+    return new DenseDoubleMatrix(dest);
+  }
+
+  /**
+   * Some strange function I found in octave but I don't know what it was named.
+   * It does however multiply the elements from the transposed vector and the
+   * normal vector and sets it into the according indices of a new constructed
+   * matrix.
+   */
+  public static DenseDoubleMatrix multiplyTransposedVectors(
+      DoubleVector transposed, DoubleVector normal) {
+    DenseDoubleMatrix m = new DenseDoubleMatrix(transposed.getLength(),
+        normal.getLength());
+    for (int row = 0; row < transposed.getLength(); row++) {
+      for (int col = 0; col < normal.getLength(); col++) {
+        m.set(row, col, transposed.get(row) * normal.get(col));
+      }
+    }
+
+    return m;
+  }
+
+  /**
+   * Just a absolute error function.
+   */
+  public static double error(DenseDoubleMatrix a, DenseDoubleMatrix b) {
+    return a.subtract(b).sum();
+  }
 
-        for (int i = 0; i < numRows; i++) {
-            Arrays.fill(matrix[i], defaultValue);
-        }
-    }
-
-    /**
-     * Creates a new empty matrix from the rows and columns filled with the given
-     * random values.
-     *
-     * @param rows the num of rows.
-     * @param columns the num of columns.
-     * @param rand the random instance to use.
-     */
-    public DenseDoubleMatrix(int rows, int columns, Random rand) {
-        this.numRows = rows;
-        this.numColumns = columns;
-        this.matrix = new double[rows][columns];
-
-        for (int i = 0; i < numRows; i++) {
-            for (int j = 0; j < numColumns; j++) {
-                matrix[i][j] = rand.nextDouble();
-            }
-        }
-    }
-
-    /**
-     * Simple copy constructor, but does only bend the reference to this instance.
-     *
-     * @param otherMatrix the other matrix.
-     */
-    public DenseDoubleMatrix(double[][] otherMatrix) {
-        this.matrix = otherMatrix;
-        this.numRows = otherMatrix.length;
-        if (matrix.length > 0)
-            this.numColumns = matrix[0].length;
-        else
-            this.numColumns = numRows;
-    }
-
-    /**
-     * Generates a matrix out of an vector array. it treats the array entries as
-     * rows and the vector itself contains the values of the columns.
-     *
-     * @param vectorArray the array of vectors.
-     */
-    public DenseDoubleMatrix(DoubleVector[] vectorArray) {
-        this.matrix = new double[vectorArray.length][];
-        this.numRows = vectorArray.length;
-
-        for (int i = 0; i < vectorArray.length; i++) {
-            this.setRowVector(i, vectorArray[i]);
-        }
-
-        if (matrix.length > 0)
-            this.numColumns = matrix[0].length;
-        else
-            this.numColumns = numRows;
-    }
-
-    /**
-     * Sets the first column of this matrix to the given vector.
-     *
-     * @param first the new first column of the given vector
-     */
-    public DenseDoubleMatrix(DenseDoubleVector first) {
-        this(first.getLength(), 1);
-        setColumn(0, first.toArray());
-    }
-
-    /**
-     * Copies the given double array v into the first row of this matrix, and
-     * creates this with the number of given rows and columns.
-     *
-     * @param v the values to put into the first row.
-     * @param rows the number of rows.
-     * @param columns the number of columns.
-     */
-    public DenseDoubleMatrix(double[] v, int rows, int columns) {
-        this.matrix = new double[rows][columns];
-
-        for (int i = 0; i < rows; i++) {
-            System.arraycopy(v, i * columns, this.matrix[i], 0, columns);
-        }
-
-        int index = 0;
-        for (int col = 0; col < columns; col++) {
-            for (int row = 0; row < rows; row++) {
-                matrix[row][col] = v[index++];
-            }
-        }
-
-        this.numRows = rows;
-        this.numColumns = columns;
-    }
-
-    /**
-     * Creates a new matrix with the given vector into the first column and the
-     * other matrix to the other columns.
-     *
-     * @param first the new first column.
-     * @param otherMatrix the other matrix to set on from the second column.
-     */
-    public DenseDoubleMatrix(DenseDoubleVector first, DoubleMatrix otherMatrix) {
-        this(otherMatrix.getRowCount(), otherMatrix.getColumnCount() + 1);
-        setColumn(0, first.toArray());
-        for (int col = 1; col < otherMatrix.getColumnCount() + 1; col++)
-            setColumnVector(col, otherMatrix.getColumnVector(col - 1));
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#get(int, int)
-    */
-    @Override
-    public final double get(int row, int col) {
-        return this.matrix[row][col];
-    }
-
-    /**
-     * Gets a whole column of the matrix as a double array.
-     */
-    public final double[] getColumn(int col) {
-        final double[] column = new double[numRows];
-        for (int r = 0; r < numRows; r++) {
-            column[r] = matrix[r][col];
-        }
-        return column;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#getColumnCount()
-    */
-    @Override
-    public final int getColumnCount() {
-        return numColumns;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#getColumnVector(int)
-    */
-    @Override
-    public final DoubleVector getColumnVector(int col) {
-        return new DenseDoubleVector(getColumn(col));
-    }
-
-    /**
-     * Get the matrix as 2-dimensional double array (first dimension is the row,
-     * second the column) to faster access the values.
-     */
-    public final double[][] getValues() {
-        return matrix;
-    }
-
-    /**
-     * Get a single row of the matrix as a double array.
-     */
-    public final double[] getRow(int row) {
-        return matrix[row];
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#getRowCount()
-    */
-    @Override
-    public final int getRowCount() {
-        return numRows;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#getRowVector(int)
-    */
-    @Override
-    public final DoubleVector getRowVector(int row) {
-        return new DenseDoubleVector(getRow(row));
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#set(int, int, double)
-    */
-    @Override
-    public final void set(int row, int col, double value) {
-        this.matrix[row][col] = value;
-    }
-
-    /**
-     * Sets the row to a given double array. This does not copy, rather than just
-     * bends the references.
-     */
-    public final void setRow(int row, double[] value) {
-        this.matrix[row] = value;
-    }
-
-    /**
-     * Sets the column to a given double array. This does not copy, rather than
-     * just bends the references.
-     */
-    public final void setColumn(int col, double[] values) {
-        for (int i = 0; i < getRowCount(); i++) {
-            this.matrix[i][col] = values[i];
-        }
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#setColumnVector(int,
-    * de.jungblut.math.DoubleVector)
-    */
-    @Override
-    public void setColumnVector(int col, DoubleVector column) {
-        this.setColumn(col, column.toArray());
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#setRowVector(int,
-    * de.jungblut.math.DoubleVector)
-    */
-    @Override
-    public void setRowVector(int rowIndex, DoubleVector row) {
-        this.setRow(rowIndex, row.toArray());
-    }
-
-    /**
-     * Returns the size of the matrix as string (ROWSxCOLUMNS).
-     */
-    public String sizeToString() {
-        return numRows + "x" + numColumns;
-    }
-
-    /**
-     * Splits the last column from this matrix. Usually used to get a prediction
-     * column from some machine learning problem.
-     *
-     * @return a tuple of a new sliced matrix and a vector which was the last
-     *         column.
-     */
-    public final Tuple<DenseDoubleMatrix, DenseDoubleVector> splitLastColumn() {
-        DenseDoubleMatrix m = new DenseDoubleMatrix(getRowCount(),
-                getColumnCount() - 1);
-        for (int i = 0; i < getRowCount(); i++) {
-            for (int j = 0; j < getColumnCount() - 1; j++) {
-                m.set(i, j, get(i, j));
-            }
-        }
-        DenseDoubleVector v = new DenseDoubleVector(getColumn(getColumnCount() - 1));
-        return new Tuple<DenseDoubleMatrix, DenseDoubleVector>(m, v);
-    }
-
-    /**
-     * Creates two matrices out of this by the given percentage. It uses a random
-     * function to determine which rows should belong to the matrix including the
-     * given percentage amount of rows.
-     *
-     * @param percentage A float value between 0.0f and 1.0f
-     * @return A tuple which includes two matrices, the first contains the
-     *         percentage of the rows from the original matrix (rows are chosen
-     *         randomly) and the second one contains all other rows.
-     */
-    public final Tuple<DenseDoubleMatrix, DenseDoubleMatrix> splitRandomMatrices(
-            float percentage) {
-        if (percentage < 0.0f || percentage > 1.0f) {
-            throw new IllegalArgumentException(
-                    "Percentage must be between 0.0 and 1.0! Given " + percentage);
-        }
-
-        if (percentage == 1.0f) {
-            return new Tuple<DenseDoubleMatrix, DenseDoubleMatrix>(this, null);
-        } else if (percentage == 0.0f) {
-            return new Tuple<DenseDoubleMatrix, DenseDoubleMatrix>(null, this);
-        }
-
-        final Random rand = new Random(System.nanoTime());
-        int firstMatrixRowsCount = Math.round(percentage * numRows);
-
-        // we first choose needed rows number of items to pick
-        final HashSet<Integer> lowerMatrixRowIndices = new HashSet<Integer>();
-        int missingRows = firstMatrixRowsCount;
-        while (missingRows > 0) {
-            final int nextIndex = rand.nextInt(numRows);
-            if (lowerMatrixRowIndices.add(nextIndex)) {
-                missingRows--;
-            }
-        }
-
-        // make to new matrixes
-        final double[][] firstMatrix = new double[firstMatrixRowsCount][numColumns];
-        int firstMatrixIndex = 0;
-        final double[][] secondMatrix = new double[numRows - firstMatrixRowsCount][numColumns];
-        int secondMatrixIndex = 0;
-
-        // then we loop over all items and put split the matrix
-        for (int r = 0; r < numRows; r++) {
-            if (lowerMatrixRowIndices.contains(r)) {
-                firstMatrix[firstMatrixIndex++] = matrix[r];
-            } else {
-                secondMatrix[secondMatrixIndex++] = matrix[r];
-            }
-        }
-
-        return new Tuple<DenseDoubleMatrix, DenseDoubleMatrix>(
-                new DenseDoubleMatrix(firstMatrix), new DenseDoubleMatrix(secondMatrix));
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#getNonDefaultBooleanMatrix()
-    */
-    @Override
-    public final BooleanMatrix getNonDefaultBooleanMatrix() {
-        DenseBooleanMatrix m = new DenseBooleanMatrix(this.numRows, this.numColumns);
-        for (int i = 0; i < numRows; i++) {
-            for (int j = 0; j < numColumns; j++) {
-                m.set(i, j, this.matrix[i][j] != NOT_FLAGGED);
-            }
-        }
-        return m;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#multiply(double)
-    */
-    @Override
-    public final DenseDoubleMatrix multiply(double scalar) {
-        DenseDoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns);
-        for (int i = 0; i < numRows; i++) {
-            for (int j = 0; j < numColumns; j++) {
-                m.set(i, j, this.matrix[i][j] * scalar);
-            }
-        }
-        return m;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#multiply(de.jungblut.math.DoubleMatrix)
-    */
-    @Override
-    public final DoubleMatrix multiply(DoubleMatrix other) {
-        DenseDoubleMatrix matrix = new DenseDoubleMatrix(this.getRowCount(),
-                other.getColumnCount());
-
-        final int m = this.numRows;
-        final int n = this.numColumns;
-        final int p = other.getColumnCount();
-
-        for (int j = p; --j >= 0;) {
-            for (int i = m; --i >= 0;) {
-                double s = 0;
-                for (int k = n; --k >= 0;) {
-                    s += get(i, k) * other.get(k, j);
-                }
-                matrix.set(i, j, s + matrix.get(i, j));
-            }
-        }
-
-        return matrix;
-        // }
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#multiplyElementWise(de.jungblut.math.
-    * BooleanMatrix)
-    */
-    @Override
-    public final DenseDoubleMatrix multiplyElementWise(BooleanMatrix other) {
-        DenseDoubleMatrix matrix = new DenseDoubleMatrix(this.numRows,
-                this.numColumns);
-
-        for (int i = 0; i < numRows; i++) {
-            for (int j = 0; j < numColumns; j++) {
-                matrix.set(i, j, this.get(i, j) * (other.get(i, j) ? 1.0d : 0.0d));
-            }
-        }
-
-        return matrix;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see
-    * de.jungblut.math.DoubleMatrix#multiplyElementWise(de.jungblut.math.DoubleMatrix
-    * )
-    */
-    @Override
-    public final DoubleMatrix multiplyElementWise(DoubleMatrix other) {
-        DenseDoubleMatrix matrix = new DenseDoubleMatrix(this.numRows,
-                this.numColumns);
-
-        for (int i = 0; i < numRows; i++) {
-            for (int j = 0; j < numColumns; j++) {
-                matrix.set(i, j, this.get(i, j) * (other.get(i, j)));
-            }
-        }
-
-        return matrix;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see
-    * de.jungblut.math.DoubleMatrix#multiplyVector(de.jungblut.math.DoubleVector)
-    */
-    @Override
-    public final DoubleVector multiplyVector(DoubleVector v) {
-        DoubleVector vector = new DenseDoubleVector(this.getRowCount());
-        for (int row = 0; row < numRows; row++) {
-            double sum = 0.0d;
-            for (int col = 0; col < numColumns; col++) {
-                sum += (matrix[row][col] * v.get(col));
-            }
-            vector.set(row, sum);
-        }
-
-        return vector;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#transpose()
-    */
-    @Override
-    public DenseDoubleMatrix transpose() {
-        DenseDoubleMatrix m = new DenseDoubleMatrix(this.numColumns, this.numRows);
-        for (int i = 0; i < numRows; i++) {
-            for (int j = 0; j < numColumns; j++) {
-                m.set(j, i, this.matrix[i][j]);
-            }
-        }
-        return m;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#subtractBy(double)
-    */
-    @Override
-    public DenseDoubleMatrix subtractBy(double amount) {
-        DenseDoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns);
-        for (int i = 0; i < numRows; i++) {
-            for (int j = 0; j < numColumns; j++) {
-                m.set(i, j, amount - this.matrix[i][j]);
-            }
-        }
-        return m;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#subtract(double)
-    */
-    @Override
-    public DenseDoubleMatrix subtract(double amount) {
-        DenseDoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns);
-        for (int i = 0; i < numRows; i++) {
-            for (int j = 0; j < numColumns; j++) {
-                m.set(i, j, this.matrix[i][j] - amount);
-            }
-        }
-        return m;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#subtract(de.jungblut.math.DoubleMatrix)
-    */
-    @Override
-    public DoubleMatrix subtract(DoubleMatrix other) {
-        DoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns);
-        for (int i = 0; i < numRows; i++) {
-            for (int j = 0; j < numColumns; j++) {
-                m.set(i, j, this.matrix[i][j] - other.get(i, j));
-            }
-        }
-        return m;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#subtract(de.jungblut.math.DoubleVector)
-    */
-    @Override
-    public DenseDoubleMatrix subtract(DoubleVector vec) {
-        DenseDoubleMatrix cop = new DenseDoubleMatrix(this.getRowCount(),
-                this.getColumnCount());
-        for (int i = 0; i < this.getColumnCount(); i++) {
-            cop.setColumn(i, getColumnVector(i).subtract(vec.get(i)).toArray());
-        }
-        return cop;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#divide(de.jungblut.math.DoubleVector)
-    */
-    @Override
-    public DoubleMatrix divide(DoubleVector vec) {
-        DoubleMatrix cop = new DenseDoubleMatrix(this.getRowCount(),
-                this.getColumnCount());
-        for (int i = 0; i < this.getColumnCount(); i++) {
-            cop.setColumnVector(i, getColumnVector(i).divide(vec.get(i)));
-        }
-        return cop;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#divide(de.jungblut.math.DoubleMatrix)
-    */
-    @Override
-    public DoubleMatrix divide(DoubleMatrix other) {
-        DoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns);
-        for (int i = 0; i < numRows; i++) {
-            for (int j = 0; j < numColumns; j++) {
-                m.set(i, j, this.matrix[i][j] / other.get(i, j));
-            }
-        }
-        return m;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#divide(double)
-    */
-    @Override
-    public DoubleMatrix divide(double scalar) {
-        DoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns);
-        for (int i = 0; i < numRows; i++) {
-            for (int j = 0; j < numColumns; j++) {
-                m.set(i, j, this.matrix[i][j] / scalar);
-            }
-        }
-        return m;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#add(de.jungblut.math.DoubleMatrix)
-    */
-    @Override
-    public DoubleMatrix add(DoubleMatrix other) {
-        DoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns);
-        for (int i = 0; i < numRows; i++) {
-            for (int j = 0; j < numColumns; j++) {
-                m.set(i, j, this.matrix[i][j] + other.get(i, j));
-            }
-        }
-        return m;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#pow(int)
-    */
-    @Override
-    public DoubleMatrix pow(int x) {
-        DoubleMatrix m = new DenseDoubleMatrix(this.numRows, this.numColumns);
-        for (int i = 0; i < numRows; i++) {
-            for (int j = 0; j < numColumns; j++) {
-                m.set(i, j, Math.pow(matrix[i][j], x));
-            }
-        }
-        return m;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#max(int)
-    */
-    @Override
-    public double max(int column) {
-        double max = Double.MIN_VALUE;
-        for (int i = 0; i < getRowCount(); i++) {
-            double d = matrix[i][column];
-            if (d > max) {
-                max = d;
-            }
-        }
-        return max;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#min(int)
-    */
-    @Override
-    public double min(int column) {
-        double min = Double.MAX_VALUE;
-        for (int i = 0; i < getRowCount(); i++) {
-            double d = matrix[i][column];
-            if (d < min) {
-                min = d;
-            }
-        }
-        return min;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#slice(int, int)
-    */
-    @Override
-    public DoubleMatrix slice(int rows, int cols) {
-        return slice(0, rows, 0, cols);
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#slice(int, int, int, int)
-    */
-    @Override
-    public DoubleMatrix slice(int rowOffset, int rowMax, int colOffset, int colMax) {
-        DenseDoubleMatrix m = new DenseDoubleMatrix(rowMax - rowOffset, colMax
-                - colOffset);
-        for (int row = rowOffset; row < rowMax; row++) {
-            for (int col = colOffset; col < colMax; col++) {
-                m.set(row - rowOffset, col - colOffset, this.get(row, col));
-            }
-        }
-        return m;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#isSparse()
-    */
-    @Override
-    public boolean isSparse() {
-        return false;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#sum()
-    */
-    @Override
-    public double sum() {
-        double x = 0.0d;
-        for (int i = 0; i < numRows; i++) {
-            for (int j = 0; j < numColumns; j++) {
-                x += Math.abs(matrix[i][j]);
-            }
-        }
-        return x;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleMatrix#columnIndices()
-    */
-    @Override
-    public int[] columnIndices() {
-        int[] x = new int[getColumnCount()];
-        for (int i = 0; i < getColumnCount(); i++)
-            x[i] = i;
-        return x;
-    }
-
-    @Override
-    public int hashCode() {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result + Arrays.hashCode(matrix);
-        result = prime * result + numColumns;
-        result = prime * result + numRows;
-        return result;
-    }
-
-    @Override
-    public boolean equals(Object obj) {
-        if (this == obj)
-            return true;
-        if (obj == null)
-            return false;
-        if (getClass() != obj.getClass())
-            return false;
-        DenseDoubleMatrix other = (DenseDoubleMatrix) obj;
-        if (!Arrays.deepEquals(matrix, other.matrix))
-            return false;
-        if (numColumns != other.numColumns)
-            return false;
-        return numRows == other.numRows;
-    }
-
-    @Override
-    public String toString() {
-        if (numRows < 10) {
-            StringBuilder sb = new StringBuilder();
-            for (int i = 0; i < numRows; i++) {
-                sb.append(Arrays.toString(matrix[i]));
-                sb.append('\n');
-            }
-            return sb.toString();
-        } else {
-            return numRows + "x" + numColumns;
-        }
-    }
-
-    /**
-     * Gets the eye matrix (ones on the main diagonal) with a given dimension.
-     */
-    public static DenseDoubleMatrix eye(int dimension) {
-        DenseDoubleMatrix m = new DenseDoubleMatrix(dimension, dimension);
-
-        for (int i = 0; i < dimension; i++) {
-            m.set(i, i, 1);
-        }
-
-        return m;
-    }
-
-    /**
-     * Deep copies the given matrix into a new returned one.
-     */
-    public static DenseDoubleMatrix copy(DenseDoubleMatrix matrix) {
-        final double[][] src = matrix.getValues();
-        final double[][] dest = new double[matrix.getRowCount()][matrix
-                .getColumnCount()];
-
-        for (int i = 0; i < dest.length; i++)
-            System.arraycopy(src[i], 0, dest[i], 0, src[i].length);
-
-        return new DenseDoubleMatrix(dest);
-    }
-
-    /**
-     * Some strange function I found in octave but I don't know what it was named.
-     * It does however multiply the elements from the transposed vector and the
-     * normal vector and sets it into the according indices of a new constructed
-     * matrix.
-     */
-    public static DenseDoubleMatrix multiplyTransposedVectors(
-            DoubleVector transposed, DoubleVector normal) {
-        DenseDoubleMatrix m = new DenseDoubleMatrix(transposed.getLength(),
-                normal.getLength());
-        for (int row = 0; row < transposed.getLength(); row++) {
-            for (int col = 0; col < normal.getLength(); col++) {
-                m.set(row, col, transposed.get(row) * normal.get(col));
-            }
-        }
-
-        return m;
-    }
-
-    /**
-     * Just a absolute error function.
-     */
-    public static double error(DenseDoubleMatrix a, DenseDoubleMatrix b) {
-        return a.subtract(b).sum();
-    }
-
-}
\ No newline at end of file
+}

Modified: hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseDoubleVector.java
URL: http://svn.apache.org/viewvc/hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseDoubleVector.java?rev=1361734&r1=1361733&r2=1361734&view=diff
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseDoubleVector.java (original)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseDoubleVector.java Sun Jul 15 17:08:36 2012
@@ -31,601 +31,601 @@ import com.google.common.collect.Abstrac
  */
 public final class DenseDoubleVector implements DoubleVector {
 
-    private final double[] vector;
+  private final double[] vector;
 
-    /**
-     * Creates a new vector with the given length.
-     */
-    public DenseDoubleVector(int length) {
-        this.vector = new double[length];
-    }
-
-    /**
-     * Creates a new vector with the given length and default value.
-     */
-    public DenseDoubleVector(int length, double val) {
-        this(length);
-        Arrays.fill(vector, val);
-    }
-
-    /**
-     * Creates a new vector with the given array.
-     */
-    public DenseDoubleVector(double[] arr) {
-        this.vector = arr;
-    }
-
-    /**
-     * Creates a new vector with the given array and the last value f1.
-     */
-    public DenseDoubleVector(double[] array, double f1) {
-        this.vector = new double[array.length + 1];
-        System.arraycopy(array, 0, this.vector, 0, array.length);
-        this.vector[array.length] = f1;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#get(int)
-    */
-    @Override
-    public final double get(int index) {
-        return vector[index];
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#getLength()
-    */
-    @Override
-    public final int getLength() {
-        return vector.length;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#getDimension()
-    */
-    @Override
-    public int getDimension() {
-        return getLength();
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#set(int, double)
-    */
-    @Override
-    public final void set(int index, double value) {
-        vector[index] = value;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#apply(de.jungblut.math.function.
-    * DoubleVectorFunction)
-    */
-    @Override
-    public DoubleVector apply(DoubleVectorFunction func) {
-        DenseDoubleVector newV = new DenseDoubleVector(this.vector);
-        for (int i = 0; i < vector.length; i++) {
-            newV.vector[i] = func.calculate(i, vector[i]);
-        }
-        return newV;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#apply(de.jungblut.math.DoubleVector,
-    * de.jungblut.math.function.DoubleDoubleVectorFunction)
-    */
-    @Override
-    public DoubleVector apply(DoubleVector other, DoubleDoubleVectorFunction func) {
-        DenseDoubleVector newV = (DenseDoubleVector) deepCopy();
-        for (int i = 0; i < vector.length; i++) {
-            newV.vector[i] = func.calculate(i, vector[i], other.get(i));
-        }
-        return newV;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#add(de.jungblut.math.DoubleVector)
-    */
-    @Override
-    public final DoubleVector add(DoubleVector v) {
-        DenseDoubleVector newv = new DenseDoubleVector(v.getLength());
-        for (int i = 0; i < v.getLength(); i++) {
-            newv.set(i, this.get(i) + v.get(i));
-        }
-        return newv;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#add(double)
-    */
-    @Override
-    public final DoubleVector add(double scalar) {
-        DoubleVector newv = new DenseDoubleVector(this.getLength());
-        for (int i = 0; i < this.getLength(); i++) {
-            newv.set(i, this.get(i) + scalar);
-        }
-        return newv;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#subtract(de.jungblut.math.DoubleVector)
-    */
-    @Override
-    public final DoubleVector subtract(DoubleVector v) {
-        DoubleVector newv = new DenseDoubleVector(v.getLength());
-        for (int i = 0; i < v.getLength(); i++) {
-            newv.set(i, this.get(i) - v.get(i));
-        }
-        return newv;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#subtract(double)
-    */
-    @Override
-    public final DoubleVector subtract(double v) {
-        DenseDoubleVector newv = new DenseDoubleVector(vector.length);
-        for (int i = 0; i < vector.length; i++) {
-            newv.set(i, vector[i] - v);
-        }
-        return newv;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#subtractFrom(double)
-    */
-    @Override
-    public final DoubleVector subtractFrom(double v) {
-        DenseDoubleVector newv = new DenseDoubleVector(vector.length);
-        for (int i = 0; i < vector.length; i++) {
-            newv.set(i, v - vector[i]);
-        }
-        return newv;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#multiply(double)
-    */
-    @Override
-    public DoubleVector multiply(double scalar) {
-        DoubleVector v = new DenseDoubleVector(this.getLength());
-        for (int i = 0; i < v.getLength(); i++) {
-            v.set(i, this.get(i) * scalar);
-        }
-        return v;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#multiply(de.jungblut.math.DoubleVector)
-    */
-    @Override
-    public DoubleVector multiply(DoubleVector vector) {
-        DoubleVector v = new DenseDoubleVector(this.getLength());
-        for (int i = 0; i < v.getLength(); i++) {
-            v.set(i, this.get(i) * vector.get(i));
-        }
-        return v;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#divide(double)
-    */
-    @Override
-    public DoubleVector divide(double scalar) {
-        DenseDoubleVector v = new DenseDoubleVector(this.getLength());
-        for (int i = 0; i < v.getLength(); i++) {
-            v.set(i, this.get(i) / scalar);
-        }
-        return v;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#pow(int)
-    */
-    @Override
-    public DoubleVector pow(int x) {
-        DenseDoubleVector v = new DenseDoubleVector(getLength());
-        for (int i = 0; i < v.getLength(); i++) {
-            double value = 0.0d;
-            // it is faster to multiply when we having ^2
-            if (x == 2) {
-                value = vector[i] * vector[i];
-            } else {
-                value = Math.pow(vector[i], x);
-            }
-            v.set(i, value);
-        }
-        return v;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#sqrt()
-    */
-    @Override
-    public DoubleVector sqrt() {
-        DoubleVector v = new DenseDoubleVector(getLength());
-        for (int i = 0; i < v.getLength(); i++) {
-            v.set(i, Math.sqrt(vector[i]));
-        }
-        return v;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#sum()
-    */
-    @Override
-    public double sum() {
-        double sum = 0.0d;
-        for (double aVector : vector) {
-            sum += aVector;
-        }
-        return sum;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#abs()
-    */
-    @Override
-    public DoubleVector abs() {
-        DoubleVector v = new DenseDoubleVector(getLength());
-        for (int i = 0; i < v.getLength(); i++) {
-            v.set(i, Math.abs(vector[i]));
-        }
-        return v;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#divideFrom(double)
-    */
-    @Override
-    public DoubleVector divideFrom(double scalar) {
-        DoubleVector v = new DenseDoubleVector(this.getLength());
-        for (int i = 0; i < v.getLength(); i++) {
-            if (this.get(i) != 0.0d) {
-                double result = scalar / this.get(i);
-                v.set(i, result);
-            } else {
-                v.set(i, 0.0d);
-            }
-        }
-        return v;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#dot(de.jungblut.math.DoubleVector)
-    */
-    @Override
-    public double dot(DoubleVector s) {
-        double dotProduct = 0.0d;
-        for (int i = 0; i < getLength(); i++) {
-            dotProduct += this.get(i) * s.get(i);
-        }
-        return dotProduct;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#slice(int)
-    */
-    @Override
-    public DoubleVector slice(int length) {
-        return slice(0, length);
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#slice(int, int)
-    */
-    @Override
-    public DoubleVector slice(int offset, int length) {
-        DoubleVector nv = new DenseDoubleVector(length - offset);
-        int index = 0;
-        for (int i = offset; i < length; i++) {
-            nv.set(index++, vector[i]);
-        }
-
-        return nv;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#max()
-    */
-    @Override
-    public double max() {
-        double max = -Double.MAX_VALUE;
-        for (int i = 0; i < getLength(); i++) {
-            double d = vector[i];
-            if (d > max) {
-                max = d;
-            }
-        }
-        return max;
-    }
-
-    /**
-     * @return the index where the maximum resides.
-     */
-    public int maxIndex() {
-        double max = -Double.MAX_VALUE;
-        int maxIndex = 0;
-        for (int i = 0; i < getLength(); i++) {
-            double d = vector[i];
-            if (d > max) {
-                max = d;
-                maxIndex = i;
-            }
-        }
-        return maxIndex;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#min()
-    */
-    @Override
-    public double min() {
-        double min = Double.MAX_VALUE;
-        for (int i = 0; i < getLength(); i++) {
-            double d = vector[i];
-            if (d < min) {
-                min = d;
-            }
-        }
-        return min;
-    }
-
-    /**
-     * @return the index where the minimum resides.
-     */
-    public int minIndex() {
-        double min = Double.MAX_VALUE;
-        int minIndex = 0;
-        for (int i = 0; i < getLength(); i++) {
-            double d = vector[i];
-            if (d < min) {
-                min = d;
-                minIndex = i;
-            }
-        }
-        return minIndex;
-    }
-
-    /**
-     * @return a new vector which has rinted each element.
-     */
-    public DenseDoubleVector rint() {
-        DenseDoubleVector v = new DenseDoubleVector(getLength());
-        for (int i = 0; i < getLength(); i++) {
-            double d = vector[i];
-            v.set(i, Math.rint(d));
-        }
-        return v;
-    }
-
-    /**
-     * @return a new vector which has rounded each element.
-     */
-    public DenseDoubleVector round() {
-        DenseDoubleVector v = new DenseDoubleVector(getLength());
-        for (int i = 0; i < getLength(); i++) {
-            double d = vector[i];
-            v.set(i, Math.round(d));
-        }
-        return v;
-    }
-
-    /**
-     * @return a new vector which has ceiled each element.
-     */
-    public DenseDoubleVector ceil() {
-        DenseDoubleVector v = new DenseDoubleVector(getLength());
-        for (int i = 0; i < getLength(); i++) {
-            double d = vector[i];
-            v.set(i, Math.ceil(d));
-        }
-        return v;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#toArray()
-    */
-    @Override
-    public final double[] toArray() {
-        return vector;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#isSparse()
-    */
-    @Override
-    public boolean isSparse() {
-        return false;
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#deepCopy()
-    */
-    @Override
-    public DoubleVector deepCopy() {
-        final double[] src = vector;
-        final double[] dest = new double[vector.length];
-        System.arraycopy(src, 0, dest, 0, vector.length);
-        return new DenseDoubleVector(dest);
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#iterateNonZero()
-    */
-    @Override
-    public Iterator<DoubleVectorElement> iterateNonZero() {
-        return new NonZeroIterator();
-    }
-
-    /*
-    * (non-Javadoc)
-    * @see de.jungblut.math.DoubleVector#iterate()
-    */
-    @Override
-    public Iterator<DoubleVectorElement> iterate() {
-        return new DefaultIterator();
-    }
-
-    @Override
-    public final String toString() {
-        if (getLength() < 20) {
-            return Arrays.toString(vector);
-        } else {
-            return getLength() + "x1";
-        }
-    }
-
-    @Override
-    public int hashCode() {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result + Arrays.hashCode(vector);
-        return result;
-    }
-
-    @Override
-    public boolean equals(Object obj) {
-        if (this == obj)
-            return true;
-        if (obj == null)
-            return false;
-        if (getClass() != obj.getClass())
-            return false;
-        DenseDoubleVector other = (DenseDoubleVector) obj;
-        return Arrays.equals(vector, other.vector);
-    }
-
-    /**
-     * Non-zero iterator for vector elements.
-     */
-    private final class NonZeroIterator extends
-            AbstractIterator<DoubleVectorElement> {
-
-        private final DoubleVectorElement element = new DoubleVectorElement();
-        private final double[] array;
-        private int currentIndex = 0;
-
-        private NonZeroIterator() {
-            this.array = vector;
-        }
-
-        @Override
-        protected final DoubleVectorElement computeNext() {
-            while (array[currentIndex] == 0.0d) {
-                currentIndex++;
-                if (currentIndex >= array.length)
-                    return endOfData();
-            }
-            element.setIndex(currentIndex);
-            element.setValue(array[currentIndex]);
-            return element;
-        }
-    }
-
-    /**
-     * Iterator for all elements.
-     */
-    private final class DefaultIterator extends
-            AbstractIterator<DoubleVectorElement> {
-
-        private final DoubleVectorElement element = new DoubleVectorElement();
-        private final double[] array;
-        private int currentIndex = 0;
-
-        private DefaultIterator() {
-            this.array = vector;
-        }
-
-        @Override
-        protected final DoubleVectorElement computeNext() {
-            if (currentIndex < array.length) {
-                element.setIndex(currentIndex);
-                element.setValue(array[currentIndex]);
-                currentIndex++;
-                return element;
-            } else {
-                return endOfData();
-            }
-        }
-
-    }
-
-    /**
-     * @return a new vector with dimension num and a default value of 1.
-     */
-    public static DenseDoubleVector ones(int num) {
-        return new DenseDoubleVector(num, 1.0d);
-    }
-
-    /**
-     * @return a new vector filled from index, to index, with a given stepsize.
-     */
-    public static DenseDoubleVector fromUpTo(double from, double to,
-                                             double stepsize) {
-        DenseDoubleVector v = new DenseDoubleVector(
-                (int) (Math.round(((to - from) / stepsize) + 0.5)));
-
-        for (int i = 0; i < v.getLength(); i++) {
-            v.set(i, from + i * stepsize);
-        }
-        return v;
-    }
-
-    /**
-     * Some crazy sort function.
-     */
-    public static List<Tuple<Double, Integer>> sort(DoubleVector vector,
-                                                    final Comparator<Double> scoreComparator) {
-        List<Tuple<Double, Integer>> list = new ArrayList<Tuple<Double, Integer>>(
-                vector.getLength());
-        for (int i = 0; i < vector.getLength(); i++) {
-            list.add(new Tuple<Double, Integer>(vector.get(i), i));
-        }
-        Collections.sort(list, new Comparator<Tuple<Double, Integer>>() {
-            @Override
-            public int compare(Tuple<Double, Integer> o1, Tuple<Double, Integer> o2) {
-                return scoreComparator.compare(o1.getFirst(), o2.getFirst());
-            }
-        });
-        return list;
-    }
-
-    @Override
-    public boolean isNamed() {
-        return false;
-    }
-
-    @Override
-    public String getName() {
-        return null;
-    }
+  /**
+   * Creates a new vector with the given length.
+   */
+  public DenseDoubleVector(int length) {
+    this.vector = new double[length];
+  }
+
+  /**
+   * Creates a new vector with the given length and default value.
+   */
+  public DenseDoubleVector(int length, double val) {
+    this(length);
+    Arrays.fill(vector, val);
+  }
+
+  /**
+   * Creates a new vector with the given array.
+   */
+  public DenseDoubleVector(double[] arr) {
+    this.vector = arr;
+  }
+
+  /**
+   * Creates a new vector with the given array and the last value f1.
+   */
+  public DenseDoubleVector(double[] array, double f1) {
+    this.vector = new double[array.length + 1];
+    System.arraycopy(array, 0, this.vector, 0, array.length);
+    this.vector[array.length] = f1;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#get(int)
+   */
+  @Override
+  public final double get(int index) {
+    return vector[index];
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#getLength()
+   */
+  @Override
+  public final int getLength() {
+    return vector.length;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#getDimension()
+   */
+  @Override
+  public int getDimension() {
+    return getLength();
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#set(int, double)
+   */
+  @Override
+  public final void set(int index, double value) {
+    vector[index] = value;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#apply(de.jungblut.math.function.
+   * DoubleVectorFunction)
+   */
+  @Override
+  public DoubleVector apply(DoubleVectorFunction func) {
+    DenseDoubleVector newV = new DenseDoubleVector(this.vector);
+    for (int i = 0; i < vector.length; i++) {
+      newV.vector[i] = func.calculate(i, vector[i]);
+    }
+    return newV;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#apply(de.jungblut.math.DoubleVector,
+   * de.jungblut.math.function.DoubleDoubleVectorFunction)
+   */
+  @Override
+  public DoubleVector apply(DoubleVector other, DoubleDoubleVectorFunction func) {
+    DenseDoubleVector newV = (DenseDoubleVector) deepCopy();
+    for (int i = 0; i < vector.length; i++) {
+      newV.vector[i] = func.calculate(i, vector[i], other.get(i));
+    }
+    return newV;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#add(de.jungblut.math.DoubleVector)
+   */
+  @Override
+  public final DoubleVector add(DoubleVector v) {
+    DenseDoubleVector newv = new DenseDoubleVector(v.getLength());
+    for (int i = 0; i < v.getLength(); i++) {
+      newv.set(i, this.get(i) + v.get(i));
+    }
+    return newv;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#add(double)
+   */
+  @Override
+  public final DoubleVector add(double scalar) {
+    DoubleVector newv = new DenseDoubleVector(this.getLength());
+    for (int i = 0; i < this.getLength(); i++) {
+      newv.set(i, this.get(i) + scalar);
+    }
+    return newv;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#subtract(de.jungblut.math.DoubleVector)
+   */
+  @Override
+  public final DoubleVector subtract(DoubleVector v) {
+    DoubleVector newv = new DenseDoubleVector(v.getLength());
+    for (int i = 0; i < v.getLength(); i++) {
+      newv.set(i, this.get(i) - v.get(i));
+    }
+    return newv;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#subtract(double)
+   */
+  @Override
+  public final DoubleVector subtract(double v) {
+    DenseDoubleVector newv = new DenseDoubleVector(vector.length);
+    for (int i = 0; i < vector.length; i++) {
+      newv.set(i, vector[i] - v);
+    }
+    return newv;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#subtractFrom(double)
+   */
+  @Override
+  public final DoubleVector subtractFrom(double v) {
+    DenseDoubleVector newv = new DenseDoubleVector(vector.length);
+    for (int i = 0; i < vector.length; i++) {
+      newv.set(i, v - vector[i]);
+    }
+    return newv;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#multiply(double)
+   */
+  @Override
+  public DoubleVector multiply(double scalar) {
+    DoubleVector v = new DenseDoubleVector(this.getLength());
+    for (int i = 0; i < v.getLength(); i++) {
+      v.set(i, this.get(i) * scalar);
+    }
+    return v;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#multiply(de.jungblut.math.DoubleVector)
+   */
+  @Override
+  public DoubleVector multiply(DoubleVector vector) {
+    DoubleVector v = new DenseDoubleVector(this.getLength());
+    for (int i = 0; i < v.getLength(); i++) {
+      v.set(i, this.get(i) * vector.get(i));
+    }
+    return v;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#divide(double)
+   */
+  @Override
+  public DoubleVector divide(double scalar) {
+    DenseDoubleVector v = new DenseDoubleVector(this.getLength());
+    for (int i = 0; i < v.getLength(); i++) {
+      v.set(i, this.get(i) / scalar);
+    }
+    return v;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#pow(int)
+   */
+  @Override
+  public DoubleVector pow(int x) {
+    DenseDoubleVector v = new DenseDoubleVector(getLength());
+    for (int i = 0; i < v.getLength(); i++) {
+      double value = 0.0d;
+      // it is faster to multiply when we having ^2
+      if (x == 2) {
+        value = vector[i] * vector[i];
+      } else {
+        value = Math.pow(vector[i], x);
+      }
+      v.set(i, value);
+    }
+    return v;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#sqrt()
+   */
+  @Override
+  public DoubleVector sqrt() {
+    DoubleVector v = new DenseDoubleVector(getLength());
+    for (int i = 0; i < v.getLength(); i++) {
+      v.set(i, Math.sqrt(vector[i]));
+    }
+    return v;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#sum()
+   */
+  @Override
+  public double sum() {
+    double sum = 0.0d;
+    for (double aVector : vector) {
+      sum += aVector;
+    }
+    return sum;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#abs()
+   */
+  @Override
+  public DoubleVector abs() {
+    DoubleVector v = new DenseDoubleVector(getLength());
+    for (int i = 0; i < v.getLength(); i++) {
+      v.set(i, Math.abs(vector[i]));
+    }
+    return v;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#divideFrom(double)
+   */
+  @Override
+  public DoubleVector divideFrom(double scalar) {
+    DoubleVector v = new DenseDoubleVector(this.getLength());
+    for (int i = 0; i < v.getLength(); i++) {
+      if (this.get(i) != 0.0d) {
+        double result = scalar / this.get(i);
+        v.set(i, result);
+      } else {
+        v.set(i, 0.0d);
+      }
+    }
+    return v;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#dot(de.jungblut.math.DoubleVector)
+   */
+  @Override
+  public double dot(DoubleVector s) {
+    double dotProduct = 0.0d;
+    for (int i = 0; i < getLength(); i++) {
+      dotProduct += this.get(i) * s.get(i);
+    }
+    return dotProduct;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#slice(int)
+   */
+  @Override
+  public DoubleVector slice(int length) {
+    return slice(0, length);
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#slice(int, int)
+   */
+  @Override
+  public DoubleVector slice(int offset, int length) {
+    DoubleVector nv = new DenseDoubleVector(length - offset);
+    int index = 0;
+    for (int i = offset; i < length; i++) {
+      nv.set(index++, vector[i]);
+    }
+
+    return nv;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#max()
+   */
+  @Override
+  public double max() {
+    double max = -Double.MAX_VALUE;
+    for (int i = 0; i < getLength(); i++) {
+      double d = vector[i];
+      if (d > max) {
+        max = d;
+      }
+    }
+    return max;
+  }
+
+  /**
+   * @return the index where the maximum resides.
+   */
+  public int maxIndex() {
+    double max = -Double.MAX_VALUE;
+    int maxIndex = 0;
+    for (int i = 0; i < getLength(); i++) {
+      double d = vector[i];
+      if (d > max) {
+        max = d;
+        maxIndex = i;
+      }
+    }
+    return maxIndex;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#min()
+   */
+  @Override
+  public double min() {
+    double min = Double.MAX_VALUE;
+    for (int i = 0; i < getLength(); i++) {
+      double d = vector[i];
+      if (d < min) {
+        min = d;
+      }
+    }
+    return min;
+  }
+
+  /**
+   * @return the index where the minimum resides.
+   */
+  public int minIndex() {
+    double min = Double.MAX_VALUE;
+    int minIndex = 0;
+    for (int i = 0; i < getLength(); i++) {
+      double d = vector[i];
+      if (d < min) {
+        min = d;
+        minIndex = i;
+      }
+    }
+    return minIndex;
+  }
+
+  /**
+   * @return a new vector which has rinted each element.
+   */
+  public DenseDoubleVector rint() {
+    DenseDoubleVector v = new DenseDoubleVector(getLength());
+    for (int i = 0; i < getLength(); i++) {
+      double d = vector[i];
+      v.set(i, Math.rint(d));
+    }
+    return v;
+  }
+
+  /**
+   * @return a new vector which has rounded each element.
+   */
+  public DenseDoubleVector round() {
+    DenseDoubleVector v = new DenseDoubleVector(getLength());
+    for (int i = 0; i < getLength(); i++) {
+      double d = vector[i];
+      v.set(i, Math.round(d));
+    }
+    return v;
+  }
+
+  /**
+   * @return a new vector which has ceiled each element.
+   */
+  public DenseDoubleVector ceil() {
+    DenseDoubleVector v = new DenseDoubleVector(getLength());
+    for (int i = 0; i < getLength(); i++) {
+      double d = vector[i];
+      v.set(i, Math.ceil(d));
+    }
+    return v;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#toArray()
+   */
+  @Override
+  public final double[] toArray() {
+    return vector;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#isSparse()
+   */
+  @Override
+  public boolean isSparse() {
+    return false;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#deepCopy()
+   */
+  @Override
+  public DoubleVector deepCopy() {
+    final double[] src = vector;
+    final double[] dest = new double[vector.length];
+    System.arraycopy(src, 0, dest, 0, vector.length);
+    return new DenseDoubleVector(dest);
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#iterateNonZero()
+   */
+  @Override
+  public Iterator<DoubleVectorElement> iterateNonZero() {
+    return new NonZeroIterator();
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#iterate()
+   */
+  @Override
+  public Iterator<DoubleVectorElement> iterate() {
+    return new DefaultIterator();
+  }
+
+  @Override
+  public final String toString() {
+    if (getLength() < 20) {
+      return Arrays.toString(vector);
+    } else {
+      return getLength() + "x1";
+    }
+  }
+
+  @Override
+  public int hashCode() {
+    final int prime = 31;
+    int result = 1;
+    result = prime * result + Arrays.hashCode(vector);
+    return result;
+  }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (this == obj)
+      return true;
+    if (obj == null)
+      return false;
+    if (getClass() != obj.getClass())
+      return false;
+    DenseDoubleVector other = (DenseDoubleVector) obj;
+    return Arrays.equals(vector, other.vector);
+  }
+
+  /**
+   * Non-zero iterator for vector elements.
+   */
+  private final class NonZeroIterator extends
+      AbstractIterator<DoubleVectorElement> {
+
+    private final DoubleVectorElement element = new DoubleVectorElement();
+    private final double[] array;
+    private int currentIndex = 0;
+
+    private NonZeroIterator() {
+      this.array = vector;
+    }
+
+    @Override
+    protected final DoubleVectorElement computeNext() {
+      while (array[currentIndex] == 0.0d) {
+        currentIndex++;
+        if (currentIndex >= array.length)
+          return endOfData();
+      }
+      element.setIndex(currentIndex);
+      element.setValue(array[currentIndex]);
+      return element;
+    }
+  }
+
+  /**
+   * Iterator for all elements.
+   */
+  private final class DefaultIterator extends
+      AbstractIterator<DoubleVectorElement> {
+
+    private final DoubleVectorElement element = new DoubleVectorElement();
+    private final double[] array;
+    private int currentIndex = 0;
+
+    private DefaultIterator() {
+      this.array = vector;
+    }
+
+    @Override
+    protected final DoubleVectorElement computeNext() {
+      if (currentIndex < array.length) {
+        element.setIndex(currentIndex);
+        element.setValue(array[currentIndex]);
+        currentIndex++;
+        return element;
+      } else {
+        return endOfData();
+      }
+    }
+
+  }
+
+  /**
+   * @return a new vector with dimension num and a default value of 1.
+   */
+  public static DenseDoubleVector ones(int num) {
+    return new DenseDoubleVector(num, 1.0d);
+  }
+
+  /**
+   * @return a new vector filled from index, to index, with a given stepsize.
+   */
+  public static DenseDoubleVector fromUpTo(double from, double to,
+      double stepsize) {
+    DenseDoubleVector v = new DenseDoubleVector(
+        (int) (Math.round(((to - from) / stepsize) + 0.5)));
+
+    for (int i = 0; i < v.getLength(); i++) {
+      v.set(i, from + i * stepsize);
+    }
+    return v;
+  }
+
+  /**
+   * Some crazy sort function.
+   */
+  public static List<Tuple<Double, Integer>> sort(DoubleVector vector,
+      final Comparator<Double> scoreComparator) {
+    List<Tuple<Double, Integer>> list = new ArrayList<Tuple<Double, Integer>>(
+        vector.getLength());
+    for (int i = 0; i < vector.getLength(); i++) {
+      list.add(new Tuple<Double, Integer>(vector.get(i), i));
+    }
+    Collections.sort(list, new Comparator<Tuple<Double, Integer>>() {
+      @Override
+      public int compare(Tuple<Double, Integer> o1, Tuple<Double, Integer> o2) {
+        return scoreComparator.compare(o1.getFirst(), o2.getFirst());
+      }
+    });
+    return list;
+  }
+
+  @Override
+  public boolean isNamed() {
+    return false;
+  }
+
+  @Override
+  public String getName() {
+    return null;
+  }
 
-}
\ No newline at end of file
+}

Modified: hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleDoubleVectorFunction.java
URL: http://svn.apache.org/viewvc/hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleDoubleVectorFunction.java?rev=1361734&r1=1361733&r2=1361734&view=diff
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleDoubleVectorFunction.java (original)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleDoubleVectorFunction.java Sun Jul 15 17:08:36 2012
@@ -23,10 +23,10 @@ package org.apache.hama.ml.math;
  */
 public interface DoubleDoubleVectorFunction {
 
-    /**
-     * Calculates the result of the left and right value of two vectors at a given
-     * index.
-     */
-    public double calculate(int index, double left, double right);
+  /**
+   * Calculates the result of the left and right value of two vectors at a given
+   * index.
+   */
+  public double calculate(int index, double left, double right);
 
-}
\ No newline at end of file
+}

Modified: hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleMatrix.java
URL: http://svn.apache.org/viewvc/hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleMatrix.java?rev=1361734&r1=1361733&r2=1361734&view=diff
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleMatrix.java (original)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleMatrix.java Sun Jul 15 17:08:36 2012
@@ -23,177 +23,165 @@ package org.apache.hama.ml.math;
  */
 public interface DoubleMatrix {
 
-    /**
-     * Not flagged value for sparse matrices, it is default to 0.0d.
-     */
-    public static final double NOT_FLAGGED = 0.0d;
-
-    /**
-     * Get a specific value of the matrix.
-     *
-     * @return Returns the integer value at in the column at the row.
-     */
-    public double get(int row, int col);
-
-    /**
-     * Returns the number of columns in the matrix. Always a constant time
-     * operation.
-     */
-    public int getColumnCount();
-
-    /**
-     * Get a whole column of the matrix as vector.
-     */
-    public DoubleVector getColumnVector(int col);
-
-    /**
-     * Returns the number of rows in this matrix. Always a constant time
-     * operation.
-     */
-    public int getRowCount();
-
-    /**
-     * Get a single row of the matrix as a vector.
-     */
-    public DoubleVector getRowVector(int row);
-
-    /**
-     * Sets the value at the given row and column index.
-     */
-    public void set(int row, int col, double value);
-
-    /**
-     * Sets a whole column at index col with the given vector.
-     */
-    public void setColumnVector(int col, DoubleVector column);
-
-    /**
-     * Sets the whole row at index rowIndex with the given vector.
-     */
-    public void setRowVector(int rowIndex, DoubleVector row);
-
-    /**
-     * Multiplies this matrix (each element) with the given scalar and returns a
-     * new matrix.
-     */
-    public DoubleMatrix multiply(double scalar);
-
-    /**
-     * Multiplies this matrix with the given other matrix.
-     */
-    public DoubleMatrix multiply(DoubleMatrix other);
-
-    /**
-     * Multiplies this matrix per element with a binary (boolean) matrix, where
-     * false = 0 and true = 1.
-     */
-    public DoubleMatrix multiplyElementWise(BooleanMatrix other);
-
-    /**
-     * Multiplies this matrix per element with a given matrix.
-     */
-    public DoubleMatrix multiplyElementWise(DoubleMatrix other);
-
-    /**
-     * Multiplies this matrix with a given vector v. The returning vector contains
-     * the sum of the rows.
-     */
-    public DoubleVector multiplyVector(DoubleVector v);
-
-    /**
-     * Transposes this matrix.
-     */
-    public DoubleMatrix transpose();
-
-    /**
-     * Substracts the given amount by each element in this matrix. <br/>
-     * = (amount - matrix value)
-     */
-    public DoubleMatrix subtractBy(double amount);
-
-    /**
-     * Subtracts each element in this matrix by the given amount.<br/>
-     * = (matrix value - amount)
-     */
-    public DoubleMatrix subtract(double amount);
-
-    /**
-     * Subtracts this matrix by the given other matrix.
-     */
-    public DoubleMatrix subtract(DoubleMatrix other);
-
-    /**
-     * Subtracts each element in a column by the related element in the given
-     * vector.
-     */
-    public DoubleMatrix subtract(DoubleVector vec);
-
-    /**
-     * Divides each element in a column by the related element in the given
-     * vector.
-     */
-    public DoubleMatrix divide(DoubleVector vec);
-
-    /**
-     * Divides this matrix by the given other matrix. (Per element division).
-     */
-    public DoubleMatrix divide(DoubleMatrix other);
-
-    /**
-     * Divides each element in this matrix by the given scalar.
-     */
-    public DoubleMatrix divide(double scalar);
-
-    /**
-     * Adds the elements in the given matrix to the elements in this matrix.
-     */
-    public DoubleMatrix add(DoubleMatrix other);
-
-    /**
-     * Pows each element by the given argument. <br/>
-     * = (matrix element^x)
-     */
-    public DoubleMatrix pow(int x);
-
-    /**
-     * Returns the maximum value of the given column.
-     */
-    public double max(int column);
-
-    /**
-     * Returns the minimum value of the given column.
-     */
-    public double min(int column);
-
-    /**
-     * Sums all elements.
-     */
-    public double sum();
-
-    /**
-     * Returns an array of column indices existing in this matrix.
-     */
-    public int[] columnIndices();
-
-    /**
-     * Returns a {@link BooleanMatrix} where each element is true when it is not
-     * 0.0d.
-     */
-    public BooleanMatrix getNonDefaultBooleanMatrix();
-
-    /**
-     * Returns true if the underlying implementation is sparse.
-     */
-    public boolean isSparse();
-
-    /**
-     * Slices the given matrix from 0-rows and from 0-columns.
-     */
-    public DoubleMatrix slice(int rows, int cols);
-
-    /**
-     * Slices the given matrix from rowOffset-rowMax and from colOffset-colMax.
-     */
-    public DoubleMatrix slice(int rowOffset, int rowMax, int colOffset, int colMax);
+  /**
+   * Not flagged value for sparse matrices, it is default to 0.0d.
+   */
+  public static final double NOT_FLAGGED = 0.0d;
+
+  /**
+   * Get a specific value of the matrix.
+   * 
+   * @return Returns the integer value at in the column at the row.
+   */
+  public double get(int row, int col);
+
+  /**
+   * Returns the number of columns in the matrix. Always a constant time
+   * operation.
+   */
+  public int getColumnCount();
+
+  /**
+   * Get a whole column of the matrix as vector.
+   */
+  public DoubleVector getColumnVector(int col);
+
+  /**
+   * Returns the number of rows in this matrix. Always a constant time
+   * operation.
+   */
+  public int getRowCount();
+
+  /**
+   * Get a single row of the matrix as a vector.
+   */
+  public DoubleVector getRowVector(int row);
+
+  /**
+   * Sets the value at the given row and column index.
+   */
+  public void set(int row, int col, double value);
+
+  /**
+   * Sets a whole column at index col with the given vector.
+   */
+  public void setColumnVector(int col, DoubleVector column);
+
+  /**
+   * Sets the whole row at index rowIndex with the given vector.
+   */
+  public void setRowVector(int rowIndex, DoubleVector row);
+
+  /**
+   * Multiplies this matrix (each element) with the given scalar and returns a
+   * new matrix.
+   */
+  public DoubleMatrix multiply(double scalar);
+
+  /**
+   * Multiplies this matrix with the given other matrix.
+   */
+  public DoubleMatrix multiply(DoubleMatrix other);
+
+  /**
+   * Multiplies this matrix per element with a given matrix.
+   */
+  public DoubleMatrix multiplyElementWise(DoubleMatrix other);
+
+  /**
+   * Multiplies this matrix with a given vector v. The returning vector contains
+   * the sum of the rows.
+   */
+  public DoubleVector multiplyVector(DoubleVector v);
+
+  /**
+   * Transposes this matrix.
+   */
+  public DoubleMatrix transpose();
+
+  /**
+   * Substracts the given amount by each element in this matrix. <br/>
+   * = (amount - matrix value)
+   */
+  public DoubleMatrix subtractBy(double amount);
+
+  /**
+   * Subtracts each element in this matrix by the given amount.<br/>
+   * = (matrix value - amount)
+   */
+  public DoubleMatrix subtract(double amount);
+
+  /**
+   * Subtracts this matrix by the given other matrix.
+   */
+  public DoubleMatrix subtract(DoubleMatrix other);
+
+  /**
+   * Subtracts each element in a column by the related element in the given
+   * vector.
+   */
+  public DoubleMatrix subtract(DoubleVector vec);
+
+  /**
+   * Divides each element in a column by the related element in the given
+   * vector.
+   */
+  public DoubleMatrix divide(DoubleVector vec);
+
+  /**
+   * Divides this matrix by the given other matrix. (Per element division).
+   */
+  public DoubleMatrix divide(DoubleMatrix other);
+
+  /**
+   * Divides each element in this matrix by the given scalar.
+   */
+  public DoubleMatrix divide(double scalar);
+
+  /**
+   * Adds the elements in the given matrix to the elements in this matrix.
+   */
+  public DoubleMatrix add(DoubleMatrix other);
+
+  /**
+   * Pows each element by the given argument. <br/>
+   * = (matrix element^x)
+   */
+  public DoubleMatrix pow(int x);
+
+  /**
+   * Returns the maximum value of the given column.
+   */
+  public double max(int column);
+
+  /**
+   * Returns the minimum value of the given column.
+   */
+  public double min(int column);
+
+  /**
+   * Sums all elements.
+   */
+  public double sum();
+
+  /**
+   * Returns an array of column indices existing in this matrix.
+   */
+  public int[] columnIndices();
+
+  /**
+   * Returns true if the underlying implementation is sparse.
+   */
+  public boolean isSparse();
+
+  /**
+   * Slices the given matrix from 0-rows and from 0-columns.
+   */
+  public DoubleMatrix slice(int rows, int cols);
+
+  /**
+   * Slices the given matrix from rowOffset-rowMax and from colOffset-colMax.
+   */
+  public DoubleMatrix slice(int rowOffset, int rowMax, int colOffset, int colMax);
 
-}
\ No newline at end of file
+}



Mime
View raw message