commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject [2/2] [math] MATH-1265
Date Fri, 04 Sep 2015 14:14:09 GMT
MATH-1265

Implement "Iterable".


Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/537a2242
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/537a2242
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/537a2242

Branch: refs/heads/MATH_3_X
Commit: 537a22428e7b900753b681f9ded432c830540975
Parents: 23f469e
Author: Gilles <erans@apache.org>
Authored: Fri Sep 4 14:39:27 2015 +0200
Committer: Gilles <erans@apache.org>
Committed: Fri Sep 4 14:39:27 2015 +0200

----------------------------------------------------------------------
 src/changes/changes.xml                         |  3 +++
 .../ml/neuralnet/twod/NeuronSquareMesh2D.java   | 12 +++++++--
 .../neuralnet/twod/NeuronSquareMesh2DTest.java  | 27 ++++++++++++++++++++
 3 files changed, 40 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/537a2242/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 88594b5..ede16f9 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -51,6 +51,9 @@ If the output is not quite correct, check for invisible trailing spaces!
   </properties>
   <body>
     <release version="3.6" date="XXXX-XX-XX" description="">
+      <action dev="erans" type="add" issue="MATH-1265">
+        "NeuronSquareMesh2D" (package "o.a.c.m.ml.neuralnet.twod") implements "Iterable".
+      </action>
       <action dev="erans" type="add" issue="MATH-1264">
         "MapUtils" (package "o.a.c.m.ml.neuralnet"): Method to sort units according to distance
         from a given vector.

http://git-wip-us.apache.org/repos/asf/commons-math/blob/537a2242/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/NeuronSquareMesh2D.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/NeuronSquareMesh2D.java
b/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/NeuronSquareMesh2D.java
index 24b70af..2f4dd2d 100644
--- a/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/NeuronSquareMesh2D.java
+++ b/src/main/java/org/apache/commons/math3/ml/neuralnet/twod/NeuronSquareMesh2D.java
@@ -19,6 +19,7 @@ package org.apache.commons.math3.ml.neuralnet.twod;
 
 import java.util.List;
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.io.Serializable;
 import java.io.ObjectInputStream;
 import org.apache.commons.math3.ml.neuralnet.Neuron;
@@ -40,7 +41,9 @@ import org.apache.commons.math3.exception.MathInternalError;
  * @see org.apache.commons.math3.ml.neuralnet.sofm
  * @since 3.3
  */
-public class NeuronSquareMesh2D implements Serializable {
+public class NeuronSquareMesh2D
+    implements Iterable<Neuron>,
+               Serializable {
     /** Serial version ID */
     private static final long serialVersionUID = 1L;
     /** Underlying network. */
@@ -194,6 +197,11 @@ public class NeuronSquareMesh2D implements Serializable {
         createLinks();
     }
 
+    /** {@inheritDoc} */
+    public Iterator<Neuron> iterator() {
+        return network.iterator();
+    }
+
     /**
      * Retrieves the underlying network.
      * A reference is returned (enabling, for example, the network to be
@@ -354,7 +362,7 @@ public class NeuronSquareMesh2D implements Serializable {
         } else {
             return new int[] { rowIndex, colIndex };
         }
-     }
+    }
 
     /**
      * Creates the neighbour relationships between neurons.

http://git-wip-us.apache.org/repos/asf/commons-math/blob/537a2242/src/test/java/org/apache/commons/math3/ml/neuralnet/twod/NeuronSquareMesh2DTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math3/ml/neuralnet/twod/NeuronSquareMesh2DTest.java
b/src/test/java/org/apache/commons/math3/ml/neuralnet/twod/NeuronSquareMesh2DTest.java
index 5799425..6cce0c9 100644
--- a/src/test/java/org/apache/commons/math3/ml/neuralnet/twod/NeuronSquareMesh2DTest.java
+++ b/src/test/java/org/apache/commons/math3/ml/neuralnet/twod/NeuronSquareMesh2DTest.java
@@ -23,6 +23,7 @@ import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.util.Collection;
+import java.util.Set;
 import java.util.HashSet;
 
 import org.apache.commons.math3.exception.NumberIsTooSmallException;
@@ -844,4 +845,30 @@ public class NeuronSquareMesh2DTest {
                                              NeuronSquareMesh2D.HorizontalDirection.RIGHT,
                                              NeuronSquareMesh2D.VerticalDirection.DOWN).getIdentifier());
     }
+
+    @Test
+    public void testIterator() {
+        final FeatureInitializer[] initArray = { init };
+        final NeuronSquareMesh2D map = new NeuronSquareMesh2D(3, true,
+                                                              3, true,
+                                                              SquareNeighbourhood.VON_NEUMANN,
+                                                              initArray);
+        final Set<Neuron> fromMap = new HashSet<Neuron>();
+        for (Neuron n : map) {
+            fromMap.add(n);
+        }
+
+        final Network net = map.getNetwork();
+        final Set<Neuron> fromNet = new HashSet<Neuron>();
+        for (Neuron n : net) {
+            fromNet.add(n);
+        }
+
+        for (Neuron n : fromMap) {
+            Assert.assertTrue(fromNet.contains(n));
+        }
+        for (Neuron n : fromNet) {
+            Assert.assertTrue(fromMap.contains(n));
+        }
+    }
 }


Mime
View raw message