1 9 package org.jscience.mathematics.vectors; 10 11 import java.util.List ; 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 24 public final class DenseVector<F extends Field<F>> extends Vector<F> { 25 26 34 protected static final XMLFormat<DenseVector> XML = new XMLFormat<DenseVector>( 35 DenseVector.class) { 36 37 @Override 38 public DenseVector newInstance(Class <DenseVector> cls, InputElement xml) 39 throws XMLStreamException { 40 return FACTORY.object(); 41 } 42 43 @SuppressWarnings ("unchecked") 44 @Override 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 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 69 final FastTable<F> _elements = new FastTable<F>(); 70 71 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 92 public static <F extends Field<F>> DenseVector<F> valueOf(List <F> elements) { 93 DenseVector<F> V = DenseVector.newInstance(); 94 V._elements.addAll(elements); 95 return V; 96 } 97 98 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 115 public int getDimension() { 116 return _elements.size(); 117 } 118 119 @Override 120 public F get(int i) { 121 return _elements.get(i); 122 } 123 124 @Override 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 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 146 public DenseVector<F> minus(Vector<F> that) { return this.plus(that.opposite()); 148 } 149 150 @Override 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 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 { 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); return sum; 172 } finally { 173 PoolContext.exit(); 174 } 175 } 176 177 @Override 178 public boolean move(ObjectSpace os) { 179 if (super.move(os)) { 180 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 196 @SuppressWarnings ("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 203 protected DenseVector create() { 204 return new DenseVector(); 205 } 206 207 @Override 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 |