Author: erans
Date: Mon Aug 29 13:49:22 2011
New Revision: 1162800
URL: http://svn.apache.org/viewvc?rev=1162800&view=rev
Log:
MATH-653
Removed methods taking a "double[]" argument.
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/EigenDecompositionImpl.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java?rev=1162800&r1=1162799&r2=1162800&view=diff
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java
(original)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/ArrayRealVector.java
Mon Aug 29 13:49:22 2011
@@ -128,7 +128,8 @@ public class ArrayRealVector extends Rea
/**
* Construct a vector from an array.
- * @param d array of Doubles.
+ *
+ * @param d Array of {@code Double}s.
*/
public ArrayRealVector(Double[] d) {
data = new double[d.length];
@@ -287,7 +288,15 @@ public class ArrayRealVector extends Rea
@Override
public ArrayRealVector add(RealVector v) {
if (v instanceof ArrayRealVector) {
- return add(((ArrayRealVector) v).data);
+ final double[] vData = ((ArrayRealVector) v).data;
+ final int dim = vData.length;
+ checkVectorDimensions(dim);
+ ArrayRealVector result = new ArrayRealVector(dim);
+ double[] resultData = result.data;
+ for (int i = 0; i < dim; i++) {
+ resultData[i] = data[i] + vData[i];
+ }
+ return result;
} else {
checkVectorDimensions(v);
double[] out = data.clone();
@@ -300,31 +309,19 @@ public class ArrayRealVector extends Rea
}
}
- /**
- * Compute the sum of this vector and {@code v}.
- * Returns a new vector. Does not change instance data.
- *
- * @param v Vector to be added.
- * @return {@code this} + {@code v}.
- * @throws org.apache.commons.math.exception.DimensionMismatchException
- * if {@code v} is not the same size as this vector.
- */
- public ArrayRealVector add(double[] v) {
- final int dim = v.length;
- checkVectorDimensions(dim);
- ArrayRealVector result = new ArrayRealVector(dim);
- double[] resultData = result.data;
- for (int i = 0; i < dim; i++) {
- resultData[i] = data[i] + v[i];
- }
- return result;
- }
-
/** {@inheritDoc} */
@Override
public ArrayRealVector subtract(RealVector v) {
- if (v instanceof ArrayRealVector) {
- return subtract(((ArrayRealVector) v).data);
+ if (v instanceof ArrayRealVector) {
+ final double[] vData = ((ArrayRealVector) v).data;
+ final int dim = vData.length;
+ checkVectorDimensions(dim);
+ ArrayRealVector result = new ArrayRealVector(dim);
+ double[] resultData = result.data;
+ for (int i = 0; i < dim; i++) {
+ resultData[i] = data[i] - vData[i];
+ }
+ return result;
} else {
checkVectorDimensions(v);
double[] out = data.clone();
@@ -337,26 +334,6 @@ public class ArrayRealVector extends Rea
}
}
- /**
- * Subtract {@code v} from this vector.
- * Returns a new vector. Does not change instance data.
- *
- * @param v Vector to be subtracted.
- * @return {@code this} - {@code v}.
- * @throws org.apache.commons.math.exception.DimensionMismatchException
- * if {@code v} is not the same size as this vector.
- */
- public ArrayRealVector subtract(double[] v) {
- final int dim = v.length;
- checkVectorDimensions(dim);
- ArrayRealVector result = new ArrayRealVector(dim);
- double[] resultData = result.data;
- for (int i = 0; i < dim; i++) {
- resultData[i] = data[i] - v[i];
- }
- return result;
- }
-
/** {@inheritDoc} */
@Override
public ArrayRealVector map(UnivariateRealFunction function) {
@@ -412,7 +389,15 @@ public class ArrayRealVector extends Rea
@Override
public ArrayRealVector ebeMultiply(RealVector v) {
if (v instanceof ArrayRealVector) {
- return ebeMultiply(((ArrayRealVector) v).data);
+ final double[] vData = ((ArrayRealVector) v).data;
+ final int dim = vData.length;
+ checkVectorDimensions(dim);
+ ArrayRealVector result = new ArrayRealVector(dim);
+ double[] resultData = result.data;
+ for (int i = 0; i < dim; i++) {
+ resultData[i] = data[i] * vData[i];
+ }
+ return result;
} else {
checkVectorDimensions(v);
double[] out = data.clone();
@@ -423,29 +408,18 @@ public class ArrayRealVector extends Rea
}
}
- /**
- * Element-by-element multiplication.
- *
- * @param v Vector by which instance elements must be multiplied.
- * @return a Vector containing {@code this[i] * v[i]} for all {@code i}.
- * @throws DimensionMismatchException if {@code v} is not the same
- * size as this vector.
- */
- public ArrayRealVector ebeMultiply(double[] v) {
- final int dim = v.length;
- checkVectorDimensions(dim);
- ArrayRealVector result = new ArrayRealVector(dim);
- double[] resultData = result.data;
- for (int i = 0; i < dim; i++) {
- resultData[i] = data[i] * v[i];
- }
- return result;
- }
-
/** {@inheritDoc} */
public ArrayRealVector ebeDivide(RealVector v) {
if (v instanceof ArrayRealVector) {
- return ebeDivide(((ArrayRealVector) v).data);
+ final double[] vData = ((ArrayRealVector) v).data;
+ final int dim = vData.length;
+ checkVectorDimensions(dim);
+ ArrayRealVector result = new ArrayRealVector(dim);
+ double[] resultData = result.data;
+ for (int i = 0; i < dim; i++) {
+ resultData[i] = data[i] / vData[i];
+ }
+ return result;
} else {
checkVectorDimensions(v);
double[] out = data.clone();
@@ -456,25 +430,6 @@ public class ArrayRealVector extends Rea
}
}
- /**
- * Element-by-element division.
- *
- * @param v Vector by which instance elements must be divided.
- * @return a vector containing {@code this[i] / v[i]} for all {@code i}.
- * @exception DimensionMismatchException if {@code v} is not the same
- * size as this vector.
- */
- public ArrayRealVector ebeDivide(double[] v) {
- final int dim = v.length;
- checkVectorDimensions(dim);
- ArrayRealVector result = new ArrayRealVector(dim);
- double[] resultData = result.data;
- for (int i = 0; i < dim; i++) {
- resultData[i] = data[i] / v[i];
- }
- return result;
- }
-
/** {@inheritDoc} */
@Override
public double[] getData() {
@@ -495,7 +450,13 @@ public class ArrayRealVector extends Rea
@Override
public double dotProduct(RealVector v) {
if (v instanceof ArrayRealVector) {
- return dotProduct(((ArrayRealVector) v).data);
+ final double[] vData = ((ArrayRealVector) v).data;
+ checkVectorDimensions(vData.length);
+ double dot = 0;
+ for (int i = 0; i < data.length; i++) {
+ dot += data[i] * vData[i];
+ }
+ return dot;
} else {
checkVectorDimensions(v);
double dot = 0;
@@ -508,23 +469,6 @@ public class ArrayRealVector extends Rea
}
}
- /**
- * Compute the dot product.
- *
- * @param v Vector with which dot product should be computed
- * @return the scalar dot product between instance and {@code v}.
- * @throws DimensionMismatchException if {@code v} is not the same
- * size as this vector.
- */
- public double dotProduct(double[] v) {
- checkVectorDimensions(v.length);
- double dot = 0;
- for (int i = 0; i < data.length; i++) {
- dot += data[i] * v[i];
- }
- return dot;
- }
-
/** {@inheritDoc} */
@Override
public double getNorm() {
@@ -559,7 +503,14 @@ public class ArrayRealVector extends Rea
@Override
public double getDistance(RealVector v) {
if (v instanceof ArrayRealVector) {
- return getDistance(((ArrayRealVector) v).data);
+ final double[] vData = ((ArrayRealVector) v).data;
+ checkVectorDimensions(vData.length);
+ double sum = 0;
+ for (int i = 0; i < data.length; ++i) {
+ final double delta = data[i] - vData[i];
+ sum += delta * delta;
+ }
+ return FastMath.sqrt(sum);
} else {
checkVectorDimensions(v);
double sum = 0;
@@ -571,34 +522,18 @@ public class ArrayRealVector extends Rea
}
}
- /**
- * Distance between two vectors.
- * This method computes the distance consistent with the
- * L<sub>2</sub> norm, i.e. the square root of the sum of
- * elements differences, or euclidian distance.
- *
- * @param v Vector to which distance is requested.
- * @return the distance between two vectors.
- * @throws DimensionMismatchException if {@code v} is not the same size as
- * this vector.
- * @see #getDistance(RealVector)
- * @see #getNorm()
- */
- public double getDistance(double[] v) {
- checkVectorDimensions(v.length);
- double sum = 0;
- for (int i = 0; i < data.length; ++i) {
- final double delta = data[i] - v[i];
- sum += delta * delta;
- }
- return FastMath.sqrt(sum);
- }
-
/** {@inheritDoc} */
@Override
public double getL1Distance(RealVector v) {
if (v instanceof ArrayRealVector) {
- return getL1Distance(((ArrayRealVector) v).data);
+ final double[] vData = ((ArrayRealVector) v).data;
+ checkVectorDimensions(vData.length);
+ double sum = 0;
+ for (int i = 0; i < data.length; ++i) {
+ final double delta = data[i] - vData[i];
+ sum += FastMath.abs(delta);
+ }
+ return sum;
} else {
checkVectorDimensions(v);
double sum = 0;
@@ -610,34 +545,18 @@ public class ArrayRealVector extends Rea
}
}
- /**
- * Distance between two vectors.
- * This method computes the distance consistent with
- * L<sub>1</sub> norm, i.e. the sum of the absolute values of
- * elements differences.
- *
- * @param v Vector to which distance is requested.
- * @return the distance between two vectors.
- * @throws DimensionMismatchException if {@code v} is not the same size
- * as this vector.
- * @see #getDistance(RealVector)
- * @see #getNorm()
- */
- public double getL1Distance(double[] v) {
- checkVectorDimensions(v.length);
- double sum = 0;
- for (int i = 0; i < data.length; ++i) {
- final double delta = data[i] - v[i];
- sum += FastMath.abs(delta);
- }
- return sum;
- }
-
/** {@inheritDoc} */
@Override
public double getLInfDistance(RealVector v) {
if (v instanceof ArrayRealVector) {
- return getLInfDistance(((ArrayRealVector) v).data);
+ final double[] vData = ((ArrayRealVector) v).data;
+ checkVectorDimensions(vData.length);
+ double max = 0;
+ for (int i = 0; i < data.length; ++i) {
+ final double delta = data[i] - vData[i];
+ max = FastMath.max(max, FastMath.abs(delta));
+ }
+ return max;
} else {
checkVectorDimensions(v);
double max = 0;
@@ -649,28 +568,6 @@ public class ArrayRealVector extends Rea
}
}
- /**
- * Distance between two vectors.
- * This method computes the distance consistent with
- * L<sub>∞</sub> norm, i.e. the max of the absolute values of
- * elements differences.
- *
- * @param v Vector to which distance is requested.
- * @return the distance between two vectors.
- * @exception IllegalArgumentException if {@code v} is not the same size
as this
- * @see #getDistance(RealVector)
- * @see #getNorm()
- */
- public double getLInfDistance(double[] v) {
- checkVectorDimensions(v.length);
- double max = 0;
- for (int i = 0; i < data.length; ++i) {
- final double delta = data[i] - v[i];
- max = FastMath.max(max, FastMath.abs(delta));
- }
- return max;
- }
-
/** {@inheritDoc} */
@Override
public RealVector unitVector() {
@@ -700,7 +597,16 @@ public class ArrayRealVector extends Rea
@Override
public RealMatrix outerProduct(RealVector v) {
if (v instanceof ArrayRealVector) {
- return outerProduct(((ArrayRealVector) v).data);
+ final double[] vData = ((ArrayRealVector) v).data;
+ final int m = data.length;
+ final int n = vData.length;
+ final RealMatrix out = MatrixUtils.createRealMatrix(m, n);
+ for (int i = 0; i < m; i++) {
+ for (int j = 0; j < n; j++) {
+ out.setEntry(i, j, data[i] * vData[j]);
+ }
+ }
+ return out;
} else {
final int m = data.length;
final int n = v.getDimension();
@@ -714,25 +620,6 @@ public class ArrayRealVector extends Rea
}
}
- /**
- * Compute the outer product.
- * @param v Vector with which outer product should be computed.
- * @return the square matrix outer product between this instance and
{@code v}.
- * @throws DimensionMismatchException if {@code v} is not the same
- * size as this vector.
- */
- public RealMatrix outerProduct(double[] v) {
- final int m = data.length;
- final int n = v.length;
- final RealMatrix out = MatrixUtils.createRealMatrix(m, n);
- for (int i = 0; i < m; i++) {
- for (int j = 0; j < n; j++) {
- out.setEntry(i, j, data[i] * v[j]);
- }
- }
- return out;
- }
-
/** {@inheritDoc} */
public double getEntry(int index) {
return data[index];
@@ -960,59 +847,27 @@ public class ArrayRealVector extends Rea
return MathUtils.hash(data);
}
- /**
- * Updates {@code this} with the linear combination of {@code this} and
- * {@code y}.
- *
- * @param a Weight of {@code this}.
- * @param b Weight of {@code y}.
- * @param y Vector with which {@code this} is linearly combined.
- * @return {@code this}, with components equal to
- * {@code a * this[i] + b * y[i]} for all {@code i}.
- * @throws org.apache.commons.math.exception.DimensionMismatchException
- * if {@code y} is not the same size as this vector.
- */
- public ArrayRealVector combine(double a, double b, double[] y) {
- return copy().combineToSelf(a, b, y);
- }
-
/** {@inheritDoc} */
@Override
public ArrayRealVector combine(double a, double b, RealVector y) {
return copy().combineToSelf(a, b, y);
}
- /**
- * Updates {@code this} with the linear combination of {@code this} and
- * {@code y}.
- *
- * @param a Weight of {@code this}.
- * @param b Weight of {@code y}.
- * @param y Vector with which {@code this} is linearly combined.
- * @return {@code this}, with components equal to
- * {@code a * this[i] + b * y[i]} for all {@code i}.
- * @throws org.apache.commons.math.exception.DimensionMismatchException
- * if {@code y} is not the same size as this vector.
- */
- public ArrayRealVector combineToSelf(double a, double b, double[] y) {
- checkVectorDimensions(y.length);
- for (int i = 0; i < this.data.length; i++) {
- data[i] = a * data[i] + b * y[i];
- }
- return this;
- }
-
/** {@inheritDoc} */
@Override
public ArrayRealVector combineToSelf(double a, double b, RealVector y) {
if (y instanceof ArrayRealVector) {
- return combineToSelf(a, b, ((ArrayRealVector) y).data);
+ final double[] yData = ((ArrayRealVector) y).data;
+ checkVectorDimensions(yData.length);
+ for (int i = 0; i < this.data.length; i++) {
+ data[i] = a * data[i] + b * yData[i];
+ }
} else {
checkVectorDimensions(y);
for (int i = 0; i < this.data.length; i++) {
data[i] = a * data[i] + b * y.getEntry(i);
}
- return this;
}
+ return this;
}
}
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/EigenDecompositionImpl.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/EigenDecompositionImpl.java?rev=1162800&r1=1162799&r2=1162800&view=diff
==============================================================================
---
commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/EigenDecompositionImpl.java
(original)
+++
commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/EigenDecompositionImpl.java
Mon Aug 29 13:49:22 2011
@@ -291,10 +291,11 @@ public class EigenDecompositionImpl impl
}
final double[] bp = new double[m];
+ final ArrayRealVector bVector = new ArrayRealVector(b, false);
for (int i = 0; i < m; ++i) {
final ArrayRealVector v = eigenvectors[i];
final double[] vData = v.getDataRef();
- final double s = v.dotProduct(b) / realEigenvalues[i];
+ final double s = v.dotProduct(bVector) / realEigenvalues[i];
for (int j = 0; j < m; ++j) {
bp[j] += s * vData[j];
}
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java?rev=1162800&r1=1162799&r2=1162800&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java
Mon Aug 29 13:49:22 2011
@@ -1200,47 +1200,6 @@ public class ArrayRealVectorTest {
}
@Test(expected=DimensionMismatchException.class)
- public void testCombinePreconditionArray() {
- final double a = 1d;
- final double b = 2d;
- double[] aux = new double[] { 3d, 4d, 5d };
- final ArrayRealVector x = new ArrayRealVector(aux, false);
- final double[] y = new double[] { 6d, 7d };
- x.combine(a, b, y);
- }
-
- @Test
- public void testCombineArray() {
- final Random random = new Random(20110726);
- final int dim = 10;
- final double a = (2 * random.nextDouble() - 1);
- final double b = (2 * random.nextDouble() - 1);
- final ArrayRealVector x = new ArrayRealVector(dim);
- final double[] y = new double[dim];
- final double[] expected = new double[dim];
- for (int i = 0; i < dim; i++) {
- final double xi = 2 * random.nextDouble() - 1;
- final double yi = 2 * random.nextDouble() - 1;
- x.setEntry(i, xi);
- y[i] = yi;
- expected[i] = a * xi + b * yi;
- }
- final double[] actual = x.combine(a, b, y).getData();
- for (int i = 0; i < dim; i++) {
- final double delta;
- if (expected[i] == 0d) {
- delta = Math.ulp(1d);
- } else {
- delta = Math.ulp(expected[i]);
- }
- Assert.assertEquals("elements [" + i + "] differ",
- expected[i],
- actual[i],
- delta);
- }
- }
-
- @Test(expected=DimensionMismatchException.class)
public void testCombinePreconditionSameType() {
final double a = 1d;
final double b = 2d;
@@ -1325,48 +1284,6 @@ public class ArrayRealVectorTest {
}
@Test(expected=DimensionMismatchException.class)
- public void testCombineToSelfPreconditionArray() {
- final double a = 1d;
- final double b = 2d;
- double[] aux = new double[] { 3d, 4d, 5d };
- final ArrayRealVector x = new ArrayRealVector(aux, false);
- final double[] y = new double[] { 6d, 7d };
- x.combineToSelf(a, b, y);
- }
-
- @Test
- public void testCombineToSelfArray() {
- final Random random = new Random(20110726);
- final int dim = 10;
- final double a = (2 * random.nextDouble() - 1);
- final double b = (2 * random.nextDouble() - 1);
- final ArrayRealVector x = new ArrayRealVector(dim);
- final double[] y = new double[dim];
- final double[] expected = new double[dim];
- for (int i = 0; i < dim; i++) {
- final double xi = 2 * random.nextDouble() - 1;
- final double yi = 2 * random.nextDouble() - 1;
- x.setEntry(i, xi);
- y[i] = yi;
- expected[i] = a * xi + b * yi;
- }
- Assert.assertSame(x, x.combineToSelf(a, b, y));
- final double[] actual = x.getData();
- for (int i = 0; i < dim; i++) {
- final double delta;
- if (expected[i] == 0d) {
- delta = Math.ulp(1d);
- } else {
- delta = Math.ulp(expected[i]);
- }
- Assert.assertEquals("elements [" + i + "] differ",
- expected[i],
- actual[i],
- delta);
- }
- }
-
- @Test(expected=DimensionMismatchException.class)
public void testCombineToSelfPreconditionSameType() {
final double a = 1d;
final double b = 2d;