You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2014/12/15 13:51:26 UTC
[1/2] [math] MATH-1180: Sequence of integers
Repository: commons-math
Updated Branches:
refs/heads/master dabc5ab66 -> 7fb571b16
MATH-1180: Sequence of integers
Added to "MathArrays": method to create an array containing a sequence of integers.
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/de73ad31
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/de73ad31
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/de73ad31
Branch: refs/heads/master
Commit: de73ad3115c19d5f0771754d233d74055a2a10bc
Parents: dabc5ab
Author: Gilles <er...@apache.org>
Authored: Mon Dec 15 13:40:04 2014 +0100
Committer: Gilles <er...@apache.org>
Committed: Mon Dec 15 13:40:04 2014 +0100
----------------------------------------------------------------------
src/changes/changes.xml | 3 +++
.../apache/commons/math3/util/MathArrays.java | 24 +++++++++++++++++---
.../commons/math3/util/MathArraysTest.java | 19 ++++++++++++++++
3 files changed, 43 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/de73ad31/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index cbf1eb8..e41d4dd 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -79,6 +79,9 @@ Users are encouraged to upgrade to this version as this release not
2. A few methods in the FastMath class are in fact slower that their
counterpart in either Math or StrictMath (cf. MATH-740 and MATH-901).
">
+ <action dev="erans" type="add" issue="MATH-1180">
+ Method to create a sequence of integers (in "o.a.c.m.util.MathArrays").
+ </action>
<action dev="erans" type="add" issue="MATH-1172">
New class "SimpleCurveFitter": Boiler-plate code to allow fitting of
a user-defined parametric function.
http://git-wip-us.apache.org/repos/asf/commons-math/blob/de73ad31/src/main/java/org/apache/commons/math3/util/MathArrays.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/util/MathArrays.java b/src/main/java/org/apache/commons/math3/util/MathArrays.java
index 72f30be..8d515c6 100644
--- a/src/main/java/org/apache/commons/math3/util/MathArrays.java
+++ b/src/main/java/org/apache/commons/math3/util/MathArrays.java
@@ -1573,9 +1573,27 @@ public class MathArrays {
* 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 sequence(n, 0, 1);
+ }
+ /**
+ * Returns an array of {@code size} integers starting at {@code start},
+ * skipping {@code stride} numbers.
+ *
+ * @param size Natural number.
+ * @param start Natural number.
+ * @param stride Natural number.
+ * @return an array whose entries are the numbers
+ * {@code start, start + stride, ..., start + (size - 1) * stride}.
+ * If {@code n == 0}, the returned array is empty.
+ *
+ * @since 3.4
+ */
+ public static int[] sequence(int size,
+ int start,
+ int stride) {
+ final int[] a = new int[size];
+ for (int i = 0; i < size; i++) {
+ a[i] = start + i * stride;
}
return a;
}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/de73ad31/src/test/java/org/apache/commons/math3/util/MathArraysTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math3/util/MathArraysTest.java b/src/test/java/org/apache/commons/math3/util/MathArraysTest.java
index b47d408..e4918ce 100644
--- a/src/test/java/org/apache/commons/math3/util/MathArraysTest.java
+++ b/src/test/java/org/apache/commons/math3/util/MathArraysTest.java
@@ -1064,6 +1064,25 @@ public class MathArraysTest {
final int[] natural = MathArrays.natural(0);
Assert.assertEquals(0, natural.length);
}
+
+ @Test
+ public void testSequence() {
+ final int size = 4;
+ final int start = 5;
+ final int stride = 2;
+ final int[] expected = {5, 7, 9, 11};
+
+ final int[] seq = MathArrays.sequence(size, start, stride);
+ for (int i = 0; i < size; i++) {
+ Assert.assertEquals(expected[i], seq[i]);
+ }
+ }
+
+ @Test
+ public void testSequenceZero() {
+ final int[] seq = MathArrays.sequence(0, 12345, 6789);
+ Assert.assertEquals(0, seq.length);
+ }
@Test
public void testVerifyValuesPositive() {
[2/2] [math] Javadoc
Posted by er...@apache.org.
Javadoc
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/7fb571b1
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/7fb571b1
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/7fb571b1
Branch: refs/heads/master
Commit: 7fb571b16cc4a0912e8d74609941563c79fe9e97
Parents: de73ad3
Author: Gilles <er...@apache.org>
Authored: Mon Dec 15 13:48:48 2014 +0100
Committer: Gilles <er...@apache.org>
Committed: Mon Dec 15 13:48:48 2014 +0100
----------------------------------------------------------------------
src/main/java/org/apache/commons/math3/util/MathArrays.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/7fb571b1/src/main/java/org/apache/commons/math3/util/MathArrays.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/util/MathArrays.java b/src/main/java/org/apache/commons/math3/util/MathArrays.java
index 8d515c6..a9e11b9 100644
--- a/src/main/java/org/apache/commons/math3/util/MathArrays.java
+++ b/src/main/java/org/apache/commons/math3/util/MathArrays.java
@@ -1584,7 +1584,7 @@ public class MathArrays {
* @param stride Natural number.
* @return an array whose entries are the numbers
* {@code start, start + stride, ..., start + (size - 1) * stride}.
- * If {@code n == 0}, the returned array is empty.
+ * If {@code size == 0}, the returned array is empty.
*
* @since 3.4
*/