mirror of
https://github.com/apache/commons-math.git
synced 2025-02-06 01:59:13 +00:00
added missing methods in Vector1D/2D/3D
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1131152 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
540bf1525f
commit
356a99a7bb
@ -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> extends Serializable {
|
||||
*/
|
||||
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> extends Serializable {
|
||||
*/
|
||||
double distanceSq(Vector<S> v);
|
||||
|
||||
/** Get a string representation of this vector.
|
||||
* @param format the custom format for components.
|
||||
/** Compute the dot-product of the instance and another vector.
|
||||
* @param v second vector
|
||||
* @return the dot product this.v
|
||||
*/
|
||||
public String toString(final NumberFormat format);
|
||||
double dotProduct(Vector<S> v);
|
||||
|
||||
/** Get a string representation of this vector.
|
||||
* @param format the custom format for components
|
||||
* @return a string representation of this vector
|
||||
*/
|
||||
String toString(final NumberFormat format);
|
||||
|
||||
}
|
||||
|
@ -18,14 +18,16 @@ package org.apache.commons.math.geometry.euclidean.oned;
|
||||
|
||||
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> {
|
||||
@ -180,6 +182,24 @@ public class Vector1D implements Vector<Euclidean1D> {
|
||||
return new Vector1D(x - factor * v1.getX());
|
||||
}
|
||||
|
||||
/** {@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<Euclidean1D> {
|
||||
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
|
||||
|
@ -35,9 +35,6 @@ import org.apache.commons.math.util.MathUtils;
|
||||
*/
|
||||
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 Serializable, Vector<Euclidean3D> {
|
||||
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 Serializable, Vector<Euclidean3D> {
|
||||
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;
|
||||
|
||||
|
@ -18,6 +18,8 @@ package org.apache.commons.math.geometry.euclidean.twod;
|
||||
|
||||
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.MathUtils;
|
||||
|
||||
/** 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> {
|
||||
@ -151,11 +153,6 @@ public class Vector2D implements Vector<Euclidean2D> {
|
||||
return ZERO;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public Vector2D toVector() {
|
||||
return new Vector2D(x, y);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public double getNorm1() {
|
||||
return FastMath.abs(x) + FastMath.abs(y);
|
||||
@ -200,6 +197,24 @@ public class Vector2D implements Vector<Euclidean2D> {
|
||||
return new Vector2D(x - factor * v2.getX(), y - factor * v2.getY());
|
||||
}
|
||||
|
||||
/** {@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<Euclidean2D> {
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user