Author: luc Date: Fri Jun 3 19:22:20 2011 New Revision: 1131152 URL: http://svn.apache.org/viewvc?rev=1131152&view=rev Log: added missing methods in Vector1D/2D/3D
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/Vector.java commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/oned/Vector1D.java commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3D.java commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2D.java Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/Vector.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/Vector.java?rev=1131152&r1=1131151&r2=1131152&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/Vector.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/Vector.java Fri Jun 3 19:22:20 2011 @@ -20,7 +20,8 @@ import java.io.Serializable; import java.text.NumberFormat; /** This interface represents a generic vector in a vectorial space or a point in an affine space. - * @version $Id:$ + * @param <S> Type of the space. + * @version $Id$ * @see Space * @see Vector * @since 3.0 @@ -83,6 +84,23 @@ public interface Vector<S extends Space> */ Vector<S> subtract(double factor, Vector<S> v); + /** Get the opposite of the instance. + * @return a new vector which is opposite to the instance + */ + Vector<S> negate(); + + /** Get a normalized vector aligned with the instance. + * @return a new normalized vector + * @exception ArithmeticException if the norm is zero + */ + Vector<S> normalize(); + + /** Multiply the instance by a scalar. + * @param a scalar + * @return a new vector + */ + Vector<S> scalarMultiply(double a); + /** * Returns true if any coordinate of this vector is NaN; false otherwise * @return true if any coordinate of this vector is NaN; false otherwise @@ -133,9 +151,16 @@ public interface Vector<S extends Space> */ double distanceSq(Vector<S> v); + /** Compute the dot-product of the instance and another vector. + * @param v second vector + * @return the dot product this.v + */ + double dotProduct(Vector<S> v); + /** Get a string representation of this vector. - * @param format the custom format for components. + * @param format the custom format for components + * @return a string representation of this vector */ - public String toString(final NumberFormat format); + String toString(final NumberFormat format); } Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/oned/Vector1D.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/oned/Vector1D.java?rev=1131152&r1=1131151&r2=1131152&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/oned/Vector1D.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/oned/Vector1D.java Fri Jun 3 19:22:20 2011 @@ -18,14 +18,16 @@ package org.apache.commons.math.geometry import java.text.NumberFormat; -import org.apache.commons.math.geometry.Vector; +import org.apache.commons.math.exception.MathArithmeticException; +import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.geometry.Space; +import org.apache.commons.math.geometry.Vector; import org.apache.commons.math.util.FastMath; import org.apache.commons.math.util.MathUtils; /** This class represents a 1D vector. * <p>Instances of this class are guaranteed to be immutable.</p> - * @version $Id:$ + * @version $Id$ * @since 3.0 */ public class Vector1D implements Vector<Euclidean1D> { @@ -181,6 +183,24 @@ public class Vector1D implements Vector< } /** {@inheritDoc} */ + public Vector1D normalize() { + double s = getNorm(); + if (s == 0) { + throw new MathArithmeticException(LocalizedFormats.CANNOT_NORMALIZE_A_ZERO_NORM_VECTOR); + } + return scalarMultiply(1 / s); + } + /** {@inheritDoc} */ + public Vector1D negate() { + return new Vector1D(-x); + } + + /** {@inheritDoc} */ + public Vector1D scalarMultiply(double a) { + return new Vector1D(a * x); + } + + /** {@inheritDoc} */ public boolean isNaN() { return Double.isNaN(x); } @@ -218,6 +238,12 @@ public class Vector1D implements Vector< return dx * dx; } + /** {@inheritDoc} */ + public double dotProduct(final Vector<Euclidean1D> v) { + final Vector1D v1 = (Vector1D) v; + return x * v1.x; + } + /** Compute the distance between two vectors according to the L<sub>2</sub> norm. * <p>Calling this method is equivalent to calling: * <code>p1.subtract(p2).getNorm()</code> except that no intermediate Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3D.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3D.java?rev=1131152&r1=1131151&r2=1131152&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3D.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3D.java Fri Jun 3 19:22:20 2011 @@ -35,9 +35,6 @@ import org.apache.commons.math.util.Math */ public class Vector3D implements Serializable, Vector<Euclidean3D> { - /** Serializable version id. */ - private static final long serialVersionUID = 1313493323784566947L; - /** Null vector (coordinates: 0, 0, 0). */ public static final Vector3D ZERO = new Vector3D(0, 0, 0); @@ -73,7 +70,8 @@ public class Vector3D implements Seriali new Vector3D(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY); /** Serializable version identifier. */ - + private static final long serialVersionUID = 1313493323784566947L; + /** Abscissa. */ private final double x; @@ -424,7 +422,10 @@ public class Vector3D implements Seriali return x * v3.x + y * v3.y + z * v3.z; } - /** {@inheritDoc} */ + /** Compute the cross-product of the instance with another vector. + * @param v other vectorvector + * @return the cross product this ^ v as a new Vector3D + */ public Vector3D crossProduct(final Vector<Euclidean3D> v) { final Vector3D v3 = (Vector3D) v; Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2D.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2D.java?rev=1131152&r1=1131151&r2=1131152&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2D.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2D.java Fri Jun 3 19:22:20 2011 @@ -18,6 +18,8 @@ package org.apache.commons.math.geometry import java.text.NumberFormat; +import org.apache.commons.math.exception.MathArithmeticException; +import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.geometry.Space; import org.apache.commons.math.geometry.Vector; import org.apache.commons.math.util.FastMath; @@ -25,7 +27,7 @@ import org.apache.commons.math.util.Math /** This class represents a 2D vector. * <p>Instances of this class are guaranteed to be immutable.</p> - * @version $Id:$ + * @version $Id$ * @since 3.0 */ public class Vector2D implements Vector<Euclidean2D> { @@ -152,11 +154,6 @@ public class Vector2D implements Vector< } /** {@inheritDoc} */ - public Vector2D toVector() { - return new Vector2D(x, y); - } - - /** {@inheritDoc} */ public double getNorm1() { return FastMath.abs(x) + FastMath.abs(y); } @@ -201,6 +198,24 @@ public class Vector2D implements Vector< } /** {@inheritDoc} */ + public Vector2D normalize() { + double s = getNorm(); + if (s == 0) { + throw new MathArithmeticException(LocalizedFormats.CANNOT_NORMALIZE_A_ZERO_NORM_VECTOR); + } + return scalarMultiply(1 / s); + } + /** {@inheritDoc} */ + public Vector2D negate() { + return new Vector2D(-x, -y); + } + + /** {@inheritDoc} */ + public Vector2D scalarMultiply(double a) { + return new Vector2D(a * x, a * y); + } + + /** {@inheritDoc} */ public boolean isNaN() { return Double.isNaN(x) || Double.isNaN(y); } @@ -242,6 +257,12 @@ public class Vector2D implements Vector< return dx * dx + dy * dy; } + /** {@inheritDoc} */ + public double dotProduct(final Vector<Euclidean2D> v) { + final Vector2D v2 = (Vector2D) v; + return x * v2.x + y * v2.y; + } + /** Compute the distance between two vectors according to the L<sub>2</sub> norm. * <p>Calling this method is equivalent to calling: * <code>p1.subtract(p2).getNorm()</code> except that no intermediate