hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From millec...@apache.org
Subject svn commit: r1537427 [1/3] - in /hama/trunk: ./ commons/ commons/src/ commons/src/main/ commons/src/main/java/ commons/src/main/java/org/ commons/src/main/java/org/apache/ commons/src/main/java/org/apache/hama/ commons/src/main/java/org/apache/hama/com...
Date Thu, 31 Oct 2013 10:32:41 GMT
Author: millecker
Date: Thu Oct 31 10:32:38 2013
New Revision: 1537427

URL: http://svn.apache.org/r1537427
Log:
HAMA-699: Add commons module

Added:
    hama/trunk/commons/   (with props)
    hama/trunk/commons/pom.xml
    hama/trunk/commons/src/
    hama/trunk/commons/src/main/
    hama/trunk/commons/src/main/java/
    hama/trunk/commons/src/main/java/org/
    hama/trunk/commons/src/main/java/org/apache/
    hama/trunk/commons/src/main/java/org/apache/hama/
    hama/trunk/commons/src/main/java/org/apache/hama/commons/
    hama/trunk/commons/src/main/java/org/apache/hama/commons/io/
    hama/trunk/commons/src/main/java/org/apache/hama/commons/io/DenseVectorWritable.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/io/MatrixWritable.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/io/SparseVectorWritable.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/io/StringArrayWritable.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/io/TextArrayWritable.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/io/VectorWritable.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/math/
    hama/trunk/commons/src/main/java/org/apache/hama/commons/math/CrossEntropy.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DenseDoubleMatrix.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DenseDoubleVector.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DoubleDoubleFunction.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DoubleDoubleVectorFunction.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DoubleFunction.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DoubleMatrix.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DoubleVector.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DoubleVectorFunction.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/math/Function.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/math/FunctionFactory.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/math/IdentityFunction.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/math/Sigmoid.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/math/SquaredError.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/math/Tanh.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/math/Tuple.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/util/
    hama/trunk/commons/src/main/java/org/apache/hama/commons/util/KeyValuePair.java
    hama/trunk/commons/src/main/java/org/apache/hama/commons/util/TextPair.java
    hama/trunk/commons/src/test/
    hama/trunk/commons/src/test/java/
    hama/trunk/commons/src/test/java/org/
    hama/trunk/commons/src/test/java/org/apache/
    hama/trunk/commons/src/test/java/org/apache/hama/
    hama/trunk/commons/src/test/java/org/apache/hama/commons/
    hama/trunk/commons/src/test/java/org/apache/hama/commons/math/
    hama/trunk/commons/src/test/java/org/apache/hama/commons/math/TestDenseDoubleMatrix.java
    hama/trunk/commons/src/test/java/org/apache/hama/commons/math/TestDenseDoubleVector.java
    hama/trunk/commons/src/test/java/org/apache/hama/commons/math/TestFunctionFactory.java
Removed:
    hama/trunk/core/src/main/java/org/apache/hama/bsp/TextArrayWritable.java
    hama/trunk/core/src/main/java/org/apache/hama/util/KeyValuePair.java
    hama/trunk/core/src/main/java/org/apache/hama/util/StringArrayWritable.java
    hama/trunk/examples/src/main/java/org/apache/hama/examples/util/DenseVectorWritable.java
    hama/trunk/examples/src/main/java/org/apache/hama/examples/util/SparseVectorWritable.java
    hama/trunk/examples/src/main/java/org/apache/hama/examples/util/TextPair.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/math/
    hama/trunk/ml/src/main/java/org/apache/hama/ml/writable/
    hama/trunk/ml/src/test/java/org/apache/hama/ml/math/
Modified:
    hama/trunk/CHANGES.txt
    hama/trunk/core/pom.xml
    hama/trunk/core/src/main/java/org/apache/hama/bsp/BSPPeer.java
    hama/trunk/core/src/main/java/org/apache/hama/bsp/BSPPeerImpl.java
    hama/trunk/core/src/main/java/org/apache/hama/bsp/PartitioningRunner.java
    hama/trunk/core/src/main/java/org/apache/hama/pipes/protocol/StreamingProtocol.java
    hama/trunk/core/src/main/java/org/apache/hama/pipes/protocol/UplinkReader.java
    hama/trunk/core/src/test/java/org/apache/hama/bsp/TestCheckpoint.java
    hama/trunk/core/src/test/java/org/apache/hama/bsp/TestKeyValueTextInputFormat.java
    hama/trunk/core/src/test/java/org/apache/hama/bsp/TestPartitioning.java
    hama/trunk/examples/pom.xml
    hama/trunk/examples/src/main/java/org/apache/hama/examples/BipartiteMatching.java
    hama/trunk/examples/src/main/java/org/apache/hama/examples/GradientDescentExample.java
    hama/trunk/examples/src/main/java/org/apache/hama/examples/InlinkCount.java
    hama/trunk/examples/src/main/java/org/apache/hama/examples/Kmeans.java
    hama/trunk/examples/src/main/java/org/apache/hama/examples/NeuralNetwork.java
    hama/trunk/examples/src/main/java/org/apache/hama/examples/PageRank.java
    hama/trunk/examples/src/main/java/org/apache/hama/examples/SpMV.java
    hama/trunk/examples/src/main/java/org/apache/hama/examples/util/FastGraphGen.java
    hama/trunk/examples/src/main/java/org/apache/hama/examples/util/SymmetricMatrixGen.java
    hama/trunk/examples/src/test/java/org/apache/hama/examples/BipartiteMatchingTest.java
    hama/trunk/examples/src/test/java/org/apache/hama/examples/FastGraphGenTest.java
    hama/trunk/examples/src/test/java/org/apache/hama/examples/NeuralNetworkTest.java
    hama/trunk/examples/src/test/java/org/apache/hama/examples/SpMVTest.java
    hama/trunk/examples/src/test/java/org/apache/hama/examples/SymmetricMatrixGenTest.java
    hama/trunk/graph/pom.xml
    hama/trunk/graph/src/main/java/org/apache/hama/graph/GraphJobRunner.java
    hama/trunk/graph/src/main/java/org/apache/hama/graph/VertexInputReader.java
    hama/trunk/graph/src/test/java/org/apache/hama/graph/TestSubmitGraphJob.java
    hama/trunk/graph/src/test/java/org/apache/hama/graph/example/PageRank.java
    hama/trunk/ml/pom.xml
    hama/trunk/ml/src/main/java/org/apache/hama/ml/ann/AbstractLayeredNeuralNetwork.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/ann/AutoEncoder.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/ann/NeuralNetworkTrainer.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/ann/SmallLayeredNeuralNetwork.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/ann/SmallLayeredNeuralNetworkMessage.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/ann/SmallLayeredNeuralNetworkTrainer.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/distance/CosineDistance.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/distance/DistanceMeasurer.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/distance/EuclidianDistance.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/kmeans/CenterMessage.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/kmeans/KMeansBSP.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/perception/MultiLayerPerceptron.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/perception/PerceptronTrainer.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/perception/SmallMLPMessage.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/perception/SmallMLPTrainer.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/perception/SmallMultiLayerPerceptron.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/regression/CostFunction.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/regression/GradientDescentBSP.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/regression/HypothesisFunction.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/regression/LinearRegression.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/regression/LinearRegressionModel.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/regression/LogisticRegression.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/regression/LogisticRegressionModel.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/regression/RegressionModel.java
    hama/trunk/ml/src/main/java/org/apache/hama/ml/regression/VectorDoubleFileInputFormat.java
    hama/trunk/ml/src/test/java/org/apache/hama/ml/ann/TestAutoEncoder.java
    hama/trunk/ml/src/test/java/org/apache/hama/ml/ann/TestSmallLayeredNeuralNetwork.java
    hama/trunk/ml/src/test/java/org/apache/hama/ml/ann/TestSmallLayeredNeuralNetworkMessage.java
    hama/trunk/ml/src/test/java/org/apache/hama/ml/kmeans/TestKMeansBSP.java
    hama/trunk/ml/src/test/java/org/apache/hama/ml/perception/TestSmallMLPMessage.java
    hama/trunk/ml/src/test/java/org/apache/hama/ml/perception/TestSmallMultiLayerPerceptron.java
    hama/trunk/ml/src/test/java/org/apache/hama/ml/regression/LinearRegressionModelTest.java
    hama/trunk/ml/src/test/java/org/apache/hama/ml/regression/LogisticRegressionModelTest.java
    hama/trunk/ml/src/test/java/org/apache/hama/ml/regression/TestLinearRegression.java
    hama/trunk/ml/src/test/java/org/apache/hama/ml/regression/TestLogisticRegression.java
    hama/trunk/ml/src/test/java/org/apache/hama/ml/regression/VectorDoubleFileInputFormatTest.java
    hama/trunk/pom.xml
    hama/trunk/src/assemble/bin.xml

Modified: hama/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hama/trunk/CHANGES.txt?rev=1537427&r1=1537426&r2=1537427&view=diff
==============================================================================
--- hama/trunk/CHANGES.txt (original)
+++ hama/trunk/CHANGES.txt Thu Oct 31 10:32:38 2013
@@ -1,6 +1,12 @@
 Hama Change Log
 
-Release 0.6.3 (unreleased changes)
+Release 0.7.0 (unreleased changes)
+
+  IMPROVEMENTS
+
+   HAMA-699: Add commons module (Martin Illecker)
+
+Release 0.6.3 - October 11, 2013
 
   NEW FEATURES
   

Propchange: hama/trunk/commons/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu Oct 31 10:32:38 2013
@@ -0,0 +1,8 @@
+*.iml
+target
+lib
+.classpath
+.project
+.settings
+logs
+docs

Added: hama/trunk/commons/pom.xml
URL: http://svn.apache.org/viewvc/hama/trunk/commons/pom.xml?rev=1537427&view=auto
==============================================================================
--- hama/trunk/commons/pom.xml (added)
+++ hama/trunk/commons/pom.xml Thu Oct 31 10:32:38 2013
@@ -0,0 +1,51 @@
+<!--
+   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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+  <parent>
+    <groupId>org.apache.hama</groupId>
+    <artifactId>hama-parent</artifactId>
+    <version>0.7.0-SNAPSHOT</version>
+  </parent>
+
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.hama</groupId>
+  <artifactId>hama-commons</artifactId>
+  <name>commons</name>
+  <version>0.7.0-SNAPSHOT</version>
+  <packaging>jar</packaging>
+
+  <dependencies>
+    <dependency>
+      <groupId>com.google.guava</groupId>
+      <artifactId>guava</artifactId>
+      <version>13.0.1</version>
+    </dependency>
+  </dependencies>
+  
+  <build>
+    <finalName>hama-commons-${project.version}</finalName>
+    <pluginManagement>
+      <plugins>
+        <plugin>
+          <artifactId>maven-surefire-plugin</artifactId>
+        </plugin>
+      </plugins>
+    </pluginManagement>
+  </build>
+</project>

Added: hama/trunk/commons/src/main/java/org/apache/hama/commons/io/DenseVectorWritable.java
URL: http://svn.apache.org/viewvc/hama/trunk/commons/src/main/java/org/apache/hama/commons/io/DenseVectorWritable.java?rev=1537427&view=auto
==============================================================================
--- hama/trunk/commons/src/main/java/org/apache/hama/commons/io/DenseVectorWritable.java (added)
+++ hama/trunk/commons/src/main/java/org/apache/hama/commons/io/DenseVectorWritable.java Thu Oct 31 10:32:38 2013
@@ -0,0 +1,87 @@
+/**
+ * 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.commons.io;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+
+import org.apache.hadoop.io.Writable;
+
+/**
+ * This class represents dense vector. It will improve memory consumption up to
+ * two times in comparison to SparseVectorWritable in case of vectors which
+ * sparsity is close to 1. Internally represents vector values as array. Can be
+ * used in SpMV for representation of input and output vector.
+ */
+public class DenseVectorWritable implements Writable {
+
+  private double values[];
+
+  public DenseVectorWritable() {
+    values = new double[0];
+  }
+
+  public int getSize() {
+    return values.length;
+  }
+
+  public void setSize(int size) {
+    values = new double[size];
+  }
+
+  public double get(int index) {
+    return values[index];
+  }
+
+  public void addCell(int index, double value) {
+    values[index] = value;
+  }
+
+  @Override
+  public void readFields(DataInput in) throws IOException {
+    int size = in.readInt();
+    int len = in.readInt();
+    setSize(size);
+    for (int i = 0; i < len; i++) {
+      int index = in.readInt();
+      double value = in.readDouble();
+      values[index] = value;
+    }
+  }
+
+  @Override
+  public void write(DataOutput out) throws IOException {
+    out.writeInt(getSize());
+    out.writeInt(getSize());
+    for (int i = 0; i < getSize(); i++) {
+      out.writeInt(i);
+      out.writeDouble(values[i]);
+    }
+  }
+
+  @Override
+  public String toString() {
+    StringBuilder st = new StringBuilder();
+    st.append(" " + getSize() + " " + getSize());
+    for (int i = 0; i < getSize(); i++)
+      st.append(" " + i + " " + values[i]);
+    return st.toString();
+  }
+
+}

Added: hama/trunk/commons/src/main/java/org/apache/hama/commons/io/MatrixWritable.java
URL: http://svn.apache.org/viewvc/hama/trunk/commons/src/main/java/org/apache/hama/commons/io/MatrixWritable.java?rev=1537427&view=auto
==============================================================================
--- hama/trunk/commons/src/main/java/org/apache/hama/commons/io/MatrixWritable.java (added)
+++ hama/trunk/commons/src/main/java/org/apache/hama/commons/io/MatrixWritable.java Thu Oct 31 10:32:38 2013
@@ -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.commons.io;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+
+import org.apache.hadoop.io.Writable;
+import org.apache.hama.commons.math.DenseDoubleMatrix;
+import org.apache.hama.commons.math.DoubleMatrix;
+
+/**
+ * Majorly designed for dense matrices, can be extended for sparse ones as well.
+ */
+public final class MatrixWritable implements Writable {
+
+  private DoubleMatrix mat;
+
+  public MatrixWritable() {
+  }
+
+  public MatrixWritable(DoubleMatrix mat) {
+    this.mat = mat;
+
+  }
+
+  @Override
+  public void readFields(DataInput in) throws IOException {
+    mat = read(in);
+  }
+
+  @Override
+  public void write(DataOutput out) throws IOException {
+    write(mat, out);
+  }
+
+  public static void write(DoubleMatrix mat, DataOutput out) throws IOException {
+    out.writeInt(mat.getRowCount());
+    out.writeInt(mat.getColumnCount());
+    for (int row = 0; row < mat.getRowCount(); row++) {
+      for (int col = 0; col < mat.getColumnCount(); col++) {
+        out.writeDouble(mat.get(row, col));
+      }
+    }
+  }
+
+  public static DoubleMatrix read(DataInput in) throws IOException {
+    DoubleMatrix mat = new DenseDoubleMatrix(in.readInt(), in.readInt());
+    for (int row = 0; row < mat.getRowCount(); row++) {
+      for (int col = 0; col < mat.getColumnCount(); col++) {
+        mat.set(row, col, in.readDouble());
+      }
+    }
+    return mat;
+  }
+
+}

Added: hama/trunk/commons/src/main/java/org/apache/hama/commons/io/SparseVectorWritable.java
URL: http://svn.apache.org/viewvc/hama/trunk/commons/src/main/java/org/apache/hama/commons/io/SparseVectorWritable.java?rev=1537427&view=auto
==============================================================================
--- hama/trunk/commons/src/main/java/org/apache/hama/commons/io/SparseVectorWritable.java (added)
+++ hama/trunk/commons/src/main/java/org/apache/hama/commons/io/SparseVectorWritable.java Thu Oct 31 10:32:38 2013
@@ -0,0 +1,105 @@
+/**
+ * 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.commons.io;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.hadoop.io.Writable;
+
+/**
+ * This class represents sparse vector. It will give improvement in memory
+ * consumption in case of vectors which sparsity is close to zero. Can be used
+ * in SpMV for representing input matrix rows efficiently. Internally represents
+ * values as list of indeces and list of values.
+ */
+public class SparseVectorWritable implements Writable {
+
+  private Integer size;
+  private List<Integer> indeces;
+  private List<Double> values;
+
+  public SparseVectorWritable() {
+    indeces = new ArrayList<Integer>();
+    values = new ArrayList<Double>();
+  }
+
+  public void clear() {
+    indeces = new ArrayList<Integer>();
+    values = new ArrayList<Double>();
+  }
+
+  public void addCell(int index, double value) {
+    indeces.add(index);
+    values.add(value);
+  }
+
+  public void setSize(int size) {
+    this.size = size;
+  }
+
+  public int getSize() {
+    if (size != null)
+      return size;
+    return indeces.size();
+  }
+
+  public List<Integer> getIndeces() {
+    return indeces;
+  }
+
+  public List<Double> getValues() {
+    return values;
+  }
+
+  @Override
+  public void readFields(DataInput in) throws IOException {
+    clear();
+    int size = in.readInt();
+    int len = in.readInt();
+    setSize(size);
+    for (int i = 0; i < len; i++) {
+      int index = in.readInt();
+      double value = in.readDouble();
+      this.addCell(index, value);
+    }
+  }
+
+  @Override
+  public void write(DataOutput out) throws IOException {
+    out.writeInt(getSize());
+    out.writeInt(indeces.size());
+    for (int i = 0; i < indeces.size(); i++) {
+      out.writeInt(indeces.get(i));
+      out.writeDouble(values.get(i));
+    }
+  }
+
+  @Override
+  public String toString() {
+    StringBuilder st = new StringBuilder();
+    st.append(" " + getSize() + " " + indeces.size());
+    for (int i = 0; i < indeces.size(); i++)
+      st.append(" " + indeces.get(i) + " " + values.get(i));
+    return st.toString();
+  }
+
+}

Added: hama/trunk/commons/src/main/java/org/apache/hama/commons/io/StringArrayWritable.java
URL: http://svn.apache.org/viewvc/hama/trunk/commons/src/main/java/org/apache/hama/commons/io/StringArrayWritable.java?rev=1537427&view=auto
==============================================================================
--- hama/trunk/commons/src/main/java/org/apache/hama/commons/io/StringArrayWritable.java (added)
+++ hama/trunk/commons/src/main/java/org/apache/hama/commons/io/StringArrayWritable.java Thu Oct 31 10:32:38 2013
@@ -0,0 +1,65 @@
+/**
+ * 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.commons.io;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+
+import org.apache.hadoop.io.Writable;
+
+/**
+ * Custom writable for string arrays, because ArrayWritable has no default
+ * constructor and is broken.
+ * 
+ */
+public class StringArrayWritable implements Writable {
+
+  private String[] array;
+
+  public StringArrayWritable() {
+    super();
+  }
+
+  public StringArrayWritable(String[] array) {
+    super();
+    this.array = array;
+  }
+
+  // no defensive copy needed because this always comes from an rpc call.
+  public String[] get() {
+    return array;
+  }
+
+  @Override
+  public void write(DataOutput out) throws IOException {
+    out.writeInt(array.length);
+    for (String s : array) {
+      out.writeUTF(s);
+    }
+  }
+
+  @Override
+  public void readFields(DataInput in) throws IOException {
+    array = new String[in.readInt()];
+    for (int i = 0; i < array.length; i++) {
+      array[i] = in.readUTF();
+    }
+  }
+
+}

Added: hama/trunk/commons/src/main/java/org/apache/hama/commons/io/TextArrayWritable.java
URL: http://svn.apache.org/viewvc/hama/trunk/commons/src/main/java/org/apache/hama/commons/io/TextArrayWritable.java?rev=1537427&view=auto
==============================================================================
--- hama/trunk/commons/src/main/java/org/apache/hama/commons/io/TextArrayWritable.java (added)
+++ hama/trunk/commons/src/main/java/org/apache/hama/commons/io/TextArrayWritable.java Thu Oct 31 10:32:38 2013
@@ -0,0 +1,29 @@
+/**
+ * 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.commons.io;
+
+import org.apache.hadoop.io.ArrayWritable;
+import org.apache.hadoop.io.Text;
+
+public class TextArrayWritable extends ArrayWritable {
+
+  public TextArrayWritable() {
+    super(Text.class);
+  }
+
+}

Added: hama/trunk/commons/src/main/java/org/apache/hama/commons/io/VectorWritable.java
URL: http://svn.apache.org/viewvc/hama/trunk/commons/src/main/java/org/apache/hama/commons/io/VectorWritable.java?rev=1537427&view=auto
==============================================================================
--- hama/trunk/commons/src/main/java/org/apache/hama/commons/io/VectorWritable.java (added)
+++ hama/trunk/commons/src/main/java/org/apache/hama/commons/io/VectorWritable.java Thu Oct 31 10:32:38 2013
@@ -0,0 +1,133 @@
+/**
+ * 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.commons.io;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+
+import org.apache.hadoop.io.WritableComparable;
+import org.apache.hama.commons.math.DenseDoubleVector;
+import org.apache.hama.commons.math.DoubleVector;
+
+/**
+ * Writable for dense vectors.
+ */
+public final class VectorWritable implements WritableComparable<VectorWritable> {
+
+  private DoubleVector vector;
+
+  public VectorWritable() {
+    super();
+  }
+
+  public VectorWritable(VectorWritable v) {
+    this.vector = v.getVector();
+  }
+
+  public VectorWritable(DoubleVector v) {
+    this.vector = v;
+  }
+
+  @Override
+  public final void write(DataOutput out) throws IOException {
+    writeVector(this.vector, out);
+  }
+
+  @Override
+  public final void readFields(DataInput in) throws IOException {
+    this.vector = readVector(in);
+  }
+
+  @Override
+  public final int compareTo(VectorWritable o) {
+    return compareVector(this, o);
+  }
+
+  @Override
+  public int hashCode() {
+    final int prime = 31;
+    int result = 1;
+    result = prime * result + ((vector == null) ? 0 : vector.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;
+    VectorWritable other = (VectorWritable) obj;
+    if (vector == null) {
+      if (other.vector != null)
+        return false;
+    } else if (!vector.equals(other.vector))
+      return false;
+    return true;
+  }
+
+  /**
+   * @return the embedded vector
+   */
+  public DoubleVector getVector() {
+    return vector;
+  }
+
+  @Override
+  public String toString() {
+    return vector.toString();
+  }
+
+  public static void writeVector(DoubleVector vector, DataOutput out)
+      throws IOException {
+    out.writeInt(vector.getLength());
+    for (int i = 0; i < vector.getDimension(); i++) {
+      out.writeDouble(vector.get(i));
+    }
+  }
+
+  public static DoubleVector readVector(DataInput in) throws IOException {
+    int length = in.readInt();
+    DoubleVector vector;
+    vector = new DenseDoubleVector(length);
+    for (int i = 0; i < length; i++) {
+      vector.set(i, in.readDouble());
+    }
+    return vector;
+  }
+
+  public static int compareVector(VectorWritable a, VectorWritable o) {
+    return compareVector(a.getVector(), o.getVector());
+  }
+
+  public static int compareVector(DoubleVector a, DoubleVector o) {
+    DoubleVector subtract = a.subtractUnsafe(o);
+    return (int) subtract.sum();
+  }
+
+  public static VectorWritable wrap(DoubleVector a) {
+    return new VectorWritable(a);
+  }
+
+  public void set(DoubleVector vector) {
+    this.vector = vector;
+  }
+}

Added: hama/trunk/commons/src/main/java/org/apache/hama/commons/math/CrossEntropy.java
URL: http://svn.apache.org/viewvc/hama/trunk/commons/src/main/java/org/apache/hama/commons/math/CrossEntropy.java?rev=1537427&view=auto
==============================================================================
--- hama/trunk/commons/src/main/java/org/apache/hama/commons/math/CrossEntropy.java (added)
+++ hama/trunk/commons/src/main/java/org/apache/hama/commons/math/CrossEntropy.java Thu Oct 31 10:32:38 2013
@@ -0,0 +1,58 @@
+/**
+ * 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.commons.math;
+
+/**
+ * The cross entropy cost function.
+ * 
+ * <pre>
+ * cost(t, y) = - t * log(y) - (1 - t) * log(1 - y),
+ * where t denotes the target value, y denotes the estimated value.
+ * </pre>
+ */
+public class CrossEntropy extends DoubleDoubleFunction {
+
+  @Override
+  public double apply(double target, double actual) {
+    double adjustedTarget = (target == 0 ? 0.000001 : target);
+    adjustedTarget = (target == 1.0 ? 0.999999 : target);
+    double adjustedActual = (actual == 0 ? 0.000001 : actual);
+    adjustedActual = (actual == 1 ? 0.999999 : actual);
+    return -adjustedTarget * Math.log(adjustedActual) - (1 - adjustedTarget)
+        * Math.log(1 - adjustedActual);
+  }
+
+  @Override
+  public double applyDerivative(double target, double actual) {
+    double adjustedTarget = target;
+    double adjustedActual = actual;
+    if (adjustedActual == 1) {
+      adjustedActual = 0.999;
+    } else if (actual == 0) {
+      adjustedActual = 0.001;
+    }
+    if (adjustedTarget == 1) {
+      adjustedTarget = 0.999;
+    } else if (adjustedTarget == 0) {
+      adjustedTarget = 0.001;
+    }
+    return -adjustedTarget / adjustedActual + (1 - adjustedTarget)
+        / (1 - adjustedActual);
+  }
+
+}

Added: hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DenseDoubleMatrix.java
URL: http://svn.apache.org/viewvc/hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DenseDoubleMatrix.java?rev=1537427&view=auto
==============================================================================
--- hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DenseDoubleMatrix.java (added)
+++ hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DenseDoubleMatrix.java Thu Oct 31 10:32:38 2013
@@ -0,0 +1,904 @@
+/**
+ * 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.commons.math;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Random;
+
+import com.google.common.base.Preconditions;
+
+/**
+ * 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#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 multiplyUnsafe(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 multiplyElementWiseUnsafe(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 multiplyVectorUnsafe(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 subtractUnsafe(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 subtractUnsafe(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 divideUnsafe(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;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public DoubleMatrix divide(DoubleVector vec) {
+    Preconditions.checkArgument(this.getColumnCount() == vec.getDimension(),
+        "Dimension mismatch.");
+    return this.divideUnsafe(vec);
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleMatrix#divide(de.jungblut.math.DoubleMatrix)
+   */
+  @Override
+  public DoubleMatrix divideUnsafe(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;
+  }
+
+  @Override
+  public DoubleMatrix divide(DoubleMatrix other) {
+    Preconditions.checkArgument(this.getRowCount() == other.getRowCount()
+        && this.getColumnCount() == other.getColumnCount());
+    return divideUnsafe(other);
+  }
+
+  /*
+   * (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.subtractUnsafe(b).sum();
+  }
+
+  @Override
+  /**
+   * {@inheritDoc}
+   */
+  public DoubleMatrix applyToElements(DoubleFunction fun) {
+    for (int r = 0; r < this.numRows; ++r) {
+      for (int c = 0; c < this.numColumns; ++c) {
+        this.set(r, c, fun.apply(this.get(r, c)));
+      }
+    }
+    return this;
+  }
+
+  @Override
+  /**
+   * {@inheritDoc}
+   */
+  public DoubleMatrix applyToElements(DoubleMatrix other,
+      DoubleDoubleFunction fun) {
+    Preconditions
+        .checkArgument(this.numRows == other.getRowCount()
+            && this.numColumns == other.getColumnCount(),
+            "Cannot apply double double function to matrices with different sizes.");
+
+    for (int r = 0; r < this.numRows; ++r) {
+      for (int c = 0; c < this.numColumns; ++c) {
+        this.set(r, c, fun.apply(this.get(r, c), other.get(r, c)));
+      }
+    }
+
+    return this;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see
+   * org.apache.hama.ml.math.DoubleMatrix#safeMultiply(org.apache.hama.ml.math
+   * .DoubleMatrix)
+   */
+  @Override
+  public DoubleMatrix multiply(DoubleMatrix other) {
+    Preconditions
+        .checkArgument(
+            this.numColumns == other.getRowCount(),
+            String
+                .format(
+                    "Matrix with size [%d, %d] cannot multiple matrix with size [%d, %d]",
+                    this.numRows, this.numColumns, other.getRowCount(),
+                    other.getColumnCount()));
+
+    return this.multiplyUnsafe(other);
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see
+   * org.apache.hama.ml.math.DoubleMatrix#safeMultiplyElementWise(org.apache
+   * .hama.ml.math.DoubleMatrix)
+   */
+  @Override
+  public DoubleMatrix multiplyElementWise(DoubleMatrix other) {
+    Preconditions.checkArgument(this.numRows == other.getRowCount()
+        && this.numColumns == other.getColumnCount(),
+        "Matrices with different dimensions cannot be multiplied elementwise.");
+    return this.multiplyElementWiseUnsafe(other);
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see
+   * org.apache.hama.ml.math.DoubleMatrix#safeMultiplyVector(org.apache.hama
+   * .ml.math.DoubleVector)
+   */
+  @Override
+  public DoubleVector multiplyVector(DoubleVector v) {
+    Preconditions.checkArgument(this.numColumns == v.getDimension(),
+        "Dimension mismatch.");
+    return this.multiplyVectorUnsafe(v);
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see org.apache.hama.ml.math.DoubleMatrix#subtract(org.apache.hama.ml.math.
+   * DoubleMatrix)
+   */
+  @Override
+  public DoubleMatrix subtract(DoubleMatrix other) {
+    Preconditions.checkArgument(this.numRows == other.getRowCount()
+        && this.numColumns == other.getColumnCount(), "Dimension mismatch.");
+    return subtractUnsafe(other);
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see org.apache.hama.ml.math.DoubleMatrix#subtract(org.apache.hama.ml.math.
+   * DoubleVector)
+   */
+  @Override
+  public DoubleMatrix subtract(DoubleVector vec) {
+    Preconditions.checkArgument(this.numColumns == vec.getDimension(),
+        "Dimension mismatch.");
+    return null;
+  }
+
+}

Added: hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DenseDoubleVector.java
URL: http://svn.apache.org/viewvc/hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DenseDoubleVector.java?rev=1537427&view=auto
==============================================================================
--- hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DenseDoubleVector.java (added)
+++ hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DenseDoubleVector.java Thu Oct 31 10:32:38 2013
@@ -0,0 +1,739 @@
+/**
+ * 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.commons.math;
+
+import java.math.BigDecimal;
+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.base.Preconditions;
+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;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public DoubleVector applyToElements(DoubleFunction func) {
+    for (int i = 0; i < vector.length; i++) {
+      this.vector[i] = func.apply(vector[i]);
+    }
+    return this;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public DoubleVector applyToElements(DoubleVector other,
+      DoubleDoubleFunction func) {
+    for (int i = 0; i < vector.length; i++) {
+      this.vector[i] = func.apply(vector[i], other.get(i));
+    }
+    return this;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#apply(de.jungblut.math.function.
+   * DoubleVectorFunction)
+   */
+  @Deprecated
+  @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)
+   */
+  @Deprecated
+  @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 addUnsafe(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 subtractUnsafe(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 multiplyUnsafe(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;
+  }
+
+  @Override
+  public DoubleVector multiply(DoubleMatrix matrix) {
+    Preconditions.checkArgument(this.vector.length == matrix.getRowCount(),
+        "Dimension mismatch when multiply a vector to a matrix.");
+    return this.multiplyUnsafe(matrix);
+  }
+
+  @Override
+  public DoubleVector multiplyUnsafe(DoubleMatrix matrix) {
+    DoubleVector vec = new DenseDoubleVector(matrix.getColumnCount());
+    for (int i = 0; i < vec.getDimension(); ++i) {
+      vec.set(i, this.multiplyUnsafe(matrix.getColumnVector(i)).sum());
+    }
+    return vec;
+  }
+
+  /*
+   * (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 dotUnsafe(DoubleVector vector) {
+    BigDecimal dotProduct = BigDecimal.valueOf(0.0d);
+    for (int i = 0; i < getLength(); i++) {
+      dotProduct = dotProduct.add(BigDecimal.valueOf(this.get(i)).multiply(BigDecimal.valueOf(vector.get(i))));
+    }
+    return dotProduct.doubleValue();
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#slice(int)
+   */
+  @Override
+  public DoubleVector slice(int length) {
+    return slice(0, length - 1);
+  }
+
+  @Override
+  public DoubleVector sliceUnsafe(int length) {
+    return sliceUnsafe(0, length - 1);
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see de.jungblut.math.DoubleVector#slice(int, int)
+   */
+  @Override
+  public DoubleVector slice(int start, int end) {
+    Preconditions.checkArgument(start >= 0 && start <= end
+        && end < vector.length, "The given from and to is invalid");
+
+    return sliceUnsafe(start, end);
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public DoubleVector sliceUnsafe(int start, int end) {
+    DoubleVector newVec = new DenseDoubleVector(end - start + 1);
+    for (int i = start, j = 0; i <= end; ++i, ++j) {
+      newVec.set(j, vector[i]);
+    }
+
+    return newVec;
+  }
+
+  /*
+   * (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;
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see org.apache.hama.ml.math.DoubleVector#safeAdd(org.apache.hama.ml.math.
+   * DoubleVector)
+   */
+  @Override
+  public DoubleVector add(DoubleVector vector) {
+    Preconditions.checkArgument(this.vector.length == vector.getDimension(),
+        "Dimensions of two vectors do not equal.");
+    return this.addUnsafe(vector);
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see
+   * org.apache.hama.ml.math.DoubleVector#safeSubtract(org.apache.hama.ml.math
+   * .DoubleVector)
+   */
+  @Override
+  public DoubleVector subtract(DoubleVector vector) {
+    Preconditions.checkArgument(this.vector.length == vector.getDimension(),
+        "Dimensions of two vectors do not equal.");
+    return this.subtractUnsafe(vector);
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see
+   * org.apache.hama.ml.math.DoubleVector#safeMultiplay(org.apache.hama.ml.math
+   * .DoubleVector)
+   */
+  @Override
+  public DoubleVector multiply(DoubleVector vector) {
+    Preconditions.checkArgument(this.vector.length == vector.getDimension(),
+        "Dimensions of two vectors do not equal.");
+    return this.multiplyUnsafe(vector);
+  }
+
+  /*
+   * (non-Javadoc)
+   * @see org.apache.hama.ml.math.DoubleVector#safeDot(org.apache.hama.ml.math.
+   * DoubleVector)
+   */
+  @Override
+  public double dot(DoubleVector vector) {
+    Preconditions.checkArgument(this.vector.length == vector.getDimension(),
+        "Dimensions of two vectors do not equal.");
+    return this.dotUnsafe(vector);
+  }
+
+}

Added: hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DoubleDoubleFunction.java
URL: http://svn.apache.org/viewvc/hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DoubleDoubleFunction.java?rev=1537427&view=auto
==============================================================================
--- hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DoubleDoubleFunction.java (added)
+++ hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DoubleDoubleFunction.java Thu Oct 31 10:32:38 2013
@@ -0,0 +1,45 @@
+/**
+ * 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.commons.math;
+
+/**
+ * A double double function takes two arguments. A vector or matrix can apply
+ * the double function to each element.
+ * 
+ */
+public abstract class DoubleDoubleFunction extends Function {
+
+  /**
+   * Apply the function to elements to two given arguments.
+   * 
+   * @param x1
+   * @param x2
+   * @return The result based on the calculation on two arguments.
+   */
+  public abstract double apply(double x1, double x2);
+
+  /**
+   * Apply the derivative of this function to two given arguments.
+   * 
+   * @param x1
+   * @param x2
+   * @return The result based on the calculation on two arguments.
+   */
+  public abstract double applyDerivative(double x1, double x2);
+
+}

Added: hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DoubleDoubleVectorFunction.java
URL: http://svn.apache.org/viewvc/hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DoubleDoubleVectorFunction.java?rev=1537427&view=auto
==============================================================================
--- hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DoubleDoubleVectorFunction.java (added)
+++ hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DoubleDoubleVectorFunction.java Thu Oct 31 10:32:38 2013
@@ -0,0 +1,35 @@
+/**
+ * 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.commons.math;
+
+/**
+ * A function that can be applied to two double vectors via {@link DoubleVector}
+ * #apply({@link DoubleVector} v, {@link DoubleDoubleVectorFunction} f);
+ * 
+ * This class will be replaced by {@link DoubleDoubleFunction}
+ */
+@Deprecated
+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);
+
+}

Added: hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DoubleFunction.java
URL: http://svn.apache.org/viewvc/hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DoubleFunction.java?rev=1537427&view=auto
==============================================================================
--- hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DoubleFunction.java (added)
+++ hama/trunk/commons/src/main/java/org/apache/hama/commons/math/DoubleFunction.java Thu Oct 31 10:32:38 2013
@@ -0,0 +1,43 @@
+/**
+ * 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.commons.math;
+
+/**
+ * A double double function takes two arguments. A vector or matrix can apply
+ * the double function to each element.
+ * 
+ */
+public abstract class DoubleFunction extends Function {
+
+  /**
+   * Apply the function to element.
+   * 
+   * @param elem The element that the function apply to.
+   * @return The result after applying the function.
+   */
+  public abstract double apply(double value);
+
+  /**
+   * Apply the gradient of the function.
+   * 
+   * @param elem
+   * @return
+   */
+  public abstract double applyDerivative(double value);
+
+}



Mime
View raw message