Added array-scaling methods to MathArrays. Patch provided by Jared Becksfort. JIRA: MATH-877
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1457130 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
606fdac7df
commit
02d2d148fc
|
@ -55,6 +55,9 @@ This is a minor release: It combines bug fixes and new features.
|
|||
Changes to existing features were made in a backwards-compatible
|
||||
way such as to allow drop-in replacement of the v3.1[.1] JAR file.
|
||||
">
|
||||
<action dev="psteitz" type="add" issue="MATH-946" due-to="Jared Becksfort">
|
||||
Added array-scaling methods to MathArrays.
|
||||
</action>
|
||||
<action dev="luc" type="update" issue="MATH-877" due-to="Peter Andrews">
|
||||
Allow direct use of SummaryStatistics in one-way ANOVA.
|
||||
</action>
|
||||
|
|
|
@ -73,6 +73,35 @@ public class MathArrays {
|
|||
int numElements);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a copy of an array scaled by a value.
|
||||
*
|
||||
* @param arr Array to scale.
|
||||
* @param val Scalar.
|
||||
* @return scaled copy of array with each entry multiplied by val.
|
||||
*/
|
||||
public static double[] scale(double val, final double[] arr) {
|
||||
double[] newArr = new double[arr.length];
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
newArr[i] = arr[i] * val;
|
||||
}
|
||||
return newArr;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Multiply each element of an array by a value.</p>
|
||||
*
|
||||
* <p>The array is modified in place (no copy is created).</p>
|
||||
*
|
||||
* @param arr Array to scale
|
||||
* @param val Scalar
|
||||
*/
|
||||
public static void scaleInPlace(double val, final double[] arr) {
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
arr[i] *= val;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an array whose contents will be the element-by-element
|
||||
* addition of the arguments.
|
||||
|
|
|
@ -33,6 +33,38 @@ import org.junit.Test;
|
|||
* @version $Id$
|
||||
*/
|
||||
public class MathArraysTest {
|
||||
|
||||
@Test
|
||||
public void testScale() {
|
||||
final double[] test = new double[] { -2.5, -1, 0, 1, 2.5 };
|
||||
final double[] correctTest = MathArrays.copyOf(test);
|
||||
final double[] correctScaled = new double[]{5.25, 2.1, 0, -2.1, -5.25};
|
||||
|
||||
final double[] scaled = MathArrays.scale(-2.1, test);
|
||||
|
||||
// Make sure test has not changed
|
||||
for (int i = 0; i < test.length; i++) {
|
||||
Assert.assertEquals(correctTest[i], test[i], 0);
|
||||
}
|
||||
|
||||
// Test scaled values
|
||||
for (int i = 0; i < scaled.length; i++) {
|
||||
Assert.assertEquals(correctScaled[i], scaled[i], 0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScaleInPlace() {
|
||||
final double[] test = new double[] { -2.5, -1, 0, 1, 2.5 };
|
||||
final double[] correctScaled = new double[]{5.25, 2.1, 0, -2.1, -5.25};
|
||||
MathArrays.scaleInPlace(-2.1, test);
|
||||
|
||||
// Make sure test has changed
|
||||
for (int i = 0; i < test.length; i++) {
|
||||
Assert.assertEquals(correctScaled[i], test[i], 0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected=DimensionMismatchException.class)
|
||||
public void testEbeAddPrecondition() {
|
||||
MathArrays.ebeAdd(new double[3], new double[4]);
|
||||
|
|
Loading…
Reference in New Issue