KickJava   Java API By Example, From Geeks To Geeks.

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


1 package JSci.maths.vectors;
2
3 import JSci.maths.ExtraMath;
4 import JSci.maths.MathDouble;
5 import JSci.maths.MathInteger;
6 import JSci.maths.Mapping;
7 import JSci.maths.algebras.Module;
8 import JSci.maths.algebras.VectorSpace;
9 import JSci.maths.fields.Ring;
10 import JSci.maths.fields.Field;
11 import JSci.maths.groups.AbelianGroup;
12
13 /**
14 * An array-based implementation of a double vector.
15 * @version 2.1
16 * @author Mark Hale
17 */

18 public class DoubleVector extends AbstractDoubleVector {
19         /**
20         * Array containing the components of the vector.
21         */

22         protected double vector[];
23         /**
24         * Constructs an empty vector.
25         * @param dim the dimension of the vector.
26         */

27         public DoubleVector(final int dim) {
28                 super(dim);
29                 vector=new double[dim];
30         }
31         /**
32         * Constructs a vector by wrapping an array.
33         * @param array an assigned value.
34         */

35         public DoubleVector(final double array[]) {
36                 super(array.length);
37                 vector=array;
38         }
39         /**
40         * Compares two double vectors for equality.
41         * @param a a double vector.
42         */

43         public boolean equals(Object JavaDoc a, double tol) {
44                 if(a!=null && (a instanceof AbstractDoubleVector) && N==((AbstractDoubleVector)a).N) {
45                         final AbstractDoubleVector dv=(AbstractDoubleVector)a;
46                         double sumSqr = 0.0;
47                         for(int i=0; i<N; i++) {
48                                 double delta = vector[i] - dv.getComponent(i);
49                                 sumSqr += delta*delta;
50                         }
51                         return (sumSqr <= tol*tol);
52                 } else
53                         return false;
54         }
55         /**
56         * Returns a comma delimited string representing the value of this vector.
57         */

58         public String JavaDoc toString() {
59                 final StringBuffer JavaDoc buf=new StringBuffer JavaDoc(8*N);
60                 int i;
61                 for(i=0;i<N-1;i++) {
62                         buf.append(vector[i]);
63                         buf.append(',');
64                 }
65                 buf.append(vector[i]);
66                 return buf.toString();
67         }
68         /**
69         * Converts this vector to an integer vector.
70         * @return an integer vector.
71         */

72         public AbstractIntegerVector toIntegerVector() {
73                 final int array[]=new int[N];
74                 for(int i=0;i<N;i++)
75                         array[i]=Math.round((float)vector[i]);
76                 return new IntegerVector(array);
77         }
78         /**
79         * Converts this vector to a complex vector.
80         * @return a complex vector.
81         */

82         public AbstractComplexVector toComplexVector() {
83                 return new ComplexVector(vector,new double[N]);
84         }
85         /**
86         * Returns a component of this vector.
87         * @param n index of the vector component.
88         * @exception VectorDimensionException If attempting to access an invalid component.
89         */

90         public double getComponent(final int n) {
91                 if(n>=0 && n<N)
92                         return vector[n];
93                 else
94                         throw new VectorDimensionException(getInvalidComponentMsg(n));
95         }
96         /**
97         * Sets the value of a component of this vector.
98         * @param n index of the vector component.
99         * @param x a number.
100         * @exception VectorDimensionException If attempting to access an invalid component.
101         */

102         public void setComponent(final int n, final double x) {
103                 if(n>=0 && n<N)
104                         vector[n]=x;
105                 else
106                         throw new VectorDimensionException(getInvalidComponentMsg(n));
107         }
108         /**
109         * Returns the l<sup>n</sup>-norm.
110         * @jsci.planetmath VectorPnorm
111         */

112         public double norm(int n) {
113                 double answer=Math.pow(Math.abs(vector[0]), n);
114                 for(int i=1; i<N; i++)
115                         answer+=Math.pow(Math.abs(vector[i]), n);
116                 return Math.pow(answer, 1.0/n);
117         }
118         /**
119         * Returns the l<sup>2</sup>-norm (magnitude).
120         * @jsci.planetmath VectorPnorm
121         */

122         public double norm() {
123                 double answer=vector[0];
124                 for(int i=1;i<N;i++)
125                         answer=ExtraMath.hypot(answer,vector[i]);
126                 return answer;
127         }
128         /**
129         * Returns the l<sup><img border=0 alt="infinity" SRC="doc-files/infinity.gif"></sup>-norm.
130         * @author Taber Smith
131         * @jsci.planetmath VectorPnorm
132         */

133         public double infNorm() {
134                 double infNorm = Math.abs(vector[0]);
135                 for(int i=1; i<N; i++) {
136                         final double abs = Math.abs(vector[i]);
137                         if(abs > infNorm)
138                                 infNorm = abs;
139                 }
140                 return infNorm;
141         }
142
143 //============
144
// OPERATIONS
145
//============
146

147         /**
148         * Returns the negative of this vector.
149         */

150         public AbelianGroup.Member negate() {
151                 final double array[]=new double[N];
152                 array[0]=-vector[0];
153                 for(int i=1;i<N;i++)
154                         array[i]=-vector[i];
155                 return new DoubleVector(array);
156         }
157
158 // ADDITION
159

160         /**
161         * Returns the addition of this vector and another.
162         */

163         public AbelianGroup.Member add(final AbelianGroup.Member v) {
164                 if(v instanceof AbstractDoubleVector)
165                         return add((AbstractDoubleVector)v);
166                 else
167                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
168         }
169         /**
170         * Returns the addition of this vector and another.
171         * @param v a double vector.
172         * @exception VectorDimensionException If the vectors are different sizes.
173         */

174         public AbstractDoubleVector add(AbstractDoubleVector v) {
175                 if(v instanceof DoubleVector)
176                         return add((DoubleVector)v);
177                 else {
178                         if(N==v.N) {
179                                 final double array[]=new double[N];
180                                 array[0]=vector[0]+v.getComponent(0);
181                                 for(int i=1;i<N;i++)
182                                         array[i]=vector[i]+v.getComponent(i);
183                                 return new DoubleVector(array);
184                         } else
185                                 throw new VectorDimensionException("Vectors are different sizes.");
186                 }
187         }
188         public DoubleVector add(final DoubleVector v) {
189                 if(N==v.N) {
190                         final double array[]=new double[N];
191                         array[0]=vector[0]+v.vector[0];
192                         for(int i=1;i<N;i++)
193                                 array[i]=vector[i]+v.vector[i];
194                         return new DoubleVector(array);
195                 } else
196                         throw new VectorDimensionException("Vectors are different sizes.");
197         }
198
199 // SUBTRACTION
200

201         /**
202         * Returns the subtraction of this vector by another.
203         */

204         public AbelianGroup.Member subtract(final AbelianGroup.Member v) {
205                 if(v instanceof AbstractDoubleVector)
206                         return subtract((AbstractDoubleVector)v);
207                 else
208                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
209         }
210         /**
211         * Returns the subtraction of this vector by another.
212         * @param v a double vector.
213         * @exception VectorDimensionException If the vectors are different sizes.
214         */

215         public AbstractDoubleVector subtract(final AbstractDoubleVector v) {
216                 if(v instanceof DoubleVector)
217                         return subtract((DoubleVector)v);
218                 else {
219                         if(N==v.N) {
220                                 final double array[]=new double[N];
221                                 array[0]=vector[0]-v.getComponent(0);
222                                 for(int i=1;i<N;i++)
223                                         array[i]=vector[i]-v.getComponent(i);
224                                 return new DoubleVector(array);
225                         } else
226                                 throw new VectorDimensionException("Vectors are different sizes.");
227                 }
228         }
229         public DoubleVector subtract(final DoubleVector v) {
230                 if(N==v.N) {
231                         final double array[]=new double[N];
232                         array[0]=vector[0]-v.vector[0];
233                         for(int i=1;i<N;i++)
234                                 array[i]=vector[i]-v.vector[i];
235                         return new DoubleVector(array);
236                 } else
237                         throw new VectorDimensionException("Vectors are different sizes.");
238         }
239
240 // SCALAR MULTIPLICATION
241

242         /**
243         * Returns the multiplication of this vector by a scalar.
244         */

245         public Module.Member scalarMultiply(Ring.Member x) {
246                 if(x instanceof MathDouble)
247                         return scalarMultiply(((MathDouble)x).value());
248                 else if(x instanceof MathInteger)
249                         return scalarMultiply(((MathInteger)x).value());
250                 else
251                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
252         }
253         /**
254         * Returns the multiplication of this vector by a scalar.
255         * @param x a double.
256         */

257         public AbstractDoubleVector scalarMultiply(final double x) {
258                 final double array[]=new double[N];
259                 array[0]=x*vector[0];
260                 for(int i=1;i<N;i++)
261                         array[i]=x*vector[i];
262                 return new DoubleVector(array);
263         }
264
265 // SCALAR DIVISION
266

267         /**
268         * Returns the division of this vector by a scalar.
269         */

270         public VectorSpace.Member scalarDivide(Field.Member x) {
271                 if(x instanceof MathDouble)
272                         return scalarDivide(((MathDouble)x).value());
273                 else
274                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
275         }
276         /**
277         * Returns the division of this vector by a scalar.
278         * @param x a double.
279         * @exception ArithmeticException If divide by zero.
280         */

281         public AbstractDoubleVector scalarDivide(final double x) {
282                 final double array[]=new double[N];
283                 array[0]=vector[0]/x;
284                 for(int i=1;i<N;i++)
285                         array[i]=vector[i]/x;
286                 return new DoubleVector(array);
287         }
288
289 // SCALAR PRODUCT
290

291         /**
292         * Returns the scalar product of this vector and another.
293         * @param v a double vector.
294         * @exception VectorDimensionException If the vectors are different sizes.
295         */

296         public double scalarProduct(final AbstractDoubleVector v) {
297                 if(v instanceof DoubleVector)
298                         return scalarProduct((DoubleVector)v);
299                 else {
300                         if(N==v.N) {
301                                 double answer=vector[0]*v.getComponent(0);
302                                 for(int i=1;i<N;i++)
303                                         answer+=vector[i]*v.getComponent(i);
304                                 return answer;
305                         } else
306                                 throw new VectorDimensionException("Vectors are different sizes.");
307                 }
308         }
309         public double scalarProduct(final DoubleVector v) {
310                 if(N==v.N) {
311                         double answer=vector[0]*v.vector[0];
312                         for(int i=1;i<N;i++)
313                                 answer+=vector[i]*v.vector[i];
314                         return answer;
315                 } else
316                         throw new VectorDimensionException("Vectors are different sizes.");
317         }
318
319 // MAP COMPONENTS
320

321         /**
322         * Applies a function on all the vector components.
323         * @param f a user-defined function.
324         * @return a double vector.
325         */

326         public AbstractDoubleVector mapComponents(final Mapping f) {
327                 final double array[]=new double[N];
328                 array[0]=f.map(vector[0]);
329                 for(int i=1;i<N;i++)
330                         array[i]=f.map(vector[i]);
331                 return new DoubleVector(array);
332         }
333 }
334
335
Popular Tags