KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.MathLib;
17 import javolution.xml.XMLFormat;
18 import javolution.xml.stream.XMLStreamException;
19
20 import org.jscience.mathematics.numbers.Float64;
21 import org.jscience.mathematics.structures.VectorSpaceNormed;
22
23 /**
24  * <p> This class represents an optimized {@link Vector vector} implementation
25  * for 64 bits floating point elements.</p>
26  *
27  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
28  * @version 3.3, January 2, 2007
29  */

30 public final class Float64Vector extends Vector<Float64> implements
31         VectorSpaceNormed<Vector<Float64>, Float64> {
32
33     /**
34      * Holds the default XML representation. For example:
35      * [code]
36      * <Float64Vector dimension="2">
37      * <Float64 value="1.0" />
38      * <Float64 value="0.0" />
39      * </Float64Vector>[/code]
40      */

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

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

82     double[] _values;
83
84     /**
85      * Returns a new vector holding the specified <code>double</code> values.
86      *
87      * @param values the vector values.
88      * @return the vector having the specified values.
89      */

90     public static Float64Vector valueOf(double... values) {
91         int n = values.length;
92         Float64Vector V = Float64Vector.newInstance(n);
93         System.arraycopy(values, 0, V._values, 0, n);
94         return V;
95     }
96
97     /**
98      * Returns a new vector holding the elements from the specified
99      * collection.
100      *
101      * @param elements the collection of floating-points numbers.
102      * @return the vector having the specified elements.
103      */

104     public static Float64Vector valueOf(List JavaDoc<Float64> elements) {
105         int n = elements.size();
106         Float64Vector V = Float64Vector.newInstance(n);
107         Iterator JavaDoc<Float64> iterator = elements.iterator();
108         for (int i = 0; i < n; i++) {
109             V._values[i] = iterator.next().doubleValue();
110         }
111         return V;
112     }
113
114     /**
115      * Returns a {@link Float64Vector} instance equivalent to the
116      * specified vector.
117      *
118      * @param that the vector to convert.
119      * @return <code>that</code> or new equivalent Float64Vector.
120      */

121     public static Float64Vector valueOf(Vector<Float64> that) {
122         if (that instanceof Float64Vector)
123             return (Float64Vector) that;
124         int n = that.getDimension();
125         Float64Vector V = Float64Vector.newInstance(n);
126         for (int i = 0; i < n; i++) {
127             V._values[i] = that.get(i).doubleValue();
128         }
129         return V;
130     }
131
132     /**
133      * Returns the value of a floating point number from this vector (fast).
134      *
135      * @param i the floating point number index.
136      * @return the value of the floating point number at <code>i</code>.
137      * @throws IndexOutOfBoundsException <code>(i < 0) || (i >= dimension())</code>
138      */

139     public double getValue(int i) {
140         if (i >= _dimension)
141             throw new ArrayIndexOutOfBoundsException JavaDoc();
142         return _values[i];
143     }
144
145     /**
146      * Returns the Euclidian norm of this vector (square root of the
147      * dot product of this vector and itself).
148      *
149      * @return <code>sqrt(this ยท this)</code>.
150      */

151     public Float64 norm() {
152         return Float64.valueOf(normValue());
153     }
154
155     /**
156      * Returns the {@link #norm()} value of this vector.
157      *
158      * @return <code>this.norm().doubleValue()</code>.
159      */

160     public double normValue() {
161         double normSquared = 0;
162         for (int i = _dimension; --i >= 0;) {
163             double values = _values[i];
164             normSquared += values * values;
165         }
166         return MathLib.sqrt(normSquared);
167     }
168
169     @Override JavaDoc
170     public int getDimension() {
171         return _dimension;
172     }
173
174     @Override JavaDoc
175     public Float64 get(int i) {
176         if (i >= _dimension)
177             throw new IndexOutOfBoundsException JavaDoc();
178         return Float64.valueOf(_values[i]);
179     }
180
181     @Override JavaDoc
182     public Float64Vector opposite() {
183         Float64Vector V = Float64Vector.newInstance(_dimension);
184         for (int i = 0; i < _dimension; i++) {
185             V._values[i] = - _values[i];
186         }
187         return V;
188     }
189
190     @Override JavaDoc
191     public Float64Vector plus(Vector<Float64> that) {
192         Float64Vector T = Float64Vector.valueOf(that);
193         if (T._dimension != _dimension) throw new DimensionException();
194         Float64Vector V = Float64Vector.newInstance(_dimension);
195         for (int i = 0; i < _dimension; i++) {
196             V._values[i] = _values[i] + T._values[i];
197         }
198         return V;
199     }
200
201     @Override JavaDoc
202     public Float64Vector minus(Vector<Float64> that) {
203         Float64Vector T = Float64Vector.valueOf(that);
204         if (T._dimension != _dimension) throw new DimensionException();
205         Float64Vector V = Float64Vector.newInstance(_dimension);
206         for (int i = 0; i < _dimension; i++) {
207             V._values[i] = _values[i] - T._values[i];
208         }
209         return V;
210     }
211
212     @Override JavaDoc
213     public Float64Vector times(Float64 k) {
214         Float64Vector V = Float64Vector.newInstance(_dimension);
215         double d = k.doubleValue();
216         for (int i = 0; i < _dimension; i++) {
217             V._values[i] = _values[i] * d;
218         }
219         return V;
220     }
221     
222     /**
223      * Equivalent to <code>this.times(Float64.valueOf(k))</code>
224      *
225      * @param k the coefficient.
226      * @return <code>this * k</code>
227      */

228     public Float64Vector times(double k) {
229         Float64Vector V = Float64Vector.newInstance(_dimension);
230         for (int i = 0; i < _dimension; i++) {
231             V._values[i] = _values[i] * k;
232         }
233         return V;
234     }
235     
236     @Override JavaDoc
237     public Float64 times(Vector<Float64> that) {
238         Float64Vector T = Float64Vector.valueOf(that);
239         if (T._dimension != _dimension)
240             throw new DimensionException();
241         double[] T_values = T._values;
242         double sum = _values[0] * T_values[0];
243         for (int i = 1; i < _dimension; i++) {
244             sum += _values[i] * T_values[i];
245         }
246         return Float64.valueOf(sum);
247     }
248
249     ///////////////////////////////
250
// Package Private Utilities //
251
///////////////////////////////
252

253     void set(int i, Float64 f) {
254          _values[i] = f.doubleValue();
255     }
256
257     ///////////////////////
258
// Factory creation. //
259
///////////////////////
260

261     static Float64Vector newInstance(final int dimension) {
262         final Float64Vector V = FACTORY.object();
263         V._dimension = dimension;
264         if ((V._values == null) || (V._values.length < dimension)) {
265             MemoryArea.getMemoryArea(V).executeInArea(new Runnable JavaDoc() {
266                 public void run() {
267                     V._values = new double[dimension];
268                 }
269             });
270         }
271         return V;
272     }
273
274     private static Factory<Float64Vector> FACTORY = new Factory<Float64Vector>() {
275         protected Float64Vector create() {
276             return new Float64Vector();
277         }
278     };
279
280     private Float64Vector() {
281     }
282
283     private static final long serialVersionUID = 1L;
284 }
Popular Tags