Author: erans
Date: Fri Sep 6 16:05:51 2013
New Revision: 1520619
URL: http://svn.apache.org/r1520619
Log:
Created "natural" method in "MathArrays" from code previously in
class "RandomDataGenerator" (private method).
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathArraysTest.java
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.java?rev=1520619&r1=1520618&r2=1520619&view=diff
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.java
(original)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.java
Fri Sep 6 16:05:51 2013
@@ -1533,4 +1533,19 @@ public class MathArrays {
public static void shuffle(int[] list) {
shuffle(list, new Well19937c());
}
+
+ /**
+ * Returns an array representing the natural number {@code n}.
+ *
+ * @param n Natural number.
+ * @return an array whose entries are the numbers 0, 1, ..., {@code n}-1.
+ * If {@code n == 0}, the returned array is empty.
+ */
+ public static int[] natural(int n) {
+ final int[] a = new int[n];
+ for (int i = 0; i < n; i++) {
+ a[i] = i;
+ }
+ return a;
+ }
}
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathArraysTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathArraysTest.java?rev=1520619&r1=1520618&r2=1520619&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathArraysTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathArraysTest.java
Fri Sep 6 16:05:51 2013
@@ -979,4 +979,21 @@ public class MathArraysTest {
}
Assert.assertTrue(ok);
}
+
+ @Test
+ public void testNatural() {
+ final int n = 4;
+ final int[] expected = {0, 1, 2, 3};
+
+ final int[] natural = MathArrays.natural(n);
+ for (int i = 0; i < n; i++) {
+ Assert.assertEquals(expected[i], natural[i]);
+ }
+ }
+
+ @Test
+ public void testNaturalZero() {
+ final int[] natural = MathArrays.natural(0);
+ Assert.assertEquals(natural.length, 0);
+ }
}