KickJava   Java API By Example, From Geeks To Geeks.

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


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

15 public abstract class AbstractComplexVector extends MathVector implements HilbertSpace.Member {
16         protected AbstractComplexVector(final int dim) {
17                 super(dim);
18         }
19         /**
20         * Compares two complex 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 complex 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 AbstractComplexVector)) {
29                         final AbstractComplexVector vec = (AbstractComplexVector) 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(12*N);
39                 int i;
40                 for(i=0; i<N-1; i++) {
41                         buf.append(getComponent(i).toString());
42                         buf.append(',');
43                 }
44                 buf.append(getComponent(i).toString());
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 the real part of this complex vector.
55         */

56         public abstract AbstractDoubleVector real();
57         /**
58         * Returns the imaginary part of this complex vector.
59         */

60         public abstract AbstractDoubleVector imag();
61         /**
62         * Returns a component of this vector.
63         * @param n index of the vector component
64         * @exception VectorDimensionException If attempting to access an invalid component.
65         */

66         public abstract Complex getComponent(int n);
67         public abstract double getRealComponent(int n);
68         public abstract double getImagComponent(int n);
69         /**
70         * Sets the value of a component of this vector.
71         * Should only be used to initialise this vector.
72         * @param n index of the vector component
73         * @param z a complex number
74         * @exception VectorDimensionException If attempting to access an invalid component.
75         */

76         public abstract void setComponent(int n, Complex z);
77         /**
78         * Sets the value of a component of this vector.
79         * Should only be used to initialise this vector.
80         * @param n index of the vector component
81         * @param x the real part of a complex number
82         * @param y the imaginary part of a complex number
83         * @exception VectorDimensionException If attempting to access an invalid component.
84         */

85         public abstract void setComponent(int n, double x, double y);
86     public Object JavaDoc getSet() {
87         throw new RuntimeException JavaDoc("Not implemented: file bug");
88     }
89         /**
90         * Returns the l<sup>2</sup>-norm (magnitude).
91         */

92         public double norm() {
93                 double answer = getRealComponent(0)*getRealComponent(0) + getImagComponent(0)*getImagComponent(0);
94                 for(int i=1;i<N;i++)
95                         answer += getRealComponent(i)*getRealComponent(i) + getImagComponent(i)*getImagComponent(i);
96                 return Math.sqrt(answer);
97         }
98         /**
99         * Returns the l<sup><img border=0 alt="infinity" SRC="doc-files/infinity.gif"></sup>-norm.
100         */

101         public double infNorm() {
102                 double infNorm = getComponent(0).mod();
103                 for(int i=1;i<N;i++) {
104                         double mod = getComponent(i).mod();
105                         if(mod > infNorm)
106                                 infNorm = mod;
107                 }
108                 return infNorm;
109         }
110
111 //============
112
// OPERATIONS
113
//============
114

115         /**
116         * Returns the complex conjugate of this vector.
117         */

118         public abstract AbstractComplexVector conjugate();
119         /**
120         * Returns the addition of this vector and another.
121         * @param v a complex vector
122         * @exception VectorDimensionException If the vectors are different sizes.
123         */

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

130         public abstract AbstractComplexVector subtract(AbstractComplexVector v);
131         /**
132         * Returns the multiplication of this vector by a scalar.
133         * @param z a complex number
134         */

135         public abstract AbstractComplexVector scalarMultiply(Complex z);
136         /**
137         * Returns the multiplication of this vector by a scalar.
138         * @param x a double
139         */

140         public abstract AbstractComplexVector scalarMultiply(double x);
141         /**
142         * Returns the division of this vector by a scalar.
143         * @param z a complex number
144         * @exception ArithmeticException If divide by zero.
145         */

146         public abstract AbstractComplexVector scalarDivide(Complex z);
147         /**
148         * Returns the division of this vector by a scalar.
149         * @param x a double
150         * @exception ArithmeticException If divide by zero.
151         */

152         public abstract AbstractComplexVector scalarDivide(double x);
153         /**
154         * Returns a normalised vector (a vector with norm equal to one).
155         */

156         public AbstractComplexVector normalize() {
157                 return this.scalarDivide(norm());
158         }
159         /**
160         * Returns the scalar product of this vector and another.
161         * @param v a complex vector
162         * @exception VectorDimensionException If the vectors are different sizes.
163         */

164         public abstract Complex scalarProduct(AbstractComplexVector v);
165         /**
166         * Applies a function on all the vector components.
167         * @param f a user-defined function
168         * @return a complex vector
169         */

170         public abstract AbstractComplexVector mapComponents(ComplexMapping f);
171 }
172
173
Popular Tags