Author: luc
Date: Sun Oct 5 13:37:15 2008
New Revision: 701883
URL: http://svn.apache.org/viewvc?rev=701883&view=rev
Log:
added methods to operate directly on double arrays
Modified:
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/linear/RealVector.java
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/linear/RealVectorImpl.java
commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/linear/RealVectorImplTest.java
Modified:
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/linear/RealVector.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/linear/RealVector.java?rev=701883&r1=701882&r2=701883&view=diff
==============================================================================
---
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/linear/RealVector.java
(original)
+++
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/linear/RealVector.java
Sun Oct 5 13:37:15 2008
@@ -57,6 +57,15 @@
throws IllegalArgumentException;
/**
+ * Compute the sum of this and v.
+ * @param v vector to be added
+ * @return this + v
+ * @throws IllegalArgumentException if v is not the same size as this
+ */
+ RealVector add(double[] v)
+ throws IllegalArgumentException;
+
+ /**
* Compute this minus v.
* @param v vector to be subtracted
* @return this + v
@@ -66,6 +75,15 @@
throws IllegalArgumentException;
/**
+ * Compute this minus v.
+ * @param v vector to be subtracted
+ * @return this + v
+ * @throws IllegalArgumentException if v is not the same size as this
+ */
+ RealVector subtract(double[] v)
+ throws IllegalArgumentException;
+
+ /**
* Map an addition operation to each entry.
* @param d value to be added to each entry
* @return this + d
@@ -449,6 +467,15 @@
throws IllegalArgumentException;
/**
+ * Element-by-element multiplication.
+ * @param v vector by which instance elements must be multiplied
+ * @return a vector containing this[i] * v[i] for all i
+ * @throws IllegalArgumentException if v is not the same size as this
+ */
+ public RealVector ebeMultiply(double[] v)
+ throws IllegalArgumentException;
+
+ /**
* Element-by-element division.
* @param v vector by which instance elements must be divided
* @return a vector containing this[i] / v[i] for all i
@@ -458,6 +485,15 @@
throws IllegalArgumentException;
/**
+ * Element-by-element division.
+ * @param v vector by which instance elements must be divided
+ * @return a vector containing this[i] / v[i] for all i
+ * @throws IllegalArgumentException if v is not the same size as this
+ */
+ public RealVector ebeDivide(double[] v)
+ throws IllegalArgumentException;
+
+ /**
* Returns vector entries as a double array.
* @return double array of entries
*/
@@ -473,6 +509,15 @@
throws IllegalArgumentException;
/**
+ * Compute the dot product.
+ * @param v vector with which dot product should be computed
+ * @return the scalar dot product between instance and v
+ * @exception IllegalArgumentException if v is not the same size as this
+ */
+ double dotProduct(double[] v)
+ throws IllegalArgumentException;
+
+ /**
* Returns the L<sub>2</sub> norm of the vector.
* <p>The L<sub>2</sub> norm is the root of the sum of
* the squared elements.</p>
@@ -522,6 +567,21 @@
/**
* Distance between two vectors.
+ * <p>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.</p>
+ * @param v vector to which distance is requested
+ * @return distance between two vectors.
+ * @exception IllegalArgumentException if v is not the same size as this
+ * @see #getL1Distance(double[])
+ * @see #getLInfDistance(double[])
+ * @see #getNorm()
+ */
+ double getDistance(double[] v)
+ throws IllegalArgumentException;
+
+ /**
+ * Distance between two vectors.
* <p>This method computes the distance consistent with
* L<sub>1</sub> norm, i.e. the sum of the absolute values of
* elements differences.</p>
@@ -538,6 +598,21 @@
/**
* Distance between two vectors.
* <p>This method computes the distance consistent with
+ * L<sub>1</sub> norm, i.e. the sum of the absolute values of
+ * elements differences.</p>
+ * @param v vector to which distance is requested
+ * @return distance between two vectors.
+ * @exception IllegalArgumentException if v is not the same size as this
+ * @see #getDistance(double[])
+ * @see #getLInfDistance(double[])
+ * @see #getL1Norm()
+ */
+ double getL1Distance(double[] v)
+ throws IllegalArgumentException;
+
+ /**
+ * Distance between two vectors.
+ * <p>This method computes the distance consistent with
* L<sub>&infty;</sub> norm, i.e. the max of the absolute values of
* elements differences.</p>
* @param v vector to which distance is requested
@@ -550,6 +625,21 @@
double getLInfDistance(RealVector v)
throws IllegalArgumentException;
+ /**
+ * Distance between two vectors.
+ * <p>This method computes the distance consistent with
+ * L<sub>&infty;</sub> norm, i.e. the max of the absolute values of
+ * elements differences.</p>
+ * @param v vector to which distance is requested
+ * @return distance between two vectors.
+ * @exception IllegalArgumentException if v is not the same size as this
+ * @see #getDistance(double[])
+ * @see #getL1Distance(double[])
+ * @see #getLInfNorm()
+ */
+ double getLInfDistance(double[] v)
+ throws IllegalArgumentException;
+
/** Creates a unit vector pointing in the direction of this vector.
* <p>The instance is not changed by this method.</p>
* @return a unit vector pointing in direction of this vector
@@ -571,6 +661,14 @@
RealVector projection(RealVector v)
throws IllegalArgumentException;
+ /** Find the orthogonal projection of this vector onto another vector.
+ * @param v vector onto which instance must be projected
+ * @return projection of the instance onto v
+ * @throws IllegalArgumentException if v is not the same size as this
+ */
+ RealVector projection(double[] v)
+ throws IllegalArgumentException;
+
/**
* Compute the outer product.
* @param v vector with which outer product should be computed
@@ -581,6 +679,15 @@
throws IllegalArgumentException;
/**
+ * Compute the outer product.
+ * @param v vector with which outer product should be computed
+ * @return the square matrix outer product between instance and v
+ * @exception IllegalArgumentException if v is not the same size as this
+ */
+ RealMatrix outerProduct(double[] v)
+ throws IllegalArgumentException;
+
+ /**
* Returns the entry in the specified index.
* <p>
* The index start at 0 and must be lesser than the size,
@@ -652,6 +759,16 @@
throws MatrixIndexException;
/**
+ * Set a set of consecutive elements.
+ * @param index index of first element to be set.
+ * @param v vector containing the values to set.
+ * @exception MatrixIndexException if the index is
+ * inconsistent with vector size
+ */
+ void set(int index, double[] v)
+ throws MatrixIndexException;
+
+ /**
* Set all elements to a single value.
* @param value single value to set for all elements
*/
Modified:
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/linear/RealVectorImpl.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/linear/RealVectorImpl.java?rev=701883&r1=701882&r2=701883&view=diff
==============================================================================
---
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/linear/RealVectorImpl.java
(original)
+++
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/linear/RealVectorImpl.java
Sun Oct 5 13:37:15 2008
@@ -29,7 +29,7 @@
public class RealVectorImpl implements RealVector, Serializable {
/** Serializable version identifier. */
- private static final long serialVersionUID = 7838747548772166404L;
+ private static final long serialVersionUID = -1097961340710804027L;
/** Default format. */
private static final RealVectorFormat DEFAULT_FORMAT =
@@ -186,6 +186,39 @@
System.arraycopy(v2.data, 0, data, v1.data.length, v2.data.length);
}
+ /**
+ * Construct a vector by appending one vector to another vector.
+ * @param v1 first vector (will be put in front of the new vector)
+ * @param v2 second vector (will be put at back of the new vector)
+ */
+ public RealVectorImpl(RealVectorImpl v1, double[] v2) {
+ data = new double[v1.data.length + v2.length];
+ System.arraycopy(v1.data, 0, data, 0, v1.data.length);
+ System.arraycopy(v2, 0, data, v1.data.length, v2.length);
+ }
+
+ /**
+ * Construct a vector by appending one vector to another vector.
+ * @param v1 first vector (will be put in front of the new vector)
+ * @param v2 second vector (will be put at back of the new vector)
+ */
+ public RealVectorImpl(double[] v1, RealVectorImpl v2) {
+ data = new double[v1.length + v2.data.length];
+ System.arraycopy(v1, 0, data, 0, v1.length);
+ System.arraycopy(v2.data, 0, data, v1.length, v2.data.length);
+ }
+
+ /**
+ * Construct a vector by appending one vector to another vector.
+ * @param v1 first vector (will be put in front of the new vector)
+ * @param v2 second vector (will be put at back of the new vector)
+ */
+ public RealVectorImpl(double[] v1, double[] v2) {
+ data = new double[v1.length + v2.length];
+ System.arraycopy(v1, 0, data, 0, v1.length);
+ System.arraycopy(v2, 0, data, v1.length, v2.length);
+ }
+
/** [EMAIL PROTECTED] */
public RealVector copy() {
return new RealVectorImpl(this, true);
@@ -206,6 +239,17 @@
}
}
+ /** [EMAIL PROTECTED] */
+ public RealVector add(double[] v)
+ throws IllegalArgumentException {
+ checkVectorDimensions(v.length);
+ double[] out = new double[data.length];
+ for (int i = 0; i < data.length; i++) {
+ out[i] = data[i] + v[i];
+ }
+ return new RealVectorImpl(out);
+ }
+
/**
* Compute the sum of this and v.
* @param v vector to be added
@@ -214,12 +258,7 @@
*/
public RealVectorImpl add(RealVectorImpl v)
throws IllegalArgumentException {
- checkVectorDimensions(v);
- double[] out = new double[data.length];
- for (int i = 0; i < data.length; i++) {
- out[i] = data[i] + v.data[i];
- }
- return new RealVectorImpl(out);
+ return (RealVectorImpl) add(v.data);
}
/** [EMAIL PROTECTED] */
@@ -237,6 +276,17 @@
}
}
+ /** [EMAIL PROTECTED] */
+ public RealVector subtract(double[] v)
+ throws IllegalArgumentException {
+ checkVectorDimensions(v.length);
+ double[] out = new double[data.length];
+ for (int i = 0; i < data.length; i++) {
+ out[i] = data[i] - v[i];
+ }
+ return new RealVectorImpl(out);
+ }
+
/**
* Compute this minus v.
* @param v vector to be subtracted
@@ -245,12 +295,7 @@
*/
public RealVectorImpl subtract(RealVectorImpl v)
throws IllegalArgumentException {
- checkVectorDimensions(v);
- double[] out = new double[data.length];
- for (int i = 0; i < data.length; i++) {
- out[i] = data[i] - v.data[i];
- }
- return new RealVectorImpl(out);
+ return (RealVectorImpl) subtract(v.data);
}
/** [EMAIL PROTECTED] */
@@ -744,6 +789,17 @@
}
}
+ /** [EMAIL PROTECTED] */
+ public RealVector ebeMultiply(double[] v)
+ throws IllegalArgumentException {
+ checkVectorDimensions(v.length);
+ double[] out = new double[data.length];
+ for (int i = 0; i < data.length; i++) {
+ out[i] = data[i] * v[i];
+ }
+ return new RealVectorImpl(out);
+ }
+
/**
* Element-by-element multiplication.
* @param v vector by which instance elements must be multiplied
@@ -752,12 +808,7 @@
*/
public RealVectorImpl ebeMultiply(RealVectorImpl v)
throws IllegalArgumentException {
- checkVectorDimensions(v);
- double[] out = new double[data.length];
- for (int i = 0; i < data.length; i++) {
- out[i] = data[i] * v.data[i];
- }
- return new RealVectorImpl(out);
+ return (RealVectorImpl) ebeMultiply(v.data);
}
/** [EMAIL PROTECTED] */
@@ -775,6 +826,17 @@
}
}
+ /** [EMAIL PROTECTED] */
+ public RealVector ebeDivide(double[] v)
+ throws IllegalArgumentException {
+ checkVectorDimensions(v.length);
+ double[] out = new double[data.length];
+ for (int i = 0; i < data.length; i++) {
+ out[i] = data[i] / v[i];
+ }
+ return new RealVectorImpl(out);
+ }
+
/**
* Element-by-element division.
* @param v vector by which instance elements must be divided
@@ -783,12 +845,7 @@
*/
public RealVectorImpl ebeDivide(RealVectorImpl v)
throws IllegalArgumentException {
- checkVectorDimensions(v);
- double[] out = new double[data.length];
- for (int i = 0; i < data.length; i++) {
- out[i] = data[i] / v.data[i];
- }
- return new RealVectorImpl(out);
+ return (RealVectorImpl) ebeDivide(v.data);
}
/** [EMAIL PROTECTED] */
@@ -820,6 +877,17 @@
}
}
+ /** [EMAIL PROTECTED] */
+ public double dotProduct(double[] v)
+ throws IllegalArgumentException {
+ checkVectorDimensions(v.length);
+ double dot = 0;
+ for (int i = 0; i < data.length; i++) {
+ dot += data[i] * v[i];
+ }
+ return dot;
+ }
+
/**
* Compute the dot product.
* @param v vector with which dot product should be computed
@@ -828,12 +896,7 @@
*/
public double dotProduct(RealVectorImpl v)
throws IllegalArgumentException {
- checkVectorDimensions(v);
- double dot = 0;
- for (int i = 0; i < data.length; i++) {
- dot += data[i] * v.getEntry(i);
- }
- return dot;
+ return dotProduct(v.data);
}
/** [EMAIL PROTECTED] */
@@ -879,7 +942,19 @@
}
}
- /**
+ /** [EMAIL PROTECTED] */
+ public double getDistance(double[] v)
+ throws IllegalArgumentException {
+ 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 Math.sqrt(sum);
+ }
+
+ /**
* Distance between two vectors.
* <p>This method computes the distance consistent with the
* L<sub>2</sub> norm, i.e. the square root of the sum of
@@ -894,13 +969,7 @@
*/
public double getDistance(RealVectorImpl v)
throws IllegalArgumentException {
- checkVectorDimensions(v);
- double sum = 0;
- for (int i = 0; i < data.length; ++i) {
- final double delta = data[i] - v.data[i];
- sum += delta * delta;
- }
- return Math.sqrt(sum);
+ return getDistance(v.data);
}
/** [EMAIL PROTECTED] */
@@ -919,6 +988,18 @@
}
}
+ /** [EMAIL PROTECTED] */
+ public double getL1Distance(double[] v)
+ throws IllegalArgumentException {
+ checkVectorDimensions(v.length);
+ double sum = 0;
+ for (int i = 0; i < data.length; ++i) {
+ final double delta = data[i] - v[i];
+ sum += Math.abs(delta);
+ }
+ return sum;
+ }
+
/**
* Distance between two vectors.
* <p>This method computes the distance consistent with
@@ -934,13 +1015,7 @@
*/
public double getL1Distance(RealVectorImpl v)
throws IllegalArgumentException {
- checkVectorDimensions(v);
- double sum = 0;
- for (int i = 0; i < data.length; ++i) {
- final double delta = data[i] - v.data[i];
- sum += Math.abs(delta);
- }
- return sum;
+ return getL1Distance(v.data);
}
/** [EMAIL PROTECTED] */
@@ -959,6 +1034,18 @@
}
}
+ /** [EMAIL PROTECTED] */
+ public double getLInfDistance(double[] v)
+ throws IllegalArgumentException {
+ checkVectorDimensions(v.length);
+ double max = 0;
+ for (int i = 0; i < data.length; ++i) {
+ final double delta = data[i] - v[i];
+ max = Math.max(max, Math.abs(delta));
+ }
+ return max;
+ }
+
/**
* Distance between two vectors.
* <p>This method computes the distance consistent with
@@ -974,13 +1061,7 @@
*/
public double getLInfDistance(RealVectorImpl v)
throws IllegalArgumentException {
- checkVectorDimensions(v);
- double max = 0;
- for (int i = 0; i < data.length; ++i) {
- final double delta = data[i] - v.data[i];
- max = Math.max(max, Math.abs(delta));
- }
- return max;
+ return getLInfDistance(v.data);
}
/** [EMAIL PROTECTED] */
@@ -1008,7 +1089,12 @@
return v.mapMultiply(dotProduct(v) / v.dotProduct(v));
}
- /** Find the orthogonal projection of this vector onto another vector.
+ /** [EMAIL PROTECTED] */
+ public RealVector projection(double[] v) {
+ return projection(new RealVectorImpl(v, false));
+ }
+
+ /** Find the orthogonal projection of this vector onto another vector.
* @param v vector onto which instance must be projected
* @return projection of the instance onto v
* @throws IllegalArgumentException if v is not the same size as this
@@ -1034,6 +1120,19 @@
}
}
+ /** [EMAIL PROTECTED] */
+ public RealMatrix outerProduct(double[] v)
+ throws IllegalArgumentException {
+ checkVectorDimensions(v.length);
+ double[][] out = new double[data.length][data.length];
+ for (int i = 0; i < data.length; i++) {
+ for (int j = 0; j < data.length; j++) {
+ out[i][j] = data[i] * v[j];
+ }
+ }
+ return new RealMatrixImpl(out);
+ }
+
/**
* Compute the outer product.
* @param v vector with which outer product should be computed
@@ -1042,14 +1141,7 @@
*/
public RealMatrixImpl outerProduct(RealVectorImpl v)
throws IllegalArgumentException {
- checkVectorDimensions(v);
- double[][] out = new double[data.length][data.length];
- for (int i = 0; i < data.length; i++) {
- for (int j = 0; j < data.length; j++) {
- out[i][j] = data[i] * v.data[j];
- }
- }
- return new RealMatrixImpl(out);
+ return (RealMatrixImpl) outerProduct(v.data);
}
/** [EMAIL PROTECTED] */
@@ -1090,10 +1182,7 @@
/** [EMAIL PROTECTED] */
public RealVector append(double[] in) {
- final double[] out = new double[data.length + in.length];
- System.arraycopy(data, 0, out, 0, data.length);
- System.arraycopy(in, 0, out, data.length, in.length);
- return new RealVectorImpl(out);
+ return new RealVectorImpl(this, in);
}
/** [EMAIL PROTECTED] */
@@ -1132,6 +1221,15 @@
}
}
+ /** [EMAIL PROTECTED] */
+ public void set(int index, double[] v) {
+ try {
+ System.arraycopy(v, 0, data, index, v.length);
+ } catch (IndexOutOfBoundsException e) {
+ throw new MatrixIndexException(e.getMessage());
+ }
+ }
+
/**
* Set a set of consecutive elements.
*
@@ -1142,11 +1240,7 @@
*/
public void set(int index, RealVectorImpl v)
throws MatrixIndexException {
- try {
- System.arraycopy(v.data, 0, data, index, v.data.length);
- } catch (IndexOutOfBoundsException e) {
- throw new MatrixIndexException(e.getMessage());
- }
+ set(index, v.data);
}
/** [EMAIL PROTECTED] */
Modified:
commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/linear/RealVectorImplTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/linear/RealVectorImplTest.java?rev=701883&r1=701882&r2=701883&view=diff
==============================================================================
---
commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/linear/RealVectorImplTest.java
(original)
+++
commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/linear/RealVectorImplTest.java
Sun Oct 5 13:37:15 2008
@@ -47,7 +47,7 @@
public static class RealVectorTestImpl implements RealVector, Serializable
{
/** Serializable version identifier. */
- private static final long serialVersionUID = 8731816072271374422L;
+ private static final long serialVersionUID = 4715341047369582908L;
/** Entries of the vector. */
protected double data[];
@@ -68,10 +68,18 @@
throw unsupported();
}
+ public RealVector add(double[] v) throws IllegalArgumentException {
+ throw unsupported();
+ }
+
public RealVector subtract(RealVector v) throws
IllegalArgumentException {
throw unsupported();
}
+ public RealVector subtract(double[] v) throws IllegalArgumentException
{
+ throw unsupported();
+ }
+
public RealVector mapAdd(double d) {
throw unsupported();
}
@@ -304,17 +312,23 @@
throw unsupported();
}
+ public RealVector ebeMultiply(double[] v) throws
IllegalArgumentException {
+ throw unsupported();
+ }
+
public RealVector ebeDivide(RealVector v) throws
IllegalArgumentException {
throw unsupported();
}
+ public RealVector ebeDivide(double[] v) throws
IllegalArgumentException {
+ throw unsupported();
+ }
+
public double[] getData() {
return data.clone();
}
public double dotProduct(RealVector v) throws IllegalArgumentException
{
-
- // checkVectorDimensions(v);
double dot = 0;
for (int i = 0; i < data.length; i++) {
dot += data[i] * v.getEntry(i);
@@ -322,6 +336,14 @@
return dot;
}
+ public double dotProduct(double[] v) throws IllegalArgumentException {
+ double dot = 0;
+ for (int i = 0; i < data.length; i++) {
+ dot += data[i] * v[i];
+ }
+ return dot;
+ }
+
public double getNorm() {
throw unsupported();
}
@@ -338,14 +360,26 @@
throw unsupported();
}
+ public double getDistance(double[] v) throws IllegalArgumentException {
+ throw unsupported();
+ }
+
public double getL1Distance(RealVector v) throws
IllegalArgumentException {
throw unsupported();
}
+ public double getL1Distance(double[] v) throws
IllegalArgumentException {
+ throw unsupported();
+ }
+
public double getLInfDistance(RealVector v) throws
IllegalArgumentException {
throw unsupported();
}
+ public double getLInfDistance(double[] v) throws
IllegalArgumentException {
+ throw unsupported();
+ }
+
public RealVector unitVector() {
throw unsupported();
}
@@ -358,10 +392,18 @@
throw unsupported();
}
+ public RealVector projection(double[] v) throws
IllegalArgumentException {
+ throw unsupported();
+ }
+
public RealMatrix outerProduct(RealVector v) throws
IllegalArgumentException {
throw unsupported();
}
+ public RealMatrix outerProduct(double[] v) throws
IllegalArgumentException {
+ throw unsupported();
+ }
+
public double getEntry(int index) throws MatrixIndexException {
return data[index];
}
@@ -394,6 +436,10 @@
throw unsupported();
}
+ public void set(int index, double[] v) throws MatrixIndexException {
+ throw unsupported();
+ }
+
public void set(double value) {
throw unsupported();
}