KickJava   Java API By Example, From Geeks To Geeks.

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


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

16 public class IntegerVector extends AbstractIntegerVector {
17         /**
18         * Array containing the components of the vector.
19         */

20         protected int vector[];
21         /**
22         * Constructs an empty vector.
23         * @param dim the dimension of the vector.
24         */

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

33         public IntegerVector(final int array[]) {
34                 super(array.length);
35                 vector=array;
36         }
37         /**
38         * Compares two integer vectors for equality.
39         * @param a an integer vector
40         */

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

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

70         public AbstractDoubleVector toDoubleVector() {
71                 final double array[]=new double[N];
72                 for(int i=0;i<N;i++)
73                         array[i]=vector[i];
74                 return new DoubleVector(array);
75         }
76         /**
77         * Converts this vector to a complex vector.
78         * @return a complex vector
79         */

80         public AbstractComplexVector toComplexVector() {
81                 final double arrayRe[]=new double[N];
82                 for(int i=0;i<N;i++)
83                         arrayRe[i]=vector[i];
84                 return new ComplexVector(arrayRe,new double[N]);
85         }
86         /**
87         * Returns a component of this vector.
88         * @param n index of the vector component
89         * @exception VectorDimensionException If attempting to access an invalid component.
90         */

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

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

112         public double norm(final 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         */

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

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

145         /**
146         * Returns the negative of this vector.
147         */

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

158         /**
159         * Returns the addition of this vector and another.
160         */

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

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

199         /**
200         * Returns the subtraction of this vector by another.
201         */

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

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

240         /**
241         * Returns the multiplication of this vector by a scalar.
242         */

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

253         public AbstractIntegerVector scalarMultiply(final int x) {
254                 final int array[]=new int[N];
255                 array[0]=x*vector[0];
256                 for(int i=1;i<N;i++)
257                         array[i]=x*vector[i];
258                 return new IntegerVector(array);
259         }
260
261 // SCALAR PRODUCT
262

263         /**
264         * Returns the scalar product of this vector and another.
265         * @param v an integer vector
266         * @exception VectorDimensionException If the vectors are different sizes.
267         */

268         public int scalarProduct(final AbstractIntegerVector v) {
269                 if(v instanceof IntegerVector)
270                         return scalarProduct((IntegerVector)v);
271                 else {
272                         if(N==v.N) {
273                                 int answer=vector[0]*v.getComponent(0);
274                                 for(int i=1;i<N;i++)
275                                         answer+=vector[i]*v.getComponent(i);
276                                 return answer;
277                         } else
278                                 throw new VectorDimensionException("Vectors are different sizes.");
279                 }
280         }
281         public int scalarProduct(final IntegerVector v) {
282                 if(N==v.N) {
283                         int answer=vector[0]*v.vector[0];
284                         for(int i=1;i<N;i++)
285                                 answer+=vector[i]*v.vector[i];
286                         return answer;
287                 } else
288                         throw new VectorDimensionException("Vectors are different sizes.");
289         }
290 }
291
292
Popular Tags