hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tomm...@apache.org
Subject svn commit: r1359570 - /hama/trunk/ml/src/main/java/org/apache/hama/ml/math/
Date Tue, 10 Jul 2012 10:11:35 GMT
Author: tommaso
Date: Tue Jul 10 10:11:34 2012
New Revision: 1359570

URL: http://svn.apache.org/viewvc?rev=1359570&view=rev
Log:
[HAMA-604] - adding math package (flat structure for now) from Thomas math package

Added:
    hama/trunk/ml/src/main/java/org/apache/hama/ml/math/
    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
    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

Added: hama/trunk/ml/src/main/java/org/apache/hama/ml/math/BooleanMatrix.java
URL: http://svn.apache.org/viewvc/hama/trunk/ml/src/main/java/org/apache/hama/ml/math/BooleanMatrix.java?rev=1359570&view=auto
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/math/BooleanMatrix.java (added)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/math/BooleanMatrix.java Tue Jul 10 10:11:34 2012
@@ -0,0 +1,73 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama.ml.math;
+
+/**
+ * A matrix consisting of booleans.
+ */
+public interface BooleanMatrix {
+
+    /**
+     * double conversion value for non-default element value.
+     */
+    public static final double NOT_FLAGGED = 0.0d;
+
+    /**
+     * Get a specific value of the matrix.
+     *
+     * @return the integer value at in the column at the row.
+     */
+    public boolean get(int row, int col);
+
+    /**
+     * Returns the number of columns in the matrix.
+     */
+    public int getColumnCount();
+
+    /**
+     * Get a whole column of the matrix as vector. If the specified column doesn't
+     * exist a IllegalArgumentException is thrown.
+     */
+    public BooleanVector getColumnVector(int col);
+
+    /**
+     * Returns the number of rows in this matrix.
+     */
+    public int getRowCount();
+
+    /**
+     * Get a single row of the matrix as a vector.
+     */
+    public BooleanVector getRowVector(int row);
+
+    /**
+     * Sets the value at the given row and column index.
+     */
+    public void set(int row, int col, boolean value);
+
+    /**
+     * Transposes this matrix.
+     */
+    public BooleanMatrix transpose();
+
+    /**
+     * Returns an array of column indices existing in this matrix.
+     */
+    public int[] columnIndices();
+
+}
\ No newline at end of file

Added: hama/trunk/ml/src/main/java/org/apache/hama/ml/math/BooleanVector.java
URL: http://svn.apache.org/viewvc/hama/trunk/ml/src/main/java/org/apache/hama/ml/math/BooleanVector.java?rev=1359570&view=auto
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/math/BooleanVector.java (added)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/math/BooleanVector.java Tue Jul 10 10:11:34 2012
@@ -0,0 +1,88 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama.ml.math;
+
+import java.util.Iterator;
+
+/**
+ * Vector consisting of booleans.
+ */
+public interface BooleanVector {
+
+    /**
+     * Gets the boolean at the given index.
+     */
+    public boolean get(int index);
+
+    /**
+     * Returns the length of this vector.
+     */
+    public int getLength();
+
+    /**
+     * Returns an array representation of the inner values.
+     */
+    public boolean[] toArray();
+
+    /**
+     * Iterates over the not-false elements in this vector.
+     */
+    public Iterator<BooleanVectorElement> iterateNonZero();
+
+    /**
+     * Element class for iterating.
+     */
+    public static final class BooleanVectorElement {
+
+        private int index;
+        private boolean value;
+
+        public BooleanVectorElement() {
+            super();
+        }
+
+        public BooleanVectorElement(int index, boolean value) {
+            super();
+            this.index = index;
+            this.value = value;
+        }
+
+        /**
+         * @return the index of the current element.
+         */
+        public final int getIndex() {
+            return index;
+        }
+
+        /**
+         * @return the value of the vector at the current index.
+         */
+        public final boolean getValue() {
+            return value;
+        }
+
+        public final void setIndex(int in) {
+            this.index = in;
+        }
+
+        public final void setValue(boolean in) {
+            this.value = in;
+        }
+    }
+
+}
\ No newline at end of file

Added: hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseBooleanMatrix.java
URL: http://svn.apache.org/viewvc/hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseBooleanMatrix.java?rev=1359570&view=auto
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseBooleanMatrix.java (added)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseBooleanMatrix.java Tue Jul 10 10:11:34 2012
@@ -0,0 +1,216 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama.ml.math;
+
+import java.util.Arrays;
+import java.util.Random;
+
+/**
+ * A dense implementation of {@link BooleanMatrix}, internal representated as a
+ * two-dimensional boolean array.
+ */
+public final class DenseBooleanMatrix implements BooleanMatrix {
+
+    protected final boolean[][] matrix;
+    protected final int numRows;
+    protected final int numColumns;
+
+    /**
+     * Creates a new empty matrix of booleans with the number of rows and columns.
+     *
+     * @param rows the number of rows.
+     * @param columns the number of columns.
+     */
+    public DenseBooleanMatrix(int rows, int columns) {
+        this.numRows = rows;
+        this.numColumns = columns;
+        this.matrix = new boolean[rows][columns];
+    }
+
+    /**
+     * Creates a new empty matrix of booleans with the number of rows and columns
+     * with a defaultvalue.
+     *
+     * @param rows the number of rows.
+     * @param columns the number of columns.
+     * @param defaultValue default value.
+     */
+    public DenseBooleanMatrix(int rows, int columns, boolean defaultValue) {
+        this.numRows = rows;
+        this.numColumns = columns;
+        this.matrix = new boolean[rows][columns];
+
+        for (int i = 0; i < numRows; i++) {
+            Arrays.fill(matrix[i], defaultValue);
+        }
+    }
+
+    /**
+     * Creates a new empty matrix of booleans with the number of rows and columns
+     * with a random instance to fill the values.
+     *
+     * @param rows the number of rows.
+     * @param columns the number of columns.
+     * @param rand the random instance to use to fill this matrix.
+     */
+    public DenseBooleanMatrix(int rows, int columns, Random rand) {
+        this.numRows = rows;
+        this.numColumns = columns;
+        this.matrix = new boolean[rows][columns];
+
+        for (int i = 0; i < numRows; i++) {
+            for (int j = 0; j < numColumns; j++) {
+                matrix[i][j] = rand.nextBoolean();
+            }
+        }
+    }
+
+    /**
+     * Copy constructor, does make a shallow copy.
+     *
+     * @param otherMatrix a two dimensional other matrix.
+     */
+    public DenseBooleanMatrix(boolean[][] otherMatrix) {
+        this.matrix = otherMatrix;
+        this.numRows = otherMatrix.length;
+        if (matrix.length > 0)
+            this.numColumns = matrix[0].length;
+        else
+            this.numColumns = numRows;
+    }
+
+    /*
+    * (non-Javadoc)
+    * @see de.jungblut.math.BooleanMatrix#get(int, int)
+    */
+    @Override
+    public final boolean get(int row, int col) {
+        return this.matrix[row][col];
+    }
+
+    /**
+     * Get a whole column of the matrix as a boolean array.
+     */
+    public final boolean[] getColumn(int col) {
+        final boolean[] column = new boolean[numRows];
+        for (int r = 0; r < numRows; r++) {
+            column[r] = matrix[r][col];
+        }
+        return column;
+    }
+
+    /*
+    * (non-Javadoc)
+    * @see de.jungblut.math.BooleanMatrix#getColumnCount()
+    */
+    @Override
+    public final int getColumnCount() {
+        return numColumns;
+    }
+
+    /*
+    * (non-Javadoc)
+    * @see de.jungblut.math.BooleanMatrix#getColumnVector(int)
+    */
+    @Override
+    public final BooleanVector getColumnVector(int col) {
+        return new DenseBooleanVector(getColumn(col));
+    }
+
+    /**
+     * Get the matrix as 2-dimensional integer array (first index is the row,
+     * second the column) to faster access the values.
+     */
+    public final boolean[][] getValues() {
+        return matrix;
+    }
+
+    /**
+     * Get a single row of the matrix as an integer array.
+     */
+    public final boolean[] getRow(int row) {
+        return matrix[row];
+    }
+
+    /*
+    * (non-Javadoc)
+    * @see de.jungblut.math.BooleanMatrix#getRowCount()
+    */
+    @Override
+    public final int getRowCount() {
+        return numRows;
+    }
+
+    /*
+    * (non-Javadoc)
+    * @see de.jungblut.math.BooleanMatrix#getRowVector(int)
+    */
+    @Override
+    public final BooleanVector getRowVector(int row) {
+        return new DenseBooleanVector(getRow(row));
+    }
+
+    /*
+    * (non-Javadoc)
+    * @see de.jungblut.math.BooleanMatrix#set(int, int, boolean)
+    */
+    @Override
+    public final void set(int row, int col, boolean value) {
+        this.matrix[row][col] = value;
+    }
+
+    /*
+    * (non-Javadoc)
+    * @see de.jungblut.math.BooleanMatrix#transpose()
+    */
+    @Override
+    public BooleanMatrix transpose() {
+        DenseBooleanMatrix m = new DenseBooleanMatrix(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;
+    }
+
+    /**
+     * Returns the size of the matrix as string (ROWSxCOLUMNS).
+     */
+    public String sizeToString() {
+        return numRows + "x" + numColumns;
+    }
+
+    @Override
+    public String toString() {
+        return Arrays.deepToString(matrix);
+    }
+
+    /*
+    * (non-Javadoc)
+    * @see de.jungblut.math.BooleanMatrix#columnIndices()
+    */
+    @Override
+    public int[] columnIndices() {
+        int[] x = new int[getColumnCount()];
+        for (int i = 0; i < getColumnCount(); i++)
+            x[i] = i;
+        return x;
+    }
+
+}
\ No newline at end of file

Added: hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseBooleanVector.java
URL: http://svn.apache.org/viewvc/hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseBooleanVector.java?rev=1359570&view=auto
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseBooleanVector.java (added)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseBooleanVector.java Tue Jul 10 10:11:34 2012
@@ -0,0 +1,123 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama.ml.math;
+
+import java.util.Arrays;
+import java.util.Iterator;
+
+import com.google.common.collect.AbstractIterator;
+
+/**
+ * Implementation of dense boolean vectors, internally represented as an array
+ * of booleans.
+ */
+public final class DenseBooleanVector implements BooleanVector {
+
+    private final boolean[] vector;
+
+    /**
+     * Vector contructor out of a given boolean array. Be aware that the
+     * references are just bend over, so there is no deep copy happening here.
+     *
+     * @param arr a boolean array.
+     */
+    public DenseBooleanVector(boolean[] arr) {
+        // normally, we should make a deep copy
+        this.vector = arr;
+    }
+
+    /*
+    * (non-Javadoc)
+    * @see de.jungblut.math.BooleanVector#get(int)
+    */
+    @Override
+    public final boolean get(int index) {
+        return vector[index];
+    }
+
+    /*
+    * (non-Javadoc)
+    * @see de.jungblut.math.BooleanVector#getLength()
+    */
+    @Override
+    public final int getLength() {
+        return vector.length;
+    }
+
+    /**
+     * Internal method to set values at a given index to the given value.
+     *
+     * @param index the given index.
+     * @param value the given value.
+     */
+    final void set(int index, boolean value) {
+        vector[index] = value;
+    }
+
+    /*
+    * (non-Javadoc)
+    * @see de.jungblut.math.BooleanVector#toArray()
+    */
+    @Override
+    public final boolean[] toArray() {
+        return vector;
+    }
+
+    @Override
+    public final String toString() {
+        return Arrays.toString(vector);
+    }
+
+    /*
+    * (non-Javadoc)
+    * @see de.jungblut.math.BooleanVector#iterateNonZero()
+    */
+    @Override
+    public Iterator<BooleanVectorElement> iterateNonZero() {
+        return new NonZeroIterator();
+    }
+
+    /**
+     * Not-false iterator class.
+     *
+     */
+    private final class NonZeroIterator extends
+            AbstractIterator<BooleanVectorElement> {
+
+        private final BooleanVectorElement element = new BooleanVectorElement();
+        private final boolean[] array;
+        private int currentIndex = 0;
+
+        private NonZeroIterator() {
+            this.array = vector;
+        }
+
+        @Override
+        protected final BooleanVectorElement computeNext() {
+            while (!array[currentIndex]) {
+                currentIndex++;
+                if (currentIndex >= array.length)
+                    return endOfData();
+            }
+            element.setIndex(currentIndex);
+            element.setValue(array[currentIndex]);
+            return element;
+        }
+    }
+
+}
\ No newline at end of file

Added: 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=1359570&view=auto
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseDoubleMatrix.java (added)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseDoubleMatrix.java Tue Jul 10 10:11:34 2012
@@ -0,0 +1,816 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama.ml.math;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Random;
+
+/**
+ * Dense double matrix implementation, internally uses two dimensional double
+ * arrays.
+ */
+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];
+
+        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

Added: 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=1359570&view=auto
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseDoubleVector.java (added)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DenseDoubleVector.java Tue Jul 10 10:11:34 2012
@@ -0,0 +1,631 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama.ml.math;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+
+import com.google.common.collect.AbstractIterator;
+
+/**
+ * Dense double vector implementation.
+ */
+public final class DenseDoubleVector implements DoubleVector {
+
+    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;
+    }
+
+}
\ No newline at end of file

Added: 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=1359570&view=auto
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleDoubleVectorFunction.java (added)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleDoubleVectorFunction.java Tue Jul 10 10:11:34 2012
@@ -0,0 +1,32 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama.ml.math;
+
+/**
+ * A function that can be applied to two double vectors via {@link DoubleVector}
+ * #apply({@link DoubleVector} v, {@link DoubleDoubleVectorFunction} f);
+ */
+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);
+
+}
\ No newline at end of file

Added: 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=1359570&view=auto
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleMatrix.java (added)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleMatrix.java Tue Jul 10 10:11:34 2012
@@ -0,0 +1,199 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama.ml.math;
+
+/**
+ * Standard matrix interface for double elements. Every implementation should
+ * return a fresh new Matrix when operating with other elements.
+ */
+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);
+
+}
\ No newline at end of file

Added: hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVector.java
URL: http://svn.apache.org/viewvc/hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVector.java?rev=1359570&view=auto
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVector.java (added)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVector.java Tue Jul 10 10:11:34 2012
@@ -0,0 +1,293 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama.ml.math;
+
+import java.util.Iterator;
+
+/**
+ * Vector with doubles. Some of the operations are mutable, unlike the apply and
+ * math functions, they return a fresh instance every time.
+ *
+ */
+public interface DoubleVector {
+
+    /**
+     * Retrieves the value at given index.
+     *
+     * @param index the index.
+     * @return a double value at the index.
+     */
+    public double get(int index);
+
+    /**
+     * Get the length of a vector, for sparse instance it is the actual length.
+     * (not the dimension!) Always a constant time operation.
+     *
+     * @return the length of the vector.
+     */
+    public int getLength();
+
+    /**
+     * Get the dimension of a vector, for dense instance it is the same like the
+     * length, for sparse instances it is usually not the same. Always a constant
+     * time operation.
+     *
+     * @return the dimension of the vector.
+     */
+    public int getDimension();
+
+    /**
+     * Set a value at the given index.
+     *
+     * @param index the index of the vector to set.
+     * @param value the value at the index of the vector to set.
+     */
+    public void set(int index, double value);
+
+    /**
+     * Apply a given {@link DoubleVectorFunction} to this vector and return a new
+     * one.
+     *
+     * @param func the function to apply.
+     * @return a new vector with the applied function.
+     */
+    public DoubleVector apply(DoubleVectorFunction func);
+
+    /**
+     * Apply a given {@link DoubleDoubleVectorFunction} to this vector and the
+     * other given vector.
+     *
+     * @param other the other vector.
+     * @param func the function to apply on this and the other vector.
+     * @return a new vector with the result of the function of the two vectors.
+     */
+    public DoubleVector apply(DoubleVector other, DoubleDoubleVectorFunction func);
+
+    /**
+     * Adds the given {@link DoubleVector} to this vector.
+     *
+     * @param v the other vector.
+     * @return a new vector with the sum of both vectors at each element index.
+     */
+    public DoubleVector add(DoubleVector v);
+
+    /**
+     * Adds the given scalar to this vector.
+     *
+     * @param scalar the scalar.
+     * @return a new vector with the result at each element index.
+     */
+    public DoubleVector add(double scalar);
+
+    /**
+     * Subtracts this vector by the given {@link DoubleVector}.
+     *
+     * @param v the other vector.
+     * @return a new vector with the difference of both vectors.
+     */
+    public DoubleVector subtract(DoubleVector v);
+
+    /**
+     * Subtracts the given scalar to this vector. (vector - scalar).
+     *
+     * @param scalar the scalar.
+     * @return a new vector with the result at each element index.
+     */
+    public DoubleVector subtract(double scalar);
+
+    /**
+     * Subtracts the given scalar from this vector. (scalar - vector).
+     *
+     * @param scalar the scalar.
+     * @return a new vector with the result at each element index.
+     */
+    public DoubleVector subtractFrom(double scalar);
+
+    /**
+     * Multiplies the given scalar to this vector.
+     *
+     * @param scalar the scalar.
+     * @return a new vector with the result of the operation.
+     */
+    public DoubleVector multiply(double scalar);
+
+    /**
+     * Multiplies the given {@link DoubleVector} with this vector.
+     *
+     * @param vector the other vector.
+     * @return a new vector with the result of the operation.
+     */
+    public DoubleVector multiply(DoubleVector vector);
+
+    /**
+     * Divides this vector by the given scalar. (= vector/scalar).
+     *
+     * @param scalar the given scalar.
+     * @return a new vector with the result of the operation.
+     */
+    public DoubleVector divide(double scalar);
+
+    /**
+     * Divides the given scalar by this vector. (= scalar/vector).
+     *
+     * @param scalar the given scalar.
+     * @return a new vector with the result of the operation.
+     */
+    public DoubleVector divideFrom(double scalar);
+
+    /**
+     * Powers this vector by the given amount. (=vector^x).
+     *
+     * @param x the given exponent.
+     * @return a new vector with the result of the operation.
+     */
+    public DoubleVector pow(int x);
+
+    /**
+     * Absolutes the vector at each element.
+     *
+     * @return a new vector that does not contain negative values anymore.
+     */
+    public DoubleVector abs();
+
+    /**
+     * Square-roots each element.
+     *
+     * @return a new vector.
+     */
+    public DoubleVector sqrt();
+
+    /**
+     * @return the sum of all elements in this vector.
+     */
+    public double sum();
+
+    /**
+     * Calculates the dot product between this vector and the given vector.
+     *
+     * @param s the given vector s.
+     * @return the dot product as a double.
+     */
+    public double dot(DoubleVector s);
+
+    /**
+     * Slices this vector from index 0 to the given length.
+     *
+     * @param length must be > 0 and smaller than the dimension of the vector.
+     * @return a new vector that is only length long.
+     */
+    public DoubleVector slice(int length);
+
+    /**
+     * Slices this vector from index offset with the given length. So you end at
+     * the upper bound of (offset+length).
+     *
+     * @param offset must be > 0 and smaller than the dimension of the vector
+     * @param length must be > 0 and smaller than the dimension of the vector.
+     *          This must be greater than the offset.
+     * @return a new vector that is only (length) long.
+     */
+    public DoubleVector slice(int offset, int length);
+
+    /**
+     * @return the maximum element value in this vector.
+     */
+    public double max();
+
+    /**
+     * @return the minimum element value in this vector.
+     */
+    public double min();
+
+    /**
+     * @return an array representation of this vector.
+     */
+    public double[] toArray();
+
+    /**
+     * @return a fresh new copy of this vector, copies all elements to a new
+     *         vector. (Does not reuse references or stuff).
+     */
+    public DoubleVector deepCopy();
+
+    /**
+     * @return an iterator that only iterates over non zero elements.
+     */
+    public Iterator<DoubleVectorElement> iterateNonZero();
+
+    /**
+     * @return an iterator that iterates over all elements.
+     */
+    public Iterator<DoubleVectorElement> iterate();
+
+    /**
+     * @return true if this instance is a sparse vector. Smarter and faster than
+     *         instanceof.
+     */
+    public boolean isSparse();
+
+    /**
+     * @return true if this instance is a named vector.Smarter and faster than
+     *         instanceof.
+     */
+    public boolean isNamed();
+
+    /**
+     * @return If this vector is a named instance, this will return its name. Or
+     *         null if this is not a named instance.
+     *
+     */
+    public String getName();
+
+    /**
+     * Class for iteration of elements, consists of an index and a value at this
+     * index. Can be reused for performance purposes.
+     */
+    public static final class DoubleVectorElement {
+
+        private int index;
+        private double value;
+
+        public DoubleVectorElement() {
+            super();
+        }
+
+        public DoubleVectorElement(int index, double value) {
+            super();
+            this.index = index;
+            this.value = value;
+        }
+
+        public final int getIndex() {
+            return index;
+        }
+
+        public final double getValue() {
+            return value;
+        }
+
+        public final void setIndex(int in) {
+            this.index = in;
+        }
+
+        public final void setValue(double in) {
+            this.value = in;
+        }
+    }
+
+}
\ No newline at end of file

Added: hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVectorFunction.java
URL: http://svn.apache.org/viewvc/hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVectorFunction.java?rev=1359570&view=auto
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVectorFunction.java (added)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/math/DoubleVectorFunction.java Tue Jul 10 10:11:34 2012
@@ -0,0 +1,31 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama.ml.math;
+
+/**
+ * A function that can be applied to a double vector via {@link DoubleVector}
+ * #apply({@link DoubleVectorFunction} f);
+ */
+public interface DoubleVectorFunction {
+
+    /**
+     * Calculates the result with a given index and value of a vector.
+     */
+    public double calculate(int index, double value);
+
+}
\ No newline at end of file

Added: hama/trunk/ml/src/main/java/org/apache/hama/ml/math/Tuple.java
URL: http://svn.apache.org/viewvc/hama/trunk/ml/src/main/java/org/apache/hama/ml/math/Tuple.java?rev=1359570&view=auto
==============================================================================
--- hama/trunk/ml/src/main/java/org/apache/hama/ml/math/Tuple.java (added)
+++ hama/trunk/ml/src/main/java/org/apache/hama/ml/math/Tuple.java Tue Jul 10 10:11:34 2012
@@ -0,0 +1,85 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama.ml.math;
+
+/**
+ * Tuple class to hold two generic attributes. This class implements hashcode,
+ * equals and comparable via the first element.
+ */
+public final class Tuple<FIRST, SECOND> implements
+        Comparable<Tuple<FIRST, SECOND>> {
+
+    private final FIRST first;
+    private final SECOND second;
+
+    public Tuple(FIRST first, SECOND second) {
+        super();
+        this.first = first;
+        this.second = second;
+    }
+
+    public final FIRST getFirst() {
+        return first;
+    }
+
+    public final SECOND getSecond() {
+        return second;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((first == null) ? 0 : first.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        @SuppressWarnings("rawtypes")
+        Tuple other = (Tuple) obj;
+        if (first == null) {
+            if (other.first != null)
+                return false;
+        } else if (!first.equals(other.first))
+            return false;
+        return true;
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public int compareTo(Tuple<FIRST, SECOND> o) {
+        if (o.getFirst() instanceof Comparable && getFirst() instanceof Comparable) {
+            return ((Comparable<FIRST>) getFirst()).compareTo(o.getFirst());
+        } else {
+            return 0;
+        }
+    }
+
+    @Override
+    public String toString() {
+        return "Tuple [first=" + first + ", second=" + second + "]";
+    }
+
+}
\ No newline at end of file



Mime
View raw message