001: package JSci.maths.vectors;
002:
003: import JSci.GlobalSettings;
004: import JSci.maths.ExtraMath;
005: import JSci.maths.Mapping;
006: import JSci.maths.algebras.*;
007: import JSci.maths.fields.*;
008: import JSci.maths.groups.AbelianGroup;
009:
010: /**
011: * The AbstractDoubleVector class encapsulates vectors containing doubles.
012: * @version 1.0
013: * @author Mark Hale
014: */
015: public abstract class AbstractDoubleVector extends MathVector implements
016: BanachSpace.Member {
017: protected AbstractDoubleVector(final int dim) {
018: super (dim);
019: }
020:
021: /**
022: * Compares two double vectors for equality.
023: * Two vectors are considered to be equal if the norm of their difference is within the zero tolerance.
024: * @param obj a double vector
025: */
026: public final boolean equals(Object obj) {
027: return equals(obj, GlobalSettings.ZERO_TOL);
028: }
029:
030: public boolean equals(Object obj, double tol) {
031: if (obj != null && (obj instanceof AbstractDoubleVector)) {
032: final AbstractDoubleVector vec = (AbstractDoubleVector) obj;
033: return (this .dimension() == vec.dimension() && this
034: .subtract(vec).norm() <= tol);
035: } else
036: return false;
037: }
038:
039: /**
040: * Returns a comma delimited string representing the value of this vector.
041: */
042: public String toString() {
043: final StringBuffer buf = new StringBuffer(8 * N);
044: int i;
045: for (i = 0; i < N - 1; i++) {
046: buf.append(getComponent(i));
047: buf.append(',');
048: }
049: buf.append(getComponent(i));
050: return buf.toString();
051: }
052:
053: /**
054: * Returns a hashcode for this vector.
055: */
056: public int hashCode() {
057: return (int) Math.exp(norm());
058: }
059:
060: /**
061: * Returns a component of this vector.
062: * @param n index of the vector component.
063: * @exception VectorDimensionException If attempting to access an invalid component.
064: */
065: public abstract double getComponent(int n);
066:
067: /**
068: * Sets the value of a component of this vector.
069: * @param n index of the vector component.
070: * @param x a number.
071: * @exception VectorDimensionException If attempting to access an invalid component.
072: */
073: public abstract void setComponent(int n, double x);
074:
075: public Object getSet() {
076: throw new RuntimeException("Not implemented: file bug");
077: }
078:
079: /**
080: * Returns the l<sup>n</sup>-norm.
081: * @jsci.planetmath VectorPnorm
082: */
083: public double norm(int n) {
084: double answer = Math.pow(Math.abs(getComponent(0)), n);
085: for (int i = 1; i < N; i++)
086: answer += Math.pow(Math.abs(getComponent(i)), n);
087: return Math.pow(answer, 1.0 / n);
088: }
089:
090: /**
091: * Returns the l<sup>2</sup>-norm (magnitude).
092: * @jsci.planetmath VectorPnorm
093: */
094: public double norm() {
095: double answer = getComponent(0);
096: for (int i = 1; i < N; i++)
097: answer = ExtraMath.hypot(answer, getComponent(i));
098: return answer;
099: }
100:
101: /**
102: * Returns the l<sup><img border=0 alt="infinity" src="doc-files/infinity.gif"></sup>-norm.
103: * @author Taber Smith
104: * @jsci.planetmath VectorPnorm
105: */
106: public double infNorm() {
107: double infNorm = Math.abs(getComponent(0));
108: for (int i = 1; i < N; i++) {
109: final double abs = Math.abs(getComponent(i));
110: if (abs > infNorm)
111: infNorm = abs;
112: }
113: return infNorm;
114: }
115:
116: /**
117: * Returns the mass (l<sup>1</sup>-norm).
118: */
119: public double mass() {
120: double mass = 0.0;
121: for (int i = 1; i < N; i++)
122: mass += getComponent(i);
123: return mass;
124: }
125:
126: //============
127: // OPERATIONS
128: //============
129:
130: /**
131: * Returns the addition of this vector and another.
132: * @param v a double vector.
133: * @exception VectorDimensionException If the vectors are different sizes.
134: */
135: public abstract AbstractDoubleVector add(AbstractDoubleVector v);
136:
137: /**
138: * Returns the subtraction of this vector by another.
139: * @param v a double vector.
140: * @exception VectorDimensionException If the vectors are different sizes.
141: */
142: public abstract AbstractDoubleVector subtract(AbstractDoubleVector v);
143:
144: /**
145: * Returns the multiplication of this vector by a scalar.
146: * @param x a double.
147: */
148: public abstract AbstractDoubleVector scalarMultiply(double x);
149:
150: /**
151: * Returns the division of this vector by a scalar.
152: * @param x a double.
153: * @exception ArithmeticException If divide by zero.
154: */
155: public abstract AbstractDoubleVector scalarDivide(double x);
156:
157: /**
158: * Returns a normalised vector (a vector with norm equal to one).
159: */
160: public AbstractDoubleVector normalize() {
161: return this .scalarDivide(norm());
162: }
163:
164: /**
165: * Returns the scalar product of this vector and another.
166: * @param v a double vector.
167: * @exception VectorDimensionException If the vectors are different sizes.
168: */
169: public abstract double scalarProduct(AbstractDoubleVector v);
170:
171: /**
172: * Applies a function on all the vector components.
173: * @param f a user-defined function.
174: * @return a double vector.
175: */
176: public abstract AbstractDoubleVector mapComponents(final Mapping f);
177: }
|