KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jscience > mathematics > vectors > ComplexVector


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2006 - JScience (http://jscience.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package org.jscience.mathematics.vectors;
10
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13
14 import javax.realtime.MemoryArea;
15
16 import javolution.xml.XMLFormat;
17 import javolution.xml.stream.XMLStreamException;
18
19 import org.jscience.mathematics.numbers.Complex;
20 import org.jscience.mathematics.structures.VectorSpaceNormed;
21
22 /**
23  * <p> This class represents an optimized {@link Vector vector} implementation
24  * for {@link Complex complex} numbers elements.</p>
25  *
26  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
27  * @version 3.3, January 2, 2007
28  */

29 public final class ComplexVector extends Vector<Complex> implements
30         VectorSpaceNormed<Vector<Complex>, Complex> {
31
32     /**
33      * Holds the default XML representation. For example:
34      * [code]
35      * <ComplexVector dimension="2">
36      * <Complex real="1.0" imaginary="-3.0" />
37      * <Complex real="0.0" imaginary="2.0" />
38      * </ComplexVector>[/code]
39      */

40     protected static final XMLFormat<ComplexVector> XML = new XMLFormat<ComplexVector>(
41             ComplexVector.class) {
42
43         @Override JavaDoc
44         public ComplexVector newInstance(Class JavaDoc<ComplexVector> cls,
45                 InputElement xml) throws XMLStreamException {
46             int dimension = xml.getAttribute("dimension", 0);
47             ComplexVector V = ComplexVector.newInstance(dimension);
48             V._dimension = dimension;
49             return V;
50         }
51
52         @SuppressWarnings JavaDoc("unchecked")
53         @Override JavaDoc
54         public void read(InputElement xml, ComplexVector V)
55                 throws XMLStreamException {
56             for (int i = 0, n = V._dimension; i < n;) {
57                 V._reals[i++] = ((Complex) xml.getNext()).doubleValue();
58             }
59             if (xml.hasNext())
60                 throw new XMLStreamException("Too many elements");
61         }
62
63         @Override JavaDoc
64         public void write(ComplexVector V, OutputElement xml)
65                 throws XMLStreamException {
66             xml.setAttribute("dimension", V._dimension);
67             for (int i = 0, n = V._dimension; i < n;) {
68                 xml.add(V.get(i++));
69             }
70         }
71     };
72
73     /**
74      * Holds the dimension.
75      */

76     int _dimension;
77
78     /**
79      * Holds the real values.
80      */

81     double[] _reals;
82
83     /**
84      * Holds the imaginary values.
85      */

86     double[] _imags;
87
88     /**
89      * Returns a new vector holding the specified complex numbers.
90      *
91      * @param elements the complex numbers elements.
92      * @return the vector having the specified complex numbers.
93      */

94     public static ComplexVector valueOf(Complex... elements) {
95         int n = elements.length;
96         ComplexVector V = ComplexVector.newInstance(n);
97         for (int i = 0; i < n; i++) {
98             Complex complex = elements[i];
99             V._reals[i] = complex.getReal();
100             V._imags[i] = complex.getImaginary();
101         }
102         return V;
103     }
104
105     /**
106      * Returns a new vector holding the elements from the specified
107      * collection.
108      *
109      * @param elements the collection of floating-points numbers.
110      * @return the vector having the specified elements.
111      */

112     public static ComplexVector valueOf(List JavaDoc<Complex> elements) {
113         int n = elements.size();
114         ComplexVector V = ComplexVector.newInstance(n);
115         Iterator JavaDoc<Complex> iterator = elements.iterator();
116         for (int i = 0; i < n; i++) {
117             Complex complex = iterator.next();
118             V._reals[i] = complex.getReal();
119             V._imags[i] = complex.getImaginary();
120         }
121         return V;
122     }
123
124     /**
125      * Returns a {@link ComplexVector} instance equivalent to the
126      * specified vector.
127      *
128      * @param that the vector to convert.
129      * @return <code>that</code> or new equivalent ComplexVector.
130      */

131     public static ComplexVector valueOf(Vector<Complex> that) {
132         if (that instanceof ComplexVector)
133             return (ComplexVector) that;
134         int n = that.getDimension();
135         ComplexVector V = ComplexVector.newInstance(n);
136         for (int i = 0; i < n; i++) {
137             Complex complex = that.get(i);
138             V._reals[i] = complex.getReal();
139             V._imags[i] = complex.getImaginary();
140         }
141         return V;
142     }
143
144     /**
145      * Returns the real value of a complex number from this vector (fast).
146      *
147      * @param i the complex number index.
148      * @return the real value of complex at <code>i</code>.
149      * @throws IndexOutOfBoundsException <code>(i < 0) || (i >= dimension())</code>
150      */

151     public double getReal(int i) {
152         if (i >= _dimension)
153             throw new ArrayIndexOutOfBoundsException JavaDoc();
154         return _reals[i];
155     }
156
157     /**
158      * Returns the imaginary value of a complex number from this vector (fast).
159      *
160      * @param i the complex number index.
161      * @return the real value of complex at <code>i</code>.
162      * @throws IndexOutOfBoundsException <code>(i < 0) || (i >= dimension())</code>
163      */

164     public double getImaginary(int i) {
165         if (i >= _dimension)
166             throw new ArrayIndexOutOfBoundsException JavaDoc();
167         return _imags[i];
168     }
169
170     /**
171      * Returns the Euclidian norm of this vector (square root of the
172      * dot product of this vector and itself).
173      *
174      * @return <code>sqrt(this ยท this)</code>.
175      */

176     public Complex norm() {
177         double normSquaredReal = 0;
178         double normSquaredImag = 0;
179         for (int i = _dimension; --i >= 0;) {
180             double real = _reals[i];
181             double imag = _imags[i];
182             normSquaredReal += real * real - imag * imag;
183             normSquaredImag += real * imag * 2.0;
184         }
185         return Complex.valueOf(normSquaredReal, normSquaredImag).sqrt();
186     }
187
188     @Override JavaDoc
189     public int getDimension() {
190         return _dimension;
191     }
192
193     @Override JavaDoc
194     public Complex get(int i) {
195         if (i >= _dimension)
196             throw new IndexOutOfBoundsException JavaDoc();
197         return Complex.valueOf(_reals[i], _imags[i]);
198     }
199
200     @Override JavaDoc
201     public ComplexVector opposite() {
202         ComplexVector V = ComplexVector.newInstance(_dimension);
203         for (int i = 0; i < _dimension; i++) {
204             V._reals[i] = -_reals[i];
205             V._imags[i] = -_imags[i];
206         }
207         return V;
208     }
209
210     @Override JavaDoc
211     public ComplexVector plus(Vector<Complex> that) {
212         ComplexVector T = ComplexVector.valueOf(that);
213         if (T._dimension != _dimension)
214             throw new DimensionException();
215         ComplexVector V = ComplexVector.newInstance(_dimension);
216         for (int i = 0; i < _dimension; i++) {
217             V._reals[i] = _reals[i] + T._reals[i];
218             V._imags[i] = _imags[i] + T._imags[i];
219         }
220         return V;
221     }
222
223     @Override JavaDoc
224     public ComplexVector minus(Vector<Complex> that) {
225         ComplexVector T = ComplexVector.valueOf(that);
226         if (T._dimension != _dimension)
227             throw new DimensionException();
228         ComplexVector V = ComplexVector.newInstance(_dimension);
229         for (int i = 0; i < _dimension; i++) {
230             V._reals[i] = _reals[i] - T._reals[i];
231             V._imags[i] = _imags[i] - T._imags[i];
232         }
233         return V;
234     }
235
236     @Override JavaDoc
237     public ComplexVector times(Complex k) {
238         ComplexVector V = ComplexVector.newInstance(_dimension);
239         for (int i = 0; i < _dimension; i++) {
240             double real = _reals[i];
241             double imag = _imags[i];
242             V._reals[i] = real * k.getReal() - imag * k.getImaginary();
243             V._imags[i] = real * k.getImaginary() + imag * k.getReal();
244         }
245         return V;
246     }
247
248     @Override JavaDoc
249     public Complex times(Vector<Complex> that) {
250         ComplexVector T = ComplexVector.valueOf(that);
251         if (T._dimension != _dimension)
252             throw new DimensionException();
253         double sumReal = _reals[0] * T._reals[0] - _imags[0] * T._imags[0];
254         double sumImag = _reals[0] * T._imags[0] + _imags[0] * T._reals[0];
255         for (int i = 1; i < _dimension; i++) {
256             sumReal += _reals[i] * T._reals[i] - _imags[i] * T._imags[i];
257             sumImag += _reals[i] * T._imags[i] + _imags[i] * T._reals[i];
258         }
259         return Complex.valueOf(sumReal, sumImag);
260     }
261
262     ///////////////////////////////
263
// Package Private Utilities //
264
///////////////////////////////
265

266     void set(int i, Complex c) {
267          _reals[i] = c.getReal();
268          _imags[i] = c.getImaginary();
269     }
270
271     ///////////////////////
272
// Factory creation. //
273
///////////////////////
274

275     static ComplexVector newInstance(final int dimension) {
276         final ComplexVector V = FACTORY.object();
277         V._dimension = dimension;
278         if ((V._reals == null) || (V._reals.length < dimension)) {
279             MemoryArea.getMemoryArea(V).executeInArea(new Runnable JavaDoc() {
280                 public void run() {
281                     V._reals = new double[dimension];
282                     V._imags = new double[dimension];
283                 }
284             });
285         }
286         return V;
287     }
288
289     private static Factory<ComplexVector> FACTORY = new Factory<ComplexVector>() {
290         protected ComplexVector create() {
291             return new ComplexVector();
292         }
293     };
294
295     private ComplexVector() {
296     }
297
298     private static final long serialVersionUID = 1L;
299 }
Popular Tags