KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jscience > mathematics > matrices > Vector


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2005 - 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.matrices;
10
11 import java.util.Collection JavaDoc;
12 import java.util.Iterator JavaDoc;
13
14 import javolution.lang.Text;
15 import javolution.lang.TextBuilder;
16 import javolution.xml.XmlElement;
17 import javolution.xml.XmlFormat;
18
19 /**
20  * <p> This class represents an immutable element of a vector space arranged as
21  * single column {@link Matrix}.</p>
22  *
23  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
24  * @version 2.0, June 6, 2004
25  * @see <a HREF="http://mathworld.wolfram.com/Vector.html">
26  * Vector -- from MathWorld</a>
27  */

28 public class Vector<O extends Operable<O>> extends Matrix<O> {
29
30     /**
31      * Overrides {@link Matrix#XML} representation for {@link Vector}
32      * and its sub-classes. This representation consists of the vector's
33      * elements as nested xml elements. For example: <pre>
34      * &lt;math:Vector&gt;
35      * &lt;math:Complex real="1.0" imaginary="0.0"/&gt;
36      * &lt;math:Complex real="0.0" imaginary="1.0"/&gt;
37      * &lt;/math:Vector&gt;</pre>
38      */

39     protected static final XmlFormat<Vector> XML = new XmlFormat<Vector>(
40             Vector.class) {
41         public void format(Vector vector, XmlElement xml) {
42             for (int i = 0; i < vector.m; i++) {
43                 xml.getContent().add(vector.o[i]);
44             }
45         }
46
47         public Vector parse(XmlElement xml) {
48             Collection JavaDoc<Operable> elements = xml.getContent();
49             return Vector.valueOf(elements);
50         }
51     };
52
53     /**
54      * Base constructor.
55      *
56      * @param size the maximum number of elements.
57      */

58     Vector(int size) {
59         super(size);
60     }
61
62     /**
63      * Creates a vector having the specified coordinates.
64      *
65      * @param coordinates the columns elements of this vector matrix.
66      */

67     public Vector(O... coordinates) {
68         super(coordinates.length);
69         this.n = 1;
70         this.m = coordinates.length;
71         for (int i = 0; i < m;) {
72             this.o[i] = coordinates[i++];
73         }
74     }
75
76     /**
77      * Returns the vector corresponding to the specified coordinates.
78      *
79      * @param coordinates the columns elements of this vector matrix.
80      * @return the vector having the specified coordinates elements.
81      */

82     public static <O extends Operable<O>> Vector<O> valueOf(O... coordinates) {
83         Vector<O> V = newInstance(coordinates.length);
84         for (int i = 0; i < V.m;) {
85             V.o[i] = coordinates[i++];
86         }
87         return V;
88     }
89
90     /**
91      * Returns a m-dimensional vector populated from the specified collection
92      * of {@link Operable} objects.
93      *
94      * @param coordinates the collection of {@link Operable} objects.
95      * @return the vector having the specified elements.
96      * @throws ClassCastException if any of the element is not {@link Operable}.
97      */

98     public static <O extends Operable<O>> Vector<O> valueOf(
99             Collection JavaDoc<O> coordinates) {
100         Vector<O> V = newInstance(coordinates.size());
101         Iterator JavaDoc<O> iterator = coordinates.iterator();
102         for (int i = 0; i < V.m;) {
103             V.o[i++] = iterator.next();
104         }
105         return V;
106     }
107
108     /**
109      * Returns the number of coordinates held by this vector.
110      *
111      * @return this vector dimension (number of rows).
112      */

113     public final int getDimension() {
114         return m;
115     }
116
117     /**
118      * Returns a single coordinate from this vector.
119      *
120      * @param i the coordinate index (range [0..n[).
121      * @return the coordinates at <code>i</code>.
122      * @throws IndexOutOfBoundsException <code>(i < 0) || (i >= size())</code>
123      */

124     public final O get(int i) {
125         if (i >= m)
126             throw new IndexOutOfBoundsException JavaDoc("i: " + i + " (Vector[" + m
127                     + "])");
128         return o[i];
129     }
130
131     /**
132      * Returns the dot product of this vector with the one specified.
133      *
134      * @param that the vector multiplier.
135      * @return <code>this ยท that</code>
136      * @throws MatrixException if <code>this.size() != that.size()</code>
137      * @see <a HREF="http://mathworld.wolfram.com/DotProduct.html">
138      * Dot Product -- from MathWorld</a>
139      */

140     public final O dot(Vector<O> that) {
141         if (this.m != that.m)
142             throw new MatrixException(this.m + " different from " + that.m);
143         O sum = this.o[0].times(that.o[0]);
144         for (int i = 1; i < m; i++) {
145             sum = sum.plus(this.o[i].times(that.o[i]));
146         }
147         return sum;
148     }
149
150     /**
151      * Returns the cross product of two 3-dimensional vectors.
152      *
153      * @param that the vector multiplier.
154      * @return <code>this x that</code>
155      * @throws MatrixException if
156      * <code>(this.size() != 3) && (that.size() != 3)</code>
157      * @see <a HREF="http://mathworld.wolfram.com/CrossProduct.html">
158      * Cross Product -- from MathWorld</a>
159      */

160     public final Vector<O> cross(Vector<O> that) {
161         if ((this.m != 3) || (that.m != 3))
162             throw new MatrixException(
163                     "The cross product of two vectors requires "
164                             + "3-dimensional vectors");
165         Vector<O> V = newInstance(3);
166         V.o[0] = this.o[1].times(that.o[2]).plus(
167                 this.o[2].times(that.o[1]).opposite());
168         V.o[1] = this.o[2].times(that.o[0]).plus(
169                 this.o[0].times(that.o[2]).opposite());
170         V.o[2] = this.o[0].times(that.o[1]).plus(
171                 this.o[1].times(that.o[0]).opposite());
172         return V;
173     }
174
175     // Overrides.
176
public Vector<O> opposite() {
177         return (Vector<O>) super.opposite();
178     }
179
180     // Overrides.
181
public Vector<O> plus(Vector<O> that) {
182         return (Vector<O>) super.plus(that);
183     }
184
185     // Overrides.
186
public Vector<O> minus(Vector<O> that) {
187         return (Vector<O>) super.minus(that);
188     }
189
190     // Overrides.
191
public Vector<O> times(O k) {
192         return (Vector<O>) super.times(k);
193     }
194
195     /**
196      * Returns the text representation of this vector.
197      *
198      * @return the text representation of this vector.
199      */

200     public Text toText() {
201         TextBuilder cb = TextBuilder.newInstance();
202         cb.append("{");
203         for (int i = 0; i < m; i++) {
204             if (i != 0) {
205                 cb.append(", ");
206             }
207             cb.append(o[i]);
208         }
209         cb.append("}");
210         return cb.toText();
211     }
212
213     /**
214      * Returns a m-dimensional vector (non-initialized).
215      *
216      * @param m the dimension of this vector.
217      * @return a m-by-1 matrix non-initialized.
218      */

219     static <O extends Operable<O>> Vector<O> newInstance(int n) {
220         Vector V;
221         if (n <= 1 << 3) {
222             V = FACTORY_3.object();
223         } else if (n <= 1 << 6) {
224             V = FACTORY_6.object();
225         } else if (n <= 1 << 9) {
226             V = FACTORY_9.object();
227         } else if (n <= 1 << 12) {
228             V = FACTORY_12.object();
229         } else if (n <= 1 << 15) {
230             V = FACTORY_15.object();
231         } else if (n <= 1 << 18) {
232             V = FACTORY_18.object();
233         } else if (n <= 1 << 21) {
234             V = FACTORY_21.object();
235         } else if (n <= 1 << 24) {
236             V = FACTORY_24.object();
237         } else if (n <= 1 << 27) {
238             V = FACTORY_27.object();
239         } else if (n <= 1 << 30) {
240             V = FACTORY_30.object();
241         } else {
242             throw new UnsupportedOperationException JavaDoc("Vector too large");
243         }
244         V.m = n;
245         V.n = 1;
246         return V;
247     }
248
249     // TBD: Use recursive structures (see Matrix).
250
//
251

252     private static final Factory<Vector> FACTORY_3 = new Factory<Vector>() {
253         protected Vector create() {
254             return new Vector(1 << 3);
255         }
256     };
257
258     private static final Factory<Vector> FACTORY_6 = new Factory<Vector>() {
259         protected Vector create() {
260             return new Vector(1 << 6);
261         }
262     };
263
264     private static final Factory<Vector> FACTORY_9 = new Factory<Vector>() {
265         protected Vector create() {
266             return new Vector(1 << 9);
267         }
268     };
269
270     private static final Factory<Vector> FACTORY_12 = new Factory<Vector>() {
271         protected Vector create() {
272             return new Vector(1 << 12);
273         }
274     };
275
276     private static final Factory<Vector> FACTORY_15 = new Factory<Vector>() {
277         protected Vector create() {
278             return new Vector(1 << 15);
279         }
280     };
281
282     private static final Factory<Vector> FACTORY_18 = new Factory<Vector>() {
283         protected Vector create() {
284             return new Vector(1 << 18);
285         }
286     };
287
288     private static final Factory<Vector> FACTORY_21 = new Factory<Vector>() {
289         protected Vector create() {
290             return new Vector(1 << 21);
291         }
292     };
293
294     private static final Factory<Vector> FACTORY_24 = new Factory<Vector>() {
295         protected Vector create() {
296             return new Vector(1 << 24);
297         }
298     };
299
300     private static final Factory<Vector> FACTORY_27 = new Factory<Vector>() {
301         protected Vector create() {
302             return new Vector(1 << 27);
303         }
304     };
305
306     private static final Factory<Vector> FACTORY_30 = new Factory<Vector>() {
307         protected Vector create() {
308             return new Vector(1 << 30);
309         }
310     };
311
312     private static final long serialVersionUID = 1L;
313 }
Popular Tags