1 9 package org.jscience.mathematics.vectors; 10 11 import java.util.Iterator ; 12 import java.util.List ; 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 30 public final class Float64Vector extends Vector<Float64> implements 31 VectorSpaceNormed<Vector<Float64>, Float64> { 32 33 41 protected static final XMLFormat<Float64Vector> XML = new XMLFormat<Float64Vector>( 42 Float64Vector.class) { 43 44 @Override 45 public Float64Vector newInstance(Class <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 ("unchecked") 54 @Override 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 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 77 int _dimension; 78 79 82 double[] _values; 83 84 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 104 public static Float64Vector valueOf(List <Float64> elements) { 105 int n = elements.size(); 106 Float64Vector V = Float64Vector.newInstance(n); 107 Iterator <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 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 139 public double getValue(int i) { 140 if (i >= _dimension) 141 throw new ArrayIndexOutOfBoundsException (); 142 return _values[i]; 143 } 144 145 151 public Float64 norm() { 152 return Float64.valueOf(normValue()); 153 } 154 155 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 170 public int getDimension() { 171 return _dimension; 172 } 173 174 @Override 175 public Float64 get(int i) { 176 if (i >= _dimension) 177 throw new IndexOutOfBoundsException (); 178 return Float64.valueOf(_values[i]); 179 } 180 181 @Override 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 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 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 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 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 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 253 void set(int i, Float64 f) { 254 _values[i] = f.doubleValue(); 255 } 256 257 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 () { 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 |