KickJava   Java API By Example, From Geeks To Geeks.

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


1 package JSci.maths.vectors;
2
3 import JSci.maths.ExtraMath;
4 import JSci.maths.groups.AbelianGroup;
5 import JSci.maths.fields.Ring;
6 import JSci.maths.algebras.Module;
7
8 /**
9 * The AbstractIntegerVector class encapsulates vectors containing integers.
10 * @version 1.0
11 * @author Mark Hale
12 */

13 public abstract class AbstractIntegerVector extends MathVector {
14         protected AbstractIntegerVector(final int dim) {
15                 super(dim);
16         }
17         /**
18         * Compares two integer vectors for equality.
19         * Two vectors are considered to be equal if the norm of their difference is zero.
20         * @param obj an integer vector
21         */

22         public boolean equals(Object JavaDoc obj) {
23                 if(obj != null && (obj instanceof AbstractIntegerVector)) {
24                         final AbstractIntegerVector vec = (AbstractIntegerVector) obj;
25                         return (this.dimension() == vec.dimension() && this.subtract(vec).norm() == 0.0);
26                 } else
27                         return false;
28         }
29         /**
30         * Returns a comma delimited string representing the value of this vector.
31         */

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

45         public int hashCode() {
46                 return (int)Math.exp(norm());
47         }
48         /**
49         * Converts this vector to a double vector.
50         * @return a double vector
51         */

52         public AbstractDoubleVector toDoubleVector() {
53                 final double array[]=new double[N];
54                 for(int i=0;i<N;i++)
55                         array[i]=getComponent(i);
56                 return new DoubleVector(array);
57         }
58         /**
59         * Returns a component of this vector.
60         * @param n index of the vector component
61         * @exception VectorDimensionException If attempting to access an invalid component.
62         */

63         public abstract int getComponent(int n);
64         /**
65         * Sets the value of a component of this vector.
66         * @param n index of the vector component
67         * @param x an integer
68         * @exception VectorDimensionException If attempting to access an invalid component.
69         */

70         public abstract void setComponent(int n, int x);
71     public Object JavaDoc getSet() {
72         throw new RuntimeException JavaDoc("Not implemented: file bug");
73     }
74         /**
75         * Returns the l<sup>n</sup>-norm.
76         */

77         public double norm(final int n) {
78                 double answer = Math.pow(Math.abs(getComponent(0)), n);
79                 for(int i=1; i<N; i++)
80                         answer += Math.pow(Math.abs(getComponent(i)), n);
81                 return Math.pow(answer, 1.0/n);
82         }
83         /**
84         * Returns the l<sup>2</sup>-norm (magnitude).
85         */

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

96         public double infNorm() {
97                 int infNorm = Math.abs(getComponent(0));
98                 for(int i=1; i<N; i++) {
99                         final int abs = Math.abs(getComponent(i));
100                         if(abs > infNorm)
101                                 infNorm = abs;
102                 }
103                 return infNorm;
104         }
105
106 //============
107
// OPERATIONS
108
//============
109

110         /**
111         * Returns the addition of this vector and another.
112         * @param v an integer vector
113         * @exception VectorDimensionException If the vectors are different sizes.
114         */

115         public abstract AbstractIntegerVector add(AbstractIntegerVector v);
116         /**
117         * Returns the subtraction of this vector by another.
118         * @param v an integer vector
119         * @exception VectorDimensionException If the vectors are different sizes.
120         */

121         public abstract AbstractIntegerVector subtract(AbstractIntegerVector v);
122         /**
123         * Returns the multiplication of this vector by a scalar.
124         * @param x an integer
125         */

126         public abstract AbstractIntegerVector scalarMultiply(int x);
127         /**
128         * Returns the scalar product of this vector and another.
129         * @param v an integer vector
130         * @exception VectorDimensionException If the vectors are different sizes.
131         */

132         public abstract int scalarProduct(AbstractIntegerVector v);
133 }
134
135
Popular Tags