KickJava   Java API By Example, From Geeks To Geeks.

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


1 package JSci.maths.vectors;
2
3 import JSci.maths.Mapping;
4 import JSci.maths.MathInteger;
5 import JSci.maths.MathDouble;
6 import JSci.maths.groups.AbelianGroup;
7 import JSci.maths.algebras.Module;
8 import JSci.maths.fields.Ring;
9
10 /**
11 * An optimised implementation of a 2D integer vector.
12 * @version 2.0
13 * @author Mark Hale
14 */

15 public final class Integer2Vector extends AbstractIntegerVector {
16         protected int x;
17         protected int y;
18         /**
19         * Constructs an empty 2-vector.
20         */

21         public Integer2Vector() {
22                 super(2);
23         }
24         /**
25         * Constructs a 2-vector.
26         * @param x x coordinate.
27         * @param y y coordinate.
28         */

29         public Integer2Vector(final int x, final int y) {
30                 this();
31                 this.x = x;
32                 this.y = y;
33         }
34         /**
35         * Constructs a 2-vector.
36         */

37         public Integer2Vector(int[] array) {
38                 this();
39                 x = array[0];
40                 y = array[1];
41         }
42         /**
43         * Compares two integer vectors for equality.
44         * @param obj a integer 2-vector
45         */

46     public boolean equals(Object JavaDoc obj, double tol) {
47                 if(obj != null && (obj instanceof Integer2Vector)) {
48                         final Integer2Vector vec = (Integer2Vector) obj;
49                         int dx = x - vec.x;
50                         int dy = y - vec.y;
51                         return (dx*dx
52                          + dy*dy == 0);
53                 } else
54                         return false;
55         }
56         /**
57         * Returns a comma delimited string representing the value of this vector.
58         */

59         public String JavaDoc toString() {
60                 final StringBuffer JavaDoc buf = new StringBuffer JavaDoc(15);
61                 buf.append(x).append(',').append(y);
62                 return buf.toString();
63         }
64         /**
65         * Converts this 2-vector to a double 2-vector.
66         * @return a double 2-vector
67         */

68         public AbstractDoubleVector toDoubleVector() {
69                 return new Double2Vector(
70                         x,
71                         y
72                 );
73         }
74         /**
75         * Converts this 2-vector to a complex 2-vector.
76         * @return a complex 2-vector
77         */

78         public AbstractComplexVector toComplexVector() {
79                 return new Complex2Vector(
80                         x, 0.0,
81                         y, 0.0
82                 );
83         }
84         /**
85         * Returns a component of this vector.
86         * @param n index of the vector component
87         * @exception VectorDimensionException If attempting to access an invalid component.
88         */

89         public int getComponent(final int n) {
90                 switch(n) {
91                         case 0 : return x;
92                         case 1 : return y;
93                         default : throw new VectorDimensionException("Invalid component.");
94                 }
95         }
96         /**
97         * Sets the value of a component of this vector.
98         * Should only be used to initialise this vector.
99         * @param n index of the vector component
100         * @param value a number
101         * @exception VectorDimensionException If attempting to access an invalid component.
102         */

103         public void setComponent(final int n, final int value) {
104                 switch(n) {
105                         case 0 : x = value; break;
106                         case 1 : y = value; break;
107                         default : throw new VectorDimensionException("Invalid component.");
108                 }
109         }
110         /**
111         * Returns the l<sup>n</sup>-norm.
112         */

113         public double norm(final int n) {
114                 final double answer = Math.pow(Math.abs(x), n)
115                         +Math.pow(Math.abs(y), 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                 return Math.sqrt(
123                         x*x
124                         +y*y
125                 );
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 = 0;
133                 int abs;
134                 abs = Math.abs(x);
135                 if(abs > infNorm)
136                         infNorm = abs;
137                 abs = Math.abs(y);
138                 if(abs > infNorm)
139                         infNorm = abs;
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                 return new Integer2Vector(
152                         -x,
153                         -y
154                 );
155         }
156
157 // ADDITION
158

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

162         public AbelianGroup.Member add(final AbelianGroup.Member vec) {
163                 if(vec instanceof AbstractIntegerVector)
164                         return add((AbstractIntegerVector)vec);
165                 else
166                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
167         }
168         /**
169         * Returns the addition of this vector and another.
170         * @param vec a integer 2-vector
171         */

172         public AbstractIntegerVector add(final AbstractIntegerVector vec) {
173                 if(vec.N == 2) {
174                         return new Integer2Vector(
175                                 x+vec.getComponent(0),
176                                 y+vec.getComponent(1)
177                         );
178                 } else
179                         throw new VectorDimensionException("Vectors are different sizes.");
180         }
181
182 // SUBTRACTION
183

184         /**
185         * Returns the subtraction of this vector by another.
186         */

187         public AbelianGroup.Member subtract(final AbelianGroup.Member vec) {
188                 if(vec instanceof AbstractIntegerVector)
189                         return subtract((AbstractIntegerVector)vec);
190                 else
191                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
192         }
193         /**
194         * Returns the subtraction of this vector by another.
195         * @param vec a integer 2-vector
196         */

197         public AbstractIntegerVector subtract(final AbstractIntegerVector vec) {
198                 if(vec.N == 2) {
199                         return new Integer2Vector(
200                                 x-vec.getComponent(0),
201                                 y-vec.getComponent(1)
202                         );
203                 } else
204                         throw new VectorDimensionException("Vectors are different sizes.");
205         }
206
207 // SCALAR MULTIPLICATION
208

209         /**
210         * Returns the multiplication of this vector by a scalar.
211         */

212         public Module.Member scalarMultiply(Ring.Member x) {
213                 if(x instanceof MathInteger)
214                         return scalarMultiply(((MathInteger)x).value());
215                 else
216                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method.");
217         }
218         /**
219         * Returns the multiplication of this vector by a scalar.
220         * @param k a integer
221         * @return a integer 2-vector
222         */

223         public AbstractIntegerVector scalarMultiply(final int k) {
224                 return new Integer2Vector(
225                         k*x,
226                         k*y
227                 );
228         }
229
230
231 // SCALAR PRODUCT
232

233         /**
234         * Returns the scalar product of this vector and another.
235         * @param vec a integer 2-vector
236         */

237         public int scalarProduct(final AbstractIntegerVector vec) {
238                 if(vec.N == 2) {
239                         return x*vec.getComponent(0)
240                                 +y*vec.getComponent(1);
241                 } else
242                         throw new VectorDimensionException("Vectors are different sizes.");
243         }
244
245
246 }
247
248
Popular Tags