KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > vectors > AbstractDoubleVector


1 package JSci.maths.vectors;
2
3 import JSci.GlobalSettings;
4 import JSci.maths.ExtraMath;
5 import JSci.maths.Mapping;
6 import JSci.maths.algebras.*;
7 import JSci.maths.fields.*;
8 import JSci.maths.groups.AbelianGroup;
9
10 /**
11 * The AbstractDoubleVector class encapsulates vectors containing doubles.
12 * @version 1.0
13 * @author Mark Hale
14 */

15 public abstract class AbstractDoubleVector extends MathVector implements BanachSpace.Member {
16         protected AbstractDoubleVector(final int dim) {
17                 super(dim);
18         }
19         /**
20         * Compares two double vectors for equality.
21         * Two vectors are considered to be equal if the norm of their difference is within the zero tolerance.
22         * @param obj a double vector
23         */

24         public final boolean equals(Object JavaDoc obj) {
25         return equals(obj, GlobalSettings.ZERO_TOL);
26         }
27     public boolean equals(Object JavaDoc obj, double tol) {
28                 if(obj != null && (obj instanceof AbstractDoubleVector)) {
29                         final AbstractDoubleVector vec = (AbstractDoubleVector) obj;
30                         return (this.dimension() == vec.dimension() && this.subtract(vec).norm() <= tol);
31                 } else
32                         return false;
33         }
34         /**
35         * Returns a comma delimited string representing the value of this vector.
36         */

37         public String JavaDoc toString() {
38                 final StringBuffer JavaDoc buf = new StringBuffer JavaDoc(8*N);
39                 int i;
40                 for(i=0; i<N-1; i++) {
41                         buf.append(getComponent(i));
42                         buf.append(',');
43                 }
44                 buf.append(getComponent(i));
45                 return buf.toString();
46         }
47         /**
48         * Returns a hashcode for this vector.
49         */

50         public int hashCode() {
51                 return (int)Math.exp(norm());
52         }
53         /**
54         * Returns a component of this vector.
55         * @param n index of the vector component.
56         * @exception VectorDimensionException If attempting to access an invalid component.
57         */

58         public abstract double getComponent(int n);
59         /**
60         * Sets the value of a component of this vector.
61         * @param n index of the vector component.
62         * @param x a number.
63         * @exception VectorDimensionException If attempting to access an invalid component.
64         */

65         public abstract void setComponent(int n, double x);
66     public Object JavaDoc getSet() {
67         throw new RuntimeException JavaDoc("Not implemented: file bug");
68     }
69         /**
70         * Returns the l<sup>n</sup>-norm.
71         * @jsci.planetmath VectorPnorm
72         */

73         public double norm(int n) {
74                 double answer = Math.pow(Math.abs(getComponent(0)), n);
75                 for(int i=1; i<N; i++)
76                         answer += Math.pow(Math.abs(getComponent(i)), n);
77                 return Math.pow(answer, 1.0/n);
78         }
79         /**
80         * Returns the l<sup>2</sup>-norm (magnitude).
81         * @jsci.planetmath VectorPnorm
82         */

83         public double norm() {
84                 double answer = getComponent(0);
85                 for(int i=1; i<N; i++)
86                         answer = ExtraMath.hypot(answer, getComponent(i));
87                 return answer;
88         }
89         /**
90         * Returns the l<sup><img border=0 alt="infinity" SRC="doc-files/infinity.gif"></sup>-norm.
91         * @author Taber Smith
92         * @jsci.planetmath VectorPnorm
93         */

94         public double infNorm() {
95                 double infNorm = Math.abs(getComponent(0));
96                 for(int i=1; i<N; i++) {
97                         final double abs = Math.abs(getComponent(i));
98                         if(abs > infNorm)
99                                 infNorm = abs;
100                 }
101                 return infNorm;
102         }
103         /**
104         * Returns the mass (l<sup>1</sup>-norm).
105         */

106         public double mass() {
107                 double mass=0.0;
108                 for(int i=1; i<N; i++)
109             mass+=getComponent(i);
110         return mass;
111         }
112
113 //============
114
// OPERATIONS
115
//============
116

117         /**
118         * Returns the addition of this vector and another.
119         * @param v a double vector.
120         * @exception VectorDimensionException If the vectors are different sizes.
121         */

122         public abstract AbstractDoubleVector add(AbstractDoubleVector v);
123         /**
124         * Returns the subtraction of this vector by another.
125         * @param v a double vector.
126         * @exception VectorDimensionException If the vectors are different sizes.
127         */

128         public abstract AbstractDoubleVector subtract(AbstractDoubleVector v);
129         /**
130         * Returns the multiplication of this vector by a scalar.
131         * @param x a double.
132         */

133         public abstract AbstractDoubleVector scalarMultiply(double x);
134         /**
135         * Returns the division of this vector by a scalar.
136         * @param x a double.
137         * @exception ArithmeticException If divide by zero.
138         */

139         public abstract AbstractDoubleVector scalarDivide(double x);
140         /**
141         * Returns a normalised vector (a vector with norm equal to one).
142         */

143         public AbstractDoubleVector normalize() {
144                 return this.scalarDivide(norm());
145         }
146         /**
147         * Returns the scalar product of this vector and another.
148         * @param v a double vector.
149         * @exception VectorDimensionException If the vectors are different sizes.
150         */

151         public abstract double scalarProduct(AbstractDoubleVector v);
152         /**
153         * Applies a function on all the vector components.
154         * @param f a user-defined function.
155         * @return a double vector.
156         */

157         public abstract AbstractDoubleVector mapComponents(final Mapping f);
158 }
159
160
Popular Tags