KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
12 import javolution.context.PoolContext;
13 import javolution.util.FastTable;
14 import javolution.xml.XMLFormat;
15 import javolution.xml.stream.XMLStreamException;
16 import org.jscience.mathematics.structures.Field;
17
18 /**
19  * <p> This class represents a dense vector.</p>
20  *
21  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
22  * @version 3.3, January 2, 2007
23  */

24 public final class DenseVector<F extends Field<F>> extends Vector<F> {
25
26     /**
27      * Holds the default XML representation for dense vectors. For example:
28      * [code]
29      * <DenseVector dimension="2">
30      * <Rational value="1/3" />
31      * <Rational value="3/5" />
32      * </DenseVector>[/code]
33      */

34     protected static final XMLFormat<DenseVector> XML = new XMLFormat<DenseVector>(
35             DenseVector.class) {
36
37         @Override JavaDoc
38         public DenseVector newInstance(Class JavaDoc<DenseVector> cls, InputElement xml)
39                 throws XMLStreamException {
40             return FACTORY.object();
41         }
42
43         @SuppressWarnings JavaDoc("unchecked")
44         @Override JavaDoc
45         public void read(InputElement xml, DenseVector V)
46                 throws XMLStreamException {
47             int dimension = xml.getAttribute("dimension", 0);
48             for (int i=0; i < dimension; i++) {
49                 V._elements.add(xml.getNext());
50             }
51             if (xml.hasNext())
52                 throw new XMLStreamException("Too many elements");
53         }
54
55         @Override JavaDoc
56         public void write(DenseVector V, OutputElement xml)
57                 throws XMLStreamException {
58             int dimension = V._elements.size();
59             xml.setAttribute("dimension", dimension);
60             for (int i = 0; i < dimension;) {
61                 xml.add(V._elements.get(i++));
62             }
63         }
64     };
65
66     /**
67      * Holds the elements.
68      */

69     final FastTable<F> _elements = new FastTable<F>();
70
71     /**
72      * Returns a dense vector holding the specified elements.
73      *
74      * @param elements the vector elements.
75      * @return the vector having the specified elements.
76      */

77     public static <F extends Field<F>> DenseVector<F> valueOf(F... elements) {
78         DenseVector<F> V = DenseVector.newInstance();
79         for (int i=0, n=elements.length; i < n;) {
80             V._elements.add(elements[i++]);
81         }
82         return V;
83     }
84
85     /**
86      * Returns a dense vector holding the elements from the specified
87      * collection.
88      *
89      * @param elements the collection of vector elements.
90      * @return the vector having the specified elements.
91      */

92     public static <F extends Field<F>> DenseVector<F> valueOf(List JavaDoc<F> elements) {
93         DenseVector<F> V = DenseVector.newInstance();
94         V._elements.addAll(elements);
95         return V;
96     }
97
98     /**
99      * Returns a dense vector equivalent to the specified vector.
100      *
101      * @param that the vector to convert.
102      * @return <code>that</code> or a dense vector holding the same elements
103      * as the specified vector.
104      */

105     public static <F extends Field<F>> DenseVector<F> valueOf(Vector<F> that) {
106         if (that instanceof DenseVector) return (DenseVector<F>) that;
107         DenseVector<F> V = DenseVector.newInstance();
108         for (int i=0, n=that.getDimension(); i < n;) {
109             V. _elements.add(that.get(i++));
110          }
111          return V;
112     }
113     
114     @Override JavaDoc
115     public int getDimension() {
116         return _elements.size();
117     }
118
119     @Override JavaDoc
120     public F get(int i) {
121         return _elements.get(i);
122     }
123
124     @Override JavaDoc
125     public DenseVector<F> opposite() {
126         DenseVector<F> V = DenseVector.newInstance();
127         for (int i = 0, n = _elements.size(); i < n;) {
128             V._elements.add(_elements.get(i++).opposite());
129         }
130         return V;
131     }
132
133     @Override JavaDoc
134     public DenseVector<F> plus(Vector<F> that) {
135         final int n = _elements.size();
136         if (that.getDimension() != n)
137             throw new DimensionException();
138         DenseVector<F> V = DenseVector.newInstance();
139         for (int i = 0; i < n; i++) {
140             V._elements.add(_elements.get(i).plus(that.get(i)));
141         }
142         return V;
143     }
144
145     @Override JavaDoc
146     public DenseVector<F> minus(Vector<F> that) { // Returns more specialized type.
147
return this.plus(that.opposite());
148     }
149     
150     @Override JavaDoc
151     public DenseVector<F> times(F k) {
152         DenseVector<F> V = DenseVector.newInstance();
153         for (int i = 0, n = _elements.size(); i < n;) {
154             V._elements.add(_elements.get(i++).times(k));
155         }
156         return V;
157     }
158
159     @Override JavaDoc
160     public F times(Vector<F> that) {
161         final int n = _elements.size();
162         if (that.getDimension() != n)
163             throw new DimensionException();
164         PoolContext.enter();
165         try { // Reduces memory allocation / garbage collection.
166
F sum = _elements.get(0).times(that.get(0));
167             for (int i = 1; i < n; i++) {
168                 sum = sum.plus(_elements.get(i).times(that.get(i)));
169             }
170             sum.move(ObjectSpace.OUTER); // Exports.
171
return sum;
172         } finally {
173             PoolContext.exit();
174         }
175     }
176
177     @Override JavaDoc
178     public boolean move(ObjectSpace os) {
179         if (super.move(os)) {
180             // The elements table itself is intrinsic (all final members are)
181
// and implicitely moves with this vector.
182
// But it may refer to locally/stack allocated elements which
183
// need to be moved along.
184
for (int i=0, n=_elements.size(); i < n; i++) {
185                 _elements.get(i).move(os);
186             }
187             return true;
188         }
189         return false;
190     }
191
192     ///////////////////////
193
// Factory creation. //
194
///////////////////////
195

196     @SuppressWarnings JavaDoc("unchecked")
197     static <F extends Field<F>> DenseVector<F> newInstance() {
198         return FACTORY.object();
199     }
200
201     private static Factory<DenseVector> FACTORY = new Factory<DenseVector>() {
202         @Override JavaDoc
203         protected DenseVector create() {
204             return new DenseVector();
205         }
206
207         @Override JavaDoc
208         protected void cleanup(DenseVector vector) {
209             vector._elements.reset();
210         }
211     };
212
213     private DenseVector() {
214     }
215
216     private static final long serialVersionUID = 1L;
217
218 }
Popular Tags